1use crate::Feature;
2use alloc::vec;
3use alloc::vec::Vec;
4use core::cmp::Ordering;
5
6use super::layout::*;
7use super::AatData;
8use crate::{Language, Mask, Tag};
9
10#[doc(alias = "hb_aat_map_t")]
14#[derive(Default)]
15pub struct AatMap {
16 pub chain_flags: Vec<Vec<RangeFlags>>,
17}
18
19#[derive(Copy, Clone)]
23pub struct RangeFlags {
24 pub flags: Mask,
25 pub cluster_first: u32,
26 pub cluster_last: u32, }
28
29#[doc(alias = "hb_aat_map_builder_t")]
33pub struct AatMapBuilder {
34 pub language: Option<Language>,
35 pub current_features: Vec<FeatureInfo>,
36 pub features: Vec<FeatureRange>,
37 pub range_first: usize,
38 pub range_last: usize,
39}
40
41impl Default for AatMapBuilder {
42 fn default() -> Self {
43 Self {
44 language: None,
45 range_first: Feature::GLOBAL_START as usize,
46 range_last: Feature::GLOBAL_END as usize,
47 current_features: Vec::default(),
48 features: Vec::default(),
49 }
50 }
51}
52
53impl AatMapBuilder {
54 pub fn new(language: Option<&Language>) -> Self {
55 Self {
56 language: language.cloned(),
57 ..Self::default()
58 }
59 }
60
61 pub fn add_feature(&mut self, aat: &AatData, feature: &Feature) -> Option<()> {
62 let feat = aat.feat.as_ref()?;
63
64 if feature.tag == Tag::new(b"aalt") {
65 let exposes_feature = feat
66 .find(FEATURE_TYPE_CHARACTER_ALTERNATIVES as u16)
67 .is_some_and(|f| f.n_settings() != 0);
68
69 if !exposes_feature {
70 return Some(());
71 }
72
73 self.features.push(FeatureRange {
74 start: feature.start,
75 end: feature.end,
76 info: FeatureInfo {
77 kind: FEATURE_TYPE_CHARACTER_ALTERNATIVES as u16,
78 setting: u16::try_from(feature.value).unwrap(),
79 is_exclusive: true,
80 },
81 });
82 }
83
84 let idx = feature_mappings
85 .binary_search_by(|map| map.ot_feature_tag.cmp(&feature.tag))
86 .ok()?;
87 let mapping = &feature_mappings[idx];
88
89 let mut feature_name = feat.find(mapping.aat_feature_type as u16);
90
91 match feature_name {
92 Some(feature) if feature.n_settings() != 0 => {}
93 _ => {
94 if mapping.aat_feature_type == FEATURE_TYPE_LOWER_CASE
98 && mapping.selector_to_enable == FEATURE_SELECTOR_LOWER_CASE_SMALL_CAPS
99 {
100 feature_name = feat.find(FEATURE_TYPE_LETTER_CASE as u16);
101 }
102 }
103 }
104
105 match feature_name {
106 Some(feature_name) if feature_name.n_settings() != 0 => {
107 let setting = if feature.value != 0 {
108 mapping.selector_to_enable
109 } else {
110 mapping.selector_to_disable
111 } as u16;
112
113 self.features.push(FeatureRange {
114 start: feature.start,
115 end: feature.end,
116 info: FeatureInfo {
117 kind: mapping.aat_feature_type as u16,
118 setting,
119 is_exclusive: feature_name.is_exclusive(),
120 },
121 });
122 }
123 _ => {}
124 }
125
126 Some(())
127 }
128
129 pub fn compile(&mut self, aat: &AatData, m: &mut AatMap) {
130 let mut feature_events = vec![];
132 for feature in &self.features {
133 if feature.start == feature.end {
134 continue;
135 }
136
137 feature_events.push(FeatureEvent {
138 index: feature.start as usize,
139 start: true,
140 seq: feature_events.len(),
141 feature: feature.info,
142 });
143
144 feature_events.push(FeatureEvent {
145 index: feature.end as usize,
146 start: false,
147 seq: feature_events.len(),
148 feature: feature.info,
149 });
150 }
151
152 crate::algs::sort_unstable_by(&mut feature_events, Ord::cmp);
153
154 feature_events.push(FeatureEvent {
156 index: u32::MAX as usize,
157 start: false,
158 seq: feature_events.len(),
159 feature: FeatureInfo::default(),
160 });
161
162 let mut active_features = vec![];
164 let mut last_index = 0;
165
166 for event in &feature_events {
167 if event.index != last_index {
168 self.current_features.clone_from(&active_features);
171 self.range_first = last_index;
172 self.range_last = event.index.wrapping_sub(1);
173
174 if !self.current_features.is_empty() {
175 self.current_features.sort();
176 let mut j = 0;
177 for i in 1..self.current_features.len() {
178 let non_exclusive = !self.current_features[i].is_exclusive
182 && (self.current_features[i].setting & !1)
183 != (self.current_features[j].setting & !1);
184
185 if self.current_features[i].kind != self.current_features[j].kind
186 || non_exclusive
187 {
188 j += 1;
189 self.current_features[j] = self.current_features[i];
190 }
191 }
192 self.current_features.truncate(j + 1);
193 }
194
195 super::morx::compile_flags(aat, self, m);
196 last_index = event.index;
197 }
198
199 if event.start {
200 active_features.push(event.feature);
201 } else {
202 if let Some(index) = active_features.iter().position(|&f| f == event.feature) {
203 active_features.remove(index);
204 }
205 }
206 }
207
208 for chain_flags in &mut m.chain_flags {
209 if let Some(last) = chain_flags.last_mut() {
210 last.cluster_last = Feature::GLOBAL_END;
211 }
212 }
213 }
214}
215
216#[derive(Copy, Clone, PartialEq, Eq, Default)]
220pub struct FeatureInfo {
221 pub kind: u16,
222 pub setting: u16,
223 pub is_exclusive: bool,
224}
225
226impl Ord for FeatureInfo {
227 fn cmp(&self, other: &Self) -> Ordering {
228 self.partial_cmp(other).unwrap()
229 }
230}
231
232impl PartialOrd for FeatureInfo {
233 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
234 if self.kind != other.kind {
235 Some(self.kind.cmp(&other.kind))
236 } else if !self.is_exclusive && (self.setting & !1) != (other.setting & !1) {
237 Some(self.setting.cmp(&other.setting))
238 } else {
239 Some(Ordering::Equal)
240 }
241 }
242}
243
244#[derive(Copy, Clone, PartialEq, Eq)]
248pub struct FeatureRange {
249 pub info: FeatureInfo,
250 pub start: u32,
251 pub end: u32,
252}
253
254#[derive(Copy, Clone, Eq, PartialEq)]
258struct FeatureEvent {
259 pub index: usize,
260 pub start: bool,
261 pub seq: usize,
263 pub feature: FeatureInfo,
264}
265
266impl Ord for FeatureEvent {
267 fn cmp(&self, other: &Self) -> Ordering {
268 self.partial_cmp(other).unwrap()
269 }
270}
271
272impl PartialOrd for FeatureEvent {
273 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
274 if self.index != other.index {
275 Some(self.index.cmp(&other.index))
276 } else if self.start != other.start {
277 Some(self.start.cmp(&other.start))
278 } else {
279 Some(self.seq.cmp(&other.seq))
280 }
281 }
282}