harfbuzz.cr
HarfBuzz for Crystal
require "harfbuzz" provides typed, immutable font objects with copied shaping and outline results. LibHarfBuzz contains the corresponding C bindings. No renderer, windowing library or Garnet dependency is involved.
font = File.open("NotoSans-Regular.ttf") { |io| HarfBuzz::Font.new(io) }
glyphs = font.shape("office", language: "en", features: ["kern=1"])
outline = font.outline(glyphs.first.id)
Install HarfBuzz 14 or newer and pkg-config using the target's dependency tooling. The shard uses HarfBuzz's pkg-config metadata for linking. It was tested with HarfBuzz 14.3.1 on macOS arm64. Other operating systems and mobile packaging are not yet verified.
Font.new(io, face_index: 0) reads at most 64 MiB and copies font bytes into native ownership; it does not close the caller's IO. The immutable native font retains its face and font tables, and its finalizer releases that reference. This CPU font data is needed for subsequent shaping and outline extraction. No extra Crystal copy is retained. to_unsafe lends the native handle while the wrapper is alive; callers must not destroy or mutate it. Outline callbacks retain a native font reference throughout their execution, including Crystal GC activity.
shape accepts one already-segmented script/direction run. Directions are LTR, RTL, TTB and BTT. The optional script: uses a four-letter ISO 15924 tag; language: defaults to und. HarfBuzz parses OpenType feature strings. Glyph positions/advances are signed integers in font design units; units_per_em provides scaling. Cluster offsets count UTF-8 bytes, not characters or glyphs. Returned arrays and records are independent values.
outline returns Move/Line/Cubic/Close commands in design units with Y pointing up. HarfBuzz converts quadratic segments to cubic curves. Empty outlines, such as spaces, return empty arrays; unavailable outlines raise. The callback boundary collects bounded commands and reports limit errors after the native call returns. glyph(char) performs nominal character lookup; metrics returns copied horizontal font metrics.
This is a shaping/outline profile, not an exhaustive HarfBuzz wrapper. Font variations, color/bitmap painting, hinted rasterization, paragraph bidi, script segmentation, fallback selection and line breaking are not supplied. Default .notdef glyphs remain visible when a font lacks coverage. Text input is limited to 65,536 bytes, output to 262,144 glyphs, features to 256, and each outline to 65,536 commands. These are wrapper limits, not native CPU/memory quotas.
The implementation uses HarfBuzz's OpenType font functions and outline callbacks, through Crystal FFI. It uses no HarfBuzz renderer. HarfBuzz carries its upstream license; this wrapper is MIT. Noto fixture provenance includes the separate font license.
Run CRYSTAL_CACHE_DIR=/tmp/harfbuzz-spec crystal spec from this shard. The optional require "harfbuzz/testing" exports redistributable fixture paths; requiring the main library does not load them.
harfbuzz.cr
- 0
- 0
- 0
- 6
- 0
- 1 day ago
- November 30, 2026
MIT License
Wed, 02 Dec 2026 02:09:02 GMT