Swift wrapper for HarfBuzz, the OpenType text shaping engine, split into two targets:
CHarfBuzz: system library target exposing the HarfBuzz headers (viapkg-configharfbuzz).HarfBuzzSwift: memory-safe wrappers forhb_face_tandhb_font_t, and shaping that returns Swift values.
Shaping turns text into positioned glyphs: it applies OpenType kerning (GPOS), ligatures and contextual forms, places marks over their bases, and orders right-to-left scripts such as Arabic and Hebrew. Pair it with a rasterizer such as swift-freetype to draw the glyphs.
HarfBuzz must be installed: brew install harfbuzz on macOS, apt install libharfbuzz-dev on Linux. Tested with HarfBuzz 10.2 (Debian 13) and 15.0 (Homebrew).
import HarfBuzzSwift
let face = try HarfBuzzFace(path: "/path/to/font.ttf") // or HarfBuzzFace(data:), index: for .ttc
let font = try HarfBuzzFont(face: face, size: 48) // positions in pixels at 48 px per em
let run = font.shape("Hello, AV!")
for glyph in run.glyphs {
glyph.glyph // glyph index in the font, for a rasterizer
glyph.cluster // UTF-8 offset of the first character it came from
glyph.xAdvance // pen movement, kerning included
glyph.xOffset // placement from the pen, as for marks
}
run.advance // total widthGlyphs come back in visual order: for right-to-left text, the first character's glyph is last. Direction, script and language are guessed from the text unless given:
let arabic = font.shape("مرحبا") // .rightToLeft, script "Arab"
let turkish = font.shape("ı", language: Language("tr"))
let vertical = font.shape("縦書き", direction: .topToBottom)OpenType features use the same syntax as CSS font-feature-settings:
font.shape("office", features: [Feature("-liga")!]) // no ligatures
font.shape("AV", features: [Feature("-kern")!]) // no kerning
font.shape("2/3", features: [Feature("frac")!, Feature("ss01=1")!])
try Feature(tag: "kern", value: 0, range: 3 ..< 5) // part of the text onlyVariable fonts take coordinates on their axes:
face.variationAxes // tag, minimum, default, maximum
let bold = try HarfBuzzFont(face: face, size: 48, variations: ["wght": 700])Glyph lookups without shaping:
font.glyph(for: "A") // nominal glyph from the character map
font.glyphName(for: glyph) // from the post or CFF table
font.horizontalAdvance(of: glyph) // before kerning
font.extents(of: glyph) // outline box, y upWithout a size, positions are in font units (face.unitsPerEm per em).
To prepare glyphs before shaping, for example to rasterize an atlas, close a set of glyphs over the font's substitutions: shaping text made of their characters only uses glyphs in the result, whatever features are on.
let glyphs = Set("Hello, world".unicodeScalars.compactMap { font.glyph(for: $0) })
face.substitutionClosure(of: glyphs) // adds ligatures, alternates, small capitals...Faces and fonts are configured when they are made and are immutable afterwards, as HarfBuzz's own objects are once in use, so both are Sendable and can shape text from several threads at once. Each shape call uses its own buffer.
swift test --sanitize=address --disable-xctestThe tests use Inter (SIL Open Font License, see Tests/HarfBuzzSwiftTests/Resources/Inter-OFL.txt).