Coverage Report

Created: 2024-03-28 05:33

/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
4.28M
  { func.push_transform (this, paint_data,
75
4.28M
                         xx, yx, xy, yy, dx, dy,
76
4.28M
                         !user_data ? nullptr : user_data->push_transform); }
77
  void pop_transform (void *paint_data)
78
4.28M
  { func.pop_transform (this, paint_data,
79
4.28M
                        !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
993k
  { func.push_clip_glyph (this, paint_data,
84
993k
                          glyph,
85
993k
                          font,
86
993k
                          !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
57.5k
  { func.push_clip_rectangle (this, paint_data,
90
57.5k
                              xmin, ymin, xmax, ymax,
91
57.5k
                              !user_data ? nullptr : user_data->push_clip_rectangle); }
92
  void pop_clip (void *paint_data)
93
1.05M
  { func.pop_clip (this, paint_data,
94
1.05M
                   !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
535k
  { func.color (this, paint_data,
99
535k
                is_foreground, color,
100
535k
                !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
223k
  { func.linear_gradient (this, paint_data,
116
223k
                          color_line, x0, y0, x1, y1, x2, y2,
117
223k
                          !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
262k
  { func.radial_gradient (this, paint_data,
123
262k
                          color_line, x0, y0, r0, x1, y1, r1,
124
262k
                          !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
118k
  { func.sweep_gradient (this, paint_data,
131
118k
                         color_line, x0, y0, start_angle, end_angle,
132
118k
                         !user_data ? nullptr : user_data->sweep_gradient); }
133
  void push_group (void *paint_data)
134
32.8M
  { func.push_group (this, paint_data,
135
32.8M
                     !user_data ? nullptr : user_data->push_group); }
136
  void pop_group (void *paint_data,
137
                  hb_paint_composite_mode_t mode)
138
32.8M
  { func.pop_group (this, paint_data,
139
32.8M
                    mode,
140
32.8M
                    !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
529k
  { return func.custom_palette_color (this, paint_data,
145
529k
                                      color_index,
146
529k
                                      color,
147
529k
                                      !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
1.02M
  {
155
1.02M
    float upem = font->face->get_upem ();
156
1.02M
    int xscale = font->x_scale, yscale = font->y_scale;
157
1.02M
    float slant = font->slant_xy;
158
159
1.02M
    push_transform (paint_data,
160
1.02M
        xscale/upem, 0, slant * yscale/upem, yscale/upem, 0, 0);
161
1.02M
  }
162
163
  void push_inverse_root_transform (void *paint_data,
164
                                    hb_font_t *font)
165
918k
  {
166
918k
    float upem = font->face->get_upem ();
167
918k
    int xscale = font->x_scale ? font->x_scale : upem;
168
918k
    int yscale = font->y_scale ? font->y_scale : upem;
169
918k
    float slant = font->slant_xy;
170
171
918k
    push_transform (paint_data,
172
918k
        upem/xscale, 0, -slant * upem/xscale, upem/yscale, 0, 0);
173
918k
  }
174
175
  HB_NODISCARD
176
  bool push_translate (void *paint_data,
177
                       float dx, float dy)
178
1.27M
  {
179
1.27M
    if (!dx && !dy)
180
68.4k
      return false;
181
182
1.20M
    push_transform (paint_data,
183
1.20M
        1.f, 0.f, 0.f, 1.f, dx, dy);
184
1.20M
    return true;
185
1.27M
  }
186
187
  HB_NODISCARD
188
  bool push_scale (void *paint_data,
189
                   float sx, float sy)
190
724k
  {
191
724k
    if (sx == 1.f && sy == 1.f)
192
30.1k
      return false;
193
194
694k
    push_transform (paint_data,
195
694k
        sx, 0.f, 0.f, sy, 0.f, 0.f);
196
694k
    return true;
197
724k
  }
198
199
  HB_NODISCARD
200
  bool push_rotate (void *paint_data,
201
                    float a)
202
191k
  {
203
191k
    if (!a)
204
52.5k
      return false;
205
206
138k
    float cc = cosf (a * (float) M_PI);
207
138k
    float ss = sinf (a * (float) M_PI);
208
138k
    push_transform (paint_data, cc, ss, -ss, cc, 0.f, 0.f);
209
138k
    return true;
210
191k
  }
211
212
  HB_NODISCARD
213
  bool push_skew (void *paint_data,
214
                  float sx, float sy)
215
134k
  {
216
134k
    if (!sx && !sy)
217
13.8k
      return false;
218
219
120k
    float x = tanf (-sx * (float) M_PI);
220
120k
    float y = tanf (+sy * (float) M_PI);
221
120k
    push_transform (paint_data, 1.f, y, x, 1.f, 0.f, 0.f);
222
120k
    return true;
223
134k
  }
224
};
225
DECLARE_NULL_INSTANCE (hb_paint_funcs_t);
226
227
228
#endif /* HB_PAINT_HH */