Register buffer vars in Indic shaper
[apps/home/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);
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 /* Main shaper */
133
134 /* Prepare */
135
136 void
137 _hb_set_unicode_props (hb_buffer_t *buffer)
138 {
139   unsigned int count = buffer->len;
140   for (unsigned int i = 1; i < count; i++)
141     hb_glyph_info_set_unicode_props (&buffer->info[i], buffer->unicode);
142 }
143
144 static void
145 hb_form_clusters (hb_buffer_t *buffer)
146 {
147   unsigned int count = buffer->len;
148   for (unsigned int i = 1; i < count; i++)
149     if (FLAG (buffer->info[i].general_category()) &
150         (FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
151          FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
152          FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))
153       buffer->info[i].cluster = buffer->info[i - 1].cluster;
154 }
155
156 static void
157 hb_ensure_native_direction (hb_buffer_t *buffer)
158 {
159   hb_direction_t direction = buffer->props.direction;
160
161   /* TODO vertical:
162    * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
163    * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
164    * first. */
165   if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
166       (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
167   {
168     hb_buffer_reverse_clusters (buffer);
169     buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
170   }
171 }
172
173
174 /* Substitute */
175
176 static void
177 hb_mirror_chars (hb_ot_shape_context_t *c)
178 {
179   hb_unicode_funcs_t *unicode = c->buffer->unicode;
180
181   if (HB_DIRECTION_IS_FORWARD (c->target_direction))
182     return;
183
184   hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
185
186   unsigned int count = c->buffer->len;
187   for (unsigned int i = 0; i < count; i++) {
188     hb_codepoint_t codepoint = hb_unicode_mirroring (unicode, c->buffer->info[i].codepoint);
189     if (likely (codepoint == c->buffer->info[i].codepoint))
190       c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
191     else
192       c->buffer->info[i].codepoint = codepoint;
193   }
194 }
195
196 static void
197 hb_map_glyphs (hb_font_t    *font,
198                hb_buffer_t  *buffer)
199 {
200   hb_codepoint_t glyph;
201
202   if (unlikely (!buffer->len))
203     return;
204
205   buffer->clear_output ();
206
207   unsigned int count = buffer->len - 1;
208   for (buffer->idx = 0; buffer->idx < count;) {
209     if (unlikely (is_variation_selector (buffer->info[buffer->idx + 1].codepoint))) {
210       hb_font_get_glyph (font, buffer->info[buffer->idx].codepoint, buffer->info[buffer->idx + 1].codepoint, &glyph);
211       buffer->replace_glyph (glyph);
212       buffer->skip_glyph ();
213     } else {
214       hb_font_get_glyph (font, buffer->info[buffer->idx].codepoint, 0, &glyph);
215       buffer->replace_glyph (glyph);
216     }
217   }
218   if (likely (buffer->idx < buffer->len)) {
219     hb_font_get_glyph (font, buffer->info[buffer->idx].codepoint, 0, &glyph);
220     buffer->replace_glyph (glyph);
221   }
222   buffer->swap_buffers ();
223 }
224
225 static void
226 hb_substitute_default (hb_ot_shape_context_t *c)
227 {
228   hb_ot_layout_substitute_start (c->buffer);
229
230   hb_mirror_chars (c);
231
232   hb_map_glyphs (c->font, c->buffer);
233 }
234
235 static void
236 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
237 {
238   if (hb_ot_layout_has_substitution (c->face))
239     c->plan->map.substitute (c->face, c->buffer);
240
241   hb_ot_layout_substitute_finish (c->buffer);
242
243   c->applied_substitute_complex = TRUE;
244   return;
245 }
246
247 static void
248 hb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
249 {
250   /* TODO Arabic */
251 }
252
253
254 /* Position */
255
256 static void
257 hb_position_default (hb_ot_shape_context_t *c)
258 {
259   hb_ot_layout_position_start (c->buffer);
260
261   unsigned int count = c->buffer->len;
262   for (unsigned int i = 0; i < count; i++) {
263     hb_font_get_glyph_advance_for_direction (c->font, c->buffer->info[i].codepoint,
264                                              c->buffer->props.direction,
265                                              &c->buffer->pos[i].x_advance,
266                                              &c->buffer->pos[i].y_advance);
267     hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
268                                                  c->buffer->props.direction,
269                                                  &c->buffer->pos[i].x_offset,
270                                                  &c->buffer->pos[i].y_offset);
271   }
272 }
273
274 static void
275 hb_ot_position_complex (hb_ot_shape_context_t *c)
276 {
277
278   if (hb_ot_layout_has_positioning (c->face))
279   {
280     /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */
281
282     unsigned int count = c->buffer->len;
283     for (unsigned int i = 0; i < count; i++) {
284       hb_font_add_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
285                                               HB_DIRECTION_LTR,
286                                               &c->buffer->pos[i].x_offset,
287                                               &c->buffer->pos[i].y_offset);
288     }
289
290     c->plan->map.position (c->font, c->buffer);
291
292     for (unsigned int i = 0; i < count; i++) {
293       hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
294                                                    HB_DIRECTION_LTR,
295                                                    &c->buffer->pos[i].x_offset,
296                                                    &c->buffer->pos[i].y_offset);
297     }
298   }
299
300   hb_ot_layout_position_finish (c->buffer);
301
302   c->applied_position_complex = TRUE;
303   return;
304 }
305
306 static void
307 hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
308 {
309   /* TODO Mark pos */
310 }
311
312 static void
313 hb_truetype_kern (hb_ot_shape_context_t *c)
314 {
315   /* TODO Check for kern=0 */
316   unsigned int count = c->buffer->len;
317   for (unsigned int i = 1; i < count; i++) {
318     hb_position_t x_kern, y_kern, kern1, kern2;
319     hb_font_get_glyph_kerning_for_direction (c->font,
320                                              c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint,
321                                              c->buffer->props.direction,
322                                              &x_kern, &y_kern);
323
324     kern1 = x_kern >> 1;
325     kern2 = x_kern - kern1;
326     c->buffer->pos[i - 1].x_advance += kern1;
327     c->buffer->pos[i].x_advance += kern2;
328     c->buffer->pos[i].x_offset += kern2;
329
330     kern1 = y_kern >> 1;
331     kern2 = y_kern - kern1;
332     c->buffer->pos[i - 1].y_advance += kern1;
333     c->buffer->pos[i].y_advance += kern2;
334     c->buffer->pos[i].y_offset += kern2;
335   }
336 }
337
338 static void
339 hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
340 {
341   hb_truetype_kern (c);
342 }
343
344
345 /* Do it! */
346
347 static void
348 hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
349 {
350   c->buffer->deallocate_var_all ();
351
352   /* Save the original direction, we use it later. */
353   c->target_direction = c->buffer->props.direction;
354
355   HB_BUFFER_ALLOCATE_VAR (c->buffer, general_category);
356   HB_BUFFER_ALLOCATE_VAR (c->buffer, combining_class);
357
358   _hb_set_unicode_props (c->buffer); /* BUFFER: Set general_category and combining_class */
359
360   hb_form_clusters (c->buffer);
361
362   hb_ensure_native_direction (c->buffer);
363
364   _hb_ot_shape_normalize (c);
365
366   hb_ot_shape_setup_masks (c);
367
368   /* SUBSTITUTE */
369   {
370     hb_substitute_default (c);
371
372     hb_ot_substitute_complex (c);
373
374     if (!c->applied_substitute_complex)
375       hb_substitute_complex_fallback (c);
376   }
377
378   /* POSITION */
379   {
380     hb_position_default (c);
381
382     hb_ot_position_complex (c);
383
384     hb_bool_t position_fallback = !c->applied_position_complex;
385     if (position_fallback)
386       hb_position_complex_fallback (c);
387
388     if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
389       hb_buffer_reverse (c->buffer);
390
391     if (position_fallback)
392       hb_position_complex_fallback_visual (c);
393   }
394
395   HB_BUFFER_DEALLOCATE_VAR (c->buffer, combining_class);
396   HB_BUFFER_DEALLOCATE_VAR (c->buffer, general_category);
397
398   c->buffer->props.direction = c->target_direction;
399
400   c->buffer->deallocate_var_all ();
401 }
402
403 static void
404 hb_ot_shape_plan_internal (hb_ot_shape_plan_t       *plan,
405                            hb_face_t                *face,
406                            const hb_segment_properties_t  *props,
407                            const hb_feature_t       *user_features,
408                            unsigned int              num_user_features)
409 {
410   hb_ot_shape_planner_t planner;
411
412   planner.shaper = hb_ot_shape_complex_categorize (props);
413
414   hb_ot_shape_collect_features (&planner, props, user_features, num_user_features);
415
416   planner.compile (face, props, *plan);
417 }
418
419 static void
420 hb_ot_shape_execute (hb_ot_shape_plan_t *plan,
421                      hb_font_t          *font,
422                      hb_buffer_t        *buffer,
423                      const hb_feature_t *user_features,
424                      unsigned int        num_user_features)
425 {
426   hb_ot_shape_context_t c = {plan, font, font->face, buffer, user_features, num_user_features};
427   hb_ot_shape_execute_internal (&c);
428 }
429
430 void
431 hb_ot_shape (hb_font_t          *font,
432              hb_buffer_t        *buffer,
433              const hb_feature_t *features,
434              unsigned int        num_features)
435 {
436   hb_ot_shape_plan_t plan;
437
438   hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
439   hb_ot_shape_execute (&plan, font, buffer, features, num_features);
440 }
441
442
443 HB_END_DECLS