1*2d1272b8SAndroid Build Coastguard Worker /*
2*2d1272b8SAndroid Build Coastguard Worker * Copyright © 2012 Google, Inc.
3*2d1272b8SAndroid Build Coastguard Worker *
4*2d1272b8SAndroid Build Coastguard Worker * This is part of HarfBuzz, a text shaping library.
5*2d1272b8SAndroid Build Coastguard Worker *
6*2d1272b8SAndroid Build Coastguard Worker * Permission is hereby granted, without written agreement and without
7*2d1272b8SAndroid Build Coastguard Worker * license or royalty fees, to use, copy, modify, and distribute this
8*2d1272b8SAndroid Build Coastguard Worker * software and its documentation for any purpose, provided that the
9*2d1272b8SAndroid Build Coastguard Worker * above copyright notice and the following two paragraphs appear in
10*2d1272b8SAndroid Build Coastguard Worker * all copies of this software.
11*2d1272b8SAndroid Build Coastguard Worker *
12*2d1272b8SAndroid Build Coastguard Worker * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13*2d1272b8SAndroid Build Coastguard Worker * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14*2d1272b8SAndroid Build Coastguard Worker * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15*2d1272b8SAndroid Build Coastguard Worker * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16*2d1272b8SAndroid Build Coastguard Worker * DAMAGE.
17*2d1272b8SAndroid Build Coastguard Worker *
18*2d1272b8SAndroid Build Coastguard Worker * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19*2d1272b8SAndroid Build Coastguard Worker * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20*2d1272b8SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21*2d1272b8SAndroid Build Coastguard Worker * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22*2d1272b8SAndroid Build Coastguard Worker * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23*2d1272b8SAndroid Build Coastguard Worker *
24*2d1272b8SAndroid Build Coastguard Worker * Google Author(s): Behdad Esfahbod
25*2d1272b8SAndroid Build Coastguard Worker */
26*2d1272b8SAndroid Build Coastguard Worker
27*2d1272b8SAndroid Build Coastguard Worker #ifndef HB_OT_SHAPER_ARABIC_FALLBACK_HH
28*2d1272b8SAndroid Build Coastguard Worker #define HB_OT_SHAPER_ARABIC_FALLBACK_HH
29*2d1272b8SAndroid Build Coastguard Worker
30*2d1272b8SAndroid Build Coastguard Worker #include "hb.hh"
31*2d1272b8SAndroid Build Coastguard Worker
32*2d1272b8SAndroid Build Coastguard Worker #include "hb-ot-shape.hh"
33*2d1272b8SAndroid Build Coastguard Worker #include "hb-ot-layout-gsub-table.hh"
34*2d1272b8SAndroid Build Coastguard Worker
35*2d1272b8SAndroid Build Coastguard Worker
36*2d1272b8SAndroid Build Coastguard Worker /* Features ordered the same as the entries in shaping_table rows,
37*2d1272b8SAndroid Build Coastguard Worker * followed by rlig. Don't change.
38*2d1272b8SAndroid Build Coastguard Worker *
39*2d1272b8SAndroid Build Coastguard Worker * We currently support one subtable per lookup, and one lookup
40*2d1272b8SAndroid Build Coastguard Worker * per feature. But we allow duplicate features, so we use that!
41*2d1272b8SAndroid Build Coastguard Worker */
42*2d1272b8SAndroid Build Coastguard Worker static const hb_tag_t arabic_fallback_features[] =
43*2d1272b8SAndroid Build Coastguard Worker {
44*2d1272b8SAndroid Build Coastguard Worker HB_TAG('i','n','i','t'),
45*2d1272b8SAndroid Build Coastguard Worker HB_TAG('m','e','d','i'),
46*2d1272b8SAndroid Build Coastguard Worker HB_TAG('f','i','n','a'),
47*2d1272b8SAndroid Build Coastguard Worker HB_TAG('i','s','o','l'),
48*2d1272b8SAndroid Build Coastguard Worker HB_TAG('r','l','i','g'),
49*2d1272b8SAndroid Build Coastguard Worker HB_TAG('r','l','i','g'),
50*2d1272b8SAndroid Build Coastguard Worker HB_TAG('r','l','i','g'),
51*2d1272b8SAndroid Build Coastguard Worker };
52*2d1272b8SAndroid Build Coastguard Worker
53*2d1272b8SAndroid Build Coastguard Worker static OT::SubstLookup *
arabic_fallback_synthesize_lookup_single(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font,unsigned int feature_index)54*2d1272b8SAndroid Build Coastguard Worker arabic_fallback_synthesize_lookup_single (const hb_ot_shape_plan_t *plan HB_UNUSED,
55*2d1272b8SAndroid Build Coastguard Worker hb_font_t *font,
56*2d1272b8SAndroid Build Coastguard Worker unsigned int feature_index)
57*2d1272b8SAndroid Build Coastguard Worker {
58*2d1272b8SAndroid Build Coastguard Worker OT::HBGlyphID16 glyphs[SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1];
59*2d1272b8SAndroid Build Coastguard Worker OT::HBGlyphID16 substitutes[SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1];
60*2d1272b8SAndroid Build Coastguard Worker unsigned int num_glyphs = 0;
61*2d1272b8SAndroid Build Coastguard Worker
62*2d1272b8SAndroid Build Coastguard Worker /* Populate arrays */
63*2d1272b8SAndroid Build Coastguard Worker for (hb_codepoint_t u = SHAPING_TABLE_FIRST; u < SHAPING_TABLE_LAST + 1; u++)
64*2d1272b8SAndroid Build Coastguard Worker {
65*2d1272b8SAndroid Build Coastguard Worker hb_codepoint_t s = shaping_table[u - SHAPING_TABLE_FIRST][feature_index];
66*2d1272b8SAndroid Build Coastguard Worker hb_codepoint_t u_glyph, s_glyph;
67*2d1272b8SAndroid Build Coastguard Worker
68*2d1272b8SAndroid Build Coastguard Worker if (!s ||
69*2d1272b8SAndroid Build Coastguard Worker !hb_font_get_glyph (font, u, 0, &u_glyph) ||
70*2d1272b8SAndroid Build Coastguard Worker !hb_font_get_glyph (font, s, 0, &s_glyph) ||
71*2d1272b8SAndroid Build Coastguard Worker u_glyph == s_glyph ||
72*2d1272b8SAndroid Build Coastguard Worker u_glyph > 0xFFFFu || s_glyph > 0xFFFFu)
73*2d1272b8SAndroid Build Coastguard Worker continue;
74*2d1272b8SAndroid Build Coastguard Worker
75*2d1272b8SAndroid Build Coastguard Worker glyphs[num_glyphs] = u_glyph;
76*2d1272b8SAndroid Build Coastguard Worker substitutes[num_glyphs] = s_glyph;
77*2d1272b8SAndroid Build Coastguard Worker
78*2d1272b8SAndroid Build Coastguard Worker num_glyphs++;
79*2d1272b8SAndroid Build Coastguard Worker }
80*2d1272b8SAndroid Build Coastguard Worker
81*2d1272b8SAndroid Build Coastguard Worker if (!num_glyphs)
82*2d1272b8SAndroid Build Coastguard Worker return nullptr;
83*2d1272b8SAndroid Build Coastguard Worker
84*2d1272b8SAndroid Build Coastguard Worker /* Bubble-sort or something equally good!
85*2d1272b8SAndroid Build Coastguard Worker * May not be good-enough for presidential candidate interviews, but good-enough for us... */
86*2d1272b8SAndroid Build Coastguard Worker hb_stable_sort (&glyphs[0], num_glyphs,
87*2d1272b8SAndroid Build Coastguard Worker (int(*)(const OT::HBUINT16*, const OT::HBUINT16 *)) OT::HBGlyphID16::cmp,
88*2d1272b8SAndroid Build Coastguard Worker &substitutes[0]);
89*2d1272b8SAndroid Build Coastguard Worker
90*2d1272b8SAndroid Build Coastguard Worker
91*2d1272b8SAndroid Build Coastguard Worker /* Each glyph takes four bytes max, and there's some overhead. */
92*2d1272b8SAndroid Build Coastguard Worker char buf[(SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1) * 4 + 128];
93*2d1272b8SAndroid Build Coastguard Worker hb_serialize_context_t c (buf, sizeof (buf));
94*2d1272b8SAndroid Build Coastguard Worker OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> ();
95*2d1272b8SAndroid Build Coastguard Worker bool ret = lookup->serialize_single (&c,
96*2d1272b8SAndroid Build Coastguard Worker OT::LookupFlag::IgnoreMarks,
97*2d1272b8SAndroid Build Coastguard Worker hb_sorted_array (glyphs, num_glyphs),
98*2d1272b8SAndroid Build Coastguard Worker hb_array (substitutes, num_glyphs));
99*2d1272b8SAndroid Build Coastguard Worker c.end_serialize ();
100*2d1272b8SAndroid Build Coastguard Worker
101*2d1272b8SAndroid Build Coastguard Worker return ret && !c.in_error () ? c.copy<OT::SubstLookup> () : nullptr;
102*2d1272b8SAndroid Build Coastguard Worker }
103*2d1272b8SAndroid Build Coastguard Worker
104*2d1272b8SAndroid Build Coastguard Worker template <typename T>
105*2d1272b8SAndroid Build Coastguard Worker static OT::SubstLookup *
arabic_fallback_synthesize_lookup_ligature(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font,const T & ligature_table,unsigned lookup_flags)106*2d1272b8SAndroid Build Coastguard Worker arabic_fallback_synthesize_lookup_ligature (const hb_ot_shape_plan_t *plan HB_UNUSED,
107*2d1272b8SAndroid Build Coastguard Worker hb_font_t *font,
108*2d1272b8SAndroid Build Coastguard Worker const T &ligature_table,
109*2d1272b8SAndroid Build Coastguard Worker unsigned lookup_flags)
110*2d1272b8SAndroid Build Coastguard Worker {
111*2d1272b8SAndroid Build Coastguard Worker OT::HBGlyphID16 first_glyphs[ARRAY_LENGTH_CONST (ligature_table)];
112*2d1272b8SAndroid Build Coastguard Worker unsigned int first_glyphs_indirection[ARRAY_LENGTH_CONST (ligature_table)];
113*2d1272b8SAndroid Build Coastguard Worker unsigned int ligature_per_first_glyph_count_list[ARRAY_LENGTH_CONST (first_glyphs)];
114*2d1272b8SAndroid Build Coastguard Worker unsigned int num_first_glyphs = 0;
115*2d1272b8SAndroid Build Coastguard Worker
116*2d1272b8SAndroid Build Coastguard Worker /* We know that all our ligatures have the same number of components. */
117*2d1272b8SAndroid Build Coastguard Worker OT::HBGlyphID16 ligature_list[ARRAY_LENGTH_CONST (first_glyphs) * ARRAY_LENGTH_CONST(ligature_table[0].ligatures)];
118*2d1272b8SAndroid Build Coastguard Worker unsigned int component_count_list[ARRAY_LENGTH_CONST (ligature_list)];
119*2d1272b8SAndroid Build Coastguard Worker OT::HBGlyphID16 component_list[ARRAY_LENGTH_CONST (ligature_list) *
120*2d1272b8SAndroid Build Coastguard Worker ARRAY_LENGTH_CONST (ligature_table[0].ligatures[0].components)];
121*2d1272b8SAndroid Build Coastguard Worker unsigned int num_ligatures = 0;
122*2d1272b8SAndroid Build Coastguard Worker unsigned int num_components = 0;
123*2d1272b8SAndroid Build Coastguard Worker
124*2d1272b8SAndroid Build Coastguard Worker /* Populate arrays */
125*2d1272b8SAndroid Build Coastguard Worker
126*2d1272b8SAndroid Build Coastguard Worker /* Sort out the first-glyphs */
127*2d1272b8SAndroid Build Coastguard Worker for (unsigned int first_glyph_idx = 0; first_glyph_idx < ARRAY_LENGTH (first_glyphs); first_glyph_idx++)
128*2d1272b8SAndroid Build Coastguard Worker {
129*2d1272b8SAndroid Build Coastguard Worker hb_codepoint_t first_u = ligature_table[first_glyph_idx].first;
130*2d1272b8SAndroid Build Coastguard Worker hb_codepoint_t first_glyph;
131*2d1272b8SAndroid Build Coastguard Worker if (!hb_font_get_glyph (font, first_u, 0, &first_glyph))
132*2d1272b8SAndroid Build Coastguard Worker continue;
133*2d1272b8SAndroid Build Coastguard Worker first_glyphs[num_first_glyphs] = first_glyph;
134*2d1272b8SAndroid Build Coastguard Worker ligature_per_first_glyph_count_list[num_first_glyphs] = 0;
135*2d1272b8SAndroid Build Coastguard Worker first_glyphs_indirection[num_first_glyphs] = first_glyph_idx;
136*2d1272b8SAndroid Build Coastguard Worker num_first_glyphs++;
137*2d1272b8SAndroid Build Coastguard Worker }
138*2d1272b8SAndroid Build Coastguard Worker hb_stable_sort (&first_glyphs[0], num_first_glyphs,
139*2d1272b8SAndroid Build Coastguard Worker (int(*)(const OT::HBUINT16*, const OT::HBUINT16 *)) OT::HBGlyphID16::cmp,
140*2d1272b8SAndroid Build Coastguard Worker &first_glyphs_indirection[0]);
141*2d1272b8SAndroid Build Coastguard Worker
142*2d1272b8SAndroid Build Coastguard Worker /* Now that the first-glyphs are sorted, walk again, populate ligatures. */
143*2d1272b8SAndroid Build Coastguard Worker for (unsigned int i = 0; i < num_first_glyphs; i++)
144*2d1272b8SAndroid Build Coastguard Worker {
145*2d1272b8SAndroid Build Coastguard Worker unsigned int first_glyph_idx = first_glyphs_indirection[i];
146*2d1272b8SAndroid Build Coastguard Worker
147*2d1272b8SAndroid Build Coastguard Worker for (unsigned int ligature_idx = 0; ligature_idx < ARRAY_LENGTH (ligature_table[0].ligatures); ligature_idx++)
148*2d1272b8SAndroid Build Coastguard Worker {
149*2d1272b8SAndroid Build Coastguard Worker hb_codepoint_t ligature_u = ligature_table[first_glyph_idx].ligatures[ligature_idx].ligature;
150*2d1272b8SAndroid Build Coastguard Worker hb_codepoint_t ligature_glyph;
151*2d1272b8SAndroid Build Coastguard Worker if (!hb_font_get_glyph (font, ligature_u, 0, &ligature_glyph))
152*2d1272b8SAndroid Build Coastguard Worker continue;
153*2d1272b8SAndroid Build Coastguard Worker
154*2d1272b8SAndroid Build Coastguard Worker const auto &components = ligature_table[first_glyph_idx].ligatures[ligature_idx].components;
155*2d1272b8SAndroid Build Coastguard Worker unsigned component_count = ARRAY_LENGTH_CONST (components);
156*2d1272b8SAndroid Build Coastguard Worker
157*2d1272b8SAndroid Build Coastguard Worker bool matched = true;
158*2d1272b8SAndroid Build Coastguard Worker for (unsigned j = 0; j < component_count; j++)
159*2d1272b8SAndroid Build Coastguard Worker {
160*2d1272b8SAndroid Build Coastguard Worker hb_codepoint_t component_u = ligature_table[first_glyph_idx].ligatures[ligature_idx].components[j];
161*2d1272b8SAndroid Build Coastguard Worker hb_codepoint_t component_glyph;
162*2d1272b8SAndroid Build Coastguard Worker if (!component_u ||
163*2d1272b8SAndroid Build Coastguard Worker !hb_font_get_nominal_glyph (font, component_u, &component_glyph))
164*2d1272b8SAndroid Build Coastguard Worker {
165*2d1272b8SAndroid Build Coastguard Worker matched = false;
166*2d1272b8SAndroid Build Coastguard Worker break;
167*2d1272b8SAndroid Build Coastguard Worker }
168*2d1272b8SAndroid Build Coastguard Worker
169*2d1272b8SAndroid Build Coastguard Worker component_list[num_components++] = component_glyph;
170*2d1272b8SAndroid Build Coastguard Worker }
171*2d1272b8SAndroid Build Coastguard Worker if (!matched)
172*2d1272b8SAndroid Build Coastguard Worker continue;
173*2d1272b8SAndroid Build Coastguard Worker
174*2d1272b8SAndroid Build Coastguard Worker component_count_list[num_ligatures] = 1 + component_count;
175*2d1272b8SAndroid Build Coastguard Worker ligature_list[num_ligatures] = ligature_glyph;
176*2d1272b8SAndroid Build Coastguard Worker
177*2d1272b8SAndroid Build Coastguard Worker ligature_per_first_glyph_count_list[i]++;
178*2d1272b8SAndroid Build Coastguard Worker
179*2d1272b8SAndroid Build Coastguard Worker num_ligatures++;
180*2d1272b8SAndroid Build Coastguard Worker }
181*2d1272b8SAndroid Build Coastguard Worker }
182*2d1272b8SAndroid Build Coastguard Worker
183*2d1272b8SAndroid Build Coastguard Worker if (!num_ligatures)
184*2d1272b8SAndroid Build Coastguard Worker return nullptr;
185*2d1272b8SAndroid Build Coastguard Worker
186*2d1272b8SAndroid Build Coastguard Worker
187*2d1272b8SAndroid Build Coastguard Worker /* 16 bytes per ligature ought to be enough... */
188*2d1272b8SAndroid Build Coastguard Worker char buf[ARRAY_LENGTH_CONST (ligature_list) * 16 + 128];
189*2d1272b8SAndroid Build Coastguard Worker hb_serialize_context_t c (buf, sizeof (buf));
190*2d1272b8SAndroid Build Coastguard Worker OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> ();
191*2d1272b8SAndroid Build Coastguard Worker bool ret = lookup->serialize_ligature (&c,
192*2d1272b8SAndroid Build Coastguard Worker lookup_flags,
193*2d1272b8SAndroid Build Coastguard Worker hb_sorted_array (first_glyphs, num_first_glyphs),
194*2d1272b8SAndroid Build Coastguard Worker hb_array (ligature_per_first_glyph_count_list, num_first_glyphs),
195*2d1272b8SAndroid Build Coastguard Worker hb_array (ligature_list, num_ligatures),
196*2d1272b8SAndroid Build Coastguard Worker hb_array (component_count_list, num_ligatures),
197*2d1272b8SAndroid Build Coastguard Worker hb_array (component_list, num_components));
198*2d1272b8SAndroid Build Coastguard Worker c.end_serialize ();
199*2d1272b8SAndroid Build Coastguard Worker
200*2d1272b8SAndroid Build Coastguard Worker return ret && !c.in_error () ? c.copy<OT::SubstLookup> () : nullptr;
201*2d1272b8SAndroid Build Coastguard Worker }
202*2d1272b8SAndroid Build Coastguard Worker
203*2d1272b8SAndroid Build Coastguard Worker static OT::SubstLookup *
arabic_fallback_synthesize_lookup(const hb_ot_shape_plan_t * plan,hb_font_t * font,unsigned int feature_index)204*2d1272b8SAndroid Build Coastguard Worker arabic_fallback_synthesize_lookup (const hb_ot_shape_plan_t *plan,
205*2d1272b8SAndroid Build Coastguard Worker hb_font_t *font,
206*2d1272b8SAndroid Build Coastguard Worker unsigned int feature_index)
207*2d1272b8SAndroid Build Coastguard Worker {
208*2d1272b8SAndroid Build Coastguard Worker if (feature_index < 4)
209*2d1272b8SAndroid Build Coastguard Worker return arabic_fallback_synthesize_lookup_single (plan, font, feature_index);
210*2d1272b8SAndroid Build Coastguard Worker else
211*2d1272b8SAndroid Build Coastguard Worker {
212*2d1272b8SAndroid Build Coastguard Worker switch (feature_index) {
213*2d1272b8SAndroid Build Coastguard Worker case 4: return arabic_fallback_synthesize_lookup_ligature (plan, font, ligature_3_table, OT::LookupFlag::IgnoreMarks);
214*2d1272b8SAndroid Build Coastguard Worker case 5: return arabic_fallback_synthesize_lookup_ligature (plan, font, ligature_table, OT::LookupFlag::IgnoreMarks);
215*2d1272b8SAndroid Build Coastguard Worker case 6: return arabic_fallback_synthesize_lookup_ligature (plan, font, ligature_mark_table, 0);
216*2d1272b8SAndroid Build Coastguard Worker }
217*2d1272b8SAndroid Build Coastguard Worker }
218*2d1272b8SAndroid Build Coastguard Worker assert (false);
219*2d1272b8SAndroid Build Coastguard Worker return nullptr;
220*2d1272b8SAndroid Build Coastguard Worker }
221*2d1272b8SAndroid Build Coastguard Worker
222*2d1272b8SAndroid Build Coastguard Worker #define ARABIC_FALLBACK_MAX_LOOKUPS ARRAY_LENGTH_CONST (arabic_fallback_features)
223*2d1272b8SAndroid Build Coastguard Worker
224*2d1272b8SAndroid Build Coastguard Worker struct arabic_fallback_plan_t
225*2d1272b8SAndroid Build Coastguard Worker {
226*2d1272b8SAndroid Build Coastguard Worker unsigned int num_lookups;
227*2d1272b8SAndroid Build Coastguard Worker bool free_lookups;
228*2d1272b8SAndroid Build Coastguard Worker
229*2d1272b8SAndroid Build Coastguard Worker hb_mask_t mask_array[ARABIC_FALLBACK_MAX_LOOKUPS];
230*2d1272b8SAndroid Build Coastguard Worker OT::SubstLookup *lookup_array[ARABIC_FALLBACK_MAX_LOOKUPS];
231*2d1272b8SAndroid Build Coastguard Worker OT::hb_ot_layout_lookup_accelerator_t *accel_array[ARABIC_FALLBACK_MAX_LOOKUPS];
232*2d1272b8SAndroid Build Coastguard Worker };
233*2d1272b8SAndroid Build Coastguard Worker
234*2d1272b8SAndroid Build Coastguard Worker #if defined(_WIN32) && !defined(HB_NO_WIN1256)
235*2d1272b8SAndroid Build Coastguard Worker #define HB_WITH_WIN1256
236*2d1272b8SAndroid Build Coastguard Worker #endif
237*2d1272b8SAndroid Build Coastguard Worker
238*2d1272b8SAndroid Build Coastguard Worker #ifdef HB_WITH_WIN1256
239*2d1272b8SAndroid Build Coastguard Worker #include "hb-ot-shaper-arabic-win1256.hh"
240*2d1272b8SAndroid Build Coastguard Worker #endif
241*2d1272b8SAndroid Build Coastguard Worker
242*2d1272b8SAndroid Build Coastguard Worker struct ManifestLookup
243*2d1272b8SAndroid Build Coastguard Worker {
244*2d1272b8SAndroid Build Coastguard Worker public:
245*2d1272b8SAndroid Build Coastguard Worker OT::Tag tag;
246*2d1272b8SAndroid Build Coastguard Worker OT::Offset16To<OT::SubstLookup> lookupOffset;
247*2d1272b8SAndroid Build Coastguard Worker public:
248*2d1272b8SAndroid Build Coastguard Worker DEFINE_SIZE_STATIC (6);
249*2d1272b8SAndroid Build Coastguard Worker };
250*2d1272b8SAndroid Build Coastguard Worker typedef OT::Array16Of<ManifestLookup> Manifest;
251*2d1272b8SAndroid Build Coastguard Worker
252*2d1272b8SAndroid Build Coastguard Worker static bool
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)253*2d1272b8SAndroid Build Coastguard Worker arabic_fallback_plan_init_win1256 (arabic_fallback_plan_t *fallback_plan HB_UNUSED,
254*2d1272b8SAndroid Build Coastguard Worker const hb_ot_shape_plan_t *plan HB_UNUSED,
255*2d1272b8SAndroid Build Coastguard Worker hb_font_t *font HB_UNUSED)
256*2d1272b8SAndroid Build Coastguard Worker {
257*2d1272b8SAndroid Build Coastguard Worker #ifdef HB_WITH_WIN1256
258*2d1272b8SAndroid Build Coastguard Worker /* Does this font look like it's Windows-1256-encoded? */
259*2d1272b8SAndroid Build Coastguard Worker hb_codepoint_t g;
260*2d1272b8SAndroid Build Coastguard Worker if (!(hb_font_get_glyph (font, 0x0627u, 0, &g) && g == 199 /* ALEF */ &&
261*2d1272b8SAndroid Build Coastguard Worker hb_font_get_glyph (font, 0x0644u, 0, &g) && g == 225 /* LAM */ &&
262*2d1272b8SAndroid Build Coastguard Worker hb_font_get_glyph (font, 0x0649u, 0, &g) && g == 236 /* ALEF MAKSURA */ &&
263*2d1272b8SAndroid Build Coastguard Worker hb_font_get_glyph (font, 0x064Au, 0, &g) && g == 237 /* YEH */ &&
264*2d1272b8SAndroid Build Coastguard Worker hb_font_get_glyph (font, 0x0652u, 0, &g) && g == 250 /* SUKUN */))
265*2d1272b8SAndroid Build Coastguard Worker return false;
266*2d1272b8SAndroid Build Coastguard Worker
267*2d1272b8SAndroid Build Coastguard Worker const Manifest &manifest = reinterpret_cast<const Manifest&> (arabic_win1256_gsub_lookups.manifest);
268*2d1272b8SAndroid Build Coastguard Worker static_assert (sizeof (arabic_win1256_gsub_lookups.manifestData) <=
269*2d1272b8SAndroid Build Coastguard Worker ARABIC_FALLBACK_MAX_LOOKUPS * sizeof (ManifestLookup), "");
270*2d1272b8SAndroid Build Coastguard Worker
271*2d1272b8SAndroid Build Coastguard Worker unsigned j = 0;
272*2d1272b8SAndroid Build Coastguard Worker unsigned int count = manifest.len;
273*2d1272b8SAndroid Build Coastguard Worker for (unsigned int i = 0; i < count; i++)
274*2d1272b8SAndroid Build Coastguard Worker {
275*2d1272b8SAndroid Build Coastguard Worker fallback_plan->mask_array[j] = plan->map.get_1_mask (manifest[i].tag);
276*2d1272b8SAndroid Build Coastguard Worker if (fallback_plan->mask_array[j])
277*2d1272b8SAndroid Build Coastguard Worker {
278*2d1272b8SAndroid Build Coastguard Worker fallback_plan->lookup_array[j] = const_cast<OT::SubstLookup*> (&(&manifest+manifest[i].lookupOffset));
279*2d1272b8SAndroid Build Coastguard Worker if (fallback_plan->lookup_array[j])
280*2d1272b8SAndroid Build Coastguard Worker {
281*2d1272b8SAndroid Build Coastguard Worker fallback_plan->accel_array[j] = OT::hb_ot_layout_lookup_accelerator_t::create (*fallback_plan->lookup_array[j]);
282*2d1272b8SAndroid Build Coastguard Worker j++;
283*2d1272b8SAndroid Build Coastguard Worker }
284*2d1272b8SAndroid Build Coastguard Worker }
285*2d1272b8SAndroid Build Coastguard Worker }
286*2d1272b8SAndroid Build Coastguard Worker
287*2d1272b8SAndroid Build Coastguard Worker fallback_plan->num_lookups = j;
288*2d1272b8SAndroid Build Coastguard Worker fallback_plan->free_lookups = false;
289*2d1272b8SAndroid Build Coastguard Worker
290*2d1272b8SAndroid Build Coastguard Worker return j > 0;
291*2d1272b8SAndroid Build Coastguard Worker #else
292*2d1272b8SAndroid Build Coastguard Worker return false;
293*2d1272b8SAndroid Build Coastguard Worker #endif
294*2d1272b8SAndroid Build Coastguard Worker }
295*2d1272b8SAndroid Build Coastguard Worker
296*2d1272b8SAndroid Build Coastguard Worker static bool
arabic_fallback_plan_init_unicode(arabic_fallback_plan_t * fallback_plan,const hb_ot_shape_plan_t * plan,hb_font_t * font)297*2d1272b8SAndroid Build Coastguard Worker arabic_fallback_plan_init_unicode (arabic_fallback_plan_t *fallback_plan,
298*2d1272b8SAndroid Build Coastguard Worker const hb_ot_shape_plan_t *plan,
299*2d1272b8SAndroid Build Coastguard Worker hb_font_t *font)
300*2d1272b8SAndroid Build Coastguard Worker {
301*2d1272b8SAndroid Build Coastguard Worker static_assert ((ARRAY_LENGTH_CONST (arabic_fallback_features) <= ARABIC_FALLBACK_MAX_LOOKUPS), "");
302*2d1272b8SAndroid Build Coastguard Worker unsigned int j = 0;
303*2d1272b8SAndroid Build Coastguard Worker for (unsigned int i = 0; i < ARRAY_LENGTH(arabic_fallback_features) ; i++)
304*2d1272b8SAndroid Build Coastguard Worker {
305*2d1272b8SAndroid Build Coastguard Worker fallback_plan->mask_array[j] = plan->map.get_1_mask (arabic_fallback_features[i]);
306*2d1272b8SAndroid Build Coastguard Worker if (fallback_plan->mask_array[j])
307*2d1272b8SAndroid Build Coastguard Worker {
308*2d1272b8SAndroid Build Coastguard Worker fallback_plan->lookup_array[j] = arabic_fallback_synthesize_lookup (plan, font, i);
309*2d1272b8SAndroid Build Coastguard Worker if (fallback_plan->lookup_array[j])
310*2d1272b8SAndroid Build Coastguard Worker {
311*2d1272b8SAndroid Build Coastguard Worker fallback_plan->accel_array[j] = OT::hb_ot_layout_lookup_accelerator_t::create (*fallback_plan->lookup_array[j]);
312*2d1272b8SAndroid Build Coastguard Worker j++;
313*2d1272b8SAndroid Build Coastguard Worker }
314*2d1272b8SAndroid Build Coastguard Worker }
315*2d1272b8SAndroid Build Coastguard Worker }
316*2d1272b8SAndroid Build Coastguard Worker
317*2d1272b8SAndroid Build Coastguard Worker fallback_plan->num_lookups = j;
318*2d1272b8SAndroid Build Coastguard Worker fallback_plan->free_lookups = true;
319*2d1272b8SAndroid Build Coastguard Worker
320*2d1272b8SAndroid Build Coastguard Worker return j > 0;
321*2d1272b8SAndroid Build Coastguard Worker }
322*2d1272b8SAndroid Build Coastguard Worker
323*2d1272b8SAndroid Build Coastguard Worker static arabic_fallback_plan_t *
arabic_fallback_plan_create(const hb_ot_shape_plan_t * plan,hb_font_t * font)324*2d1272b8SAndroid Build Coastguard Worker arabic_fallback_plan_create (const hb_ot_shape_plan_t *plan,
325*2d1272b8SAndroid Build Coastguard Worker hb_font_t *font)
326*2d1272b8SAndroid Build Coastguard Worker {
327*2d1272b8SAndroid Build Coastguard Worker arabic_fallback_plan_t *fallback_plan = (arabic_fallback_plan_t *) hb_calloc (1, sizeof (arabic_fallback_plan_t));
328*2d1272b8SAndroid Build Coastguard Worker if (unlikely (!fallback_plan))
329*2d1272b8SAndroid Build Coastguard Worker return const_cast<arabic_fallback_plan_t *> (&Null (arabic_fallback_plan_t));
330*2d1272b8SAndroid Build Coastguard Worker
331*2d1272b8SAndroid Build Coastguard Worker fallback_plan->num_lookups = 0;
332*2d1272b8SAndroid Build Coastguard Worker fallback_plan->free_lookups = false;
333*2d1272b8SAndroid Build Coastguard Worker
334*2d1272b8SAndroid Build Coastguard Worker /* Try synthesizing GSUB table using Unicode Arabic Presentation Forms,
335*2d1272b8SAndroid Build Coastguard Worker * in case the font has cmap entries for the presentation-forms characters. */
336*2d1272b8SAndroid Build Coastguard Worker if (arabic_fallback_plan_init_unicode (fallback_plan, plan, font))
337*2d1272b8SAndroid Build Coastguard Worker return fallback_plan;
338*2d1272b8SAndroid Build Coastguard Worker
339*2d1272b8SAndroid Build Coastguard Worker /* See if this looks like a Windows-1256-encoded font. If it does, use a
340*2d1272b8SAndroid Build Coastguard Worker * hand-coded GSUB table. */
341*2d1272b8SAndroid Build Coastguard Worker if (arabic_fallback_plan_init_win1256 (fallback_plan, plan, font))
342*2d1272b8SAndroid Build Coastguard Worker return fallback_plan;
343*2d1272b8SAndroid Build Coastguard Worker
344*2d1272b8SAndroid Build Coastguard Worker assert (fallback_plan->num_lookups == 0);
345*2d1272b8SAndroid Build Coastguard Worker hb_free (fallback_plan);
346*2d1272b8SAndroid Build Coastguard Worker return const_cast<arabic_fallback_plan_t *> (&Null (arabic_fallback_plan_t));
347*2d1272b8SAndroid Build Coastguard Worker }
348*2d1272b8SAndroid Build Coastguard Worker
349*2d1272b8SAndroid Build Coastguard Worker static void
arabic_fallback_plan_destroy(arabic_fallback_plan_t * fallback_plan)350*2d1272b8SAndroid Build Coastguard Worker arabic_fallback_plan_destroy (arabic_fallback_plan_t *fallback_plan)
351*2d1272b8SAndroid Build Coastguard Worker {
352*2d1272b8SAndroid Build Coastguard Worker if (!fallback_plan || fallback_plan->num_lookups == 0)
353*2d1272b8SAndroid Build Coastguard Worker return;
354*2d1272b8SAndroid Build Coastguard Worker
355*2d1272b8SAndroid Build Coastguard Worker for (unsigned int i = 0; i < fallback_plan->num_lookups; i++)
356*2d1272b8SAndroid Build Coastguard Worker if (fallback_plan->lookup_array[i])
357*2d1272b8SAndroid Build Coastguard Worker {
358*2d1272b8SAndroid Build Coastguard Worker hb_free (fallback_plan->accel_array[i]);
359*2d1272b8SAndroid Build Coastguard Worker if (fallback_plan->free_lookups)
360*2d1272b8SAndroid Build Coastguard Worker hb_free (fallback_plan->lookup_array[i]);
361*2d1272b8SAndroid Build Coastguard Worker }
362*2d1272b8SAndroid Build Coastguard Worker
363*2d1272b8SAndroid Build Coastguard Worker hb_free (fallback_plan);
364*2d1272b8SAndroid Build Coastguard Worker }
365*2d1272b8SAndroid Build Coastguard Worker
366*2d1272b8SAndroid Build Coastguard Worker static void
arabic_fallback_plan_shape(arabic_fallback_plan_t * fallback_plan,hb_font_t * font,hb_buffer_t * buffer)367*2d1272b8SAndroid Build Coastguard Worker arabic_fallback_plan_shape (arabic_fallback_plan_t *fallback_plan,
368*2d1272b8SAndroid Build Coastguard Worker hb_font_t *font,
369*2d1272b8SAndroid Build Coastguard Worker hb_buffer_t *buffer)
370*2d1272b8SAndroid Build Coastguard Worker {
371*2d1272b8SAndroid Build Coastguard Worker OT::hb_ot_apply_context_t c (0, font, buffer, hb_blob_get_empty ());
372*2d1272b8SAndroid Build Coastguard Worker for (unsigned int i = 0; i < fallback_plan->num_lookups; i++)
373*2d1272b8SAndroid Build Coastguard Worker if (fallback_plan->lookup_array[i]) {
374*2d1272b8SAndroid Build Coastguard Worker c.set_lookup_mask (fallback_plan->mask_array[i]);
375*2d1272b8SAndroid Build Coastguard Worker if (fallback_plan->accel_array[i])
376*2d1272b8SAndroid Build Coastguard Worker hb_ot_layout_substitute_lookup (&c,
377*2d1272b8SAndroid Build Coastguard Worker *fallback_plan->lookup_array[i],
378*2d1272b8SAndroid Build Coastguard Worker *fallback_plan->accel_array[i]);
379*2d1272b8SAndroid Build Coastguard Worker }
380*2d1272b8SAndroid Build Coastguard Worker }
381*2d1272b8SAndroid Build Coastguard Worker
382*2d1272b8SAndroid Build Coastguard Worker
383*2d1272b8SAndroid Build Coastguard Worker #endif /* HB_OT_SHAPER_ARABIC_FALLBACK_HH */
384