use crate::{
point, v2, Glyph, GlyphId, GlyphSvg, Outline, OutlinedGlyph, PxScale, PxScaleFont, Rect,
ScaleFont,
};
pub trait Font {
fn units_per_em(&self) -> Option<f32>;
fn pt_to_px_scale(&self, pt_size: f32) -> Option<PxScale> {
let px_per_em = pt_size * (96.0 / 72.0);
let units_per_em = self.units_per_em()?;
let height = self.height_unscaled();
Some(PxScale::from(px_per_em * height / units_per_em))
}
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 italic_angle(&self) -> f32 {
0.0
}
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<'_>;
#[allow(deprecated)]
#[deprecated(
since = "0.2.22",
note = "Deprecated in favor of `glyph_raster_image2`"
)]
fn glyph_raster_image(&self, id: GlyphId, pixel_size: u16) -> Option<crate::GlyphImage> {
self.glyph_raster_image2(id, pixel_size)
.map(|i| crate::GlyphImage {
origin: i.origin,
scale: i.pixels_per_em.into(),
data: i.data,
format: i.format,
})
}
fn glyph_raster_image2(&self, id: GlyphId, pixel_size: u16) -> Option<v2::GlyphImage>;
fn glyph_svg_image(&self, id: GlyphId) -> Option<GlyphSvg> {
_ = id;
None }
#[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(),
}
}
#[inline]
fn font_data(&self) -> &[u8] {
unimplemented!()
}
}
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 italic_angle(&self) -> f32 {
(*self).italic_angle()
}
#[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()
}
#[inline]
fn glyph_raster_image2(&self, id: GlyphId, size: u16) -> Option<v2::GlyphImage> {
(*self).glyph_raster_image2(id, size)
}
#[inline]
fn glyph_svg_image(&self, id: GlyphId) -> Option<GlyphSvg> {
(*self).glyph_svg_image(id)
}
#[inline]
fn font_data(&self) -> &[u8] {
(*self).font_data()
}
}