MoonBit port of Rust swash for font metadata, text shaping, and glyph scaling/rendering.
Add imports in your moon.pkg:
import {
"Milky2018/moon_swash" @swash,
"Milky2018/moon_swash/shape" @swash_shape,
"Milky2018/moon_swash/scale" @swash_scale,
}
Optional modules:
import {
"Milky2018/moon_swash/iter" @swash_iter,
"Milky2018/moon_swash/proxy" @swash_proxy,
}
let data : Bytes = ...
let font = @swash.FontRef::from_offset(data, 0).unwrap()let cx = @swash_shape.ShapeContext::new()
let shaper = cx.builder(font).size(14.0).build()
shaper.add_str("Hello, world!")
shaper.shape_with(fn(cluster) {
// cluster.glyphs() / cluster.source() ...
cluster |> ignore
})let gid = font.charmap().map(('Q').to_int().reinterpret_as_uint())
let scx = @swash_scale.ScaleContext::new()
let scaler = scx.builder(font).size(14.0).hint(true).build()
let render = @swash_scale.Render::new([@swash_scale.Source::Outline])
let img = render.render(scaler, gid).unwrap()
// Zero-copy pixel access:
let pixels = img.data_view()
pixels |> ignore
// GPU-friendly RGBA8 (premultiplied):
let white = [(255).to_byte(), (255).to_byte(), (255).to_byte(), (255).to_byte()]
img.to_rgba8(white) |> ignore
let rgba8 = img.data_view()
rgba8 |> ignoreMilky2018/moon_swash: core font APIs (FontRef, attributes, metrics, variations, palettes, strikes).Milky2018/moon_swash/shape: OpenType/AAT shaping and glyph clustering.Milky2018/moon_swash/scale: outline/bitmap scaling and rendering.
Content::SubpixelMaskstores per-channel coverage in RGB; the alpha channel is unused by the rasterizer.Image::to_rgba8(base_color)convertsMask/SubpixelMaskinto premultiplied RGBA8 and sets alpha tomax(r, g, b)coverage (modulated bybase_color[3]).- Use premultiplied-alpha blending when compositing (
src = 1,dst = 1 - srcA).
Apache-2.0.