use crate::{Angle, BoundingBox, Path, Rect, Rotate, SampleSettings, Scale, Spline, Translate, V2};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct GlyphId(pub u32);
#[derive(Clone, Debug)]
pub struct Glyph {
pub id: GlyphId,
pub advance: f32,
pub contours: Vec<Spline>,
}
impl Glyph {
pub fn to_paths(&self, sample_settings: SampleSettings) -> Vec<Path> {
self.contours
.iter()
.map(|contour| contour.to_path(sample_settings))
.collect()
}
}
impl Translate for Glyph {
fn translate(&self, dist: V2) -> Self {
Self {
id: self.id,
advance: self.advance,
contours: self
.contours
.iter()
.map(|contour| contour.translate(dist))
.collect(),
}
}
}
impl Rotate for Glyph {
fn rotate(&self, angle: Angle) -> Self {
Self {
id: self.id,
advance: self.advance,
contours: self
.contours
.iter()
.map(|contour| contour.rotate(angle))
.collect(),
}
}
}
impl Scale for Glyph {
fn scale(&self, factor: f32) -> Self {
Self {
id: self.id,
advance: self.advance * factor,
contours: self
.contours
.iter()
.map(|contour| contour.scale(factor))
.collect(),
}
}
}
impl BoundingBox for Glyph {
fn bounding_box(&self) -> Option<Rect> {
self.contours
.iter()
.filter_map(Spline::bounding_box)
.reduce(|a, b| Rect::new(a.bl().min(b.bl()), a.tr().max(b.tr())))
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct PositionedGlyph {
pub id: GlyphId,
pub offset: V2,
pub advance: f32,
}