AtlatestRepositorysigil-ui
1
# Grayscale monospace text3
`(sigil ui text)` draws outline text into the existing software raster. It4
loads explicit Regular and Bold font files as owned bytes through `(sigil font)`,5
with native grayscale coverage. The selected backend is FreeType on Linux,6
CoreText on macOS. Windows has no font backend in sigil-font 0.1.0. Cache and7
monospace layout use only the shared font interface. Byte loading does not8
initialize a system font inventory or change environment values. Optional9
family discovery is supported; shaping is a future addition. Each backend10
requires its own platform qualification.12
```scheme13
(import (sigil ui text) (sigil ui raster) (sigil ui display))15
(define font (make-text-font "fonts/Regular.ttf" "fonts/Bold.ttf"))16
(dynamic-wind17
(lambda () #f)18
(lambda ()19
(define canvas (make-canvas 640 96 #x101010))20
(draw-text! font canvas 8 4 "Oriel λ Ω Ж" 14 #xe6e2d5 scale: 2)21
(render-items! canvas22
(list (mono-text-item font 8 24 "NAV" 14 #xedbb45 bold?: #t)) 2))23
(lambda () (text-font-close! font)))24
```26
Positions and sizes use logical pixels. Drawing applies an integer surface27
scale from 1 to 8; the raster receives device pixels. The top position is the28
top of the measured line, not its baseline. Size must be positive and at most29
256 logical pixels. `bold?: #t` selects the supplied Bold face.31
`(text-metrics font size)` returns `advance:`, `ascent:`, `descent:` and32
`height:`. Both primary faces contribute to a shared monospace cell, while33
all opened faces contribute to the line height,34
so changing weight does not move the caret. The advance retains the font's35
fractional logical width. Drawing rounds each glyph origin after applying36
the output scale, keeping columns aligned without accumulating rounding error.37
Vertical metrics round outward to whole logical pixels. `(measure-text font38
text size)` returns the primary-cell width of the selected glyph run.39
Its optional `bold?:` selects the same face policy as drawing.41
Consumers should round column capacities down, keep text positions real, and42
round positions only after applying the output scale. Use the same advance43
for glyph columns. When opting into fallback files, measure prefixes with44
`measure-text` for clipping, cursor placement and text hit testing.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 stays48
inside the measured run and the raster's bounds. `mono-text-item` supplies49
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.52
Without fallback files, each Unicode code point keeps its existing single53
primary cell. Optional `fallback-files:` is an ordered list of up to eight54
explicit font paths. Their total size is limited to 64 MiB; missing, empty55
or oversized inputs raise. Each default collection face is opened once and56
can serve either weight without synthesizing Bold. Applications choose the57
files; rendering never scans installed fonts or changes font configuration.59
Lookup tries the selected primary face, then the fallback faces in order.60
A fallback glyph consumes enough whole primary cells for its unhinted61
advance, with a minimum of one. This keeps wider CJK glyphs from overlapping62
the next character and keeps measurement independent of the output scale.63
Line metrics include the fallbacks to avoid clipping their ascenders or64
descenders. For example, an application with a bundled CJK collection can65
pass `fallback-files: (list "fonts/NotoSansCJK-Regular.ttc")`.67
The renderer does not shape, join, reorder or combine characters and does68
not enable ligatures. The original string is retained. A code point absent69
from every selected face draws a visible replacement box, with no silent70
question-mark substitution. Color emoji and grapheme layout are outside71
this interface.73
Each font owns its faces and copied masks. Call `text-font-close!`74
after discarding its display items, including when an operation fails.75
Repeated close is safe. Every fallback and both primary closes are attempted76
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 backend78
also supplies a native guardian for abandoned faces. Input files are bounded79
to 64 MiB each before reading; the native face owns its independent byte copy.80
Missing, empty and oversized files raise distinct errors that include the path.82
`make-text-font` accepts optional `family:` discovery through `(sigil font)`.83
The family is trimmed before lookup; matching ignores case. Choose a monospace84
family: proportional glyphs wider than M can overlap the next cell. It uses the85
installed family only when both real Regular and Bold faces exist. Otherwise,86
including when discovery raises, it closes any partial pair and opens the87
supplied files. Errors opening those files still raise.89
`text-font-source` returns `family` or `files`, distinguishing fallback even90
when the files share the requested family name. `text-font-discovery-error`91
returns the original discovery exception, or `#f` when discovery did not raise.92
`text-font-family` returns the selected face's family name; compare names93
case-insensitively. Omitting `family:` uses only the explicit files, without94
system discovery.96
The mask cache uses least-recently-used eviction with independent byte and97
glyph-count limits: 4 MiB and 1024 entries by default. Override them with98
`cache-bytes:` and `cache-glyphs:` when creating the font. A mask larger than99
the cache budget still renders but is not retained. Cache keys distinguish100
the actual face, weight, logical size, scale and glyph index. Face-local IDs101
cannot collide across primary and fallback fonts. Size/scale changes cannot102
reuse a mask with different rasterization inputs. Hash buckets and use stamps103
make hits avoid rebuilding the cache; only insertion overflow scans for the104
oldest entry. `text-cache-stats` reports entries,105
bytes, hits, misses and limits for qualification. Temporary coverage copies106
also obey the font interface's owned-coverage limit; native rasterizer temporary107
allocations are separate. Superseded coverage copies are not retained alongside108
cached masks. Byte limits count mask coverage, separately from cache metadata.109
Small one-pixel metric rasters used for fallback cell counts share these110
same byte and entry limits.112
Tests default to the checked-in OFL JetBrains Mono Regular and Bold faces in113
`test/fixtures/`, with their license and SHA-256 manifest. Optional114
`SIGIL_UI_TEST_REGULAR` and `SIGIL_UI_TEST_BOLD` override those paths. Plain115
`sigil test` needs no font environment variables. Tests check antialiasing,116
baseline/bearing/row placement at scales 1 and 2, weight, clipping/repaint,117
missing-glyph cell bounds, eviction order, cache limits and lifecycle. No118
installed host font or desktop service is required.120
`test/test-text-fallback.sgl` uses the pinned Noto Sans Symbols2 fixture to121
check independent raster references, face-local glyph-ID cache collisions,122
whole-cell advances and lifecycle/bounds. For real CJK coverage, the separate123
`scripts/check-text-cjk.sgl` test uses the licensed collection identified by124
`test/fixtures/cjk-resource.sgv`. Supply its verified path as125
`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 for127
14/20-pixel Regular/Bold captures at scales1/2. The large collection is a128
qualification resource, not a bundled library dependency.