gfx/harfbuzz/src/hb-ot-shape-complex-arabic-fallback.hh

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-arabic-fallback.hh	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,260 @@
     1.4 +/*
     1.5 + * Copyright © 2012  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 +#ifndef HB_OT_SHAPE_COMPLEX_ARABIC_FALLBACK_HH
    1.31 +#define HB_OT_SHAPE_COMPLEX_ARABIC_FALLBACK_HH
    1.32 +
    1.33 +#include "hb-private.hh"
    1.34 +
    1.35 +#include "hb-ot-shape-private.hh"
    1.36 +#include "hb-ot-layout-gsub-table.hh"
    1.37 +
    1.38 +
    1.39 +static const hb_tag_t arabic_fallback_features[] =
    1.40 +{
    1.41 +  HB_TAG('i','n','i','t'),
    1.42 +  HB_TAG('m','e','d','i'),
    1.43 +  HB_TAG('f','i','n','a'),
    1.44 +  HB_TAG('i','s','o','l'),
    1.45 +  HB_TAG('r','l','i','g'),
    1.46 +};
    1.47 +
    1.48 +/* Same order as the fallback feature array */
    1.49 +enum {
    1.50 +  FALLBACK_INIT,
    1.51 +  FALLBACK_MEDI,
    1.52 +  FALLBACK_FINA,
    1.53 +  FALLBACK_ISOL,
    1.54 +  FALLBACK_RLIG,
    1.55 +  ARABIC_NUM_FALLBACK_FEATURES
    1.56 +};
    1.57 +
    1.58 +static OT::SubstLookup *
    1.59 +arabic_fallback_synthesize_lookup_single (const hb_ot_shape_plan_t *plan HB_UNUSED,
    1.60 +					  hb_font_t *font,
    1.61 +					  unsigned int feature_index)
    1.62 +{
    1.63 +  OT::GlyphID glyphs[SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1];
    1.64 +  OT::GlyphID substitutes[SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1];
    1.65 +  unsigned int num_glyphs = 0;
    1.66 +
    1.67 +  /* Populate arrays */
    1.68 +  for (hb_codepoint_t u = SHAPING_TABLE_FIRST; u < SHAPING_TABLE_LAST + 1; u++)
    1.69 +  {
    1.70 +    hb_codepoint_t s = shaping_table[u - SHAPING_TABLE_FIRST][feature_index];
    1.71 +    hb_codepoint_t u_glyph, s_glyph;
    1.72 +
    1.73 +    if (!s ||
    1.74 +	!hb_font_get_glyph (font, u, 0, &u_glyph) ||
    1.75 +	!hb_font_get_glyph (font, s, 0, &s_glyph) ||
    1.76 +	u_glyph == s_glyph ||
    1.77 +	u_glyph > 0xFFFF || s_glyph > 0xFFFF)
    1.78 +      continue;
    1.79 +
    1.80 +    glyphs[num_glyphs].set (u_glyph);
    1.81 +    substitutes[num_glyphs].set (s_glyph);
    1.82 +
    1.83 +    num_glyphs++;
    1.84 +  }
    1.85 +
    1.86 +  /* Bubble-sort!
    1.87 +   * May not be good-enough for presidential candidate interviews, but good-enough for us... */
    1.88 +  hb_bubble_sort (&glyphs[0], num_glyphs, OT::GlyphID::cmp, &substitutes[0]);
    1.89 +
    1.90 +  OT::Supplier<OT::GlyphID> glyphs_supplier      (glyphs, num_glyphs);
    1.91 +  OT::Supplier<OT::GlyphID> substitutes_supplier (substitutes, num_glyphs);
    1.92 +
    1.93 +  /* Each glyph takes four bytes max, and there's some overhead. */
    1.94 +  char buf[(SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1) * 4 + 128];
    1.95 +  OT::hb_serialize_context_t c (buf, sizeof (buf));
    1.96 +  OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> ();
    1.97 +  bool ret = lookup->serialize_single (&c,
    1.98 +				       OT::LookupFlag::IgnoreMarks,
    1.99 +				       glyphs_supplier,
   1.100 +				       substitutes_supplier,
   1.101 +				       num_glyphs);
   1.102 +  c.end_serialize ();
   1.103 +  /* TODO sanitize the results? */
   1.104 +
   1.105 +  return ret ? c.copy<OT::SubstLookup> () : NULL;
   1.106 +}
   1.107 +
   1.108 +static OT::SubstLookup *
   1.109 +arabic_fallback_synthesize_lookup_ligature (const hb_ot_shape_plan_t *plan HB_UNUSED,
   1.110 +					    hb_font_t *font)
   1.111 +{
   1.112 +  OT::GlyphID first_glyphs[ARRAY_LENGTH_CONST (ligature_table)];
   1.113 +  unsigned int first_glyphs_indirection[ARRAY_LENGTH_CONST (ligature_table)];
   1.114 +  unsigned int ligature_per_first_glyph_count_list[ARRAY_LENGTH_CONST (first_glyphs)];
   1.115 +  unsigned int num_first_glyphs = 0;
   1.116 +
   1.117 +  /* We know that all our ligatures are 2-component */
   1.118 +  OT::GlyphID ligature_list[ARRAY_LENGTH_CONST (first_glyphs) * ARRAY_LENGTH_CONST(ligature_table[0].ligatures)];
   1.119 +  unsigned int component_count_list[ARRAY_LENGTH_CONST (ligature_list)];
   1.120 +  OT::GlyphID component_list[ARRAY_LENGTH_CONST (ligature_list) * 1/* One extra component per ligature */];
   1.121 +  unsigned int num_ligatures = 0;
   1.122 +
   1.123 +  /* Populate arrays */
   1.124 +
   1.125 +  /* Sort out the first-glyphs */
   1.126 +  for (unsigned int first_glyph_idx = 0; first_glyph_idx < ARRAY_LENGTH (first_glyphs); first_glyph_idx++)
   1.127 +  {
   1.128 +    hb_codepoint_t first_u = ligature_table[first_glyph_idx].first;
   1.129 +    hb_codepoint_t first_glyph;
   1.130 +    if (!hb_font_get_glyph (font, first_u, 0, &first_glyph))
   1.131 +      continue;
   1.132 +    first_glyphs[num_first_glyphs].set (first_glyph);
   1.133 +    ligature_per_first_glyph_count_list[num_first_glyphs] = 0;
   1.134 +    first_glyphs_indirection[num_first_glyphs] = first_glyph_idx;
   1.135 +    num_first_glyphs++;
   1.136 +  }
   1.137 +  hb_bubble_sort (&first_glyphs[0], num_first_glyphs, OT::GlyphID::cmp, &first_glyphs_indirection[0]);
   1.138 +
   1.139 +  /* Now that the first-glyphs are sorted, walk again, populate ligatures. */
   1.140 +  for (unsigned int i = 0; i < num_first_glyphs; i++)
   1.141 +  {
   1.142 +    unsigned int first_glyph_idx = first_glyphs_indirection[i];
   1.143 +
   1.144 +    for (unsigned int second_glyph_idx = 0; second_glyph_idx < ARRAY_LENGTH (ligature_table[0].ligatures); second_glyph_idx++)
   1.145 +    {
   1.146 +      hb_codepoint_t second_u   = ligature_table[first_glyph_idx].ligatures[second_glyph_idx].second;
   1.147 +      hb_codepoint_t ligature_u = ligature_table[first_glyph_idx].ligatures[second_glyph_idx].ligature;
   1.148 +      hb_codepoint_t second_glyph, ligature_glyph;
   1.149 +      if (!second_u ||
   1.150 +	  !hb_font_get_glyph (font, second_u,   0, &second_glyph) ||
   1.151 +	  !hb_font_get_glyph (font, ligature_u, 0, &ligature_glyph))
   1.152 +	continue;
   1.153 +
   1.154 +      ligature_per_first_glyph_count_list[i]++;
   1.155 +
   1.156 +      ligature_list[num_ligatures].set (ligature_glyph);
   1.157 +      component_count_list[num_ligatures] = 2;
   1.158 +      component_list[num_ligatures].set (second_glyph);
   1.159 +      num_ligatures++;
   1.160 +    }
   1.161 +  }
   1.162 +
   1.163 +  OT::Supplier<OT::GlyphID>   first_glyphs_supplier                      (first_glyphs, num_first_glyphs);
   1.164 +  OT::Supplier<unsigned int > ligature_per_first_glyph_count_supplier    (ligature_per_first_glyph_count_list, num_first_glyphs);
   1.165 +  OT::Supplier<OT::GlyphID>   ligatures_supplier                         (ligature_list, num_ligatures);
   1.166 +  OT::Supplier<unsigned int > component_count_supplier                   (component_count_list, num_ligatures);
   1.167 +  OT::Supplier<OT::GlyphID>   component_supplier                         (component_list, num_ligatures);
   1.168 +
   1.169 +  /* 16 bytes per ligature ought to be enough... */
   1.170 +  char buf[ARRAY_LENGTH_CONST (ligature_list) * 16 + 128];
   1.171 +  OT::hb_serialize_context_t c (buf, sizeof (buf));
   1.172 +  OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> ();
   1.173 +  bool ret = lookup->serialize_ligature (&c,
   1.174 +					 OT::LookupFlag::IgnoreMarks,
   1.175 +					 first_glyphs_supplier,
   1.176 +					 ligature_per_first_glyph_count_supplier,
   1.177 +					 num_first_glyphs,
   1.178 +					 ligatures_supplier,
   1.179 +					 component_count_supplier,
   1.180 +					 component_supplier);
   1.181 +
   1.182 +  c.end_serialize ();
   1.183 +  /* TODO sanitize the results? */
   1.184 +
   1.185 +  return ret ? c.copy<OT::SubstLookup> () : NULL;
   1.186 +}
   1.187 +
   1.188 +static OT::SubstLookup *
   1.189 +arabic_fallback_synthesize_lookup (const hb_ot_shape_plan_t *plan,
   1.190 +				   hb_font_t *font,
   1.191 +				   unsigned int feature_index)
   1.192 +{
   1.193 +  if (feature_index < 4)
   1.194 +    return arabic_fallback_synthesize_lookup_single (plan, font, feature_index);
   1.195 +  else
   1.196 +    return arabic_fallback_synthesize_lookup_ligature (plan, font);
   1.197 +}
   1.198 +
   1.199 +struct arabic_fallback_plan_t
   1.200 +{
   1.201 +  ASSERT_POD ();
   1.202 +
   1.203 +  hb_mask_t mask_array[ARABIC_NUM_FALLBACK_FEATURES];
   1.204 +  OT::SubstLookup *lookup_array[ARABIC_NUM_FALLBACK_FEATURES];
   1.205 +  hb_ot_layout_lookup_accelerator_t accel_array[ARABIC_NUM_FALLBACK_FEATURES];
   1.206 +};
   1.207 +
   1.208 +static const arabic_fallback_plan_t arabic_fallback_plan_nil = {};
   1.209 +
   1.210 +static arabic_fallback_plan_t *
   1.211 +arabic_fallback_plan_create (const hb_ot_shape_plan_t *plan,
   1.212 +			     hb_font_t *font)
   1.213 +{
   1.214 +  arabic_fallback_plan_t *fallback_plan = (arabic_fallback_plan_t *) calloc (1, sizeof (arabic_fallback_plan_t));
   1.215 +  if (unlikely (!fallback_plan))
   1.216 +    return const_cast<arabic_fallback_plan_t *> (&arabic_fallback_plan_nil);
   1.217 +
   1.218 +  for (unsigned int i = 0; i < ARABIC_NUM_FALLBACK_FEATURES; i++)
   1.219 +  {
   1.220 +    fallback_plan->mask_array[i] = plan->map.get_1_mask (arabic_fallback_features[i]);
   1.221 +    if (fallback_plan->mask_array[i]) {
   1.222 +      fallback_plan->lookup_array[i] = arabic_fallback_synthesize_lookup (plan, font, i);
   1.223 +      if (fallback_plan->lookup_array[i])
   1.224 +	fallback_plan->accel_array[i].init (*fallback_plan->lookup_array[i]);
   1.225 +    }
   1.226 +  }
   1.227 +
   1.228 +  return fallback_plan;
   1.229 +}
   1.230 +
   1.231 +static void
   1.232 +arabic_fallback_plan_destroy (arabic_fallback_plan_t *fallback_plan)
   1.233 +{
   1.234 +  if (!fallback_plan || fallback_plan == &arabic_fallback_plan_nil)
   1.235 +    return;
   1.236 +
   1.237 +  for (unsigned int i = 0; i < ARABIC_NUM_FALLBACK_FEATURES; i++)
   1.238 +    if (fallback_plan->lookup_array[i])
   1.239 +    {
   1.240 +      fallback_plan->accel_array[i].fini (fallback_plan->lookup_array[i]);
   1.241 +      free (fallback_plan->lookup_array[i]);
   1.242 +    }
   1.243 +
   1.244 +  free (fallback_plan);
   1.245 +}
   1.246 +
   1.247 +static void
   1.248 +arabic_fallback_plan_shape (arabic_fallback_plan_t *fallback_plan,
   1.249 +			    hb_font_t *font,
   1.250 +			    hb_buffer_t *buffer)
   1.251 +{
   1.252 +  OT::hb_apply_context_t c (0, font, buffer);
   1.253 +  for (unsigned int i = 0; i < ARABIC_NUM_FALLBACK_FEATURES; i++)
   1.254 +    if (fallback_plan->lookup_array[i]) {
   1.255 +      c.set_lookup_mask (fallback_plan->mask_array[i]);
   1.256 +      hb_ot_layout_substitute_lookup (&c,
   1.257 +				      *fallback_plan->lookup_array[i],
   1.258 +				      fallback_plan->accel_array[i]);
   1.259 +    }
   1.260 +}
   1.261 +
   1.262 +
   1.263 +#endif /* HB_OT_SHAPE_COMPLEX_ARABIC_FALLBACK_HH */

mercurial