Commit77f09d60Recorded11 Nov 2026Repositorysigil-ui
Render missing outline glyphs through explicit fallback fonts
Changed
NEWS.md | 10 ++++++++++
docs/text.md | 54 ++++++++++++++++++++++++++++++++++++++++++------------
scripts/check-text-cjk.sgl | 41 +++++++++++++++++++++++++++++++++++++++++
src/sigil/ui/display.sgl | 3 ++-
src/sigil/ui/text.sgl | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
test/fixtures/Noto-Symbols-OFL.txt | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test/fixtures/NotoSansSymbols2-Regular.ttf | Bin 0 -> 1233128 bytes
test/fixtures/README.md | 7 +++++++
test/fixtures/SHA256SUMS | 2 ++
test/fixtures/cjk-resource.sgv | 7 +++++++
test/test-text-fallback.sgl | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11 files changed, 399 insertions(+), 44 deletions(-)Diff
NEWS.mdmodified
@@ -1,3 +1,13 @@
+1
# Unreleased+2
+3
- Outline text accepts an explicit ordered `fallback-files:` chain. Missing+4
primary glyphs use the selected fallback face, with wider glyphs occupying+5
whole primary cells. Measurement and display-item bounds use the same policy;+6
fallback line metrics prevent clipping. Existing callers without fallbacks+7
keep their layout. Applications select the files; drawing does not discover+8
system fonts. The owned mask cache distinguishes face-local glyph IDs and+9
retains its byte and entry limits. Shaping and color emoji remain separate.+10
11
# sigil-ui 0.2.2 12
13
A chooser and Wayland input release.docs/text.mdmodified
@@ -29,16 +29,19 @@ top of the measured line, not its baseline. Size must be positive and at most
29
256 logical pixels. `bold?: #t` selects the supplied Bold face. 30
31
`(text-metrics font size)` returns `advance:`, `ascent:`, `descent:` and−32
`height:`. Both faces contribute to a shared monospace cell and line height,+32
`height:`. Both primary faces contribute to a shared monospace cell, while+33
all opened faces contribute to the line height, 34
so changing weight does not move the caret. The advance retains the font's 35
fractional logical width. Drawing rounds each glyph origin after applying 36
the output scale, keeping columns aligned without accumulating rounding error. 37
Vertical metrics round outward to whole logical pixels. `(measure-text font−37
text size)` is the string's code-point count times the fractional advance.+38
text size)` returns the primary-cell width of the selected glyph run.+39
Its optional `bold?:` selects the same face policy as drawing. 40
41
Consumers should round column capacities down, keep text positions real, and 42
round positions only after applying the output scale. Use the same advance−41
for glyph columns, cursor placement and any text hit-testing.+43
for glyph columns. When opting into fallback files, measure prefixes with+44
`measure-text` for clipping, cursor placement and text hit testing. 45
46
`(draw-text! font canvas x y text size color)` returns that logical width. 47
Optional `clip:` is a device-pixel `(x0 y0 x1 y1)` rectangle. Drawing stays@@ -46,17 +49,31 @@ inside the measured run and the raster's bounds. `mono-text-item` supplies
49
the same text in a display list, with bounds, translation and damage clipping. 50
The existing bitmap `text-item` and font-set APIs remain available. 51
−49
This first API gives each Unicode code point one cell. It does not shape,−50
join, reorder or combine characters, and does not enable ligatures. The−51
original string is retained. A code point absent from the selected face draws−52
a visible replacement box; CJK and emoji therefore remain observable when a−53
Latin/Greek/Cyrillic face lacks them. There is no silent question-mark−54
substitution. A discovery/shaping layer can supply richer fallback later.+52
Without fallback files, each Unicode code point keeps its existing single+53
primary cell. Optional `fallback-files:` is an ordered list of up to eight+54
explicit font paths. Their total size is limited to 64 MiB; missing, empty+55
or oversized inputs raise. Each default collection face is opened once and+56
can serve either weight without synthesizing Bold. Applications choose the+57
files; rendering never scans installed fonts or changes font configuration.+58
+59
Lookup tries the selected primary face, then the fallback faces in order.+60
A fallback glyph consumes enough whole primary cells for its unhinted+61
advance, with a minimum of one. This keeps wider CJK glyphs from overlapping+62
the next character and keeps measurement independent of the output scale.+63
Line metrics include the fallbacks to avoid clipping their ascenders or+64
descenders. For example, an application with a bundled CJK collection can+65
pass `fallback-files: (list "fonts/NotoSansCJK-Regular.ttc")`.+66
+67
The renderer does not shape, join, reorder or combine characters and does+68
not enable ligatures. The original string is retained. A code point absent+69
from every selected face draws a visible replacement box, with no silent+70
question-mark substitution. Color emoji and grapheme layout are outside+71
this interface. 72
73
Each font owns its faces and copied masks. Call `text-font-close!` 74
after discarding its display items, including when an operation fails.−58
Repeated close is safe. Closing Regular is still attempted if closing Bold−59
raises. Drawing or measuring a closed font raises an error.+75
Repeated close is safe. Every fallback and both primary closes are attempted+76
even if an earlier close raises. Drawing or measuring a closed font raises an error. 77
Constructing a font with a bad second face closes the first face. Each backend 78
also supplies a native guardian for abandoned faces. Input files are bounded 79
to 64 MiB each before reading; the native face owns its independent byte copy.@@ -80,7 +97,8 @@ The mask cache uses least-recently-used eviction with independent byte and
97
glyph-count limits: 4 MiB and 1024 entries by default. Override them with 98
`cache-bytes:` and `cache-glyphs:` when creating the font. A mask larger than 99
the cache budget still renders but is not retained. Cache keys distinguish−83
face weight, logical size, scale and glyph index. Size/scale changes cannot+100
the actual face, weight, logical size, scale and glyph index. Face-local IDs+101
cannot collide across primary and fallback fonts. Size/scale changes cannot 102
reuse a mask with different rasterization inputs. Hash buckets and use stamps 103
make hits avoid rebuilding the cache; only insertion overflow scans for the 104
oldest entry. `text-cache-stats` reports entries,@@ -88,6 +106,8 @@ bytes, hits, misses and limits for qualification. Temporary coverage copies
106
also obey the font interface's owned-coverage limit; native rasterizer temporary 107
allocations are separate. Superseded coverage copies are not retained alongside 108
cached masks. Byte limits count mask coverage, separately from cache metadata.+109
Small one-pixel metric rasters used for fallback cell counts share these+110
same byte and entry limits. 111
112
Tests default to the checked-in OFL JetBrains Mono Regular and Bold faces in 113
`test/fixtures/`, with their license and SHA-256 manifest. Optional@@ -96,3 +116,13 @@ Tests default to the checked-in OFL JetBrains Mono Regular and Bold faces in
116
baseline/bearing/row placement at scales 1 and 2, weight, clipping/repaint, 117
missing-glyph cell bounds, eviction order, cache limits and lifecycle. No 118
installed host font or desktop service is required.+119
+120
`test/test-text-fallback.sgl` uses the pinned Noto Sans Symbols2 fixture to+121
check independent raster references, face-local glyph-ID cache collisions,+122
whole-cell advances and lifecycle/bounds. For real CJK coverage, the separate+123
`scripts/check-text-cjk.sgl` test uses the licensed collection identified by+124
`test/fixtures/cjk-resource.sgv`. Supply its verified path as+125
`SIGIL_UI_TEST_CJK` when running it through the native UI test runner;+126
optional `SIGIL_UI_TEST_CJK_SHOTS` names an existing output directory for+127
14/20-pixel Regular/Bold captures at scales1/2. The large collection is a+128
qualification resource, not a bundled library dependency.scripts/check-text-cjk.sgladded
@@ -0,0 +1,41 @@
+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 output+38
(write-png! (path-join output+39
(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)))))src/sigil/ui/display.sglmodified
@@ -105,7 +105,8 @@
105
((mono-text? item) 106
(let* ((x (mono-text-x item)) (y (mono-text-y item)) 107
(width (outline:measure-text−108
(mono-text-font item) (mono-text-string item) (mono-text-size item)))+108
(mono-text-font item) (mono-text-string item) (mono-text-size item)+109
bold?: (mono-text-bold? item))) 110
(height (dict-ref 111
(outline:text-metrics (mono-text-font item) (mono-text-size item)) 112
height:)))src/sigil/ui/text.sglmodified
@@ -8,7 +8,7 @@
8
text-metrics measure-text draw-text! text-cache-stats) 9
(begin 10
(define-struct font-state−11
(regular) (bold) (family) (source) (discovery-error)+11
(regular) (bold) (fallbacks mutable: #t) (family) (source) (discovery-error) 12
(advance) (ascent) (descent) (height) 13
(byte-limit) (glyph-limit) 14
(cache mutable: #t)@@ -24,7 +24,7 @@
24
(define text-font-discovery-error font-state-discovery-error) 25
26
(define-struct cached-glyph−27
(bold?) (size) (scale) (index) (mask) (bytes) (slot)+27
(face) (bold?) (size) (scale) (index) (mask) (bytes) (slot) 28
(used mutable: #t)) 29
30
(define (check-font font)@@ -48,27 +48,40 @@
48
(font-open-bytes (read-file-bytes file)))) 49
50
;; Attempt both releases even when one backend close raises.−51
(define (close-faces! regular bold)−52
(dynamic-wind−53
(lambda () #f)−54
(lambda () (when bold (font-face-close! bold)))−55
(lambda () (when regular (font-face-close! regular)))))+51
(define (close-faces! regular bold (opts: (fallbacks '())))+52
(let close ((faces (append fallbacks (list bold regular))))+53
(unless (null? faces)+54
(dynamic-wind+55
(lambda () #f)+56
(lambda () (when (car faces) (font-face-close! (car faces))))+57
(lambda () (close (cdr faces))))))) 58
59
;;; The owner must close the font after its display items are discarded. 60
;;; An optional family needs real Regular and Bold faces; otherwise use files.+61
;;; Explicit fallback files are opened once, in order, without discovery.+62
;;; Their default collection face serves either weight without synthesis. 63
(define (make-text-font regular-path bold-path−60
(keys: (family #f) (cache-bytes 4194304) (cache-glyphs 1024)))+64
(keys: (family #f) (fallback-files '()) (cache-bytes 4194304) (cache-glyphs 1024))) 65
(unless (and (exact-integer? cache-bytes) (> cache-bytes 0) 66
(exact-integer? cache-glyphs) (> cache-glyphs 0)) 67
(error "Text cache limits must be positive exact integers")) 68
(unless (or (not family) (string? family)) 69
(error "Text font family must be a nonempty string or #f"))+70
(unless (and (list? fallback-files) (<= (length fallback-files) 8)+71
(every string? fallback-files))+72
(error "Text fallback files must be a list of at most eight paths"))+73
(let total ((files fallback-files) (bytes 0))+74
(unless (null? files)+75
(let ((size (file-size (car files))))+76
(unless (and size (> size 0) (<= (+ bytes size) 67108864))+77
(error "Text fallback files are missing, empty or exceed the 64-MiB input limit" (car files)))+78
(total (cdr files) (+ bytes size))))) 79
(let ((preferred (and family (string-trim family)))−67
(regular #f) (bold #f) (source 'files) (discovery-error #f))+80
(regular #f) (bold #f) (fallbacks '()) (source 'files) (discovery-error #f)) 81
(when (and preferred (string=? preferred "")) 82
(error "Text font family must be a nonempty string or #f")) 83
(guard (failure−71
(#t (guard (cleanup (#t #f)) (close-faces! regular bold))+84
(#t (guard (cleanup (#t #f)) (close-faces! regular bold fallbacks)) 85
(raise failure))) 86
(when preferred 87
(guard (failure (#t (set! discovery-error failure)))@@ -86,16 +99,22 @@
99
;; File loading is outside the optional discovery guard. 100
(set! regular (open-face regular-path)) 101
(set! bold (open-face bold-path))))+102
(for-each (lambda (file)+103
(set! fallbacks (append fallbacks (list (open-face file))))) fallback-files) 104
(let* ((a (font-face-em-metrics regular)) 105
(b (font-face-em-metrics bold))+106
(extra (map font-face-em-metrics fallbacks)) 107
(advance (max (dict-ref a advance:) (dict-ref b advance:)))−92
(ascent (max (dict-ref a ascent:) (dict-ref b ascent:)))−93
(descent (max 0 (dict-ref a descent:) (dict-ref b descent:)))−94
(height (max (dict-ref a line-height:) (dict-ref b line-height:)−95
(+ ascent descent))))+108
(ascent (apply max (append (list (dict-ref a ascent:) (dict-ref b ascent:))+109
(map (lambda (m) (dict-ref m ascent:)) extra))))+110
(descent (apply max (append (list 0 (dict-ref a descent:) (dict-ref b descent:))+111
(map (lambda (m) (dict-ref m descent:)) extra))))+112
(height (apply max (append+113
(list (dict-ref a line-height:) (dict-ref b line-height:) (+ ascent descent))+114
(map (lambda (m) (dict-ref m line-height:)) extra))))) 115
(unless (and (> advance 0) (> ascent 0) (> height 0)) 116
(error "Text font has invalid layout metrics"))−98
(font-state regular: regular bold: bold+117
(font-state regular: regular bold: bold fallbacks: fallbacks 118
family: (font-face-family regular) 119
source: source discovery-error: discovery-error 120
advance: advance ascent: ascent descent: descent height: height@@ -109,9 +128,12 @@
128
(set-font-state-cache! font #f) 129
(set-font-state-glyphs! font 0) 130
(set-font-state-bytes! font 0)−112
(close-faces! (font-state-regular font) (font-state-bold font))))+131
(let ((fallbacks (font-state-fallbacks font)))+132
(set-font-state-fallbacks! font '())+133
(close-faces! (font-state-regular font) (font-state-bold font) fallbacks)))) 134
−114
;;; Logical advance and line metrics. No shaping: one cell per code point.+135
;;; Primary cell advance and shared line metrics. Fallbacks can use+136
;;; multiple whole cells; shaping and grapheme clustering are not provided. 137
(define (text-metrics font size) 138
(check-font font) 139
(check-size size)@@ -123,9 +145,17 @@
145
ascent: ascent descent: descent 146
height: (max (+ ascent descent) (up (* size (font-state-height font))))))) 147
−126
(define (measure-text font text size)+148
(define (measure-text font text size (keys: (bold? #f))) 149
(unless (string? text) (error "Expected a text string"))−128
(* (string-length text) (dict-ref (text-metrics font size) advance:)))+150
(unless (boolean? bold?) (error "Text weight must be boolean"))+151
(let ((advance (dict-ref (text-metrics font size) advance:))+152
(primary (if bold? (font-state-bold font) (font-state-regular font))))+153
(if (null? (font-state-fallbacks font))+154
(* (string-length text) advance)+155
(let loop ((characters (string->list text)) (columns 0))+156
(if (null? characters) (* columns advance)+157
(loop (cdr characters)+158
(+ columns (caddr (select-glyph font primary bold? (char->integer (car characters))))))))))) 159
160
(define (next-use! font) 161
(let ((next (+ 1 (font-state-clock font))))@@ -137,11 +167,12 @@
167
(define (cache-base bold? size scale) 168
(+ (if bold? 13 0) (* 131 scale) (* 17 (down (* size 64))))) 169
−140
(define (cached font slot bold? size scale index)+170
(define (cached font slot face bold? size scale index) 171
(let find ((entries (vector-ref (font-state-cache font) slot))) 172
(and (pair? entries) 173
(let ((entry (car entries)))−144
(if (and (eq? bold? (cached-glyph-bold? entry))+174
(if (and (eq? face (cached-glyph-face entry))+175
(eq? bold? (cached-glyph-bold? entry)) 176
(= size (cached-glyph-size entry)) 177
(= scale (cached-glyph-scale entry)) 178
(= index (cached-glyph-index entry)))@@ -166,7 +197,7 @@
197
(set-font-state-bytes! font (- (font-state-bytes font) (cached-glyph-bytes oldest))) 198
(set-font-state-glyphs! font (- (font-state-glyphs font) 1))))) 199
−169
(define (remember! font slot bold? size scale index glyph)+200
(define (remember! font slot face bold? size scale index glyph) 201
(let ((bytes (bytevector-length (dict-ref glyph coverage:)))) 202
(when (<= bytes (font-state-byte-limit font)) 203
(let trim ()@@ -175,7 +206,7 @@
206
(evict-oldest! font) 207
(trim))) 208
(let* ((buckets (font-state-cache font))−178
(entry (cached-glyph bold?: bold? size: size scale: scale index: index+209
(entry (cached-glyph face: face bold?: bold? size: size scale: scale index: index 210
mask: glyph bytes: bytes slot: slot used: (next-use! font)))) 211
(vector-set! buckets slot (cons entry (vector-ref buckets slot))) 212
(set-font-state-bytes! font (+ bytes (font-state-bytes font)))@@ -183,7 +214,7 @@
214
215
(define (glyph-mask font face bold? size scale glyph-index base) 216
(let* ((slot (modulo (+ base glyph-index) 1024))−186
(found (cached font slot bold? size scale glyph-index)))+217
(found (cached font slot face bold? size scale glyph-index))) 218
(if found 219
(begin 220
(set-font-state-hits! font (+ 1 (font-state-hits font)))@@ -191,10 +222,27 @@
222
(cached-glyph-mask found)) 223
(begin 224
(set-font-state-misses! font (+ 1 (font-state-misses font)))+225
(font-face-set-size! face size scale: scale) 226
(let ((glyph (font-rasterize-glyph face glyph-index)))−195
(remember! font slot bold? size scale glyph-index glyph)+227
(remember! font slot face bold? size scale glyph-index glyph) 228
glyph))))) 229
+230
;; Glyph IDs are face-local. A fallback's unhinted advance at one device+231
;; em gives its em advance without scale-dependent rounding. These small+232
;; metric rasters share the same bounded cache as drawing masks.+233
(define (select-glyph font primary bold? codepoint)+234
(let ((index (font-glyph-index primary codepoint)))+235
(if (> index 0)+236
(list primary index 1)+237
(let ((found (font-find-glyph (font-state-fallbacks font) codepoint)))+238
(if (not found)+239
(list #f 0 1)+240
(let* ((face (dict-ref found face:))+241
(index (dict-ref found index:))+242
(metrics (glyph-mask font face bold? 1 1 index (cache-base bold? 1 1)))+243
(columns (max 1 (up (/ (dict-ref metrics advance:) (font-state-advance font))))))+244
(list face index columns)))))))+245
246
(define (text-cache-stats font) 247
(unless (text-font? font) (error "Expected a text font")) 248
(dict glyphs: (font-state-glyphs font) bytes: (font-state-bytes font)@@ -226,7 +274,8 @@
274
(canvas-fill! canvas left top stroke height color clip) 275
(canvas-fill! canvas (+ left (- width stroke)) top stroke height color clip))) 276
−229
;;; Draw UTF-8 text as a monospace code-point run. x/y are logical pixels,+277
;;; Draw UTF-8 text on the primary cell grid. Wider fallback glyphs use+278
;;; whole cells. x/y are logical pixels, 279
;;; y is its top, and clip (when supplied) is a device-pixel x0/y0/x1/y1. 280
;;; Missing glyphs draw visible boxes. Coverage is grayscale, never LCD. 281
(define (draw-text! font canvas x y text size color@@ -239,25 +288,26 @@
288
(error "Invalid text position, string, weight or scale")) 289
(let* ((metrics (text-metrics font size)) 290
(advance (dict-ref metrics advance:))−242
(width (* (string-length text) advance))+291
(width (measure-text font text size bold?: bold?)) 292
(height (dict-ref metrics height:)) 293
(bounds (run-clip x y width height scale clip)) 294
(baseline (pixel (* (+ y (dict-ref metrics ascent:)) scale))) 295
(base (cache-base bold? size scale)) 296
(face (if bold? (font-state-bold font) (font-state-regular font)))) 297
(when (and (< (car bounds) (caddr bounds)) (< (cadr bounds) (cadddr bounds)))−249
(font-face-set-size! face size scale: scale) 298
(let loop ((characters (string->list text)) (index 0)) 299
(unless (null? characters) 300
(let* ((codepoint (char->integer (car characters)))−253
(glyph-index (font-glyph-index face codepoint))+301
(selected (select-glyph font face bold? codepoint))+302
(selected-face (car selected))+303
(glyph-index (cadr selected)) 304
(left (pixel (* (+ x (* index advance)) scale)))) 305
(if (= glyph-index 0) 306
(replacement-box! canvas left baseline (* advance scale) size scale color bounds)−257
(let ((glyph (glyph-mask font face bold? size scale glyph-index base)))+307
(let ((glyph (glyph-mask font selected-face bold? size scale glyph-index base))) 308
(when (and (> (dict-ref glyph width:) 0) (> (dict-ref glyph height:) 0)) 309
(canvas-mask! canvas (+ left (dict-ref glyph left:)) 310
(- baseline (dict-ref glyph top:)) 311
(dict-ref glyph coverage:) (dict-ref glyph width:) color bounds))))−262
(loop (cdr characters) (+ index 1))))))+312
(loop (cdr characters) (+ index (caddr selected))))))) 313
width))))test/fixtures/Noto-Symbols-OFL.txtadded
@@ -0,0 +1,93 @@
+1
Copyright 2022 The Noto Project Authors (https://github.com/notofonts/symbols)+2
+3
This Font Software is licensed under the SIL Open Font License, Version 1.1.+4
This license is copied below, and is also available with a FAQ at:+5
https://scripts.sil.org/OFL+6
+7
+8
-----------------------------------------------------------+9
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007+10
-----------------------------------------------------------+11
+12
PREAMBLE+13
The goals of the Open Font License (OFL) are to stimulate worldwide+14
development of collaborative font projects, to support the font creation+15
efforts of academic and linguistic communities, and to provide a free and+16
open framework in which fonts may be shared and improved in partnership+17
with others.+18
+19
The OFL allows the licensed fonts to be used, studied, modified and+20
redistributed freely as long as they are not sold by themselves. The+21
fonts, including any derivative works, can be bundled, embedded, +22
redistributed and/or sold with any software provided that any reserved+23
names are not used by derivative works. The fonts and derivatives,+24
however, cannot be released under any other type of license. The+25
requirement for fonts to remain under this license does not apply+26
to any document created using the fonts or their derivatives.+27
+28
DEFINITIONS+29
"Font Software" refers to the set of files released by the Copyright+30
Holder(s) under this license and clearly marked as such. This may+31
include source files, build scripts and documentation.+32
+33
"Reserved Font Name" refers to any names specified as such after the+34
copyright statement(s).+35
+36
"Original Version" refers to the collection of Font Software components as+37
distributed by the Copyright Holder(s).+38
+39
"Modified Version" refers to any derivative made by adding to, deleting,+40
or substituting -- in part or in whole -- any of the components of the+41
Original Version, by changing formats or by porting the Font Software to a+42
new environment.+43
+44
"Author" refers to any designer, engineer, programmer, technical+45
writer or other person who contributed to the Font Software.+46
+47
PERMISSION & CONDITIONS+48
Permission is hereby granted, free of charge, to any person obtaining+49
a copy of the Font Software, to use, study, copy, merge, embed, modify,+50
redistribute, and sell modified and unmodified copies of the Font+51
Software, subject to the following conditions:+52
+53
1) Neither the Font Software nor any of its individual components,+54
in Original or Modified Versions, may be sold by itself.+55
+56
2) Original or Modified Versions of the Font Software may be bundled,+57
redistributed and/or sold with any software, provided that each copy+58
contains the above copyright notice and this license. These can be+59
included either as stand-alone text files, human-readable headers or+60
in the appropriate machine-readable metadata fields within text or+61
binary files as long as those fields can be easily viewed by the user.+62
+63
3) No Modified Version of the Font Software may use the Reserved Font+64
Name(s) unless explicit written permission is granted by the corresponding+65
Copyright Holder. This restriction only applies to the primary font name as+66
presented to the users.+67
+68
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font+69
Software shall not be used to promote, endorse or advertise any+70
Modified Version, except to acknowledge the contribution(s) of the+71
Copyright Holder(s) and the Author(s) or with their explicit written+72
permission.+73
+74
5) The Font Software, modified or unmodified, in part or in whole,+75
must be distributed entirely under this license, and must not be+76
distributed under any other license. The requirement for fonts to+77
remain under this license does not apply to any document created+78
using the Font Software.+79
+80
TERMINATION+81
This license becomes null and void if any of the above conditions are+82
not met.+83
+84
DISCLAIMER+85
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+86
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF+87
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT+88
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE+89
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,+90
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL+91
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING+92
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM+93
OTHER DEALINGS IN THE FONT SOFTWARE.test/fixtures/NotoSansSymbols2-Regular.ttfbinary
Binary. Not rendered here.
test/fixtures/README.mdmodified
@@ -10,3 +10,10 @@ the same fixtures used by sigil-font's portable conformance tests.
10
The default suite loads these files directly without installing fonts or 11
requiring an environment variable. `SIGIL_UI_TEST_REGULAR` and 12
`SIGIL_UI_TEST_BOLD` can override their paths.+13
+14
Noto Sans Symbols 2 Regular is the unmodified fallback fixture used by+15
sigil-font, from google/fonts commit+16
`5bb28733cec313b63fbd415b574b5ae35c8aa8b8`, `ofl/notosanssymbols2`.+17
Its license is `Noto-Symbols-OFL.txt`; its identity and license hash are in+18
`SHA256SUMS`. Fallback tests use glyphs absent from JetBrains Mono and a+19
face-local glyph-ID collision to check selection and cache isolation.test/fixtures/SHA256SUMSmodified
@@ -1,3 +1,5 @@
1
a0bf60ef0f83c5ed4d7a75d45838548b1f6873372dfac88f71804491898d138f JetBrainsMono-Regular.ttf 2
5590990c82e097397517f275f430af4546e1c45cff408bde4255dad142479dcb JetBrainsMono-Bold.ttf 3
30f0c136e3c88e422d0791acd97238870f9054a9729bc34cf2ff0d4ed8cac4ad OFL.txt+4
7d5fb73b7ca67a6798101741f5d280a3d016a56a197afcd4199dbb57b4b82a21 NotoSansSymbols2-Regular.ttf+5
b118dd41337806a5d4797052c77caf3bd096aed783e5eb21b4d11154351e1ac0 Noto-Symbols-OFL.txttest/fixtures/cjk-resource.sgvadded
@@ -0,0 +1,7 @@
+1
(font-resources+2
(file "NotoSansCJK-Regular.ttc"+3
"https://raw.githubusercontent.com/notofonts/noto-cjk/f8d157532fbfaeda587e826d4cd5b21a49186f7c/Sans/OTC/NotoSansCJK-Regular.ttc"+4
19484784 "b76b0433203017ca80401b2ee0dd69350349871c4b19d504c34dbdd80541690a")+5
(file "Noto-CJK-LICENSE.txt"+6
"https://raw.githubusercontent.com/notofonts/noto-cjk/f8d157532fbfaeda587e826d4cd5b21a49186f7c/Sans/LICENSE"+7
4301 "6a73f9541c2de74158c0e7cf6b0a58ef774f5a780bf191f2d7ec9cc53efe2bf2"))test/test-text-fallback.sgladded
@@ -0,0 +1,114 @@
+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))))))))+38
+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)))))+61
+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))))+86
+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)))))+108
+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: (make-list 9 symbols)))+114
(assert-error (make-text-font regular bold fallback-files: (list "missing-fallback.ttf"))))