AtlatestRepositorysigil-ui

sigil-ui / tree / testtest-text-fallback.sgl

1(import (sigil test) (sigil fs) (sigil path) (sigil process) (sigil math) (sigil list)
2 (sigil font) (sigil ui text) (sigil ui raster) (sigil ui display))
3(define fixtures (path-join (current-directory) "test/fixtures"))
4(define regular (path-join fixtures "JetBrainsMono-Regular.ttf"))
5(define bold (path-join fixtures "JetBrainsMono-Bold.ttf"))
6(define symbols (path-join fixtures "NotoSansSymbols2-Regular.ttf"))
7(define (with-faces body)
8 (let ((a (font-open-bytes (read-file-bytes regular)))
9 (b (font-open-bytes (read-file-bytes bold)))
10 (s (font-open-bytes (read-file-bytes symbols)))
11 (font (make-text-font regular bold fallback-files: (list symbols))))
12 (dynamic-wind (lambda () #f) (lambda () (body font a b s))
13 (lambda () (text-font-close! font) (font-face-close! a) (font-face-close! b) (font-face-close! s)))))
14(define (probe-codepoint primary fallback)
15 (let find ((codes '(#x1F700 #x1F5F8 #x2611 #x2713 #x2603 #x1D400)))
16 (cond ((null? codes) (error "Pinned fonts have no fallback probe"))
17 ((and (= 0 (font-glyph-index primary (car codes)))
18 (> (font-glyph-index fallback (car codes)) 0)) (car codes))
19 (else (find (cdr codes))))))
20(define (reference-columns face index)
21 ;; Read at 64 px, independently of the renderer's one-pixel metric cache.
22 ;; The JetBrains fixture cell is .6 em.
23 (font-face-set-size! face 64)
24 (max 1 (exact (ceiling (/ (dict-ref (font-rasterize-glyph face index) advance:) 64 0.6)))))
25(define (reference! canvas font entries scale)
26 (let* ((metrics (text-metrics font 14))
27 (advance (dict-ref metrics advance:))
28 (baseline (exact (round (* (+ 4 (dict-ref metrics ascent:)) scale)))))
29 (let loop ((entries entries) (column 0))
30 (unless (null? entries)
31 (let* ((entry (car entries)) (face (car entry)) (index (cadr entry)))
32 (font-face-set-size! face 14 scale: scale)
33 (let ((glyph (font-rasterize-glyph face index))
34 (left (exact (round (* (+ 4 (* column advance)) scale)))))
35 (canvas-mask! canvas (+ left (dict-ref glyph left:)) (- baseline (dict-ref glyph top:))
36 (dict-ref glyph coverage:) (dict-ref glyph width:) #xffffff))
37 (loop (cdr entries) (+ column (caddr entry))))))))
39(test "explicit fallback glyphs match their own face at both weights and scales"
40 (with-faces (lambda (font a b s)
41 (define codepoint (probe-codepoint a s))
42 (define index (font-glyph-index s codepoint))
43 (define columns (reference-columns s index))
44 (define text (string-append "A" (string (integer->char codepoint)) "B"))
45 (for-each (lambda (scale)
46 (for-each (lambda (bold?)
47 (let* ((primary (if bold? b a))
48 (actual (make-canvas (* 160 scale) (* 64 scale)))
49 (expected (make-canvas (* 160 scale) (* 64 scale)))
50 (width (draw-text! font actual 4 4 text 14 #xffffff scale: scale bold?: bold?)))
51 (reference! expected font
52 (list (list primary (font-glyph-index primary 65) 1)
53 (list s index columns)
54 (list primary (font-glyph-index primary 66) 1)) scale)
55 (assert-true (< (abs (- width (* (+ 2 columns) 8.4))) 0.000001))
56 (assert-equal width (measure-text font text 14 bold?: bold?))
57 (assert-equal (canvas-pixels expected) (canvas-pixels actual))
58 (assert-equal (exact (ceiling width))
59 (caddr (item-bounds (mono-text-item font 0 0 text 14 #xffffff bold?: bold?)))))) '(#f #t)))
60 '(1 2)))))
62(test "face-local glyph ID collisions cannot reuse another face's cached pixels"
63 (with-faces (lambda (font a b s)
64 (define indices
65 (let collect ((code 32) (pairs '()))
66 (if (= code #x600) pairs
67 (let ((index (font-glyph-index a code)))
68 (collect (+ code 1) (if (> index 0) (cons (cons index code) pairs) pairs))))))
69 (define collision
70 (let find ((code #x2000))
71 (when (= code #x3000) (error "Pinned fonts have no glyph-ID collision probe"))
72 (let* ((index (font-glyph-index s code)) (primary (assv index indices)))
73 (if (and primary (= 0 (font-glyph-index a code)))
74 (list (cdr primary) code index) (find (+ code 1))))))
75 (define primary-code (car collision))
76 (define fallback-code (cadr collision))
77 (define index (caddr collision))
78 (define columns (reference-columns s index))
79 (define actual (make-canvas 160 64))
80 (define expected (make-canvas 160 64))
81 (draw-text! font actual 4 4 (string (integer->char primary-code) (integer->char fallback-code)) 14 #xffffff)
82 (reference! expected font (list (list a index 1) (list s index columns)) 1)
83 (assert-equal (canvas-pixels expected) (canvas-pixels actual))
84 (assert-true (> (canvas-count-other actual 0 0 160 64 0) 0))
85 (display "Fallback collision probe: ") (write collision) (newline))))
87(test "fallback measurement is scale independent and cache growth stays bounded"
88 (let ((font (make-text-font regular bold fallback-files: (list symbols) cache-bytes: 2048 cache-glyphs: 4))
89 (a (font-open-bytes (read-file-bytes regular)))
90 (s (font-open-bytes (read-file-bytes symbols))))
91 (dynamic-wind (lambda () #f)
92 (lambda ()
93 (define character (integer->char (probe-codepoint a s)))
94 (define text (string #\M character #\M character))
95 (define width (measure-text font text 12.5))
96 (for-each (lambda (scale)
97 (let ((canvas (make-canvas (* 200 scale) (* 64 scale))))
98 (assert-equal width (draw-text! font canvas 4 4 text 12.5 #xffffff scale: scale))
99 (assert-true (<= (dict-ref (text-cache-stats font) bytes:) 2048))
100 (assert-true (<= (dict-ref (text-cache-stats font) glyphs:) 4)))) '(1 2 1 2))
101 (text-font-close! font)
102 (text-font-close! font)
103 (assert-equal 0 (dict-ref (text-cache-stats font) bytes:))
104 (assert-equal 0 (dict-ref (text-cache-stats font) glyphs:))
105 (assert-error (measure-text font text 14))
106 (assert-error (draw-text! font (make-canvas 160 64) 4 4 text 14 #xffffff)))
107 (lambda () (text-font-close! font) (font-face-close! a) (font-face-close! s)))))
109(test "fallback files are explicit bounded inputs and malformed chains fail"
110 (assert-error (make-text-font regular bold fallback-files: #f))
111 (assert-error (make-text-font regular bold fallback-files: (list 1)))
112 (assert-error (make-text-font regular bold fallback-files: (cons symbols symbols)))
113 (assert-error (make-text-font regular bold fallback-files: (list symbols symbols)))
114 (assert-error (make-text-font regular bold fallback-files: (list symbols (string-append symbols ""))))
115 (assert-error (make-text-font regular bold fallback-files: (make-list 9 symbols)))
116 (assert-error (make-text-font regular bold fallback-files: (list "missing-fallback.ttf"))))