6f8abb8b02f50c7487e407131622ec8cbccf3c7e
[profile/ivi/org.tizen.video-player.git] / src / hb-ot-shape-normalize.cc
1 /*
2  * Copyright © 2011  Google, Inc.
3  *
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
27 #include "hb-ot-shape-private.hh"
28 #include "hb-ot-shape-complex-private.hh"
29
30 HB_BEGIN_DECLS
31
32 /*
33  * HIGHLEVEL DESIGN:
34  *
35  * This file exports one main function: _hb_ot_shape_normalize().
36  *
37  * This function closely reflects the Unicode Normalization Algorithm,
38  * yet it's different.  The shaper an either prefer decomposed (NFD) or
39  * composed (NFC).
40  *
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.
45  *
46  * The goals are:
47  *
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.
54  *
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.
58  *
59  *   - When a font does not support a character but supports its decomposition,
60  *     well, use the decomposition.
61  *
62  *   - The Indic shaper requests decomposed output.  This will handle splitting
63  *     matra for the Indic shaper.
64  */
65
66
67 static bool
68 decompose (hb_ot_shape_context_t *c,
69            bool shortest,
70            hb_codepoint_t ab)
71 {
72   hb_codepoint_t a, b, glyph;
73
74   if (!hb_unicode_decompose (c->buffer->unicode, ab, &a, &b) ||
75       (b && !hb_font_get_glyph (c->font, b, 0, &glyph)))
76     return FALSE;
77
78   bool has_a = hb_font_get_glyph (c->font, a, 0, &glyph);
79   if (shortest && has_a) {
80     /* Output a and b */
81     c->buffer->output_glyph (a);
82     if (b)
83       c->buffer->output_glyph (b);
84     return TRUE;
85   }
86
87   if (decompose (c, shortest, a)) {
88     if (b)
89       c->buffer->output_glyph (b);
90     return TRUE;
91   }
92
93   if (has_a) {
94     c->buffer->output_glyph (a);
95     if (b)
96       c->buffer->output_glyph (b);
97     return TRUE;
98   }
99
100   return FALSE;
101 }
102
103 static bool
104 decompose_current_glyph (hb_ot_shape_context_t *c,
105                          bool shortest)
106 {
107   if (decompose (c, shortest, c->buffer->info[c->buffer->idx].codepoint)) {
108     c->buffer->skip_glyph ();
109     return TRUE;
110   } else {
111     c->buffer->next_glyph ();
112     return FALSE;
113   }
114 }
115
116 static bool
117 decompose_single_char_cluster (hb_ot_shape_context_t *c,
118                                bool will_recompose)
119 {
120   hb_codepoint_t glyph;
121
122   /* If recomposing and font supports this, we're good to go */
123   if (will_recompose && hb_font_get_glyph (c->font, c->buffer->info[c->buffer->idx].codepoint, 0, &glyph)) {
124     c->buffer->next_glyph ();
125     return FALSE;
126   }
127
128   return decompose_current_glyph (c, will_recompose);
129 }
130
131 static bool
132 decompose_multi_char_cluster (hb_ot_shape_context_t *c,
133                               unsigned int end)
134 {
135   bool changed = FALSE;
136
137   /* TODO Currently if there's a variation-selector we give-up, it's just too hard. */
138   for (unsigned int i = c->buffer->idx; i < end; i++)
139     if (unlikely (is_variation_selector (c->buffer->info[i].codepoint)))
140       return changed;
141
142   while (c->buffer->idx < end)
143     changed |= decompose_current_glyph (c, FALSE);
144
145   return changed;
146 }
147
148 void
149 _hb_ot_shape_normalize (hb_ot_shape_context_t *c)
150 {
151   hb_buffer_t *buffer = c->buffer;
152   bool recompose = !hb_ot_shape_complex_prefer_decomposed (c->plan->shaper);
153   bool changed = FALSE;
154   bool has_multichar_clusters = FALSE;
155   unsigned int count;
156
157   /* We do a farily straightforward yet custom normalization process in three
158    * separate rounds: decompose, reorder, recompose (if desired).  Currently
159    * this makes two buffer swaps.  We can make it faster by moving the last
160    * two rounds into the inner loop for the first round, but it's more readable
161    * this way. */
162
163
164   /* First round, decompose */
165
166   buffer->clear_output ();
167   count = buffer->len;
168   for (buffer->idx = 0; buffer->idx < count;)
169   {
170     unsigned int end;
171     for (end = buffer->idx + 1; end < count; end++)
172       if (buffer->info[buffer->idx].cluster != buffer->info[end].cluster)
173         break;
174
175     if (buffer->idx + 1 == end)
176       changed |= decompose_single_char_cluster (c, recompose);
177     else {
178       changed |= decompose_multi_char_cluster (c, end);
179       has_multichar_clusters = TRUE;
180     }
181   }
182   buffer->swap_buffers ();
183
184
185   /* Technically speaking, two characters with ccc=0 may combine.  But all
186    * those cases are in languages that the indic module handles (which expects
187    * decomposed), or in Hangul jamo, which again, we want decomposed anyway.
188    * So we don't bother combining across cluster boundaries.
189    *
190    * TODO: Am I right about Hangul?  If I am, we should add a Hangul module
191    * that requests decomposed. */
192
193   if (!has_multichar_clusters)
194     return; /* Done! */
195
196   if (changed)
197     _hb_set_unicode_props (c->buffer); /* BUFFER: Set general_category and combining_class in var1 */
198
199
200   /* Second round, reorder (inplace) */
201
202   count = buffer->len;
203   for (unsigned int i = 0; i < count; i++)
204   {
205     if (buffer->info[i].combining_class() == 0)
206       continue;
207
208     unsigned int end;
209     for (end = i + 1; end < count; end++)
210       if (buffer->info[end].combining_class() == 0)
211         break;
212
213     /* We are going to do a bubble-sort.  Only do this if the
214      * sequence is short.  Doing it on long sequences can result
215      * in an O(n^2) DoS. */
216     if (end - i > 10) {
217       i = end;
218       continue;
219     }
220
221     unsigned int k = end - i - 1;
222     do {
223       hb_glyph_info_t *pinfo = buffer->info + i;
224       unsigned int new_k = 0;
225
226       for (unsigned int j = 0; j < k; j++)
227         if (pinfo[j].combining_class() > pinfo[j+1].combining_class()) {
228           hb_glyph_info_t t;
229           t = pinfo[j];
230           pinfo[j] = pinfo[j + 1];
231           pinfo[j + 1] = t;
232
233           new_k = j;
234         }
235       k = new_k;
236     } while (k);
237
238     i = end;
239   }
240
241
242   if (!recompose)
243     return;
244
245   /* Third round, recompose */
246
247   /* As noted in the comment earlier, we don't try to combine
248    * ccc=0 chars with their previous Starter. */
249
250   buffer->clear_output ();
251   count = buffer->len;
252   unsigned int starter = 0;
253   buffer->next_glyph ();
254   while (buffer->idx < count)
255   {
256     if (buffer->info[buffer->idx].combining_class() == 0) {
257       starter = buffer->out_len;
258       buffer->next_glyph ();
259       continue;
260     }
261
262     hb_codepoint_t composed, glyph;
263     if ((buffer->out_info[buffer->out_len - 1].combining_class() >=
264          buffer->info[buffer->idx].combining_class()) ||
265         !hb_unicode_compose (c->buffer->unicode,
266                              buffer->out_info[starter].codepoint,
267                              buffer->info[buffer->idx].codepoint,
268                              &composed) ||
269         !hb_font_get_glyph (c->font, composed, 0, &glyph))
270     {
271       /* Blocked, or doesn't compose. */
272       buffer->next_glyph ();
273       continue;
274     }
275
276     /* Composes. Modify starter and carry on. */
277     buffer->out_info[starter].codepoint = composed;
278     hb_glyph_info_set_unicode_props (&buffer->out_info[starter], buffer->unicode);
279
280     buffer->skip_glyph ();
281   }
282   buffer->swap_buffers ();
283
284 }
285
286 HB_END_DECLS