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):
text:text.alloythe API (Fonts,TextStyle,Span,Layoutwithhit,caret,selection,next_position,previous_position,line_start,line_end),paragraphs.alloylayout,font/(face.alloytables and metrics,outline.alloyglyfwith composites,raster.alloycoverage),shaping/(shaper.alloynormalization, clusters and the feature list,gsub.alloy,gpos.alloy,context.alloy,opentype.alloy),unicode/(generate.alloywritingtables.alloyfrom UCD 16.0, whichfetch.shdownloads by pinned SHA-256s;lines.alloyUAX #14,graphemes.alloyUAX #29,properties.alloy). Inter 4.1 regular and bold, subset, about 100 KB each (fonts/, OFL). Shaping applies GSUB 1 to 7 and GPOS 1, 2 and 4 to 9 (contexts in formats 1 to 3, lookup flags, GDEF classes and mark filtering sets),kernwhere a face has no GPOS, and normalizes as HarfBuzz does; each lookup keeps a bit set of the glyphs its subtables can start at (LookupSets, made when a face is read), which made layout four times faster.draw:paint.text(&layout, origin, color), decorations as rectangles; at encoding each glyph becomes a mask instance fromglyphs.alloy's cache (coverage per face, glyph, size in quarter pixels and quarter-pixel shift, in R8 pages of 1024 pixels, the one used least recently cleared past four and the nodes using it encoded again), snapped to whole pixels where the node is only moved and scaled evenly, transformed otherwise; glyphs well clear of the clip are skipped before rasterizing.canvas.fonts()makes the fonts when first asked, so a program drawing no text carries no faces (the cards startup stays 0.82 MB).window:set_text_input(::Some(Area))and `Event::Preedit(Preedit { text, cursor })`, an empty one ending the composition before itsText: Wayland throughtext-input-unstable-v3(itsdelete_surrounding_textignored, as no surrounding text is sent), Win32 through IMM32 (compositions without the system's window, keys the input method takes skipped, no input method until asked for), the web through a hidden textarea at the caret (paste reaches it asText); headless replays keep the area last asked for.- Tests:
alloyc test text/tests.alloy(faces, outlines, rasterizing, 13 strings shaped ashb-shapeshapes them,tests/shaping.txtfromtests/harfbuzz.sh, layout, carets, selections, spans), Unicode'sLineBreakTest.txtandGraphemeBreakTest.txtpassing whole (unicode/conformance.alloy), two draw tests (a glyph page, eviction),tests/canvas.alloy's text scene the same on the CPU device, lavapipe, Dawn, the GTX 1660 and the RTX 4070 Ti, the window's composition test, andtests/text_input.alloyin node's page, whose fake input method composes 你 (tests/web/dom.mjs), and under weston, which lacks text-input-v3.samples/editor.alloyis a text field (arrows, Home, End, Shift, Ctrl+A, the pointer, Backspace, Delete, Enter, compositions underlined);editor.exeis in.alloy\window-testson the Windows desktop. - Benchmarks, the best of five, in milliseconds:
| | 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.
- Left for later: CFF and variable fonts, hinting, bidi and complex scripts, fallback (F10); GSUB 8 and GPOS 3 (cursive); a layout is made whole each time (a paragraph's 0.27 ms makes incremental layout F5's to decide); the Wayland and Windows input methods wait for a person with one.
**F10a as built** (2026-10-14; decisions/F10.md):
- Formats:
font/cff.alloyreads CFF (Type 2 charstrings with local and global subroutines, flex, hints skipped, CID-keyed fonts through FDSelect 0, 3 and 4) and CFF2 (blend,vsindex), as cubicPointKind::Cubicpoints thatraster.alloyflattens into lines by their deviation; collections (ttcf) of either. Variable fonts:fvaraxes and named instances,avar1,gvar(shared and private points, IUP) and CFF2 blends throughfont/variations.alloy's item variation stores,HVARadvances,MVARmetrics, GDEF's store for GPOS value records and anchors, GSUB's and GPOS's feature variations (rvrnon).face.at(values)makes an instance sharing the file's bytes and lookup sets;FontStorekeeps instances by face, weight (when there is awghtaxis) and size (when there isopsz), so a style's weight 1-1000 and size pick one, and named instances are a family's faces. Left out:avar2, CFF'sseacaccents and arithmetic operators, hinting. - Itemization:
unicode/generate.alloyadds Scripts, Script_Extensions, emoji properties, Bidi_Brackets and Default_Ignorable to the tables (fetch.shpins the UCD 16.0 files and emoji-test.txt);unicode/runs.alloysplits a paragraph into runs by script (UAX #24, Common and Inherited joining their neighbors, a closing bracket taking its opening one's script) and by emoji presentation (UTS #51 sequences: modifiers, flags, keycaps, ZWJ, tags, VS15 and VS16), andparagraphs.alloyshapes each run in the face covering its clusters, never splitting one across faces. The shaper drops a variation selector the face has no glyph for (a keycap's VS16); hiding default ignorables as HarfBuzz does, with GSUB skipping them, waits for F10c's shaping. - Fallback (
fallback.alloy,system/): fontconfig'sFcFontSortthroughLibraryon Linux, DirectWrite'sIDWriteFontFallbackthroughpkg::alloy::com(moved out ofwindowintocomfor this) on Windows, Core Text'sCTFontCreateForStringon macOS, and on the web the font files a program serves and names withfonts.fetch(&path); a font picked is read whole once and kept as a family, answers cached per script, language, weight and block of 256 characters (per character where the block's lacks one), the families picked for a script tried first.Fonts::bundled()leaves the system out (the interpreter,ui's trees without a scene);TextStyle.languageis the tag fallback asks with;face_namegives a face's PostScript name. - Tests:
check.alloy(check <fonts directory>, run byscripts/test.sh) compares 242 glyph outlines of CFF, CID-keyed, CFF2 and variable fonts at several instances with fontTools' (tests/outlines.txtfromtests/outlines.py) and 14 variable shaping runs withhb-shape --variations(tests/variations.txt), on fontstests/fonts.shfetches at pinned SHA-256s into a cache (Source Sans, Source Han Sans, Noto Sans CJK's collection, Inter's and Source Sans 3's variable faces, HarfBuzz's test fonts), and asks this machine's fontconfig for Han, Arabic, Devanagari and emoji;tests.alloythe runs;unicode/conformance.alloyemoji-test.txt's 3988 sequences, each one cluster in emoji presentation;tests/fallback.alloy(Windows, by hand throughwindows-tests.ps1),tests/fetch.alloyin node's page and under weston. Samplesamples/world.alloy: a greeting in 15 scripts with a weight slider and a field to type in;world.exeis in.alloy\window-tests\uion the Windows desktop. - Benchmarks, biscuit's CPU, the best of five, in milliseconds: the first fallback lookup of a session (fontconfig opened, Noto Sans CJK read) 9.9, a 20-character Han line once cached 0.007; the 94 ASCII glyphs at 12, 16 and 32 px 0.82 in Inter, 0.82 in Source Sans (CFF), 1.2 in Inter's variable face at weight 650 (deltas applied each time); the 10,000 words 18.8 and a paragraph 0.17, as before. Hello world grew from 1.04 to 1.20 MB natively and from 0.61 to 0.73 MB of wasm, F10's whole estimate (question 7).
- Left for later: when a page's fetched font arrives nothing lays the text out again (question 6); emoji are laid out in the color font but drawn blank until F10d; Windows' and macOS's fallback wait for a person to run them.
**F10b as built** (2026-10-14; decisions/F10.md, "Bidi"):
- Unicode data:
unicode/generate.alloyadds Bidi_Class (aBidiClassenum in a trie of its own, DerivedBidiClass.txt's@missingdefaults included, a MIRRORED bit beside it) and BidiMirroring.txt's pairs; about 16 KB of tables.fetch.shpins them with BidiTest.txt and BidiCharacterTest.txt. unicode/bidi.alloy: UAX #9 whole (P2-P3, X1-X10 with the overflow counts, isolating run sequences, W1-W7, N0 with canonical brackets, N1-N2, I1-I2, L1, L2);conformance.alloypasses BidiTest.txt's 770,241 cases and BidiCharacterTest.txt's 91,707.- Layout:
TextStyle.direction(Direction::Autoby default: each paragraph's first strong character; an empty paragraph keeps the one before's), paragraphs being the text between mandatory breaks (so U+2028, VT and FF start one too); a paragraph with nothing right to left takes one look at each non-ASCII character and stays at level 0. Runs of one style and level are shaped in their direction: mirrored glyphs,rtla,rtlm(kept off mirrored clusters), GPOS marks resolved backwards as HarfBuzz does, and its modified combining classes for every script (Hebrew points in SBL order, shadda first). Lines are filled in the text's order, then their glyphs reordered (L1 on their clusters, L2):glyphsare in visual order, each with its cluster; spaces a right-to-left line hangs go to its left;Line.direction;Align::StartandEnd(andui'sTextAlign) follow each paragraph. - Carets: a layout keeps each grapheme's leading and trailing edges in
visual order (
Line.graphemes, its text under 4 GiB);caret(at)is drawn after the grapheme ending atat, on its side, or before the line's first;hitfinds the nearest caret;selectiongives a box for each visual stretch;next_positionandprevious_positiongo through the carets ordered by x then offset (so two drawn together are both reached), past a line's end on to the next line (the previous right to left);next_logicalandprevious_logicalkeep the text's order, for deletion;line_ofis exported. A position at a direction change has no caret on its other side (question 8). ui:TextFieldmoves visually, deletes logically, collapses a selection at the end shown on the arrow's side, keeps a short right-to-left line at its right, and composes in either direction.- Tests: layout goldens in
tests.alloy(visual order, carets as boundaries, walks both ways, hits, selections, wrapped lines, numbers in Arabic, explicit directions, empty paragraphs), three right-to-left runs in Inter and five of pointed Hebrew in Noto Sans Hebrew 3.001 shaped ashb-shape --direction=rtlshapes them (check.alloy; Noto Sans Hebrew and Arabic fetched pinned bytests/fonts.sh), aTextFieldediting Hebrew with Latin, and three scenes replayed at scale 2 (tests/golden/ui/bidi_*.png) in committed fonts (tests/fonts, OFL). Latin output is unchanged; the access trees too. - Benchmarks, biscuit's CPU, the best of five: 10,000 words in Arabic 19.4 ms (16.8 without bidi), mixed with Latin in turns of three 22.7 ms, Latin 19.0 against 18.9 before; hello world 1.20 to 1.26 MB natively and 0.73 to 0.78 MB of wasm (+59 and +54 KB; question 7).
- Left for F10c: Arabic's joining and the other shapers (Arabic shows
unjoined), the face's script and language systems (lookups are still
latn's orDFLT's), HarfBuzz's reversal of a Latin run forced right to left by an override.
**F10c as built** (2026-10-14; decisions/F10.md, "Shaping"):
- Unicode data:
unicode/generate.alloyadds General_Category with Joining_Type (Syriac's Alaph and Dalath Rish apart), and the Indic shaper's category and position and the USE category HarfBuzz 14.1.0's generators derive from Indic_Syllabic_Category and Indic_Positional_Category with its ms-use overrides (both matching them code point for code point), IndicShapingInvalidCluster.txt's vowel sequences, Arabic's presentation forms (shaping_data.alloy,ucd.alloythe generators' helpers).unicode/language_tags.alloyreads hb-ot-tag-table.hh intoshaping/language_tags.alloy, andunicode/machines.alloyHarfBuzz's Ragel syllable machines (Indic, Khmer, Myanmar, USE) intoshaping/machines.alloy;fetch.shpins them all. - Shaping rewritten after hb-ot-shape.cc:
shaping/buffer.alloy(glyph infos with Unicode and glyph props, ligature ids, clusters merged as HarfBuzz merges them),plan.alloy(a plan per face, script, language, direction and features, cached inFonts: mask bits, lookups in stages with the shaper's pauses),tags.alloy(the script's tags, dev3 to deva, and a BCP 47 tag's language systems, soTextStyle.languagepickslocl's forms),normalize.alloy,apply.alloy(HarfBuzz's skipping rules, per-syllable matching, subtables skipped by their digests), GSUB 8 and GPOS 3, default ignorables hidden; the shapers dispatched fromshapers.alloy:arabic.alloy(Arabic, Syriac; joining with five characters of context either side,stch, UTR #53's mark order, presentation-form lookups made for faces without the features),hebrew.alloy,indic.alloy(nine scripts: syllables, reph, half, below and post forms, pre-base matras, initial and final reordering),khmer.alloy,myanmar.alloy,universal.alloy(Tibetan, Sinhala, Javanese, Mongolian, N'Ko and the 79 other scripts HarfBuzz sends to the USE),thai.alloy(Thai, Lao: Sara Am, private-use forms),hangul.alloy, withsyllables.alloy(the machines run, dotted circles) andfallback.alloy(marks placed by combining class where GPOS is not applied). Thekerntable's formats 0, 2 and Apple's, faces without outlines, cmap 13. A run in a script with no complex shaper takes the default shaper and pays for none of the others. - Line breaking: the tables keep UAX #14's SA for Thai's, Lao's,
Khmer's and Myanmar's bases,
lines.alloyresolving it to AL (LB1), so Unicode's break tests pass whole; a line overflowing in such text ends at its last grapheme boundary between two SA characters when that is past the last opportunity (question 4, A), and a word wider than a line breaks only where a glyph cluster starts a grapheme. - Tests:
check.alloyshapes HarfBuzz 14.1.0's own tests of the OpenType shapers (tests/harfbuzz.txt, 4901 runs; left out: Apple's and Microsoft's shapers, vertical text, cluster levels and other options text has no equivalent for, a Mac resource fork, a font HarfBuzz's sanitizer cuts, two legacy Arabic fonts, Zawgyi) and 4986 strings oftests/scripts.casesin the pinned Noto fonts of fifteen scripts (386 Arabic, 789 Devanagari, 592 Bengali, 436 Tamil, 442 Thai, 274 Lao, 322 Khmer, 303 Myanmar, 203 Tibetan, 252 Mongolian, 202 N'Ko, 236 Javanese, 186 Syriac, 231 Hebrew, 132 Hangul and Han), every one ashb-shapeshapes it;tests.alloyThai's breaks; the ui goldens gaincomplex_scripts(Khmer typed, Devanagari, Thai wrapping) in subsets intests/fonts, andbidi_arabicchanged once, its Arabic now joined. - Benchmarks, biscuit's CPU, the best of five, in milliseconds: 10,000 words of Latin 20.5 (the F10b build measures the same today, 19.6 to 20.4), of Arabic 36.0 (19.4 unjoined before), of Devanagari 52.4, Arabic mixed with Latin 34.6; HarfBuzz shapes Devanagari about three times faster. Hello world 1.51 MB natively and 1.00 MB of wasm, +256 and +216 KB over F10b, the wasm budget met by 34 bytes (question 7, updated).
- Sample:
samples/world.alloyin 24 scripts, built for every target;world.exein.alloy\window-tests\uion the Windows desktop. - Left for later: color emoji (F10d); dictionaries for SA line breaks
(question 4, B); Zawgyi, vertical text and Apple's
morx; HarfBuzz's buffer flags (a dotted circle before a run starting with a mark) and cluster levels.
**F10d as built** (2026-10-15; decisions/F10.md, "Color emoji"):
font/color.alloy: COLR and CPAL, version 0's layers and version 1's paint graphs (every format, 1 to 32: layers, solid colors, linear, radial and sweep gradients with their three extend modes, glyphs as clips, color glyphs with their clip boxes and a guard against cycles, every transform, composites in all 28 modes, Porter and Duff's and the W3C's blends), varied through COLR's index map and variation store at a variable face's position; drawn on the CPU into a surface of premultiplied floats as large as the glyph's clip box (else what its glyphs cover), gradients interpolated premultiplied, each glyph clip a coverage mask from the rasterizer, which takes any affine transform now (rasterize_with). Palette 0; entry 0xFFFF is the text's color, and a glyph using it says so (ColorMask.foreground).font/bitmaps.alloy: CBDT through CBLC's strikes (index formats 1 to 5, image formats 17 to 19) and sbix (dupefollowed; JPEG and TIFF left out), the strike at or just above the size, else the largest; its PNG decoded byfont/png.alloy(moved in from the viewer sample, inflate rewritten with nine-bit tables, since F12b in std::deflate), premultiplied and resampled, a box filter shrinking and bilinear growing.- The API:
fonts.glyph_image(face, glyph, size, shift, foreground)givesGlyphImage::Coverageor::Color(ColorMask), andfonts.is_color(face, glyph);drawdraws them with no program change. An emoji run whose style's face is itself a color font stays in it rather than going to fallback, so a program may bundle its emoji font; fallback otherwise picks the system's (Noto Color Emoji here, Segoe UI Emoji on Windows, Apple Color Emoji on macOS). The rasterizer no longer drops an edge a hair left of its mask by rounding, which skewed and scaled clips met, and cuts a mask to a window (rasterize_within, a color glyph's surface). A malformed font is bounded: paints nest 64 deep and a glyph visits 20,000, composites nest 4 deep, a color glyph is at most 2048 pixels a side, transforms past reason draw nothing, and a PNG's header alone allocates little. - Tests:
tests.alloy(COLR version 0, CBDT and sbix squares of ChromaCheck, the text's color in a COLR version 1 glyph, the fonts' API; PNGstests/pngs.pywrites, damaged ones, headers out of reason);check.alloy'scolorsmode draws sheets compared with golden images intests/golden/text(sheets.alloy): every glyph of Google's COLR version 1 test font, at its default and at a position on its 44 axes, emoji with sequences, a modifier, flags and keycaps in Noto Color Emoji 2.048 (COLR version 1), Twemoji Mozilla 0.7.0 (version 0) and Twemoji 14.0.2 (CBDT, from Fedora's package), and sbix and CBDT test glyphs, all pinned bytests/fonts.sh; matched by eye and pixel by pixel withhb-view14.1.0 (cairo) while built, which differs only in sampling, in a sweep's patches, and in drawing a stop's color where cairo draws something near it. 669 emoji strings (every 11th fully qualified sequence of emoji-test.txt and 43 harder ones in Noto Color Emoji, every 40th in both Twemojis) shape ashb-shapeshapes them (tests/scripts.cases).draw's goldenemojiscene from subsets intests/fonts, the same on the CPU, lavapipe and Dawn. - Benchmarks, biscuit's CPU, the best of five, in milliseconds: 100 emoji at 32 pixels drawn into their colors 10.3 in Noto Color Emoji (COLR version 1), 10.5 in Twemoji (CBDT: PNGs decoded and shrunk); a 1920x1080 page of 1914 emoji of 100 kinds, rasterized and packed first 23.7 on lavapipe, drawn again 13.2; ASCII rasterized as before (0.9). Hello world 1.60 MB natively and 1.08 MB of wasm, F10d adding about 85 KB to each (question 7: budgets of 1.1 MB of wasm and 1.7 MB natively).
- Sample:
samples/world.alloywith emoji sequences, skin tones, flags, keycaps and tags, in color. - Left for later: SVG glyphs (
SVG, not one of the four formats), palettes other than the first, JPEG and TIFF in sbix, hinting.
**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):
- Out to pkg::alloy::text_scripts: the eight complex shapers, the
syllable machines, HarfBuzz's language tags, the scripts' Unicode
tables (
unicode/generate.alloywrites them into../text_scripts/unicode/script_tables.alloybesidetables.alloy), bidi, and Thai's, Lao's, Khmer's and Myanmar's breaks between graphemes. Text keeps hooks:ScriptHooks(the shaper for a run, a tag's language systems, mirrors, a paragraph's levels, a line's order), each plan'sComplexShaperobject, whichshapers.alloycalls at the shaper's points;Fonts::install_scripts. - Out to pkg::alloy::text_fonts: CFF and CFF2, variations, COLR,
CBDT, sbix, PNG,
fallback.alloyandsystem/,sheets.alloy. Text keepsFontFormats(a struct of functions each face read after keeps: reading the tables, instances, varied advances and deltas, color glyphs), theOutlinesinterface CFF's glyph table is, and theFontFallbackobjectfallbackandfetchcall;Fonts::install_formats. - The bundled font: Inter Regular with HarfBuzz's default features and
tnum(fonts/subset.sh, 75,544 bytes; the tests' fuller regular and bold intests/fonts). A weight of 600 or more on a lighter face with no weight axis gets an instance withFace.embolden(em / 24): outlines thickened as FreeType's FT_Outline_EmboldenXY, moved up and right by half as HarfBuzz's synthetic bold, advances other than 0 and extents wider by the strength.FontStore.instancesis keyed byInstanceKey. - The counter 1,214,688 bytes natively and 710,130 of wasm, from 1,633,488 and 1,087,148; the measures in REFOCUS.md.
FontsFontStoreFamilyREGULARSEMIBOLDBOLDScriptHooksFontFallbackWantedFonts::createFonts::bundledinstall_scriptsinstall_formatsscriptsread_facekeepfamily_offallbackfetchplandefaultaddadd_tofamily_sizeface_namefaceface_forface_forGlyphMaskglyph_maskColorMaskGlyphImageis_colorglyph_imageTextColorTextColor::rgbFeatureFeature::onFeature::offDirectionTextStyleTextStyle::ofSpanAlignPlacedGlyphLineTextBoxDecorationGraphemestartendleftrightLayoutLayout::createLayout::spansLayout::fittedLayout::fittedhitcaretselectionnext_positionprevious_positionnext_logicalprevious_logicalline_startline_endPlacedGraphemegraphemes_in_orderline_ofline_graphemescaret_xgrapheme_countgrapheme_endsword_startsfold_casestarts_folded
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
exp fn Fonts::create() -> Fontsexp fn Fonts::bundled() -> Fontsexp fn install_scripts(self fonts: &var Fonts, hooks: ScriptHooks)exp fn install_formats(self fonts: &var Fonts, formats: FontFormats, fallback: *var FontFallback)exp fn scripts(self fonts: &Fonts) -> Option<ScriptHooks>exp fn fallback( self fonts: &Fonts, text: &[u8], start: u64, end: u64, wanted: &Wanted, primary: u32 ) -> Option<u32>exp fn fetch(self fonts: &Fonts, path: &[u8])exp fn plan( self fonts: &Fonts, face: u32, script: Script, language: &[u8], rtl: bool, features: &[Feature] ) -> &Planexp fn default(self fonts: &Fonts) -> Familyexp fn add(self fonts: &var Fonts, bytes: Vector<u8>) -> Option<Family>exp fn add_to(self fonts: &var Fonts, family: Family, bytes: Vector<u8>) -> boolexp fn family_size(self fonts: &Fonts, family: Family) -> u64exp fn face_name(self fonts: &Fonts, face: u32) -> Stringexp fn face(self fonts: &Fonts, index: u32) -> &Faceexp fn face_for(self fonts: &Fonts, family: Family, weight: u32, size: f32) -> u32exp fn glyph_mask(self fonts: &Fonts, face: u32, glyph: u32, size: f32, shift: f32) -> GlyphMaskexp fn is_color(self fonts: &Fonts, face: u32, glyph: u32) -> boolexp fn glyph_image( self fonts: &Fonts, face: u32, glyph: u32, size: f32, shift: f32, foreground: TextColor ) -> GlyphImage
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: boolwhether 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
exp fn read_face(self store: &FontStore, bytes: FaceBytes, index: u32) -> Option<Face>exp fn keep(self store: &var FontStore, face: Face) -> u32exp fn family_of(self store: &var FontStore, face: Face) -> u32exp fn face_for(self store: &var FontStore, family: Family, weight: u32, size: f32) -> u32
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]) -> Tagsthe 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) -> ParagraphLevelsthe 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
primarylacksfn 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: Scriptexp language: &[u8]exp weight: u32exp size: f32exp 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: u32exp height: u32exp left: i32from the pen, on the baseline, to the mask's top left
exp top: i32exp 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: u32exp height: u32exp left: i32exp top: i32exp pixels: Vector<u8>exp foreground: boolwhether 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: GlyphMaskColor: 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: f32exp g: f32exp b: f32exp 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: u32exp 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
AutoAutoRightToLeftLeftToRightRightToLeft
type TextStylesource
exp type TextStyle = struct
Fields
exp family: Familyexp size: f32pixels
exp weight: u32REGULAR, BOLD or between
exp line_height: f32a 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: boolexp strikethrough: boolexp features: Vector<Feature>exp language: Stringa BCP 47 tag, as "ar" or "zh-Hant", none for the user's: the language fallback asks fonts for
exp direction: Directionthe 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: u64exp end: u64exp 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
StartCenterEndLeftRight
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: u32exp glyph: u32exp size: f32exp x: f32exp y: f32exp cluster: u64exp 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: u64exp end: u64exp glyphs: [u64 : 2]exp top: f32exp baseline: f32exp height: f32exp x: f32exp width: f32exp direction: Directionexp 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: f32exp y: f32exp width: f32exp height: f32
type Decorationsource
exp type Decoration = struct
An underline or strikethrough.
Fields
exp box: TextBoxexp 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: u32exp length: u32exp leading: f32exp 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
fn rightsource
exp fn right(self g: &Grapheme) -> f32
type Layoutsource
exp type Layout = struct
Fields
exp text: Stringexp glyphs: Vector<PlacedGlyph>exp lines: Vector<Line>exp decorations: Vector<Decoration>exp width: f32exp height: f32exp graphemes: Vector<Grapheme>
Functions
exp fn Layout::create(fonts: &Fonts, text: &[u8], style: &TextStyle, width: f32) -> Layoutexp fn Layout::spans( fonts: &Fonts, text: &[u8], spans: &[Span], width: f32, align: Align ) -> Layoutexp fn Layout::fitted(fonts: &Fonts, text: &[u8], style: &TextStyle, width: f32) -> Layoutexp fn Layout::fitted( fonts: &Fonts, text: &[u8], style: &TextStyle, width: f32, align: Align ) -> Layoutexp fn hit(self layout: &Layout, x: f32, y: f32) -> u64exp fn caret(self layout: &Layout, at: u64) -> TextBoxexp fn selection(self layout: &Layout, start: u64, end: u64) -> Vector<TextBox>exp fn next_position(self layout: &Layout, at: u64) -> u64exp fn previous_position(self layout: &Layout, at: u64) -> u64exp fn next_logical(self layout: &Layout, at: u64) -> u64exp fn previous_logical(self layout: &Layout, at: u64) -> u64exp fn line_start(self layout: &Layout, at: u64) -> u64exp fn line_end(self layout: &Layout, at: u64) -> u64exp fn graphemes_in_order(self layout: &Layout) -> Vector<PlacedGrapheme>exp fn line_of(self layout: &Layout, at: u64) -> u64exp fn line_graphemes(self layout: &Layout, k: u64) -> &[Grapheme]exp fn caret_x(self layout: &Layout, k: u64, at: u64) -> f32
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: u32exp end: u32exp left: f32exp 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".