use crate::{point, Glyph, GlyphId, Outline, OutlinedGlyph, PxScale, PxScaleFont, Rect, ScaleFont};
pub trait Font {
fn units_per_em(&self) -> Option<f32>;
fn ascent_unscaled(&self) -> f32;
fn descent_unscaled(&self) -> f32;
#[inline]
fn height_unscaled(&self) -> f32 {
self.ascent_unscaled() - self.descent_unscaled()
}
fn line_gap_unscaled(&self) -> f32;
fn glyph_id(&self, c: char) -> GlyphId;
fn h_advance_unscaled(&self, id: GlyphId) -> f32;
fn h_side_bearing_unscaled(&self, id: GlyphId) -> f32;
fn v_advance_unscaled(&self, id: GlyphId) -> f32;
fn v_side_bearing_unscaled(&self, id: GlyphId) -> f32;
fn kern_unscaled(&self, first: GlyphId, second: GlyphId) -> f32;
fn outline(&self, id: GlyphId) -> Option<Outline>;
fn glyph_count(&self) -> usize;
fn codepoint_ids(&self) -> crate::CodepointIdIter<'_>;
#[inline]
fn glyph_bounds(&self, glyph: &Glyph) -> Rect
where
Self: Sized,
{
let sf = self.as_scaled(glyph.scale);
let pos = glyph.position;
Rect {
min: point(pos.x - sf.h_side_bearing(glyph.id), pos.y - sf.ascent()),
max: point(pos.x + sf.h_advance(glyph.id), pos.y - sf.descent()),
}
}
#[inline]
fn outline_glyph(&self, glyph: Glyph) -> Option<OutlinedGlyph>
where
Self: Sized,
{
let outline = self.outline(glyph.id)?;
let scale_factor = self.as_scaled(glyph.scale).scale_factor();
Some(OutlinedGlyph::new(glyph, outline, scale_factor))
}
#[inline]
fn as_scaled<S: Into<PxScale>>(&self, scale: S) -> PxScaleFont<&'_ Self>
where
Self: Sized,
{
PxScaleFont {
font: &self,
scale: scale.into(),
}
}
#[inline]
fn into_scaled<S: Into<PxScale>>(self, scale: S) -> PxScaleFont<Self>
where
Self: core::marker::Sized,
{
PxScaleFont {
font: self,
scale: scale.into(),
}
}
}
impl<F: Font> Font for &F {
#[inline]
fn units_per_em(&self) -> Option<f32> {
(*self).units_per_em()
}
#[inline]
fn ascent_unscaled(&self) -> f32 {
(*self).ascent_unscaled()
}
#[inline]
fn descent_unscaled(&self) -> f32 {
(*self).descent_unscaled()
}
#[inline]
fn line_gap_unscaled(&self) -> f32 {
(*self).line_gap_unscaled()
}
#[inline]
fn glyph_id(&self, c: char) -> GlyphId {
(*self).glyph_id(c)
}
#[inline]
fn h_advance_unscaled(&self, id: GlyphId) -> f32 {
(*self).h_advance_unscaled(id)
}
#[inline]
fn h_side_bearing_unscaled(&self, id: GlyphId) -> f32 {
(*self).h_side_bearing_unscaled(id)
}
#[inline]
fn v_advance_unscaled(&self, id: GlyphId) -> f32 {
(*self).v_advance_unscaled(id)
}
#[inline]
fn v_side_bearing_unscaled(&self, id: GlyphId) -> f32 {
(*self).v_side_bearing_unscaled(id)
}
#[inline]
fn kern_unscaled(&self, first: GlyphId, second: GlyphId) -> f32 {
(*self).kern_unscaled(first, second)
}
#[inline]
fn outline(&self, glyph: GlyphId) -> Option<Outline> {
(*self).outline(glyph)
}
#[inline]
fn glyph_count(&self) -> usize {
(*self).glyph_count()
}
#[inline]
fn codepoint_ids(&self) -> crate::CodepointIdIter<'_> {
(*self).codepoint_ids()
}
}