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