                        Blitz Tricks and Tips...
|1-Reading the Workbench Font Settings


                    A Possible Blitz Series by D!ck.

Imagine you are writing a program that is designed to run on a Workbench
screen.   To  make  it  absolutely  accurate  you will have to scale the
window  according to the size of the font that is currently set.  If the
user  has a system font of 24 points and you have programmed for 8-point
Topaz,  then  you are going to hit problems when the lower areas of your
window are inaccessible.

This was the exact problem I faced during the writing of ADFTool II, and
there's  no easy way around it.  There's no command to tell you what the
size  of the fonts are, or even the names of them, so you have to resort
to a little trickery.

Loading the file "ENV:Sys/font.prefs" (the file in RAM that contains the
current  font  settings for a Workbench screen) I noticed that the names
of  the  current  fonts were equally spread throughout the file and that
the filesize did not alter, even if fontnames of a different length were
included.  I also noticed that the point size was stored as a word value
four bytes in front of the first character of the font name.

Armed with this information I set about reading the actual file into RAM
and stripping the information that I wanted straight out of the file.  A
short time later - it was a simple enough job - I had knocked up the bit
of source code below, it is in the form of a statement so that it can be
inserted into your "includes" file and forgotten about.

    ---- 8< ---------------------------------------------------- START

    Statement _READFONTPREFS{}
      SHARED wbfont$,syfont$,scfont$
      SHARED wbfont.w,syfont.w,scfont.w
      ;--------------------------------- Variables
      pref$  = "ENV:Sys/font.prefs"
      fail.b = False
      ;--------------------------------- Load & Strip file
      If Exists(pref$)=518
        filepos.l=AllocMem(518,65536)
        rf.b=ReadFile(1,pref$)
        If rf=True
          ReadMem 1,filepos,518
          CloseFile 1
          wbfont$ = Peek$  (filepos+ 62)
          wbfont  = Peek.w (filepos+ 58)
          syfont$ = Peek$  (filepos+226)
          syfont  = Peek.w (filepos+222)
          scfont$ = Peek$  (filepos+390)
          scfont  = Peek.w (filepos+386)
        Else
          fail=True
        EndIf
        FreeMem filepos.l,518
      Else
        fail=True
      EndIf
      ;--------------------------------- Any problems?
      If fail=True
        wbfonts$="Topaz.font" : wbfont=8
        syfonts$="Topaz.font" : syfont=8
        scfonts$="Topaz.font" : scfont=8
      EndIf
    End Statement

    ---- 8< ---------------------------------------------------- END

The  code  is  simple  enough  to follow to anyone who programs in Blitz
much,  and  so  I won't bother with an overly detailed explanation.  You
will  notice that all font names and sizes are stored in seperate names,
which  can  easily be changed to a couple of arrays.  Basically, though,
wbfont$  and wbfont refer to the Workbench Icon font, syfont$ and syfont
refer  to  the  system  default font and scfont$ and scfont refer to the
Screen font, all as they are set in the Font program in Prefs.

If  anything fails in this procedure - for example if the fonts have not
been  changed  and  therefore  the file in RAM will not exist - then the
three  font types are all set to boring 8-point Topaz, the normal system
defaults.   This is especially important where a program is not run from
a fully-configured Workbench.

I  am  very interested to hear from anyone else who has hacked around in
this  manner  to overcome a problem, if you've got something of interest
then  please  send  it to me, you can pass it to Kei/Carnage who will in
turn pass it on to me, or you can E-Mail me direct, my E-Mail address is
in the submissions section.

-!-
