/src/ttf-parser/src/tables/cff/cff2.rs
Line | Count | Source |
1 | | //! A [Compact Font Format 2 Table]( |
2 | | //! https://docs.microsoft.com/en-us/typography/opentype/spec/cff2) implementation. |
3 | | |
4 | | // https://docs.microsoft.com/en-us/typography/opentype/spec/cff2charstr |
5 | | |
6 | | use core::convert::TryFrom; |
7 | | use core::ops::Range; |
8 | | |
9 | | use super::argstack::ArgumentsStack; |
10 | | use super::charstring::CharStringParser; |
11 | | use super::dict::DictionaryParser; |
12 | | use super::index::{parse_index, Index}; |
13 | | use super::{calc_subroutine_bias, conv_subroutine_index, Builder, CFFError}; |
14 | | use crate::parser::{NumFrom, Stream, TryNumFrom}; |
15 | | use crate::var_store::*; |
16 | | use crate::{GlyphId, NormalizedCoordinate, OutlineBuilder, Rect, RectF}; |
17 | | |
18 | | // https://docs.microsoft.com/en-us/typography/opentype/spec/cff2#7-top-dict-data |
19 | | // 'Operators in DICT may be preceded by up to a maximum of 513 operands.' |
20 | | const MAX_OPERANDS_LEN: usize = 513; |
21 | | |
22 | | // https://docs.microsoft.com/en-us/typography/opentype/spec/cff2charstr#appendix-b-cff2-charstring-implementation-limits |
23 | | const STACK_LIMIT: u8 = 10; |
24 | | const MAX_ARGUMENTS_STACK_LEN: usize = 513; |
25 | | |
26 | | const TWO_BYTE_OPERATOR_MARK: u8 = 12; |
27 | | |
28 | | // https://docs.microsoft.com/en-us/typography/opentype/spec/cff2charstr#4-charstring-operators |
29 | | mod operator { |
30 | | pub const HORIZONTAL_STEM: u8 = 1; |
31 | | pub const VERTICAL_STEM: u8 = 3; |
32 | | pub const VERTICAL_MOVE_TO: u8 = 4; |
33 | | pub const LINE_TO: u8 = 5; |
34 | | pub const HORIZONTAL_LINE_TO: u8 = 6; |
35 | | pub const VERTICAL_LINE_TO: u8 = 7; |
36 | | pub const CURVE_TO: u8 = 8; |
37 | | pub const CALL_LOCAL_SUBROUTINE: u8 = 10; |
38 | | pub const VS_INDEX: u8 = 15; |
39 | | pub const BLEND: u8 = 16; |
40 | | pub const HORIZONTAL_STEM_HINT_MASK: u8 = 18; |
41 | | pub const HINT_MASK: u8 = 19; |
42 | | pub const COUNTER_MASK: u8 = 20; |
43 | | pub const MOVE_TO: u8 = 21; |
44 | | pub const HORIZONTAL_MOVE_TO: u8 = 22; |
45 | | pub const VERTICAL_STEM_HINT_MASK: u8 = 23; |
46 | | pub const CURVE_LINE: u8 = 24; |
47 | | pub const LINE_CURVE: u8 = 25; |
48 | | pub const VV_CURVE_TO: u8 = 26; |
49 | | pub const HH_CURVE_TO: u8 = 27; |
50 | | pub const SHORT_INT: u8 = 28; |
51 | | pub const CALL_GLOBAL_SUBROUTINE: u8 = 29; |
52 | | pub const VH_CURVE_TO: u8 = 30; |
53 | | pub const HV_CURVE_TO: u8 = 31; |
54 | | pub const HFLEX: u8 = 34; |
55 | | pub const FLEX: u8 = 35; |
56 | | pub const HFLEX1: u8 = 36; |
57 | | pub const FLEX1: u8 = 37; |
58 | | pub const FIXED_16_16: u8 = 255; |
59 | | } |
60 | | |
61 | | // https://docs.microsoft.com/en-us/typography/opentype/spec/cff2#table-9-top-dict-operator-entries |
62 | | mod top_dict_operator { |
63 | | pub const CHAR_STRINGS_OFFSET: u16 = 17; |
64 | | pub const VARIATION_STORE_OFFSET: u16 = 24; |
65 | | pub const FONT_DICT_INDEX_OFFSET: u16 = 1236; |
66 | | } |
67 | | |
68 | | // https://docs.microsoft.com/en-us/typography/opentype/spec/cff2#table-10-font-dict-operator-entries |
69 | | mod font_dict_operator { |
70 | | pub const PRIVATE_DICT_SIZE_AND_OFFSET: u16 = 18; |
71 | | } |
72 | | |
73 | | // https://docs.microsoft.com/en-us/typography/opentype/spec/cff2#table-16-private-dict-operators |
74 | | mod private_dict_operator { |
75 | | pub const LOCAL_SUBROUTINES_OFFSET: u16 = 19; |
76 | | } |
77 | | |
78 | | #[derive(Clone, Copy, Default)] |
79 | | struct TopDictData { |
80 | | char_strings_offset: usize, |
81 | | font_dict_index_offset: Option<usize>, |
82 | | variation_store_offset: Option<usize>, |
83 | | } |
84 | | |
85 | 4.05k | fn parse_top_dict(data: &[u8]) -> Option<TopDictData> { |
86 | 4.05k | let mut dict_data = TopDictData::default(); |
87 | | |
88 | 4.05k | let mut operands_buffer = [0.0; MAX_OPERANDS_LEN]; |
89 | 4.05k | let mut dict_parser = DictionaryParser::new(data, &mut operands_buffer); |
90 | 1.44M | while let Some(operator) = dict_parser.parse_next() { |
91 | 1.44M | if operator.get() == top_dict_operator::CHAR_STRINGS_OFFSET { |
92 | 7.63k | dict_data.char_strings_offset = dict_parser.parse_offset()?; |
93 | 1.43M | } else if operator.get() == top_dict_operator::FONT_DICT_INDEX_OFFSET { |
94 | 15.9k | dict_data.font_dict_index_offset = dict_parser.parse_offset(); |
95 | 1.42M | } else if operator.get() == top_dict_operator::VARIATION_STORE_OFFSET { |
96 | 36.4k | dict_data.variation_store_offset = dict_parser.parse_offset(); |
97 | 1.38M | } |
98 | | } |
99 | | |
100 | | // Must be set, otherwise there are nothing to parse. |
101 | 3.81k | if dict_data.char_strings_offset == 0 { |
102 | 364 | return None; |
103 | 3.45k | } |
104 | | |
105 | 3.45k | Some(dict_data) |
106 | 4.05k | } |
107 | | |
108 | 19.6k | fn parse_font_dict(data: &[u8]) -> Option<Range<usize>> { |
109 | 19.6k | let mut private_dict_range = None; |
110 | | |
111 | 19.6k | let mut operands_buffer = [0.0; MAX_OPERANDS_LEN]; |
112 | 19.6k | let mut dict_parser = DictionaryParser::new(data, &mut operands_buffer); |
113 | 109k | while let Some(operator) = dict_parser.parse_next() { |
114 | 94.6k | if operator.get() == font_dict_operator::PRIVATE_DICT_SIZE_AND_OFFSET { |
115 | 4.44k | dict_parser.parse_operands()?; |
116 | 3.99k | let operands = dict_parser.operands(); |
117 | | |
118 | 3.99k | if operands.len() == 2 { |
119 | 2.44k | let len = usize::try_from(operands[0] as i32).ok()?; |
120 | 2.06k | let start = usize::try_from(operands[1] as i32).ok()?; |
121 | 1.86k | let end = start.checked_add(len)?; |
122 | 1.86k | private_dict_range = Some(start..end); |
123 | 1.55k | } |
124 | | |
125 | 3.41k | break; |
126 | 90.1k | } |
127 | | } |
128 | | |
129 | 18.6k | private_dict_range |
130 | 19.6k | } |
131 | | |
132 | 1.68k | fn parse_private_dict(data: &[u8]) -> Option<usize> { |
133 | 1.68k | let mut subroutines_offset = None; |
134 | 1.68k | let mut operands_buffer = [0.0; MAX_OPERANDS_LEN]; |
135 | 1.68k | let mut dict_parser = DictionaryParser::new(data, &mut operands_buffer); |
136 | 107k | while let Some(operator) = dict_parser.parse_next() { |
137 | 107k | if operator.get() == private_dict_operator::LOCAL_SUBROUTINES_OFFSET { |
138 | 1.22k | dict_parser.parse_operands()?; |
139 | 998 | let operands = dict_parser.operands(); |
140 | | |
141 | 998 | if operands.len() == 1 { |
142 | 609 | subroutines_offset = usize::try_from(operands[0] as i32).ok(); |
143 | 609 | } |
144 | | |
145 | 998 | break; |
146 | 105k | } |
147 | | } |
148 | | |
149 | 1.45k | subroutines_offset |
150 | 1.68k | } |
151 | | |
152 | | /// CFF2 allows up to 65535 scalars, but an average font will have 3-5. |
153 | | /// So 64 is more than enough. |
154 | | const SCALARS_MAX: u8 = 64; |
155 | | |
156 | | #[derive(Clone, Copy)] |
157 | | pub(crate) struct Scalars { |
158 | | d: [f32; SCALARS_MAX as usize], // 256B |
159 | | len: u8, |
160 | | } |
161 | | |
162 | | impl Default for Scalars { |
163 | 142k | fn default() -> Self { |
164 | 142k | Scalars { |
165 | 142k | d: [0.0; SCALARS_MAX as usize], |
166 | 142k | len: 0, |
167 | 142k | } |
168 | 142k | } |
169 | | } |
170 | | |
171 | | impl Scalars { |
172 | 176k | pub fn len(&self) -> u8 { |
173 | 176k | self.len |
174 | 176k | } |
175 | | |
176 | 143k | pub fn clear(&mut self) { |
177 | 143k | self.len = 0; |
178 | 143k | } |
179 | | |
180 | 1.13M | pub fn at(&self, i: u8) -> f32 { |
181 | 1.13M | if i < self.len { |
182 | 1.13M | self.d[usize::from(i)] |
183 | | } else { |
184 | 0 | 0.0 |
185 | | } |
186 | 1.13M | } |
187 | | |
188 | 774k | pub fn push(&mut self, n: f32) -> Option<()> { |
189 | 774k | if self.len < SCALARS_MAX { |
190 | 772k | self.d[usize::from(self.len)] = n; |
191 | 772k | self.len += 1; |
192 | 772k | Some(()) |
193 | | } else { |
194 | 1.67k | None |
195 | | } |
196 | 774k | } |
197 | | } |
198 | | |
199 | | struct CharStringParserContext<'a> { |
200 | | metadata: &'a Table<'a>, |
201 | | coordinates: &'a [NormalizedCoordinate], |
202 | | scalars: Scalars, |
203 | | had_vsindex: bool, |
204 | | had_blend: bool, |
205 | | stems_len: u32, |
206 | | } |
207 | | |
208 | | impl CharStringParserContext<'_> { |
209 | 143k | fn update_scalars(&mut self, index: u16) -> Result<(), CFFError> { |
210 | 143k | self.scalars.clear(); |
211 | | |
212 | 143k | let indices = self |
213 | 143k | .metadata |
214 | 143k | .item_variation_store |
215 | 143k | .region_indices(index) |
216 | 143k | .ok_or(CFFError::InvalidItemVariationDataIndex)?; |
217 | 890k | for index in indices { |
218 | 774k | let scalar = self |
219 | 774k | .metadata |
220 | 774k | .item_variation_store |
221 | 774k | .regions |
222 | 774k | .evaluate_region(index, self.coordinates); |
223 | 774k | self.scalars |
224 | 774k | .push(scalar) |
225 | 774k | .ok_or(CFFError::BlendRegionsLimitReached)?; |
226 | | } |
227 | | |
228 | 116k | Ok(()) |
229 | 143k | } |
230 | | } |
231 | | |
232 | 142k | fn parse_char_string( |
233 | 142k | data: &[u8], |
234 | 142k | metadata: &Table, |
235 | 142k | coordinates: &[NormalizedCoordinate], |
236 | 142k | builder: &mut dyn OutlineBuilder, |
237 | 142k | ) -> Result<Rect, CFFError> { |
238 | 142k | let mut ctx = CharStringParserContext { |
239 | 142k | metadata, |
240 | 142k | coordinates, |
241 | 142k | scalars: Scalars::default(), |
242 | 142k | had_vsindex: false, |
243 | 142k | had_blend: false, |
244 | 142k | stems_len: 0, |
245 | 142k | }; |
246 | | |
247 | | // Load scalars at default index. |
248 | 142k | ctx.update_scalars(0)?; |
249 | | |
250 | 115k | let mut inner_builder = Builder { |
251 | 115k | builder, |
252 | 115k | bbox: RectF::new(), |
253 | 115k | transform: None, |
254 | 115k | }; |
255 | | |
256 | 115k | let stack = ArgumentsStack { |
257 | 115k | data: &mut [0.0; MAX_ARGUMENTS_STACK_LEN], // 2052B |
258 | 115k | len: 0, |
259 | 115k | max_len: MAX_ARGUMENTS_STACK_LEN, |
260 | 115k | }; |
261 | 115k | let mut parser = CharStringParser { |
262 | 115k | stack, |
263 | 115k | builder: &mut inner_builder, |
264 | 115k | x: 0.0, |
265 | 115k | y: 0.0, |
266 | 115k | has_move_to: false, |
267 | 115k | is_first_move_to: true, |
268 | 115k | width_only: false, |
269 | 115k | }; |
270 | 115k | _parse_char_string(&mut ctx, data, 0, &mut parser)?; |
271 | | // let _ = _parse_char_string(&mut ctx, data, 0.0, 0.0, &mut stack, 0, &mut inner_builder)?; |
272 | | |
273 | 46.3k | let bbox = parser.builder.bbox; |
274 | | |
275 | | // Check that bbox was changed. |
276 | 46.3k | if bbox.is_default() { |
277 | 37.1k | return Err(CFFError::ZeroBBox); |
278 | 9.17k | } |
279 | | |
280 | 9.17k | bbox.to_rect().ok_or(CFFError::BboxOverflow) |
281 | 142k | } |
282 | | |
283 | 289k | fn _parse_char_string( |
284 | 289k | ctx: &mut CharStringParserContext, |
285 | 289k | char_string: &[u8], |
286 | 289k | depth: u8, |
287 | 289k | p: &mut CharStringParser, |
288 | 289k | ) -> Result<(), CFFError> { |
289 | 289k | let mut s = Stream::new(char_string); |
290 | 4.25M | while !s.at_end() { |
291 | 4.07M | let op = s.read::<u8>().ok_or(CFFError::ReadOutOfBounds)?; |
292 | 4.07M | match op { |
293 | | 0 | 2 | 9 | 11 | 13 | 14 | 17 => { |
294 | | // Reserved. |
295 | 27.5k | return Err(CFFError::InvalidOperator); |
296 | | } |
297 | | operator::HORIZONTAL_STEM |
298 | | | operator::VERTICAL_STEM |
299 | | | operator::HORIZONTAL_STEM_HINT_MASK |
300 | 39.7k | | operator::VERTICAL_STEM_HINT_MASK => { |
301 | 39.7k | // y dy {dya dyb}* hstem |
302 | 39.7k | // x dx {dxa dxb}* vstem |
303 | 39.7k | // y dy {dya dyb}* hstemhm |
304 | 39.7k | // x dx {dxa dxb}* vstemhm |
305 | 39.7k | |
306 | 39.7k | ctx.stems_len += p.stack.len() as u32 >> 1; |
307 | 39.7k | |
308 | 39.7k | // We are ignoring the hint operators. |
309 | 39.7k | p.stack.clear(); |
310 | 39.7k | } |
311 | | operator::VERTICAL_MOVE_TO => { |
312 | 8.87k | p.parse_vertical_move_to(0)?; |
313 | | } |
314 | | operator::LINE_TO => { |
315 | 45.3k | p.parse_line_to()?; |
316 | | } |
317 | | operator::HORIZONTAL_LINE_TO => { |
318 | 33.3k | p.parse_horizontal_line_to()?; |
319 | | } |
320 | | operator::VERTICAL_LINE_TO => { |
321 | 17.3k | p.parse_vertical_line_to()?; |
322 | | } |
323 | | operator::CURVE_TO => { |
324 | 3.66k | p.parse_curve_to()?; |
325 | | } |
326 | | operator::CALL_LOCAL_SUBROUTINE => { |
327 | 167k | if p.stack.is_empty() { |
328 | 1.18k | return Err(CFFError::InvalidArgumentsStackLength); |
329 | 166k | } |
330 | | |
331 | 166k | if depth == STACK_LIMIT { |
332 | 17 | return Err(CFFError::NestingLimitReached); |
333 | 166k | } |
334 | | |
335 | 166k | let subroutine_bias = calc_subroutine_bias(ctx.metadata.local_subrs.len()); |
336 | 166k | let index = conv_subroutine_index(p.stack.pop(), subroutine_bias)?; |
337 | 164k | let char_string = ctx |
338 | 164k | .metadata |
339 | 164k | .local_subrs |
340 | 164k | .get(index) |
341 | 164k | .ok_or(CFFError::InvalidSubroutineIndex)?; |
342 | 161k | _parse_char_string(ctx, char_string, depth + 1, p)?; |
343 | | } |
344 | | TWO_BYTE_OPERATOR_MARK => { |
345 | | // flex |
346 | 3.08k | let op2 = s.read::<u8>().ok_or(CFFError::ReadOutOfBounds)?; |
347 | 2.80k | match op2 { |
348 | 364 | operator::HFLEX => p.parse_hflex()?, |
349 | 446 | operator::FLEX => p.parse_flex()?, |
350 | 732 | operator::HFLEX1 => p.parse_hflex1()?, |
351 | 785 | operator::FLEX1 => p.parse_flex1()?, |
352 | 473 | _ => return Err(CFFError::UnsupportedOperator), |
353 | | } |
354 | | } |
355 | | operator::VS_INDEX => { |
356 | | // |- ivs vsindex (15) |- |
357 | | |
358 | | // `vsindex` must precede the first `blend` operator, and may occur only once. |
359 | 3.17k | if ctx.had_blend || ctx.had_vsindex { |
360 | | // TODO: maybe add a custom error |
361 | 283 | return Err(CFFError::InvalidOperator); |
362 | 2.88k | } |
363 | | |
364 | 2.88k | if p.stack.len() != 1 { |
365 | 1.10k | return Err(CFFError::InvalidArgumentsStackLength); |
366 | 1.78k | } |
367 | | |
368 | 1.78k | let index = u16::try_num_from(p.stack.pop()) |
369 | 1.78k | .ok_or(CFFError::InvalidItemVariationDataIndex)?; |
370 | 1.32k | ctx.update_scalars(index)?; |
371 | | |
372 | 949 | ctx.had_vsindex = true; |
373 | | |
374 | 949 | p.stack.clear(); |
375 | | } |
376 | | operator::BLEND => { |
377 | | // num(0)..num(n-1), delta(0,0)..delta(k-1,0), |
378 | | // delta(0,1)..delta(k-1,1) .. delta(0,n-1)..delta(k-1,n-1) |
379 | | // n blend (16) val(0)..val(n-1) |
380 | | |
381 | 177k | ctx.had_blend = true; |
382 | | |
383 | 177k | let n = u16::try_num_from(p.stack.pop()) |
384 | 177k | .ok_or(CFFError::InvalidNumberOfBlendOperands)?; |
385 | 176k | let k = ctx.scalars.len(); |
386 | | |
387 | 176k | let len = usize::from(n) * (usize::from(k) + 1); |
388 | 176k | if p.stack.len() < len { |
389 | 2.60k | return Err(CFFError::InvalidArgumentsStackLength); |
390 | 174k | } |
391 | | |
392 | 174k | let start = p.stack.len() - len; |
393 | 575k | for i in (0..n).rev() { |
394 | 1.13M | for j in 0..k { |
395 | 1.13M | let delta = p.stack.pop(); |
396 | 1.13M | p.stack.data[start + usize::from(i)] += delta * ctx.scalars.at(k - j - 1); |
397 | 1.13M | } |
398 | | } |
399 | | } |
400 | 4.55k | operator::HINT_MASK | operator::COUNTER_MASK => { |
401 | 4.55k | ctx.stems_len += p.stack.len() as u32 >> 1; |
402 | 4.55k | s.advance(usize::num_from((ctx.stems_len + 7) >> 3)); |
403 | 4.55k | |
404 | 4.55k | // We are ignoring the hint operators. |
405 | 4.55k | p.stack.clear(); |
406 | 4.55k | } |
407 | | operator::MOVE_TO => { |
408 | 24.9k | p.parse_move_to(0)?; |
409 | | } |
410 | | operator::HORIZONTAL_MOVE_TO => { |
411 | 11.6k | p.parse_horizontal_move_to(0)?; |
412 | | } |
413 | | operator::CURVE_LINE => { |
414 | 2.95k | p.parse_curve_line()?; |
415 | | } |
416 | | operator::LINE_CURVE => { |
417 | 2.62k | p.parse_line_curve()?; |
418 | | } |
419 | | operator::VV_CURVE_TO => { |
420 | 10.7k | p.parse_vv_curve_to()?; |
421 | | } |
422 | | operator::HH_CURVE_TO => { |
423 | 11.5k | p.parse_hh_curve_to()?; |
424 | | } |
425 | | operator::SHORT_INT => { |
426 | 24.1k | let n = s.read::<i16>().ok_or(CFFError::ReadOutOfBounds)?; |
427 | 23.7k | p.stack.push(f32::from(n))?; |
428 | | } |
429 | | operator::CALL_GLOBAL_SUBROUTINE => { |
430 | 14.9k | if p.stack.is_empty() { |
431 | 639 | return Err(CFFError::InvalidArgumentsStackLength); |
432 | 14.3k | } |
433 | | |
434 | 14.3k | if depth == STACK_LIMIT { |
435 | 44 | return Err(CFFError::NestingLimitReached); |
436 | 14.3k | } |
437 | | |
438 | 14.3k | let subroutine_bias = calc_subroutine_bias(ctx.metadata.global_subrs.len()); |
439 | 14.3k | let index = conv_subroutine_index(p.stack.pop(), subroutine_bias)?; |
440 | 13.9k | let char_string = ctx |
441 | 13.9k | .metadata |
442 | 13.9k | .global_subrs |
443 | 13.9k | .get(index) |
444 | 13.9k | .ok_or(CFFError::InvalidSubroutineIndex)?; |
445 | 13.2k | _parse_char_string(ctx, char_string, depth + 1, p)?; |
446 | | } |
447 | | operator::VH_CURVE_TO => { |
448 | 12.0k | p.parse_vh_curve_to()?; |
449 | | } |
450 | | operator::HV_CURVE_TO => { |
451 | 17.6k | p.parse_hv_curve_to()?; |
452 | | } |
453 | 3.29M | 32..=246 => { |
454 | 3.00M | p.parse_int1(op)?; |
455 | | } |
456 | 291k | 247..=250 => { |
457 | 127k | p.parse_int2(op, &mut s)?; |
458 | | } |
459 | 163k | 251..=254 => { |
460 | 163k | p.parse_int3(op, &mut s)?; |
461 | | } |
462 | | operator::FIXED_16_16 => { |
463 | 120k | p.parse_fixed(&mut s)?; |
464 | | } |
465 | | } |
466 | | } |
467 | | |
468 | 180k | Ok(()) |
469 | 289k | } |
470 | | |
471 | | /// A [Compact Font Format 2 Table]( |
472 | | /// https://docs.microsoft.com/en-us/typography/opentype/spec/cff2). |
473 | | #[derive(Clone, Copy, Default)] |
474 | | pub struct Table<'a> { |
475 | | global_subrs: Index<'a>, |
476 | | local_subrs: Index<'a>, |
477 | | char_strings: Index<'a>, |
478 | | item_variation_store: ItemVariationStore<'a>, |
479 | | } |
480 | | |
481 | | impl<'a> Table<'a> { |
482 | | /// Parses a table from raw data. |
483 | 4.38k | pub fn parse(data: &'a [u8]) -> Option<Self> { |
484 | 4.38k | let mut s = Stream::new(data); |
485 | | |
486 | | // Parse Header. |
487 | 4.38k | let major = s.read::<u8>()?; |
488 | 4.37k | s.skip::<u8>(); // minor |
489 | 4.37k | let header_size = s.read::<u8>()?; |
490 | 4.36k | let top_dict_length = s.read::<u16>()?; |
491 | | |
492 | 4.36k | if major != 2 { |
493 | 224 | return None; |
494 | 4.14k | } |
495 | | |
496 | | // Jump to Top DICT. It's not necessarily right after the header. |
497 | 4.14k | if header_size > 5 { |
498 | 1.24k | s.advance(usize::from(header_size) - 5); |
499 | 2.89k | } |
500 | | |
501 | 4.14k | let top_dict_data = s.read_bytes(usize::from(top_dict_length))?; |
502 | 4.05k | let top_dict = parse_top_dict(top_dict_data)?; |
503 | | |
504 | 3.45k | let mut metadata = Self::default(); |
505 | | |
506 | | // Parse Global Subroutines INDEX. |
507 | 3.45k | metadata.global_subrs = parse_index::<u32>(&mut s)?; |
508 | | |
509 | | metadata.char_strings = { |
510 | 3.04k | let mut s = Stream::new_at(data, top_dict.char_strings_offset)?; |
511 | 2.91k | parse_index::<u32>(&mut s)? |
512 | | }; |
513 | | |
514 | 2.80k | if let Some(offset) = top_dict.variation_store_offset { |
515 | 1.77k | let mut s = Stream::new_at(data, offset)?; |
516 | 1.66k | s.skip::<u16>(); // length |
517 | 1.66k | metadata.item_variation_store = ItemVariationStore::parse(s)?; |
518 | 1.03k | } |
519 | | |
520 | | // TODO: simplify |
521 | 2.52k | if let Some(offset) = top_dict.font_dict_index_offset { |
522 | 1.44k | let mut s = Stream::new_at(data, offset)?; |
523 | 19.6k | 'outer: for font_dict_data in parse_index::<u32>(&mut s)? { |
524 | 19.6k | if let Some(private_dict_range) = parse_font_dict(font_dict_data) { |
525 | | // 'Private DICT size and offset, from start of the CFF2 table.' |
526 | 1.86k | let private_dict_data = data.get(private_dict_range.clone())?; |
527 | 1.68k | if let Some(subroutines_offset) = parse_private_dict(private_dict_data) { |
528 | | // 'The local subroutines offset is relative to the beginning |
529 | | // of the Private DICT data.' |
530 | 284 | if let Some(start) = |
531 | 284 | private_dict_range.start.checked_add(subroutines_offset) |
532 | | { |
533 | 284 | let data = data.get(start..data.len())?; |
534 | 149 | let mut s = Stream::new(data); |
535 | 149 | metadata.local_subrs = parse_index::<u32>(&mut s)?; |
536 | 119 | break 'outer; |
537 | 0 | } |
538 | 1.39k | } |
539 | 17.7k | } |
540 | | } |
541 | 1.08k | } |
542 | | |
543 | 2.10k | Some(metadata) |
544 | 4.38k | } |
545 | | |
546 | | /// Outlines a glyph. |
547 | 24.1M | pub fn outline( |
548 | 24.1M | &self, |
549 | 24.1M | coordinates: &[NormalizedCoordinate], |
550 | 24.1M | glyph_id: GlyphId, |
551 | 24.1M | builder: &mut dyn OutlineBuilder, |
552 | 24.1M | ) -> Result<Rect, CFFError> { |
553 | 24.1M | let data = self |
554 | 24.1M | .char_strings |
555 | 24.1M | .get(u32::from(glyph_id.0)) |
556 | 24.1M | .ok_or(CFFError::NoGlyph)?; |
557 | 142k | parse_char_string(data, self, coordinates, builder) |
558 | 24.1M | } |
559 | | } |
560 | | |
561 | | impl core::fmt::Debug for Table<'_> { |
562 | 0 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
563 | 0 | write!(f, "Table {{ ... }}") |
564 | 0 | } |
565 | | } |