Sign in

    #gmlewis/moonbit-fonts

    check

    This is an experimental package to manipulate open source fonts with MoonBit.

    All fonts are open source and their licenses can all be found in their corresponding repos which are organized by the first letter of the name of the font:

    mbt-fonts-* packages contain hardcoded font data using Map[String, @fonts.Glyph].

    To add a font dependency, use the MoonBit package name (which follows the gmlewis/fonts-<letter>/<fontname> pattern). For example, to add the baloo font, run:
    moon add gmlewis/fonts-b/baloo

    The corresponding GitHub repositories are:

    • font: https://github.com/gmlewis/mbt-fonts-a/tree/master/aaarghnormal
    • license: https://github.com/gmlewis/go-fonts-a/tree/master/fonts/aaarghnormal
    • ...
    • font: https://github.com/gmlewis/mbt-fonts-b/tree/master/baloo
    • license: https://github.com/gmlewis/go-fonts-b/tree/master/fonts/baloo
    • ...
    • font: https://github.com/gmlewis/mbt-fonts-z/tree/master/znikomitno24
    • license: https://github.com/gmlewis/go-fonts-z/tree/master/fonts/znikomitno24

    #Quick Start

    See the examples/quick-start directory for a valid example of how to use this package.

    #Examples

    #checkerboard

    checkerboard is a simple example to create a draw.Graphic using the @draw API.

    #canvas-checkerboard

    canvas-checkerboard renders the checkerboard Graphic to an HTML5 canvas. Type ./run-canvas-checkerboard.sh in a terminal then open your browser to http://localhost:8080/examples/canvas-checkerboard to view it.

    #svg-checkerboard

    svg-checkerboard "renders" the checkerboard Graphic to an SVG file. Type moon run examples/svg-checkerboard > examples/svg-checkerboard/checkerboard.svg in a terminal then open this file in your browser to view it. For example, google-chrome examples/svg-checkerboard/checkerboard.svg.

    #Status

    The code has been updated to support compiler:

    $ moon version --all moon 0.1.20251222 (3f6c70c 2025-12-22) ~/.moon/bin/moon moonc v0.6.36+607dbed8f (2025-12-22) ~/.moon/bin/moonc moonrun 0.1.20251222 (3f6c70c 2025-12-22) ~/.moon/bin/moonrun moon-pilot 0.0.1-df92511 (2025-12-22) ~/.moon/bin/moon-pilot


    Enjoy!


    #License

    Please note that all fonts have their own licenses which are included in their respective original directories (see above).

    Copyright 2019-2024 Glenn M. Lewis. All Rights Reserved.

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


    FontError

    pub(all) suberror FontError String

    FontError represents an error that occurred during font processing.
    impl Eq for FontError
    impl Show for FontError

    AbsoluteCmd

    pub(all) enum AbsoluteCmd {
    M
    L
    C
    Q
    Z
    }

    AbsoluteCmd represents a supported absolute SVG command.
    impl Eq for AbsoluteCmd
    impl Show for AbsoluteCmd

    Cmd

    type Cmd

    Cmd represents an SVG command along with its parameters.
    impl Eq for Cmd
    impl Show for Cmd

    Font

    pub(all) struct Font {
    id : String
    horiz_adv_x : Double
    units_per_em : Double
    ascent : Double
    descent : Double
    glyphs : Map[String, Glyph]
    }

    Font represents an entire font.
    impl Eq for Font
    impl Show for Font
    impl ToJson for Font

    Font::gen_path

    fn Font::gen_path(self : Font, text : String, alignment? :
    Alignment
    , y_up? : Bool) -> Glyph raise FontError

    gen_path "renders" the provided text string into an SVG path using the provided font information. It performs the necessary path translations in order to combine the individual glyphs into a "super glyph" that can be rendered as a whole.

    Note that gen_path operates on the string as a left-justified whole and aligns the entire path relative to alignment accordingly.

    If you want multiple lines centered horizontally, use gen_paths instead.

    Note that the SVG standard states that positive-Y is "down" with origin coordinates in the upper-left, however, all SVG fonts appear to have their origin in the lower left with positive-Y moving "up". This package attempts to convert the SVG paths such that the font is equally readable in both the y_up=false SVG canvas environment or in a 3D y_up=true rendering environment.

    Font::gen_paths

    fn Font::gen_paths(self : Font, lines : Array[String], alignment? :
    Alignment
    , y_up? : Bool) -> Array[Glyph] raise FontError

    gen_paths handles multiple strings independently so that they can be aligned and distributed more flexibly.

    GerberLP

    pub(all) enum GerberLP {
    Dark
    Clear
    }

    GerberLP represents whether a subpath is Dark or Clear.
    impl Eq for GerberLP
    impl Show for GerberLP

    Glyph

    pub(all) struct Glyph {
    char : String
    horiz_adv_x : Double
    gerber_lp : String
    d : String
    xmin : Double
    ymin : Double
    xmax : Double
    ymax : Double
    }

    Glyph represents a single glyph within each Font and also represents multiple glyphs combined together (for example after using Font.gen_path and switching back and forth between Glyphs which are optimized for storage, and Paths which are optimized for processing).
    impl Eq for Glyph
    impl Show for Glyph
    impl ToJson for Glyph

    ParamPair

    pub(all) struct ParamPair {
    x : Double
    y : Double
    }

    ParamPair represents an X,Y absolute coordinate pair of parameters.
    impl Eq for ParamPair

    ParamPair::to_string

    fn ParamPair::to_string(self : ParamPair) -> String

    to_string returns a string representation of a ParamPair.

    Params

    type Params

    Params represents the parameters to an SVG command.
    impl Eq for Params
    impl Show for Params

    Params::length

    fn Params::length(self : Params) -> Int

    PathCmd

    pub(all) struct PathCmd {
    cmd : AbsoluteCmd
    gerber_lp : GerberLP
    params : Array[ParamPair]
    }

    PathCmd represents an individual absolute SVG command.
    impl Eq for PathCmd
    impl Show for PathCmd

    PathCmd::bbox

    bbox returns the minimum bounding box of a PathCmd.

    PathCmd::clone

    fn PathCmd::clone(self : PathCmd) -> PathCmd

    clone makes a deep copy of a PathCmd.

    PathCmd::from_svg_cmd

    fn PathCmd::from_svg_cmd(svg_cmd : Cmd, gerber_lp : GerberLP) -> PathCmd raise FontError

    from_svg_cmd creates a PathCmd from a raw SVG command and a GerberLP.

    PathCmd::to_svg_cmd

    fn PathCmd::to_svg_cmd(self : PathCmd) -> (Cmd, String)

    to_svg_cmd converts a PathCmd back to a raw SVG command and its GerberLP code.

    PathCmdFn

    pub(all) type PathCmdFn (Int, PathCmd) -> PathCmd

    PathCmdFn represents a function that processes or transforms individual commands. The first argument is the index within the command.

    PathCmdFn::inner

    #deprecated("Use `struct T(A)` to declare a newtype and use `.0` access the underlying type instead.")
    fn PathCmdFn::inner(self : PathCmdFn) -> ((Int, PathCmd) -> PathCmd)
    Convert newtype to its underlying type, automatically derived.

    SVGPath

    pub(all) struct SVGPath {
    char : String
    cmds : Array[PathCmd]
    xmin : Double
    ymin : Double
    xmax : Double
    ymax : Double
    }

    An SVGPath is identical to a Glyph but has been optimized internally for further manipulation, whereas a Glyph is optimized for compact storage of font data within all the font packages. An SVGPath can be converted to a Glyph and vice versa.
    impl Eq for SVGPath
    impl Show for SVGPath

    SVGPath::clone

    fn SVGPath::clone(self : SVGPath) -> SVGPath

    clone makes a deep copy of an SVGPath.

    SVGPath::from_glyph

    fn SVGPath::from_glyph(g : Glyph, path_cmd_fn? : PathCmdFn) -> SVGPath raise FontError

    from_glyph returns an SVGPath from a Glyph, optionally processing every Cmd with a processing function. Note that apart from path_cmd_fn, from_glyph makes no attempt to process the individual glyphs and simply transforms the representation.

    SVGPath::to_glyph

    fn SVGPath::to_glyph(self : SVGPath, path_cmd_fn? : PathCmdFn) -> Glyph

    to_glyph returns a "super" Glyph from an SVGPath, optionally processing every PathCmd with a processing function. Note that apart from path_cmd_fn, to_glyph makes no attempt to process the individual glyphs and simply transforms the representation.

    glyph_bbox

    fn glyph_bbox(d : String) ->
    BoundingBox
    raise FontError

    glyph_bbox returns the conservative bounding box of a Glyph path. It only supports the absolute SVG commands: M, L, C, Q, Z.

    Note that this function does NOT fully analyze the Cubic or Quadratic Bézier curves to determine their exact bounding boxes, but instead takes a naive and conservative approach by encapsulating the bounds of all control points in addition to curve anchor points which may result in returning a much larger bounding box than is actually needed to fully contain the glyph.

    translate_path

    fn translate_path(d : String, x : Double, y : Double, invert_y? : Bool) -> String raise FontError

    translate_path translates (moves) an SVG path by the provided offsets. If invert_y is true, all y values are scaled by -1.