Imported Upstream version 1.7.6
[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-unicode-private.hh"
41 #include "hb-set-private.hh"
42
43 #include "hb-ot-layout-gsubgpos-private.hh"
44 //#include "hb-aat-layout-private.hh"
45
46 static hb_tag_t common_features[] = {
47   HB_TAG('c','c','m','p'),
48   HB_TAG('l','o','c','l'),
49   HB_TAG('m','a','r','k'),
50   HB_TAG('m','k','m','k'),
51   HB_TAG('r','l','i','g'),
52 };
53
54
55 static hb_tag_t horizontal_features[] = {
56   HB_TAG('c','a','l','t'),
57   HB_TAG('c','l','i','g'),
58   HB_TAG('c','u','r','s'),
59   HB_TAG('k','e','r','n'),
60   HB_TAG('l','i','g','a'),
61   HB_TAG('r','c','l','t'),
62 };
63
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   hb_ot_map_builder_t *map = &planner->map;
73
74   map->add_global_bool_feature (HB_TAG('r','v','r','n'));
75   map->add_gsub_pause (nullptr);
76
77   switch (props->direction) {
78     case HB_DIRECTION_LTR:
79       map->add_global_bool_feature (HB_TAG ('l','t','r','a'));
80       map->add_global_bool_feature (HB_TAG ('l','t','r','m'));
81       break;
82     case HB_DIRECTION_RTL:
83       map->add_global_bool_feature (HB_TAG ('r','t','l','a'));
84       map->add_feature (HB_TAG ('r','t','l','m'), 1, F_NONE);
85       break;
86     case HB_DIRECTION_TTB:
87     case HB_DIRECTION_BTT:
88     case HB_DIRECTION_INVALID:
89     default:
90       break;
91   }
92
93   map->add_feature (HB_TAG ('f','r','a','c'), 1, F_NONE);
94   map->add_feature (HB_TAG ('n','u','m','r'), 1, F_NONE);
95   map->add_feature (HB_TAG ('d','n','o','m'), 1, F_NONE);
96
97   if (planner->shaper->collect_features)
98     planner->shaper->collect_features (planner);
99
100   for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
101     map->add_global_bool_feature (common_features[i]);
102
103   if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
104     for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
105       map->add_feature (horizontal_features[i], 1, F_GLOBAL |
106                         (horizontal_features[i] == HB_TAG('k','e','r','n') ?
107                          F_HAS_FALLBACK : F_NONE));
108   else
109   {
110     /* We really want to find a 'vert' feature if there's any in the font, no
111      * matter which script/langsys it is listed (or not) under.
112      * See various bugs referenced from:
113      * https://github.com/harfbuzz/harfbuzz/issues/63 */
114     map->add_feature (HB_TAG ('v','e','r','t'), 1, F_GLOBAL | F_GLOBAL_SEARCH);
115   }
116
117   if (planner->shaper->override_features)
118     planner->shaper->override_features (planner);
119
120   for (unsigned int i = 0; i < num_user_features; i++) {
121     const hb_feature_t *feature = &user_features[i];
122     map->add_feature (feature->tag, feature->value,
123                       (feature->start == 0 && feature->end == (unsigned int) -1) ?
124                        F_GLOBAL : F_NONE);
125   }
126 }
127
128
129 /*
130  * shaper face data
131  */
132
133 HB_SHAPER_DATA_ENSURE_DEFINE(ot, face)
134
135 hb_ot_shaper_face_data_t *
136 _hb_ot_shaper_face_data_create (hb_face_t *face)
137 {
138   return _hb_ot_layout_create (face);
139 }
140
141 void
142 _hb_ot_shaper_face_data_destroy (hb_ot_shaper_face_data_t *data)
143 {
144   _hb_ot_layout_destroy (data);
145 }
146
147
148 /*
149  * shaper font data
150  */
151
152 HB_SHAPER_DATA_ENSURE_DEFINE(ot, font)
153
154 struct hb_ot_shaper_font_data_t {};
155
156 hb_ot_shaper_font_data_t *
157 _hb_ot_shaper_font_data_create (hb_font_t *font HB_UNUSED)
158 {
159   return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
160 }
161
162 void
163 _hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data)
164 {
165 }
166
167
168 /*
169  * shaper shape_plan data
170  */
171
172 hb_ot_shaper_shape_plan_data_t *
173 _hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan,
174                                       const hb_feature_t *user_features,
175                                       unsigned int        num_user_features,
176                                       const int          *coords,
177                                       unsigned int        num_coords)
178 {
179   hb_ot_shape_plan_t *plan = (hb_ot_shape_plan_t *) calloc (1, sizeof (hb_ot_shape_plan_t));
180   if (unlikely (!plan))
181     return nullptr;
182
183   hb_ot_shape_planner_t planner (shape_plan);
184
185   planner.shaper = hb_ot_shape_complex_categorize (&planner);
186
187   hb_ot_shape_collect_features (&planner, &shape_plan->props,
188                                 user_features, num_user_features);
189
190   planner.compile (*plan, coords, num_coords);
191
192   if (plan->shaper->data_create) {
193     plan->data = plan->shaper->data_create (plan);
194     if (unlikely (!plan->data))
195       return nullptr;
196   }
197
198   return plan;
199 }
200
201 void
202 _hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *plan)
203 {
204   if (plan->shaper->data_destroy)
205     plan->shaper->data_destroy (const_cast<void *> (plan->data));
206
207   plan->finish ();
208
209   free (plan);
210 }
211
212
213 /*
214  * shaper
215  */
216
217 struct hb_ot_shape_context_t
218 {
219   hb_ot_shape_plan_t *plan;
220   hb_font_t *font;
221   hb_face_t *face;
222   hb_buffer_t  *buffer;
223   const hb_feature_t *user_features;
224   unsigned int        num_user_features;
225
226   /* Transient stuff */
227   bool fallback_positioning;
228   bool fallback_glyph_classes;
229   hb_direction_t target_direction;
230 };
231
232
233
234 /* Main shaper */
235
236
237 /* Prepare */
238
239 static void
240 hb_set_unicode_props (hb_buffer_t *buffer)
241 {
242   unsigned int count = buffer->len;
243   hb_glyph_info_t *info = buffer->info;
244   for (unsigned int i = 0; i < count; i++)
245     _hb_glyph_info_set_unicode_props (&info[i], buffer);
246 }
247
248 static void
249 hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
250 {
251   if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
252       buffer->context_len[0] ||
253       _hb_glyph_info_get_general_category (&buffer->info[0]) !=
254       HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
255     return;
256
257   if (!font->has_glyph (0x25CCu))
258     return;
259
260   hb_glyph_info_t dottedcircle = {0};
261   dottedcircle.codepoint = 0x25CCu;
262   _hb_glyph_info_set_unicode_props (&dottedcircle, buffer);
263
264   buffer->clear_output ();
265
266   buffer->idx = 0;
267   hb_glyph_info_t info = dottedcircle;
268   info.cluster = buffer->cur().cluster;
269   info.mask = buffer->cur().mask;
270   buffer->output_info (info);
271   while (buffer->idx < buffer->len && !buffer->in_error)
272     buffer->next_glyph ();
273
274   buffer->swap_buffers ();
275 }
276
277 static void
278 hb_form_clusters (hb_buffer_t *buffer)
279 {
280   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII))
281     return;
282
283   /* Loop duplicated in hb_ensure_native_direction(), and in _hb-coretext.cc */
284   unsigned int base = 0;
285   unsigned int count = buffer->len;
286   hb_glyph_info_t *info = buffer->info;
287   for (unsigned int i = 1; i < count; i++)
288   {
289     if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i])) &&
290                 !_hb_glyph_info_is_joiner (&info[i])))
291     {
292       if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
293         buffer->merge_clusters (base, i);
294       else
295         buffer->unsafe_to_break (base, i);
296       base = i;
297     }
298   }
299   if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
300     buffer->merge_clusters (base, count);
301   else
302     buffer->unsafe_to_break (base, count);
303 }
304
305 static void
306 hb_ensure_native_direction (hb_buffer_t *buffer)
307 {
308   hb_direction_t direction = buffer->props.direction;
309
310   /* TODO vertical:
311    * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
312    * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
313    * first. */
314   if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
315       (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
316   {
317     /* Same loop as hb_form_clusters().
318      * Since form_clusters() merged clusters already, we don't merge. */
319     unsigned int base = 0;
320     unsigned int count = buffer->len;
321     hb_glyph_info_t *info = buffer->info;
322     for (unsigned int i = 1; i < count; i++)
323     {
324       if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i]))))
325       {
326         if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
327           buffer->merge_clusters (base, i);
328         buffer->reverse_range (base, i);
329
330         base = i;
331       }
332     }
333     if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
334       buffer->merge_clusters (base, count);
335     buffer->reverse_range (base, count);
336
337     buffer->reverse ();
338
339     buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
340   }
341 }
342
343
344 /* Substitute */
345
346 static inline void
347 hb_ot_mirror_chars (hb_ot_shape_context_t *c)
348 {
349   if (HB_DIRECTION_IS_FORWARD (c->target_direction))
350     return;
351
352   hb_buffer_t *buffer = c->buffer;
353   hb_unicode_funcs_t *unicode = buffer->unicode;
354   hb_mask_t rtlm_mask = c->plan->rtlm_mask;
355
356   unsigned int count = buffer->len;
357   hb_glyph_info_t *info = buffer->info;
358   for (unsigned int i = 0; i < count; i++) {
359     hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
360     if (likely (codepoint == info[i].codepoint || !c->font->has_glyph (codepoint)))
361       info[i].mask |= rtlm_mask;
362     else
363       info[i].codepoint = codepoint;
364   }
365 }
366
367 static inline void
368 hb_ot_shape_setup_masks_fraction (hb_ot_shape_context_t *c)
369 {
370   if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
371       !c->plan->has_frac)
372     return;
373
374   hb_buffer_t *buffer = c->buffer;
375
376   hb_mask_t pre_mask, post_mask;
377   if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
378   {
379     pre_mask = c->plan->numr_mask | c->plan->frac_mask;
380     post_mask = c->plan->frac_mask | c->plan->dnom_mask;
381   }
382   else
383   {
384     pre_mask = c->plan->frac_mask | c->plan->dnom_mask;
385     post_mask = c->plan->numr_mask | c->plan->frac_mask;
386   }
387
388   unsigned int count = buffer->len;
389   hb_glyph_info_t *info = buffer->info;
390   for (unsigned int i = 0; i < count; i++)
391   {
392     if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
393     {
394       unsigned int start = i, end = i + 1;
395       while (start &&
396              _hb_glyph_info_get_general_category (&info[start - 1]) ==
397              HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
398         start--;
399       while (end < count &&
400              _hb_glyph_info_get_general_category (&info[end]) ==
401              HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
402         end++;
403
404       buffer->unsafe_to_break (start, end);
405
406       for (unsigned int j = start; j < i; j++)
407         info[j].mask |= pre_mask;
408       info[i].mask |= c->plan->frac_mask;
409       for (unsigned int j = i + 1; j < end; j++)
410         info[j].mask |= post_mask;
411
412       i = end - 1;
413     }
414   }
415 }
416
417 static inline void
418 hb_ot_shape_initialize_masks (hb_ot_shape_context_t *c)
419 {
420   hb_ot_map_t *map = &c->plan->map;
421   hb_buffer_t *buffer = c->buffer;
422
423   hb_mask_t global_mask = map->get_global_mask ();
424   buffer->reset_masks (global_mask);
425 }
426
427 static inline void
428 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
429 {
430   hb_ot_map_t *map = &c->plan->map;
431   hb_buffer_t *buffer = c->buffer;
432
433   hb_ot_shape_setup_masks_fraction (c);
434
435   if (c->plan->shaper->setup_masks)
436     c->plan->shaper->setup_masks (c->plan, buffer, c->font);
437
438   for (unsigned int i = 0; i < c->num_user_features; i++)
439   {
440     const hb_feature_t *feature = &c->user_features[i];
441     if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
442       unsigned int shift;
443       hb_mask_t mask = map->get_mask (feature->tag, &shift);
444       buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
445     }
446   }
447 }
448
449 static void
450 hb_ot_zero_width_default_ignorables (hb_ot_shape_context_t *c)
451 {
452   hb_buffer_t *buffer = c->buffer;
453
454   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
455       (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) ||
456       (buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES))
457     return;
458
459   unsigned int count = buffer->len;
460   hb_glyph_info_t *info = buffer->info;
461   hb_glyph_position_t *pos = buffer->pos;
462   unsigned int i = 0;
463   for (i = 0; i < count; i++)
464     if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
465       pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
466 }
467
468 static void
469 hb_ot_hide_default_ignorables (hb_ot_shape_context_t *c)
470 {
471   hb_buffer_t *buffer = c->buffer;
472
473   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
474       (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
475     return;
476
477   unsigned int count = buffer->len;
478   hb_glyph_info_t *info = buffer->info;
479   hb_glyph_position_t *pos = buffer->pos;
480   unsigned int i = 0;
481   for (i = 0; i < count; i++)
482   {
483     if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
484       break;
485   }
486
487   /* No default-ignorables found; return. */
488   if (i == count)
489     return;
490
491   hb_codepoint_t space;
492   if (!(buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES) &&
493       c->font->get_nominal_glyph (' ', &space))
494   {
495     /* Replace default-ignorables with a zero-advance space glyph. */
496     for (/*continue*/; i < count; i++)
497     {
498       if (_hb_glyph_info_is_default_ignorable (&info[i]))
499         info[i].codepoint = space;
500     }
501   }
502   else
503   {
504     /* Merge clusters and delete default-ignorables.
505      * NOTE! We can't use out-buffer as we have positioning data. */
506     unsigned int j = i;
507     for (; i < count; i++)
508     {
509       if (_hb_glyph_info_is_default_ignorable (&info[i]))
510       {
511         /* Merge clusters.
512          * Same logic as buffer->delete_glyph(), but for in-place removal. */
513
514         unsigned int cluster = info[i].cluster;
515         if (i + 1 < count && cluster == info[i + 1].cluster)
516           continue; /* Cluster survives; do nothing. */
517
518         if (j)
519         {
520           /* Merge cluster backward. */
521           if (cluster < info[j - 1].cluster)
522           {
523             unsigned int mask = info[i].mask;
524             unsigned int old_cluster = info[j - 1].cluster;
525             for (unsigned k = j; k && info[k - 1].cluster == old_cluster; k--)
526               buffer->set_cluster (info[k - 1], cluster, mask);
527           }
528           continue;
529         }
530
531         if (i + 1 < count)
532           buffer->merge_clusters (i, i + 2); /* Merge cluster forward. */
533
534         continue;
535       }
536
537       if (j != i)
538       {
539         info[j] = info[i];
540         pos[j] = pos[i];
541       }
542       j++;
543     }
544     buffer->len = j;
545   }
546 }
547
548
549 static inline void
550 hb_ot_map_glyphs_fast (hb_buffer_t  *buffer)
551 {
552   /* Normalization process sets up glyph_index(), we just copy it. */
553   unsigned int count = buffer->len;
554   hb_glyph_info_t *info = buffer->info;
555   for (unsigned int i = 0; i < count; i++)
556     info[i].codepoint = info[i].glyph_index();
557
558   buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
559 }
560
561 static inline void
562 hb_synthesize_glyph_classes (hb_ot_shape_context_t *c)
563 {
564   unsigned int count = c->buffer->len;
565   hb_glyph_info_t *info = c->buffer->info;
566   for (unsigned int i = 0; i < count; i++)
567   {
568     hb_ot_layout_glyph_props_flags_t klass;
569
570     /* Never mark default-ignorables as marks.
571      * They won't get in the way of lookups anyway,
572      * but having them as mark will cause them to be skipped
573      * over if the lookup-flag says so, but at least for the
574      * Mongolian variation selectors, looks like Uniscribe
575      * marks them as non-mark.  Some Mongolian fonts without
576      * GDEF rely on this.  Another notable character that
577      * this applies to is COMBINING GRAPHEME JOINER. */
578     klass = (_hb_glyph_info_get_general_category (&info[i]) !=
579              HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
580              _hb_glyph_info_is_default_ignorable (&info[i])) ?
581             HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
582             HB_OT_LAYOUT_GLYPH_PROPS_MARK;
583     _hb_glyph_info_set_glyph_props (&info[i], klass);
584   }
585 }
586
587 static inline void
588 hb_ot_substitute_default (hb_ot_shape_context_t *c)
589 {
590   hb_buffer_t *buffer = c->buffer;
591
592   hb_ot_mirror_chars (c);
593
594   HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
595
596   _hb_ot_shape_normalize (c->plan, buffer, c->font);
597
598   hb_ot_shape_setup_masks (c);
599
600   /* This is unfortunate to go here, but necessary... */
601   if (c->fallback_positioning)
602     _hb_ot_shape_fallback_position_recategorize_marks (c->plan, c->font, buffer);
603
604   hb_ot_map_glyphs_fast (buffer);
605
606   HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
607 }
608
609 static inline void
610 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
611 {
612   hb_buffer_t *buffer = c->buffer;
613
614   hb_ot_layout_substitute_start (c->font, buffer);
615
616   if (!hb_ot_layout_has_glyph_classes (c->face))
617     hb_synthesize_glyph_classes (c);
618
619   c->plan->substitute (c->font, buffer);
620
621   /* XXX Call morx instead. */
622   //hb_aat_layout_substitute (c->font, c->buffer);
623 }
624
625 static inline void
626 hb_ot_substitute (hb_ot_shape_context_t *c)
627 {
628   hb_ot_substitute_default (c);
629
630   _hb_buffer_allocate_gsubgpos_vars (c->buffer);
631
632   hb_ot_substitute_complex (c);
633 }
634
635 /* Position */
636
637 static inline void
638 adjust_mark_offsets (hb_glyph_position_t *pos)
639 {
640   pos->x_offset -= pos->x_advance;
641   pos->y_offset -= pos->y_advance;
642 }
643
644 static inline void
645 zero_mark_width (hb_glyph_position_t *pos)
646 {
647   pos->x_advance = 0;
648   pos->y_advance = 0;
649 }
650
651 static inline void
652 zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
653 {
654   unsigned int count = buffer->len;
655   hb_glyph_info_t *info = buffer->info;
656   for (unsigned int i = 0; i < count; i++)
657     if (_hb_glyph_info_is_mark (&info[i]))
658     {
659       if (adjust_offsets)
660         adjust_mark_offsets (&buffer->pos[i]);
661       zero_mark_width (&buffer->pos[i]);
662     }
663 }
664
665 static inline void
666 hb_ot_position_default (hb_ot_shape_context_t *c)
667 {
668   hb_direction_t direction = c->buffer->props.direction;
669   unsigned int count = c->buffer->len;
670   hb_glyph_info_t *info = c->buffer->info;
671   hb_glyph_position_t *pos = c->buffer->pos;
672
673   if (HB_DIRECTION_IS_HORIZONTAL (direction))
674   {
675     for (unsigned int i = 0; i < count; i++)
676       pos[i].x_advance = c->font->get_glyph_h_advance (info[i].codepoint);
677     /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
678     if (c->font->has_glyph_h_origin_func ())
679       for (unsigned int i = 0; i < count; i++)
680         c->font->subtract_glyph_h_origin (info[i].codepoint,
681                                           &pos[i].x_offset,
682                                           &pos[i].y_offset);
683   }
684   else
685   {
686     for (unsigned int i = 0; i < count; i++)
687     {
688       pos[i].y_advance = c->font->get_glyph_v_advance (info[i].codepoint);
689       c->font->subtract_glyph_v_origin (info[i].codepoint,
690                                         &pos[i].x_offset,
691                                         &pos[i].y_offset);
692     }
693   }
694   if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
695     _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
696 }
697
698 static inline void
699 hb_ot_position_complex (hb_ot_shape_context_t *c)
700 {
701   unsigned int count = c->buffer->len;
702   hb_glyph_info_t *info = c->buffer->info;
703   hb_glyph_position_t *pos = c->buffer->pos;
704
705   /* If the font has no GPOS, AND, no fallback positioning will
706    * happen, AND, direction is forward, then when zeroing mark
707    * widths, we shift the mark with it, such that the mark
708    * is positioned hanging over the previous glyph.  When
709    * direction is backward we don't shift and it will end up
710    * hanging over the next glyph after the final reordering.
711    * If fallback positinoing happens or GPOS is present, we don't
712    * care.
713    */
714   bool adjust_offsets_when_zeroing = c->fallback_positioning &&
715                                      !c->plan->shaper->fallback_position &&
716                                      HB_DIRECTION_IS_FORWARD (c->buffer->props.direction);
717
718   /* We change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */
719
720   /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
721   if (c->font->has_glyph_h_origin_func ())
722     for (unsigned int i = 0; i < count; i++)
723       c->font->add_glyph_h_origin (info[i].codepoint,
724                                    &pos[i].x_offset,
725                                    &pos[i].y_offset);
726
727   hb_ot_layout_position_start (c->font, c->buffer);
728
729   switch (c->plan->shaper->zero_width_marks)
730   {
731     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
732       zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
733       break;
734
735     default:
736     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
737     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
738       break;
739   }
740
741   if (likely (!c->fallback_positioning))
742     c->plan->position (c->font, c->buffer);
743
744   switch (c->plan->shaper->zero_width_marks)
745   {
746     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
747       zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
748       break;
749
750     default:
751     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
752     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
753       break;
754   }
755
756   /* Finishing off GPOS has to follow a certain order. */
757   hb_ot_layout_position_finish_advances (c->font, c->buffer);
758   hb_ot_zero_width_default_ignorables (c);
759   hb_ot_layout_position_finish_offsets (c->font, c->buffer);
760
761   /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
762   if (c->font->has_glyph_h_origin_func ())
763     for (unsigned int i = 0; i < count; i++)
764       c->font->subtract_glyph_h_origin (info[i].codepoint,
765                                         &pos[i].x_offset,
766                                         &pos[i].y_offset);
767 }
768
769 static inline void
770 hb_ot_position (hb_ot_shape_context_t *c)
771 {
772   c->buffer->clear_positions ();
773
774   hb_ot_position_default (c);
775
776   hb_ot_position_complex (c);
777
778   if (c->fallback_positioning && c->plan->shaper->fallback_position)
779     _hb_ot_shape_fallback_position (c->plan, c->font, c->buffer);
780
781   if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
782     hb_buffer_reverse (c->buffer);
783
784   /* Visual fallback goes here. */
785
786   if (c->fallback_positioning)
787     _hb_ot_shape_fallback_kern (c->plan, c->font, c->buffer);
788
789   _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
790
791   //hb_aat_layout_position (c->font, c->buffer);
792 }
793
794 static inline void
795 hb_propagate_flags (hb_buffer_t *buffer)
796 {
797   /* Propagate cluster-level glyph flags to be the same on all cluster glyphs.
798    * Simplifies using them. */
799
800   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_UNSAFE_TO_BREAK))
801     return;
802
803   hb_glyph_info_t *info = buffer->info;
804
805   foreach_cluster (buffer, start, end)
806   {
807     unsigned int mask = 0;
808     for (unsigned int i = start; i < end; i++)
809       if (info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)
810       {
811          mask = HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
812          break;
813       }
814     if (mask)
815       for (unsigned int i = start; i < end; i++)
816         info[i].mask |= mask;
817   }
818 }
819
820 /* Pull it all together! */
821
822 static void
823 hb_ot_shape_internal (hb_ot_shape_context_t *c)
824 {
825   c->buffer->deallocate_var_all ();
826   c->buffer->scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
827   if (likely (!_hb_unsigned_int_mul_overflows (c->buffer->len, HB_BUFFER_MAX_LEN_FACTOR)))
828   {
829     c->buffer->max_len = MAX (c->buffer->len * HB_BUFFER_MAX_LEN_FACTOR,
830                               (unsigned) HB_BUFFER_MAX_LEN_MIN);
831   }
832   if (likely (!_hb_unsigned_int_mul_overflows (c->buffer->len, HB_BUFFER_MAX_OPS_FACTOR)))
833   {
834     c->buffer->max_ops = MAX (c->buffer->len * HB_BUFFER_MAX_OPS_FACTOR,
835                               (unsigned) HB_BUFFER_MAX_OPS_MIN);
836   }
837
838   bool disable_otl = c->plan->shaper->disable_otl && c->plan->shaper->disable_otl (c->plan);
839   //c->fallback_substitute     = disable_otl || !hb_ot_layout_has_substitution (c->face);
840   c->fallback_positioning    = disable_otl || !hb_ot_layout_has_positioning (c->face);
841   c->fallback_glyph_classes  = disable_otl || !hb_ot_layout_has_glyph_classes (c->face);
842
843   /* Save the original direction, we use it later. */
844   c->target_direction = c->buffer->props.direction;
845
846   _hb_buffer_allocate_unicode_vars (c->buffer);
847
848   c->buffer->clear_output ();
849
850   hb_ot_shape_initialize_masks (c);
851   hb_set_unicode_props (c->buffer);
852   hb_insert_dotted_circle (c->buffer, c->font);
853
854   hb_form_clusters (c->buffer);
855
856   hb_ensure_native_direction (c->buffer);
857
858   if (c->plan->shaper->preprocess_text)
859     c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
860
861   hb_ot_substitute (c);
862   hb_ot_position (c);
863
864   hb_ot_hide_default_ignorables (c);
865
866   if (c->plan->shaper->postprocess_glyphs)
867     c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
868
869   hb_propagate_flags (c->buffer);
870
871   _hb_buffer_deallocate_unicode_vars (c->buffer);
872
873   c->buffer->props.direction = c->target_direction;
874
875   c->buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
876   c->buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
877   c->buffer->deallocate_var_all ();
878 }
879
880
881 hb_bool_t
882 _hb_ot_shape (hb_shape_plan_t    *shape_plan,
883               hb_font_t          *font,
884               hb_buffer_t        *buffer,
885               const hb_feature_t *features,
886               unsigned int        num_features)
887 {
888   hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features};
889   hb_ot_shape_internal (&c);
890
891   return true;
892 }
893
894
895 /**
896  * hb_ot_shape_plan_collect_lookups:
897  *
898  * Since: 0.9.7
899  **/
900 void
901 hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
902                                   hb_tag_t         table_tag,
903                                   hb_set_t        *lookup_indexes /* OUT */)
904 {
905   /* XXX Does the first part always succeed? */
906   HB_SHAPER_DATA_GET (shape_plan)->collect_lookups (table_tag, lookup_indexes);
907 }
908
909
910 /* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
911 static void
912 add_char (hb_font_t          *font,
913           hb_unicode_funcs_t *unicode,
914           hb_bool_t           mirror,
915           hb_codepoint_t      u,
916           hb_set_t           *glyphs)
917 {
918   hb_codepoint_t glyph;
919   if (font->get_nominal_glyph (u, &glyph))
920     glyphs->add (glyph);
921   if (mirror)
922   {
923     hb_codepoint_t m = unicode->mirroring (u);
924     if (m != u && font->get_nominal_glyph (m, &glyph))
925       glyphs->add (glyph);
926   }
927 }
928
929
930 /**
931  * hb_ot_shape_glyphs_closure:
932  *
933  * Since: 0.9.2
934  **/
935 void
936 hb_ot_shape_glyphs_closure (hb_font_t          *font,
937                             hb_buffer_t        *buffer,
938                             const hb_feature_t *features,
939                             unsigned int        num_features,
940                             hb_set_t           *glyphs)
941 {
942   hb_ot_shape_plan_t plan;
943
944   const char *shapers[] = {"ot", nullptr};
945   hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
946                                                              features, num_features, shapers);
947
948   bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
949
950   unsigned int count = buffer->len;
951   hb_glyph_info_t *info = buffer->info;
952   for (unsigned int i = 0; i < count; i++)
953     add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
954
955   hb_set_t *lookups = hb_set_create ();
956   hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, lookups);
957
958   /* And find transitive closure. */
959   hb_set_t *copy = hb_set_create ();
960   do {
961     copy->set (glyphs);
962     for (hb_codepoint_t lookup_index = HB_SET_VALUE_INVALID; hb_set_next (lookups, &lookup_index);)
963       hb_ot_layout_lookup_substitute_closure (font->face, lookup_index, glyphs);
964   } while (!copy->is_equal (glyphs));
965   hb_set_destroy (copy);
966
967   hb_set_destroy (lookups);
968
969   hb_shape_plan_destroy (shape_plan);
970 }