1use core::ptr;
2
3use crate::aat::map::*;
4use crate::ot::layout::*;
5use crate::ot::map::*;
6use crate::ot::shaper::*;
7use crate::shaper_font::LayoutData;
8use crate::{Direction, Feature, Language, Script, Tag};
9
10use super::plan::ShapePlan;
11
12pub struct ShapePlanner<'a> {
13 pub layout: LayoutData<'a>,
14 pub direction: Direction,
15 pub script: Option<Script>,
16 pub language: Option<Language>,
17 pub ot_map: OtMapBuilder<'a>,
18 pub aat_map: AatMapBuilder,
19 pub apply_morx: bool,
20 pub script_zero_marks: bool,
21 pub script_fallback_position: bool,
22 pub shaper: &'static OtShaper,
23}
24
25impl<'a> ShapePlanner<'a> {
26 pub fn new(
27 layout: LayoutData<'a>,
28 direction: Direction,
29 script: Option<Script>,
30 language: Option<&Language>,
31 ) -> Self {
32 let ot_map = OtMapBuilder::new(layout, script, language);
33 let aat_map = AatMapBuilder::new(language);
34
35 let mut shaper = match script {
36 Some(script) => categorize(
37 script,
38 direction,
39 ot_map.chosen_script(LayoutTableKind::Gsub),
40 ),
41 None => &DEFAULT_SHAPER,
42 };
43
44 let script_zero_marks = shaper.zero_width_marks != ZeroWidthMarks::None;
45 let script_fallback_position = shaper.fallback_position;
46
47 let apply_morx = (layout.aat.morx.is_some() || layout.aat.mort.is_some())
49 && (direction.is_horizontal() || layout.ot.gsub().is_none());
50
51 if apply_morx && !ptr::eq(ptr::from_ref(shaper), ptr::from_ref(&DEFAULT_SHAPER)) {
53 shaper = &DUMBER_SHAPER;
54 }
55
56 ShapePlanner {
57 layout,
58 direction,
59 script,
60 language: language.cloned(),
61 ot_map,
62 aat_map,
63 apply_morx,
64 script_zero_marks,
65 script_fallback_position,
66 shaper,
67 }
68 }
69
70 pub fn collect_features(&mut self, user_features: &[Feature]) {
71 static COMMON_FEATURES: &[(Tag, MapFeatureFlags)] = &[
72 (Tag::new(b"abvm"), F_GLOBAL),
73 (Tag::new(b"blwm"), F_GLOBAL),
74 (Tag::new(b"ccmp"), F_GLOBAL),
75 (Tag::new(b"locl"), F_GLOBAL),
76 (Tag::new(b"mark"), F_GLOBAL_MANUAL_JOINERS),
77 (Tag::new(b"mkmk"), F_GLOBAL_MANUAL_JOINERS),
78 (Tag::new(b"rlig"), F_GLOBAL),
79 ];
80
81 static HORIZONTAL_FEATURES: &[(Tag, MapFeatureFlags)] = &[
82 (Tag::new(b"calt"), F_GLOBAL),
83 (Tag::new(b"clig"), F_GLOBAL),
84 (Tag::new(b"curs"), F_GLOBAL),
85 (Tag::new(b"dist"), F_GLOBAL),
86 (Tag::new(b"kern"), F_GLOBAL_HAS_FALLBACK),
87 (Tag::new(b"liga"), F_GLOBAL),
88 (Tag::new(b"rclt"), F_GLOBAL),
89 ];
90
91 let empty = F_NONE;
92 self.ot_map.is_simple = true;
93
94 self.ot_map.enable_feature(Tag::new(b"rvrn"), empty, 1);
95 self.ot_map.add_gsub_pause(None);
96
97 match self.direction {
98 Direction::LeftToRight => {
99 self.ot_map.enable_feature(Tag::new(b"ltra"), empty, 1);
100 self.ot_map.enable_feature(Tag::new(b"ltrm"), empty, 1);
101 }
102 Direction::RightToLeft => {
103 self.ot_map.enable_feature(Tag::new(b"rtla"), empty, 1);
104 self.ot_map.add_feature(Tag::new(b"rtlm"), empty, 1);
105 }
106 _ => {}
107 }
108
109 self.ot_map.add_feature(Tag::new(b"frac"), empty, 1);
111 self.ot_map.add_feature(Tag::new(b"numr"), empty, 1);
112 self.ot_map.add_feature(Tag::new(b"dnom"), empty, 1);
113
114 self.ot_map
116 .enable_feature(Tag::new(b"rand"), F_RANDOM, OtMap::MAX_VALUE);
117
118 self.ot_map
122 .enable_feature(Tag::new(b"trak"), F_HAS_FALLBACK, 1);
123
124 self.ot_map.enable_feature(Tag::new(b"Harf"), empty, 1); self.ot_map.enable_feature(Tag::new(b"HARF"), empty, 1); if let Some(func) = self.shaper.collect_features {
128 self.ot_map.is_simple = false;
129 func(self);
130 }
131
132 self.ot_map.enable_feature(Tag::new(b"Buzz"), empty, 1); self.ot_map.enable_feature(Tag::new(b"BUZZ"), empty, 1); for &(tag, flags) in COMMON_FEATURES {
136 self.ot_map.add_feature(tag, flags, 1);
137 }
138
139 if self.direction.is_horizontal() {
140 for &(tag, flags) in HORIZONTAL_FEATURES {
141 self.ot_map.add_feature(tag, flags, 1);
142 }
143 } else {
144 self.ot_map
153 .enable_feature(Tag::new(b"vert"), F_GLOBAL_SEARCH, 1);
154 }
155
156 if !user_features.is_empty() {
157 self.ot_map.is_simple = false;
158 }
159
160 for feature in user_features {
161 let flags = if feature.is_global() { F_GLOBAL } else { empty };
162 self.ot_map.add_feature(feature.tag, flags, feature.value);
163 }
164
165 if let Some(func) = self.shaper.override_features {
166 func(self);
167 }
168 }
169
170 pub fn compile(mut self, features: &[Feature]) -> ShapePlan {
171 let ot_map = self.ot_map.compile();
172 let mut aat_map = AatMap::default();
173 if self.apply_morx {
174 self.aat_map.compile(self.layout.aat, &mut aat_map);
175 }
176
177 let frac_mask = ot_map.get_1_mask(Tag::new(b"frac"));
178 let numr_mask = ot_map.get_1_mask(Tag::new(b"numr"));
179 let dnom_mask = ot_map.get_1_mask(Tag::new(b"dnom"));
180 let has_frac = frac_mask != 0 || (numr_mask != 0 && dnom_mask != 0);
181
182 let rtlm_mask = ot_map.get_1_mask(Tag::new(b"rtlm"));
183 let has_vert = ot_map.get_1_mask(Tag::new(b"vert")) != 0;
184
185 let horizontal = self.direction.is_horizontal();
186 let kern_tag = if horizontal {
187 Tag::new(b"kern")
188 } else {
189 Tag::new(b"vkrn")
190 };
191 let kern_mask = ot_map.get_mask(kern_tag).0;
192 let requested_kerning = kern_mask != 0;
193
194 let has_gpos_kern = ot_map
195 .get_feature_index(LayoutTableKind::Gpos, kern_tag)
196 .is_some();
197 let disable_gpos = self.shaper.gpos_tag.is_some()
198 && self.shaper.gpos_tag != ot_map.chosen_script(LayoutTableKind::Gpos);
199
200 let fallback_glyph_classes = !has_glyph_classes(self.layout.ot);
202
203 let apply_morx = self.apply_morx;
205
206 let mut apply_gpos = false;
207 let mut apply_kerx = false;
208 let mut apply_kern = false;
209
210 let has_kerx = self.layout.aat.kerx.is_some();
212 let has_gsub = !apply_morx && self.layout.ot.gsub().is_some();
213 let has_gpos = !disable_gpos && self.layout.ot.gpos().is_some();
214
215 if has_kerx && !(has_gsub && has_gpos) {
218 apply_kerx = true;
219 } else if has_gpos {
220 apply_gpos = true;
221 }
222
223 if !apply_kerx && (!has_gpos_kern || !apply_gpos) {
224 if has_kerx {
225 apply_kerx = true;
226 } else if has_kerning(self.layout.aat) {
227 apply_kern = self.script_fallback_position;
228 }
229 }
230
231 let apply_fallback_kern = !(apply_gpos || apply_kerx || apply_kern);
232 let zero_marks = self.script_zero_marks
233 && !apply_kerx
234 && (!apply_kern || !has_machine_kerning(self.layout.aat));
235
236 let has_gpos_mark = ot_map.get_1_mask(Tag::new(b"mark")) != 0;
237
238 let mut adjust_mark_positioning_when_zeroing =
239 !apply_gpos && !apply_kerx && (!apply_kern || !has_cross_kerning(self.layout.aat));
240
241 let fallback_mark_positioning =
242 adjust_mark_positioning_when_zeroing && self.script_fallback_position;
243
244 if apply_morx {
248 adjust_mark_positioning_when_zeroing = false;
249 }
250
251 let apply_trak = self.layout.apply_trak;
254
255 let mut plan = ShapePlan {
256 direction: self.direction,
257 script: self.script,
258 language: self.language,
259 shaper: self.shaper,
260 ot_map,
261 aat_map,
262 data: None,
263 frac_mask,
264 numr_mask,
265 dnom_mask,
266 rtlm_mask,
267 kern_mask,
268 requested_kerning,
269 has_frac,
270 has_vert,
271 has_gpos_mark,
272 zero_marks,
273 fallback_glyph_classes,
274 fallback_mark_positioning,
275 adjust_mark_positioning_when_zeroing,
276 apply_gpos,
277 apply_kern,
278 apply_fallback_kern,
279 apply_kerx,
280 apply_morx,
281 apply_trak,
282 user_features: features.into(),
283 };
284
285 if let Some(func) = self.shaper.create_data {
286 plan.data = Some(func(&plan));
287 }
288
289 plan
290 }
291}