Coverage Report

Created: 2026-09-04 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/hb-paint.hh
Line
Count
Source
1
/*
2
 * Copyright © 2022 Matthias Clasen
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_HH
26
#define HB_PAINT_HH
27
28
#include "hb.hh"
29
#include "hb-face.hh"
30
#include "hb-font.hh"
31
#include "hb-geometry.hh"
32
33
#define HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS \
34
0
  HB_PAINT_FUNC_IMPLEMENT (push_transform) \
35
0
  HB_PAINT_FUNC_IMPLEMENT (pop_transform) \
36
0
  HB_PAINT_FUNC_IMPLEMENT (color_glyph) \
37
0
  HB_PAINT_FUNC_IMPLEMENT (push_clip_glyph) \
38
0
  HB_PAINT_FUNC_IMPLEMENT (push_clip_rectangle) \
39
0
  HB_PAINT_FUNC_IMPLEMENT (push_clip_path_start) \
40
0
  HB_PAINT_FUNC_IMPLEMENT (push_clip_path_end) \
41
0
  HB_PAINT_FUNC_IMPLEMENT (pop_clip) \
42
0
  HB_PAINT_FUNC_IMPLEMENT (color) \
43
0
  HB_PAINT_FUNC_IMPLEMENT (image) \
44
0
  HB_PAINT_FUNC_IMPLEMENT (linear_gradient) \
45
0
  HB_PAINT_FUNC_IMPLEMENT (radial_gradient) \
46
0
  HB_PAINT_FUNC_IMPLEMENT (sweep_gradient) \
47
0
  HB_PAINT_FUNC_IMPLEMENT (push_group) \
48
0
  HB_PAINT_FUNC_IMPLEMENT (push_group_for) \
49
0
  HB_PAINT_FUNC_IMPLEMENT (pop_group) \
50
0
  HB_PAINT_FUNC_IMPLEMENT (custom_palette_color) \
51
0
  HB_PAINT_FUNC_IMPLEMENT (fill_glyph) \
52
  /* ^--- Add new callbacks here */
53
54
struct hb_paint_funcs_t
55
{
56
  hb_object_header_t header;
57
58
  struct {
59
#define HB_PAINT_FUNC_IMPLEMENT(name) hb_paint_##name##_func_t name;
60
    HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
61
#undef HB_PAINT_FUNC_IMPLEMENT
62
  } func;
63
64
  struct {
65
#define HB_PAINT_FUNC_IMPLEMENT(name) void *name;
66
    HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
67
#undef HB_PAINT_FUNC_IMPLEMENT
68
  } *user_data;
69
70
  struct {
71
#define HB_PAINT_FUNC_IMPLEMENT(name) hb_destroy_func_t name;
72
    HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
73
#undef HB_PAINT_FUNC_IMPLEMENT
74
  } *destroy;
75
76
  void push_transform (void *paint_data,
77
                       float xx, float yx,
78
                       float xy, float yy,
79
                       float dx, float dy)
80
0
  {
81
    // Handle -0.f to avoid -0.f == 0.f in the transform matrix.
82
0
    if (dx == -0.f) dx = 0.f;
83
0
    if (dy == -0.f) dy = 0.f;
84
0
    func.push_transform (this, paint_data,
85
0
                         xx, yx, xy, yy, dx, dy,
86
0
                         !user_data ? nullptr : user_data->push_transform); }
87
  void pop_transform (void *paint_data)
88
0
  { func.pop_transform (this, paint_data,
89
0
                        !user_data ? nullptr : user_data->pop_transform); }
90
  bool color_glyph (void *paint_data,
91
                    hb_codepoint_t glyph,
92
                    hb_font_t *font)
93
0
  { return func.color_glyph (this, paint_data,
94
0
                             glyph,
95
0
                             font,
96
0
                             !user_data ? nullptr : user_data->color_glyph); }
97
  void push_clip_glyph (void *paint_data,
98
                        hb_codepoint_t glyph,
99
                        hb_font_t *font)
100
0
  { func.push_clip_glyph (this, paint_data,
101
0
                          glyph,
102
0
                          font,
103
0
                          !user_data ? nullptr : user_data->push_clip_glyph); }
104
  void push_clip_rectangle (void *paint_data,
105
                           float xmin, float ymin, float xmax, float ymax)
106
0
  { func.push_clip_rectangle (this, paint_data,
107
0
                              xmin, ymin, xmax, ymax,
108
0
                              !user_data ? nullptr : user_data->push_clip_rectangle); }
109
  hb_draw_funcs_t *push_clip_path_start (void *paint_data,
110
                                         void **draw_data)
111
0
  { return func.push_clip_path_start (this, paint_data, draw_data,
112
0
                                      !user_data ? nullptr : user_data->push_clip_path_start); }
113
  void push_clip_path_end (void *paint_data)
114
0
  { func.push_clip_path_end (this, paint_data,
115
0
                             !user_data ? nullptr : user_data->push_clip_path_end); }
116
  void pop_clip (void *paint_data)
117
0
  { func.pop_clip (this, paint_data,
118
0
                   !user_data ? nullptr : user_data->pop_clip); }
119
  void color (void *paint_data,
120
              hb_bool_t is_foreground,
121
              hb_color_t color)
122
0
  { func.color (this, paint_data,
123
0
                is_foreground, color,
124
0
                !user_data ? nullptr : user_data->color); }
125
  void fill_glyph (void *paint_data,
126
                   hb_codepoint_t glyph,
127
                   hb_font_t *font,
128
                   hb_bool_t is_foreground,
129
                   hb_color_t color)
130
0
  { func.fill_glyph (this, paint_data,
131
0
                     glyph, font,
132
0
                     is_foreground, color,
133
0
                     !user_data ? nullptr : user_data->fill_glyph); }
134
  bool image (void *paint_data,
135
              hb_blob_t *image,
136
              unsigned width, unsigned height,
137
              hb_tag_t format,
138
              float slant,
139
              hb_glyph_extents_t *extents)
140
0
  { return func.image (this, paint_data,
141
0
                       image, width, height, format, slant, extents,
142
0
                       !user_data ? nullptr : user_data->image); }
143
  void linear_gradient (void *paint_data,
144
                        hb_color_line_t *color_line,
145
                        float x0, float y0,
146
                        float x1, float y1,
147
                        float x2, float y2)
148
0
  { func.linear_gradient (this, paint_data,
149
0
                          color_line, x0, y0, x1, y1, x2, y2,
150
0
                          !user_data ? nullptr : user_data->linear_gradient); }
151
  void radial_gradient (void *paint_data,
152
                        hb_color_line_t *color_line,
153
                        float x0, float y0, float r0,
154
                        float x1, float y1, float r1)
155
0
  { func.radial_gradient (this, paint_data,
156
0
                          color_line, x0, y0, r0, x1, y1, r1,
157
0
                          !user_data ? nullptr : user_data->radial_gradient); }
158
  void sweep_gradient (void *paint_data,
159
                       hb_color_line_t *color_line,
160
                       float x0, float y0,
161
                       float start_angle,
162
                       float end_angle)
163
0
  { func.sweep_gradient (this, paint_data,
164
0
                         color_line, x0, y0, start_angle, end_angle,
165
0
                         !user_data ? nullptr : user_data->sweep_gradient); }
166
  void push_group (void *paint_data)
167
0
  { func.push_group (this, paint_data,
168
0
                     !user_data ? nullptr : user_data->push_group); }
169
  void push_group_for (void *paint_data,
170
                       hb_paint_composite_mode_t mode)
171
0
  { func.push_group_for (this, paint_data,
172
0
                         mode,
173
0
                         !user_data ? nullptr : user_data->push_group_for); }
174
  void pop_group (void *paint_data,
175
                  hb_paint_composite_mode_t mode)
176
0
  { func.pop_group (this, paint_data,
177
0
                    mode,
178
0
                    !user_data ? nullptr : user_data->pop_group); }
179
  bool custom_palette_color (void *paint_data,
180
                             unsigned int color_index,
181
                             hb_color_t *color)
182
0
  { return func.custom_palette_color (this, paint_data,
183
0
                                      color_index,
184
0
                                      color,
185
0
                                      !user_data ? nullptr : user_data->custom_palette_color); }
186
187
188
  /* Internal specializations. */
189
190
  void push_font_transform (void *paint_data,
191
                            const hb_font_t *font)
192
0
  {
193
0
    float upem = font->face->get_upem ();
194
0
    int xscale = font->x_scale, yscale = font->y_scale;
195
196
0
    push_transform (paint_data,
197
0
        xscale/upem, 0,
198
0
        0, yscale/upem,
199
0
        0, 0);
200
0
  }
201
202
  void push_inverse_font_transform (void *paint_data,
203
                                    const hb_font_t *font)
204
0
  {
205
0
    float upem = font->face->get_upem ();
206
0
    int xscale = font->x_scale ? font->x_scale : upem;
207
0
    int yscale = font->y_scale ? font->y_scale : upem;
208
209
0
    push_transform (paint_data,
210
0
        upem/xscale, 0,
211
0
        0, upem/yscale,
212
0
        0, 0);
213
0
  }
214
215
  void push_transform (void *paint_data, hb_transform_t<float> t)
216
0
  {
217
0
    push_transform (paint_data, t.xx, t.yx, t.xy, t.yy, t.x0, t.y0);
218
0
  }
219
220
  void push_translate (void *paint_data,
221
                       float dx, float dy)
222
0
  {
223
0
    push_transform (paint_data,
224
0
        hb_transform_t<float>::translation (dx, dy));
225
0
  }
226
227
  void push_scale (void *paint_data,
228
                   float sx, float sy)
229
0
  {
230
0
    push_transform (paint_data,
231
0
        hb_transform_t<float>::scaling (sx, sy));
232
0
  }
233
  void push_scale_around_center (void *paint_data,
234
         float sx, float sy,
235
         float cx, float cy)
236
0
  {
237
0
    push_transform (paint_data,
238
0
        hb_transform_t<float>::scaling_around_center (sx, sy, cx, cy));
239
0
  }
240
241
  void push_rotate (void *paint_data,
242
                    float a)
243
0
  {
244
0
    push_transform (paint_data,
245
0
        hb_transform_t<float>::rotation (a * HB_PI));
246
0
  }
247
248
  void push_rotate_around_center (void *paint_data,
249
          float a,
250
          float cx, float cy)
251
0
  {
252
0
    push_transform (paint_data,
253
0
        hb_transform_t<float>::rotation_around_center (a * HB_PI, cx, cy));
254
0
  }
255
256
  void push_skew (void *paint_data,
257
                  float sx, float sy)
258
0
  {
259
0
    push_transform (paint_data,
260
0
        hb_transform_t<float>::skewing (-sx * HB_PI, sy * HB_PI));
261
0
  }
262
  void push_skew_around_center (void *paint_data,
263
        float sx, float sy,
264
        float cx, float cy)
265
0
  {
266
0
    push_transform (paint_data,
267
0
        hb_transform_t<float>::skewing_around_center (-sx * HB_PI, sy * HB_PI, cx, cy));
268
0
  }
269
};
270
DECLARE_NULL_INSTANCE (hb_paint_funcs_t);
271
272
273
/* Linearly interpolate between two hb_color_t values, component-wise,
274
 * in byte-channel space with rounding. */
275
static inline hb_color_t
276
hb_color_lerp (hb_color_t c0, hb_color_t c1, float t)
277
0
{
278
0
  auto lerp = [&] (unsigned shift) -> unsigned {
279
0
    unsigned v0 = (c0 >> shift) & 0xFF;
280
0
    unsigned v1 = (c1 >> shift) & 0xFF;
281
0
    return (unsigned) (v0 + t * ((float) v1 - (float) v0) + 0.5f);
282
0
  };
283
0
  return HB_COLOR (lerp (0), lerp (8), lerp (16), lerp (24));
284
0
}
Unexecuted instantiation: hb-font.cc:hb_color_lerp(unsigned int, unsigned int, float)
Unexecuted instantiation: hb-ot-face.cc:hb_color_lerp(unsigned int, unsigned int, float)
Unexecuted instantiation: hb-ot-font.cc:hb_color_lerp(unsigned int, unsigned int, float)
Unexecuted instantiation: VARC.cc:hb_color_lerp(unsigned int, unsigned int, float)
Unexecuted instantiation: hb-static.cc:hb_color_lerp(unsigned int, unsigned int, float)
Unexecuted instantiation: hb-ft.cc:hb_color_lerp(unsigned int, unsigned int, float)
Unexecuted instantiation: hb-paint.cc:hb_color_lerp(unsigned int, unsigned int, float)
Unexecuted instantiation: hb-ot-cff1-table.cc:hb_color_lerp(unsigned int, unsigned int, float)
Unexecuted instantiation: hb-ot-cff2-table.cc:hb_color_lerp(unsigned int, unsigned int, float)
285
286
#endif /* HB_PAINT_HH */