Allegro provides routines for loading fonts directly from GRX format .fnt files, 8x8 or 8x16 BIOS format .fnt files, from bitmap images, from datafiles 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 which allows you to load them on the fly (like AllegTTF or Glyph Keeper, listed among others at http://www.allegro.cc/) and render them directly, or generate a bitmapped version of a TrueType font with tools like TTF2PCX (http://www.talula.demon.co.uk/ttf2pcx/index.html).
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. Note that another way of loading fonts is embedding them into a datafile and using the datafile related functions.
Example:
FONT *myfont; PALETTE palette; ... myfont = load_font("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 nil on error. Allegro garbage collection routine is responsible for destroying the font.
Return value: Returns TRUE if the font is a color font, FALSE if it is not.
See also: is_mono_font.
Return value: Returns TRUE if the font is a monochrome font, FALSE if it is not.
See also: is_color_font.
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.
This is C example: (convert to Lua)FONT *f; int range; int n; ... range = get_font_ranges(f); printf("The font has %d character ranges:\n", range); for (n = 0; n < range; n++) printf("Range %d from 0x%03x - 0x%03x\n", get_font_range_begin(f, n), get_font_range_end(f, 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, transpose_font.
This is C example: (convert to Lua) 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, transpose_font.
This is C example: (convert to Lua)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, transpose_font.
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 */ fontcopy = extract_font_range(myfont, -1, -1); ... destroy_font(capitals); destroy_font(fontcopy);
Return value: Returns a pointer to the new font or nil on error.
See also: get_font_range_begin, get_font_range_end, merge_fonts, transpose_font.
int transpose_font(FONT *f, int drange)
This is C example: (convert to Lua) FONT *myfont; FONT *capitals; ... /* Create a font of only capital letters */ capitals = extract_font_range(myfont, 'A', 'Z'); /* Now transpose the characters in the font so that they will be used */ /* for the lower case letters a-z */ transpose_font(capitals, 'a'-'A'); textout_ex(screen, capitals, "allcaps", 100, 100, makecol(255,255,255), 0);
Return value: Returns 0 on success, -1 on failure.
See also: get_font_range_begin, get_font_range_end, merge_fonts, extract_font_range.
FONT *merge_fonts(FONT *f1, FONT *f2)
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 = extract_font_range(myfont, -1, 'A'-1); upper_range = extract_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.
Examples using this: exfont.
FONT *load_dat_font(const char *filename, RGB *pal, void *param)
For example, suppose you have a datafile named `fonts.dat' with the following contents:
Then the following code will load FONT_1_DATA as a FONT and return FONT_1_PALETTE as the palette:FONT FONT_1_DATA FONT FONT_2_DATA FONT FONT_3_DATA PAL FONT_1_PALETTE PAL FONT_2_PALETTE
If instead you want to load the second font, FONT_2, from the datafile, you would use:FONT *f; PALETTE pal; char *names[] = { "FONT_1_DATA", "FONT_1_PALETTE" } f = load_dat_font("fonts.dat", pal, names);
If you want to load the third font, but not bother with a palette, use:FONT *f; PALETTE pal; char *names[] = { "FONT_2_DATA", "FONT_2_PALETTE" } f = load_dat_font("fonts.dat", pal, names);
FONT *f; char *names[] = { "FONT_3_DATA", NULL } f = load_dat_font("fonts.dat", NULL, names);
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)
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)
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)
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)
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). The way the characters are separated depends on the colordepth of the image file:
Note that in each horizontal row the 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.
Take care with high and true color fonts: Allegro will convert these to the current colordepth when you load the font. If you try to use a font on a bitmap with a different color depth Allegro will do color conversions on the fly, which will be rather slow. For optimal performance you should set the colordepth to the colordepth you want to use before loading any fonts.
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, grab_font_from_bitmap.
FONT *grab_font_from_bitmap(BITMAP *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: load_bitmap_font.
FONT *load_txt_font(const char *filename, RGB *pal, void *param)
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.ascii.fnt 0x20 0x7F - 0xA0 0xFF dingbats.fnt 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.