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 super::argstack::ArgumentsStack;
10use super::charstring::CharStringParser;
11use super::dict::DictionaryParser;
12use super::index::{parse_index, Index};
13use super::{calc_subroutine_bias, conv_subroutine_index, Builder, CFFError};
14use crate::parser::{NumFrom, Stream, TryNumFrom};
15use crate::var_store::*;
16use 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.'
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.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.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] as i32).ok()?;
120                let start = usize::try_from(operands[1] as i32).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.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] 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.
154const SCALARS_MAX: u8 = 64;
155
156#[derive(Clone, Copy)]
157pub(crate) struct Scalars {
158    d: [f32; SCALARS_MAX as usize], // 256B
159    len: u8,
160}
161
162impl Default for Scalars {
163    fn default() -> Self {
164        Scalars {
165            d: [0.0; SCALARS_MAX as usize],
166            len: 0,
167        }
168    }
169}
170
171impl 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
199struct 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
208impl CharStringParserContext<'_> {
209    fn update_scalars(&mut self, index: u16) -> Result<(), CFFError> {
210        self.scalars.clear();
211
212        let indices = self
213            .metadata
214            .item_variation_store
215            .region_indices(index)
216            .ok_or(CFFError::InvalidItemVariationDataIndex)?;
217        for index in indices {
218            let scalar = self
219                .metadata
220                .item_variation_store
221                .regions
222                .evaluate_region(index, self.coordinates);
223            self.scalars
224                .push(scalar)
225                .ok_or(CFFError::BlendRegionsLimitReached)?;
226        }
227
228        Ok(())
229    }
230}
231
232fn 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: RectF::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        width_only: false,
268    };
269    _parse_char_string(&mut ctx, data, 0, &mut parser)?;
270    // let _ = _parse_char_string(&mut ctx, data, 0.0, 0.0, &mut stack, 0, &mut inner_builder)?;
271
272    let bbox = parser.builder.bbox;
273
274    // Check that bbox was changed.
275    if bbox.is_default() {
276        return Err(CFFError::ZeroBBox);
277    }
278
279    bbox.to_rect().ok_or(CFFError::BboxOverflow)
280}
281
282fn _parse_char_string(
283    ctx: &mut CharStringParserContext,
284    char_string: &[u8],
285    depth: u8,
286    p: &mut CharStringParser,
287) -> Result<(), CFFError> {
288    let mut s = Stream::new(char_string);
289    while !s.at_end() {
290        let op = s.read::<u8>().ok_or(CFFError::ReadOutOfBounds)?;
291        match op {
292            0 | 2 | 9 | 11 | 13 | 14 | 17 => {
293                // Reserved.
294                return Err(CFFError::InvalidOperator);
295            }
296            operator::HORIZONTAL_STEM
297            | operator::VERTICAL_STEM
298            | operator::HORIZONTAL_STEM_HINT_MASK
299            | operator::VERTICAL_STEM_HINT_MASK => {
300                // y dy {dya dyb}* hstem
301                // x dx {dxa dxb}* vstem
302                // y dy {dya dyb}* hstemhm
303                // x dx {dxa dxb}* vstemhm
304
305                ctx.stems_len += p.stack.len() as u32 >> 1;
306
307                // We are ignoring the hint operators.
308                p.stack.clear();
309            }
310            operator::VERTICAL_MOVE_TO => {
311                p.parse_vertical_move_to(0)?;
312            }
313            operator::LINE_TO => {
314                p.parse_line_to()?;
315            }
316            operator::HORIZONTAL_LINE_TO => {
317                p.parse_horizontal_line_to()?;
318            }
319            operator::VERTICAL_LINE_TO => {
320                p.parse_vertical_line_to()?;
321            }
322            operator::CURVE_TO => {
323                p.parse_curve_to()?;
324            }
325            operator::CALL_LOCAL_SUBROUTINE => {
326                if p.stack.is_empty() {
327                    return Err(CFFError::InvalidArgumentsStackLength);
328                }
329
330                if depth == STACK_LIMIT {
331                    return Err(CFFError::NestingLimitReached);
332                }
333
334                let subroutine_bias = calc_subroutine_bias(ctx.metadata.local_subrs.len());
335                let index = conv_subroutine_index(p.stack.pop(), subroutine_bias)?;
336                let char_string = ctx
337                    .metadata
338                    .local_subrs
339                    .get(index)
340                    .ok_or(CFFError::InvalidSubroutineIndex)?;
341                _parse_char_string(ctx, char_string, depth + 1, p)?;
342            }
343            TWO_BYTE_OPERATOR_MARK => {
344                // flex
345                let op2 = s.read::<u8>().ok_or(CFFError::ReadOutOfBounds)?;
346                match op2 {
347                    operator::HFLEX => p.parse_hflex()?,
348                    operator::FLEX => p.parse_flex()?,
349                    operator::HFLEX1 => p.parse_hflex1()?,
350                    operator::FLEX1 => p.parse_flex1()?,
351                    _ => return Err(CFFError::UnsupportedOperator),
352                }
353            }
354            operator::VS_INDEX => {
355                // |- ivs vsindex (15) |-
356
357                // `vsindex` must precede the first `blend` operator, and may occur only once.
358                if ctx.had_blend || ctx.had_vsindex {
359                    // TODO: maybe add a custom error
360                    return Err(CFFError::InvalidOperator);
361                }
362
363                if p.stack.len() != 1 {
364                    return Err(CFFError::InvalidArgumentsStackLength);
365                }
366
367                let index = u16::try_num_from(p.stack.pop())
368                    .ok_or(CFFError::InvalidItemVariationDataIndex)?;
369                ctx.update_scalars(index)?;
370
371                ctx.had_vsindex = true;
372
373                p.stack.clear();
374            }
375            operator::BLEND => {
376                // num(0)..num(n-1), delta(0,0)..delta(k-1,0),
377                // delta(0,1)..delta(k-1,1) .. delta(0,n-1)..delta(k-1,n-1)
378                // n blend (16) val(0)..val(n-1)
379
380                ctx.had_blend = true;
381
382                let n = u16::try_num_from(p.stack.pop())
383                    .ok_or(CFFError::InvalidNumberOfBlendOperands)?;
384                let k = ctx.scalars.len();
385
386                let len = usize::from(n) * (usize::from(k) + 1);
387                if p.stack.len() < len {
388                    return Err(CFFError::InvalidArgumentsStackLength);
389                }
390
391                let start = p.stack.len() - len;
392                for i in (0..n).rev() {
393                    for j in 0..k {
394                        let delta = p.stack.pop();
395                        p.stack.data[start + usize::from(i)] += delta * ctx.scalars.at(k - j - 1);
396                    }
397                }
398            }
399            operator::HINT_MASK | operator::COUNTER_MASK => {
400                ctx.stems_len += p.stack.len() as u32 >> 1;
401                s.advance(usize::num_from((ctx.stems_len + 7) >> 3));
402
403                // We are ignoring the hint operators.
404                p.stack.clear();
405            }
406            operator::MOVE_TO => {
407                p.parse_move_to(0)?;
408            }
409            operator::HORIZONTAL_MOVE_TO => {
410                p.parse_horizontal_move_to(0)?;
411            }
412            operator::CURVE_LINE => {
413                p.parse_curve_line()?;
414            }
415            operator::LINE_CURVE => {
416                p.parse_line_curve()?;
417            }
418            operator::VV_CURVE_TO => {
419                p.parse_vv_curve_to()?;
420            }
421            operator::HH_CURVE_TO => {
422                p.parse_hh_curve_to()?;
423            }
424            operator::SHORT_INT => {
425                let n = s.read::<i16>().ok_or(CFFError::ReadOutOfBounds)?;
426                p.stack.push(f32::from(n))?;
427            }
428            operator::CALL_GLOBAL_SUBROUTINE => {
429                if p.stack.is_empty() {
430                    return Err(CFFError::InvalidArgumentsStackLength);
431                }
432
433                if depth == STACK_LIMIT {
434                    return Err(CFFError::NestingLimitReached);
435                }
436
437                let subroutine_bias = calc_subroutine_bias(ctx.metadata.global_subrs.len());
438                let index = conv_subroutine_index(p.stack.pop(), subroutine_bias)?;
439                let char_string = ctx
440                    .metadata
441                    .global_subrs
442                    .get(index)
443                    .ok_or(CFFError::InvalidSubroutineIndex)?;
444                _parse_char_string(ctx, char_string, depth + 1, p)?;
445            }
446            operator::VH_CURVE_TO => {
447                p.parse_vh_curve_to()?;
448            }
449            operator::HV_CURVE_TO => {
450                p.parse_hv_curve_to()?;
451            }
452            32..=246 => {
453                p.parse_int1(op)?;
454            }
455            247..=250 => {
456                p.parse_int2(op, &mut s)?;
457            }
458            251..=254 => {
459                p.parse_int3(op, &mut s)?;
460            }
461            operator::FIXED_16_16 => {
462                p.parse_fixed(&mut s)?;
463            }
464        }
465    }
466
467    Ok(())
468}
469
470/// A [Compact Font Format 2 Table](
471/// https://docs.microsoft.com/en-us/typography/opentype/spec/cff2).
472#[derive(Clone, Copy, Default)]
473pub struct Table<'a> {
474    global_subrs: Index<'a>,
475    local_subrs: Index<'a>,
476    char_strings: Index<'a>,
477    item_variation_store: ItemVariationStore<'a>,
478}
479
480impl<'a> Table<'a> {
481    /// Parses a table from raw data.
482    pub fn parse(data: &'a [u8]) -> Option<Self> {
483        let mut s = Stream::new(data);
484
485        // Parse Header.
486        let major = s.read::<u8>()?;
487        s.skip::<u8>(); // minor
488        let header_size = s.read::<u8>()?;
489        let top_dict_length = s.read::<u16>()?;
490
491        if major != 2 {
492            return None;
493        }
494
495        // Jump to Top DICT. It's not necessarily right after the header.
496        if header_size > 5 {
497            s.advance(usize::from(header_size) - 5);
498        }
499
500        let top_dict_data = s.read_bytes(usize::from(top_dict_length))?;
501        let top_dict = parse_top_dict(top_dict_data)?;
502
503        let mut metadata = Self::default();
504
505        // Parse Global Subroutines INDEX.
506        metadata.global_subrs = parse_index::<u32>(&mut s)?;
507
508        metadata.char_strings = {
509            let mut s = Stream::new_at(data, top_dict.char_strings_offset)?;
510            parse_index::<u32>(&mut s)?
511        };
512
513        if let Some(offset) = top_dict.variation_store_offset {
514            let mut s = Stream::new_at(data, offset)?;
515            s.skip::<u16>(); // length
516            metadata.item_variation_store = ItemVariationStore::parse(s)?;
517        }
518
519        // TODO: simplify
520        if let Some(offset) = top_dict.font_dict_index_offset {
521            let mut s = Stream::new_at(data, offset)?;
522            'outer: for font_dict_data in parse_index::<u32>(&mut s)? {
523                if let Some(private_dict_range) = parse_font_dict(font_dict_data) {
524                    // 'Private DICT size and offset, from start of the CFF2 table.'
525                    let private_dict_data = data.get(private_dict_range.clone())?;
526                    if let Some(subroutines_offset) = parse_private_dict(private_dict_data) {
527                        // 'The local subroutines offset is relative to the beginning
528                        // of the Private DICT data.'
529                        if let Some(start) =
530                            private_dict_range.start.checked_add(subroutines_offset)
531                        {
532                            let data = data.get(start..data.len())?;
533                            let mut s = Stream::new(data);
534                            metadata.local_subrs = parse_index::<u32>(&mut s)?;
535                            break 'outer;
536                        }
537                    }
538                }
539            }
540        }
541
542        Some(metadata)
543    }
544
545    /// Outlines a glyph.
546    pub fn outline(
547        &self,
548        coordinates: &[NormalizedCoordinate],
549        glyph_id: GlyphId,
550        builder: &mut dyn OutlineBuilder,
551    ) -> Result<Rect, CFFError> {
552        let data = self
553            .char_strings
554            .get(u32::from(glyph_id.0))
555            .ok_or(CFFError::NoGlyph)?;
556        parse_char_string(data, self, coordinates, builder)
557    }
558}
559
560impl core::fmt::Debug for Table<'_> {
561    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
562        write!(f, "Table {{ ... }}")
563    }
564}