blob: a9019fb7270494ba412ce116676eeea5b2106dd5 [file] [log] [blame]
Behdad Esfahbod655586f2011-07-21 00:51:18 -04001/*
Behdad Esfahbod11138cc2012-04-05 17:25:19 -04002 * Copyright © 2011,2012 Google, Inc.
Behdad Esfahbod655586f2011-07-21 00:51:18 -04003 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
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.
11 *
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
16 * DAMAGE.
17 *
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.
23 *
24 * Google Author(s): Behdad Esfahbod
25 */
26
Behdad Esfahbod11138cc2012-04-05 17:25:19 -040027#include "hb-ot-shape-normalize-private.hh"
Behdad Esfahbod655586f2011-07-21 00:51:18 -040028#include "hb-ot-shape-private.hh"
29
Behdad Esfahbod655586f2011-07-21 00:51:18 -040030
Behdad Esfahbod5d90a342011-07-21 15:25:01 -040031/*
32 * HIGHLEVEL DESIGN:
33 *
34 * This file exports one main function: _hb_ot_shape_normalize().
35 *
36 * This function closely reflects the Unicode Normalization Algorithm,
Behdad Esfahbodc3466712012-03-06 20:47:50 -050037 * yet it's different.
38 *
39 * Each shaper specifies whether it prefers decomposed (NFD) or composed (NFC).
40 * The logic however tries to use whatever the font can support.
Behdad Esfahbod5d90a342011-07-21 15:25:01 -040041 *
42 * In general what happens is that: each grapheme is decomposed in a chain
Behdad Esfahbod947c9a72011-09-16 16:33:18 -040043 * of 1:2 decompositions, marks reordered, and then recomposed if desired,
Behdad Esfahbod5d90a342011-07-21 15:25:01 -040044 * so far it's like Unicode Normalization. However, the decomposition and
45 * recomposition only happens if the font supports the resulting characters.
46 *
47 * The goals are:
48 *
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.
55 *
56 * - When a font has a precomposed character for a sequence but the 'ccmp'
Behdad Esfahbod947c9a72011-09-16 16:33:18 -040057 * feature in the font is not adequate, use the precomposed character
Behdad Esfahbod5d90a342011-07-21 15:25:01 -040058 * which typically has better mark positioning.
59 *
Behdad Esfahbod55deff72011-09-28 16:20:09 -040060 * - When a font does not support a combining mark, but supports it precomposed
Behdad Esfahbodc3466712012-03-06 20:47:50 -050061 * with previous base, use that. This needs the itemizer to have this
Behdad Esfahbode3b2e072012-03-07 10:21:24 -050062 * knowledge too. We need to provide assistance to the itemizer.
Behdad Esfahbod55deff72011-09-28 16:20:09 -040063 *
Behdad Esfahbod5d90a342011-07-21 15:25:01 -040064 * - When a font does not support a character but supports its decomposition,
65 * well, use the decomposition.
66 *
67 * - The Indic shaper requests decomposed output. This will handle splitting
68 * matra for the Indic shaper.
69 */
70
Behdad Esfahbodc311d852011-07-23 23:43:54 -040071static void
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +020072output_glyph (hb_buffer_t *buffer, hb_codepoint_t glyph)
Behdad Esfahbodc311d852011-07-23 23:43:54 -040073{
Behdad Esfahbodc311d852011-07-23 23:43:54 -040074 buffer->output_glyph (glyph);
Behdad Esfahbod99c26952012-05-13 15:45:18 +020075 _hb_glyph_info_set_unicode_props (&buffer->prev(), buffer->unicode);
Behdad Esfahbodc311d852011-07-23 23:43:54 -040076}
Behdad Esfahbod5c6f5982011-07-21 11:31:08 -040077
Behdad Esfahbod45412522011-07-22 11:07:05 -040078static bool
Behdad Esfahbod11138cc2012-04-05 17:25:19 -040079decompose (hb_font_t *font, hb_buffer_t *buffer,
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -040080 bool shortest,
Behdad Esfahbod45412522011-07-22 11:07:05 -040081 hb_codepoint_t ab)
82{
83 hb_codepoint_t a, b, glyph;
Behdad Esfahbod45412522011-07-22 11:07:05 -040084
Behdad Esfahbod11138cc2012-04-05 17:25:19 -040085 if (!hb_unicode_decompose (buffer->unicode, ab, &a, &b) ||
86 (b && !hb_font_get_glyph (font, b, 0, &glyph)))
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -040087 return FALSE;
Behdad Esfahbod45412522011-07-22 11:07:05 -040088
Behdad Esfahbod11138cc2012-04-05 17:25:19 -040089 bool has_a = hb_font_get_glyph (font, a, 0, &glyph);
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -040090 if (shortest && has_a) {
91 /* Output a and b */
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +020092 output_glyph (buffer, a);
Behdad Esfahboddcdc51c2011-07-22 17:14:46 -040093 if (b)
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +020094 output_glyph (buffer, b);
Behdad Esfahbod45412522011-07-22 11:07:05 -040095 return TRUE;
96 }
97
Behdad Esfahbod11138cc2012-04-05 17:25:19 -040098 if (decompose (font, buffer, shortest, a)) {
Behdad Esfahboddcdc51c2011-07-22 17:14:46 -040099 if (b)
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +0200100 output_glyph (buffer, b);
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400101 return TRUE;
102 }
Behdad Esfahbod5c6f5982011-07-21 11:31:08 -0400103
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400104 if (has_a) {
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +0200105 output_glyph (buffer, a);
Behdad Esfahboddcdc51c2011-07-22 17:14:46 -0400106 if (b)
Behdad Esfahbod9f377ed2012-05-13 16:13:44 +0200107 output_glyph (buffer, b);
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400108 return TRUE;
109 }
110
Behdad Esfahbod5c6f5982011-07-21 11:31:08 -0400111 return FALSE;
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400112}
113
Behdad Esfahbodc311d852011-07-23 23:43:54 -0400114static void
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400115decompose_current_glyph (hb_font_t *font, hb_buffer_t *buffer,
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400116 bool shortest)
Behdad Esfahbodd6b9c6d2011-07-21 12:16:45 -0400117{
Behdad Esfahbod99c26952012-05-13 15:45:18 +0200118 if (decompose (font, buffer, shortest, buffer->cur().codepoint))
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400119 buffer->skip_glyph ();
Behdad Esfahbodc311d852011-07-23 23:43:54 -0400120 else
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400121 buffer->next_glyph ();
Behdad Esfahbodd6b9c6d2011-07-21 12:16:45 -0400122}
123
Behdad Esfahbodc311d852011-07-23 23:43:54 -0400124static void
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400125decompose_single_char_cluster (hb_font_t *font, hb_buffer_t *buffer,
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400126 bool will_recompose)
127{
128 hb_codepoint_t glyph;
129
130 /* If recomposing and font supports this, we're good to go */
Behdad Esfahbod99c26952012-05-13 15:45:18 +0200131 if (will_recompose && hb_font_get_glyph (font, buffer->cur().codepoint, 0, &glyph)) {
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400132 buffer->next_glyph ();
Behdad Esfahbodc311d852011-07-23 23:43:54 -0400133 return;
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400134 }
135
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400136 decompose_current_glyph (font, buffer, will_recompose);
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400137}
138
Behdad Esfahbodc311d852011-07-23 23:43:54 -0400139static void
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400140decompose_multi_char_cluster (hb_font_t *font, hb_buffer_t *buffer,
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400141 unsigned int end)
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400142{
Behdad Esfahbod5d90a342011-07-21 15:25:01 -0400143 /* TODO Currently if there's a variation-selector we give-up, it's just too hard. */
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400144 for (unsigned int i = buffer->idx; i < end; i++)
145 if (unlikely (_hb_unicode_is_variation_selector (buffer->info[i].codepoint))) {
146 while (buffer->idx < end)
147 buffer->next_glyph ();
Behdad Esfahbodc311d852011-07-23 23:43:54 -0400148 return;
Behdad Esfahbodaf913c52011-10-17 11:39:28 -0700149 }
Behdad Esfahbodd6b9c6d2011-07-21 12:16:45 -0400150
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400151 while (buffer->idx < end)
152 decompose_current_glyph (font, buffer, FALSE);
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400153}
154
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400155static int
156compare_combining_class (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
157{
Behdad Esfahbodd1deaa22012-05-09 15:04:13 +0200158 unsigned int a = _hb_glyph_info_get_modified_combining_class (pa);
159 unsigned int b = _hb_glyph_info_get_modified_combining_class (pb);
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400160
161 return a < b ? -1 : a == b ? 0 : +1;
162}
163
Behdad Esfahbod45412522011-07-22 11:07:05 -0400164void
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400165_hb_ot_shape_normalize (hb_font_t *font, hb_buffer_t *buffer,
166 hb_ot_shape_normalization_mode_t mode)
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400167{
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400168 bool recompose = mode != HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED;
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400169 bool has_multichar_clusters = FALSE;
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400170 unsigned int count;
Behdad Esfahbod5c6f5982011-07-21 11:31:08 -0400171
Behdad Esfahbodc311d852011-07-23 23:43:54 -0400172 /* We do a fairly straightforward yet custom normalization process in three
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400173 * separate rounds: decompose, reorder, recompose (if desired). Currently
174 * this makes two buffer swaps. We can make it faster by moving the last
175 * two rounds into the inner loop for the first round, but it's more readable
176 * this way. */
Behdad Esfahbod5d90a342011-07-21 15:25:01 -0400177
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400178
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400179 /* First round, decompose */
180
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400181 buffer->clear_output ();
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400182 count = buffer->len;
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400183 for (buffer->idx = 0; buffer->idx < count;)
Behdad Esfahbod5d90a342011-07-21 15:25:01 -0400184 {
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400185 unsigned int end;
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400186 for (end = buffer->idx + 1; end < count; end++)
Behdad Esfahbod99c26952012-05-13 15:45:18 +0200187 if (buffer->cur().cluster != buffer->info[end].cluster)
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400188 break;
Behdad Esfahbod5d90a342011-07-21 15:25:01 -0400189
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400190 if (buffer->idx + 1 == end)
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400191 decompose_single_char_cluster (font, buffer, recompose);
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400192 else {
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400193 decompose_multi_char_cluster (font, buffer, end);
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400194 has_multichar_clusters = TRUE;
195 }
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400196 }
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400197 buffer->swap_buffers ();
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400198
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400199
Behdad Esfahbod96831842012-04-07 14:57:21 -0400200 if (mode != HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_FULL && !has_multichar_clusters)
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400201 return; /* Done! */
202
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400203
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400204 /* Second round, reorder (inplace) */
205
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400206 count = buffer->len;
207 for (unsigned int i = 0; i < count; i++)
208 {
Behdad Esfahbodd1deaa22012-05-09 15:04:13 +0200209 if (_hb_glyph_info_get_modified_combining_class (&buffer->info[i]) == 0)
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400210 continue;
211
212 unsigned int end;
213 for (end = i + 1; end < count; end++)
Behdad Esfahbodd1deaa22012-05-09 15:04:13 +0200214 if (_hb_glyph_info_get_modified_combining_class (&buffer->info[end]) == 0)
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400215 break;
216
217 /* We are going to do a bubble-sort. Only do this if the
218 * sequence is short. Doing it on long sequences can result
219 * in an O(n^2) DoS. */
220 if (end - i > 10) {
221 i = end;
222 continue;
223 }
224
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400225 hb_bubble_sort (buffer->info + i, end - i, compare_combining_class);
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400226
227 i = end;
228 }
229
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400230
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400231 if (!recompose)
232 return;
233
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400234 /* Third round, recompose */
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400235
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400236 /* As noted in the comment earlier, we don't try to combine
237 * ccc=0 chars with their previous Starter. */
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400238
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400239 buffer->clear_output ();
240 count = buffer->len;
241 unsigned int starter = 0;
242 buffer->next_glyph ();
243 while (buffer->idx < count)
244 {
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400245 hb_codepoint_t composed, glyph;
Behdad Esfahbod96831842012-04-07 14:57:21 -0400246 if (/* If mode is NOT COMPOSED_FULL (ie. it's COMPOSED_DIACRITICS), we don't try to
247 * compose a CCC=0 character with it's preceding starter. */
248 (mode == HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_FULL ||
Behdad Esfahbod99c26952012-05-13 15:45:18 +0200249 _hb_glyph_info_get_modified_combining_class (&buffer->cur()) != 0) &&
Behdad Esfahbod96831842012-04-07 14:57:21 -0400250 /* If there's anything between the starter and this char, they should have CCC
251 * smaller than this character's. */
252 (starter == buffer->out_len - 1 ||
Behdad Esfahbod99c26952012-05-13 15:45:18 +0200253 _hb_glyph_info_get_modified_combining_class (&buffer->prev()) < _hb_glyph_info_get_modified_combining_class (&buffer->cur())) &&
Behdad Esfahbod96831842012-04-07 14:57:21 -0400254 /* And compose. */
Behdad Esfahbode02d9252012-04-07 14:49:13 -0400255 hb_unicode_compose (buffer->unicode,
256 buffer->out_info[starter].codepoint,
Behdad Esfahbod99c26952012-05-13 15:45:18 +0200257 buffer->cur().codepoint,
Behdad Esfahbode02d9252012-04-07 14:49:13 -0400258 &composed) &&
Behdad Esfahbod96831842012-04-07 14:57:21 -0400259 /* And the font has glyph for the composite. */
Behdad Esfahbode02d9252012-04-07 14:49:13 -0400260 hb_font_get_glyph (font, composed, 0, &glyph))
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400261 {
Behdad Esfahbode02d9252012-04-07 14:49:13 -0400262 /* Composes. Modify starter and carry on. */
263 buffer->out_info[starter].codepoint = composed;
Behdad Esfahbod29a7e302012-04-24 16:01:30 -0400264 /* XXX update cluster */
Behdad Esfahbodd1deaa22012-05-09 15:04:13 +0200265 _hb_glyph_info_set_unicode_props (&buffer->out_info[starter], buffer->unicode);
Behdad Esfahbode02d9252012-04-07 14:49:13 -0400266
267 buffer->skip_glyph ();
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400268 continue;
269 }
270
Behdad Esfahbode02d9252012-04-07 14:49:13 -0400271 /* Blocked, or doesn't compose. */
272 buffer->next_glyph ();
Behdad Esfahbod96831842012-04-07 14:57:21 -0400273
Behdad Esfahbod99c26952012-05-13 15:45:18 +0200274 if (_hb_glyph_info_get_modified_combining_class (&buffer->prev()) == 0)
Behdad Esfahbod96831842012-04-07 14:57:21 -0400275 starter = buffer->out_len - 1;
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400276 }
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400277 buffer->swap_buffers ();
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400278
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400279}