pub struct VectorFont {
pub src: String,
pub units_per_em: f64,
pub ascender: f64,
pub descender: f64,
pub line_gap: f64,
pub cap_height: f64,
pub x_height: f64,
/* private fields */
}Expand description
A parsed font with the metrics and kerning sdf-text needs — a port of
lib3’s VectorFont. See the module doc for what diverges from a plain
ttf-parser reading.
Fields§
§src: StringSource URL/label, for cache keying. Mirrors VectorFont.src.
units_per_em: f64Font units per em.
ascender: f64Typographic ascender, font units.
descender: f64Typographic descender, font units (negative).
line_gap: f64Typographic line gap, font units.
cap_height: f64Cap height, font units, or 0 if the font has none.
x_height: f64X-height, font units, or 0 if the font has none.
Implementations§
Source§impl VectorFont
impl VectorFont
Sourcepub fn parse(data: Vec<u8>, src: impl Into<String>) -> Result<Self, Error>
pub fn parse(data: Vec<u8>, src: impl Into<String>) -> Result<Self, Error>
Port of new VectorFont(opentype.parse(buffer), src).
VectorFont.load in the JS also accepts a URL and fetches it; there
is no fetch here, so callers read the bytes themselves (see the skip
register in the crate README).
Sourcepub fn has_gpos(&self) -> bool
pub fn has_gpos(&self) -> bool
Whether the font has a GPOS table at all — the condition under which
opentype.js never falls back to kern.
Sourcepub fn glyph_for_char(&self, ch: char) -> GlyphId
pub fn glyph_for_char(&self, ch: char) -> GlyphId
Port of glyphForChar. opentype.js’s charToGlyph falls back to glyph 0
(.notdef) for an unmapped character rather than returning null, so this
never fails for a parsed font.
Sourcepub fn advance_width(&self, ch: char) -> f64
pub fn advance_width(&self, ch: char) -> f64
Horizontal advance in font units.
Sourcepub fn bounding_box(&self, ch: char) -> Option<BBox>
pub fn bounding_box(&self, ch: char) -> Option<BBox>
Ink bounding box in font units, Y-up; None for blank glyphs.
JS: if (!(b.x2 > b.x1 && b.y2 > b.y1)) return null; — so the all-zero
box opentype hands back for an empty outline becomes None.
Sourcepub fn glyph_path(&self, gid: GlyphId) -> Vec<PathCommand>
pub fn glyph_path(&self, gid: GlyphId) -> Vec<PathCommand>
glyph.path — the Y-up command list, Z-terminated per contour.
Sourcepub fn glyph_path_y_down(&self, gid: GlyphId) -> Vec<PathCommand>
pub fn glyph_path_y_down(&self, gid: GlyphId) -> Vec<PathCommand>
glyph.getPath(0, 0, unitsPerEm) — the same outline with Y negated and
the Z commands dropped (opentype only emits Z for stroked paths).
fontSize == unitsPerEm makes the scale exactly 1, so coordinates stay
at their font-unit values (halves appear from implied midpoints).
Sourcepub fn measure(&self, text: &str, font_size: f64) -> f64
pub fn measure(&self, text: &str, font_size: f64) -> f64
The width of one run of text at font_size, in the units
Text lays out in — the sum of the glyph advances with
the kerning between each adjacent pair applied, scaled by
font_size / units_per_em.
This is the measurement layout code needs before there is anything to
lay out: a d3 separation callback, a fit, a column width. It is the
same arithmetic layout_text_vector uses for its own line widths (it is
the same function), so the number agrees with the TextRenderInfo a
Text of that string and size reports — see the check in
tests/vector_font.rs.
Three things it deliberately does not do, all of them layout rather than measurement:
letterSpacingis zero. With a non-zero letter spacing the width ismeasure( s, size ) + letter_spacing * ( s.chars().count() - 1 ), spacing being applied between glyphs only.- No line breaking, so
max_widthplays no part; this is the width the text wants. - Newlines are not special:
'\n'is measured as the glyph the font maps it to (zero-advance.notdefin Roboto). Measure each line separately and take the widest.
The width is the advance sum, not the ink extent: it includes the first
and last glyphs’ side bearings, which is what a layout wants and what
TextRenderInfo::block_bounds (an ink box) is not.
There is no measure_info companion: the full-TextRenderInfo
measurement with no scene object already exists as
layout_text_vector( params, Some( font ) ),
which is what Text::sync itself calls.