michael@0: /* michael@0: * Copyright © 2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #ifndef HB_OT_SHAPE_COMPLEX_ARABIC_FALLBACK_HH michael@0: #define HB_OT_SHAPE_COMPLEX_ARABIC_FALLBACK_HH michael@0: michael@0: #include "hb-private.hh" michael@0: michael@0: #include "hb-ot-shape-private.hh" michael@0: #include "hb-ot-layout-gsub-table.hh" michael@0: michael@0: michael@0: static const hb_tag_t arabic_fallback_features[] = michael@0: { michael@0: HB_TAG('i','n','i','t'), michael@0: HB_TAG('m','e','d','i'), michael@0: HB_TAG('f','i','n','a'), michael@0: HB_TAG('i','s','o','l'), michael@0: HB_TAG('r','l','i','g'), michael@0: }; michael@0: michael@0: /* Same order as the fallback feature array */ michael@0: enum { michael@0: FALLBACK_INIT, michael@0: FALLBACK_MEDI, michael@0: FALLBACK_FINA, michael@0: FALLBACK_ISOL, michael@0: FALLBACK_RLIG, michael@0: ARABIC_NUM_FALLBACK_FEATURES michael@0: }; michael@0: michael@0: static OT::SubstLookup * michael@0: arabic_fallback_synthesize_lookup_single (const hb_ot_shape_plan_t *plan HB_UNUSED, michael@0: hb_font_t *font, michael@0: unsigned int feature_index) michael@0: { michael@0: OT::GlyphID glyphs[SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1]; michael@0: OT::GlyphID substitutes[SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1]; michael@0: unsigned int num_glyphs = 0; michael@0: michael@0: /* Populate arrays */ michael@0: for (hb_codepoint_t u = SHAPING_TABLE_FIRST; u < SHAPING_TABLE_LAST + 1; u++) michael@0: { michael@0: hb_codepoint_t s = shaping_table[u - SHAPING_TABLE_FIRST][feature_index]; michael@0: hb_codepoint_t u_glyph, s_glyph; michael@0: michael@0: if (!s || michael@0: !hb_font_get_glyph (font, u, 0, &u_glyph) || michael@0: !hb_font_get_glyph (font, s, 0, &s_glyph) || michael@0: u_glyph == s_glyph || michael@0: u_glyph > 0xFFFF || s_glyph > 0xFFFF) michael@0: continue; michael@0: michael@0: glyphs[num_glyphs].set (u_glyph); michael@0: substitutes[num_glyphs].set (s_glyph); michael@0: michael@0: num_glyphs++; michael@0: } michael@0: michael@0: /* Bubble-sort! michael@0: * May not be good-enough for presidential candidate interviews, but good-enough for us... */ michael@0: hb_bubble_sort (&glyphs[0], num_glyphs, OT::GlyphID::cmp, &substitutes[0]); michael@0: michael@0: OT::Supplier glyphs_supplier (glyphs, num_glyphs); michael@0: OT::Supplier substitutes_supplier (substitutes, num_glyphs); michael@0: michael@0: /* Each glyph takes four bytes max, and there's some overhead. */ michael@0: char buf[(SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1) * 4 + 128]; michael@0: OT::hb_serialize_context_t c (buf, sizeof (buf)); michael@0: OT::SubstLookup *lookup = c.start_serialize (); michael@0: bool ret = lookup->serialize_single (&c, michael@0: OT::LookupFlag::IgnoreMarks, michael@0: glyphs_supplier, michael@0: substitutes_supplier, michael@0: num_glyphs); michael@0: c.end_serialize (); michael@0: /* TODO sanitize the results? */ michael@0: michael@0: return ret ? c.copy () : NULL; michael@0: } michael@0: michael@0: static OT::SubstLookup * michael@0: arabic_fallback_synthesize_lookup_ligature (const hb_ot_shape_plan_t *plan HB_UNUSED, michael@0: hb_font_t *font) michael@0: { michael@0: OT::GlyphID first_glyphs[ARRAY_LENGTH_CONST (ligature_table)]; michael@0: unsigned int first_glyphs_indirection[ARRAY_LENGTH_CONST (ligature_table)]; michael@0: unsigned int ligature_per_first_glyph_count_list[ARRAY_LENGTH_CONST (first_glyphs)]; michael@0: unsigned int num_first_glyphs = 0; michael@0: michael@0: /* We know that all our ligatures are 2-component */ michael@0: OT::GlyphID ligature_list[ARRAY_LENGTH_CONST (first_glyphs) * ARRAY_LENGTH_CONST(ligature_table[0].ligatures)]; michael@0: unsigned int component_count_list[ARRAY_LENGTH_CONST (ligature_list)]; michael@0: OT::GlyphID component_list[ARRAY_LENGTH_CONST (ligature_list) * 1/* One extra component per ligature */]; michael@0: unsigned int num_ligatures = 0; michael@0: michael@0: /* Populate arrays */ michael@0: michael@0: /* Sort out the first-glyphs */ michael@0: for (unsigned int first_glyph_idx = 0; first_glyph_idx < ARRAY_LENGTH (first_glyphs); first_glyph_idx++) michael@0: { michael@0: hb_codepoint_t first_u = ligature_table[first_glyph_idx].first; michael@0: hb_codepoint_t first_glyph; michael@0: if (!hb_font_get_glyph (font, first_u, 0, &first_glyph)) michael@0: continue; michael@0: first_glyphs[num_first_glyphs].set (first_glyph); michael@0: ligature_per_first_glyph_count_list[num_first_glyphs] = 0; michael@0: first_glyphs_indirection[num_first_glyphs] = first_glyph_idx; michael@0: num_first_glyphs++; michael@0: } michael@0: hb_bubble_sort (&first_glyphs[0], num_first_glyphs, OT::GlyphID::cmp, &first_glyphs_indirection[0]); michael@0: michael@0: /* Now that the first-glyphs are sorted, walk again, populate ligatures. */ michael@0: for (unsigned int i = 0; i < num_first_glyphs; i++) michael@0: { michael@0: unsigned int first_glyph_idx = first_glyphs_indirection[i]; michael@0: michael@0: for (unsigned int second_glyph_idx = 0; second_glyph_idx < ARRAY_LENGTH (ligature_table[0].ligatures); second_glyph_idx++) michael@0: { michael@0: hb_codepoint_t second_u = ligature_table[first_glyph_idx].ligatures[second_glyph_idx].second; michael@0: hb_codepoint_t ligature_u = ligature_table[first_glyph_idx].ligatures[second_glyph_idx].ligature; michael@0: hb_codepoint_t second_glyph, ligature_glyph; michael@0: if (!second_u || michael@0: !hb_font_get_glyph (font, second_u, 0, &second_glyph) || michael@0: !hb_font_get_glyph (font, ligature_u, 0, &ligature_glyph)) michael@0: continue; michael@0: michael@0: ligature_per_first_glyph_count_list[i]++; michael@0: michael@0: ligature_list[num_ligatures].set (ligature_glyph); michael@0: component_count_list[num_ligatures] = 2; michael@0: component_list[num_ligatures].set (second_glyph); michael@0: num_ligatures++; michael@0: } michael@0: } michael@0: michael@0: OT::Supplier first_glyphs_supplier (first_glyphs, num_first_glyphs); michael@0: OT::Supplier ligature_per_first_glyph_count_supplier (ligature_per_first_glyph_count_list, num_first_glyphs); michael@0: OT::Supplier ligatures_supplier (ligature_list, num_ligatures); michael@0: OT::Supplier component_count_supplier (component_count_list, num_ligatures); michael@0: OT::Supplier component_supplier (component_list, num_ligatures); michael@0: michael@0: /* 16 bytes per ligature ought to be enough... */ michael@0: char buf[ARRAY_LENGTH_CONST (ligature_list) * 16 + 128]; michael@0: OT::hb_serialize_context_t c (buf, sizeof (buf)); michael@0: OT::SubstLookup *lookup = c.start_serialize (); michael@0: bool ret = lookup->serialize_ligature (&c, michael@0: OT::LookupFlag::IgnoreMarks, michael@0: first_glyphs_supplier, michael@0: ligature_per_first_glyph_count_supplier, michael@0: num_first_glyphs, michael@0: ligatures_supplier, michael@0: component_count_supplier, michael@0: component_supplier); michael@0: michael@0: c.end_serialize (); michael@0: /* TODO sanitize the results? */ michael@0: michael@0: return ret ? c.copy () : NULL; michael@0: } michael@0: michael@0: static OT::SubstLookup * michael@0: arabic_fallback_synthesize_lookup (const hb_ot_shape_plan_t *plan, michael@0: hb_font_t *font, michael@0: unsigned int feature_index) michael@0: { michael@0: if (feature_index < 4) michael@0: return arabic_fallback_synthesize_lookup_single (plan, font, feature_index); michael@0: else michael@0: return arabic_fallback_synthesize_lookup_ligature (plan, font); michael@0: } michael@0: michael@0: struct arabic_fallback_plan_t michael@0: { michael@0: ASSERT_POD (); michael@0: michael@0: hb_mask_t mask_array[ARABIC_NUM_FALLBACK_FEATURES]; michael@0: OT::SubstLookup *lookup_array[ARABIC_NUM_FALLBACK_FEATURES]; michael@0: hb_ot_layout_lookup_accelerator_t accel_array[ARABIC_NUM_FALLBACK_FEATURES]; michael@0: }; michael@0: michael@0: static const arabic_fallback_plan_t arabic_fallback_plan_nil = {}; michael@0: michael@0: static arabic_fallback_plan_t * michael@0: arabic_fallback_plan_create (const hb_ot_shape_plan_t *plan, michael@0: hb_font_t *font) michael@0: { michael@0: arabic_fallback_plan_t *fallback_plan = (arabic_fallback_plan_t *) calloc (1, sizeof (arabic_fallback_plan_t)); michael@0: if (unlikely (!fallback_plan)) michael@0: return const_cast (&arabic_fallback_plan_nil); michael@0: michael@0: for (unsigned int i = 0; i < ARABIC_NUM_FALLBACK_FEATURES; i++) michael@0: { michael@0: fallback_plan->mask_array[i] = plan->map.get_1_mask (arabic_fallback_features[i]); michael@0: if (fallback_plan->mask_array[i]) { michael@0: fallback_plan->lookup_array[i] = arabic_fallback_synthesize_lookup (plan, font, i); michael@0: if (fallback_plan->lookup_array[i]) michael@0: fallback_plan->accel_array[i].init (*fallback_plan->lookup_array[i]); michael@0: } michael@0: } michael@0: michael@0: return fallback_plan; michael@0: } michael@0: michael@0: static void michael@0: arabic_fallback_plan_destroy (arabic_fallback_plan_t *fallback_plan) michael@0: { michael@0: if (!fallback_plan || fallback_plan == &arabic_fallback_plan_nil) michael@0: return; michael@0: michael@0: for (unsigned int i = 0; i < ARABIC_NUM_FALLBACK_FEATURES; i++) michael@0: if (fallback_plan->lookup_array[i]) michael@0: { michael@0: fallback_plan->accel_array[i].fini (fallback_plan->lookup_array[i]); michael@0: free (fallback_plan->lookup_array[i]); michael@0: } michael@0: michael@0: free (fallback_plan); michael@0: } michael@0: michael@0: static void michael@0: arabic_fallback_plan_shape (arabic_fallback_plan_t *fallback_plan, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer) michael@0: { michael@0: OT::hb_apply_context_t c (0, font, buffer); michael@0: for (unsigned int i = 0; i < ARABIC_NUM_FALLBACK_FEATURES; i++) michael@0: if (fallback_plan->lookup_array[i]) { michael@0: c.set_lookup_mask (fallback_plan->mask_array[i]); michael@0: hb_ot_layout_substitute_lookup (&c, michael@0: *fallback_plan->lookup_array[i], michael@0: fallback_plan->accel_array[i]); michael@0: } michael@0: } michael@0: michael@0: michael@0: #endif /* HB_OT_SHAPE_COMPLEX_ARABIC_FALLBACK_HH */