Michael R Sweet
Copyright © 2018-2024 by Michael R Sweet
TTF is a simple C library for using TrueType and OpenType font files.
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`
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_data*/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.
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_data);
filename | Filename |
---|---|
idx | Font number to create in collection (0-based) |
err_cb | Error callback or NULL to log to stderr |
err_data | Error callback data |
New font object
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_data" 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_data, const char *message) { fprintf(stderr, "ERROR: %sn", message); }
Free all memory used for a font family object.
void ttfDelete(ttf_t *font);
font | Font |
---|
Get the maximum height of non-accented characters.
int ttfGetAscent(ttf_t *font);
font | Font |
---|
Ascent in 1000ths
Get the bounds of all characters in a font.
ttf_rect_t *ttfGetBounds(ttf_t *font, ttf_rect_t *bounds);
font | Font |
---|---|
bounds | Bounds buffer |
Bounds or NULL
on error
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.
Get the Unicode to glyph mapping table.
const int *ttfGetCMap(ttf_t *font, size_t *num_cmap);
font | Font |
---|---|
num_cmap | Number of entries in table |
CMap table
Get the height of capital letters.
int ttfGetCapHeight(ttf_t *font);
font | Font |
---|
Capital letter height in 1000ths
Get the copyright text for a font.
const char *ttfGetCopyright(ttf_t *font);
font | Font |
---|
Copyright text
Get the maximum depth of non-accented characters.
int ttfGetDescent(ttf_t *font);
font | Font |
---|
Descent in 1000ths
Get the extents of a UTF-8 string.
ttf_rect_t *ttfGetExtents(ttf_t *font, float size, const char *s, ttf_rect_t *extents);
font | Font |
---|---|
size | Font size |
s | String |
extents | Extents of the string |
Pointer to extents or NULL
on error
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.
Get the family name of a font.
const char *ttfGetFamily(ttf_t *font);
font | Font |
---|
Family name
Get the italic angle.
float ttfGetItalicAngle(ttf_t *font);
font | Font |
---|
Angle in degrees
Get the last character in the font.
int ttfGetMaxChar(ttf_t *font);
font | Font |
---|
Last character in font
Get the first character in the font.
int ttfGetMinChar(ttf_t *font);
font | Font |
---|
First character in font
Get the number of fonts in this collection.
size_t ttfGetNumFonts(ttf_t *font);
font | Font |
---|
Number of fonts
Get the PostScript name of a font.
const char *ttfGetPostScriptName(ttf_t *font);
font | Font |
---|
PostScript name
Get the font "stretch" value...
ttf_stretch_t ttfGetStretch(ttf_t *font);
font | Font |
---|
Stretch value
Get the font style.
ttf_style_t ttfGetStyle(ttf_t *font);
font | Font |
---|
Style
Get the version number of a font.
const char *ttfGetVersion(ttf_t *font);
font | Font |
---|
Version number
Get the weight of a font.
ttf_weight_t ttfGetWeight(ttf_t *font);
font | Font |
---|
Weight
Get the width of a single character.
int ttfGetWidth(ttf_t *font, int ch);
font | Font |
---|---|
ch | Unicode character |
Width in 1000ths
Get the height of lowercase letters.
int ttfGetXHeight(ttf_t *font);
font | Font |
---|
Lowercase letter height in 1000ths
Determine whether a font is fixedpitch.
bool ttfIsFixedPitch(ttf_t *font);
font | Font |
---|
true
if fixed pitch, false
otherwise
Font error callback
typedef void (*ttf_err_cb_t)(void *data, const char *message);
Bounding rectangle
typedef struct ttf_rect_s ttf_rect_t;
Font stretch
typedef enum ttf_stretch_e ttf_stretch_t;
Font style
typedef enum ttf_style_e ttf_style_t;
Font object
typedef struct _ttf_s ttf_t;
Font variant
typedef enum ttf_variant_e ttf_variant_t;
Font weight
typedef enum ttf_weight_e ttf_weight_t;
Bounding rectangle
struct ttf_rect_s {
float bottom;
float left;
float right;
float top;
};
bottom | Bottom offset |
---|---|
left | Left offset |
right | Right offset |
top | Top offset |
Font stretch
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 |
Font style
TTF_STYLE_ITALIC | Italic font |
---|---|
TTF_STYLE_NORMAL | Normal font |
TTF_STYLE_OBLIQUE | Oblique (angled) font |
Font variant
TTF_VARIANT_NORMAL | Normal font |
---|---|
TTF_VARIANT_SMALL_CAPS | Font whose lowercase letters are small capitals |
Font weight
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) |