Skip to main content

TextEngine

Struct TextEngine 

Source
pub struct TextEngine { /* private fields */ }
Expand description

Fonts and metrics.

§Examples

use ferrochart_text::TextEngine;

let engine = TextEngine::new();
// The bundled font is monospaced: every digit has the same advance.
let adv = engine.ascii_advances(0);
assert!(adv[b'0' as usize] > 0.0);
assert_eq!(adv[b'1' as usize], adv[b'8' as usize]);
assert!(engine.line_metrics(0, 12.0).height() > 12.0);

Implementations§

Source§

impl TextEngine

Source

pub fn new() -> Self

An engine with the bundled font as font 0.

Source

pub fn add_font(&mut self, data: Vec<u8>) -> Result<u16, FontError>

Adds a font (TTF/OTF data) and returns its index.

§Errors

FontError::Invalid if the data cannot be parsed, FontError::TooManyFonts beyond u16::MAX fonts.

Source

pub fn font_count(&self) -> usize

Number of loaded fonts.

Source

pub fn ascii_advances(&self, id: u16) -> &[f32; 128]

ASCII advances of font id at a font size of 1 px. Unknown ids use font 0.

Source

pub fn subscript_advances(&self, id: u16) -> &[f32; 10]

Advances of the subscript digits ₀..=₉ (U+2080..=U+2089) of font id at a font size of 1 px. Unknown ids use font 0.

Source

pub fn line_metrics(&self, id: u16, size: f32) -> LineMetrics

Line metrics of font id at size px.

Source

pub fn advance(&self, id: u16, c: char, size: f32) -> f32

Advance of c at size px.

Source

pub fn width(&self, id: u16, text: &str, size: f32) -> f32

Width of text at size px: the sum of the advances, without kerning, exactly as the core’s ASCII fast path measures it.

Source

pub fn rasterize(&self, id: u16, c: char, size: f32) -> Option<GlyphBitmap>

Rasterizes c at size px with the pen on an integer pixel. None for glyphs without an outline (space).

Source

pub fn glyph_id(&self, id: u16, c: char) -> u16

The glyph id of c (for cache keys).

Trait Implementations§

Source§

impl Debug for TextEngine

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TextEngine

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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