sdl2-ttf clib Multimedia

SDL2_ttf TrueType & OpenType font rendering. Render text to SDL2 surfaces or GPU textures with antialiasing, styles, glyph metrics, and Unicode support.

ppm install sdl2-ttf

What is SDL2_ttf?

SDL2_ttf renders TrueType and OpenType fonts to SDL2 surfaces using the FreeType engine. It is the standard way to display text in SDL2 games and interactive applications. Fonts are loaded from .ttf or .otf files (or from memory), scaled to any point size, and rendered with configurable quality modes. The resulting surfaces can be used directly with SDL2 or uploaded to the GPU as textures for efficient rendering.

Full UTF-8 is supported — including Arabic, CJK, Devanagari, and emoji. For complex shaping (ligatures, bidirectional text), combine with HarfBuzz.

Render Quality Modes

ModeDescriptionBest For
SolidFastest. Single foreground color, no antialiasing. Palette surface.HUDs, game scores, real-time counters
ShadedAntialiased with opaque background box. Cleaner than Solid.Subtitles, console text, fixed backgrounds
BlendedBest quality. Full alpha-blended antialiasing. ARGB surface.UI labels, menus, general text
LCDSub-pixel rendering for crisp text on LCD screens.Desktop apps, readability on LCD

Types

All handle types are opaque Integer values backed by the native C library.

TypeDescription
TTTFFontFont handle returned by OpenFont. Must be closed with CloseFont.
TSDLSurfaceSDL_Surface handle. Pixel data in CPU memory.
TSDLTextureSDL_Texture handle. Pixel data on GPU memory.
TSDLRendererSDL_Renderer handle. Required for RenderTextTexture.
TSDLColorPacked RGBA color value. Created with MakeColor(R,G,B,A).

TTTFHinting

ValueDescription
thNormal = 0Default FreeType hinting. Good balance.
thLight = 1Lighter hinting, softer appearance.
thMono = 2Monochrome hinting for crisp 1-bit rendering.
thNone = 3No hinting. Pure glyph outlines.
thLightSub = 4Light hinting with sub-pixel positioning.

TTTFStyle — combinable flags

ValueHexDescription
tsNormal$00No style applied.
tsBold$01Bold weight (synthesized if font has no bold face).
tsItalic$02Italic slant (synthesized if font has no italic face).
tsUnderline$04Underline decoration.
tsStrikethrough$08Strikethrough decoration.

TTTFGlyphMetrics record

FieldDescription
MinXLeft bearing — distance from pen to leftmost pixel.
MaxXRightmost pixel position.
MinYBottom bearing — distance from baseline to bottommost pixel.
MaxYTopmost pixel position.
AdvanceHorizontal advance — how far the pen moves after this glyph.

API Reference

Init

FunctionDescription
Init: BooleanInitialize SDL2_ttf. Must be called before any other TTF function. Returns True on success.
QuitShutdown SDL2_ttf and free all internal resources.
Version: stringReturns the linked SDL2_ttf version string.

Open / Close Font

FunctionDescription
OpenFont(Path, PointSize): TTTFFontOpen a TrueType font at a given point size (72 DPI). Returns a font handle.
OpenFontIndex(Path, PointSize, FaceIndex): TTTFFontOpen a specific face inside a .ttc font collection.
OpenFontFromMemory(Data, PointSize): TTTFFontLoad a font from an in-memory byte string. Useful for embedded assets.
CloseFont(Font)Close the font and release all memory associated with it.

Font Properties

FunctionDescription
SetStyle(Font, Style)Set style flags. Combine with or: tsBold or tsItalic.
GetStyle(Font): IntegerReturn current style bitmask.
SetHinting(Font, Hinting)Set FreeType hinting mode (TTTFHinting enum).
SetOutline(Font, Outline)Set outline stroke width in pixels. 0 = no outline.
SetKerning(Font, Enable)Enable or disable kerning. Kerning is on by default.
GetHeight(Font): IntegerMaximum glyph height in pixels.
GetAscent(Font): IntegerPositive pixels above the baseline.
GetDescent(Font): IntegerNegative pixels below the baseline.
GetLineSkip(Font): IntegerRecommended line spacing in pixels.
GetFamilyName(Font): stringFont family name (e.g. 'DejaVu Sans').
GetStyleName(Font): stringFace style name (e.g. 'Bold Italic').

Text Measurement

FunctionDescription
SizeText(Font, Text, out W, H)Measure pixel dimensions of a string without rendering it.
MeasureText(Font, Text, MaxWidth, out FitChars, ActualWidth)Find how many characters fit within MaxWidth pixels and the actual rendered width.
GlyphMetrics(Font, Codepoint): TTTFGlyphMetricsReturn per-glyph bearing and advance for a Unicode codepoint.
GlyphProvided(Font, Codepoint): BooleanCheck if the font contains a glyph for the given codepoint.

Render Text → Surface

FunctionDescription
RenderSolid(Font, Text, FgColor): TSDLSurfaceSolid render: fast, no antialiasing. Best for HUDs and scores.
RenderShaded(Font, Text, FgColor, BgColor): TSDLSurfaceAntialiased with opaque background box. Good for subtitles.
RenderBlended(Font, Text, FgColor): TSDLSurfaceBest quality: full alpha-blended antialiasing.
RenderBlendedWrapped(Font, Text, FgColor, WrapWidth): TSDLSurfaceBlended render with automatic line wrapping at WrapWidth pixels.
RenderLCD(Font, Text, FgColor, BgColor): TSDLSurfaceSub-pixel LCD rendering for crisp text on screens.

Render Text → Texture

FunctionDescription
RenderTextTexture(Renderer, Font, Text, FgColor): TSDLTextureRender blended text directly to a GPU SDL_Texture. Skips the surface step for convenience.

Color & Error

FunctionDescription
MakeColor(R, G, B, A): TSDLColorPack RGBA components (0–255) into a single TSDLColor value.
GetError: stringReturn the last SDL2_ttf error message string.

Code Example

uses sdl2_ttf;

var
  Font   : TTTFFont;
  Surface: TSDLSurface;
  Tex    : TSDLTexture;
  W, H   : Integer;
  White  : TSDLColor;

begin
  Init;
  Font := OpenFont('assets/DejaVuSans.ttf', 24);
  SetStyle(Font, tsBold);
  White := MakeColor(255, 255, 255, 255);

  { Measure before rendering }
  SizeText(Font, 'Score: 1234', W, H);
  WriteLn('Text size: ', W, 'x', H, ' px');

  { Best quality render }
  Surface := RenderBlended(Font, 'Hello World!', White);

  { Convert to GPU texture }
  Tex := RenderTextTexture(Renderer, Font, 'FPS: 60', White);

  { Wrapped text }
  Surface := RenderBlendedWrapped(Font, LongParagraph, White, 400);

  CloseFont(Font);
  Quit;
end.
Combining Style Flags

Combine style flags with or: SetStyle(Font, tsBold or tsItalic). SDL2_ttf synthesizes bold and italic algorithmically even when the font file does not contain a native bold or italic face. Results vary by font — use a font family with real bold/italic faces when quality matters.

Performance Tip

Cache rendered text as surfaces or textures. Re-rendering every frame is expensive. Only call RenderBlended or RenderTextTexture when the text content actually changes — store the result and reuse it each frame until the text updates.

Install

ppm install sdl2-ttf

Requires libsdl2-ttf-dev

sudo apt install libsdl2-ttf-dev

Package Info

Version1.0.0
Typeclib
CategoryMultimedia
Native liblibpai_sdl2ttf.so
Requires SDL2 + FreeType

API

  • Init()
  • Quit()
  • Version()
  • OpenFont(path, size)
  • OpenFontIndex(path, size, idx)
  • OpenFontFromMemory(data, size)
  • CloseFont(font)
  • SetStyle(font, style)
  • GetStyle(font)
  • SetHinting(font, hint)
  • SetOutline(font, px)
  • SetKerning(font, bool)
  • GetHeight(font)
  • GetAscent(font)
  • GetDescent(font)
  • GetLineSkip(font)
  • GetFamilyName(font)
  • GetStyleName(font)
  • SizeText(font, text, W, H)
  • MeasureText(font, text, max, ...)
  • GlyphMetrics(font, codepoint)
  • GlyphProvided(font, codepoint)
  • RenderSolid(font, text, fg)
  • RenderShaded(font, text, fg, bg)
  • RenderBlended(font, text, fg)
  • RenderBlendedWrapped(font, text, fg, w)
  • RenderLCD(font, text, fg, bg)
  • RenderTextTexture(rnd, font, text, fg)
  • MakeColor(r, g, b, a)
  • GetError()