Commit866204d6Recorded3 Mar 2026Repositorysigil-cairo

Add cairo-get-matrix and cairo-set-matrix bindings

Message

Needed for text rendering at non-1x canvas scales, where the CTM must be inspected and modified to keep HarfBuzz shaping in sync with Cairo glyph rendering.

Changed
 src/cairo.sgl | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
Diff
src/cairo.sglmodified
@@ -186,6 +186,8 @@
186
cairo-scale
187
cairo-rotate
188
cairo-identity-matrix
+189
cairo-get-matrix
+190
cairo-set-matrix
191
192
;; Patterns
193
cairo-pattern-create-rgb
@@ -654,6 +656,14 @@
656
(c-function cairo-lib "cairo_identity_matrix"
657
(list ffi/pointer) ffi/void))
658
+659
(define %get-matrix
+660
(c-function cairo-lib "cairo_get_matrix"
+661
(list ffi/pointer ffi/pointer) ffi/void))
+662
+663
(define %set-matrix
+664
(c-function cairo-lib "cairo_set_matrix"
+665
(list ffi/pointer ffi/pointer) ffi/void))
+666
667
;; Patterns
668
(define %pattern-create-rgb
669
(c-function cairo-lib "cairo_pattern_create_rgb"
@@ -1147,6 +1157,36 @@
1157
(: pointer? -> void?)
1158
(%identity-matrix cr))
1159
+1160
;;; Get the current transformation matrix.
+1161
;;; Returns a dict with xx: yx: xy: yy: x0: y0: fields.
+1162
(define (cairo-get-matrix cr)
+1163
(: pointer? -> dict?)
+1164
(let* ((matrix-size (c-struct-size cairo-matrix-layout))
+1165
(buf (c-alloc matrix-size)))
+1166
(%get-matrix cr buf)
+1167
(let ((result (dict xx: (c-struct-ref buf cairo-matrix-layout xx:)
+1168
yx: (c-struct-ref buf cairo-matrix-layout yx:)
+1169
xy: (c-struct-ref buf cairo-matrix-layout xy:)
+1170
yy: (c-struct-ref buf cairo-matrix-layout yy:)
+1171
x0: (c-struct-ref buf cairo-matrix-layout x0:)
+1172
y0: (c-struct-ref buf cairo-matrix-layout y0:))))
+1173
(c-free buf)
+1174
result)))
+1175
+1176
;;; Set the transformation matrix from a dict with xx: yx: xy: yy: x0: y0:.
+1177
(define (cairo-set-matrix cr matrix)
+1178
(: pointer? dict? -> void?)
+1179
(let* ((matrix-size (c-struct-size cairo-matrix-layout))
+1180
(buf (c-alloc matrix-size)))
+1181
(c-struct-set! buf cairo-matrix-layout xx: (dict-ref matrix xx:))
+1182
(c-struct-set! buf cairo-matrix-layout yx: (dict-ref matrix yx:))
+1183
(c-struct-set! buf cairo-matrix-layout xy: (dict-ref matrix xy:))
+1184
(c-struct-set! buf cairo-matrix-layout yy: (dict-ref matrix yy:))
+1185
(c-struct-set! buf cairo-matrix-layout x0: (dict-ref matrix x0:))
+1186
(c-struct-set! buf cairo-matrix-layout y0: (dict-ref matrix y0:))
+1187
(%set-matrix cr buf)
+1188
(c-free buf)))
+1189
1190
;; ============================================================
1191
;; Public API — Patterns
1192
;; ============================================================