1use alloc::boxed::Box;
2use core::any::Any;
3use smallvec::SmallVec;
4
5use crate::aat::map::AatMap;
6use crate::ot::shaper::OtShaper;
7
8use crate::ot::map::*;
9use crate::LayoutData;
10use crate::{font::Font, Direction, Feature, Language, Mask, Script, ShapePlanner, ShaperFont};
11use read_fonts::types::F2Dot14;
12
13pub struct ShapePlan {
18 pub(crate) direction: Direction,
19 pub(crate) script: Option<Script>,
20 pub(crate) language: Option<Language>,
21 pub(crate) shaper: &'static OtShaper,
22 pub(crate) ot_map: OtMap,
23 pub(crate) aat_map: AatMap,
24 pub(crate) data: Option<Box<dyn Any + Send + Sync>>,
25
26 pub(crate) frac_mask: Mask,
27 pub(crate) numr_mask: Mask,
28 pub(crate) dnom_mask: Mask,
29 pub(crate) rtlm_mask: Mask,
30 pub(crate) kern_mask: Mask,
31
32 pub(crate) requested_kerning: bool,
33 pub(crate) has_frac: bool,
34 pub(crate) has_vert: bool,
35 pub(crate) has_gpos_mark: bool,
36 pub(crate) zero_marks: bool,
37 pub(crate) fallback_glyph_classes: bool,
38 pub(crate) fallback_mark_positioning: bool,
39 pub(crate) adjust_mark_positioning_when_zeroing: bool,
40
41 pub(crate) apply_gpos: bool,
42 pub(crate) apply_fallback_kern: bool,
43 pub(crate) apply_kern: bool,
44 pub(crate) apply_kerx: bool,
45 pub(crate) apply_morx: bool,
46 pub(crate) apply_trak: bool,
47
48 pub(crate) user_features: SmallVec<[Feature; 4]>,
49}
50
51impl ShapePlan {
52 pub fn new(
58 font: &Font,
59 direction: Direction,
60 script: Option<Script>,
61 language: Option<&Language>,
62 user_features: &[Feature],
63 ) -> Self {
64 let shaping_font = ShaperFont::new(font);
65 Self::from_layout(
66 shaping_font.layout(),
67 direction,
68 script,
69 language,
70 user_features,
71 )
72 }
73
74 pub(crate) fn from_layout(
75 layout: LayoutData<'_>,
76 direction: Direction,
77 script: Option<Script>,
78 language: Option<&Language>,
79 user_features: &[Feature],
80 ) -> Self {
81 assert_ne!(
82 direction,
83 Direction::Invalid,
84 "Direction must not be Invalid"
85 );
86 let mut planner = ShapePlanner::new(layout, direction, script, language);
87 planner.collect_features(user_features);
88 planner.compile(user_features)
89 }
90
91 pub(crate) fn data<T: 'static>(&self) -> &T {
92 self.data.as_ref().unwrap().downcast_ref().unwrap()
93 }
94
95 pub fn direction(&self) -> Direction {
97 self.direction
98 }
99
100 pub fn script(&self) -> Option<Script> {
102 self.script
103 }
104
105 pub fn language(&self) -> Option<&Language> {
107 self.language.as_ref()
108 }
109}
110
111pub struct ShapePlanKey<'a> {
113 script: Option<Script>,
114 direction: Direction,
115 language: Option<&'a Language>,
116 pub(crate) feature_variations: [Option<u32>; 2],
117 lookup_coords: Option<&'a [F2Dot14]>,
119 features: &'a [Feature],
120}
121
122impl<'a> ShapePlanKey<'a> {
123 pub fn new(font: &'a Font, script: Option<Script>, direction: Direction) -> Self {
125 let variations = font.feature_variations();
126 Self {
127 script,
128 direction,
129 language: None,
130 feature_variations: [variations.gsub, variations.gpos],
131 lookup_coords: variations
132 .has_lookup_variations
133 .then(|| font.normalized_coords()),
134 features: &[],
135 }
136 }
137
138 pub fn language(mut self, language: Option<&'a Language>) -> Self {
140 self.language = language;
141 self
142 }
143
144 pub fn features(mut self, features: &'a [Feature]) -> Self {
146 self.features = features;
147 self
148 }
149
150 pub fn matches(&self, plan: &ShapePlan) -> bool {
152 self.script == plan.script
153 && self.direction == plan.direction
154 && self.language == plan.language.as_ref()
155 && self.feature_variations == *plan.ot_map.feature_variations()
156 && self.lookup_coords == plan.ot_map.lookup_coords()
157 && features_equivalent(self.features, &plan.user_features)
158 }
159}
160
161fn features_equivalent(features_a: &[Feature], features_b: &[Feature]) -> bool {
162 if features_a.len() != features_b.len() {
163 return false;
164 }
165 for (a, b) in features_a.iter().zip(features_b) {
166 if a.tag != b.tag
167 || a.value != b.value
168 || (a.start == Feature::GLOBAL_START && a.end == Feature::GLOBAL_END)
169 != (b.start == Feature::GLOBAL_START && b.end == Feature::GLOBAL_END)
170 {
171 return false;
172 }
173 }
174 true
175}
176
177#[cfg(test)]
178mod tests {
179 use super::ShapePlan;
180
181 #[test]
182 fn test_shape_plan_is_send_and_sync() {
183 fn ensure_send_and_sync<T: Send + Sync>() {}
184 ensure_send_and_sync::<ShapePlan>();
185 }
186}