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
use crate::{NormalizedCoordinate, Tag};
use crate::parser::{FromData, LazyArray16, LazyArray32};
use crate::parser::{Offset, Offset32, Stream};
use super::{Feature, FeatureIndex, VariationIndex, RecordListItem};

/// A [Feature Variations Table](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#featurevariations-table).
#[derive(Clone, Copy, Debug)]
pub struct FeatureVariations<'a> {
    data: &'a [u8],
    records: LazyArray32<'a, FeatureVariationRecord>,
}

impl<'a> FeatureVariations<'a> {
    pub(crate) fn parse(data: &'a [u8]) -> Option<Self> {
        let mut s = Stream::new(data);
        let major_version = s.read::<u16>()?;
        s.skip::<u16>(); // minor version
        if major_version != 1 {
            return None;
        }

        let count = s.read::<u32>()?;
        let records = s.read_array32(count)?;
        Some(Self { data, records })
    }

    /// Returns a [`VariationIndex`] for variation coordinates.
    pub fn find_index(&self, coords: &[NormalizedCoordinate]) -> Option<VariationIndex> {
        for i in 0..self.records.len() {
            let record = self.records.get(i)?;
            let offset = record.conditions.to_usize();
            let set = ConditionSet::parse(self.data.get(offset..)?)?;
            if set.evaluate(coords) {
                return Some(i);
            }
        }
        None
    }

    /// Returns a [`Feature`] at specified indices.
    pub fn find_substitute(
        &self,
        feature_index: FeatureIndex,
        variation_index: VariationIndex,
    ) -> Option<Feature<'a>> {
        let offset = self.records.get(variation_index)?.substitutions.to_usize();
        let subst = FeatureTableSubstitution::parse(self.data.get(offset..)?)?;
        subst.find_substitute(feature_index)
    }
}

#[derive(Clone, Copy, Debug)]
struct FeatureVariationRecord {
    conditions: Offset32,
    substitutions: Offset32,
}

impl FromData for FeatureVariationRecord {
    const SIZE: usize = 8;

    #[inline]
    fn parse(data: &[u8]) -> Option<Self> {
        let mut s = Stream::new(data);
        Some(Self {
            conditions: s.read::<Offset32>()?,
            substitutions: s.read::<Offset32>()?,
        })
    }
}

#[derive(Clone, Copy, Debug)]
struct ConditionSet<'a> {
    data: &'a [u8],
    conditions: LazyArray16<'a, Offset32>,
}

impl<'a> ConditionSet<'a> {
    fn parse(data: &'a [u8]) -> Option<Self> {
        let mut s = Stream::new(data);
        let count = s.read::<u16>()?;
        let conditions = s.read_array16(count)?;
        Some(Self { data, conditions })
    }

    fn evaluate(&self, coords: &[NormalizedCoordinate]) -> bool {
        self.conditions.into_iter().all(|offset| {
            self.data.get(offset.to_usize()..)
                .and_then(Condition::parse)
                .map_or(false, |c| c.evaluate(coords))
        })
    }
}

#[derive(Clone, Copy, Debug)]
enum Condition {
    Format1 {
        axis_index: u16,
        filter_range_min: i16,
        filter_range_max: i16,
    }
}

impl Condition {
    fn parse(data: &[u8]) -> Option<Self> {
        let mut s = Stream::new(data);
        let format = s.read::<u16>()?;
        match format {
            1 => {
                let axis_index = s.read::<u16>()?;
                let filter_range_min = s.read::<i16>()?;
                let filter_range_max = s.read::<i16>()?;
                Some(Self::Format1 { axis_index, filter_range_min, filter_range_max })
            }
            _ => None,
        }
    }

    fn evaluate(&self, coords: &[NormalizedCoordinate]) -> bool {
        let Self::Format1 { axis_index, filter_range_min, filter_range_max } = *self;
        let coord = coords.get(usize::from(axis_index)).map(|c| c.get()).unwrap_or(0);
        filter_range_min <= coord && coord <= filter_range_max
    }
}

#[derive(Clone, Copy, Debug)]
struct FeatureTableSubstitution<'a> {
    data: &'a [u8],
    records: LazyArray16<'a, FeatureTableSubstitutionRecord>,
}

impl<'a> FeatureTableSubstitution<'a> {
    fn parse(data: &'a [u8]) -> Option<Self> {
        let mut s = Stream::new(data);
        let major_version = s.read::<u16>()?;
        s.skip::<u16>(); // minor version
        if major_version != 1 {
            return None;
        }

        let count = s.read::<u16>()?;
        let records = s.read_array16(count)?;
        Some(Self { data, records })
    }

    fn find_substitute(&self, feature_index: FeatureIndex) -> Option<Feature<'a>> {
        for record in self.records {
            if record.feature_index == feature_index {
                let offset = record.feature.to_usize();
                // TODO: set tag
                return Feature::parse(Tag::from_bytes(b"DFLT"), self.data.get(offset..)?);
            }
        }
        None
    }
}

#[derive(Clone, Copy, Debug)]
struct FeatureTableSubstitutionRecord {
    feature_index: FeatureIndex,
    feature: Offset32,
}

impl FromData for FeatureTableSubstitutionRecord {
    const SIZE: usize = 6;

    #[inline]
    fn parse(data: &[u8]) -> Option<Self> {
        let mut s = Stream::new(data);
        Some(Self {
            feature_index: s.read::<FeatureIndex>()?,
            feature: s.read::<Offset32>()?,
        })
    }
}