ui_controls

module pkg::alloy::textsource

Text in pure Alloy (PLAN.md, stages F4 and F10): TrueType faces, text split into runs by script and emoji presentation, shaping by the faces' OpenType features, line breaking by UAX #14 with carets on grapheme clusters by UAX #29, and glyphs rasterized into coverage masks. It knows nothing of the GPU: pkg::alloy::draw draws a layout. Text is shaped as HarfBuzz shapes it. The bundled font is Inter Regular, made bold by thickening it. Two packages install more on the fonts (REFOCUS.md, R6d): pkg::alloy::text_scripts the complex scripts' shapers and both directions mixed by UAX #9, pkg::alloy::text_fonts CFF, variable and color faces (COLR, CBDT, sbix, as premultiplied RGBA) and fallback to the system's fonts. Without them every run is shaped by the default shaper, left to right, and characters Inter lacks show as boxes, the same on every target.

var fonts = Fonts::create();
const style = TextStyle::of(fonts.default(), 15.0);
const layout = Layout::create(&fonts, &"Hello, world.", &style, 300.0);
const at = layout.hit(40.0, 8.0); // a byte offset on a grapheme boundary
const caret = layout.caret(at);

## How it was built

Notes kept from each stage as it was finished, the sizes and numbers of their day; what changed since is in REVIEW.md and the history.

**F4 as built** (2026-10-12):

| | lavapipe / biscuit's CPU | GTX 1660 | RTX 4070 Ti | |---|---|---|---| | laying out 10,000 words in 100 paragraphs | 30 | | 17 | | one of those paragraphs again | 0.27 | | 0.15 | | rasterizing 94 ASCII glyphs at 12, 16 and 32 px | 1.5 | | 0.84 | | a 1920x1080 page of text, glyphs rasterized first | 25 | 6.7 | 3.9 | | that page drawn again | 21 | 2.3 | 1.4 |

Hello world with text presents its first frame 64 ms after main under weston on lavapipe, at 86 MB resident (lavapipe's LLVM again); it is 1.23 MB natively and 0.94 MB of wasm, of which the faces are 206 KB and the Unicode tables about 70 KB. Release wasm modules no longer carry their name section (63 KB here; decided 2026-10-12), which also took the cards startup to 0.54 MB of wasm.

**F10a as built** (2026-10-14; decisions/F10.md):

**F10b as built** (2026-10-14; decisions/F10.md, "Bidi"):

**F10c as built** (2026-10-14; decisions/F10.md, "Shaping"):

**F10d as built** (2026-10-15; decisions/F10.md, "Color emoji"):

**F14b as built** (2026-10-20; decisions/F14.md, "Words"): UAX #29's word boundaries (Unicode 16.0) in unicode/words.alloy, from WordBreakProperty.txt, pinned by unicode/fetch.sh and generated by generate.alloy into WORD_BREAKS (1888 runs, the property in the low five bits) and the WordBreak enum; Extended_Pictographic is the tables' own. word_boundaries gives every boundary, WB1 to WB999 with WB4's ignored characters standing for what they follow; word_starts(text) the boundaries before a letter, a digit, Katakana, an ideograph (line breaking's ID), an emoji or a flag, never inside a run of Thai, Lao, Khmer or Myanmar (line breaking's SA, no dictionary here), so a word runs from its start to the next one's, the spaces after it included. unicode/conformance.alloy words passes all 1826 cases of WordBreakTest.txt (pinned beside the tables and run by scripts/test.sh with the other UCD tests there rather than in tests.alloy, which checks the starts: an apostrophe, a decimal number, snake case, a decomposed "é", Arabic, Thai, Japanese, a family emoji). About 7.5 KB of table and 5 KB of code in every ui program, which ui's maps and TextField's word moves reach. Layout::graphemes_in_order gives each grapheme drawn, its bytes and its left and right edges, in the text's order, for those maps.

**F14 question 11A as built** (2026-10-20): Direction::AutoRightToLeft reads a paragraph by its first strong character as Auto does, but right to left without one (bidi_levels' fallback, HL1), an empty text and an empty first paragraph too, as an element reading right to left does with dir=auto: "-5 (2/3)" shows "(2/3) 5-", "›" its mirror. Tested in tests.alloy.

**F14c as built** (2026-10-20; decisions/F14.md, question 10A): the system's fallback faces (fallback.alloy's system_family) and the files fetch_font reads natively are mapped through std::fs's KeptMappedFile (FaceBytes::Mapped), not read whole, so only the parts touched (directory, cmap, the glyphs drawn) are resident and the system may drop them again; they stay mapped as long as the program runs, as their faces do (question 13). Where nothing maps (the web, WASI, alloyc run) the file is read whole as before. In the glyph cache's long run (tests/glyph_memory.alloy, 24 scripts and emoji) fontconfig gives 18 files, 30.7 MB (Noto Sans CJK's collection 19 MB), of which 8.9 MB are resident at the end; read whole, the run was about 25 MB larger (73.5 MB against 48.6 MB). About 4 KB natively and 2.4 KB of wasm in every program drawing text.

**R6d as built** (2026-10-22; REFOCUS.md, questions 5A, 6B and 7B):

type Fontssource

exp type Fonts = struct

The faces a program lays text out with, in families: the bundled Inter (regular, bold made by thickening it) first, then the fonts a program adds and those fallback finds for characters a style's faces lack, once pkg::alloy::text_fonts is installed. Laying text out adds faces (a variable face's instances at the weights and sizes styles ask for), so they are kept behind a lock, reached through a &Fonts.

Functions

type FontStoresource

exp type FontStore = struct

Fields

exp faces: Vector<*Face>

each face boxed, so that it never moves: a reference to one lasts as long as the fonts

exp families: Vector<Vector<u32>>

each family's faces

exp system: bool

whether fallback may ask the system for its fonts (Fonts::create)

exp scripts: Option<ScriptHooks>

what text_scripts and text_fonts install

exp formats: Option<FontFormats>
exp fallback: Option<*var FontFallback>

Functions

type Familysource

exp type Family = struct

A family of faces, of different weights.

Fields

exp index: u32

const REGULARsource

exp const REGULAR: u32 = 400;

Weights, as OS/2's usWeightClass gives them.

const SEMIBOLDsource

exp const SEMIBOLD: u32 = 600;

const BOLDsource

exp const BOLD: u32 = 700;

type ScriptHookssource

exp type ScriptHooks = struct

What pkg::alloy::text_scripts installs on the fonts (install_scripts): the complex shapers, the language systems of BCP 47 tags, and bidi. Without it every run is shaped by the default shaper and laid out left to right.

Fields

exp shaper: (Script, u32) -> Option<*var ComplexShaper>

the complex shaper for a run of the script in a face whose GSUB was read for the tag, none for the default shaper

exp languages: (&[u8]) -> Tags

the language systems the BCP 47 tag names, in the order a face's are searched

exp mirror: (u32) -> Option<u32>

the character mirroring the code point (Bidi_Mirroring_Glyph)

exp levels: (&[u8], u64, u64, Direction) -> ParagraphLevels

the levels of the paragraph's bytes from start to end (UAX #9)

exp reorder: (&[u32], &var [u8], u8, &var [u64])

a line's glyphs in visual order (L1, L2): from the code points their clusters start with and their levels, and the paragraph's level, the levels L1 resets and the glyphs' order from the left

interface FontFallbacksource

exp interface FontFallback

What pkg::alloy::text_fonts installs for characters a style's faces lack: faces from the system's fonts, and from files a program fetches.

Required functions

fn face( self: &var, store: &var FontStore, text: &[u8], start: u64, end: u64, wanted: &Wanted, primary: u32 ) -> Option<u32>;

a face covering the grapheme cluster from start to end that the face primary lacks

fn fetch(self: &var, store: &var FontStore, path: &[u8]);

starts fetching a font file served beside the program

type Wantedsource

exp type Wanted = struct

What a run asks fallback for: its script, language, weight and size, and whether it is in emoji presentation.

Fields

exp script: Script
exp language: &[u8]
exp weight: u32
exp size: f32
exp emoji: bool

fn Fonts::createsource

exp fn Fonts::create() -> Fonts

fn Fonts::bundledsource

exp fn Fonts::bundled() -> Fonts

The bundled family alone, fallback never asking the system: the same faces on every machine, for tests and programs carrying the fonts they need (add, and text_fonts' fetch).

fn install_scriptssource

exp fn install_scripts(self fonts: &var Fonts, hooks: ScriptHooks)

Installs the complex shapers and bidi (pkg::alloy::text_scripts' install_into), for text laid out from here on.

fn install_formatssource

exp fn install_formats(self fonts: &var Fonts, formats: FontFormats, fallback: *var FontFallback)

Installs the font formats and fallback (pkg::alloy::text_fonts' install_into), for the fonts added from here on.

fn scriptssource

exp fn scripts(self fonts: &Fonts) -> Option<ScriptHooks>

What text_scripts installed, if it was.

fn read_facesource

exp fn read_face(self store: &FontStore, bytes: FaceBytes, index: u32) -> Option<Face>

The face of the font's bytes (a collection's by index), read with the formats installed; None when it does not read.

fn keepsource

exp fn keep(self store: &var FontStore, face: Face) -> u32

Keeps the face, giving its index.

fn family_ofsource

exp fn family_of(self store: &var FontStore, face: Face) -> u32

Keeps the face as a family of its own, giving the family's index.

fn fallbacksource

exp fn fallback(
    self fonts: &Fonts,
    text: &[u8],
    start: u64,
    end: u64,
    wanted: &Wanted,
    primary: u32
) -> Option<u32>

A face covering the grapheme cluster from start to end that the face primary lacks, from text_fonts' fallback.

fn fetchsource

exp fn fetch(self fonts: &Fonts, path: &[u8])

Starts fetching a font file served beside the program, for fallback once pkg::alloy::text_fonts is installed (on the web, which gives a page no system fonts): characters it covers lay out in it once it has arrived, which pkg::alloy::window tells as Event::FontsArrived, for laying text out again. Elsewhere the file is read now.

fn plansource

exp fn plan(
    self fonts: &Fonts,
    face: u32,
    script: Script,
    language: &[u8],
    rtl: bool,
    features: &[Feature]
) -> &Plan

The plan for shaping runs of the script, language, direction and features in the face, made the first time it is asked for.

fn defaultsource

exp fn default(self fonts: &Fonts) -> Family

The bundled family, Inter.

fn addsource

exp fn add(self fonts: &var Fonts, bytes: Vector<u8>) -> Option<Family>

A font's face (a .ttf or .otf file's bytes, TrueType, CFF or CFF2 outlines, or a collection's first face) as a family of its own: a variable face's named instances are the family's faces. None when it does not read.

fn add_tosource

exp fn add_to(self fonts: &var Fonts, family: Family, bytes: Vector<u8>) -> bool

Adds a face of another weight to the family (a variable face's named instances); false when it does not read.

fn family_sizesource

exp fn family_size(self fonts: &Fonts, family: Family) -> u64

How many faces the family has.

fn face_namesource

exp fn face_name(self fonts: &Fonts, face: u32) -> String

The PostScript name of the face at the index (a placed glyph's), as its font names it.

fn facesource

exp fn face(self fonts: &Fonts, index: u32) -> &Face

The face at the index.

fn face_forsource

exp fn face_for(self fonts: &Fonts, family: Family, weight: u32, size: f32) -> u32

The family's face nearest the weight; a variable one at the weight and, with an optical size axis, the size in pixels.

fn face_forsource

exp fn face_for(self store: &var FontStore, family: Family, weight: u32, size: f32) -> u32

type GlyphMasksource

exp type GlyphMask = struct

A glyph's coverage at the size in pixels, moved right by shift, a fraction of a pixel.

Fields

exp width: u32
exp height: u32
exp left: i32

from the pen, on the baseline, to the mask's top left

exp top: i32
exp coverage: Vector<u8>

a byte a pixel, row after row

fn glyph_masksource

exp fn glyph_mask(self fonts: &Fonts, face: u32, glyph: u32, size: f32, shift: f32) -> GlyphMask

type ColorMasksource

exp type ColorMask = struct

A color glyph's pixels (COLR's layers or paints, a CBDT or sbix bitmap): premultiplied RGBA, four bytes a pixel, placed as a mask is.

Fields

exp width: u32
exp height: u32
exp left: i32
exp top: i32
exp pixels: Vector<u8>
exp foreground: bool

whether it shows the text's color (a palette entry 0xFFFF), so is drawn anew for text of another color

type GlyphImagesource

exp type GlyphImage = enum

A glyph's pixels: an outline's coverage, or a color glyph's colors.

Variants

Coverage: GlyphMask
Color: ColorMask

fn is_colorsource

exp fn is_color(self fonts: &Fonts, face: u32, glyph: u32) -> bool

Whether the face draws the glyph in color (COLR, CBDT or sbix), whole pixels apart rather than at quarter-pixel shifts.

fn glyph_imagesource

exp fn glyph_image(
    self fonts: &Fonts,
    face: u32,
    glyph: u32,
    size: f32,
    shift: f32,
    foreground: TextColor
) -> GlyphImage

The glyph's pixels at the size in pixels, moved right by shift, a fraction of a pixel (an outline's): a color glyph's in its own colors, whatever the text's but where its palette takes the foreground, the text's color.

type TextColorsource

exp type TextColor = struct

A color, in sRGB with straight alpha, for spans drawn in another color than the text's.

Fields

exp r: f32
exp g: f32
exp b: f32
exp a: f32

Functions

fn TextColor::rgbsource

exp fn TextColor::rgb(r: f32, g: f32, b: f32) -> TextColor

type Featuresource

exp type Feature = struct

An OpenType feature turned on or off: Feature::on(&"tnum").

Fields

exp tag: u32
exp on: bool

Functions

fn Feature::onsource

exp fn Feature::on(name: &[u8]) -> Feature

fn Feature::offsource

exp fn Feature::off(name: &[u8]) -> Feature

type Directionsource

exp type Direction = enum

The direction a paragraph reads in: Auto takes its first strong character's (UAX #9's P2 and P3), left to right without one; AutoRightToLeft too, but right to left without one, as text in an element reading right to left does with dir=auto.

Variants

Auto
AutoRightToLeft
LeftToRight
RightToLeft

type TextStylesource

exp type TextStyle = struct

Fields

exp family: Family
exp size: f32

pixels

exp weight: u32

REGULAR, BOLD or between

exp line_height: f32

a multiple of the size, or 0 for the face's own spacing

exp color: Option<TextColor>

None: the color the text is drawn in

exp underline: bool
exp strikethrough: bool
exp features: Vector<Feature>
exp language: String

a BCP 47 tag, as "ar" or "zh-Hant", none for the user's: the language fallback asks fonts for

exp direction: Direction

the paragraphs' direction, by the style of their first byte

Functions

fn TextStyle::ofsource

exp fn TextStyle::of(family: Family, size: f32) -> TextStyle

type Spansource

exp type Span = struct

A range of the text, in bytes, in its own style.

Fields

exp start: u64
exp end: u64
exp style: TextStyle

type Alignsource

exp type Align = enum

Where lines go across the layout: Start and End on each paragraph's own side (the left left to right, the right right to left), Left and Right on those whatever the paragraph: a box's start, as ui aligns a text by its element's direction.

Variants

Start
Center
End
Left
Right

type PlacedGlyphsource

exp type PlacedGlyph = struct

A glyph placed: its face's index in the fonts, its pen position from the layout's top left, y on the baseline, and the byte offset of the text it shows; a line's glyphs are in visual order, from the left.

Fields

exp face: u32
exp glyph: u32
exp size: f32
exp x: f32
exp y: f32
exp cluster: u64
exp color: Option<TextColor>

type Linesource

exp type Line = struct

A line: its bytes, glyphs and box (spaces hanging past its end left out); baseline is from the layout's top; its paragraph's direction, LeftToRight or RightToLeft.

Fields

exp start: u64
exp end: u64
exp glyphs: [u64 : 2]
exp top: f32
exp baseline: f32
exp height: f32
exp x: f32
exp width: f32
exp direction: Direction
exp graphemes: [u64 : 2]

its graphemes in the layout's

type TextBoxsource

exp type TextBox = struct

A rectangle, in the layout's coordinates.

Fields

exp x: f32
exp y: f32
exp width: f32
exp height: f32

type Decorationsource

exp type Decoration = struct

An underline or strikethrough.

Fields

exp box: TextBox
exp color: Option<TextColor>

type Graphemesource

exp type Grapheme = struct

A grapheme cluster placed on its line: its bytes (a layout's text stays under 4 GiB), and the edges of its share of its glyphs' advance where a caret before and after it is drawn: its left and right, or its right and left right to left. A line's are in visual order, from the left.

Fields

exp at: u32
exp length: u32
exp leading: f32
exp trailing: f32

Functions

fn startsource

exp fn start(self g: &Grapheme) -> u64

fn endsource

exp fn end(self g: &Grapheme) -> u64

fn leftsource

exp fn left(self g: &Grapheme) -> f32

type Layoutsource

exp type Layout = struct

Fields

exp text: String
exp glyphs: Vector<PlacedGlyph>
exp lines: Vector<Line>
exp decorations: Vector<Decoration>
exp width: f32
exp height: f32
exp graphemes: Vector<Grapheme>

Functions

fn Layout::createsource

exp fn Layout::create(fonts: &Fonts, text: &[u8], style: &TextStyle, width: f32) -> Layout

The text in one style, broken into lines no wider than width (any width at all with f32 infinity or 0), aligned at the start.

fn Layout::spanssource

exp fn Layout::spans(
    fonts: &Fonts,
    text: &[u8],
    spans: &[Span],
    width: f32,
    align: Align
) -> Layout

The text in spans of styles, which cover it in order (a byte no span covers takes the first span's style).

fn Layout::fittedsource

exp fn Layout::fitted(fonts: &Fonts, text: &[u8], style: &TextStyle, width: f32) -> Layout

The text in one style broken at width as create breaks it, but as wide as its widest line: one line as wide as the text when it fits.

fn Layout::fittedsource

exp fn Layout::fitted(
    fonts: &Fonts,
    text: &[u8],
    style: &TextStyle,
    width: f32,
    align: Align
) -> Layout

fitted, its lines aligned so within its widest.

fn hitsource

exp fn hit(self layout: &Layout, x: f32, y: f32) -> u64

The byte offset nearest the point, on a grapheme boundary: of the carets drawn on the line at the point's height, the nearest.

fn caretsource

exp fn caret(self layout: &Layout, at: u64) -> TextBox

The caret's box at the byte offset: on the line it starts, one pixel wide, after the character before it (on that character's side where the direction changes: the side of the run the text was typed into), or before the line's first one at its start.

fn selectionsource

exp fn selection(self layout: &Layout, start: u64, end: u64) -> Vector<TextBox>

The boxes covering the bytes from start to end: one a line, or more on a line where the range runs on in another direction.

fn next_positionsource

exp fn next_position(self layout: &Layout, at: u64) -> u64

The caret position drawn next right of the offset's (decisions/F10.md, question 3): on its line, or past the line's right end the next line's leftmost (the previous line's right to left).

fn previous_positionsource

exp fn previous_position(self layout: &Layout, at: u64) -> u64

The caret position drawn next left of the offset's: on its line, or past the line's left end the previous line's rightmost (the next line's right to left).

fn next_logicalsource

exp fn next_logical(self layout: &Layout, at: u64) -> u64

The grapheme boundary after the offset in the text, or the text's end: the next character for Delete and screen readers, whatever its side.

fn previous_logicalsource

exp fn previous_logical(self layout: &Layout, at: u64) -> u64

The grapheme boundary before the offset in the text, or 0.

fn line_startsource

exp fn line_start(self layout: &Layout, at: u64) -> u64

The start of the line holding the offset, for a Home key.

fn line_endsource

exp fn line_end(self layout: &Layout, at: u64) -> u64

The end of the line holding the offset, for an End key: before the spaces a wrapped line hangs past its width.

type PlacedGraphemesource

exp type PlacedGrapheme = struct

A grapheme as a layout draws it: its bytes in the text, and its left and right on its line.

Fields

exp start: u32
exp end: u32
exp left: f32
exp right: f32

fn graphemes_in_ordersource

exp fn graphemes_in_order(self layout: &Layout) -> Vector<PlacedGrapheme>

The layout's graphemes in the text's order, as screen readers walk them; what draws nothing, a line's break, is left out.

fn line_ofsource

exp fn line_of(self layout: &Layout, at: u64) -> u64

The line holding the offset: a line's end is the next line's start.

fn line_graphemessource

exp fn line_graphemes(self layout: &Layout, k: u64) -> &[Grapheme]

The line's graphemes, from the left.

fn caret_xsource

exp fn caret_x(self layout: &Layout, k: u64, at: u64) -> f32

Where the caret at the offset is drawn on the line: after the grapheme ending there, or at the line's start before its first.

fn grapheme_countsource

exp fn grapheme_count(text: &[u8]) -> u64

How many grapheme clusters the text has (UAX #29), as a person counts its characters: "é" written as two code points is one, and so is a family emoji.

fn grapheme_endssource

exp fn grapheme_ends(text: &[u8]) -> Vector<u64>

Where the text's grapheme clusters end (UAX #29), one offset a cluster in order, the last the text's length: none for empty text.

fn word_startssource

exp fn word_starts(text: &[u8]) -> Vector<u32>

Where the text's words start (UAX #29), in bytes: before each letter, digit, ideograph or emoji that a word boundary comes before, a word running on to the next one's start, the spaces after it included. A run of Thai, Lao, Khmer or Myanmar is one word, as no dictionary splits it.

fn fold_casesource

exp fn fold_case(c: u32) -> u32

The code point's simple case folding (Unicode's CaseFolding.txt, its C and S mappings), by which text compares whatever its case: A is a, Σ and ς are σ.

fn starts_foldedsource

exp fn starts_folded(text: &[u8], prefix: &[u8]) -> bool

Whether the text starts with the prefix, code point by code point after simple case folding: "Hamlet" starts with "ha".