Coverage Report

Created: 2025-07-07 10:01

/work/workdir/UnpackedTarball/harfbuzz/src/hb-outline.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2023  Behdad Esfahbod
3
 * Copyright © 1999  David Turner
4
 * Copyright © 2005  Werner Lemberg
5
 * Copyright © 2013-2015  Alexei Podtelezhnikov
6
 *
7
 *  This is part of HarfBuzz, a text shaping library.
8
 *
9
 * Permission is hereby granted, without written agreement and without
10
 * license or royalty fees, to use, copy, modify, and distribute this
11
 * software and its documentation for any purpose, provided that the
12
 * above copyright notice and the following two paragraphs appear in
13
 * all copies of this software.
14
 *
15
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
16
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
17
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
18
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19
 * DAMAGE.
20
 *
21
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
22
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
23
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
24
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
25
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26
 */
27
28
#include "hb.hh"
29
30
#ifndef HB_NO_OUTLINE
31
32
#include "hb-outline.hh"
33
34
#include "hb-machinery.hh"
35
36
37
void hb_outline_t::replay (hb_draw_funcs_t *pen, void *pen_data) const
38
0
{
39
0
  hb_draw_state_t st = HB_DRAW_STATE_DEFAULT;
40
41
0
  unsigned first = 0;
42
0
  for (unsigned contour : contours)
43
0
  {
44
0
    auto it = points.as_array ().sub_array (first, contour - first);
45
0
    while (it)
46
0
    {
47
0
      hb_outline_point_t p1 = *it++;
48
0
      switch (p1.type)
49
0
      {
50
0
  case hb_outline_point_t::type_t::MOVE_TO:
51
0
  {
52
0
    pen->move_to (pen_data, st,
53
0
         p1.x, p1.y);
54
0
  }
55
0
  break;
56
0
  case hb_outline_point_t::type_t::LINE_TO:
57
0
  {
58
0
    pen->line_to (pen_data, st,
59
0
         p1.x, p1.y);
60
0
  }
61
0
  break;
62
0
  case hb_outline_point_t::type_t::QUADRATIC_TO:
63
0
  {
64
0
    hb_outline_point_t p2 = *it++;
65
0
    pen->quadratic_to (pen_data, st,
66
0
        p1.x, p1.y,
67
0
        p2.x, p2.y);
68
0
  }
69
0
  break;
70
0
  case hb_outline_point_t::type_t::CUBIC_TO:
71
0
  {
72
0
    hb_outline_point_t p2 = *it++;
73
0
    hb_outline_point_t p3 = *it++;
74
0
    pen->cubic_to (pen_data, st,
75
0
          p1.x, p1.y,
76
0
          p2.x, p2.y,
77
0
          p3.x, p3.y);
78
0
  }
79
0
  break;
80
0
      }
81
0
    }
82
0
    pen->close_path (pen_data, st);
83
0
    first = contour;
84
0
  }
85
0
}
86
87
void hb_outline_t::slant (float slant_xy)
88
0
{
89
0
  for (auto &p : points)
90
0
    p.x += slant_xy * p.y;
91
0
}
92
93
float hb_outline_t::control_area () const
94
0
{
95
0
  float a = 0;
96
0
  unsigned first = 0;
97
0
  for (unsigned contour : contours)
98
0
  {
99
0
    for (unsigned i = first; i < contour; i++)
100
0
    {
101
0
      unsigned j = i + 1 < contour ? i + 1 : first;
102
103
0
      auto &pi = points[i];
104
0
      auto &pj = points[j];
105
0
      a += pi.x * pj.y - pi.y * pj.x;
106
0
    }
107
108
0
    first = contour;
109
0
  }
110
0
  return a * .5f;
111
0
}
112
113
void hb_outline_t::embolden (float x_strength, float y_strength,
114
           float x_shift, float y_shift)
115
0
{
116
  /* This function is a straight port of FreeType's FT_Outline_EmboldenXY.
117
   * Permission has been obtained from the FreeType authors of the code
118
   * to relicense it under the HarfBuzz license. */
119
120
0
  if (!x_strength && !y_strength) return;
121
0
  if (!points) return;
122
123
0
  x_strength /= 2.f;
124
0
  y_strength /= 2.f;
125
126
0
  bool orientation_negative = control_area () < 0;
127
128
0
  signed first = 0;
129
0
  for (unsigned c = 0; c < contours.length; c++)
130
0
  {
131
0
    hb_outline_vector_t in, out, anchor, shift;
132
0
    float l_in, l_out, l_anchor = 0, l, q, d;
133
134
0
    l_in = 0;
135
0
    signed last = (int) contours[c] - 1;
136
137
    /* pacify compiler */
138
0
    in.x = in.y = anchor.x = anchor.y = 0;
139
140
    /* Counter j cycles though the points; counter i advances only  */
141
    /* when points are moved; anchor k marks the first moved point. */
142
0
    for ( signed i = last, j = first, k = -1;
143
0
    j != i && i != k;
144
0
    j = j < last ? j + 1 : first )
145
0
    {
146
0
      if ( j != k )
147
0
      {
148
0
  out.x = points[j].x - points[i].x;
149
0
  out.y = points[j].y - points[i].y;
150
0
  l_out = out.normalize_len ();
151
152
0
  if ( l_out == 0 )
153
0
    continue;
154
0
      }
155
0
      else
156
0
      {
157
0
  out   = anchor;
158
0
  l_out = l_anchor;
159
0
      }
160
161
0
      if ( l_in != 0 )
162
0
      {
163
0
  if ( k < 0 )
164
0
  {
165
0
    k        = i;
166
0
    anchor   = in;
167
0
    l_anchor = l_in;
168
0
  }
169
170
0
  d = in.x * out.x + in.y * out.y;
171
172
  /* shift only if turn is less than ~160 degrees */
173
0
  if ( d > -15.f/16.f )
174
0
  {
175
0
    d = d + 1.f;
176
177
    /* shift components along lateral bisector in proper orientation */
178
0
    shift.x = in.y + out.y;
179
0
    shift.y = in.x + out.x;
180
181
0
    if ( orientation_negative )
182
0
      shift.x = -shift.x;
183
0
    else
184
0
      shift.y = -shift.y;
185
186
    /* restrict shift magnitude to better handle collapsing segments */
187
0
    q = out.x * in.y - out.y * in.x;
188
0
    if ( orientation_negative )
189
0
      q = -q;
190
191
0
    l = hb_min (l_in, l_out);
192
193
    /* non-strict inequalities avoid divide-by-zero when q == l == 0 */
194
0
    if (x_strength * q <= l * d)
195
0
      shift.x = shift.x * x_strength / d;
196
0
    else
197
0
      shift.x = shift.x * l / q;
198
199
200
0
    if (y_strength * q <= l * d)
201
0
      shift.y = shift.y * y_strength / d;
202
0
    else
203
0
      shift.y = shift.y * l / q;
204
0
  }
205
0
  else
206
0
    shift.x = shift.y = 0;
207
208
0
  for ( ;
209
0
        i != j;
210
0
        i = i < last ? i + 1 : first )
211
0
  {
212
0
    points[i].x += x_shift + shift.x;
213
0
    points[i].y += y_shift + shift.y;
214
0
  }
215
0
      }
216
0
      else
217
0
  i = j;
218
219
0
      in   = out;
220
0
      l_in = l_out;
221
0
    }
222
223
0
    first = last + 1;
224
0
  }
225
0
}
226
227
static void
228
hb_outline_recording_pen_move_to (hb_draw_funcs_t *dfuncs HB_UNUSED,
229
          void *data,
230
          hb_draw_state_t *st,
231
          float to_x, float to_y,
232
          void *user_data HB_UNUSED)
233
0
{
234
0
  hb_outline_t *c = (hb_outline_t *) data;
235
236
0
  c->points.push (hb_outline_point_t {to_x, to_y, hb_outline_point_t::type_t::MOVE_TO});
237
0
}
238
239
static void
240
hb_outline_recording_pen_line_to (hb_draw_funcs_t *dfuncs HB_UNUSED,
241
          void *data,
242
          hb_draw_state_t *st,
243
          float to_x, float to_y,
244
          void *user_data HB_UNUSED)
245
0
{
246
0
  hb_outline_t *c = (hb_outline_t *) data;
247
248
0
  c->points.push (hb_outline_point_t {to_x, to_y, hb_outline_point_t::type_t::LINE_TO});
249
0
}
250
251
static void
252
hb_outline_recording_pen_quadratic_to (hb_draw_funcs_t *dfuncs HB_UNUSED,
253
               void *data,
254
               hb_draw_state_t *st,
255
               float control_x, float control_y,
256
               float to_x, float to_y,
257
               void *user_data HB_UNUSED)
258
0
{
259
0
  hb_outline_t *c = (hb_outline_t *) data;
260
261
0
  c->points.push (hb_outline_point_t {control_x, control_y, hb_outline_point_t::type_t::QUADRATIC_TO});
262
0
  c->points.push (hb_outline_point_t {to_x, to_y, hb_outline_point_t::type_t::QUADRATIC_TO});
263
0
}
264
265
static void
266
hb_outline_recording_pen_cubic_to (hb_draw_funcs_t *dfuncs HB_UNUSED,
267
           void *data,
268
           hb_draw_state_t *st,
269
           float control1_x, float control1_y,
270
           float control2_x, float control2_y,
271
           float to_x, float to_y,
272
           void *user_data HB_UNUSED)
273
0
{
274
0
  hb_outline_t *c = (hb_outline_t *) data;
275
276
0
  c->points.push (hb_outline_point_t {control1_x, control1_y, hb_outline_point_t::type_t::CUBIC_TO});
277
0
  c->points.push (hb_outline_point_t {control2_x, control2_y, hb_outline_point_t::type_t::CUBIC_TO});
278
0
  c->points.push (hb_outline_point_t {to_x, to_y, hb_outline_point_t::type_t::CUBIC_TO});
279
0
}
280
281
static void
282
hb_outline_recording_pen_close_path (hb_draw_funcs_t *dfuncs HB_UNUSED,
283
             void *data,
284
             hb_draw_state_t *st,
285
             void *user_data HB_UNUSED)
286
0
{
287
0
  hb_outline_t *c = (hb_outline_t *) data;
288
289
0
  c->contours.push (c->points.length);
290
0
}
291
292
static inline void free_static_outline_recording_pen_funcs ();
293
294
static struct hb_outline_recording_pen_funcs_lazy_loader_t : hb_draw_funcs_lazy_loader_t<hb_outline_recording_pen_funcs_lazy_loader_t>
295
{
296
  static hb_draw_funcs_t *create ()
297
0
  {
298
0
    hb_draw_funcs_t *funcs = hb_draw_funcs_create ();
299
300
0
    hb_draw_funcs_set_move_to_func (funcs, hb_outline_recording_pen_move_to, nullptr, nullptr);
301
0
    hb_draw_funcs_set_line_to_func (funcs, hb_outline_recording_pen_line_to, nullptr, nullptr);
302
0
    hb_draw_funcs_set_quadratic_to_func (funcs, hb_outline_recording_pen_quadratic_to, nullptr, nullptr);
303
0
    hb_draw_funcs_set_cubic_to_func (funcs, hb_outline_recording_pen_cubic_to, nullptr, nullptr);
304
0
    hb_draw_funcs_set_close_path_func (funcs, hb_outline_recording_pen_close_path, nullptr, nullptr);
305
306
0
    hb_draw_funcs_make_immutable (funcs);
307
308
0
    hb_atexit (free_static_outline_recording_pen_funcs);
309
310
0
    return funcs;
311
0
  }
312
} static_outline_recording_pen_funcs;
313
314
static inline
315
void free_static_outline_recording_pen_funcs ()
316
0
{
317
0
  static_outline_recording_pen_funcs.free_instance ();
318
0
}
319
320
hb_draw_funcs_t *
321
hb_outline_recording_pen_get_funcs ()
322
0
{
323
0
  return static_outline_recording_pen_funcs.get_unconst ();
324
0
}
325
326
327
#endif