/work/workdir/UnpackedTarball/harfbuzz/src/hb-paint.hh
Line | Count | Source |
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 | | #include "hb-geometry.hh" |
32 | | |
33 | | #define HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS \ |
34 | 0 | HB_PAINT_FUNC_IMPLEMENT (push_transform) \ |
35 | 0 | HB_PAINT_FUNC_IMPLEMENT (pop_transform) \ |
36 | 0 | HB_PAINT_FUNC_IMPLEMENT (color_glyph) \ |
37 | 0 | HB_PAINT_FUNC_IMPLEMENT (push_clip_glyph) \ |
38 | 0 | HB_PAINT_FUNC_IMPLEMENT (push_clip_rectangle) \ |
39 | 0 | HB_PAINT_FUNC_IMPLEMENT (push_clip_path_start) \ |
40 | 0 | HB_PAINT_FUNC_IMPLEMENT (push_clip_path_end) \ |
41 | 0 | HB_PAINT_FUNC_IMPLEMENT (pop_clip) \ |
42 | 0 | HB_PAINT_FUNC_IMPLEMENT (color) \ |
43 | 0 | HB_PAINT_FUNC_IMPLEMENT (image) \ |
44 | 0 | HB_PAINT_FUNC_IMPLEMENT (linear_gradient) \ |
45 | 0 | HB_PAINT_FUNC_IMPLEMENT (radial_gradient) \ |
46 | 0 | HB_PAINT_FUNC_IMPLEMENT (sweep_gradient) \ |
47 | 0 | HB_PAINT_FUNC_IMPLEMENT (push_group) \ |
48 | 0 | HB_PAINT_FUNC_IMPLEMENT (push_group_for) \ |
49 | 0 | HB_PAINT_FUNC_IMPLEMENT (pop_group) \ |
50 | 0 | HB_PAINT_FUNC_IMPLEMENT (custom_palette_color) \ |
51 | | /* ^--- Add new callbacks here */ |
52 | | |
53 | | struct hb_paint_funcs_t |
54 | | { |
55 | | hb_object_header_t header; |
56 | | |
57 | | struct { |
58 | | #define HB_PAINT_FUNC_IMPLEMENT(name) hb_paint_##name##_func_t name; |
59 | | HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS |
60 | | #undef HB_PAINT_FUNC_IMPLEMENT |
61 | | } func; |
62 | | |
63 | | struct { |
64 | | #define HB_PAINT_FUNC_IMPLEMENT(name) void *name; |
65 | | HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS |
66 | | #undef HB_PAINT_FUNC_IMPLEMENT |
67 | | } *user_data; |
68 | | |
69 | | struct { |
70 | | #define HB_PAINT_FUNC_IMPLEMENT(name) hb_destroy_func_t name; |
71 | | HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS |
72 | | #undef HB_PAINT_FUNC_IMPLEMENT |
73 | | } *destroy; |
74 | | |
75 | | void push_transform (void *paint_data, |
76 | | float xx, float yx, |
77 | | float xy, float yy, |
78 | | float dx, float dy) |
79 | 0 | { |
80 | | // Handle -0.f to avoid -0.f == 0.f in the transform matrix. |
81 | 0 | if (dx == -0.f) dx = 0.f; |
82 | 0 | if (dy == -0.f) dy = 0.f; |
83 | 0 | func.push_transform (this, paint_data, |
84 | 0 | xx, yx, xy, yy, dx, dy, |
85 | 0 | !user_data ? nullptr : user_data->push_transform); } |
86 | | void pop_transform (void *paint_data) |
87 | 0 | { func.pop_transform (this, paint_data, |
88 | 0 | !user_data ? nullptr : user_data->pop_transform); } |
89 | | bool color_glyph (void *paint_data, |
90 | | hb_codepoint_t glyph, |
91 | | hb_font_t *font) |
92 | 0 | { return func.color_glyph (this, paint_data, |
93 | 0 | glyph, |
94 | 0 | font, |
95 | 0 | !user_data ? nullptr : user_data->push_clip_glyph); } |
96 | | void push_clip_glyph (void *paint_data, |
97 | | hb_codepoint_t glyph, |
98 | | hb_font_t *font) |
99 | 0 | { func.push_clip_glyph (this, paint_data, |
100 | 0 | glyph, |
101 | 0 | font, |
102 | 0 | !user_data ? nullptr : user_data->push_clip_glyph); } |
103 | | void push_clip_rectangle (void *paint_data, |
104 | | float xmin, float ymin, float xmax, float ymax) |
105 | 0 | { func.push_clip_rectangle (this, paint_data, |
106 | 0 | xmin, ymin, xmax, ymax, |
107 | 0 | !user_data ? nullptr : user_data->push_clip_rectangle); } |
108 | | hb_draw_funcs_t *push_clip_path_start (void *paint_data, |
109 | | void **draw_data) |
110 | 0 | { return func.push_clip_path_start (this, paint_data, draw_data, |
111 | 0 | !user_data ? nullptr : user_data->push_clip_path_start); } |
112 | | void push_clip_path_end (void *paint_data) |
113 | 0 | { func.push_clip_path_end (this, paint_data, |
114 | 0 | !user_data ? nullptr : user_data->push_clip_path_end); } |
115 | | void pop_clip (void *paint_data) |
116 | 0 | { func.pop_clip (this, paint_data, |
117 | 0 | !user_data ? nullptr : user_data->pop_clip); } |
118 | | void color (void *paint_data, |
119 | | hb_bool_t is_foreground, |
120 | | hb_color_t color) |
121 | 0 | { func.color (this, paint_data, |
122 | 0 | is_foreground, color, |
123 | 0 | !user_data ? nullptr : user_data->color); } |
124 | | bool image (void *paint_data, |
125 | | hb_blob_t *image, |
126 | | unsigned width, unsigned height, |
127 | | hb_tag_t format, |
128 | | float slant, |
129 | | hb_glyph_extents_t *extents) |
130 | 0 | { return func.image (this, paint_data, |
131 | 0 | image, width, height, format, slant, extents, |
132 | 0 | !user_data ? nullptr : user_data->image); } |
133 | | void linear_gradient (void *paint_data, |
134 | | hb_color_line_t *color_line, |
135 | | float x0, float y0, |
136 | | float x1, float y1, |
137 | | float x2, float y2) |
138 | 0 | { func.linear_gradient (this, paint_data, |
139 | 0 | color_line, x0, y0, x1, y1, x2, y2, |
140 | 0 | !user_data ? nullptr : user_data->linear_gradient); } |
141 | | void radial_gradient (void *paint_data, |
142 | | hb_color_line_t *color_line, |
143 | | float x0, float y0, float r0, |
144 | | float x1, float y1, float r1) |
145 | 0 | { func.radial_gradient (this, paint_data, |
146 | 0 | color_line, x0, y0, r0, x1, y1, r1, |
147 | 0 | !user_data ? nullptr : user_data->radial_gradient); } |
148 | | void sweep_gradient (void *paint_data, |
149 | | hb_color_line_t *color_line, |
150 | | float x0, float y0, |
151 | | float start_angle, |
152 | | float end_angle) |
153 | 0 | { func.sweep_gradient (this, paint_data, |
154 | 0 | color_line, x0, y0, start_angle, end_angle, |
155 | 0 | !user_data ? nullptr : user_data->sweep_gradient); } |
156 | | void push_group (void *paint_data) |
157 | 0 | { func.push_group (this, paint_data, |
158 | 0 | !user_data ? nullptr : user_data->push_group); } |
159 | | void push_group_for (void *paint_data, |
160 | | hb_paint_composite_mode_t mode) |
161 | 0 | { func.push_group_for (this, paint_data, |
162 | 0 | mode, |
163 | 0 | !user_data ? nullptr : user_data->push_group_for); } |
164 | | void pop_group (void *paint_data, |
165 | | hb_paint_composite_mode_t mode) |
166 | 0 | { func.pop_group (this, paint_data, |
167 | 0 | mode, |
168 | 0 | !user_data ? nullptr : user_data->pop_group); } |
169 | | bool custom_palette_color (void *paint_data, |
170 | | unsigned int color_index, |
171 | | hb_color_t *color) |
172 | 0 | { return func.custom_palette_color (this, paint_data, |
173 | 0 | color_index, |
174 | 0 | color, |
175 | 0 | !user_data ? nullptr : user_data->custom_palette_color); } |
176 | | |
177 | | |
178 | | /* Internal specializations. */ |
179 | | |
180 | | void push_font_transform (void *paint_data, |
181 | | const hb_font_t *font) |
182 | 0 | { |
183 | 0 | float upem = font->face->get_upem (); |
184 | 0 | int xscale = font->x_scale, yscale = font->y_scale; |
185 | |
|
186 | 0 | push_transform (paint_data, |
187 | 0 | xscale/upem, 0, |
188 | 0 | 0, yscale/upem, |
189 | 0 | 0, 0); |
190 | 0 | } |
191 | | |
192 | | void push_inverse_font_transform (void *paint_data, |
193 | | const hb_font_t *font) |
194 | 0 | { |
195 | 0 | float upem = font->face->get_upem (); |
196 | 0 | int xscale = font->x_scale ? font->x_scale : upem; |
197 | 0 | int yscale = font->y_scale ? font->y_scale : upem; |
198 | |
|
199 | 0 | push_transform (paint_data, |
200 | 0 | upem/xscale, 0, |
201 | 0 | 0, upem/yscale, |
202 | 0 | 0, 0); |
203 | 0 | } |
204 | | |
205 | | void push_transform (void *paint_data, hb_transform_t<float> t) |
206 | 0 | { |
207 | 0 | push_transform (paint_data, t.xx, t.yx, t.xy, t.yy, t.x0, t.y0); |
208 | 0 | } |
209 | | |
210 | | void push_translate (void *paint_data, |
211 | | float dx, float dy) |
212 | 0 | { |
213 | 0 | push_transform (paint_data, |
214 | 0 | hb_transform_t<float>::translation (dx, dy)); |
215 | 0 | } |
216 | | |
217 | | void push_scale (void *paint_data, |
218 | | float sx, float sy) |
219 | 0 | { |
220 | 0 | push_transform (paint_data, |
221 | 0 | hb_transform_t<float>::scaling (sx, sy)); |
222 | 0 | } |
223 | | void push_scale_around_center (void *paint_data, |
224 | | float sx, float sy, |
225 | | float cx, float cy) |
226 | 0 | { |
227 | 0 | push_transform (paint_data, |
228 | 0 | hb_transform_t<float>::scaling_around_center (sx, sy, cx, cy)); |
229 | 0 | } |
230 | | |
231 | | void push_rotate (void *paint_data, |
232 | | float a) |
233 | 0 | { |
234 | 0 | push_transform (paint_data, |
235 | 0 | hb_transform_t<float>::rotation (a * HB_PI)); |
236 | 0 | } |
237 | | |
238 | | void push_rotate_around_center (void *paint_data, |
239 | | float a, |
240 | | float cx, float cy) |
241 | 0 | { |
242 | 0 | push_transform (paint_data, |
243 | 0 | hb_transform_t<float>::rotation_around_center (a * HB_PI, cx, cy)); |
244 | 0 | } |
245 | | |
246 | | void push_skew (void *paint_data, |
247 | | float sx, float sy) |
248 | 0 | { |
249 | 0 | push_transform (paint_data, |
250 | 0 | hb_transform_t<float>::skewing (-sx * HB_PI, sy * HB_PI)); |
251 | 0 | } |
252 | | void push_skew_around_center (void *paint_data, |
253 | | float sx, float sy, |
254 | | float cx, float cy) |
255 | 0 | { |
256 | 0 | push_transform (paint_data, |
257 | 0 | hb_transform_t<float>::skewing_around_center (-sx * HB_PI, sy * HB_PI, cx, cy)); |
258 | 0 | } |
259 | | }; |
260 | | DECLARE_NULL_INSTANCE (hb_paint_funcs_t); |
261 | | |
262 | | |
263 | | /* Linearly interpolate between two hb_color_t values, component-wise, |
264 | | * in byte-channel space with rounding. */ |
265 | | static inline hb_color_t |
266 | | hb_color_lerp (hb_color_t c0, hb_color_t c1, float t) |
267 | 0 | { |
268 | 0 | auto lerp = [&] (unsigned shift) -> unsigned { |
269 | 0 | unsigned v0 = (c0 >> shift) & 0xFF; |
270 | 0 | unsigned v1 = (c1 >> shift) & 0xFF; |
271 | 0 | return (unsigned) (v0 + t * ((float) v1 - (float) v0) + 0.5f); |
272 | 0 | }; |
273 | 0 | return HB_COLOR (lerp (0), lerp (8), lerp (16), lerp (24)); |
274 | 0 | } Unexecuted instantiation: hb-font.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-ot-color.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-ot-face.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-ot-font.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: VARC.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-static.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-paint.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-ot-cff1-table.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-ot-cff2-table.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-subset-plan.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-subset-plan-var.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-subset-table-cff.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-subset-table-color.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-subset-table-other.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-subset-cff1.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-subset-cff2.cc:hb_color_lerp(unsigned int, unsigned int, float) Unexecuted instantiation: hb-subset-cff-common.cc:hb_color_lerp(unsigned int, unsigned int, float) |
275 | | |
276 | | #endif /* HB_PAINT_HH */ |