blob: d291a4b973121f8f22a59de682c43518a817fd08 [file] [log] [blame]
Matthias Clasen83d0a492022-12-13 21:14:25 -05001/*
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"
Behdad Esfahbod4b0285b2022-12-17 11:18:42 -070029#include "hb-face.hh"
30#include "hb-font.hh"
Matthias Clasen83d0a492022-12-13 21:14:25 -050031
32#define HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS \
33 HB_PAINT_FUNC_IMPLEMENT (push_transform) \
34 HB_PAINT_FUNC_IMPLEMENT (pop_transform) \
Matthias Clasenc9350832022-12-14 22:32:40 -050035 HB_PAINT_FUNC_IMPLEMENT (push_clip_glyph) \
Matthias Clasen485ba9b2022-12-17 12:25:04 -050036 HB_PAINT_FUNC_IMPLEMENT (push_clip_rectangle) \
Matthias Clasen83d0a492022-12-13 21:14:25 -050037 HB_PAINT_FUNC_IMPLEMENT (pop_clip) \
Matthias Clasen0d890062022-12-17 00:07:30 -050038 HB_PAINT_FUNC_IMPLEMENT (color) \
Matthias Clasen82e23f32022-12-17 00:33:59 -050039 HB_PAINT_FUNC_IMPLEMENT (image) \
Matthias Clasen83d0a492022-12-13 21:14:25 -050040 HB_PAINT_FUNC_IMPLEMENT (linear_gradient) \
41 HB_PAINT_FUNC_IMPLEMENT (radial_gradient) \
42 HB_PAINT_FUNC_IMPLEMENT (sweep_gradient) \
43 HB_PAINT_FUNC_IMPLEMENT (push_group) \
Matthias Clasen627c8572022-12-14 22:36:54 -050044 HB_PAINT_FUNC_IMPLEMENT (pop_group) \
Behdad Esfahbodbd733142023-01-18 20:37:21 -070045 HB_PAINT_FUNC_IMPLEMENT (custom_palette_color) \
Matthias Clasen83d0a492022-12-13 21:14:25 -050046 /* ^--- Add new callbacks here */
47
48struct 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,
Matthias Clasen64f1b552022-12-14 23:58:25 -050071 float xx, float yx,
72 float xy, float yy,
Matthias Clasen63870042022-12-21 16:43:19 -050073 float dx, float dy)
Matthias Clasen83d0a492022-12-13 21:14:25 -050074 { func.push_transform (this, paint_data,
Matthias Clasen64f1b552022-12-14 23:58:25 -050075 xx, yx, xy, yy, dx, dy,
Matthias Clasen83d0a492022-12-13 21:14:25 -050076 !user_data ? nullptr : user_data->push_transform); }
Matthias Clasen63870042022-12-21 16:43:19 -050077 void pop_transform (void *paint_data)
78 { func.pop_transform (this, paint_data,
Matthias Clasen83d0a492022-12-13 21:14:25 -050079 !user_data ? nullptr : user_data->pop_transform); }
Matthias Clasenc9350832022-12-14 22:32:40 -050080 void push_clip_glyph (void *paint_data,
Matthias Clasen97224f32022-12-21 02:11:36 -050081 hb_codepoint_t glyph,
Matthias Clasen63870042022-12-21 16:43:19 -050082 hb_font_t *font)
Matthias Clasenc9350832022-12-14 22:32:40 -050083 { func.push_clip_glyph (this, paint_data,
84 glyph,
Matthias Clasen63870042022-12-21 16:43:19 -050085 font,
Matthias Clasenc9350832022-12-14 22:32:40 -050086 !user_data ? nullptr : user_data->push_clip_glyph); }
Matthias Clasen485ba9b2022-12-17 12:25:04 -050087 void push_clip_rectangle (void *paint_data,
Matthias Clasen63870042022-12-21 16:43:19 -050088 float xmin, float ymin, float xmax, float ymax)
Matthias Clasen485ba9b2022-12-17 12:25:04 -050089 { func.push_clip_rectangle (this, paint_data,
90 xmin, ymin, xmax, ymax,
91 !user_data ? nullptr : user_data->push_clip_rectangle); }
Matthias Clasen63870042022-12-21 16:43:19 -050092 void pop_clip (void *paint_data)
93 { func.pop_clip (this, paint_data,
Matthias Clasen83d0a492022-12-13 21:14:25 -050094 !user_data ? nullptr : user_data->pop_clip); }
Matthias Clasen0d890062022-12-17 00:07:30 -050095 void color (void *paint_data,
Matthias Clasenc2219332022-12-21 18:39:27 -050096 hb_bool_t is_foreground,
Matthias Clasen63870042022-12-21 16:43:19 -050097 hb_color_t color)
Matthias Clasen0d890062022-12-17 00:07:30 -050098 { func.color (this, paint_data,
Matthias Clasenc2219332022-12-21 18:39:27 -050099 is_foreground, color,
Matthias Clasen0d890062022-12-17 00:07:30 -0500100 !user_data ? nullptr : user_data->color); }
Behdad Esfahbodf70c5d62022-12-24 10:56:06 -0700101 bool image (void *paint_data,
Matthias Clasen37f3f0f2022-12-17 11:49:18 -0500102 hb_blob_t *image,
Behdad Esfahbodc5f90382022-12-22 12:16:55 -0700103 unsigned width, unsigned height,
Matthias Clasen0a2f3672022-12-17 13:51:23 -0500104 hb_tag_t format,
Behdad Esfahbod237955d2022-12-22 08:12:47 -0700105 float slant,
Matthias Clasen63870042022-12-21 16:43:19 -0500106 hb_glyph_extents_t *extents)
Behdad Esfahbodf70c5d62022-12-24 10:56:06 -0700107 { return func.image (this, paint_data,
108 image, width, height, format, slant, extents,
109 !user_data ? nullptr : user_data->image); }
Matthias Clasen83d0a492022-12-13 21:14:25 -0500110 void linear_gradient (void *paint_data,
111 hb_color_line_t *color_line,
Matthias Clasenc9350832022-12-14 22:32:40 -0500112 float x0, float y0,
113 float x1, float y1,
Matthias Clasen63870042022-12-21 16:43:19 -0500114 float x2, float y2)
Matthias Clasen83d0a492022-12-13 21:14:25 -0500115 { func.linear_gradient (this, paint_data,
116 color_line, x0, y0, x1, y1, x2, y2,
117 !user_data ? nullptr : user_data->linear_gradient); }
118 void radial_gradient (void *paint_data,
119 hb_color_line_t *color_line,
Matthias Clasenc9350832022-12-14 22:32:40 -0500120 float x0, float y0, float r0,
Matthias Clasen63870042022-12-21 16:43:19 -0500121 float x1, float y1, float r1)
Matthias Clasen83d0a492022-12-13 21:14:25 -0500122 { func.radial_gradient (this, paint_data,
123 color_line, x0, y0, r0, x1, y1, r1,
124 !user_data ? nullptr : user_data->radial_gradient); }
125 void sweep_gradient (void *paint_data,
126 hb_color_line_t *color_line,
Matthias Clasenc9350832022-12-14 22:32:40 -0500127 float x0, float y0,
Matthias Clasen83d0a492022-12-13 21:14:25 -0500128 float start_angle,
Matthias Clasen63870042022-12-21 16:43:19 -0500129 float end_angle)
Matthias Clasen83d0a492022-12-13 21:14:25 -0500130 { func.sweep_gradient (this, paint_data,
131 color_line, x0, y0, start_angle, end_angle,
132 !user_data ? nullptr : user_data->sweep_gradient); }
Matthias Clasen63870042022-12-21 16:43:19 -0500133 void push_group (void *paint_data)
134 { func.push_group (this, paint_data,
Matthias Clasen83d0a492022-12-13 21:14:25 -0500135 !user_data ? nullptr : user_data->push_group); }
Matthias Clasen627c8572022-12-14 22:36:54 -0500136 void pop_group (void *paint_data,
Matthias Clasen63870042022-12-21 16:43:19 -0500137 hb_paint_composite_mode_t mode)
Matthias Clasen627c8572022-12-14 22:36:54 -0500138 { func.pop_group (this, paint_data,
139 mode,
140 !user_data ? nullptr : user_data->pop_group); }
Behdad Esfahbod638e0ed2023-01-20 13:01:22 -0700141 bool custom_palette_color (void *paint_data,
142 unsigned int color_index,
143 hb_color_t *color)
Behdad Esfahbodbd733142023-01-18 20:37:21 -0700144 { return func.custom_palette_color (this, paint_data,
145 color_index,
Behdad Esfahbod638e0ed2023-01-20 13:01:22 -0700146 color,
Behdad Esfahbodbd733142023-01-18 20:37:21 -0700147 !user_data ? nullptr : user_data->custom_palette_color); }
Matthias Clasen83d0a492022-12-13 21:14:25 -0500148
Behdad Esfahboddf916772022-12-31 11:12:02 -0700149
150 /* Internal specializations. */
151
Matthias Clasen686e6272022-12-17 12:44:16 -0500152 void push_root_transform (void *paint_data,
Matthias Clasen63870042022-12-21 16:43:19 -0500153 const hb_font_t *font)
Matthias Clasen686e6272022-12-17 12:44:16 -0500154 {
Behdad Esfahbod4b0285b2022-12-17 11:18:42 -0700155 float upem = font->face->get_upem ();
Behdad Esfahbod99da0e62022-12-31 11:04:40 -0700156 int xscale = font->x_scale, yscale = font->y_scale;
Behdad Esfahbod4b0285b2022-12-17 11:18:42 -0700157 float slant = font->slant_xy;
158
Behdad Esfahboddf916772022-12-31 11:12:02 -0700159 push_transform (paint_data,
160 xscale/upem, 0, slant * yscale/upem, yscale/upem, 0, 0);
Behdad Esfahbod4b0285b2022-12-17 11:18:42 -0700161 }
Matthias Clasen97224f32022-12-21 02:11:36 -0500162
163 void push_inverse_root_transform (void *paint_data,
Matthias Clasen63870042022-12-21 16:43:19 -0500164 hb_font_t *font)
Matthias Clasen97224f32022-12-21 02:11:36 -0500165 {
Matthias Clasen97224f32022-12-21 02:11:36 -0500166 float upem = font->face->get_upem ();
Behdad Esfahbod99da0e62022-12-31 11:04:40 -0700167 int xscale = font->x_scale ? font->x_scale : upem;
168 int yscale = font->y_scale ? font->y_scale : upem;
Matthias Clasen97224f32022-12-21 02:11:36 -0500169 float slant = font->slant_xy;
170
Behdad Esfahboddf916772022-12-31 11:12:02 -0700171 push_transform (paint_data,
172 upem/xscale, 0, -slant * upem/xscale, upem/yscale, 0, 0);
Matthias Clasen97224f32022-12-21 02:11:36 -0500173 }
Behdad Esfahbod7363eb32022-12-31 11:24:42 -0700174
Behdad Esfahbod3ff91c42022-12-31 14:49:41 -0700175 HB_NODISCARD
176 bool push_translate (void *paint_data,
Behdad Esfahbod7363eb32022-12-31 11:24:42 -0700177 float dx, float dy)
178 {
Behdad Esfahbod3ff91c42022-12-31 14:49:41 -0700179 if (!dx && !dy)
180 return false;
181
182 push_transform (paint_data,
183 1.f, 0.f, 0.f, 1.f, dx, dy);
184 return true;
Behdad Esfahbod7363eb32022-12-31 11:24:42 -0700185 }
Behdad Esfahbodce783512022-12-31 11:32:15 -0700186
Behdad Esfahbod3ff91c42022-12-31 14:49:41 -0700187 HB_NODISCARD
188 bool push_scale (void *paint_data,
Behdad Esfahbodce783512022-12-31 11:32:15 -0700189 float sx, float sy)
190 {
Behdad Esfahbod3ff91c42022-12-31 14:49:41 -0700191 if (sx == 1.f && sy == 1.f)
192 return false;
193
194 push_transform (paint_data,
195 sx, 0.f, 0.f, sy, 0.f, 0.f);
196 return true;
Behdad Esfahbodce783512022-12-31 11:32:15 -0700197 }
Behdad Esfahbod46adf312022-12-31 11:35:39 -0700198
Behdad Esfahbod3ff91c42022-12-31 14:49:41 -0700199 HB_NODISCARD
200 bool push_rotate (void *paint_data,
Behdad Esfahbod46adf312022-12-31 11:35:39 -0700201 float a)
202 {
Behdad Esfahbod3ff91c42022-12-31 14:49:41 -0700203 if (!a)
204 return false;
205
Behdad Esfahbod75e64982023-03-14 12:41:46 -0600206 float cc = cosf (a * HB_PI);
207 float ss = sinf (a * HB_PI);
Behdad Esfahbod3ff91c42022-12-31 14:49:41 -0700208 push_transform (paint_data, cc, ss, -ss, cc, 0.f, 0.f);
209 return true;
Behdad Esfahbod46adf312022-12-31 11:35:39 -0700210 }
Behdad Esfahbod6b47fcb2022-12-31 11:40:12 -0700211
Behdad Esfahbod3ff91c42022-12-31 14:49:41 -0700212 HB_NODISCARD
213 bool push_skew (void *paint_data,
Behdad Esfahbod6b47fcb2022-12-31 11:40:12 -0700214 float sx, float sy)
215 {
Behdad Esfahbod3ff91c42022-12-31 14:49:41 -0700216 if (!sx && !sy)
217 return false;
218
Behdad Esfahbod75e64982023-03-14 12:41:46 -0600219 float x = tanf (-sx * HB_PI);
220 float y = tanf (+sy * HB_PI);
Behdad Esfahbod3ff91c42022-12-31 14:49:41 -0700221 push_transform (paint_data, 1.f, y, x, 1.f, 0.f, 0.f);
222 return true;
Behdad Esfahbod6b47fcb2022-12-31 11:40:12 -0700223 }
Matthias Clasen83d0a492022-12-13 21:14:25 -0500224};
225DECLARE_NULL_INSTANCE (hb_paint_funcs_t);
226
Matthias Clasen71bd5a02022-12-21 16:18:46 -0500227
Matthias Clasen83d0a492022-12-13 21:14:25 -0500228#endif /* HB_PAINT_HH */