Coverage Report

Created: 2026-03-31 11:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/harfbuzz/src/hb-subset-cff-common.cc
Line
Count
Source
1
/*
2
 * Copyright © 2018 Adobe 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
 * Adobe Author(s): Michiharu Ariza
25
 */
26
27
#include "hb.hh"
28
29
#ifndef HB_NO_SUBSET_CFF
30
31
#include "hb-ot-cff-common.hh"
32
#include "hb-ot-cff2-table.hh"
33
#include "hb-subset-cff-common.hh"
34
35
/* Disable FDSelect format 0 for compatibility with fonttools which doesn't seem choose it.
36
 * Rarely any/much smaller than format 3 anyway. */
37
#define CFF_SERIALIZE_FDSELECT_0  0
38
39
using namespace CFF;
40
41
42
/* Determine an optimal FDSelect format according to a provided plan.
43
 *
44
 * Return value: FDSelect format, size, and ranges for the most compact subset FDSelect
45
 * along with a font index remapping table
46
 */
47
48
bool
49
hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan,
50
           unsigned int fdCount,
51
           const FDSelect &src, /* IN */
52
           unsigned int &subset_fd_count /* OUT */,
53
           unsigned int &subset_fdselect_size /* OUT */,
54
           unsigned int &subset_fdselect_format /* OUT */,
55
           hb_vector_t<code_pair_t> &fdselect_ranges /* OUT */,
56
           hb_inc_bimap_t &fdmap /* OUT */)
57
0
{
58
0
  subset_fd_count = 0;
59
0
  subset_fdselect_size = 0;
60
0
  subset_fdselect_format = 0;
61
0
  unsigned int num_ranges = 0;
62
63
0
  unsigned int subset_num_glyphs = plan->num_output_glyphs ();
64
0
  if (subset_num_glyphs == 0)
65
0
    return true;
66
67
0
  {
68
    /* use hb_set to determine the subset of font dicts */
69
0
    hb_set_t set;
70
0
    hb_codepoint_t prev_fd = CFF_UNDEF_CODE;
71
0
    auto it = hb_iter (plan->new_to_old_gid_list);
72
0
    auto _ = *it;
73
0
    for (hb_codepoint_t gid = 0; gid < subset_num_glyphs; gid++)
74
0
    {
75
0
      hb_codepoint_t old_glyph;
76
0
      if (gid == _.first)
77
0
      {
78
0
  old_glyph = _.second;
79
0
  _ = *++it;
80
0
      }
81
0
      else
82
0
      {
83
  /* fonttools retains FDSelect & font dicts for missing glyphs. do the same */
84
0
  old_glyph = gid;
85
0
      }
86
0
      auto fd_range = src.get_fd_range (old_glyph);
87
0
      unsigned fd = fd_range.first;
88
89
0
      if (fd != prev_fd)
90
0
      {
91
0
  set.add (fd);
92
0
  num_ranges++;
93
0
  prev_fd = fd;
94
0
  fdselect_ranges.push (code_pair_t { fd, gid });
95
96
0
  if (gid == old_glyph)
97
0
    gid = hb_min (_.first - 1, fd_range.second - 1);
98
0
      }
99
0
    }
100
101
0
    subset_fd_count = set.get_population ();
102
0
    if (subset_fd_count == fdCount)
103
0
    {
104
      /* all font dicts belong to the subset. no need to subset FDSelect & FDArray */
105
0
      fdmap.identity (fdCount);
106
0
    }
107
0
    else
108
0
    {
109
      /* create a fdmap */
110
0
      fdmap.reset ();
111
112
0
      hb_codepoint_t fd = CFF_UNDEF_CODE;
113
0
      while (set.next (&fd))
114
0
  fdmap.add (fd);
115
0
      if (unlikely (fdmap.get_population () != subset_fd_count))
116
0
  return false;
117
0
    }
118
119
    /* update each font dict index stored as "code" in fdselect_ranges */
120
0
    for (unsigned int i = 0; i < fdselect_ranges.length; i++)
121
0
      fdselect_ranges[i].code = fdmap[fdselect_ranges[i].code];
122
0
  }
123
124
  /* determine which FDSelect format is most compact */
125
0
  if (subset_fd_count > 0xFF)
126
0
  {
127
0
    if (unlikely (src.format != 4))
128
0
      return false;
129
0
    subset_fdselect_format = 4;
130
0
    subset_fdselect_size = FDSelect::min_size + FDSelect4::min_size + FDSelect4_Range::static_size * num_ranges + HBUINT32::static_size;
131
0
  }
132
0
  else
133
0
  {
134
#if CFF_SERIALIZE_FDSELECT_0
135
    unsigned int format0_size = FDSelect::min_size + FDSelect0::min_size + HBUINT8::static_size * subset_num_glyphs;
136
#endif
137
0
    unsigned int format3_size = FDSelect::min_size + FDSelect3::min_size + FDSelect3_Range::static_size * num_ranges + HBUINT16::static_size;
138
139
#if CFF_SERIALIZE_FDSELECT_0
140
    if (format0_size <= format3_size)
141
    {
142
      // subset_fdselect_format = 0;
143
      subset_fdselect_size = format0_size;
144
    }
145
    else
146
#endif
147
0
    {
148
0
      subset_fdselect_format = 3;
149
0
      subset_fdselect_size = format3_size;
150
0
    }
151
0
  }
152
153
0
  return true;
154
0
}
155
156
template <typename FDSELECT3_4>
157
static inline bool
158
serialize_fdselect_3_4 (hb_serialize_context_t *c,
159
      const unsigned int num_glyphs,
160
      const FDSelect &src,
161
      unsigned int size,
162
      const hb_vector_t<code_pair_t> &fdselect_ranges)
163
0
{
164
0
  TRACE_SERIALIZE (this);
165
0
  FDSELECT3_4 *p = c->allocate_size<FDSELECT3_4> (size);
166
0
  if (unlikely (!p)) return_trace (false);
167
0
  p->nRanges () = fdselect_ranges.length;
168
0
  for (unsigned int i = 0; i < fdselect_ranges.length; i++)
169
0
  {
170
0
    p->ranges[i].first = fdselect_ranges[i].glyph;
171
0
    p->ranges[i].fd = fdselect_ranges[i].code;
172
0
  }
173
0
  p->sentinel () = num_glyphs;
174
0
  return_trace (true);
175
0
}
Unexecuted instantiation: hb-subset-cff-common.cc:bool serialize_fdselect_3_4<CFF::FDSelect3_4<OT::NumType<true, unsigned short, 2u>, OT::NumType<true, unsigned char, 1u> > >(hb_serialize_context_t*, unsigned int, CFF::FDSelect const&, unsigned int, hb_vector_t<CFF::code_pair_t, false> const&)
Unexecuted instantiation: hb-subset-cff-common.cc:bool serialize_fdselect_3_4<CFF::FDSelect3_4<OT::NumType<true, unsigned int, 4u>, OT::NumType<true, unsigned short, 2u> > >(hb_serialize_context_t*, unsigned int, CFF::FDSelect const&, unsigned int, hb_vector_t<CFF::code_pair_t, false> const&)
176
177
/* Serialize a subset FDSelect format planned above. */
178
bool
179
hb_serialize_cff_fdselect (hb_serialize_context_t *c,
180
         const unsigned int num_glyphs,
181
         const FDSelect &src,
182
         unsigned int fd_count,
183
         unsigned int fdselect_format,
184
         unsigned int size,
185
         const hb_vector_t<code_pair_t> &fdselect_ranges)
186
0
{
187
0
  TRACE_SERIALIZE (this);
188
0
  FDSelect *p = c->allocate_min<FDSelect> ();
189
0
  if (unlikely (!p)) return_trace (false);
190
0
  p->format = fdselect_format;
191
0
  size -= FDSelect::min_size;
192
193
0
  switch (fdselect_format)
194
0
  {
195
#if CFF_SERIALIZE_FDSELECT_0
196
  case 0:
197
  {
198
    FDSelect0 *p = c->allocate_size<FDSelect0> (size);
199
    if (unlikely (!p)) return_trace (false);
200
    unsigned int range_index = 0;
201
    unsigned int fd = fdselect_ranges[range_index++].code;
202
    for (unsigned int i = 0; i < num_glyphs; i++)
203
    {
204
      if ((range_index < fdselect_ranges.len) &&
205
    (i >= fdselect_ranges[range_index].glyph))
206
      {
207
  fd = fdselect_ranges[range_index++].code;
208
      }
209
      p->fds[i] = fd;
210
    }
211
    return_trace (true);
212
  }
213
#endif /* CFF_SERIALIZE_FDSELECT_0 */
214
215
0
  case 3:
216
0
    return serialize_fdselect_3_4<FDSelect3> (c, num_glyphs, src,
217
0
                size, fdselect_ranges);
218
219
0
  case 4:
220
0
    return serialize_fdselect_3_4<FDSelect4> (c, num_glyphs, src,
221
0
                size, fdselect_ranges);
222
223
0
  default:
224
0
    return_trace (false);
225
0
  }
226
0
}
227
228
229
#endif