AtlatestRepositorysigil-ui

sigil-ui / tree / testtest-text.sgl

1(import (sigil test) (sigil process) (sigil fs) (sigil math) (sigil list)
2 (sigil path) (sigil string)
3 (sigil ui text) (sigil ui raster) (sigil ui display))
4
5(define fixtures (path-join (current-directory) "test/fixtures"))
6(define regular (or (getenv "SIGIL_UI_TEST_REGULAR") (path-join fixtures "JetBrainsMono-Regular.ttf")))
7(define bold (or (getenv "SIGIL_UI_TEST_BOLD") (path-join fixtures "JetBrainsMono-Bold.ttf")))
8
9(test "checked-in outline fixtures match their licensed SHA-256 manifest"
10 (for-each
11 (lambda (line)
12 (let ((parts (string-split line " ")))
13 (assert-equal 2 (length parts))
14 (assert-equal (car parts) (file-sha256 (path-join fixtures (cadr parts))))))
15 (string-split (string-trim (read-file-string (path-join fixtures "SHA256SUMS"))) "\n")))
16(define (with-font body)
17 (let ((font (make-text-font regular bold)))
18 (dynamic-wind (lambda () #f) (lambda () (body font))
19 (lambda () (text-font-close! font)))))
20(define (paint font text (keys: (scale 1) (bold? #f)))
21 (let ((canvas (make-canvas (* 320 scale) (* 48 scale))))
22 (draw-text! font canvas 4 4 text 14 #xffffff scale: scale bold?: bold?)
23 canvas))
25(define (error-message thunk)
26 (guard (failure (#t (exception-message failure))) (thunk) #f))
28(define (ink-bounds canvas)
29 (let ((left (canvas-width canvas)) (right -1)
30 (top (canvas-height canvas)) (bottom -1))
31 (do ((y 0 (+ y 1))) ((= y (canvas-height canvas)))
32 (do ((x 0 (+ x 1))) ((= x (canvas-width canvas)))
33 (when (> (canvas-pixel canvas x y) 0)
34 (set! left (min left x)) (set! right (max right x))
35 (set! top (min top y)) (set! bottom (max bottom y)))))
36 (and (>= right 0) (list left top right bottom))))
38(define (row-ink canvas y)
39 (let sum ((x 0) (total 0))
40 (if (= x (canvas-width canvas)) total
41 (sum (+ x 1) (+ total (bitwise-and (canvas-pixel canvas x y) 255))))))
43(define (column-ink canvas x)
44 (let sum ((y 0) (total 0))
45 (if (= y (canvas-height canvas)) total
46 (sum (+ y 1) (+ total (bitwise-and (canvas-pixel canvas x y) 255))))))
48(test "bundled faces preserve fractional advance and integer vertical metrics"
49 (with-font (lambda (font)
50 (assert-equal "JetBrains Mono" (text-font-family font))
51 (assert-equal 'files (text-font-source font))
52 (assert-false (text-font-discovery-error font))
53 (let* ((metrics (text-metrics font 14)) (advance (dict-ref metrics advance:)))
54 (assert-true (< (abs (- advance 8.4)) 0.000001))
55 (assert-true (exact-integer? (dict-ref metrics height:)))
56 (assert-true (> advance 0))
57 (assert-equal (* 3 advance) (measure-text font "iWλ" 14))
58 (assert-equal (* 3 advance) (measure-text font "ЖΩx" 14))
59 (assert-true (= 0 (measure-text font "" 14)))
60 (assert-true (>= (dict-ref metrics height:)
61 (+ (dict-ref metrics ascent:) (dict-ref metrics descent:))))))))
63(test "long monospace runs round each physical glyph origin without cumulative drift"
64 (with-font (lambda (font)
65 (for-each (lambda (scale)
66 (for-each (lambda (bold?)
67 (let ((actual (make-canvas (* 480 scale) (* 48 scale)))
68 (expected (make-canvas (* 480 scale) (* 48 scale)))
69 (text (make-string 48 #\|)))
70 (draw-text! font actual 4 4 text 14 #xffffff scale: scale bold?: bold?)
71 ;; JetBrains Mono is 600 units per 1000 em. Compute the expected
72 ;; physical origins independently of text-metrics and measure-text.
73 (do ((column 0 (+ column 1))) ((= column 48))
74 (let ((origin (exact (round (* (+ 4 (* column 14 0.6)) scale)))))
75 (draw-text! font expected (/ origin scale) 4 "|" 14 #xffffff
76 scale: scale bold?: bold?)))
77 (assert-equal (canvas-pixels expected) (canvas-pixels actual))
78 (assert-true (< (abs (- (measure-text font text 14) 403.2)) 0.000001))))
79 '(#f #t))) '(1 2)))))
81(test "Latin Greek and Cyrillic rasterize with grayscale antialiasing at both scales"
82 (with-font (lambda (font)
83 (for-each (lambda (scale)
84 (let* ((canvas (paint font "Oriel λ Ω Ж" scale: scale))
85 (rgb (canvas->rgb canvas)) (gray? #f))
86 (do ((i 0 (+ i 3))) ((= i (bytevector-length rgb)))
87 (let ((r (bytevector-u8-ref rgb i))
88 (g (bytevector-u8-ref rgb (+ i 1)))
89 (b (bytevector-u8-ref rgb (+ i 2))))
90 (assert-equal r g)
91 (assert-equal g b)
92 (when (< 0 r 255) (set! gray? #t))))
93 (assert-true gray?))) '(1 2)))))
95(test "Bold uses its own face rather than duplicating the Regular bitmap"
96 (with-font (lambda (font)
97 (let ((a (paint font "Tabs λ Ж")) (b (paint font "Tabs λ Ж" bold?: #t)))
98 (assert-false (equal? (canvas-pixels a) (canvas-pixels b)))
99 (assert-true (> (canvas-count-other b 0 0 320 48 0) 0))))))
101(test "missing CJK and emoji glyphs are visible boxes with the original cell count"
102 (with-font (lambda (font)
103 (let ((canvas (paint font "界😀")))
104 (assert-equal (* 2 (dict-ref (text-metrics font 14) advance:))
105 (measure-text font "界😀" 14))
106 (assert-true (> (canvas-count-other canvas 0 0 320 48 0) 0))))))
108(test "portable placement preserves the baseline descender and T/L ink orientation"
109 (with-font (lambda (font)
110 (for-each (lambda (scale)
111 (let* ((x 7) (y 5) (size 16)
112 (baseline (* scale (+ y (dict-ref (text-metrics font size) ascent:)))))
113 (for-each (lambda (glyph)
114 (let ((canvas (make-canvas (* 100 scale) (* 64 scale))))
115 (draw-text! font canvas x y glyph size #xffffff scale: scale)
116 (let ((bounds (ink-bounds canvas)))
117 (assert-true bounds)
118 (assert-true (>= (car bounds) (* x scale)))
119 (cond
120 ((string=? glyph "M")
121 (assert-true (<= (abs (- (cadddr bounds) baseline)) 1)))
122 ((string=? glyph "g")
123 (assert-true (> (cadddr bounds) baseline)))
124 ((string=? glyph "T")
125 (assert-true (> (row-ink canvas (cadr bounds))
126 (row-ink canvas (cadddr bounds)))))
127 ((string=? glyph "L")
128 (assert-true (> (column-ink canvas (car bounds))
129 (column-ink canvas (caddr bounds)))))))))
130 '("M" "g" "T" "L")))) '(1 2)))))
132(test "a missing-glyph box stays inside its own cell between blank neighbors"
133 (with-font (lambda (font)
134 (for-each (lambda (scale)
135 (let* ((canvas (make-canvas (* 100 scale) (* 64 scale)))
136 (metrics (text-metrics font 16))
137 (advance (dict-ref metrics advance:)))
138 (draw-text! font canvas 7 5 " 界 " 16 #xffffff scale: scale)
139 (let ((bounds (ink-bounds canvas)))
140 (assert-true bounds)
141 (assert-true (>= (car bounds) (* scale (+ 7 advance))))
142 (assert-true (< (caddr bounds) (* scale (+ 7 (* 2 advance)))))
143 (assert-true (>= (cadr bounds) (* scale 5)))
144 (assert-true (< (cadddr bounds) (* scale (+ 5 (dict-ref metrics height:))))))))
145 '(1 2)))))
147(test "glyph cache distinguishes size scale and weight and records hits"
148 (with-font (lambda (font)
149 (paint font "ABBA")
150 (let ((first (text-cache-stats font)))
151 (assert-equal 2 (dict-ref first misses:))
152 (assert-equal 2 (dict-ref first hits:)))
153 (paint font "ABBA")
154 (assert-equal 2 (dict-ref (text-cache-stats font) misses:))
155 (paint font "ABBA" scale: 2)
156 (assert-equal 4 (dict-ref (text-cache-stats font) misses:))
157 (paint font "ABBA" bold?: #t)
158 (assert-equal 6 (dict-ref (text-cache-stats font) misses:))
159 (let ((canvas (make-canvas 320 64)))
160 (draw-text! font canvas 0 0 "AB" 20 #xffffff))
161 (assert-equal 8 (dict-ref (text-cache-stats font) misses:)))))
163(test "fractional device sizes cannot reuse a differently rounded cached mask"
164 (with-font (lambda (cached)
165 (with-font (lambda (fresh)
166 (let ((warmup (make-canvas 160 48))
167 (actual (make-canvas 160 48))
168 (expected (make-canvas 160 48)))
169 (draw-text! cached warmup 4 4 "MTg" 12.5 #xffffff)
170 (draw-text! cached actual 4 4 "MTg" 13 #xffffff)
171 (draw-text! fresh expected 4 4 "MTg" 13 #xffffff)
172 (assert-equal (canvas-pixels expected) (canvas-pixels actual))))))))
174(test "cache eviction obeys both count and byte limits across scale changes"
175 (let ((font (make-text-font regular bold cache-bytes: 300 cache-glyphs: 2)))
176 (dynamic-wind (lambda () #f)
177 (lambda ()
178 (for-each (lambda (scale)
179 (paint font "ABCDEFGHIJKLMNOPQRSTUVWXYZ" scale: scale)
180 (let ((stats (text-cache-stats font)))
181 (assert-true (<= (dict-ref stats bytes:) 300))
182 (assert-true (<= (dict-ref stats glyphs:) 2)))) '(1 2 1)))
183 (lambda () (text-font-close! font)))))
185(test "a cache hit protects the glyph from eviction and preserves hit/miss counts"
186 (let ((font (make-text-font regular bold cache-glyphs: 2)))
187 (dynamic-wind (lambda () #f)
188 (lambda ()
189 (paint font "AB")
190 (assert-equal 2 (dict-ref (text-cache-stats font) misses:))
191 (paint font "A")
192 (assert-equal 1 (dict-ref (text-cache-stats font) hits:))
193 (paint font "C")
194 (assert-equal 3 (dict-ref (text-cache-stats font) misses:))
195 (paint font "A")
196 (assert-equal 2 (dict-ref (text-cache-stats font) hits:))
197 (assert-equal 3 (dict-ref (text-cache-stats font) misses:))
198 (paint font "B")
199 (assert-equal 4 (dict-ref (text-cache-stats font) misses:))
200 (paint font "A")
201 (assert-equal 3 (dict-ref (text-cache-stats font) hits:))
202 (assert-equal 2 (dict-ref (text-cache-stats font) glyphs:)))
203 (lambda () (text-font-close! font)))))
205(test "a glyph larger than the cache budget still renders without being retained"
206 (let ((font (make-text-font regular bold cache-bytes: 1 cache-glyphs: 1)))
207 (dynamic-wind (lambda () #f)
208 (lambda ()
209 (let ((canvas (paint font "A")))
210 (assert-true (> (canvas-count-other canvas 0 0 320 48 0) 0))
211 (assert-equal 0 (dict-ref (text-cache-stats font) bytes:))
212 (assert-equal 0 (dict-ref (text-cache-stats font) glyphs:))))
213 (lambda () (text-font-close! font)))))
215(test "clipped drawing matches a full render inside damage and preserves outside pixels"
216 (with-font (lambda (font)
217 (for-each (lambda (scale)
218 (let ((full (make-canvas (* 96 scale) (* 40 scale)))
219 (part (make-canvas (* 96 scale) (* 40 scale)))
220 (clip (map (lambda (v) (* v scale)) '(12 8 47 25))))
221 (draw-text! font full -2 0 "Prompt λ Ж" 14 #xffffff scale: scale)
222 (draw-text! font part -2 0 "Prompt λ Ж" 14 #xffffff scale: scale clip: clip)
223 (do ((y 0 (+ y 1))) ((= y (* 40 scale)))
224 (do ((x 0 (+ x 1))) ((= x (* 96 scale)))
225 (assert-equal
226 (if (and (<= (car clip) x) (< x (caddr clip))
227 (<= (cadr clip) y) (< y (cadddr clip)))
228 (canvas-pixel full x y) 0)
229 (canvas-pixel part x y)))))) '(1 2)))))
231(test "outline display item bounds translation and retained repaint agree"
232 (with-font (lambda (font)
233 (let* ((item (mono-text-item font 2 3 "Oriel λ" 14 #xffffff bold?: #t))
234 (bounds (item-bounds item))
235 (moved (translate-item item 7 9))
236 (full (make-canvas 200 40)) (part (make-canvas 200 40)))
237 (assert-true (mono-text-item? item))
238 (assert-equal (list 9 12 (caddr bounds) (cadddr bounds)) (item-bounds moved))
239 (render-items! full (list item) 1)
240 (repaint-rects! part (list item) 1 0 (list bounds))
241 (assert-equal (canvas-pixels full) (canvas-pixels part))))))
243(test "close releases cached coverage and rejects subsequent draw or measurement"
244 (let ((font (make-text-font regular bold)))
245 (paint font "Cache")
246 (text-font-close! font)
247 (text-font-close! font)
248 (assert-equal 0 (dict-ref (text-cache-stats font) bytes:))
249 (assert-error (measure-text font "A" 14))
250 (assert-error (paint font "A"))))
252(test "invalid sizes scales cache limits and font paths fail explicitly"
253 (assert-error (make-text-font regular bold family: " "))
254 (assert-error (make-text-font regular "/missing/bold.ttf"))
255 (assert-error (make-text-font regular bold cache-bytes: 0))
256 (assert-error (make-text-font regular bold cache-glyphs: -1))
257 (with-font (lambda (font)
258 (for-each (lambda (size) (assert-error (text-metrics font size))) '(0 -1 257 +inf.0 +nan.0))
259 (let ((canvas (make-canvas 32 32)))
260 (assert-error (draw-text! font canvas 0 0 "A" 14 #xffffff scale: 0))
261 (assert-error (draw-text! font canvas 0 0 "A" 14 #xffffff bold?: 'bold))
262 (assert-error (draw-text! font canvas 0 0 "A" 14 #xffffff clip: '(1 2)))))))
264(test "missing and empty font files report their path and distinct causes"
265 (let ((message (error-message (lambda () (make-text-font regular "/missing/bold.ttf")))))
266 (assert-true (string-contains? message "Text font file not found"))
267 (assert-true (string-contains? message "/missing/bold.ttf")))
268 (call-with-temp-file
269 (lambda (empty)
270 (let ((message (error-message (lambda () (make-text-font empty bold)))))
271 (assert-true (string-contains? message "Text font file is empty"))
272 (assert-true (string-contains? message empty))))))
274(test "an optional discovery error falls back to files and retains its original cause"
275 (let ((font (make-text-font regular bold
276 family: (string-append "Jet" (string (integer->char 0)) "Brains"))))
277 (dynamic-wind (lambda () #f)
278 (lambda ()
279 (assert-equal 'files (text-font-source font))
280 (assert-true (text-font-discovery-error font))
281 (assert-true (string? (exception-message (text-font-discovery-error font))))
282 (assert-true (> (canvas-count-other (paint font "Fallback") 0 0 320 48 0) 0)))
283 (lambda () (text-font-close! font)))))
285(test "an unavailable configured family uses the supplied bundled faces"
286 (let ((font (make-text-font regular bold family: "Sigil missing font 21a84bd3")))
287 (dynamic-wind (lambda () #f)
288 (lambda ()
289 (assert-equal "JetBrains Mono" (text-font-family font))
290 (assert-equal 'files (text-font-source font))
291 (assert-true (> (canvas-count-other (paint font "Fallback") 0 0 320 48 0) 0)))
292 (lambda () (text-font-close! font)))))
294(test "spaces keep advance without painting and tiny missing glyphs remain visible"
295 (with-font (lambda (font)
296 (let ((canvas (make-canvas 64 32)))
297 (assert-equal (measure-text font " " 14)
298 (draw-text! font canvas 0 0 " " 14 #xffffff))
299 (assert-equal 0 (canvas-count-other canvas 0 0 64 32 0))
300 (draw-text! font canvas 0 0 "界" 1 #xffffff)
301 (assert-true (> (canvas-count-other canvas 0 0 64 32 0) 0))))))
303(when (equal? (getenv "SIGIL_UI_TEST_FAMILY_FIXTURES") "1")
304 (test "an installed configured pair does not open fallback files"
305 (let ((font (make-text-font "/missing/regular.ttf" "/missing/bold.ttf"
306 family: " jEtBrAiNs mOnO ")))
307 (dynamic-wind (lambda () #f)
308 (lambda ()
309 (assert-equal "JetBrains Mono" (text-font-family font))
310 (assert-equal 'family (text-font-source font))
311 (assert-false (text-font-discovery-error font))
312 (assert-true (> (canvas-count-other (paint font "Family" bold?: #t) 0 0 320 48 0) 0)))
313 (lambda () (text-font-close! font)))))
315 (test "an incomplete or variable-only installed family falls back to the bundled pair"
316 (for-each (lambda (family)
317 (let ((font (make-text-font regular bold family: family)))
318 (dynamic-wind (lambda () #f)
319 (lambda ()
320 (assert-equal "JetBrains Mono" (text-font-family font))
321 (assert-equal 'files (text-font-source font)))
322 (lambda () (text-font-close! font)))))
323 '("Noto Sans Symbols 2" "Noto Sans"))))