text-layout
Garnet text layout
require "garnet-text-layout" composes neutral Garnet::Text::Font providers into immutable, drawable paragraphs. It resolves bidi levels with FriBidi, finds Unicode line and grapheme opportunities with libunibreak, segments script/direction/font runs, shapes each run through the selected font, and lowers the result to ordinary Garnet vector meshes.
family = Garnet::Text::FontFamily.new([
latin_font.as(Garnet::Text::Font),
arabic_font.as(Garnet::Text::Font),
])
paragraph = Garnet::Text::Paragraph.layout(
"Hello مرحبا from Garnet",
family,
Garnet::Text::ParagraphStyle.new(
size: 24_f32,
width: 320_f32,
wrap: :word,
align: :start,
language: "ar"
)
)
entity << Garnet::Graphics::Components::Drawable.new(paragraph.primitive)
Paragraph#lines returns copied line arrays with logical UTF-8 ranges, top/baseline metrics, visual positioned runs, and advance widths. bounds reports ink rather than layout extent. hit_test(x, y) maps a visual point to a source Caret; position(caret) maps a representable cluster edge back to paragraph coordinates and returns nil inside a ligature or other indivisible shaped cluster. Caret offsets always count UTF-8 bytes.
Word wrapping uses Unicode line opportunities. Grapheme wrapping additionally permits emergency breaks between extended grapheme clusters. Hard CR, LF, CRLF, line-separator and paragraph-separator boundaries create lines without being passed into the shaper. Start/end alignment follows the resolved base direction. Font fallback selects the first provider covering an entire grapheme cluster, so marks and ZWJ sequences are never split across fonts. Arrays supplied by callers and returned by the API are copied.
This shard is sequential and backend-neutral. It depends on the neutral text/vector contracts plus the small FriBidi and libunibreak adapters; it has no renderer, host, scene, ECS, or HarfBuzz dependency. A font provider supplies shaping and Unicode script identification. garnet-text-harfbuzz is the first such provider.
Retain the result of paragraph.primitive or paragraph.drawing across frames. Each paragraph primitive retains its prepared draws for one tessellation tolerance, so unchanged text reuses immutable mesh streams and backend uploads. A new tolerance replaces that bounded preparation cache; previously returned draws remain valid. Callback changes to a Draw value do not alter cached draws. Calling primitive again intentionally creates a separate preparation owner.
The current profile lays out horizontal text with one style per paragraph. It does not yet expose attributed spans, justification, hyphenation, vertical layout, Unicode bidi control filtering, selection rectangles, font-feature spans, color/bitmap glyphs, language-aware fallback policy, or IME state. Wrapping reshapes candidate lines for correct boundary shaping and is bounded to 65,536 source bytes, 16,384 break opportunities, 4,096 lines, and 64 fallback fonts.
Install FriBidi, libunibreak, and the native dependencies required by the chosen font provider. Run shards install --local and CRYSTAL_CACHE_DIR=/tmp/text-layout-spec crystal spec from this shard. scripts/check-composition unit|gl|gles|metal|vulkan exercises the shared integration.
text-layout
- 0
- 0
- 0
- 2
- 6
- 1 day ago
- November 30, 2026
MIT License
Wed, 02 Dec 2026 02:09:24 GMT