< prev index next >

src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-shape.cc

Print this page




  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('l','i','g','a'),
  59   HB_TAG('r','c','l','t'),
  60 };
  61 
  62 
  63 
  64 static void
  65 hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
  66                               const hb_segment_properties_t  *props,
  67                               const hb_feature_t             *user_features,
  68                               unsigned int                    num_user_features)
  69 {
  70   hb_ot_map_builder_t *map = &planner->map;
  71 
  72   map->add_global_bool_feature (HB_TAG('r','v','r','n'));
  73   map->add_gsub_pause (NULL);
  74 
  75   switch (props->direction) {
  76     case HB_DIRECTION_LTR:
  77       map->add_global_bool_feature (HB_TAG ('l','t','r','a'));
  78       map->add_global_bool_feature (HB_TAG ('l','t','r','m'));
  79       break;
  80     case HB_DIRECTION_RTL:
  81       map->add_global_bool_feature (HB_TAG ('r','t','l','a'));
  82       map->add_feature (HB_TAG ('r','t','l','m'), 1, F_NONE);
  83       break;
  84     case HB_DIRECTION_TTB:
  85     case HB_DIRECTION_BTT:
  86     case HB_DIRECTION_INVALID:
  87     default:
  88       break;
  89   }
  90 
  91   map->add_feature (HB_TAG ('f','r','a','c'), 1, F_NONE);
  92   map->add_feature (HB_TAG ('n','u','m','r'), 1, F_NONE);
  93   map->add_feature (HB_TAG ('d','n','o','m'), 1, F_NONE);


 111      * https://github.com/behdad/harfbuzz/issues/63 */
 112     map->add_feature (HB_TAG ('v','e','r','t'), 1, F_GLOBAL | F_GLOBAL_SEARCH);
 113   }
 114 
 115   if (planner->shaper->override_features)
 116     planner->shaper->override_features (planner);
 117 
 118   for (unsigned int i = 0; i < num_user_features; i++) {
 119     const hb_feature_t *feature = &user_features[i];
 120     map->add_feature (feature->tag, feature->value,
 121                       (feature->start == 0 && feature->end == (unsigned int) -1) ?
 122                        F_GLOBAL : F_NONE);
 123   }
 124 }
 125 
 126 
 127 /*
 128  * shaper face data
 129  */
 130 


 131 hb_ot_shaper_face_data_t *
 132 _hb_ot_shaper_face_data_create (hb_face_t *face)
 133 {
 134   return _hb_ot_layout_create (face);
 135 }
 136 
 137 void
 138 _hb_ot_shaper_face_data_destroy (hb_ot_shaper_face_data_t *data)
 139 {
 140   _hb_ot_layout_destroy (data);
 141 }
 142 
 143 
 144 /*
 145  * shaper font data
 146  */
 147 


 148 struct hb_ot_shaper_font_data_t {};
 149 
 150 hb_ot_shaper_font_data_t *
 151 _hb_ot_shaper_font_data_create (hb_font_t *font HB_UNUSED)
 152 {
 153   return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
 154 }
 155 
 156 void
 157 _hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data)
 158 {
 159 }
 160 
 161 
 162 /*
 163  * shaper shape_plan data
 164  */
 165 
 166 hb_ot_shaper_shape_plan_data_t *
 167 _hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan,
 168                                       const hb_feature_t *user_features,
 169                                       unsigned int        num_user_features,
 170                                       const int          *coords,
 171                                       unsigned int        num_coords)
 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,
 182                                 user_features, num_user_features);
 183 
 184   planner.compile (*plan, coords, num_coords);
 185 
 186   if (plan->shaper->data_create) {
 187     plan->data = plan->shaper->data_create (plan);
 188     if (unlikely (!plan->data))
 189       return NULL;
 190   }
 191 
 192   return plan;
 193 }
 194 
 195 void
 196 _hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *plan)
 197 {
 198   if (plan->shaper->data_destroy)
 199     plan->shaper->data_destroy (const_cast<void *> (plan->data));
 200 
 201   plan->finish ();
 202 
 203   free (plan);
 204 }
 205 
 206 
 207 /*
 208  * shaper
 209  */


 254   hb_glyph_info_t dottedcircle = {0};
 255   dottedcircle.codepoint = 0x25CCu;
 256   _hb_glyph_info_set_unicode_props (&dottedcircle, buffer);
 257 
 258   buffer->clear_output ();
 259 
 260   buffer->idx = 0;
 261   hb_glyph_info_t info = dottedcircle;
 262   info.cluster = buffer->cur().cluster;
 263   info.mask = buffer->cur().mask;
 264   buffer->output_info (info);
 265   while (buffer->idx < buffer->len && !buffer->in_error)
 266     buffer->next_glyph ();
 267 
 268   buffer->swap_buffers ();
 269 }
 270 
 271 static void
 272 hb_form_clusters (hb_buffer_t *buffer)
 273 {
 274   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
 275       buffer->cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
 276     return;
 277 
 278   /* Loop duplicated in hb_ensure_native_direction(), and in _hb-coretext.cc */
 279   unsigned int base = 0;
 280   unsigned int count = buffer->len;
 281   hb_glyph_info_t *info = buffer->info;
 282   for (unsigned int i = 1; i < count; i++)
 283   {
 284     if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i])) &&
 285                 !_hb_glyph_info_is_joiner (&info[i])))
 286     {

 287       buffer->merge_clusters (base, i);


 288       base = i;
 289     }
 290   }

 291   buffer->merge_clusters (base, count);


 292 }
 293 
 294 static void
 295 hb_ensure_native_direction (hb_buffer_t *buffer)
 296 {
 297   hb_direction_t direction = buffer->props.direction;
 298 
 299   /* TODO vertical:
 300    * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
 301    * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
 302    * first. */
 303   if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
 304       (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
 305   {
 306     /* Same loop as hb_form_clusters().
 307      * Since form_clusters() merged clusters already, we don't merge. */
 308     unsigned int base = 0;
 309     unsigned int count = buffer->len;
 310     hb_glyph_info_t *info = buffer->info;
 311     for (unsigned int i = 1; i < count; i++)


 345   unsigned int count = buffer->len;
 346   hb_glyph_info_t *info = buffer->info;
 347   for (unsigned int i = 0; i < count; i++) {
 348     hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
 349     if (likely (codepoint == info[i].codepoint || !c->font->has_glyph (codepoint)))
 350       info[i].mask |= rtlm_mask;
 351     else
 352       info[i].codepoint = codepoint;
 353   }
 354 }
 355 
 356 static inline void
 357 hb_ot_shape_setup_masks_fraction (hb_ot_shape_context_t *c)
 358 {
 359   if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
 360       !c->plan->has_frac)
 361     return;
 362 
 363   hb_buffer_t *buffer = c->buffer;
 364 
 365   /* TODO look in pre/post context text also. */











 366   unsigned int count = buffer->len;
 367   hb_glyph_info_t *info = buffer->info;
 368   for (unsigned int i = 0; i < count; i++)
 369   {
 370     if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
 371     {
 372       unsigned int start = i, end = i + 1;
 373       while (start &&
 374              _hb_glyph_info_get_general_category (&info[start - 1]) ==
 375              HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
 376         start--;
 377       while (end < count &&
 378              _hb_glyph_info_get_general_category (&info[end]) ==
 379              HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
 380         end++;
 381 


 382       for (unsigned int j = start; j < i; j++)
 383         info[j].mask |= c->plan->numr_mask | c->plan->frac_mask;
 384       info[i].mask |= c->plan->frac_mask;
 385       for (unsigned int j = i + 1; j < end; j++)
 386         info[j].mask |= c->plan->frac_mask | c->plan->dnom_mask;
 387 
 388       i = end - 1;
 389     }
 390   }
 391 }
 392 
 393 static inline void
 394 hb_ot_shape_initialize_masks (hb_ot_shape_context_t *c)
 395 {
 396   hb_ot_map_t *map = &c->plan->map;
 397   hb_buffer_t *buffer = c->buffer;
 398 
 399   hb_mask_t global_mask = map->get_global_mask ();
 400   buffer->reset_masks (global_mask);
 401 }
 402 
 403 static inline void
 404 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
 405 {
 406   hb_ot_map_t *map = &c->plan->map;


 477   {
 478     /* Merge clusters and delete default-ignorables.
 479      * NOTE! We can't use out-buffer as we have positioning data. */
 480     unsigned int j = i;
 481     for (; i < count; i++)
 482     {
 483       if (_hb_glyph_info_is_default_ignorable (&info[i]))
 484       {
 485         /* Merge clusters.
 486          * Same logic as buffer->delete_glyph(), but for in-place removal. */
 487 
 488         unsigned int cluster = info[i].cluster;
 489         if (i + 1 < count && cluster == info[i + 1].cluster)
 490           continue; /* Cluster survives; do nothing. */
 491 
 492         if (j)
 493         {
 494           /* Merge cluster backward. */
 495           if (cluster < info[j - 1].cluster)
 496           {

 497             unsigned int old_cluster = info[j - 1].cluster;
 498             for (unsigned k = j; k && info[k - 1].cluster == old_cluster; k--)
 499               info[k - 1].cluster = cluster;
 500           }
 501           continue;
 502         }
 503 
 504         if (i + 1 < count)
 505           buffer->merge_clusters (i, i + 2); /* Merge cluster forward. */
 506 
 507         continue;
 508       }
 509 
 510       if (j != i)
 511       {
 512         info[j] = info[i];
 513         pos[j] = pos[i];
 514       }
 515       j++;
 516     }
 517     buffer->len = j;
 518   }
 519 }


 545      * but having them as mark will cause them to be skipped
 546      * over if the lookup-flag says so, but at least for the
 547      * Mongolian variation selectors, looks like Uniscribe
 548      * marks them as non-mark.  Some Mongolian fonts without
 549      * GDEF rely on this.  Another notable character that
 550      * this applies to is COMBINING GRAPHEME JOINER. */
 551     klass = (_hb_glyph_info_get_general_category (&info[i]) !=
 552              HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
 553              _hb_glyph_info_is_default_ignorable (&info[i])) ?
 554             HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
 555             HB_OT_LAYOUT_GLYPH_PROPS_MARK;
 556     _hb_glyph_info_set_glyph_props (&info[i], klass);
 557   }
 558 }
 559 
 560 static inline void
 561 hb_ot_substitute_default (hb_ot_shape_context_t *c)
 562 {
 563   hb_buffer_t *buffer = c->buffer;
 564 
 565   hb_ot_shape_initialize_masks (c);
 566 
 567   hb_ot_mirror_chars (c);
 568 
 569   HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
 570 
 571   _hb_ot_shape_normalize (c->plan, buffer, c->font);
 572 
 573   hb_ot_shape_setup_masks (c);
 574 
 575   /* This is unfortunate to go here, but necessary... */
 576   if (c->fallback_positioning)
 577     _hb_ot_shape_fallback_position_recategorize_marks (c->plan, c->font, buffer);
 578 
 579   hb_ot_map_glyphs_fast (buffer);
 580 
 581   HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
 582 }
 583 
 584 static inline void
 585 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
 586 {


 655                                           &pos[i].x_offset,
 656                                           &pos[i].y_offset);
 657   }
 658   else
 659   {
 660     for (unsigned int i = 0; i < count; i++)
 661     {
 662       pos[i].y_advance = c->font->get_glyph_v_advance (info[i].codepoint);
 663       c->font->subtract_glyph_v_origin (info[i].codepoint,
 664                                         &pos[i].x_offset,
 665                                         &pos[i].y_offset);
 666     }
 667   }
 668   if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
 669     _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
 670 }
 671 
 672 static inline void
 673 hb_ot_position_complex (hb_ot_shape_context_t *c)
 674 {
 675   hb_ot_layout_position_start (c->font, c->buffer);
 676 
 677   unsigned int count = c->buffer->len;


 678 
 679   /* If the font has no GPOS, AND, no fallback positioning will
 680    * happen, AND, direction is forward, then when zeroing mark
 681    * widths, we shift the mark with it, such that the mark
 682    * is positioned hanging over the previous glyph.  When
 683    * direction is backward we don't shift and it will end up
 684    * hanging over the next glyph after the final reordering.
 685    * If fallback positinoing happens or GPOS is present, we don't
 686    * care.
 687    */
 688   bool adjust_offsets_when_zeroing = c->fallback_positioning &&
 689                                      !c->plan->shaper->fallback_position &&
 690                                      HB_DIRECTION_IS_FORWARD (c->buffer->props.direction);
 691 











 692   switch (c->plan->shaper->zero_width_marks)
 693   {
 694     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
 695       zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
 696       break;
 697 
 698     default:
 699     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
 700     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
 701       break;
 702   }
 703 
 704   if (likely (!c->fallback_positioning))
 705   {
 706     hb_glyph_info_t *info = c->buffer->info;
 707     hb_glyph_position_t *pos = c->buffer->pos;
 708 
 709     /* Change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */
 710 
 711     /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
 712     if (c->font->has_glyph_h_origin_func ())
 713       for (unsigned int i = 0; i < count; i++)
 714         c->font->add_glyph_h_origin (info[i].codepoint,
 715                                      &pos[i].x_offset,
 716                                      &pos[i].y_offset);
 717 
 718     c->plan->position (c->font, c->buffer);
 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->subtract_glyph_h_origin (info[i].codepoint,
 724                                           &pos[i].x_offset,
 725                                           &pos[i].y_offset);
 726 
 727   }
 728 
 729   switch (c->plan->shaper->zero_width_marks)
 730   {
 731     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
 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_EARLY:
 738       break;
 739   }
 740 
 741   /* Finishing off GPOS has to follow a certain order. */
 742   hb_ot_layout_position_finish_advances (c->font, c->buffer);
 743   hb_ot_zero_width_default_ignorables (c);
 744   hb_ot_layout_position_finish_offsets (c->font, c->buffer);







 745 }
 746 
 747 static inline void
 748 hb_ot_position (hb_ot_shape_context_t *c)
 749 {
 750   c->buffer->clear_positions ();
 751 
 752   hb_ot_position_default (c);
 753 
 754   hb_ot_position_complex (c);
 755 
 756   if (c->fallback_positioning && c->plan->shaper->fallback_position)
 757     _hb_ot_shape_fallback_position (c->plan, c->font, c->buffer);
 758 
 759   if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
 760     hb_buffer_reverse (c->buffer);
 761 
 762   /* Visual fallback goes here. */
 763 
 764   if (c->fallback_positioning)
 765     _hb_ot_shape_fallback_kern (c->plan, c->font, c->buffer);
 766 
 767   _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
 768 }
 769 

























 770 
 771 /* Pull it all together! */
 772 
 773 static void
 774 hb_ot_shape_internal (hb_ot_shape_context_t *c)
 775 {
 776   c->buffer->deallocate_var_all ();
 777   c->buffer->scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
 778   if (likely (!_hb_unsigned_int_mul_overflows (c->buffer->len, HB_BUFFER_MAX_EXPANSION_FACTOR)))
 779   {
 780     c->buffer->max_len = MAX (c->buffer->len * HB_BUFFER_MAX_EXPANSION_FACTOR,
 781                               (unsigned) HB_BUFFER_MAX_LEN_MIN);
 782   }
 783 
 784   bool disable_otl = c->plan->shaper->disable_otl && c->plan->shaper->disable_otl (c->plan);
 785   //c->fallback_substitute     = disable_otl || !hb_ot_layout_has_substitution (c->face);
 786   c->fallback_positioning    = disable_otl || !hb_ot_layout_has_positioning (c->face);
 787   c->fallback_glyph_classes  = disable_otl || !hb_ot_layout_has_glyph_classes (c->face);
 788 
 789   /* Save the original direction, we use it later. */
 790   c->target_direction = c->buffer->props.direction;
 791 
 792   _hb_buffer_allocate_unicode_vars (c->buffer);
 793 
 794   c->buffer->clear_output ();
 795 

 796   hb_set_unicode_props (c->buffer);
 797   hb_insert_dotted_circle (c->buffer, c->font);

 798   hb_form_clusters (c->buffer);
 799 
 800   hb_ensure_native_direction (c->buffer);
 801 
 802   if (c->plan->shaper->preprocess_text)
 803     c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
 804 
 805   hb_ot_substitute (c);
 806   hb_ot_position (c);
 807 
 808   hb_ot_hide_default_ignorables (c);
 809 
 810   if (c->plan->shaper->postprocess_glyphs)
 811     c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
 812 


 813   _hb_buffer_deallocate_unicode_vars (c->buffer);
 814 
 815   c->buffer->props.direction = c->target_direction;
 816 
 817   c->buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
 818   c->buffer->deallocate_var_all ();
 819 }
 820 
 821 
 822 hb_bool_t
 823 _hb_ot_shape (hb_shape_plan_t    *shape_plan,
 824               hb_font_t          *font,
 825               hb_buffer_t        *buffer,
 826               const hb_feature_t *features,
 827               unsigned int        num_features)
 828 {
 829   hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features};
 830   hb_ot_shape_internal (&c);
 831 
 832   return true;


 865     if (m != u && font->get_nominal_glyph (m, &glyph))
 866       glyphs->add (glyph);
 867   }
 868 }
 869 
 870 
 871 /**
 872  * hb_ot_shape_glyphs_closure:
 873  *
 874  * Since: 0.9.2
 875  **/
 876 void
 877 hb_ot_shape_glyphs_closure (hb_font_t          *font,
 878                             hb_buffer_t        *buffer,
 879                             const hb_feature_t *features,
 880                             unsigned int        num_features,
 881                             hb_set_t           *glyphs)
 882 {
 883   hb_ot_shape_plan_t plan;
 884 
 885   const char *shapers[] = {"ot", NULL};
 886   hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
 887                                                              features, num_features, shapers);
 888 
 889   bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
 890 
 891   unsigned int count = buffer->len;
 892   hb_glyph_info_t *info = buffer->info;
 893   for (unsigned int i = 0; i < count; i++)
 894     add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
 895 
 896   hb_set_t lookups;
 897   lookups.init ();
 898   hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, &lookups);
 899 
 900   /* And find transitive closure. */
 901   hb_set_t copy;
 902   copy.init ();
 903   do {
 904     copy.set (glyphs);
 905     for (hb_codepoint_t lookup_index = -1; hb_set_next (&lookups, &lookup_index);)
 906       hb_ot_layout_lookup_substitute_closure (font->face, lookup_index, glyphs);
 907   } while (!copy.is_equal (glyphs));



 908 
 909   hb_shape_plan_destroy (shape_plan);
 910 }


  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('l','i','g','a'),
  59   HB_TAG('r','c','l','t'),
  60 };
  61 
  62 
  63 
  64 static void
  65 hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
  66                               const hb_segment_properties_t  *props,
  67                               const hb_feature_t             *user_features,
  68                               unsigned int                    num_user_features)
  69 {
  70   hb_ot_map_builder_t *map = &planner->map;
  71 
  72   map->add_global_bool_feature (HB_TAG('r','v','r','n'));
  73   map->add_gsub_pause (nullptr);
  74 
  75   switch (props->direction) {
  76     case HB_DIRECTION_LTR:
  77       map->add_global_bool_feature (HB_TAG ('l','t','r','a'));
  78       map->add_global_bool_feature (HB_TAG ('l','t','r','m'));
  79       break;
  80     case HB_DIRECTION_RTL:
  81       map->add_global_bool_feature (HB_TAG ('r','t','l','a'));
  82       map->add_feature (HB_TAG ('r','t','l','m'), 1, F_NONE);
  83       break;
  84     case HB_DIRECTION_TTB:
  85     case HB_DIRECTION_BTT:
  86     case HB_DIRECTION_INVALID:
  87     default:
  88       break;
  89   }
  90 
  91   map->add_feature (HB_TAG ('f','r','a','c'), 1, F_NONE);
  92   map->add_feature (HB_TAG ('n','u','m','r'), 1, F_NONE);
  93   map->add_feature (HB_TAG ('d','n','o','m'), 1, F_NONE);


 111      * https://github.com/behdad/harfbuzz/issues/63 */
 112     map->add_feature (HB_TAG ('v','e','r','t'), 1, F_GLOBAL | F_GLOBAL_SEARCH);
 113   }
 114 
 115   if (planner->shaper->override_features)
 116     planner->shaper->override_features (planner);
 117 
 118   for (unsigned int i = 0; i < num_user_features; i++) {
 119     const hb_feature_t *feature = &user_features[i];
 120     map->add_feature (feature->tag, feature->value,
 121                       (feature->start == 0 && feature->end == (unsigned int) -1) ?
 122                        F_GLOBAL : F_NONE);
 123   }
 124 }
 125 
 126 
 127 /*
 128  * shaper face data
 129  */
 130 
 131 HB_SHAPER_DATA_ENSURE_DEFINE(ot, face)
 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 HB_SHAPER_DATA_ENSURE_DEFINE(ot, font)
 151 
 152 struct hb_ot_shaper_font_data_t {};
 153 
 154 hb_ot_shaper_font_data_t *
 155 _hb_ot_shaper_font_data_create (hb_font_t *font HB_UNUSED)
 156 {
 157   return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
 158 }
 159 
 160 void
 161 _hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data)
 162 {
 163 }
 164 
 165 
 166 /*
 167  * shaper shape_plan data
 168  */
 169 
 170 hb_ot_shaper_shape_plan_data_t *
 171 _hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan,
 172                                       const hb_feature_t *user_features,
 173                                       unsigned int        num_user_features,
 174                                       const int          *coords,
 175                                       unsigned int        num_coords)
 176 {
 177   hb_ot_shape_plan_t *plan = (hb_ot_shape_plan_t *) calloc (1, sizeof (hb_ot_shape_plan_t));
 178   if (unlikely (!plan))
 179     return nullptr;
 180 
 181   hb_ot_shape_planner_t planner (shape_plan);
 182 
 183   planner.shaper = hb_ot_shape_complex_categorize (&planner);
 184 
 185   hb_ot_shape_collect_features (&planner, &shape_plan->props,
 186                                 user_features, num_user_features);
 187 
 188   planner.compile (*plan, coords, num_coords);
 189 
 190   if (plan->shaper->data_create) {
 191     plan->data = plan->shaper->data_create (plan);
 192     if (unlikely (!plan->data))
 193       return nullptr;
 194   }
 195 
 196   return plan;
 197 }
 198 
 199 void
 200 _hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *plan)
 201 {
 202   if (plan->shaper->data_destroy)
 203     plan->shaper->data_destroy (const_cast<void *> (plan->data));
 204 
 205   plan->finish ();
 206 
 207   free (plan);
 208 }
 209 
 210 
 211 /*
 212  * shaper
 213  */


 258   hb_glyph_info_t dottedcircle = {0};
 259   dottedcircle.codepoint = 0x25CCu;
 260   _hb_glyph_info_set_unicode_props (&dottedcircle, buffer);
 261 
 262   buffer->clear_output ();
 263 
 264   buffer->idx = 0;
 265   hb_glyph_info_t info = dottedcircle;
 266   info.cluster = buffer->cur().cluster;
 267   info.mask = buffer->cur().mask;
 268   buffer->output_info (info);
 269   while (buffer->idx < buffer->len && !buffer->in_error)
 270     buffer->next_glyph ();
 271 
 272   buffer->swap_buffers ();
 273 }
 274 
 275 static void
 276 hb_form_clusters (hb_buffer_t *buffer)
 277 {
 278   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII))

 279     return;
 280 
 281   /* Loop duplicated in hb_ensure_native_direction(), and in _hb-coretext.cc */
 282   unsigned int base = 0;
 283   unsigned int count = buffer->len;
 284   hb_glyph_info_t *info = buffer->info;
 285   for (unsigned int i = 1; i < count; i++)
 286   {
 287     if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i])) &&
 288                 !_hb_glyph_info_is_joiner (&info[i])))
 289     {
 290       if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
 291         buffer->merge_clusters (base, i);
 292       else
 293         buffer->unsafe_to_break (base, i);
 294       base = i;
 295     }
 296   }
 297   if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
 298     buffer->merge_clusters (base, count);
 299   else
 300     buffer->unsafe_to_break (base, count);
 301 }
 302 
 303 static void
 304 hb_ensure_native_direction (hb_buffer_t *buffer)
 305 {
 306   hb_direction_t direction = buffer->props.direction;
 307 
 308   /* TODO vertical:
 309    * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
 310    * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
 311    * first. */
 312   if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
 313       (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
 314   {
 315     /* Same loop as hb_form_clusters().
 316      * Since form_clusters() merged clusters already, we don't merge. */
 317     unsigned int base = 0;
 318     unsigned int count = buffer->len;
 319     hb_glyph_info_t *info = buffer->info;
 320     for (unsigned int i = 1; i < count; i++)


 354   unsigned int count = buffer->len;
 355   hb_glyph_info_t *info = buffer->info;
 356   for (unsigned int i = 0; i < count; i++) {
 357     hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
 358     if (likely (codepoint == info[i].codepoint || !c->font->has_glyph (codepoint)))
 359       info[i].mask |= rtlm_mask;
 360     else
 361       info[i].codepoint = codepoint;
 362   }
 363 }
 364 
 365 static inline void
 366 hb_ot_shape_setup_masks_fraction (hb_ot_shape_context_t *c)
 367 {
 368   if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
 369       !c->plan->has_frac)
 370     return;
 371 
 372   hb_buffer_t *buffer = c->buffer;
 373 
 374   hb_mask_t pre_mask, post_mask;
 375   if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
 376   {
 377     pre_mask = c->plan->numr_mask | c->plan->frac_mask;
 378     post_mask = c->plan->frac_mask | c->plan->dnom_mask;
 379   }
 380   else
 381   {
 382     pre_mask = c->plan->frac_mask | c->plan->dnom_mask;
 383     post_mask = c->plan->numr_mask | c->plan->frac_mask;
 384   }
 385 
 386   unsigned int count = buffer->len;
 387   hb_glyph_info_t *info = buffer->info;
 388   for (unsigned int i = 0; i < count; i++)
 389   {
 390     if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
 391     {
 392       unsigned int start = i, end = i + 1;
 393       while (start &&
 394              _hb_glyph_info_get_general_category (&info[start - 1]) ==
 395              HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
 396         start--;
 397       while (end < count &&
 398              _hb_glyph_info_get_general_category (&info[end]) ==
 399              HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
 400         end++;
 401 
 402       buffer->unsafe_to_break (start, end);
 403 
 404       for (unsigned int j = start; j < i; j++)
 405         info[j].mask |= pre_mask;
 406       info[i].mask |= c->plan->frac_mask;
 407       for (unsigned int j = i + 1; j < end; j++)
 408         info[j].mask |= post_mask;
 409 
 410       i = end - 1;
 411     }
 412   }
 413 }
 414 
 415 static inline void
 416 hb_ot_shape_initialize_masks (hb_ot_shape_context_t *c)
 417 {
 418   hb_ot_map_t *map = &c->plan->map;
 419   hb_buffer_t *buffer = c->buffer;
 420 
 421   hb_mask_t global_mask = map->get_global_mask ();
 422   buffer->reset_masks (global_mask);
 423 }
 424 
 425 static inline void
 426 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
 427 {
 428   hb_ot_map_t *map = &c->plan->map;


 499   {
 500     /* Merge clusters and delete default-ignorables.
 501      * NOTE! We can't use out-buffer as we have positioning data. */
 502     unsigned int j = i;
 503     for (; i < count; i++)
 504     {
 505       if (_hb_glyph_info_is_default_ignorable (&info[i]))
 506       {
 507         /* Merge clusters.
 508          * Same logic as buffer->delete_glyph(), but for in-place removal. */
 509 
 510         unsigned int cluster = info[i].cluster;
 511         if (i + 1 < count && cluster == info[i + 1].cluster)
 512           continue; /* Cluster survives; do nothing. */
 513 
 514         if (j)
 515         {
 516           /* Merge cluster backward. */
 517           if (cluster < info[j - 1].cluster)
 518           {
 519             unsigned int mask = info[i].mask;
 520             unsigned int old_cluster = info[j - 1].cluster;
 521             for (unsigned k = j; k && info[k - 1].cluster == old_cluster; k--)
 522               buffer->set_cluster (info[k - 1], cluster, mask);
 523           }
 524           continue;
 525         }
 526 
 527         if (i + 1 < count)
 528           buffer->merge_clusters (i, i + 2); /* Merge cluster forward. */
 529 
 530         continue;
 531       }
 532 
 533       if (j != i)
 534       {
 535         info[j] = info[i];
 536         pos[j] = pos[i];
 537       }
 538       j++;
 539     }
 540     buffer->len = j;
 541   }
 542 }


 568      * but having them as mark will cause them to be skipped
 569      * over if the lookup-flag says so, but at least for the
 570      * Mongolian variation selectors, looks like Uniscribe
 571      * marks them as non-mark.  Some Mongolian fonts without
 572      * GDEF rely on this.  Another notable character that
 573      * this applies to is COMBINING GRAPHEME JOINER. */
 574     klass = (_hb_glyph_info_get_general_category (&info[i]) !=
 575              HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
 576              _hb_glyph_info_is_default_ignorable (&info[i])) ?
 577             HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
 578             HB_OT_LAYOUT_GLYPH_PROPS_MARK;
 579     _hb_glyph_info_set_glyph_props (&info[i], klass);
 580   }
 581 }
 582 
 583 static inline void
 584 hb_ot_substitute_default (hb_ot_shape_context_t *c)
 585 {
 586   hb_buffer_t *buffer = c->buffer;
 587 


 588   hb_ot_mirror_chars (c);
 589 
 590   HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
 591 
 592   _hb_ot_shape_normalize (c->plan, buffer, c->font);
 593 
 594   hb_ot_shape_setup_masks (c);
 595 
 596   /* This is unfortunate to go here, but necessary... */
 597   if (c->fallback_positioning)
 598     _hb_ot_shape_fallback_position_recategorize_marks (c->plan, c->font, buffer);
 599 
 600   hb_ot_map_glyphs_fast (buffer);
 601 
 602   HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
 603 }
 604 
 605 static inline void
 606 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
 607 {


 676                                           &pos[i].x_offset,
 677                                           &pos[i].y_offset);
 678   }
 679   else
 680   {
 681     for (unsigned int i = 0; i < count; i++)
 682     {
 683       pos[i].y_advance = c->font->get_glyph_v_advance (info[i].codepoint);
 684       c->font->subtract_glyph_v_origin (info[i].codepoint,
 685                                         &pos[i].x_offset,
 686                                         &pos[i].y_offset);
 687     }
 688   }
 689   if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
 690     _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
 691 }
 692 
 693 static inline void
 694 hb_ot_position_complex (hb_ot_shape_context_t *c)
 695 {


 696   unsigned int count = c->buffer->len;
 697   hb_glyph_info_t *info = c->buffer->info;
 698   hb_glyph_position_t *pos = c->buffer->pos;
 699 
 700   /* If the font has no GPOS, AND, no fallback positioning will
 701    * happen, AND, direction is forward, then when zeroing mark
 702    * widths, we shift the mark with it, such that the mark
 703    * is positioned hanging over the previous glyph.  When
 704    * direction is backward we don't shift and it will end up
 705    * hanging over the next glyph after the final reordering.
 706    * If fallback positinoing happens or GPOS is present, we don't
 707    * care.
 708    */
 709   bool adjust_offsets_when_zeroing = c->fallback_positioning &&
 710                                      !c->plan->shaper->fallback_position &&
 711                                      HB_DIRECTION_IS_FORWARD (c->buffer->props.direction);
 712 
 713   /* We change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */
 714 
 715   /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
 716   if (c->font->has_glyph_h_origin_func ())
 717     for (unsigned int i = 0; i < count; i++)
 718       c->font->add_glyph_h_origin (info[i].codepoint,
 719                                    &pos[i].x_offset,
 720                                    &pos[i].y_offset);
 721 
 722   hb_ot_layout_position_start (c->font, c->buffer);
 723 
 724   switch (c->plan->shaper->zero_width_marks)
 725   {
 726     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
 727       zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
 728       break;
 729 
 730     default:
 731     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
 732     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
 733       break;
 734   }
 735 
 736   if (likely (!c->fallback_positioning))













 737     c->plan->position (c->font, c->buffer);
 738 









 739   switch (c->plan->shaper->zero_width_marks)
 740   {
 741     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
 742       zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
 743       break;
 744 
 745     default:
 746     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
 747     case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
 748       break;
 749   }
 750 
 751   /* Finishing off GPOS has to follow a certain order. */
 752   hb_ot_layout_position_finish_advances (c->font, c->buffer);
 753   hb_ot_zero_width_default_ignorables (c);
 754   hb_ot_layout_position_finish_offsets (c->font, c->buffer);
 755 
 756   /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
 757   if (c->font->has_glyph_h_origin_func ())
 758     for (unsigned int i = 0; i < count; i++)
 759       c->font->subtract_glyph_h_origin (info[i].codepoint,
 760                                         &pos[i].x_offset,
 761                                         &pos[i].y_offset);
 762 }
 763 
 764 static inline void
 765 hb_ot_position (hb_ot_shape_context_t *c)
 766 {
 767   c->buffer->clear_positions ();
 768 
 769   hb_ot_position_default (c);
 770 
 771   hb_ot_position_complex (c);
 772 
 773   if (c->fallback_positioning && c->plan->shaper->fallback_position)
 774     _hb_ot_shape_fallback_position (c->plan, c->font, c->buffer);
 775 
 776   if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
 777     hb_buffer_reverse (c->buffer);
 778 
 779   /* Visual fallback goes here. */
 780 
 781   if (c->fallback_positioning)
 782     _hb_ot_shape_fallback_kern (c->plan, c->font, c->buffer);
 783 
 784   _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
 785 }
 786 
 787 static inline void
 788 hb_propagate_flags (hb_buffer_t *buffer)
 789 {
 790   /* Propagate cluster-level glyph flags to be the same on all cluster glyphs.
 791    * Simplifies using them. */
 792 
 793   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_UNSAFE_TO_BREAK))
 794     return;
 795 
 796   hb_glyph_info_t *info = buffer->info;
 797 
 798   foreach_cluster (buffer, start, end)
 799   {
 800     unsigned int mask = 0;
 801     for (unsigned int i = start; i < end; i++)
 802       if (info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)
 803       {
 804          mask = HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
 805          break;
 806       }
 807     if (mask)
 808       for (unsigned int i = start; i < end; i++)
 809         info[i].mask |= mask;
 810   }
 811 }
 812 
 813 /* Pull it all together! */
 814 
 815 static void
 816 hb_ot_shape_internal (hb_ot_shape_context_t *c)
 817 {
 818   c->buffer->deallocate_var_all ();
 819   c->buffer->scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
 820   if (likely (!_hb_unsigned_int_mul_overflows (c->buffer->len, HB_BUFFER_MAX_EXPANSION_FACTOR)))
 821   {
 822     c->buffer->max_len = MAX (c->buffer->len * HB_BUFFER_MAX_EXPANSION_FACTOR,
 823                               (unsigned) HB_BUFFER_MAX_LEN_MIN);
 824   }
 825 
 826   bool disable_otl = c->plan->shaper->disable_otl && c->plan->shaper->disable_otl (c->plan);
 827   //c->fallback_substitute     = disable_otl || !hb_ot_layout_has_substitution (c->face);
 828   c->fallback_positioning    = disable_otl || !hb_ot_layout_has_positioning (c->face);
 829   c->fallback_glyph_classes  = disable_otl || !hb_ot_layout_has_glyph_classes (c->face);
 830 
 831   /* Save the original direction, we use it later. */
 832   c->target_direction = c->buffer->props.direction;
 833 
 834   _hb_buffer_allocate_unicode_vars (c->buffer);
 835 
 836   c->buffer->clear_output ();
 837 
 838   hb_ot_shape_initialize_masks (c);
 839   hb_set_unicode_props (c->buffer);
 840   hb_insert_dotted_circle (c->buffer, c->font);
 841 
 842   hb_form_clusters (c->buffer);
 843 
 844   hb_ensure_native_direction (c->buffer);
 845 
 846   if (c->plan->shaper->preprocess_text)
 847     c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
 848 
 849   hb_ot_substitute (c);
 850   hb_ot_position (c);
 851 
 852   hb_ot_hide_default_ignorables (c);
 853 
 854   if (c->plan->shaper->postprocess_glyphs)
 855     c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
 856 
 857   hb_propagate_flags (c->buffer);
 858 
 859   _hb_buffer_deallocate_unicode_vars (c->buffer);
 860 
 861   c->buffer->props.direction = c->target_direction;
 862 
 863   c->buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
 864   c->buffer->deallocate_var_all ();
 865 }
 866 
 867 
 868 hb_bool_t
 869 _hb_ot_shape (hb_shape_plan_t    *shape_plan,
 870               hb_font_t          *font,
 871               hb_buffer_t        *buffer,
 872               const hb_feature_t *features,
 873               unsigned int        num_features)
 874 {
 875   hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features};
 876   hb_ot_shape_internal (&c);
 877 
 878   return true;


 911     if (m != u && font->get_nominal_glyph (m, &glyph))
 912       glyphs->add (glyph);
 913   }
 914 }
 915 
 916 
 917 /**
 918  * hb_ot_shape_glyphs_closure:
 919  *
 920  * Since: 0.9.2
 921  **/
 922 void
 923 hb_ot_shape_glyphs_closure (hb_font_t          *font,
 924                             hb_buffer_t        *buffer,
 925                             const hb_feature_t *features,
 926                             unsigned int        num_features,
 927                             hb_set_t           *glyphs)
 928 {
 929   hb_ot_shape_plan_t plan;
 930 
 931   const char *shapers[] = {"ot", nullptr};
 932   hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
 933                                                              features, num_features, shapers);
 934 
 935   bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
 936 
 937   unsigned int count = buffer->len;
 938   hb_glyph_info_t *info = buffer->info;
 939   for (unsigned int i = 0; i < count; i++)
 940     add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
 941 
 942   hb_set_t *lookups = hb_set_create ();
 943   hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, lookups);

 944 
 945   /* And find transitive closure. */
 946   hb_set_t *copy = hb_set_create ();

 947   do {
 948     copy->set (glyphs);
 949     for (hb_codepoint_t lookup_index = -1; hb_set_next (lookups, &lookup_index);)
 950       hb_ot_layout_lookup_substitute_closure (font->face, lookup_index, glyphs);
 951   } while (!copy->is_equal (glyphs));
 952   hb_set_destroy (copy);
 953 
 954   hb_set_destroy (lookups);
 955 
 956   hb_shape_plan_destroy (shape_plan);
 957 }
< prev index next >