pedantic.software repositories

libsfnt - SFNT (TrueType, OpenType) font parsing library

References

BranchCommitAuthorDate
masterMake install rule compatible with bmakeFrançois-Xavier Carton2021-10-22 16:15
 
TagCommitAuthorDateTarball
1.0Initial releaseFrançois-Xavier Carton2021-01-30 22:42libsfnt-1.0.tar.gz
1.1More robust mean (no wrapping possible)François-Xavier Carton2021-04-18 12:11libsfnt-1.1.tar.gz
 

libsfnt - a SFNT (TrueType, OpenType) font parsing library

Description

libsfnt is a library providing function to read SFNT files.

SFNT files are composed of several sections called tables. To read a SFNT file, the font directory (list of tables) needs to be read. This allows finding the tables that needs to be parsed. Then, each table can be parsed, either by using libsfnt directly, if it has support for it, or by using the low level functions to read SFNT data types.

Motivation

The motivation is to provide a library that only provides font parsing, without doing any processing. Any processing should be done in separate libraries, as different applications may exists (not only rendering).

Conventions

Most functions return either an int, or a pointer. The convention is that 0 (or NULL) is returned on error, while non-zero is returned on success.

A couple of functions that cannot fail return either void or the requested value directly.

When the library allocates structures, there will be a corresponding function to free the result. And when in doubt, well, read the source code :)

Standard

The library implements the ISO/IEC 14496-22 standard: Information technology - Coding of audio-visual objects - Part 22: Open Font Format.

This standard is publicly available here (requires to accept a licence agreement): https://standards.iso.org/ittf/PubliclyAvailableStandards/index.html

More specifically, the fourth edition was used to implement the library (ISO/IEC 14496-22:2019). When in doubt, the Apple and Microsoft documentation was also consulted.

Consulting these references is the best way to obtain information on the tables.

Usage

Reading the font directory

First, to be able to do anything, read the list of tables (font directory):

#include <stdio.h>
#include <sfnt/font_directory.h>

FILE* f;
struct SFNT_FontDirectory fontdir;
int hasfontdir = 0;

if (!(f = fopen("/path/to/font.ttf", "rb"))) {
    perror("failed to read font file");
} else if (!(hasfontdir = sfnt_read_font_directory(f, &fontdir))) {
    fputs("Error: failed to read font directory\n", stderr);
} else {
    /* do something (read tables, etc...) */
}

if (f) fclose(f);
if (hasfontdir) sfnt_free_font_directory(&fontdir);

Reading tables

libsfnt provides support for most tables. For these, the corresponding functions can be used. Example, to read the cmap table:

#include <sfnt/table/cmap.h>
struct SFNT_TableCmap cmaptbl;

if (sfnt_read_cmap(&fontdir, f, &cmaptbl)) {
    /* do something with cmaptbl */
    sfnt_free_cmap(&cmaptbl);
}

In case the table is not explicitely supported in libsfnt, tables can be parsed by reading each member separately. The functions in <sfnt/basic_types.h> can be used to that effect. The reader is invited to use the existing code for supported tables as a model.

Font collections (.ttc files)

There is support for reading font collections. These files are like regular SFNT files, except that they start with a specific header listing the fonts contained in the file. There are font directories for each font, which can be read as for regular SFNT files. The offsets are with respect to the whole files, meaning that no special treatment is needed.

To read font collections, include the <sfnt/font_collection.h> header, and use the provided functions.

Tables

The following groups of table are implemented:

The following tables are not yet implemented, but might be in the future:

The following tables are not supported, and won't be in libsfnt: