Imported Upstream version 8.2.2
[platform/upstream/harfbuzz.git] / src / hb-paint.cc
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 #include "hb.hh"
26
27 #ifndef HB_NO_PAINT
28
29 #include "hb-paint.hh"
30
31 /**
32  * SECTION: hb-paint
33  * @title: hb-paint
34  * @short_description: Glyph painting
35  * @include: hb.h
36  *
37  * Functions for painting glyphs.
38  *
39  * The main purpose of these functions is to paint (extract) color glyph layers
40  * from the COLRv1 table, but the API works for drawing ordinary outlines and
41  * images as well.
42  *
43  * The #hb_paint_funcs_t struct can be used with hb_font_paint_glyph().
44  **/
45
46 static void
47 hb_paint_push_transform_nil (hb_paint_funcs_t *funcs, void *paint_data,
48                              float xx, float yx,
49                              float xy, float yy,
50                              float dx, float dy,
51                              void *user_data) {}
52
53 static void
54 hb_paint_pop_transform_nil (hb_paint_funcs_t *funcs, void *paint_data,
55                             void *user_data) {}
56
57 static hb_bool_t
58 hb_paint_color_glyph_nil (hb_paint_funcs_t *funcs, void *paint_data,
59                           hb_codepoint_t glyph,
60                           hb_font_t *font,
61                           void *user_data) { return false; }
62
63 static void
64 hb_paint_push_clip_glyph_nil (hb_paint_funcs_t *funcs, void *paint_data,
65                               hb_codepoint_t glyph,
66                               hb_font_t *font,
67                               void *user_data) {}
68
69 static void
70 hb_paint_push_clip_rectangle_nil (hb_paint_funcs_t *funcs, void *paint_data,
71                                   float xmin, float ymin, float xmax, float ymax,
72                                   void *user_data) {}
73
74 static void
75 hb_paint_pop_clip_nil (hb_paint_funcs_t *funcs, void *paint_data,
76                        void *user_data) {}
77
78 static void
79 hb_paint_color_nil (hb_paint_funcs_t *funcs, void *paint_data,
80                     hb_bool_t is_foreground,
81                     hb_color_t color,
82                     void *user_data) {}
83
84 static hb_bool_t
85 hb_paint_image_nil (hb_paint_funcs_t *funcs, void *paint_data,
86                     hb_blob_t *image,
87                     unsigned int width,
88                     unsigned int height,
89                     hb_tag_t format,
90                     float slant_xy,
91                     hb_glyph_extents_t *extents,
92                     void *user_data) { return false; }
93
94 static void
95 hb_paint_linear_gradient_nil (hb_paint_funcs_t *funcs, void *paint_data,
96                               hb_color_line_t *color_line,
97                               float x0, float y0,
98                               float x1, float y1,
99                               float x2, float y2,
100                               void *user_data) {}
101
102 static void
103 hb_paint_radial_gradient_nil (hb_paint_funcs_t *funcs, void *paint_data,
104                               hb_color_line_t *color_line,
105                               float x0, float y0, float r0,
106                               float x1, float y1, float r1,
107                               void *user_data) {}
108
109 static void
110 hb_paint_sweep_gradient_nil (hb_paint_funcs_t *funcs, void *paint_data,
111                              hb_color_line_t *color_line,
112                              float x0, float y0,
113                              float start_angle,
114                              float end_angle,
115                              void *user_data) {}
116
117 static void
118 hb_paint_push_group_nil (hb_paint_funcs_t *funcs, void *paint_data,
119                          void *user_data) {}
120
121 static void
122 hb_paint_pop_group_nil (hb_paint_funcs_t *funcs, void *paint_data,
123                         hb_paint_composite_mode_t mode,
124                         void *user_data) {}
125
126 static hb_bool_t
127 hb_paint_custom_palette_color_nil (hb_paint_funcs_t *funcs, void *paint_data,
128                                    unsigned int color_index,
129                                    hb_color_t *color,
130                                    void *user_data) { return false; }
131
132 static bool
133 _hb_paint_funcs_set_preamble (hb_paint_funcs_t  *funcs,
134                              bool                func_is_null,
135                              void              **user_data,
136                              hb_destroy_func_t  *destroy)
137 {
138   if (hb_object_is_immutable (funcs))
139   {
140     if (*destroy)
141       (*destroy) (*user_data);
142     return false;
143   }
144
145   if (func_is_null)
146   {
147     if (*destroy)
148       (*destroy) (*user_data);
149     *destroy = nullptr;
150     *user_data = nullptr;
151   }
152
153   return true;
154 }
155
156 static bool
157 _hb_paint_funcs_set_middle (hb_paint_funcs_t  *funcs,
158                             void              *user_data,
159                             hb_destroy_func_t  destroy)
160 {
161   if (user_data && !funcs->user_data)
162   {
163     funcs->user_data = (decltype (funcs->user_data)) hb_calloc (1, sizeof (*funcs->user_data));
164     if (unlikely (!funcs->user_data))
165       goto fail;
166   }
167   if (destroy && !funcs->destroy)
168   {
169     funcs->destroy = (decltype (funcs->destroy)) hb_calloc (1, sizeof (*funcs->destroy));
170     if (unlikely (!funcs->destroy))
171       goto fail;
172   }
173
174   return true;
175
176 fail:
177   if (destroy)
178     (destroy) (user_data);
179   return false;
180 }
181
182 #define HB_PAINT_FUNC_IMPLEMENT(name)                                           \
183                                                                                 \
184 void                                                                            \
185 hb_paint_funcs_set_##name##_func (hb_paint_funcs_t         *funcs,              \
186                                   hb_paint_##name##_func_t  func,               \
187                                   void                     *user_data,          \
188                                   hb_destroy_func_t         destroy)            \
189 {                                                                               \
190   if (!_hb_paint_funcs_set_preamble (funcs, !func, &user_data, &destroy))       \
191       return;                                                                   \
192                                                                                 \
193   if (funcs->destroy && funcs->destroy->name)                                   \
194     funcs->destroy->name (!funcs->user_data ? nullptr : funcs->user_data->name);\
195                                                                                 \
196   if (!_hb_paint_funcs_set_middle (funcs, user_data, destroy))                  \
197       return;                                                                   \
198                                                                                 \
199   if (func)                                                                     \
200     funcs->func.name = func;                                                    \
201   else                                                                          \
202     funcs->func.name = hb_paint_##name##_nil;                                   \
203                                                                                 \
204   if (funcs->user_data)                                                         \
205     funcs->user_data->name = user_data;                                         \
206   if (funcs->destroy)                                                           \
207     funcs->destroy->name = destroy;                                             \
208 }
209
210 HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
211 #undef HB_PAINT_FUNC_IMPLEMENT
212
213 /**
214  * hb_paint_funcs_create:
215  *
216  * Creates a new #hb_paint_funcs_t structure of paint functions.
217  *
218  * The initial reference count of 1 should be released with hb_paint_funcs_destroy()
219  * when you are done using the #hb_paint_funcs_t. This function never returns
220  * `NULL`. If memory cannot be allocated, a special singleton #hb_paint_funcs_t
221  * object will be returned.
222  *
223  * Returns value: (transfer full): the paint-functions structure
224  *
225  * Since: 7.0.0
226  */
227 hb_paint_funcs_t *
228 hb_paint_funcs_create ()
229 {
230   hb_paint_funcs_t *funcs;
231   if (unlikely (!(funcs = hb_object_create<hb_paint_funcs_t> ())))
232     return const_cast<hb_paint_funcs_t *> (&Null (hb_paint_funcs_t));
233
234   funcs->func =  Null (hb_paint_funcs_t).func;
235
236   return funcs;
237 }
238
239 DEFINE_NULL_INSTANCE (hb_paint_funcs_t) =
240 {
241   HB_OBJECT_HEADER_STATIC,
242
243   {
244 #define HB_PAINT_FUNC_IMPLEMENT(name) hb_paint_##name##_nil,
245     HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
246 #undef HB_PAINT_FUNC_IMPLEMENT
247   }
248 };
249
250 /**
251  * hb_paint_funcs_get_empty:
252  *
253  * Fetches the singleton empty paint-functions structure.
254  *
255  * Return value: (transfer full): The empty paint-functions structure
256  *
257  * Since: 7.0.0
258  **/
259 hb_paint_funcs_t *
260 hb_paint_funcs_get_empty ()
261 {
262   return const_cast<hb_paint_funcs_t *> (&Null (hb_paint_funcs_t));
263 }
264
265 /**
266  * hb_paint_funcs_reference: (skip)
267  * @funcs: The paint-functions structure
268  *
269  * Increases the reference count on a paint-functions structure.
270  *
271  * This prevents @funcs from being destroyed until a matching
272  * call to hb_paint_funcs_destroy() is made.
273  *
274  * Return value: The paint-functions structure
275  *
276  * Since: 7.0.0
277  */
278 hb_paint_funcs_t *
279 hb_paint_funcs_reference (hb_paint_funcs_t *funcs)
280 {
281   return hb_object_reference (funcs);
282 }
283
284 /**
285  * hb_paint_funcs_destroy: (skip)
286  * @funcs: The paint-functions structure
287  *
288  * Decreases the reference count on a paint-functions structure.
289  *
290  * When the reference count reaches zero, the structure
291  * is destroyed, freeing all memory.
292  *
293  * Since: 7.0.0
294  */
295 void
296 hb_paint_funcs_destroy (hb_paint_funcs_t *funcs)
297 {
298   if (!hb_object_destroy (funcs)) return;
299
300   if (funcs->destroy)
301   {
302 #define HB_PAINT_FUNC_IMPLEMENT(name) \
303     if (funcs->destroy->name) funcs->destroy->name (!funcs->user_data ? nullptr : funcs->user_data->name);
304       HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
305 #undef HB_PAINT_FUNC_IMPLEMENT
306   }
307
308   hb_free (funcs->destroy);
309   hb_free (funcs->user_data);
310   hb_free (funcs);
311 }
312
313 /**
314  * hb_paint_funcs_set_user_data: (skip)
315  * @funcs: The paint-functions structure
316  * @key: The user-data key
317  * @data: A pointer to the user data
318  * @destroy: (nullable): A callback to call when @data is not needed anymore
319  * @replace: Whether to replace an existing data with the same key
320  *
321  * Attaches a user-data key/data pair to the specified paint-functions structure.
322  *
323  * Return value: `true` if success, `false` otherwise
324  *
325  * Since: 7.0.0
326  **/
327 hb_bool_t
328 hb_paint_funcs_set_user_data (hb_paint_funcs_t *funcs,
329                              hb_user_data_key_t *key,
330                              void *              data,
331                              hb_destroy_func_t   destroy,
332                              hb_bool_t           replace)
333 {
334   return hb_object_set_user_data (funcs, key, data, destroy, replace);
335 }
336
337 /**
338  * hb_paint_funcs_get_user_data: (skip)
339  * @funcs: The paint-functions structure
340  * @key: The user-data key to query
341  *
342  * Fetches the user-data associated with the specified key,
343  * attached to the specified paint-functions structure.
344  *
345  * Return value: (transfer none): A pointer to the user data
346  *
347  * Since: 7.0.0
348  **/
349 void *
350 hb_paint_funcs_get_user_data (const hb_paint_funcs_t *funcs,
351                              hb_user_data_key_t       *key)
352 {
353   return hb_object_get_user_data (funcs, key);
354 }
355
356 /**
357  * hb_paint_funcs_make_immutable:
358  * @funcs: The paint-functions structure
359  *
360  * Makes a paint-functions structure immutable.
361  *
362  * After this call, all attempts to set one of the callbacks
363  * on @funcs will fail.
364  *
365  * Since: 7.0.0
366  */
367 void
368 hb_paint_funcs_make_immutable (hb_paint_funcs_t *funcs)
369 {
370   if (hb_object_is_immutable (funcs))
371     return;
372
373   hb_object_make_immutable (funcs);
374 }
375
376 /**
377  * hb_paint_funcs_is_immutable:
378  * @funcs: The paint-functions structure
379  *
380  * Tests whether a paint-functions structure is immutable.
381  *
382  * Return value: `true` if @funcs is immutable, `false` otherwise
383  *
384  * Since: 7.0.0
385  */
386 hb_bool_t
387 hb_paint_funcs_is_immutable (hb_paint_funcs_t *funcs)
388 {
389   return hb_object_is_immutable (funcs);
390 }
391
392
393 /**
394  * hb_color_line_get_color_stops:
395  * @color_line: a #hb_color_line_t object
396  * @start: the index of the first color stop to return
397  * @count: (inout) (optional): Input = the maximum number of feature tags to return;
398  *     Output = the actual number of feature tags returned (may be zero)
399  * @color_stops: (out) (array length=count) (optional): Array of #hb_color_stop_t to populate
400  *
401  * Fetches a list of color stops from the given color line object.
402  *
403  * Note that due to variations being applied, the returned color stops
404  * may be out of order. It is the callers responsibility to ensure that
405  * color stops are sorted by their offset before they are used.
406  *
407  * Return value: the total number of color stops in @color_line
408  *
409  * Since: 7.0.0
410  */
411 unsigned int
412 hb_color_line_get_color_stops (hb_color_line_t *color_line,
413                                unsigned int start,
414                                unsigned int *count,
415                                hb_color_stop_t *color_stops)
416 {
417   return color_line->get_color_stops (color_line,
418                                       color_line->data,
419                                       start, count,
420                                       color_stops,
421                                       color_line->get_color_stops_user_data);
422 }
423
424 /**
425  * hb_color_line_get_extend:
426  * @color_line: a #hb_color_line_t object
427  *
428  * Fetches the extend mode of the color line object.
429  *
430  * Return value: the extend mode of @color_line
431  *
432  * Since: 7.0.0
433  */
434 hb_paint_extend_t
435 hb_color_line_get_extend (hb_color_line_t *color_line)
436 {
437   return color_line->get_extend (color_line,
438                                  color_line->data,
439                                  color_line->get_extend_user_data);
440 }
441
442
443 /**
444  * hb_paint_push_transform:
445  * @funcs: paint functions
446  * @paint_data: associated data passed by the caller
447  * @xx: xx component of the transform matrix
448  * @yx: yx component of the transform matrix
449  * @xy: xy component of the transform matrix
450  * @yy: yy component of the transform matrix
451  * @dx: dx component of the transform matrix
452  * @dy: dy component of the transform matrix
453  *
454  * Perform a "push-transform" paint operation.
455  *
456  * Since: 7.0.0
457  */
458 void
459 hb_paint_push_transform (hb_paint_funcs_t *funcs, void *paint_data,
460                          float xx, float yx,
461                          float xy, float yy,
462                          float dx, float dy)
463 {
464   funcs->push_transform (paint_data, xx, yx, xy, yy, dx, dy);
465 }
466
467 /**
468  * hb_paint_pop_transform:
469  * @funcs: paint functions
470  * @paint_data: associated data passed by the caller
471  *
472  * Perform a "pop-transform" paint operation.
473  *
474  * Since: 7.0.0
475  */
476 void
477 hb_paint_pop_transform (hb_paint_funcs_t *funcs, void *paint_data)
478 {
479   funcs->pop_transform (paint_data);
480 }
481
482 /**
483  * hb_paint_color_glyph:
484  * @funcs: paint functions
485  * @paint_data: associated data passed by the caller
486  * @glyph: the glyph ID
487  * @font: the font
488  *
489  * Perform a "color-glyph" paint operation.
490  *
491  * Since: 8.2.0
492  */
493 hb_bool_t
494 hb_paint_color_glyph (hb_paint_funcs_t *funcs, void *paint_data,
495                       hb_codepoint_t glyph,
496                       hb_font_t *font)
497 {
498   return funcs->color_glyph (paint_data, glyph, font);
499 }
500
501 /**
502  * hb_paint_push_clip_glyph:
503  * @funcs: paint functions
504  * @paint_data: associated data passed by the caller
505  * @glyph: the glyph ID
506  * @font: the font
507  *
508  * Perform a "push-clip-glyph" paint operation.
509  *
510  * Since: 7.0.0
511  */
512 void
513 hb_paint_push_clip_glyph (hb_paint_funcs_t *funcs, void *paint_data,
514                           hb_codepoint_t glyph,
515                           hb_font_t *font)
516 {
517   funcs->push_clip_glyph (paint_data, glyph, font);
518 }
519
520 /**
521  * hb_paint_push_clip_rectangle:
522  * @funcs: paint functions
523  * @paint_data: associated data passed by the caller
524  * @xmin: min X for the rectangle
525  * @ymin: min Y for the rectangle
526  * @xmax: max X for the rectangle
527  * @ymax: max Y for the rectangle
528  *
529  * Perform a "push-clip-rect" paint operation.
530  *
531  * Since: 7.0.0
532  */
533 void
534 hb_paint_push_clip_rectangle (hb_paint_funcs_t *funcs, void *paint_data,
535                               float xmin, float ymin, float xmax, float ymax)
536 {
537   funcs->push_clip_rectangle (paint_data, xmin, ymin, xmax, ymax);
538 }
539
540 /**
541  * hb_paint_pop_clip:
542  * @funcs: paint functions
543  * @paint_data: associated data passed by the caller
544  *
545  * Perform a "pop-clip" paint operation.
546  *
547  * Since: 7.0.0
548  */
549 void
550 hb_paint_pop_clip (hb_paint_funcs_t *funcs, void *paint_data)
551 {
552   funcs->pop_clip (paint_data);
553 }
554
555 /**
556  * hb_paint_color:
557  * @funcs: paint functions
558  * @paint_data: associated data passed by the caller
559  * @is_foreground: whether the color is the foreground
560  * @color: The color to use
561  *
562  * Perform a "color" paint operation.
563  *
564  * Since: 7.0.0
565  */
566 void
567 hb_paint_color (hb_paint_funcs_t *funcs, void *paint_data,
568                 hb_bool_t is_foreground,
569                 hb_color_t color)
570 {
571   funcs->color (paint_data, is_foreground, color);
572 }
573
574 /**
575  * hb_paint_image:
576  * @funcs: paint functions
577  * @paint_data: associated data passed by the caller
578  * @image: image data
579  * @width: width of the raster image in pixels, or 0
580  * @height: height of the raster image in pixels, or 0
581  * @format: the image format as a tag
582  * @slant: the synthetic slant ratio to be applied to the image during rendering
583  * @extents: (nullable): the extents of the glyph
584  *
585  * Perform a "image" paint operation.
586  *
587  * Since: 7.0.0
588  */
589 void
590 hb_paint_image (hb_paint_funcs_t *funcs, void *paint_data,
591                 hb_blob_t *image,
592                 unsigned int width,
593                 unsigned int height,
594                 hb_tag_t format,
595                 float slant,
596                 hb_glyph_extents_t *extents)
597 {
598   funcs->image (paint_data, image, width, height, format, slant, extents);
599 }
600
601 /**
602  * hb_paint_linear_gradient:
603  * @funcs: paint functions
604  * @paint_data: associated data passed by the caller
605  * @color_line: Color information for the gradient
606  * @x0: X coordinate of the first point
607  * @y0: Y coordinate of the first point
608  * @x1: X coordinate of the second point
609  * @y1: Y coordinate of the second point
610  * @x2: X coordinate of the third point
611  * @y2: Y coordinate of the third point
612  *
613  * Perform a "linear-gradient" paint operation.
614  *
615  * Since: 7.0.0
616  */
617 void
618 hb_paint_linear_gradient (hb_paint_funcs_t *funcs, void *paint_data,
619                           hb_color_line_t *color_line,
620                           float x0, float y0,
621                           float x1, float y1,
622                           float x2, float y2)
623 {
624   funcs->linear_gradient (paint_data, color_line, x0, y0, x1, y1, x2, y2);
625 }
626
627 /**
628  * hb_paint_radial_gradient:
629  * @funcs: paint functions
630  * @paint_data: associated data passed by the caller
631  * @color_line: Color information for the gradient
632  * @x0: X coordinate of the first circle's center
633  * @y0: Y coordinate of the first circle's center
634  * @r0: radius of the first circle
635  * @x1: X coordinate of the second circle's center
636  * @y1: Y coordinate of the second circle's center
637  * @r1: radius of the second circle
638  *
639  * Perform a "radial-gradient" paint operation.
640  *
641  * Since: 7.0.0
642  */
643 void
644 hb_paint_radial_gradient (hb_paint_funcs_t *funcs, void *paint_data,
645                           hb_color_line_t *color_line,
646                           float x0, float y0, float r0,
647                           float x1, float y1, float r1)
648 {
649   funcs->radial_gradient (paint_data, color_line, x0, y0, r0, y1, x1, r1);
650 }
651
652 /**
653  * hb_paint_sweep_gradient:
654  * @funcs: paint functions
655  * @paint_data: associated data passed by the caller
656  * @color_line: Color information for the gradient
657  * @x0: X coordinate of the circle's center
658  * @y0: Y coordinate of the circle's center
659  * @start_angle: the start angle
660  * @end_angle: the end angle
661  *
662  * Perform a "sweep-gradient" paint operation.
663  *
664  * Since: 7.0.0
665  */
666 void
667 hb_paint_sweep_gradient (hb_paint_funcs_t *funcs, void *paint_data,
668                          hb_color_line_t *color_line,
669                          float x0, float y0,
670                          float start_angle, float end_angle)
671 {
672   funcs->sweep_gradient (paint_data, color_line, x0, y0, start_angle, end_angle);
673 }
674
675 /**
676  * hb_paint_push_group:
677  * @funcs: paint functions
678  * @paint_data: associated data passed by the caller
679  *
680  * Perform a "push-group" paint operation.
681  *
682  * Since: 7.0.0
683  */
684 void
685 hb_paint_push_group (hb_paint_funcs_t *funcs, void *paint_data)
686 {
687   funcs->push_group (paint_data);
688 }
689
690 /**
691  * hb_paint_pop_group:
692  * @funcs: paint functions
693  * @paint_data: associated data passed by the caller
694  * @mode: the compositing mode to use
695  *
696  * Perform a "pop-group" paint operation.
697  *
698  * Since: 7.0.0
699  */
700 void
701 hb_paint_pop_group (hb_paint_funcs_t *funcs, void *paint_data,
702                     hb_paint_composite_mode_t mode)
703 {
704   funcs->pop_group (paint_data, mode);
705 }
706
707 /**
708  * hb_paint_custom_palette_color:
709  * @funcs: paint functions
710  * @paint_data: associated data passed by the caller
711  * @color_index: color index
712  * @color: (out): fetched color
713  *
714  * Gets the custom palette color for @color_index.
715  *
716  * Return value: `true` if found, `false` otherwise
717  *
718  * Since: 7.0.0
719  */
720 hb_bool_t
721 hb_paint_custom_palette_color (hb_paint_funcs_t *funcs, void *paint_data,
722                                unsigned int color_index,
723                                hb_color_t *color)
724 {
725   return funcs->custom_palette_color (paint_data, color_index, color);
726 }
727
728 #endif