Coverage Report

Created: 2026-06-13 06:37

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