AtlatestRepositorysigil-ui
1
;;; Run with the native UI test runner and an explicit, verified CJK fixture.2
(import (sigil test) (sigil fs) (sigil path) (sigil process) (sigil math) (sigil string)3
(sigil font) (sigil ui text) (sigil ui raster) (sigil ui png))4
(define file (getenv "SIGIL_UI_TEST_CJK"))5
(define output (getenv "SIGIL_UI_TEST_CJK_SHOTS"))6
(define regular (path-join (current-directory) "test/fixtures/JetBrainsMono-Regular.ttf"))7
(define bold (path-join (current-directory) "test/fixtures/JetBrainsMono-Bold.ttf"))8
(test "manifest-pinned CJK glyphs render without replacement boxes at both scales"9
(unless (and file (equal? (file-sha256 file)10
"b76b0433203017ca80401b2ee0dd69350349871c4b19d504c34dbdd80541690a"))11
(error "Expected the pinned Noto Sans CJK Regular collection"))12
(let ((font (make-text-font regular bold fallback-files: (list file)))13
(face (font-open-bytes (read-file-bytes file))))14
(dynamic-wind (lambda () #f)15
(lambda ()16
(for-each (lambda (size)17
(for-each (lambda (scale)18
(for-each (lambda (bold?)19
(let* ((actual (make-canvas (* 320 scale) (* 64 scale) #x101010))20
(expected (make-canvas (* 320 scale) (* 64 scale) #x101010))21
(text "中文日本語")22
(advance (dict-ref (text-metrics font size) advance:))23
(baseline (exact (round (* (+ 4 (dict-ref (text-metrics font size) ascent:)) scale)))))24
(assert-equal (* 10 advance)25
(draw-text! font actual 4 4 text size #xffffff scale: scale bold?: bold?))26
(font-face-set-size! face size scale: scale)27
(let draw ((characters (string->list text)) (column 0))28
(unless (null? characters)29
(let* ((index (font-glyph-index face (char->integer (car characters))))30
(glyph (font-rasterize-glyph face index))31
(left (exact (round (* (+ 4 (* column advance)) scale)))))32
(assert-true (> index 0))33
(canvas-mask! expected (+ left (dict-ref glyph left:)) (- baseline (dict-ref glyph top:))34
(dict-ref glyph coverage:) (dict-ref glyph width:) #xffffff)35
(draw (cdr characters) (+ column 2)))))36
(assert-equal (canvas-pixels expected) (canvas-pixels actual))37
(when output38
(write-png! (path-join output39
(str "cjk-" size "-" scale "-" (if bold? "bold" "regular") ".png")) actual))))40
'(#f #t))) '(1 2))) '(14 20)))41
(lambda () (text-font-close! font) (font-face-close! face)))))