Programming for Palm OS/C/Fonts

defining and using fonts

edit

Make a font available to your application with:

 FontPtr  font = MemHandleLock( DmGetResource('NFNT', TinyFont));
 FntDefineFont( 0x80, font);

..where TinyFont is the resource ID of the font to be made available.

To set the current font (honoured by explicit drawing functions but not automatically used by all controls, which hold an independent reference to the font they should use):

 FntSetFont( 0x80);


calculating position based on font size

edit
  • FntCharsWidth works out the width in pixels of the text given to it
  • FntCharHeight provides the height of the font, which is the sum of the ascent and descent and does not include any space you might wish between lines of text
  • FntAverageCharWidth provides the width in pixels of the widest character, despite its name.


working example

edit

This snippet depends upon font resources.


#include <PalmOS.h>
#include "Fonts.h"


UInt32 PilotMain( UInt16 cmd, void *cmdPBP, UInt16 launchFlags)
{
  EventType  event;
  FontPtr    font;
  char       *msg;
  UInt16     msgLen;
  Coord      y;
  Coord      x;

  if ( sysAppLaunchCmdNormalLaunch == cmd)
  {
    font = MemHandleLock( DmGetResource('NFNT', TinyFont));
    FntDefineFont( 0x80, font);
    FntSetFont( 0x80);

    // locate the text in the middle of the screen
    msg    = "Hello, world!";
    msgLen = StrLen( msg);
    x      = (160 - FntCharsWidth(msg, msgLen)) / 2;
    y      = (160 - FntCharHeight()) / 2;
    WinDrawChars( msg, msgLen, x, y);

    do {
      EvtGetEvent( &event, evtWaitForever);

      SysHandleEvent( &event);

    } while (event.eType != appStopEvent);
  }

  return 0;
}

See Programming for Palm OS/Small fonts for fonts that might be useful to Palm programmers.