# Font addons

These functions are declared in the following header file.
Link with allegro_font.

    #include <allegro5/allegro_font.h>

## General font routines

### API: ALLEGRO_FONT

A handle identifying any kind of font. Usually you will create it with
[al_load_font] which supports loading all kinds of TrueType fonts supported by
the FreeType library. If you instead pass the filename of a bitmap file, it will
be loaded with [al_load_bitmap] and a font in Allegro's bitmap font format will
be created from it with [al_grab_font_from_bitmap].

### API: al_init_font_addon

Initialise the font addon.

Note that if you intend to load bitmap fonts, you will need to initialise
allegro_image separately (unless you are using another library to load images).

See also: [al_init_image_addon], [al_init_ttf_addon], [al_shutdown_font_addon]

### API: al_shutdown_font_addon

Shut down the font addon. This is done automatically at program exit,
but can be called any time the user wishes as well.

See also: [al_init_font_addon]

### API: al_load_font

Loads a font from disk. This will use [al_load_bitmap_font] if you pass the
name of a known bitmap format, or else [al_load_ttf_font].

Bitmap and TTF fonts are affected by the current bitmap flags at the time the
font is loaded.

See also: [al_destroy_font], [al_init_font_addon], [al_register_font_loader]

### API: al_destroy_font

Frees the memory being used by a font structure.
Does nothing if passed NULL.

See also: [al_load_font]

### API: al_register_font_loader

Informs Allegro of a new font file type, telling it how to load files of
this format.

The extension should include the leading dot ('.') character.
It will be matched case-insensitively.

The `load_font` argument may be NULL to unregister an entry.

Returns true on success, false on error.
Returns false if unregistering an entry that doesn't exist.

See also: [al_init_font_addon]

### API: al_get_font_line_height

Returns the usual height of a line of text in the specified font. For bitmap
fonts this is simply the height of all glyph bitmaps. For truetype fonts it
is whatever the font file specifies. In particular, some special glyphs may be
higher than the height returned here.

If the X is the position you specify to draw text, the meaning of
ascent and descent and the line height is like in the figure below.

    X------------------------
        /\         |        |
       /  \        |        |
      /____\       ascent   |
     /      \      |        |
    /        \     |        height
    ----------------        |
                   |        |
                   descent  |
                   |        |
    -------------------------

See also: [al_get_text_width], [al_get_text_dimensions]

### API: al_get_font_ascent

Returns the ascent of the specified font.

See also: [al_get_font_descent], [al_get_font_line_height]

### API: al_get_font_descent

Returns the descent of the specified font.

See also: [al_get_font_ascent], [al_get_font_line_height]

### API: al_get_text_width

Calculates the length of a string in a particular font, in pixels.

See also: [al_get_ustr_width], [al_get_font_line_height],
[al_get_text_dimensions]

### API: al_get_ustr_width

Like [al_get_text_width] but expects an ALLEGRO_USTR.

See also: [al_get_text_width], [al_get_ustr_dimensions]

### API: al_draw_text

Writes the NUL-terminated string `text` onto the target bitmap at position `x`,
`y`, using the specified `font`.

The `flags` parameter can be 0 or one of the following flags:

- ALLEGRO_ALIGN_LEFT - Draw the text left-aligned (same as 0).
- ALLEGRO_ALIGN_CENTRE - Draw the text centered around the given position.
- ALLEGRO_ALIGN_RIGHT - Draw the text right-aligned to the given position.

It can also be combined with this flag:

- ALLEGRO_ALIGN_INTEGER - Always draw text aligned to an integer pixel
  position.  This is formerly the default behaviour.  Since: 5.0.8, 5.1.4

See also: [al_draw_ustr], [al_draw_textf], [al_draw_justified_text]

### API: al_draw_ustr

Like [al_draw_text], except the text is passed as an ALLEGRO_USTR instead of
a NUL-terminated char array.

See also: [al_draw_text], [al_draw_justified_ustr]

### API: al_draw_justified_text

Like [al_draw_text], but justifies the string to the region x1-x2.

The *diff* parameter is the maximum amount of horizontal space to
allow between words.  If justisfying the text would exceed *diff* pixels,
or the string contains less than two words, then the string will be drawn
left aligned.

The `flags` parameter can be 0 or one of the following flags:

- ALLEGRO_ALIGN_INTEGER - Draw text aligned to integer pixel positions.
  Since: 5.0.8, 5.1.5

See also: [al_draw_justified_textf], [al_draw_justified_ustr]

### API: al_draw_justified_ustr

Like [al_draw_justified_text], except the text is passed as an ALLEGRO_USTR
instead of a NUL-terminated char array.

See also: [al_draw_justified_text], [al_draw_justified_textf].

### API: al_draw_textf

Formatted text output, using a printf() style format string.
All parameters have the same meaning as with [al_draw_text] otherwise.

See also: [al_draw_text], [al_draw_ustr]

### API: al_draw_justified_textf

Formatted text output, using a printf() style format string.
All parameters have the same meaning as with [al_draw_justified_text]
otherwise.

See also: [al_draw_justified_text], [al_draw_justified_ustr].

### API: al_get_text_dimensions

Sometimes, the [al_get_text_width] and [al_get_font_line_height]
functions are not enough for exact text placement, so this function
returns some additional information.

Returned variables (all in pixel):

- x, y - Offset to upper left corner of bounding box.
- w, h - Dimensions of bounding box.

Note that glyphs may go to the left and upwards of the X, in which
case x and y will have negative values.

See also: [al_get_text_width], [al_get_font_line_height],
[al_get_ustr_dimensions]

### API: al_get_ustr_dimensions

Sometimes, the [al_get_ustr_width] and [al_get_font_line_height]
functions are not enough for exact text placement, so this function
returns some additional information.

See also: [al_get_text_dimensions]

### API: al_get_allegro_font_version

Returns the (compiled) version of the addon, in the same format as
[al_get_allegro_version].

## Bitmap fonts

### API: al_grab_font_from_bitmap

Creates a new font from an Allegro bitmap. You can delete the bitmap after the
function returns as the font will contain a copy for itself.

Parameters:

- bmp: The bitmap with the glyphs drawn onto it
- n: Number of unicode ranges in the bitmap.
- ranges: 'n' pairs of first and last unicode point to map glyphs to for
  each range.

The bitmap format is as in the following example, which contains three glyphs
for 1, 2 and 3.

    .............
    . 1 .222.333.
    . 1 .  2.  3.
    . 1 .222.333.
    . 1 .2  .  3.
    . 1 .222.333.
    .............

In the above illustration, the dot is for pixels having the background color. It
is determined by the color of the top left pixel in the bitmap. There should be
a border of at least 1 pixel with this color to the bitmap edge and between all
glyphs.

Each glyph is inside a rectangle of pixels not containing the background
color. The height of all glyph rectangles should be the same, but the width
can vary.

The placement of the rectangles does not matter, except that glyphs are scanned
from left to right and top to bottom to match them to the specified unicode
codepoints.

The glyphs will simply be drawn using [al_draw_bitmap], so usually you will want
the rectangles filled with full transparency and the glyphs drawn in opaque
white.

Examples:

    int ranges[] = {32, 126};
    al_grab_font_from_bitmap(bitmap, 1, ranges)

    int ranges[] = {
        0x0020, 0x007F,  /* ASCII */
        0x00A1, 0x00FF,  /* Latin 1 */
        0x0100, 0x017F,  /* Extended-A */
        0x20AC, 0x20AC}; /* Euro */
    al_grab_font_from_bitmap(bitmap, 4, ranges)

The first example will grab glyphs for the 95 standard printable ASCII
characters, beginning with the space character (32) and ending with the
tilde character (126). The second example will map the first 96 glyphs
found in the bitmap to ASCII range, the next 95 glyphs to Latin 1, the
next 128 glyphs to Extended-A, and the last glyph to the Euro character.
(This is just the characters found in the Allegro 4 font.)

See also: [al_load_bitmap], [al_grab_font_from_bitmap]

### API: al_load_bitmap_font

Load a bitmap font from. It does this by first calling [al_load_bitmap] and then
[al_grab_font_from_bitmap]. If you want to for example load an old A4 font,
you could load the bitmap yourself, then call [al_convert_mask_to_alpha] on it
and only then pass it to [al_grab_font_from_bitmap].

### API: al_create_builtin_font

Creates a monochrome bitmap font (8x8 pixels per character).

This font is primarily intended to be used for displaying information in
environments or during early runtime states where no external font data is
available or loaded (e.g. for debugging).

The builtin font contains the following unicode character ranges:

    0x0020 to 0x007F (ASCII)
    0x00A1 to 0x00FF (Latin 1)
    0x0100 to 0x017F (Extended A)
    0x20AC to 0x20AC (euro currency symbol)

Returns NULL on an error.

The font memory must be freed the same way as for any other font,
using [al_destroy_font].

Since: 5.0.8, 5.1.3

See also: [al_load_bitmap_font], [al_destroy_font]

## TTF fonts

These functions are declared in the following header file.
Link with allegro_ttf.

    #include <allegro5/allegro_ttf.h>

### API: al_init_ttf_addon

Call this after [al_init_font_addon] to make [al_load_font] recognize
".ttf" and other formats supported by [al_load_ttf_font].

Returns true on success, false on failure.

### API: al_shutdown_ttf_addon

Unloads the ttf addon again. You normally don't need to call this.

### API: al_load_ttf_font

Loads a TrueType font from a file using the FreeType library. Quoting
from the FreeType FAQ this means support for many different font
formats:

*TrueType, OpenType, Type1, CID, CFF, Windows FON/FNT, X11 PCF, and
others*

The *size* parameter determines the size the font will be rendered at,
specified in pixels. The standard font size is measured in *units per
EM*, if you instead want to specify the size as the total height of
glyphs in pixels, pass it as a negative value.

> *Note:* If you want to display text at multiple sizes, load the font
multiple times with different size parameters.

The following flags are supported:

* ALLEGRO_TTF_NO_KERNING - Do not use any kerning even if the font file
  supports it.

* ALLEGRO_TTF_MONOCHROME - Load as a monochrome font (which means no
  anti-aliasing of the font is done).

* ALLEGRO_TTF_NO_AUTOHINT - Disable the Auto Hinter which is enabled by default
  in newer versions of FreeType. Since: 5.0.6, 5.1.2

See also: [al_init_ttf_addon], [al_load_ttf_font_f]

### API: al_load_ttf_font_f

Like [al_load_ttf_font], but the font is read from the file handle. The filename
is only used to find possible additional files next to a font file.

> *Note:* The file handle is owned by the returned ALLEGRO_FONT object and must not
be freed by the caller, as FreeType expects to be able to read from it at a
later time.

### API: al_load_ttf_font_stretch

Like [al_load_ttf_font], except it takes separate width and height
parameters instead of a single size parameter.

If the height is a positive value, and the width zero or positive, then font
will be stretched according to those parameters. The width must not be negative
if the height is positive.

As with [al_load_ttf_font], the height may be a negative value to specify
the total height in pixels.  Then the width must also be a negative value, or
zero.

The behaviour is undefined the height is positive while width is negative,
or if the height is negative while the width is positive.

Since: 5.0.6, 5.1.0

See also: [al_load_ttf_font], [al_load_ttf_font_stretch_f]

### API: al_load_ttf_font_stretch_f

Like [al_load_ttf_font_stretch], but the font is read from the file handle. The
filename is only used to find possible additional files next to a font file.

> *Note:* The file handle is owned by the returned ALLEGRO_FONT object and must not
be freed by the caller, as FreeType expects to be able to read from it at a
later time.

Since: 5.0.6, 5.1.0

See also: [al_load_ttf_font_stretch]

### API: al_get_allegro_ttf_version

Returns the (compiled) version of the addon, in the same format as
[al_get_allegro_version].
