Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 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" |
Behdad Esfahbod | 4b0285b | 2022-12-17 11:18:42 -0700 | [diff] [blame] | 29 | #include "hb-face.hh" |
| 30 | #include "hb-font.hh" |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 31 | |
| 32 | #define HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS \ |
| 33 | HB_PAINT_FUNC_IMPLEMENT (push_transform) \ |
| 34 | HB_PAINT_FUNC_IMPLEMENT (pop_transform) \ |
Matthias Clasen | c935083 | 2022-12-14 22:32:40 -0500 | [diff] [blame] | 35 | HB_PAINT_FUNC_IMPLEMENT (push_clip_glyph) \ |
Matthias Clasen | 485ba9b | 2022-12-17 12:25:04 -0500 | [diff] [blame] | 36 | HB_PAINT_FUNC_IMPLEMENT (push_clip_rectangle) \ |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 37 | HB_PAINT_FUNC_IMPLEMENT (pop_clip) \ |
Matthias Clasen | 0d89006 | 2022-12-17 00:07:30 -0500 | [diff] [blame] | 38 | HB_PAINT_FUNC_IMPLEMENT (color) \ |
Matthias Clasen | 82e23f3 | 2022-12-17 00:33:59 -0500 | [diff] [blame] | 39 | HB_PAINT_FUNC_IMPLEMENT (image) \ |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 40 | 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 Clasen | 627c857 | 2022-12-14 22:36:54 -0500 | [diff] [blame] | 44 | HB_PAINT_FUNC_IMPLEMENT (pop_group) \ |
Behdad Esfahbod | bd73314 | 2023-01-18 20:37:21 -0700 | [diff] [blame] | 45 | HB_PAINT_FUNC_IMPLEMENT (custom_palette_color) \ |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 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, |
Matthias Clasen | 64f1b55 | 2022-12-14 23:58:25 -0500 | [diff] [blame] | 71 | float xx, float yx, |
| 72 | float xy, float yy, |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 73 | float dx, float dy) |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 74 | { func.push_transform (this, paint_data, |
Matthias Clasen | 64f1b55 | 2022-12-14 23:58:25 -0500 | [diff] [blame] | 75 | xx, yx, xy, yy, dx, dy, |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 76 | !user_data ? nullptr : user_data->push_transform); } |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 77 | void pop_transform (void *paint_data) |
| 78 | { func.pop_transform (this, paint_data, |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 79 | !user_data ? nullptr : user_data->pop_transform); } |
Matthias Clasen | c935083 | 2022-12-14 22:32:40 -0500 | [diff] [blame] | 80 | void push_clip_glyph (void *paint_data, |
Matthias Clasen | 97224f3 | 2022-12-21 02:11:36 -0500 | [diff] [blame] | 81 | hb_codepoint_t glyph, |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 82 | hb_font_t *font) |
Matthias Clasen | c935083 | 2022-12-14 22:32:40 -0500 | [diff] [blame] | 83 | { func.push_clip_glyph (this, paint_data, |
| 84 | glyph, |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 85 | font, |
Matthias Clasen | c935083 | 2022-12-14 22:32:40 -0500 | [diff] [blame] | 86 | !user_data ? nullptr : user_data->push_clip_glyph); } |
Matthias Clasen | 485ba9b | 2022-12-17 12:25:04 -0500 | [diff] [blame] | 87 | void push_clip_rectangle (void *paint_data, |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 88 | float xmin, float ymin, float xmax, float ymax) |
Matthias Clasen | 485ba9b | 2022-12-17 12:25:04 -0500 | [diff] [blame] | 89 | { func.push_clip_rectangle (this, paint_data, |
| 90 | xmin, ymin, xmax, ymax, |
| 91 | !user_data ? nullptr : user_data->push_clip_rectangle); } |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 92 | void pop_clip (void *paint_data) |
| 93 | { func.pop_clip (this, paint_data, |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 94 | !user_data ? nullptr : user_data->pop_clip); } |
Matthias Clasen | 0d89006 | 2022-12-17 00:07:30 -0500 | [diff] [blame] | 95 | void color (void *paint_data, |
Matthias Clasen | c221933 | 2022-12-21 18:39:27 -0500 | [diff] [blame] | 96 | hb_bool_t is_foreground, |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 97 | hb_color_t color) |
Matthias Clasen | 0d89006 | 2022-12-17 00:07:30 -0500 | [diff] [blame] | 98 | { func.color (this, paint_data, |
Matthias Clasen | c221933 | 2022-12-21 18:39:27 -0500 | [diff] [blame] | 99 | is_foreground, color, |
Matthias Clasen | 0d89006 | 2022-12-17 00:07:30 -0500 | [diff] [blame] | 100 | !user_data ? nullptr : user_data->color); } |
Behdad Esfahbod | f70c5d6 | 2022-12-24 10:56:06 -0700 | [diff] [blame] | 101 | bool image (void *paint_data, |
Matthias Clasen | 37f3f0f | 2022-12-17 11:49:18 -0500 | [diff] [blame] | 102 | hb_blob_t *image, |
Behdad Esfahbod | c5f9038 | 2022-12-22 12:16:55 -0700 | [diff] [blame] | 103 | unsigned width, unsigned height, |
Matthias Clasen | 0a2f367 | 2022-12-17 13:51:23 -0500 | [diff] [blame] | 104 | hb_tag_t format, |
Behdad Esfahbod | 237955d | 2022-12-22 08:12:47 -0700 | [diff] [blame] | 105 | float slant, |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 106 | hb_glyph_extents_t *extents) |
Behdad Esfahbod | f70c5d6 | 2022-12-24 10:56:06 -0700 | [diff] [blame] | 107 | { return func.image (this, paint_data, |
| 108 | image, width, height, format, slant, extents, |
| 109 | !user_data ? nullptr : user_data->image); } |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 110 | void linear_gradient (void *paint_data, |
| 111 | hb_color_line_t *color_line, |
Matthias Clasen | c935083 | 2022-12-14 22:32:40 -0500 | [diff] [blame] | 112 | float x0, float y0, |
| 113 | float x1, float y1, |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 114 | float x2, float y2) |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 115 | { 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 Clasen | c935083 | 2022-12-14 22:32:40 -0500 | [diff] [blame] | 120 | float x0, float y0, float r0, |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 121 | float x1, float y1, float r1) |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 122 | { 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 Clasen | c935083 | 2022-12-14 22:32:40 -0500 | [diff] [blame] | 127 | float x0, float y0, |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 128 | float start_angle, |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 129 | float end_angle) |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 130 | { func.sweep_gradient (this, paint_data, |
| 131 | color_line, x0, y0, start_angle, end_angle, |
| 132 | !user_data ? nullptr : user_data->sweep_gradient); } |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 133 | void push_group (void *paint_data) |
| 134 | { func.push_group (this, paint_data, |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 135 | !user_data ? nullptr : user_data->push_group); } |
Matthias Clasen | 627c857 | 2022-12-14 22:36:54 -0500 | [diff] [blame] | 136 | void pop_group (void *paint_data, |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 137 | hb_paint_composite_mode_t mode) |
Matthias Clasen | 627c857 | 2022-12-14 22:36:54 -0500 | [diff] [blame] | 138 | { func.pop_group (this, paint_data, |
| 139 | mode, |
| 140 | !user_data ? nullptr : user_data->pop_group); } |
Behdad Esfahbod | 638e0ed | 2023-01-20 13:01:22 -0700 | [diff] [blame] | 141 | bool custom_palette_color (void *paint_data, |
| 142 | unsigned int color_index, |
| 143 | hb_color_t *color) |
Behdad Esfahbod | bd73314 | 2023-01-18 20:37:21 -0700 | [diff] [blame] | 144 | { return func.custom_palette_color (this, paint_data, |
| 145 | color_index, |
Behdad Esfahbod | 638e0ed | 2023-01-20 13:01:22 -0700 | [diff] [blame] | 146 | color, |
Behdad Esfahbod | bd73314 | 2023-01-18 20:37:21 -0700 | [diff] [blame] | 147 | !user_data ? nullptr : user_data->custom_palette_color); } |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 148 | |
Behdad Esfahbod | df91677 | 2022-12-31 11:12:02 -0700 | [diff] [blame] | 149 | |
| 150 | /* Internal specializations. */ |
| 151 | |
Matthias Clasen | 686e627 | 2022-12-17 12:44:16 -0500 | [diff] [blame] | 152 | void push_root_transform (void *paint_data, |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 153 | const hb_font_t *font) |
Matthias Clasen | 686e627 | 2022-12-17 12:44:16 -0500 | [diff] [blame] | 154 | { |
Behdad Esfahbod | 4b0285b | 2022-12-17 11:18:42 -0700 | [diff] [blame] | 155 | float upem = font->face->get_upem (); |
Behdad Esfahbod | 99da0e6 | 2022-12-31 11:04:40 -0700 | [diff] [blame] | 156 | int xscale = font->x_scale, yscale = font->y_scale; |
Behdad Esfahbod | 4b0285b | 2022-12-17 11:18:42 -0700 | [diff] [blame] | 157 | float slant = font->slant_xy; |
| 158 | |
Behdad Esfahbod | df91677 | 2022-12-31 11:12:02 -0700 | [diff] [blame] | 159 | push_transform (paint_data, |
| 160 | xscale/upem, 0, slant * yscale/upem, yscale/upem, 0, 0); |
Behdad Esfahbod | 4b0285b | 2022-12-17 11:18:42 -0700 | [diff] [blame] | 161 | } |
Matthias Clasen | 97224f3 | 2022-12-21 02:11:36 -0500 | [diff] [blame] | 162 | |
| 163 | void push_inverse_root_transform (void *paint_data, |
Matthias Clasen | 6387004 | 2022-12-21 16:43:19 -0500 | [diff] [blame] | 164 | hb_font_t *font) |
Matthias Clasen | 97224f3 | 2022-12-21 02:11:36 -0500 | [diff] [blame] | 165 | { |
Matthias Clasen | 97224f3 | 2022-12-21 02:11:36 -0500 | [diff] [blame] | 166 | float upem = font->face->get_upem (); |
Behdad Esfahbod | 99da0e6 | 2022-12-31 11:04:40 -0700 | [diff] [blame] | 167 | int xscale = font->x_scale ? font->x_scale : upem; |
| 168 | int yscale = font->y_scale ? font->y_scale : upem; |
Matthias Clasen | 97224f3 | 2022-12-21 02:11:36 -0500 | [diff] [blame] | 169 | float slant = font->slant_xy; |
| 170 | |
Behdad Esfahbod | df91677 | 2022-12-31 11:12:02 -0700 | [diff] [blame] | 171 | push_transform (paint_data, |
| 172 | upem/xscale, 0, -slant * upem/xscale, upem/yscale, 0, 0); |
Matthias Clasen | 97224f3 | 2022-12-21 02:11:36 -0500 | [diff] [blame] | 173 | } |
Behdad Esfahbod | 7363eb3 | 2022-12-31 11:24:42 -0700 | [diff] [blame] | 174 | |
Behdad Esfahbod | 3ff91c4 | 2022-12-31 14:49:41 -0700 | [diff] [blame] | 175 | HB_NODISCARD |
| 176 | bool push_translate (void *paint_data, |
Behdad Esfahbod | 7363eb3 | 2022-12-31 11:24:42 -0700 | [diff] [blame] | 177 | float dx, float dy) |
| 178 | { |
Behdad Esfahbod | 3ff91c4 | 2022-12-31 14:49:41 -0700 | [diff] [blame] | 179 | 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 Esfahbod | 7363eb3 | 2022-12-31 11:24:42 -0700 | [diff] [blame] | 185 | } |
Behdad Esfahbod | ce78351 | 2022-12-31 11:32:15 -0700 | [diff] [blame] | 186 | |
Behdad Esfahbod | 3ff91c4 | 2022-12-31 14:49:41 -0700 | [diff] [blame] | 187 | HB_NODISCARD |
| 188 | bool push_scale (void *paint_data, |
Behdad Esfahbod | ce78351 | 2022-12-31 11:32:15 -0700 | [diff] [blame] | 189 | float sx, float sy) |
| 190 | { |
Behdad Esfahbod | 3ff91c4 | 2022-12-31 14:49:41 -0700 | [diff] [blame] | 191 | 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 Esfahbod | ce78351 | 2022-12-31 11:32:15 -0700 | [diff] [blame] | 197 | } |
Behdad Esfahbod | 46adf31 | 2022-12-31 11:35:39 -0700 | [diff] [blame] | 198 | |
Behdad Esfahbod | 3ff91c4 | 2022-12-31 14:49:41 -0700 | [diff] [blame] | 199 | HB_NODISCARD |
| 200 | bool push_rotate (void *paint_data, |
Behdad Esfahbod | 46adf31 | 2022-12-31 11:35:39 -0700 | [diff] [blame] | 201 | float a) |
| 202 | { |
Behdad Esfahbod | 3ff91c4 | 2022-12-31 14:49:41 -0700 | [diff] [blame] | 203 | if (!a) |
| 204 | return false; |
| 205 | |
Behdad Esfahbod | 75e6498 | 2023-03-14 12:41:46 -0600 | [diff] [blame] | 206 | float cc = cosf (a * HB_PI); |
| 207 | float ss = sinf (a * HB_PI); |
Behdad Esfahbod | 3ff91c4 | 2022-12-31 14:49:41 -0700 | [diff] [blame] | 208 | push_transform (paint_data, cc, ss, -ss, cc, 0.f, 0.f); |
| 209 | return true; |
Behdad Esfahbod | 46adf31 | 2022-12-31 11:35:39 -0700 | [diff] [blame] | 210 | } |
Behdad Esfahbod | 6b47fcb | 2022-12-31 11:40:12 -0700 | [diff] [blame] | 211 | |
Behdad Esfahbod | 3ff91c4 | 2022-12-31 14:49:41 -0700 | [diff] [blame] | 212 | HB_NODISCARD |
| 213 | bool push_skew (void *paint_data, |
Behdad Esfahbod | 6b47fcb | 2022-12-31 11:40:12 -0700 | [diff] [blame] | 214 | float sx, float sy) |
| 215 | { |
Behdad Esfahbod | 3ff91c4 | 2022-12-31 14:49:41 -0700 | [diff] [blame] | 216 | if (!sx && !sy) |
| 217 | return false; |
| 218 | |
Behdad Esfahbod | 75e6498 | 2023-03-14 12:41:46 -0600 | [diff] [blame] | 219 | float x = tanf (-sx * HB_PI); |
| 220 | float y = tanf (+sy * HB_PI); |
Behdad Esfahbod | 3ff91c4 | 2022-12-31 14:49:41 -0700 | [diff] [blame] | 221 | push_transform (paint_data, 1.f, y, x, 1.f, 0.f, 0.f); |
| 222 | return true; |
Behdad Esfahbod | 6b47fcb | 2022-12-31 11:40:12 -0700 | [diff] [blame] | 223 | } |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 224 | }; |
| 225 | DECLARE_NULL_INSTANCE (hb_paint_funcs_t); |
| 226 | |
Matthias Clasen | 71bd5a0 | 2022-12-21 16:18:46 -0500 | [diff] [blame] | 227 | |
Matthias Clasen | 83d0a49 | 2022-12-13 21:14:25 -0500 | [diff] [blame] | 228 | #endif /* HB_PAINT_HH */ |