Update copyright headers
[apps/core/preloaded/video-player.git] / src / hb-ot-shape.cc
1 /*
2  * Copyright © 2009,2010  Red Hat, Inc.
3  * Copyright © 2010,2011  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 hb_tag_t early_features[] = {
38   HB_TAG('c','c','m','p'),
39   HB_TAG('l','o','c','l'),
40 };
41
42 hb_tag_t common_features[] = {
43   HB_TAG('m','a','r','k'),
44   HB_TAG('m','k','m','k'),
45   HB_TAG('r','l','i','g'),
46 };
47
48 hb_tag_t horizontal_features[] = {
49   HB_TAG('c','a','l','t'),
50   HB_TAG('c','l','i','g'),
51   HB_TAG('c','u','r','s'),
52   HB_TAG('k','e','r','n'),
53   HB_TAG('l','i','g','a'),
54 };
55
56 /* Note:
57  * Technically speaking, vrt2 and vert are mutually exclusive.
58  * According to the spec, valt and vpal are also mutually exclusive.
59  * But we apply them all for now.
60  */
61 hb_tag_t vertical_features[] = {
62   HB_TAG('v','a','l','t'),
63   HB_TAG('v','e','r','t'),
64   HB_TAG('v','k','r','n'),
65   HB_TAG('v','p','a','l'),
66   HB_TAG('v','r','t','2'),
67 };
68
69 static void
70 hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
71                               const hb_segment_properties_t  *props,
72                               const hb_feature_t             *user_features,
73                               unsigned int                    num_user_features)
74 {
75   switch (props->direction) {
76     case HB_DIRECTION_LTR:
77       planner->map.add_bool_feature (HB_TAG ('l','t','r','a'));
78       planner->map.add_bool_feature (HB_TAG ('l','t','r','m'));
79       break;
80     case HB_DIRECTION_RTL:
81       planner->map.add_bool_feature (HB_TAG ('r','t','l','a'));
82       planner->map.add_bool_feature (HB_TAG ('r','t','l','m'), false);
83       break;
84     case HB_DIRECTION_TTB:
85     case HB_DIRECTION_BTT:
86     case HB_DIRECTION_INVALID:
87     default:
88       break;
89   }
90
91 #define ADD_FEATURES(array) \
92   HB_STMT_START { \
93     for (unsigned int i = 0; i < ARRAY_LENGTH (array); i++) \
94       planner->map.add_bool_feature (array[i]); \
95   } HB_STMT_END
96
97   ADD_FEATURES (early_features);
98
99   hb_ot_shape_complex_collect_features (planner, props);
100
101   ADD_FEATURES (common_features);
102
103   if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
104     ADD_FEATURES (horizontal_features);
105   else
106     ADD_FEATURES (vertical_features);
107
108 #undef ADD_FEATURES
109
110   for (unsigned int i = 0; i < num_user_features; i++) {
111     const hb_feature_t *feature = &user_features[i];
112     planner->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
113   }
114 }
115
116
117 static void
118 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
119 {
120   hb_mask_t global_mask = c->plan->map.get_global_mask ();
121   c->buffer->reset_masks (global_mask);
122
123   hb_ot_shape_complex_setup_masks (c); /* BUFFER: Clobbers var2 */
124
125   for (unsigned int i = 0; i < c->num_user_features; i++)
126   {
127     const hb_feature_t *feature = &c->user_features[i];
128     if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
129       unsigned int shift;
130       hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift);
131       c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
132     }
133   }
134 }
135
136
137 static void
138 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
139 {
140   if (!hb_ot_layout_has_substitution (c->face))
141     return;
142
143   c->plan->map.substitute (c->face, c->buffer);
144
145   c->applied_substitute_complex = TRUE;
146   return;
147 }
148
149 static void
150 hb_ot_position_complex (hb_ot_shape_context_t *c)
151 {
152
153   if (!hb_ot_layout_has_positioning (c->face))
154     return;
155
156   unsigned int count = c->buffer->len;
157   for (unsigned int i = 0; i < count; i++) {
158     hb_font_add_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
159                                             HB_DIRECTION_LTR,
160                                             &c->buffer->pos[i].x_offset,
161                                             &c->buffer->pos[i].y_offset);
162   }
163
164   c->plan->map.position (c->font, c->buffer);
165
166   for (unsigned int i = 0; i < count; i++) {
167     hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
168                                                  HB_DIRECTION_LTR,
169                                                  &c->buffer->pos[i].x_offset,
170                                                  &c->buffer->pos[i].y_offset);
171   }
172
173   hb_ot_layout_position_finish (c->buffer);
174
175   c->applied_position_complex = TRUE;
176   return;
177 }
178
179
180 /* Main shaper */
181
182 /* Prepare */
183
184 static inline hb_bool_t
185 is_variation_selector (hb_codepoint_t unicode)
186 {
187   return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
188                    (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
189                    (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
190 }
191
192 static void
193 hb_set_unicode_props (hb_ot_shape_context_t *c)
194 {
195   hb_unicode_funcs_t *unicode = c->buffer->unicode;
196   hb_glyph_info_t *info = c->buffer->info;
197
198   unsigned int count = c->buffer->len;
199   for (unsigned int i = 1; i < count; i++) {
200     info[i].general_category() = unicode->get_general_category (info[i].codepoint);
201     info[i].combining_class() = unicode->get_combining_class (info[i].codepoint);
202   }
203 }
204
205 static void
206 hb_form_clusters (hb_ot_shape_context_t *c)
207 {
208   unsigned int count = c->buffer->len;
209   for (unsigned int i = 1; i < count; i++)
210     /* TODO: Match other mark types too? */
211     if (c->buffer->info[i].general_category() == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
212       c->buffer->info[i].cluster = c->buffer->info[i - 1].cluster;
213 }
214
215 static void
216 hb_ensure_native_direction (hb_ot_shape_context_t *c)
217 {
218   hb_direction_t direction = c->buffer->props.direction;
219
220   /* TODO vertical:
221    * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
222    * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
223    * first. */
224   if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (c->buffer->props.script)) ||
225       (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
226   {
227     hb_buffer_reverse_clusters (c->buffer);
228     c->buffer->props.direction = HB_DIRECTION_REVERSE (c->buffer->props.direction);
229   }
230 }
231
232 static void
233 hb_reset_glyph_infos (hb_ot_shape_context_t *c)
234 {
235   unsigned int count = c->buffer->len;
236   for (unsigned int i = 0; i < count; i++)
237     c->buffer->info[i].var1.u32 = c->buffer->info[i].var2.u32 = 0;
238 }
239
240
241 /* Substitute */
242
243 static void
244 hb_mirror_chars (hb_ot_shape_context_t *c)
245 {
246   hb_unicode_funcs_t *unicode = c->buffer->unicode;
247
248   if (HB_DIRECTION_IS_FORWARD (c->target_direction))
249     return;
250
251   hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
252
253   unsigned int count = c->buffer->len;
254   for (unsigned int i = 0; i < count; i++) {
255     hb_codepoint_t codepoint = unicode->get_mirroring (c->buffer->info[i].codepoint);
256     if (likely (codepoint == c->buffer->info[i].codepoint))
257       c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
258     else
259       c->buffer->info[i].codepoint = codepoint;
260   }
261 }
262
263 static void
264 hb_map_glyphs (hb_font_t    *font,
265                hb_buffer_t  *buffer)
266 {
267   if (unlikely (!buffer->len))
268     return;
269
270   hb_codepoint_t glyph;
271   buffer->clear_output ();
272   unsigned int count = buffer->len - 1;
273   for (buffer->i = 0; buffer->i < count;) {
274     if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
275       hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint, &glyph);
276       buffer->replace_glyph (glyph);
277       buffer->i++;
278     } else {
279       hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, 0, &glyph);
280       buffer->replace_glyph (glyph);
281     }
282   }
283   if (likely (buffer->i < buffer->len)) {
284     hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, 0, &glyph);
285     buffer->replace_glyph (glyph);
286   }
287   buffer->swap ();
288 }
289
290 static void
291 hb_substitute_default (hb_ot_shape_context_t *c)
292 {
293   hb_map_glyphs (c->font, c->buffer);
294 }
295
296 static void
297 hb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
298 {
299   /* TODO Arabic */
300 }
301
302
303 /* Position */
304
305 static void
306 hb_position_default (hb_ot_shape_context_t *c)
307 {
308   hb_ot_layout_position_start (c->buffer);
309
310   unsigned int count = c->buffer->len;
311   for (unsigned int i = 0; i < count; i++) {
312     hb_font_get_glyph_advance_for_direction (c->font, c->buffer->info[i].codepoint,
313                                              c->buffer->props.direction,
314                                              &c->buffer->pos[i].x_advance,
315                                              &c->buffer->pos[i].y_advance);
316     hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
317                                                  c->buffer->props.direction,
318                                                  &c->buffer->pos[i].x_offset,
319                                                  &c->buffer->pos[i].y_offset);
320   }
321 }
322
323 static void
324 hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
325 {
326   /* TODO Mark pos */
327 }
328
329 static void
330 hb_truetype_kern (hb_ot_shape_context_t *c)
331 {
332   /* TODO Check for kern=0 */
333   unsigned int count = c->buffer->len;
334   for (unsigned int i = 1; i < count; i++) {
335     hb_position_t x_kern, y_kern, kern1, kern2;
336     hb_font_get_glyph_kerning_for_direction (c->font,
337                                              c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint,
338                                              c->buffer->props.direction,
339                                              &x_kern, &y_kern);
340
341     kern1 = x_kern >> 1;
342     kern2 = x_kern - kern1;
343     c->buffer->pos[i - 1].x_advance += kern1;
344     c->buffer->pos[i].x_advance += kern2;
345     c->buffer->pos[i].x_offset += kern2;
346
347     kern1 = y_kern >> 1;
348     kern2 = y_kern - kern1;
349     c->buffer->pos[i - 1].y_advance += kern1;
350     c->buffer->pos[i].y_advance += kern2;
351     c->buffer->pos[i].y_offset += kern2;
352   }
353 }
354
355 static void
356 hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
357 {
358   hb_truetype_kern (c);
359 }
360
361
362 /* Do it! */
363
364 static void
365 hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
366 {
367   /* Save the original direction, we use it later. */
368   c->target_direction = c->buffer->props.direction;
369
370   hb_reset_glyph_infos (c); /* BUFFER: Clear buffer var1 and var2 */
371
372   hb_set_unicode_props (c); /* BUFFER: Set general_category and combining_class in var1 */
373
374   hb_ensure_native_direction (c);
375
376   hb_form_clusters (c);
377
378   hb_ot_shape_setup_masks (c); /* BUFFER: Clobbers var2 */
379
380   /* SUBSTITUTE */
381   {
382     /* Mirroring needs to see the original direction */
383     hb_mirror_chars (c);
384
385     hb_substitute_default (c);
386
387     hb_ot_substitute_complex (c);
388
389     if (!c->applied_substitute_complex)
390       hb_substitute_complex_fallback (c);
391   }
392
393   /* POSITION */
394   {
395     hb_position_default (c);
396
397     hb_ot_position_complex (c);
398
399     hb_bool_t position_fallback = !c->applied_position_complex;
400     if (position_fallback)
401       hb_position_complex_fallback (c);
402
403     if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
404       hb_buffer_reverse (c->buffer);
405
406     if (position_fallback)
407       hb_position_complex_fallback_visual (c);
408   }
409
410   c->buffer->props.direction = c->target_direction;
411 }
412
413 static void
414 hb_ot_shape_plan_internal (hb_ot_shape_plan_t       *plan,
415                            hb_face_t                *face,
416                            const hb_segment_properties_t  *props,
417                            const hb_feature_t       *user_features,
418                            unsigned int              num_user_features)
419 {
420   hb_ot_shape_planner_t planner;
421
422   planner.shaper = hb_ot_shape_complex_categorize (props);
423
424   hb_ot_shape_collect_features (&planner, props, user_features, num_user_features);
425
426   planner.compile (face, props, *plan);
427 }
428
429 static void
430 hb_ot_shape_execute (hb_ot_shape_plan_t *plan,
431                      hb_font_t          *font,
432                      hb_buffer_t        *buffer,
433                      const hb_feature_t *user_features,
434                      unsigned int        num_user_features)
435 {
436   hb_ot_shape_context_t c = {plan, font, font->face, buffer, user_features, num_user_features};
437   hb_ot_shape_execute_internal (&c);
438 }
439
440 void
441 hb_ot_shape (hb_font_t          *font,
442              hb_buffer_t        *buffer,
443              const hb_feature_t *features,
444              unsigned int        num_features)
445 {
446   hb_ot_shape_plan_t plan;
447
448   hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
449   hb_ot_shape_execute (&plan, font, buffer, features, num_features);
450 }
451
452
453 HB_END_DECLS