< Programming for Palm OS < C

defining and using fonts

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

  • 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

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.

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.