1use 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, ReadArgs, 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#[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 const TAG: Tag = Tag::new(b"glyf");
34}
35
36#[derive(Clone, Debug, PartialEq, Eq)]
38pub enum Glyph {
39 Empty,
41 Simple(SimpleGlyph),
42 Composite(CompositeGlyph),
43}
44
45#[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 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 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 ReadArgs for Glyph {
123 type Args = ();
124}
125
126impl<'a> FontRead<'a> for Glyph {
127 fn read_with_args(
128 data: read_fonts::FontData<'a>,
129 _: (),
130 ) -> Result<Self, read_fonts::ReadError> {
131 read_fonts::tables::glyf::Glyph::read(data).map(|g| Glyph::from_table_ref(&g))
132 }
133}
134
135impl From<SimpleGlyph> for Glyph {
136 fn from(value: SimpleGlyph) -> Self {
137 if value.contours.is_empty() {
138 Glyph::Empty
139 } else {
140 Glyph::Simple(value)
141 }
142 }
143}
144
145impl From<CompositeGlyph> for Glyph {
146 fn from(value: CompositeGlyph) -> Self {
147 Glyph::Composite(value)
148 }
149}
150
151impl Validate for Glyph {
152 fn validate_impl(&self, ctx: &mut ValidationCtx) {
153 match self {
154 Glyph::Empty => (),
155 Glyph::Simple(glyph) => glyph.validate_impl(ctx),
156 Glyph::Composite(glyph) => glyph.validate_impl(ctx),
157 }
158 }
159}
160
161impl FontWrite for Glyph {
162 fn write_into(&self, writer: &mut crate::TableWriter) {
163 match self {
164 Glyph::Empty => (),
165 Glyph::Simple(glyph) => glyph.write_into(writer),
166 Glyph::Composite(glyph) => glyph.write_into(writer),
167 }
168 }
169}
170
171impl Validate for Glyf {
172 fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
173}
174
175impl FontWrite for Glyf {
176 fn write_into(&self, writer: &mut TableWriter) {
177 writer.write_slice(&self.0)
178 }
179}
180
181#[cfg(test)]
182mod tests {
183 use super::*;
184
185 #[test]
186 fn union_box() {
187 assert_eq!(
188 Bbox {
189 x_min: -1,
190 y_min: -2,
191 x_max: 5,
192 y_max: 6
193 },
194 Bbox {
195 x_min: 0,
196 y_min: 0,
197 x_max: 5,
198 y_max: 6
199 }
200 .union(Bbox {
201 x_min: -1,
202 y_min: -2,
203 x_max: 3,
204 y_max: 4
205 })
206 )
207 }
208}