Coverage Report

Created: 2026-10-30 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/hb-aat-layout-trak-table.hh
Line
Count
Source
1
/*
2
 * Copyright © 2018  Ebrahim Byagowi
3
 * Copyright © 2018  Google, Inc.
4
 *
5
 *  This is part of HarfBuzz, a text shaping library.
6
 *
7
 * Permission is hereby granted, without written agreement and without
8
 * license or royalty fees, to use, copy, modify, and distribute this
9
 * software and its documentation for any purpose, provided that the
10
 * above copyright notice and the following two paragraphs appear in
11
 * all copies of this software.
12
 *
13
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17
 * DAMAGE.
18
 *
19
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24
 *
25
 * Google Author(s): Behdad Esfahbod
26
 */
27
28
#ifndef HB_AAT_LAYOUT_TRAK_TABLE_HH
29
#define HB_AAT_LAYOUT_TRAK_TABLE_HH
30
31
#include "hb-aat-layout-common.hh"
32
#include "hb-ot-layout.hh"
33
#include "hb-open-type.hh"
34
#include "hb-ot-stat-table.hh"
35
36
/*
37
 * trak -- Tracking
38
 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6trak.html
39
 */
40
#define HB_AAT_TAG_trak HB_TAG('t','r','a','k')
41
42
43
namespace AAT {
44
45
46
struct TrackTableEntry
47
{
48
  friend struct TrackData;
49
50
0
  float get_track_value () const { return track.to_float (); }
51
52
  float interpolate_at (unsigned int idx,
53
      float ptem,
54
      const void *base,
55
      hb_array_t<const F16DOT16> size_table) const
56
0
  {
57
0
    const FWORD *values = (base+valuesZ).arrayZ;
58
59
0
    float s0 = size_table[idx].to_float ();
60
0
    float s1 = size_table[idx + 1].to_float ();
61
0
    int v0 = values[idx];
62
0
    int v1 = values[idx + 1];
63
64
    // Deal with font bugs.
65
0
    if (unlikely (s1 < s0))
66
0
    { hb_swap (s0, s1); hb_swap (v0, v1); }
67
0
    if (unlikely (ptem < s0)) return v0;
68
0
    if (unlikely (ptem > s1)) return v1;
69
0
    if (unlikely (s0 == s1)) return (v0 + v1) * 0.5f;
70
71
0
    float t = (ptem - s0) / (s1 - s0);
72
0
    return v0 + t * (v1 - v0);
73
0
  }
74
75
  float get_value (float ptem,
76
       const void *base,
77
       hb_array_t<const F16DOT16> size_table) const
78
0
  {
79
0
    const FWORD *values = (base+valuesZ).arrayZ;
80
81
0
    unsigned int n_sizes = size_table.length;
82
83
    /*
84
     * Choose size.
85
     */
86
0
    if (!n_sizes) return 0.f;
87
0
    if (n_sizes == 1) return values[0];
88
89
    // At least two entries.
90
91
0
    unsigned i;
92
0
    for (i = 0; i < n_sizes; i++)
93
0
      if (size_table[i].to_float () >= ptem)
94
0
  break;
95
96
    // Boundary conditions.
97
0
    if (i == 0)       return values[0];
98
0
    if (i == n_sizes) return values[n_sizes - 1];
99
100
    // Exact match.
101
0
    if (size_table[i].to_float () == ptem) return values[i];
102
103
    // Interpolate.
104
0
    return interpolate_at (i - 1, ptem, base, size_table);
105
0
  }
106
107
  public:
108
  bool sanitize (hb_sanitize_context_t *c,
109
     const void *base,
110
     unsigned int n_sizes) const
111
0
  {
112
0
    TRACE_SANITIZE (this);
113
0
    return_trace (likely (c->check_struct (this) &&
114
0
        (valuesZ.sanitize (c, base, n_sizes))));
115
0
  }
116
117
  protected:
118
  F16DOT16  track;    /* Track value for this record. */
119
  OT::NameID  trackNameID;  /* The 'name' table index for this track.
120
         * (a short word or phrase like "loose"
121
         * or "very tight") */
122
  NNOffset16To<UnsizedArrayOf<FWORD>>
123
    valuesZ;  /* Offset from start of tracking table to
124
         * per-size tracking values for this track. */
125
126
  public:
127
  DEFINE_SIZE_STATIC (8);
128
};
129
130
struct TrackData
131
{
132
  float get_tracking (const void *base, float ptem, float track = 0.f) const
133
0
  {
134
0
    unsigned count = nTracks;
135
0
    hb_array_t<const F16DOT16> size_table = (base+sizeTable).as_array (nSizes);
136
137
0
    if (!count) return 0.f;
138
0
    if (count == 1) return trackTable[0].get_value (ptem, base, size_table);
139
140
    // At least two entries.
141
142
0
    unsigned i = 0;
143
0
    unsigned j = count - 1;
144
145
    // Find the two entries that track is between.
146
0
    while (i + 1 < count && trackTable[i + 1].get_track_value () <= track)
147
0
      i++;
148
0
    while (j > 0 && trackTable[j - 1].get_track_value () >= track)
149
0
      j--;
150
151
    // Exact match.
152
0
    if (i == j) return trackTable[i].get_value (ptem, base, size_table);
153
154
    // Interpolate.
155
156
0
    float t0 = trackTable[i].get_track_value ();
157
0
    float t1 = trackTable[j].get_track_value ();
158
159
0
    float a = trackTable[i].get_value (ptem, base, size_table);
160
0
    float b = trackTable[j].get_value (ptem, base, size_table);
161
162
    // Deal with font bugs.
163
0
    if (unlikely (t0 == t1)) return (a + b) * 0.5f;
164
165
0
    float t = (track - t0) / (t1 - t0);
166
0
    return a + t * (b - a);
167
0
  }
168
169
  bool sanitize (hb_sanitize_context_t *c, const void *base) const
170
0
  {
171
0
    TRACE_SANITIZE (this);
172
0
    return_trace (likely (c->check_struct (this) &&
173
0
        hb_barrier () &&
174
0
        sizeTable.sanitize (c, base, nSizes) &&
175
0
        trackTable.sanitize (c, nTracks, base, nSizes)));
176
0
  }
177
178
  protected:
179
  HBUINT16  nTracks;  /* Number of separate tracks included in this table. */
180
  HBUINT16  nSizes;   /* Number of point sizes included in this table. */
181
  NNOffset32To<UnsizedArrayOf<F16DOT16>>
182
    sizeTable;  /* Offset from start of the tracking table to
183
         * Array[nSizes] of size values.. */
184
  UnsizedArrayOf<TrackTableEntry>
185
    trackTable; /* Array[nTracks] of TrackTableEntry records. */
186
187
  public:
188
  DEFINE_SIZE_ARRAY (8, trackTable);
189
};
190
191
struct trak
192
{
193
  static constexpr hb_tag_t tableTag = HB_AAT_TAG_trak;
194
195
1.72k
  bool has_data () const { return version.to_int (); }
196
197
  hb_position_t get_h_tracking (hb_font_t *font, float track = 0.f) const
198
0
  {
199
0
    float ptem = font->ptem > 0.f ? font->ptem : HB_CORETEXT_DEFAULT_FONT_SIZE;
200
0
    return font->em_scalef_x ((this+horizData).get_tracking (this, ptem, track));
201
0
  }
202
  hb_position_t get_v_tracking (hb_font_t *font, float track = 0.f) const
203
0
  {
204
0
    float ptem = font->ptem > 0.f ? font->ptem : HB_CORETEXT_DEFAULT_FONT_SIZE;
205
0
    return font->em_scalef_y ((this+vertData).get_tracking (this, ptem, track));
206
0
  }
207
  hb_position_t get_tracking (hb_font_t *font, hb_direction_t dir, float track = 0.f) const
208
0
  {
209
0
#ifndef HB_NO_STYLE
210
0
    if (!font->face->table.STAT->has_data ())
211
0
      return 0;
212
0
    return HB_DIRECTION_IS_HORIZONTAL (dir) ?
213
0
      get_h_tracking (font, track) :
214
0
      get_v_tracking (font, track);
215
0
#else
216
0
    return 0;
217
0
#endif
218
0
  }
219
220
  bool apply (hb_aat_apply_context_t *c, float track = 0.f) const
221
0
  {
222
0
    TRACE_APPLY (this);
223
224
0
    float ptem = c->font->ptem;
225
0
    if (unlikely (ptem <= 0.f))
226
0
    {
227
      /* https://developer.apple.com/documentation/coretext/1508745-ctfontcreatewithgraphicsfont */
228
0
      ptem = HB_CORETEXT_DEFAULT_FONT_SIZE;
229
0
    }
230
231
0
    hb_buffer_t *buffer = c->buffer;
232
0
    if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
233
0
    {
234
0
      hb_position_t advance_to_add = get_h_tracking (c->font, track);
235
0
      foreach_grapheme (buffer, start, end)
236
0
  buffer->pos[start].x_advance = hb_saturate_add (buffer->pos[start].x_advance, advance_to_add);
237
0
    }
238
0
    else
239
0
    {
240
0
      hb_position_t advance_to_add = get_v_tracking (c->font, track);
241
0
      foreach_grapheme (buffer, start, end)
242
0
  buffer->pos[start].y_advance = hb_saturate_add (buffer->pos[start].y_advance, advance_to_add);
243
0
    }
244
245
0
    return_trace (true);
246
0
  }
247
248
  bool sanitize (hb_sanitize_context_t *c) const
249
0
  {
250
0
    TRACE_SANITIZE (this);
251
252
0
    return_trace (likely (c->check_struct (this) &&
253
0
        hb_barrier () &&
254
0
        version.major == 1 &&
255
0
        horizData.sanitize (c, this, this) &&
256
0
        vertData.sanitize (c, this, this)));
257
0
  }
258
259
  protected:
260
  FixedVersion<>version;  /* Version of the tracking table
261
         * (0x00010000u for version 1.0). */
262
  HBUINT16  format;   /* Format of the tracking table (set to 0). */
263
  Offset16To<TrackData>
264
    horizData;  /* Offset from start of tracking table to TrackData
265
         * for horizontal text (or 0 if none). */
266
  Offset16To<TrackData>
267
    vertData; /* Offset from start of tracking table to TrackData
268
         * for vertical text (or 0 if none). */
269
  HBUINT16  reserved; /* Reserved. Set to 0. */
270
271
  public:
272
  DEFINE_SIZE_STATIC (12);
273
};
274
275
} /* namespace AAT */
276
277
278
#endif /* HB_AAT_LAYOUT_TRAK_TABLE_HH */