TTF v1.1.0 Manual

Michael R Sweet

Copyright © 2018-2025 by Michael R Sweet

Contents

Introduction

TTF is a simple C library for using TrueType and OpenType font files.

Using the TTF Library

The TTF library has a single header file:

#include <ttf.h>

Use the pkg-config command to get the proper compiler and linker options:

CFLAGS = `pkg-config --cflags ttf`
LIBS = `pkg-config --libs ttf`

cc -o myprogram `pkg-config --cflags ttf` myprogram.c `pkg-config --libs ttf`

Opening Fonts

The ttfCreate function opens a font file, returning a pointer to a ttf_t object:

ttf_t *font = ttfCreate("FILENAME.ttf", /*idx*/0, /*err_cb*/NULL, /*err_cbdata*/NULL);

The returned pointer can be used with the other TTF library functions to provide various bits of metadata and measure the bounds of a given string of Unicode text. Once you are done with the font data, use the ttfDelete function to free the memory that was used.

If you already have a font file loaded in memory, the ttfCreateData function can be used to create a ttf_t object from the buffer:

ttf_t *font = ttfCreateData(data, datasize, /*idx*/0, /*err_cb*/NULL, /*err_cbdata*/NULL);

The error callback function ("err_cb") and data ("err_cbdata") allow you to specify a function that will receive any error messages that are ordinarily displayed to the standard error file. The callback receives the data pointer and a message string, for example the following callback will display the error message to the standard output file instead:

void
my_error_cb(void *err_cbdata, const char *message)
{
  (void)err_cbdata;

  puts(message);
}

Accessing System and User Fonts

TTF also provides a ttf_cache_t object representing a cache of available fonts. The [`ttfCacheCreate] function creates a cache of fonts that are provided by the operating system and/or installed by the user:

ttf_cache_t *cache = ttfCacheCreate("my-application-name", /*err_cb*/NULL, /*err_cbdata*/NULL);

The error callback function and data are the same as you would provide to the ttfCreate or ttfCreateData functions.

Once loaded, you can access the available fonts using the ttfCacheFind or ttfCacheGetFont functions, for example:

// Find Helvetica
ttf_t *font = ttfCacheFind(cache, "Helvetica", TTF_STYLE_NORMAL, TTF_WEIGHT_400, TTF_STRETCH_NORMAL);

// List all fonts
size_t i, num_fonts = ttfCacheGetNumFonts(cache);

for (i = 0; i < num_fonts; i ++)
{
  font = ttfCacheGetFont(cache, i);

  puts(ttfGetFamily(font));
}

Note: Do not call ttfDelete on fonts obtained from the cache. Instead, call ttfCacheDelete to free all fonts in the cache.

Functions

ttfCacheAdd

Add a font to the cache.

void ttfCacheAdd(ttf_cache_t *cache, ttf_t *font, const char *filename);

Parameters

cache Font cache
font Font
filename Filename/URL or NULL for in-memory

Discussion

This function adds the specified font to the cache.

Note: Once added, the cache takes over management of the font. Do not call ttfDelete with the font pointer. Instead, the font will be freed when you call ttfCacheDelete to free the cache.

ttfCacheCreate

Create a cache of system and user fonts.

ttf_cache_t *ttfCacheCreate(const char *appname, ttf_err_cb_t err_cb, void *err_cbdata);

Parameters

appname Application name
err_cb Error callback function
err_cbdata Error callback data

Return Value

Font cache

Discussion

This function creates a cache of system and user fonts, loading any previously cached results for the named application "appname".

The "err_cb" and "err_cbdata" arguments specify a callback function and data pointer for receiving error messages. If NULL, errors are sent to the stderr file. The callback function receives the data pointer and a text message string, for example:

void my_err_cb(void *err_cbdata, const char *message)
{
  fprintf(stderr, "ERROR: %s\n", message);
}

ttfCacheDelete

Delete a cache.

void ttfCacheDelete(ttf_cache_t *cache);

Parameters

cache Font cache

Discussion

This function deletes a cache, freeing any loaded fonts.

ttfCacheFind

Find a font in the cache.

ttf_t *ttfCacheFind(ttf_cache_t *cache, const char *family, ttf_style_t style, ttf_weight_t weight, ttf_stretch_t stretch);

Parameters

cache Font cache
family Family name
style Font style or TTF_STYLE_UNSPEC
weight Font weight or TTF_WEIGHT_UNSPEC
stretch Font stretch or TTF_STRETCH_UNSPEC

Return Value

Font or NULL if no matching font found

Discussion

This function finds a font in the cache. The returned ttf_t object is managed by the cache and should not be deleted.

ttfCacheGetFamily

Get the font family at index N.

const char *ttfCacheGetFamily(ttf_cache_t *cache, size_t n);

Parameters

cache Font cache
n Font index starting at 0

Return Value

Font family name or NULL

ttfCacheGetFilename

Get the font filename at index N.

const char *ttfCacheGetFilename(ttf_cache_t *cache, size_t n);

Parameters

cache Font cache
n Font index starting at 0

Return Value

Font filename/URL or NULL if none

ttfCacheGetFont

Get the font at index N

ttf_t *ttfCacheGetFont(ttf_cache_t *cache, size_t n);

Parameters

cache Font cache
n Font index starting at 0

Return Value

Font

Discussion

This function gets a font from the cache. The returned ttf_t object is managed by the cache and should not be deleted.

ttfCacheGetIndex

Get the font index for the composite font at index N.

size_t ttfCacheGetIndex(ttf_cache_t *cache, size_t n);

Parameters

cache Font cache
n Cached font index starting at 0

Return Value

Composite font index

ttfCacheGetNumFonts

Get the number of fonts in the cache.

size_t ttfCacheGetNumFonts(ttf_cache_t *cache);

Parameters

cache Font cache

Return Value

Number of fonts

ttfCacheGetStretch

Get the font stretch at index N.

ttf_stretch_t ttfCacheGetStretch(ttf_cache_t *cache, size_t n);

Parameters

cache Font cache
n Font index starting at 0

Return Value

Font stretch or TTF_STRETCH_UNSPEC

ttfCacheGetStyle

Get the font style at index N.

ttf_style_t ttfCacheGetStyle(ttf_cache_t *cache, size_t n);

Parameters

cache Font cache
n Font index starting at 0

Return Value

Font style or TTF_STYLE_UNSPEC

ttfCacheGetWeight

Get the font weight at index N.

ttf_weight_t ttfCacheGetWeight(ttf_cache_t *cache, size_t n);

Parameters

cache Font cache
n Font index starting at 0

Return Value

Font weight or TTF_WEIGHT_UNSPEC

ttfContainsChar

Test for the presence of a Unicode character in a font.

bool ttfContainsChar(ttf_t *font, int ch);

Parameters

font Font
ch Unicode character

Return Value

true if font contains the character, false otherwise

ttfContainsChars

Test for the presence of all characters in a UTF-8 (Unicode) string in a font.

bool ttfContainsChars(ttf_t *font, const char *s);

Parameters

font Font
s UTF-8 string

Return Value

true if font contains the characters in the string, false otherwise

ttfCreate

Create a new font object for the named font file.

ttf_t *ttfCreate(const char *filename, size_t idx, ttf_err_cb_t err_cb, void *err_cbdata);

Parameters

filename Filename
idx Font number to create in collection (0-based)
err_cb Error callback or NULL to log to stderr
err_cbdata Error callback data

Return Value

New font object

Discussion

This function creates a new font object for the named TrueType or OpenType font file or collection. The "filename" argument specifies the name of the file to read.

The "idx" argument specifies the font to load from a collection - the first font is number 0. Once created, you can call the ttfGetNumFonts function to determine whether the loaded font file is a collection with more than one font.

The "err_cb" and "err_cbdata" arguments specify a callback function and data pointer for receiving error messages. If NULL, errors are sent to the stderr file. The callback function receives the data pointer and a text message string, for example:

void my_err_cb(void *err_cbdata, const char *message)
{
  fprintf(stderr, "ERROR: %s\n", message);
}

ttfCreateData

Create a new font object from a memory buffer.

ttf_t *ttfCreateData(const void *data, size_t datasize, size_t idx, ttf_err_cb_t err_cb, void *err_cbdata);

Parameters

data Buffer
datasize Size of buffer in bytes
idx Font number to create in collection (0-based)
err_cb Error callback or NULL to log to stderr
err_cbdata Error callback data

Return Value

New font object

Discussion

This function creates a new font object from a memory buffer. The "data" argument specifies a pointer to the first byte of data and the "datasize" argument specifies the length of the memory buffer in bytes.

Note: The caller is responsible for ensuring that the memory buffer is available until the font object is deleted with ttfDelete.


The "idx" argument specifies the font to load from a collection - the first font is number 0. Once created, you can call the ttfGetNumFonts function to determine whether the loaded font file is a collection with more than one font.

The "err_cb" and "err_cbdata" arguments specify a callback function and data pointer for receiving error messages. If NULL, errors are sent to the stderr file. The callback function receives the data pointer and a text message string, for example:
void my_err_cb(void *err_cbdata, const char *message)
{
  fprintf(stderr, "ERROR: %s\n", message);
}

ttfDelete

Free all memory used for a font family object.

void ttfDelete(ttf_t *font);

Parameters

font Font

ttfGetAscent

Get the maximum height of non-accented characters.

int ttfGetAscent(ttf_t *font);

Parameters

font Font

Return Value

Ascent in 1000ths

ttfGetBounds

Get the bounds of all characters in a font.

ttf_rect_t *ttfGetBounds(ttf_t *font, ttf_rect_t *bounds);

Parameters

font Font
bounds Bounds buffer

Return Value

Bounds or NULL on error

Discussion

This function gets the bounds of all characters in a font. The "bounds" argument is a pointer to a ttf_rect_t structure that will be filled with the limits for characters in the font scaled to a 1000x1000 unit square.

ttfGetCMap

Get the Unicode to glyph mapping table.

const int *ttfGetCMap(ttf_t *font, size_t *num_cmap);

Parameters

font Font
num_cmap Number of entries in table

Return Value

CMap table

ttfGetCapHeight

Get the height of capital letters.

int ttfGetCapHeight(ttf_t *font);

Parameters

font Font

Return Value

Capital letter height in 1000ths

ttfGetCopyright

Get the copyright text for a font.

const char *ttfGetCopyright(ttf_t *font);

Parameters

font Font

Return Value

Copyright text

ttfGetDescent

Get the maximum depth of non-accented characters.

int ttfGetDescent(ttf_t *font);

Parameters

font Font

Return Value

Descent in 1000ths

ttfGetExtents

Get the extents of a UTF-8 string.

ttf_rect_t *ttfGetExtents(ttf_t *font, float size, const char *s, ttf_rect_t *extents);

Parameters

font Font
size Font size
s String
extents Extents of the string

Return Value

Pointer to extents or NULL on error

Discussion

This function computes the extents of the UTF-8 string "s" when rendered using the specified font "font" and size "size". The "extents" argument is a pointer to a ttf_rect_t structure that is filled with the extents of a simple rendering of the string with no kerning or rewriting applied. The values are scaled using the specified font size.

ttfGetFamily

Get the family name of a font.

const char *ttfGetFamily(ttf_t *font);

Parameters

font Font

Return Value

Family name

ttfGetItalicAngle

Get the italic angle.

float ttfGetItalicAngle(ttf_t *font);

Parameters

font Font

Return Value

Angle in degrees

ttfGetKernedExtents

Get the kerned extents of a string.

size_t ttfGetKernedExtents(ttf_t *font, float size, const char *s, ttf_rect_t *extents, size_t max_adjs, double *adjs);

Parameters

font Font
size Font size
s String
extents Kerned extents of string
max_adjs Maximum number of kerning adjustments
adjs Array of kerning adjustments

Return Value

Number of kerned pairs

Discussion

This function computes the kerned extents of the UTF-8 string "s" when rendered using the specified font "font" and size "size". The "extents" argument is a pointer to a ttf_rect_t structure that is filled with the extents of a kerned rendering of the string with no rewriting applied. The "adjs" argument specifies and array of double values up to "max_adjs" long containing the kerning adjustments between each pair of characters. The extent and adjustment values are scaled using the specified font size.

ttfGetMaxChar

Get the last character in the font.

int ttfGetMaxChar(ttf_t *font);

Parameters

font Font

Return Value

Last character in font

ttfGetMinChar

Get the first character in the font.

int ttfGetMinChar(ttf_t *font);

Parameters

font Font

Return Value

First character in font

ttfGetNumFonts

Get the number of fonts in this collection.

size_t ttfGetNumFonts(ttf_t *font);

Parameters

font Font

Return Value

Number of fonts

ttfGetPostScriptName

Get the PostScript name of a font.

const char *ttfGetPostScriptName(ttf_t *font);

Parameters

font Font

Return Value

PostScript name

ttfGetStretch

Get the font "stretch" value...

ttf_stretch_t ttfGetStretch(ttf_t *font);

Parameters

font Font

Return Value

Stretch value

ttfGetStyle

Get the font style.

ttf_style_t ttfGetStyle(ttf_t *font);

Parameters

font Font

Return Value

Style

ttfGetVersion

Get the version number of a font.

const char *ttfGetVersion(ttf_t *font);

Parameters

font Font

Return Value

Version number

ttfGetWeight

Get the weight of a font.

ttf_weight_t ttfGetWeight(ttf_t *font);

Parameters

font Font

Return Value

Weight

ttfGetWidth

Get the width of a single character.

int ttfGetWidth(ttf_t *font, int ch);

Parameters

font Font
ch Unicode character

Return Value

Width in 1000ths

ttfGetXHeight

Get the height of lowercase letters.

int ttfGetXHeight(ttf_t *font);

Parameters

font Font

Return Value

Lowercase letter height in 1000ths

ttfIsFixedPitch

Determine whether a font is fixedpitch.

bool ttfIsFixedPitch(ttf_t *font);

Parameters

font Font

Return Value

true if fixed pitch, false otherwise

Data Types

ttf_cache_t

Font cache

typedef struct _ttf_cache_s ttf_cache_t;

ttf_err_cb_t

Font error callback

typedef void (*ttf_err_cb_t)(void *data, const char *message);

ttf_rect_t

Bounding rectangle

typedef struct ttf_rect_s ttf_rect_t;

ttf_stretch_t

Font stretch

typedef enum ttf_stretch_e ttf_stretch_t;

ttf_style_t

Font style

typedef enum ttf_style_e ttf_style_t;

ttf_t

Font object

typedef struct _ttf_s ttf_t;

ttf_weight_t

Font weight

typedef enum ttf_weight_e ttf_weight_t;

Structures

ttf_rect_s

Bounding rectangle

struct ttf_rect_s {
    float bottom;
    float left;
    float right;
    float top;
};

Members

bottom Bottom offset
left Left offset
right Right offset
top Top offset

Constants

ttf_stretch_e

Font stretch

Constants

TTF_STRETCH_CONDENSED condensed
TTF_STRETCH_EXPANDED expanded
TTF_STRETCH_EXTRA_CONDENSED extra-condensed
TTF_STRETCH_EXTRA_EXPANDED extra-expanded
TTF_STRETCH_NORMAL normal
TTF_STRETCH_SEMI_CONDENSED semi-condensed
TTF_STRETCH_SEMI_EXPANDED semi-expanded
TTF_STRETCH_ULTRA_CONDENSED ultra-condensed
TTF_STRETCH_ULTRA_EXPANDED ultra-expanded
TTF_STRETCH_UNSPEC Unspecified

ttf_style_e

Font style

Constants

TTF_STYLE_ITALIC Italic font
TTF_STYLE_NORMAL Normal font
TTF_STYLE_OBLIQUE Oblique (angled) font
TTF_STYLE_UNSPEC Unspecified

ttf_weight_e

Font weight

Constants

TTF_WEIGHT_100 Weight 100 (Thin)
TTF_WEIGHT_200 Weight 200 (Extra/Ultra-Light)
TTF_WEIGHT_300 Weight 300 (Light)
TTF_WEIGHT_400 Weight 400 (Normal/Regular)
TTF_WEIGHT_500 Weight 500 (Medium)
TTF_WEIGHT_600 Weight 600 (Semi/Demi-Bold)
TTF_WEIGHT_700 Weight 700 (Bold)
TTF_WEIGHT_800 Weight 800 (Extra/Ultra-Bold)
TTF_WEIGHT_900 Weight 900 (Black/Heavy)
TTF_WEIGHT_UNSPEC Unspecified