Skip to main content
The TTFReader class provides low-level parsing and reading of TrueType font (.ttf) files. It extracts glyph outlines, font metrics, and character mappings from TTF files.

Structures

TTFHeader

struct TTFHeader {
    uint32_t scalerType;
    uint16_t numTables;
    uint16_t searchRange;
    uint16_t entrySelector;
    uint16_t rangeShift;
};
Represents the TTF file header (Offset Table).

GlyphHeader

struct GlyphHeader {
    int16_t numberOfContours;
    int16_t xMin, yMin, xMax, yMax;
};
Contains metadata for a single glyph.

TTFPoint

struct TTFPoint {
    int16_t x, y;
    bool onCurve;
};
Represents a point in a glyph outline.

SimpleGlyph

struct SimpleGlyph {
    GlyphHeader header;
    std::vector<uint16_t> endPtsOfContours;
    std::vector<TTFPoint> points;
};
Complete simple glyph data including outline points.

BezierPoint

struct BezierPoint {
    float x, y;
    BezierPoint(float x = 0, float y = 0);
};
Floating-point coordinate for Bezier curve calculations.

TableEntry

struct TableEntry {
    char tag[5];
    uint32_t checksum;
    uint32_t offset;
    uint32_t length;
};
Represents a table directory entry in the TTF file.

Constructor & Destructor

TTFReader();
~TTFReader();

File Operations

openFont()

bool openFont(const std::string& filename)
Opens a TrueType font file for reading.
filename
const std::string&
required
Path to the .ttf font file
return
bool
True if file opened successfully, false otherwise
TTFReader reader;
if (reader.openFont("font.ttf")) {
    // Process font
}

close()

void close()
Closes the currently open font file.

Header and Table Operations

readHeader()

bool readHeader(TTFHeader& header)
Reads the TTF file header.
header
TTFHeader&
required
Reference to header structure to fill
return
bool
True if header read successfully

readTableEntry()

bool readTableEntry(TableEntry& entry)
Reads a table directory entry.
entry
TableEntry&
required
Reference to entry structure to fill
return
bool
True if entry read successfully

findTable()

bool findTable(const std::string& tableName, TableEntry& entry)
Finds a specific table in the TTF file.
tableName
const std::string&
required
Name of the table (e.g., “glyf”, “cmap”, “head”)
entry
TableEntry&
required
Reference to entry structure to fill
return
bool
True if table found

seekToTable()

bool seekToTable(const std::string& tableName)
Seeks to the beginning of a specific table.
tableName
const std::string&
required
Name of the table to seek to
return
bool
True if table found and seeked successfully

Glyph Operations

readGlyphHeader()

bool readGlyphHeader(GlyphHeader& header)
Reads a glyph header from the current file position.
header
GlyphHeader&
required
Reference to header structure to fill
return
bool
True if header read successfully

readSimpleGlyph()

bool readSimpleGlyph(SimpleGlyph& glyph)
Reads a complete simple glyph including all outline points.
glyph
SimpleGlyph&
required
Reference to glyph structure to fill
return
bool
True if glyph read successfully

readGlyphByIndex()

bool readGlyphByIndex(int glyphIndex, SimpleGlyph& glyph)
Reads a glyph by its index in the font.
glyphIndex
int
required
Index of the glyph to read
glyph
SimpleGlyph&
required
Reference to glyph structure to fill
return
bool
True if glyph read successfully

loadLocaTable()

bool loadLocaTable()
Loads the ‘loca’ table which contains glyph offsets.
return
bool
True if table loaded successfully

Character Mapping

loadCmapTable()

bool loadCmapTable()
Loads the ‘cmap’ table for character to glyph mapping.
return
bool
True if table loaded successfully

getGlyphIndex()

uint16_t getGlyphIndex(uint32_t unicode)
Converts a Unicode character code to a glyph index.
unicode
uint32_t
required
Unicode code point
return
uint16_t
Glyph index for the character
uint16_t glyphIndex = reader.getGlyphIndex('A');

Font Metrics

loadHeadTable()

bool loadHeadTable()
Loads the ‘head’ table containing font metrics.
return
bool
True if table loaded successfully

getUnitsPerEm()

uint16_t getUnitsPerEm() const
return
uint16_t
Number of font units per em square

Bezier Curve Operations

quadraticBezier()

BezierPoint quadraticBezier(const BezierPoint& start, const BezierPoint& control, 
                           const BezierPoint& end, float t)
Calculates a point on a quadratic Bezier curve.
start
const BezierPoint&
required
Start point
control
const BezierPoint&
required
Control point
end
const BezierPoint&
required
End point
t
float
required
Parameter (0.0 to 1.0)
return
BezierPoint
Point on the curve at parameter t

generateBezierCurve()

std::vector<BezierPoint> generateBezierCurve(const BezierPoint& start, 
                                             const BezierPoint& control,
                                             const BezierPoint& end, 
                                             int resolution = 20)
Generates points along a quadratic Bezier curve.
start
const BezierPoint&
required
Start point
control
const BezierPoint&
required
Control point
end
const BezierPoint&
required
End point
resolution
int
default:"20"
Number of points to generate
return
std::vector<BezierPoint>
Vector of points along the curve

generateGlyphOutline()

std::vector<BezierPoint> generateGlyphOutline(const SimpleGlyph& glyph, int resolution = 20)
Generates the complete outline of a glyph as a series of points.
glyph
const SimpleGlyph&
required
Glyph to generate outline for
resolution
int
default:"20"
Number of points per curve segment
return
std::vector<BezierPoint>
Vector of points forming the complete glyph outline

Utility Methods

readBytes()

std::vector<uint8_t> readBytes(size_t count)
Reads raw bytes from the current file position.
count
size_t
required
Number of bytes to read
return
std::vector<uint8_t>
Vector of bytes read

Example Usage

TTFReader reader;
if (!reader.openFont("font.ttf")) {
    return;
}

// Load necessary tables
reader.loadHeadTable();
reader.loadLocaTable();
reader.loadCmapTable();

// Get glyph for character 'A'
uint16_t glyphIndex = reader.getGlyphIndex('A');

// Read the glyph
SimpleGlyph glyph;
if (reader.readGlyphByIndex(glyphIndex, glyph)) {
    // Generate outline points
    auto outline = reader.generateGlyphOutline(glyph, 30);
    
    // Use outline points for rendering
    for (const auto& point : outline) {
        // Render point...
    }
}

reader.close();

Build docs developers (and LLMs) love