gfx/harfbuzz/src/hb-ot-shape-complex-sea.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/harfbuzz/src/hb-ot-shape-complex-sea.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,377 @@
     1.4 +/*
     1.5 + * Copyright © 2011,2012,2013  Google, Inc.
     1.6 + *
     1.7 + *  This is part of HarfBuzz, a text shaping library.
     1.8 + *
     1.9 + * Permission is hereby granted, without written agreement and without
    1.10 + * license or royalty fees, to use, copy, modify, and distribute this
    1.11 + * software and its documentation for any purpose, provided that the
    1.12 + * above copyright notice and the following two paragraphs appear in
    1.13 + * all copies of this software.
    1.14 + *
    1.15 + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
    1.16 + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
    1.17 + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
    1.18 + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
    1.19 + * DAMAGE.
    1.20 + *
    1.21 + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
    1.22 + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    1.23 + * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
    1.24 + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
    1.25 + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
    1.26 + *
    1.27 + * Google Author(s): Behdad Esfahbod
    1.28 + */
    1.29 +
    1.30 +#include "hb-ot-shape-complex-indic-private.hh"
    1.31 +
    1.32 +/* buffer var allocations */
    1.33 +#define sea_category() complex_var_u8_0() /* indic_category_t */
    1.34 +#define sea_position() complex_var_u8_1() /* indic_position_t */
    1.35 +
    1.36 +
    1.37 +/*
    1.38 + * South-East Asian shaper.
    1.39 + * Loosely based on the Myanmar spec / shaper.
    1.40 + * There is no OpenType spec for this.
    1.41 + */
    1.42 +
    1.43 +static const hb_tag_t
    1.44 +basic_features[] =
    1.45 +{
    1.46 +  /*
    1.47 +   * Basic features.
    1.48 +   * These features are applied in order, one at a time, after initial_reordering.
    1.49 +   */
    1.50 +  HB_TAG('p','r','e','f'),
    1.51 +  HB_TAG('a','b','v','f'),
    1.52 +  HB_TAG('b','l','w','f'),
    1.53 +  HB_TAG('p','s','t','f'),
    1.54 +};
    1.55 +static const hb_tag_t
    1.56 +other_features[] =
    1.57 +{
    1.58 +  /*
    1.59 +   * Other features.
    1.60 +   * These features are applied all at once, after final_reordering.
    1.61 +   */
    1.62 +  HB_TAG('p','r','e','s'),
    1.63 +  HB_TAG('a','b','v','s'),
    1.64 +  HB_TAG('b','l','w','s'),
    1.65 +  HB_TAG('p','s','t','s'),
    1.66 +  /* Positioning features, though we don't care about the types. */
    1.67 +  HB_TAG('d','i','s','t'),
    1.68 +};
    1.69 +
    1.70 +static void
    1.71 +setup_syllables (const hb_ot_shape_plan_t *plan,
    1.72 +		 hb_font_t *font,
    1.73 +		 hb_buffer_t *buffer);
    1.74 +static void
    1.75 +initial_reordering (const hb_ot_shape_plan_t *plan,
    1.76 +		    hb_font_t *font,
    1.77 +		    hb_buffer_t *buffer);
    1.78 +static void
    1.79 +final_reordering (const hb_ot_shape_plan_t *plan,
    1.80 +		  hb_font_t *font,
    1.81 +		  hb_buffer_t *buffer);
    1.82 +
    1.83 +static void
    1.84 +collect_features_sea (hb_ot_shape_planner_t *plan)
    1.85 +{
    1.86 +  hb_ot_map_builder_t *map = &plan->map;
    1.87 +
    1.88 +  /* Do this before any lookups have been applied. */
    1.89 +  map->add_gsub_pause (setup_syllables);
    1.90 +
    1.91 +  map->add_global_bool_feature (HB_TAG('l','o','c','l'));
    1.92 +  /* The Indic specs do not require ccmp, but we apply it here since if
    1.93 +   * there is a use of it, it's typically at the beginning. */
    1.94 +  map->add_global_bool_feature (HB_TAG('c','c','m','p'));
    1.95 +
    1.96 +  map->add_gsub_pause (initial_reordering);
    1.97 +  for (unsigned int i = 0; i < ARRAY_LENGTH (basic_features); i++)
    1.98 +  {
    1.99 +    map->add_feature (basic_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
   1.100 +    map->add_gsub_pause (NULL);
   1.101 +  }
   1.102 +  map->add_gsub_pause (final_reordering);
   1.103 +  for (unsigned int i = 0; i < ARRAY_LENGTH (other_features); i++)
   1.104 +    map->add_feature (other_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
   1.105 +}
   1.106 +
   1.107 +static void
   1.108 +override_features_sea (hb_ot_shape_planner_t *plan)
   1.109 +{
   1.110 +  plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL);
   1.111 +}
   1.112 +
   1.113 +
   1.114 +enum syllable_type_t {
   1.115 +  consonant_syllable,
   1.116 +  broken_cluster,
   1.117 +  non_sea_cluster,
   1.118 +};
   1.119 +
   1.120 +#include "hb-ot-shape-complex-sea-machine.hh"
   1.121 +
   1.122 +
   1.123 +/* Note: This enum is duplicated in the -machine.rl source file.
   1.124 + * Not sure how to avoid duplication. */
   1.125 +enum sea_category_t {
   1.126 +//  OT_C    = 1,
   1.127 +  OT_GB   = 12, /* Generic Base XXX DOTTED CIRCLE only for now */
   1.128 +//  OT_H    = 4,  /* Halant */
   1.129 +  OT_IV   = 2,  /* Independent Vowel */
   1.130 +  OT_MR   = 22, /* Medial Ra */
   1.131 +//  OT_CM   = 17, /* Consonant Medial */
   1.132 +  OT_VAbv = 26,
   1.133 +  OT_VBlw = 27,
   1.134 +  OT_VPre = 28,
   1.135 +  OT_VPst = 29,
   1.136 +  OT_T    = 3,  /* Tone Marks */
   1.137 +//  OT_A    = 10, /* Anusvara */
   1.138 +};
   1.139 +
   1.140 +static inline void
   1.141 +set_sea_properties (hb_glyph_info_t &info)
   1.142 +{
   1.143 +  hb_codepoint_t u = info.codepoint;
   1.144 +  unsigned int type = hb_indic_get_categories (u);
   1.145 +  indic_category_t cat = (indic_category_t) (type & 0x7F);
   1.146 +  indic_position_t pos = (indic_position_t) (type >> 8);
   1.147 +
   1.148 +  /* Medial Ra */
   1.149 +  if (u == 0x1A55 || u == 0xAA34)
   1.150 +    cat = (indic_category_t) OT_MR;
   1.151 +
   1.152 +  if (cat == OT_M)
   1.153 +  {
   1.154 +    switch ((int) pos)
   1.155 +    {
   1.156 +      case POS_PRE_C:	cat = (indic_category_t) OT_VPre; break;
   1.157 +      case POS_ABOVE_C:	cat = (indic_category_t) OT_VAbv; break;
   1.158 +      case POS_BELOW_C:	cat = (indic_category_t) OT_VBlw; break;
   1.159 +      case POS_POST_C:	cat = (indic_category_t) OT_VPst; break;
   1.160 +    }
   1.161 +  }
   1.162 +
   1.163 +  info.sea_category() = (sea_category_t) cat;
   1.164 +  info.sea_position() = pos;
   1.165 +}
   1.166 +
   1.167 +
   1.168 +static void
   1.169 +setup_masks_sea (const hb_ot_shape_plan_t *plan HB_UNUSED,
   1.170 +		 hb_buffer_t              *buffer,
   1.171 +		 hb_font_t                *font HB_UNUSED)
   1.172 +{
   1.173 +  HB_BUFFER_ALLOCATE_VAR (buffer, sea_category);
   1.174 +  HB_BUFFER_ALLOCATE_VAR (buffer, sea_position);
   1.175 +
   1.176 +  /* We cannot setup masks here.  We save information about characters
   1.177 +   * and setup masks later on in a pause-callback. */
   1.178 +
   1.179 +  unsigned int count = buffer->len;
   1.180 +  for (unsigned int i = 0; i < count; i++)
   1.181 +    set_sea_properties (buffer->info[i]);
   1.182 +}
   1.183 +
   1.184 +static void
   1.185 +setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
   1.186 +		 hb_font_t *font HB_UNUSED,
   1.187 +		 hb_buffer_t *buffer)
   1.188 +{
   1.189 +  find_syllables (buffer);
   1.190 +}
   1.191 +
   1.192 +static int
   1.193 +compare_sea_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
   1.194 +{
   1.195 +  int a = pa->sea_position();
   1.196 +  int b = pb->sea_position();
   1.197 +
   1.198 +  return a < b ? -1 : a == b ? 0 : +1;
   1.199 +}
   1.200 +
   1.201 +
   1.202 +static void
   1.203 +initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan,
   1.204 +				       hb_face_t *face,
   1.205 +				       hb_buffer_t *buffer,
   1.206 +				       unsigned int start, unsigned int end)
   1.207 +{
   1.208 +  hb_glyph_info_t *info = buffer->info;
   1.209 +  unsigned int base = start;
   1.210 +
   1.211 +  /* Reorder! */
   1.212 +  unsigned int i = start;
   1.213 +  for (; i < base; i++)
   1.214 +    info[i].sea_position() = POS_PRE_C;
   1.215 +  if (i < end)
   1.216 +  {
   1.217 +    info[i].sea_position() = POS_BASE_C;
   1.218 +    i++;
   1.219 +  }
   1.220 +  for (; i < end; i++)
   1.221 +  {
   1.222 +    if (info[i].sea_category() == OT_MR) /* Pre-base reordering */
   1.223 +    {
   1.224 +      info[i].sea_position() = POS_PRE_C;
   1.225 +      continue;
   1.226 +    }
   1.227 +    if (info[i].sea_category() == OT_VPre) /* Left matra */
   1.228 +    {
   1.229 +      info[i].sea_position() = POS_PRE_M;
   1.230 +      continue;
   1.231 +    }
   1.232 +
   1.233 +    info[i].sea_position() = POS_AFTER_MAIN;
   1.234 +  }
   1.235 +
   1.236 +  buffer->merge_clusters (start, end);
   1.237 +  /* Sit tight, rock 'n roll! */
   1.238 +  hb_bubble_sort (info + start, end - start, compare_sea_order);
   1.239 +}
   1.240 +
   1.241 +static void
   1.242 +initial_reordering_broken_cluster (const hb_ot_shape_plan_t *plan,
   1.243 +				   hb_face_t *face,
   1.244 +				   hb_buffer_t *buffer,
   1.245 +				   unsigned int start, unsigned int end)
   1.246 +{
   1.247 +  /* We already inserted dotted-circles, so just call the consonant_syllable. */
   1.248 +  initial_reordering_consonant_syllable (plan, face, buffer, start, end);
   1.249 +}
   1.250 +
   1.251 +static void
   1.252 +initial_reordering_non_sea_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED,
   1.253 +				    hb_face_t *face HB_UNUSED,
   1.254 +				    hb_buffer_t *buffer HB_UNUSED,
   1.255 +				    unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
   1.256 +{
   1.257 +  /* Nothing to do right now.  If we ever switch to using the output
   1.258 +   * buffer in the reordering process, we'd need to next_glyph() here. */
   1.259 +}
   1.260 +
   1.261 +
   1.262 +static void
   1.263 +initial_reordering_syllable (const hb_ot_shape_plan_t *plan,
   1.264 +			     hb_face_t *face,
   1.265 +			     hb_buffer_t *buffer,
   1.266 +			     unsigned int start, unsigned int end)
   1.267 +{
   1.268 +  syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F);
   1.269 +  switch (syllable_type) {
   1.270 +  case consonant_syllable:	initial_reordering_consonant_syllable  (plan, face, buffer, start, end); return;
   1.271 +  case broken_cluster:		initial_reordering_broken_cluster      (plan, face, buffer, start, end); return;
   1.272 +  case non_sea_cluster:		initial_reordering_non_sea_cluster     (plan, face, buffer, start, end); return;
   1.273 +  }
   1.274 +}
   1.275 +
   1.276 +static inline void
   1.277 +insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED,
   1.278 +		       hb_font_t *font,
   1.279 +		       hb_buffer_t *buffer)
   1.280 +{
   1.281 +  /* Note: This loop is extra overhead, but should not be measurable. */
   1.282 +  bool has_broken_syllables = false;
   1.283 +  unsigned int count = buffer->len;
   1.284 +  for (unsigned int i = 0; i < count; i++)
   1.285 +    if ((buffer->info[i].syllable() & 0x0F) == broken_cluster) {
   1.286 +      has_broken_syllables = true;
   1.287 +      break;
   1.288 +    }
   1.289 +  if (likely (!has_broken_syllables))
   1.290 +    return;
   1.291 +
   1.292 +
   1.293 +  hb_codepoint_t dottedcircle_glyph;
   1.294 +  if (!font->get_glyph (0x25CC, 0, &dottedcircle_glyph))
   1.295 +    return;
   1.296 +
   1.297 +  hb_glyph_info_t dottedcircle = {0};
   1.298 +  dottedcircle.codepoint = 0x25CC;
   1.299 +  set_sea_properties (dottedcircle);
   1.300 +  dottedcircle.codepoint = dottedcircle_glyph;
   1.301 +
   1.302 +  buffer->clear_output ();
   1.303 +
   1.304 +  buffer->idx = 0;
   1.305 +  unsigned int last_syllable = 0;
   1.306 +  while (buffer->idx < buffer->len)
   1.307 +  {
   1.308 +    unsigned int syllable = buffer->cur().syllable();
   1.309 +    syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F);
   1.310 +    if (unlikely (last_syllable != syllable && syllable_type == broken_cluster))
   1.311 +    {
   1.312 +      last_syllable = syllable;
   1.313 +
   1.314 +      hb_glyph_info_t info = dottedcircle;
   1.315 +      info.cluster = buffer->cur().cluster;
   1.316 +      info.mask = buffer->cur().mask;
   1.317 +      info.syllable() = buffer->cur().syllable();
   1.318 +
   1.319 +      buffer->output_info (info);
   1.320 +    }
   1.321 +    else
   1.322 +      buffer->next_glyph ();
   1.323 +  }
   1.324 +
   1.325 +  buffer->swap_buffers ();
   1.326 +}
   1.327 +
   1.328 +static void
   1.329 +initial_reordering (const hb_ot_shape_plan_t *plan,
   1.330 +		    hb_font_t *font,
   1.331 +		    hb_buffer_t *buffer)
   1.332 +{
   1.333 +  insert_dotted_circles (plan, font, buffer);
   1.334 +
   1.335 +  hb_glyph_info_t *info = buffer->info;
   1.336 +  unsigned int count = buffer->len;
   1.337 +  if (unlikely (!count)) return;
   1.338 +  unsigned int last = 0;
   1.339 +  unsigned int last_syllable = info[0].syllable();
   1.340 +  for (unsigned int i = 1; i < count; i++)
   1.341 +    if (last_syllable != info[i].syllable()) {
   1.342 +      initial_reordering_syllable (plan, font->face, buffer, last, i);
   1.343 +      last = i;
   1.344 +      last_syllable = info[last].syllable();
   1.345 +    }
   1.346 +  initial_reordering_syllable (plan, font->face, buffer, last, count);
   1.347 +}
   1.348 +
   1.349 +static void
   1.350 +final_reordering (const hb_ot_shape_plan_t *plan,
   1.351 +		  hb_font_t *font HB_UNUSED,
   1.352 +		  hb_buffer_t *buffer)
   1.353 +{
   1.354 +  hb_glyph_info_t *info = buffer->info;
   1.355 +  unsigned int count = buffer->len;
   1.356 +
   1.357 +  /* Zero syllables now... */
   1.358 +  for (unsigned int i = 0; i < count; i++)
   1.359 +    info[i].syllable() = 0;
   1.360 +
   1.361 +  HB_BUFFER_DEALLOCATE_VAR (buffer, sea_category);
   1.362 +  HB_BUFFER_DEALLOCATE_VAR (buffer, sea_position);
   1.363 +}
   1.364 +
   1.365 +
   1.366 +const hb_ot_complex_shaper_t _hb_ot_complex_shaper_sea =
   1.367 +{
   1.368 +  "sea",
   1.369 +  collect_features_sea,
   1.370 +  override_features_sea,
   1.371 +  NULL, /* data_create */
   1.372 +  NULL, /* data_destroy */
   1.373 +  NULL, /* preprocess_text */
   1.374 +  HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
   1.375 +  NULL, /* decompose */
   1.376 +  NULL, /* compose */
   1.377 +  setup_masks_sea,
   1.378 +  HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
   1.379 +  false, /* fallback_position */
   1.380 +};

mercurial