19cf680c6886e2a06abc7eadfac55a29bedcc59f
[framework/uifw/harfbuzz.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-normalize-private.hh"
31
32 #include "hb-font-private.hh"
33 #include "hb-set-private.hh"
34
35
36
37 hb_tag_t common_features[] = {
38   HB_TAG('c','c','m','p'),
39   HB_TAG('l','i','g','a'),
40   HB_TAG('l','o','c','l'),
41   HB_TAG('m','a','r','k'),
42   HB_TAG('m','k','m','k'),
43   HB_TAG('r','l','i','g'),
44 };
45
46
47 hb_tag_t horizontal_features[] = {
48   HB_TAG('c','a','l','t'),
49   HB_TAG('c','l','i','g'),
50   HB_TAG('c','u','r','s'),
51   HB_TAG('k','e','r','n'),
52 };
53
54 /* Note:
55  * Technically speaking, vrt2 and vert are mutually exclusive.
56  * According to the spec, valt and vpal are also mutually exclusive.
57  * But we apply them all for now.
58  */
59 hb_tag_t vertical_features[] = {
60   HB_TAG('v','a','l','t'),
61   HB_TAG('v','e','r','t'),
62   HB_TAG('v','k','r','n'),
63   HB_TAG('v','p','a','l'),
64   HB_TAG('v','r','t','2'),
65 };
66
67
68
69 struct hb_ot_shape_planner_t
70 {
71   hb_ot_map_builder_t map;
72   hb_ot_complex_shaper_t shaper;
73
74   hb_ot_shape_planner_t (void) : map () {}
75   ~hb_ot_shape_planner_t (void) { map.finish (); }
76
77   inline void compile (hb_face_t *face,
78                        const hb_segment_properties_t *props,
79                        struct hb_ot_shape_plan_t &plan)
80   {
81     plan.shaper = shaper;
82     map.compile (face, props, plan.map);
83   }
84
85   private:
86   NO_COPY (hb_ot_shape_planner_t);
87 };
88
89 static void
90 hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
91                               const hb_segment_properties_t  *props,
92                               const hb_feature_t             *user_features,
93                               unsigned int                    num_user_features)
94 {
95   switch (props->direction) {
96     case HB_DIRECTION_LTR:
97       planner->map.add_bool_feature (HB_TAG ('l','t','r','a'));
98       planner->map.add_bool_feature (HB_TAG ('l','t','r','m'));
99       break;
100     case HB_DIRECTION_RTL:
101       planner->map.add_bool_feature (HB_TAG ('r','t','l','a'));
102       planner->map.add_bool_feature (HB_TAG ('r','t','l','m'), false);
103       break;
104     case HB_DIRECTION_TTB:
105     case HB_DIRECTION_BTT:
106     case HB_DIRECTION_INVALID:
107     default:
108       break;
109   }
110
111 #define ADD_FEATURES(array) \
112   HB_STMT_START { \
113     for (unsigned int i = 0; i < ARRAY_LENGTH (array); i++) \
114       planner->map.add_bool_feature (array[i]); \
115   } HB_STMT_END
116
117   hb_ot_shape_complex_collect_features (planner->shaper, &planner->map, props);
118
119   ADD_FEATURES (common_features);
120
121   if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
122     ADD_FEATURES (horizontal_features);
123   else
124     ADD_FEATURES (vertical_features);
125
126 #undef ADD_FEATURES
127
128   for (unsigned int i = 0; i < num_user_features; i++) {
129     const hb_feature_t *feature = &user_features[i];
130     planner->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
131   }
132 }
133
134
135 struct hb_ot_shape_context_t
136 {
137   /* Input to hb_ot_shape_execute() */
138   hb_ot_shape_plan_t *plan;
139   hb_font_t *font;
140   hb_face_t *face;
141   hb_buffer_t  *buffer;
142   const hb_feature_t *user_features;
143   unsigned int        num_user_features;
144
145   /* Transient stuff */
146   hb_direction_t target_direction;
147   hb_bool_t applied_position_complex;
148 };
149
150 static void
151 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
152 {
153   hb_mask_t global_mask = c->plan->map.get_global_mask ();
154   c->buffer->reset_masks (global_mask);
155
156   hb_ot_shape_complex_setup_masks (c->plan->shaper, &c->plan->map, c->buffer, c->font);
157
158   for (unsigned int i = 0; i < c->num_user_features; i++)
159   {
160     const hb_feature_t *feature = &c->user_features[i];
161     if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
162       unsigned int shift;
163       hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift);
164       c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
165     }
166   }
167 }
168
169
170 /* Main shaper */
171
172 /* Prepare */
173
174 static void
175 hb_set_unicode_props (hb_buffer_t *buffer)
176 {
177   unsigned int count = buffer->len;
178   for (unsigned int i = 0; i < count; i++)
179     _hb_glyph_info_set_unicode_props (&buffer->info[i], buffer->unicode);
180 }
181
182 static void
183 hb_form_clusters (hb_buffer_t *buffer)
184 {
185   unsigned int count = buffer->len;
186   for (unsigned int i = 1; i < count; i++)
187     if (FLAG (_hb_glyph_info_get_general_category (&buffer->info[i])) &
188         (FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
189          FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
190          FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))
191       buffer->info[i].cluster = buffer->info[i - 1].cluster; /* XXX do the min() here */
192 }
193
194 static void
195 hb_ensure_native_direction (hb_buffer_t *buffer)
196 {
197   hb_direction_t direction = buffer->props.direction;
198
199   /* TODO vertical:
200    * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
201    * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
202    * first. */
203   if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
204       (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
205   {
206     hb_buffer_reverse_clusters (buffer);
207     buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
208   }
209 }
210
211
212 /* Substitute */
213
214 static void
215 hb_mirror_chars (hb_ot_shape_context_t *c)
216 {
217   hb_unicode_funcs_t *unicode = c->buffer->unicode;
218
219   if (HB_DIRECTION_IS_FORWARD (c->target_direction))
220     return;
221
222   hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
223
224   unsigned int count = c->buffer->len;
225   for (unsigned int i = 0; i < count; i++) {
226     hb_codepoint_t codepoint = hb_unicode_mirroring (unicode, c->buffer->info[i].codepoint);
227     if (likely (codepoint == c->buffer->info[i].codepoint))
228       c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
229     else
230       c->buffer->info[i].codepoint = codepoint;
231   }
232 }
233
234 static void
235 hb_map_glyphs (hb_font_t    *font,
236                hb_buffer_t  *buffer)
237 {
238   hb_codepoint_t glyph;
239
240   if (unlikely (!buffer->len))
241     return;
242
243   buffer->clear_output ();
244
245   unsigned int count = buffer->len - 1;
246   for (buffer->idx = 0; buffer->idx < count;) {
247     if (unlikely (_hb_unicode_is_variation_selector (buffer->cur(+1).codepoint))) {
248       hb_font_get_glyph (font, buffer->cur().codepoint, buffer->cur(+1).codepoint, &glyph);
249       buffer->replace_glyphs (2, 1, &glyph);
250     } else {
251       hb_font_get_glyph (font, buffer->cur().codepoint, 0, &glyph);
252       buffer->replace_glyph (glyph);
253     }
254   }
255   if (likely (buffer->idx < buffer->len)) {
256     hb_font_get_glyph (font, buffer->cur().codepoint, 0, &glyph);
257     buffer->replace_glyph (glyph);
258   }
259   buffer->swap_buffers ();
260 }
261
262 static void
263 hb_substitute_default (hb_ot_shape_context_t *c)
264 {
265   hb_ot_layout_substitute_start (c->buffer);
266
267   hb_mirror_chars (c);
268
269   hb_map_glyphs (c->font, c->buffer);
270 }
271
272 static void
273 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
274 {
275   if (hb_ot_layout_has_substitution (c->face)) {
276     c->plan->map.substitute (c->face, c->buffer);
277   }
278
279   hb_ot_layout_substitute_finish (c->buffer);
280
281   return;
282 }
283
284
285 /* Position */
286
287 static void
288 hb_position_default (hb_ot_shape_context_t *c)
289 {
290   hb_ot_layout_position_start (c->buffer);
291
292   unsigned int count = c->buffer->len;
293   for (unsigned int i = 0; i < count; i++) {
294     hb_font_get_glyph_advance_for_direction (c->font, c->buffer->info[i].codepoint,
295                                              c->buffer->props.direction,
296                                              &c->buffer->pos[i].x_advance,
297                                              &c->buffer->pos[i].y_advance);
298     hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
299                                                  c->buffer->props.direction,
300                                                  &c->buffer->pos[i].x_offset,
301                                                  &c->buffer->pos[i].y_offset);
302   }
303 }
304
305 static void
306 hb_ot_position_complex (hb_ot_shape_context_t *c)
307 {
308
309   if (hb_ot_layout_has_positioning (c->face))
310   {
311     /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */
312
313     unsigned int count = c->buffer->len;
314     for (unsigned int i = 0; i < count; i++) {
315       hb_font_add_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
316                                               HB_DIRECTION_LTR,
317                                               &c->buffer->pos[i].x_offset,
318                                               &c->buffer->pos[i].y_offset);
319     }
320
321     c->plan->map.position (c->font, c->buffer);
322
323     for (unsigned int i = 0; i < count; i++) {
324       hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
325                                                    HB_DIRECTION_LTR,
326                                                    &c->buffer->pos[i].x_offset,
327                                                    &c->buffer->pos[i].y_offset);
328     }
329
330     c->applied_position_complex = true;
331   }
332
333   hb_ot_layout_position_finish (c->buffer);
334
335   return;
336 }
337
338 static void
339 hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
340 {
341   /* TODO Mark pos */
342 }
343
344 static void
345 hb_truetype_kern (hb_ot_shape_context_t *c)
346 {
347   /* TODO Check for kern=0 */
348   unsigned int count = c->buffer->len;
349   for (unsigned int i = 1; i < count; i++) {
350     hb_position_t x_kern, y_kern, kern1, kern2;
351     hb_font_get_glyph_kerning_for_direction (c->font,
352                                              c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint,
353                                              c->buffer->props.direction,
354                                              &x_kern, &y_kern);
355
356     kern1 = x_kern >> 1;
357     kern2 = x_kern - kern1;
358     c->buffer->pos[i - 1].x_advance += kern1;
359     c->buffer->pos[i].x_advance += kern2;
360     c->buffer->pos[i].x_offset += kern2;
361
362     kern1 = y_kern >> 1;
363     kern2 = y_kern - kern1;
364     c->buffer->pos[i - 1].y_advance += kern1;
365     c->buffer->pos[i].y_advance += kern2;
366     c->buffer->pos[i].y_offset += kern2;
367   }
368 }
369
370 static void
371 hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
372 {
373   hb_truetype_kern (c);
374 }
375
376 static void
377 hb_hide_zerowidth (hb_ot_shape_context_t *c)
378 {
379   /* TODO Save the space character in the font? */
380   hb_codepoint_t space;
381   if (!hb_font_get_glyph (c->font, ' ', 0, &space))
382     return; /* No point! */
383
384   unsigned int count = c->buffer->len;
385   for (unsigned int i = 0; i < count; i++)
386     if (unlikely (_hb_glyph_info_is_zero_width (&c->buffer->info[i]))) {
387       c->buffer->info[i].codepoint = space;
388       c->buffer->pos[i].x_advance = 0;
389       c->buffer->pos[i].y_advance = 0;
390     }
391 }
392
393
394 /* Do it! */
395
396 static void
397 hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
398 {
399   c->buffer->deallocate_var_all ();
400
401   /* Save the original direction, we use it later. */
402   c->target_direction = c->buffer->props.direction;
403
404   HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props0);
405   HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props1);
406
407   hb_set_unicode_props (c->buffer);
408
409   hb_form_clusters (c->buffer);
410
411   hb_ensure_native_direction (c->buffer);
412
413   _hb_ot_shape_normalize (c->font, c->buffer, hb_ot_shape_complex_normalization_preference (c->plan->shaper));
414
415   hb_ot_shape_setup_masks (c);
416
417   /* SUBSTITUTE */
418   {
419     hb_substitute_default (c);
420
421     hb_ot_substitute_complex (c);
422   }
423
424   /* POSITION */
425   {
426     hb_position_default (c);
427
428     hb_ot_position_complex (c);
429
430     hb_bool_t position_fallback = !c->applied_position_complex;
431     if (position_fallback)
432       hb_position_complex_fallback (c);
433
434     if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
435       hb_buffer_reverse (c->buffer);
436
437     if (position_fallback)
438       hb_position_complex_fallback_visual (c);
439   }
440
441   hb_hide_zerowidth (c);
442
443   HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props1);
444   HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props0);
445
446   c->buffer->props.direction = c->target_direction;
447
448   c->buffer->deallocate_var_all ();
449 }
450
451 static void
452 hb_ot_shape_plan_internal (hb_ot_shape_plan_t       *plan,
453                            hb_face_t                *face,
454                            const hb_segment_properties_t  *props,
455                            const hb_feature_t       *user_features,
456                            unsigned int              num_user_features)
457 {
458   hb_ot_shape_planner_t planner;
459
460   assert (HB_DIRECTION_IS_VALID (props->direction));
461
462   planner.shaper = hb_ot_shape_complex_categorize (props);
463
464   hb_ot_shape_collect_features (&planner, props, user_features, num_user_features);
465
466   planner.compile (face, props, *plan);
467 }
468
469 static void
470 hb_ot_shape_execute (hb_ot_shape_plan_t *plan,
471                      hb_font_t          *font,
472                      hb_buffer_t        *buffer,
473                      const hb_feature_t *user_features,
474                      unsigned int        num_user_features)
475 {
476   hb_ot_shape_context_t c = {plan, font, font->face, buffer, user_features, num_user_features};
477   hb_ot_shape_execute_internal (&c);
478 }
479
480 hb_bool_t
481 _hb_ot_shape (hb_font_t          *font,
482               hb_buffer_t        *buffer,
483               const hb_feature_t *features,
484               unsigned int        num_features)
485 {
486   hb_ot_shape_plan_t plan;
487
488   buffer->guess_properties ();
489
490   hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
491   hb_ot_shape_execute (&plan, font, buffer, features, num_features);
492
493   return true;
494 }
495
496
497 void
498 hb_ot_shape_glyphs_closure (hb_font_t          *font,
499                             hb_buffer_t        *buffer,
500                             const hb_feature_t *features,
501                             unsigned int        num_features,
502                             hb_set_t           *glyphs)
503 {
504   hb_ot_shape_plan_t plan;
505
506   buffer->guess_properties ();
507
508   hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
509
510   /* TODO: normalization? have shapers do closure()? */
511   /* TODO: Deal with mirrored chars? */
512   hb_map_glyphs (font, buffer);
513
514   /* Seed it.  It's user's responsibility to have cleard glyphs
515    * if that's what they desire. */
516   unsigned int count = buffer->len;
517   for (unsigned int i = 0; i < count; i++)
518     hb_set_add (glyphs, buffer->info[i].codepoint);
519
520   /* And find transitive closure. */
521   hb_set_t copy;
522   copy.init ();
523
524   do {
525     copy.set (glyphs);
526     plan.map.substitute_closure (font->face, glyphs);
527   } while (!copy.equal (glyphs));
528 }