hb-ot-shape-complex-arabic-fallback.hh 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright © 2012  Google, Inc.
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Google Author(s): Behdad Esfahbod
 */

#ifndef HB_OT_SHAPE_COMPLEX_ARABIC_FALLBACK_HH
#define HB_OT_SHAPE_COMPLEX_ARABIC_FALLBACK_HH

30
#include "hb.hh"
31

32
#include "hb-ot-shape.hh"
33 34 35
#include "hb-ot-layout-gsub-table.hh"


36 37
/* Features ordered the same as the entries in shaping_table rows,
 * followed by rlig.  Don't change. */
38 39 40 41 42 43 44 45 46 47
static const hb_tag_t arabic_fallback_features[] =
{
  HB_TAG('i','n','i','t'),
  HB_TAG('m','e','d','i'),
  HB_TAG('f','i','n','a'),
  HB_TAG('i','s','o','l'),
  HB_TAG('r','l','i','g'),
};

static OT::SubstLookup *
B
Behdad Esfahbod 已提交
48
arabic_fallback_synthesize_lookup_single (const hb_ot_shape_plan_t *plan HB_UNUSED,
49 50 51 52 53 54 55 56 57 58 59 60 61
					  hb_font_t *font,
					  unsigned int feature_index)
{
  OT::GlyphID glyphs[SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1];
  OT::GlyphID substitutes[SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1];
  unsigned int num_glyphs = 0;

  /* Populate arrays */
  for (hb_codepoint_t u = SHAPING_TABLE_FIRST; u < SHAPING_TABLE_LAST + 1; u++)
  {
    hb_codepoint_t s = shaping_table[u - SHAPING_TABLE_FIRST][feature_index];
    hb_codepoint_t u_glyph, s_glyph;

62
    if (!s ||
63 64 65
	!hb_font_get_glyph (font, u, 0, &u_glyph) ||
	!hb_font_get_glyph (font, s, 0, &s_glyph) ||
	u_glyph == s_glyph ||
66
	u_glyph > 0xFFFFu || s_glyph > 0xFFFFu)
67 68
      continue;

69 70
    glyphs[num_glyphs] = u_glyph;
    substitutes[num_glyphs] = s_glyph;
71 72 73 74

    num_glyphs++;
  }

75
  if (!num_glyphs)
B
Behdad Esfahbod 已提交
76
    return nullptr;
77

78
  /* Bubble-sort or something equally good!
79
   * May not be good-enough for presidential candidate interviews, but good-enough for us... */
B
Behdad Esfahbod 已提交
80 81 82
  hb_stable_sort (&glyphs[0], num_glyphs,
		  (int(*)(const OT::HBUINT16*, const OT::HBUINT16 *)) OT::GlyphID::cmp,
		  &substitutes[0]);
83 84 85 86


  /* Each glyph takes four bytes max, and there's some overhead. */
  char buf[(SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1) * 4 + 128];
87
  hb_serialize_context_t c (buf, sizeof (buf));
88 89 90
  OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> ();
  bool ret = lookup->serialize_single (&c,
				       OT::LookupFlag::IgnoreMarks,
B
Behdad Esfahbod 已提交
91
				       hb_sorted_array (glyphs, num_glyphs),
B
Behdad Esfahbod 已提交
92
				       hb_array (substitutes, num_glyphs));
93 94 95
  c.end_serialize ();
  /* TODO sanitize the results? */

B
Behdad Esfahbod 已提交
96
  return ret ? c.copy<OT::SubstLookup> () : nullptr;
97 98 99
}

static OT::SubstLookup *
B
Behdad Esfahbod 已提交
100
arabic_fallback_synthesize_lookup_ligature (const hb_ot_shape_plan_t *plan HB_UNUSED,
101 102 103 104 105 106 107 108
					    hb_font_t *font)
{
  OT::GlyphID first_glyphs[ARRAY_LENGTH_CONST (ligature_table)];
  unsigned int first_glyphs_indirection[ARRAY_LENGTH_CONST (ligature_table)];
  unsigned int ligature_per_first_glyph_count_list[ARRAY_LENGTH_CONST (first_glyphs)];
  unsigned int num_first_glyphs = 0;

  /* We know that all our ligatures are 2-component */
B
Minor  
Behdad Esfahbod 已提交
109 110 111
  OT::GlyphID ligature_list[ARRAY_LENGTH_CONST (first_glyphs) * ARRAY_LENGTH_CONST(ligature_table[0].ligatures)];
  unsigned int component_count_list[ARRAY_LENGTH_CONST (ligature_list)];
  OT::GlyphID component_list[ARRAY_LENGTH_CONST (ligature_list) * 1/* One extra component per ligature */];
112 113 114 115 116 117 118 119 120 121 122
  unsigned int num_ligatures = 0;

  /* Populate arrays */

  /* Sort out the first-glyphs */
  for (unsigned int first_glyph_idx = 0; first_glyph_idx < ARRAY_LENGTH (first_glyphs); first_glyph_idx++)
  {
    hb_codepoint_t first_u = ligature_table[first_glyph_idx].first;
    hb_codepoint_t first_glyph;
    if (!hb_font_get_glyph (font, first_u, 0, &first_glyph))
      continue;
123
    first_glyphs[num_first_glyphs] = first_glyph;
124 125 126 127
    ligature_per_first_glyph_count_list[num_first_glyphs] = 0;
    first_glyphs_indirection[num_first_glyphs] = first_glyph_idx;
    num_first_glyphs++;
  }
B
Behdad Esfahbod 已提交
128 129 130
  hb_stable_sort (&first_glyphs[0], num_first_glyphs,
		  (int(*)(const OT::HBUINT16*, const OT::HBUINT16 *)) OT::GlyphID::cmp,
		  &first_glyphs_indirection[0]);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

  /* Now that the first-glyphs are sorted, walk again, populate ligatures. */
  for (unsigned int i = 0; i < num_first_glyphs; i++)
  {
    unsigned int first_glyph_idx = first_glyphs_indirection[i];

    for (unsigned int second_glyph_idx = 0; second_glyph_idx < ARRAY_LENGTH (ligature_table[0].ligatures); second_glyph_idx++)
    {
      hb_codepoint_t second_u   = ligature_table[first_glyph_idx].ligatures[second_glyph_idx].second;
      hb_codepoint_t ligature_u = ligature_table[first_glyph_idx].ligatures[second_glyph_idx].ligature;
      hb_codepoint_t second_glyph, ligature_glyph;
      if (!second_u ||
	  !hb_font_get_glyph (font, second_u,   0, &second_glyph) ||
	  !hb_font_get_glyph (font, ligature_u, 0, &ligature_glyph))
	continue;

      ligature_per_first_glyph_count_list[i]++;

149
      ligature_list[num_ligatures] = ligature_glyph;
150
      component_count_list[num_ligatures] = 2;
151
      component_list[num_ligatures] = second_glyph;
152 153 154 155
      num_ligatures++;
    }
  }

156
  if (!num_ligatures)
B
Behdad Esfahbod 已提交
157
    return nullptr;
158

159

B
Minor  
Behdad Esfahbod 已提交
160 161
  /* 16 bytes per ligature ought to be enough... */
  char buf[ARRAY_LENGTH_CONST (ligature_list) * 16 + 128];
162
  hb_serialize_context_t c (buf, sizeof (buf));
163 164 165
  OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> ();
  bool ret = lookup->serialize_ligature (&c,
					 OT::LookupFlag::IgnoreMarks,
B
Behdad Esfahbod 已提交
166
					 hb_sorted_array (first_glyphs, num_first_glyphs),
B
Behdad Esfahbod 已提交
167 168 169 170
					 hb_array (ligature_per_first_glyph_count_list, num_first_glyphs),
					 hb_array (ligature_list, num_ligatures),
					 hb_array (component_count_list, num_ligatures),
					 hb_array (component_list, num_ligatures));
171 172 173
  c.end_serialize ();
  /* TODO sanitize the results? */

B
Behdad Esfahbod 已提交
174
  return ret ? c.copy<OT::SubstLookup> () : nullptr;
175 176 177 178 179 180 181 182 183 184 185 186 187
}

static OT::SubstLookup *
arabic_fallback_synthesize_lookup (const hb_ot_shape_plan_t *plan,
				   hb_font_t *font,
				   unsigned int feature_index)
{
  if (feature_index < 4)
    return arabic_fallback_synthesize_lookup_single (plan, font, feature_index);
  else
    return arabic_fallback_synthesize_lookup_ligature (plan, font);
}

188
#define ARABIC_FALLBACK_MAX_LOOKUPS 5
189

190 191
struct arabic_fallback_plan_t
{
192 193 194
  unsigned int num_lookups;
  bool free_lookups;

195 196
  hb_mask_t mask_array[ARABIC_FALLBACK_MAX_LOOKUPS];
  OT::SubstLookup *lookup_array[ARABIC_FALLBACK_MAX_LOOKUPS];
B
Behdad Esfahbod 已提交
197
  OT::hb_ot_layout_lookup_accelerator_t accel_array[ARABIC_FALLBACK_MAX_LOOKUPS];
198 199
};

K
Ken Brown 已提交
200
#if defined(_WIN32) && !defined(HB_NO_WIN1256)
201 202 203 204 205 206 207
#define HB_WITH_WIN1256
#endif

#ifdef HB_WITH_WIN1256
#include "hb-ot-shape-complex-arabic-win1256.hh"
#endif

208 209
struct ManifestLookup
{
B
Minor  
Behdad Esfahbod 已提交
210
  public:
211
  OT::Tag tag;
212
  OT::OffsetTo<OT::SubstLookup> lookupOffset;
B
Minor  
Behdad Esfahbod 已提交
213 214
  public:
  DEFINE_SIZE_STATIC (6);
215 216 217 218
};
typedef OT::ArrayOf<ManifestLookup> Manifest;

static bool
219 220 221
arabic_fallback_plan_init_win1256 (arabic_fallback_plan_t *fallback_plan HB_UNUSED,
				   const hb_ot_shape_plan_t *plan HB_UNUSED,
				   hb_font_t *font HB_UNUSED)
222 223 224 225 226 227 228 229 230 231 232 233
{
#ifdef HB_WITH_WIN1256
  /* Does this font look like it's Windows-1256-encoded? */
  hb_codepoint_t g;
  if (!(hb_font_get_glyph (font, 0x0627u, 0, &g) && g == 199 /* ALEF */ &&
	hb_font_get_glyph (font, 0x0644u, 0, &g) && g == 225 /* LAM */ &&
	hb_font_get_glyph (font, 0x0649u, 0, &g) && g == 236 /* ALEF MAKSURA */ &&
	hb_font_get_glyph (font, 0x064Au, 0, &g) && g == 237 /* YEH */ &&
	hb_font_get_glyph (font, 0x0652u, 0, &g) && g == 250 /* SUKUN */))
    return false;

  const Manifest &manifest = reinterpret_cast<const Manifest&> (arabic_win1256_gsub_lookups.manifest);
234 235
  static_assert (sizeof (arabic_win1256_gsub_lookups.manifestData) / sizeof (ManifestLookup)
		 <= ARABIC_FALLBACK_MAX_LOOKUPS, "");
236 237 238 239 240 241
  /* TODO sanitize the table? */

  unsigned j = 0;
  unsigned int count = manifest.len;
  for (unsigned int i = 0; i < count; i++)
  {
242
    fallback_plan->mask_array[j] = plan->map.get_1_mask (manifest[i].tag);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    if (fallback_plan->mask_array[j])
    {
      fallback_plan->lookup_array[j] = const_cast<OT::SubstLookup*> (&(&manifest+manifest[i].lookupOffset));
      if (fallback_plan->lookup_array[j])
      {
	fallback_plan->accel_array[j].init (*fallback_plan->lookup_array[j]);
	j++;
      }
    }
  }

  fallback_plan->num_lookups = j;
  fallback_plan->free_lookups = false;

  return j > 0;
#else
  return false;
#endif
}

static bool
arabic_fallback_plan_init_unicode (arabic_fallback_plan_t *fallback_plan,
				   const hb_ot_shape_plan_t *plan,
				   hb_font_t *font)
{
268
  static_assert ((ARRAY_LENGTH_CONST(arabic_fallback_features) <= ARABIC_FALLBACK_MAX_LOOKUPS), "");
269
  unsigned int j = 0;
270
  for (unsigned int i = 0; i < ARRAY_LENGTH(arabic_fallback_features) ; i++)
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
  {
    fallback_plan->mask_array[j] = plan->map.get_1_mask (arabic_fallback_features[i]);
    if (fallback_plan->mask_array[j])
    {
      fallback_plan->lookup_array[j] = arabic_fallback_synthesize_lookup (plan, font, i);
      if (fallback_plan->lookup_array[j])
      {
	fallback_plan->accel_array[j].init (*fallback_plan->lookup_array[j]);
	j++;
      }
    }
  }

  fallback_plan->num_lookups = j;
  fallback_plan->free_lookups = true;

  return j > 0;
}

290 291 292 293 294 295
static arabic_fallback_plan_t *
arabic_fallback_plan_create (const hb_ot_shape_plan_t *plan,
			     hb_font_t *font)
{
  arabic_fallback_plan_t *fallback_plan = (arabic_fallback_plan_t *) calloc (1, sizeof (arabic_fallback_plan_t));
  if (unlikely (!fallback_plan))
296
    return const_cast<arabic_fallback_plan_t *> (&Null(arabic_fallback_plan_t));
297

298 299 300 301 302 303 304
  fallback_plan->num_lookups = 0;
  fallback_plan->free_lookups = false;

  /* Try synthesizing GSUB table using Unicode Arabic Presentation Forms,
   * in case the font has cmap entries for the presentation-forms characters. */
  if (arabic_fallback_plan_init_unicode (fallback_plan, plan, font))
    return fallback_plan;
305

306 307 308 309 310
  /* See if this looks like a Windows-1256-encoded font.  If it does, use a
   * hand-coded GSUB table. */
  if (arabic_fallback_plan_init_win1256 (fallback_plan, plan, font))
    return fallback_plan;

311
  assert (fallback_plan->num_lookups == 0);
312
  free (fallback_plan);
313
  return const_cast<arabic_fallback_plan_t *> (&Null(arabic_fallback_plan_t));
314 315 316 317 318
}

static void
arabic_fallback_plan_destroy (arabic_fallback_plan_t *fallback_plan)
{
319
  if (!fallback_plan || fallback_plan->num_lookups == 0)
320 321
    return;

322
  for (unsigned int i = 0; i < fallback_plan->num_lookups; i++)
323
    if (fallback_plan->lookup_array[i])
324
    {
325
      fallback_plan->accel_array[i].fini ();
326 327
      if (fallback_plan->free_lookups)
	free (fallback_plan->lookup_array[i]);
328
    }
329 330 331 332 333 334 335 336 337

  free (fallback_plan);
}

static void
arabic_fallback_plan_shape (arabic_fallback_plan_t *fallback_plan,
			    hb_font_t *font,
			    hb_buffer_t *buffer)
{
338
  OT::hb_ot_apply_context_t c (0, font, buffer);
339
  for (unsigned int i = 0; i < fallback_plan->num_lookups; i++)
340
    if (fallback_plan->lookup_array[i]) {
B
Behdad Esfahbod 已提交
341 342 343 344
      c.set_lookup_mask (fallback_plan->mask_array[i]);
      hb_ot_layout_substitute_lookup (&c,
				      *fallback_plan->lookup_array[i],
				      fallback_plan->accel_array[i]);
345 346 347 348 349
    }
}


#endif /* HB_OT_SHAPE_COMPLEX_ARABIC_FALLBACK_HH */