Loading custom fonts

Shari shari at gypsyware.com
Sun Nov 30 12:00:06 EST 2008


Not a question, but a solution.

Using revFontLoad to load a font you've stored in a folder and 
distributed with the app:

The issue is that if you are distributing the app to unknown users 
you will not have a full path file to the revFont.bundle or 
revFont.dll file when the app first launches if you distribute them 
as externals with your app.  Once you actually get the path and set 
the externals of the app it is too late and revFontLoad fails.  You 
get the error that it cannot find the revFontLoad handler.

I found a workaround that appears to work.  Tested on MacOSX 10.4.11. 
preOpenStack in the main stack calls the following handlers.  This is 
a cross-platform solution and both the MacOSX and Windows externals 
are distributed with each app.

on preOpenStack # of your main stack
   setPaths
   installStacks
   startUsing
   loadMyFonts
end preOpenStack

on setPaths
    global prefStack,fExternals,fFonts

   # it calculates the full paths to several stacks
   # and puts them into globals, prefStack is one of those
   # stacks and is located in a writeable place

   # fExternals is the full path to the folder you include
   # with the app that stores .bundle and .dll files, including
   # revFont.bundle and revFont.dll

   # fFonts is the full path to the folder where the fonts
   # are stored that I am including with the app

end setPaths

on installStacks
    # it installs prefStack to a writeable location
    # on the users computer if it is not already installed
end installStacks

on startUsing
   global fExternals,prefStack

   # load the externals
   if the platform is "MacOS" then
     put fExternals & "revfont.bundle" into loadWhat
   else put fExternals & "revfont.dll" into loadWhat
   set the externals of stack prefStack to loadWhat
   save stack prefStack

   # you must close the stack and reopen it before
   # revFont becomes available to your handlers

   set the destroyWindowProperty of stack prefStack to true
   close stack prefStack
   start using stack prefStack
end startUsing

on loadMyFonts
   global fFonts
   put fFonts into fontList
   put fontList into tDir
   delete the last char of tDir # delete trailing /
   set the directory to tDir
   put the files into fileList
   filter fileList with "*.ttf" # delete all non-ttf-font files

   repeat for each line x in fileList
     put fontList & x into theFont
     revFontLoad theFont
   end repeat

   # set font heirarchy
   # in case a font doesn't load, I create a heirarchy of
   # first choice font, second choice, etc. then set the
   # main font of the stack(s) to whichever font actually exists,
   # so in the following example Baskerville is my first choice,
   # Rockwell is my second choice, etc.

   # Included in the heirarchy are several fonts that are
   # usually already installed in case my fonts do not load, last resorts

   # I use the name of the font as it appears in your standard
   # font menus, not the filename

   # Since fonts come in various standard sizes, the number
   # after the dash is the font size I want used if this font is
   # the one used so that things fit properly in my fields

   put "baskerville-20,rockwell-18,book antigua-18," into tit
   put "chalkboard bold-16,gill sans-20,trebuchet ms-18,tahoma-18," after tit
   put "arial-20,verdana-18" after tit

   set the wholeMatches to true
   # so that Trebuchet doesn't match Trebuchet MS
   repeat for each item x in tit
     set the itemDel to "-"
     if item 1 of x is among the lines of the fontNames then
       set the textFont of stack "myStack" to \
           item 1 of x
       set the textSize of stack "myStack" to \
           item 2 of x
       exit repeat
     end if
     set the itemDel to comma
   end repeat

   # This assumes that you do not have fonts set for the individual
   # fields in the stack, that the fields use the stack font and size

end loadMyFonts
-- 
   Dogs and bears, sports and cars, and patriots t-shirts
   http://www.villagetshirts.com
  WlND0WS and MAClNT0SH shareware games
  http://www.gypsyware.com



More information about the use-livecode mailing list