You can now create an X Window System program which uses fonts(see later to retrieve it):
#include <X11/Xlib.h> /* X library */
#include <stdio.h> /* std input-output */
#include <X11/Xutil.h> /* X utilities */
main (argc, argv)
int argc;
char **argv;
{
Display *myDisplay; /* pointer to the display */
int myScreen; /* screen we are using */
int myDepth; /* colour plane depth */
XSetWindowAttributes myWindowAttributes; /* structure of window attibutes */
unsigned long myWindowMask; /* mask for window attributes */
Window myWindow; /* window structure */
GC myGC;
unsigned long myWhitePixel;
unsigned long myBlackPixel;
XGCValues myGCValues;
unsigned long myValueMask;
XSizeHints theSizeHints; /* window hints for window manger */
int x = 200; /* x top left corner of window */
int y = 150; /* y top left corner of window */
unsigned int width = 850; /* width of the window */
unsigned int height = 700; /* height of the window */
int border_width = 20; /* border width of the window */
XFontStruct *myfont;
char *fontname;
char character_string[200];
char *alphabet0="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *alphabet1="abcdefghijklmnopqrstuvwxyz";
char *alphabet2="0123456789";
int base_y;
myDisplay = XOpenDisplay ("");
if (myDisplay == NULL)
{
fprintf (stderr, "ERROR: Could not open a connection to X on display %s\n",
XDisplayName (NULL));
exit (0);
}
myScreen = DefaultScreen (myDisplay);
myDepth = DefaultDepth (myDisplay, myScreen);
myWhitePixel = WhitePixel (myDisplay, myScreen);
myBlackPixel = BlackPixel (myDisplay, myScreen);
myWindowAttributes.border_pixel = BlackPixel (myDisplay, myScreen);
myWindowAttributes.background_pixel = WhitePixel (myDisplay, myScreen);
myWindowAttributes.override_redirect = True;
myWindowMask = CWBackPixel | CWBorderPixel | CWOverrideRedirect;
myWindow = XCreateWindow (myDisplay, RootWindow (myDisplay, myScreen),
x, y, width, height, border_width,
myDepth, InputOutput, CopyFromParent,
myWindowMask, &myWindowAttributes);
theSizeHints.flags = PPosition | PSize; /* set mask for the hints */
theSizeHints.x = x; /* x position */
theSizeHints.y = y; /* y position */
theSizeHints.width = width; /* width of the window */
theSizeHints.height = height; /* height of the window */
XSetWMNormalHints (myDisplay, myWindow, &theSizeHints);
myGC = XCreateGC (myDisplay, myWindow, (unsigned long) 0, &myGCValues);
if (myGC == 0) /* error... cannot create gc */
{
XDestroyWindow(myDisplay, myScreen);
exit (0);
}
else /* set forground and background defaults */
{
XSetForeground (myDisplay, myGC, myBlackPixel);
XSetBackground (myDisplay, myGC, myWhitePixel);
}
XMapWindow (myDisplay, myWindow);
fontname="8x13";
sprintf(character_string,"This is font %s",fontname);
myfont = XLoadQueryFont (myDisplay, fontname);
if (myfont == (XFontStruct *)NULL)
{
fprintf(stderr,"Cannot get font:%s.\n",fontname);
exit();
}
XSetFont (myDisplay, myGC, myfont->fid);
base_y=50;
XDrawImageString (myDisplay, myWindow, myGC, 50, base_y,
character_string, strlen(character_string));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+25,
alphabet0, strlen(alphabet0));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+50,
alphabet1, strlen(alphabet1));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+75,
alphabet2, strlen(alphabet2));
fontname="-b&h-lucida-medium-r-normal-*-*-180-*-*-*-*-*-*";
sprintf(character_string,"This is font %s",fontname);
myfont = XLoadQueryFont (myDisplay, fontname);
if (myfont == (XFontStruct *)NULL)
{
fprintf(stderr,"Cannot get font:%s.\n",fontname);
exit();
}
XSetFont (myDisplay, myGC, myfont->fid);
base_y=150;
XDrawImageString (myDisplay, myWindow, myGC, 50, base_y,
character_string, strlen(character_string));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+25,
alphabet0, strlen(alphabet0));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+50,
alphabet1, strlen(alphabet1));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+75,
alphabet2, strlen(alphabet2));
fontname="-*-*-*-*-*-*-*-*-*-*-*-*-*-*";
sprintf(character_string,"This is font %s",fontname);
myfont = XLoadQueryFont (myDisplay, fontname);
if (myfont == (XFontStruct *)NULL)
{
fprintf(stderr,"Cannot get font:%s.\n",fontname);
exit();
}
XSetFont (myDisplay, myGC, myfont->fid);
base_y=250;
XDrawImageString (myDisplay, myWindow, myGC, 50, base_y,
character_string, strlen(character_string));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+25,
alphabet0, strlen(alphabet0));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+50,
alphabet1, strlen(alphabet1));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+75,
alphabet2, strlen(alphabet2));
fontname="-*-times-*-*-*-*-*-240-*-*-*-*-*-*";
sprintf(character_string,"This is font %s",fontname);
myfont = XLoadQueryFont (myDisplay, fontname);
if (myfont == (XFontStruct *)NULL)
{
fprintf(stderr,"Cannot get font:%s.\n",fontname);
exit();
}
XSetFont (myDisplay, myGC, myfont->fid);
base_y=350;
XDrawImageString (myDisplay, myWindow, myGC, 50, base_y,
character_string, strlen(character_string));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+25,
alphabet0, strlen(alphabet0));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+50,
alphabet1, strlen(alphabet1));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+75,
alphabet2, strlen(alphabet2));
fontname="-*-symbol-*-*-*-*-24-400-*-*-*-*-*-*";
sprintf(character_string,"This is font %s",fontname);
base_y=475;
XDrawImageString (myDisplay, myWindow, myGC, 50, base_y,
character_string, strlen(character_string));
myfont = XLoadQueryFont (myDisplay, fontname);
if (myfont == (XFontStruct *)NULL)
{
fprintf(stderr,"Cannot get font:%s.\n",fontname);
exit();
}
XSetFont (myDisplay, myGC, myfont->fid);
base_y=500;
XDrawImageString (myDisplay, myWindow, myGC, 50, base_y,
character_string, strlen(character_string));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+25,
alphabet0, strlen(alphabet0));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+50,
alphabet1, strlen(alphabet1));
XDrawImageString (myDisplay, myWindow, myGC, 80, base_y+75,
alphabet2, strlen(alphabet2));
XFlush (myDisplay);
sleep(40);
XFreeFont (myDisplay, myfont);
XDestroyWindow (myDisplay, myWindow);
XCloseDisplay (myDisplay);
exit (0);
}
LOCAL_LIBRARIES = $(XLIB) -lm
FILE = simplewindow
SRCS = $(FILE).c
OBJS = $(FILE).o
ComplexProgramTarget($(FILE))
After retrieving and saving the code plus Imakefile, you
should be able to type:
xmkmf -a ; make FILE=font
This will produce the executable called font.
It is possible to execute a (different) font program and display it on your screen. To run it, your system must be running X and the server control is set to xhost www.cs.curtin.edu.au/. Also it will not work if you are using a proxy server or you are running your browser on a machine different from the one you are displaying it on. Many example programs are available