Skip to main content

VectorFont

Struct VectorFont 

Source
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: String

Source URL/label, for cache keying. Mirrors VectorFont.src.

§units_per_em: f64

Font units per em.

§ascender: f64

Typographic ascender, font units.

§descender: f64

Typographic descender, font units (negative).

§line_gap: f64

Typographic line gap, font units.

§cap_height: f64

Cap height, font units, or 0 if the font has none.

§x_height: f64

X-height, font units, or 0 if the font has none.

Implementations§

Source§

impl VectorFont

Source

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).

Source

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.

Source

pub fn has_kern(&self) -> bool

Whether the font has a legacy kern table.

Source

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.

Source

pub fn advance_width(&self, ch: char) -> f64

Horizontal advance in font units.

Source

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.

Source

pub fn glyph_path(&self, gid: GlyphId) -> Vec<PathCommand>

glyph.path — the Y-up command list, Z-terminated per contour.

Source

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).

Source

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:

  • letterSpacing is zero. With a non-zero letter spacing the width is measure( s, size ) + letter_spacing * ( s.chars().count() - 1 ), spacing being applied between glyphs only.
  • No line breaking, so max_width plays 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 .notdef in 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.

Source

pub fn kerning(&self, left: char, right: char) -> f64

Kerning adjustment in font units. See the module doc for the semantics.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,