harfrust/ot/shaper/
hangul.rs1use 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 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 buffer.clear_output();
169 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 if start < end && end == buffer.out_len {
182 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 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 buffer.next_glyph();
211 }
212 }
213
214 start = buffer.out_len;
215 end = buffer.out_len;
216 continue;
217 }
218
219 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 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 tindex = t - T_BASE;
235 } else {
236 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 if is_combining_l(l) && is_combining_v(v) && (t == 0 || is_combining_t(t)) {
246 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 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 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 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 buffer.unsafe_to_break(Some(buffer.idx), Some(buffer.idx + 2));
299 }
300 }
301
302 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 has_glyph && tindex == 0 {
319 buffer.next_glyph();
320 s_len += 1;
321 }
322
323 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 buffer.unsafe_to_break(Some(buffer.idx), Some(buffer.idx + 2));
344 }
345 }
346
347 if has_glyph {
348 end = start + 1;
350 buffer.next_glyph();
351 continue;
352 }
353 }
354
355 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 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};