Skip to main content

harfrust/ot/shaper/
hebrew.rs

1use super::Tag;
2use super::*;
3use crate::buffer::Buffer;
4use crate::plan::ShapePlan;
5use crate::unicode::{self, combining_class, modified_combining_class, Codepoint};
6
7pub const HEBREW_SHAPER: OtShaper = OtShaper {
8    collect_features: None,
9    override_features: None,
10    create_data: None,
11    preprocess_text: None,
12    postprocess_glyphs: None,
13    normalization_preference: NormalizationMode::Auto,
14    decompose: None,
15    compose: Some(compose),
16    setup_masks: None,
17    gpos_tag: Some(Tag::new(b"hebr")),
18    reorder_marks: Some(reorder_marks_hebrew),
19    zero_width_marks: ZeroWidthMarks::ByGdefLate,
20    fallback_position: true,
21};
22
23fn reorder_marks_hebrew(_: &ShapePlan, buffer: &mut Buffer, start: usize, end: usize) {
24    let marks = buffer.info.get(start..end).unwrap_or_default();
25    let found = marks.windows(3).position(|window| {
26        let [c0, c1, c2] = window else {
27            return false;
28        };
29
30        // patach or qamats
31        (c0.modified_combining_class() == modified_combining_class::CCC17
32            || c0.modified_combining_class() == modified_combining_class::CCC18)
33            // sheva or hiriq
34            && (c1.modified_combining_class() == modified_combining_class::CCC10
35                || c1.modified_combining_class() == modified_combining_class::CCC14)
36            // meteg or below
37            && (c2.modified_combining_class() == modified_combining_class::CCC22
38                || c2.modified_combining_class() == combining_class::Below)
39    });
40
41    if let Some(offset) = found {
42        let i = start + offset + 2;
43        buffer.merge_clusters(i - 1, i + 1);
44        if let Some(pair) = buffer.info.get_mut(i - 1..=i) {
45            pair.swap(0, 1);
46        }
47    }
48}
49
50static S_DAGESH_FORMS: &[Codepoint] = &[
51    0xFB30, // ALEF
52    0xFB31, // BET
53    0xFB32, // GIMEL
54    0xFB33, // DALET
55    0xFB34, // HE
56    0xFB35, // VAV
57    0xFB36, // ZAYIN
58    0x0000, // HET
59    0xFB38, // TET
60    0xFB39, // YOD
61    0xFB3A, // FINAL KAF
62    0xFB3B, // KAF
63    0xFB3C, // LAMED
64    0x0000, // FINAL MEM
65    0xFB3E, // MEM
66    0x0000, // FINAL NUN
67    0xFB40, // NUN
68    0xFB41, // SAMEKH
69    0x0000, // AYIN
70    0xFB43, // FINAL PE
71    0xFB44, // PE
72    0x0000, // FINAL TSADI
73    0xFB46, // TSADI
74    0xFB47, // QOF
75    0xFB48, // RESH
76    0xFB49, // SHIN
77    0xFB4A, // TAV
78];
79
80fn compose(ctx: &NormalizeContext, a: Codepoint, b: Codepoint) -> Option<Codepoint> {
81    // Hebrew presentation-form shaping.
82    // https://bugzilla.mozilla.org/show_bug.cgi?id=728866
83    // Hebrew presentation forms with dagesh, for characters U+05D0..05EA;
84    // Note that some letters do not have a dagesh presForm encoded.
85    match unicode::compose(a, b) {
86        Some(c) => Some(c),
87        None if !ctx.plan.has_gpos_mark => {
88            // Special-case Hebrew presentation forms that are excluded from
89            // standard normalization, but wanted for old fonts.
90            match b {
91                0x05B4 => {
92                    // HIRIQ
93                    match a {
94                        0x05D9 => Some(0xFB1D), // YOD
95                        _ => None,
96                    }
97                }
98                0x05B7 => {
99                    // PATAH
100                    match a {
101                        0x05D9 => Some(0xFB1F), // YIDDISH YOD YOD
102                        0x05D0 => Some(0xFB2E), // ALEF
103                        _ => None,
104                    }
105                }
106                0x05B8 => {
107                    // QAMATS
108                    match a {
109                        0x05D0 => Some(0xFB2F), // ALEF
110                        _ => None,
111                    }
112                }
113                0x05B9 => {
114                    // HOLAM
115                    match a {
116                        0x05D5 => Some(0xFB4B), // VAV
117                        _ => None,
118                    }
119                }
120                0x05BC => {
121                    // DAGESH
122                    match a {
123                        0x05D0..=0x05EA => {
124                            let c = S_DAGESH_FORMS[a as usize - 0x05D0];
125                            if c != 0 {
126                                Some(c)
127                            } else {
128                                None
129                            }
130                        }
131                        0xFB2A => Some(0xFB2C), // SHIN WITH SHIN DOT
132                        0xFB2B => Some(0xFB2D), // SHIN WITH SIN DOT
133                        _ => None,
134                    }
135                }
136                0x05BF => {
137                    // RAFE
138                    match a {
139                        0x05D1 => Some(0xFB4C), // BET
140                        0x05DB => Some(0xFB4D), // KAF
141                        0x05E4 => Some(0xFB4E), // PE
142                        _ => None,
143                    }
144                }
145                0x05C1 => {
146                    // SHIN DOT
147                    match a {
148                        0x05E9 => Some(0xFB2A), // SHIN
149                        0xFB49 => Some(0xFB2C), // SHIN WITH DAGESH
150                        _ => None,
151                    }
152                }
153                0x05C2 => {
154                    // SIN DOT
155                    match a {
156                        0x05E9 => Some(0xFB2B), // SHIN
157                        0xFB49 => Some(0xFB2D), // SHIN WITH DAGESH
158                        _ => None,
159                    }
160                }
161                _ => None,
162            }
163        }
164        None => None,
165    }
166}