Font Example

In this section I present a simple nano-X TrueType font application.

Example Using Fonts

Copy the source shown below into a file named "fonts.c". Compile the application with the following command.

$ gcc fonts.c -I/usr/include/microwin \
> -o fonts -lnano-X

Example 3-1. fonts.c

 
#include <stdio.h>
#define MWINCLUDECOLORS
#include "microwin/nano-X.h"

GR_WINDOW_ID  wid;
GR_GC_ID      gc;
GR_FONT_ID    font_a, font_b, font_c, font_d;

void event_handler (GR_EVENT *event);

int main (void)
{
    if (GrOpen() < 0)
    {
        fprintf (stderr, "GrOpen failed");
        exit (1);
    }

    gc = GrNewGC();
    GrSetGCUseBackground (gc, GR_FALSE);
    GrSetGCForeground (gc, RED);
 
    wid = GrNewWindowEx (GR_WM_PROPS_APPFRAME |
                         GR_WM_PROPS_CAPTION  |
                         GR_WM_PROPS_CLOSEBOX,
                         "Font Test Window",
                         GR_ROOT_WINDOW_ID, 50, 50, 
                         200, 130, WHITE);

    GrSelectEvents (wid, GR_EVENT_MASK_EXPOSURE | 
                         GR_EVENT_MASK_CLOSE_REQ);

    font_a = GrCreateFont ("arial", 12, NULL);
    font_b = GrCreateFont ("comic", 16, NULL);
    font_c = GrCreateFont ("comic", 24, NULL);
    font_d = GrCreateFont ("arial", 36, NULL);

    GrMapWindow (wid);
    GrMainLoop (event_handler);
    return 0;
}

void event_handler (GR_EVENT *event)
{
    switch (event->type)
    {
    case GR_EVENT_TYPE_EXPOSURE:
        GrSetGCFont (gc, font_a);
        GrText (wid, gc, 20, 20, "Arial 12",  -1, GR_TFASCII);
        GrSetGCFont (gc, font_b);
        GrText (wid, gc, 20, 40, "Comic 16",  -1, GR_TFASCII);
        GrSetGCFont (gc, font_c);
        GrText (wid, gc, 20, 70, "Comic 24",  -1, GR_TFASCII);
        GrSetGCFont (gc, font_d);
        GrText (wid, gc, 20, 110, "Arial 36",  -1, GR_TFASCII);
        break;

    case GR_EVENT_TYPE_CLOSE_REQ:
        GrClose();
        exit (0);
    }
}

Run the example application with the following command. You will see a window appear as shown below.

$ nano-X& sleep 1; nanowm& sleep 1; ./fonts& 

Figure 3-1. TrueType Font Example

<< Image Object Missing >>