Enum image2::text::Font

source ·
pub enum Font<'a> {
    Ref(Arc<Face<'a>>),
    Owned(Arc<OwnedFace>),
}
Expand description

A single font. This may or may not own the font data.

Lifetime

The lifetime reflects the font data lifetime. Font<'static> covers most cases ie both dynamically loaded owned data and for referenced compile time font data.

Example

let font_data: &[u8] = include_bytes!("../dev/fonts/dejavu/DejaVuSansMono.ttf");
let font: Font<'static> = Font::try_from_bytes(font_data)?;

let owned_font_data: Vec<u8> = font_data.to_vec();
let from_owned_font: Font<'static> = Font::try_from_vec(owned_font_data)?;

Variants§

§

Ref(Arc<Face<'a>>)

§

Owned(Arc<OwnedFace>)

Implementations§

source§

impl Font<'_>

source

pub fn try_from_bytes(bytes: &[u8]) -> Option<Font<'_>>

Creates a Font from byte-slice data.

Returns None for invalid data.

source

pub fn try_from_bytes_and_index(bytes: &[u8], index: u32) -> Option<Font<'_>>

Creates a Font from byte-slice data & a font collection index.

Returns None for invalid data.

source

pub fn try_from_vec(data: Vec<u8, Global>) -> Option<Font<'static>>

Creates a Font from owned font data.

Returns None for invalid data.

source

pub fn try_from_vec_and_index( data: Vec<u8, Global>, index: u32 ) -> Option<Font<'static>>

Creates a Font from owned font data & a font collection index.

Returns None for invalid data.

source§

impl<'font> Font<'font>

source

pub fn v_metrics(&self, scale: Scale) -> VMetrics

The “vertical metrics” for this font at a given scale. These metrics are shared by all of the glyphs in the font. See VMetrics for more detail.

source

pub fn v_metrics_unscaled(&self) -> VMetrics

Get the unscaled VMetrics for this font, shared by all glyphs. See VMetrics for more detail.

source

pub fn units_per_em(&self) -> u16

Returns the units per EM square of this font

source

pub fn glyph_count(&self) -> usize

The number of glyphs present in this font. Glyph identifiers for this font will always be in the range 0..self.glyph_count()

source

pub fn glyph<C>(&self, id: C) -> Glyph<'font>where C: IntoGlyphId,

Returns the corresponding glyph for a Unicode code point or a glyph id for this font.

If id is a GlyphId, it must be valid for this font; otherwise, this function panics. GlyphIds should always be produced by looking up some other sort of designator (like a Unicode code point) in a font, and should only be used to index the font they were produced for.

Note that code points without corresponding glyphs in this font map to the “.notdef” glyph, glyph 0.

source

pub fn glyphs_for<I, 'a>(&'a self, itr: I) -> GlyphIter<'a, 'font, I>where I: Iterator, <I as Iterator>::Item: IntoGlyphId,

A convenience function.

Returns an iterator that produces the glyphs corresponding to the code points or glyph ids produced by the given iterator itr.

This is equivalent in behaviour to itr.map(|c| font.glyph(c)).

source

pub fn layout<'a, 's>( &'a self, s: &'s str, scale: Scale, start: Point<f32> ) -> LayoutIter<'a, 'font, 's>

A convenience function for laying out glyphs for a string horizontally. It does not take control characters like line breaks into account, as treatment of these is likely to depend on the application.

Note that this function does not perform Unicode normalisation. Composite characters (such as ö constructed from two code points, ¨ and o), will not be normalised to single code points. So if a font does not contain a glyph for each separate code point, but does contain one for the normalised single code point (which is common), the desired glyph will not be produced, despite being present in the font. Deal with this by performing Unicode normalisation on the input string before passing it to layout. The crate unicode-normalization is perfect for this purpose.

Calling this function is equivalent to a longer sequence of operations involving glyphs_for, e.g.

font.layout("Hello World!", scale, start)

produces an iterator with behaviour equivalent to the following:

font.glyphs_for("Hello World!".chars())
    .scan((None, 0.0), |(last, x), g| {
        let g = g.scaled(scale);
        if let Some(last) = last {
            *x += font.pair_kerning(scale, *last, g.id());
        }
        let w = g.h_metrics().advance_width;
        let next = g.positioned(start + vector(*x, 0.0));
        *last = Some(next.id());
        *x += w;
        Some(next)
    })
source

pub fn pair_kerning<A, B>(&self, scale: Scale, first: A, second: B) -> f32where A: IntoGlyphId, B: IntoGlyphId,

Returns additional kerning to apply as well as that given by HMetrics for a particular pair of glyphs.

source

pub fn scale_for_pixel_height(&self, height: f32) -> f32

Computes a scale factor to produce a font whose “height” is ‘pixels’ tall. Height is measured as the distance from the highest ascender to the lowest descender; in other words, it’s equivalent to calling GetFontVMetrics and computing: scale = pixels / (ascent - descent) so if you prefer to measure height by the ascent only, use a similar calculation.

Trait Implementations§

source§

impl<'a> Clone for Font<'a>

source§

fn clone(&self) -> Font<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Font<'_>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Font<'a>

§

impl<'a> Send for Font<'a>

§

impl<'a> Sync for Font<'a>

§

impl<'a> Unpin for Font<'a>

§

impl<'a> UnwindSafe for Font<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.