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