oxideav-ttf 0.1.8

Pure-Rust TrueType font parser for the oxideav framework — sfnt + cmap + glyf + hmtx + GSUB ligatures + GPOS kerning
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
//! `MVAR` — Metrics Variations Table.
//!
//! Spec: ISO/IEC 14496-22:2019 §7.3.6 ("MVAR – Metrics variations
//! table"). The shared `ItemVariationStore` substructure used by MVAR
//! (and by HVAR / VVAR / GDEF) is specified in §7.2.3.
//!
//! MVAR provides per-instance adjustments for font-wide metric values
//! (e.g. `OS/2.sxHeight`, `OS/2.sCapHeight`, `hhea.caretSlopeRise`)
//! in a variable font. Each adjustment is keyed by a four-byte tag
//! ('xhgt', 'cpht', 'unds', …) and resolved through an embedded
//! `ItemVariationStore` whose region-scaled deltas are summed against
//! the current normalised coordinate vector.
//!
//! ## Header layout
//!
//! ```text
//!   0  / 2 / majorVersion          (== 1)
//!   2  / 2 / minorVersion          (== 0)
//!   4  / 2 / (reserved, set to 0)
//!   6  / 2 / valueRecordSize       (== 8 in v1; future minor versions
//!                                   may grow it — we use this for the
//!                                   record stride)
//!   8  / 2 / valueRecordCount
//!  10  / 2 / itemVariationStoreOffset  (relative to MVAR start; zero
//!                                       allowed only when count == 0)
//!  12  / .. / valueRecords[valueRecordCount]
//! ```
//!
//! Each `ValueRecord` (`valueRecordSize` bytes, minimum 8):
//!
//! ```text
//!   0 / 4 / valueTag                       (e.g. b"xhgt")
//!   4 / 2 / deltaSetOuterIndex             (into IVS subtable array)
//!   6 / 2 / deltaSetInnerIndex             (into the delta-set row)
//! ```
//!
//! Records MUST be sorted in binary order of `valueTag`; we trust the
//! ordering for binary-search lookup but do not enforce it.
//!
//! ## ItemVariationStore (§7.2.3)
//!
//! ```text
//!   0 / 2 / format                          (== 1)
//!   2 / 4 / variationRegionListOffset       (relative to IVS start)
//!   6 / 2 / itemVariationDataCount
//!   8 / 4*N / itemVariationDataOffsets[N]   (each relative to IVS start)
//! ```
//!
//! The VariationRegionList:
//!
//! ```text
//!   0 / 2 / axisCount                       (== fvar.axisCount)
//!   2 / 2 / regionCount
//!   4 / 6*axisCount*regionCount / regions   (F2DOT14 start/peak/end
//!                                            per axis, per region)
//! ```
//!
//! Each `ItemVariationData` subtable:
//!
//! ```text
//!   0 / 2 / itemCount                       (delta-set rows)
//!   2 / 2 / shortDeltaCount                 (cols using int16; rest int8)
//!   4 / 2 / regionIndexCount                (cols)
//!   6 / 2*regionIndexCount / regionIndexes[]
//!   .. / itemCount * (shortDeltaCount*2 + (regionIndexCount-shortDeltaCount))
//!        / deltaSets[itemCount][regionIndexCount]
//! ```
//!
//! ## Processing (§7.3.6.2 / §7.1)
//!
//! Given a normalised coordinate vector `n[axisCount]`:
//!
//! 1. Locate the value record for the requested tag.
//! 2. Pick `IVD = subtables[outer]`, then `row = IVD.deltaSets[inner]`.
//! 3. For each column `k` of the row, the region scalar is the
//!    product of per-axis scalars for `IVD.regionIndexes[k]`. A
//!    per-axis scalar is `1` if `peak == 0` (axis ignored), `0` if
//!    `n` lies outside `[start, end]` or has opposite sign to `peak`,
//!    `(n - start) / (peak - start)` on the rising side, and
//!    `(end - n) / (end - peak)` on the falling side.
//! 4. The interpolated adjustment is `Σₖ scalar(k) * delta(k)` (the
//!    delta is `int16` for the first `shortDeltaCount` columns and
//!    `int8` for the rest).
//!
//! Future-version compatibility: `valueRecordSize` is honoured as the
//! stride so a minor-version bump that grows the record (with the
//! first 8 bytes preserved per the spec note in §7.3.6.1) parses
//! correctly; the trailing bytes are ignored.

use crate::parser::{read_i16, read_i32, read_i8, read_u16, read_u32};
use crate::Error;

/// One region axis as a `(start, peak, end)` triple in F2DOT14 (decoded
/// to f32). One per variation axis declared in `fvar`.
type RegionAxis = (f32, f32, f32);
/// One `VariationRegion` — a slice of `(start, peak, end)` per axis.
type Region = Vec<RegionAxis>;

/// Bound on the value-record count we will parse. Real-world MVAR
/// tables stay in low double digits (the §7.3.6.3 registry defines
/// 39 standard tags); the cap exists purely to bound work on a
/// malformed header.
const MAX_VALUE_RECORDS: u16 = 2048;
/// Bound on the IVS subtable count. The spec allows up to 65 536;
/// fonts in practice ship 1.
const MAX_IVD_SUBTABLES: u16 = 4096;
/// Bound on per-IVS region count. The staged common-formats chapter
/// caps `regionCount` below 32,768 ("must be less than 32,768" —
/// the high-order bit is reserved and must be cleared).
const MAX_REGIONS: u16 = 0x7FFF;

/// Parsed MVAR table.
#[derive(Debug, Clone)]
pub struct MvarTable {
    /// Value records, in document order. Each is
    /// `(tag, outer_index, inner_index)`. Sorted by tag in the source
    /// file (binary-search-friendly) but we do not re-sort.
    records: Vec<([u8; 4], u16, u16)>,
    /// The IVS substructure parsed out of the table body. `None` only
    /// when `valueRecordCount == 0` (no metric variations published).
    ivs: Option<ItemVariationStore>,
}

/// Parsed `ItemVariationStore` (§7.2.3).
#[derive(Debug, Clone)]
pub struct ItemVariationStore {
    /// `fvar.axisCount` repeated for self-validation; callers should
    /// pass an equal-length normalised vector when computing scalars.
    axis_count: u16,
    /// All regions referenced by any subtable. Each region is one
    /// `(start, peak, end)` triple per axis, in axis order.
    regions: Vec<Region>,
    /// Item-variation data subtables.
    subtables: Vec<ItemVariationData>,
}

/// `wordDeltaCount` flag: "word" deltas are long (`int32`) and the
/// short deltas `int16`, instead of `int16` + `int8`. Per the staged
/// common-formats chapter this is only meaningful for top-level
/// tables carrying variable 32-bit values (currently COLR).
const LONG_WORDS_FLAG: u16 = 0x8000;
/// `wordDeltaCount` mask for the count of "word"-representation
/// deltas leading each delta-set row.
const WORD_DELTA_COUNT_MASK: u16 = 0x7FFF;

#[derive(Debug, Clone)]
struct ItemVariationData {
    /// Indices into the parent `ItemVariationStore::regions` array,
    /// one per column of `delta_sets`.
    region_indexes: Vec<u16>,
    /// `delta_sets[row][col]` — the dense delta matrix. The split
    /// between the leading "word" columns and the trailing "short"
    /// columns from `wordDeltaCount` (`int16`+`int8`, or
    /// `int32`+`int16` under the `LONG_WORDS` flag) is consumed at
    /// parse time; widened values are stored as `i32`.
    delta_sets: Vec<Vec<i32>>,
}

impl ItemVariationData {
    /// A placeholder for a NULL `itemVariationDataOffsets` entry —
    /// the staged chapter's "there is no item variation data subtable
    /// for that index" case. Any inner index against it resolves to
    /// no variation.
    fn null() -> Self {
        Self {
            region_indexes: Vec::new(),
            delta_sets: Vec::new(),
        }
    }
}

impl MvarTable {
    pub fn parse(bytes: &[u8]) -> Result<Self, Error> {
        if bytes.len() < 12 {
            return Err(Error::UnexpectedEof);
        }
        let major = read_u16(bytes, 0)?;
        let _minor = read_u16(bytes, 2)?;
        // bytes[4..6]: reserved
        let value_record_size = read_u16(bytes, 6)?;
        let value_record_count = read_u16(bytes, 8)?;
        let ivs_offset = read_u16(bytes, 10)? as usize;

        if major != 1 {
            return Err(Error::BadStructure("MVAR majorVersion != 1"));
        }
        if value_record_count == 0 {
            // No metric variations published — return an empty
            // table; lookups will all return `None`.
            return Ok(Self {
                records: Vec::new(),
                ivs: None,
            });
        }
        if value_record_size < 8 {
            return Err(Error::BadStructure("MVAR valueRecordSize < 8"));
        }
        if value_record_count > MAX_VALUE_RECORDS {
            return Err(Error::BadStructure("MVAR valueRecordCount exceeds cap"));
        }
        // valueRecordCount > 0 ⇒ ivs_offset must be > 0 per §7.3.6.1.
        if ivs_offset == 0 || ivs_offset > bytes.len() {
            return Err(Error::BadOffset);
        }

        let stride = value_record_size as usize;
        let total_records_bytes = (value_record_count as usize)
            .checked_mul(stride)
            .ok_or(Error::BadOffset)?;
        if 12usize
            .checked_add(total_records_bytes)
            .map(|end| end > bytes.len())
            .unwrap_or(true)
        {
            return Err(Error::UnexpectedEof);
        }

        let mut records = Vec::with_capacity(value_record_count as usize);
        for i in 0..value_record_count as usize {
            let off = 12 + i * stride;
            let tag = [bytes[off], bytes[off + 1], bytes[off + 2], bytes[off + 3]];
            let outer = read_u16(bytes, off + 4)?;
            let inner = read_u16(bytes, off + 6)?;
            records.push((tag, outer, inner));
        }

        let ivs = ItemVariationStore::parse(&bytes[ivs_offset..])?;
        Ok(Self {
            records,
            ivs: Some(ivs),
        })
    }

    /// Number of value records in the table.
    pub fn value_record_count(&self) -> usize {
        self.records.len()
    }

    /// Iterate `(tag, outer, inner)` triples in document order.
    pub fn value_records(&self) -> impl Iterator<Item = ([u8; 4], u16, u16)> + '_ {
        self.records.iter().copied()
    }

    /// Compute the metric delta for `tag` against `normalised_coords`.
    ///
    /// Returns `None` when:
    /// * `tag` is not present in the value-record array, or
    /// * the value record's `(outer, inner)` pair is out of range for
    ///   the embedded IVS.
    ///
    /// Returns `Some(0.0)` when the variation evaluates to zero (e.g.
    /// at the default coordinate) — this is distinct from "tag
    /// absent" because callers may want to log presence even when the
    /// adjustment is currently nil.
    pub fn delta_for_tag(&self, tag: &[u8; 4], normalised_coords: &[f32]) -> Option<f32> {
        let ivs = self.ivs.as_ref()?;
        let (_, outer, inner) = self.records.iter().copied().find(|(t, _, _)| t == tag)?;
        ivs.delta(outer, inner, normalised_coords)
    }

    /// Direct access to the embedded `ItemVariationStore`. Intended
    /// for tests / debugging.
    pub fn item_variation_store(&self) -> Option<&ItemVariationStore> {
        self.ivs.as_ref()
    }
}

impl ItemVariationStore {
    /// Parse an `ItemVariationStore` (§7.2.3) starting at the given
    /// byte slice. Exposed at crate scope so sibling tables that
    /// embed an IVS (notably `HVAR` / `VVAR`) can share the decoder.
    pub(crate) fn parse(bytes: &[u8]) -> Result<Self, Error> {
        if bytes.len() < 8 {
            return Err(Error::UnexpectedEof);
        }
        let format = read_u16(bytes, 0)?;
        if format != 1 {
            return Err(Error::BadStructure("IVS format != 1"));
        }
        let vrl_off = read_u32(bytes, 2)? as usize;
        let ivd_count = read_u16(bytes, 6)?;
        if ivd_count > MAX_IVD_SUBTABLES {
            return Err(Error::BadStructure("IVS subtable count exceeds cap"));
        }
        if vrl_off == 0 || vrl_off > bytes.len() {
            return Err(Error::BadOffset);
        }

        let regions = parse_region_list(&bytes[vrl_off..])?;

        let mut subtables = Vec::with_capacity(ivd_count as usize);
        let off_base = 8usize;
        let need = (ivd_count as usize)
            .checked_mul(4)
            .and_then(|n| n.checked_add(off_base))
            .ok_or(Error::BadOffset)?;
        if need > bytes.len() {
            return Err(Error::UnexpectedEof);
        }
        for i in 0..ivd_count as usize {
            let off = off_base + i * 4;
            let sub_off = read_u32(bytes, off)? as usize;
            if sub_off == 0 {
                // A NULL offset means "no item variation data subtable
                // for that index" per the staged chapter; items using
                // this outer index have no variation.
                subtables.push(ItemVariationData::null());
                continue;
            }
            if sub_off > bytes.len() {
                return Err(Error::BadOffset);
            }
            subtables.push(ItemVariationData::parse(
                &bytes[sub_off..],
                regions.len() as u16,
            )?);
        }

        // axis_count is taken from the region list (§7.2.3.1: equals
        // `fvar.axisCount`).
        let axis_count = if let Some(first) = regions.first() {
            first.len() as u16
        } else {
            0
        };

        Ok(Self {
            axis_count,
            regions,
            subtables,
        })
    }

    /// Number of variation axes the store references.
    pub fn axis_count(&self) -> u16 {
        self.axis_count
    }

    /// Number of regions the store defines.
    pub fn region_count(&self) -> usize {
        self.regions.len()
    }

    /// Number of `ItemVariationData` subtables.
    pub fn subtable_count(&self) -> usize {
        self.subtables.len()
    }

    /// Number of variation regions referenced by subtable `index` (its
    /// `regionIndexCount`). This is the `k` a CFF2 `blend` operator uses
    /// for the `vsindex`-selected ItemVariationData. `None` when the
    /// subtable index is out of range.
    pub fn region_index_count(&self, index: usize) -> Option<usize> {
        self.subtables.get(index).map(|s| s.region_indexes.len())
    }

    /// The region scalars for subtable `index` at `normalised_coords`,
    /// one per region referenced by that `ItemVariationData` (in
    /// `region_indexes` order). This is exactly the set of `k` scalars a
    /// CFF2 `blend` operator multiplies its per-region deltas by. Empty
    /// when the subtable index is out of range.
    pub fn region_scalars(&self, index: usize, normalised_coords: &[f32]) -> Vec<f32> {
        let Some(sub) = self.subtables.get(index) else {
            return Vec::new();
        };
        sub.region_indexes
            .iter()
            .map(|&ri| {
                self.regions
                    .get(ri as usize)
                    .map(|r| region_scalar(r, normalised_coords))
                    .unwrap_or(0.0)
            })
            .collect()
    }

    /// Interpolated delta for `(outer, inner)` against
    /// `normalised_coords`. `None` when either index is out of range.
    pub fn delta(&self, outer: u16, inner: u16, normalised_coords: &[f32]) -> Option<f32> {
        let sub = self.subtables.get(outer as usize)?;
        let row = sub.delta_sets.get(inner as usize)?;
        let mut acc = 0.0f32;
        for (col, &delta) in row.iter().enumerate() {
            let region_index = *sub.region_indexes.get(col)? as usize;
            let region = self.regions.get(region_index)?;
            let scalar = region_scalar(region, normalised_coords);
            if scalar == 0.0 {
                continue;
            }
            acc += scalar * delta as f32;
        }
        Some(acc)
    }
}

fn parse_region_list(bytes: &[u8]) -> Result<Vec<Region>, Error> {
    if bytes.len() < 4 {
        return Err(Error::UnexpectedEof);
    }
    let axis_count = read_u16(bytes, 0)?;
    let region_count = read_u16(bytes, 2)?;
    if region_count > MAX_REGIONS {
        // The high-order bit of regionCount is reserved and must be
        // cleared (regionCount < 32,768).
        return Err(Error::BadStructure("IVS regionCount reserved bit set"));
    }
    let stride = (axis_count as usize)
        .checked_mul(6)
        .ok_or(Error::BadOffset)?;
    let total = (region_count as usize)
        .checked_mul(stride)
        .and_then(|n| n.checked_add(4))
        .ok_or(Error::BadOffset)?;
    if total > bytes.len() {
        return Err(Error::UnexpectedEof);
    }
    let mut regions = Vec::with_capacity(region_count as usize);
    for r in 0..region_count as usize {
        let base = 4 + r * stride;
        let mut axes = Vec::with_capacity(axis_count as usize);
        for a in 0..axis_count as usize {
            let off = base + a * 6;
            let start = f2dot14(read_i16(bytes, off)?);
            let peak = f2dot14(read_i16(bytes, off + 2)?);
            let end = f2dot14(read_i16(bytes, off + 4)?);
            axes.push((start, peak, end));
        }
        regions.push(axes);
    }
    Ok(regions)
}

impl ItemVariationData {
    fn parse(bytes: &[u8], region_count_in_store: u16) -> Result<Self, Error> {
        if bytes.len() < 6 {
            return Err(Error::UnexpectedEof);
        }
        let item_count = read_u16(bytes, 0)?;
        let word_delta_count = read_u16(bytes, 2)?;
        let region_index_count = read_u16(bytes, 4)?;
        // The wordDeltaCount field packs the LONG_WORDS flag (high
        // bit) with the count of leading "word"-representation deltas
        // per row. Without the flag rows mix int16 + int8; with it
        // they mix int32 + int16.
        let long_words = word_delta_count & LONG_WORDS_FLAG != 0;
        let word_count = word_delta_count & WORD_DELTA_COUNT_MASK;
        // No explicit row/column cap is needed beyond the u16 width:
        // the trailing bounds check on `total` against `bytes.len()`
        // already rejects oversized matrices.
        if word_count > region_index_count {
            return Err(Error::BadStructure("wordDeltaCount > regionIndexCount"));
        }
        let region_index_bytes = (region_index_count as usize)
            .checked_mul(2)
            .ok_or(Error::BadOffset)?;
        let header_end = 6usize
            .checked_add(region_index_bytes)
            .ok_or(Error::BadOffset)?;
        if header_end > bytes.len() {
            return Err(Error::UnexpectedEof);
        }
        let mut region_indexes = Vec::with_capacity(region_index_count as usize);
        for i in 0..region_index_count as usize {
            let idx = read_u16(bytes, 6 + i * 2)?;
            if idx >= region_count_in_store {
                return Err(Error::BadStructure("IVD region index out of range"));
            }
            region_indexes.push(idx);
        }

        // Per-row byte length: regionIndexCount + wordCount short
        // units, doubled under LONG_WORDS ("2 x that amount").
        let unit = if long_words { 2usize } else { 1 };
        let row_size = (region_index_count as usize)
            .checked_add(word_count as usize)
            .and_then(|n| n.checked_mul(unit))
            .ok_or(Error::BadOffset)?;
        let total = (item_count as usize)
            .checked_mul(row_size)
            .and_then(|n| n.checked_add(header_end))
            .ok_or(Error::BadOffset)?;
        if total > bytes.len() {
            return Err(Error::UnexpectedEof);
        }

        let mut delta_sets = Vec::with_capacity(item_count as usize);
        for r in 0..item_count as usize {
            let row_start = header_end + r * row_size;
            let mut row = Vec::with_capacity(region_index_count as usize);
            let word_bytes = 2 * unit;
            for c in 0..word_count as usize {
                let off = row_start + c * word_bytes;
                row.push(if long_words {
                    read_i32(bytes, off)?
                } else {
                    read_i16(bytes, off)? as i32
                });
            }
            let short_base = row_start + (word_count as usize) * word_bytes;
            for c in 0..(region_index_count - word_count) as usize {
                let off = short_base + c * unit;
                row.push(if long_words {
                    read_i16(bytes, off)? as i32
                } else {
                    read_i8(bytes, off)? as i32
                });
            }
            delta_sets.push(row);
        }

        Ok(Self {
            region_indexes,
            delta_sets,
        })
    }
}

/// Combined per-region scalar (§7.1 / §7.2.3.1). Returns 0 when the
/// coordinate is outside the region span on any axis, 1 when every
/// per-axis peak is 0 (the "region matches all instances" degenerate
/// case the spec mentions in passing for placeholder regions).
fn region_scalar(region: &[RegionAxis], coords: &[f32]) -> f32 {
    let mut s = 1.0f32;
    for (ai, &(start, peak, end)) in region.iter().enumerate() {
        let c = coords.get(ai).copied().unwrap_or(0.0);
        if peak == 0.0 {
            // §7.2.3.1: peakCoord == 0 ⇒ axis does not factor.
            continue;
        }
        if c == peak {
            continue;
        }
        if c <= start || c >= end {
            return 0.0;
        }
        if c < peak {
            // Rising edge.
            if (peak - start).abs() < f32::EPSILON {
                return 0.0;
            }
            s *= (c - start) / (peak - start);
        } else {
            // Falling edge.
            if (end - peak).abs() < f32::EPSILON {
                return 0.0;
            }
            s *= (end - c) / (end - peak);
        }
    }
    s
}

#[inline]
fn f2dot14(raw: i16) -> f32 {
    raw as f32 / 16384.0
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Hand-built single-axis MVAR with one tag and one region/row.
    /// Region: opsz-style (start=0, peak=+1, end=+1).
    /// IVD: one row, one int16 delta of -100 against region 0.
    fn build_single_axis_mvar() -> Vec<u8> {
        // Layout:
        //   [0..12)  MVAR header
        //   [12..20) one ValueRecord (8 bytes)
        //   [20..)   IVS
        // IVS layout (starts at offset 20):
        //   IVS+0..8     header (format=1, vrlOff=12, ivdCount=1)
        //   IVS+8..12    ivdOffsets[0]
        //   IVS+12..22   region list (axisCount=1, regionCount=1, +1 region × 6 B)
        //   IVS+22..32   IVD subtable
        // IVD layout (10 bytes total):
        //   [0..6)   header (itemCount=1, shortDeltaCount=1, regionIxCount=1)
        //   [6..8)   regionIndexes[0] = 0
        //   [8..10)  deltaSet[0]: int16 delta
        let ivd_rel = 22u32;
        let mut b = vec![0u8; 12 + 8 + 22 + 10];

        // MVAR header
        b[0..2].copy_from_slice(&1u16.to_be_bytes()); // major
                                                      // minor / reserved = 0
        b[6..8].copy_from_slice(&8u16.to_be_bytes()); // valueRecordSize
        b[8..10].copy_from_slice(&1u16.to_be_bytes()); // valueRecordCount
        b[10..12].copy_from_slice(&20u16.to_be_bytes()); // ivsOffset

        // Value record at [12..20)
        b[12..16].copy_from_slice(b"xhgt");
        b[16..18].copy_from_slice(&0u16.to_be_bytes()); // outer
        b[18..20].copy_from_slice(&0u16.to_be_bytes()); // inner

        let ivs = 20usize;
        // IVS header
        b[ivs..ivs + 2].copy_from_slice(&1u16.to_be_bytes()); // format
        b[ivs + 2..ivs + 6].copy_from_slice(&12u32.to_be_bytes()); // vrlOff
        b[ivs + 6..ivs + 8].copy_from_slice(&1u16.to_be_bytes()); // ivdCount
        b[ivs + 8..ivs + 12].copy_from_slice(&ivd_rel.to_be_bytes());

        // Region list at IVS+12..22 (axisCount=1, regionCount=1, +6 B region).
        let rl = ivs + 12;
        b[rl..rl + 2].copy_from_slice(&1u16.to_be_bytes()); // axisCount
        b[rl + 2..rl + 4].copy_from_slice(&1u16.to_be_bytes()); // regionCount
                                                                // axis 0: start=0, peak=16384 (=+1), end=16384
        b[rl + 4..rl + 6].copy_from_slice(&0i16.to_be_bytes());
        b[rl + 6..rl + 8].copy_from_slice(&16384i16.to_be_bytes());
        b[rl + 8..rl + 10].copy_from_slice(&16384i16.to_be_bytes());

        // IVD at IVS+22..32.
        let ivd = ivs + ivd_rel as usize;
        b[ivd..ivd + 2].copy_from_slice(&1u16.to_be_bytes()); // itemCount
        b[ivd + 2..ivd + 4].copy_from_slice(&1u16.to_be_bytes()); // shortDeltaCount
        b[ivd + 4..ivd + 6].copy_from_slice(&1u16.to_be_bytes()); // regionIndexCount
        b[ivd + 6..ivd + 8].copy_from_slice(&0u16.to_be_bytes()); // regionIndexes[0] = 0
        b[ivd + 8..ivd + 10].copy_from_slice(&(-100i16).to_be_bytes()); // row[0][0]
        b
    }

    #[test]
    fn parses_minimal_table() {
        let raw = build_single_axis_mvar();
        let m = MvarTable::parse(&raw).expect("parse");
        assert_eq!(m.value_record_count(), 1);
        assert_eq!(
            m.value_records().collect::<Vec<_>>(),
            vec![(*b"xhgt", 0u16, 0u16)]
        );
        let ivs = m.item_variation_store().expect("ivs");
        assert_eq!(ivs.axis_count(), 1);
        assert_eq!(ivs.region_count(), 1);
        assert_eq!(ivs.subtable_count(), 1);
    }

    #[test]
    fn delta_zero_at_default_coords() {
        let raw = build_single_axis_mvar();
        let m = MvarTable::parse(&raw).unwrap();
        // Normalised coord at axis default (= 0): peak=+1 ⇒ scalar 0 ⇒
        // delta == 0.
        let d = m.delta_for_tag(b"xhgt", &[0.0]).expect("known tag");
        assert_eq!(d, 0.0);
    }

    #[test]
    fn delta_interpolates_along_axis() {
        let raw = build_single_axis_mvar();
        let m = MvarTable::parse(&raw).unwrap();
        // At halfway up the rising edge: scalar = (0.5 - 0) / (1 - 0) = 0.5
        // delta = 0.5 * -100 = -50.0
        let d = m.delta_for_tag(b"xhgt", &[0.5]).expect("known tag");
        assert!((d - (-50.0)).abs() < 1e-5, "got {d}");
        // At the peak: scalar = 1, delta = -100.
        let d = m.delta_for_tag(b"xhgt", &[1.0]).expect("known tag");
        assert!((d - (-100.0)).abs() < 1e-5, "got {d}");
    }

    #[test]
    fn unknown_tag_returns_none() {
        let raw = build_single_axis_mvar();
        let m = MvarTable::parse(&raw).unwrap();
        assert!(m.delta_for_tag(b"none", &[0.5]).is_none());
    }

    #[test]
    fn empty_value_records_parses_with_no_ivs() {
        // valueRecordCount = 0, ivsOffset = 0 — minimum legal header.
        let mut b = vec![0u8; 12];
        b[0..2].copy_from_slice(&1u16.to_be_bytes());
        // Everything else 0.
        let m = MvarTable::parse(&b).expect("parse");
        assert_eq!(m.value_record_count(), 0);
        assert!(m.item_variation_store().is_none());
        assert!(m.delta_for_tag(b"xhgt", &[0.0]).is_none());
    }

    #[test]
    fn rejects_short_value_record_size() {
        let mut b = vec![0u8; 14];
        b[0..2].copy_from_slice(&1u16.to_be_bytes());
        b[6..8].copy_from_slice(&4u16.to_be_bytes()); // valueRecordSize = 4 (< 8)
        b[8..10].copy_from_slice(&1u16.to_be_bytes()); // 1 record
        b[10..12].copy_from_slice(&12u16.to_be_bytes());
        assert!(matches!(MvarTable::parse(&b), Err(Error::BadStructure(_))));
    }

    #[test]
    fn honours_larger_value_record_stride() {
        // Simulate a hypothetical minor-version with a 10-byte value
        // record (first 8 bytes match v1, two trailing bytes ignored).
        let stride: u16 = 10;
        let count: u16 = 1;
        let ivd_rel: u32 = 22;
        // header 12 + record 10 + IVS{8 + 4 + 10 + 10} = 12 + 10 + 32
        let mut b = vec![0u8; 12 + stride as usize + 32];
        b[0..2].copy_from_slice(&1u16.to_be_bytes());
        b[6..8].copy_from_slice(&stride.to_be_bytes());
        b[8..10].copy_from_slice(&count.to_be_bytes());
        let ivs_off: u16 = 12 + stride;
        b[10..12].copy_from_slice(&ivs_off.to_be_bytes());
        // ValueRecord (8-byte prefix + 2 trailing bytes)
        b[12..16].copy_from_slice(b"hasc");
        b[16..18].copy_from_slice(&0u16.to_be_bytes()); // outer
        b[18..20].copy_from_slice(&0u16.to_be_bytes()); // inner
                                                        // bytes 20..22 are the unknown trailing bytes; we leave them zero.
                                                        // IVS at ivs_off
        let ivs = ivs_off as usize;
        b[ivs..ivs + 2].copy_from_slice(&1u16.to_be_bytes());
        b[ivs + 2..ivs + 6].copy_from_slice(&12u32.to_be_bytes()); // vrlOff
        b[ivs + 6..ivs + 8].copy_from_slice(&1u16.to_be_bytes());
        b[ivs + 8..ivs + 12].copy_from_slice(&ivd_rel.to_be_bytes());
        // region list at IVS+12, single region with 1 axis (10 B)
        let rl = ivs + 12;
        b[rl..rl + 2].copy_from_slice(&1u16.to_be_bytes()); // axisCount
        b[rl + 2..rl + 4].copy_from_slice(&1u16.to_be_bytes()); // regionCount
        b[rl + 6..rl + 8].copy_from_slice(&16384i16.to_be_bytes());
        b[rl + 8..rl + 10].copy_from_slice(&16384i16.to_be_bytes());
        // IVD at IVS+22
        let ivd = ivs + ivd_rel as usize;
        b[ivd..ivd + 2].copy_from_slice(&1u16.to_be_bytes());
        b[ivd + 2..ivd + 4].copy_from_slice(&1u16.to_be_bytes());
        b[ivd + 4..ivd + 6].copy_from_slice(&1u16.to_be_bytes());
        b[ivd + 6..ivd + 8].copy_from_slice(&0u16.to_be_bytes());
        b[ivd + 8..ivd + 10].copy_from_slice(&(42i16).to_be_bytes());

        let m = MvarTable::parse(&b).expect("parse");
        assert_eq!(m.value_record_count(), 1);
        // Round-trip the delta: scalar=1 at peak ⇒ delta=42.
        let d = m.delta_for_tag(b"hasc", &[1.0]).expect("tag");
        assert!((d - 42.0).abs() < 1e-5);
    }

    #[test]
    fn region_scalar_zero_outside_span() {
        // Region: start=-1, peak=-1, end=0 (typical "wght going below default")
        let region = [(-1.0f32, -1.0, 0.0)];
        // Coord +0.5 has opposite sign from peak — scalar 0.
        assert_eq!(region_scalar(&region, &[0.5]), 0.0);
        // Coord 0 == end — scalar 0.
        assert_eq!(region_scalar(&region, &[0.0]), 0.0);
        // Coord -1 == peak — scalar 1.
        assert_eq!(region_scalar(&region, &[-1.0]), 1.0);
        // Coord -0.5 on the falling edge: (end - c)/(end - peak)
        //   = (0 - (-0.5)) / (0 - (-1)) = 0.5
        let s = region_scalar(&region, &[-0.5]);
        assert!((s - 0.5).abs() < 1e-5);
    }

    #[test]
    fn region_scalar_axis_ignored_when_peak_zero() {
        // Two-axis region: axis 0 ignored (peak=0), axis 1 active.
        let region = [(0.0f32, 0.0, 0.0), (0.0, 1.0, 1.0)];
        // Axis 0 coordinate is irrelevant.
        let s = region_scalar(&region, &[0.7, 1.0]);
        assert!((s - 1.0).abs() < 1e-5);
    }

    #[test]
    fn rejects_ivs_format_other_than_1() {
        // Same scaffold as the minimal-table builder but flip the IVS
        // format byte.
        let mut b = build_single_axis_mvar();
        let ivs = 20usize;
        b[ivs..ivs + 2].copy_from_slice(&2u16.to_be_bytes());
        assert!(matches!(MvarTable::parse(&b), Err(Error::BadStructure(_))));
    }

    #[test]
    fn rejects_out_of_range_region_index_in_ivd() {
        // Build a valid table then poke the regionIndexes[0] to 5
        // (region_count is 1, so 5 is out of range). IVD starts at
        // MVAR offset 20 (IVS) + 22 (IVS-relative IVD offset) = 42.
        let mut b = build_single_axis_mvar();
        let ivd = 20 + 22;
        b[ivd + 6..ivd + 8].copy_from_slice(&5u16.to_be_bytes());
        assert!(matches!(MvarTable::parse(&b), Err(Error::BadStructure(_))));
    }

    /// Build a standalone single-axis IVS blob with `region_count`
    /// identical rising-edge regions (peak +1) and one IVD subtable
    /// whose header + row bytes are supplied verbatim.
    fn build_ivs_blob(region_count: u16, ivd_body: &[u8]) -> Vec<u8> {
        let mut b = Vec::new();
        b.extend_from_slice(&1u16.to_be_bytes()); // format
        b.extend_from_slice(&12u32.to_be_bytes()); // vrlOff
        b.extend_from_slice(&1u16.to_be_bytes()); // ivdCount
        let ivd_off = 12 + 4 + region_count as u32 * 6;
        b.extend_from_slice(&ivd_off.to_be_bytes()); // ivdOffsets[0]
        b.extend_from_slice(&1u16.to_be_bytes()); // axisCount
        b.extend_from_slice(&region_count.to_be_bytes()); // regionCount
        for _ in 0..region_count {
            b.extend_from_slice(&0i16.to_be_bytes()); // start
            b.extend_from_slice(&16384i16.to_be_bytes()); // peak
            b.extend_from_slice(&16384i16.to_be_bytes()); // end
        }
        b.extend_from_slice(ivd_body);
        b
    }

    #[test]
    fn long_words_flag_decodes_int32_deltas() {
        // wordDeltaCount = 0x8001 (LONG_WORDS | 1): one int32 word
        // delta, no shorts. The value exceeds the int16 range to
        // prove the widened read.
        let mut ivd = Vec::new();
        ivd.extend_from_slice(&1u16.to_be_bytes()); // itemCount
        ivd.extend_from_slice(&0x8001u16.to_be_bytes()); // wordDeltaCount
        ivd.extend_from_slice(&1u16.to_be_bytes()); // regionIndexCount
        ivd.extend_from_slice(&0u16.to_be_bytes()); // regionIndexes[0]
        ivd.extend_from_slice(&100_000i32.to_be_bytes()); // row[0][0]
        let blob = build_ivs_blob(1, &ivd);
        let ivs = ItemVariationStore::parse(&blob).expect("parse");
        let d = ivs.delta(0, 0, &[1.0]).expect("delta");
        assert!((d - 100_000.0).abs() < 1e-3, "got {d}");
        // Half-scalar interpolation still applies to long deltas.
        let d = ivs.delta(0, 0, &[0.5]).expect("delta");
        assert!((d - 50_000.0).abs() < 1e-3, "got {d}");
    }

    #[test]
    fn long_words_flag_mixes_int32_and_int16() {
        // Two regions; wordDeltaCount = LONG_WORDS | 1 → row is one
        // int32 followed by one int16.
        let mut ivd = Vec::new();
        ivd.extend_from_slice(&1u16.to_be_bytes()); // itemCount
        ivd.extend_from_slice(&0x8001u16.to_be_bytes()); // wordDeltaCount
        ivd.extend_from_slice(&2u16.to_be_bytes()); // regionIndexCount
        ivd.extend_from_slice(&0u16.to_be_bytes()); // regionIndexes[0]
        ivd.extend_from_slice(&1u16.to_be_bytes()); // regionIndexes[1]
        ivd.extend_from_slice(&70_000i32.to_be_bytes()); // word delta
        ivd.extend_from_slice(&(-300i16).to_be_bytes()); // short delta
        let blob = build_ivs_blob(2, &ivd);
        let ivs = ItemVariationStore::parse(&blob).expect("parse");
        // Both regions peak at +1 on the sole axis: both scalars 1.
        let d = ivs.delta(0, 0, &[1.0]).expect("delta");
        assert!((d - 69_700.0).abs() < 1e-3, "got {d}");
    }

    #[test]
    fn word_count_exceeding_region_count_rejected() {
        // wordDeltaCount = LONG_WORDS | 2 with regionIndexCount = 1:
        // the masked count (2) must be <= regionIndexCount.
        let mut ivd = Vec::new();
        ivd.extend_from_slice(&1u16.to_be_bytes());
        ivd.extend_from_slice(&0x8002u16.to_be_bytes());
        ivd.extend_from_slice(&1u16.to_be_bytes());
        ivd.extend_from_slice(&0u16.to_be_bytes());
        ivd.extend_from_slice(&[0u8; 8]);
        let blob = build_ivs_blob(1, &ivd);
        assert!(matches!(
            ItemVariationStore::parse(&blob),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn null_ivd_offset_means_no_variation() {
        // ivdCount = 2 with offsets[1] = NULL: the store parses, and
        // any inner index against outer 1 resolves to no variation.
        let mut b = Vec::new();
        b.extend_from_slice(&1u16.to_be_bytes()); // format
        b.extend_from_slice(&16u32.to_be_bytes()); // vrlOff
        b.extend_from_slice(&2u16.to_be_bytes()); // ivdCount
        b.extend_from_slice(&26u32.to_be_bytes()); // ivdOffsets[0]
        b.extend_from_slice(&0u32.to_be_bytes()); // ivdOffsets[1] = NULL
        b.extend_from_slice(&1u16.to_be_bytes()); // axisCount
        b.extend_from_slice(&1u16.to_be_bytes()); // regionCount
        b.extend_from_slice(&0i16.to_be_bytes());
        b.extend_from_slice(&16384i16.to_be_bytes());
        b.extend_from_slice(&16384i16.to_be_bytes());
        // IVD 0 at offset 26: one int16 delta of -100.
        b.extend_from_slice(&1u16.to_be_bytes()); // itemCount
        b.extend_from_slice(&1u16.to_be_bytes()); // wordDeltaCount
        b.extend_from_slice(&1u16.to_be_bytes()); // regionIndexCount
        b.extend_from_slice(&0u16.to_be_bytes()); // regionIndexes[0]
        b.extend_from_slice(&(-100i16).to_be_bytes());
        let ivs = ItemVariationStore::parse(&b).expect("parse");
        assert_eq!(ivs.subtable_count(), 2);
        assert!(ivs.delta(0, 0, &[1.0]).is_some());
        assert!(ivs.delta(1, 0, &[1.0]).is_none(), "NULL subtable");
        assert_eq!(ivs.region_index_count(1), Some(0));
    }

    #[test]
    fn region_count_reserved_bit_rejected() {
        // regionCount with the reserved high-order bit set must be
        // rejected (regionCount < 32,768).
        let mut b = build_single_axis_mvar();
        let rl = 20 + 12;
        b[rl + 2..rl + 4].copy_from_slice(&0x8001u16.to_be_bytes());
        assert!(matches!(MvarTable::parse(&b), Err(Error::BadStructure(_))));
    }
}