Coverage Report

Created: 2026-09-01 06:20

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
28
{
58
28
  OT::HBGlyphID16 glyphs[SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1];
59
28
  OT::HBGlyphID16 substitutes[SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1];
60
28
  unsigned int num_glyphs = 0;
61
62
  /* Populate arrays */
63
5.04k
  for (hb_codepoint_t u = SHAPING_TABLE_FIRST; u < SHAPING_TABLE_LAST + 1; u++)
64
5.01k
  {
65
5.01k
    hb_codepoint_t s = shaping_table[u - SHAPING_TABLE_FIRST][feature_index];
66
5.01k
    hb_codepoint_t u_glyph, s_glyph;
67
68
5.01k
    if (!s ||
69
1.69k
  !hb_font_get_glyph (font, u, 0, &u_glyph) ||
70
0
  !hb_font_get_glyph (font, s, 0, &s_glyph) ||
71
0
  u_glyph == s_glyph ||
72
0
  u_glyph > 0xFFFFu || s_glyph > 0xFFFFu)
73
5.01k
      continue;
74
75
0
    glyphs[num_glyphs] = u_glyph;
76
0
    substitutes[num_glyphs] = s_glyph;
77
78
0
    num_glyphs++;
79
0
  }
80
81
28
  if (!num_glyphs)
82
28
    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
0
  hb_stable_sort (&glyphs[0], num_glyphs,
87
0
      (int(*)(const OT::HBUINT16*, const OT::HBUINT16 *)) OT::HBGlyphID16::cmp,
88
0
      &substitutes[0]);
89
90
91
  /* Each glyph takes four bytes max, and there's some overhead. */
92
0
  char buf[(SHAPING_TABLE_LAST - SHAPING_TABLE_FIRST + 1) * 4 + 128];
93
0
  hb_serialize_context_t c (buf, sizeof (buf));
94
0
  OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> ();
95
0
  bool ret = lookup->serialize_single (&c,
96
0
               OT::LookupFlag::IgnoreMarks,
97
0
               hb_sorted_array (glyphs, num_glyphs),
98
0
               hb_array (substitutes, num_glyphs));
99
0
  c.end_serialize ();
100
101
0
  return ret && !c.in_error () ? c.copy<OT::SubstLookup> () : nullptr;
102
28
}
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
21
{
111
21
  OT::HBGlyphID16 first_glyphs[ARRAY_LENGTH_CONST (ligature_table)];
112
21
  unsigned int first_glyphs_indirection[ARRAY_LENGTH_CONST (ligature_table)];
113
21
  unsigned int ligature_per_first_glyph_count_list[ARRAY_LENGTH_CONST (first_glyphs)];
114
21
  unsigned int num_first_glyphs = 0;
115
116
  /* We know that all our ligatures have the same number of components. */
117
21
  OT::HBGlyphID16 ligature_list[ARRAY_LENGTH_CONST (first_glyphs) * ARRAY_LENGTH_CONST(ligature_table[0].ligatures)];
118
21
  unsigned int component_count_list[ARRAY_LENGTH_CONST (ligature_list)];
119
21
  OT::HBGlyphID16 component_list[ARRAY_LENGTH_CONST (ligature_list) *
120
21
         ARRAY_LENGTH_CONST (ligature_table[0].ligatures[0].components)];
121
21
  unsigned int num_ligatures = 0;
122
21
  unsigned int num_components = 0;
123
124
  /* Populate arrays */
125
126
  /* Sort out the first-glyphs */
127
161
  for (unsigned int first_glyph_idx = 0; first_glyph_idx < ARRAY_LENGTH (first_glyphs); first_glyph_idx++)
128
140
  {
129
140
    hb_codepoint_t first_u = ligature_table[first_glyph_idx].first;
130
140
    hb_codepoint_t first_glyph;
131
140
    if (!hb_font_get_glyph (font, first_u, 0, &first_glyph))
132
140
      continue;
133
0
    first_glyphs[num_first_glyphs] = first_glyph;
134
0
    ligature_per_first_glyph_count_list[num_first_glyphs] = 0;
135
0
    first_glyphs_indirection[num_first_glyphs] = first_glyph_idx;
136
0
    num_first_glyphs++;
137
0
  }
138
21
  hb_stable_sort (&first_glyphs[0], num_first_glyphs,
139
21
      (int(*)(const OT::HBUINT16*, const OT::HBUINT16 *)) OT::HBGlyphID16::cmp,
140
21
      &first_glyphs_indirection[0]);
141
142
  /* Now that the first-glyphs are sorted, walk again, populate ligatures. */
143
21
  for (unsigned int i = 0; i < num_first_glyphs; i++)
144
0
  {
145
0
    unsigned int first_glyph_idx = first_glyphs_indirection[i];
146
147
0
    for (unsigned int ligature_idx = 0; ligature_idx < ARRAY_LENGTH (ligature_table[0].ligatures); ligature_idx++)
148
0
    {
149
0
      hb_codepoint_t ligature_u = ligature_table[first_glyph_idx].ligatures[ligature_idx].ligature;
150
0
      hb_codepoint_t ligature_glyph;
151
0
      if (!hb_font_get_glyph (font, ligature_u, 0, &ligature_glyph))
152
0
  continue;
153
154
0
      const auto &components = ligature_table[first_glyph_idx].ligatures[ligature_idx].components;
155
0
      unsigned component_count = ARRAY_LENGTH_CONST (components);
156
157
0
      bool matched = true;
158
0
      for (unsigned j = 0; j < component_count; j++)
159
0
      {
160
0
  hb_codepoint_t component_u   = ligature_table[first_glyph_idx].ligatures[ligature_idx].components[j];
161
0
  hb_codepoint_t component_glyph;
162
0
  if (!component_u ||
163
0
      !hb_font_get_nominal_glyph (font, component_u, &component_glyph))
164
0
  {
165
0
    matched = false;
166
0
    break;
167
0
  }
168
169
0
  component_list[num_components++] = component_glyph;
170
0
      }
171
0
      if (!matched)
172
0
        continue;
173
174
0
      component_count_list[num_ligatures] = 1 + component_count;
175
0
      ligature_list[num_ligatures] = ligature_glyph;
176
177
0
      ligature_per_first_glyph_count_list[i]++;
178
179
0
      num_ligatures++;
180
0
    }
181
0
  }
182
183
21
  if (!num_ligatures)
184
21
    return nullptr;
185
186
187
  /* 16 bytes per ligature ought to be enough... */
188
0
  char buf[ARRAY_LENGTH_CONST (ligature_list) * 16 + 128];
189
0
  hb_serialize_context_t c (buf, sizeof (buf));
190
0
  OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> ();
191
0
  bool ret = lookup->serialize_ligature (&c,
192
0
           lookup_flags,
193
0
           hb_sorted_array (first_glyphs, num_first_glyphs),
194
0
           hb_array (ligature_per_first_glyph_count_list, num_first_glyphs),
195
0
           hb_array (ligature_list, num_ligatures),
196
0
           hb_array (component_count_list, num_ligatures),
197
0
           hb_array (component_list, num_components));
198
0
  c.end_serialize ();
199
200
0
  return ret && !c.in_error () ? c.copy<OT::SubstLookup> () : nullptr;
201
21
}
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
7
{
111
7
  OT::HBGlyphID16 first_glyphs[ARRAY_LENGTH_CONST (ligature_table)];
112
7
  unsigned int first_glyphs_indirection[ARRAY_LENGTH_CONST (ligature_table)];
113
7
  unsigned int ligature_per_first_glyph_count_list[ARRAY_LENGTH_CONST (first_glyphs)];
114
7
  unsigned int num_first_glyphs = 0;
115
116
  /* We know that all our ligatures have the same number of components. */
117
7
  OT::HBGlyphID16 ligature_list[ARRAY_LENGTH_CONST (first_glyphs) * ARRAY_LENGTH_CONST(ligature_table[0].ligatures)];
118
7
  unsigned int component_count_list[ARRAY_LENGTH_CONST (ligature_list)];
119
7
  OT::HBGlyphID16 component_list[ARRAY_LENGTH_CONST (ligature_list) *
120
7
         ARRAY_LENGTH_CONST (ligature_table[0].ligatures[0].components)];
121
7
  unsigned int num_ligatures = 0;
122
7
  unsigned int num_components = 0;
123
124
  /* Populate arrays */
125
126
  /* Sort out the first-glyphs */
127
14
  for (unsigned int first_glyph_idx = 0; first_glyph_idx < ARRAY_LENGTH (first_glyphs); first_glyph_idx++)
128
7
  {
129
7
    hb_codepoint_t first_u = ligature_table[first_glyph_idx].first;
130
7
    hb_codepoint_t first_glyph;
131
7
    if (!hb_font_get_glyph (font, first_u, 0, &first_glyph))
132
7
      continue;
133
0
    first_glyphs[num_first_glyphs] = first_glyph;
134
0
    ligature_per_first_glyph_count_list[num_first_glyphs] = 0;
135
0
    first_glyphs_indirection[num_first_glyphs] = first_glyph_idx;
136
0
    num_first_glyphs++;
137
0
  }
138
7
  hb_stable_sort (&first_glyphs[0], num_first_glyphs,
139
7
      (int(*)(const OT::HBUINT16*, const OT::HBUINT16 *)) OT::HBGlyphID16::cmp,
140
7
      &first_glyphs_indirection[0]);
141
142
  /* Now that the first-glyphs are sorted, walk again, populate ligatures. */
143
7
  for (unsigned int i = 0; i < num_first_glyphs; i++)
144
0
  {
145
0
    unsigned int first_glyph_idx = first_glyphs_indirection[i];
146
147
0
    for (unsigned int ligature_idx = 0; ligature_idx < ARRAY_LENGTH (ligature_table[0].ligatures); ligature_idx++)
148
0
    {
149
0
      hb_codepoint_t ligature_u = ligature_table[first_glyph_idx].ligatures[ligature_idx].ligature;
150
0
      hb_codepoint_t ligature_glyph;
151
0
      if (!hb_font_get_glyph (font, ligature_u, 0, &ligature_glyph))
152
0
  continue;
153
154
0
      const auto &components = ligature_table[first_glyph_idx].ligatures[ligature_idx].components;
155
0
      unsigned component_count = ARRAY_LENGTH_CONST (components);
156
157
0
      bool matched = true;
158
0
      for (unsigned j = 0; j < component_count; j++)
159
0
      {
160
0
  hb_codepoint_t component_u   = ligature_table[first_glyph_idx].ligatures[ligature_idx].components[j];
161
0
  hb_codepoint_t component_glyph;
162
0
  if (!component_u ||
163
0
      !hb_font_get_nominal_glyph (font, component_u, &component_glyph))
164
0
  {
165
0
    matched = false;
166
0
    break;
167
0
  }
168
169
0
  component_list[num_components++] = component_glyph;
170
0
      }
171
0
      if (!matched)
172
0
        continue;
173
174
0
      component_count_list[num_ligatures] = 1 + component_count;
175
0
      ligature_list[num_ligatures] = ligature_glyph;
176
177
0
      ligature_per_first_glyph_count_list[i]++;
178
179
0
      num_ligatures++;
180
0
    }
181
0
  }
182
183
7
  if (!num_ligatures)
184
7
    return nullptr;
185
186
187
  /* 16 bytes per ligature ought to be enough... */
188
0
  char buf[ARRAY_LENGTH_CONST (ligature_list) * 16 + 128];
189
0
  hb_serialize_context_t c (buf, sizeof (buf));
190
0
  OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> ();
191
0
  bool ret = lookup->serialize_ligature (&c,
192
0
           lookup_flags,
193
0
           hb_sorted_array (first_glyphs, num_first_glyphs),
194
0
           hb_array (ligature_per_first_glyph_count_list, num_first_glyphs),
195
0
           hb_array (ligature_list, num_ligatures),
196
0
           hb_array (component_count_list, num_ligatures),
197
0
           hb_array (component_list, num_components));
198
0
  c.end_serialize ();
199
200
0
  return ret && !c.in_error () ? c.copy<OT::SubstLookup> () : nullptr;
201
7
}
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
7
{
111
7
  OT::HBGlyphID16 first_glyphs[ARRAY_LENGTH_CONST (ligature_table)];
112
7
  unsigned int first_glyphs_indirection[ARRAY_LENGTH_CONST (ligature_table)];
113
7
  unsigned int ligature_per_first_glyph_count_list[ARRAY_LENGTH_CONST (first_glyphs)];
114
7
  unsigned int num_first_glyphs = 0;
115
116
  /* We know that all our ligatures have the same number of components. */
117
7
  OT::HBGlyphID16 ligature_list[ARRAY_LENGTH_CONST (first_glyphs) * ARRAY_LENGTH_CONST(ligature_table[0].ligatures)];
118
7
  unsigned int component_count_list[ARRAY_LENGTH_CONST (ligature_list)];
119
7
  OT::HBGlyphID16 component_list[ARRAY_LENGTH_CONST (ligature_list) *
120
7
         ARRAY_LENGTH_CONST (ligature_table[0].ligatures[0].components)];
121
7
  unsigned int num_ligatures = 0;
122
7
  unsigned int num_components = 0;
123
124
  /* Populate arrays */
125
126
  /* Sort out the first-glyphs */
127
133
  for (unsigned int first_glyph_idx = 0; first_glyph_idx < ARRAY_LENGTH (first_glyphs); first_glyph_idx++)
128
126
  {
129
126
    hb_codepoint_t first_u = ligature_table[first_glyph_idx].first;
130
126
    hb_codepoint_t first_glyph;
131
126
    if (!hb_font_get_glyph (font, first_u, 0, &first_glyph))
132
126
      continue;
133
0
    first_glyphs[num_first_glyphs] = first_glyph;
134
0
    ligature_per_first_glyph_count_list[num_first_glyphs] = 0;
135
0
    first_glyphs_indirection[num_first_glyphs] = first_glyph_idx;
136
0
    num_first_glyphs++;
137
0
  }
138
7
  hb_stable_sort (&first_glyphs[0], num_first_glyphs,
139
7
      (int(*)(const OT::HBUINT16*, const OT::HBUINT16 *)) OT::HBGlyphID16::cmp,
140
7
      &first_glyphs_indirection[0]);
141
142
  /* Now that the first-glyphs are sorted, walk again, populate ligatures. */
143
7
  for (unsigned int i = 0; i < num_first_glyphs; i++)
144
0
  {
145
0
    unsigned int first_glyph_idx = first_glyphs_indirection[i];
146
147
0
    for (unsigned int ligature_idx = 0; ligature_idx < ARRAY_LENGTH (ligature_table[0].ligatures); ligature_idx++)
148
0
    {
149
0
      hb_codepoint_t ligature_u = ligature_table[first_glyph_idx].ligatures[ligature_idx].ligature;
150
0
      hb_codepoint_t ligature_glyph;
151
0
      if (!hb_font_get_glyph (font, ligature_u, 0, &ligature_glyph))
152
0
  continue;
153
154
0
      const auto &components = ligature_table[first_glyph_idx].ligatures[ligature_idx].components;
155
0
      unsigned component_count = ARRAY_LENGTH_CONST (components);
156
157
0
      bool matched = true;
158
0
      for (unsigned j = 0; j < component_count; j++)
159
0
      {
160
0
  hb_codepoint_t component_u   = ligature_table[first_glyph_idx].ligatures[ligature_idx].components[j];
161
0
  hb_codepoint_t component_glyph;
162
0
  if (!component_u ||
163
0
      !hb_font_get_nominal_glyph (font, component_u, &component_glyph))
164
0
  {
165
0
    matched = false;
166
0
    break;
167
0
  }
168
169
0
  component_list[num_components++] = component_glyph;
170
0
      }
171
0
      if (!matched)
172
0
        continue;
173
174
0
      component_count_list[num_ligatures] = 1 + component_count;
175
0
      ligature_list[num_ligatures] = ligature_glyph;
176
177
0
      ligature_per_first_glyph_count_list[i]++;
178
179
0
      num_ligatures++;
180
0
    }
181
0
  }
182
183
7
  if (!num_ligatures)
184
7
    return nullptr;
185
186
187
  /* 16 bytes per ligature ought to be enough... */
188
0
  char buf[ARRAY_LENGTH_CONST (ligature_list) * 16 + 128];
189
0
  hb_serialize_context_t c (buf, sizeof (buf));
190
0
  OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> ();
191
0
  bool ret = lookup->serialize_ligature (&c,
192
0
           lookup_flags,
193
0
           hb_sorted_array (first_glyphs, num_first_glyphs),
194
0
           hb_array (ligature_per_first_glyph_count_list, num_first_glyphs),
195
0
           hb_array (ligature_list, num_ligatures),
196
0
           hb_array (component_count_list, num_ligatures),
197
0
           hb_array (component_list, num_components));
198
0
  c.end_serialize ();
199
200
0
  return ret && !c.in_error () ? c.copy<OT::SubstLookup> () : nullptr;
201
7
}
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
7
{
111
7
  OT::HBGlyphID16 first_glyphs[ARRAY_LENGTH_CONST (ligature_table)];
112
7
  unsigned int first_glyphs_indirection[ARRAY_LENGTH_CONST (ligature_table)];
113
7
  unsigned int ligature_per_first_glyph_count_list[ARRAY_LENGTH_CONST (first_glyphs)];
114
7
  unsigned int num_first_glyphs = 0;
115
116
  /* We know that all our ligatures have the same number of components. */
117
7
  OT::HBGlyphID16 ligature_list[ARRAY_LENGTH_CONST (first_glyphs) * ARRAY_LENGTH_CONST(ligature_table[0].ligatures)];
118
7
  unsigned int component_count_list[ARRAY_LENGTH_CONST (ligature_list)];
119
7
  OT::HBGlyphID16 component_list[ARRAY_LENGTH_CONST (ligature_list) *
120
7
         ARRAY_LENGTH_CONST (ligature_table[0].ligatures[0].components)];
121
7
  unsigned int num_ligatures = 0;
122
7
  unsigned int num_components = 0;
123
124
  /* Populate arrays */
125
126
  /* Sort out the first-glyphs */
127
14
  for (unsigned int first_glyph_idx = 0; first_glyph_idx < ARRAY_LENGTH (first_glyphs); first_glyph_idx++)
128
7
  {
129
7
    hb_codepoint_t first_u = ligature_table[first_glyph_idx].first;
130
7
    hb_codepoint_t first_glyph;
131
7
    if (!hb_font_get_glyph (font, first_u, 0, &first_glyph))
132
7
      continue;
133
0
    first_glyphs[num_first_glyphs] = first_glyph;
134
0
    ligature_per_first_glyph_count_list[num_first_glyphs] = 0;
135
0
    first_glyphs_indirection[num_first_glyphs] = first_glyph_idx;
136
0
    num_first_glyphs++;
137
0
  }
138
7
  hb_stable_sort (&first_glyphs[0], num_first_glyphs,
139
7
      (int(*)(const OT::HBUINT16*, const OT::HBUINT16 *)) OT::HBGlyphID16::cmp,
140
7
      &first_glyphs_indirection[0]);
141
142
  /* Now that the first-glyphs are sorted, walk again, populate ligatures. */
143
7
  for (unsigned int i = 0; i < num_first_glyphs; i++)
144
0
  {
145
0
    unsigned int first_glyph_idx = first_glyphs_indirection[i];
146
147
0
    for (unsigned int ligature_idx = 0; ligature_idx < ARRAY_LENGTH (ligature_table[0].ligatures); ligature_idx++)
148
0
    {
149
0
      hb_codepoint_t ligature_u = ligature_table[first_glyph_idx].ligatures[ligature_idx].ligature;
150
0
      hb_codepoint_t ligature_glyph;
151
0
      if (!hb_font_get_glyph (font, ligature_u, 0, &ligature_glyph))
152
0
  continue;
153
154
0
      const auto &components = ligature_table[first_glyph_idx].ligatures[ligature_idx].components;
155
0
      unsigned component_count = ARRAY_LENGTH_CONST (components);
156
157
0
      bool matched = true;
158
0
      for (unsigned j = 0; j < component_count; j++)
159
0
      {
160
0
  hb_codepoint_t component_u   = ligature_table[first_glyph_idx].ligatures[ligature_idx].components[j];
161
0
  hb_codepoint_t component_glyph;
162
0
  if (!component_u ||
163
0
      !hb_font_get_nominal_glyph (font, component_u, &component_glyph))
164
0
  {
165
0
    matched = false;
166
0
    break;
167
0
  }
168
169
0
  component_list[num_components++] = component_glyph;
170
0
      }
171
0
      if (!matched)
172
0
        continue;
173
174
0
      component_count_list[num_ligatures] = 1 + component_count;
175
0
      ligature_list[num_ligatures] = ligature_glyph;
176
177
0
      ligature_per_first_glyph_count_list[i]++;
178
179
0
      num_ligatures++;
180
0
    }
181
0
  }
182
183
7
  if (!num_ligatures)
184
7
    return nullptr;
185
186
187
  /* 16 bytes per ligature ought to be enough... */
188
0
  char buf[ARRAY_LENGTH_CONST (ligature_list) * 16 + 128];
189
0
  hb_serialize_context_t c (buf, sizeof (buf));
190
0
  OT::SubstLookup *lookup = c.start_serialize<OT::SubstLookup> ();
191
0
  bool ret = lookup->serialize_ligature (&c,
192
0
           lookup_flags,
193
0
           hb_sorted_array (first_glyphs, num_first_glyphs),
194
0
           hb_array (ligature_per_first_glyph_count_list, num_first_glyphs),
195
0
           hb_array (ligature_list, num_ligatures),
196
0
           hb_array (component_count_list, num_ligatures),
197
0
           hb_array (component_list, num_components));
198
0
  c.end_serialize ();
199
200
0
  return ret && !c.in_error () ? c.copy<OT::SubstLookup> () : nullptr;
201
7
}
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
49
{
208
49
  if (feature_index < 4)
209
28
    return arabic_fallback_synthesize_lookup_single (plan, font, feature_index);
210
21
  else
211
21
  {
212
21
    switch (feature_index) {
213
7
      case 4: return arabic_fallback_synthesize_lookup_ligature (plan, font, ligature_3_table, OT::LookupFlag::IgnoreMarks);
214
7
      case 5: return arabic_fallback_synthesize_lookup_ligature (plan, font, ligature_table, OT::LookupFlag::IgnoreMarks);
215
7
      case 6: return arabic_fallback_synthesize_lookup_ligature (plan, font, ligature_mark_table, 0);
216
21
    }
217
21
  }
218
49
  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
#ifndef HB_NO_WIN1256
235
#include "hb-ot-shaper-arabic-win1256.hh"
236
#endif
237
238
struct ManifestLookup
239
{
240
  public:
241
  OT::Tag tag;
242
  OT::Offset16To<OT::SubstLookup> lookupOffset;
243
  public:
244
  DEFINE_SIZE_STATIC (6);
245
};
246
typedef OT::Array16Of<ManifestLookup> Manifest;
247
248
static bool
249
arabic_fallback_plan_init_win1256 (arabic_fallback_plan_t *fallback_plan HB_UNUSED,
250
           const hb_ot_shape_plan_t *plan HB_UNUSED,
251
           hb_font_t *font HB_UNUSED)
252
7
{
253
7
#ifndef HB_NO_WIN1256
254
  /* Does this font look like it's Windows-1256-encoded? */
255
7
  hb_codepoint_t g;
256
7
  if (!(hb_font_get_glyph (font, 0x0627u, 0, &g) && g == 199 /* ALEF */ &&
257
0
  hb_font_get_glyph (font, 0x0644u, 0, &g) && g == 225 /* LAM */ &&
258
0
  hb_font_get_glyph (font, 0x0649u, 0, &g) && g == 236 /* ALEF MAKSURA */ &&
259
0
  hb_font_get_glyph (font, 0x064Au, 0, &g) && g == 237 /* YEH */ &&
260
0
  hb_font_get_glyph (font, 0x0652u, 0, &g) && g == 250 /* SUKUN */))
261
7
    return false;
262
263
0
  const Manifest &manifest = reinterpret_cast<const Manifest&> (arabic_win1256_gsub_lookups.manifest);
264
0
  static_assert (sizeof (arabic_win1256_gsub_lookups.manifestData) <=
265
0
     ARABIC_FALLBACK_MAX_LOOKUPS * sizeof (ManifestLookup), "");
266
267
0
  unsigned j = 0;
268
0
  unsigned int count = manifest.len;
269
0
  for (unsigned int i = 0; i < count; i++)
270
0
  {
271
0
    fallback_plan->mask_array[j] = plan->map.get_1_mask (manifest[i].tag);
272
0
    if (fallback_plan->mask_array[j])
273
0
    {
274
0
      fallback_plan->lookup_array[j] = const_cast<OT::SubstLookup*> (&(&manifest+manifest[i].lookupOffset));
275
0
      if (fallback_plan->lookup_array[j])
276
0
      {
277
0
  fallback_plan->accel_array[j] = OT::hb_ot_layout_lookup_accelerator_t::create (*fallback_plan->lookup_array[j]);
278
0
  j++;
279
0
      }
280
0
    }
281
0
  }
282
283
0
  fallback_plan->num_lookups = j;
284
0
  fallback_plan->free_lookups = false;
285
286
0
  return j > 0;
287
#else
288
  return false;
289
#endif
290
7
}
291
292
static bool
293
arabic_fallback_plan_init_unicode (arabic_fallback_plan_t *fallback_plan,
294
           const hb_ot_shape_plan_t *plan,
295
           hb_font_t *font)
296
7
{
297
7
  static_assert ((ARRAY_LENGTH_CONST (arabic_fallback_features) <= ARABIC_FALLBACK_MAX_LOOKUPS), "");
298
7
  unsigned int j = 0;
299
56
  for (unsigned int i = 0; i < ARRAY_LENGTH(arabic_fallback_features) ; i++)
300
49
  {
301
49
    fallback_plan->mask_array[j] = plan->map.get_1_mask (arabic_fallback_features[i]);
302
49
    if (fallback_plan->mask_array[j])
303
49
    {
304
49
      fallback_plan->lookup_array[j] = arabic_fallback_synthesize_lookup (plan, font, i);
305
49
      if (fallback_plan->lookup_array[j])
306
0
      {
307
0
  fallback_plan->accel_array[j] = OT::hb_ot_layout_lookup_accelerator_t::create (*fallback_plan->lookup_array[j]);
308
0
  j++;
309
0
      }
310
49
    }
311
49
  }
312
313
7
  fallback_plan->num_lookups = j;
314
7
  fallback_plan->free_lookups = true;
315
316
7
  return j > 0;
317
7
}
318
319
static arabic_fallback_plan_t *
320
arabic_fallback_plan_create (const hb_ot_shape_plan_t *plan,
321
           hb_font_t *font)
322
7
{
323
7
  arabic_fallback_plan_t *fallback_plan = (arabic_fallback_plan_t *) hb_calloc (1, sizeof (arabic_fallback_plan_t));
324
7
  if (unlikely (!fallback_plan))
325
0
    return const_cast<arabic_fallback_plan_t *> (&Null (arabic_fallback_plan_t));
326
327
7
  fallback_plan->num_lookups = 0;
328
7
  fallback_plan->free_lookups = false;
329
330
  /* Try synthesizing GSUB table using Unicode Arabic Presentation Forms,
331
   * in case the font has cmap entries for the presentation-forms characters. */
332
7
  if (arabic_fallback_plan_init_unicode (fallback_plan, plan, font))
333
0
    return fallback_plan;
334
335
  /* See if this looks like a Windows-1256-encoded font.  If it does, use a
336
   * hand-coded GSUB table. */
337
7
  if (arabic_fallback_plan_init_win1256 (fallback_plan, plan, font))
338
0
    return fallback_plan;
339
340
7
  assert (fallback_plan->num_lookups == 0);
341
7
  hb_free (fallback_plan);
342
7
  return const_cast<arabic_fallback_plan_t *> (&Null (arabic_fallback_plan_t));
343
7
}
344
345
static void
346
arabic_fallback_plan_destroy (arabic_fallback_plan_t *fallback_plan)
347
12
{
348
12
  if (!fallback_plan || fallback_plan->num_lookups == 0)
349
12
    return;
350
351
0
  for (unsigned int i = 0; i < fallback_plan->num_lookups; i++)
352
0
    if (fallback_plan->lookup_array[i])
353
0
    {
354
0
      if (fallback_plan->accel_array[i])
355
0
  fallback_plan->accel_array[i]->fini ();
356
0
      hb_free (fallback_plan->accel_array[i]);
357
0
      if (fallback_plan->free_lookups)
358
0
  hb_free (fallback_plan->lookup_array[i]);
359
0
    }
360
361
0
  hb_free (fallback_plan);
362
0
}
363
364
static void
365
arabic_fallback_plan_shape (arabic_fallback_plan_t *fallback_plan,
366
          hb_font_t *font,
367
          hb_buffer_t *buffer)
368
2.77k
{
369
2.77k
  OT::hb_ot_apply_context_t c (0, font, buffer, hb_blob_get_empty ());
370
2.77k
  for (unsigned int i = 0; i < fallback_plan->num_lookups; i++)
371
0
    if (fallback_plan->lookup_array[i]) {
372
0
      c.set_lookup_mask (fallback_plan->mask_array[i]);
373
0
      if (fallback_plan->accel_array[i])
374
0
  hb_ot_layout_substitute_lookup (&c,
375
0
          *fallback_plan->lookup_array[i],
376
0
          *fallback_plan->accel_array[i]);
377
0
    }
378
2.77k
}
379
380
381
#endif /* HB_OT_SHAPER_ARABIC_FALLBACK_HH */