Hide internal symbols
[framework/uifw/harfbuzz.git] / src / hb-ot-shape.cc
1 /*
2  * Copyright © 2009,2010  Red Hat, Inc.
3  * Copyright © 2010  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Red Hat Author(s): Behdad Esfahbod
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #include "hb-ot-shape-private.hh"
30 #include "hb-ot-shape-complex-private.hh"
31
32 #include "hb-font-private.hh"
33
34 HB_BEGIN_DECLS
35
36
37 /* XXX vertical */
38 hb_tag_t default_features[] = {
39   HB_TAG('c','a','l','t'),
40   HB_TAG('c','c','m','p'),
41   HB_TAG('c','l','i','g'),
42   HB_TAG('c','u','r','s'),
43   HB_TAG('k','e','r','n'),
44   HB_TAG('l','i','g','a'),
45   HB_TAG('l','o','c','l'),
46   HB_TAG('m','a','r','k'),
47   HB_TAG('m','k','m','k'),
48   HB_TAG('r','l','i','g')
49 };
50
51 static void
52 hb_ot_shape_collect_features (hb_ot_shape_plan_t       *plan,
53                               const hb_segment_properties_t  *props,
54                               const hb_feature_t       *user_features,
55                               unsigned int              num_user_features)
56 {
57   switch (props->direction) {
58     case HB_DIRECTION_LTR:
59       plan->map.add_bool_feature (HB_TAG ('l','t','r','a'));
60       plan->map.add_bool_feature (HB_TAG ('l','t','r','m'));
61       break;
62     case HB_DIRECTION_RTL:
63       plan->map.add_bool_feature (HB_TAG ('r','t','l','a'));
64       plan->map.add_bool_feature (HB_TAG ('r','t','l','m'), false);
65       break;
66     case HB_DIRECTION_TTB:
67     case HB_DIRECTION_BTT:
68     case HB_DIRECTION_INVALID:
69     default:
70       break;
71   }
72
73   for (unsigned int i = 0; i < ARRAY_LENGTH (default_features); i++)
74     plan->map.add_bool_feature (default_features[i]);
75
76   hb_ot_shape_complex_collect_features (plan, props);
77
78   for (unsigned int i = 0; i < num_user_features; i++) {
79     const hb_feature_t *feature = &user_features[i];
80     plan->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
81   }
82 }
83
84
85 static void
86 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
87 {
88   hb_mask_t global_mask = c->plan->map.get_global_mask ();
89   c->buffer->reset_masks (global_mask);
90
91   hb_ot_shape_complex_setup_masks (c); /* BUFFER: Clobbers var2 */
92
93   for (unsigned int i = 0; i < c->num_user_features; i++)
94   {
95     const hb_feature_t *feature = &c->user_features[i];
96     if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
97       unsigned int shift;
98       hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift);
99       c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
100     }
101   }
102 }
103
104
105 static void
106 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
107 {
108   if (!hb_ot_layout_has_substitution (c->face))
109     return;
110
111   c->plan->map.substitute (c->face, c->buffer);
112
113   c->applied_substitute_complex = TRUE;
114   return;
115 }
116
117 static void
118 hb_ot_position_complex (hb_ot_shape_context_t *c)
119 {
120
121   if (!hb_ot_layout_has_positioning (c->face))
122     return;
123
124   unsigned int count = c->buffer->len;
125   for (unsigned int i = 0; i < count; i++) {
126     hb_font_add_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
127                                             HB_DIRECTION_LTR,
128                                             &c->buffer->pos[i].x_offset,
129                                             &c->buffer->pos[i].y_offset);
130   }
131
132   c->plan->map.position (c->font, c->face, c->buffer);
133
134   for (unsigned int i = 0; i < count; i++) {
135     hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
136                                                  HB_DIRECTION_LTR,
137                                                  &c->buffer->pos[i].x_offset,
138                                                  &c->buffer->pos[i].y_offset);
139   }
140
141   hb_ot_layout_position_finish (c->buffer);
142
143   c->applied_position_complex = TRUE;
144   return;
145 }
146
147
148 /* Main shaper */
149
150 /* Prepare */
151
152 static inline hb_bool_t
153 is_variation_selector (hb_codepoint_t unicode)
154 {
155   return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
156                    (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
157                    (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
158 }
159
160 static void
161 hb_set_unicode_props (hb_ot_shape_context_t *c)
162 {
163   hb_unicode_funcs_t *unicode = c->buffer->unicode;
164   hb_glyph_info_t *info = c->buffer->info;
165
166   unsigned int count = c->buffer->len;
167   for (unsigned int i = 1; i < count; i++) {
168     info[i].general_category() = unicode->get_general_category (info[i].codepoint);
169     info[i].combining_class() = unicode->get_combining_class (info[i].codepoint);
170   }
171 }
172
173 static void
174 hb_form_clusters (hb_ot_shape_context_t *c)
175 {
176   unsigned int count = c->buffer->len;
177   for (unsigned int i = 1; i < count; i++)
178     if (c->buffer->info[i].general_category() == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
179       c->buffer->info[i].cluster = c->buffer->info[i - 1].cluster;
180 }
181
182 static void
183 hb_ensure_native_direction (hb_ot_shape_context_t *c)
184 {
185   hb_direction_t direction = c->buffer->props.direction;
186
187   /* TODO vertical:
188    * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
189    * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
190    * first. */
191   if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (c->buffer->props.script)) ||
192       (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
193   {
194     hb_buffer_reverse_clusters (c->buffer);
195     c->buffer->props.direction = HB_DIRECTION_REVERSE (c->buffer->props.direction);
196   }
197 }
198
199 static void
200 hb_reset_glyph_infos (hb_ot_shape_context_t *c)
201 {
202   unsigned int count = c->buffer->len;
203   for (unsigned int i = 0; i < count; i++)
204     c->buffer->info[i].var1.u32 = c->buffer->info[i].var2.u32 = 0;
205 }
206
207
208 /* Substitute */
209
210 static void
211 hb_mirror_chars (hb_ot_shape_context_t *c)
212 {
213   hb_unicode_funcs_t *unicode = c->buffer->unicode;
214
215   if (HB_DIRECTION_IS_FORWARD (c->target_direction))
216     return;
217
218   hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
219
220   unsigned int count = c->buffer->len;
221   for (unsigned int i = 0; i < count; i++) {
222     hb_codepoint_t codepoint = unicode->get_mirroring (c->buffer->info[i].codepoint);
223     if (likely (codepoint == c->buffer->info[i].codepoint))
224       c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
225     else
226       c->buffer->info[i].codepoint = codepoint;
227   }
228 }
229
230 static void
231 hb_map_glyphs (hb_font_t    *font,
232                hb_buffer_t  *buffer)
233 {
234   if (unlikely (!buffer->len))
235     return;
236
237   hb_codepoint_t glyph;
238   buffer->clear_output ();
239   unsigned int count = buffer->len - 1;
240   for (buffer->i = 0; buffer->i < count;) {
241     if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
242       hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint, &glyph);
243       buffer->replace_glyph (glyph);
244       buffer->i++;
245     } else {
246       hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, 0, &glyph);
247       buffer->replace_glyph (glyph);
248     }
249   }
250   if (likely (buffer->i < buffer->len)) {
251     hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, 0, &glyph);
252     buffer->replace_glyph (glyph);
253   }
254   buffer->swap ();
255 }
256
257 static void
258 hb_substitute_default (hb_ot_shape_context_t *c)
259 {
260   hb_map_glyphs (c->font, c->buffer);
261 }
262
263 static void
264 hb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
265 {
266   /* TODO Arabic */
267 }
268
269
270 /* Position */
271
272 static void
273 hb_position_default (hb_ot_shape_context_t *c)
274 {
275   hb_ot_layout_position_start (c->buffer);
276
277   unsigned int count = c->buffer->len;
278   for (unsigned int i = 0; i < count; i++) {
279     hb_font_get_glyph_advance_for_direction (c->font, c->buffer->info[i].codepoint,
280                                              c->buffer->props.direction,
281                                              &c->buffer->pos[i].x_advance,
282                                              &c->buffer->pos[i].y_advance);
283     hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
284                                                  c->buffer->props.direction,
285                                                  &c->buffer->pos[i].x_offset,
286                                                  &c->buffer->pos[i].y_offset);
287   }
288 }
289
290 static void
291 hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
292 {
293   /* TODO Mark pos */
294 }
295
296 static void
297 hb_truetype_kern (hb_ot_shape_context_t *c)
298 {
299   /* TODO Check for kern=0 */
300   unsigned int count = c->buffer->len;
301   for (unsigned int i = 1; i < count; i++) {
302     hb_position_t x_kern, y_kern, kern1, kern2;
303     hb_font_get_glyph_kerning_for_direction (c->font,
304                                              c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint,
305                                              c->buffer->props.direction,
306                                              &x_kern, &y_kern);
307
308     kern1 = x_kern >> 1;
309     kern2 = x_kern - kern1;
310     c->buffer->pos[i - 1].x_advance += kern1;
311     c->buffer->pos[i].x_advance += kern2;
312     c->buffer->pos[i].x_offset += kern2;
313
314     kern1 = y_kern >> 1;
315     kern2 = y_kern - kern1;
316     c->buffer->pos[i - 1].y_advance += kern1;
317     c->buffer->pos[i].y_advance += kern2;
318     c->buffer->pos[i].y_offset += kern2;
319   }
320 }
321
322 static void
323 hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
324 {
325   hb_truetype_kern (c);
326 }
327
328
329 /* Do it! */
330
331 static void
332 hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
333 {
334   /* Save the original direction, we use it later. */
335   c->target_direction = c->buffer->props.direction;
336
337   hb_reset_glyph_infos (c); /* BUFFER: Clear buffer var1 and var2 */
338
339   hb_set_unicode_props (c); /* BUFFER: Set general_category and combining_class in var1 */
340
341   hb_ensure_native_direction (c);
342
343   hb_form_clusters (c);
344
345   hb_ot_shape_setup_masks (c); /* BUFFER: Clobbers var2 */
346
347   /* SUBSTITUTE */
348   {
349     /* Mirroring needs to see the original direction */
350     hb_mirror_chars (c);
351
352     hb_substitute_default (c);
353
354     hb_ot_substitute_complex (c);
355
356     if (!c->applied_substitute_complex)
357       hb_substitute_complex_fallback (c);
358   }
359
360   /* POSITION */
361   {
362     hb_position_default (c);
363
364     hb_ot_position_complex (c);
365
366     hb_bool_t position_fallback = !c->applied_position_complex;
367     if (position_fallback)
368       hb_position_complex_fallback (c);
369
370     if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
371       hb_buffer_reverse (c->buffer);
372
373     if (position_fallback)
374       hb_position_complex_fallback_visual (c);
375   }
376
377   c->buffer->props.direction = c->target_direction;
378 }
379
380 static void
381 hb_ot_shape_plan_internal (hb_ot_shape_plan_t       *plan,
382                            hb_face_t                *face,
383                            const hb_segment_properties_t  *props,
384                            const hb_feature_t       *user_features,
385                            unsigned int              num_user_features)
386 {
387   plan->shaper = hb_ot_shape_complex_categorize (props);
388
389   hb_ot_shape_collect_features (plan, props, user_features, num_user_features);
390
391   plan->map.compile (face, props);
392 }
393
394 static void
395 hb_ot_shape_execute (hb_ot_shape_plan_t *plan,
396                      hb_font_t          *font,
397                      hb_buffer_t        *buffer,
398                      const hb_feature_t *user_features,
399                      unsigned int        num_user_features)
400 {
401   hb_ot_shape_context_t c = {plan, font, font->face, buffer, user_features, num_user_features};
402   hb_ot_shape_execute_internal (&c);
403 }
404
405 void
406 hb_ot_shape (hb_font_t          *font,
407              hb_buffer_t        *buffer,
408              const hb_feature_t *features,
409              unsigned int        num_features)
410 {
411   hb_ot_shape_plan_t plan;
412
413   hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
414   hb_ot_shape_execute (&plan, font, buffer, features, num_features);
415 }
416
417
418 HB_END_DECLS