Amiga C Tutorial

11. Fonts

The Amiga can use two different fonts: Bitmap and Compugraphic or Outline fonts using the diskfont.library. A font is basically a mixture of the typeface (shape of letters), style (bold, plain, italic) and size (measured in fonts). The Amiga has one built in font called Topaz which can be accessed at all times. If using a disk based font, then you need to use the diskfont.library to load a font from disk. Other font types such as the more common TrueType and PostScript (Type 1) fonts, you need to use another library.

To use fonts you will need to include the following header files:
#include <proto/graphics.h>
#include <proto/diskfont.h>
#include <diskfont/diskfont.h>
#include <graphics/text.h>

Loading a disk font

To load a diskfont, use the OpenDiskFont() function. You need to specify a TextAttr structure to state which font you require, the format of the command is:

TextFont font = OpenDiskFont(struct TextAttr textAttr)

The format of the TextAttr is:

struct TextAttr {
      STRPTR ta_Name; /* name of the font */
      UWORD ta_YSize; /* height of the font */
      UBYTE ta_Style; /* intrinsic font style */
      UBYTE ta_Flags; /* font preferences and flags */
   };
 

For example, to specify a Topaz font, size 11 with Bold and Italic:

struct TextAttr myta = {
      "topaz.font"
      11,
      FSF_ITALIC | FSF_BOLD,
      NULL
   };
 

If you are drawing with this font, you can change the default Rastport font with the SetFont() command and when done with it using the CloseFont() functions:
e.g.

 struct TextFont *myfont, *oldfont;
 struct RastPort *myrp;
 struct Window *mywin;
 if (myfont = OpenDiskFont(&myta))
   {
     /* you would probably set the font of the rastport you are going to use */
     myrp = mywin->RPort
     oldfont = myrp->Font;
     SetFont(myrp, myfont);
   /* perform whatever drawing you need to do */
  /* time to clean up. If the rastport is not exclusively yours,
     you may need to restore the original font or other Rasport values    */
     SetFont(myrp, oldfont);
     CloseFont(myfont);
   }
 

For AmigaOS4, prefix OpenDiskFont with 'IDiskfont->'. Prefix SetFont and CloseFont with 'IGraphics->'.

If you want to know what fonts are available in your program before trying to load one from disk, then you can use the AvailFonts() function which can read all the fonts from memory and disk and present them in an AvailFontsHeader structure, followed by a number of AvailFonts structures to be read by your program. You need to allocate some memory before calling this function to store the font information.

LONG error = AvailFonts(struct AvailFontsHeader *buffer, LONG bufBytes, ULONG flags )

For example,

 int afShortage, afSize;
 struct AvailFontsHeader *afh;
 afSize = 400;
   do {
      afh = (struct AvailFontsHeader *) AllocMem(afSize, 0);
      if (afh) {
          afShortage = AvailFonts(afh, afSize,    AFF_MEMORY|AFF_DISK);
          if (afShortage) {
            FreeMem(afh, afSize);
            afSize += afShortage;
          }
     }
     else {
        fail("AllocMem of AvailFonts buffer afh failed\n");
        break;
     }
 }while (afShortage);

For AmigaOS 4, prefix AvailFonts with 'IDiskfont->'. Prefix AllocMem and FreeMem with 'IExec->'.

Next Page