12 TrueType Fonts
This section covers TrueType font loading and text rendering using SDL_ttf.
12.1 Font Management
procedure
path : (or/c string? path?) size : real? cust : custodian? = (current-custodian)
size is the point size of the font.
(define font (open-font "/path/to/font.ttf" 24))
procedure
(open-font-io source size [ #:close? close? #:custodian cust]) → font? source : (or/c bytes? input-port? cpointer?) size : real? close? : boolean? = #t cust : custodian? = (current-custodian)
When close? is #t, SDL takes ownership of the stream.
procedure
(close-font! f) → void?
f : font?
Note: Fonts are automatically closed when their custodian shuts down.
procedure
f : font? cust : custodian? = (current-custodian)
12.2 Font Properties
12.2.1 Metrics
procedure
(font-height f) → exact-integer?
f : font?
This is the recommended line spacing for text.
procedure
(font-ascent f) → exact-integer?
f : font?
procedure
(font-descent f) → exact-integer?
f : font?
Note: This is typically a negative value.
procedure
(font-line-skip f) → exact-integer?
f : font?
12.2.2 Style
procedure
(font-style f) → (listof symbol?)
f : font?
Possible values: 'normal, 'bold, 'italic, 'underline, 'strikethrough.
procedure
(set-font-style! f style ...) → void?
f : font? style : symbol?
(set-font-style! font 'bold) (set-font-style! font 'bold 'italic) (set-font-style! font 'normal) ; Reset to normal
procedure
(font-outline f) → exact-integer?
f : font?
procedure
(set-font-outline! f pixels) → void?
f : font? pixels : exact-integer?
procedure
(font-hinting f) → symbol?
f : font?
Values: 'normal, 'light, 'mono, 'none, 'light-subpixel.
procedure
(set-font-hinting! f mode) → void?
f : font? mode : symbol?
12.2.3 Other Properties
procedure
(font-weight f) → exact-integer?
f : font?
procedure
(font-family-name f) → (or/c string? #f)
f : font?
procedure
(font-style-name f) → (or/c string? #f)
f : font?
procedure
(font-fixed-width? f) → boolean?
f : font?
procedure
(font-scalable? f) → boolean?
f : font?
procedure
(font-kerning? f) → boolean?
f : font?
procedure
(set-font-kerning! f enabled?) → void?
f : font? enabled? : boolean?
procedure
(set-font-size! f size [ #:hdpi hdpi #:vdpi vdpi]) → void? f : font? size : real? hdpi : (or/c exact-nonnegative-integer? #f) = #f vdpi : (or/c exact-nonnegative-integer? #f) = #f
Optional DPI parameters allow for high-DPI rendering.
12.3 Text Measurement
procedure
(text-size f text) →
exact-integer? exact-integer? f : font? text : string?
(define-values (w h) (text-size font "Hello, World!"))
procedure
(text-size-wrapped f text wrap-width)
→
exact-integer? exact-integer? f : font? text : string? wrap-width : exact-nonnegative-integer?
procedure
(measure-text f text max-width)
→
exact-integer? exact-nonnegative-integer? f : font? text : string? max-width : exact-nonnegative-integer?
Returns the actual width used and the number of characters that fit.
12.4 Text Rendering
procedure
(draw-text! renderer font text x y color [ #:mode mode #:custodian cust]) → void? renderer : renderer? font : font? text : string? x : real? y : real?
color :
(or/c (list/c byte? byte? byte?) (list/c byte? byte? byte? byte?)) mode : (or/c 'solid 'blended) = 'blended cust : custodian? = (current-custodian)
This is a convenience function that renders text to a temporary texture and draws it immediately.
'solid —
Fast but rough edges 'blended —
High quality with anti-aliasing (default)
(draw-text! ren font "Hello, World!" 100 100 '(255 255 255)) (draw-text! ren font "Score: 42" 10 10 '(255 255 0))
procedure
(render-text font text color #:renderer renderer [ #:mode mode #:custodian cust]) → (or/c texture? #f) font : font? text : string?
color :
(or/c (list/c byte? byte? byte?) (list/c byte? byte? byte? byte?)) renderer : renderer? mode : (or/c 'solid 'blended) = 'blended cust : custodian? = (current-custodian)
Returns #f for empty text or if the text is too large for a texture.
This is useful when you want to render the same text multiple times without re-rasterizing it each frame.
(define score-label (render-text font "Score:" '(255 255 255) #:renderer ren)) ;; Use the texture multiple times (render-texture! ren score-label 10 10)
12.5 Text Direction and Alignment
For languages with different text directions (RTL, vertical), these functions configure text layout.
procedure
(font-direction f) → symbol?
f : font?
Values: 'ltr (left-to-right), 'rtl (right-to-left), 'ttb (top-to-bottom), 'btt (bottom-to-top).
procedure
(set-font-direction! f direction) → void?
f : font? direction : symbol?
Note: Requires HarfBuzz support. May fail on some builds.
procedure
(font-wrap-alignment f) → symbol?
f : font?
Values: 'left, 'center, 'right.
procedure
(set-font-wrap-alignment! f alignment) → void?
f : font? alignment : symbol?
12.6 Fallback Fonts
Fallback fonts provide glyphs for characters not present in the primary font.
procedure
(add-fallback-font! f fallback) → void?
f : font? fallback : font?
When the primary font doesn’t have a glyph, SDL_ttf will try the fallback.
procedure
(remove-fallback-font! f fallback) → void?
f : font? fallback : font?
procedure
(clear-fallback-fonts! f) → void?
f : font?
12.7 Glyph Operations
procedure
(font-has-glyph? f ch) → boolean?
f : font? ch : (or/c char? exact-nonnegative-integer?)
procedure
(glyph-metrics f ch) →
exact-integer? exact-integer? exact-integer? exact-integer? exact-integer? f : font? ch : (or/c char? exact-nonnegative-integer?)
Returns: (values min-x max-x min-y max-y advance).
procedure
(glyph-kerning f prev-ch ch) → exact-integer?
f : font? prev-ch : (or/c char? exact-nonnegative-integer?) ch : (or/c char? exact-nonnegative-integer?)
12.8 Advanced: Text Objects
For complex text layouts and efficient updates, SDL_ttf provides text objects that can be modified and re-rendered efficiently.
procedure
(make-text font text [ #:engine engine #:custodian cust]) → text? font : font? text : string?
engine :
(or/c renderer-text-engine? surface-text-engine? gpu-text-engine? #f) = #f cust : custodian? = (current-custodian)
procedure
(text-set-string! t text) → void?
t : text? text : string?
procedure
(text-append-string! t text) → void?
t : text? text : string?
procedure
(text-insert-string! t offset text) → void?
t : text? offset : exact-nonnegative-integer? text : string?
procedure
(text-delete-string! t offset length) → void?
t : text? offset : exact-nonnegative-integer? length : exact-nonnegative-integer?
procedure
(text-object-size t) →
exact-integer? exact-integer? t : text?
procedure
(draw-renderer-text! t x y) → void?
t : text? x : real? y : real?
12.9 Text Engines
Text engines manage glyph caching and rendering. They’re needed for advanced text object rendering.
procedure
(make-renderer-text-engine renderer [ #:custodian cust]) → renderer-text-engine? renderer : renderer? cust : custodian? = (current-custodian)
procedure
v : any/c
procedure
(make-surface-text-engine [#:custodian cust])
→ surface-text-engine? cust : custodian? = (current-custodian)
procedure
(surface-text-engine? v) → boolean?
v : any/c
12.10 SDF Rendering
Signed Distance Field rendering allows for high-quality text at any scale.
procedure
(set-font-sdf! f enabled?) → void?
f : font? enabled? : boolean?
12.11 Version Information
procedure
(ttf-version) →
exact-nonnegative-integer? exact-nonnegative-integer? exact-nonnegative-integer?
procedure
(freetype-version) →
exact-nonnegative-integer? exact-nonnegative-integer? exact-nonnegative-integer?
procedure
(harfbuzz-version) →
(or/c exact-nonnegative-integer? #f) (or/c exact-nonnegative-integer? #f) (or/c exact-nonnegative-integer? #f)
Returns (values #f #f #f) if HarfBuzz is not available.