1use ab_glyph::Font;
4
5mod config;
6mod raster;
7mod session;
8#[cfg(test)]
9mod session_tests;
10#[cfg(test)]
11mod tests;
12pub use config::{
13 FontBudget, FontBudgetResource, FontError, TextDirection, TextLayoutBudget, TextStyle,
14};
15use config::{check, reserve};
16pub use raster::RasterizedGlyph;
17pub use session::{ShapedLineError, TextShapingSession};
18
19#[derive(Debug, Clone)]
27pub struct FontFace {
28 font: std::sync::Arc<ab_glyph::FontVec>,
29 source_capacity: usize,
30 glyph_count: usize,
31}
32
33impl FontFace {
34 pub fn from_bytes(bytes: Vec<u8>, budget: FontBudget) -> Result<Self, FontError> {
37 check(
38 FontBudgetResource::FontBytes,
39 bytes.capacity(),
40 budget.max_font_bytes(),
41 )?;
42 let face =
43 rustybuzz::ttf_parser::Face::parse(&bytes, 0).map_err(|_| FontError::InvalidFont)?;
44 let tables = face.tables();
45 if tables.glyf.is_none() && tables.cff.is_none() && tables.cff2.is_none() {
46 return Err(FontError::InvalidFont);
47 }
48 let glyph_count = usize::from(face.number_of_glyphs());
49 check(
50 FontBudgetResource::FontGlyphs,
51 glyph_count,
52 budget.max_font_glyphs(),
53 )?;
54 if face.units_per_em() == 0 || face.ascender() <= face.descender() {
55 return Err(FontError::InvalidFont);
56 }
57 let source_capacity = bytes.capacity();
58 let font = ab_glyph::FontVec::try_from_vec(bytes).map_err(|_| FontError::InvalidFont)?;
59 Ok(Self {
60 font: std::sync::Arc::new(font),
61 source_capacity,
62 glyph_count,
63 })
64 }
65
66 pub fn font_data(&self) -> &[u8] {
68 self.font.as_slice()
69 }
70 pub const fn allocation_bytes(&self) -> usize {
73 self.source_capacity
74 }
75 pub const fn glyph_count(&self) -> usize {
77 self.glyph_count
78 }
79
80 pub fn shape_line(
88 &self,
89 text: &str,
90 style: &TextStyle,
91 budget: &TextLayoutBudget,
92 ) -> Result<ShapedLine, FontError> {
93 TextShapingSession::new(self, *style, *budget)?.shape_line(text)
94 }
95
96 pub fn rasterize_glyph(
102 &self,
103 glyph_id: u16,
104 style: &TextStyle,
105 budget: &TextLayoutBudget,
106 ) -> Result<RasterizedGlyph, FontError> {
107 raster::rasterize(self, glyph_id, style, budget)
108 }
109}
110
111#[derive(Debug, Clone, Copy, PartialEq)]
113pub struct ShapedGlyph {
114 glyph_id: u16,
115 logical_x: f32,
116 logical_y: f32,
117 cluster: usize,
118}
119
120impl ShapedGlyph {
121 pub const fn glyph_id(self) -> u16 {
123 self.glyph_id
124 }
125 pub const fn logical_x(self) -> f32 {
127 self.logical_x
128 }
129 pub const fn logical_y(self) -> f32 {
131 self.logical_y
132 }
133 pub const fn cluster(self) -> usize {
135 self.cluster
136 }
137}
138
139#[derive(Debug)]
141pub struct ShapedLine {
142 font: FontFace,
143 style: TextStyle,
144 text: String,
145 glyphs: Vec<ShapedGlyph>,
146 advance: f32,
147 ascent: f32,
148 descent: f32,
149 line_height: f32,
150}
151
152impl ShapedLine {
153 pub fn text(&self) -> &str {
155 &self.text
156 }
157 pub const fn font(&self) -> &FontFace {
159 &self.font
160 }
161 pub const fn style(&self) -> &TextStyle {
163 &self.style
164 }
165 pub fn validate_for(
169 &self,
170 font: &FontFace,
171 style: &TextStyle,
172 budget: &TextLayoutBudget,
173 ) -> Result<(), ShapedLineError> {
174 self.validate_provenance(font, style)?;
175 check(
176 FontBudgetResource::TextBytes,
177 self.text.len(),
178 budget.max_text_bytes(),
179 )?;
180 check(
181 FontBudgetResource::ShapedGlyphs,
182 self.glyphs.len(),
183 budget.max_shaped_glyphs(),
184 )?;
185 Ok(())
186 }
187
188 pub(crate) fn validate_provenance(
189 &self,
190 font: &FontFace,
191 style: &TextStyle,
192 ) -> Result<(), ShapedLineError> {
193 if !std::sync::Arc::ptr_eq(&self.font.font, &font.font) {
194 return Err(ShapedLineError::FontMismatch);
195 }
196 if self.style != *style {
197 return Err(ShapedLineError::StyleMismatch);
198 }
199 Ok(())
200 }
201 pub fn glyphs(&self) -> &[ShapedGlyph] {
203 &self.glyphs
204 }
205 pub const fn advance(&self) -> f32 {
207 self.advance
208 }
209 pub const fn ascent(&self) -> f32 {
211 self.ascent
212 }
213 pub const fn descent(&self) -> f32 {
215 self.descent
216 }
217 pub const fn line_height(&self) -> f32 {
219 self.line_height
220 }
221 pub fn allocation_bytes(&self) -> usize {
223 self.glyphs
224 .capacity()
225 .saturating_mul(std::mem::size_of::<ShapedGlyph>())
226 .saturating_add(self.text.capacity())
227 }
228}
229
230fn represent(value: f64) -> Result<f32, FontError> {
231 let result = value as f32;
232 if result.is_finite() {
233 Ok(result)
234 } else {
235 Err(FontError::InvalidGeometry)
236 }
237}