limoni-text-rendering
Skill151 starsChanged 3 months agoApache-2.0View on GitHub
How Limoni measures and stores text — UAX #29 grapheme clusters, cell width, the cluster table, mode 2027 and the diff's cursor resync. Load before touching core/grapheme, core/cell widths, Buffer.SetString, the ANSI diff's glyph emission, or anything about emoji, flags, combining marks or East Asian width.
In this repository6 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- llms.txtllms.txt
- limoni_developmentSkill
- limoni-agent-surfaceSkill
- limoni-performanceSkill
- limoni-zestSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 36
Correctness is checked against the standard: all 766 cases of GraphemeBreakTest.txt pass.
- Distinctive: line 115
Every fix above has a test that fails on the old code: run new tests in a worktree of the previous commit before trusting them.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Text: clusters, widths, and the cost of getting them right
A cell holds one rune, but a character can be several code points. Multi-code-
point clusters are interned and the cell stores a handle above the Unicode range
(cell.RuneClusterBase), so Cell stays 16 bytes and plain text never touches
the table.
Layout of the work
| Path | What it owns |
core/grapheme/gen.go | //go:build ignore generator; reads the UCD, writes tables.go |
core/grapheme/tables.go | generated ranges + property bits (GCB, ExtPict, InCB, emoji, width) |
core/grapheme/grapheme.go | Next, RuneWidth, StringWidth, Count, GB3–GB999 |
core/grapheme/mode.go | clusters on/off (LIMONI_GRAPHEME=0), mode 2027 sequences |
core/cell/clusters.go | interning, NextCluster, ClusterContent, AppendContent |
core/buffer/*.go | SetStringWithin writes clusters; the diff emits them |
Regenerating for a new Unicode version: download the files gen.go lists
(GraphemeBreakProperty.txt, emoji-data.txt, DerivedCoreProperties.txt,
EastAsianWidth.txt, extracted/DerivedGeneralCategory.txt), run
go run gen.go -version <v> -dir <ucd>, and replace
testdata/GraphemeBreakTest.txt too.
Rules worth remembering
- Width of a cluster is the widest code point in it, with VS16 forcing two
columns and VS15 one. Zero width comes from general category Mn/Me/Cf — not
from GCB Extend, which wrongly zeroes U+FF9E.
EastAsianWidth.txthas@missingdefaults; ignoring them mismeasures whole unassigned blocks. - Correctness is checked against the standard: all 766 cases of
GraphemeBreakTest.txtpass. Breaking GB11, GB9c or GB12/13 on purpose fails 3, 16 and 6 cases — use that when changing the state machine. - Terminals disagree. Setup asks for mode 2027 (
CSI ? 2027 h), which Ghostty, WezTerm, foot and Contour implement. Others advance per code point, so the diff re-anchors the cursor after every cluster: CHA in stream and inline mode, a forgotten cursor position in sparse mode. Without it, one family emoji shifts the rest of the row.core/buffer/cluster_test.gointerprets the output the way such a terminal would. - REP never repeats a cluster: a terminal may repeat only its last code point.
- The table is capped at
1 << 20clusters; past it a cluster degrades to its first code point rather than growing the process without limit. LIMONI_GRAPHEME=0(orcell.SetGraphemeClusters(false)) restores one code point per cell and stops the mode 2027 request.
Performance traps (this cost a day)
Segmentation is on the hot path; integrating it first cost 20–90% across the draw benchmarks. What recovered it:
cell.AppendContentmust stay inlinable. Budget is 80; inliningutf8.AppendRuneinto it pushed the cost to 143 and the diff lost a quarter of its speed. Keep the ASCII byte fast path first and everything else inappendContentSlow. Check withgo build -gcflags=-m.- ASCII fast paths in
Buffer.SetStringWithin,cell.StringWidthandgrapheme.Next: printable ASCII followed by another ASCII byte is a one-column cluster, no segmentation needed. - ASCII after non-ASCII always breaks (no ASCII code point is Extend, ZWJ,
SpacingMark or Extended_Pictographic), so
Nextreturns right after the first rune unless it is Prepend. This is the common shape of UI text. - Direct tables beat binary search:
bmpProps[0x10000]andpictPropsfor U+1F000–U+1FFFF, filled frompropTableininit, cover nearly everything a UI draws. 136 KB static, emoji width 10.7ns → 4.5ns.
Final measured cost against the previous commit, same machine, -count=3
medians: TextHeavyFrame +5% (three symbols per line), Diff_FullChanges +2%,
HundredLayers and Diff_PartialChanges unchanged, zero allocations throughout.
Benchmarking method (use this, not README numbers)
Absolute figures drift with the machine's state. Compare commits on one machine in one sitting:
git worktree add --detach /tmp/base HEAD # or the commit before the change
(cd /tmp/base && go test ./benchmarks -run '^$' -bench X -count=3)
go test ./benchmarks -run '^$' -bench X -count=3
git worktree remove /tmp/baseThen follow the repo's benchmark honesty rules: publish the delta and say the table's absolute numbers were not re-measured, rather than quietly editing rows.
Widgets: the tools, and what still walks runes
Widgets measure in columns and cut on cluster boundaries. Use these, never
utf8.RuneCountInString as a width or string([]rune(s)[:n]) to cut:
| Need | Use |
| width of a string | cell.StringWidth |
| longest prefix fitting n columns | cell.Truncate(s, n) — a substring, no allocation |
| draw cut text with "…" / "..." | setEllipsized / setClipped (widgets) — prefix and suffix drawn separately, no concatenation |
| cursor movement / deletion by cluster | clusterBounds(text, runeIndex) (widgets) |
| skip columns when scrolling sideways | skipColumns (logview.go) |
Traps paid for here:
- Table's old
clipToWidthskipped zero-width runes without advancing the byte offset and returned"e\xcc"— invalid UTF-8. Checkutf8.ValidStringin truncation tests. - TextInput put one rune per cell: 日本 drew as blanks (the second rune
overwrote the first's continuation cell) and Backspace left "man ZWJ woman
ZWJ" behind.
TextInputState.Textstays[]runefor compatibility; a cached string (str()) is rebuilt only when Text changes, which is what keeps Draw allocation-free. - Tests must spell combining characters as escapes. Writing a Go test
through a heredoc or an editor turned
\u200Dand\u0301into the literal characters, which a normaliser can silently rewrite. Check withpython3 -c "print([hex(ord(c)) for c in set(open(f).read()) if ord(c) in (0x301,0x200d)])". - Every fix above has a test that fails on the old code: run new tests in a worktree of the previous commit before trusting them.
Markdown measures words by cluster at parse time (StyledSegment.WordWidths)
and lays them out cluster by cluster (appendClusters); the palette's match
highlighting (drawHighlighted) walks clusters too. FuzzyMatch scores by
rune, which is a ranking choice, not a layout one.
Writing a row of cells that includes a wide character's continuation cell
through SetCell/SetCellDirect is safe: setContinuation keeps the wide
character on its left. Before that, the continuation counted as a narrow cell
overwriting the right half and blanked the character — every CJK character
and emoji in Markdown drew as spaces. TestWritingContinuationKeepsTheWideCharacter.
The handshake decides whether re-anchoring is needed
driver.ProbeQueries asks DECRQM 2027 and measures: it writes a ZWJ family
emoji and reads the cursor back. CapabilityProfile.ClusterWidths (→
DiffOptions.ClusterWidths) is true when mode 2027 is on or the family measured
2 columns; the diff then skips appendClusterResync. Keep the
cell.IsCluster(...) && !opts.ClusterWidths order: the other order cost 5% on
BenchmarkDiff_FullChanges, because the option load ran for every cell.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

