Coverage Report

Created: 2024-07-15 14:08

/src/harfbuzz/src/hb-paint-extents.hh
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2022 Behdad Esfahbod
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
25
#ifndef HB_PAINT_EXTENTS_HH
26
#define HB_PAINT_EXTENTS_HH
27
28
#include "hb.hh"
29
#include "hb-paint.h"
30
31
32
typedef struct hb_extents_t
33
{
34
22.0M
  hb_extents_t () {}
35
  hb_extents_t (float xmin, float ymin, float xmax, float ymax) :
36
30.0k
    xmin (xmin), ymin (ymin), xmax (xmax), ymax (ymax) {}
37
38
2.54M
  bool is_empty () const { return xmin >= xmax || ymin >= ymax; }
39
50.2M
  bool is_void () const { return xmin > xmax; }
40
41
  void union_ (const hb_extents_t &o)
42
1.84M
  {
43
1.84M
    xmin = hb_min (xmin, o.xmin);
44
1.84M
    ymin = hb_min (ymin, o.ymin);
45
1.84M
    xmax = hb_max (xmax, o.xmax);
46
1.84M
    ymax = hb_max (ymax, o.ymax);
47
1.84M
  }
48
49
  void intersect (const hb_extents_t &o)
50
1.79k
  {
51
1.79k
    xmin = hb_max (xmin, o.xmin);
52
1.79k
    ymin = hb_max (ymin, o.ymin);
53
1.79k
    xmax = hb_min (xmax, o.xmax);
54
1.79k
    ymax = hb_min (ymax, o.ymax);
55
1.79k
  }
56
57
  void
58
  add_point (float x, float y)
59
50.2M
  {
60
50.2M
    if (unlikely (is_void ()))
61
3.44M
    {
62
3.44M
      xmin = xmax = x;
63
3.44M
      ymin = ymax = y;
64
3.44M
    }
65
46.8M
    else
66
46.8M
    {
67
46.8M
      xmin = hb_min (xmin, x);
68
46.8M
      ymin = hb_min (ymin, y);
69
46.8M
      xmax = hb_max (xmax, x);
70
46.8M
      ymax = hb_max (ymax, y);
71
46.8M
    }
72
50.2M
  }
73
74
  float xmin = 0.f;
75
  float ymin = 0.f;
76
  float xmax = -1.f;
77
  float ymax = -1.f;
78
} hb_extents_t;
79
80
typedef struct hb_transform_t
81
{
82
5.01M
  hb_transform_t () {}
83
  hb_transform_t (float xx, float yx,
84
      float xy, float yy,
85
      float x0, float y0) :
86
4.95M
    xx (xx), yx (yx), xy (xy), yy (yy), x0 (x0), y0 (y0) {}
87
88
  void multiply (const hb_transform_t &o)
89
4.95M
  {
90
    /* Copied from cairo, with "o" being "a" there and "this" being "b" there. */
91
4.95M
    hb_transform_t r;
92
93
4.95M
    r.xx = o.xx * xx + o.yx * xy;
94
4.95M
    r.yx = o.xx * yx + o.yx * yy;
95
96
4.95M
    r.xy = o.xy * xx + o.yy * xy;
97
4.95M
    r.yy = o.xy * yx + o.yy * yy;
98
99
4.95M
    r.x0 = o.x0 * xx + o.y0 * xy + x0;
100
4.95M
    r.y0 = o.x0 * yx + o.y0 * yy + y0;
101
102
4.95M
    *this = r;
103
4.95M
  }
104
105
  void transform_distance (float &dx, float &dy) const
106
10.1M
  {
107
10.1M
    float new_x = xx * dx + xy * dy;
108
10.1M
    float new_y = yx * dx + yy * dy;
109
10.1M
    dx = new_x;
110
10.1M
    dy = new_y;
111
10.1M
  }
112
113
  void transform_point (float &x, float &y) const
114
10.1M
  {
115
10.1M
    transform_distance (x, y);
116
10.1M
    x += x0;
117
10.1M
    y += y0;
118
10.1M
  }
119
120
  void transform_extents (hb_extents_t &extents) const
121
2.53M
  {
122
2.53M
    float quad_x[4], quad_y[4];
123
124
2.53M
    quad_x[0] = extents.xmin;
125
2.53M
    quad_y[0] = extents.ymin;
126
2.53M
    quad_x[1] = extents.xmin;
127
2.53M
    quad_y[1] = extents.ymax;
128
2.53M
    quad_x[2] = extents.xmax;
129
2.53M
    quad_y[2] = extents.ymin;
130
2.53M
    quad_x[3] = extents.xmax;
131
2.53M
    quad_y[3] = extents.ymax;
132
133
2.53M
    extents = hb_extents_t {};
134
12.6M
    for (unsigned i = 0; i < 4; i++)
135
10.1M
    {
136
10.1M
      transform_point (quad_x[i], quad_y[i]);
137
10.1M
      extents.add_point (quad_x[i], quad_y[i]);
138
10.1M
    }
139
2.53M
  }
140
141
  float xx = 1.f;
142
  float yx = 0.f;
143
  float xy = 0.f;
144
  float yy = 1.f;
145
  float x0 = 0.f;
146
  float y0 = 0.f;
147
} hb_transform_t;
148
149
typedef struct hb_bounds_t
150
{
151
  enum status_t {
152
    UNBOUNDED,
153
    BOUNDED,
154
    EMPTY,
155
  };
156
157
17.0M
  hb_bounds_t (status_t status) : status (status) {}
158
  hb_bounds_t (const hb_extents_t &extents) :
159
2.53M
    status (extents.is_empty () ? EMPTY : BOUNDED), extents (extents) {}
160
161
  void union_ (const hb_bounds_t &o)
162
19.0M
  {
163
19.0M
    if (o.status == UNBOUNDED)
164
607k
      status = UNBOUNDED;
165
18.4M
    else if (o.status == BOUNDED)
166
3.39M
    {
167
3.39M
      if (status == EMPTY)
168
1.43M
  *this = o;
169
1.96M
      else if (status == BOUNDED)
170
1.84M
        extents.union_ (o.extents);
171
3.39M
    }
172
19.0M
  }
173
174
  void intersect (const hb_bounds_t &o)
175
10.8k
  {
176
10.8k
    if (o.status == EMPTY)
177
5.09k
      status = EMPTY;
178
5.74k
    else if (o.status == BOUNDED)
179
3.49k
    {
180
3.49k
      if (status == UNBOUNDED)
181
75
  *this = o;
182
3.42k
      else if (status == BOUNDED)
183
1.79k
      {
184
1.79k
        extents.intersect (o.extents);
185
1.79k
  if (extents.is_empty ())
186
908
    status = EMPTY;
187
1.79k
      }
188
3.49k
    }
189
10.8k
  }
190
191
  status_t status;
192
  hb_extents_t extents;
193
} hb_bounds_t;
194
195
typedef struct  hb_paint_extents_context_t hb_paint_extents_context_t;
196
197
struct hb_paint_extents_context_t
198
{
199
  hb_paint_extents_context_t ()
200
60.3k
  {
201
60.3k
    transforms.push (hb_transform_t{});
202
60.3k
    clips.push (hb_bounds_t{hb_bounds_t::UNBOUNDED});
203
60.3k
    groups.push (hb_bounds_t{hb_bounds_t::EMPTY});
204
60.3k
  }
205
206
  hb_extents_t get_extents ()
207
60.3k
  {
208
60.3k
    return groups.tail().extents;
209
60.3k
  }
210
211
  bool is_bounded ()
212
24.9k
  {
213
24.9k
    return groups.tail().status != hb_bounds_t::UNBOUNDED;
214
24.9k
  }
215
216
  void push_transform (const hb_transform_t &trans)
217
4.95M
  {
218
4.95M
    hb_transform_t t = transforms.tail ();
219
4.95M
    t.multiply (trans);
220
4.95M
    transforms.push (t);
221
4.95M
  }
222
223
  void pop_transform ()
224
4.95M
  {
225
4.95M
    transforms.pop ();
226
4.95M
  }
227
228
  void push_clip (hb_extents_t extents)
229
2.53M
  {
230
    /* Transform extents and push a new clip. */
231
2.53M
    const hb_transform_t &t = transforms.tail ();
232
2.53M
    t.transform_extents (extents);
233
234
2.53M
    clips.push (hb_bounds_t {extents});
235
2.53M
  }
236
237
  void pop_clip ()
238
2.53M
  {
239
2.53M
    clips.pop ();
240
2.53M
  }
241
242
  void push_group ()
243
16.8M
  {
244
16.8M
    groups.push (hb_bounds_t {hb_bounds_t::EMPTY});
245
16.8M
  }
246
247
  void pop_group (hb_paint_composite_mode_t mode)
248
16.8M
  {
249
16.8M
    const hb_bounds_t src_bounds = groups.pop ();
250
16.8M
    hb_bounds_t &backdrop_bounds = groups.tail ();
251
252
    // https://learn.microsoft.com/en-us/typography/opentype/spec/colr#format-32-paintcomposite
253
16.8M
    switch ((int) mode)
254
16.8M
    {
255
20.7k
      case HB_PAINT_COMPOSITE_MODE_CLEAR:
256
20.7k
  backdrop_bounds.status = hb_bounds_t::EMPTY;
257
20.7k
  break;
258
5.32k
      case HB_PAINT_COMPOSITE_MODE_SRC:
259
8.31k
      case HB_PAINT_COMPOSITE_MODE_SRC_OUT:
260
8.31k
  backdrop_bounds = src_bounds;
261
8.31k
  break;
262
5.32k
      case HB_PAINT_COMPOSITE_MODE_DEST:
263
8.95k
      case HB_PAINT_COMPOSITE_MODE_DEST_OUT:
264
8.95k
  break;
265
4.01k
      case HB_PAINT_COMPOSITE_MODE_SRC_IN:
266
10.8k
      case HB_PAINT_COMPOSITE_MODE_DEST_IN:
267
10.8k
  backdrop_bounds.intersect (src_bounds);
268
10.8k
  break;
269
16.8M
      default:
270
16.8M
  backdrop_bounds.union_ (src_bounds);
271
16.8M
  break;
272
16.8M
     }
273
16.8M
  }
274
275
  void paint ()
276
2.23M
  {
277
2.23M
    const hb_bounds_t &clip = clips.tail ();
278
2.23M
    hb_bounds_t &group = groups.tail ();
279
280
2.23M
    group.union_ (clip);
281
2.23M
  }
282
283
  protected:
284
  hb_vector_t<hb_transform_t> transforms;
285
  hb_vector_t<hb_bounds_t> clips;
286
  hb_vector_t<hb_bounds_t> groups;
287
};
288
289
HB_INTERNAL hb_paint_funcs_t *
290
hb_paint_extents_get_funcs ();
291
292
293
#endif /* HB_PAINT_EXTENTS_HH */