Commit3e9bd29cRecorded2 Mar 2026Repositorysigil-cairo

Add glyph-level text rendering to Cairo bindings

Message

Add cairo-show-glyphs for rendering positioned glyphs from HarfBuzz shaping output. Add cairo-ft-font-face-create for creating Cairo font faces from FreeType faces, cairo-set-font-face, cairo-font-face-destroy, and cairo-glyph-layout struct. Enables the full FreeType + HarfBuzz + Cairo text rendering pipeline.

Changed
 src/cairo.sgl | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)
Diff
src/cairo.sglmodified
@@ -173,6 +173,13 @@
173
cairo-text-extents
174
cairo-font-extents
175
+176
;; Glyph rendering
+177
cairo-glyph-layout
+178
cairo-show-glyphs
+179
cairo-ft-font-face-create
+180
cairo-set-font-face
+181
cairo-font-face-destroy
+182
183
;; Transforms
184
cairo-translate
185
cairo-scale
@@ -317,6 +324,13 @@
324
x0: ffi/double
325
y0: ffi/double))
326
+327
;; cairo_glyph_t — for glyph-level text rendering
+328
(define cairo-glyph-layout
+329
(c-struct-layout
+330
index: ffi/ulong
+331
x: ffi/double
+332
y: ffi/double))
+333
334
;; ============================================================
335
;; Finalizer Pointers (looked up once)
336
;; ============================================================
@@ -324,6 +338,7 @@
338
(define %cairo-destroy-ptr (c-symbol cairo-lib "cairo_destroy"))
339
(define %surface-destroy-ptr (c-symbol cairo-lib "cairo_surface_destroy"))
340
(define %pattern-destroy-ptr (c-symbol cairo-lib "cairo_pattern_destroy"))
+341
(define %font-face-destroy-ptr (c-symbol cairo-lib "cairo_font_face_destroy"))
342
343
;; ============================================================
344
;; Raw Function Bindings
@@ -600,6 +615,23 @@
615
(c-function cairo-lib "cairo_font_extents"
616
(list ffi/pointer ffi/pointer) ffi/void))
617
+618
;; Glyph rendering
+619
(define %show-glyphs
+620
(c-function cairo-lib "cairo_show_glyphs"
+621
(list ffi/pointer ffi/pointer ffi/int) ffi/void))
+622
+623
(define %set-font-face
+624
(c-function cairo-lib "cairo_set_font_face"
+625
(list ffi/pointer ffi/pointer) ffi/void))
+626
+627
(define %font-face-destroy
+628
(c-function cairo-lib "cairo_font_face_destroy"
+629
(list ffi/pointer) ffi/void))
+630
+631
(define %ft-font-face-create
+632
(c-function cairo-lib "cairo_ft_font_face_create_for_ft_face"
+633
(list ffi/pointer ffi/int) ffi/pointer))
+634
635
;; Transforms
636
(define %translate
637
(c-function cairo-lib "cairo_translate"
@@ -1035,6 +1067,50 @@
1067
(%font-extents cr extents)
1068
(c-struct->dict extents cairo-font-extents-layout))))
1069
+1070
;; ============================================================
+1071
;; Public API — Glyph Rendering
+1072
;; ============================================================
+1073
+1074
;;; Render glyphs at specified positions on a Cairo context.
+1075
;;;
+1076
;;; GLYPHS is a list of dicts with keys: index: (glyph index),
+1077
;;; x: (x position), y: (y position).
+1078
(define (cairo-show-glyphs cr glyphs)
+1079
(: pointer? list? -> void?)
+1080
(let* ((n (length glyphs))
+1081
(glyph-size (c-struct-size cairo-glyph-layout))
+1082
(buf (c-alloc (* n glyph-size))))
+1083
(let loop ((i 0) (gs glyphs))
+1084
(when (pair? gs)
+1085
(let ((g (car gs))
+1086
(ptr (pointer+ buf (* i glyph-size))))
+1087
(c-struct-set! ptr cairo-glyph-layout index: (dict-ref g index:))
+1088
(c-struct-set! ptr cairo-glyph-layout x: (dict-ref g x:))
+1089
(c-struct-set! ptr cairo-glyph-layout y: (dict-ref g y:))
+1090
(loop (+ i 1) (cdr gs)))))
+1091
(%show-glyphs cr buf n)
+1092
(c-free buf)))
+1093
+1094
;;; Create a Cairo font face from a FreeType face.
+1095
;;;
+1096
;;; Requires Cairo to be built with FreeType support.
+1097
(define (cairo-ft-font-face-create ft-face load-flags)
+1098
(: pointer? integer? -> pointer?)
+1099
(let ((face (%ft-font-face-create ft-face load-flags)))
+1100
(set-pointer-finalizer! face %font-face-destroy-ptr)
+1101
face))
+1102
+1103
;;; Set the font face for a Cairo context.
+1104
(define (cairo-set-font-face cr face)
+1105
(: pointer? pointer? -> void?)
+1106
(%set-font-face cr face))
+1107
+1108
;;; Destroy a Cairo font face.
+1109
(define (cairo-font-face-destroy face)
+1110
(: pointer? -> void?)
+1111
(set-pointer-finalizer! face #f)
+1112
(%font-face-destroy face))
+1113
1114
;; ============================================================
1115
;; Public API — Transforms
1116
;; ============================================================