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
struct TTFHeader {
uint32_t scalerType;
uint16_t numTables;
uint16_t searchRange;
uint16_t entrySelector;
uint16_t rangeShift;
};
Represents the TTF file header (Offset Table).
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
True if file opened successfully, false otherwise
TTFReader reader;
if (reader.openFont("font.ttf")) {
// Process font
}
close()
Closes the currently open font file.
Header and Table Operations
bool readHeader(TTFHeader& header)
Reads the TTF file header.
Reference to header structure to fill
True if header read successfully
readTableEntry()
bool readTableEntry(TableEntry& entry)
Reads a table directory entry.
Reference to entry structure to fill
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”)
Reference to entry structure to fill
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
True if table found and seeked successfully
Glyph Operations
bool readGlyphHeader(GlyphHeader& header)
Reads a glyph header from the current file position.
Reference to header structure to fill
True if header read successfully
readSimpleGlyph()
bool readSimpleGlyph(SimpleGlyph& glyph)
Reads a complete simple glyph including all outline points.
Reference to glyph structure to fill
True if glyph read successfully
readGlyphByIndex()
bool readGlyphByIndex(int glyphIndex, SimpleGlyph& glyph)
Reads a glyph by its index in the font.
Index of the glyph to read
Reference to glyph structure to fill
True if glyph read successfully
loadLocaTable()
Loads the ‘loca’ table which contains glyph offsets.
True if table loaded successfully
Character Mapping
loadCmapTable()
Loads the ‘cmap’ table for character to glyph mapping.
True if table loaded successfully
getGlyphIndex()
uint16_t getGlyphIndex(uint32_t unicode)
Converts a Unicode character code to a glyph index.
Glyph index for the character
uint16_t glyphIndex = reader.getGlyphIndex('A');
Font Metrics
loadHeadTable()
Loads the ‘head’ table containing font metrics.
True if table loaded successfully
getUnitsPerEm()
uint16_t getUnitsPerEm() const
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
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
Number of points to generate
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
Number of points per curve segment
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.
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();