[Previous] [Contents] [Index] [Next]

PfDynamicLoad()

Dynamically load a font

Synopsis:

long PfDynamicLoad( char const *pkcFontFile,
                    char * pszDescription );

Library:

ph

Description:

The function asks the font manager to load a font file dynamically.

The pkcFontFile argument is the full pathname of the font file to load. If pkcFontFile is NULL, this function sets errno to EFAULT and returns -1L.

The pszDescription argument is a pointer to an array of MAX_DESC_LENGTH + 1 bytes. This array, on successful completion of the load request, contains the description name of the loaded font, e.g. Comic Sans MS. If a scalable "collection" is encountered, only the first description found is returned. You can use this description to verify that the font contains what you think it does. If pszDescription is NULL, the parameter is ignored.

Returns:

A nonnegative dynamic font ID that can be used to refer to the file when calling PfDynamicUnload(), or -1L if an error occurred (errno is set).

Errors:

EBUSY
There are too many dynamic fonts already loaded.
EBADF
Trouble opening or closing file descriptors.
EFAULT
The pkcFontFile argument is NULL.
ENOTSUP
An attempt was made to load an unsupported font type.
ENOMEM
Not enough memory available to proceed with the load.
EINVAL
An invalid condition was encountered, possibly due to an invalid font file.
EEXIST
The file that was attempted to be loaded was either already dynamically loaded, or statically installed.

Examples:

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>

#include <Ph.h>
#include <Pt.h>

int fnLoad(PtWidget_t * pwgt, void * pvData,
           PtCallbackInfo_t * ptsInfo);
int fnUnload(PtWidget_t * pwgt, void * pvData,
             PtCallbackInfo_t * ptsInfo);
int fnChangeDisplay(void);

typedef unsigned int BOOL;

#define FALSE 0
#define TRUE !FALSE

PtWidget_t * pwndMain = NULL, * pbtnLoad = NULL, 
           * pbtnUnload = NULL, * ptxtDisplay = NULL;
long lID = 0L;
BOOL bLoaded = FALSE;
char * pcFace = NULL;

int main (int argc, char *argv[])
{   PtArg_t args[4];
    PhPoint_t win_size, pntPOS, pntDIM;
    short nArgs = 0;
    char szHelvetica14[MAX_FONT_TAG];

    PtInit (NULL);

//  set base pwndMain parms
    win_size.x = 450;
    win_size.y = 450;

    PtSetArg(&args[0],Pt_ARG_DIM, &win_size, 0);
    // window title = name  of program
    PtSetArg(&args[1],Pt_ARG_WINDOW_TITLE, 
             (long)"Load On The Fly", 0);

    pwndMain = PtCreateWidget (PtWindow, Pt_NO_PARENT, 2, args);

    nArgs = 0;
    pntDIM.x = 100;
    pntDIM.y = 20;
    PtSetArg(&args[0], Pt_ARG_DIM, &pntDIM, 0L);
    nArgs++;
    pntPOS.x = 100;
    pntPOS.y = 10;
    PtSetArg(&args[1], Pt_ARG_POS, &pntPOS, 0L);
    nArgs++;
    PtSetArg(&args[2], Pt_ARG_TEXT_STRING, (long)"LOAD", 0L);
    nArgs++;
    PtSetArg(&args[3], Pt_ARG_TEXT_FONT,
             (long)PfGenerateFontName("Helvetica", 0, 14, 
                                      szHelvetica14),
             0L);
    nArgs++;
    pbtnLoad = PtCreateWidget(PtButton, pwndMain, nArgs, args);
    PtAddCallback(pbtnLoad, Pt_CB_ACTIVATE, fnLoad, argv[1]);
    PtRealizeWidget(pbtnLoad);

    nArgs = 0;
    pntDIM.x = 40;
    pntDIM.y = 20;
    PtSetArg(&args[0], Pt_ARG_DIM, &pntDIM, 0L);
    nArgs++;
    pntPOS.x = 160;
    pntPOS.y = 10;
    PtSetArg(&args[1], Pt_ARG_POS, &pntPOS, 0L);
    nArgs++;
    PtSetArg(&args[2], Pt_ARG_TEXT_STRING, (long)"UNLOAD", 0L);
    nArgs++;
    PtSetArg(&args[3], Pt_ARG_TEXT_FONT, (long)szHelvetica14,
             0L);
    nArgs++;
    pbtnUnload = PtCreateWidget(PtButton, pwndMain, nArgs,
                                args);
    PtAddCallback(pbtnUnload, Pt_CB_ACTIVATE, fnUnload, NULL);
    PtRealizeWidget(pbtnUnload);

    nArgs = 0;
    pntDIM.x = 40;
    pntDIM.y = 20;
    PtSetArg(&args[0], Pt_ARG_DIM, &pntDIM, 0L);
    nArgs++;
    pntPOS.x = 100;
    pntPOS.y = 50;
    PtSetArg(&args[1], Pt_ARG_POS, &pntPOS, 0L);
    nArgs++;
    PtSetArg(&args[2], Pt_ARG_TEXT_STRING, (long)"Hello", 0L);
    nArgs++;
    PtSetArg(&args[3], Pt_ARG_TEXT_FONT, (long)szHelvetica14,
             0L);
    nArgs++;
    ptxtDisplay = PtCreateWidget(PtText, pwndMain, nArgs, args);

    (void) PtRealizeWidget(pwndMain);

    pcFace = argv[2];

    PtMainLoop ();

    return(EXIT_SUCCESS);
}

int fnLoad(PtWidget_t * pwgt, void * pvData,
           PtCallbackInfo_t * ptsInfo)
{
   if((lID = PfDynamicLoad((char const *)pvData, NULL)) == -1)
   {  perror("fnLoad:  ");
      return(Pt_CONTINUE);
   }
   else 
   {  bLoaded = TRUE;
      fnChangeDisplay();
      printf("Return code is %ld\n", lID);
   }

   return(Pt_CONTINUE);
}

int fnUnload(PtWidget_t * pwgt, void * pvData,
             PtCallbackInfo_t * ptsInfo)
{
   if(bLoaded)
     if(PfDynamicUnload(lID) == -1)
       perror("fnUnload:  ");
     else
     {  bLoaded = FALSE;
        fnChangeDisplay();
     }

   return(Pt_CONTINUE);
}

int fnChangeDisplay(void)
{  PtArg_t tsArg;
   char szHelvetica12[MAX_FONT_TAG];

   pcFace = PtFontSelection(pwndMain, NULL, "Select Font",
               PfGenerateFontName("Helvetica", 0, 12,
                                  szHelvetica12),
               -1L, PHFONT_ALL_FONTS, "AaBb");

   PtSetArg(&tsArg, Pt_ARG_TEXT_FONT, (long)pcFace, 0L);
   PtSetResources(ptxtDisplay, 1, &tsArg);

   return(Pt_CONTINUE);
}

Classification:

Photon

Safety:
Interrupt handler No
Signal handler No
Thread No

See also:

PfDynamicUnload()

Fonts chapter of the Photon Programmer's Guide


[Previous] [Contents] [Index] [Next]