Coverage Report

Created: 2026-09-28 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/harfbuzz/src/hb-ot-cff2-table.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
#ifndef HB_OT_CFF2_TABLE_CC
28
#define HB_OT_CFF2_TABLE_CC
29
#ifdef HB_OT_CFF2_TABLE_CC /* Pacify -Wunused-macros. */
30
31
#include "hb.hh"
32
33
#ifndef HB_NO_OT_FONT_CFF
34
35
#include "hb-ot-cff2-table.hh"
36
#include "hb-cff2-interp-cs.hh"
37
#include "hb-draw.hh"
38
39
using namespace CFF;
40
41
struct cff2_extents_param_t
42
{
43
  cff2_extents_param_t ()
44
0
  {
45
0
    min_x.set_int (INT_MAX);
46
0
    min_y.set_int (INT_MAX);
47
0
    max_x.set_int (INT_MIN);
48
0
    max_y.set_int (INT_MIN);
49
0
  }
50
51
0
  void   start_path ()       { path_open = true; }
52
0
  void     end_path ()       { path_open = false; }
53
0
  bool is_path_open () const { return path_open; }
54
55
  void update_bounds (const point_t &pt)
56
0
  {
57
0
    if (pt.x < min_x) min_x = pt.x;
58
0
    if (pt.x > max_x) max_x = pt.x;
59
0
    if (pt.y < min_y) min_y = pt.y;
60
0
    if (pt.y > max_y) max_y = pt.y;
61
0
  }
62
63
  bool  path_open = false;
64
  number_t min_x;
65
  number_t min_y;
66
  number_t max_x;
67
  number_t max_y;
68
};
69
70
struct cff2_path_procs_extents_t : path_procs_t<cff2_path_procs_extents_t, cff2_cs_interp_env_t<number_t>, cff2_extents_param_t>
71
{
72
  static void moveto (cff2_cs_interp_env_t<number_t> &env, cff2_extents_param_t& param, const point_t &pt)
73
0
  {
74
0
    param.end_path ();
75
0
    env.moveto (pt);
76
0
  }
77
78
  static void line (cff2_cs_interp_env_t<number_t> &env, cff2_extents_param_t& param, const point_t &pt1)
79
0
  {
80
0
    if (!param.is_path_open ())
81
0
    {
82
0
      param.start_path ();
83
0
      param.update_bounds (env.get_pt ());
84
0
    }
85
0
    env.moveto (pt1);
86
0
    param.update_bounds (env.get_pt ());
87
0
  }
88
89
  static void curve (cff2_cs_interp_env_t<number_t> &env, cff2_extents_param_t& param, const point_t &pt1, const point_t &pt2, const point_t &pt3)
90
0
  {
91
0
    if (!param.is_path_open ())
92
0
    {
93
0
      param.start_path ();
94
0
      param.update_bounds (env.get_pt ());
95
0
    }
96
    /* include control points */
97
0
    param.update_bounds (pt1);
98
0
    param.update_bounds (pt2);
99
0
    env.moveto (pt3);
100
0
    param.update_bounds (env.get_pt ());
101
0
  }
102
};
103
104
struct cff2_cs_opset_extents_t : cff2_cs_opset_t<cff2_cs_opset_extents_t, cff2_extents_param_t, number_t, cff2_path_procs_extents_t> {};
105
106
bool OT::cff2::accelerator_t::get_extents (hb_font_t *font,
107
             hb_codepoint_t glyph,
108
             hb_glyph_extents_t *extents) const
109
0
{
110
0
  return get_extents_at (font, glyph, extents, hb_array (font->coords, font->num_coords));
111
0
}
112
113
bool OT::cff2::accelerator_t::get_extents_at (hb_font_t *font,
114
                hb_codepoint_t glyph,
115
                hb_glyph_extents_t *extents,
116
                hb_array_t<const int> coords,
117
                int64_t *budget) const
118
0
{
119
#ifdef HB_NO_OT_FONT_CFF
120
  /* XXX Remove check when this code moves to .hh file. */
121
  return true;
122
#endif
123
124
0
  if (unlikely (!is_valid () || (glyph >= num_glyphs))) return false;
125
126
0
  int64_t stack_budget = HB_BUDGET_GLYPH;
127
0
  if (!budget) budget = &stack_budget;
128
129
0
  unsigned int fd = fdSelect->get_fd (glyph);
130
0
  const hb_ubytes_t str = (*charStrings)[glyph];
131
0
  cff2_cs_interp_env_t<number_t> env (str, *this, fd, coords.arrayZ, coords.length);
132
0
  cff2_cs_interpreter_t<cff2_cs_opset_extents_t, cff2_extents_param_t, number_t> interp (env);
133
0
  cff2_extents_param_t  param;
134
0
  if (unlikely (!interp.interpret (param, budget))) return false;
135
136
0
  if (param.min_x >= param.max_x)
137
0
  {
138
0
    extents->width = 0;
139
0
    extents->x_bearing = 0;
140
0
  }
141
0
  else
142
0
  {
143
0
    double x_bearing = roundf (param.min_x.to_real ());
144
0
    extents->x_bearing = hb_clamp_to<hb_position_t> (x_bearing);
145
0
    extents->width = hb_clamp_to<hb_position_t> ((double) roundf (param.max_x.to_real ()) - x_bearing);
146
0
  }
147
0
  if (param.min_y >= param.max_y)
148
0
  {
149
0
    extents->height = 0;
150
0
    extents->y_bearing = 0;
151
0
  }
152
0
  else
153
0
  {
154
0
    double y_bearing = roundf (param.max_y.to_real ());
155
0
    extents->y_bearing = hb_clamp_to<hb_position_t> (y_bearing);
156
0
    extents->height = hb_clamp_to<hb_position_t> ((double) roundf (param.min_y.to_real ()) - y_bearing);
157
0
  }
158
159
0
  font->scale_glyph_extents (extents);
160
161
0
  return true;
162
0
}
163
164
struct cff2_path_param_t
165
{
166
  cff2_path_param_t (hb_font_t *font_, hb_draw_session_t &draw_session_)
167
0
  {
168
0
    draw_session = &draw_session_;
169
0
    font = font_;
170
0
  }
171
172
  void move_to (const point_t &p)
173
0
  { draw_session->move_to (font->em_fscalef_x (p.x.to_real ()), font->em_fscalef_y (p.y.to_real ())); }
174
175
  void line_to (const point_t &p)
176
0
  { draw_session->line_to (font->em_fscalef_x (p.x.to_real ()), font->em_fscalef_y (p.y.to_real ())); }
177
178
  void cubic_to (const point_t &p1, const point_t &p2, const point_t &p3)
179
0
  {
180
0
    draw_session->cubic_to (font->em_fscalef_x (p1.x.to_real ()), font->em_fscalef_y (p1.y.to_real ()),
181
0
         font->em_fscalef_x (p2.x.to_real ()), font->em_fscalef_y (p2.y.to_real ()),
182
0
         font->em_fscalef_x (p3.x.to_real ()), font->em_fscalef_y (p3.y.to_real ()));
183
0
  }
184
185
  protected:
186
  hb_draw_session_t *draw_session;
187
  hb_font_t *font;
188
};
189
190
struct cff2_path_procs_path_t : path_procs_t<cff2_path_procs_path_t, cff2_cs_interp_env_t<number_t>, cff2_path_param_t>
191
{
192
  static void moveto (cff2_cs_interp_env_t<number_t> &env, cff2_path_param_t& param, const point_t &pt)
193
0
  {
194
0
    param.move_to (pt);
195
0
    env.moveto (pt);
196
0
  }
197
198
  static void line (cff2_cs_interp_env_t<number_t> &env, cff2_path_param_t& param, const point_t &pt1)
199
0
  {
200
0
    param.line_to (pt1);
201
0
    env.moveto (pt1);
202
0
  }
203
204
  static void curve (cff2_cs_interp_env_t<number_t> &env, cff2_path_param_t& param, const point_t &pt1, const point_t &pt2, const point_t &pt3)
205
0
  {
206
0
    param.cubic_to (pt1, pt2, pt3);
207
0
    env.moveto (pt3);
208
0
  }
209
};
210
211
struct cff2_cs_opset_path_t : cff2_cs_opset_t<cff2_cs_opset_path_t, cff2_path_param_t, number_t, cff2_path_procs_path_t> {};
212
213
bool OT::cff2::accelerator_t::get_path (hb_font_t *font, hb_codepoint_t glyph,
214
               hb_draw_session_t &draw_session,
215
               int64_t *budget) const
216
1
{
217
1
  int64_t stack_budget = HB_BUDGET_GLYPH;
218
1
  if (!budget) budget = &stack_budget;
219
220
1
  return get_path_at (font,
221
1
          glyph,
222
1
          draw_session,
223
1
          hb_array (font->coords,
224
1
        font->has_nonzero_coords ? font->num_coords : 0),
225
1
          budget);
226
1
}
227
228
bool OT::cff2::accelerator_t::get_path_at (hb_font_t *font, hb_codepoint_t glyph, hb_draw_session_t &draw_session, hb_array_t<const int> coords, int64_t *budget) const
229
1
{
230
#ifdef HB_NO_OT_FONT_CFF
231
  /* XXX Remove check when this code moves to .hh file. */
232
  return true;
233
#endif
234
235
1
  if (unlikely (!is_valid () || (glyph >= num_glyphs))) return false;
236
237
0
  int64_t stack_budget = HB_BUDGET_GLYPH;
238
0
  if (!budget) budget = &stack_budget;
239
240
0
  unsigned int fd = fdSelect->get_fd (glyph);
241
0
  const hb_ubytes_t str = (*charStrings)[glyph];
242
0
  cff2_cs_interp_env_t<number_t> env (str, *this, fd, coords.arrayZ, coords.length);
243
0
  cff2_cs_interpreter_t<cff2_cs_opset_path_t, cff2_path_param_t, number_t> interp (env);
244
0
  cff2_path_param_t param (font, draw_session);
245
0
  if (unlikely (!interp.interpret (param, budget))) return false;
246
0
  return true;
247
0
}
248
249
#endif
250
251
#endif /* HB_OT_CFF2_TABLE_CC pacify */
252
#endif /* HB_OT_CFF2_TABLE_CC guard */