Sign in

    moon_skrifa

    MoonBit port of Fontations skrifa (font parsing, metrics, color/bitmap glyph helpers).

    font
    opentype
    truetype
    Download zip
    Author
    Version
    0.1.4
    License
    Apache-2.0
    Last updated
    8 months ago
    Downloads
    56K

    No dependencies

    #Milky2018/moon_skrifa

    MoonBit port of Fontations skrifa (font parsing + metrics + color/bitmap glyph helpers).

    Repository: https://github.com/moonbit-community/moon_skrifa

    #Status

    • The public API and behavior aim to match fontations/skrifa (fontations-reference/ in this repo).
    • Embedded hinting (TrueType glyf) is implemented and wired into outline drawing via HintingInstance + DrawSettings::hinted(...).
      • Supports both simple and composite glyf glyphs (FreeType-style path building).
      • Non-pedantic hinting matches fontations behavior (interpreter errors may be ignored); pedantic mode returns errors.
    • Runtime autohinting (for fonts without embedded instructions) is implemented and integrated:
      • style classification + outline topology (segments/edges)
      • edge fitting + point alignment for PathStyle::FreeType and PathStyle::HarfBuzz
    • CFF/CFF2 outlines and Engine::Interpreter hinting are supported (including Type2 stem hinting parity tests).
    • Remaining parity work is tracked in bd (primarily broader core-module parity sweeps and maintenance).

    #Packages

    • Milky2018/moon_skrifa: facade re-exporting Milky2018/moon_skrifa/core (+ OutlineGlyph*)
    • Milky2018/moon_skrifa/core: core metadata (FontRef, charmap, names/strings, metrics, bitmaps, COLR traversal)
    • Milky2018/moon_skrifa/outline: outline extraction (glyf/CFF/CFF2), pens + SVG path output

    #Install / Import

    Add the dependency to your package moon.pkg.json:

    { "import": [ "moonbitlang/core/prelude", "moonbitlang/core/bytes", { "path": "Milky2018/moon_skrifa", "alias": "moon_skrifa" }, { "path": "Milky2018/moon_skrifa/outline", "alias": "moon_skrifa_outline" } ] }

    Then use the package via @moon_skrifa / @moon_skrifa_outline:

    let font = @moon_skrifa.FontRef::new(font_bytes).unwrap()
    let outlines = @moon_skrifa_outline.OutlineGlyphCollection::from_font(font)

    #Core APIs

    #Variation Axes / Locations (fvar/avar)

    If you have user-space axis values (e.g. wght=700), use axes().location(...) to build a normalized location (this also applies avar remapping when present):

    let axes = font.axes()
    let settings : Array[@moon_skrifa.VariationSetting] = Array::from_fixed_array([
    @moon_skrifa.Setting::new(@moon_skrifa.Tag::from_str("wght").unwrap(), 700.0),
    ])
    let loc = axes.location(settings.op_as_view()).as_ref()

    #Charmap (codepoint -> glyph id)

    let cmap = font.charmap().unwrap()
    let gid = cmap.glyph_id(0x41).unwrap() // 'A'

    #Name / Localized Strings (name table)

    let family = font.localized_strings(@moon_skrifa.STRING_ID_FAMILY_NAME)
    match family.english_or_first() {
    None => ()
    Some(s) => {
    let lang = s.language() // Option[String]
    let value = s.to_string()
    // ...
    }
    }

    #Font / Glyph Metrics

    let fm = font.metrics(@moon_skrifa.Size::new(16.0), @moon_skrifa.LocationRef::default())
    let gm = font.glyph_metrics(@moon_skrifa.Size::unscaled(), @moon_skrifa.LocationRef::default())

    let aw = gm.advance_width(gid).unwrap()
    let lsb = gm.left_side_bearing(gid).unwrap()
    let bounds = gm.bounds(gid) // Option[GlyphBounds]

    GlyphMetrics supports:
    • HVAR deltas when present
    • gvar phantom-deltas fallback for advance/lsb when HVAR is missing

    #Bitmap Strikes (sbix/CBDT/EBDT)

    let strikes = font.bitmap_strikes()
    for i in 0..<strikes.len() {
    let strike = strikes.get(i).unwrap()
    let bmp = strike.get(gid)
    // BitmapGlyph has format/data/ppem/origin/width/height
    }

    #COLRv1 Paint Traversal (gradients/var/clip)

    Implement ColorPainter and call:

    let loc = @moon_skrifa.LocationRef::default()
    try! font.paint_colr_v1_at(gid, loc, painter) |> ignore

    Brush variants include:
    • Solid(palette_index, alpha)
    • LinearGradient(p0, p1, stops, extend)
    • RadialGradient(c0, r0, c1, r1, stops, extend)
    • SweepGradient(c0, start_deg, end_deg, stops, extend)

    #Outline Package

    Use OutlineGlyphCollection to get outlines from glyf/CFF/CFF2:

    let gid = 1

    let settings =
    @moon_skrifa_outline.DrawSettings::unhinted(
    @moon_skrifa.Size::new(16.0),
    @moon_skrifa.LocationRef::default(),
    )

    let svg = outlines.svg_path(gid, settings).unwrap()
    let path = outlines.path(gid, settings).unwrap()

    DrawSettings controls:
    • size (@moon_skrifa.Size)
    • location (@moon_skrifa.LocationRef)
      • normalized coords are Int in F2Dot14; 16384 == 1.0
      • if you have user coords, prefer font.axes().location(...).as_ref()
    • path_style (PathStyle::FreeType or PathStyle::HarfBuzz)
    • optional embedded hinting via HintingInstance + DrawSettings::hinted(...)

    Create and use a hinting instance:

    let outlines0 = @moon_skrifa_outline.OutlineGlyphCollection::from_font(font)
    let hinter =
    @moon_skrifa_outline.HintingInstance::new(
    outlines0,
    @moon_skrifa.Size::new(16.0),
    @moon_skrifa.LocationRef::default(),
    @moon_skrifa_outline.HintingOptions::default(),
    ).unwrap()

    let settings = @moon_skrifa_outline.DrawSettings::hinted(hinter, false)
    let svg = outlines.svg_path(gid, settings).unwrap()

    #Development

    • moon check
    • moon test
    • moon fmt
    • moon info

    Attributes

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    Axis

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    AxisCollection

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    BitmapFormat

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    BitmapGlyph

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    BitmapImageFormat

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    BitmapStrike

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    BitmapStrikes

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    Brush

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    Charmap

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    Color

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    ColorGlyphCollection

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    ColorLayer

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    ColorPainter

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    ColorPalettes

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    ColorStop

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    CompositeMode

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    Decoration

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    Extend

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    FontRef

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    GlyphBounds

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    GlyphId

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    GlyphMetrics

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    GlyphName

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    GlyphNameSource

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    GlyphNames

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    HmtxMetrics

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    LocalizedString

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    LocalizedStrings

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    Location

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    LocationRef

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    MapVariant

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    MappingIndex

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    Metrics

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    NamedInstance

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    NamedInstanceCollection

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    NormalizedCoord

    type NormalizedCoord = Int

    Instance-related types.

    Ported from fontations/skrifa (Apache-2.0 OR MIT).

    OutlineGlyph

    Re-export common outline types from Milky2018/moon_skrifa/outline.

    OutlineGlyphCollection

    Re-export common outline types from Milky2018/moon_skrifa/outline.

    PaintCachedColorGlyph

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    PaintError

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    Setting

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    Size

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    Stretch

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    StringId

    type StringId = UInt16

    OpenType name identifier (NameID).

    This is a UInt16 in the OpenType spec.

    Style

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    TableProvider

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    Tag

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    Transform

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    VariationSetting

    type VariationSetting =
    Setting
    [Double]

    Definitions for specifying variations and typographic features.

    Ported from fontations/skrifa/src/setting.rs (Apache-2.0 OR MIT).

    Weight

    Re-export the main skrifa APIs from Milky2018/moon_skrifa/core.

    MetadataProvider

    Facade for types that can provide font metadata.

    Ported from fontations/skrifa/src/provider.rs (Apache-2.0 OR MIT).

    STRING_ID_COMPATIBLE_FULL_NAME

    let STRING_ID_COMPATIBLE_FULL_NAME : UInt16

    Compatible full name (Macintosh only).
    let STRING_ID_COPYRIGHT_NOTICE : UInt16

    Copyright notice.

    STRING_ID_DARK_BACKGROUND_PALETTE

    let STRING_ID_DARK_BACKGROUND_PALETTE : UInt16

    Dark background palette label.

    STRING_ID_DESCRIPTION

    let STRING_ID_DESCRIPTION : UInt16

    Description.

    STRING_ID_DESIGNER

    let STRING_ID_DESIGNER : UInt16

    Designer name.

    STRING_ID_DESIGNER_URL

    let STRING_ID_DESIGNER_URL : UInt16

    Designer URL.

    STRING_ID_FAMILY_NAME

    let STRING_ID_FAMILY_NAME : UInt16

    STRING_ID_FULL_NAME

    let STRING_ID_FULL_NAME : UInt16

    Full font name.

    STRING_ID_LAST_ALLOWED_NAME_ID

    let STRING_ID_LAST_ALLOWED_NAME_ID : UInt16

    The last value that is available for font-specific names.

    STRING_ID_LAST_RESERVED_NAME_ID

    let STRING_ID_LAST_RESERVED_NAME_ID : UInt16

    The last value that is explicitly reserved for standard names.

    STRING_ID_LICENSE_DESCRIPTION

    let STRING_ID_LICENSE_DESCRIPTION : UInt16

    License description.

    STRING_ID_LICENSE_URL

    let STRING_ID_LICENSE_URL : UInt16

    License URL.

    STRING_ID_LIGHT_BACKGROUND_PALETTE

    let STRING_ID_LIGHT_BACKGROUND_PALETTE : UInt16

    Light background palette label.

    STRING_ID_MANUFACTURER

    let STRING_ID_MANUFACTURER : UInt16

    Manufacturer name.

    STRING_ID_POSTSCRIPT_CID_NAME

    let STRING_ID_POSTSCRIPT_CID_NAME : UInt16

    PostScript CID findfont name.

    STRING_ID_POSTSCRIPT_NAME

    let STRING_ID_POSTSCRIPT_NAME : UInt16

    PostScript name.

    STRING_ID_SAMPLE_TEXT

    let STRING_ID_SAMPLE_TEXT : UInt16

    Sample text.

    STRING_ID_SUBFAMILY_NAME

    let STRING_ID_SUBFAMILY_NAME : UInt16

    STRING_ID_TRADEMARK

    let STRING_ID_TRADEMARK : UInt16

    Trademark.

    STRING_ID_TYPOGRAPHIC_FAMILY_NAME

    let STRING_ID_TYPOGRAPHIC_FAMILY_NAME : UInt16

    Typographic family name.

    STRING_ID_TYPOGRAPHIC_SUBFAMILY_NAME

    let STRING_ID_TYPOGRAPHIC_SUBFAMILY_NAME : UInt16

    Typographic subfamily name.

    STRING_ID_UNIQUE_ID

    let STRING_ID_UNIQUE_ID : UInt16

    Unique font identifier.

    STRING_ID_VARIATIONS_POSTSCRIPT_NAME_PREFIX

    let STRING_ID_VARIATIONS_POSTSCRIPT_NAME_PREFIX : UInt16

    Variations PostScript name prefix.

    STRING_ID_VENDOR_URL

    let STRING_ID_VENDOR_URL : UInt16

    Vendor URL.

    STRING_ID_VERSION_STRING

    let STRING_ID_VERSION_STRING : UInt16

    Version string.

    STRING_ID_WWS_FAMILY_NAME

    let STRING_ID_WWS_FAMILY_NAME : UInt16

    WWS family name.

    STRING_ID_WWS_SUBFAMILY_NAME

    let STRING_ID_WWS_SUBFAMILY_NAME : UInt16

    WWS subfamily name.

    string_id_checked_add

    fn string_id_checked_add(id : UInt16, rhs : UInt16) -> UInt16?

    string_id_is_reserved

    fn string_id_is_reserved(id : UInt16) -> Bool

    string_id_predefined

    fn string_id_predefined() -> Array[UInt16]

    Returns the set of predefined name identifiers according to the OpenType spec.

    This matches fontations/font-types::NameId::predefined(): IDs 0..15 and 16..=25 (note: ID 15 is not assigned).

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io