Coverage Report

Created: 2026-04-09 11:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/harfbuzz/src/hb-ot-post-table.hh
Line
Count
Source
1
/*
2
 * Copyright © 2016  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_POST_TABLE_HH
28
#define HB_OT_POST_TABLE_HH
29
30
#include "hb-open-type.hh"
31
#include "hb-ot-var-mvar-table.hh"
32
33
#define HB_STRING_ARRAY_NAME format1_names
34
#define HB_STRING_ARRAY_LIST "hb-ot-post-macroman.hh"
35
#include "hb-string-array.hh"
36
#undef HB_STRING_ARRAY_LIST
37
#undef HB_STRING_ARRAY_NAME
38
39
/*
40
 * post -- PostScript
41
 * https://docs.microsoft.com/en-us/typography/opentype/spec/post
42
 */
43
#define HB_OT_TAG_post HB_TAG('p','o','s','t')
44
45
46
namespace OT {
47
48
49
struct postV2Tail
50
{
51
  friend struct post;
52
53
  bool sanitize (hb_sanitize_context_t *c) const
54
6.31k
  {
55
6.31k
    TRACE_SANITIZE (this);
56
6.31k
    return_trace (glyphNameIndex.sanitize (c));
57
6.31k
  }
58
59
  template<typename Iterator>
60
  bool serialize (hb_serialize_context_t *c,
61
                  Iterator it,
62
                  const void* _post) const;
63
64
  bool subset (hb_subset_context_t *c) const;
65
66
  protected:
67
  Array16Of<HBUINT16> glyphNameIndex; /* This is not an offset, but is the
68
           * ordinal number of the glyph in 'post'
69
           * string tables. */
70
/*UnsizedArrayOf<HBUINT8>
71
      namesX;*/ /* Glyph names with length bytes [variable]
72
           * (a Pascal string). */
73
74
  public:
75
  DEFINE_SIZE_ARRAY (2, glyphNameIndex);
76
};
77
78
struct post
79
{
80
  static constexpr hb_tag_t tableTag = HB_OT_TAG_post;
81
82
  bool serialize (hb_serialize_context_t *c, bool glyph_names) const
83
6.24k
  {
84
6.24k
    TRACE_SERIALIZE (this);
85
6.24k
    post *post_prime = c->allocate_min<post> ();
86
6.24k
    if (unlikely (!post_prime))  return_trace (false);
87
88
6.24k
    hb_memcpy (post_prime, this, post::min_size);
89
6.24k
    if (!glyph_names)
90
6.24k
      return_trace (c->check_assign (post_prime->version.major, 3,
91
6.24k
                                     HB_SERIALIZE_ERROR_INT_OVERFLOW)); // Version 3 does not have any glyph names.
92
93
6.24k
    return_trace (true);
94
6.24k
  }
95
96
  bool subset (hb_subset_context_t *c) const
97
6.24k
  {
98
6.24k
    TRACE_SUBSET (this);
99
6.24k
    auto *post_prime = c->serializer->start_embed<post> ();
100
101
6.24k
    bool glyph_names = c->plan->flags & HB_SUBSET_FLAGS_GLYPH_NAMES;
102
6.24k
    if (!serialize (c->serializer, glyph_names))
103
0
      return_trace (false);
104
105
6.24k
#ifndef HB_NO_VAR
106
6.24k
    if (c->plan->normalized_coords)
107
0
    {
108
0
      auto &MVAR = *c->plan->source->table.MVAR;
109
0
      auto *table = post_prime;
110
111
0
      HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_UNDERLINE_SIZE,   underlineThickness);
112
0
      HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_UNDERLINE_OFFSET, underlinePosition);
113
0
    }
114
6.24k
#endif
115
116
6.24k
    Triple *axis_range;
117
6.24k
    if (c->plan->user_axes_location.has (HB_TAG ('s','l','n','t'), &axis_range))
118
0
    {
119
0
      float italic_angle = hb_max (-90.0, hb_min (axis_range->middle, 90.0));
120
0
      if (post_prime->italicAngle.to_float () != italic_angle)
121
0
        post_prime->italicAngle.set_float (italic_angle);
122
0
    }
123
124
6.24k
    if (glyph_names && version.major == 2)
125
0
    {
126
0
      hb_barrier ();
127
0
      return_trace (v2X.subset (c));
128
0
    }
129
130
6.24k
    return_trace (true);
131
6.24k
  }
132
133
  struct accelerator_t
134
  {
135
    friend struct postV2Tail;
136
137
    accelerator_t (hb_face_t *face)
138
62
    {
139
62
      table = hb_sanitize_context_t ().reference_table<post> (face);
140
62
      unsigned int table_length = table.get_length ();
141
142
62
      version = table->version.to_int ();
143
62
      if (version != 0x00020000) return;
144
62
      hb_barrier ();
145
146
62
      const postV2Tail &v2 = table->v2X;
147
148
62
      glyphNameIndex = &v2.glyphNameIndex;
149
62
      pool = &StructAfter<uint8_t> (v2.glyphNameIndex);
150
151
62
      const uint8_t *end = (const uint8_t *) (const void *) table + table_length;
152
62
      index_to_offset.alloc (hb_min (face->get_num_glyphs (), table_length / 8));
153
62
      for (const uint8_t *data = pool;
154
146k
     index_to_offset.length < 65535 && data < end && data + *data < end;
155
146k
     data += 1 + *data)
156
146k
  index_to_offset.push (data - pool);
157
62
    }
158
    ~accelerator_t ()
159
0
    {
160
0
      hb_free (gids_sorted_by_name.get_acquire ());
161
0
      table.destroy ();
162
0
    }
163
164
    bool get_glyph_name (hb_codepoint_t glyph,
165
       char *buf, unsigned int buf_len) const
166
0
    {
167
0
      hb_bytes_t s = find_glyph_name (glyph);
168
0
      if (!s.length) return false;
169
0
      if (!buf_len) return true;
170
0
      unsigned int len = hb_min (buf_len - 1, s.length);
171
0
      strncpy (buf, s.arrayZ, len);
172
0
      buf[len] = '\0';
173
0
      return true;
174
0
    }
175
176
    bool get_glyph_from_name (const char *name, int len,
177
            hb_codepoint_t *glyph) const
178
0
    {
179
0
      unsigned int count = get_glyph_count ();
180
0
      if (unlikely (!count)) return false;
181
182
0
      if (len < 0) len = strlen (name);
183
184
0
      if (unlikely (!len)) return false;
185
186
0
    retry:
187
0
      uint16_t *gids = gids_sorted_by_name.get_acquire ();
188
189
0
      if (unlikely (!gids))
190
0
      {
191
0
  gids = (uint16_t *) hb_malloc (count * sizeof (gids[0]));
192
0
  if (unlikely (!gids))
193
0
    return false; /* Anything better?! */
194
195
0
  for (unsigned int i = 0; i < count; i++)
196
0
    gids[i] = i;
197
0
  auto thiz = this;
198
0
  hb_array_t<uint16_t> (gids, count)
199
0
    .qsort ([thiz] (const uint16_t &a, const uint16_t &b) {
200
0
      return thiz->find_glyph_name (a).cmp (thiz->find_glyph_name (b)) > 0;
201
0
    });
202
203
0
  if (unlikely (!gids_sorted_by_name.cmpexch (nullptr, gids)))
204
0
  {
205
0
    hb_free (gids);
206
0
    goto retry;
207
0
  }
208
0
      }
209
210
0
      hb_bytes_t st (name, len);
211
0
      auto* gid = hb_bsearch (st, gids, count, sizeof (gids[0]), cmp_key, (void *) this);
212
0
      if (gid)
213
0
      {
214
0
  *glyph = *gid;
215
0
  return true;
216
0
      }
217
218
0
      return false;
219
0
    }
220
221
    hb_blob_ptr_t<post> table;
222
223
    protected:
224
225
    unsigned int get_glyph_count () const
226
0
    {
227
0
      if (version == 0x00010000)
228
0
      {
229
0
        hb_barrier ();
230
0
  return format1_names_length;
231
0
      }
232
233
0
      if (version == 0x00020000)
234
0
      {
235
0
        hb_barrier ();
236
0
  return glyphNameIndex->len;
237
0
      }
238
239
0
      return 0;
240
0
    }
241
242
    static int cmp_key (const void *pk, const void *po, void *arg)
243
0
    {
244
0
      const accelerator_t *thiz = (const accelerator_t *) arg;
245
0
      const hb_bytes_t *key = (const hb_bytes_t *) pk;
246
0
      uint16_t o = * (const uint16_t *) po;
247
0
      return thiz->find_glyph_name (o).cmp (*key);
248
0
    }
249
250
    hb_bytes_t find_glyph_name (hb_codepoint_t glyph) const
251
0
    {
252
0
      if (version == 0x00010000)
253
0
      {
254
0
        hb_barrier ();
255
0
  if (glyph >= format1_names_length)
256
0
    return hb_bytes_t ();
257
258
0
  return format1_names (glyph);
259
0
      }
260
261
0
      if (version != 0x00020000)
262
0
  return hb_bytes_t ();
263
0
      hb_barrier ();
264
265
0
      if (glyph >= glyphNameIndex->len)
266
0
  return hb_bytes_t ();
267
268
0
      unsigned int index = glyphNameIndex->arrayZ[glyph];
269
0
      if (index < format1_names_length)
270
0
  return format1_names (index);
271
0
      index -= format1_names_length;
272
273
0
      if (index >= index_to_offset.length)
274
0
  return hb_bytes_t ();
275
0
      unsigned int offset = index_to_offset[index];
276
277
0
      const uint8_t *data = pool + offset;
278
0
      unsigned int name_length = *data;
279
0
      data++;
280
281
0
      return hb_bytes_t ((const char *) data, name_length);
282
0
    }
283
284
    private:
285
    uint32_t version;
286
    const Array16Of<HBUINT16> *glyphNameIndex = nullptr;
287
    hb_vector_t<uint32_t> index_to_offset;
288
    const uint8_t *pool = nullptr;
289
    mutable hb_atomic_t<uint16_t *> gids_sorted_by_name;
290
  };
291
292
833k
  bool has_data () const { return version.to_int (); }
293
294
  bool sanitize (hb_sanitize_context_t *c) const
295
6.31k
  {
296
6.31k
    TRACE_SANITIZE (this);
297
6.31k
    return_trace (c->check_struct (this) &&
298
6.31k
      hb_barrier () &&
299
6.31k
      (version.to_int () == 0x00010000 ||
300
6.31k
       (version.to_int () == 0x00020000 && hb_barrier () && v2X.sanitize (c)) ||
301
6.31k
       version.to_int () == 0x00030000));
302
6.31k
  }
303
304
  public:
305
  FixedVersion<>version;    /* 0x00010000 for version 1.0
306
           * 0x00020000 for version 2.0
307
           * 0x00025000 for version 2.5 (deprecated)
308
           * 0x00030000 for version 3.0 */
309
  F16DOT16  italicAngle;    /* Italic angle in counter-clockwise degrees
310
           * from the vertical. Zero for upright text,
311
           * negative for text that leans to the right
312
           * (forward). */
313
  FWORD   underlinePosition;  /* This is the suggested distance of the top
314
           * of the underline from the baseline
315
           * (negative values indicate below baseline).
316
           * The PostScript definition of this FontInfo
317
           * dictionary key (the y coordinate of the
318
           * center of the stroke) is not used for
319
           * historical reasons. The value of the
320
           * PostScript key may be calculated by
321
           * subtracting half the underlineThickness
322
           * from the value of this field. */
323
  FWORD   underlineThickness; /* Suggested values for the underline
324
             thickness. */
325
  HBUINT32  isFixedPitch;   /* Set to 0 if the font is proportionally
326
           * spaced, non-zero if the font is not
327
           * proportionally spaced (i.e. monospaced). */
328
  HBUINT32  minMemType42;   /* Minimum memory usage when an OpenType font
329
           * is downloaded. */
330
  HBUINT32  maxMemType42;   /* Maximum memory usage when an OpenType font
331
           * is downloaded. */
332
  HBUINT32  minMemType1;    /* Minimum memory usage when an OpenType font
333
           * is downloaded as a Type 1 font. */
334
  HBUINT32  maxMemType1;    /* Maximum memory usage when an OpenType font
335
           * is downloaded as a Type 1 font. */
336
  postV2Tail  v2X;
337
  DEFINE_SIZE_MIN (32);
338
};
339
340
struct post_accelerator_t : post::accelerator_t {
341
62
  post_accelerator_t (hb_face_t *face) : post::accelerator_t (face) {}
342
};
343
344
345
} /* namespace OT */
346
347
348
#endif /* HB_OT_POST_TABLE_HH */