jdk
728 строк · 21.6 Кб
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_PAINT28
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
46static void47hb_paint_push_transform_nil (hb_paint_funcs_t *funcs, void *paint_data,48float xx, float yx,49float xy, float yy,50float dx, float dy,51void *user_data) {}52
53static void54hb_paint_pop_transform_nil (hb_paint_funcs_t *funcs, void *paint_data,55void *user_data) {}56
57static hb_bool_t58hb_paint_color_glyph_nil (hb_paint_funcs_t *funcs, void *paint_data,59hb_codepoint_t glyph,60hb_font_t *font,61void *user_data) { return false; }62
63static void64hb_paint_push_clip_glyph_nil (hb_paint_funcs_t *funcs, void *paint_data,65hb_codepoint_t glyph,66hb_font_t *font,67void *user_data) {}68
69static void70hb_paint_push_clip_rectangle_nil (hb_paint_funcs_t *funcs, void *paint_data,71float xmin, float ymin, float xmax, float ymax,72void *user_data) {}73
74static void75hb_paint_pop_clip_nil (hb_paint_funcs_t *funcs, void *paint_data,76void *user_data) {}77
78static void79hb_paint_color_nil (hb_paint_funcs_t *funcs, void *paint_data,80hb_bool_t is_foreground,81hb_color_t color,82void *user_data) {}83
84static hb_bool_t85hb_paint_image_nil (hb_paint_funcs_t *funcs, void *paint_data,86hb_blob_t *image,87unsigned int width,88unsigned int height,89hb_tag_t format,90float slant_xy,91hb_glyph_extents_t *extents,92void *user_data) { return false; }93
94static void95hb_paint_linear_gradient_nil (hb_paint_funcs_t *funcs, void *paint_data,96hb_color_line_t *color_line,97float x0, float y0,98float x1, float y1,99float x2, float y2,100void *user_data) {}101
102static void103hb_paint_radial_gradient_nil (hb_paint_funcs_t *funcs, void *paint_data,104hb_color_line_t *color_line,105float x0, float y0, float r0,106float x1, float y1, float r1,107void *user_data) {}108
109static void110hb_paint_sweep_gradient_nil (hb_paint_funcs_t *funcs, void *paint_data,111hb_color_line_t *color_line,112float x0, float y0,113float start_angle,114float end_angle,115void *user_data) {}116
117static void118hb_paint_push_group_nil (hb_paint_funcs_t *funcs, void *paint_data,119void *user_data) {}120
121static void122hb_paint_pop_group_nil (hb_paint_funcs_t *funcs, void *paint_data,123hb_paint_composite_mode_t mode,124void *user_data) {}125
126static hb_bool_t127hb_paint_custom_palette_color_nil (hb_paint_funcs_t *funcs, void *paint_data,128unsigned int color_index,129hb_color_t *color,130void *user_data) { return false; }131
132static bool133_hb_paint_funcs_set_preamble (hb_paint_funcs_t *funcs,134bool func_is_null,135void **user_data,136hb_destroy_func_t *destroy)137{
138if (hb_object_is_immutable (funcs))139{140if (*destroy)141(*destroy) (*user_data);142return false;143}144
145if (func_is_null)146{147if (*destroy)148(*destroy) (*user_data);149*destroy = nullptr;150*user_data = nullptr;151}152
153return true;154}
155
156static bool157_hb_paint_funcs_set_middle (hb_paint_funcs_t *funcs,158void *user_data,159hb_destroy_func_t destroy)160{
161if (user_data && !funcs->user_data)162{163funcs->user_data = (decltype (funcs->user_data)) hb_calloc (1, sizeof (*funcs->user_data));164if (unlikely (!funcs->user_data))165goto fail;166}167if (destroy && !funcs->destroy)168{169funcs->destroy = (decltype (funcs->destroy)) hb_calloc (1, sizeof (*funcs->destroy));170if (unlikely (!funcs->destroy))171goto fail;172}173
174return true;175
176fail:177if (destroy)178(destroy) (user_data);179return false;180}
181
182#define HB_PAINT_FUNC_IMPLEMENT(name) \183\184void \185hb_paint_funcs_set_##name##_func (hb_paint_funcs_t *funcs, \186hb_paint_##name##_func_t func, \187void *user_data, \188hb_destroy_func_t destroy) \189{ \190if (!_hb_paint_funcs_set_preamble (funcs, !func, &user_data, &destroy)) \191return; \192\193if (funcs->destroy && funcs->destroy->name) \194funcs->destroy->name (!funcs->user_data ? nullptr : funcs->user_data->name);\195\196if (!_hb_paint_funcs_set_middle (funcs, user_data, destroy)) \197return; \198\199if (func) \200funcs->func.name = func; \201else \202funcs->func.name = hb_paint_##name##_nil; \203\204if (funcs->user_data) \205funcs->user_data->name = user_data; \206if (funcs->destroy) \207funcs->destroy->name = destroy; \208}
209
210HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
211#undef HB_PAINT_FUNC_IMPLEMENT212
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*/
227hb_paint_funcs_t *228hb_paint_funcs_create ()229{
230hb_paint_funcs_t *funcs;231if (unlikely (!(funcs = hb_object_create<hb_paint_funcs_t> ())))232return const_cast<hb_paint_funcs_t *> (&Null (hb_paint_funcs_t));233
234funcs->func = Null (hb_paint_funcs_t).func;235
236return funcs;237}
238
239DEFINE_NULL_INSTANCE (hb_paint_funcs_t) =240{
241HB_OBJECT_HEADER_STATIC,242
243{244#define HB_PAINT_FUNC_IMPLEMENT(name) hb_paint_##name##_nil,245HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
246#undef HB_PAINT_FUNC_IMPLEMENT247}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**/
259hb_paint_funcs_t *260hb_paint_funcs_get_empty ()261{
262return 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*/
278hb_paint_funcs_t *279hb_paint_funcs_reference (hb_paint_funcs_t *funcs)280{
281return 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*/
295void
296hb_paint_funcs_destroy (hb_paint_funcs_t *funcs)297{
298if (!hb_object_destroy (funcs)) return;299
300if (funcs->destroy)301{302#define HB_PAINT_FUNC_IMPLEMENT(name) \303if (funcs->destroy->name) funcs->destroy->name (!funcs->user_data ? nullptr : funcs->user_data->name);304HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
305#undef HB_PAINT_FUNC_IMPLEMENT306}307
308hb_free (funcs->destroy);309hb_free (funcs->user_data);310hb_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**/
327hb_bool_t
328hb_paint_funcs_set_user_data (hb_paint_funcs_t *funcs,329hb_user_data_key_t *key,330void * data,331hb_destroy_func_t destroy,332hb_bool_t replace)333{
334return 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**/
349void *350hb_paint_funcs_get_user_data (const hb_paint_funcs_t *funcs,351hb_user_data_key_t *key)352{
353return 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*/
367void
368hb_paint_funcs_make_immutable (hb_paint_funcs_t *funcs)369{
370if (hb_object_is_immutable (funcs))371return;372
373hb_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*/
386hb_bool_t
387hb_paint_funcs_is_immutable (hb_paint_funcs_t *funcs)388{
389return 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*/
411unsigned int412hb_color_line_get_color_stops (hb_color_line_t *color_line,413unsigned int start,414unsigned int *count,415hb_color_stop_t *color_stops)416{
417return color_line->get_color_stops (color_line,418color_line->data,419start, count,420color_stops,421color_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*/
434hb_paint_extend_t
435hb_color_line_get_extend (hb_color_line_t *color_line)436{
437return color_line->get_extend (color_line,438color_line->data,439color_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*/
458void
459hb_paint_push_transform (hb_paint_funcs_t *funcs, void *paint_data,460float xx, float yx,461float xy, float yy,462float dx, float dy)463{
464funcs->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*/
476void
477hb_paint_pop_transform (hb_paint_funcs_t *funcs, void *paint_data)478{
479funcs->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*/
493hb_bool_t
494hb_paint_color_glyph (hb_paint_funcs_t *funcs, void *paint_data,495hb_codepoint_t glyph,496hb_font_t *font)497{
498return 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*/
512void
513hb_paint_push_clip_glyph (hb_paint_funcs_t *funcs, void *paint_data,514hb_codepoint_t glyph,515hb_font_t *font)516{
517funcs->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*/
533void
534hb_paint_push_clip_rectangle (hb_paint_funcs_t *funcs, void *paint_data,535float xmin, float ymin, float xmax, float ymax)536{
537funcs->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*/
549void
550hb_paint_pop_clip (hb_paint_funcs_t *funcs, void *paint_data)551{
552funcs->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*/
566void
567hb_paint_color (hb_paint_funcs_t *funcs, void *paint_data,568hb_bool_t is_foreground,569hb_color_t color)570{
571funcs->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*/
589void
590hb_paint_image (hb_paint_funcs_t *funcs, void *paint_data,591hb_blob_t *image,592unsigned int width,593unsigned int height,594hb_tag_t format,595float slant,596hb_glyph_extents_t *extents)597{
598funcs->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*/
617void
618hb_paint_linear_gradient (hb_paint_funcs_t *funcs, void *paint_data,619hb_color_line_t *color_line,620float x0, float y0,621float x1, float y1,622float x2, float y2)623{
624funcs->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*/
643void
644hb_paint_radial_gradient (hb_paint_funcs_t *funcs, void *paint_data,645hb_color_line_t *color_line,646float x0, float y0, float r0,647float x1, float y1, float r1)648{
649funcs->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*/
666void
667hb_paint_sweep_gradient (hb_paint_funcs_t *funcs, void *paint_data,668hb_color_line_t *color_line,669float x0, float y0,670float start_angle, float end_angle)671{
672funcs->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*/
684void
685hb_paint_push_group (hb_paint_funcs_t *funcs, void *paint_data)686{
687funcs->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*/
700void
701hb_paint_pop_group (hb_paint_funcs_t *funcs, void *paint_data,702hb_paint_composite_mode_t mode)703{
704funcs->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*/
720hb_bool_t
721hb_paint_custom_palette_color (hb_paint_funcs_t *funcs, void *paint_data,722unsigned int color_index,723hb_color_t *color)724{
725return funcs->custom_palette_color (paint_data, color_index, color);726}
727
728#endif729