2 * Copyright © 2011 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-private.hh"
28 #include "hb-ot-shape-complex-private.hh"
35 * This file exports one main function: _hb_ot_shape_normalize().
37 * This function closely reflects the Unicode Normalization Algorithm,
38 * yet it's different. The shaper an either prefer decomposed (NFD) or
41 * In general what happens is that: each grapheme is decomposed in a chain
42 * of 1:2 decompositions, marks reordered, and then recomposed if desires,
43 * so far it's like Unicode Normalization. However, the decomposition and
44 * recomposition only happens if the font supports the resulting characters.
48 * - Try to render all canonically equivalent strings similarly. To really
49 * achieve this we have to always do the full decomposition and then
50 * selectively recompose from there. It's kinda too expensive though, so
51 * we skip some cases. For example, if composed is desired, we simply
52 * don't touch 1-character clusters that are supported by the font, even
53 * though their NFC may be different.
55 * - When a font has a precomposed character for a sequence but the 'ccmp'
56 * feature in the font is not adequate, form use the precomposed character
57 * which typically has better mark positioning.
59 * - When a font does not support a character but supports its decomposition,
60 * well, use the decomposition.
62 * - The Indic shaper requests decomposed output. This will handle splitting
63 * matra for the Indic shaper.
68 decompose (hb_ot_shape_context_t *c,
72 hb_codepoint_t a, b, glyph;
74 if (!hb_unicode_decompose (c->buffer->unicode, ab, &a, &b) ||
75 !hb_font_get_glyph (c->font, b, 0, &glyph))
78 /* XXX handle singleton decompositions */
79 bool has_a = hb_font_get_glyph (c->font, a, 0, &glyph);
80 if (shortest && has_a) {
82 c->buffer->output_glyph (a);
83 c->buffer->output_glyph (b);
87 if (decompose (c, shortest, a)) {
88 c->buffer->output_glyph (b);
93 c->buffer->output_glyph (a);
94 c->buffer->output_glyph (b);
102 decompose_current_glyph (hb_ot_shape_context_t *c,
105 if (decompose (c, shortest, c->buffer->info[c->buffer->idx].codepoint)) {
106 c->buffer->skip_glyph ();
109 c->buffer->next_glyph ();
115 decompose_single_char_cluster (hb_ot_shape_context_t *c,
118 hb_codepoint_t glyph;
120 /* If recomposing and font supports this, we're good to go */
121 if (will_recompose && hb_font_get_glyph (c->font, c->buffer->info[c->buffer->idx].codepoint, 0, &glyph)) {
122 c->buffer->next_glyph ();
126 return decompose_current_glyph (c, will_recompose);
130 decompose_multi_char_cluster (hb_ot_shape_context_t *c,
133 bool changed = FALSE;
135 /* TODO Currently if there's a variation-selector we give-up, it's just too hard. */
136 for (unsigned int i = c->buffer->idx; i < end; i++)
137 if (unlikely (is_variation_selector (c->buffer->info[i].codepoint)))
140 while (c->buffer->idx < end)
141 changed |= decompose_current_glyph (c, FALSE);
147 _hb_ot_shape_normalize (hb_ot_shape_context_t *c)
149 hb_buffer_t *buffer = c->buffer;
150 bool recompose = !hb_ot_shape_complex_prefer_decomposed (c->plan->shaper);
151 bool changed = FALSE;
152 bool has_multichar_clusters = FALSE;
155 buffer->clear_output ();
158 /* First round, decompose */
161 for (buffer->idx = 0; buffer->idx < count;)
164 for (end = buffer->idx + 1; end < count; end++)
165 if (buffer->info[buffer->idx].cluster != buffer->info[end].cluster)
168 if (buffer->idx + 1 == end)
169 changed |= decompose_single_char_cluster (c, recompose);
171 changed |= decompose_multi_char_cluster (c, end);
172 has_multichar_clusters = TRUE;
175 buffer->swap_buffers ();
178 /* Technically speaking, two characters with ccc=0 may combine. But all
179 * those cases are in languages that the indic module handles (which expects
180 * decomposed), or in Hangul jamo, which again, we want decomposed anyway.
181 * So we don't bother combining across cluster boundaries. */
183 if (!has_multichar_clusters)
187 _hb_set_unicode_props (c->buffer); /* BUFFER: Set general_category and combining_class in var1 */
190 /* Second round, reorder (inplace) */
193 for (unsigned int i = 0; i < count; i++)
195 if (buffer->info[i].combining_class() == 0)
199 for (end = i + 1; end < count; end++)
200 if (buffer->info[end].combining_class() == 0)
203 /* We are going to do a bubble-sort. Only do this if the
204 * sequence is short. Doing it on long sequences can result
205 * in an O(n^2) DoS. */
211 unsigned int k = end - i - 1;
213 hb_glyph_info_t *pinfo = buffer->info + i;
214 unsigned int new_k = 0;
216 for (unsigned int j = 0; j < k; j++)
217 if (pinfo[j].combining_class() > pinfo[j+1].combining_class()) {
220 pinfo[j] = pinfo[j + 1];
232 /* Third round, recompose */