gfx/harfbuzz/src/hb-ot-shape-normalize.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 © 2011,2012 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-normalize-private.hh"
michael@0 28 #include "hb-ot-shape-complex-private.hh"
michael@0 29 #include "hb-ot-shape-private.hh"
michael@0 30
michael@0 31
michael@0 32 /*
michael@0 33 * HIGHLEVEL DESIGN:
michael@0 34 *
michael@0 35 * This file exports one main function: _hb_ot_shape_normalize().
michael@0 36 *
michael@0 37 * This function closely reflects the Unicode Normalization Algorithm,
michael@0 38 * yet it's different.
michael@0 39 *
michael@0 40 * Each shaper specifies whether it prefers decomposed (NFD) or composed (NFC).
michael@0 41 * The logic however tries to use whatever the font can support.
michael@0 42 *
michael@0 43 * In general what happens is that: each grapheme is decomposed in a chain
michael@0 44 * of 1:2 decompositions, marks reordered, and then recomposed if desired,
michael@0 45 * so far it's like Unicode Normalization. However, the decomposition and
michael@0 46 * recomposition only happens if the font supports the resulting characters.
michael@0 47 *
michael@0 48 * The goals are:
michael@0 49 *
michael@0 50 * - Try to render all canonically equivalent strings similarly. To really
michael@0 51 * achieve this we have to always do the full decomposition and then
michael@0 52 * selectively recompose from there. It's kinda too expensive though, so
michael@0 53 * we skip some cases. For example, if composed is desired, we simply
michael@0 54 * don't touch 1-character clusters that are supported by the font, even
michael@0 55 * though their NFC may be different.
michael@0 56 *
michael@0 57 * - When a font has a precomposed character for a sequence but the 'ccmp'
michael@0 58 * feature in the font is not adequate, use the precomposed character
michael@0 59 * which typically has better mark positioning.
michael@0 60 *
michael@0 61 * - When a font does not support a combining mark, but supports it precomposed
michael@0 62 * with previous base, use that. This needs the itemizer to have this
michael@0 63 * knowledge too. We need to provide assistance to the itemizer.
michael@0 64 *
michael@0 65 * - When a font does not support a character but supports its decomposition,
michael@0 66 * well, use the decomposition (preferring the canonical decomposition, but
michael@0 67 * falling back to the compatibility decomposition if necessary). The
michael@0 68 * compatibility decomposition is really nice to have, for characters like
michael@0 69 * ellipsis, or various-sized space characters.
michael@0 70 *
michael@0 71 * - The complex shapers can customize the compose and decompose functions to
michael@0 72 * offload some of their requirements to the normalizer. For example, the
michael@0 73 * Indic shaper may want to disallow recomposing of two matras.
michael@0 74 *
michael@0 75 * - We try compatibility decomposition if decomposing through canonical
michael@0 76 * decomposition alone failed to find a sequence that the font supports.
michael@0 77 * We don't try compatibility decomposition recursively during the canonical
michael@0 78 * decomposition phase. This has minimal impact. There are only a handful
michael@0 79 * of Greek letter that have canonical decompositions that include characters
michael@0 80 * with compatibility decomposition. Those can be found using this command:
michael@0 81 *
michael@0 82 * egrep "`echo -n ';('; grep ';<' UnicodeData.txt | cut -d';' -f1 | tr '\n' '|'; echo ') '`" UnicodeData.txt
michael@0 83 */
michael@0 84
michael@0 85 static bool
michael@0 86 decompose_unicode (const hb_ot_shape_normalize_context_t *c,
michael@0 87 hb_codepoint_t ab,
michael@0 88 hb_codepoint_t *a,
michael@0 89 hb_codepoint_t *b)
michael@0 90 {
michael@0 91 return c->unicode->decompose (ab, a, b);
michael@0 92 }
michael@0 93
michael@0 94 static bool
michael@0 95 compose_unicode (const hb_ot_shape_normalize_context_t *c,
michael@0 96 hb_codepoint_t a,
michael@0 97 hb_codepoint_t b,
michael@0 98 hb_codepoint_t *ab)
michael@0 99 {
michael@0 100 return c->unicode->compose (a, b, ab);
michael@0 101 }
michael@0 102
michael@0 103 static inline void
michael@0 104 set_glyph (hb_glyph_info_t &info, hb_font_t *font)
michael@0 105 {
michael@0 106 font->get_glyph (info.codepoint, 0, &info.glyph_index());
michael@0 107 }
michael@0 108
michael@0 109 static inline void
michael@0 110 output_char (hb_buffer_t *buffer, hb_codepoint_t unichar, hb_codepoint_t glyph)
michael@0 111 {
michael@0 112 buffer->cur().glyph_index() = glyph;
michael@0 113 buffer->output_glyph (unichar);
michael@0 114 _hb_glyph_info_set_unicode_props (&buffer->prev(), buffer->unicode);
michael@0 115 }
michael@0 116
michael@0 117 static inline void
michael@0 118 next_char (hb_buffer_t *buffer, hb_codepoint_t glyph)
michael@0 119 {
michael@0 120 buffer->cur().glyph_index() = glyph;
michael@0 121 buffer->next_glyph ();
michael@0 122 }
michael@0 123
michael@0 124 static inline void
michael@0 125 skip_char (hb_buffer_t *buffer)
michael@0 126 {
michael@0 127 buffer->skip_glyph ();
michael@0 128 }
michael@0 129
michael@0 130 /* Returns 0 if didn't decompose, number of resulting characters otherwise. */
michael@0 131 static inline unsigned int
michael@0 132 decompose (const hb_ot_shape_normalize_context_t *c, bool shortest, hb_codepoint_t ab)
michael@0 133 {
michael@0 134 hb_codepoint_t a, b, a_glyph, b_glyph;
michael@0 135 hb_buffer_t * const buffer = c->buffer;
michael@0 136 hb_font_t * const font = c->font;
michael@0 137
michael@0 138 if (!c->decompose (c, ab, &a, &b) ||
michael@0 139 (b && !font->get_glyph (b, 0, &b_glyph)))
michael@0 140 return 0;
michael@0 141
michael@0 142 bool has_a = font->get_glyph (a, 0, &a_glyph);
michael@0 143 if (shortest && has_a) {
michael@0 144 /* Output a and b */
michael@0 145 output_char (buffer, a, a_glyph);
michael@0 146 if (likely (b)) {
michael@0 147 output_char (buffer, b, b_glyph);
michael@0 148 return 2;
michael@0 149 }
michael@0 150 return 1;
michael@0 151 }
michael@0 152
michael@0 153 unsigned int ret;
michael@0 154 if ((ret = decompose (c, shortest, a))) {
michael@0 155 if (b) {
michael@0 156 output_char (buffer, b, b_glyph);
michael@0 157 return ret + 1;
michael@0 158 }
michael@0 159 return ret;
michael@0 160 }
michael@0 161
michael@0 162 if (has_a) {
michael@0 163 output_char (buffer, a, a_glyph);
michael@0 164 if (likely (b)) {
michael@0 165 output_char (buffer, b, b_glyph);
michael@0 166 return 2;
michael@0 167 }
michael@0 168 return 1;
michael@0 169 }
michael@0 170
michael@0 171 return 0;
michael@0 172 }
michael@0 173
michael@0 174 /* Returns 0 if didn't decompose, number of resulting characters otherwise. */
michael@0 175 static inline unsigned int
michael@0 176 decompose_compatibility (const hb_ot_shape_normalize_context_t *c, hb_codepoint_t u)
michael@0 177 {
michael@0 178 unsigned int len, i;
michael@0 179 hb_codepoint_t decomposed[HB_UNICODE_MAX_DECOMPOSITION_LEN];
michael@0 180 hb_codepoint_t glyphs[HB_UNICODE_MAX_DECOMPOSITION_LEN];
michael@0 181
michael@0 182 len = c->buffer->unicode->decompose_compatibility (u, decomposed);
michael@0 183 if (!len)
michael@0 184 return 0;
michael@0 185
michael@0 186 for (i = 0; i < len; i++)
michael@0 187 if (!c->font->get_glyph (decomposed[i], 0, &glyphs[i]))
michael@0 188 return 0;
michael@0 189
michael@0 190 for (i = 0; i < len; i++)
michael@0 191 output_char (c->buffer, decomposed[i], glyphs[i]);
michael@0 192
michael@0 193 return len;
michael@0 194 }
michael@0 195
michael@0 196 static inline void
michael@0 197 decompose_current_character (const hb_ot_shape_normalize_context_t *c, bool shortest)
michael@0 198 {
michael@0 199 hb_buffer_t * const buffer = c->buffer;
michael@0 200 hb_codepoint_t glyph;
michael@0 201
michael@0 202 /* Kind of a cute waterfall here... */
michael@0 203 if (shortest && c->font->get_glyph (buffer->cur().codepoint, 0, &glyph))
michael@0 204 next_char (buffer, glyph);
michael@0 205 else if (decompose (c, shortest, buffer->cur().codepoint))
michael@0 206 skip_char (buffer);
michael@0 207 else if (!shortest && c->font->get_glyph (buffer->cur().codepoint, 0, &glyph))
michael@0 208 next_char (buffer, glyph);
michael@0 209 else if (decompose_compatibility (c, buffer->cur().codepoint))
michael@0 210 skip_char (buffer);
michael@0 211 else
michael@0 212 next_char (buffer, glyph); /* glyph is initialized in earlier branches. */
michael@0 213 }
michael@0 214
michael@0 215 static inline void
michael@0 216 handle_variation_selector_cluster (const hb_ot_shape_normalize_context_t *c, unsigned int end, bool short_circuit)
michael@0 217 {
michael@0 218 /* TODO Currently if there's a variation-selector we give-up, it's just too hard. */
michael@0 219 hb_buffer_t * const buffer = c->buffer;
michael@0 220 hb_font_t * const font = c->font;
michael@0 221 for (; buffer->idx < end - 1;) {
michael@0 222 if (unlikely (buffer->unicode->is_variation_selector (buffer->cur(+1).codepoint))) {
michael@0 223 /* The next two lines are some ugly lines... But work. */
michael@0 224 if (font->get_glyph (buffer->cur().codepoint, buffer->cur(+1).codepoint, &buffer->cur().glyph_index()))
michael@0 225 {
michael@0 226 buffer->replace_glyphs (2, 1, &buffer->cur().codepoint);
michael@0 227 }
michael@0 228 else
michael@0 229 {
michael@0 230 /* Just pass on the two characters separately, let GSUB do its magic. */
michael@0 231 set_glyph (buffer->cur(), font);
michael@0 232 buffer->next_glyph ();
michael@0 233 set_glyph (buffer->cur(), font);
michael@0 234 buffer->next_glyph ();
michael@0 235 }
michael@0 236 /* Skip any further variation selectors. */
michael@0 237 while (buffer->idx < end && unlikely (buffer->unicode->is_variation_selector (buffer->cur().codepoint)))
michael@0 238 {
michael@0 239 set_glyph (buffer->cur(), font);
michael@0 240 buffer->next_glyph ();
michael@0 241 }
michael@0 242 } else {
michael@0 243 set_glyph (buffer->cur(), font);
michael@0 244 buffer->next_glyph ();
michael@0 245 }
michael@0 246 }
michael@0 247 if (likely (buffer->idx < end)) {
michael@0 248 set_glyph (buffer->cur(), font);
michael@0 249 buffer->next_glyph ();
michael@0 250 }
michael@0 251 }
michael@0 252
michael@0 253 static inline void
michael@0 254 decompose_multi_char_cluster (const hb_ot_shape_normalize_context_t *c, unsigned int end, bool short_circuit)
michael@0 255 {
michael@0 256 hb_buffer_t * const buffer = c->buffer;
michael@0 257 for (unsigned int i = buffer->idx; i < end; i++)
michael@0 258 if (unlikely (buffer->unicode->is_variation_selector (buffer->info[i].codepoint))) {
michael@0 259 handle_variation_selector_cluster (c, end, short_circuit);
michael@0 260 return;
michael@0 261 }
michael@0 262
michael@0 263 while (buffer->idx < end)
michael@0 264 decompose_current_character (c, short_circuit);
michael@0 265 }
michael@0 266
michael@0 267 static inline void
michael@0 268 decompose_cluster (const hb_ot_shape_normalize_context_t *c, unsigned int end, bool might_short_circuit, bool always_short_circuit)
michael@0 269 {
michael@0 270 if (likely (c->buffer->idx + 1 == end))
michael@0 271 decompose_current_character (c, might_short_circuit);
michael@0 272 else
michael@0 273 decompose_multi_char_cluster (c, end, always_short_circuit);
michael@0 274 }
michael@0 275
michael@0 276
michael@0 277 static int
michael@0 278 compare_combining_class (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
michael@0 279 {
michael@0 280 unsigned int a = _hb_glyph_info_get_modified_combining_class (pa);
michael@0 281 unsigned int b = _hb_glyph_info_get_modified_combining_class (pb);
michael@0 282
michael@0 283 return a < b ? -1 : a == b ? 0 : +1;
michael@0 284 }
michael@0 285
michael@0 286
michael@0 287 void
michael@0 288 _hb_ot_shape_normalize (const hb_ot_shape_plan_t *plan,
michael@0 289 hb_buffer_t *buffer,
michael@0 290 hb_font_t *font)
michael@0 291 {
michael@0 292 hb_ot_shape_normalization_mode_t mode = plan->shaper->normalization_preference;
michael@0 293 const hb_ot_shape_normalize_context_t c = {
michael@0 294 plan,
michael@0 295 buffer,
michael@0 296 font,
michael@0 297 buffer->unicode,
michael@0 298 plan->shaper->decompose ? plan->shaper->decompose : decompose_unicode,
michael@0 299 plan->shaper->compose ? plan->shaper->compose : compose_unicode
michael@0 300 };
michael@0 301
michael@0 302 bool always_short_circuit = mode == HB_OT_SHAPE_NORMALIZATION_MODE_NONE;
michael@0 303 bool might_short_circuit = always_short_circuit ||
michael@0 304 (mode != HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED &&
michael@0 305 mode != HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT);
michael@0 306 unsigned int count;
michael@0 307
michael@0 308 /* We do a fairly straightforward yet custom normalization process in three
michael@0 309 * separate rounds: decompose, reorder, recompose (if desired). Currently
michael@0 310 * this makes two buffer swaps. We can make it faster by moving the last
michael@0 311 * two rounds into the inner loop for the first round, but it's more readable
michael@0 312 * this way. */
michael@0 313
michael@0 314
michael@0 315 /* First round, decompose */
michael@0 316
michael@0 317 buffer->clear_output ();
michael@0 318 count = buffer->len;
michael@0 319 for (buffer->idx = 0; buffer->idx < count;)
michael@0 320 {
michael@0 321 unsigned int end;
michael@0 322 for (end = buffer->idx + 1; end < count; end++)
michael@0 323 if (buffer->cur().cluster != buffer->info[end].cluster)
michael@0 324 break;
michael@0 325
michael@0 326 decompose_cluster (&c, end, might_short_circuit, always_short_circuit);
michael@0 327 }
michael@0 328 buffer->swap_buffers ();
michael@0 329
michael@0 330
michael@0 331 /* Second round, reorder (inplace) */
michael@0 332
michael@0 333 count = buffer->len;
michael@0 334 for (unsigned int i = 0; i < count; i++)
michael@0 335 {
michael@0 336 if (_hb_glyph_info_get_modified_combining_class (&buffer->info[i]) == 0)
michael@0 337 continue;
michael@0 338
michael@0 339 unsigned int end;
michael@0 340 for (end = i + 1; end < count; end++)
michael@0 341 if (_hb_glyph_info_get_modified_combining_class (&buffer->info[end]) == 0)
michael@0 342 break;
michael@0 343
michael@0 344 /* We are going to do a bubble-sort. Only do this if the
michael@0 345 * sequence is short. Doing it on long sequences can result
michael@0 346 * in an O(n^2) DoS. */
michael@0 347 if (end - i > 10) {
michael@0 348 i = end;
michael@0 349 continue;
michael@0 350 }
michael@0 351
michael@0 352 hb_bubble_sort (buffer->info + i, end - i, compare_combining_class);
michael@0 353
michael@0 354 i = end;
michael@0 355 }
michael@0 356
michael@0 357
michael@0 358 if (mode == HB_OT_SHAPE_NORMALIZATION_MODE_NONE ||
michael@0 359 mode == HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED)
michael@0 360 return;
michael@0 361
michael@0 362 /* Third round, recompose */
michael@0 363
michael@0 364 /* As noted in the comment earlier, we don't try to combine
michael@0 365 * ccc=0 chars with their previous Starter. */
michael@0 366
michael@0 367 buffer->clear_output ();
michael@0 368 count = buffer->len;
michael@0 369 unsigned int starter = 0;
michael@0 370 buffer->next_glyph ();
michael@0 371 while (buffer->idx < count)
michael@0 372 {
michael@0 373 hb_codepoint_t composed, glyph;
michael@0 374 if (/* We don't try to compose a non-mark character with it's preceding starter.
michael@0 375 * This is both an optimization to avoid trying to compose every two neighboring
michael@0 376 * glyphs in most scripts AND a desired feature for Hangul. Apparently Hangul
michael@0 377 * fonts are not designed to mix-and-match pre-composed syllables and Jamo. */
michael@0 378 HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&buffer->cur())) &&
michael@0 379 /* If there's anything between the starter and this char, they should have CCC
michael@0 380 * smaller than this character's. */
michael@0 381 (starter == buffer->out_len - 1 ||
michael@0 382 _hb_glyph_info_get_modified_combining_class (&buffer->prev()) < _hb_glyph_info_get_modified_combining_class (&buffer->cur())) &&
michael@0 383 /* And compose. */
michael@0 384 c.compose (&c,
michael@0 385 buffer->out_info[starter].codepoint,
michael@0 386 buffer->cur().codepoint,
michael@0 387 &composed) &&
michael@0 388 /* And the font has glyph for the composite. */
michael@0 389 font->get_glyph (composed, 0, &glyph))
michael@0 390 {
michael@0 391 /* Composes. */
michael@0 392 buffer->next_glyph (); /* Copy to out-buffer. */
michael@0 393 if (unlikely (buffer->in_error))
michael@0 394 return;
michael@0 395 buffer->merge_out_clusters (starter, buffer->out_len);
michael@0 396 buffer->out_len--; /* Remove the second composable. */
michael@0 397 /* Modify starter and carry on. */
michael@0 398 buffer->out_info[starter].codepoint = composed;
michael@0 399 buffer->out_info[starter].glyph_index() = glyph;
michael@0 400 _hb_glyph_info_set_unicode_props (&buffer->out_info[starter], buffer->unicode);
michael@0 401
michael@0 402 continue;
michael@0 403 }
michael@0 404
michael@0 405 /* Blocked, or doesn't compose. */
michael@0 406 buffer->next_glyph ();
michael@0 407
michael@0 408 if (_hb_glyph_info_get_modified_combining_class (&buffer->prev()) == 0)
michael@0 409 starter = buffer->out_len - 1;
michael@0 410 }
michael@0 411 buffer->swap_buffers ();
michael@0 412
michael@0 413 }

mercurial