AtlatestRepositorysigil-ui
1# Grayscale monospace text
2
3`(sigil ui text)` draws outline text into the existing software raster. It
4loads explicit Regular and Bold font files as owned bytes through `(sigil font)`,
5with native grayscale coverage. The selected backend is FreeType on Linux,
6CoreText on macOS. Windows has no font backend in sigil-font 0.1.0. Cache and
7monospace layout use only the shared font interface. Byte loading does not
8initialize a system font inventory or change environment values. Optional
9family discovery is supported; shaping is a future addition. Each backend
10requires its own platform qualification.
12```scheme
13(import (sigil ui text) (sigil ui raster) (sigil ui display))
15(define font (make-text-font "fonts/Regular.ttf" "fonts/Bold.ttf"))
16(dynamic-wind
17 (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! canvas
22 (list (mono-text-item font 8 24 "NAV" 14 #xedbb45 bold?: #t)) 2))
23 (lambda () (text-font-close! font)))
24```
26Positions and sizes use logical pixels. Drawing applies an integer surface
27scale from 1 to 8; the raster receives device pixels. The top position is the
28top of the measured line, not its baseline. Size must be positive and at most
29256 logical pixels. `bold?: #t` selects the supplied Bold face.
31`(text-metrics font size)` returns `advance:`, `ascent:`, `descent:` and
32`height:`. Both primary faces contribute to a shared monospace cell, while
33all opened faces contribute to the line height,
34so changing weight does not move the caret. The advance retains the font's
35fractional logical width. Drawing rounds each glyph origin after applying
36the output scale, keeping columns aligned without accumulating rounding error.
37Vertical metrics round outward to whole logical pixels. `(measure-text font
38text size)` returns the primary-cell width of the selected glyph run.
39Its optional `bold?:` selects the same face policy as drawing.
41Consumers should round column capacities down, keep text positions real, and
42round positions only after applying the output scale. Use the same advance
43for glyph columns. When opting into fallback files, measure prefixes with
44`measure-text` for clipping, cursor placement and text hit testing.
46`(draw-text! font canvas x y text size color)` returns that logical width.
47Optional `clip:` is a device-pixel `(x0 y0 x1 y1)` rectangle. Drawing stays
48inside the measured run and the raster's bounds. `mono-text-item` supplies
49the same text in a display list, with bounds, translation and damage clipping.
50The existing bitmap `text-item` and font-set APIs remain available.
52Without fallback files, each Unicode code point keeps its existing single
53primary cell. Optional `fallback-files:` is an ordered list of up to eight
54explicit font paths. Their total size is limited to 64 MiB; missing, empty
55or oversized inputs raise. Each default collection face is opened once and
56can serve either weight without synthesizing Bold. Applications choose the
57files; rendering never scans installed fonts or changes font configuration.
59Lookup tries the selected primary face, then the fallback faces in order.
60A fallback glyph consumes enough whole primary cells for its unhinted
61advance, with a minimum of one. This keeps wider CJK glyphs from overlapping
62the next character and keeps measurement independent of the output scale.
63Line metrics include the fallbacks to avoid clipping their ascenders or
64descenders. For example, an application with a bundled CJK collection can
65pass `fallback-files: (list "fonts/NotoSansCJK-Regular.ttc")`.
67The renderer does not shape, join, reorder or combine characters and does
68not enable ligatures. The original string is retained. A code point absent
69from every selected face draws a visible replacement box, with no silent
70question-mark substitution. Color emoji and grapheme layout are outside
71this interface.
73Each font owns its faces and copied masks. Call `text-font-close!`
74after discarding its display items, including when an operation fails.
75Repeated close is safe. Every fallback and both primary closes are attempted
76even if an earlier close raises. Drawing or measuring a closed font raises an error.
77Constructing a font with a bad second face closes the first face. Each backend
78also supplies a native guardian for abandoned faces. Input files are bounded
79to 64 MiB each before reading; the native face owns its independent byte copy.
80Missing, empty and oversized files raise distinct errors that include the path.
82`make-text-font` accepts optional `family:` discovery through `(sigil font)`.
83The family is trimmed before lookup; matching ignores case. Choose a monospace
84family: proportional glyphs wider than M can overlap the next cell. It uses the
85installed family only when both real Regular and Bold faces exist. Otherwise,
86including when discovery raises, it closes any partial pair and opens the
87supplied files. Errors opening those files still raise.
89`text-font-source` returns `family` or `files`, distinguishing fallback even
90when the files share the requested family name. `text-font-discovery-error`
91returns the original discovery exception, or `#f` when discovery did not raise.
92`text-font-family` returns the selected face's family name; compare names
93case-insensitively. Omitting `family:` uses only the explicit files, without
94system discovery.
96The mask cache uses least-recently-used eviction with independent byte and
97glyph-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
99the cache budget still renders but is not retained. Cache keys distinguish
100the actual face, weight, logical size, scale and glyph index. Face-local IDs
101cannot collide across primary and fallback fonts. Size/scale changes cannot
102reuse a mask with different rasterization inputs. Hash buckets and use stamps
103make hits avoid rebuilding the cache; only insertion overflow scans for the
104oldest entry. `text-cache-stats` reports entries,
105bytes, hits, misses and limits for qualification. Temporary coverage copies
106also obey the font interface's owned-coverage limit; native rasterizer temporary
107allocations are separate. Superseded coverage copies are not retained alongside
108cached masks. Byte limits count mask coverage, separately from cache metadata.
109Small one-pixel metric rasters used for fallback cell counts share these
110same byte and entry limits.
112Tests default to the checked-in OFL JetBrains Mono Regular and Bold faces in
113`test/fixtures/`, with their license and SHA-256 manifest. Optional
114`SIGIL_UI_TEST_REGULAR` and `SIGIL_UI_TEST_BOLD` override those paths. Plain
115`sigil test` needs no font environment variables. Tests check antialiasing,
116baseline/bearing/row placement at scales 1 and 2, weight, clipping/repaint,
117missing-glyph cell bounds, eviction order, cache limits and lifecycle. No
118installed host font or desktop service is required.
120`test/test-text-fallback.sgl` uses the pinned Noto Sans Symbols2 fixture to
121check independent raster references, face-local glyph-ID cache collisions,
122whole-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;
126optional `SIGIL_UI_TEST_CJK_SHOTS` names an existing output directory for
12714/20-pixel Regular/Bold captures at scales1/2. The large collection is a
128qualification resource, not a bundled library dependency.