1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 // This file is available under and governed by the GNU General Public
  26 // License version 2 only, as published by the Free Software Foundation.
  27 // However, the following notice accompanied the original version of this
  28 // file:
  29 //
  30 /*
  31  * Copyright © 2009,2010  Red Hat, Inc.
  32  * Copyright © 2010,2011,2012  Google, Inc.
  33  *
  34  *  This is part of HarfBuzz, a text shaping library.
  35  *
  36  * Permission is hereby granted, without written agreement and without
  37  * license or royalty fees, to use, copy, modify, and distribute this
  38  * software and its documentation for any purpose, provided that the
  39  * above copyright notice and the following two paragraphs appear in
  40  * all copies of this software.
  41  *
  42  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  43  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  44  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  45  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  46  * DAMAGE.
  47  *
  48  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  49  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  50  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  51  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  52  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  53  *
  54  * Red Hat Author(s): Behdad Esfahbod
  55  * Google Author(s): Behdad Esfahbod
  56  */
  57 
  58 #define HB_SHAPER ot
  59 #define hb_ot_shaper_face_data_t hb_ot_layout_t
  60 #define hb_ot_shaper_shape_plan_data_t hb_ot_shape_plan_t
  61 #include "hb-shaper-impl-private.hh"
  62 
  63 #include "hb-ot-shape-private.hh"
  64 #include "hb-ot-shape-complex-private.hh"
  65 #include "hb-ot-shape-fallback-private.hh"
  66 #include "hb-ot-shape-normalize-private.hh"
  67 
  68 #include "hb-ot-layout-private.hh"
  69 #include "hb-unicode-private.hh"
  70 #include "hb-set-private.hh"
  71 
  72 
  73 static hb_tag_t common_features[] = {
  74   HB_TAG('c','c','m','p'),
  75   HB_TAG('l','o','c','l'),
  76   HB_TAG('m','a','r','k'),
  77   HB_TAG('m','k','m','k'),
  78   HB_TAG('r','l','i','g'),
  79 };
  80 
  81 
  82 static hb_tag_t horizontal_features[] = {
  83   HB_TAG('c','a','l','t'),
  84   HB_TAG('c','l','i','g'),
  85   HB_TAG('c','u','r','s'),
  86   HB_TAG('k','e','r','n'),
  87   HB_TAG('l','i','g','a'),
  88   HB_TAG('r','c','l','t'),
  89 };
  90 
  91 
  92 
  93 static void
  94 hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
  95                               const hb_segment_properties_t  *props,
  96                               const hb_feature_t             *user_features,
  97                               unsigned int                    num_user_features)
  98 {
  99   hb_ot_map_builder_t *map = &planner->map;
 100 
 101   switch (props->direction) {
 102     case HB_DIRECTION_LTR:
 103       map->add_global_bool_feature (HB_TAG ('l','t','r','a'));
 104       map->add_global_bool_feature (HB_TAG ('l','t','r','m'));
 105       break;
 106     case HB_DIRECTION_RTL:
 107       map->add_global_bool_feature (HB_TAG ('r','t','l','a'));
 108       map->add_feature (HB_TAG ('r','t','l','m'), 1, F_NONE);
 109       break;
 110     case HB_DIRECTION_TTB:
 111     case HB_DIRECTION_BTT:
 112     case HB_DIRECTION_INVALID:
 113     default:
 114       break;
 115   }
 116 
 117   map->add_feature (HB_TAG ('f','r','a','c'), 1, F_NONE);
 118   map->add_feature (HB_TAG ('n','u','m','r'), 1, F_NONE);
 119   map->add_feature (HB_TAG ('d','n','o','m'), 1, F_NONE);
 120 
 121   if (planner->shaper->collect_features)
 122     planner->shaper->collect_features (planner);
 123 
 124   for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
 125     map->add_global_bool_feature (common_features[i]);
 126 
 127   if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
 128     for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
 129       map->add_feature (horizontal_features[i], 1, F_GLOBAL |
 130                         (horizontal_features[i] == HB_TAG('k','e','r','n') ?
 131                          F_HAS_FALLBACK : F_NONE));
 132   else
 133   {
 134     /* We really want to find a 'vert' feature if there's any in the font, no
 135      * matter which script/langsys it is listed (or not) under.
 136      * See various bugs referenced from:
 137      * https://github.com/behdad/harfbuzz/issues/63 */
 138     map->add_feature (HB_TAG ('v','e','r','t'), 1, F_GLOBAL | F_GLOBAL_SEARCH);
 139   }
 140 
 141   if (planner->shaper->override_features)
 142     planner->shaper->override_features (planner);
 143 
 144   for (unsigned int i = 0; i < num_user_features; i++) {
 145     const hb_feature_t *feature = &user_features[i];
 146     map->add_feature (feature->tag, feature->value,
 147                       (feature->start == 0 && feature->end == (unsigned int) -1) ?
 148                        F_GLOBAL : F_NONE);
 149   }
 150 }
 151 
 152 
 153 /*
 154  * shaper face data
 155  */
 156 
 157 hb_ot_shaper_face_data_t *
 158 _hb_ot_shaper_face_data_create (hb_face_t *face)
 159 {
 160   return _hb_ot_layout_create (face);
 161 }
 162 
 163 void
 164 _hb_ot_shaper_face_data_destroy (hb_ot_shaper_face_data_t *data)
 165 {
 166   _hb_ot_layout_destroy (data);
 167 }
 168 
 169 
 170 /*
 171  * shaper font data
 172  */
 173 
 174 struct hb_ot_shaper_font_data_t {};
 175 
 176 hb_ot_shaper_font_data_t *
 177 _hb_ot_shaper_font_data_create (hb_font_t *font)
 178 {
 179   return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
 180 }
 181 
 182 void
 183 _hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data)
 184 {
 185 }
 186 
 187 
 188 /*
 189  * shaper shape_plan data
 190  */
 191 
 192 hb_ot_shaper_shape_plan_data_t *
 193 _hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan,
 194                                       const hb_feature_t *user_features,
 195                                       unsigned int        num_user_features)
 196 {
 197   hb_ot_shape_plan_t *plan = (hb_ot_shape_plan_t *) calloc (1, sizeof (hb_ot_shape_plan_t));
 198   if (unlikely (!plan))
 199     return NULL;
 200 
 201   hb_ot_shape_planner_t planner (shape_plan);
 202 
 203   planner.shaper = hb_ot_shape_complex_categorize (&planner);
 204 
 205   hb_ot_shape_collect_features (&planner, &shape_plan->props, user_features, num_user_features);
 206 
 207   planner.compile (*plan);
 208 
 209   if (plan->shaper->data_create) {
 210     plan->data = plan->shaper->data_create (plan);
 211     if (unlikely (!plan->data))
 212       return NULL;
 213   }
 214 
 215   return plan;
 216 }
 217 
 218 void
 219 _hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *plan)
 220 {
 221   if (plan->shaper->data_destroy)
 222     plan->shaper->data_destroy (const_cast<void *> (plan->data));
 223 
 224   plan->finish ();
 225 
 226   free (plan);
 227 }
 228 
 229 
 230 /*
 231  * shaper
 232  */
 233 
 234 struct hb_ot_shape_context_t
 235 {
 236   hb_ot_shape_plan_t *plan;
 237   hb_font_t *font;
 238   hb_face_t *face;
 239   hb_buffer_t  *buffer;
 240   const hb_feature_t *user_features;
 241   unsigned int        num_user_features;
 242 
 243   /* Transient stuff */
 244   hb_direction_t target_direction;
 245 };
 246 
 247 
 248 
 249 /* Main shaper */
 250 
 251 
 252 /* Prepare */
 253 
 254 static void
 255 hb_set_unicode_props (hb_buffer_t *buffer)
 256 {
 257   unsigned int count = buffer->len;
 258   hb_glyph_info_t *info = buffer->info;
 259   for (unsigned int i = 0; i < count; i++)
 260     _hb_glyph_info_set_unicode_props (&info[i], buffer->unicode);
 261 }
 262 
 263 static void
 264 hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
 265 {
 266   if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
 267       buffer->context_len[0] ||
 268       _hb_glyph_info_get_general_category (&buffer->info[0]) !=
 269       HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
 270     return;
 271 
 272   if (!font->has_glyph (0x25CCu))
 273     return;
 274 
 275   hb_glyph_info_t dottedcircle = {0};
 276   dottedcircle.codepoint = 0x25CCu;
 277   _hb_glyph_info_set_unicode_props (&dottedcircle, buffer->unicode);
 278 
 279   buffer->clear_output ();
 280 
 281   buffer->idx = 0;
 282   hb_glyph_info_t info = dottedcircle;
 283   info.cluster = buffer->cur().cluster;
 284   info.mask = buffer->cur().mask;
 285   buffer->output_info (info);
 286   while (buffer->idx < buffer->len)
 287     buffer->next_glyph ();
 288 
 289   buffer->swap_buffers ();
 290 }
 291 
 292 static void
 293 hb_form_clusters (hb_buffer_t *buffer)
 294 {
 295   if (buffer->cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
 296     return;
 297 
 298   /* Loop duplicated in hb_ensure_native_direction(). */
 299   unsigned int base = 0;
 300   unsigned int count = buffer->len;
 301   hb_glyph_info_t *info = buffer->info;
 302   for (unsigned int i = 1; i < count; i++)
 303   {
 304     if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i]))))
 305     {
 306       buffer->merge_clusters (base, i);
 307       base = i;
 308     }
 309   }
 310   buffer->merge_clusters (base, count);
 311 }
 312 
 313 static void
 314 hb_ensure_native_direction (hb_buffer_t *buffer)
 315 {
 316   hb_direction_t direction = buffer->props.direction;
 317 
 318   /* TODO vertical:
 319    * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
 320    * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
 321    * first. */
 322   if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
 323       (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
 324   {
 325     /* Same loop as hb_form_clusters().
 326      * Since form_clusters() merged clusters already, we don't merge. */
 327     unsigned int base = 0;
 328     unsigned int count = buffer->len;
 329     hb_glyph_info_t *info = buffer->info;
 330     for (unsigned int i = 1; i < count; i++)
 331     {
 332       if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i]))))
 333       {
 334         if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
 335           buffer->merge_clusters (base, i);
 336         buffer->reverse_range (base, i);
 337 
 338         base = i;
 339       }
 340     }
 341     if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
 342       buffer->merge_clusters (base, count);
 343     buffer->reverse_range (base, count);
 344 
 345     buffer->reverse ();
 346 
 347     buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
 348   }
 349 }
 350 
 351 
 352 /* Substitute */
 353 
 354 static inline void
 355 hb_ot_mirror_chars (hb_ot_shape_context_t *c)
 356 {
 357   if (HB_DIRECTION_IS_FORWARD (c->target_direction))
 358     return;
 359 
 360   hb_buffer_t *buffer = c->buffer;
 361   hb_unicode_funcs_t *unicode = buffer->unicode;
 362   hb_mask_t rtlm_mask = c->plan->rtlm_mask;
 363 
 364   unsigned int count = buffer->len;
 365   hb_glyph_info_t *info = buffer->info;
 366   for (unsigned int i = 0; i < count; i++) {
 367     hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
 368     if (likely (codepoint == info[i].codepoint || !c->font->has_glyph (codepoint)))
 369       info[i].mask |= rtlm_mask;
 370     else
 371       info[i].codepoint = codepoint;
 372   }
 373 }
 374 
 375 static inline void
 376 hb_ot_shape_setup_masks_fraction (hb_ot_shape_context_t *c)
 377 {
 378   if (!c->plan->has_frac)
 379     return;
 380 
 381   hb_buffer_t *buffer = c->buffer;
 382 
 383   /* TODO look in pre/post context text also. */
 384   unsigned int count = buffer->len;
 385   hb_glyph_info_t *info = buffer->info;
 386   for (unsigned int i = 0; i < count; i++)
 387   {
 388     if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
 389     {
 390       unsigned int start = i, end = i + 1;
 391       while (start &&
 392              _hb_glyph_info_get_general_category (&info[start - 1]) ==
 393              HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
 394         start--;
 395       while (end < count &&
 396              _hb_glyph_info_get_general_category (&info[end]) ==
 397              HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
 398         end++;
 399 
 400       for (unsigned int j = start; j < i; j++)
 401         info[j].mask |= c->plan->numr_mask | c->plan->frac_mask;
 402       info[i].mask |= c->plan->frac_mask;
 403       for (unsigned int j = i + 1; j < end; j++)
 404         info[j].mask |= c->plan->frac_mask | c->plan->dnom_mask;
 405 
 406       i = end - 1;
 407     }
 408   }
 409 }
 410 
 411 static inline void
 412 hb_ot_shape_initialize_masks (hb_ot_shape_context_t *c)
 413 {
 414   hb_ot_map_t *map = &c->plan->map;
 415   hb_buffer_t *buffer = c->buffer;
 416 
 417   hb_mask_t global_mask = map->get_global_mask ();
 418   buffer->reset_masks (global_mask);
 419 }
 420 
 421 static inline void
 422 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
 423 {
 424   hb_ot_map_t *map = &c->plan->map;
 425   hb_buffer_t *buffer = c->buffer;
 426 
 427   hb_ot_shape_setup_masks_fraction (c);
 428 
 429   if (c->plan->shaper->setup_masks)
 430     c->plan->shaper->setup_masks (c->plan, buffer, c->font);
 431 
 432   for (unsigned int i = 0; i < c->num_user_features; i++)
 433   {
 434     const hb_feature_t *feature = &c->user_features[i];
 435     if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
 436       unsigned int shift;
 437       hb_mask_t mask = map->get_mask (feature->tag, &shift);
 438       buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
 439     }
 440   }
 441 }
 442 
 443 static void
 444 hb_ot_zero_width_default_ignorables (hb_ot_shape_context_t *c)
 445 {
 446   hb_buffer_t *buffer = c->buffer;
 447 
 448   if (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES)
 449     return;
 450 
 451   unsigned int count = buffer->len;
 452   hb_glyph_info_t *info = buffer->info;
 453   hb_glyph_position_t *pos = buffer->pos;
 454   unsigned int i = 0;
 455   for (i = 0; i < count; i++)
 456     if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
 457       pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
 458 }
 459 
 460 static void
 461 hb_ot_hide_default_ignorables (hb_ot_shape_context_t *c)
 462 {
 463   hb_buffer_t *buffer = c->buffer;
 464 
 465   if (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES)
 466     return;
 467 
 468   unsigned int count = buffer->len;
 469   hb_glyph_info_t *info = buffer->info;
 470   hb_glyph_position_t *pos = buffer->pos;
 471   unsigned int i = 0;
 472   for (i = 0; i < count; i++)
 473   {
 474     if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
 475       break;
 476   }
 477 
 478   /* No default-ignorables found; return. */
 479   if (i == count)
 480     return;
 481 
 482   hb_codepoint_t space;
 483   if (c->font->get_glyph (' ', 0, &space))
 484   {
 485     /* Replace default-ignorables with a zero-advance space glyph. */
 486     for (/*continue*/; i < count; i++)
 487     {
 488       if (_hb_glyph_info_is_default_ignorable (&info[i]))
 489         info[i].codepoint = space;
 490     }
 491   }
 492   else
 493   {
 494     /* Merge clusters and delete default-ignorables.
 495      * NOTE! We can't use out-buffer as we have positioning data. */
 496     unsigned int j = i;
 497     for (; i < count; i++)
 498     {
 499       if (_hb_glyph_info_is_default_ignorable (&info[i]))
 500       {
 501         /* Merge clusters.
 502          * Same logic as buffer->delete_glyph(), but for in-place removal. */
 503 
 504         unsigned int cluster = info[i].cluster;
 505         if (i + 1 < count && cluster == info[i + 1].cluster)
 506           continue; /* Cluster survives; do nothing. */
 507 
 508         if (j)
 509         {
 510           /* Merge cluster backward. */
 511           if (cluster < info[j - 1].cluster)
 512           {
 513             unsigned int old_cluster = info[j - 1].cluster;
 514             for (unsigned k = j; k && info[k - 1].cluster == old_cluster; k--)
 515               info[k - 1].cluster = cluster;
 516           }
 517           continue;
 518         }
 519 
 520         if (i + 1 < count)
 521           buffer->merge_clusters (i, i + 2); /* Merge cluster forward. */
 522 
 523         continue;
 524       }
 525 
 526       if (j != i)
 527       {
 528         info[j] = info[i];
 529         pos[j] = pos[i];
 530       }
 531       j++;
 532     }
 533     buffer->len = j;
 534   }
 535 }
 536 
 537 
 538 static inline void
 539 hb_ot_map_glyphs_fast (hb_buffer_t  *buffer)
 540 {
 541   /* Normalization process sets up glyph_index(), we just copy it. */
 542   unsigned int count = buffer->len;
 543   hb_glyph_info_t *info = buffer->info;
 544   for (unsigned int i = 0; i < count; i++)
 545     info[i].codepoint = info[i].glyph_index();
 546 
 547   buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
 548 }
 549 
 550 static inline void
 551 hb_synthesize_glyph_classes (hb_ot_shape_context_t *c)
 552 {
 553   unsigned int count = c->buffer->len;
 554   hb_glyph_info_t *info = c->buffer->info;
 555   for (unsigned int i = 0; i < count; i++)
 556   {
 557     hb_ot_layout_glyph_class_mask_t klass;
 558 
 559     /* Never mark default-ignorables as marks.
 560      * They won't get in the way of lookups anyway,
 561      * but having them as mark will cause them to be skipped
 562      * over if the lookup-flag says so, but at least for the
 563      * Mongolian variation selectors, looks like Uniscribe
 564      * marks them as non-mark.  Some Mongolian fonts without
 565      * GDEF rely on this.  Another notable character that
 566      * this applies to is COMBINING GRAPHEME JOINER. */
 567     klass = (_hb_glyph_info_get_general_category (&info[i]) !=
 568              HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
 569              _hb_glyph_info_is_default_ignorable (&info[i])) ?
 570             HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
 571             HB_OT_LAYOUT_GLYPH_PROPS_MARK;
 572     _hb_glyph_info_set_glyph_props (&info[i], klass);
 573   }
 574 }
 575 
 576 static inline void
 577 hb_ot_substitute_default (hb_ot_shape_context_t *c)
 578 {
 579   hb_buffer_t *buffer = c->buffer;
 580 
 581   if (c->plan->shaper->preprocess_text)
 582     c->plan->shaper->preprocess_text (c->plan, buffer, c->font);
 583 
 584   hb_ot_shape_initialize_masks (c);
 585 
 586   hb_ot_mirror_chars (c);
 587 
 588   HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
 589 
 590   _hb_ot_shape_normalize (c->plan, buffer, c->font);
 591 
 592   hb_ot_shape_setup_masks (c);
 593 
 594   /* This is unfortunate to go here, but necessary... */
 595   if (!hb_ot_layout_has_positioning (c->face))
 596     _hb_ot_shape_fallback_position_recategorize_marks (c->plan, c->font, buffer);
 597 
 598   hb_ot_map_glyphs_fast (buffer);
 599 
 600   HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
 601 }
 602 
 603 static inline void
 604 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
 605 {
 606   hb_buffer_t *buffer = c->buffer;
 607 
 608   _hb_buffer_allocate_gsubgpos_vars (buffer);
 609   hb_ot_layout_substitute_start (c->font, buffer);
 610 
 611   if (!hb_ot_layout_has_glyph_classes (c->face))
 612     hb_synthesize_glyph_classes (c);
 613 
 614   c->plan->substitute (c->font, buffer);
 615 
 616   hb_ot_layout_substitute_finish (c->font, buffer);
 617 
 618   return;
 619 }
 620 
 621 static inline void
 622 hb_ot_substitute (hb_ot_shape_context_t *c)
 623 {
 624   hb_ot_substitute_default (c);
 625   hb_ot_substitute_complex (c);
 626 }
 627 
 628 /* Position */
 629 
 630 static inline void
 631 adjust_mark_offsets (hb_glyph_position_t *pos)
 632 {
 633   pos->x_offset -= pos->x_advance;
 634   pos->y_offset -= pos->y_advance;
 635 }
 636 
 637 static inline void
 638 zero_mark_width (hb_glyph_position_t *pos)
 639 {
 640   pos->x_advance = 0;
 641   pos->y_advance = 0;
 642 }
 643 
 644 static inline void
 645 zero_mark_widths_by_unicode (hb_buffer_t *buffer, bool adjust_offsets)
 646 {
 647   unsigned int count = buffer->len;
 648   hb_glyph_info_t *info = buffer->info;
 649   for (unsigned int i = 0; i < count; i++)
 650     if (_hb_glyph_info_get_general_category (&info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
 651     {
 652       if (adjust_offsets)
 653         adjust_mark_offsets (&buffer->pos[i]);
 654       zero_mark_width (&buffer->pos[i]);
 655     }
 656 }
 657 
 658 static inline void
 659 zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
 660 {
 661   unsigned int count = buffer->len;
 662   hb_glyph_info_t *info = buffer->info;
 663   for (unsigned int i = 0; i < count; i++)
 664     if (_hb_glyph_info_is_mark (&info[i]))
 665     {
 666       if (adjust_offsets)
 667         adjust_mark_offsets (&buffer->pos[i]);
 668       zero_mark_width (&buffer->pos[i]);
 669     }
 670 }
 671 
 672 static inline void
 673 hb_ot_position_default (hb_ot_shape_context_t *c)
 674 {
 675   hb_direction_t direction = c->buffer->props.direction;
 676   unsigned int count = c->buffer->len;
 677   hb_glyph_info_t *info = c->buffer->info;
 678   hb_glyph_position_t *pos = c->buffer->pos;
 679   for (unsigned int i = 0; i < count; i++)
 680   {
 681     c->font->get_glyph_advance_for_direction (info[i].codepoint,
 682                                               direction,
 683                                               &pos[i].x_advance,
 684                                               &pos[i].y_advance);
 685     c->font->subtract_glyph_origin_for_direction (info[i].codepoint,
 686                                                   direction,
 687                                                   &pos[i].x_offset,
 688                                                   &pos[i].y_offset);
 689 
 690   }
 691 }
 692 
 693 static inline bool
 694 hb_ot_position_complex (hb_ot_shape_context_t *c)
 695 {
 696   bool ret = false;
 697   unsigned int count = c->buffer->len;
 698   bool has_positioning = hb_ot_layout_has_positioning (c->face);
 699   /* If the font has no GPOS, AND, no fallback positioning will
 700    * happen, AND, direction is forward, then when zeroing mark
 701    * widths, we shift the mark with it, such that the mark
 702    * is positioned hanging over the previous glyph.  When
 703    * direction is backward we don't shift and it will end up
 704    * hanging over the next glyph after the final reordering.
 705    * If fallback positinoing happens or GPOS is present, we don't
 706    * care.
 707    */
 708   bool adjust_offsets_when_zeroing = !(has_positioning || c->plan->shaper->fallback_position ||
 709                                        HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction));
 710 
 711   switch (c->plan->shaper->zero_width_marks)
 712   {
 713     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
 714       zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
 715       break;
 716 
 717     /* Not currently used for any shaper:
 718     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_EARLY:
 719       zero_mark_widths_by_unicode (c->buffer, adjust_offsets_when_zeroing);
 720       break;
 721     */
 722 
 723     default:
 724     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
 725     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_LATE:
 726     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
 727       break;
 728   }
 729 
 730   if (has_positioning)
 731   {
 732     hb_glyph_info_t *info = c->buffer->info;
 733     hb_glyph_position_t *pos = c->buffer->pos;
 734 
 735     /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */
 736 
 737     for (unsigned int i = 0; i < count; i++) {
 738       c->font->add_glyph_origin_for_direction (info[i].codepoint,
 739                                                HB_DIRECTION_LTR,
 740                                                &pos[i].x_offset,
 741                                                &pos[i].y_offset);
 742     }
 743 
 744     c->plan->position (c->font, c->buffer);
 745 
 746     for (unsigned int i = 0; i < count; i++) {
 747       c->font->subtract_glyph_origin_for_direction (info[i].codepoint,
 748                                                     HB_DIRECTION_LTR,
 749                                                     &pos[i].x_offset,
 750                                                     &pos[i].y_offset);
 751     }
 752 
 753     ret = true;
 754   }
 755 
 756   switch (c->plan->shaper->zero_width_marks)
 757   {
 758     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_LATE:
 759       zero_mark_widths_by_unicode (c->buffer, adjust_offsets_when_zeroing);
 760       break;
 761 
 762     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
 763       zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
 764       break;
 765 
 766     default:
 767     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
 768     //case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_EARLY:
 769     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
 770       break;
 771   }
 772 
 773   return ret;
 774 }
 775 
 776 static inline void
 777 hb_ot_position (hb_ot_shape_context_t *c)
 778 {
 779   hb_ot_layout_position_start (c->font, c->buffer);
 780 
 781   hb_ot_position_default (c);
 782 
 783   hb_bool_t fallback = !hb_ot_position_complex (c);
 784 
 785   hb_ot_zero_width_default_ignorables (c);
 786 
 787   hb_ot_layout_position_finish (c->font, c->buffer);
 788 
 789   if (fallback && c->plan->shaper->fallback_position)
 790     _hb_ot_shape_fallback_position (c->plan, c->font, c->buffer);
 791 
 792   if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
 793     hb_buffer_reverse (c->buffer);
 794 
 795   /* Visual fallback goes here. */
 796 
 797   if (fallback)
 798     _hb_ot_shape_fallback_kern (c->plan, c->font, c->buffer);
 799 
 800   _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
 801 }
 802 
 803 
 804 /* Pull it all together! */
 805 
 806 static void
 807 hb_ot_shape_internal (hb_ot_shape_context_t *c)
 808 {
 809   c->buffer->deallocate_var_all ();
 810 
 811   /* Save the original direction, we use it later. */
 812   c->target_direction = c->buffer->props.direction;
 813 
 814   _hb_buffer_allocate_unicode_vars (c->buffer);
 815 
 816   c->buffer->clear_output ();
 817 
 818   hb_set_unicode_props (c->buffer);
 819   hb_insert_dotted_circle (c->buffer, c->font);
 820   hb_form_clusters (c->buffer);
 821 
 822   hb_ensure_native_direction (c->buffer);
 823 
 824   hb_ot_substitute (c);
 825   hb_ot_position (c);
 826 
 827   hb_ot_hide_default_ignorables (c);
 828 
 829   _hb_buffer_deallocate_unicode_vars (c->buffer);
 830 
 831   c->buffer->props.direction = c->target_direction;
 832 
 833   c->buffer->deallocate_var_all ();
 834 }
 835 
 836 
 837 hb_bool_t
 838 _hb_ot_shape (hb_shape_plan_t    *shape_plan,
 839               hb_font_t          *font,
 840               hb_buffer_t        *buffer,
 841               const hb_feature_t *features,
 842               unsigned int        num_features)
 843 {
 844   hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features};
 845   hb_ot_shape_internal (&c);
 846 
 847   return true;
 848 }
 849 
 850 
 851 /**
 852  * Since: 0.9.7
 853  **/
 854 void
 855 hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
 856                                   hb_tag_t         table_tag,
 857                                   hb_set_t        *lookup_indexes /* OUT */)
 858 {
 859   /* XXX Does the first part always succeed? */
 860   HB_SHAPER_DATA_GET (shape_plan)->collect_lookups (table_tag, lookup_indexes);
 861 }
 862 
 863 
 864 /* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
 865 static void
 866 add_char (hb_font_t          *font,
 867           hb_unicode_funcs_t *unicode,
 868           hb_bool_t           mirror,
 869           hb_codepoint_t      u,
 870           hb_set_t           *glyphs)
 871 {
 872   hb_codepoint_t glyph;
 873   if (font->get_glyph (u, 0, &glyph))
 874     glyphs->add (glyph);
 875   if (mirror)
 876   {
 877     hb_codepoint_t m = unicode->mirroring (u);
 878     if (m != u && font->get_glyph (m, 0, &glyph))
 879       glyphs->add (glyph);
 880   }
 881 }
 882 
 883 
 884 /**
 885  * Since: 0.9.2
 886  **/
 887 void
 888 hb_ot_shape_glyphs_closure (hb_font_t          *font,
 889                             hb_buffer_t        *buffer,
 890                             const hb_feature_t *features,
 891                             unsigned int        num_features,
 892                             hb_set_t           *glyphs)
 893 {
 894   hb_ot_shape_plan_t plan;
 895 
 896   const char *shapers[] = {"ot", NULL};
 897   hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
 898                                                              features, num_features, shapers);
 899 
 900   bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
 901 
 902   unsigned int count = buffer->len;
 903   hb_glyph_info_t *info = buffer->info;
 904   for (unsigned int i = 0; i < count; i++)
 905     add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
 906 
 907   hb_set_t lookups;
 908   lookups.init ();
 909   hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, &lookups);
 910 
 911   /* And find transitive closure. */
 912   hb_set_t copy;
 913   copy.init ();
 914   do {
 915     copy.set (glyphs);
 916     for (hb_codepoint_t lookup_index = -1; hb_set_next (&lookups, &lookup_index);)
 917       hb_ot_layout_lookup_substitute_closure (font->face, lookup_index, glyphs);
 918   } while (!copy.is_equal (glyphs));
 919 
 920   hb_shape_plan_destroy (shape_plan);
 921 }