Skip to main content

harfrust/ot/shaper/
hangul.rs

1use alloc::boxed::Box;
2
3use super::*;
4use crate::normalize::NormalizationMode;
5use crate::ot::map::*;
6use crate::plan::ShapePlan;
7use crate::unicode::Codepoint;
8use crate::BufferFlags;
9use crate::Mask;
10use crate::ShaperFont;
11
12const LJMO: u8 = 1;
13const VJMO: u8 = 2;
14const TJMO: u8 = 3;
15
16impl GlyphInfo {
17    declare_buffer_var_alias!(
18        OT_SHAPER_VAR_U8_AUXILIARY_VAR,
19        u8,
20        HANGUL_SHAPING_FEATURE_VAR,
21        hangul_shaping_feature,
22        set_hangul_shaping_feature
23    );
24}
25
26fn collect_features_hangul(planner: &mut ShapePlanner) {
27    planner.ot_map.add_feature(Tag::new(b"ljmo"), F_NONE, 1);
28    planner.ot_map.add_feature(Tag::new(b"vjmo"), F_NONE, 1);
29    planner.ot_map.add_feature(Tag::new(b"tjmo"), F_NONE, 1);
30}
31
32fn override_features_hangul(planner: &mut ShapePlanner) {
33    // Uniscribe does not apply 'calt' for Hangul, and certain fonts
34    // (Noto Sans CJK, Source Han Sans, etc) apply all of jamo lookups
35    // in calt, which is not desirable.
36    //
37    // Rather than disabling 'calt' for the entire run, which also turns
38    // it off for any other characters in the run, allocate a mask for it
39    // and clear that mask on jamo only (in setup_masks_hangul). That
40    // keeps jamo out of reach of such lookups while letting the rest of
41    // the run shape with 'calt' as usual.
42    //
43    // https://github.com/harfbuzz/harfbuzz/discussions/4853
44    planner.ot_map.add_feature(Tag::new(b"calt"), F_NONE, 1);
45}
46
47struct HangulShapePlan {
48    mask_array: [Mask; 4],
49    calt_mask: Mask,
50}
51
52fn data_create_hangul(map: &OtMap) -> HangulShapePlan {
53    HangulShapePlan {
54        mask_array: [
55            0,
56            map.get_1_mask(Tag::new(b"ljmo")),
57            map.get_1_mask(Tag::new(b"vjmo")),
58            map.get_1_mask(Tag::new(b"tjmo")),
59        ],
60        calt_mask: map.get_1_mask(Tag::new(b"calt")),
61    }
62}
63
64const L_BASE: u32 = 0x1100;
65const V_BASE: u32 = 0x1161;
66const T_BASE: u32 = 0x11A7;
67const L_COUNT: u32 = 19;
68const V_COUNT: u32 = 21;
69const T_COUNT: u32 = 28;
70const N_COUNT: u32 = V_COUNT * T_COUNT;
71const S_COUNT: u32 = L_COUNT * N_COUNT;
72const S_BASE: u32 = 0xAC00;
73
74fn is_combining_l(u: u32) -> bool {
75    (L_BASE..L_BASE + L_COUNT).contains(&u)
76}
77
78fn is_combining_v(u: u32) -> bool {
79    (V_BASE..V_BASE + V_COUNT).contains(&u)
80}
81
82fn is_combining_t(u: u32) -> bool {
83    (T_BASE + 1..T_BASE + T_COUNT).contains(&u)
84}
85
86fn is_combined_s(u: u32) -> bool {
87    (S_BASE..S_BASE + S_COUNT).contains(&u)
88}
89
90fn is_l(u: u32) -> bool {
91    (0x1100..=0x115F).contains(&u) || (0xA960..=0xA97C).contains(&u)
92}
93
94fn is_v(u: u32) -> bool {
95    (0x1160..=0x11A7).contains(&u) || (0xD7B0..=0xD7C6).contains(&u)
96}
97
98fn is_t(u: u32) -> bool {
99    (0x11A8..=0x11FF).contains(&u) || (0xD7CB..=0xD7FB).contains(&u)
100}
101
102fn is_hangul_tone(u: u32) -> bool {
103    (0x302E..=0x302F).contains(&u)
104}
105
106fn is_zero_width_char(font: &ShaperFont<'_, '_>, c: Codepoint) -> bool {
107    if let Some(glyph) = font.nominal_glyph(c) {
108        font.glyph_h_advance(glyph) == 0
109    } else {
110        false
111    }
112}
113
114fn preprocess_text_hangul(
115    _: &ShapePlan,
116    font: &ShaperFont<'_, '_>,
117    buffer: &mut Buffer,
118) -> Option<()> {
119    buffer.allocate_var(GlyphInfo::HANGUL_SHAPING_FEATURE_VAR);
120
121    // Hangul syllables come in two shapes: LV, and LVT.  Of those:
122    //
123    //   - LV can be precomposed, or decomposed.  Lets call those
124    //     <LV> and <L,V>,
125    //   - LVT can be fully precomposed, partially precomposed, or
126    //     fully decomposed.  Ie. <LVT>, <LV,T>, or <L,V,T>.
127    //
128    // The composition / decomposition is mechanical.  However, not
129    // all <L,V> sequences compose, and not all <LV,T> sequences
130    // compose.
131    //
132    // Here are the specifics:
133    //
134    //   - <L>: U+1100..115F, U+A960..A97F
135    //   - <V>: U+1160..11A7, U+D7B0..D7C7
136    //   - <T>: U+11A8..11FF, U+D7CB..D7FB
137    //
138    //   - Only the <L,V> sequences for some of the U+11xx ranges combine.
139    //   - Only <LV,T> sequences for some of the Ts in U+11xx range combine.
140    //
141    // Here is what we want to accomplish in this shaper:
142    //
143    //   - If the whole syllable can be precomposed, do that,
144    //   - Otherwise, fully decompose and apply ljmo/vjmo/tjmo features.
145    //   - If a valid syllable is followed by a Hangul tone mark, reorder the tone
146    //     mark to precede the whole syllable - unless it is a zero-width glyph, in
147    //     which case we leave it untouched, assuming it's designed to overstrike.
148    //
149    // That is, of the different possible syllables:
150    //
151    //   <L>
152    //   <L,V>
153    //   <L,V,T>
154    //   <LV>
155    //   <LVT>
156    //   <LV, T>
157    //
158    // - <L> needs no work.
159    //
160    // - <LV> and <LVT> can stay the way they are if the font supports them, otherwise we
161    //   should fully decompose them if font supports.
162    //
163    // - <L,V> and <L,V,T> we should compose if the whole thing can be composed.
164    //
165    // - <LV,T> we should compose if the whole thing can be composed, otherwise we should
166    //   decompose.
167
168    buffer.clear_output();
169    // Extent of most recently seen syllable; valid only if start < end
170    let mut start = 0;
171    let mut end = 0;
172    buffer.idx = 0;
173    while buffer.idx < buffer.len {
174        let u = buffer.cur(0)?.glyph_id;
175        let c = buffer.cur(0)?.as_codepoint();
176
177        if is_hangul_tone(u) {
178            // We could cache the width of the tone marks and the existence of dotted-circle,
179            // but the use of the Hangul tone mark characters seems to be rare enough that
180            // I didn't bother for now.
181            if start < end && end == buffer.out_len {
182                // Tone mark follows a valid syllable; move it in front, unless it's zero width.
183                buffer.unsafe_to_break_from_outbuffer(Some(start), Some(buffer.idx));
184                buffer.next_glyph();
185                if !is_zero_width_char(font, c) {
186                    buffer.merge_out_clusters(start, end + 1);
187                    if let Some(syllable) = buffer.out_info_mut().get_mut(start..=end) {
188                        syllable.rotate_right(1);
189                    }
190                }
191            } else {
192                // No valid syllable as base for tone mark; try to insert dotted circle.
193                if !buffer
194                    .flags
195                    .contains(BufferFlags::DO_NOT_INSERT_DOTTED_CIRCLE)
196                    && font.has_glyph(0x25CC)
197                {
198                    let mut chars = [0; 2];
199                    if !is_zero_width_char(font, c) {
200                        chars[0] = u;
201                        chars[1] = 0x25CC;
202                    } else {
203                        chars[0] = 0x25CC;
204                        chars[1] = u;
205                    }
206
207                    buffer.replace_glyphs(1, 2, &chars);
208                } else {
209                    // No dotted circle available in the font; just leave tone mark untouched.
210                    buffer.next_glyph();
211                }
212            }
213
214            start = buffer.out_len;
215            end = buffer.out_len;
216            continue;
217        }
218
219        // Remember current position as a potential syllable start;
220        // will only be used if we set end to a later position.
221        start = buffer.out_len;
222
223        if is_l(u) && buffer.idx + 1 < buffer.len {
224            let l = u;
225            let v = buffer.cur(1)?.glyph_id;
226            if is_v(v) {
227                // Have <L,V> or <L,V,T>.
228                let mut t = 0;
229                let mut tindex = 0;
230                if buffer.idx + 2 < buffer.len {
231                    t = buffer.cur(2)?.glyph_id;
232                    if is_t(t) {
233                        // Only used if isCombiningT (t); otherwise invalid.
234                        tindex = t - T_BASE;
235                    } else {
236                        // The next character was not a trailing jamo.
237                        t = 0;
238                    }
239                }
240
241                let offset = if t != 0 { 3 } else { 2 };
242                buffer.unsafe_to_break(Some(buffer.idx), Some(buffer.idx + offset));
243
244                // We've got a syllable <L,V,T?>; see if it can potentially be composed.
245                if is_combining_l(l) && is_combining_v(v) && (t == 0 || is_combining_t(t)) {
246                    // Try to compose; if this succeeds, end is set to start+1.
247                    let s = S_BASE + (l - L_BASE) * N_COUNT + (v - V_BASE) * T_COUNT + tindex;
248                    if font.has_glyph(s) {
249                        let n = if t != 0 { 3 } else { 2 };
250                        buffer.replace_glyphs(n, 1, &[s]);
251                        end = start + 1;
252                        continue;
253                    }
254                }
255
256                // We didn't compose, either because it's an Old Hangul syllable without a
257                // precomposed character in Unicode, or because the font didn't support the
258                // necessary precomposed glyph.
259                // Set jamo features on the individual glyphs, and advance past them.
260                buffer.cur_mut(0)?.set_hangul_shaping_feature(LJMO);
261                buffer.next_glyph();
262                buffer.cur_mut(0)?.set_hangul_shaping_feature(VJMO);
263                buffer.next_glyph();
264                if t != 0 {
265                    buffer.cur_mut(0)?.set_hangul_shaping_feature(TJMO);
266                    buffer.next_glyph();
267                    end = start + 3;
268                } else {
269                    end = start + 2;
270                }
271
272                buffer.merge_out_grapheme_clusters(start, end);
273
274                continue;
275            }
276        } else if is_combined_s(u) {
277            // Have <LV>, <LVT>, or <LV,T>
278            let s = u;
279            let has_glyph = font.has_glyph(s);
280
281            let lindex = (s - S_BASE) / N_COUNT;
282            let nindex = (s - S_BASE) % N_COUNT;
283            let vindex = nindex / T_COUNT;
284            let tindex = nindex % T_COUNT;
285
286            if tindex == 0 && buffer.idx + 1 < buffer.len && is_combining_t(buffer.cur(1)?.glyph_id)
287            {
288                // <LV,T>, try to combine.
289                let new_tindex = buffer.cur(1)?.glyph_id - T_BASE;
290                let new_s = s + new_tindex;
291
292                if font.has_glyph(new_s) {
293                    buffer.replace_glyphs(2, 1, &[new_s]);
294                    end = start + 1;
295                    continue;
296                } else {
297                    // Mark unsafe between LV and T.
298                    buffer.unsafe_to_break(Some(buffer.idx), Some(buffer.idx + 2));
299                }
300            }
301
302            // Otherwise, decompose if font doesn't support <LV> or <LVT>,
303            // or if having non-combining <LV,T>.  Note that we already handled
304            // combining <LV,T> above.
305            if !has_glyph
306                || (tindex == 0 && buffer.idx + 1 < buffer.len && is_t(buffer.cur(1)?.glyph_id))
307            {
308                let decomposed = [L_BASE + lindex, V_BASE + vindex, T_BASE + tindex];
309                if font.has_glyph(decomposed[0])
310                    && font.has_glyph(decomposed[1])
311                    && (tindex == 0 || font.has_glyph(decomposed[2]))
312                {
313                    let mut s_len = if tindex != 0 { 3 } else { 2 };
314                    buffer.replace_glyphs(1, s_len, &decomposed);
315
316                    // If we decomposed an LV because of a non-combining T following,
317                    // we want to include this T in the syllable.
318                    if has_glyph && tindex == 0 {
319                        buffer.next_glyph();
320                        s_len += 1;
321                    }
322
323                    // We decomposed S: apply jamo features to the individual glyphs
324                    // that are now in `buffer.out_info`.
325                    end = start + s_len;
326
327                    let jamos = buffer
328                        .out_info_mut()
329                        .get_mut(start..end)
330                        .unwrap_or_default();
331                    for (info, feature) in jamos.iter_mut().zip([LJMO, VJMO, TJMO]) {
332                        info.set_hangul_shaping_feature(feature);
333                    }
334
335                    buffer.merge_out_grapheme_clusters(start, end);
336
337                    continue;
338                } else if tindex == 0
339                    && buffer.idx + 1 > buffer.len
340                    && is_t(buffer.cur(1)?.glyph_id)
341                {
342                    // Mark unsafe between LV and T.
343                    buffer.unsafe_to_break(Some(buffer.idx), Some(buffer.idx + 2));
344                }
345            }
346
347            if has_glyph {
348                // We didn't decompose the S, so just advance past it.
349                end = start + 1;
350                buffer.next_glyph();
351                continue;
352            }
353        }
354
355        // Didn't find a recognizable syllable, so we leave end <= start;
356        // this will prevent tone-mark reordering happening.
357        buffer.next_glyph();
358    }
359
360    buffer.sync();
361    Some(())
362}
363
364fn setup_masks_hangul(plan: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) {
365    let hangul_plan = plan.data::<HangulShapePlan>();
366    for info in buffer.info_slice_mut() {
367        info.mask |= hangul_plan.mask_array[info.hangul_shaping_feature() as usize];
368
369        // Keep 'calt' away from jamo; see override_features_hangul().
370        let u = info.glyph_id;
371        if is_l(u) || is_v(u) || is_t(u) {
372            info.mask &= !hangul_plan.calt_mask;
373        }
374    }
375
376    buffer.deallocate_var(GlyphInfo::HANGUL_SHAPING_FEATURE_VAR);
377}
378
379pub const HANGUL_SHAPER: OtShaper = OtShaper {
380    collect_features: Some(collect_features_hangul),
381    override_features: Some(override_features_hangul),
382    create_data: Some(|plan| Box::new(data_create_hangul(&plan.ot_map))),
383    preprocess_text: Some(preprocess_text_hangul),
384    postprocess_glyphs: None,
385    normalization_preference: NormalizationMode::None,
386    decompose: None,
387    compose: None,
388    setup_masks: Some(setup_masks_hangul),
389    gpos_tag: None,
390    reorder_marks: None,
391    zero_width_marks: ZeroWidthMarks::None,
392    fallback_position: true,
393};