Atv0.2.2Repositorysigil-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 faces contribute to a shared monospace cell and 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) is the string's code-point count times the fractional advance.

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, cursor placement and any 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.

This first API gives each Unicode code point one cell. It does not shape, join, reorder or combine characters, and does not enable ligatures. The original string is retained. A code point absent from the selected face draws a visible replacement box; CJK and emoji therefore remain observable when a Latin/Greek/Cyrillic face lacks them. There is no silent question-mark substitution. A discovery/shaping layer can supply richer fallback later.

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. Closing Regular is still attempted if closing Bold 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 face weight, logical size, scale and glyph index. 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.

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.