Fonts

Allegro provides routines for loading fonts from GRX format .fnt files, 8x8 or 8x16 BIOS format .fnt files, and from bitmap images, or you can import a multiple-range Unicode font by writing a .txt script that specifies a number of different source files for each range of characters.

By default, Allegro can only use bitmapped (non-scalable) fonts. If you want to use TrueType fonts, you will need to use an add-on library.


void register_font_file_type(const char *ext, FONT *(*load)(const char *filename, RGB *pal, void *param));

Informs the load_font() functions of a new file type, providing a routine to read fonts in this format. The function you supply must follow the following prototype:
      FONT *load_my_font(const char *filename, RGB *pal, void *param)
      {
         ...
      }
The pal parameter can optionally be used to return a palette for the FONT. The parameter param can be anything you like: you can use this to pass information to your loading routine, such as for instance the font height, the character range to load or the index number of a font in a datafile. If you choose to write your own font loading code, your function should be prepared to deal with a value of NULL for either of these parameters.
See also: load_font.
FONT *load_font(const char *filename, RGB *pal, void *param);

Loads a font from a file. At present, this supports loading fonts from a GRX format .fnt file, a 8x8 or 8x16 BIOS format .fnt file or any bitmap format that can be loaded by load_bitmap().

If the font contains palette information, then the palette is returned in the second parameter, which should be an array of 256 RGB structures (a PALETTE). The pal argument may be NULL. In this case, the palette data, if present, is simply not returned.

The third parameter can be used to pass specific information to a custom loader routine. Normally, you can just leave this as NULL.

Example:

      FONT *myfont;
      PALETTE palette;
      ...
      myfont = load_bitmap("my_font.pcx", palette, NULL);
      if (!myfont)
         abort_on_error("Couldn't load font!");
      ...
      textout_centre_ex(screen, myfont, "This is my own pretty font!",
                        SCREEN_W / 2, SCREEN_H / 2, white, black);
      ...
      destroy_font(bmp);

Return value: Returns a pointer to the font or NULL on error. Remember that you are responsible for destroying the font when you are finished with it to avoid memory leaks.

See also: register_font_file_type, load_bitmap, load_dat_font, load_bios_font, load_grx_font, load_grx_or_bios_font, load_bitmap_font, load_txt_font.
int is_color_font(FONT *f)

This function returns TRUE if the font is an Allegro color font (as opposed to a monochrome font).

Return value: Returns TRUE if the font is a color font, FALSE if it is not.

See also: is_mono_font.
int is_mono_font(FONT *f)

This function returns TRUE if the font is an Allegro monochrome font (as opposed to a color font).

Return value: Returns TRUE if the font is a monochrome font, FALSE if it is not.

See also: is_color_font.
FONT *is_compatible_font(FONT *f1, FONT *f2)

This function compares the two fonts. If it returns TRUE, then you should normally be able to merge the two fonts. If it returns FALSE, this is less certain.

Return value: Returns TRUE if the two fonts are of the same general type (both are color fonts or both are monochrome fonts, for instance).

See also: merge_fonts, is_color_font, is_mono_font.
int get_font_ranges(FONT *f)

This function returns the number of character ranges in a font. You should query each of these ranges with get_font_range_begin() and get_font_range_end() to find out what characters are availabel in the font. Example:
      FONT *f;
      int range;
      int n;
      ...
      
      range = get_font_ranges(f);
      printf("The font has %d character ranges:\n", range);
      for (n=0; n

Return value: Returns the number of continuous character ranges in a font, or -1 if that information is not available.

See also: get_font_range_begin, get_font_range_end.
int get_font_range_begin(FONT *f, int range)

This function returns the start of the character range for the font f, or -1 if that information is not available for the selected font. You can pass -1 for the range parameter if you want to know the start of the whole font range, or a number from 0 to (but not including) get_font_ranges(f) to get the start of a specific character range in the font. Example:
      printf("The font has a character range of %d - %d\n",
             get_font_range_begin(font, -1), get_font_range_end(font, -1));
      

Return value: Returns the first character in the font range, or -1 if that information is not available.

See also: get_font_ranges, get_font_range_end.
int get_font_range_end(FONT *f, int range)

This function returns the last character of the character range for the font f. You can pass -1 for the range parameter if you want to know the start of the whole font range, or a number from 0 to (but not including) get_font_ranges(f) to get the start of a specific character range in the font. You should check the start and end of all font ranges to see if a specific character is actually available in the font. Not all characters in the range returned by get_font_range_begin(f, -1) and get_font_range_end(f, -1) need to be available! Example:
      printf("The font has a character range of %d - %d\n",
             get_font_range_begin(font, -1), get_font_range_end(font, -1));
      

Return value: Returns the last character in the font range, or -1 if that information is not available.

See also: get_font_ranges, get_font_range_begin.
FONT *extract_font_range(FONT *f, int begin, int end)

This function extracts a character range from a font and returns a new font that contains only the range of characters selected by this function. You can pass -1 for either the lower or upper bound if you want to select all characters from the start or the end of the font. Example:
      FONT *myfont;
      FONT *capitals;
      FONT *fontcopy;
      ...
      /* Create a font of only capital letters */
      capitals = extract_font_range(myfont, 'A', 'Z');

      /* Create a copy of the font */
      fontopy = extract_font_range(myfont, -1, -1);
      

Return value: Returns a pointer to the new font or NULL on error. Remember that you are responsible for destroying the font when you are finished with it to avoid memory leaks.

See also: get_font_range_begin, get_font_range_end, merge_fonts.
FONT *merge_fonts(FONT *f1, FONT *f2)

This function merges the character ranges from two fonts and returns a new font containing all characters in the old fonts. In general, you cannot merge fonts of different types (eg, TrueType fonts and bitmapped fonts), but as a special case, this function can promote a monochrome bitmapped font to a color font and merge those. Example:
      FONT *myfont;
      FONT *myfancy_font;
      FONT *lower_range;
      FONT *upper_range;
      FONT *capitals;
      FONT *combined_font;
      FONT *tempfont;
      ...
      /* Create a font that contains the capatials from  */
      /* the fancy font but other characters from myfont */
      lower_range = extarct_font_range(myfont, -1, 'A'-1);
      upper_range = extarct_font_range(myfont, 'Z'+1, -1);
      capitals = extract_font_range(myfancy_font, 'A', 'Z');

      tempfont = merge_fonts(lower_range, capitals);
      combined_font = merge_fonts(tempfont, upper_range);

      /* Clean up temporary fonts */
      destroy_font(lower_range);
      destroy_font(upper_range);
      destroy_font(capitals);
      destroy_font(combined_font);

Return value: Returns a pointer to the new font or NULL on error. Remember that you are responsible for destroying the font when you are finished with it to avoid memory leaks.

See also: extract_font_range, is_color_font, is_mono_font.
FONT *load_dat_font(const char *filename, RGB *pal, void *param)

Loads a FONT from an Allegro datafile. You can set param parameter to point to an array that holds two strings that identify the font and the palette in the datafile by name. The first string in this list is the name of the font. You can pass NULL here to just load the first font found in the datafile. The second string can be used to specify the name of the palette associated with the font. This is only returned if the pal parameter is not NULL. If you pass NULL for the name of the palette, the last palette found before the font was found is returned. You can also pass NULL for param, which is treated as if you had passed NULL for both strings seperately. In this case, the function will simply load the first font it finds from the datafile and the palette that precedes it.

For example, suppose you have a datafile named `fonts.dat' with the following contents:

      FONT  FONT_1_DATA
      FONT  FONT_2_DATA
      FONT  FONT_3_DATA
      PAL   FONT_1_PALETTE
      PAL   FONT_2_PALETTE
Then the following code will load FONT_1_DATA as a FONT and return FONT_1_PALETTE as the palette:
      FONT *f;
      PALETTE pal;
      char *names[] = { "FONT_1_DATA", "FONT_1_PALETTE" }
      
      f = load_dat_font("fonts.dat", pal, NULL);
If instead you want to load the second font, FONT_2, from the datafile, you would use:
      FONT *f;
      PALETTE pal;
      char *names[] = { "FONT_2_DATA", "FONT_2_PALETTE" }
      
      f = load_dat_font("fonts.dat", pal, NULL);
If you want to load the third font, but not bother with a palette, use:
      FONT *f;
      char *names[] = { "FONT_3_DATA", NULL }
      
      f = load_dat_font("fonts.dat", NULL, &n);

Return value: Returns a pointer to the font or NULL on error. Remember that you are responsible for destroying the font when you are finished with it to avoid memory leaks.

See also: register_font_file_type, load_font.
FONT *load_bios_font(const char *filename, RGB *pal, void *param)

Loads a 8x8 or 8x16 BIOS format font. You shouldn't normally call this routine directly.

Return value: Returns a pointer to the font or NULL on error. Remember that you are responsible for destroying the font when you are finished with it to avoid memory leaks.

See also: register_font_file_type, load_font.
FONT *load_grx_font(const char *filename, RGB *pal, void *param)

Loads a GRX format font. You shouldn't normally call this routine directly.

Return value: Returns a pointer to the font or NULL on error. Remember that you are responsible for destroying the font when you are finished with it to avoid memory leaks.

See also: register_font_file_type, load_font.
FONT *load_grx_or_bios_font(const char *filename, RGB *pal,void *param))

Loads either a BIOS or GRX format font. You shouldn't normally call this routine directly.

Return value: Returns a pointer to the font or NULL on error. Remember that you are responsible for destroying the font when you are finished with it to avoid memory leaks.

See also: register_font_file_type, load_font.
FONT *load_bitmap_font(const char *filename, RGB *pal, void *param)

Tries to grab a font from a bitmap. The bitmap can be in any format that load_bitmap understands but it must have a color depth of 8 bits.

The size of each character is determined by the layout of the image, which should be a rectangular grid containing all the ASCII characters from space (32) up to the tilde (126). Use color 0 for the transparent portions of the characters and fill the spaces between each letter with color 255.

Note that in each horizontal row the color-0 bounding boxes around the characters should align and have the same height.

Probably the easiest way to get to grips with how this works is to load up the `demo.dat' file and export the TITLE_FONT into a PCX file. Have a look at the resulting picture in your paint program: that is the format a font should be in.

Return value: Returns a pointer to the font or NULL on error. Remember that you are responsible for destroying the font when you are finished with it to avoid memory leaks.

See also: register_font_file_type, load_font, load_bitmap, set_color_depth.
FONT *load_txt_font(const char *filename, RGB *pal, void *param)

This function can be used to load scripted fonts. The script file contains a number of lines in the format "filename start end", which specify the source file for that range of characters, the Unicode value of the first character in the range, and the end character in the range (optional, if left out, the entire input file will be grabbed). If the filename is replaced by a hyphen, more characters will be grabbed from the previous input file. For example, the script:
      ascii.fnt 0x20 0x7F
      - 0xA0 0xFF
      dingbats.fnt 0x1000
   
would import the first 96 characters from ascii.fnt as the range 0x20-0x7F, the next 96 characters from ascii.fnt as the range 0xA0-0xFF, and the entire contents of dingbats.fnt starting at Unicode position 0x1000.

Return value: Returns a pointer to the font or NULL on error. Remember that you are responsible for destroying the font when you are finished with it to avoid memory leaks.

See also: register_font_file_type, load_font.

Back to contents