Coverage Report

Created: 2025-07-07 10:01

/work/workdir/UnpackedTarball/harfbuzz/src/hb-paint.hh
Line
Count
Source (jump to first uncovered line)
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
32
#define HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS \
33
0
  HB_PAINT_FUNC_IMPLEMENT (push_transform) \
34
0
  HB_PAINT_FUNC_IMPLEMENT (pop_transform) \
35
0
  HB_PAINT_FUNC_IMPLEMENT (color_glyph) \
36
0
  HB_PAINT_FUNC_IMPLEMENT (push_clip_glyph) \
37
0
  HB_PAINT_FUNC_IMPLEMENT (push_clip_rectangle) \
38
0
  HB_PAINT_FUNC_IMPLEMENT (pop_clip) \
39
0
  HB_PAINT_FUNC_IMPLEMENT (color) \
40
0
  HB_PAINT_FUNC_IMPLEMENT (image) \
41
0
  HB_PAINT_FUNC_IMPLEMENT (linear_gradient) \
42
0
  HB_PAINT_FUNC_IMPLEMENT (radial_gradient) \
43
0
  HB_PAINT_FUNC_IMPLEMENT (sweep_gradient) \
44
0
  HB_PAINT_FUNC_IMPLEMENT (push_group) \
45
0
  HB_PAINT_FUNC_IMPLEMENT (pop_group) \
46
0
  HB_PAINT_FUNC_IMPLEMENT (custom_palette_color) \
47
  /* ^--- Add new callbacks here */
48
49
struct hb_paint_funcs_t
50
{
51
  hb_object_header_t header;
52
53
  struct {
54
#define HB_PAINT_FUNC_IMPLEMENT(name) hb_paint_##name##_func_t name;
55
    HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
56
#undef HB_PAINT_FUNC_IMPLEMENT
57
  } func;
58
59
  struct {
60
#define HB_PAINT_FUNC_IMPLEMENT(name) void *name;
61
    HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
62
#undef HB_PAINT_FUNC_IMPLEMENT
63
  } *user_data;
64
65
  struct {
66
#define HB_PAINT_FUNC_IMPLEMENT(name) hb_destroy_func_t name;
67
    HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
68
#undef HB_PAINT_FUNC_IMPLEMENT
69
  } *destroy;
70
71
  void push_transform (void *paint_data,
72
                       float xx, float yx,
73
                       float xy, float yy,
74
                       float dx, float dy)
75
0
  { func.push_transform (this, paint_data,
76
0
                         xx, yx, xy, yy, dx, dy,
77
0
                         !user_data ? nullptr : user_data->push_transform); }
78
  void pop_transform (void *paint_data)
79
0
  { func.pop_transform (this, paint_data,
80
0
                        !user_data ? nullptr : user_data->pop_transform); }
81
  bool color_glyph (void *paint_data,
82
                    hb_codepoint_t glyph,
83
                    hb_font_t *font)
84
0
  { return func.color_glyph (this, paint_data,
85
0
                             glyph,
86
0
                             font,
87
0
                             !user_data ? nullptr : user_data->push_clip_glyph); }
88
  void push_clip_glyph (void *paint_data,
89
                        hb_codepoint_t glyph,
90
                        hb_font_t *font)
91
0
  { func.push_clip_glyph (this, paint_data,
92
0
                          glyph,
93
0
                          font,
94
0
                          !user_data ? nullptr : user_data->push_clip_glyph); }
95
  void push_clip_rectangle (void *paint_data,
96
                           float xmin, float ymin, float xmax, float ymax)
97
0
  { func.push_clip_rectangle (this, paint_data,
98
0
                              xmin, ymin, xmax, ymax,
99
0
                              !user_data ? nullptr : user_data->push_clip_rectangle); }
100
  void pop_clip (void *paint_data)
101
0
  { func.pop_clip (this, paint_data,
102
0
                   !user_data ? nullptr : user_data->pop_clip); }
103
  void color (void *paint_data,
104
              hb_bool_t is_foreground,
105
              hb_color_t color)
106
0
  { func.color (this, paint_data,
107
0
                is_foreground, color,
108
0
                !user_data ? nullptr : user_data->color); }
109
  bool image (void *paint_data,
110
              hb_blob_t *image,
111
              unsigned width, unsigned height,
112
              hb_tag_t format,
113
              float slant,
114
              hb_glyph_extents_t *extents)
115
0
  { return func.image (this, paint_data,
116
0
                       image, width, height, format, slant, extents,
117
0
                       !user_data ? nullptr : user_data->image); }
118
  void linear_gradient (void *paint_data,
119
                        hb_color_line_t *color_line,
120
                        float x0, float y0,
121
                        float x1, float y1,
122
                        float x2, float y2)
123
0
  { func.linear_gradient (this, paint_data,
124
0
                          color_line, x0, y0, x1, y1, x2, y2,
125
0
                          !user_data ? nullptr : user_data->linear_gradient); }
126
  void radial_gradient (void *paint_data,
127
                        hb_color_line_t *color_line,
128
                        float x0, float y0, float r0,
129
                        float x1, float y1, float r1)
130
0
  { func.radial_gradient (this, paint_data,
131
0
                          color_line, x0, y0, r0, x1, y1, r1,
132
0
                          !user_data ? nullptr : user_data->radial_gradient); }
133
  void sweep_gradient (void *paint_data,
134
                       hb_color_line_t *color_line,
135
                       float x0, float y0,
136
                       float start_angle,
137
                       float end_angle)
138
0
  { func.sweep_gradient (this, paint_data,
139
0
                         color_line, x0, y0, start_angle, end_angle,
140
0
                         !user_data ? nullptr : user_data->sweep_gradient); }
141
  void push_group (void *paint_data)
142
0
  { func.push_group (this, paint_data,
143
0
                     !user_data ? nullptr : user_data->push_group); }
144
  void pop_group (void *paint_data,
145
                  hb_paint_composite_mode_t mode)
146
0
  { func.pop_group (this, paint_data,
147
0
                    mode,
148
0
                    !user_data ? nullptr : user_data->pop_group); }
149
  bool custom_palette_color (void *paint_data,
150
                             unsigned int color_index,
151
                             hb_color_t *color)
152
0
  { return func.custom_palette_color (this, paint_data,
153
0
                                      color_index,
154
0
                                      color,
155
0
                                      !user_data ? nullptr : user_data->custom_palette_color); }
156
157
158
  /* Internal specializations. */
159
160
  void push_font_transform (void *paint_data,
161
                            const hb_font_t *font)
162
0
  {
163
0
    float upem = font->face->get_upem ();
164
0
    int xscale = font->x_scale, yscale = font->y_scale;
165
166
0
    push_transform (paint_data,
167
0
        xscale/upem, 0,
168
0
        0, yscale/upem,
169
0
        0, 0);
170
0
  }
171
172
  void push_inverse_font_transform (void *paint_data,
173
                                    const hb_font_t *font)
174
0
  {
175
0
    float upem = font->face->get_upem ();
176
0
    int xscale = font->x_scale ? font->x_scale : upem;
177
0
    int yscale = font->y_scale ? font->y_scale : upem;
178
179
0
    push_transform (paint_data,
180
0
        upem/xscale, 0,
181
0
        0, upem/yscale,
182
0
        0, 0);
183
0
  }
184
185
  HB_NODISCARD
186
  bool push_translate (void *paint_data,
187
                       float dx, float dy)
188
0
  {
189
0
    if (!dx && !dy)
190
0
      return false;
191
192
0
    push_transform (paint_data,
193
0
        1.f, 0.f, 0.f, 1.f, dx, dy);
194
0
    return true;
195
0
  }
196
197
  HB_NODISCARD
198
  bool push_scale (void *paint_data,
199
                   float sx, float sy)
200
0
  {
201
0
    if (sx == 1.f && sy == 1.f)
202
0
      return false;
203
204
0
    push_transform (paint_data,
205
0
        sx, 0.f, 0.f, sy, 0.f, 0.f);
206
0
    return true;
207
0
  }
208
209
  HB_NODISCARD
210
  bool push_rotate (void *paint_data,
211
                    float a)
212
0
  {
213
0
    if (!a)
214
0
      return false;
215
216
0
    float cc = cosf (a * HB_PI);
217
0
    float ss = sinf (a * HB_PI);
218
0
    push_transform (paint_data, cc, ss, -ss, cc, 0.f, 0.f);
219
0
    return true;
220
0
  }
221
222
  HB_NODISCARD
223
  bool push_skew (void *paint_data,
224
                  float sx, float sy)
225
0
  {
226
0
    if (!sx && !sy)
227
0
      return false;
228
229
0
    float x = tanf (-sx * HB_PI);
230
0
    float y = tanf (+sy * HB_PI);
231
0
    push_transform (paint_data, 1.f, y, x, 1.f, 0.f, 0.f);
232
0
    return true;
233
0
  }
234
};
235
DECLARE_NULL_INSTANCE (hb_paint_funcs_t);
236
237
238
#endif /* HB_PAINT_HH */