Using custom fonts in PalmPilot
Palm III - Palm recommended approach
Include your font as resource into application. Insert the following code into StartApplication (do not forget error checking):
static int StartApplication(void)
{
VoidHand fontHandle;
FontType *fontPtr;
// 'Font' and 0x1000 are resourse type and ID,
// defined by you in resource description
fontHandle=DmGetResource('Font',0x1000);
fontPtr=MemHandleLock(fontHandle);
// user defined fonts start from 129
FntDefineFont(129,fontPtr);
FrmGotoForm(frmMain);
}
static int StopApplication(void)
{
VoidHand fontHandle;
FontType *fontPtr;
fontHandle=DmGetResource('Font',0x1000);
fontPtr=MemHandleUnlock(fontHandle);
}
This would allow you to use user-defined font ids in forms and other UI resources:
LABEL "your label" AUTOID AT (25 55) USABLE FONT 129
Hacking standard fonts
The previously listed code that restored system fonts from ROM no longer works, starting with OS 4.0. You should backup upon startup any values you are changing within your application and restore them on exit.
// Sets current font from locked pointer
void SetFont(UInt16 index, UInt32 ptr, UInt32 version)
{
if(version<0x03000000l)
*(((UInt32 *)0x1d2)+index)=ptr;
else
*((*((UInt32 **)0x1da))+index)=ptr;
}
// Gets current font's locked pointer
UInt32 GetFont(UInt16 index, UInt32 version)
{
if(version<0x03000000l)
return *(((UInt32 *)0x1d2)+index);
else
return *((*((UInt32 **)0x1da))+index);
}
// This routine sets user-defined font from VoidHand
// handle should be locked
Boolean SetFont(UInt index, VoidHand fh)
{
void *fp;
DWord version;
FtrGet(sysFtrCreator,sysFtrNumROMVersion,&version);
if(fh!=0 &&MemHandleLockCount(fh)!=0
&&(fp=MemHandleLock(fh))!=NULL){
SetFont(index,fp);
( MemHandleUnlock(fh);
return true;
}
return false;
}
Using the above functions:
static int StartApplication(void)
{
VoidHand fontHandle;
FontType *fontPtr;
// 'Font' and 0x1000 are resourse type and ID,
// defined by you in resource description
fontHandle=DmGetResource('Font',0x1000);
fontPtr=MemHandleLock(fontHandle);
// 0 - standard font
SetFont(0,fonthandle);
FrmGotoForm(frmMain);
}
static int StopApplication(void)
{
VoidHand fontHandle;
FontType *fontPtr;
SetStandardFont(0);
fontHandle=DmGetResource('Font',0x1000);
fontPtr=MemHandleUnlock(fontHandle);
}
Notes
You also can set font immediately before drawing functions, although this would not allow you to use it in the form resource.
The following code sets font from locked pointer until next FntSetFont is called:
// This routine sets user-defined font from pointer
// pointer should be locked
Boolean SetFontPtr(FontType *fontPtr)
{
DWord version;
FtrGet(sysFtrCreator,sysFtrNumROMVersion,&version);
if(version<0x03000000l)
*((DWord *)0x1ce)=(DWord)fp;
else
*((DWord *)0x1d6)=(DWord)fp;
return true;
}
To return to system value, simply call FntSetFont(0);
Metrowerks Codewarrior (cortesy of James Lynes)
After some research and experimentation, I think the best way to use CW on windows to include your binary font is as follows:
- Get your font file in binary form (using your editor?) MYFNT.PFT
- Type-up a "Rez" text resource file (MYFNT.R) containing the line:
read 'Font' (1000) "MYFNT.PFT"
...where 1000 is the resource ID
- Use CW's AddFile to put the file under the AppResources of your project
- Make sure that in the project settings - file mapping section that you have a definition of TEXT, all flags off, and REZ compiler for files with .R extensions
- Use your(Sergey's) suggested code to bring the font resource into your C code
IT WORKS!
|
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234
|
|