Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-svg

Source of release 0.9.2

0.9.2 / src / svg / text_fonts.lucb

#==============================================================================================
#
#   svg/text_fonts - Where SVG text gets its glyphs
#
#   DESCRIPTION:
#       The text layout (text_layout.lucb) asks a GlyphSource for faces, glyphs, advances,
#       kerning and outlines. A caller passes one in Options (a font set of its own, or a
#       test's synthetic glyphs); without one, `load` uses FontFiles over the system's font
#       directories (text_font_files.lucb), read and shaped by luce-fonts, and with
#       `system_fonts` false text is laid out but draws nothing. A source that shapes
#       (ligatures, marks, Arabic joining, Indic reordering) does so in `shape`; one that
#       does not maps a scalar at a time with kerning between pairs. Scalars a face lacks
#       are looked up in the source's fallback face.
#
#==============================================================================================

## A face's vertical metrics, in its font units (y up, the baseline at 0).
pub struct FaceMetrics:
    pub var units_per_em: f64 = 1000.0
    pub var ascent: f64 = 800.0
    pub var descent: f64 = -200.0
    pub var x_height: f64 = 500.0
    pub var underline_position: f64 = -100.0
    pub var underline_thickness: f64 = 50.0
    pub var strikeout_position: f64 = 250.0
    pub var subscript_offset: f64 = 140.0
    pub var superscript_offset: f64 = 480.0

## What a font style asks for.
pub struct FaceQuery:
    ## The CSS font-family list as written (names unquoted by the source, generic
    ## families such as serif and sans-serif included).
    pub var families: str = ""
    ## 100..900.
    pub var weight: u32 = 400
    ## 0 normal, 1 italic, 2 oblique.
    pub var style: u8 = 0
    ## Percent of normal width (50..200).
    pub var stretch: f64 = 100.0

## A glyph as a shaper places it: its glyph, the index (in the run it was given) of the
## first scalar of the cluster it belongs to, and its advance and offsets in font units
## (y up).
pub struct GlyphRecord:
    pub var glyph: u32 = 0
    pub var cluster: usize = 0
    pub var advance: f64 = 0.0
    pub var x_offset: f64 = 0.0
    pub var y_offset: f64 = 0.0

## A source of faces and glyphs for SVG text. Faces and glyphs are numbered by the
## source; outlines are in font units with y up.
pub interface GlyphSource:
    ## The face that best matches `query`, or none.
    func select_face(query: FaceQuery) -> u32?

    ## A face with a glyph for `scalar`, to use where `face` has none; none to give up.
    func fallback_face(face: u32, scalar: u32) -> u32?

    ## The face's metrics.
    func face_metrics(face: u32) -> FaceMetrics

    ## The glyph `face` maps `scalar` to; 0 when it has none.
    func glyph_index(face: u32, scalar: u32) -> u32

    ## How far a glyph advances the pen, in font units.
    func glyph_advance(face: u32, glyph: u32) -> f64

    ## The kerning adjustment between two glyphs, in font units.
    func glyph_kerning(face: u32, left: u32, right: u32) -> f64

    ## Shape `scalars`, one run of one direction in logical order, with `face` as a
    ## shaper such as HarfBuzz does (ligatures, marks, contextual forms; kerning when
    ## `kerning`, the smcp feature when `small_caps`): write its glyphs into `out` in
    ## visual order and answer how many there are, even when `out` holds fewer (it is
    ## then called again with room for all). Glyph 0 marks a scalar the face lacks.
    ## None when the source does not shape: glyphs are then mapped a scalar at a time.
    func shape(face: u32, scalars: const u32[], rtl: bool, kerning: bool, small_caps: bool, out: GlyphRecord[]) -> usize?!

    ## Append the glyph's outline to `path`; false when it has none.
    func glyph_outline(face: u32, glyph: u32, path: Path*) -> bool!

## Shape `scalars` without a shaper: a glyph per scalar, kerned against the next when
## `kerning`, reversed for right-to-left runs.
func shape_simply(source: GlyphSource, face: u32, scalars: const u32[], rtl: bool, kerning: bool, out: List[GlyphRecord]*) -> !:
    var fonts = source
    let start = out.used
    for at in 0..<scalars.length:
        let glyph = fonts.glyph_index(face, scalars[at])
        if kerning and out.used > start:
            let previous = &out.items[out.used - 1]
            previous.advance += fonts.glyph_kerning(face, previous.glyph, glyph)
        try out.push(GlyphRecord(glyph = glyph, cluster = at, advance = fonts.glyph_advance(face, glyph)))
    if rtl:
        var lo = start
        var hi = out.used
        while lo + 1 < hi:
            hi -= 1
            let swap = out.items[lo]
            out.items[lo] = out.items[hi]
            out.items[hi] = swap
            lo += 1