Skip to main content

harfrust/ot/shaper/
arabic_fallback.rs

1use super::arabic_table::{LIGATURE_3_TABLE, LIGATURE_MARK_TABLE, LIGATURE_TABLE, SHAPING_TABLE};
2use crate::buffer::Buffer;
3use crate::ot::apply::ApplyContext;
4use crate::ot::layout::{apply_synthesized_subst_lookup, LayoutTableKind};
5use crate::ot::lookup::{cache_synthesized_subst, CachedLookupRef};
6use crate::ot::lookup_flags;
7use crate::plan::ShapePlan;
8use crate::ShaperFont;
9use crate::{Mask, Tag};
10use alloc::vec::Vec;
11
12const FALLBACK_FEATURES: [Tag; 7] = [
13    Tag::new(b"init"),
14    Tag::new(b"medi"),
15    Tag::new(b"fina"),
16    Tag::new(b"isol"),
17    Tag::new(b"rlig"),
18    Tag::new(b"rlig"),
19    Tag::new(b"rlig"),
20];
21
22// HarfBuzz uses this signature to identify legacy Windows-1256 Arabic fonts.
23// These fonts encode Arabic glyphs at fixed glyph IDs rather than providing
24// Unicode presentation forms, so use the matching fallback GSUB recipe below.
25const WIN1256_SIGNATURE: &[(u32, u16)] = &[
26    (0x0627, 199), // ALEF
27    (0x0644, 225), // LAM
28    (0x0649, 236), // ALEF MAKSURA
29    (0x064A, 237), // YEH
30    (0x0652, 250), // SUKUN
31];
32
33const WIN1256_INIT_MAPPINGS: &[(u16, u16)] = &[
34    (198, 162),
35    (200, 4),
36    (201, 5),
37    (202, 5),
38    (203, 6),
39    (204, 7),
40    (205, 9),
41    (206, 11),
42    (211, 13),
43    (212, 14),
44    (213, 15),
45    (214, 26),
46    (218, 27),
47    (219, 30),
48    (221, 128),
49    (222, 131),
50    (223, 140),
51    (225, 141),
52    (227, 142),
53    (228, 143),
54    (229, 144),
55    (236, 154),
56    (237, 154),
57];
58
59const WIN1256_MEDI_MAPPINGS: &[(u16, u16)] = &[
60    (165, 170),
61    (178, 179),
62    (180, 185),
63    (198, 162),
64    (200, 4),
65    (201, 5),
66    (202, 5),
67    (203, 6),
68    (204, 7),
69    (205, 9),
70    (206, 11),
71    (211, 13),
72    (212, 14),
73    (213, 15),
74    (214, 26),
75    (218, 28),
76    (219, 31),
77    (221, 129),
78    (222, 138),
79    (223, 140),
80    (225, 141),
81    (227, 142),
82    (228, 143),
83    (229, 149),
84    (236, 154),
85    (237, 154),
86    (252, 255),
87];
88
89const WIN1256_FINA_MAPPINGS: &[(u16, u16)] = &[
90    (165, 170),
91    (178, 179),
92    (180, 185),
93    (194, 2),
94    (195, 1),
95    (197, 3),
96    (198, 181),
97    (199, 0),
98    (201, 159),
99    (204, 8),
100    (205, 10),
101    (206, 12),
102    (218, 29),
103    (219, 127),
104    (229, 152),
105    (236, 160),
106    (237, 156),
107    (252, 255),
108];
109
110const WIN1256_LAM_ALEF_LIGATURES: &[([u16; 2], u16)] = &[
111    ([225, 199], 165),
112    ([225, 195], 178),
113    ([225, 194], 180),
114    ([225, 197], 252),
115];
116
117const WIN1256_SHADDA_LIGATURES: &[([u16; 2], u16)] =
118    &[([248, 243], 172), ([248, 245], 173), ([248, 246], 175)];
119
120pub(crate) struct FallbackPlan {
121    lookups: Vec<FallbackLookup>,
122}
123
124struct FallbackLookup {
125    mask: Mask,
126    lookup: CachedLookupRef,
127}
128
129impl FallbackLookup {
130    fn new(mask: Mask, data: Vec<u8>) -> Option<Self> {
131        let lookup = cache_synthesized_subst(data)?;
132        Some(Self { mask, lookup })
133    }
134}
135
136impl FallbackPlan {
137    pub(crate) fn new(plan: &ShapePlan, font: &ShaperFont<'_, '_>) -> Option<Self> {
138        Self::new_unicode(plan, font).or_else(|| Self::new_win1256(plan, font))
139    }
140
141    fn new_unicode(plan: &ShapePlan, font: &ShaperFont<'_, '_>) -> Option<Self> {
142        let mut lookups = Vec::with_capacity(FALLBACK_FEATURES.len());
143
144        for (feature_index, feature) in FALLBACK_FEATURES.iter().enumerate() {
145            let mask = plan.ot_map.get_1_mask(*feature);
146            if mask == 0 {
147                continue;
148            }
149
150            let lookup = match feature_index {
151                0..=3 => synthesize_single_lookup(font, feature_index, mask),
152                4 => synthesize_ligature_lookup(
153                    font,
154                    LIGATURE_3_TABLE,
155                    lookup_flags::IGNORE_MARKS,
156                    mask,
157                ),
158                5 => synthesize_ligature_lookup(
159                    font,
160                    LIGATURE_TABLE,
161                    lookup_flags::IGNORE_MARKS,
162                    mask,
163                ),
164                6 => synthesize_ligature_lookup(font, LIGATURE_MARK_TABLE, 0, mask),
165                _ => unreachable!(),
166            };
167            if let Some(lookup) = lookup {
168                lookups.push(lookup);
169            }
170        }
171
172        (!lookups.is_empty()).then_some(Self { lookups })
173    }
174
175    fn new_win1256(plan: &ShapePlan, font: &ShaperFont<'_, '_>) -> Option<Self> {
176        if !is_win1256_font(font) {
177            return None;
178        }
179
180        let mut lookups = Vec::with_capacity(5);
181        add_direct_ligature_lookup(
182            &mut lookups,
183            plan.ot_map.get_1_mask(Tag::new(b"rlig")),
184            WIN1256_LAM_ALEF_LIGATURES,
185            lookup_flags::IGNORE_MARKS,
186        );
187        add_direct_single_lookup(
188            &mut lookups,
189            plan.ot_map.get_1_mask(Tag::new(b"init")),
190            WIN1256_INIT_MAPPINGS,
191        );
192        add_direct_single_lookup(
193            &mut lookups,
194            plan.ot_map.get_1_mask(Tag::new(b"medi")),
195            WIN1256_MEDI_MAPPINGS,
196        );
197        add_direct_single_lookup(
198            &mut lookups,
199            plan.ot_map.get_1_mask(Tag::new(b"fina")),
200            WIN1256_FINA_MAPPINGS,
201        );
202        add_direct_ligature_lookup(
203            &mut lookups,
204            plan.ot_map.get_1_mask(Tag::new(b"rlig")),
205            WIN1256_SHADDA_LIGATURES,
206            0,
207        );
208
209        (!lookups.is_empty()).then_some(Self { lookups })
210    }
211
212    pub(crate) fn apply(&self, font: &ShaperFont<'_, '_>, buffer: &mut Buffer) {
213        let mut ctx = ApplyContext::new(
214            LayoutTableKind::Gsub,
215            font.layout(),
216            font.scale,
217            buffer,
218            #[cfg(feature = "tracing")]
219            font,
220        );
221        for lookup in &self.lookups {
222            ctx.set_lookup_mask(lookup.mask);
223            apply_synthesized_subst_lookup(&mut ctx, lookup.lookup.get());
224        }
225    }
226}
227
228fn is_win1256_font(font: &ShaperFont<'_, '_>) -> bool {
229    WIN1256_SIGNATURE.iter().all(|&(codepoint, glyph)| {
230        font.nominal_glyph(codepoint)
231            .is_some_and(|actual| actual.to_u32() == u32::from(glyph))
232    })
233}
234
235fn add_direct_single_lookup(
236    lookups: &mut Vec<FallbackLookup>,
237    mask: Mask,
238    mappings: &[(u16, u16)],
239) {
240    if mask == 0 {
241        return;
242    }
243
244    let Some(data) = serialize_single_lookup(mappings, lookup_flags::IGNORE_MARKS) else {
245        return;
246    };
247    if let Some(lookup) = FallbackLookup::new(mask, data) {
248        lookups.push(lookup);
249    }
250}
251
252fn add_direct_ligature_lookup<const N: usize>(
253    lookups: &mut Vec<FallbackLookup>,
254    mask: Mask,
255    ligatures: &[([u16; N], u16)],
256    flags: u16,
257) {
258    if mask == 0 {
259        return;
260    }
261
262    if let Some(data) = serialize_ligature_lookup(ligatures, flags) {
263        if let Some(lookup) = FallbackLookup::new(mask, data) {
264            lookups.push(lookup);
265        }
266    }
267}
268
269fn glyph_for_codepoint(font: &ShaperFont<'_, '_>, codepoint: u16) -> Option<u16> {
270    let glyph = font.nominal_glyph(u32::from(codepoint))?;
271    u16::try_from(glyph.to_u32()).ok()
272}
273
274fn synthesize_single_lookup(
275    font: &ShaperFont<'_, '_>,
276    feature_index: usize,
277    mask: Mask,
278) -> Option<FallbackLookup> {
279    let mut mappings = Vec::with_capacity(SHAPING_TABLE.len());
280    for &(codepoint, forms) in SHAPING_TABLE {
281        let form = forms[feature_index];
282        if form == 0 {
283            continue;
284        }
285
286        let Some(glyph) = glyph_for_codepoint(font, codepoint) else {
287            continue;
288        };
289        let Some(substitute) = glyph_for_codepoint(font, form) else {
290            continue;
291        };
292        if glyph != substitute {
293            mappings.push((glyph, substitute));
294        }
295    }
296    if mappings.is_empty() {
297        return None;
298    }
299
300    crate::algs::stable_sort_by(&mut mappings, |a, b| a.0.cmp(&b.0));
301    FallbackLookup::new(
302        mask,
303        serialize_single_lookup(&mappings, lookup_flags::IGNORE_MARKS)?,
304    )
305}
306
307fn synthesize_ligature_lookup<const N: usize>(
308    font: &ShaperFont<'_, '_>,
309    rules: &[([u16; N], u16)],
310    flags: u16,
311    mask: Mask,
312) -> Option<FallbackLookup> {
313    let mut ligatures = Vec::with_capacity(rules.len());
314    for &(components, ligature) in rules {
315        let mut component_glyphs = [0; N];
316        let mut matched = true;
317        for (glyph, &codepoint) in component_glyphs.iter_mut().zip(components.iter()) {
318            let Some(component) = glyph_for_codepoint(font, codepoint) else {
319                matched = false;
320                break;
321            };
322            *glyph = component;
323        }
324        if !matched {
325            continue;
326        }
327        let Some(ligature) = glyph_for_codepoint(font, ligature) else {
328            continue;
329        };
330        ligatures.push((component_glyphs, ligature));
331    }
332    if ligatures.is_empty() {
333        return None;
334    }
335
336    crate::algs::stable_sort_by(&mut ligatures, |a, b| a.0[0].cmp(&b.0[0]));
337    FallbackLookup::new(mask, serialize_ligature_lookup(&ligatures, flags)?)
338}
339
340fn write_u16(data: &mut Vec<u8>, value: u16) {
341    data.extend_from_slice(&value.to_be_bytes());
342}
343
344fn patch_u16(data: &mut [u8], offset: usize, value: u16) {
345    data[offset..offset + 2].copy_from_slice(&value.to_be_bytes());
346}
347
348fn serialize_single_lookup(mappings: &[(u16, u16)], flags: u16) -> Option<Vec<u8>> {
349    let count = u16::try_from(mappings.len()).ok()?;
350    let coverage_offset =
351        u16::try_from(6usize.checked_add(mappings.len().checked_mul(2)?)?).ok()?;
352    let mut data = Vec::with_capacity(12 + mappings.len() * 4);
353
354    // Lookup table: type 1 (single substitution), one subtable at offset 8.
355    write_u16(&mut data, 1);
356    write_u16(&mut data, flags);
357    write_u16(&mut data, 1);
358    write_u16(&mut data, 8);
359
360    // SingleSubstFormat2 and its coverage table.
361    write_u16(&mut data, 2);
362    write_u16(&mut data, coverage_offset);
363    write_u16(&mut data, count);
364    for &(_, substitute) in mappings {
365        write_u16(&mut data, substitute);
366    }
367    write_u16(&mut data, 1);
368    write_u16(&mut data, count);
369    for &(glyph, _) in mappings {
370        write_u16(&mut data, glyph);
371    }
372
373    Some(data)
374}
375
376fn serialize_ligature_lookup<const N: usize>(
377    ligatures: &[([u16; N], u16)],
378    flags: u16,
379) -> Option<Vec<u8>> {
380    const LOOKUP_SIZE: usize = 8;
381
382    let mut set_starts = Vec::new();
383    for (index, (components, _)) in ligatures.iter().enumerate() {
384        if index == 0 || components[0] != ligatures[index - 1].0[0] {
385            set_starts.push(index);
386        }
387    }
388    let set_count = u16::try_from(set_starts.len()).ok()?;
389    let component_count = u16::try_from(N).ok()?;
390    let mut data = Vec::new();
391
392    // Lookup table: type 4 (ligature substitution), one subtable at offset 8.
393    write_u16(&mut data, 4);
394    write_u16(&mut data, flags);
395    write_u16(&mut data, 1);
396    write_u16(&mut data, LOOKUP_SIZE as u16);
397
398    // LigatureSubstFormat1 header. Fill offsets once their targets are written.
399    write_u16(&mut data, 1);
400    let coverage_offset_position = data.len();
401    write_u16(&mut data, 0);
402    write_u16(&mut data, set_count);
403    let set_offsets_position = data.len();
404    for _ in &set_starts {
405        write_u16(&mut data, 0);
406    }
407
408    let coverage_offset = u16::try_from(data.len().checked_sub(LOOKUP_SIZE)?).ok()?;
409    patch_u16(&mut data, coverage_offset_position, coverage_offset);
410    write_u16(&mut data, 1);
411    write_u16(&mut data, set_count);
412    for &set_start in &set_starts {
413        write_u16(&mut data, ligatures[set_start].0[0]);
414    }
415
416    for (set_index, &set_start) in set_starts.iter().enumerate() {
417        let set_end = set_starts
418            .get(set_index + 1)
419            .copied()
420            .unwrap_or(ligatures.len());
421        let set_start_in_data = data.len();
422        let set_offset = u16::try_from(set_start_in_data.checked_sub(LOOKUP_SIZE)?).ok()?;
423        patch_u16(&mut data, set_offsets_position + set_index * 2, set_offset);
424
425        let ligature_count = u16::try_from(set_end.checked_sub(set_start)?).ok()?;
426        write_u16(&mut data, ligature_count);
427        let ligature_offsets_position = data.len();
428        for _ in set_start..set_end {
429            write_u16(&mut data, 0);
430        }
431
432        for (ligature_index, &(components, ligature)) in
433            ligatures[set_start..set_end].iter().enumerate()
434        {
435            let ligature_offset = u16::try_from(data.len().checked_sub(set_start_in_data)?).ok()?;
436            patch_u16(
437                &mut data,
438                ligature_offsets_position + ligature_index * 2,
439                ligature_offset,
440            );
441            write_u16(&mut data, ligature);
442            write_u16(&mut data, component_count);
443            for component in &components[1..] {
444                write_u16(&mut data, *component);
445            }
446        }
447    }
448
449    Some(data)
450}
451
452#[cfg(test)]
453mod tests {
454    use super::*;
455
456    #[test]
457    fn serializes_single_substitution_lookup() {
458        let data = serialize_single_lookup(&[(3, 8), (7, 9)], lookup_flags::IGNORE_MARKS).unwrap();
459        assert_eq!(
460            data,
461            [
462                0, 1, 0, 8, 0, 1, 0, 8, // Lookup
463                0, 2, 0, 10, 0, 2, 0, 8, 0, 9, // SingleSubstFormat2
464                0, 1, 0, 2, 0, 3, 0, 7, // CoverageFormat1
465            ]
466        );
467        assert!(cache_synthesized_subst(data).is_some());
468    }
469
470    #[test]
471    fn serializes_ligature_substitution_lookup() {
472        let data = serialize_ligature_lookup(
473            &[([3, 4], 9), ([3, 5], 10), ([7, 8], 11)],
474            lookup_flags::IGNORE_MARKS,
475        )
476        .unwrap();
477        assert!(cache_synthesized_subst(data).is_some());
478    }
479
480    #[test]
481    fn win1256_tables_are_sorted() {
482        // The lookups are serialized from these tables as they are.
483        for mappings in [
484            WIN1256_INIT_MAPPINGS,
485            WIN1256_MEDI_MAPPINGS,
486            WIN1256_FINA_MAPPINGS,
487        ] {
488            assert!(mappings.is_sorted_by_key(|(glyph, _)| *glyph));
489        }
490        for ligatures in [WIN1256_LAM_ALEF_LIGATURES, WIN1256_SHADDA_LIGATURES] {
491            assert!(ligatures.is_sorted_by_key(|(components, _)| components[0]));
492        }
493    }
494
495    #[test]
496    fn serializes_win1256_fallback_lookups() {
497        let single =
498            serialize_single_lookup(WIN1256_MEDI_MAPPINGS, lookup_flags::IGNORE_MARKS).unwrap();
499        assert!(cache_synthesized_subst(single).is_some());
500
501        let ligature =
502            serialize_ligature_lookup(WIN1256_LAM_ALEF_LIGATURES, lookup_flags::IGNORE_MARKS)
503                .unwrap();
504        assert!(cache_synthesized_subst(ligature).is_some());
505    }
506}