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