freetype clib Multimedia

Industry-standard font rendering engine. Load TrueType, OpenType, CFF, and WOFF fonts; render glyphs to anti-aliased bitmaps with full hinting support.

ppm install freetype

What is FreeType?

FreeType is the industry-standard open-source font rendering engine, used in virtually every major computing platform. It loads and decodes font files in every common format — TrueType (TTF), OpenType (OTF), WOFF, Type 1, CFF, PCF, and more — and renders individual glyphs to pixel bitmaps with high-quality anti-aliasing.

At small sizes, FreeType's hinting engine snaps glyph outlines to the pixel grid so characters remain crisp and readable. At larger sizes it produces smooth, sub-pixel-accurate outlines. Three hinting strategies are available: TrueType native bytecode hints (best for screen text), FreeType's own auto-hinter (good for OpenType/CFF), and no hinting (best for large sizes or subpixel rendering).

FreeType powers Android, Chrome, Firefox, the entire Linux desktop stack (GTK, Qt, Pango, Cairo), LibreOffice, SDL2_ttf, and many embedded systems. It is the de-facto standard for anything that draws text.

CLib package — requires libfreetype-dev

Ubuntu/Debian: sudo apt install libfreetype-dev • Fedora/RHEL: sudo dnf install freetype-devel • macOS: brew install freetype

Font Rendering Pipeline

Every glyph goes through the same sequence of steps:

Load font file Select face & size Load glyph Apply hinting Render to bitmap Blit to screen

FreeType uses 26.6 fixed-point format for glyph metrics (multiply by 64 to get real pixels). The advance width tells you how far to move the pen after drawing a glyph; the bearing is the offset from the pen position to the glyph's bounding box.

Types

TFTLibrary / TFTFace / TFTGlyph — handles (Integer)

Opaque integer handles returned by the library. Pass them to every API call. Always free with FreeLibrary / FreeFace.

TFTRenderMode
ValueDescription
rmMono1-bit monochrome bitmap (aliased, sharp edges)
rmNormal8-bit grayscale anti-aliased (most common mode)
rmLCDHorizontal subpixel rendering for LCD screens (R,G,B strips)
rmLCDVVertical subpixel rendering for rotated LCD panels
TFTBitmap
FieldTypeDescription
WidthIntegerWidth in pixels (LCD mode: divide by 3 for visual pixels)
RowsIntegerHeight in pixels
PitchIntegerBytes per row (may be negative for bottom-up bitmaps)
PixelModeTFTPixelModepmMono / pmGray / pmLCD / pmLCDV
DatastringRaw pixel bytes
TFTGlyphMetrics — all values in 26.6 fixed-point (divide by 64 for pixels)
FieldDescription
Width / HeightGlyph bounding box size
HoriBearingX / YLeft and top bearing (horizontal layout)
HoriAdvanceHow far to advance the pen after this glyph
VertBearingX / YBearings for vertical text layout
VertAdvanceAdvance for vertical text layout
TFTFaceInfo
FieldDescription
FamilyName / StyleNamee.g. 'Arial' / 'Bold Italic'
NumGlyphs / NumFacesTotal glyphs; faces in font file (usually 1)
IsScalable / IsFixedWidthVector (TTF/OTF) vs bitmap; monospace flag
HasKerningFont contains a kern table
UnitsPerEMDesign units per Em (typically 2048 for TrueType)
Ascender / Descender / HeightLine metrics in design units
TFTKerning

Fields X and Y (Int64, 26.6 fixed-point): horizontal and vertical kerning adjustment between two adjacent glyphs.

Code Example — Render “Hello” Glyph by Glyph

uses freetype;

var
  Lib     : TFTLibrary;
  Face    : TFTFace;
  Bmp     : TFTBitmap;
  Metrics : TFTGlyphMetrics;
  C       : Char;

begin
  Lib  := InitLibrary;
  Face := NewFace(Lib, '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 0);
  SetPixelSize(Face, 0, 32);  { 0 = auto width, 32 = height in pixels }

  for C in 'Hello' do begin
    LoadChar(Face, Ord(C));       { GetGlyphIndex + LoadGlyph in one call }
    RenderGlyph(Face, rmNormal);  { 8-bit grayscale anti-aliased }
    Bmp     := GetBitmap(Face);
    Metrics := GetMetrics(Face);
    { Bmp.Data holds Bmp.Rows * Bmp.Pitch bytes of grayscale pixel data }
    { Pen advance (pixels) = Metrics.HoriAdvance div 64 }
    { blit Bmp.Data to your screen surface at the current pen position }
  end;

  FreeFace(Face);
  FreeLibrary(Lib);
end.

API Reference

Library

FunctionDescription
InitLibrary(): TFTLibraryInitialize the FreeType library. Call once per process.
FreeLibrary(Lib)Release the library and all associated resources.
FreeTypeVersion(): stringReturn the linked FreeType version string (e.g. "2.13.2").

Face (font file)

FunctionDescription
NewFace(Lib, Path, FaceIndex): TFTFaceLoad a font face from a file path. FaceIndex 0 = first face.
NewFaceMemory(Lib, Data, FaceIndex): TFTFaceLoad a font face from an in-memory byte string.
FreeFace(Face)Release a font face handle.
GetFaceInfo(Face): TFTFaceInfoReturn family name, style, glyph count, line metrics, flags.

Size Selection

FunctionDescription
SetPixelSize(Face, Width, Height)Set rendering size in pixels. Pass 0 for Width to auto-compute from height.
SetCharSize(Face, WidthPt, HeightPt, HorizDPI, VertDPI)Set size in points at a given DPI (typically 72 or 96). Use for print-quality output.

Glyph Rendering

FunctionDescription
GetGlyphIndex(Face, Codepoint): IntegerMap a Unicode codepoint to a glyph index. Returns 0 if not in font.
LoadGlyph(Face, GlyphIndex)Load a glyph by its index into the glyph slot.
LoadChar(Face, Codepoint)Convenience: GetGlyphIndex + LoadGlyph in one call.
RenderGlyph(Face, Mode)Render the loaded glyph to a bitmap using the given TFTRenderMode.
GetBitmap(Face): TFTBitmapReturn the rendered bitmap from the glyph slot.
GetMetrics(Face): TFTGlyphMetricsReturn advance width, bearing, and bounding box (26.6 fixed-point).

Kerning

FunctionDescription
GetKerning(Face, LeftGlyph, RightGlyph): TFTKerningReturn the kerning adjustment (X, Y) between two adjacent glyph indices. Both indices must come from GetGlyphIndex.

Text Metrics & Convenience

FunctionDescription
MeasureText(Face, Text): IntegerReturn the total advance width of a UTF-8 string in pixels.
GetTextBounds(Face, Text; out Width, Height)Return bounding box dimensions of a UTF-8 string.
RenderText(Face, Text): TFTBitmapRender a full UTF-8 string to an 8-bit grayscale bitmap in one call.
RenderTextRGBA(Face, Text, R, G, B): TFTBitmapRender a UTF-8 string to an RGBA bitmap with the given color, ready for direct alpha-blending onto a surface.
FreeType + HarfBuzz

FreeType renders individual glyphs to bitmaps. For complex scripts, ligatures, and right-to-left text you need HarfBuzz alongside it. HarfBuzz runs the shaping algorithm: it decides which glyphs to use (applying ligature substitutions, mark positioning, RTL reordering) and tells you the exact X/Y placement of each glyph. FreeType then renders each glyph at those coordinates. For simple Latin text FreeType alone is sufficient; for Arabic, Devanagari, or full Unicode shaping, use the harfbuzz PPM package together with this one.

Install

ppm install freetype
Requires: libfreetype-dev

Package Info

Version1.0.0
Typeclib
CategoryMultimedia
Native liblibpai_ft.so

API

  • InitLibrary()
  • FreeLibrary(Lib)
  • FreeTypeVersion()
  • NewFace(Lib, Path, Idx)
  • NewFaceMemory(Lib, Data, Idx)
  • FreeFace(Face)
  • GetFaceInfo(Face)
  • SetPixelSize(Face, W, H)
  • SetCharSize(Face, Wpt, Hpt, HDPI, VDPI)
  • GetGlyphIndex(Face, Cp)
  • LoadGlyph(Face, Idx)
  • LoadChar(Face, Cp)
  • RenderGlyph(Face, Mode)
  • GetBitmap(Face)
  • GetMetrics(Face)
  • GetKerning(Face, L, R)
  • MeasureText(Face, Text)
  • GetTextBounds(Face, Text, W, H)
  • RenderText(Face, Text)
  • RenderTextRGBA(Face, Text, R, G, B)
Used everywhere

Powers Android, Chrome, Firefox, Linux desktop (GTK, Qt, Pango, Cairo), LibreOffice, SDL2_ttf, and most embedded systems with a display.

26.6 fixed-point

Divide metric fields by 64 to convert to pixels. E.g. HoriAdvance div 64 = pixel advance.