Trait ab_glyph::Font [−][src]
Functionality required from font data.
See also FontArc
, FontRef
and FontVec
.
Units
Units of unscaled accessors are "font units", which is an arbitrary unit
defined by the font. See Font::units_per_em
.
Font sizes are typically specified in "points". According to the modern standard, 1pt = 1/72in. The "point size" of a font is the number of points per em.
The DPI (dots-per-inch) of a screen depends on the screen in question; 96 DPI is often considered the "standard". For high-DPI displays the DPI may be specified directly or one may multiply 96 by a scale-factor.
Thus, for example, a 10pt font on a 96 pixels-per-inch display has
10 / 72 * 96 = 13.333... pixels-per-em. If we divide this number by
units_per_em
we then get a scaling factor: pixels-per-font-unit.
Note however that since PxScale
values are relative to the text height,
one further step is needed: multiply by Font::height_unscaled
.
use ab_glyph::{Font, PxScale}; fn pt_size_to_px_scale<F: Font>(font: &F, pt_size: f32, screen_scale_factor: f32) -> PxScale { let px_per_em = pt_size * screen_scale_factor * (96.0 / 72.0); let units_per_em = font.units_per_em().unwrap(); let height = font.height_unscaled(); PxScale::from(px_per_em * height / units_per_em) }
Required methods
fn units_per_em(&self) -> Option<f32>
[src]
Get the size of the font unit
This returns "font units per em", where 1em is a base unit of font scale (typically the width of a capital 'M').
Returns None
in case the font unit size exceeds the expected range.
See Face::units_per_em
.
fn ascent_unscaled(&self) -> f32
[src]
Unscaled glyph ascent.
Scaling can be done with as_scaled.
fn descent_unscaled(&self) -> f32
[src]
Unscaled glyph descent.
Scaling can be done with as_scaled.
fn line_gap_unscaled(&self) -> f32
[src]
Unscaled line gap.
Scaling can be done with as_scaled.
fn glyph_id(&self, c: char) -> GlyphId
[src]
Lookup a GlyphId
matching a given char
.
Scaling can be done with as_scaled.
fn h_advance_unscaled(&self, id: GlyphId) -> f32
[src]
Unscaled horizontal advance for a given glyph id.
Scaling can be done with as_scaled.
fn h_side_bearing_unscaled(&self, id: GlyphId) -> f32
[src]
Unscaled horizontal side bearing for a given glyph id.
Scaling can be done with as_scaled.
fn v_advance_unscaled(&self, id: GlyphId) -> f32
[src]
Unscaled vertical advance for a given glyph id.
Scaling can be done with as_scaled.
fn v_side_bearing_unscaled(&self, id: GlyphId) -> f32
[src]
Unscaled vertical side bearing for a given glyph id.
Scaling can be done with as_scaled.
fn kern_unscaled(&self, first: GlyphId, second: GlyphId) -> f32
[src]
Returns additional unscaled kerning to apply for a particular pair of glyph ids.
Scaling can be done with as_scaled.
fn outline(&self, id: GlyphId) -> Option<Outline>
[src]
Compute unscaled glyph outline curves & bounding box.
fn glyph_count(&self) -> usize
[src]
The number of glyphs present in this font. Glyph identifiers for this
font will always be in the range 0..self.glyph_count()
fn codepoint_ids(&self) -> CodepointIdIter<'_>ⓘNotable traits for CodepointIdIter<'a>
impl<'a> Iterator for CodepointIdIter<'a> type Item = (GlyphId, char);
[src]
Notable traits for CodepointIdIter<'a>
impl<'a> Iterator for CodepointIdIter<'a> type Item = (GlyphId, char);
Returns an iterator of all distinct (GlyphId, char)
pairs. Not ordered.
Example
let font = FontRef::try_from_slice(include_bytes!("../../dev/fonts/Exo2-Light.otf"))?; // Iterate over pairs, each id will appear at most once. let mut codepoint_ids = font.codepoint_ids(); assert_eq!(codepoint_ids.next(), Some((GlyphId(408), '\r'))); assert_eq!(codepoint_ids.next(), Some((GlyphId(1), ' '))); assert_eq!(codepoint_ids.next(), Some((GlyphId(75), '!'))); // Build a lookup map for all ids let map: HashMap<_, _> = font.codepoint_ids().collect(); assert_eq!(map.get(&GlyphId(75)), Some(&'!'));
Provided methods
fn height_unscaled(&self) -> f32
[src]
Unscaled height ascent - descent
.
Scaling can be done with as_scaled.
fn glyph_bounds(&self, glyph: &Glyph) -> Rect where
Self: Sized,
[src]
Self: Sized,
Returns the layout bounds of this glyph. These are different to the outline px_bounds()
.
Horizontally: Glyph position +/- h_advance/h_side_bearing. Vertically: Glyph position +/- ascent/descent.
fn outline_glyph(&self, glyph: Glyph) -> Option<OutlinedGlyph> where
Self: Sized,
[src]
Self: Sized,
Compute glyph outline ready for drawing.
fn as_scaled<S: Into<PxScale>>(&self, scale: S) -> PxScaleFont<&Self> where
Self: Sized,
[src]
Self: Sized,
Construct a PxScaleFontRef
by associating with the
given pixel scale
.
Example
let font = FontRef::try_from_slice(include_bytes!("../../dev/fonts/Exo2-Light.otf"))?; assert_eq!(font.descent_unscaled(), -201.0); assert_eq!(font.as_scaled(24.0).descent(), -4.02); assert_eq!(font.as_scaled(50.0).descent(), -8.375);
fn into_scaled<S: Into<PxScale>>(self, scale: S) -> PxScaleFont<Self> where
Self: Sized,
[src]
Self: Sized,
Move into a PxScaleFont
associated with the
given pixel scale
.
Implementations on Foreign Types
impl<F: Font> Font for &F
[src]
fn units_per_em(&self) -> Option<f32>
[src]
fn ascent_unscaled(&self) -> f32
[src]
fn descent_unscaled(&self) -> f32
[src]
fn line_gap_unscaled(&self) -> f32
[src]
fn glyph_id(&self, c: char) -> GlyphId
[src]
fn h_advance_unscaled(&self, id: GlyphId) -> f32
[src]
fn h_side_bearing_unscaled(&self, id: GlyphId) -> f32
[src]
fn v_advance_unscaled(&self, id: GlyphId) -> f32
[src]
fn v_side_bearing_unscaled(&self, id: GlyphId) -> f32
[src]
fn kern_unscaled(&self, first: GlyphId, second: GlyphId) -> f32
[src]
fn outline(&self, glyph: GlyphId) -> Option<Outline>
[src]
fn glyph_count(&self) -> usize
[src]
fn codepoint_ids(&self) -> CodepointIdIter<'_>ⓘNotable traits for CodepointIdIter<'a>
impl<'a> Iterator for CodepointIdIter<'a> type Item = (GlyphId, char);
[src]
Notable traits for CodepointIdIter<'a>
impl<'a> Iterator for CodepointIdIter<'a> type Item = (GlyphId, char);
Implementors
impl Font for FontArc
[src]
fn units_per_em(&self) -> Option<f32>
[src]
fn ascent_unscaled(&self) -> f32
[src]
fn descent_unscaled(&self) -> f32
[src]
fn line_gap_unscaled(&self) -> f32
[src]
fn glyph_id(&self, c: char) -> GlyphId
[src]
fn h_advance_unscaled(&self, id: GlyphId) -> f32
[src]
fn h_side_bearing_unscaled(&self, id: GlyphId) -> f32
[src]
fn v_advance_unscaled(&self, id: GlyphId) -> f32
[src]
fn v_side_bearing_unscaled(&self, id: GlyphId) -> f32
[src]
fn kern_unscaled(&self, first: GlyphId, second: GlyphId) -> f32
[src]
fn outline(&self, glyph: GlyphId) -> Option<Outline>
[src]
fn glyph_count(&self) -> usize
[src]
fn codepoint_ids(&self) -> CodepointIdIter<'_>ⓘNotable traits for CodepointIdIter<'a>
impl<'a> Iterator for CodepointIdIter<'a> type Item = (GlyphId, char);
[src]
Notable traits for CodepointIdIter<'a>
impl<'a> Iterator for CodepointIdIter<'a> type Item = (GlyphId, char);
impl Font for FontRef<'_>
[src]
fn units_per_em(&self) -> Option<f32>
[src]
fn ascent_unscaled(&self) -> f32
[src]
fn descent_unscaled(&self) -> f32
[src]
fn line_gap_unscaled(&self) -> f32
[src]
fn glyph_id(&self, c: char) -> GlyphId
[src]
fn h_advance_unscaled(&self, id: GlyphId) -> f32
[src]
fn h_side_bearing_unscaled(&self, id: GlyphId) -> f32
[src]
fn v_advance_unscaled(&self, id: GlyphId) -> f32
[src]
fn v_side_bearing_unscaled(&self, id: GlyphId) -> f32
[src]
fn kern_unscaled(&self, first: GlyphId, second: GlyphId) -> f32
[src]
fn outline(&self, id: GlyphId) -> Option<Outline>
[src]
fn glyph_count(&self) -> usize
[src]
fn codepoint_ids<'a>(&'a self) -> CodepointIdIter<'a>ⓘNotable traits for CodepointIdIter<'a>
impl<'a> Iterator for CodepointIdIter<'a> type Item = (GlyphId, char);
[src]
Notable traits for CodepointIdIter<'a>
impl<'a> Iterator for CodepointIdIter<'a> type Item = (GlyphId, char);
impl Font for FontVec
[src]
fn units_per_em(&self) -> Option<f32>
[src]
fn ascent_unscaled(&self) -> f32
[src]
fn descent_unscaled(&self) -> f32
[src]
fn line_gap_unscaled(&self) -> f32
[src]
fn glyph_id(&self, c: char) -> GlyphId
[src]
fn h_advance_unscaled(&self, id: GlyphId) -> f32
[src]
fn h_side_bearing_unscaled(&self, id: GlyphId) -> f32
[src]
fn v_advance_unscaled(&self, id: GlyphId) -> f32
[src]
fn v_side_bearing_unscaled(&self, id: GlyphId) -> f32
[src]
fn kern_unscaled(&self, first: GlyphId, second: GlyphId) -> f32
[src]
fn outline(&self, id: GlyphId) -> Option<Outline>
[src]
fn glyph_count(&self) -> usize
[src]
fn codepoint_ids<'a>(&'a self) -> CodepointIdIter<'a>ⓘNotable traits for CodepointIdIter<'a>
impl<'a> Iterator for CodepointIdIter<'a> type Item = (GlyphId, char);
[src]
Notable traits for CodepointIdIter<'a>
impl<'a> Iterator for CodepointIdIter<'a> type Item = (GlyphId, char);