libsfnt - SFNT (TrueType, OpenType) font parsing library
References
| Branch | Commit | Author | Date | |
|---|---|---|---|---|
| master | Make install rule compatible with bmake | François-Xavier Carton | 2021-10-22 16:15 | |
| Tag | Commit | Author | Date | Tarball |
| 1.0 | Initial release | François-Xavier Carton | 2021-01-30 22:42 | libsfnt-1.0.tar.gz |
| 1.1 | More robust mean (no wrapping possible) | François-Xavier Carton | 2021-04-18 12:11 | libsfnt-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:
- required tables: cmap, head, hhea, hmtx, maxp, name, OS/2, post
- TrueType tables: cvt, fpgm, glyf, loca, prep, gasp
- CFF tables (but not CFF data parsing): CFF, CFF2, VORG
- SVG tables (but not SVG data parsing): SVG
The following tables are not yet implemented, but might be in the future:
- bitmap tables: EBDT, EBLC, EBSC, CBDT, CBLC, sbix
- optional tables: DSIG, hdmx, kern, LTSH, MERG, meta, PCLT, VDMX, vhea, vmtx, COLR, CPAL
The following tables are not supported, and won't be in libsfnt:
- Advanced Typographic Tables (AAT): these are way too complex and would warrant a dedicated library