text-harfbuzz
Garnet text HarfBuzz adapter
require "garnet-text-harfbuzz"
font = Garnet::Text::HarfBuzzFont.new(
Garnet::Content::FileSource.new("fonts/NotoSans-Regular.ttf"))
run = font.shape("office", 24_f32, language: "en")
drawing = Garnet::Graphics::Drawing.record do |r|
r.translate(20_f32, 50_f32)
r.draw(run.drawing)
end
HarfBuzzFont.load(source, face_index) is the same as HarfBuzzFont.new under the shared X.load(source) loader name. Requiring the shard registers a default "font" importer for .ttf, .otf, .ttc and .otc (see content).
The provider opens and closes a fresh content reader during font construction. File and memory sources follow the same contract. Font bytes become HarfBuzz-owned font tables; the content source and reader are not retained. face_index: selects a face in a font collection.
shape(text, size, direction:, script:, language:, features:) converts copied HarfBuzz output into the neutral glyph-run contract. Sizes are em heights in pixels, finite within (0, 4096]. Each unique glyph's analytic outline is converted once per run, with Y flipped and coordinates normalized to em units. Placement offsets and advances are scaled from the same native font instance. Glyph cluster ends come from sorted source boundaries, so ligatures and combining sequences retain full UTF-8 spans even when visual glyph order is RTL. supports?(char) exposes nominal coverage for caller-controlled font selection.
For Arabic, explicitly select the run's direction and script, for example font.shape("سلام", 24_f32, direction: :rtl, script: "Arab", language: "ar"). HarfBuzz performs joining and shaping; this method does not implement the paragraph bidi algorithm. Input must already represent one script/direction run. Control characters and line separators are rejected rather than silently rendered as a multiline layout. The optional paragraph layout shard performs bidi, script segmentation, fallback, wrapping, and alignment above this interface. Missing glyph IDs render the font's .notdef when shaping a run directly.
Requires the harfbuzz shard's native dependencies. FreeType is not required for this outline profile: HarfBuzz exposes the curves directly. Hinted rasterization and bitmap/color glyph support remain later providers; the graphics layer has no font-library dependency.
Run shards install --local in this workspace, then CRYSTAL_CACHE_DIR=/tmp/text-harfbuzz-spec crystal spec. The standalone adapter specs use the optional redistributable harfbuzz/testing fixtures. scripts/check-composition unit|gl|gles|metal|vulkan exercises the shared integration, and the mixed gallery includes Latin and Arabic text.
text-harfbuzz
- 0
- 0
- 0
- 5
- 3
- 1 day ago
- November 30, 2026
MIT License
Wed, 02 Dec 2026 02:08:58 GMT