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::{BBox, GlyphId, NormalizedCoordinate, OutlineBuilder, Rect}; |
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 | fn parse_top_dict(data: &[u8]) -> Option<TopDictData> { |
86 | let mut dict_data: TopDictData = TopDictData::default(); |
87 | |
88 | let mut operands_buffer: [f64; 513] = [0.0; MAX_OPERANDS_LEN]; |
89 | let mut dict_parser: DictionaryParser<'_> = DictionaryParser::new(data, &mut operands_buffer); |
90 | while let Some(operator: Operator) = dict_parser.parse_next() { |
91 | if operator.get() == top_dict_operator::CHAR_STRINGS_OFFSET { |
92 | dict_data.char_strings_offset = dict_parser.parse_offset()?; |
93 | } else if operator.get() == top_dict_operator::FONT_DICT_INDEX_OFFSET { |
94 | dict_data.font_dict_index_offset = dict_parser.parse_offset(); |
95 | } else if operator.get() == top_dict_operator::VARIATION_STORE_OFFSET { |
96 | dict_data.variation_store_offset = dict_parser.parse_offset(); |
97 | } |
98 | } |
99 | |
100 | // Must be set, otherwise there are nothing to parse. |
101 | if dict_data.char_strings_offset == 0 { |
102 | return None; |
103 | } |
104 | |
105 | Some(dict_data) |
106 | } |
107 | |
108 | fn parse_font_dict(data: &[u8]) -> Option<Range<usize>> { |
109 | let mut private_dict_range: Option> = None; |
110 | |
111 | let mut operands_buffer: [f64; 513] = [0.0; MAX_OPERANDS_LEN]; |
112 | let mut dict_parser: DictionaryParser<'_> = DictionaryParser::new(data, &mut operands_buffer); |
113 | while let Some(operator: Operator) = dict_parser.parse_next() { |
114 | if operator.get() == font_dict_operator::PRIVATE_DICT_SIZE_AND_OFFSET { |
115 | dict_parser.parse_operands()?; |
116 | let operands: &[f64] = dict_parser.operands(); |
117 | |
118 | if operands.len() == 2 { |
119 | let len: usize = usize::try_from(operands[0] as i32).ok()?; |
120 | let start: usize = usize::try_from(operands[1] as i32).ok()?; |
121 | let end: usize = start.checked_add(len)?; |
122 | private_dict_range = Some(start..end); |
123 | } |
124 | |
125 | break; |
126 | } |
127 | } |
128 | |
129 | private_dict_range |
130 | } |
131 | |
132 | fn parse_private_dict(data: &[u8]) -> Option<usize> { |
133 | let mut subroutines_offset: Option = None; |
134 | let mut operands_buffer: [f64; 513] = [0.0; MAX_OPERANDS_LEN]; |
135 | let mut dict_parser: DictionaryParser<'_> = DictionaryParser::new(data, &mut operands_buffer); |
136 | while let Some(operator: Operator) = dict_parser.parse_next() { |
137 | if operator.get() == private_dict_operator::LOCAL_SUBROUTINES_OFFSET { |
138 | dict_parser.parse_operands()?; |
139 | let operands: &[f64] = dict_parser.operands(); |
140 | |
141 | if operands.len() == 1 { |
142 | subroutines_offset = usize::try_from(operands[0] as i32).ok(); |
143 | } |
144 | |
145 | break; |
146 | } |
147 | } |
148 | |
149 | subroutines_offset |
150 | } |
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 | fn default() -> Self { |
164 | Scalars { |
165 | d: [0.0; SCALARS_MAX as usize], |
166 | len: 0, |
167 | } |
168 | } |
169 | } |
170 | |
171 | impl Scalars { |
172 | pub fn len(&self) -> u8 { |
173 | self.len |
174 | } |
175 | |
176 | pub fn clear(&mut self) { |
177 | self.len = 0; |
178 | } |
179 | |
180 | pub fn at(&self, i: u8) -> f32 { |
181 | if i < self.len { |
182 | self.d[usize::from(i)] |
183 | } else { |
184 | 0.0 |
185 | } |
186 | } |
187 | |
188 | pub fn push(&mut self, n: f32) -> Option<()> { |
189 | if self.len < SCALARS_MAX { |
190 | self.d[usize::from(self.len)] = n; |
191 | self.len += 1; |
192 | Some(()) |
193 | } else { |
194 | None |
195 | } |
196 | } |
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 | fn update_scalars(&mut self, index: u16) -> Result<(), CFFError> { |
210 | self.scalars.clear(); |
211 | |
212 | let indices: LazyArray16<'_, u16> = self |
213 | .metadata |
214 | .item_variation_store |
215 | .region_indices(index) |
216 | .ok_or(err:CFFError::InvalidItemVariationDataIndex)?; |
217 | for index: u16 in indices { |
218 | let scalar: f32 = self |
219 | .metadata |
220 | .item_variation_store |
221 | .regions |
222 | .evaluate_region(index, self.coordinates); |
223 | self.scalars |
224 | .push(scalar) |
225 | .ok_or(err:CFFError::BlendRegionsLimitReached)?; |
226 | } |
227 | |
228 | Ok(()) |
229 | } |
230 | } |
231 | |
232 | fn parse_char_string( |
233 | data: &[u8], |
234 | metadata: &Table, |
235 | coordinates: &[NormalizedCoordinate], |
236 | builder: &mut dyn OutlineBuilder, |
237 | ) -> Result<Rect, CFFError> { |
238 | let mut ctx = CharStringParserContext { |
239 | metadata, |
240 | coordinates, |
241 | scalars: Scalars::default(), |
242 | had_vsindex: false, |
243 | had_blend: false, |
244 | stems_len: 0, |
245 | }; |
246 | |
247 | // Load scalars at default index. |
248 | ctx.update_scalars(0)?; |
249 | |
250 | let mut inner_builder = Builder { |
251 | builder, |
252 | bbox: BBox::new(), |
253 | }; |
254 | |
255 | let stack = ArgumentsStack { |
256 | data: &mut [0.0; MAX_ARGUMENTS_STACK_LEN], // 2052B |
257 | len: 0, |
258 | max_len: MAX_ARGUMENTS_STACK_LEN, |
259 | }; |
260 | let mut parser = CharStringParser { |
261 | stack, |
262 | builder: &mut inner_builder, |
263 | x: 0.0, |
264 | y: 0.0, |
265 | has_move_to: false, |
266 | is_first_move_to: true, |
267 | }; |
268 | _parse_char_string(&mut ctx, data, 0, &mut parser)?; |
269 | // let _ = _parse_char_string(&mut ctx, data, 0.0, 0.0, &mut stack, 0, &mut inner_builder)?; |
270 | |
271 | let bbox = parser.builder.bbox; |
272 | |
273 | // Check that bbox was changed. |
274 | if bbox.is_default() { |
275 | return Err(CFFError::ZeroBBox); |
276 | } |
277 | |
278 | bbox.to_rect().ok_or(CFFError::BboxOverflow) |
279 | } |
280 | |
281 | fn _parse_char_string( |
282 | ctx: &mut CharStringParserContext, |
283 | char_string: &[u8], |
284 | depth: u8, |
285 | p: &mut CharStringParser, |
286 | ) -> Result<(), CFFError> { |
287 | let mut s = Stream::new(char_string); |
288 | while !s.at_end() { |
289 | let op = s.read::<u8>().ok_or(CFFError::ReadOutOfBounds)?; |
290 | match op { |
291 | 0 | 2 | 9 | 11 | 13 | 14 | 17 => { |
292 | // Reserved. |
293 | return Err(CFFError::InvalidOperator); |
294 | } |
295 | operator::HORIZONTAL_STEM |
296 | | operator::VERTICAL_STEM |
297 | | operator::HORIZONTAL_STEM_HINT_MASK |
298 | | operator::VERTICAL_STEM_HINT_MASK => { |
299 | // y dy {dya dyb}* hstem |
300 | // x dx {dxa dxb}* vstem |
301 | // y dy {dya dyb}* hstemhm |
302 | // x dx {dxa dxb}* vstemhm |
303 | |
304 | ctx.stems_len += p.stack.len() as u32 >> 1; |
305 | |
306 | // We are ignoring the hint operators. |
307 | p.stack.clear(); |
308 | } |
309 | operator::VERTICAL_MOVE_TO => { |
310 | p.parse_vertical_move_to(0)?; |
311 | } |
312 | operator::LINE_TO => { |
313 | p.parse_line_to()?; |
314 | } |
315 | operator::HORIZONTAL_LINE_TO => { |
316 | p.parse_horizontal_line_to()?; |
317 | } |
318 | operator::VERTICAL_LINE_TO => { |
319 | p.parse_vertical_line_to()?; |
320 | } |
321 | operator::CURVE_TO => { |
322 | p.parse_curve_to()?; |
323 | } |
324 | operator::CALL_LOCAL_SUBROUTINE => { |
325 | if p.stack.is_empty() { |
326 | return Err(CFFError::InvalidArgumentsStackLength); |
327 | } |
328 | |
329 | if depth == STACK_LIMIT { |
330 | return Err(CFFError::NestingLimitReached); |
331 | } |
332 | |
333 | let subroutine_bias = calc_subroutine_bias(ctx.metadata.local_subrs.len()); |
334 | let index = conv_subroutine_index(p.stack.pop(), subroutine_bias)?; |
335 | let char_string = ctx |
336 | .metadata |
337 | .local_subrs |
338 | .get(index) |
339 | .ok_or(CFFError::InvalidSubroutineIndex)?; |
340 | _parse_char_string(ctx, char_string, depth + 1, p)?; |
341 | } |
342 | TWO_BYTE_OPERATOR_MARK => { |
343 | // flex |
344 | let op2 = s.read::<u8>().ok_or(CFFError::ReadOutOfBounds)?; |
345 | match op2 { |
346 | operator::HFLEX => p.parse_hflex()?, |
347 | operator::FLEX => p.parse_flex()?, |
348 | operator::HFLEX1 => p.parse_hflex1()?, |
349 | operator::FLEX1 => p.parse_flex1()?, |
350 | _ => return Err(CFFError::UnsupportedOperator), |
351 | } |
352 | } |
353 | operator::VS_INDEX => { |
354 | // |- ivs vsindex (15) |- |
355 | |
356 | // `vsindex` must precede the first `blend` operator, and may occur only once. |
357 | if ctx.had_blend || ctx.had_vsindex { |
358 | // TODO: maybe add a custom error |
359 | return Err(CFFError::InvalidOperator); |
360 | } |
361 | |
362 | if p.stack.len() != 1 { |
363 | return Err(CFFError::InvalidArgumentsStackLength); |
364 | } |
365 | |
366 | let index = u16::try_num_from(p.stack.pop()) |
367 | .ok_or(CFFError::InvalidItemVariationDataIndex)?; |
368 | ctx.update_scalars(index)?; |
369 | |
370 | ctx.had_vsindex = true; |
371 | |
372 | p.stack.clear(); |
373 | } |
374 | operator::BLEND => { |
375 | // num(0)..num(n-1), delta(0,0)..delta(k-1,0), |
376 | // delta(0,1)..delta(k-1,1) .. delta(0,n-1)..delta(k-1,n-1) |
377 | // n blend (16) val(0)..val(n-1) |
378 | |
379 | ctx.had_blend = true; |
380 | |
381 | let n = u16::try_num_from(p.stack.pop()) |
382 | .ok_or(CFFError::InvalidNumberOfBlendOperands)?; |
383 | let k = ctx.scalars.len(); |
384 | |
385 | let len = usize::from(n) * (usize::from(k) + 1); |
386 | if p.stack.len() < len { |
387 | return Err(CFFError::InvalidArgumentsStackLength); |
388 | } |
389 | |
390 | let start = p.stack.len() - len; |
391 | for i in (0..n).rev() { |
392 | for j in 0..k { |
393 | let delta = p.stack.pop(); |
394 | p.stack.data[start + usize::from(i)] += delta * ctx.scalars.at(k - j - 1); |
395 | } |
396 | } |
397 | } |
398 | operator::HINT_MASK | operator::COUNTER_MASK => { |
399 | ctx.stems_len += p.stack.len() as u32 >> 1; |
400 | s.advance(usize::num_from((ctx.stems_len + 7) >> 3)); |
401 | |
402 | // We are ignoring the hint operators. |
403 | p.stack.clear(); |
404 | } |
405 | operator::MOVE_TO => { |
406 | p.parse_move_to(0)?; |
407 | } |
408 | operator::HORIZONTAL_MOVE_TO => { |
409 | p.parse_horizontal_move_to(0)?; |
410 | } |
411 | operator::CURVE_LINE => { |
412 | p.parse_curve_line()?; |
413 | } |
414 | operator::LINE_CURVE => { |
415 | p.parse_line_curve()?; |
416 | } |
417 | operator::VV_CURVE_TO => { |
418 | p.parse_vv_curve_to()?; |
419 | } |
420 | operator::HH_CURVE_TO => { |
421 | p.parse_hh_curve_to()?; |
422 | } |
423 | operator::SHORT_INT => { |
424 | let n = s.read::<i16>().ok_or(CFFError::ReadOutOfBounds)?; |
425 | p.stack.push(f32::from(n))?; |
426 | } |
427 | operator::CALL_GLOBAL_SUBROUTINE => { |
428 | if p.stack.is_empty() { |
429 | return Err(CFFError::InvalidArgumentsStackLength); |
430 | } |
431 | |
432 | if depth == STACK_LIMIT { |
433 | return Err(CFFError::NestingLimitReached); |
434 | } |
435 | |
436 | let subroutine_bias = calc_subroutine_bias(ctx.metadata.global_subrs.len()); |
437 | let index = conv_subroutine_index(p.stack.pop(), subroutine_bias)?; |
438 | let char_string = ctx |
439 | .metadata |
440 | .global_subrs |
441 | .get(index) |
442 | .ok_or(CFFError::InvalidSubroutineIndex)?; |
443 | _parse_char_string(ctx, char_string, depth + 1, p)?; |
444 | } |
445 | operator::VH_CURVE_TO => { |
446 | p.parse_vh_curve_to()?; |
447 | } |
448 | operator::HV_CURVE_TO => { |
449 | p.parse_hv_curve_to()?; |
450 | } |
451 | 32..=246 => { |
452 | p.parse_int1(op)?; |
453 | } |
454 | 247..=250 => { |
455 | p.parse_int2(op, &mut s)?; |
456 | } |
457 | 251..=254 => { |
458 | p.parse_int3(op, &mut s)?; |
459 | } |
460 | operator::FIXED_16_16 => { |
461 | p.parse_fixed(&mut s)?; |
462 | } |
463 | } |
464 | } |
465 | |
466 | Ok(()) |
467 | } |
468 | |
469 | /// A [Compact Font Format 2 Table]( |
470 | /// https://docs.microsoft.com/en-us/typography/opentype/spec/cff2). |
471 | #[derive (Clone, Copy, Default)] |
472 | pub struct Table<'a> { |
473 | global_subrs: Index<'a>, |
474 | local_subrs: Index<'a>, |
475 | char_strings: Index<'a>, |
476 | item_variation_store: ItemVariationStore<'a>, |
477 | } |
478 | |
479 | impl<'a> Table<'a> { |
480 | /// Parses a table from raw data. |
481 | pub fn parse(data: &'a [u8]) -> Option<Self> { |
482 | let mut s = Stream::new(data); |
483 | |
484 | // Parse Header. |
485 | let major = s.read::<u8>()?; |
486 | s.skip::<u8>(); // minor |
487 | let header_size = s.read::<u8>()?; |
488 | let top_dict_length = s.read::<u16>()?; |
489 | |
490 | if major != 2 { |
491 | return None; |
492 | } |
493 | |
494 | // Jump to Top DICT. It's not necessarily right after the header. |
495 | if header_size > 5 { |
496 | s.advance(usize::from(header_size) - 5); |
497 | } |
498 | |
499 | let top_dict_data = s.read_bytes(usize::from(top_dict_length))?; |
500 | let top_dict = parse_top_dict(top_dict_data)?; |
501 | |
502 | let mut metadata = Self::default(); |
503 | |
504 | // Parse Global Subroutines INDEX. |
505 | metadata.global_subrs = parse_index::<u32>(&mut s)?; |
506 | |
507 | metadata.char_strings = { |
508 | let mut s = Stream::new_at(data, top_dict.char_strings_offset)?; |
509 | parse_index::<u32>(&mut s)? |
510 | }; |
511 | |
512 | if let Some(offset) = top_dict.variation_store_offset { |
513 | let mut s = Stream::new_at(data, offset)?; |
514 | s.skip::<u16>(); // length |
515 | metadata.item_variation_store = ItemVariationStore::parse(s)?; |
516 | } |
517 | |
518 | // TODO: simplify |
519 | if let Some(offset) = top_dict.font_dict_index_offset { |
520 | let mut s = Stream::new_at(data, offset)?; |
521 | 'outer: for font_dict_data in parse_index::<u32>(&mut s)? { |
522 | if let Some(private_dict_range) = parse_font_dict(font_dict_data) { |
523 | // 'Private DICT size and offset, from start of the CFF2 table.' |
524 | let private_dict_data = data.get(private_dict_range.clone())?; |
525 | if let Some(subroutines_offset) = parse_private_dict(private_dict_data) { |
526 | // 'The local subroutines offset is relative to the beginning |
527 | // of the Private DICT data.' |
528 | if let Some(start) = |
529 | private_dict_range.start.checked_add(subroutines_offset) |
530 | { |
531 | let data = data.get(start..data.len())?; |
532 | let mut s = Stream::new(data); |
533 | metadata.local_subrs = parse_index::<u32>(&mut s)?; |
534 | break 'outer; |
535 | } |
536 | } |
537 | } |
538 | } |
539 | } |
540 | |
541 | Some(metadata) |
542 | } |
543 | |
544 | /// Outlines a glyph. |
545 | pub fn outline( |
546 | &self, |
547 | coordinates: &[NormalizedCoordinate], |
548 | glyph_id: GlyphId, |
549 | builder: &mut dyn OutlineBuilder, |
550 | ) -> Result<Rect, CFFError> { |
551 | let data = self |
552 | .char_strings |
553 | .get(u32::from(glyph_id.0)) |
554 | .ok_or(CFFError::NoGlyph)?; |
555 | parse_char_string(data, self, coordinates, builder) |
556 | } |
557 | } |
558 | |
559 | impl core::fmt::Debug for Table<'_> { |
560 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
561 | write!(f, "Table {{ ... }}" ) |
562 | } |
563 | } |
564 | |