Coverage Report

Created: 2025-10-10 06:17

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
21
  { 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.75k
  {
48
1.75k
    TRACE_SANITIZE (this);
49
1.75k
    return_trace (GSUBGPOS::sanitize<PosLookup> (c));
50
1.75k
  }
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
    pos[i].x_offset += pos[j].x_offset;
111
0
    pos[i].y_offset += pos[j].y_offset;
112
113
    // i is the position of the mark; j is the base.
114
0
    if (j < i)
115
0
    {
116
      /* This is the common case: mark follows base.
117
       * And currently the only way in OpenType. */
118
0
      if (HB_DIRECTION_IS_FORWARD (direction))
119
0
  for (unsigned int k = j; k < i; k++) {
120
0
    pos[i].x_offset -= pos[k].x_advance;
121
0
    pos[i].y_offset -= pos[k].y_advance;
122
0
  }
123
0
      else
124
0
  for (unsigned int k = j + 1; k < i + 1; k++) {
125
0
    pos[i].x_offset += pos[k].x_advance;
126
0
    pos[i].y_offset += pos[k].y_advance;
127
0
  }
128
0
    }
129
0
    else // j > i
130
0
    {
131
      /* This can happen with `kerx`: a mark attaching
132
       * to a base after it in the logical order. */
133
0
      if (HB_DIRECTION_IS_FORWARD (direction))
134
0
  for (unsigned int k = i; k < j; k++) {
135
0
    pos[i].x_offset += pos[k].x_advance;
136
0
    pos[i].y_offset += pos[k].y_advance;
137
0
  }
138
0
      else
139
0
  for (unsigned int k = i + 1; k < j + 1; k++) {
140
0
    pos[i].x_offset -= pos[k].x_advance;
141
0
    pos[i].y_offset -= pos[k].y_advance;
142
0
  }
143
0
    }
144
0
  }
145
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)
146
147
void
148
GPOS::position_start (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
149
100k
{
150
100k
  unsigned int count = buffer->len;
151
763k
  for (unsigned int i = 0; i < count; i++)
152
662k
    buffer->pos[i].attach_chain() = buffer->pos[i].attach_type() = 0;
153
100k
}
154
155
void
156
GPOS::position_finish_advances (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer HB_UNUSED)
157
100k
{
158
  //_hb_buffer_assert_gsubgpos_vars (buffer);
159
100k
}
160
161
void
162
GPOS::position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer)
163
100k
{
164
100k
  _hb_buffer_assert_gsubgpos_vars (buffer);
165
166
100k
  unsigned int len;
167
100k
  hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &len);
168
100k
  hb_direction_t direction = buffer->props.direction;
169
170
  /* Handle attachments */
171
100k
  if (buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT)
172
0
  {
173
0
    auto *pos = buffer->pos;
174
    // https://github.com/harfbuzz/harfbuzz/issues/5514
175
0
    if (HB_DIRECTION_IS_FORWARD (direction))
176
0
    {
177
0
      for (unsigned i = 0; i < len; i++)
178
0
  if (pos[i].attach_chain())
179
0
    propagate_attachment_offsets (pos, len, i, direction);
180
0
    } else {
181
0
      for (unsigned i = len; i-- > 0; )
182
0
  if (pos[i].attach_chain())
183
0
    propagate_attachment_offsets (pos, len, i, direction);
184
0
    }
185
0
  }
186
187
100k
  if (unlikely (font->slant_xy) &&
188
0
      HB_DIRECTION_IS_HORIZONTAL (direction))
189
0
  {
190
    /* Slanting shaping results is only supported for horizontal text,
191
     * as it gets weird otherwise. */
192
0
    for (unsigned i = 0; i < len; i++)
193
0
      if (unlikely (pos[i].y_offset))
194
0
        pos[i].x_offset += roundf (font->slant_xy * pos[i].y_offset);
195
0
  }
196
100k
}
197
198
}
199
200
struct GPOS_accelerator_t : Layout::GPOS::accelerator_t {
201
1.75k
  GPOS_accelerator_t (hb_face_t *face) : Layout::GPOS::accelerator_t (face) {}
202
};
203
204
}
205
206
#endif  /* OT_LAYOUT_GPOS_GPOS_HH */