/src/harfbuzz/src/hb-ot-shaper-hangul.cc
Line | Count | Source |
1 | | /* |
2 | | * Copyright © 2013 Google, Inc. |
3 | | * |
4 | | * This is part of HarfBuzz, a text shaping library. |
5 | | * |
6 | | * Permission is hereby granted, without written agreement and without |
7 | | * license or royalty fees, to use, copy, modify, and distribute this |
8 | | * software and its documentation for any purpose, provided that the |
9 | | * above copyright notice and the following two paragraphs appear in |
10 | | * all copies of this software. |
11 | | * |
12 | | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
13 | | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
14 | | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
15 | | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
16 | | * DAMAGE. |
17 | | * |
18 | | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
19 | | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
20 | | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
21 | | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
22 | | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
23 | | * |
24 | | * Google Author(s): Behdad Esfahbod |
25 | | */ |
26 | | |
27 | | #include "hb.hh" |
28 | | |
29 | | #ifndef HB_NO_OT_SHAPE |
30 | | |
31 | | #include "hb-ot-shaper.hh" |
32 | | |
33 | | |
34 | | /* Hangul shaper */ |
35 | | |
36 | | |
37 | | /* Same order as the feature array below */ |
38 | | enum { |
39 | | _JMO, |
40 | | |
41 | | LJMO, |
42 | | VJMO, |
43 | | TJMO, |
44 | | |
45 | | FIRST_HANGUL_FEATURE = LJMO, |
46 | | HANGUL_FEATURE_COUNT = TJMO + 1 |
47 | | }; |
48 | | |
49 | | static const hb_tag_t hangul_features[HANGUL_FEATURE_COUNT] = |
50 | | { |
51 | | HB_TAG_NONE, |
52 | | HB_TAG('l','j','m','o'), |
53 | | HB_TAG('v','j','m','o'), |
54 | | HB_TAG('t','j','m','o') |
55 | | }; |
56 | | |
57 | | static void |
58 | | collect_features_hangul (hb_ot_shape_planner_t *plan) |
59 | 0 | { |
60 | 0 | hb_ot_map_builder_t *map = &plan->map; |
61 | |
|
62 | 0 | for (unsigned int i = FIRST_HANGUL_FEATURE; i < HANGUL_FEATURE_COUNT; i++) |
63 | 0 | map->add_feature (hangul_features[i]); |
64 | 0 | } |
65 | | |
66 | | static void |
67 | | override_features_hangul (hb_ot_shape_planner_t *plan) |
68 | 0 | { |
69 | | /* Uniscribe does not apply 'calt' for Hangul, and certain fonts |
70 | | * (Noto Sans CJK, Source Han Sans, etc) apply all of jamo lookups |
71 | | * in calt, which is not desirable. |
72 | | * |
73 | | * Rather than disabling 'calt' for the entire run, which also turns |
74 | | * it off for any other characters in the run, allocate a mask for it |
75 | | * and clear that mask on jamo only (in setup_masks_hangul). That |
76 | | * keeps jamo out of reach of such lookups while letting the rest of |
77 | | * the run shape with 'calt' as usual. |
78 | | * |
79 | | * https://github.com/harfbuzz/harfbuzz/discussions/4853 */ |
80 | 0 | plan->map.add_feature (HB_TAG('c','a','l','t')); |
81 | 0 | } |
82 | | |
83 | | struct hangul_shape_plan_t |
84 | | { |
85 | | hb_mask_t mask_array[HANGUL_FEATURE_COUNT]; |
86 | | hb_mask_t calt_mask; |
87 | | }; |
88 | | |
89 | | static void * |
90 | | data_create_hangul (const hb_ot_shape_plan_t *plan) |
91 | 0 | { |
92 | 0 | hangul_shape_plan_t *hangul_plan = (hangul_shape_plan_t *) hb_calloc (1, sizeof (hangul_shape_plan_t)); |
93 | 0 | if (unlikely (!hangul_plan)) |
94 | 0 | return nullptr; |
95 | | |
96 | 0 | for (unsigned int i = 0; i < HANGUL_FEATURE_COUNT; i++) |
97 | 0 | hangul_plan->mask_array[i] = plan->map.get_1_mask (hangul_features[i]); |
98 | |
|
99 | 0 | hangul_plan->calt_mask = plan->map.get_1_mask (HB_TAG('c','a','l','t')); |
100 | |
|
101 | 0 | return hangul_plan; |
102 | 0 | } |
103 | | |
104 | | static void |
105 | | data_destroy_hangul (void *data) |
106 | 0 | { |
107 | 0 | hb_free (data); |
108 | 0 | } |
109 | | |
110 | | /* Constants for algorithmic hangul syllable [de]composition. */ |
111 | 0 | #define LBase 0x1100u |
112 | 0 | #define VBase 0x1161u |
113 | 0 | #define TBase 0x11A7u |
114 | 0 | #define LCount 19u |
115 | 0 | #define VCount 21u |
116 | 0 | #define TCount 28u |
117 | 0 | #define SBase 0xAC00u |
118 | 0 | #define NCount (VCount * TCount) |
119 | 0 | #define SCount (LCount * NCount) |
120 | | |
121 | 0 | #define isCombiningL(u) (hb_in_range<hb_codepoint_t> ((u), LBase, LBase+LCount-1)) |
122 | 0 | #define isCombiningV(u) (hb_in_range<hb_codepoint_t> ((u), VBase, VBase+VCount-1)) |
123 | 0 | #define isCombiningT(u) (hb_in_range<hb_codepoint_t> ((u), TBase+1, TBase+TCount-1)) |
124 | 0 | #define isCombinedS(u) (hb_in_range<hb_codepoint_t> ((u), SBase, SBase+SCount-1)) |
125 | | |
126 | 0 | #define isL(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x1100u, 0x115Fu, 0xA960u, 0xA97Cu)) |
127 | 0 | #define isV(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x1160u, 0x11A7u, 0xD7B0u, 0xD7C6u)) |
128 | 0 | #define isT(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x11A8u, 0x11FFu, 0xD7CBu, 0xD7FBu)) |
129 | | |
130 | 0 | #define isHangulTone(u) (hb_in_range<hb_codepoint_t> ((u), 0x302Eu, 0x302Fu)) |
131 | | |
132 | | /* buffer var allocations */ |
133 | 0 | #define hangul_shaping_feature() ot_shaper_var_u8_auxiliary() /* hangul jamo shaping feature */ |
134 | | |
135 | | static bool |
136 | | is_zero_width_char (hb_font_t *font, |
137 | | hb_codepoint_t unicode) |
138 | 0 | { |
139 | 0 | hb_codepoint_t glyph; |
140 | 0 | return hb_font_get_glyph (font, unicode, 0, &glyph) && hb_font_get_glyph_h_advance (font, glyph) == 0; |
141 | 0 | } |
142 | | |
143 | | static void |
144 | | preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED, |
145 | | hb_buffer_t *buffer, |
146 | | hb_font_t *font) |
147 | 0 | { |
148 | 0 | HB_BUFFER_ALLOCATE_VAR (buffer, hangul_shaping_feature); |
149 | | |
150 | | /* Hangul syllables come in two shapes: LV, and LVT. Of those: |
151 | | * |
152 | | * - LV can be precomposed, or decomposed. Lets call those |
153 | | * <LV> and <L,V>, |
154 | | * - LVT can be fully precomposed, partially precomposed, or |
155 | | * fully decomposed. Ie. <LVT>, <LV,T>, or <L,V,T>. |
156 | | * |
157 | | * The composition / decomposition is mechanical. However, not |
158 | | * all <L,V> sequences compose, and not all <LV,T> sequences |
159 | | * compose. |
160 | | * |
161 | | * Here are the specifics: |
162 | | * |
163 | | * - <L>: U+1100..115F, U+A960..A97F |
164 | | * - <V>: U+1160..11A7, U+D7B0..D7C7 |
165 | | * - <T>: U+11A8..11FF, U+D7CB..D7FB |
166 | | * |
167 | | * - Only the <L,V> sequences for some of the U+11xx ranges combine. |
168 | | * - Only <LV,T> sequences for some of the Ts in U+11xx range combine. |
169 | | * |
170 | | * Here is what we want to accomplish in this shaper: |
171 | | * |
172 | | * - If the whole syllable can be precomposed, do that, |
173 | | * - Otherwise, fully decompose and apply ljmo/vjmo/tjmo features. |
174 | | * - If a valid syllable is followed by a Hangul tone mark, reorder the tone |
175 | | * mark to precede the whole syllable - unless it is a zero-width glyph, in |
176 | | * which case we leave it untouched, assuming it's designed to overstrike. |
177 | | * |
178 | | * That is, of the different possible syllables: |
179 | | * |
180 | | * <L> |
181 | | * <L,V> |
182 | | * <L,V,T> |
183 | | * <LV> |
184 | | * <LVT> |
185 | | * <LV, T> |
186 | | * |
187 | | * - <L> needs no work. |
188 | | * |
189 | | * - <LV> and <LVT> can stay the way they are if the font supports them, otherwise we |
190 | | * should fully decompose them if font supports. |
191 | | * |
192 | | * - <L,V> and <L,V,T> we should compose if the whole thing can be composed. |
193 | | * |
194 | | * - <LV,T> we should compose if the whole thing can be composed, otherwise we should |
195 | | * decompose. |
196 | | */ |
197 | |
|
198 | 0 | buffer->clear_output (); |
199 | 0 | unsigned int start = 0, end = 0; /* Extent of most recently seen syllable; |
200 | | * valid only if start < end |
201 | | */ |
202 | 0 | unsigned int count = buffer->len; |
203 | |
|
204 | 0 | for (buffer->idx = 0; buffer->idx < count && buffer->successful;) |
205 | 0 | { |
206 | 0 | hb_codepoint_t u = buffer->cur().codepoint; |
207 | |
|
208 | 0 | if (isHangulTone (u)) |
209 | 0 | { |
210 | | /* |
211 | | * We could cache the width of the tone marks and the existence of dotted-circle, |
212 | | * but the use of the Hangul tone mark characters seems to be rare enough that |
213 | | * I didn't bother for now. |
214 | | */ |
215 | 0 | if (start < end && end == buffer->out_len) |
216 | 0 | { |
217 | | /* Tone mark follows a valid syllable; move it in front, unless it's zero width. */ |
218 | 0 | buffer->unsafe_to_break_from_outbuffer (start, buffer->idx); |
219 | 0 | if (unlikely (!buffer->next_glyph ())) break; |
220 | 0 | if (!is_zero_width_char (font, u)) |
221 | 0 | { |
222 | 0 | buffer->merge_out_clusters (start, end + 1); |
223 | 0 | hb_glyph_info_t *info = buffer->out_info; |
224 | 0 | hb_glyph_info_t tone = info[end]; |
225 | 0 | memmove (&info[start + 1], &info[start], (end - start) * sizeof (hb_glyph_info_t)); |
226 | 0 | info[start] = tone; |
227 | 0 | } |
228 | 0 | } |
229 | 0 | else |
230 | 0 | { |
231 | | /* No valid syllable as base for tone mark; try to insert dotted circle. */ |
232 | 0 | if (!(buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE) && |
233 | 0 | font->has_glyph (0x25CCu)) |
234 | 0 | { |
235 | 0 | hb_codepoint_t chars[2]; |
236 | 0 | if (!is_zero_width_char (font, u)) |
237 | 0 | { |
238 | 0 | chars[0] = u; |
239 | 0 | chars[1] = 0x25CCu; |
240 | 0 | } else |
241 | 0 | { |
242 | 0 | chars[0] = 0x25CCu; |
243 | 0 | chars[1] = u; |
244 | 0 | } |
245 | 0 | (void) buffer->replace_glyphs (1, 2, chars); |
246 | 0 | } |
247 | 0 | else |
248 | 0 | { |
249 | | /* No dotted circle available in the font; just leave tone mark untouched. */ |
250 | 0 | (void) buffer->next_glyph (); |
251 | 0 | } |
252 | 0 | } |
253 | 0 | start = end = buffer->out_len; |
254 | 0 | continue; |
255 | 0 | } |
256 | | |
257 | 0 | start = buffer->out_len; /* Remember current position as a potential syllable start; |
258 | | * will only be used if we set end to a later position. |
259 | | */ |
260 | |
|
261 | 0 | if (isL (u) && buffer->idx + 1 < count) |
262 | 0 | { |
263 | 0 | hb_codepoint_t l = u; |
264 | 0 | hb_codepoint_t v = buffer->cur(+1).codepoint; |
265 | 0 | if (isV (v)) |
266 | 0 | { |
267 | | /* Have <L,V> or <L,V,T>. */ |
268 | 0 | hb_codepoint_t t = 0; |
269 | 0 | unsigned int tindex = 0; |
270 | 0 | if (buffer->idx + 2 < count) |
271 | 0 | { |
272 | 0 | t = buffer->cur(+2).codepoint; |
273 | 0 | if (isT (t)) |
274 | 0 | tindex = t - TBase; /* Only used if isCombiningT (t); otherwise invalid. */ |
275 | 0 | else |
276 | 0 | t = 0; /* The next character was not a trailing jamo. */ |
277 | 0 | } |
278 | 0 | buffer->unsafe_to_break (buffer->idx, buffer->idx + (t ? 3 : 2)); |
279 | | |
280 | | /* We've got a syllable <L,V,T?>; see if it can potentially be composed. */ |
281 | 0 | if (isCombiningL (l) && isCombiningV (v) && (t == 0 || isCombiningT (t))) |
282 | 0 | { |
283 | | /* Try to compose; if this succeeds, end is set to start+1. */ |
284 | 0 | hb_codepoint_t s = SBase + (l - LBase) * NCount + (v - VBase) * TCount + tindex; |
285 | 0 | if (font->has_glyph (s)) |
286 | 0 | { |
287 | 0 | (void) buffer->replace_glyphs (t ? 3 : 2, 1, &s); |
288 | 0 | end = start + 1; |
289 | 0 | continue; |
290 | 0 | } |
291 | 0 | } |
292 | | |
293 | | /* We didn't compose, either because it's an Old Hangul syllable without a |
294 | | * precomposed character in Unicode, or because the font didn't support the |
295 | | * necessary precomposed glyph. |
296 | | * Set jamo features on the individual glyphs, and advance past them. |
297 | | */ |
298 | 0 | buffer->cur().hangul_shaping_feature() = LJMO; |
299 | 0 | (void) buffer->next_glyph (); |
300 | 0 | buffer->cur().hangul_shaping_feature() = VJMO; |
301 | 0 | (void) buffer->next_glyph (); |
302 | 0 | if (t) |
303 | 0 | { |
304 | 0 | buffer->cur().hangul_shaping_feature() = TJMO; |
305 | 0 | (void) buffer->next_glyph (); |
306 | 0 | end = start + 3; |
307 | 0 | } |
308 | 0 | else |
309 | 0 | end = start + 2; |
310 | 0 | if (unlikely (!buffer->successful)) |
311 | 0 | break; |
312 | 0 | buffer->merge_out_grapheme_clusters (start, end); |
313 | 0 | continue; |
314 | 0 | } |
315 | 0 | } |
316 | | |
317 | 0 | else if (isCombinedS (u)) |
318 | 0 | { |
319 | | /* Have <LV>, <LVT>, or <LV,T> */ |
320 | 0 | hb_codepoint_t s = u; |
321 | 0 | bool has_glyph = font->has_glyph (s); |
322 | 0 | unsigned int lindex = (s - SBase) / NCount; |
323 | 0 | unsigned int nindex = (s - SBase) % NCount; |
324 | 0 | unsigned int vindex = nindex / TCount; |
325 | 0 | unsigned int tindex = nindex % TCount; |
326 | |
|
327 | 0 | if (!tindex && |
328 | 0 | buffer->idx + 1 < count && |
329 | 0 | isCombiningT (buffer->cur(+1).codepoint)) |
330 | 0 | { |
331 | | /* <LV,T>, try to combine. */ |
332 | 0 | unsigned int new_tindex = buffer->cur(+1).codepoint - TBase; |
333 | 0 | hb_codepoint_t new_s = s + new_tindex; |
334 | 0 | if (font->has_glyph (new_s)) |
335 | 0 | { |
336 | 0 | (void) buffer->replace_glyphs (2, 1, &new_s); |
337 | 0 | end = start + 1; |
338 | 0 | continue; |
339 | 0 | } |
340 | 0 | else |
341 | 0 | buffer->unsafe_to_break (buffer->idx, buffer->idx + 2); /* Mark unsafe between LV and T. */ |
342 | 0 | } |
343 | | |
344 | | /* Otherwise, decompose if font doesn't support <LV> or <LVT>, |
345 | | * or if having non-combining <LV,T>. Note that we already handled |
346 | | * combining <LV,T> above. */ |
347 | 0 | if (!has_glyph || |
348 | 0 | (!tindex && |
349 | 0 | buffer->idx + 1 < count && |
350 | 0 | isT (buffer->cur(+1).codepoint))) |
351 | 0 | { |
352 | 0 | hb_codepoint_t decomposed[3] = {LBase + lindex, |
353 | 0 | VBase + vindex, |
354 | 0 | TBase + tindex}; |
355 | 0 | if (font->has_glyph (decomposed[0]) && |
356 | 0 | font->has_glyph (decomposed[1]) && |
357 | 0 | (!tindex || font->has_glyph (decomposed[2]))) |
358 | 0 | { |
359 | 0 | unsigned int s_len = tindex ? 3 : 2; |
360 | 0 | (void) buffer->replace_glyphs (1, s_len, decomposed); |
361 | | |
362 | | /* If we decomposed an LV because of a non-combining T following, |
363 | | * we want to include this T in the syllable. |
364 | | */ |
365 | 0 | if (has_glyph && !tindex) |
366 | 0 | { |
367 | 0 | (void) buffer->next_glyph (); |
368 | 0 | s_len++; |
369 | 0 | } |
370 | 0 | if (unlikely (!buffer->successful)) |
371 | 0 | break; |
372 | | |
373 | | /* We decomposed S: apply jamo features to the individual glyphs |
374 | | * that are now in buffer->out_info. |
375 | | */ |
376 | 0 | hb_glyph_info_t *info = buffer->out_info; |
377 | 0 | end = start + s_len; |
378 | |
|
379 | 0 | unsigned int i = start; |
380 | 0 | info[i++].hangul_shaping_feature() = LJMO; |
381 | 0 | info[i++].hangul_shaping_feature() = VJMO; |
382 | 0 | if (i < end) |
383 | 0 | info[i++].hangul_shaping_feature() = TJMO; |
384 | |
|
385 | 0 | buffer->merge_out_grapheme_clusters (start, end); |
386 | 0 | continue; |
387 | 0 | } |
388 | 0 | else if ((!tindex && buffer->idx + 1 < count && isT (buffer->cur(+1).codepoint))) |
389 | 0 | buffer->unsafe_to_break (buffer->idx, buffer->idx + 2); /* Mark unsafe between LV and T. */ |
390 | 0 | } |
391 | | |
392 | 0 | if (has_glyph) |
393 | 0 | { |
394 | | /* We didn't decompose the S, so just advance past it and fall through. */ |
395 | 0 | end = start + 1; |
396 | 0 | } |
397 | 0 | } |
398 | | |
399 | | /* Didn't find a recognizable syllable, so we leave end <= start; |
400 | | * this will prevent tone-mark reordering happening. |
401 | | */ |
402 | 0 | (void) buffer->next_glyph (); |
403 | 0 | } |
404 | 0 | buffer->sync (); |
405 | 0 | } |
406 | | |
407 | | static void |
408 | | setup_masks_hangul (const hb_ot_shape_plan_t *plan, |
409 | | hb_buffer_t *buffer, |
410 | | hb_font_t *font HB_UNUSED) |
411 | 0 | { |
412 | 0 | const hangul_shape_plan_t *hangul_plan = (const hangul_shape_plan_t *) plan->data; |
413 | |
|
414 | 0 | if (likely (hangul_plan)) |
415 | 0 | { |
416 | 0 | unsigned int count = buffer->len; |
417 | 0 | hb_glyph_info_t *info = buffer->info; |
418 | 0 | for (unsigned int i = 0; i < count; i++, info++) |
419 | 0 | { |
420 | 0 | info->mask |= hangul_plan->mask_array[info->hangul_shaping_feature()]; |
421 | | |
422 | | /* Keep 'calt' away from jamo; see override_features_hangul(). */ |
423 | 0 | hb_codepoint_t u = info->codepoint; |
424 | 0 | if (isL (u) || isV (u) || isT (u)) |
425 | 0 | info->mask &= ~hangul_plan->calt_mask; |
426 | 0 | } |
427 | 0 | } |
428 | |
|
429 | | HB_BUFFER_DEALLOCATE_VAR (buffer, hangul_shaping_feature); |
430 | 0 | } |
431 | | |
432 | | |
433 | | const hb_ot_shaper_t _hb_ot_shaper_hangul = |
434 | | { |
435 | | collect_features_hangul, |
436 | | override_features_hangul, |
437 | | data_create_hangul, |
438 | | data_destroy_hangul, |
439 | | preprocess_text_hangul, |
440 | | nullptr, /* postprocess_glyphs */ |
441 | | nullptr, /* decompose */ |
442 | | nullptr, /* compose */ |
443 | | setup_masks_hangul, |
444 | | nullptr, /* reorder_marks */ |
445 | | HB_TAG_NONE, /* gpos_tag */ |
446 | | HB_OT_SHAPE_NORMALIZATION_MODE_NONE, |
447 | | HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE, |
448 | | true, /* fallback_position */ |
449 | | }; |
450 | | |
451 | | |
452 | | #endif |