Coverage Report

Created: 2023-09-25 06:24

/src/harfbuzz/src/OT/Layout/GPOS/MarkBasePosFormat1.hh
Line
Count
Source (jump to first uncovered line)
1
#ifndef OT_LAYOUT_GPOS_MARKBASEPOSFORMAT1_HH
2
#define OT_LAYOUT_GPOS_MARKBASEPOSFORMAT1_HH
3
4
#include "MarkArray.hh"
5
6
namespace OT {
7
namespace Layout {
8
namespace GPOS_impl {
9
10
typedef AnchorMatrix BaseArray;         /* base-major--
11
                                         * in order of BaseCoverage Index--,
12
                                         * mark-minor--
13
                                         * ordered by class--zero-based. */
14
15
template <typename Types>
16
struct MarkBasePosFormat1_2
17
{
18
  protected:
19
  HBUINT16      format;                 /* Format identifier--format = 1 */
20
  typename Types::template OffsetTo<Coverage>
21
                markCoverage;           /* Offset to MarkCoverage table--from
22
                                         * beginning of MarkBasePos subtable */
23
  typename Types::template OffsetTo<Coverage>
24
                baseCoverage;           /* Offset to BaseCoverage table--from
25
                                         * beginning of MarkBasePos subtable */
26
  HBUINT16      classCount;             /* Number of classes defined for marks */
27
  typename Types::template OffsetTo<MarkArray>
28
                markArray;              /* Offset to MarkArray table--from
29
                                         * beginning of MarkBasePos subtable */
30
  typename Types::template OffsetTo<BaseArray>
31
                baseArray;              /* Offset to BaseArray table--from
32
                                         * beginning of MarkBasePos subtable */
33
34
  public:
35
  DEFINE_SIZE_STATIC (4 + 4 * Types::size);
36
37
    bool sanitize (hb_sanitize_context_t *c) const
38
4
  {
39
4
    TRACE_SANITIZE (this);
40
4
    return_trace (c->check_struct (this) &&
41
4
                  markCoverage.sanitize (c, this) &&
42
4
                  baseCoverage.sanitize (c, this) &&
43
4
                  markArray.sanitize (c, this) &&
44
4
                  baseArray.sanitize (c, this, (unsigned int) classCount));
45
4
  }
46
47
  bool intersects (const hb_set_t *glyphs) const
48
0
  {
49
0
    return (this+markCoverage).intersects (glyphs) &&
50
0
           (this+baseCoverage).intersects (glyphs);
51
0
  }
52
53
0
  void closure_lookups (hb_closure_lookups_context_t *c) const {}
54
55
  void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
56
0
  {
57
0
    + hb_zip (this+markCoverage, this+markArray)
58
0
    | hb_filter (c->glyph_set, hb_first)
59
0
    | hb_map (hb_second)
60
0
    | hb_apply ([&] (const MarkRecord& record) { record.collect_variation_indices (c, &(this+markArray)); })
61
0
    ;
62
0
63
0
    hb_map_t klass_mapping;
64
0
    Markclass_closure_and_remap_indexes (this+markCoverage, this+markArray, *c->glyph_set, &klass_mapping);
65
0
66
0
    unsigned basecount = (this+baseArray).rows;
67
0
    auto base_iter =
68
0
    + hb_zip (this+baseCoverage, hb_range (basecount))
69
0
    | hb_filter (c->glyph_set, hb_first)
70
0
    | hb_map (hb_second)
71
0
    ;
72
0
73
0
    hb_sorted_vector_t<unsigned> base_indexes;
74
0
    for (const unsigned row : base_iter)
75
0
    {
76
0
      + hb_range ((unsigned) classCount)
77
0
      | hb_filter (klass_mapping)
78
0
      | hb_map ([&] (const unsigned col) { return row * (unsigned) classCount + col; })
79
0
      | hb_sink (base_indexes)
80
0
      ;
81
0
    }
82
0
    (this+baseArray).collect_variation_indices (c, base_indexes.iter ());
83
0
  }
84
85
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
86
0
  {
87
0
    if (unlikely (!(this+markCoverage).collect_coverage (c->input))) return;
88
0
    if (unlikely (!(this+baseCoverage).collect_coverage (c->input))) return;
89
0
  }
90
91
4
  const Coverage &get_coverage () const { return this+markCoverage; }
92
93
  static inline bool accept (hb_buffer_t *buffer, unsigned idx)
94
0
  {
95
    /* We only want to attach to the first of a MultipleSubst sequence.
96
     * https://github.com/harfbuzz/harfbuzz/issues/740
97
     * Reject others...
98
     * ...but stop if we find a mark in the MultipleSubst sequence:
99
     * https://github.com/harfbuzz/harfbuzz/issues/1020 */
100
0
    return !_hb_glyph_info_multiplied (&buffer->info[idx]) ||
101
0
     0 == _hb_glyph_info_get_lig_comp (&buffer->info[idx]) ||
102
0
     (idx == 0 ||
103
0
      _hb_glyph_info_is_mark (&buffer->info[idx - 1]) ||
104
0
      !_hb_glyph_info_multiplied (&buffer->info[idx - 1]) ||
105
0
      _hb_glyph_info_get_lig_id (&buffer->info[idx]) !=
106
0
      _hb_glyph_info_get_lig_id (&buffer->info[idx - 1]) ||
107
0
      _hb_glyph_info_get_lig_comp (&buffer->info[idx]) !=
108
0
      _hb_glyph_info_get_lig_comp (&buffer->info[idx - 1]) + 1
109
0
      );
110
0
  }
111
112
  bool apply (hb_ot_apply_context_t *c) const
113
0
  {
114
0
    TRACE_APPLY (this);
115
0
    hb_buffer_t *buffer = c->buffer;
116
0
    unsigned int mark_index = (this+markCoverage).get_coverage  (buffer->cur().codepoint);
117
0
    if (likely (mark_index == NOT_COVERED)) return_trace (false);
118
119
    /* Now we search backwards for a non-mark glyph.
120
     * We don't use skippy_iter.prev() to avoid O(n^2) behavior. */
121
122
0
    hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
123
0
    skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
124
125
0
    if (c->last_base_until > buffer->idx)
126
0
    {
127
0
      c->last_base_until = 0;
128
0
      c->last_base = -1;
129
0
    }
130
0
    unsigned j;
131
0
    for (j = buffer->idx; j > c->last_base_until; j--)
132
0
    {
133
0
      auto match = skippy_iter.match (buffer->info[j - 1]);
134
0
      if (match == skippy_iter.MATCH)
135
0
      {
136
        // https://github.com/harfbuzz/harfbuzz/issues/4124
137
0
  if (!accept (buffer, j - 1) &&
138
0
      NOT_COVERED == (this+baseCoverage).get_coverage  (buffer->info[j - 1].codepoint))
139
0
    match = skippy_iter.SKIP;
140
0
      }
141
0
      if (match == skippy_iter.MATCH)
142
0
      {
143
0
  c->last_base = (signed) j - 1;
144
0
  break;
145
0
      }
146
0
    }
147
0
    c->last_base_until = buffer->idx;
148
0
    if (c->last_base == -1)
149
0
    {
150
0
      buffer->unsafe_to_concat_from_outbuffer (0, buffer->idx + 1);
151
0
      return_trace (false);
152
0
    }
153
154
0
    unsigned idx = (unsigned) c->last_base;
155
156
    /* Checking that matched glyph is actually a base glyph by GDEF is too strong; disabled */
157
    //if (!_hb_glyph_info_is_base_glyph (&buffer->info[idx])) { return_trace (false); }
158
159
0
    unsigned int base_index = (this+baseCoverage).get_coverage  (buffer->info[idx].codepoint);
160
0
    if (base_index == NOT_COVERED)
161
0
    {
162
0
      buffer->unsafe_to_concat_from_outbuffer (idx, buffer->idx + 1);
163
0
      return_trace (false);
164
0
    }
165
166
0
    return_trace ((this+markArray).apply (c, mark_index, base_index, this+baseArray, classCount, idx));
167
0
  }
168
169
  bool subset (hb_subset_context_t *c) const
170
0
  {
171
0
    TRACE_SUBSET (this);
172
0
    const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
173
0
    const hb_map_t &glyph_map = *c->plan->glyph_map;
174
0
175
0
    auto *out = c->serializer->start_embed (*this);
176
0
    if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
177
0
    out->format = format;
178
0
179
0
    hb_map_t klass_mapping;
180
0
    Markclass_closure_and_remap_indexes (this+markCoverage, this+markArray, glyphset, &klass_mapping);
181
0
182
0
    if (!klass_mapping.get_population ()) return_trace (false);
183
0
    out->classCount = klass_mapping.get_population ();
184
0
185
0
    auto mark_iter =
186
0
    + hb_zip (this+markCoverage, this+markArray)
187
0
    | hb_filter (glyphset, hb_first)
188
0
    ;
189
0
190
0
    hb_sorted_vector_t<hb_codepoint_t> new_coverage;
191
0
    + mark_iter
192
0
    | hb_map (hb_first)
193
0
    | hb_map (glyph_map)
194
0
    | hb_sink (new_coverage)
195
0
    ;
196
0
197
0
    if (!out->markCoverage.serialize_serialize (c->serializer, new_coverage.iter ()))
198
0
      return_trace (false);
199
0
200
0
    if (unlikely (!out->markArray.serialize_subset (c, markArray, this,
201
0
                (this+markCoverage).iter (),
202
0
                &klass_mapping)))
203
0
      return_trace (false);
204
0
205
0
    unsigned basecount = (this+baseArray).rows;
206
0
    auto base_iter =
207
0
    + hb_zip (this+baseCoverage, hb_range (basecount))
208
0
    | hb_filter (glyphset, hb_first)
209
0
    ;
210
0
211
0
    new_coverage.reset ();
212
0
    + base_iter
213
0
    | hb_map (hb_first)
214
0
    | hb_map (glyph_map)
215
0
    | hb_sink (new_coverage)
216
0
    ;
217
0
218
0
    if (!out->baseCoverage.serialize_serialize (c->serializer, new_coverage.iter ()))
219
0
      return_trace (false);
220
0
221
0
    hb_sorted_vector_t<unsigned> base_indexes;
222
0
    for (const unsigned row : + base_iter
223
0
                              | hb_map (hb_second))
224
0
    {
225
0
      + hb_range ((unsigned) classCount)
226
0
      | hb_filter (klass_mapping)
227
0
      | hb_map ([&] (const unsigned col) { return row * (unsigned) classCount + col; })
228
0
      | hb_sink (base_indexes)
229
0
      ;
230
0
    }
231
0
232
0
    return_trace (out->baseArray.serialize_subset (c, baseArray, this,
233
0
               base_iter.len (),
234
0
               base_indexes.iter ()));
235
0
  }
236
};
237
238
239
}
240
}
241
}
242
243
#endif /* OT_LAYOUT_GPOS_MARKBASEPOSFORMAT1_HH */