ttf_parser/tables/
gvar.rs

1// https://docs.microsoft.com/en-us/typography/opentype/spec/gvar
2// https://docs.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuple-variation-store
3
4use core::cmp;
5use core::convert::TryFrom;
6use core::num::NonZeroU16;
7
8use crate::{loca, GlyphId, OutlineBuilder, Rect, BBox, NormalizedCoordinate};
9use crate::parser::{Stream, Offset, Offset16, Offset32, LazyArray16, F2DOT14};
10use crate::glyf::{self, Transform};
11
12/// 'The TrueType rasterizer dynamically generates 'phantom' points for each glyph
13/// that represent horizontal and vertical advance widths and side bearings,
14/// and the variation data within the `gvar` table includes data for these phantom points.'
15///
16/// We don't actually use them, but they are required during deltas parsing.
17const PHANTOM_POINTS_LEN: usize = 4;
18
19#[derive(Clone, Copy)]
20enum GlyphVariationDataOffsets<'a> {
21    Short(LazyArray16<'a, Offset16>),
22    Long(LazyArray16<'a, Offset32>),
23}
24
25#[derive(Clone, Copy)]
26pub struct Table<'a> {
27    axis_count: NonZeroU16,
28    shared_tuple_records: LazyArray16<'a, F2DOT14>,
29    offsets: GlyphVariationDataOffsets<'a>,
30    glyphs_variation_data: &'a [u8],
31}
32
33impl<'a> Table<'a> {
34    // https://docs.microsoft.com/en-us/typography/opentype/spec/gvar#gvar-header
35    pub fn parse(data: &'a [u8]) -> Option<Self> {
36        let mut s = Stream::new(data);
37        let version: u32 = s.read()?;
38        if version != 0x00010000 {
39            return None;
40        }
41
42        let axis_count: u16 = s.read()?;
43        let shared_tuple_count: u16 = s.read()?;
44        let shared_tuples_offset: Offset32 = s.read()?;
45        let glyph_count: u16 = s.read()?;
46        let flags: u16 = s.read()?;
47        let glyph_variation_data_array_offset: Offset32 = s.read()?;
48
49        // The axis count cannot be zero.
50        let axis_count = NonZeroU16::new(axis_count)?;
51
52        let shared_tuple_records = {
53            let mut sub_s = Stream::new_at(data, shared_tuples_offset.to_usize())?;
54            sub_s.read_array16::<F2DOT14>(shared_tuple_count.checked_mul(axis_count.get())?)?
55        };
56
57        let glyphs_variation_data = data.get(glyph_variation_data_array_offset.to_usize()..)?;
58        let offsets = {
59            let offsets_count = glyph_count.checked_add(1)?;
60            let is_long_format = flags & 1 == 1; // The first bit indicates a long format.
61            if is_long_format {
62                GlyphVariationDataOffsets::Long(s.read_array16::<Offset32>(offsets_count)?)
63            } else {
64                GlyphVariationDataOffsets::Short(s.read_array16::<Offset16>(offsets_count)?)
65            }
66        };
67
68        Some(Table {
69            axis_count,
70            shared_tuple_records,
71            offsets,
72            glyphs_variation_data,
73        })
74    }
75
76    #[inline]
77    fn parse_variation_data(
78        &self,
79        glyph_id: GlyphId,
80        coordinates: &[NormalizedCoordinate],
81        points_len: u16,
82        tuples: &mut VariationTuples<'a>,
83    ) -> Option<()> {
84        tuples.len = 0;
85
86        if coordinates.len() != usize::from(self.axis_count.get()) {
87            return None;
88        }
89
90        let next_glyph_id = glyph_id.0.checked_add(1)?;
91
92        let (start, end) = match self.offsets {
93            GlyphVariationDataOffsets::Short(ref array) => {
94                // 'If the short format (Offset16) is used for offsets,
95                // the value stored is the offset divided by 2.'
96                (array.get(glyph_id.0)?.to_usize() * 2, array.get(next_glyph_id)?.to_usize() * 2)
97            }
98            GlyphVariationDataOffsets::Long(ref array) => {
99                (array.get(glyph_id.0)?.to_usize(), array.get(next_glyph_id)?.to_usize())
100            }
101        };
102
103        // Ignore empty data.
104        if start == end {
105            return Some(());
106        }
107
108        let data = self.glyphs_variation_data.get(start..end)?;
109        parse_variation_data(coordinates, &self.shared_tuple_records, points_len, data, tuples)
110    }
111}
112
113
114pub(crate) fn outline(
115    loca_table: loca::Table,
116    glyf_table: &[u8],
117    gvar_table: &Table,
118    coordinates: &[NormalizedCoordinate],
119    glyph_id: GlyphId,
120    builder: &mut dyn OutlineBuilder,
121) -> Option<Rect> {
122    let mut b = glyf::Builder::new(Transform::default(), Some(BBox::new()), builder);
123
124    let range = loca_table.glyph_range(glyph_id)?;
125    let glyph_data = glyf_table.get(range)?;
126
127    outline_var_impl(loca_table, glyf_table, gvar_table,
128                     glyph_id, glyph_data, coordinates, 0, &mut b);
129    b.bbox.and_then(|bbox| bbox.to_rect())
130}
131
132fn outline_var_impl<'a>(
133    loca_table: loca::Table,
134    glyf_table: &[u8],
135    gvar_table: &Table,
136    glyph_id: GlyphId,
137    data: &[u8],
138    coordinates: &[NormalizedCoordinate],
139    depth: u8,
140    builder: &mut glyf::Builder,
141) -> Option<()> {
142    if depth >= glyf::MAX_COMPONENTS {
143        return None;
144    }
145
146    let mut s = Stream::new(data);
147    let number_of_contours: i16 = s.read()?;
148
149    // Skip bbox.
150    //
151    // In case of a variable font, a bounding box defined in the `glyf` data
152    // refers to the default variation values. Which is not what we want.
153    // Instead, we have to manually calculate outline's bbox.
154    s.advance(8);
155
156    // TODO: This is the most expensive part. Find a way to allocate it only once.
157    // `VariationTuples` is a very large struct, so allocate it once.
158    let mut tuples = VariationTuples {
159        headers: [VariationTuple::default(); MAX_TUPLES_LEN as usize],
160        len: 0,
161    };
162
163    if number_of_contours > 0 {
164        // Simple glyph.
165
166        let number_of_contours = NonZeroU16::new(number_of_contours as u16)?;
167        let mut glyph_points = glyf::parse_simple_outline(s.tail()?, number_of_contours)?;
168        let all_glyph_points = glyph_points.clone();
169        let points_len = glyph_points.points_left;
170        gvar_table.parse_variation_data(glyph_id, coordinates, points_len, &mut tuples)?;
171
172        while let Some(point) = glyph_points.next() {
173            let (x, y) = tuples.apply(all_glyph_points.clone(), glyph_points.clone(), point)?;
174            builder.push_point(x, y, point.on_curve_point, point.last_point);
175        }
176
177        Some(())
178    } else if number_of_contours < 0 {
179        // Composite glyph.
180
181        // In case of a composite glyph, `gvar` data contains position adjustments
182        // for each component.
183        // Basically, an additional translation used during transformation.
184        // So we have to push zero points manually, instead of parsing the `glyf` data.
185        //
186        // Details:
187        // https://docs.microsoft.com/en-us/typography/opentype/spec/gvar#point-numbers-and-processing-for-composite-glyphs
188
189        let mut components = glyf::CompositeGlyphIter::new(s.tail()?);
190        let components_count = components.clone().count() as u16;
191        gvar_table.parse_variation_data(glyph_id, coordinates, components_count, &mut tuples)?;
192
193        while let Some(component) = components.next() {
194            let (tx, ty) = tuples.apply_null()?;
195
196            let mut transform = builder.transform;
197
198            // Variation component offset should be applied only when
199            // the ARGS_ARE_XY_VALUES flag is set.
200            if component.flags.args_are_xy_values() {
201                transform = Transform::combine(transform, Transform::new_translate(tx, ty));
202            }
203
204            transform = Transform::combine(transform, component.transform);
205
206            let mut b = glyf::Builder::new(transform, builder.bbox, builder.builder);
207            let range = loca_table.glyph_range(component.glyph_id)?;
208            let glyph_data = glyf_table.get(range)?;
209            outline_var_impl(
210                loca_table, glyf_table, gvar_table, component.glyph_id,
211                glyph_data, coordinates, depth + 1, &mut b,
212            )?;
213
214            // Take updated bbox.
215            builder.bbox = b.bbox;
216        }
217
218        Some(())
219    } else {
220        // An empty glyph.
221        None
222    }
223}
224
225// https://docs.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuple-variation-store-header
226fn parse_variation_data<'a>(
227    coordinates: &[NormalizedCoordinate],
228    shared_tuple_records: &LazyArray16<F2DOT14>,
229    points_len: u16,
230    data: &'a [u8],
231    tuples: &mut VariationTuples<'a>,
232) -> Option<()> {
233    const SHARED_POINT_NUMBERS_FLAG: u16 = 0x8000;
234    const COUNT_MASK: u16 = 0x0FFF;
235
236    let mut main_stream = Stream::new(data);
237    let tuple_variation_count: u16 = main_stream.read()?;
238    let data_offset: Offset16 = main_stream.read()?;
239
240    // 'The high 4 bits are flags, and the low 12 bits
241    // are the number of tuple variation tables for this glyph.'
242    let has_shared_point_numbers = tuple_variation_count & SHARED_POINT_NUMBERS_FLAG != 0;
243    let tuple_variation_count = tuple_variation_count & COUNT_MASK;
244
245    // 'The number of tuple variation tables can be any number between 1 and 4095.'
246    // No need to check for 4095, because this is 0x0FFF that we masked before.
247    if tuple_variation_count == 0 {
248        return None;
249    }
250
251    if tuple_variation_count >= MAX_TUPLES_LEN {
252        return None;
253    }
254
255    // A glyph variation data consists of three parts: header + variation tuples + serialized data.
256    // Each tuple has it's own chunk in the serialized data.
257    // Because of that, we are using two parsing streams: one for tuples and one for serialized data.
258    // So we can parse them in parallel and avoid needless allocations.
259    let mut serialized_stream = Stream::new_at(data, data_offset.to_usize())?;
260
261    // All tuples in the variation data can reference the same point numbers,
262    // which are defined at the start of the serialized data.
263    let mut shared_point_numbers = None;
264    if has_shared_point_numbers {
265        shared_point_numbers = PackedPointsIter::new(&mut serialized_stream)?;
266    }
267
268    parse_variation_tuples(
269        tuple_variation_count,
270        coordinates,
271        shared_tuple_records,
272        shared_point_numbers,
273        points_len.checked_add(PHANTOM_POINTS_LEN as u16)?,
274        main_stream,
275        serialized_stream,
276        tuples,
277    )
278}
279
280#[derive(Clone, Copy, Default, Debug)]
281struct PointAndDelta {
282    x: i16,
283    y: i16,
284    x_delta: f32,
285    y_delta: f32,
286}
287
288// This structure will be used by the `VariationTuples` stack buffer,
289// so it has to be as small as possible.
290#[derive(Clone, Copy, Default)]
291struct VariationTuple<'a> {
292    set_points: Option<SetPointsIter<'a>>,
293    deltas: PackedDeltasIter<'a>,
294    /// The last parsed point with delta in the contour.
295    /// Used during delta resolving.
296    prev_point: Option<PointAndDelta>,
297}
298
299/// The maximum number of variation tuples.
300///
301/// The TrueType spec allows up to 4095 tuples, which is way larger
302/// than we do. But in reality, an average font will have less than 10 tuples.
303const MAX_TUPLES_LEN: u16 = 32;
304
305/// A list of variation tuples.
306///
307/// This is the only part of the `gvar` algorithm that actually allocates a data.
308/// On stack and not on heap, but still.
309/// This is probably unavoidable due to `gvar` structure,
310/// since we have to iterate all tuples in parallel.
311struct VariationTuples<'a> {
312    headers: [VariationTuple<'a>; MAX_TUPLES_LEN as usize], // 2560B
313    len: u16,
314}
315
316impl<'a> VariationTuples<'a> {
317    #[inline]
318    fn as_mut_slice(&mut self) -> &mut [VariationTuple<'a>] {
319        &mut self.headers[0..usize::from(self.len)]
320    }
321
322    fn apply(
323        &mut self,
324        all_points: glyf::GlyphPointsIter,
325        points: glyf::GlyphPointsIter,
326        point: glyf::GlyphPoint,
327    ) -> Option<(f32, f32)> {
328        let mut x = f32::from(point.x);
329        let mut y = f32::from(point.y);
330
331        for tuple in self.as_mut_slice() {
332            if let Some(ref mut set_points) = tuple.set_points {
333                if set_points.next()? {
334                    if let Some((x_delta, y_delta)) = tuple.deltas.next() {
335                        // Remember the last set point and delta.
336                        tuple.prev_point = Some(PointAndDelta {
337                            x: point.x, y: point.y, x_delta, y_delta
338                        });
339
340                        x += x_delta;
341                        y += y_delta;
342                    } else {
343                        // If there are no more deltas, we have to resolve them manually.
344                        let set_points = set_points.clone();
345                        let (x_delta, y_delta) = infer_deltas(
346                            tuple, set_points, points.clone(), all_points.clone(), point
347                        );
348
349                        x += x_delta;
350                        y += y_delta;
351                    }
352                } else {
353                    // Point is not referenced, so we have to resolve it.
354                    let set_points = set_points.clone();
355                    let (x_delta, y_delta) = infer_deltas(
356                        tuple, set_points, points.clone(), all_points.clone(), point
357                    );
358
359                    x += x_delta;
360                    y += y_delta;
361                }
362
363                if point.last_point {
364                    tuple.prev_point = None;
365                }
366            } else {
367                if let Some((x_delta, y_delta)) = tuple.deltas.next() {
368                    x += x_delta;
369                    y += y_delta;
370                }
371            }
372        }
373
374        Some((x, y))
375    }
376
377    // This is just like `apply()`, but without `infer_deltas`,
378    // since we use it only for component points and not a contour.
379    // And since there are no contour and no points, `infer_deltas()` will do nothing.
380    fn apply_null(&mut self) -> Option<(f32, f32)> {
381        let mut x = 0.0;
382        let mut y = 0.0;
383
384        for tuple in self.as_mut_slice() {
385            if let Some(ref mut set_points) = tuple.set_points {
386                if set_points.next()? {
387                    if let Some((x_delta, y_delta)) = tuple.deltas.next() {
388                        x += x_delta;
389                        y += y_delta;
390                    }
391                }
392            } else {
393                if let Some((x_delta, y_delta)) = tuple.deltas.next() {
394                    x += x_delta;
395                    y += y_delta;
396                }
397            }
398        }
399
400        Some((x, y))
401    }
402}
403
404
405#[derive(Clone, Copy, Default, Debug)]
406struct TupleVariationHeaderData {
407    scalar: f32,
408    has_private_point_numbers: bool,
409    serialized_data_len: u16,
410}
411
412// https://docs.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuplevariationheader
413fn parse_variation_tuples<'a>(
414    count: u16,
415    coordinates: &[NormalizedCoordinate],
416    shared_tuple_records: &LazyArray16<F2DOT14>,
417    shared_point_numbers: Option<PackedPointsIter<'a>>,
418    points_len: u16,
419    mut main_s: Stream<'a>,
420    mut serialized_s: Stream<'a>,
421    tuples: &mut VariationTuples<'a>,
422) -> Option<()> {
423    debug_assert!(core::mem::size_of::<VariationTuple>() <= 80);
424
425    // `TupleVariationHeader` has a variable size, so we cannot use a `LazyArray`.
426    for _ in 0..count {
427        let header = parse_tuple_variation_header(coordinates, shared_tuple_records, &mut main_s)?;
428        if !(header.scalar > 0.0) {
429            // Serialized data for headers with non-positive scalar should be skipped.
430            serialized_s.advance(usize::from(header.serialized_data_len));
431            continue;
432        }
433
434        let serialized_data_start = serialized_s.offset();
435
436        // Resolve point numbers source.
437        let point_numbers = if header.has_private_point_numbers {
438            PackedPointsIter::new(&mut serialized_s)?
439        } else {
440            shared_point_numbers.clone()
441        };
442
443        // TODO: this
444        // Since the packed representation can include zero values,
445        // it is possible for a given point number to be repeated in the derived point number list.
446        // In that case, there will be multiple delta values in the deltas data
447        // associated with that point number. All of these deltas must be applied
448        // cumulatively to the given point.
449
450        let deltas_count = if let Some(point_numbers) = point_numbers.clone() {
451            u16::try_from(point_numbers.clone().count()).ok()?
452        } else {
453            points_len
454        };
455
456        let deltas = {
457            // Use `checked_sub` in case we went over the `serialized_data_len`.
458            let left = usize::from(header.serialized_data_len)
459                .checked_sub(serialized_s.offset() - serialized_data_start)?;
460            let deltas_data = serialized_s.read_bytes(left)?;
461            PackedDeltasIter::new(header.scalar, deltas_count, deltas_data)
462        };
463
464        let tuple = VariationTuple {
465            set_points: point_numbers.map(SetPointsIter::new),
466            deltas,
467            prev_point: None,
468        };
469
470        tuples.headers[usize::from(tuples.len)] = tuple;
471        tuples.len += 1;
472    }
473
474    Some(())
475}
476
477// https://docs.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuplevariationheader
478fn parse_tuple_variation_header(
479    coordinates: &[NormalizedCoordinate],
480    shared_tuple_records: &LazyArray16<F2DOT14>,
481    s: &mut Stream,
482) -> Option<TupleVariationHeaderData> {
483    const EMBEDDED_PEAK_TUPLE_FLAG: u16 = 0x8000;
484    const INTERMEDIATE_REGION_FLAG: u16 = 0x4000;
485    const PRIVATE_POINT_NUMBERS_FLAG: u16 = 0x2000;
486    const TUPLE_INDEX_MASK: u16 = 0x0FFF;
487
488    let serialized_data_size: u16 = s.read()?;
489    let tuple_index: u16 = s.read()?;
490
491    let has_embedded_peak_tuple = tuple_index & EMBEDDED_PEAK_TUPLE_FLAG != 0;
492    let has_intermediate_region = tuple_index & INTERMEDIATE_REGION_FLAG != 0;
493    let has_private_point_numbers = tuple_index & PRIVATE_POINT_NUMBERS_FLAG != 0;
494    let tuple_index = tuple_index & TUPLE_INDEX_MASK;
495
496    let axis_count = coordinates.len() as u16;
497
498    let peak_tuple = if has_embedded_peak_tuple {
499        s.read_array16::<F2DOT14>(axis_count)?
500    } else {
501        // Use shared tuples.
502        let start = tuple_index.checked_mul(axis_count)?;
503        let end = start.checked_add(axis_count)?;
504        shared_tuple_records.slice(start..end)?
505    };
506
507    let (start_tuple, end_tuple) = if has_intermediate_region {
508        (s.read_array16::<F2DOT14>(axis_count)?, s.read_array16::<F2DOT14>(axis_count)?)
509    } else {
510        (LazyArray16::<F2DOT14>::default(), LazyArray16::<F2DOT14>::default())
511    };
512
513    let mut header = TupleVariationHeaderData {
514        scalar: 0.0,
515        has_private_point_numbers,
516        serialized_data_len: serialized_data_size,
517    };
518
519    // Calculate the scalar value according to the pseudo-code described at:
520    // https://docs.microsoft.com/en-us/typography/opentype/spec/otvaroverview#algorithm-for-interpolation-of-instance-values
521    let mut scalar = 1.0;
522    for i in 0..axis_count {
523        let v = coordinates[usize::from(i)].get();
524        let peak = peak_tuple.get(i)?.0;
525        if peak == 0 || v == peak {
526            continue;
527        }
528
529        if has_intermediate_region {
530            let start = start_tuple.get(i)?.0;
531            let end = end_tuple.get(i)?.0;
532            if start > peak || peak > end || (start < 0 && end > 0 && peak != 0) {
533                continue;
534            }
535
536            if v < start || v > end {
537                return Some(header);
538            }
539
540            if v < peak {
541                if peak != start {
542                    scalar *= f32::from(v - start) / f32::from(peak - start);
543                }
544            } else {
545                if peak != end {
546                    scalar *= f32::from(end - v) / f32::from(end - peak);
547                }
548            }
549        } else if v == 0 || v < cmp::min(0, peak) || v > cmp::max(0, peak) {
550            // 'If the instance coordinate is out of range for some axis, then the
551            // region and its associated deltas are not applicable.'
552            return Some(header);
553        } else {
554            scalar *= f32::from(v) / f32::from(peak);
555        }
556    }
557
558    header.scalar = scalar;
559    Some(header)
560}
561
562
563// https://docs.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#packed-point-numbers
564mod packed_points {
565    use crate::parser::{Stream, FromData};
566
567    struct Control(u8);
568
569    impl Control {
570        const POINTS_ARE_WORDS_FLAG: u8 = 0x80;
571        const POINT_RUN_COUNT_MASK: u8 = 0x7F;
572
573        #[inline]
574        fn is_points_are_words(&self) -> bool { self.0 & Self::POINTS_ARE_WORDS_FLAG != 0 }
575
576        // 'Mask for the low 7 bits to provide the number of point values in the run, minus one.'
577        // So we have to add 1.
578        // It will never overflow because of a mask.
579        #[inline]
580        fn run_count(&self) -> u8 { (self.0 & Self::POINT_RUN_COUNT_MASK) + 1 }
581    }
582
583    impl FromData for Control {
584        const SIZE: usize = 1;
585
586        #[inline]
587        fn parse(data: &[u8]) -> Option<Self> { data.get(0).copied().map(Control) }
588    }
589
590
591    #[derive(Clone, Copy, PartialEq)]
592    enum State {
593        Control,
594        ShortPoint,
595        LongPoint,
596    }
597
598    // This structure will be used by the `VariationTuples` stack buffer,
599    // so it has to be as small as possible.
600    // Therefore we cannot use `Stream` and other abstractions.
601    #[derive(Clone, Copy)]
602    pub struct PackedPointsIter<'a> {
603        data: &'a [u8],
604        // u16 is enough, since the maximum number of points is 32767.
605        offset: u16,
606        state: State,
607        points_left: u8,
608    }
609
610    impl<'a> PackedPointsIter<'a> {
611        // The first Option::None indicates a parsing error.
612        // The second Option::None indicates "no points".
613        pub fn new<'b>(s: &'b mut Stream<'a>) -> Option<Option<Self>> {
614            // The total amount of points can be set as one or two bytes
615            // depending on the first bit.
616            let b1: u8 = s.read()?;
617            let mut count = u16::from(b1);
618            if b1 & Control::POINTS_ARE_WORDS_FLAG != 0 {
619                let b2: u8 = s.read()?;
620                count = (u16::from(b1 & Control::POINT_RUN_COUNT_MASK) << 8) | u16::from(b2);
621            }
622
623            if count == 0 {
624                // No points is not an error.
625                return Some(None);
626            }
627
628            let start = s.offset();
629            let tail = s.tail()?;
630
631            // The actual packed points data size is not stored,
632            // so we have to parse the points first to advance the provided stream.
633            // Since deltas will be right after points.
634            let mut i = 0;
635            while i < count {
636                let control: Control = s.read()?;
637                let run_count = u16::from(control.run_count());
638                let is_points_are_words = control.is_points_are_words();
639                // Do not actually parse the number, simply advance.
640                s.advance_checked(if is_points_are_words { 2 } else { 1 } * usize::from(run_count))?;
641                i += run_count;
642            }
643
644            if i == 0 {
645                // No points is not an error.
646                return Some(None);
647            }
648
649            if i > count {
650                // Malformed font.
651                return None;
652            }
653
654            // Check that points data size is smaller than the storage type
655            // used by the iterator.
656            let data_len = s.offset() - start;
657            if data_len > usize::from(core::u16::MAX) {
658                return None;
659            }
660
661            Some(Some(PackedPointsIter {
662                data: &tail[0..data_len],
663                offset: 0,
664                state: State::Control,
665                points_left: 0,
666            }))
667        }
668    }
669
670    impl<'a> Iterator for PackedPointsIter<'a> {
671        type Item = u16;
672
673        fn next(&mut self) -> Option<Self::Item> {
674            if usize::from(self.offset) >= self.data.len() {
675                return None;
676            }
677
678            if self.state == State::Control {
679                let control = Control(self.data[usize::from(self.offset)]);
680                self.offset += 1;
681
682                self.points_left = control.run_count();
683                self.state = if control.is_points_are_words() {
684                    State::LongPoint
685                } else {
686                    State::ShortPoint
687                };
688
689                self.next()
690            } else {
691                let mut s = Stream::new_at(self.data, usize::from(self.offset))?;
692                let point = if self.state == State::LongPoint {
693                    self.offset += 2;
694                    s.read::<u16>()?
695                } else {
696                    self.offset += 1;
697                    u16::from(s.read::<u8>()?)
698                };
699
700                self.points_left -= 1;
701                if self.points_left == 0 {
702                    self.state = State::Control;
703                }
704
705                Some(point)
706            }
707        }
708    }
709
710
711    // The `PackedPointsIter` will return referenced point numbers as deltas.
712    // i.e. 1 2 4 is actually 1 3 7
713    // But this is not very useful in our current algorithm,
714    // so we will convert it once again into:
715    // false true false true false false false true
716    // This way we can iterate glyph points and point numbers in parallel.
717    #[derive(Clone, Copy)]
718    pub struct SetPointsIter<'a> {
719        iter: PackedPointsIter<'a>,
720        unref_count: u16,
721    }
722
723    impl<'a> SetPointsIter<'a> {
724        #[inline]
725        pub fn new(mut iter: PackedPointsIter<'a>) -> Self {
726            let unref_count = iter.next().unwrap_or(0);
727            SetPointsIter { iter, unref_count }
728        }
729
730        #[inline]
731        pub fn restart(self) -> Self {
732            let mut iter = self.iter.clone();
733            iter.offset = 0;
734            iter.state = State::Control;
735            iter.points_left = 0;
736
737            let unref_count = iter.next().unwrap_or(0);
738            SetPointsIter { iter, unref_count }
739        }
740    }
741
742    impl<'a> Iterator for SetPointsIter<'a> {
743        type Item = bool;
744
745        #[inline]
746        fn next(&mut self) -> Option<Self::Item> {
747            if self.unref_count != 0 {
748                self.unref_count -= 1;
749                return Some(false);
750            }
751
752            if let Some(unref_count) = self.iter.next() {
753                self.unref_count = unref_count;
754                if self.unref_count != 0 {
755                    self.unref_count -= 1;
756                }
757            }
758
759            // Iterator will be returning `Some(true)` after "finished".
760            // This is because this iterator will be zipped with the `glyf::GlyphPointsIter`
761            // and the number of glyph points can be larger than the amount of set points.
762            // Anyway, this is a non-issue in a well-formed font.
763            Some(true)
764        }
765    }
766
767
768    #[cfg(test)]
769    mod tests {
770        use super::*;
771
772        struct NewControl {
773            deltas_are_words: bool,
774            run_count: u8,
775        }
776
777        fn gen_control(control: NewControl) -> u8 {
778            assert!(control.run_count > 0, "run count cannot be zero");
779
780            let mut n = 0;
781            if control.deltas_are_words { n |= 0x80; }
782            n |= (control.run_count - 1) & 0x7F;
783            n
784        }
785
786        #[test]
787        fn empty() {
788            let mut s = Stream::new(&[]);
789            assert!(PackedPointsIter::new(&mut s).is_none());
790        }
791
792        #[test]
793        fn single_zero_control() {
794            let mut s = Stream::new(&[0]);
795            assert!(PackedPointsIter::new(&mut s).unwrap().is_none());
796        }
797
798        #[test]
799        fn single_point() {
800            let data = vec![
801                1, // total count
802                gen_control(NewControl { deltas_are_words: false, run_count: 1 }),
803                1
804            ];
805
806            let points_iter = PackedPointsIter::new(&mut Stream::new(&data)).unwrap().unwrap();
807            let mut iter = SetPointsIter::new(points_iter);
808            assert_eq!(iter.next().unwrap(), false);
809            assert_eq!(iter.next().unwrap(), true);
810            assert_eq!(iter.next().unwrap(), true); // Endlessly true.
811        }
812
813        #[test]
814        fn set_0_and_2() {
815            let data = vec![
816                2, // total count
817                gen_control(NewControl { deltas_are_words: false, run_count: 2 }),
818                0, 2
819            ];
820
821            let points_iter = PackedPointsIter::new(&mut Stream::new(&data)).unwrap().unwrap();
822            let mut iter = SetPointsIter::new(points_iter);
823            assert_eq!(iter.next().unwrap(), true);
824            assert_eq!(iter.next().unwrap(), false);
825            assert_eq!(iter.next().unwrap(), true);
826            assert_eq!(iter.next().unwrap(), true); // Endlessly true.
827        }
828
829        #[test]
830        fn set_1_and_2() {
831            let data = vec![
832                2, // total count
833                gen_control(NewControl { deltas_are_words: false, run_count: 2 }),
834                1, 1
835            ];
836
837            let points_iter = PackedPointsIter::new(&mut Stream::new(&data)).unwrap().unwrap();
838            let mut iter = SetPointsIter::new(points_iter);
839            assert_eq!(iter.next().unwrap(), false);
840            assert_eq!(iter.next().unwrap(), true);
841            assert_eq!(iter.next().unwrap(), true);
842            assert_eq!(iter.next().unwrap(), true); // Endlessly true.
843        }
844
845        #[test]
846        fn set_1_and_3() {
847            let data = vec![
848                2, // total count
849                gen_control(NewControl { deltas_are_words: false, run_count: 2 }),
850                1, 2
851            ];
852
853            let points_iter = PackedPointsIter::new(&mut Stream::new(&data)).unwrap().unwrap();
854            let mut iter = SetPointsIter::new(points_iter);
855            assert_eq!(iter.next().unwrap(), false);
856            assert_eq!(iter.next().unwrap(), true);
857            assert_eq!(iter.next().unwrap(), false);
858            assert_eq!(iter.next().unwrap(), true);
859            assert_eq!(iter.next().unwrap(), true); // Endlessly true.
860        }
861
862        #[test]
863        fn set_2_5_7() {
864            let data = vec![
865                3, // total count
866                gen_control(NewControl { deltas_are_words: false, run_count: 3 }),
867                2, 3, 2
868            ];
869
870            let points_iter = PackedPointsIter::new(&mut Stream::new(&data)).unwrap().unwrap();
871            let mut iter = SetPointsIter::new(points_iter);
872            assert_eq!(iter.next().unwrap(), false);
873            assert_eq!(iter.next().unwrap(), false);
874            assert_eq!(iter.next().unwrap(), true);
875            assert_eq!(iter.next().unwrap(), false);
876            assert_eq!(iter.next().unwrap(), false);
877            assert_eq!(iter.next().unwrap(), true);
878            assert_eq!(iter.next().unwrap(), false);
879            assert_eq!(iter.next().unwrap(), true);
880            assert_eq!(iter.next().unwrap(), true); // Endlessly true.
881        }
882
883        #[test]
884        fn more_than_127_points() {
885            let mut data = vec![];
886            // total count
887            data.push(Control::POINTS_ARE_WORDS_FLAG);
888            data.push(150);
889
890            data.push(gen_control(NewControl { deltas_are_words: false, run_count: 100 }));
891            for _ in 0..100 {
892                data.push(2);
893            }
894            data.push(gen_control(NewControl { deltas_are_words: false, run_count: 50 }));
895            for _ in 0..50 {
896                data.push(2);
897            }
898
899            let points_iter = PackedPointsIter::new(&mut Stream::new(&data)).unwrap().unwrap();
900            let mut iter = SetPointsIter::new(points_iter);
901            assert_eq!(iter.next().unwrap(), false);
902            for _ in 0..150 {
903                assert_eq!(iter.next().unwrap(), false);
904                assert_eq!(iter.next().unwrap(), true);
905            }
906            assert_eq!(iter.next().unwrap(), true);
907            assert_eq!(iter.next().unwrap(), true); // Endlessly true.
908        }
909
910        #[test]
911        fn long_points() {
912            let data = vec![
913                2, // total count
914                gen_control(NewControl { deltas_are_words: true, run_count: 2 }),
915                0, 2, 0, 3
916            ];
917
918            let points_iter = PackedPointsIter::new(&mut Stream::new(&data)).unwrap().unwrap();
919            let mut iter = SetPointsIter::new(points_iter);
920            assert_eq!(iter.next().unwrap(), false);
921            assert_eq!(iter.next().unwrap(), false);
922            assert_eq!(iter.next().unwrap(), true);
923            assert_eq!(iter.next().unwrap(), false);
924            assert_eq!(iter.next().unwrap(), false);
925            assert_eq!(iter.next().unwrap(), true);
926            assert_eq!(iter.next().unwrap(), true); // Endlessly true.
927        }
928
929        #[test]
930        fn multiple_runs() {
931            let data = vec![
932                5, // total count
933                gen_control(NewControl { deltas_are_words: true, run_count: 2 }),
934                0, 2, 0, 3,
935                gen_control(NewControl { deltas_are_words: false, run_count: 3 }),
936                2, 3, 2
937            ];
938
939            let points_iter = PackedPointsIter::new(&mut Stream::new(&data)).unwrap().unwrap();
940            let mut iter = SetPointsIter::new(points_iter);
941            assert_eq!(iter.next().unwrap(), false);
942            assert_eq!(iter.next().unwrap(), false);
943            assert_eq!(iter.next().unwrap(), true);
944            assert_eq!(iter.next().unwrap(), false);
945            assert_eq!(iter.next().unwrap(), false);
946            assert_eq!(iter.next().unwrap(), true);
947            assert_eq!(iter.next().unwrap(), false);
948            assert_eq!(iter.next().unwrap(), true);
949            assert_eq!(iter.next().unwrap(), false);
950            assert_eq!(iter.next().unwrap(), false);
951            assert_eq!(iter.next().unwrap(), true);
952            assert_eq!(iter.next().unwrap(), false);
953            assert_eq!(iter.next().unwrap(), true);
954            assert_eq!(iter.next().unwrap(), true); // Endlessly true.
955        }
956
957        #[test]
958        fn runs_overflow() {
959            // TrueType allows up to 32767 points.
960            let data = vec![0xFF; 0xFFFF * 2];
961            assert!(PackedPointsIter::new(&mut Stream::new(&data)).is_none());
962        }
963    }
964}
965
966use packed_points::*;
967
968
969// https://docs.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#packed-deltas
970mod packed_deltas {
971    use crate::parser::Stream;
972
973    struct Control(u8);
974
975    impl Control {
976        const DELTAS_ARE_ZERO_FLAG: u8 = 0x80;
977        const DELTAS_ARE_WORDS_FLAG: u8 = 0x40;
978        const DELTA_RUN_COUNT_MASK: u8 = 0x3F;
979
980        #[inline]
981        fn is_deltas_are_zero(&self) -> bool { self.0 & Self::DELTAS_ARE_ZERO_FLAG != 0 }
982
983        #[inline]
984        fn is_deltas_are_words(&self) -> bool { self.0 & Self::DELTAS_ARE_WORDS_FLAG != 0 }
985
986        // 'Mask for the low 6 bits to provide the number of delta values in the run, minus one.'
987        // So we have to add 1.
988        // It will never overflow because of a mask.
989        #[inline]
990        fn run_count(&self) -> u8 { (self.0 & Self::DELTA_RUN_COUNT_MASK) + 1 }
991    }
992
993
994    #[derive(Clone, Copy, PartialEq, Debug)]
995    enum State {
996        Control,
997        ZeroDelta,
998        ShortDelta,
999        LongDelta,
1000    }
1001
1002    impl Default for State {
1003        #[inline]
1004        fn default() -> Self {
1005            State::Control
1006        }
1007    }
1008
1009
1010    #[derive(Clone, Copy, Default)]
1011    struct RunState {
1012        data_offset: u16,
1013        state: State,
1014        run_deltas_left: u8,
1015    }
1016
1017    impl RunState {
1018        fn next(&mut self, data: &[u8], scalar: f32) -> Option<f32> {
1019            if self.state == State::Control {
1020                if usize::from(self.data_offset) == data.len() {
1021                    return None;
1022                }
1023
1024                let control = Control(Stream::read_at::<u8>(data, usize::from(self.data_offset))?);
1025                self.data_offset += 1;
1026
1027                self.run_deltas_left = control.run_count();
1028                self.state = if control.is_deltas_are_zero() {
1029                    State::ZeroDelta
1030                } else if control.is_deltas_are_words() {
1031                    State::LongDelta
1032                } else {
1033                    State::ShortDelta
1034                };
1035
1036                self.next(data, scalar)
1037            } else {
1038                let mut s = Stream::new_at(data, usize::from(self.data_offset))?;
1039                let delta = if self.state == State::LongDelta {
1040                    self.data_offset += 2;
1041                    f32::from(s.read::<i16>()?) * scalar
1042                } else if self.state == State::ZeroDelta {
1043                    0.0
1044                } else {
1045                    self.data_offset += 1;
1046                    f32::from(s.read::<i8>()?) * scalar
1047                };
1048
1049                self.run_deltas_left -= 1;
1050                if self.run_deltas_left == 0 {
1051                    self.state = State::Control;
1052                }
1053
1054                Some(delta)
1055            }
1056        }
1057    }
1058
1059
1060    // This structure will be used by the `VariationTuples` stack buffer,
1061    // so it has to be as small as possible.
1062    // Therefore we cannot use `Stream` and other abstractions.
1063    #[derive(Clone, Copy, Default)]
1064    pub struct PackedDeltasIter<'a> {
1065        data: &'a [u8],
1066        x_run: RunState,
1067        y_run: RunState,
1068
1069        /// A total number of deltas per axis.
1070        ///
1071        /// Required only by restart()
1072        total_count: u16,
1073
1074        scalar: f32,
1075    }
1076
1077    impl<'a> PackedDeltasIter<'a> {
1078        /// `count` indicates a number of delta pairs.
1079        pub fn new(scalar: f32, count: u16, data: &'a [u8]) -> Self {
1080            debug_assert!(core::mem::size_of::<PackedDeltasIter>() <= 32);
1081
1082            let mut iter = PackedDeltasIter {
1083                data,
1084                total_count: count,
1085                scalar,
1086                ..PackedDeltasIter::default()
1087            };
1088
1089            // 'The packed deltas are arranged with all of the deltas for X coordinates first,
1090            // followed by the deltas for Y coordinates.'
1091            // So we have to skip X deltas in the Y deltas iterator.
1092            //
1093            // Note that Y deltas doesn't necessarily start with a Control byte
1094            // and can actually start in the middle of the X run.
1095            // So we can't simply split the input data in half
1096            // and process those chunks separately.
1097            for _ in 0..count {
1098                iter.y_run.next(data, scalar);
1099            }
1100
1101            iter
1102        }
1103
1104        #[inline]
1105        pub fn restart(self) -> Self {
1106            PackedDeltasIter::new(self.scalar, self.total_count, self.data)
1107        }
1108
1109        #[inline]
1110        pub fn next(&mut self) -> Option<(f32, f32)> {
1111            let x = self.x_run.next(self.data, self.scalar)?;
1112            let y = self.y_run.next(self.data, self.scalar)?;
1113            Some((x, y))
1114        }
1115    }
1116
1117    #[cfg(test)]
1118    mod tests {
1119        use super::*;
1120
1121        struct NewControl {
1122            deltas_are_zero: bool,
1123            deltas_are_words: bool,
1124            run_count: u8,
1125        }
1126
1127        fn gen_control(control: NewControl) -> u8 {
1128            assert!(control.run_count > 0, "run count cannot be zero");
1129
1130            let mut n = 0;
1131            if control.deltas_are_zero  { n |= 0x80; }
1132            if control.deltas_are_words { n |= 0x40; }
1133            n |= (control.run_count - 1) & 0x3F;
1134            n
1135        }
1136
1137        #[test]
1138        fn empty() {
1139            let mut iter = PackedDeltasIter::new(1.0, 1, &[]);
1140            assert!(iter.next().is_none());
1141        }
1142
1143        #[test]
1144        fn single_delta() {
1145            let data = vec![
1146                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: false, run_count: 2 }),
1147                2, 3
1148            ];
1149
1150            let mut iter = PackedDeltasIter::new(1.0, 1, &data);
1151            assert_eq!(iter.next().unwrap(), (2.0, 3.0));
1152            assert!(iter.next().is_none());
1153        }
1154
1155        #[test]
1156        fn two_deltas() {
1157            let data = vec![
1158                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: false, run_count: 4 }),
1159                2, 3, 4, 5,
1160            ];
1161
1162            let mut iter = PackedDeltasIter::new(1.0, 2, &data);
1163            // Remember that X deltas are defined first.
1164            assert_eq!(iter.next().unwrap(), (2.0, 4.0));
1165            assert_eq!(iter.next().unwrap(), (3.0, 5.0));
1166            assert!(iter.next().is_none());
1167        }
1168
1169        #[test]
1170        fn single_long_delta() {
1171            let data = vec![
1172                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: true, run_count: 2 }),
1173                0, 2, 0, 3
1174            ];
1175
1176            let mut iter = PackedDeltasIter::new(1.0, 1, &data);
1177            assert_eq!(iter.next().unwrap(), (2.0, 3.0));
1178            assert!(iter.next().is_none());
1179        }
1180
1181        #[test]
1182        fn zeros() {
1183            let data = vec![
1184                gen_control(NewControl { deltas_are_zero: true, deltas_are_words: false, run_count: 4 }),
1185            ];
1186
1187            let mut iter = PackedDeltasIter::new(1.0, 2, &data);
1188            assert_eq!(iter.next().unwrap(), (0.0, 0.0));
1189            assert_eq!(iter.next().unwrap(), (0.0, 0.0));
1190            assert!(iter.next().is_none());
1191        }
1192
1193        #[test]
1194        fn zero_words() {
1195            // When `deltas_are_zero` is set, `deltas_are_words` should be ignored.
1196
1197            let data = vec![
1198                gen_control(NewControl { deltas_are_zero: true, deltas_are_words: true, run_count: 4 }),
1199            ];
1200
1201            let mut iter = PackedDeltasIter::new(1.0, 2, &data);
1202            assert_eq!(iter.next().unwrap(), (0.0, 0.0));
1203            assert_eq!(iter.next().unwrap(), (0.0, 0.0));
1204            assert!(iter.next().is_none());
1205        }
1206
1207        #[test]
1208        fn zero_runs() {
1209            let data = vec![
1210                gen_control(NewControl { deltas_are_zero: true, deltas_are_words: false, run_count: 2 }),
1211                gen_control(NewControl { deltas_are_zero: true, deltas_are_words: false, run_count: 4 }),
1212                gen_control(NewControl { deltas_are_zero: true, deltas_are_words: false, run_count: 6 }),
1213            ];
1214
1215            let mut iter = PackedDeltasIter::new(1.0, 6, &data);
1216            // First run.
1217            assert_eq!(iter.next().unwrap(), (0.0, 0.0));
1218            // Second run.
1219            assert_eq!(iter.next().unwrap(), (0.0, 0.0));
1220            assert_eq!(iter.next().unwrap(), (0.0, 0.0));
1221            // Third run.
1222            assert_eq!(iter.next().unwrap(), (0.0, 0.0));
1223            assert_eq!(iter.next().unwrap(), (0.0, 0.0));
1224            assert_eq!(iter.next().unwrap(), (0.0, 0.0));
1225            assert!(iter.next().is_none());
1226        }
1227
1228        #[test]
1229        fn delta_after_zeros() {
1230            let data = vec![
1231                gen_control(NewControl { deltas_are_zero: true, deltas_are_words: false, run_count: 2 }),
1232                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: false, run_count: 2 }),
1233                2, 3
1234            ];
1235
1236            let mut iter = PackedDeltasIter::new(1.0, 2, &data);
1237            assert_eq!(iter.next().unwrap(), (0.0, 2.0));
1238            assert_eq!(iter.next().unwrap(), (0.0, 3.0));
1239            assert!(iter.next().is_none());
1240        }
1241
1242        #[test]
1243        fn unexpected_end_of_data_1() {
1244            let data = vec![
1245                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: false, run_count: 2 }),
1246            ];
1247
1248            let mut iter = PackedDeltasIter::new(1.0, 1, &data);
1249            assert!(iter.next().is_none());
1250        }
1251
1252        #[test]
1253        fn unexpected_end_of_data_2() {
1254            // Only X is set.
1255
1256            let data = vec![
1257                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: false, run_count: 2 }),
1258                1
1259            ];
1260
1261            let mut iter = PackedDeltasIter::new(1.0, 1, &data);
1262            assert!(iter.next().is_none());
1263        }
1264
1265        #[test]
1266        fn unexpected_end_of_data_3() {
1267            let data = vec![
1268                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: true, run_count: 2 }),
1269            ];
1270
1271            let mut iter = PackedDeltasIter::new(1.0, 1, &data);
1272            assert!(iter.next().is_none());
1273        }
1274
1275        #[test]
1276        fn unexpected_end_of_data_4() {
1277            // X data is too short.
1278
1279            let data = vec![
1280                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: true, run_count: 2 }),
1281                1
1282            ];
1283
1284            let mut iter = PackedDeltasIter::new(1.0, 1, &data);
1285            assert!(iter.next().is_none());
1286        }
1287
1288        #[test]
1289        fn unexpected_end_of_data_6() {
1290            // Only X is set.
1291
1292            let data = vec![
1293                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: true, run_count: 2 }),
1294                0, 1
1295            ];
1296
1297            let mut iter = PackedDeltasIter::new(1.0, 1, &data);
1298            assert!(iter.next().is_none());
1299        }
1300
1301        #[test]
1302        fn unexpected_end_of_data_7() {
1303            // Y data is too short.
1304
1305            let data = vec![
1306                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: true, run_count: 2 }),
1307                0, 1, 0
1308            ];
1309
1310            let mut iter = PackedDeltasIter::new(1.0, 1, &data);
1311            assert!(iter.next().is_none());
1312        }
1313
1314        #[test]
1315        fn single_run() {
1316            let data = vec![
1317                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: false, run_count: 1 }),
1318                2, 3
1319            ];
1320
1321            let mut iter = PackedDeltasIter::new(1.0, 1, &data);
1322            assert!(iter.next().is_none());
1323        }
1324
1325        #[test]
1326        fn too_many_pairs() {
1327            let data = vec![
1328                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: false, run_count: 2 }),
1329                2, 3
1330            ];
1331
1332            // We have only one pair, not 10.
1333            let mut iter = PackedDeltasIter::new(1.0, 10, &data);
1334            assert!(iter.next().is_none());
1335        }
1336
1337        #[test]
1338        fn invalid_number_of_pairs() {
1339            let data = vec![
1340                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: false, run_count: 2 }),
1341                2, 3, 4, 5, 6, 7,
1342            ];
1343
1344            // We have 3 pairs, not 4.
1345            // We don't actually check this, since it will be very expensive.
1346            // And it should not happen in a well-formed font anyway.
1347            // So as long as it doesn't panic - we are fine.
1348            let mut iter = PackedDeltasIter::new(1.0, 4, &data);
1349            assert_eq!(iter.next().unwrap(), (2.0, 7.0));
1350            assert!(iter.next().is_none());
1351        }
1352
1353        #[test]
1354        fn mixed_runs() {
1355            let data = vec![
1356                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: false, run_count: 3 }),
1357                2, 3, 4,
1358                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: true, run_count: 2 }),
1359                0, 5, 0, 6,
1360                gen_control(NewControl { deltas_are_zero: true, deltas_are_words: false, run_count: 1 }),
1361            ];
1362
1363            let mut iter = PackedDeltasIter::new(1.0, 3, &data);
1364            assert_eq!(iter.next().unwrap(), (2.0, 5.0));
1365            assert_eq!(iter.next().unwrap(), (3.0, 6.0));
1366            assert_eq!(iter.next().unwrap(), (4.0, 0.0));
1367            assert!(iter.next().is_none());
1368        }
1369
1370        #[test]
1371        fn non_default_scalar() {
1372            let data = vec![
1373                gen_control(NewControl { deltas_are_zero: false, deltas_are_words: false, run_count: 2 }),
1374                2, 3
1375            ];
1376
1377            let mut iter = PackedDeltasIter::new(0.5, 1, &data);
1378            assert_eq!(iter.next().unwrap(), (1.0, 1.5));
1379            assert!(iter.next().is_none());
1380        }
1381
1382        #[test]
1383        fn runs_overflow() {
1384            let data = vec![0xFF; 0xFFFF];
1385            let mut iter = PackedDeltasIter::new(1.0, 0xFFFF, &data);
1386            // As long as it doesn't panic - we are fine.
1387            assert_eq!(iter.next().unwrap(), (0.0, 0.0));
1388        }
1389    }
1390}
1391
1392use packed_deltas::PackedDeltasIter;
1393
1394
1395/// Infer unreferenced deltas.
1396///
1397/// A font can define deltas only for specific points, to reduce the file size.
1398/// In this case, we have to infer undefined/unreferenced deltas manually,
1399/// depending on the context.
1400///
1401/// This is already a pretty complex task, since deltas should be resolved
1402/// only inside the current contour (do not confuse with component).
1403/// And during resolving we can actually wrap around the contour.
1404/// So if there is no deltas after the current one, we have to use
1405/// the first delta of the current contour instead.
1406/// Same goes for the previous delta. If there are no deltas
1407/// before the current one, we have to use the last one in the current contour.
1408///
1409/// And in case of `ttf-parser` everything is becoming even more complex,
1410/// since we don't actually have a list of points and deltas, only iterators.
1411/// Because of `ttf-parser`'s allocation free policy.
1412/// Which makes the code even more complicated.
1413///
1414/// https://docs.microsoft.com/en-us/typography/opentype/spec/gvar#inferred-deltas-for-un-referenced-point-numbers
1415fn infer_deltas(
1416    tuple: &VariationTuple,
1417    points_set: SetPointsIter,
1418    // A points iterator that starts after the current point.
1419    points: glyf::GlyphPointsIter,
1420    // A points iterator that starts from the first point in the glyph.
1421    all_points: glyf::GlyphPointsIter,
1422    curr_point: glyf::GlyphPoint,
1423) -> (f32, f32) {
1424    let mut current_contour = points.current_contour();
1425    if curr_point.last_point && current_contour != 0 {
1426        // When we parsed the last point of a contour,
1427        // an iterator had switched to the next contour.
1428        // So we have to move to the previous one.
1429        current_contour -= 1;
1430    }
1431
1432    let prev_point = if let Some(prev_point) = tuple.prev_point {
1433        // If a contour already had a delta - just use it.
1434        prev_point
1435    } else {
1436        // If not, find the last point with delta in the current contour.
1437        let mut last_point = None;
1438        let mut deltas = tuple.deltas.clone();
1439        for (point, is_set) in points.clone().zip(points_set.clone()) {
1440            if is_set {
1441                if let Some((x_delta, y_delta)) = deltas.next() {
1442                    last_point = Some(PointAndDelta {
1443                        x: point.x,
1444                        y: point.y,
1445                        x_delta,
1446                        y_delta,
1447                    });
1448                }
1449            }
1450
1451            if point.last_point {
1452                break;
1453            }
1454        }
1455
1456        // If there is no last point, there are no deltas.
1457        match last_point {
1458            Some(p) => p,
1459            None => return (0.0, 0.0),
1460        }
1461    };
1462
1463    let mut next_point = None;
1464    if !curr_point.last_point {
1465        // If the current point is not the last one in the contour,
1466        // find the first set delta in the current contour.
1467        let mut deltas = tuple.deltas.clone();
1468        for (point, is_set) in points.clone().zip(points_set.clone()) {
1469            if is_set {
1470                if let Some((x_delta, y_delta)) = deltas.next() {
1471                    next_point = Some(PointAndDelta {
1472                        x: point.x,
1473                        y: point.y,
1474                        x_delta,
1475                        y_delta,
1476                    });
1477                }
1478
1479                break;
1480            }
1481
1482            if point.last_point {
1483                break;
1484            }
1485        }
1486    }
1487
1488    if next_point.is_none() {
1489        // If there were no deltas after the current point,
1490        // restart from the start of the contour.
1491        //
1492        // This is probably the most expensive branch,
1493        // but nothing we can do about it since `glyf`/`gvar` data structure
1494        // doesn't allow implementing a reverse iterator.
1495        // So we have to parse everything once again.
1496
1497        let mut all_points = all_points.clone();
1498        let mut deltas = tuple.deltas.clone().restart();
1499        let mut points_set = points_set.clone().restart();
1500
1501        let mut contour = 0;
1502        while let (Some(point), Some(is_set)) = (all_points.next(), points_set.next()) {
1503            // First, we have to skip already processed contours.
1504            if contour != current_contour {
1505                if is_set {
1506                    let _ = deltas.next();
1507                }
1508
1509                contour = all_points.current_contour();
1510                continue;
1511            }
1512
1513            if is_set {
1514                let (x_delta, y_delta) = deltas.next().unwrap_or((0.0, 0.0));
1515                next_point = Some(PointAndDelta {
1516                    x: point.x,
1517                    y: point.y,
1518                    x_delta,
1519                    y_delta,
1520                });
1521
1522                break;
1523            }
1524
1525            if point.last_point {
1526                break;
1527            }
1528        }
1529    }
1530
1531    // If there is no next point, there are no deltas.
1532    let next_point = match next_point {
1533        Some(p) => p,
1534        None => return (0.0, 0.0),
1535    };
1536
1537    let dx = infer_delta(prev_point.x, curr_point.x, next_point.x,
1538                         prev_point.x_delta, next_point.x_delta);
1539
1540    let dy = infer_delta(prev_point.y, curr_point.y, next_point.y,
1541                         prev_point.y_delta, next_point.y_delta);
1542
1543    (dx, dy)
1544}
1545
1546fn infer_delta(
1547    prev_point: i16,
1548    target_point: i16,
1549    next_point: i16,
1550    prev_delta: f32,
1551    next_delta: f32,
1552) -> f32 {
1553    if prev_point == next_point {
1554        if prev_delta == next_delta { prev_delta } else { 0.0 }
1555    } else if target_point <= prev_point.min(next_point) {
1556        if prev_point < next_point { prev_delta } else { next_delta }
1557    } else if target_point >= prev_point.max(next_point) {
1558        if prev_point > next_point { prev_delta } else { next_delta }
1559    } else {
1560        // 'Target point coordinate is between adjacent point coordinates.'
1561        //
1562        // 'Target point delta is derived from the adjacent point deltas
1563        // using linear interpolation.'
1564        let d =   f32::from(try_opt_or!(target_point.checked_sub(prev_point), 0.0))
1565                / f32::from(try_opt_or!(next_point.checked_sub(prev_point), 0.0));
1566        (1.0 - d) * prev_delta + d * next_delta
1567    }
1568}