2 * Copyright © 2011,2012 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
27 #include "hb-ot-shape-normalize-private.hh"
28 #include "hb-ot-shape-private.hh"
34 * This file exports one main function: _hb_ot_shape_normalize().
36 * This function closely reflects the Unicode Normalization Algorithm,
39 * Each shaper specifies whether it prefers decomposed (NFD) or composed (NFC).
40 * The logic however tries to use whatever the font can support.
42 * In general what happens is that: each grapheme is decomposed in a chain
43 * of 1:2 decompositions, marks reordered, and then recomposed if desired,
44 * so far it's like Unicode Normalization. However, the decomposition and
45 * recomposition only happens if the font supports the resulting characters.
49 * - Try to render all canonically equivalent strings similarly. To really
50 * achieve this we have to always do the full decomposition and then
51 * selectively recompose from there. It's kinda too expensive though, so
52 * we skip some cases. For example, if composed is desired, we simply
53 * don't touch 1-character clusters that are supported by the font, even
54 * though their NFC may be different.
56 * - When a font has a precomposed character for a sequence but the 'ccmp'
57 * feature in the font is not adequate, use the precomposed character
58 * which typically has better mark positioning.
60 * - When a font does not support a combining mark, but supports it precomposed
61 * with previous base, use that. This needs the itemizer to have this
62 * knowledge too. We need to provide assistance to the itemizer.
64 * - When a font does not support a character but supports its decomposition,
65 * well, use the decomposition (preferring the canonical decomposition, but
66 * falling back to the compatibility decomposition if necessary). The
67 * compatibility decomposition is really nice to have, for characters like
68 * ellipsis, or various-sized space characters.
70 * - The complex shapers can customize the compose and decompose functions to
71 * offload some of their requirements to the normalizer. For example, the
72 * Indic shaper may want to disallow recomposing of two matras.
74 * - We try compatibility decomposition if decomposing through canonical
75 * decomposition alone failed to find a sequence that the font supports.
76 * We don't try compatibility decomposition recursively during the canonical
77 * decomposition phase. This has minimal impact. There are only a handful
78 * of Greek letter that have canonical decompositions that include characters
79 * with compatibility decomposition. Those can be found using this command:
81 * egrep "`echo -n ';('; grep ';<' UnicodeData.txt | cut -d';' -f1 | tr '\n' '|'; echo ') '`" UnicodeData.txt
85 decompose_func (hb_unicode_funcs_t
*unicode
,
90 /* XXX FIXME, move these to complex shapers and propagage to normalizer.*/
92 case 0x0AC9 : return false;
94 case 0x0931 : return false;
95 case 0x0B94 : return false;
97 /* These ones have Unicode decompositions, but we do it
98 * this way to be close to what Uniscribe does. */
99 case 0x0DDA : *a
= 0x0DD9; *b
= 0x0DDA; return true;
100 case 0x0DDC : *a
= 0x0DD9; *b
= 0x0DDC; return true;
101 case 0x0DDD : *a
= 0x0DD9; *b
= 0x0DDD; return true;
102 case 0x0DDE : *a
= 0x0DD9; *b
= 0x0DDE; return true;
104 case 0x0F77 : *a
= 0x0FB2; *b
= 0x0F81; return true;
105 case 0x0F79 : *a
= 0x0FB3; *b
= 0x0F81; return true;
106 case 0x17BE : *a
= 0x17C1; *b
= 0x17BE; return true;
107 case 0x17BF : *a
= 0x17C1; *b
= 0x17BF; return true;
108 case 0x17C0 : *a
= 0x17C1; *b
= 0x17C0; return true;
109 case 0x17C4 : *a
= 0x17C1; *b
= 0x17C4; return true;
110 case 0x17C5 : *a
= 0x17C1; *b
= 0x17C5; return true;
111 case 0x1925 : *a
= 0x1920; *b
= 0x1923; return true;
112 case 0x1926 : *a
= 0x1920; *b
= 0x1924; return true;
113 case 0x1B3C : *a
= 0x1B42; *b
= 0x1B3C; return true;
114 case 0x1112E : *a
= 0x11127; *b
= 0x11131; return true;
115 case 0x1112F : *a
= 0x11127; *b
= 0x11132; return true;
117 case 0x0B57 : *a
= 0xno decomp
, -> RIGHT
; return true;
118 case 0x1C29 : *a
= 0xno decomp
, -> LEFT
; return true;
119 case 0xA9C0 : *a
= 0xno decomp
, -> RIGHT
; return true;
120 case 0x111BF : *a
= 0xno decomp
, -> ABOVE
; return true;
123 return unicode
->decompose (ab
, a
, b
);
127 compose_func (hb_unicode_funcs_t
*unicode
,
132 /* XXX, this belongs to indic normalizer. */
133 if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (unicode
->general_category (a
)))
135 /* XXX, add composition-exclusion exceptions to Indic shaper. */
136 if (a
== 0x09AF && b
== 0x09BC) { *ab
= 0x09DF; return true; }
138 /* XXX, these belong to the hebew / default shaper. */
139 /* Hebrew presentation-form shaping.
140 * https://bugzilla.mozilla.org/show_bug.cgi?id=728866 */
141 // Hebrew presentation forms with dagesh, for characters 0x05D0..0x05EA;
142 // note that some letters do not have a dagesh presForm encoded
143 static const hb_codepoint_t sDageshForms
[0x05EA - 0x05D0 + 1] = {
173 hb_bool_t found
= unicode
->compose (a
, b
, ab
);
175 if (!found
&& (b
& ~0x7F) == 0x0580) {
176 // special-case Hebrew presentation forms that are excluded from
177 // standard normalization, but wanted for old fonts
179 case 0x05B4: // HIRIQ
180 if (a
== 0x05D9) { // YOD
185 case 0x05B7: // patah
186 if (a
== 0x05F2) { // YIDDISH YOD YOD
189 } else if (a
== 0x05D0) { // ALEF
194 case 0x05B8: // QAMATS
195 if (a
== 0x05D0) { // ALEF
200 case 0x05B9: // HOLAM
201 if (a
== 0x05D5) { // VAV
206 case 0x05BC: // DAGESH
207 if (a
>= 0x05D0 && a
<= 0x05EA) {
208 *ab
= sDageshForms
[a
- 0x05D0];
210 } else if (a
== 0xFB2A) { // SHIN WITH SHIN DOT
213 } else if (a
== 0xFB2B) { // SHIN WITH SIN DOT
234 case 0x05C1: // SHIN DOT
235 if (a
== 0x05E9) { // SHIN
238 } else if (a
== 0xFB49) { // SHIN WITH DAGESH
243 case 0x05C2: // SIN DOT
244 if (a
== 0x05E9) { // SHIN
247 } else if (a
== 0xFB49) { // SHIN WITH DAGESH
260 set_glyph (hb_glyph_info_t
&info
, hb_font_t
*font
)
262 font
->get_glyph (info
.codepoint
, 0, &info
.glyph_index());
266 output_char (hb_buffer_t
*buffer
, hb_codepoint_t unichar
, hb_codepoint_t glyph
)
268 buffer
->cur().glyph_index() = glyph
;
269 buffer
->output_glyph (unichar
);
270 _hb_glyph_info_set_unicode_props (&buffer
->prev(), buffer
->unicode
);
274 next_char (hb_buffer_t
*buffer
, hb_codepoint_t glyph
)
276 buffer
->cur().glyph_index() = glyph
;
277 buffer
->next_glyph ();
281 skip_char (hb_buffer_t
*buffer
)
283 buffer
->skip_glyph ();
286 /* Returns 0 if didn't decompose, number of resulting characters otherwise. */
287 static inline unsigned int
288 decompose (hb_font_t
*font
, hb_buffer_t
*buffer
, bool shortest
, hb_codepoint_t ab
)
290 hb_codepoint_t a
, b
, a_glyph
, b_glyph
;
292 if (!decompose_func (buffer
->unicode
, ab
, &a
, &b
) ||
293 (b
&& !font
->get_glyph (b
, 0, &b_glyph
)))
296 bool has_a
= font
->get_glyph (a
, 0, &a_glyph
);
297 if (shortest
&& has_a
) {
299 output_char (buffer
, a
, a_glyph
);
301 output_char (buffer
, b
, b_glyph
);
308 if ((ret
= decompose (font
, buffer
, shortest
, a
))) {
310 output_char (buffer
, b
, b_glyph
);
317 output_char (buffer
, a
, a_glyph
);
319 output_char (buffer
, b
, b_glyph
);
328 /* Returns 0 if didn't decompose, number of resulting characters otherwise. */
330 decompose_compatibility (hb_font_t
*font
, hb_buffer_t
*buffer
, hb_codepoint_t u
)
333 hb_codepoint_t decomposed
[HB_UNICODE_MAX_DECOMPOSITION_LEN
];
334 hb_codepoint_t glyphs
[HB_UNICODE_MAX_DECOMPOSITION_LEN
];
336 len
= buffer
->unicode
->decompose_compatibility (u
, decomposed
);
340 for (i
= 0; i
< len
; i
++)
341 if (!font
->get_glyph (decomposed
[i
], 0, &glyphs
[i
]))
344 for (i
= 0; i
< len
; i
++)
345 output_char (buffer
, decomposed
[i
], glyphs
[i
]);
350 /* Returns true if recomposition may be benefitial. */
352 decompose_current_character (hb_font_t
*font
, hb_buffer_t
*buffer
, bool shortest
)
354 hb_codepoint_t glyph
;
355 unsigned int len
= 1;
357 /* Kind of a cute waterfall here... */
358 if (shortest
&& font
->get_glyph (buffer
->cur().codepoint
, 0, &glyph
))
359 next_char (buffer
, glyph
);
360 else if ((len
= decompose (font
, buffer
, shortest
, buffer
->cur().codepoint
)))
362 else if (!shortest
&& font
->get_glyph (buffer
->cur().codepoint
, 0, &glyph
))
363 next_char (buffer
, glyph
);
364 else if ((len
= decompose_compatibility (font
, buffer
, buffer
->cur().codepoint
)))
367 next_char (buffer
, glyph
); /* glyph is initialized in earlier branches. */
370 * A recomposition would only be useful if we decomposed into at least three
377 handle_variation_selector_cluster (hb_font_t
*font
, hb_buffer_t
*buffer
, unsigned int end
)
379 for (; buffer
->idx
< end
- 1;) {
380 if (unlikely (buffer
->unicode
->is_variation_selector (buffer
->cur(+1).codepoint
))) {
381 /* The next two lines are some ugly lines... But work. */
382 font
->get_glyph (buffer
->cur().codepoint
, buffer
->cur(+1).codepoint
, &buffer
->cur().glyph_index());
383 buffer
->replace_glyphs (2, 1, &buffer
->cur().codepoint
);
385 set_glyph (buffer
->cur(), font
);
386 buffer
->next_glyph ();
389 if (likely (buffer
->idx
< end
)) {
390 set_glyph (buffer
->cur(), font
);
391 buffer
->next_glyph ();
395 /* Returns true if recomposition may be benefitial. */
397 decompose_multi_char_cluster (hb_font_t
*font
, hb_buffer_t
*buffer
, unsigned int end
)
399 /* TODO Currently if there's a variation-selector we give-up, it's just too hard. */
400 for (unsigned int i
= buffer
->idx
; i
< end
; i
++)
401 if (unlikely (buffer
->unicode
->is_variation_selector (buffer
->info
[i
].codepoint
))) {
402 handle_variation_selector_cluster (font
, buffer
, end
);
406 while (buffer
->idx
< end
)
407 decompose_current_character (font
, buffer
, false);
408 /* We can be smarter here and only return true if there are at least two ccc!=0 marks.
409 * But does not matter. */
414 decompose_cluster (hb_font_t
*font
, hb_buffer_t
*buffer
, bool short_circuit
, unsigned int end
)
416 if (likely (buffer
->idx
+ 1 == end
))
417 return decompose_current_character (font
, buffer
, short_circuit
);
419 return decompose_multi_char_cluster (font
, buffer
, end
);
424 compare_combining_class (const hb_glyph_info_t
*pa
, const hb_glyph_info_t
*pb
)
426 unsigned int a
= _hb_glyph_info_get_modified_combining_class (pa
);
427 unsigned int b
= _hb_glyph_info_get_modified_combining_class (pb
);
429 return a
< b
? -1 : a
== b
? 0 : +1;
434 _hb_ot_shape_normalize (hb_font_t
*font
, hb_buffer_t
*buffer
,
435 hb_ot_shape_normalization_mode_t mode
)
437 bool short_circuit
= mode
!= HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED
&&
438 mode
!= HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT
;
439 bool can_use_recompose
= false;
442 /* We do a fairly straightforward yet custom normalization process in three
443 * separate rounds: decompose, reorder, recompose (if desired). Currently
444 * this makes two buffer swaps. We can make it faster by moving the last
445 * two rounds into the inner loop for the first round, but it's more readable
449 /* First round, decompose */
451 buffer
->clear_output ();
453 for (buffer
->idx
= 0; buffer
->idx
< count
;)
456 for (end
= buffer
->idx
+ 1; end
< count
; end
++)
457 if (buffer
->cur().cluster
!= buffer
->info
[end
].cluster
)
460 can_use_recompose
= decompose_cluster (font
, buffer
, short_circuit
, end
) || can_use_recompose
;
462 buffer
->swap_buffers ();
465 if (mode
!= HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_FULL
&& !can_use_recompose
)
469 /* Second round, reorder (inplace) */
472 for (unsigned int i
= 0; i
< count
; i
++)
474 if (_hb_glyph_info_get_modified_combining_class (&buffer
->info
[i
]) == 0)
478 for (end
= i
+ 1; end
< count
; end
++)
479 if (_hb_glyph_info_get_modified_combining_class (&buffer
->info
[end
]) == 0)
482 /* We are going to do a bubble-sort. Only do this if the
483 * sequence is short. Doing it on long sequences can result
484 * in an O(n^2) DoS. */
490 hb_bubble_sort (buffer
->info
+ i
, end
- i
, compare_combining_class
);
496 if (mode
== HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED
)
499 /* Third round, recompose */
501 /* As noted in the comment earlier, we don't try to combine
502 * ccc=0 chars with their previous Starter. */
504 buffer
->clear_output ();
506 unsigned int starter
= 0;
507 buffer
->next_glyph ();
508 while (buffer
->idx
< count
)
510 hb_codepoint_t composed
, glyph
;
511 if (/* If mode is NOT COMPOSED_FULL (ie. it's COMPOSED_DIACRITICS), we don't try to
512 * compose a CCC=0 character with it's preceding starter. */
513 (mode
== HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_FULL
||
514 _hb_glyph_info_get_modified_combining_class (&buffer
->cur()) != 0) &&
515 /* If there's anything between the starter and this char, they should have CCC
516 * smaller than this character's. */
517 (starter
== buffer
->out_len
- 1 ||
518 _hb_glyph_info_get_modified_combining_class (&buffer
->prev()) < _hb_glyph_info_get_modified_combining_class (&buffer
->cur())) &&
520 compose_func (buffer
->unicode
,
521 buffer
->out_info
[starter
].codepoint
,
522 buffer
->cur().codepoint
,
524 /* And the font has glyph for the composite. */
525 font
->get_glyph (composed
, 0, &glyph
))
528 buffer
->next_glyph (); /* Copy to out-buffer. */
529 if (unlikely (buffer
->in_error
))
531 buffer
->merge_out_clusters (starter
, buffer
->out_len
);
532 buffer
->out_len
--; /* Remove the second composable. */
533 buffer
->out_info
[starter
].codepoint
= composed
; /* Modify starter and carry on. */
534 set_glyph (buffer
->out_info
[starter
], font
);
535 _hb_glyph_info_set_unicode_props (&buffer
->out_info
[starter
], buffer
->unicode
);
540 /* Blocked, or doesn't compose. */
541 buffer
->next_glyph ();
543 if (_hb_glyph_info_get_modified_combining_class (&buffer
->prev()) == 0)
544 starter
= buffer
->out_len
- 1;
546 buffer
->swap_buffers ();