Skip to main content

skrifa/outline/cff/
mod.rs

1//! Support for scaling CFF outlines.
2
3mod hint;
4
5use super::{GlyphHMetrics, OutlinePen};
6use hint::{HintParams, HintState, HintingSink};
7use read_fonts::{
8    ps::{
9        cff::{blend::BlendState, dict, fd_select::FdSelect, index::Index},
10        cs::{self, CommandSink, NopFilterSink, TransformSink},
11        error::Error,
12        transform::{self, FontMatrix, ScaledFontMatrix},
13    },
14    tables::variations::ItemVariationStore,
15    types::{F2Dot14, Fixed, GlyphId},
16    FontData, FontRead, FontRef, ReadError, TableProvider,
17};
18use std::ops::Range;
19
20/// Type for loading, scaling and hinting outlines in CFF/CFF2 tables.
21///
22/// The skrifa crate provides a higher level interface for this that handles
23/// caching and abstracting over the different outline formats. Consider using
24/// that if detailed control over resources is not required.
25///
26/// # Subfonts
27///
28/// CFF tables can contain multiple logical "subfonts" which determine the
29/// state required for processing some subset of glyphs. This state is
30/// accessed using the [`FDArray and FDSelect`](https://adobe-type-tools.github.io/font-tech-notes/pdfs/5176.CFF.pdf#page=28)
31/// operators to select an appropriate subfont for any given glyph identifier.
32/// This process is exposed on this type with the
33/// [`subfont_index`](Self::subfont_index) method to retrieve the subfont
34/// index for the requested glyph followed by using the
35/// [`subfont`](Self::subfont) method to create an appropriately configured
36/// subfont for that glyph.
37#[derive(Clone)]
38pub(crate) struct Outlines<'a> {
39    pub(crate) font: FontRef<'a>,
40    pub(crate) glyph_metrics: GlyphHMetrics<'a>,
41    offset_data: FontData<'a>,
42    global_subrs: Index<'a>,
43    top_dict: TopDict<'a>,
44    version: u16,
45    units_per_em: u16,
46}
47
48impl<'a> Outlines<'a> {
49    /// Creates a new scaler for the given font.
50    ///
51    /// This will choose an underlying CFF2 or CFF table from the font, in that
52    /// order.
53    pub fn new(font: &FontRef<'a>) -> Option<Self> {
54        let units_per_em = font.head().ok()?.units_per_em();
55        Self::from_cff2(font, units_per_em).or_else(|| Self::from_cff(font, units_per_em))
56    }
57
58    pub fn from_cff(font: &FontRef<'a>, units_per_em: u16) -> Option<Self> {
59        let cff1 = font.cff().ok()?;
60        let glyph_metrics = GlyphHMetrics::new(font)?;
61        // "The Name INDEX in the CFF data must contain only one entry;
62        // that is, there must be only one font in the CFF FontSet"
63        // So we always pass 0 for Top DICT index when reading from an
64        // OpenType font.
65        // <https://learn.microsoft.com/en-us/typography/opentype/spec/cff>
66        let top_dict_data = cff1.top_dicts().get(0).ok()?;
67        let top_dict = TopDict::new(cff1.offset_data().as_bytes(), top_dict_data, false).ok()?;
68        Some(Self {
69            font: font.clone(),
70            glyph_metrics,
71            offset_data: cff1.offset_data(),
72            global_subrs: cff1.global_subrs().into(),
73            top_dict,
74            version: 1,
75            units_per_em,
76        })
77    }
78
79    pub fn from_cff2(font: &FontRef<'a>, units_per_em: u16) -> Option<Self> {
80        let cff2 = font.cff2().ok()?;
81        let glyph_metrics = GlyphHMetrics::new(font)?;
82        let table_data = cff2.offset_data().as_bytes();
83        let top_dict = TopDict::new(table_data, cff2.top_dict_data(), true).ok()?;
84        Some(Self {
85            font: font.clone(),
86            glyph_metrics,
87            offset_data: cff2.offset_data(),
88            global_subrs: cff2.global_subrs().into(),
89            top_dict,
90            version: 2,
91            units_per_em,
92        })
93    }
94
95    pub fn is_cff2(&self) -> bool {
96        self.version == 2
97    }
98
99    pub fn units_per_em(&self) -> u16 {
100        self.units_per_em
101    }
102
103    /// Returns the number of available glyphs.
104    pub fn glyph_count(&self) -> usize {
105        self.top_dict.charstrings.count() as usize
106    }
107
108    /// Returns the number of available subfonts.
109    pub fn subfont_count(&self) -> u32 {
110        // All CFF fonts have at least one logical subfont.
111        self.top_dict.font_dicts.count().max(1)
112    }
113
114    /// Returns the subfont (or Font DICT) index for the given glyph
115    /// identifier.
116    pub fn subfont_index(&self, glyph_id: GlyphId) -> u32 {
117        // For CFF tables, an FDSelect index will be present for CID-keyed
118        // fonts. Otherwise, the Top DICT will contain an entry for the
119        // "global" Private DICT.
120        // See <https://adobe-type-tools.github.io/font-tech-notes/pdfs/5176.CFF.pdf#page=27>
121        //
122        // CFF2 tables always contain a Font DICT and an FDSelect is only
123        // present if the size of the DICT is greater than 1.
124        // See <https://learn.microsoft.com/en-us/typography/opentype/spec/cff2#10-font-dict-index-font-dicts-and-fdselect>
125        //
126        // In both cases, we return a subfont index of 0 when FDSelect is missing.
127        self.top_dict
128            .fd_select
129            .as_ref()
130            .and_then(|select| select.font_index(glyph_id))
131            .unwrap_or(0) as u32
132    }
133
134    /// Creates a new subfont for the given index, size, normalized
135    /// variation coordinates and hinting state.
136    ///
137    /// The index of a subfont for a particular glyph can be retrieved with
138    /// the [`subfont_index`](Self::subfont_index) method.
139    pub fn subfont(
140        &self,
141        index: u32,
142        size: Option<f32>,
143        coords: &[F2Dot14],
144    ) -> Result<Subfont, Error> {
145        let font_dict = self.parse_font_dict(index)?;
146        let blend_state = self
147            .top_dict
148            .var_store
149            .clone()
150            .map(|store| BlendState::new(store, coords, 0))
151            .transpose()?;
152        let private_dict =
153            PrivateDict::new(self.offset_data, font_dict.private_dict_range, blend_state)?;
154        let upem = self.units_per_em as i32;
155        let mut scale = match size {
156            Some(ppem) if upem > 0 => {
157                // Note: we do an intermediate scale to 26.6 to ensure we
158                // match FreeType
159                Some(Fixed::from_bits((ppem * 64.) as i32) / Fixed::from_bits(upem))
160            }
161            _ => None,
162        };
163        let scale_requested = size.is_some();
164        // Compute our font matrix and adjusted UPEM
165        // See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/f1cd6dbfa0c98f352b698448f40ac27e8fb3832e/src/cff/cffobjs.c#L746>
166        let font_matrix = if let Some(top_matrix) = self.top_dict.font_matrix {
167            // We have a top dict matrix. Now check for a font dict matrix.
168            if let Some(sub_matrix) = font_dict.font_matrix {
169                let scaling = if top_matrix.scale > 1 && sub_matrix.scale > 1 {
170                    top_matrix.scale.min(sub_matrix.scale)
171                } else {
172                    1
173                };
174                // Concatenate and scale
175                let matrix =
176                    transform::combine_scaled(&top_matrix.matrix, &sub_matrix.matrix, scaling);
177                let upem = Fixed::from_bits(sub_matrix.scale).mul_div(
178                    Fixed::from_bits(top_matrix.scale),
179                    Fixed::from_bits(scaling),
180                );
181                // Then normalize
182                Some(
183                    ScaledFontMatrix {
184                        matrix,
185                        scale: upem.to_bits(),
186                    }
187                    .normalize(),
188                )
189            } else {
190                // Top matrix was already normalized on load
191                Some(top_matrix)
192            }
193        } else {
194            // Just normalize if we have a subfont matrix
195            font_dict.font_matrix.map(|matrix| matrix.normalize())
196        };
197        // Now adjust our scale factor if necessary
198        // See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/f1cd6dbfa0c98f352b698448f40ac27e8fb3832e/src/cff/cffgload.c#L450>
199        let mut font_matrix = if let Some(matrix) = font_matrix {
200            // If the scaling factor from our matrix does not equal the nominal
201            // UPEM of the font then adjust the scale.
202            if matrix.scale != upem {
203                // In this case, we need to force a scale for "unscaled"
204                // requests in order to apply the adjusted UPEM from the
205                // font matrix.
206                let original_scale = scale.unwrap_or(Fixed::from_i32(64));
207                scale = Some(
208                    original_scale.mul_div(Fixed::from_bits(upem), Fixed::from_bits(matrix.scale)),
209                );
210            }
211            Some(matrix.matrix)
212        } else {
213            None
214        };
215        if font_matrix == Some(FontMatrix::IDENTITY) {
216            // Let's not waste time applying an identity matrix. This occurs
217            // fairly often after normalization.
218            font_matrix = None;
219        }
220        let hint_scale = scale_for_hinting(scale);
221        let hint_state = HintState::new(&private_dict.hint_params, hint_scale);
222        Ok(Subfont {
223            is_cff2: self.is_cff2(),
224            scale,
225            scale_requested,
226            subrs_offset: private_dict.subrs_offset,
227            hint_state,
228            store_index: private_dict.store_index,
229            font_matrix,
230        })
231    }
232
233    /// Loads and scales an outline for the given subfont instance, glyph
234    /// identifier and normalized variation coordinates.
235    ///
236    /// Before calling this method, use [`subfont_index`](Self::subfont_index)
237    /// to retrieve the subfont index for the desired glyph and then
238    /// [`subfont`](Self::subfont) to create an instance of the subfont for a
239    /// particular size and location in variation space.
240    /// Creating subfont instances is not free, so this process is exposed in
241    /// discrete steps to allow for caching.
242    ///
243    /// The result is emitted to the specified pen.
244    pub fn draw(
245        &self,
246        subfont: &Subfont,
247        glyph_id: GlyphId,
248        coords: &[F2Dot14],
249        hint: bool,
250        pen: &mut impl OutlinePen,
251    ) -> Result<(), Error> {
252        let cff_data = self.offset_data.as_bytes();
253        let charstrings = self.top_dict.charstrings.clone();
254        let charstring_data = charstrings.get(glyph_id.to_u32() as usize)?;
255        let subrs = subfont.subrs(self)?;
256        let blend_state = subfont.blend_state(self, coords)?;
257        let cs_eval = CharstringEvaluator {
258            cff_data,
259            charstrings,
260            global_subrs: self.global_subrs.clone(),
261            subrs,
262            blend_state,
263            charstring_data,
264        };
265        // Only apply hinting if we have a scale
266        let apply_hinting = hint && subfont.scale_requested;
267        let mut pen_sink = PenSink::new(pen);
268        let mut simplifying_adapter = NopFilterSink::new(&mut pen_sink);
269        if let Some(matrix) = subfont.font_matrix {
270            if apply_hinting {
271                let mut transform_sink =
272                    HintedTransformingSink::new(&mut simplifying_adapter, matrix);
273                let mut hinting_adapter =
274                    HintingSink::new(&subfont.hint_state, &mut transform_sink);
275                cs_eval.evaluate(&mut hinting_adapter)?;
276            } else {
277                let mut transform_sink = TransformSink::from_matrix_scale(
278                    &mut simplifying_adapter,
279                    matrix,
280                    subfont.scale,
281                );
282                cs_eval.evaluate(&mut transform_sink)?;
283            }
284        } else if apply_hinting {
285            let mut hinting_adapter =
286                HintingSink::new(&subfont.hint_state, &mut simplifying_adapter);
287            cs_eval.evaluate(&mut hinting_adapter)?;
288        } else {
289            let mut scaling_adapter = TransformSink::from_matrix_scale(
290                &mut simplifying_adapter,
291                FontMatrix::IDENTITY,
292                subfont.scale,
293            );
294            cs_eval.evaluate(&mut scaling_adapter)?;
295        }
296        Ok(())
297    }
298
299    fn parse_font_dict(&self, subfont_index: u32) -> Result<FontDict, Error> {
300        if self.top_dict.font_dicts.count() != 0 {
301            // If we have a font dict array, extract the private dict range
302            // from the font dict at the given index.
303            let font_dict_data = self.top_dict.font_dicts.get(subfont_index as usize)?;
304            FontDict::new(font_dict_data)
305        } else {
306            // Use the private dict range from the top dict.
307            // Note: "A Private DICT is required but may be specified as having
308            // a length of 0 if there are no non-default values to be stored."
309            // <https://adobe-type-tools.github.io/font-tech-notes/pdfs/5176.CFF.pdf#page=25>
310            let range = self.top_dict.private_dict_range.clone();
311            Ok(FontDict {
312                private_dict_range: range.start as usize..range.end as usize,
313                font_matrix: None,
314            })
315        }
316    }
317}
318
319/// When hinting, use a modified scale factor.
320///
321/// See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/psaux/psft.c#L279>
322fn scale_for_hinting(scale: Option<Fixed>) -> Fixed {
323    Fixed::from_bits((scale.unwrap_or(Fixed::ONE).to_bits().saturating_add(32)) / 64)
324}
325
326struct CharstringEvaluator<'a> {
327    cff_data: &'a [u8],
328    charstrings: Index<'a>,
329    global_subrs: Index<'a>,
330    subrs: Option<Index<'a>>,
331    blend_state: Option<BlendState<'a>>,
332    charstring_data: &'a [u8],
333}
334
335impl CharstringEvaluator<'_> {
336    fn evaluate(self, sink: &mut impl CommandSink) -> Result<Option<Fixed>, Error> {
337        let subrs = self.subrs.unwrap_or_default();
338        let ctx = (self.cff_data, &self.charstrings, &self.global_subrs, &subrs);
339        cs::evaluate(&ctx, self.blend_state, self.charstring_data, sink)
340    }
341}
342
343/// Specifies local subroutines and hinting parameters for some subset of
344/// glyphs in a CFF or CFF2 table.
345///
346/// This type is designed to be cacheable to avoid re-evaluating the private
347/// dict every time a charstring is processed.
348///
349/// For variable fonts, this is dependent on a location in variation space.
350#[derive(Clone)]
351pub(crate) struct Subfont {
352    is_cff2: bool,
353    scale: Option<Fixed>,
354    /// When we have a font matrix, we might force a scale even if the user
355    /// requested unscaled output. In this case, we shouldn't apply hinting
356    /// and this keeps track of that.
357    scale_requested: bool,
358    subrs_offset: Option<usize>,
359    pub(crate) hint_state: HintState,
360    store_index: u16,
361    font_matrix: Option<FontMatrix>,
362}
363
364impl Subfont {
365    /// Returns the local subroutine index.
366    pub fn subrs<'a>(&self, scaler: &Outlines<'a>) -> Result<Option<Index<'a>>, Error> {
367        if let Some(subrs_offset) = self.subrs_offset {
368            let offset_data = scaler.offset_data.as_bytes();
369            let index_data = offset_data.get(subrs_offset..).unwrap_or_default();
370            Ok(Some(Index::new(index_data, self.is_cff2)?))
371        } else {
372            Ok(None)
373        }
374    }
375
376    /// Creates a new blend state for the given normalized variation
377    /// coordinates.
378    pub fn blend_state<'a>(
379        &self,
380        scaler: &Outlines<'a>,
381        coords: &'a [F2Dot14],
382    ) -> Result<Option<BlendState<'a>>, Error> {
383        if let Some(var_store) = scaler.top_dict.var_store.clone() {
384            Ok(Some(BlendState::new(var_store, coords, self.store_index)?))
385        } else {
386            Ok(None)
387        }
388    }
389}
390
391/// Entries that we parse from the Private DICT to support charstring
392/// evaluation.
393#[derive(Default)]
394struct PrivateDict {
395    hint_params: HintParams,
396    subrs_offset: Option<usize>,
397    store_index: u16,
398}
399
400impl PrivateDict {
401    fn new(
402        data: FontData,
403        range: Range<usize>,
404        blend_state: Option<BlendState<'_>>,
405    ) -> Result<Self, Error> {
406        let private_dict_data = data.read_array(range.clone())?;
407        let mut dict = Self::default();
408        for entry in dict::entries(private_dict_data, blend_state) {
409            use dict::Entry::*;
410            match entry? {
411                BlueValues(values) => dict.hint_params.blues = values,
412                FamilyBlues(values) => dict.hint_params.family_blues = values,
413                OtherBlues(values) => dict.hint_params.other_blues = values,
414                FamilyOtherBlues(values) => dict.hint_params.family_other_blues = values,
415                BlueScale(value) => dict.hint_params.blue_scale = value,
416                BlueShift(value) => dict.hint_params.blue_shift = value,
417                BlueFuzz(value) => dict.hint_params.blue_fuzz = value,
418                LanguageGroup(group) => dict.hint_params.language_group = group,
419                // Subrs offset is relative to the private DICT
420                SubrsOffset(offset) => {
421                    dict.subrs_offset = Some(
422                        range
423                            .start
424                            .checked_add(offset)
425                            .ok_or(ReadError::OutOfBounds)?,
426                    )
427                }
428                VariationStoreIndex(index) => dict.store_index = index,
429                _ => {}
430            }
431        }
432        Ok(dict)
433    }
434}
435
436/// Entries that we parse from a Font DICT.
437#[derive(Clone, Default)]
438struct FontDict {
439    private_dict_range: Range<usize>,
440    font_matrix: Option<ScaledFontMatrix>,
441}
442
443impl FontDict {
444    fn new(font_dict_data: &[u8]) -> Result<Self, Error> {
445        let mut range = None;
446        let mut font_matrix = None;
447        for entry in dict::entries(font_dict_data, None) {
448            match entry? {
449                dict::Entry::PrivateDictRange(r) => {
450                    range = Some(r);
451                }
452                // We store this matrix unnormalized since FreeType
453                // concatenates this with the top dict matrix (if present)
454                // before normalizing
455                dict::Entry::FontMatrix(matrix) => font_matrix = Some(matrix),
456                _ => {}
457            }
458        }
459        Ok(Self {
460            private_dict_range: range.ok_or(Error::MissingPrivateDict)?,
461            font_matrix,
462        })
463    }
464}
465
466/// Entries that we parse from the Top DICT that are required to support
467/// charstring evaluation.
468#[derive(Clone, Default)]
469struct TopDict<'a> {
470    charstrings: Index<'a>,
471    font_dicts: Index<'a>,
472    fd_select: Option<FdSelect<'a>>,
473    private_dict_range: Range<u32>,
474    font_matrix: Option<ScaledFontMatrix>,
475    var_store: Option<ItemVariationStore<'a>>,
476}
477
478impl<'a> TopDict<'a> {
479    fn new(table_data: &'a [u8], top_dict_data: &'a [u8], is_cff2: bool) -> Result<Self, Error> {
480        let mut items = TopDict::default();
481        for entry in dict::entries(top_dict_data, None) {
482            match entry? {
483                dict::Entry::CharstringsOffset(offset) => {
484                    items.charstrings =
485                        Index::new(table_data.get(offset..).unwrap_or_default(), is_cff2)?;
486                }
487                dict::Entry::FdArrayOffset(offset) => {
488                    items.font_dicts =
489                        Index::new(table_data.get(offset..).unwrap_or_default(), is_cff2)?;
490                }
491                dict::Entry::FdSelectOffset(offset) => {
492                    items.fd_select = Some(FdSelect::read(FontData::new(
493                        table_data.get(offset..).unwrap_or_default(),
494                    ))?);
495                }
496                dict::Entry::PrivateDictRange(range) => {
497                    items.private_dict_range = range.start as u32..range.end as u32;
498                }
499                dict::Entry::FontMatrix(matrix) => {
500                    // Store this matrix normalized since FT always applies normalization
501                    items.font_matrix = Some(matrix.normalize());
502                }
503                dict::Entry::VariationStoreOffset(offset) if is_cff2 => {
504                    // IVS is preceded by a 2 byte length, but ensure that
505                    // we don't overflow
506                    // See <https://github.com/googlefonts/fontations/issues/1223>
507                    let offset = offset.checked_add(2).ok_or(ReadError::OutOfBounds)?;
508                    items.var_store = Some(ItemVariationStore::read(FontData::new(
509                        table_data.get(offset..).unwrap_or_default(),
510                    ))?);
511                }
512                _ => {}
513            }
514        }
515        Ok(items)
516    }
517}
518
519/// Command sink that sends the results of charstring evaluation to
520/// an [OutlinePen].
521struct PenSink<'a, P>(&'a mut P);
522
523impl<'a, P> PenSink<'a, P> {
524    fn new(pen: &'a mut P) -> Self {
525        Self(pen)
526    }
527}
528
529impl<P> CommandSink for PenSink<'_, P>
530where
531    P: OutlinePen,
532{
533    fn move_to(&mut self, x: Fixed, y: Fixed) {
534        self.0.move_to(x.to_f32(), y.to_f32());
535    }
536
537    fn line_to(&mut self, x: Fixed, y: Fixed) {
538        self.0.line_to(x.to_f32(), y.to_f32());
539    }
540
541    fn curve_to(&mut self, cx0: Fixed, cy0: Fixed, cx1: Fixed, cy1: Fixed, x: Fixed, y: Fixed) {
542        self.0.curve_to(
543            cx0.to_f32(),
544            cy0.to_f32(),
545            cx1.to_f32(),
546            cy1.to_f32(),
547            x.to_f32(),
548            y.to_f32(),
549        );
550    }
551
552    fn close(&mut self) {
553        self.0.close();
554    }
555}
556
557/// Command sink adapter that applies a transform to hinted coordinates.
558struct HintedTransformingSink<'a, S> {
559    inner: &'a mut S,
560    matrix: FontMatrix,
561}
562
563impl<'a, S> HintedTransformingSink<'a, S> {
564    fn new(sink: &'a mut S, matrix: FontMatrix) -> Self {
565        Self {
566            inner: sink,
567            matrix,
568        }
569    }
570
571    fn transform(&self, x: Fixed, y: Fixed) -> (Fixed, Fixed) {
572        // FreeType applies the transform to 26.6 values but we maintain
573        // values in 16.16 so convert, transform and then convert back
574        let (x, y) = self.matrix.transform(
575            Fixed::from_bits(x.to_bits() >> 10),
576            Fixed::from_bits(y.to_bits() >> 10),
577        );
578        (
579            Fixed::from_bits(x.to_bits() << 10),
580            Fixed::from_bits(y.to_bits() << 10),
581        )
582    }
583}
584
585impl<S: CommandSink> CommandSink for HintedTransformingSink<'_, S> {
586    fn hstem(&mut self, y: Fixed, dy: Fixed) {
587        self.inner.hstem(y, dy);
588    }
589
590    fn vstem(&mut self, x: Fixed, dx: Fixed) {
591        self.inner.vstem(x, dx);
592    }
593
594    fn hint_mask(&mut self, mask: &[u8]) {
595        self.inner.hint_mask(mask);
596    }
597
598    fn counter_mask(&mut self, mask: &[u8]) {
599        self.inner.counter_mask(mask);
600    }
601
602    fn clear_hints(&mut self) {
603        self.inner.clear_hints();
604    }
605
606    fn move_to(&mut self, x: Fixed, y: Fixed) {
607        let (x, y) = self.transform(x, y);
608        self.inner.move_to(x, y);
609    }
610
611    fn line_to(&mut self, x: Fixed, y: Fixed) {
612        let (x, y) = self.transform(x, y);
613        self.inner.line_to(x, y);
614    }
615
616    fn curve_to(&mut self, cx1: Fixed, cy1: Fixed, cx2: Fixed, cy2: Fixed, x: Fixed, y: Fixed) {
617        let (cx1, cy1) = self.transform(cx1, cy1);
618        let (cx2, cy2) = self.transform(cx2, cy2);
619        let (x, y) = self.transform(x, y);
620        self.inner.curve_to(cx1, cy1, cx2, cy2, x, y);
621    }
622
623    fn close(&mut self) {
624        self.inner.close();
625    }
626
627    fn finish(&mut self) {
628        self.inner.finish();
629    }
630}
631
632#[cfg(test)]
633mod tests {
634    use super::{super::pen::SvgPen, *};
635    use crate::{
636        outline::{HintingInstance, HintingOptions},
637        prelude::{LocationRef, Size},
638        MetadataProvider,
639    };
640    use font_test_data::bebuffer::BeBuffer;
641    use raw::tables::cff2::Cff2;
642    use read_fonts::ps::hinting::Blues;
643    use read_fonts::FontRef;
644
645    #[test]
646    fn read_cff_static() {
647        let font = FontRef::new(font_test_data::NOTO_SERIF_DISPLAY_TRIMMED).unwrap();
648        let cff = Outlines::new(&font).unwrap();
649        assert!(!cff.is_cff2());
650        assert!(cff.top_dict.var_store.is_none());
651        assert!(cff.top_dict.font_dicts.count() == 0);
652        assert!(!cff.top_dict.private_dict_range.is_empty());
653        assert!(cff.top_dict.fd_select.is_none());
654        assert_eq!(cff.subfont_count(), 1);
655        assert_eq!(cff.subfont_index(GlyphId::new(1)), 0);
656        assert_eq!(cff.global_subrs.count(), 17);
657    }
658
659    #[test]
660    fn read_cff2_static() {
661        let font = FontRef::new(font_test_data::CANTARELL_VF_TRIMMED).unwrap();
662        let cff = Outlines::new(&font).unwrap();
663        assert!(cff.is_cff2());
664        assert!(cff.top_dict.var_store.is_some());
665        assert!(cff.top_dict.font_dicts.count() != 0);
666        assert!(cff.top_dict.private_dict_range.is_empty());
667        assert!(cff.top_dict.fd_select.is_none());
668        assert_eq!(cff.subfont_count(), 1);
669        assert_eq!(cff.subfont_index(GlyphId::new(1)), 0);
670        assert_eq!(cff.global_subrs.count(), 0);
671    }
672
673    #[test]
674    fn read_example_cff2_table() {
675        let cff2 = Cff2::read(FontData::new(font_test_data::cff2::EXAMPLE)).unwrap();
676        let top_dict =
677            TopDict::new(cff2.offset_data().as_bytes(), cff2.top_dict_data(), true).unwrap();
678        assert!(top_dict.var_store.is_some());
679        assert!(top_dict.font_dicts.count() != 0);
680        assert!(top_dict.private_dict_range.is_empty());
681        assert!(top_dict.fd_select.is_none());
682        assert_eq!(cff2.global_subrs().count(), 0);
683    }
684
685    #[test]
686    fn cff2_variable_outlines_match_freetype() {
687        compare_glyphs(
688            font_test_data::CANTARELL_VF_TRIMMED,
689            font_test_data::CANTARELL_VF_TRIMMED_GLYPHS,
690        );
691    }
692
693    #[test]
694    fn cff_static_outlines_match_freetype() {
695        compare_glyphs(
696            font_test_data::NOTO_SERIF_DISPLAY_TRIMMED,
697            font_test_data::NOTO_SERIF_DISPLAY_TRIMMED_GLYPHS,
698        );
699    }
700
701    #[test]
702    fn unhinted_ends_with_close() {
703        let font = FontRef::new(font_test_data::CANTARELL_VF_TRIMMED).unwrap();
704        let glyph = font.outline_glyphs().get(GlyphId::new(1)).unwrap();
705        let mut svg = SvgPen::default();
706        glyph.draw(Size::unscaled(), &mut svg).unwrap();
707        assert!(svg.to_string().ends_with('Z'));
708    }
709
710    #[test]
711    fn hinted_ends_with_close() {
712        let font = FontRef::new(font_test_data::CANTARELL_VF_TRIMMED).unwrap();
713        let glyphs = font.outline_glyphs();
714        let hinter = HintingInstance::new(
715            &glyphs,
716            Size::unscaled(),
717            LocationRef::default(),
718            HintingOptions::default(),
719        )
720        .unwrap();
721        let glyph = glyphs.get(GlyphId::new(1)).unwrap();
722        let mut svg = SvgPen::default();
723        glyph.draw(&hinter, &mut svg).unwrap();
724        assert!(svg.to_string().ends_with('Z'));
725    }
726
727    /// Ensure we don't reject an empty Private DICT
728    #[test]
729    fn empty_private_dict() {
730        let font = FontRef::new(font_test_data::MATERIAL_ICONS_SUBSET).unwrap();
731        let outlines = super::Outlines::new(&font).unwrap();
732        assert!(outlines.top_dict.private_dict_range.is_empty());
733        assert!(outlines
734            .parse_font_dict(0)
735            .unwrap()
736            .private_dict_range
737            .is_empty());
738    }
739
740    /// Fuzzer caught add with overflow when computing subrs offset.
741    /// See <https://issues.oss-fuzz.com/issues/377965575>
742    #[test]
743    fn subrs_offset_overflow() {
744        // A private DICT with an overflowing subrs offset
745        let private_dict = BeBuffer::new()
746            .push(0u32) // pad so that range doesn't start with 0 and we overflow
747            .push(29u8) // integer operator
748            .push(-1i32) // integer value
749            .push(19u8) // subrs offset operator
750            .to_vec();
751        // Just don't panic with overflow
752        assert!(
753            PrivateDict::new(FontData::new(&private_dict), 4..private_dict.len(), None).is_err()
754        );
755    }
756
757    // Fuzzer caught add with overflow when computing offset to
758    // var store.
759    // See <https://issues.oss-fuzz.com/issues/377574377>
760    #[test]
761    fn top_dict_ivs_offset_overflow() {
762        // A top DICT with a var store offset of -1 which will cause an
763        // overflow
764        let top_dict = BeBuffer::new()
765            .push(29u8) // integer operator
766            .push(-1i32) // integer value
767            .push(24u8) // var store offset operator
768            .to_vec();
769        // Just don't panic with overflow
770        assert!(TopDict::new(&[], &top_dict, true).is_err());
771    }
772
773    /// Actually apply a scale when the computed scale factor is
774    /// equal to Fixed::ONE.
775    ///
776    /// Specifically, when upem = 512 and ppem = 8, this results in
777    /// a scale factor of 65536 which was being interpreted as an
778    /// unscaled draw request.
779    #[test]
780    fn proper_scaling_when_factor_equals_fixed_one() {
781        let font = FontRef::new(font_test_data::MATERIAL_ICONS_SUBSET).unwrap();
782        assert_eq!(font.head().unwrap().units_per_em(), 512);
783        let glyphs = font.outline_glyphs();
784        let glyph = glyphs.get(GlyphId::new(1)).unwrap();
785        let mut svg = SvgPen::with_precision(6);
786        glyph
787            .draw((Size::new(8.0), LocationRef::default()), &mut svg)
788            .unwrap();
789        // This was initially producing unscaled values like M405.000...
790        assert!(svg.starts_with("M6.328125,7.000000 L1.671875,7.000000"));
791    }
792
793    /// For the given font data and extracted outlines, parse the extracted
794    /// outline data into a set of expected values and compare these with the
795    /// results generated by the scaler.
796    ///
797    /// This will compare all outlines at various sizes and (for variable
798    /// fonts), locations in variation space.
799    fn compare_glyphs(font_data: &[u8], expected_outlines: &str) {
800        use super::super::testing;
801        let font = FontRef::new(font_data).unwrap();
802        let expected_outlines = testing::parse_glyph_outlines(expected_outlines);
803        let outlines = super::Outlines::new(&font).unwrap();
804        let mut path = testing::Path::default();
805        for expected_outline in &expected_outlines {
806            if expected_outline.size == 0.0 && !expected_outline.coords.is_empty() {
807                continue;
808            }
809            let size = (expected_outline.size != 0.0).then_some(expected_outline.size);
810            path.elements.clear();
811            let subfont = outlines
812                .subfont(
813                    outlines.subfont_index(expected_outline.glyph_id),
814                    size,
815                    &expected_outline.coords,
816                )
817                .unwrap();
818            outlines
819                .draw(
820                    &subfont,
821                    expected_outline.glyph_id,
822                    &expected_outline.coords,
823                    false,
824                    &mut path,
825                )
826                .unwrap();
827            if path.elements != expected_outline.path {
828                panic!(
829                    "mismatch in glyph path for id {} (size: {}, coords: {:?}): path: {:?} expected_path: {:?}",
830                    expected_outline.glyph_id,
831                    expected_outline.size,
832                    expected_outline.coords,
833                    &path.elements,
834                    &expected_outline.path
835                );
836            }
837        }
838    }
839
840    // We were overwriting family_other_blues with family_blues.
841    #[test]
842    fn capture_family_other_blues() {
843        let private_dict_data = &font_test_data::cff2::EXAMPLE[0x4f..=0xc0];
844        let store =
845            ItemVariationStore::read(FontData::new(&font_test_data::cff2::EXAMPLE[18..])).unwrap();
846        let coords = &[F2Dot14::from_f32(0.0)];
847        let blend_state = BlendState::new(store, coords, 0).unwrap();
848        let private_dict = PrivateDict::new(
849            FontData::new(private_dict_data),
850            0..private_dict_data.len(),
851            Some(blend_state),
852        )
853        .unwrap();
854        assert_eq!(
855            private_dict.hint_params.family_other_blues,
856            Blues::new([-249.0, -239.0].map(Fixed::from_f64).into_iter())
857        )
858    }
859
860    #[test]
861    fn implied_seac() {
862        let font = FontRef::new(font_test_data::CHARSTRING_PATH_OPS).unwrap();
863        let glyphs = font.outline_glyphs();
864        let gid = GlyphId::new(3);
865        assert_eq!(font.glyph_names().get(gid).unwrap(), "Scaron");
866        let glyph = glyphs.get(gid).unwrap();
867        let mut pen = SvgPen::new();
868        glyph
869            .draw((Size::unscaled(), LocationRef::default()), &mut pen)
870            .unwrap();
871        // This triggers the seac behavior in the endchar operator which
872        // loads an accent character followed by a base character. Ensure
873        // that we have a path to represent each by checking for two closepath
874        // commands.
875        assert_eq!(pen.to_string().chars().filter(|ch| *ch == 'Z').count(), 2);
876    }
877
878    #[test]
879    fn implied_seac_clears_hints() {
880        let font = FontRef::new(font_test_data::CHARSTRING_PATH_OPS).unwrap();
881        let outlines = Outlines::from_cff(&font, 1000).unwrap();
882        let subfont = outlines.subfont(0, Some(16.0), &[]).unwrap();
883        let cff_data = outlines.offset_data.as_bytes();
884        let charstrings = outlines.top_dict.charstrings.clone();
885        let charstring_data = charstrings.get(3).unwrap();
886        let subrs = subfont.subrs(&outlines).unwrap();
887        let blend_state = None;
888        let cs_eval = CharstringEvaluator {
889            cff_data,
890            charstrings,
891            global_subrs: outlines.global_subrs.clone(),
892            subrs,
893            blend_state,
894            charstring_data,
895        };
896        struct ClearHintsCountingSink(u32);
897        impl CommandSink for ClearHintsCountingSink {
898            fn move_to(&mut self, _: Fixed, _: Fixed) {}
899            fn line_to(&mut self, _: Fixed, _: Fixed) {}
900            fn curve_to(&mut self, _: Fixed, _: Fixed, _: Fixed, _: Fixed, _: Fixed, _: Fixed) {}
901            fn close(&mut self) {}
902            fn clear_hints(&mut self) {
903                self.0 += 1;
904            }
905        }
906        let mut sink = ClearHintsCountingSink(0);
907        cs_eval.evaluate(&mut sink).unwrap();
908        // We should have cleared hints twice.. once for the base and once
909        // for the accent
910        assert_eq!(sink.0, 2);
911    }
912
913    const TRANSFORM: FontMatrix = FontMatrix::from_elements([
914        Fixed::ONE,
915        Fixed::ZERO,
916        // 0.167007446289062
917        Fixed::from_bits(10945),
918        Fixed::ONE,
919        Fixed::ZERO,
920        Fixed::ZERO,
921    ]);
922
923    #[test]
924    fn hinted_transform_sink() {
925        // A few points taken from the test font in <https://github.com/googlefonts/fontations/issues/1581>
926        // Inputs and expected values extracted from FreeType
927        let input = [(383i32, 117i32), (450, 20), (555, -34), (683, -34)]
928            .map(|(x, y)| (Fixed::from_bits(x << 10), Fixed::from_bits(y << 10)));
929        let expected = [(403, 117i32), (453, 20), (549, -34), (677, -34)]
930            .map(|(x, y)| (Fixed::from_bits(x << 10), Fixed::from_bits(y << 10)));
931        let mut dummy = ();
932        let sink = HintedTransformingSink::new(&mut dummy, TRANSFORM);
933        let transformed = input.map(|(x, y)| sink.transform(x, y));
934        assert_eq!(transformed, expected);
935    }
936
937    /// See <https://github.com/googlefonts/fontations/issues/1638>
938    #[test]
939    fn nested_font_matrices() {
940        // Expected values extracted from FreeType debugging session
941        let font = FontRef::new(font_test_data::MATERIAL_ICONS_SUBSET_MATRIX).unwrap();
942        let outlines = Outlines::from_cff(&font, 512).unwrap();
943        // Check the normalized top dict matrix
944        let top_matrix = outlines.top_dict.font_matrix.unwrap();
945        let expected_top_matrix = [65536, 0, 5604, 65536, 0, 0].map(Fixed::from_bits);
946        assert_eq!(top_matrix.matrix.elements(), expected_top_matrix);
947        assert_eq!(top_matrix.scale, 512);
948        // Check the unnormalized font dict matrix
949        let sub_matrix = outlines.parse_font_dict(0).unwrap().font_matrix.unwrap();
950        let expected_sub_matrix = [327680, 0, 0, 327680, 0, 0].map(Fixed::from_bits);
951        assert_eq!(sub_matrix.matrix.elements(), expected_sub_matrix);
952        assert_eq!(sub_matrix.scale, 10);
953        // Check the normalized combined matrix
954        let subfont = outlines.subfont(0, Some(24.0), &[]).unwrap();
955        let expected_combined_matrix = [65536, 0, 5604, 65536, 0, 0].map(Fixed::from_bits);
956        assert_eq!(
957            subfont.font_matrix.unwrap().elements(),
958            expected_combined_matrix
959        );
960        // Check the final scale
961        assert_eq!(subfont.scale.unwrap().to_bits(), 98304);
962    }
963
964    /// OSS fuzz caught add with overflow for hint scale computation.
965    /// See <https://oss-fuzz.com/testcase-detail/6498790355042304>
966    /// and <https://issues.oss-fuzz.com/issues/444024349>
967    #[test]
968    fn subfont_hint_scale_overflow() {
969        // Just don't panic with overflow
970        let _ = scale_for_hinting(Some(Fixed::from_bits(i32::MAX)));
971    }
972}