blob: 0bcf7f58a7427774cd784e91c2f20dbeefdac6f6 [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 Esfahbod683b5032012-04-14 20:47:14 -040071static inline void
72set_unicode_props (hb_glyph_info_t *info, hb_unicode_funcs_t *unicode)
73{
74 info->general_category() = hb_unicode_general_category (unicode, info->codepoint);
75 info->combining_class() = _hb_unicode_modified_combining_class (unicode, info->codepoint);
76}
77
Behdad Esfahbodc311d852011-07-23 23:43:54 -040078static void
Behdad Esfahbod11138cc2012-04-05 17:25:19 -040079output_glyph (hb_font_t *font, hb_buffer_t *buffer,
Behdad Esfahbodc311d852011-07-23 23:43:54 -040080 hb_codepoint_t glyph)
81{
Behdad Esfahbodc311d852011-07-23 23:43:54 -040082 buffer->output_glyph (glyph);
Behdad Esfahbod683b5032012-04-14 20:47:14 -040083 set_unicode_props (&buffer->out_info[buffer->out_len - 1], buffer->unicode);
Behdad Esfahbodc311d852011-07-23 23:43:54 -040084}
Behdad Esfahbod5c6f5982011-07-21 11:31:08 -040085
Behdad Esfahbod45412522011-07-22 11:07:05 -040086static bool
Behdad Esfahbod11138cc2012-04-05 17:25:19 -040087decompose (hb_font_t *font, hb_buffer_t *buffer,
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -040088 bool shortest,
Behdad Esfahbod45412522011-07-22 11:07:05 -040089 hb_codepoint_t ab)
90{
91 hb_codepoint_t a, b, glyph;
Behdad Esfahbod45412522011-07-22 11:07:05 -040092
Behdad Esfahbod11138cc2012-04-05 17:25:19 -040093 if (!hb_unicode_decompose (buffer->unicode, ab, &a, &b) ||
94 (b && !hb_font_get_glyph (font, b, 0, &glyph)))
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -040095 return FALSE;
Behdad Esfahbod45412522011-07-22 11:07:05 -040096
Behdad Esfahbod11138cc2012-04-05 17:25:19 -040097 bool has_a = hb_font_get_glyph (font, a, 0, &glyph);
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -040098 if (shortest && has_a) {
99 /* Output a and b */
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400100 output_glyph (font, buffer, a);
Behdad Esfahboddcdc51c2011-07-22 17:14:46 -0400101 if (b)
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400102 output_glyph (font, buffer, b);
Behdad Esfahbod45412522011-07-22 11:07:05 -0400103 return TRUE;
104 }
105
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400106 if (decompose (font, buffer, shortest, a)) {
Behdad Esfahboddcdc51c2011-07-22 17:14:46 -0400107 if (b)
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400108 output_glyph (font, buffer, b);
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400109 return TRUE;
110 }
Behdad Esfahbod5c6f5982011-07-21 11:31:08 -0400111
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400112 if (has_a) {
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400113 output_glyph (font, buffer, a);
Behdad Esfahboddcdc51c2011-07-22 17:14:46 -0400114 if (b)
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400115 output_glyph (font, buffer, b);
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400116 return TRUE;
117 }
118
Behdad Esfahbod5c6f5982011-07-21 11:31:08 -0400119 return FALSE;
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400120}
121
Behdad Esfahbodc311d852011-07-23 23:43:54 -0400122static void
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400123decompose_current_glyph (hb_font_t *font, hb_buffer_t *buffer,
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400124 bool shortest)
Behdad Esfahbodd6b9c6d2011-07-21 12:16:45 -0400125{
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400126 if (decompose (font, buffer, shortest, buffer->info[buffer->idx].codepoint))
127 buffer->skip_glyph ();
Behdad Esfahbodc311d852011-07-23 23:43:54 -0400128 else
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400129 buffer->next_glyph ();
Behdad Esfahbodd6b9c6d2011-07-21 12:16:45 -0400130}
131
Behdad Esfahbodc311d852011-07-23 23:43:54 -0400132static void
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400133decompose_single_char_cluster (hb_font_t *font, hb_buffer_t *buffer,
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400134 bool will_recompose)
135{
136 hb_codepoint_t glyph;
137
138 /* If recomposing and font supports this, we're good to go */
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400139 if (will_recompose && hb_font_get_glyph (font, buffer->info[buffer->idx].codepoint, 0, &glyph)) {
140 buffer->next_glyph ();
Behdad Esfahbodc311d852011-07-23 23:43:54 -0400141 return;
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400142 }
143
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400144 decompose_current_glyph (font, buffer, will_recompose);
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400145}
146
Behdad Esfahbodc311d852011-07-23 23:43:54 -0400147static void
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400148decompose_multi_char_cluster (hb_font_t *font, hb_buffer_t *buffer,
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400149 unsigned int end)
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400150{
Behdad Esfahbod5d90a342011-07-21 15:25:01 -0400151 /* TODO Currently if there's a variation-selector we give-up, it's just too hard. */
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400152 for (unsigned int i = buffer->idx; i < end; i++)
153 if (unlikely (_hb_unicode_is_variation_selector (buffer->info[i].codepoint))) {
154 while (buffer->idx < end)
155 buffer->next_glyph ();
Behdad Esfahbodc311d852011-07-23 23:43:54 -0400156 return;
Behdad Esfahbodaf913c52011-10-17 11:39:28 -0700157 }
Behdad Esfahbodd6b9c6d2011-07-21 12:16:45 -0400158
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400159 while (buffer->idx < end)
160 decompose_current_glyph (font, buffer, FALSE);
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400161}
162
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400163static int
164compare_combining_class (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
165{
166 unsigned int a = pa->combining_class();
167 unsigned int b = pb->combining_class();
168
169 return a < b ? -1 : a == b ? 0 : +1;
170}
171
Behdad Esfahbod45412522011-07-22 11:07:05 -0400172void
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400173_hb_ot_shape_normalize (hb_font_t *font, hb_buffer_t *buffer,
174 hb_ot_shape_normalization_mode_t mode)
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400175{
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400176 bool recompose = mode != HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED;
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400177 bool has_multichar_clusters = FALSE;
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400178 unsigned int count;
Behdad Esfahbod5c6f5982011-07-21 11:31:08 -0400179
Behdad Esfahbodc311d852011-07-23 23:43:54 -0400180 /* We do a fairly straightforward yet custom normalization process in three
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400181 * separate rounds: decompose, reorder, recompose (if desired). Currently
182 * this makes two buffer swaps. We can make it faster by moving the last
183 * two rounds into the inner loop for the first round, but it's more readable
184 * this way. */
Behdad Esfahbod5d90a342011-07-21 15:25:01 -0400185
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400186
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400187 /* First round, decompose */
188
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400189 buffer->clear_output ();
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400190 count = buffer->len;
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400191 for (buffer->idx = 0; buffer->idx < count;)
Behdad Esfahbod5d90a342011-07-21 15:25:01 -0400192 {
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400193 unsigned int end;
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400194 for (end = buffer->idx + 1; end < count; end++)
195 if (buffer->info[buffer->idx].cluster != buffer->info[end].cluster)
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400196 break;
Behdad Esfahbod5d90a342011-07-21 15:25:01 -0400197
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400198 if (buffer->idx + 1 == end)
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400199 decompose_single_char_cluster (font, buffer, recompose);
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400200 else {
Behdad Esfahbod11138cc2012-04-05 17:25:19 -0400201 decompose_multi_char_cluster (font, buffer, end);
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400202 has_multichar_clusters = TRUE;
203 }
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400204 }
Behdad Esfahbod468e9cb2011-07-22 11:28:07 -0400205 buffer->swap_buffers ();
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400206
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400207
Behdad Esfahbod96831842012-04-07 14:57:21 -0400208 if (mode != HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_FULL && !has_multichar_clusters)
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400209 return; /* Done! */
210
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400211
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400212 /* Second round, reorder (inplace) */
213
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400214 count = buffer->len;
215 for (unsigned int i = 0; i < count; i++)
216 {
217 if (buffer->info[i].combining_class() == 0)
218 continue;
219
220 unsigned int end;
221 for (end = i + 1; end < count; end++)
222 if (buffer->info[end].combining_class() == 0)
223 break;
224
225 /* We are going to do a bubble-sort. Only do this if the
226 * sequence is short. Doing it on long sequences can result
227 * in an O(n^2) DoS. */
228 if (end - i > 10) {
229 i = end;
230 continue;
231 }
232
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400233 hb_bubble_sort (buffer->info + i, end - i, compare_combining_class);
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400234
235 i = end;
236 }
237
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400238
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400239 if (!recompose)
240 return;
241
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400242 /* Third round, recompose */
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400243
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400244 /* As noted in the comment earlier, we don't try to combine
245 * ccc=0 chars with their previous Starter. */
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400246
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400247 buffer->clear_output ();
248 count = buffer->len;
249 unsigned int starter = 0;
250 buffer->next_glyph ();
251 while (buffer->idx < count)
252 {
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400253 hb_codepoint_t composed, glyph;
Behdad Esfahbod96831842012-04-07 14:57:21 -0400254 if (/* If mode is NOT COMPOSED_FULL (ie. it's COMPOSED_DIACRITICS), we don't try to
255 * compose a CCC=0 character with it's preceding starter. */
256 (mode == HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_FULL ||
257 buffer->info[buffer->idx].combining_class() != 0) &&
258 /* If there's anything between the starter and this char, they should have CCC
259 * smaller than this character's. */
260 (starter == buffer->out_len - 1 ||
Behdad Esfahbodbec2ac42012-04-07 14:51:17 -0400261 buffer->out_info[buffer->out_len - 1].combining_class() < buffer->info[buffer->idx].combining_class()) &&
Behdad Esfahbod96831842012-04-07 14:57:21 -0400262 /* And compose. */
Behdad Esfahbode02d9252012-04-07 14:49:13 -0400263 hb_unicode_compose (buffer->unicode,
264 buffer->out_info[starter].codepoint,
265 buffer->info[buffer->idx].codepoint,
266 &composed) &&
Behdad Esfahbod96831842012-04-07 14:57:21 -0400267 /* And the font has glyph for the composite. */
Behdad Esfahbode02d9252012-04-07 14:49:13 -0400268 hb_font_get_glyph (font, composed, 0, &glyph))
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400269 {
Behdad Esfahbode02d9252012-04-07 14:49:13 -0400270 /* Composes. Modify starter and carry on. */
271 buffer->out_info[starter].codepoint = composed;
Behdad Esfahbod29a7e302012-04-24 16:01:30 -0400272 /* XXX update cluster */
Behdad Esfahbod683b5032012-04-14 20:47:14 -0400273 set_unicode_props (&buffer->out_info[starter], buffer->unicode);
Behdad Esfahbode02d9252012-04-07 14:49:13 -0400274
275 buffer->skip_glyph ();
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400276 continue;
277 }
278
Behdad Esfahbode02d9252012-04-07 14:49:13 -0400279 /* Blocked, or doesn't compose. */
280 buffer->next_glyph ();
Behdad Esfahbod96831842012-04-07 14:57:21 -0400281
282 if (buffer->out_info[buffer->out_len - 1].combining_class() == 0)
283 starter = buffer->out_len - 1;
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400284 }
Behdad Esfahbod5389ff42011-07-22 20:22:49 -0400285 buffer->swap_buffers ();
Behdad Esfahbod34c22f82011-07-22 17:04:20 -0400286
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400287}