Skip to main content

textflow/shaping/
scripts.rs

1use super::{
2    FontFeature, GlyphBuffer, GlyphMask, LookupRequest, LookupStatus, ScriptProvider, ShapeError,
3    ShapeRequest, TextRange,
4};
5use crate::unicode::{graphemes, Script};
6
7const ALL: GlyphMask = GlyphMask::ALL;
8
9fn enabled(request: &ShapeRequest<'_>, tag: [u8; 4], default: bool) -> bool {
10    request
11        .features
12        .iter()
13        .filter(|feature| feature.tag == tag && overlaps(feature, request))
14        .fold(default, |_, feature| feature.value != 0)
15}
16
17fn overlaps(feature: &FontFeature, request: &ShapeRequest<'_>) -> bool {
18    feature.range.end > request.range.start as u32 && feature.range.start < request.range.end as u32
19}
20
21fn substitute(
22    request: &ShapeRequest<'_>,
23    font: &dyn super::ShapingData,
24    glyphs: &mut GlyphBuffer<'_>,
25    tag: [u8; 4],
26    mask: GlyphMask,
27    default: bool,
28) -> Result<LookupStatus, ShapeError> {
29    if !enabled(request, tag, default) {
30        return Ok(LookupStatus::NotFound);
31    }
32    font.substitute(LookupRequest::new(request, tag, mask), glyphs)
33}
34
35fn position(
36    request: &ShapeRequest<'_>,
37    font: &dyn super::ShapingData,
38    glyphs: &mut GlyphBuffer<'_>,
39    tag: [u8; 4],
40    default: bool,
41) -> Result<LookupStatus, ShapeError> {
42    if !enabled(request, tag, default) {
43        return Ok(LookupStatus::NotFound);
44    }
45    font.position(LookupRequest::new(request, tag, ALL), glyphs)
46}
47
48fn normalize_clusters(request: &ShapeRequest<'_>, glyphs: &mut GlyphBuffer<'_>) {
49    let text = &request.text[request.range.clone()];
50    for cluster in graphemes(text) {
51        let range = TextRange::new(
52            (request.range.start + cluster.range.start) as u32,
53            (request.range.start + cluster.range.end) as u32,
54        );
55        let mut matched = 0;
56        for glyph in glyphs.glyphs_mut() {
57            if glyph.cluster.start >= range.start && glyph.cluster.start < range.end {
58                glyph.cluster = range;
59                matched += 1;
60            }
61        }
62        if matched > 1 {
63            for glyph in glyphs.glyphs_mut() {
64                if glyph.cluster == range {
65                    glyph.set_unsafe_to_break(true);
66                }
67            }
68        }
69    }
70}
71
72#[cfg(feature = "script-arabic")]
73/// Built-in Arabic script provider.
74pub const ARABIC: Arabic = Arabic;
75
76#[cfg(feature = "script-arabic")]
77/// Applies Arabic joining forms, ligatures, cursive attachment, and mark positioning.
78pub struct Arabic;
79
80#[cfg(feature = "script-arabic")]
81const ISOL: GlyphMask = GlyphMask::new(1 << 0);
82#[cfg(feature = "script-arabic")]
83const INIT: GlyphMask = GlyphMask::new(1 << 1);
84#[cfg(feature = "script-arabic")]
85const MEDI: GlyphMask = GlyphMask::new(1 << 2);
86#[cfg(feature = "script-arabic")]
87const FINA: GlyphMask = GlyphMask::new(1 << 3);
88
89#[cfg(feature = "script-arabic")]
90#[derive(Clone, Copy, Eq, PartialEq)]
91enum Joining {
92    None,
93    Right,
94    Dual,
95    Transparent,
96}
97
98#[cfg(feature = "script-arabic")]
99impl Joining {
100    const fn joins_previous(self) -> bool {
101        matches!(self, Self::Right | Self::Dual)
102    }
103
104    const fn joins_next(self) -> bool {
105        matches!(self, Self::Dual)
106    }
107}
108
109#[cfg(feature = "script-arabic")]
110fn joining(character: char) -> Joining {
111    let scalar = character as u32;
112    if matches!(
113        scalar,
114        0x0610..=0x061A | 0x064B..=0x065F | 0x0670 | 0x06D6..=0x06ED | 0x08D3..=0x08FF
115    ) {
116        return Joining::Transparent;
117    }
118    if matches!(
119        scalar,
120        0x0622..=0x0625
121            | 0x0627
122            | 0x0629
123            | 0x062F..=0x0632
124            | 0x0648
125            | 0x0671..=0x0673
126            | 0x0675..=0x0677
127            | 0x0688..=0x0699
128            | 0x06C0
129            | 0x06C3..=0x06CB
130            | 0x06CD
131            | 0x06CF
132            | 0x06D2..=0x06D3
133    ) {
134        return Joining::Right;
135    }
136    if matches!(
137        scalar,
138        0x0626
139            | 0x0628
140            | 0x062A..=0x062E
141            | 0x0633..=0x063A
142            | 0x0641..=0x0647
143            | 0x0649..=0x064A
144            | 0x066E..=0x066F
145            | 0x0678..=0x0687
146            | 0x069A..=0x06BF
147            | 0x06CC
148            | 0x06CE
149            | 0x06D0..=0x06D1
150            | 0x06FA..=0x06FC
151    ) {
152        return Joining::Dual;
153    }
154    Joining::None
155}
156
157#[cfg(feature = "script-arabic")]
158fn character_at(
159    request: &ShapeRequest<'_>,
160    glyphs: &GlyphBuffer<'_>,
161    index: usize,
162) -> Option<char> {
163    let start = usize::try_from(glyphs.get(index)?.cluster.start).ok()?;
164    request.text.get(start..)?.chars().next()
165}
166
167#[cfg(feature = "script-arabic")]
168fn neighbor(
169    request: &ShapeRequest<'_>,
170    glyphs: &GlyphBuffer<'_>,
171    index: usize,
172    previous: bool,
173) -> Option<Joining> {
174    if previous {
175        for slot in (0..index).rev() {
176            let class = joining(character_at(request, glyphs, slot)?);
177            if class != Joining::Transparent {
178                return Some(class);
179            }
180        }
181    } else {
182        for slot in index + 1..glyphs.len() {
183            let class = joining(character_at(request, glyphs, slot)?);
184            if class != Joining::Transparent {
185                return Some(class);
186            }
187        }
188    }
189    None
190}
191
192#[cfg(feature = "script-arabic")]
193fn assign_joining_masks(
194    request: &ShapeRequest<'_>,
195    glyphs: &mut GlyphBuffer<'_>,
196) -> Result<bool, ShapeError> {
197    let mut joined = false;
198    for index in 0..glyphs.len() {
199        let current =
200            joining(character_at(request, glyphs, index).ok_or(ShapeError::InvalidGlyphRange)?);
201        if current == Joining::Transparent {
202            glyphs.set_mask(index, GlyphMask::NONE)?;
203            continue;
204        }
205        let previous = neighbor(request, glyphs, index, true)
206            .is_some_and(|class| class.joins_next() && current.joins_previous());
207        let next = neighbor(request, glyphs, index, false)
208            .is_some_and(|class| current.joins_next() && class.joins_previous());
209        let mask = match (previous, next) {
210            (false, false) => ISOL,
211            (false, true) => INIT,
212            (true, true) => MEDI,
213            (true, false) => FINA,
214        };
215        joined |= previous || next;
216        if previous || next {
217            glyphs
218                .get_mut(index)
219                .expect("valid joining slot")
220                .set_unsafe_to_break(true);
221        }
222        glyphs.set_mask(index, mask)?;
223    }
224    Ok(joined)
225}
226
227#[cfg(feature = "script-arabic")]
228impl ScriptProvider for Arabic {
229    fn script(&self) -> Script {
230        Script::Arabic
231    }
232
233    fn substitute(
234        &self,
235        request: &ShapeRequest<'_>,
236        font: &dyn super::ShapingData,
237        glyphs: &mut GlyphBuffer<'_>,
238    ) -> Result<(), ShapeError> {
239        let joined = assign_joining_masks(request, glyphs)?;
240        normalize_clusters(request, glyphs);
241        substitute(request, font, glyphs, *b"ccmp", ALL, true)?;
242        substitute(request, font, glyphs, *b"locl", ALL, true)?;
243        let mut forms = 0;
244        for (tag, mask) in [
245            (*b"isol", ISOL),
246            (*b"fina", FINA),
247            (*b"medi", MEDI),
248            (*b"init", INIT),
249        ] {
250            forms += usize::from(
251                substitute(request, font, glyphs, tag, mask, true)? == LookupStatus::Applied,
252            );
253        }
254        if joined && forms == 0 {
255            return Err(ShapeError::ShapingUnavailable);
256        }
257        for tag in [*b"rlig", *b"calt", *b"liga"] {
258            substitute(request, font, glyphs, tag, ALL, true)?;
259        }
260        Ok(())
261    }
262
263    fn position(
264        &self,
265        request: &ShapeRequest<'_>,
266        font: &dyn super::ShapingData,
267        glyphs: &mut GlyphBuffer<'_>,
268    ) -> Result<(), ShapeError> {
269        position(request, font, glyphs, *b"curs", true)?;
270        position(request, font, glyphs, *b"kern", true)?;
271        let has_marks = request.text[request.range.clone()]
272            .chars()
273            .any(|character| joining(character) == Joining::Transparent);
274        let mark = position(request, font, glyphs, *b"mark", true)?;
275        let mkmk = position(request, font, glyphs, *b"mkmk", true)?;
276        if has_marks && mark == LookupStatus::NotFound && mkmk == LookupStatus::NotFound {
277            return Err(ShapeError::ShapingUnavailable);
278        }
279        Ok(())
280    }
281}
282
283#[cfg(feature = "script-thai")]
284/// Built-in Thai script provider.
285pub const THAI: Thai = Thai;
286
287#[cfg(feature = "script-thai")]
288/// Applies Thai cluster substitutions and mark positioning.
289pub struct Thai;
290
291#[cfg(feature = "script-thai")]
292fn thai_mark(character: char) -> bool {
293    matches!(character as u32, 0x0E31 | 0x0E34..=0x0E3A | 0x0E47..=0x0E4E)
294}
295
296#[cfg(feature = "script-thai")]
297impl ScriptProvider for Thai {
298    fn script(&self) -> Script {
299        Script::Thai
300    }
301
302    fn substitute(
303        &self,
304        request: &ShapeRequest<'_>,
305        font: &dyn super::ShapingData,
306        glyphs: &mut GlyphBuffer<'_>,
307    ) -> Result<(), ShapeError> {
308        normalize_clusters(request, glyphs);
309        for tag in [*b"ccmp", *b"locl", *b"liga"] {
310            substitute(request, font, glyphs, tag, ALL, true)?;
311        }
312        Ok(())
313    }
314
315    fn position(
316        &self,
317        request: &ShapeRequest<'_>,
318        font: &dyn super::ShapingData,
319        glyphs: &mut GlyphBuffer<'_>,
320    ) -> Result<(), ShapeError> {
321        position(request, font, glyphs, *b"kern", true)?;
322        let has_marks = request.text[request.range.clone()].chars().any(thai_mark);
323        let mark = position(request, font, glyphs, *b"mark", true)?;
324        let mkmk = position(request, font, glyphs, *b"mkmk", true)?;
325        if has_marks && mark == LookupStatus::NotFound && mkmk == LookupStatus::NotFound {
326            return Err(ShapeError::ShapingUnavailable);
327        }
328        Ok(())
329    }
330}
331
332#[cfg(test)]
333mod tests {
334    use super::*;
335    use crate::bidi::Direction;
336    use crate::shaping::{
337        FlowPoint, FontAccessError, FontId, GlyphId, GlyphSource, ShapedGlyph, ShapingData,
338    };
339
340    struct Font;
341
342    impl GlyphSource for Font {
343        fn id(&self) -> FontId {
344            FontId::new(1)
345        }
346        fn metrics(&self) -> Result<super::super::FontMetrics, FontAccessError> {
347            Ok(super::super::FontMetrics::default())
348        }
349        fn glyph_for(&self, character: char) -> Result<Option<GlyphId>, FontAccessError> {
350            Ok(Some(GlyphId::new(character as u16)))
351        }
352        fn glyph_advance(&self, _glyph: GlyphId) -> Result<FlowPoint, FontAccessError> {
353            Ok(FlowPoint { x: 10, y: 0 })
354        }
355    }
356
357    struct Data;
358    impl ShapingData for Data {
359        fn substitute(
360            &self,
361            request: LookupRequest<'_, '_>,
362            glyphs: &mut GlyphBuffer<'_>,
363        ) -> Result<LookupStatus, ShapeError> {
364            for index in 0..glyphs.len() {
365                if glyphs.mask(index).is_some_and(|mask| request.selects(mask)) {
366                    glyphs.set_glyph(index, GlyphId::new(100 + request.feature()[0] as u16))?;
367                }
368            }
369            Ok(LookupStatus::Applied)
370        }
371        fn position(
372            &self,
373            _request: LookupRequest<'_, '_>,
374            _glyphs: &mut GlyphBuffer<'_>,
375        ) -> Result<LookupStatus, ShapeError> {
376            Ok(LookupStatus::Applied)
377        }
378    }
379
380    struct MissingData;
381
382    impl ShapingData for MissingData {
383        fn substitute(
384            &self,
385            _request: LookupRequest<'_, '_>,
386            _glyphs: &mut GlyphBuffer<'_>,
387        ) -> Result<LookupStatus, ShapeError> {
388            Ok(LookupStatus::NotFound)
389        }
390
391        fn position(
392            &self,
393            _request: LookupRequest<'_, '_>,
394            _glyphs: &mut GlyphBuffer<'_>,
395        ) -> Result<LookupStatus, ShapeError> {
396            Ok(LookupStatus::NotFound)
397        }
398    }
399
400    #[cfg(feature = "script-arabic")]
401    struct FormData;
402
403    #[cfg(feature = "script-arabic")]
404    impl ShapingData for FormData {
405        fn substitute(
406            &self,
407            request: LookupRequest<'_, '_>,
408            glyphs: &mut GlyphBuffer<'_>,
409        ) -> Result<LookupStatus, ShapeError> {
410            let Some(form) = [*b"isol", *b"init", *b"medi", *b"fina"]
411                .iter()
412                .position(|tag| *tag == request.feature())
413            else {
414                return Ok(LookupStatus::NotFound);
415            };
416            for index in 0..glyphs.len() {
417                if glyphs.mask(index).is_some_and(|mask| request.selects(mask)) {
418                    glyphs.set_glyph(index, GlyphId::new(200 + form as u16))?;
419                }
420            }
421            Ok(LookupStatus::Applied)
422        }
423
424        fn position(
425            &self,
426            _request: LookupRequest<'_, '_>,
427            _glyphs: &mut GlyphBuffer<'_>,
428        ) -> Result<LookupStatus, ShapeError> {
429            Ok(LookupStatus::Applied)
430        }
431    }
432
433    #[cfg(feature = "script-arabic")]
434    #[test]
435    fn arabic_assigns_contextual_forms_without_allocating() {
436        let providers: [&dyn ScriptProvider; 1] = [&ARABIC];
437        let face = super::super::ScriptTypeface::new(&Font, &Data).with_scripts(&providers);
438        let text = "مرحبا";
439        let request =
440            ShapeRequest::new(text, 0..text.len(), Direction::RightToLeft, Script::Arabic);
441        let mut output = [ShapedGlyph::default(); 5];
442        let count = super::super::Typeface::shape_into(&face, &request, &mut output).unwrap();
443        assert_eq!(count, 5);
444        assert!(output
445            .iter()
446            .all(|glyph| glyph.glyph_id() == GlyphId::new(208)));
447        assert!(output.iter().all(|glyph| glyph.unsafe_to_break()));
448    }
449
450    #[cfg(feature = "script-arabic")]
451    #[test]
452    fn arabic_joining_masks_skip_transparent_marks() {
453        let text = "بَبب";
454        let request =
455            ShapeRequest::new(text, 0..text.len(), Direction::RightToLeft, Script::Arabic);
456        let mut output = [ShapedGlyph::default(); 4];
457        for (slot, (offset, character)) in output.iter_mut().zip(text.char_indices()) {
458            *slot = ShapedGlyph::new(
459                GlyphId::new(character as u16),
460                TextRange::new(offset as u32, (offset + character.len_utf8()) as u32),
461            );
462        }
463        let mut glyphs = GlyphBuffer::new(&mut output, 4).unwrap();
464
465        assert!(assign_joining_masks(&request, &mut glyphs).unwrap());
466        assert_eq!(glyphs.mask(0), Some(INIT));
467        assert_eq!(glyphs.mask(1), Some(GlyphMask::NONE));
468        assert_eq!(glyphs.mask(2), Some(MEDI));
469        assert_eq!(glyphs.mask(3), Some(FINA));
470    }
471
472    #[cfg(feature = "script-arabic")]
473    #[test]
474    fn arabic_marks_do_not_receive_joining_forms() {
475        let providers: [&dyn ScriptProvider; 1] = [&ARABIC];
476        let face = super::super::ScriptTypeface::new(&Font, &FormData).with_scripts(&providers);
477        let text = "بَب";
478        let request =
479            ShapeRequest::new(text, 0..text.len(), Direction::RightToLeft, Script::Arabic);
480        let mut output = [ShapedGlyph::default(); 3];
481
482        let count = super::super::Typeface::shape_into(&face, &request, &mut output).unwrap();
483
484        assert_eq!(count, 3);
485        assert_eq!(output[1].glyph_id(), GlyphId::new('\u{64e}' as u16));
486        assert_ne!(output[0].cluster, output[1].cluster);
487        assert_eq!(output[1].cluster, output[2].cluster);
488    }
489
490    #[cfg(feature = "script-arabic")]
491    #[test]
492    fn arabic_marks_require_positioning_data() {
493        let providers: [&dyn ScriptProvider; 1] = [&ARABIC];
494        let face = super::super::ScriptTypeface::new(&Font, &MissingData).with_scripts(&providers);
495        let text = "بَ";
496        let request =
497            ShapeRequest::new(text, 0..text.len(), Direction::RightToLeft, Script::Arabic);
498        let mut output = [ShapedGlyph::default(); 2];
499
500        assert_eq!(
501            super::super::Typeface::shape_into(&face, &request, &mut output),
502            Err(ShapeError::ShapingUnavailable)
503        );
504    }
505
506    #[cfg(feature = "script-thai")]
507    #[test]
508    fn thai_keeps_base_and_mark_in_one_cluster() {
509        let providers: [&dyn ScriptProvider; 1] = [&THAI];
510        let face = super::super::ScriptTypeface::new(&Font, &Data).with_scripts(&providers);
511        let text = "กิ";
512        let request = ShapeRequest::new(text, 0..text.len(), Direction::LeftToRight, Script::Thai);
513        let mut output = [ShapedGlyph::default(); 2];
514        let count = super::super::Typeface::shape_into(&face, &request, &mut output).unwrap();
515        assert_eq!(count, 2);
516        assert!(output
517            .iter()
518            .all(|glyph| glyph.glyph_id() == GlyphId::new(208)));
519        assert_eq!(output[0].cluster, output[1].cluster);
520        assert!(output.iter().all(|glyph| glyph.unsafe_to_break()));
521    }
522
523    #[cfg(feature = "script-thai")]
524    #[test]
525    fn thai_marks_require_positioning_data() {
526        let providers: [&dyn ScriptProvider; 1] = [&THAI];
527        let face = super::super::ScriptTypeface::new(&Font, &MissingData).with_scripts(&providers);
528        let text = "กิ";
529        let request = ShapeRequest::new(text, 0..text.len(), Direction::LeftToRight, Script::Thai);
530        let mut output = [ShapedGlyph::default(); 2];
531
532        assert_eq!(
533            super::super::Typeface::shape_into(&face, &request, &mut output),
534            Err(ShapeError::ShapingUnavailable)
535        );
536    }
537}