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