ttf_parser/tables/cff/
cff2.rs

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