How does FreeType turn a glyph into editable curves?
Count contours, lines, and quadratic Béziers in A and O from Adobe Source Sans 3, then see how FreeType reconstructs implied endpoints.
Example result Using the same TTF with FreeType 2.15.0, A produces two contours, 12 lines, and four quadratic curves. O produces two contours and 22 quadratic curves.
Turn A and O into artwork nodes
When lettering on a nameplate or stamp becomes a path, the editor can work with its outer edges and inner holes. Here the FreeType project’s font engine reads Adobe’s Source Sans 3 TrueType file. The example counts contours and path segments for A and O. FreeType project home
Walk the points, curve tags, and contour boundaries
FT_Get_Char_Index finds each glyph using the active character map. FT_Load_Glyph with FT_LOAD_NO_SCALE retrieves outlines in font units and disables hinting. FT_Outline_Decompose uses the last-point index of each contour to walk the points and curve tags, calling the application for each emitted segment.
A contour typically begins at an on-curve point. If a TrueType quadratic contour starts with an off-curve control point, the implementation starts at the last point when it is on the curve. When both ends are quadratic control points, it starts at their midpoint. As the loop runs, an on-curve point usually emits line_to. At a quadratic control point, it examines the following point. If that point is another quadratic control point, the midpoint is an implied endpoint and the second control is carried into the next conic_to. This is why some emitted endpoints have no matching index in the stored point array.
A closing curve may return to the start. Otherwise the library emits a line to close the contour. The code also has a cubic_to branch. This example handles two glyphs in isolation. A line of text also needs glyph substitution, positioning, spacing, and direction. Exporting filled artwork requires preserving holes and fill rules and scaling font units to drawing units.
Contour start and move_to · Lines, quadratic curves, and implied endpoints · Cubic curves and contour closure
Build and run locally
Use a terminal on macOS or Linux with curl, tar, CMake 3.12+, and a C++17 toolchain. The static build disables optional dependencies and HVF support outside this TTF example. The font is fetched from a pinned Adobe release. Tested on Apple Silicon macOS. Source Sans 3 3.052R · Font license
Download the C++17 example · Plain-text build commands
Read the example code
#include <iostream>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
struct SegmentCounts {
unsigned contours = 0;
unsigned lines = 0;
unsigned quadratics = 0;
unsigned cubics = 0;
};
int on_move(const FT_Vector*, void* user) {
++static_cast<SegmentCounts*>(user)->contours;
return 0;
}
int on_line(const FT_Vector*, void* user) {
++static_cast<SegmentCounts*>(user)->lines;
return 0;
}
int on_conic(const FT_Vector*, const FT_Vector*, void* user) {
++static_cast<SegmentCounts*>(user)->quadratics;
return 0;
}
int on_cubic(const FT_Vector*, const FT_Vector*, const FT_Vector*, void* user) {
++static_cast<SegmentCounts*>(user)->cubics;
return 0;
}
bool show_outline(FT_Face face, char character) {
const auto glyph = FT_Get_Char_Index(face, character);
if (!glyph || FT_Load_Glyph(face, glyph, FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP))
return false;
if (face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
return false;
FT_Outline& outline = face->glyph->outline;
SegmentCounts counts;
const FT_Outline_Funcs callbacks{on_move, on_line, on_conic, on_cubic, 0, 0};
if (FT_Outline_Decompose(&outline, &callbacks, &counts))
return false;
std::cout << character << ": contours=" << counts.contours
<< ", lines=" << counts.lines << ", quadratic=" << counts.quadratics
<< ", cubic=" << counts.cubics << '\n';
return true;
}
int main(int argc, char** argv) {
if (argc != 2) {
std::cerr << "usage: ./demo SourceSans3-Regular.ttf\n";
return 1;
}
FT_Library library = nullptr;
if (FT_Init_FreeType(&library)) {
std::cerr << "Could not initialize FreeType\n";
return 1;
}
FT_Face face = nullptr;
if (FT_New_Face(library, argv[1], 0, &face)) {
std::cerr << "Could not open font\n";
FT_Done_FreeType(library);
return 1;
}
FT_Int major, minor, patch;
FT_Library_Version(library, &major, &minor, &patch);
std::cout << "FreeType " << major << '.' << minor << '.' << patch << '\n';
const bool ok = show_outline(face, 'A') && show_outline(face, 'O');
FT_Done_Face(face);
FT_Done_FreeType(library);
if (!ok)
std::cerr << "Could not decompose outline\n";
return ok ? 0 : 1;
}mkdir freetype-study && cd freetype-study
curl -fsSL https://codeload.github.com/freetype/freetype/tar.gz/refs/tags/VER-2-15-0 -o freetype.tgz
tar -xzf freetype.tgz
curl -fsSLO https://www.juyaoai.com/examples/freetype-glyph-outlines/demo.cpp
curl -fsSLO https://raw.githubusercontent.com/adobe-fonts/source-sans/3.052R/TTF/SourceSans3-Regular.ttf
cmake -S freetype-VER-2-15-0 -B ft-build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF \
-DFT_DISABLE_ZLIB=ON -DFT_DISABLE_BZIP2=ON -DFT_DISABLE_PNG=ON \
-DFT_DISABLE_HARFBUZZ=ON -DFT_DISABLE_BROTLI=ON -DFT_DISABLE_HVF=ON
cmake --build ft-build --target freetype --parallel 2
c++ -std=c++17 -I ft-build/include -I freetype-VER-2-15-0/include demo.cpp ft-build/libfreetype.a -o demo
./demo SourceSans3-Regular.ttfOutput and a follow-up experiment
FreeType 2.15.0
A: contours=2, lines=12, quadratic=4, cubic=0
O: contours=2, lines=0, quadratic=22, cubic=0Try C in place of O and the same font yields a single contour for C. O has separate loops for its outer edge and the hole. Extend the callbacks to capture coordinates and direction, then convert to drawing units and preview the fill.