write_fonts/tables/
glyf.rs

1//! The [glyf (Glyph Data)](https://docs.microsoft.com/en-us/typography/opentype/spec/glyf) table
2
3use crate::{
4    from_obj::{FromObjRef, FromTableRef},
5    validate::{Validate, ValidationCtx},
6    FontWrite, OtRound, TableWriter,
7};
8
9use font_types::Tag;
10use kurbo::Rect;
11use read_fonts::{FontRead, TopLevelTable};
12
13mod composite;
14mod glyf_loca_builder;
15mod simple;
16
17pub use composite::{Anchor, Component, ComponentFlags, CompositeGlyph, Transform};
18pub use glyf_loca_builder::{GlyfLocaBuilder, SomeGlyph};
19pub use simple::{Contour, MalformedPath, SimpleGlyph};
20
21/// The [glyf (Glyph Data)](https://docs.microsoft.com/en-us/typography/opentype/spec/glyf) table
22///
23/// This table is the concatenated bytes of all the glyphs in the font, with
24/// the positions of each individual glyph stored in the ['loca' table][super::loca].
25/// As such, these two tables must be constructed together. The [`GlyfLocaBuilder`]
26/// type is provided to simplify this.
27#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
28#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
29pub struct Glyf(Vec<u8>);
30
31impl TopLevelTable for Glyf {
32    /// 'glyf'
33    const TAG: Tag = Tag::new(b"glyf");
34}
35
36/// A simple or composite glyph
37#[derive(Clone, Debug, PartialEq, Eq)]
38pub enum Glyph {
39    /// An empty glyph gets an entry in `loca`, but no data is written to `glyf`
40    Empty,
41    Simple(SimpleGlyph),
42    Composite(CompositeGlyph),
43}
44
45/// A Bounding box.
46///
47/// This should be the minimum rectangle which fully encloses the glyph outline;
48/// importantly this can only be determined by computing the individual Bezier
49/// segments, and cannot be determiend from points alone.
50#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
51pub struct Bbox {
52    pub x_min: i16,
53    pub y_min: i16,
54    pub x_max: i16,
55    pub y_max: i16,
56}
57
58impl Glyph {
59    /// The bounding box for the glyph
60    pub fn bbox(&self) -> Option<Bbox> {
61        match self {
62            Glyph::Empty => None,
63            Glyph::Simple(glyph) => Some(glyph.bbox),
64            Glyph::Composite(glyph) => Some(glyph.bbox),
65        }
66    }
67}
68
69impl Bbox {
70    /// Return the smallest bounding box covering `self` and `other`
71    pub fn union(self, other: Bbox) -> Bbox {
72        Bbox {
73            x_min: self.x_min.min(other.x_min),
74            y_min: self.y_min.min(other.y_min),
75            x_max: self.x_max.max(other.x_max),
76            y_max: self.y_max.max(other.y_max),
77        }
78    }
79}
80
81impl From<Rect> for Bbox {
82    fn from(value: Rect) -> Self {
83        Bbox {
84            x_min: value.min_x().ot_round(),
85            y_min: value.min_y().ot_round(),
86            x_max: value.max_x().ot_round(),
87            y_max: value.max_y().ot_round(),
88        }
89    }
90}
91
92impl FontWrite for Bbox {
93    fn write_into(&self, writer: &mut crate::TableWriter) {
94        let Bbox {
95            x_min,
96            y_min,
97            x_max,
98            y_max,
99        } = *self;
100        [x_min, y_min, x_max, y_max].write_into(writer)
101    }
102}
103
104impl<'a> FromObjRef<read_fonts::tables::glyf::Glyph<'a>> for Glyph {
105    fn from_obj_ref(
106        from: &read_fonts::tables::glyf::Glyph<'a>,
107        data: read_fonts::FontData,
108    ) -> Self {
109        match from {
110            read_fonts::tables::glyf::Glyph::Simple(glyph) => {
111                Self::Simple(SimpleGlyph::from_obj_ref(glyph, data))
112            }
113            read_fonts::tables::glyf::Glyph::Composite(glyph) => {
114                Self::Composite(CompositeGlyph::from_obj_ref(glyph, data))
115            }
116        }
117    }
118}
119
120impl FromTableRef<read_fonts::tables::glyf::Glyph<'_>> for Glyph {}
121
122impl<'a> FontRead<'a> for Glyph {
123    fn read(data: read_fonts::FontData<'a>) -> Result<Self, read_fonts::ReadError> {
124        read_fonts::tables::glyf::Glyph::read(data).map(|g| Glyph::from_table_ref(&g))
125    }
126}
127
128impl From<SimpleGlyph> for Glyph {
129    fn from(value: SimpleGlyph) -> Self {
130        if value.contours.is_empty() {
131            Glyph::Empty
132        } else {
133            Glyph::Simple(value)
134        }
135    }
136}
137
138impl From<CompositeGlyph> for Glyph {
139    fn from(value: CompositeGlyph) -> Self {
140        Glyph::Composite(value)
141    }
142}
143
144impl Validate for Glyph {
145    fn validate_impl(&self, ctx: &mut ValidationCtx) {
146        match self {
147            Glyph::Empty => (),
148            Glyph::Simple(glyph) => glyph.validate_impl(ctx),
149            Glyph::Composite(glyph) => glyph.validate_impl(ctx),
150        }
151    }
152}
153
154impl FontWrite for Glyph {
155    fn write_into(&self, writer: &mut crate::TableWriter) {
156        match self {
157            Glyph::Empty => (),
158            Glyph::Simple(glyph) => glyph.write_into(writer),
159            Glyph::Composite(glyph) => glyph.write_into(writer),
160        }
161    }
162}
163
164impl Validate for Glyf {
165    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
166}
167
168impl FontWrite for Glyf {
169    fn write_into(&self, writer: &mut TableWriter) {
170        writer.write_slice(&self.0)
171    }
172}
173
174#[cfg(test)]
175mod tests {
176    use super::*;
177
178    #[test]
179    fn union_box() {
180        assert_eq!(
181            Bbox {
182                x_min: -1,
183                y_min: -2,
184                x_max: 5,
185                y_max: 6
186            },
187            Bbox {
188                x_min: 0,
189                y_min: 0,
190                x_max: 5,
191                y_max: 6
192            }
193            .union(Bbox {
194                x_min: -1,
195                y_min: -2,
196                x_max: 3,
197                y_max: 4
198            })
199        )
200    }
201}