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 | HB_PAINT_FUNC_IMPLEMENT (push_transform) \ |
34 | HB_PAINT_FUNC_IMPLEMENT (pop_transform) \ |
35 | HB_PAINT_FUNC_IMPLEMENT (push_clip_glyph) \ |
36 | HB_PAINT_FUNC_IMPLEMENT (push_clip_rectangle) \ |
37 | HB_PAINT_FUNC_IMPLEMENT (pop_clip) \ |
38 | HB_PAINT_FUNC_IMPLEMENT (color) \ |
39 | HB_PAINT_FUNC_IMPLEMENT (image) \ |
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) \ |
44 | HB_PAINT_FUNC_IMPLEMENT (pop_group) \ |
45 | 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 ; |
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 | { func.push_transform (this, paint_data, |
75 | xx, yx, xy, yy, dx, dy, |
76 | !user_data ? nullptr : user_data->push_transform); } |
77 | void pop_transform (void *paint_data) |
78 | { func.pop_transform (this, paint_data, |
79 | !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 | { func.push_clip_glyph (this, paint_data, |
84 | glyph, |
85 | font, |
86 | !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 | { func.push_clip_rectangle (this, paint_data, |
90 | xmin, ymin, xmax, ymax, |
91 | !user_data ? nullptr : user_data->push_clip_rectangle); } |
92 | void pop_clip (void *paint_data) |
93 | { func.pop_clip (this, paint_data, |
94 | !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 | { func.color (this, paint_data, |
99 | is_foreground, color, |
100 | !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 | { return func.image (this, paint_data, |
108 | image, width, height, format, slant, extents, |
109 | !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 | { 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, |
120 | float x0, float y0, float r0, |
121 | float x1, float y1, float r1) |
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, |
127 | float x0, float y0, |
128 | float start_angle, |
129 | float end_angle) |
130 | { func.sweep_gradient (this, paint_data, |
131 | color_line, x0, y0, start_angle, end_angle, |
132 | !user_data ? nullptr : user_data->sweep_gradient); } |
133 | void push_group (void *paint_data) |
134 | { func.push_group (this, paint_data, |
135 | !user_data ? nullptr : user_data->push_group); } |
136 | void pop_group (void *paint_data, |
137 | hb_paint_composite_mode_t mode) |
138 | { func.pop_group (this, paint_data, |
139 | mode, |
140 | !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 | { return func.custom_palette_color (this, paint_data, |
145 | color_index, |
146 | color, |
147 | !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 | { |
155 | float upem = font->face->get_upem (); |
156 | int xscale = font->x_scale, yscale = font->y_scale; |
157 | float slant = font->slant_xy; |
158 | |
159 | push_transform (paint_data, |
160 | xscale/upem, 0, slant * yscale/upem, yscale/upem, 0, 0); |
161 | } |
162 | |
163 | void push_inverse_root_transform (void *paint_data, |
164 | hb_font_t *font) |
165 | { |
166 | float upem = font->face->get_upem (); |
167 | int xscale = font->x_scale ? font->x_scale : upem; |
168 | int yscale = font->y_scale ? font->y_scale : upem; |
169 | float slant = font->slant_xy; |
170 | |
171 | push_transform (paint_data, |
172 | upem/xscale, 0, -slant * upem/xscale, upem/yscale, 0, 0); |
173 | } |
174 | |
175 | HB_NODISCARD |
176 | bool push_translate (void *paint_data, |
177 | float dx, float dy) |
178 | { |
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; |
185 | } |
186 | |
187 | HB_NODISCARD |
188 | bool push_scale (void *paint_data, |
189 | float sx, float sy) |
190 | { |
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; |
197 | } |
198 | |
199 | HB_NODISCARD |
200 | bool push_rotate (void *paint_data, |
201 | float a) |
202 | { |
203 | if (!a) |
204 | return false; |
205 | |
206 | float cc = cosf (a * HB_PI); |
207 | float ss = sinf (a * HB_PI); |
208 | push_transform (paint_data, cc, ss, -ss, cc, 0.f, 0.f); |
209 | return true; |
210 | } |
211 | |
212 | HB_NODISCARD |
213 | bool push_skew (void *paint_data, |
214 | float sx, float sy) |
215 | { |
216 | if (!sx && !sy) |
217 | return false; |
218 | |
219 | float x = tanf (-sx * HB_PI); |
220 | float y = tanf (+sy * HB_PI); |
221 | push_transform (paint_data, 1.f, y, x, 1.f, 0.f, 0.f); |
222 | return true; |
223 | } |
224 | }; |
225 | DECLARE_NULL_INSTANCE (hb_paint_funcs_t); |
226 | |
227 | |
228 | #endif /* HB_PAINT_HH */ |
229 | |