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-ot-shape-complex.hh" |
28 | |
29 | |
30 | /* Hangul shaper */ |
31 | |
32 | |
33 | /* Same order as the feature array below */ |
34 | enum { |
35 | _JMO, |
36 | |
37 | LJMO, |
38 | VJMO, |
39 | TJMO, |
40 | |
41 | FIRST_HANGUL_FEATURE = LJMO, |
42 | HANGUL_FEATURE_COUNT = TJMO + 1 |
43 | }; |
44 | |
45 | static const hb_tag_t hangul_features[HANGUL_FEATURE_COUNT] = |
46 | { |
47 | HB_TAG_NONE, |
48 | HB_TAG('l','j','m','o'), |
49 | HB_TAG('v','j','m','o'), |
50 | HB_TAG('t','j','m','o') |
51 | }; |
52 | |
53 | static void |
54 | collect_features_hangul (hb_ot_shape_planner_t *plan) |
55 | { |
56 | hb_ot_map_builder_t *map = &plan->map; |
57 | |
58 | for (unsigned int i = FIRST_HANGUL_FEATURE; i < HANGUL_FEATURE_COUNT; i++) |
59 | map->add_feature (hangul_features[i]); |
60 | } |
61 | |
62 | static void |
63 | override_features_hangul (hb_ot_shape_planner_t *plan) |
64 | { |
65 | /* Uniscribe does not apply 'calt' for Hangul, and certain fonts |
66 | * (Noto Sans CJK, Source Sans Han, etc) apply all of jamo lookups |
67 | * in calt, which is not desirable. */ |
68 | plan->map.disable_feature (HB_TAG('c','a','l','t')); |
69 | } |
70 | |
71 | struct hangul_shape_plan_t |
72 | { |
73 | hb_mask_t mask_array[HANGUL_FEATURE_COUNT]; |
74 | }; |
75 | |
76 | static void * |
77 | data_create_hangul (const hb_ot_shape_plan_t *plan) |
78 | { |
79 | hangul_shape_plan_t *hangul_plan = (hangul_shape_plan_t *) calloc (1, sizeof (hangul_shape_plan_t)); |
80 | if (unlikely (!hangul_plan)) |
81 | return nullptr; |
82 | |
83 | for (unsigned int i = 0; i < HANGUL_FEATURE_COUNT; i++) |
84 | hangul_plan->mask_array[i] = plan->map.get_1_mask (hangul_features[i]); |
85 | |
86 | return hangul_plan; |
87 | } |
88 | |
89 | static void |
90 | data_destroy_hangul (void *data) |
91 | { |
92 | free (data); |
93 | } |
94 | |
95 | /* Constants for algorithmic hangul syllable [de]composition. */ |
96 | #define LBase 0x1100u |
97 | #define VBase 0x1161u |
98 | #define TBase 0x11A7u |
99 | #define LCount 19u |
100 | #define VCount 21u |
101 | #define TCount 28u |
102 | #define SBase 0xAC00u |
103 | #define NCount (VCount * TCount) |
104 | #define SCount (LCount * NCount) |
105 | |
106 | #define isCombiningL(u) (hb_in_range<hb_codepoint_t> ((u), LBase, LBase+LCount-1)) |
107 | #define isCombiningV(u) (hb_in_range<hb_codepoint_t> ((u), VBase, VBase+VCount-1)) |
108 | #define isCombiningT(u) (hb_in_range<hb_codepoint_t> ((u), TBase+1, TBase+TCount-1)) |
109 | #define isCombinedS(u) (hb_in_range<hb_codepoint_t> ((u), SBase, SBase+SCount-1)) |
110 | |
111 | #define isL(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x1100u, 0x115Fu, 0xA960u, 0xA97Cu)) |
112 | #define isV(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x1160u, 0x11A7u, 0xD7B0u, 0xD7C6u)) |
113 | #define isT(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x11A8u, 0x11FFu, 0xD7CBu, 0xD7FBu)) |
114 | |
115 | #define isHangulTone(u) (hb_in_range<hb_codepoint_t> ((u), 0x302Eu, 0x302Fu)) |
116 | |
117 | /* buffer var allocations */ |
118 | #define hangul_shaping_feature() complex_var_u8_0() /* hangul jamo shaping feature */ |
119 | |
120 | static bool |
121 | is_zero_width_char (hb_font_t *font, |
122 | hb_codepoint_t unicode) |
123 | { |
124 | hb_codepoint_t glyph; |
125 | return hb_font_get_glyph (font, unicode, 0, &glyph) && hb_font_get_glyph_h_advance (font, glyph) == 0; |
126 | } |
127 | |
128 | static void |
129 | preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED, |
130 | hb_buffer_t *buffer, |
131 | hb_font_t *font) |
132 | { |
133 | HB_BUFFER_ALLOCATE_VAR (buffer, hangul_shaping_feature); |
134 | |
135 | /* Hangul syllables come in two shapes: LV, and LVT. Of those: |
136 | * |
137 | * - LV can be precomposed, or decomposed. Lets call those |
138 | * <LV> and <L,V>, |
139 | * - LVT can be fully precomposed, partically precomposed, or |
140 | * fully decomposed. Ie. <LVT>, <LV,T>, or <L,V,T>. |
141 | * |
142 | * The composition / decomposition is mechanical. However, not |
143 | * all <L,V> sequences compose, and not all <LV,T> sequences |
144 | * compose. |
145 | * |
146 | * Here are the specifics: |
147 | * |
148 | * - <L>: U+1100..115F, U+A960..A97F |
149 | * - <V>: U+1160..11A7, U+D7B0..D7C7 |
150 | * - <T>: U+11A8..11FF, U+D7CB..D7FB |
151 | * |
152 | * - Only the <L,V> sequences for some of the U+11xx ranges combine. |
153 | * - Only <LV,T> sequences for some of the Ts in U+11xx range combine. |
154 | * |
155 | * Here is what we want to accomplish in this shaper: |
156 | * |
157 | * - If the whole syllable can be precomposed, do that, |
158 | * - Otherwise, fully decompose and apply ljmo/vjmo/tjmo features. |
159 | * - If a valid syllable is followed by a Hangul tone mark, reorder the tone |
160 | * mark to precede the whole syllable - unless it is a zero-width glyph, in |
161 | * which case we leave it untouched, assuming it's designed to overstrike. |
162 | * |
163 | * That is, of the different possible syllables: |
164 | * |
165 | * <L> |
166 | * <L,V> |
167 | * <L,V,T> |
168 | * <LV> |
169 | * <LVT> |
170 | * <LV, T> |
171 | * |
172 | * - <L> needs no work. |
173 | * |
174 | * - <LV> and <LVT> can stay the way they are if the font supports them, otherwise we |
175 | * should fully decompose them if font supports. |
176 | * |
177 | * - <L,V> and <L,V,T> we should compose if the whole thing can be composed. |
178 | * |
179 | * - <LV,T> we should compose if the whole thing can be composed, otherwise we should |
180 | * decompose. |
181 | */ |
182 | |
183 | buffer->clear_output (); |
184 | unsigned int start = 0, end = 0; /* Extent of most recently seen syllable; |
185 | * valid only if start < end |
186 | */ |
187 | unsigned int count = buffer->len; |
188 | |
189 | for (buffer->idx = 0; buffer->idx < count && buffer->successful;) |
190 | { |
191 | hb_codepoint_t u = buffer->cur().codepoint; |
192 | |
193 | if (isHangulTone (u)) |
194 | { |
195 | /* |
196 | * We could cache the width of the tone marks and the existence of dotted-circle, |
197 | * but the use of the Hangul tone mark characters seems to be rare enough that |
198 | * I didn't bother for now. |
199 | */ |
200 | if (start < end && end == buffer->out_len) |
201 | { |
202 | /* Tone mark follows a valid syllable; move it in front, unless it's zero width. */ |
203 | buffer->unsafe_to_break_from_outbuffer (start, buffer->idx); |
204 | buffer->next_glyph (); |
205 | if (!is_zero_width_char (font, u)) |
206 | { |
207 | buffer->merge_out_clusters (start, end + 1); |
208 | hb_glyph_info_t *info = buffer->out_info; |
209 | hb_glyph_info_t tone = info[end]; |
210 | memmove (&info[start + 1], &info[start], (end - start) * sizeof (hb_glyph_info_t)); |
211 | info[start] = tone; |
212 | } |
213 | } |
214 | else |
215 | { |
216 | /* No valid syllable as base for tone mark; try to insert dotted circle. */ |
217 | if (font->has_glyph (0x25CCu)) |
218 | { |
219 | hb_codepoint_t chars[2]; |
220 | if (!is_zero_width_char (font, u)) { |
221 | chars[0] = u; |
222 | chars[1] = 0x25CCu; |
223 | } else { |
224 | chars[0] = 0x25CCu; |
225 | chars[1] = u; |
226 | } |
227 | buffer->replace_glyphs (1, 2, chars); |
228 | } |
229 | else |
230 | { |
231 | /* No dotted circle available in the font; just leave tone mark untouched. */ |
232 | buffer->next_glyph (); |
233 | } |
234 | } |
235 | start = end = buffer->out_len; |
236 | continue; |
237 | } |
238 | |
239 | start = buffer->out_len; /* Remember current position as a potential syllable start; |
240 | * will only be used if we set end to a later position. |
241 | */ |
242 | |
243 | if (isL (u) && buffer->idx + 1 < count) |
244 | { |
245 | hb_codepoint_t l = u; |
246 | hb_codepoint_t v = buffer->cur(+1).codepoint; |
247 | if (isV (v)) |
248 | { |
249 | /* Have <L,V> or <L,V,T>. */ |
250 | hb_codepoint_t t = 0; |
251 | unsigned int tindex = 0; |
252 | if (buffer->idx + 2 < count) |
253 | { |
254 | t = buffer->cur(+2).codepoint; |
255 | if (isT (t)) |
256 | tindex = t - TBase; /* Only used if isCombiningT (t); otherwise invalid. */ |
257 | else |
258 | t = 0; /* The next character was not a trailing jamo. */ |
259 | } |
260 | buffer->unsafe_to_break (buffer->idx, buffer->idx + (t ? 3 : 2)); |
261 | |
262 | /* We've got a syllable <L,V,T?>; see if it can potentially be composed. */ |
263 | if (isCombiningL (l) && isCombiningV (v) && (t == 0 || isCombiningT (t))) |
264 | { |
265 | /* Try to compose; if this succeeds, end is set to start+1. */ |
266 | hb_codepoint_t s = SBase + (l - LBase) * NCount + (v - VBase) * TCount + tindex; |
267 | if (font->has_glyph (s)) |
268 | { |
269 | buffer->replace_glyphs (t ? 3 : 2, 1, &s); |
270 | if (unlikely (!buffer->successful)) |
271 | return; |
272 | end = start + 1; |
273 | continue; |
274 | } |
275 | } |
276 | |
277 | /* We didn't compose, either because it's an Old Hangul syllable without a |
278 | * precomposed character in Unicode, or because the font didn't support the |
279 | * necessary precomposed glyph. |
280 | * Set jamo features on the individual glyphs, and advance past them. |
281 | */ |
282 | buffer->cur().hangul_shaping_feature() = LJMO; |
283 | buffer->next_glyph (); |
284 | buffer->cur().hangul_shaping_feature() = VJMO; |
285 | buffer->next_glyph (); |
286 | if (t) |
287 | { |
288 | buffer->cur().hangul_shaping_feature() = TJMO; |
289 | buffer->next_glyph (); |
290 | end = start + 3; |
291 | } |
292 | else |
293 | end = start + 2; |
294 | if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES) |
295 | buffer->merge_out_clusters (start, end); |
296 | continue; |
297 | } |
298 | } |
299 | |
300 | else if (isCombinedS (u)) |
301 | { |
302 | /* Have <LV>, <LVT>, or <LV,T> */ |
303 | hb_codepoint_t s = u; |
304 | bool has_glyph = font->has_glyph (s); |
305 | unsigned int lindex = (s - SBase) / NCount; |
306 | unsigned int nindex = (s - SBase) % NCount; |
307 | unsigned int vindex = nindex / TCount; |
308 | unsigned int tindex = nindex % TCount; |
309 | |
310 | if (!tindex && |
311 | buffer->idx + 1 < count && |
312 | isCombiningT (buffer->cur(+1).codepoint)) |
313 | { |
314 | /* <LV,T>, try to combine. */ |
315 | unsigned int new_tindex = buffer->cur(+1).codepoint - TBase; |
316 | hb_codepoint_t new_s = s + new_tindex; |
317 | if (font->has_glyph (new_s)) |
318 | { |
319 | buffer->replace_glyphs (2, 1, &new_s); |
320 | if (unlikely (!buffer->successful)) |
321 | return; |
322 | end = start + 1; |
323 | continue; |
324 | } |
325 | else |
326 | buffer->unsafe_to_break (buffer->idx, buffer->idx + 2); /* Mark unsafe between LV and T. */ |
327 | } |
328 | |
329 | /* Otherwise, decompose if font doesn't support <LV> or <LVT>, |
330 | * or if having non-combining <LV,T>. Note that we already handled |
331 | * combining <LV,T> above. */ |
332 | if (!has_glyph || |
333 | (!tindex && |
334 | buffer->idx + 1 < count && |
335 | isT (buffer->cur(+1).codepoint))) |
336 | { |
337 | hb_codepoint_t decomposed[3] = {LBase + lindex, |
338 | VBase + vindex, |
339 | TBase + tindex}; |
340 | if (font->has_glyph (decomposed[0]) && |
341 | font->has_glyph (decomposed[1]) && |
342 | (!tindex || font->has_glyph (decomposed[2]))) |
343 | { |
344 | unsigned int s_len = tindex ? 3 : 2; |
345 | buffer->replace_glyphs (1, s_len, decomposed); |
346 | |
347 | /* If we decomposed an LV because of a non-combining T following, |
348 | * we want to include this T in the syllable. |
349 | */ |
350 | if (has_glyph && !tindex) |
351 | { |
352 | buffer->next_glyph (); |
353 | s_len++; |
354 | } |
355 | |
356 | if (unlikely (!buffer->successful)) |
357 | return; |
358 | |
359 | /* We decomposed S: apply jamo features to the individual glyphs |
360 | * that are now in buffer->out_info. |
361 | */ |
362 | hb_glyph_info_t *info = buffer->out_info; |
363 | end = start + s_len; |
364 | |
365 | unsigned int i = start; |
366 | info[i++].hangul_shaping_feature() = LJMO; |
367 | info[i++].hangul_shaping_feature() = VJMO; |
368 | if (i < end) |
369 | info[i++].hangul_shaping_feature() = TJMO; |
370 | |
371 | if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES) |
372 | buffer->merge_out_clusters (start, end); |
373 | continue; |
374 | } |
375 | else if ((!tindex && buffer->idx + 1 < count && isT (buffer->cur(+1).codepoint))) |
376 | buffer->unsafe_to_break (buffer->idx, buffer->idx + 2); /* Mark unsafe between LV and T. */ |
377 | } |
378 | |
379 | if (has_glyph) |
380 | { |
381 | /* We didn't decompose the S, so just advance past it. */ |
382 | end = start + 1; |
383 | buffer->next_glyph (); |
384 | continue; |
385 | } |
386 | } |
387 | |
388 | /* Didn't find a recognizable syllable, so we leave end <= start; |
389 | * this will prevent tone-mark reordering happening. |
390 | */ |
391 | buffer->next_glyph (); |
392 | } |
393 | buffer->swap_buffers (); |
394 | } |
395 | |
396 | static void |
397 | setup_masks_hangul (const hb_ot_shape_plan_t *plan, |
398 | hb_buffer_t *buffer, |
399 | hb_font_t *font HB_UNUSED) |
400 | { |
401 | const hangul_shape_plan_t *hangul_plan = (const hangul_shape_plan_t *) plan->data; |
402 | |
403 | if (likely (hangul_plan)) |
404 | { |
405 | unsigned int count = buffer->len; |
406 | hb_glyph_info_t *info = buffer->info; |
407 | for (unsigned int i = 0; i < count; i++, info++) |
408 | info->mask |= hangul_plan->mask_array[info->hangul_shaping_feature()]; |
409 | } |
410 | |
411 | HB_BUFFER_DEALLOCATE_VAR (buffer, hangul_shaping_feature); |
412 | } |
413 | |
414 | |
415 | const hb_ot_complex_shaper_t _hb_ot_complex_shaper_hangul = |
416 | { |
417 | collect_features_hangul, |
418 | override_features_hangul, |
419 | data_create_hangul, |
420 | data_destroy_hangul, |
421 | preprocess_text_hangul, |
422 | nullptr, /* postprocess_glyphs */ |
423 | HB_OT_SHAPE_NORMALIZATION_MODE_NONE, |
424 | nullptr, /* decompose */ |
425 | nullptr, /* compose */ |
426 | setup_masks_hangul, |
427 | HB_TAG_NONE, /* gpos_tag */ |
428 | nullptr, /* reorder_marks */ |
429 | HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE, |
430 | false, /* fallback_position */ |
431 | }; |
432 | |