/work/workdir/UnpackedTarball/harfbuzz/src/hb-paint-extents.cc
Line | Count | Source |
1 | | /* |
2 | | * Copyright © 2022 Behdad Esfahbod |
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 | | #include "hb.hh" |
26 | | |
27 | | #ifndef HB_NO_PAINT |
28 | | |
29 | | #include "hb-paint-extents.hh" |
30 | | |
31 | | #include "hb-draw.hh" |
32 | | |
33 | | #include "hb-machinery.hh" |
34 | | |
35 | | |
36 | | /* |
37 | | * This file implements bounds-extraction computation of COLRv1 fonts as described in: |
38 | | * |
39 | | * https://learn.microsoft.com/en-us/typography/opentype/spec/colr#glyph-metrics-and-boundedness |
40 | | */ |
41 | | |
42 | | static void |
43 | | hb_paint_extents_push_transform (hb_paint_funcs_t *funcs HB_UNUSED, |
44 | | void *paint_data, |
45 | | float xx, float yx, |
46 | | float xy, float yy, |
47 | | float dx, float dy, |
48 | | void *user_data HB_UNUSED) |
49 | 0 | { |
50 | 0 | hb_paint_extents_context_t *c = (hb_paint_extents_context_t *) paint_data; |
51 | |
|
52 | 0 | c->push_transform (hb_transform_t<> {xx, yx, xy, yy, dx, dy}); |
53 | 0 | } |
54 | | |
55 | | static void |
56 | | hb_paint_extents_pop_transform (hb_paint_funcs_t *funcs HB_UNUSED, |
57 | | void *paint_data, |
58 | | void *user_data HB_UNUSED) |
59 | 0 | { |
60 | 0 | hb_paint_extents_context_t *c = (hb_paint_extents_context_t *) paint_data; |
61 | |
|
62 | 0 | c->pop_transform (); |
63 | 0 | } |
64 | | |
65 | | /* Like the hb_draw_extents_get_funcs() sink, but tied to the paint |
66 | | * session's live work budget. */ |
67 | | struct hb_paint_extents_sink_t |
68 | | { |
69 | | hb_extents_t<> extents; |
70 | | int64_t *budget_remaining; |
71 | | |
72 | | bool consume_segment () |
73 | 0 | { |
74 | 0 | return hb_budget_spend (*budget_remaining, HB_BUDGET_1); |
75 | 0 | } |
76 | | }; |
77 | | |
78 | | static void |
79 | | hb_paint_extents_count_move_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *data, |
80 | | hb_draw_state_t *st HB_UNUSED, |
81 | | float to_x, float to_y, |
82 | | void *user_data HB_UNUSED) |
83 | 0 | { |
84 | 0 | hb_paint_extents_sink_t *sink = (hb_paint_extents_sink_t *) data; |
85 | 0 | if (unlikely (!sink->consume_segment ())) return; |
86 | 0 | sink->extents.add_point (to_x, to_y); |
87 | 0 | } |
88 | | |
89 | | static void |
90 | | hb_paint_extents_count_line_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *data, |
91 | | hb_draw_state_t *st HB_UNUSED, |
92 | | float to_x, float to_y, |
93 | | void *user_data HB_UNUSED) |
94 | 0 | { |
95 | 0 | hb_paint_extents_sink_t *sink = (hb_paint_extents_sink_t *) data; |
96 | 0 | if (unlikely (!sink->consume_segment ())) return; |
97 | 0 | sink->extents.add_point (to_x, to_y); |
98 | 0 | } |
99 | | |
100 | | static void |
101 | | hb_paint_extents_count_quadratic_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *data, |
102 | | hb_draw_state_t *st HB_UNUSED, |
103 | | float control_x, float control_y, |
104 | | float to_x, float to_y, |
105 | | void *user_data HB_UNUSED) |
106 | 0 | { |
107 | 0 | hb_paint_extents_sink_t *sink = (hb_paint_extents_sink_t *) data; |
108 | 0 | if (unlikely (!sink->consume_segment ())) return; |
109 | 0 | sink->extents.add_point (control_x, control_y); |
110 | 0 | sink->extents.add_point (to_x, to_y); |
111 | 0 | } |
112 | | |
113 | | static void |
114 | | hb_paint_extents_count_cubic_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *data, |
115 | | hb_draw_state_t *st HB_UNUSED, |
116 | | float control1_x, float control1_y, |
117 | | float control2_x, float control2_y, |
118 | | float to_x, float to_y, |
119 | | void *user_data HB_UNUSED) |
120 | 0 | { |
121 | 0 | hb_paint_extents_sink_t *sink = (hb_paint_extents_sink_t *) data; |
122 | 0 | if (unlikely (!sink->consume_segment ())) return; |
123 | 0 | sink->extents.add_point (control1_x, control1_y); |
124 | 0 | sink->extents.add_point (control2_x, control2_y); |
125 | 0 | sink->extents.add_point (to_x, to_y); |
126 | 0 | } |
127 | | |
128 | | static int64_t * |
129 | | hb_paint_extents_get_budget_remaining (hb_draw_funcs_t *, void *draw_data, void *) |
130 | 0 | { |
131 | 0 | return ((hb_paint_extents_sink_t *) draw_data)->budget_remaining; |
132 | 0 | } |
133 | | |
134 | | static inline void free_static_paint_extents_draw_funcs (); |
135 | | |
136 | | static struct hb_paint_extents_draw_funcs_lazy_loader_t : hb_draw_funcs_lazy_loader_t<hb_paint_extents_draw_funcs_lazy_loader_t> |
137 | | { |
138 | | static hb_draw_funcs_t *create () |
139 | 0 | { |
140 | 0 | hb_draw_funcs_t *funcs = hb_draw_funcs_create (); |
141 | |
|
142 | 0 | hb_draw_funcs_set_move_to_func (funcs, hb_paint_extents_count_move_to, nullptr, nullptr); |
143 | 0 | hb_draw_funcs_set_line_to_func (funcs, hb_paint_extents_count_line_to, nullptr, nullptr); |
144 | 0 | hb_draw_funcs_set_quadratic_to_func (funcs, hb_paint_extents_count_quadratic_to, nullptr, nullptr); |
145 | 0 | hb_draw_funcs_set_cubic_to_func (funcs, hb_paint_extents_count_cubic_to, nullptr, nullptr); |
146 | 0 | hb_draw_funcs_set_get_budget_remaining_func (funcs, hb_paint_extents_get_budget_remaining, nullptr, nullptr); |
147 | |
|
148 | 0 | hb_draw_funcs_make_immutable (funcs); |
149 | |
|
150 | 0 | hb_atexit (free_static_paint_extents_draw_funcs); |
151 | |
|
152 | 0 | return funcs; |
153 | 0 | } |
154 | | } static_paint_extents_draw_funcs; |
155 | | |
156 | | static inline |
157 | | void free_static_paint_extents_draw_funcs () |
158 | 0 | { |
159 | 0 | static_paint_extents_draw_funcs.free_instance (); |
160 | 0 | } |
161 | | |
162 | | static void |
163 | | hb_paint_extents_push_clip_glyph (hb_paint_funcs_t *funcs HB_UNUSED, |
164 | | void *paint_data, |
165 | | hb_codepoint_t glyph, |
166 | | hb_font_t *font, |
167 | | void *user_data HB_UNUSED) |
168 | 0 | { |
169 | 0 | hb_paint_extents_context_t *c = (hb_paint_extents_context_t *) paint_data; |
170 | |
|
171 | 0 | hb_paint_extents_sink_t sink {{}, &c->budget_remaining}; |
172 | | /* Skip the outline extraction when the session work budget is |
173 | | * spent; an empty clip clips everything out. */ |
174 | 0 | if (likely (c->budget_remaining >= 0)) |
175 | 0 | hb_font_draw_glyph (font, glyph, static_paint_extents_draw_funcs.get_unconst (), &sink); |
176 | 0 | c->push_clip (sink.extents); |
177 | 0 | } |
178 | | |
179 | | static void |
180 | | hb_paint_extents_push_clip_rectangle (hb_paint_funcs_t *funcs HB_UNUSED, |
181 | | void *paint_data, |
182 | | float xmin, float ymin, float xmax, float ymax, |
183 | | void *user_data) |
184 | 0 | { |
185 | 0 | hb_paint_extents_context_t *c = (hb_paint_extents_context_t *) paint_data; |
186 | |
|
187 | 0 | hb_extents_t<> extents = {xmin, ymin, xmax, ymax}; |
188 | 0 | c->push_clip (extents); |
189 | 0 | } |
190 | | |
191 | | static void |
192 | | hb_paint_extents_pop_clip (hb_paint_funcs_t *funcs HB_UNUSED, |
193 | | void *paint_data, |
194 | | void *user_data HB_UNUSED) |
195 | 0 | { |
196 | 0 | hb_paint_extents_context_t *c = (hb_paint_extents_context_t *) paint_data; |
197 | |
|
198 | 0 | c->pop_clip (); |
199 | 0 | } |
200 | | |
201 | | static void |
202 | | hb_paint_extents_push_group (hb_paint_funcs_t *funcs HB_UNUSED, |
203 | | void *paint_data, |
204 | | void *user_data HB_UNUSED) |
205 | 0 | { |
206 | 0 | hb_paint_extents_context_t *c = (hb_paint_extents_context_t *) paint_data; |
207 | |
|
208 | 0 | c->push_group (); |
209 | 0 | } |
210 | | |
211 | | static void |
212 | | hb_paint_extents_pop_group (hb_paint_funcs_t *funcs HB_UNUSED, |
213 | | void *paint_data, |
214 | | hb_paint_composite_mode_t mode, |
215 | | void *user_data HB_UNUSED) |
216 | 0 | { |
217 | 0 | hb_paint_extents_context_t *c = (hb_paint_extents_context_t *) paint_data; |
218 | |
|
219 | 0 | c->pop_group (mode); |
220 | 0 | } |
221 | | |
222 | | static hb_bool_t |
223 | | hb_paint_extents_paint_image (hb_paint_funcs_t *funcs HB_UNUSED, |
224 | | void *paint_data, |
225 | | hb_blob_t *blob HB_UNUSED, |
226 | | unsigned int width HB_UNUSED, |
227 | | unsigned int height HB_UNUSED, |
228 | | hb_tag_t format HB_UNUSED, |
229 | | float slant HB_UNUSED, |
230 | | hb_glyph_extents_t *glyph_extents, |
231 | | void *user_data HB_UNUSED) |
232 | 0 | { |
233 | 0 | hb_paint_extents_context_t *c = (hb_paint_extents_context_t *) paint_data; |
234 | |
|
235 | 0 | if (!glyph_extents) |
236 | 0 | return false; // Happens with SVG images. |
237 | | |
238 | 0 | hb_extents_t<> extents = {(float) glyph_extents->x_bearing, |
239 | 0 | (float) glyph_extents->y_bearing + glyph_extents->height, |
240 | 0 | (float) glyph_extents->x_bearing + glyph_extents->width, |
241 | 0 | (float) glyph_extents->y_bearing}; |
242 | 0 | c->push_clip (extents); |
243 | 0 | c->paint (); |
244 | 0 | c->pop_clip (); |
245 | |
|
246 | 0 | return true; |
247 | 0 | } |
248 | | |
249 | | static void |
250 | | hb_paint_extents_paint_color (hb_paint_funcs_t *funcs HB_UNUSED, |
251 | | void *paint_data, |
252 | | hb_bool_t use_foreground HB_UNUSED, |
253 | | hb_color_t color HB_UNUSED, |
254 | | void *user_data HB_UNUSED) |
255 | 0 | { |
256 | 0 | hb_paint_extents_context_t *c = (hb_paint_extents_context_t *) paint_data; |
257 | |
|
258 | 0 | c->paint (); |
259 | 0 | } |
260 | | |
261 | | static void |
262 | | hb_paint_extents_paint_linear_gradient (hb_paint_funcs_t *funcs HB_UNUSED, |
263 | | void *paint_data, |
264 | | hb_color_line_t *color_line HB_UNUSED, |
265 | | float x0 HB_UNUSED, float y0 HB_UNUSED, |
266 | | float x1 HB_UNUSED, float y1 HB_UNUSED, |
267 | | float x2 HB_UNUSED, float y2 HB_UNUSED, |
268 | | void *user_data HB_UNUSED) |
269 | 0 | { |
270 | 0 | hb_paint_extents_context_t *c = (hb_paint_extents_context_t *) paint_data; |
271 | |
|
272 | 0 | c->paint (); |
273 | 0 | } |
274 | | |
275 | | static void |
276 | | hb_paint_extents_paint_radial_gradient (hb_paint_funcs_t *funcs HB_UNUSED, |
277 | | void *paint_data, |
278 | | hb_color_line_t *color_line HB_UNUSED, |
279 | | float x0 HB_UNUSED, float y0 HB_UNUSED, float r0 HB_UNUSED, |
280 | | float x1 HB_UNUSED, float y1 HB_UNUSED, float r1 HB_UNUSED, |
281 | | void *user_data HB_UNUSED) |
282 | 0 | { |
283 | 0 | hb_paint_extents_context_t *c = (hb_paint_extents_context_t *) paint_data; |
284 | |
|
285 | 0 | c->paint (); |
286 | 0 | } |
287 | | |
288 | | static void |
289 | | hb_paint_extents_paint_sweep_gradient (hb_paint_funcs_t *funcs HB_UNUSED, |
290 | | void *paint_data, |
291 | | hb_color_line_t *color_line HB_UNUSED, |
292 | | float cx HB_UNUSED, float cy HB_UNUSED, |
293 | | float start_angle HB_UNUSED, |
294 | | float end_angle HB_UNUSED, |
295 | | void *user_data HB_UNUSED) |
296 | 0 | { |
297 | 0 | hb_paint_extents_context_t *c = (hb_paint_extents_context_t *) paint_data; |
298 | |
|
299 | 0 | c->paint (); |
300 | 0 | } |
301 | | |
302 | | static hb_bool_t |
303 | | hb_paint_extents_set_budget (hb_paint_funcs_t *, void *paint_data, |
304 | | int64_t budget, void *) |
305 | 0 | { |
306 | 0 | auto *context = (hb_paint_extents_context_t *) paint_data; |
307 | 0 | context->budget = budget; |
308 | 0 | context->budget_initialized = true; |
309 | 0 | context->recharge_budget (); |
310 | 0 | return true; |
311 | 0 | } |
312 | | |
313 | | static int64_t |
314 | | hb_paint_extents_get_budget (hb_paint_funcs_t *, void *paint_data, void *) |
315 | 0 | { |
316 | 0 | return ((hb_paint_extents_context_t *) paint_data)->budget; |
317 | 0 | } |
318 | | |
319 | | static int64_t * |
320 | | hb_paint_extents_get_budget_remaining (hb_paint_funcs_t *, void *paint_data, void *) |
321 | 0 | { |
322 | 0 | return &((hb_paint_extents_context_t *) paint_data)->budget_remaining; |
323 | 0 | } |
324 | | |
325 | | static inline void free_static_paint_extents_funcs (); |
326 | | |
327 | | static struct hb_paint_extents_funcs_lazy_loader_t : hb_paint_funcs_lazy_loader_t<hb_paint_extents_funcs_lazy_loader_t> |
328 | | { |
329 | | static hb_paint_funcs_t *create () |
330 | 0 | { |
331 | 0 | hb_paint_funcs_t *funcs = hb_paint_funcs_create (); |
332 | |
|
333 | 0 | hb_paint_funcs_set_push_transform_func (funcs, hb_paint_extents_push_transform, nullptr, nullptr); |
334 | 0 | hb_paint_funcs_set_pop_transform_func (funcs, hb_paint_extents_pop_transform, nullptr, nullptr); |
335 | 0 | hb_paint_funcs_set_push_clip_glyph_func (funcs, hb_paint_extents_push_clip_glyph, nullptr, nullptr); |
336 | 0 | hb_paint_funcs_set_push_clip_rectangle_func (funcs, hb_paint_extents_push_clip_rectangle, nullptr, nullptr); |
337 | 0 | hb_paint_funcs_set_pop_clip_func (funcs, hb_paint_extents_pop_clip, nullptr, nullptr); |
338 | 0 | hb_paint_funcs_set_push_group_func (funcs, hb_paint_extents_push_group, nullptr, nullptr); |
339 | 0 | hb_paint_funcs_set_pop_group_func (funcs, hb_paint_extents_pop_group, nullptr, nullptr); |
340 | 0 | hb_paint_funcs_set_color_func (funcs, hb_paint_extents_paint_color, nullptr, nullptr); |
341 | 0 | hb_paint_funcs_set_image_func (funcs, hb_paint_extents_paint_image, nullptr, nullptr); |
342 | 0 | hb_paint_funcs_set_linear_gradient_func (funcs, hb_paint_extents_paint_linear_gradient, nullptr, nullptr); |
343 | 0 | hb_paint_funcs_set_radial_gradient_func (funcs, hb_paint_extents_paint_radial_gradient, nullptr, nullptr); |
344 | 0 | hb_paint_funcs_set_sweep_gradient_func (funcs, hb_paint_extents_paint_sweep_gradient, nullptr, nullptr); |
345 | 0 | hb_paint_funcs_set_set_budget_func (funcs, hb_paint_extents_set_budget, nullptr, nullptr); |
346 | 0 | hb_paint_funcs_set_get_budget_func (funcs, hb_paint_extents_get_budget, nullptr, nullptr); |
347 | 0 | hb_paint_funcs_set_get_budget_remaining_func (funcs, hb_paint_extents_get_budget_remaining, nullptr, nullptr); |
348 | |
|
349 | 0 | hb_paint_funcs_make_immutable (funcs); |
350 | |
|
351 | 0 | hb_atexit (free_static_paint_extents_funcs); |
352 | |
|
353 | 0 | return funcs; |
354 | 0 | } |
355 | | } static_paint_extents_funcs; |
356 | | |
357 | | static inline |
358 | | void free_static_paint_extents_funcs () |
359 | 0 | { |
360 | 0 | static_paint_extents_funcs.free_instance (); |
361 | 0 | } |
362 | | |
363 | | hb_paint_funcs_t * |
364 | | hb_paint_extents_get_funcs () |
365 | 0 | { |
366 | 0 | return static_paint_extents_funcs.get_unconst (); |
367 | 0 | } |
368 | | |
369 | | |
370 | | #endif |