PfDynamicLoad(), PfDynamicLoadCx()

Dynamically load a font

Synopsis:

#include <photon/Pf.h>
long PfDynamicLoad( char const *pkcFontFile,
                    char *pszDescription );

#include <photon/Pf.h>
long PfDynamicLoadCx( struct _Pf_ctrl * context,
                      char const * pkcFontFile,
                      FontDescription pszDescription );

Arguments:

context
(PfDynamicLoadCx() only) A pointer to the font context to use, returned by PfAttachCx() or PfAttachDllCx().
pkcFontFile
The full pathname of the font file that you want to load.
pszDescription
A pointer to an array of MAX_DESC_LENGTH + 1 bytes, where the function can store the description of the font, or NULL if you don't want the description.

Library:

PfDynamicLoad()
ph
PfDynamicLoadCx()
font

Description:

These functions request that the font manager dynamically install a font file. The font can be stored in any location. For example, a web server might need to dynamically load fonts embedded in HTML stored in its own temporary font directory.

If pkcFontFile is NULL, these functions set errno to EFAULT and return -1L.

If these functions successfully load the font, they fill the pszDescription array with the descriptive 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:

PfDynamicLoadCx():

#include <font_api.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>

int main(int argc, char const * argv[])
{  fontdll_t dll;

   fprintf(stderr, "POINT : PfDynamicLoadCx.\n");

   if((dll = PfAttachLocalDll(NULL, NULL)) != NULL)
   {  struct _Pf_ctrl * pf;

      if((pf = PfAttachDllCx(dll, 0)) != NULL)
      {  FontDescription desc;
         long id, id2;
         char fullpath[_POSIX_PATH_MAX];

         /* Full path to font file. */
         snprintf(fullpath, sizeof(fullpath), "%s", argv[1]);

         if((id = PfDynamicLoadCx(pf, fullpath, desc)) != -1L)
         {  if((id2 = PfDynamicFontIDCx(pf, fullpath)) != -1L)
            {  fprintf(stderr, "Comparing id %ld to %ld.\n", id, id2);

               if(id != id2)
               {  fprintf(stderr,
                     "NOTE : Retrieved id is not the same as loaded id, \
file : %s.\n", fullpath);
                  fprintf(stderr, "FAIL : PfDynamicLoadCx.\n");
                  exit(EXIT_FAILURE);
               }
            }

            fprintf(stderr, "NOTE : font foundry name %s.\n", desc);

            if(PfDynamicUnloadCx(pf, id) == -1L)
            {  fprintf(stderr, "NOTE : PfDynamicUnloadCx failed, errno %d.\n",
                       errno);
               fprintf(stderr, "FAIL : PfDynamicLoadCx.\n");
               exit(EXIT_FAILURE);
            }
         }
         else
         {  fprintf(stderr, "NOTE : PfDynamicLoadCx failed, errno %d.\n", errno);
            fprintf(stderr, "FAIL : PfDynamicLoadCx.\n");
            exit(EXIT_FAILURE);
         }

         PfDetachCx(pf);
         fprintf(stderr, "PASS : PfDynamicLoadCx.\n");
      }
      else
      {  fprintf(stderr, "UNRES : Unable to attach to fontserver, errno %d.\n",
                 errno);
         fprintf(stderr, "FAIL : PfDynamicLoadCx.\n");
      }
   }
   else
   {  fprintf(stderr, "UNRES : Unable to load dll font instance, errno %d.\n",
              errno);
      fprintf(stderr, "FAIL : PfDynamicLoadCx.\n");
   }

   return(0);
}

PfDynamicLoad():

#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 szTextFont[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("TextFont", 0, 9,
                                      szTextFont),
             0L);
    nArgs++;
    pbtnLoad = PtCreateWidget(PtButton, pwndMain, nArgs, args);
    PtAddCallback(pbtnLoad, Pt_CB_ACTIVATE, fnLoad, argv[1]);
    PtRealizeWidget(pbtnLoad);

    nArgs = 0;
    pntDIM.x = 100;
    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)"UNLOAD", 0L);
    nArgs++;
    PtSetArg(&args[3], Pt_ARG_TEXT_FONT, (long)szTextFont,
             0L);
    nArgs++;
    pbtnUnload = PtCreateWidget(PtButton, pwndMain, nArgs,
                                args);
    PtAddCallback(pbtnUnload, Pt_CB_ACTIVATE, fnUnload, NULL);
    PtRealizeWidget(pbtnUnload);

    nArgs = 0;
    pntDIM.x = 100;
    pntDIM.y = 20;
    PtSetArg(&args[0], Pt_ARG_DIM, &pntDIM, 0L);
    nArgs++;
    pntPOS.x = 100;
    pntPOS.y = 90;
    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)szTextFont,
             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 szTextFont12[MAX_FONT_TAG];

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

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

   return(Pt_CONTINUE);
}

Classification:

Photon

PfDynamicLoad()

Safety:
Interrupt handler No
Signal handler No
Thread No

PfDynamicLoadCx()

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

PfDynamicUnload(), PfDynamicUnloadCx()

Fonts chapter of the Photon Programmer's Guide