96461d7079e31442d6ecfe1641fd9e26e2e92103
[platform/upstream/harfbuzz.git] / src / hb-ot-shape.cc
1 /*
2  * Copyright © 2009,2010  Red Hat, Inc.
3  * Copyright © 2010,2011,2012  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 #define HB_SHAPER ot
30 #define hb_ot_shaper_face_data_t hb_ot_layout_t
31 #define hb_ot_shaper_shape_plan_data_t hb_ot_shape_plan_t
32 #include "hb-shaper-impl-private.hh"
33
34 #include "hb-ot-shape-private.hh"
35 #include "hb-ot-shape-complex-private.hh"
36 #include "hb-ot-shape-fallback-private.hh"
37 #include "hb-ot-shape-normalize-private.hh"
38
39 #include "hb-ot-layout-private.hh"
40 #include "hb-set-private.hh"
41
42
43 static hb_tag_t common_features[] = {
44   HB_TAG('c','c','m','p'),
45   HB_TAG('l','i','g','a'),
46   HB_TAG('l','o','c','l'),
47   HB_TAG('m','a','r','k'),
48   HB_TAG('m','k','m','k'),
49   HB_TAG('r','l','i','g'),
50 };
51
52
53 static hb_tag_t horizontal_features[] = {
54   HB_TAG('c','a','l','t'),
55   HB_TAG('c','l','i','g'),
56   HB_TAG('c','u','r','s'),
57   HB_TAG('k','e','r','n'),
58   HB_TAG('r','c','l','t'),
59 };
60
61 /* Note:
62  * Technically speaking, vrt2 and vert are mutually exclusive.
63  * According to the spec, valt and vpal are also mutually exclusive.
64  * But we apply them all for now.
65  */
66 static hb_tag_t vertical_features[] = {
67   HB_TAG('v','a','l','t'),
68   HB_TAG('v','e','r','t'),
69   HB_TAG('v','k','r','n'),
70   HB_TAG('v','p','a','l'),
71   HB_TAG('v','r','t','2'),
72 };
73
74
75
76 static void
77 hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
78                               const hb_segment_properties_t  *props,
79                               const hb_feature_t             *user_features,
80                               unsigned int                    num_user_features)
81 {
82   hb_ot_map_builder_t *map = &planner->map;
83
84   switch (props->direction) {
85     case HB_DIRECTION_LTR:
86       map->add_bool_feature (HB_TAG ('l','t','r','a'));
87       map->add_bool_feature (HB_TAG ('l','t','r','m'));
88       break;
89     case HB_DIRECTION_RTL:
90       map->add_bool_feature (HB_TAG ('r','t','l','a'));
91       map->add_bool_feature (HB_TAG ('r','t','l','m'), false);
92       break;
93     case HB_DIRECTION_TTB:
94     case HB_DIRECTION_BTT:
95     case HB_DIRECTION_INVALID:
96     default:
97       break;
98   }
99
100 #define ADD_FEATURES(array) \
101   HB_STMT_START { \
102     for (unsigned int i = 0; i < ARRAY_LENGTH (array); i++) \
103       map->add_bool_feature (array[i]); \
104   } HB_STMT_END
105
106   if (planner->shaper->collect_features)
107     planner->shaper->collect_features (planner);
108
109   ADD_FEATURES (common_features);
110
111   if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
112     ADD_FEATURES (horizontal_features);
113   else
114     ADD_FEATURES (vertical_features);
115
116   if (planner->shaper->override_features)
117     planner->shaper->override_features (planner);
118
119 #undef ADD_FEATURES
120
121   for (unsigned int i = 0; i < num_user_features; i++) {
122     const hb_feature_t *feature = &user_features[i];
123     map->add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
124   }
125 }
126
127
128 /*
129  * shaper face data
130  */
131
132 hb_ot_shaper_face_data_t *
133 _hb_ot_shaper_face_data_create (hb_face_t *face)
134 {
135   return _hb_ot_layout_create (face);
136 }
137
138 void
139 _hb_ot_shaper_face_data_destroy (hb_ot_shaper_face_data_t *data)
140 {
141   _hb_ot_layout_destroy (data);
142 }
143
144
145 /*
146  * shaper font data
147  */
148
149 struct hb_ot_shaper_font_data_t {};
150
151 hb_ot_shaper_font_data_t *
152 _hb_ot_shaper_font_data_create (hb_font_t *font)
153 {
154   return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
155 }
156
157 void
158 _hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data)
159 {
160 }
161
162
163 /*
164  * shaper shape_plan data
165  */
166
167 hb_ot_shaper_shape_plan_data_t *
168 _hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan,
169                                       const hb_feature_t *user_features,
170                                       unsigned int        num_user_features)
171 {
172   hb_ot_shape_plan_t *plan = (hb_ot_shape_plan_t *) calloc (1, sizeof (hb_ot_shape_plan_t));
173   if (unlikely (!plan))
174     return NULL;
175
176   hb_ot_shape_planner_t planner (shape_plan);
177
178   planner.shaper = hb_ot_shape_complex_categorize (&planner);
179
180   hb_ot_shape_collect_features (&planner, &shape_plan->props, user_features, num_user_features);
181
182   planner.compile (*plan);
183
184   if (plan->shaper->data_create) {
185     plan->data = plan->shaper->data_create (plan);
186     if (unlikely (!plan->data))
187       return NULL;
188   }
189
190   return plan;
191 }
192
193 void
194 _hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *plan)
195 {
196   if (plan->shaper->data_destroy)
197     plan->shaper->data_destroy (const_cast<void *> (plan->data));
198
199   plan->finish ();
200
201   free (plan);
202 }
203
204
205 /*
206  * shaper
207  */
208
209 struct hb_ot_shape_context_t
210 {
211   hb_ot_shape_plan_t *plan;
212   hb_font_t *font;
213   hb_face_t *face;
214   hb_buffer_t  *buffer;
215   const hb_feature_t *user_features;
216   unsigned int        num_user_features;
217
218   /* Transient stuff */
219   hb_direction_t target_direction;
220 };
221
222
223
224 /* Main shaper */
225
226
227 /* Prepare */
228
229 static void
230 hb_set_unicode_props (hb_buffer_t *buffer)
231 {
232   unsigned int count = buffer->len;
233   for (unsigned int i = 0; i < count; i++)
234     _hb_glyph_info_set_unicode_props (&buffer->info[i], buffer->unicode);
235 }
236
237 static void
238 hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
239 {
240   if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
241       _hb_glyph_info_get_general_category (&buffer->info[0]) !=
242       HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
243     return;
244
245   hb_codepoint_t dottedcircle_glyph;
246   if (!font->get_glyph (0x25CC, 0, &dottedcircle_glyph))
247     return;
248
249   hb_glyph_info_t dottedcircle;
250   dottedcircle.codepoint = 0x25CC;
251   _hb_glyph_info_set_unicode_props (&dottedcircle, buffer->unicode);
252
253   buffer->clear_output ();
254
255   buffer->idx = 0;
256   hb_glyph_info_t info = dottedcircle;
257   info.cluster = buffer->cur().cluster;
258   info.mask = buffer->cur().mask;
259   buffer->output_info (info);
260   while (buffer->idx < buffer->len)
261     buffer->next_glyph ();
262
263   buffer->swap_buffers ();
264 }
265
266 static void
267 hb_form_clusters (hb_buffer_t *buffer)
268 {
269   unsigned int count = buffer->len;
270   for (unsigned int i = 1; i < count; i++)
271     if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&buffer->info[i])))
272       buffer->merge_clusters (i - 1, i + 1);
273 }
274
275 static void
276 hb_ensure_native_direction (hb_buffer_t *buffer)
277 {
278   hb_direction_t direction = buffer->props.direction;
279
280   /* TODO vertical:
281    * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
282    * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
283    * first. */
284   if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
285       (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
286   {
287     hb_buffer_reverse_clusters (buffer);
288     buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
289   }
290 }
291
292
293 /* Substitute */
294
295 static inline void
296 hb_ot_mirror_chars (hb_ot_shape_context_t *c)
297 {
298   if (HB_DIRECTION_IS_FORWARD (c->target_direction))
299     return;
300
301   hb_unicode_funcs_t *unicode = c->buffer->unicode;
302   hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
303
304   unsigned int count = c->buffer->len;
305   for (unsigned int i = 0; i < count; i++) {
306     hb_codepoint_t codepoint = unicode->mirroring (c->buffer->info[i].codepoint);
307     if (likely (codepoint == c->buffer->info[i].codepoint))
308       c->buffer->info[i].mask |= rtlm_mask;
309     else
310       c->buffer->info[i].codepoint = codepoint;
311   }
312 }
313
314 static inline void
315 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
316 {
317   hb_ot_map_t *map = &c->plan->map;
318
319   hb_mask_t global_mask = map->get_global_mask ();
320   c->buffer->reset_masks (global_mask);
321
322   if (c->plan->shaper->setup_masks)
323     c->plan->shaper->setup_masks (c->plan, c->buffer, c->font);
324
325   for (unsigned int i = 0; i < c->num_user_features; i++)
326   {
327     const hb_feature_t *feature = &c->user_features[i];
328     if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
329       unsigned int shift;
330       hb_mask_t mask = map->get_mask (feature->tag, &shift);
331       c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
332     }
333   }
334 }
335
336 static inline void
337 hb_ot_map_glyphs_fast (hb_buffer_t  *buffer)
338 {
339   /* Normalization process sets up glyph_index(), we just copy it. */
340   unsigned int count = buffer->len;
341   for (unsigned int i = 0; i < count; i++)
342     buffer->info[i].codepoint = buffer->info[i].glyph_index();
343 }
344
345 static inline void
346 hb_synthesize_glyph_classes (hb_ot_shape_context_t *c)
347 {
348   unsigned int count = c->buffer->len;
349   for (unsigned int i = 0; i < count; i++)
350     c->buffer->info[i].glyph_props() = _hb_glyph_info_get_general_category (&c->buffer->info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ?
351                                        HB_OT_LAYOUT_GLYPH_PROPS_MARK :
352                                        HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH;
353 }
354
355 static inline void
356 hb_ot_substitute_default (hb_ot_shape_context_t *c)
357 {
358   if (c->plan->shaper->preprocess_text)
359     c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
360
361   hb_ot_mirror_chars (c);
362
363   HB_BUFFER_ALLOCATE_VAR (c->buffer, glyph_index);
364
365   _hb_ot_shape_normalize (c->plan, c->buffer, c->font);
366
367   hb_ot_shape_setup_masks (c);
368
369   /* This is unfortunate to go here, but necessary... */
370   if (!hb_ot_layout_has_positioning (c->face))
371     _hb_ot_shape_fallback_position_recategorize_marks (c->plan, c->font, c->buffer);
372
373   hb_ot_map_glyphs_fast (c->buffer);
374
375   HB_BUFFER_DEALLOCATE_VAR (c->buffer, glyph_index);
376 }
377
378 static inline void
379 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
380 {
381   hb_ot_layout_substitute_start (c->font, c->buffer);
382
383   if (!hb_ot_layout_has_glyph_classes (c->face))
384     hb_synthesize_glyph_classes (c);
385
386   c->plan->substitute (c->font, c->buffer);
387
388   hb_ot_layout_substitute_finish (c->font, c->buffer);
389
390   return;
391 }
392
393 static inline void
394 hb_ot_substitute (hb_ot_shape_context_t *c)
395 {
396   hb_ot_substitute_default (c);
397   hb_ot_substitute_complex (c);
398 }
399
400 /* Position */
401
402 static inline void
403 hb_ot_position_default (hb_ot_shape_context_t *c)
404 {
405   hb_ot_layout_position_start (c->font, c->buffer);
406
407   unsigned int count = c->buffer->len;
408   for (unsigned int i = 0; i < count; i++) {
409     c->font->get_glyph_advance_for_direction (c->buffer->info[i].codepoint,
410                                               c->buffer->props.direction,
411                                               &c->buffer->pos[i].x_advance,
412                                               &c->buffer->pos[i].y_advance);
413     c->font->subtract_glyph_origin_for_direction (c->buffer->info[i].codepoint,
414                                                   c->buffer->props.direction,
415                                                   &c->buffer->pos[i].x_offset,
416                                                   &c->buffer->pos[i].y_offset);
417   }
418 }
419
420 static inline bool
421 hb_ot_position_complex (hb_ot_shape_context_t *c)
422 {
423   bool ret = false;
424
425   if (hb_ot_layout_has_positioning (c->face))
426   {
427     /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */
428
429     unsigned int count = c->buffer->len;
430     for (unsigned int i = 0; i < count; i++) {
431       c->font->add_glyph_origin_for_direction (c->buffer->info[i].codepoint,
432                                                HB_DIRECTION_LTR,
433                                                &c->buffer->pos[i].x_offset,
434                                                &c->buffer->pos[i].y_offset);
435     }
436
437     c->plan->position (c->font, c->buffer);
438
439     for (unsigned int i = 0; i < count; i++) {
440       c->font->subtract_glyph_origin_for_direction (c->buffer->info[i].codepoint,
441                                                     HB_DIRECTION_LTR,
442                                                     &c->buffer->pos[i].x_offset,
443                                                     &c->buffer->pos[i].y_offset);
444     }
445
446     ret = true;
447   }
448
449   hb_ot_layout_position_finish (c->font, c->buffer, c->plan->shaper->zero_width_attached_marks);
450
451   return ret;
452 }
453
454 static inline void
455 hb_ot_truetype_kern (hb_ot_shape_context_t *c)
456 {
457   /* TODO Check for kern=0 */
458   unsigned int count = c->buffer->len;
459   for (unsigned int i = 1; i < count; i++) {
460     hb_position_t x_kern, y_kern, kern1, kern2;
461     c->font->get_glyph_kerning_for_direction (c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint,
462                                               c->buffer->props.direction,
463                                               &x_kern, &y_kern);
464
465     kern1 = x_kern >> 1;
466     kern2 = x_kern - kern1;
467     c->buffer->pos[i - 1].x_advance += kern1;
468     c->buffer->pos[i].x_advance += kern2;
469     c->buffer->pos[i].x_offset += kern2;
470
471     kern1 = y_kern >> 1;
472     kern2 = y_kern - kern1;
473     c->buffer->pos[i - 1].y_advance += kern1;
474     c->buffer->pos[i].y_advance += kern2;
475     c->buffer->pos[i].y_offset += kern2;
476   }
477 }
478
479 static inline void
480 hb_ot_position (hb_ot_shape_context_t *c)
481 {
482   hb_ot_position_default (c);
483
484   hb_bool_t fallback = !hb_ot_position_complex (c);
485
486   if (fallback && c->plan->shaper->fallback_position)
487     _hb_ot_shape_fallback_position (c->plan, c->font, c->buffer);
488
489   if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
490     hb_buffer_reverse (c->buffer);
491
492   /* Visual fallback goes here. */
493
494   if (fallback)
495     hb_ot_truetype_kern (c);
496 }
497
498
499 /* Post-process */
500
501 static void
502 hb_ot_hide_default_ignorables (hb_ot_shape_context_t *c)
503 {
504   if (c->buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES)
505     return;
506
507   hb_codepoint_t space = 0;
508
509   unsigned int count = c->buffer->len;
510   for (unsigned int i = 0; i < count; i++)
511     if (unlikely (!is_a_ligature (c->buffer->info[i]) &&
512                   _hb_glyph_info_is_default_ignorable (&c->buffer->info[i])))
513     {
514       if (!space) {
515         /* We assume that the space glyph is not gid0. */
516         if (unlikely (!c->font->get_glyph (' ', 0, &space)) || !space)
517         return; /* No point! */
518       }
519       c->buffer->info[i].codepoint = space;
520       c->buffer->pos[i].x_advance = 0;
521       c->buffer->pos[i].y_advance = 0;
522     }
523 }
524
525
526 /* Pull it all together! */
527
528 static void
529 hb_ot_shape_internal (hb_ot_shape_context_t *c)
530 {
531   c->buffer->deallocate_var_all ();
532
533   /* Save the original direction, we use it later. */
534   c->target_direction = c->buffer->props.direction;
535
536   HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props0);
537   HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props1);
538
539   c->buffer->clear_output ();
540
541   hb_set_unicode_props (c->buffer);
542   hb_insert_dotted_circle (c->buffer, c->font);
543   hb_form_clusters (c->buffer);
544
545   hb_ensure_native_direction (c->buffer);
546
547   hb_ot_substitute (c);
548   hb_ot_position (c);
549
550   hb_ot_hide_default_ignorables (c);
551
552   HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props1);
553   HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props0);
554
555   c->buffer->props.direction = c->target_direction;
556
557   c->buffer->deallocate_var_all ();
558 }
559
560
561 hb_bool_t
562 _hb_ot_shape (hb_shape_plan_t    *shape_plan,
563               hb_font_t          *font,
564               hb_buffer_t        *buffer,
565               const hb_feature_t *features,
566               unsigned int        num_features)
567 {
568   hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features};
569   hb_ot_shape_internal (&c);
570
571   return true;
572 }
573
574
575 void
576 hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
577                                   hb_tag_t         table_tag,
578                                   hb_set_t        *lookup_indexes /* OUT */)
579 {
580   /* XXX Does the first part always succeed? */
581   HB_SHAPER_DATA_GET (shape_plan)->collect_lookups (table_tag, lookup_indexes);
582 }
583
584
585 /* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
586 static void
587 add_char (hb_font_t          *font,
588           hb_unicode_funcs_t *unicode,
589           hb_bool_t           mirror,
590           hb_codepoint_t      u,
591           hb_set_t           *glyphs)
592 {
593   hb_codepoint_t glyph;
594   if (font->get_glyph (u, 0, &glyph))
595     glyphs->add (glyph);
596   if (mirror)
597   {
598     hb_codepoint_t m = unicode->mirroring (u);
599     if (m != u && font->get_glyph (m, 0, &glyph))
600       glyphs->add (glyph);
601   }
602 }
603
604
605 void
606 hb_ot_shape_glyphs_closure (hb_font_t          *font,
607                             hb_buffer_t        *buffer,
608                             const hb_feature_t *features,
609                             unsigned int        num_features,
610                             hb_set_t           *glyphs)
611 {
612   hb_ot_shape_plan_t plan;
613
614   buffer->guess_segment_properties ();
615
616   const char *shapers[] = {"ot", NULL};
617   hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
618                                                              features, num_features, shapers);
619
620   bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
621
622   unsigned int count = buffer->len;
623   for (unsigned int i = 0; i < count; i++)
624     add_char (font, buffer->unicode, mirror, buffer->info[i].codepoint, glyphs);
625
626   hb_set_t lookups;
627   lookups.init ();
628   hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, &lookups);
629
630   /* And find transitive closure. */
631   hb_set_t copy;
632   copy.init ();
633   do {
634     copy.set (glyphs);
635     for (hb_codepoint_t lookup_index = -1; hb_set_next (&lookups, &lookup_index);)
636       hb_ot_layout_lookup_substitute_closure (font->face, lookup_index, glyphs);
637   } while (!copy.is_equal (glyphs));
638
639   hb_shape_plan_destroy (shape_plan);
640 }