Coverage Report

Created: 2026-08-14 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/OT/Layout/GPOS/GPOS.hh
Line
Count
Source
1
#ifndef OT_LAYOUT_GPOS_GPOS_HH
2
#define OT_LAYOUT_GPOS_GPOS_HH
3
4
#include "../../../hb-ot-layout-common.hh"
5
#include "../../../hb-ot-layout-gsubgpos.hh"
6
#include "Common.hh"
7
#include "PosLookup.hh"
8
9
namespace OT {
10
11
using Layout::GPOS_impl::PosLookup;
12
13
namespace Layout {
14
15
static void
16
propagate_attachment_offsets (hb_glyph_position_t *pos,
17
                              unsigned int len,
18
                              unsigned int i,
19
                              hb_direction_t direction,
20
                              unsigned nesting_level = HB_MAX_NESTING_LEVEL);
21
22
/*
23
 * GPOS -- Glyph Positioning
24
 * https://docs.microsoft.com/en-us/typography/opentype/spec/gpos
25
 */
26
27
struct GPOS : GSUBGPOS
28
{
29
  static constexpr hb_tag_t tableTag = HB_OT_TAG_GPOS;
30
31
  using Lookup = PosLookup;
32
33
  const PosLookup& get_lookup (unsigned int i) const
34
50
  { return static_cast<const PosLookup &> (GSUBGPOS::get_lookup (i)); }
35
36
  static inline void position_start (hb_font_t *font, hb_buffer_t *buffer);
37
  static inline void position_finish_advances (hb_font_t *font, hb_buffer_t *buffer);
38
  static inline void position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer);
39
40
  bool subset (hb_subset_context_t *c) const
41
0
  {
42
0
    hb_subset_layout_context_t l (c, tableTag);
43
0
    return GSUBGPOS::subset<PosLookup> (&l);
44
0
  }
45
46
  bool sanitize (hb_sanitize_context_t *c) const
47
1.92k
  {
48
1.92k
    TRACE_SANITIZE (this);
49
1.92k
    return_trace (GSUBGPOS::sanitize<PosLookup> (c));
50
1.92k
  }
51
52
  HB_INTERNAL bool is_blocklisted (hb_blob_t *blob,
53
                                   hb_face_t *face) const;
54
55
  void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
56
0
  {
57
0
    for (unsigned i = 0; i < GSUBGPOS::get_lookup_count (); i++)
58
0
    {
59
0
      if (!c->gpos_lookups->has (i)) continue;
60
0
      const PosLookup &l = get_lookup (i);
61
0
      l.dispatch (c);
62
0
    }
63
0
  }
64
65
  void closure_lookups (hb_face_t      *face,
66
                        const hb_set_t *glyphs,
67
                        hb_set_t       *lookup_indexes /* IN/OUT */) const
68
0
  { GSUBGPOS::closure_lookups<PosLookup> (face, glyphs, lookup_indexes); }
69
70
  typedef GSUBGPOS::accelerator_t<GPOS> accelerator_t;
71
};
72
73
74
static void
75
propagate_attachment_offsets (hb_glyph_position_t *pos,
76
                              unsigned int len,
77
                              unsigned int i,
78
                              hb_direction_t direction,
79
                              unsigned nesting_level)
80
0
{
81
  /* Adjusts offsets of attached glyphs (both cursive and mark) to accumulate
82
   * offset of glyph they are attached to. */
83
0
  int chain = pos[i].attach_chain();
84
0
  int type = pos[i].attach_type();
85
86
0
  pos[i].attach_chain() = 0;
87
88
0
  unsigned int j = (int) i + chain;
89
90
0
  if (unlikely (j >= len))
91
0
    return;
92
93
0
  if (unlikely (!nesting_level))
94
0
    return;
95
96
0
  if (pos[j].attach_chain())
97
0
    propagate_attachment_offsets (pos, len, j, direction, nesting_level - 1);
98
99
0
  assert (!!(type & GPOS_impl::ATTACH_TYPE_MARK) ^ !!(type & GPOS_impl::ATTACH_TYPE_CURSIVE));
100
101
0
  if (type & GPOS_impl::ATTACH_TYPE_CURSIVE)
102
0
  {
103
0
    if (HB_DIRECTION_IS_HORIZONTAL (direction))
104
0
      pos[i].y_offset += pos[j].y_offset;
105
0
    else
106
0
      pos[i].x_offset += pos[j].x_offset;
107
0
  }
108
0
  else /*if (type & GPOS_impl::ATTACH_TYPE_MARK)*/
109
0
  {
110
0
    if (HB_DIRECTION_IS_HORIZONTAL (direction))
111
0
      pos[i].x_offset += pos[j].x_offset;
112
0
    else
113
0
      pos[i].y_offset += pos[j].y_offset;
114
115
    // i is the position of the mark; j is the base.
116
0
    if (j < i)
117
0
    {
118
      /* This is the common case: mark follows base.
119
       * And currently the only way in OpenType. */
120
0
      if (HB_DIRECTION_IS_FORWARD (direction))
121
0
  for (unsigned int k = j; k < i; k++) {
122
0
    pos[i].x_offset -= pos[k].x_advance;
123
0
    pos[i].y_offset -= pos[k].y_advance;
124
0
  }
125
0
      else
126
0
  for (unsigned int k = j + 1; k < i + 1; k++) {
127
0
    pos[i].x_offset += pos[k].x_advance;
128
0
    pos[i].y_offset += pos[k].y_advance;
129
0
  }
130
0
    }
131
0
    else // j > i
132
0
    {
133
      /* This can happen with `kerx`: a mark attaching
134
       * to a base after it in the logical order. */
135
0
      if (HB_DIRECTION_IS_FORWARD (direction))
136
0
  for (unsigned int k = i; k < j; k++) {
137
0
    pos[i].x_offset += pos[k].x_advance;
138
0
    pos[i].y_offset += pos[k].y_advance;
139
0
  }
140
0
      else
141
0
  for (unsigned int k = i + 1; k < j + 1; k++) {
142
0
    pos[i].x_offset -= pos[k].x_advance;
143
0
    pos[i].y_offset -= pos[k].y_advance;
144
0
  }
145
0
    }
146
0
  }
147
0
}
Unexecuted instantiation: hb-ot-face.cc:OT::Layout::propagate_attachment_offsets(hb_glyph_position_t*, unsigned int, unsigned int, hb_direction_t, unsigned int)
Unexecuted instantiation: hb-aat-layout.cc:OT::Layout::propagate_attachment_offsets(hb_glyph_position_t*, unsigned int, unsigned int, hb_direction_t, unsigned int)
Unexecuted instantiation: hb-ot-layout.cc:OT::Layout::propagate_attachment_offsets(hb_glyph_position_t*, unsigned int, unsigned int, hb_direction_t, unsigned int)
Unexecuted instantiation: hb-ot-shape-fallback.cc:OT::Layout::propagate_attachment_offsets(hb_glyph_position_t*, unsigned int, unsigned int, hb_direction_t, unsigned int)
148
149
void
150
GPOS::position_start (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
151
239k
{
152
239k
  unsigned int count = buffer->len;
153
2.16M
  for (unsigned int i = 0; i < count; i++)
154
1.92M
    buffer->pos[i].attach_chain() = buffer->pos[i].attach_type() = 0;
155
239k
}
156
157
void
158
GPOS::position_finish_advances (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer HB_UNUSED)
159
239k
{
160
  //_hb_buffer_assert_gsubgpos_vars (buffer);
161
239k
}
162
163
void
164
GPOS::position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer)
165
239k
{
166
239k
  _hb_buffer_assert_gsubgpos_vars (buffer);
167
168
239k
  unsigned int len;
169
239k
  hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &len);
170
239k
  hb_direction_t direction = buffer->props.direction;
171
172
  /* Handle attachments */
173
239k
  if (buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT)
174
0
  {
175
0
    auto *pos = buffer->pos;
176
    // https://github.com/harfbuzz/harfbuzz/issues/5514
177
0
    if (HB_DIRECTION_IS_FORWARD (direction))
178
0
    {
179
0
      for (unsigned i = 0; i < len; i++)
180
0
  if (pos[i].attach_chain())
181
0
    propagate_attachment_offsets (pos, len, i, direction);
182
0
    } else {
183
0
      for (unsigned i = len; i-- > 0; )
184
0
  if (pos[i].attach_chain())
185
0
    propagate_attachment_offsets (pos, len, i, direction);
186
0
    }
187
0
  }
188
189
239k
  if (unlikely (font->slant_xy) &&
190
0
      HB_DIRECTION_IS_HORIZONTAL (direction))
191
0
  {
192
    /* Slanting shaping results is only supported for horizontal text,
193
     * as it gets weird otherwise. */
194
0
    for (unsigned i = 0; i < len; i++)
195
0
      if (unlikely (pos[i].y_offset))
196
0
        pos[i].x_offset += roundf (font->slant_xy * pos[i].y_offset);
197
0
  }
198
239k
}
199
200
}
201
202
struct GPOS_accelerator_t : Layout::GPOS::accelerator_t {
203
1.92k
  GPOS_accelerator_t (hb_face_t *face) : Layout::GPOS::accelerator_t (face) {}
204
};
205
206
}
207
208
#endif  /* OT_LAYOUT_GPOS_GPOS_HH */