Coverage Report

Created: 2024-01-23 06:24

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