Atv0.2.4Repositorysigil-ui
Document

Grayscale monospace text

(sigil ui text) draws outline text into the existing software raster. It loads explicit Regular and Bold font files as owned bytes through (sigil font), with native grayscale coverage. The selected backend is FreeType on Linux, CoreText on macOS. Windows has no font backend in sigil-font 0.1.0. Cache and monospace layout use only the shared font interface. Byte loading does not initialize a system font inventory or change environment values. Optional family discovery is supported; shaping is a future addition. Each backend requires its own platform qualification.

(import (sigil ui text) (sigil ui raster) (sigil ui display))

(define font (make-text-font "fonts/Regular.ttf" "fonts/Bold.ttf"))
(dynamic-wind
  (lambda () #f)
  (lambda ()
    (define canvas (make-canvas 640 96 #x101010))
    (draw-text! font canvas 8 4 "Oriel λ Ω Ж" 14 #xe6e2d5 scale: 2)
    (render-items! canvas
      (list (mono-text-item font 8 24 "NAV" 14 #xedbb45 bold?: #t)) 2))
  (lambda () (text-font-close! font)))

Positions and sizes use logical pixels. Drawing applies an integer surface scale from 1 to 8; the raster receives device pixels. The top position is the top of the measured line, not its baseline. Size must be positive and at most 256 logical pixels. bold?: #t selects the supplied Bold face.

(text-metrics font size) returns advance:, ascent:, descent: and height:. Both primary faces contribute to a shared monospace cell, while all opened faces contribute to the line height, so changing weight does not move the caret. The advance retains the font's fractional logical width. Drawing rounds each glyph origin after applying the output scale, keeping columns aligned without accumulating rounding error. Vertical metrics round outward to whole logical pixels. (measure-text font text size) returns the primary-cell width of the selected glyph run. Its optional bold?: selects the same face policy as drawing.

Consumers should round column capacities down, keep text positions real, and round positions only after applying the output scale. Use the same advance for glyph columns. When opting into fallback files, measure prefixes with measure-text for clipping, cursor placement and text hit testing.

(draw-text! font canvas x y text size color) returns that logical width. Optional clip: is a device-pixel (x0 y0 x1 y1) rectangle. Drawing stays inside the measured run and the raster's bounds. mono-text-item supplies the same text in a display list, with bounds, translation and damage clipping. The existing bitmap text-item and font-set APIs remain available.

Without fallback files, each Unicode code point keeps its existing single primary cell. Optional fallback-files: is an ordered list of up to eight explicit font paths. Their total size is limited to 64 MiB; missing, empty or oversized inputs raise. Each default collection face is opened once and can serve either weight without synthesizing Bold. Applications choose the files; rendering never scans installed fonts or changes font configuration.

Lookup tries the selected primary face, then the fallback faces in order. A fallback glyph consumes enough whole primary cells for its unhinted advance, with a minimum of one. This keeps wider CJK glyphs from overlapping the next character and keeps measurement independent of the output scale. Line metrics include the fallbacks to avoid clipping their ascenders or descenders. For example, an application with a bundled CJK collection can pass fallback-files: (list "fonts/NotoSansCJK-Regular.ttc").

The renderer does not shape, join, reorder or combine characters and does not enable ligatures. The original string is retained. A code point absent from every selected face draws a visible replacement box, with no silent question-mark substitution. Color emoji and grapheme layout are outside this interface.

Each font owns its faces and copied masks. Call text-font-close! after discarding its display items, including when an operation fails. Repeated close is safe. Every fallback and both primary closes are attempted even if an earlier close raises. Drawing or measuring a closed font raises an error. Constructing a font with a bad second face closes the first face. Each backend also supplies a native guardian for abandoned faces. Input files are bounded to 64 MiB each before reading; the native face owns its independent byte copy. Missing, empty and oversized files raise distinct errors that include the path.

make-text-font accepts optional family: discovery through (sigil font). The family is trimmed before lookup; matching ignores case. Choose a monospace family: proportional glyphs wider than M can overlap the next cell. It uses the installed family only when both real Regular and Bold faces exist. Otherwise, including when discovery raises, it closes any partial pair and opens the supplied files. Errors opening those files still raise.

text-font-source returns family or files, distinguishing fallback even when the files share the requested family name. text-font-discovery-error returns the original discovery exception, or #f when discovery did not raise. text-font-family returns the selected face's family name; compare names case-insensitively. Omitting family: uses only the explicit files, without system discovery.

The mask cache uses least-recently-used eviction with independent byte and glyph-count limits: 4 MiB and 1024 entries by default. Override them with cache-bytes: and cache-glyphs: when creating the font. A mask larger than the cache budget still renders but is not retained. Cache keys distinguish the actual face, weight, logical size, scale and glyph index. Face-local IDs cannot collide across primary and fallback fonts. Size/scale changes cannot reuse a mask with different rasterization inputs. Hash buckets and use stamps make hits avoid rebuilding the cache; only insertion overflow scans for the oldest entry. text-cache-stats reports entries, bytes, hits, misses and limits for qualification. Temporary coverage copies also obey the font interface's owned-coverage limit; native rasterizer temporary allocations are separate. Superseded coverage copies are not retained alongside cached masks. Byte limits count mask coverage, separately from cache metadata. Small one-pixel metric rasters used for fallback cell counts share these same byte and entry limits.

Tests default to the checked-in OFL JetBrains Mono Regular and Bold faces in test/fixtures/, with their license and SHA-256 manifest. Optional SIGIL_UI_TEST_REGULAR and SIGIL_UI_TEST_BOLD override those paths. Plain sigil test needs no font environment variables. Tests check antialiasing, baseline/bearing/row placement at scales 1 and 2, weight, clipping/repaint, missing-glyph cell bounds, eviction order, cache limits and lifecycle. No installed host font or desktop service is required.

test/test-text-fallback.sgl uses the pinned Noto Sans Symbols2 fixture to check independent raster references, face-local glyph-ID cache collisions, whole-cell advances and lifecycle/bounds. For real CJK coverage, the separate scripts/check-text-cjk.sgl test uses the licensed collection identified by test/fixtures/cjk-resource.sgv. Supply its verified path as SIGIL_UI_TEST_CJK when running it through the native UI test runner; optional SIGIL_UI_TEST_CJK_SHOTS names an existing output directory for 14/20-pixel Regular/Bold captures at scales1/2. The large collection is a qualification resource, not a bundled library dependency.