gfx/harfbuzz/src/hb-ot-shape-complex-hangul.cc

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial