xref: /aosp_15_r20/external/harfbuzz_ng/src/hb-ot-color.cc (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
1 /*
2  * Copyright © 2016  Google, Inc.
3  * Copyright © 2018  Ebrahim Byagowi
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Google Author(s): Sascha Brawer, Behdad Esfahbod
26  */
27 
28 #include "hb.hh"
29 
30 #ifndef HB_NO_COLOR
31 
32 #include "hb-ot.h"
33 
34 #include "OT/Color/CBDT/CBDT.hh"
35 #include "OT/Color/COLR/COLR.hh"
36 #include "OT/Color/CPAL/CPAL.hh"
37 #include "OT/Color/sbix/sbix.hh"
38 #include "OT/Color/svg/svg.hh"
39 
40 
41 /**
42  * SECTION:hb-ot-color
43  * @title: hb-ot-color
44  * @short_description: OpenType Color Fonts
45  * @include: hb-ot.h
46  *
47  * Functions for fetching color-font information from OpenType font faces.
48  *
49  * HarfBuzz supports `COLR`/`CPAL`, `sbix`, `CBDT`, and `SVG` color fonts.
50  **/
51 
52 
53 /*
54  * CPAL
55  */
56 
57 
58 /**
59  * hb_ot_color_has_palettes:
60  * @face: #hb_face_t to work upon
61  *
62  * Tests whether a face includes a `CPAL` color-palette table.
63  *
64  * Return value: `true` if data found, `false` otherwise
65  *
66  * Since: 2.1.0
67  */
68 hb_bool_t
hb_ot_color_has_palettes(hb_face_t * face)69 hb_ot_color_has_palettes (hb_face_t *face)
70 {
71   return face->table.CPAL->has_data ();
72 }
73 
74 /**
75  * hb_ot_color_palette_get_count:
76  * @face: #hb_face_t to work upon
77  *
78  * Fetches the number of color palettes in a face.
79  *
80  * Return value: the number of palettes found
81  *
82  * Since: 2.1.0
83  */
84 unsigned int
hb_ot_color_palette_get_count(hb_face_t * face)85 hb_ot_color_palette_get_count (hb_face_t *face)
86 {
87   return face->table.CPAL->get_palette_count ();
88 }
89 
90 /**
91  * hb_ot_color_palette_get_name_id:
92  * @face: #hb_face_t to work upon
93  * @palette_index: The index of the color palette
94  *
95  * Fetches the `name` table Name ID that provides display names for
96  * a `CPAL` color palette.
97  *
98  * Palette display names can be generic (e.g., "Default") or provide
99  * specific, themed names (e.g., "Spring", "Summer", "Fall", and "Winter").
100  *
101  * Return value: the Named ID found for the palette.
102  * If the requested palette has no name the result is #HB_OT_NAME_ID_INVALID.
103  *
104  * Since: 2.1.0
105  */
106 hb_ot_name_id_t
hb_ot_color_palette_get_name_id(hb_face_t * face,unsigned int palette_index)107 hb_ot_color_palette_get_name_id (hb_face_t *face,
108 				 unsigned int palette_index)
109 {
110   return face->table.CPAL->get_palette_name_id (palette_index);
111 }
112 
113 /**
114  * hb_ot_color_palette_color_get_name_id:
115  * @face: #hb_face_t to work upon
116  * @color_index: The index of the color
117  *
118  * Fetches the `name` table Name ID that provides display names for
119  * the specified color in a face's `CPAL` color palette.
120  *
121  * Display names can be generic (e.g., "Background") or specific
122  * (e.g., "Eye color").
123  *
124  * Return value: the Name ID found for the color.
125  *
126  * Since: 2.1.0
127  */
128 hb_ot_name_id_t
hb_ot_color_palette_color_get_name_id(hb_face_t * face,unsigned int color_index)129 hb_ot_color_palette_color_get_name_id (hb_face_t *face,
130 				       unsigned int color_index)
131 {
132   return face->table.CPAL->get_color_name_id (color_index);
133 }
134 
135 /**
136  * hb_ot_color_palette_get_flags:
137  * @face: #hb_face_t to work upon
138  * @palette_index: The index of the color palette
139  *
140  * Fetches the flags defined for a color palette.
141  *
142  * Return value: the #hb_ot_color_palette_flags_t of the requested color palette
143  *
144  * Since: 2.1.0
145  */
146 hb_ot_color_palette_flags_t
hb_ot_color_palette_get_flags(hb_face_t * face,unsigned int palette_index)147 hb_ot_color_palette_get_flags (hb_face_t *face,
148 			       unsigned int palette_index)
149 {
150   return face->table.CPAL->get_palette_flags (palette_index);
151 }
152 
153 /**
154  * hb_ot_color_palette_get_colors:
155  * @face: #hb_face_t to work upon
156  * @palette_index: the index of the color palette to query
157  * @start_offset: offset of the first color to retrieve
158  * @color_count: (inout) (optional): Input = the maximum number of colors to return;
159  *               Output = the actual number of colors returned (may be zero)
160  * @colors: (out) (array length=color_count) (nullable): The array of #hb_color_t records found
161  *
162  * Fetches a list of the colors in a color palette.
163  *
164  * After calling this function, @colors will be filled with the palette
165  * colors. If @colors is NULL, the function will just return the number
166  * of total colors without storing any actual colors; this can be used
167  * for allocating a buffer of suitable size before calling
168  * hb_ot_color_palette_get_colors() a second time.
169  *
170  * The RGBA values in the palette are unpremultiplied. See the
171  * OpenType spec [CPAL](https://learn.microsoft.com/en-us/typography/opentype/spec/cpal)
172  * section for details.
173  *
174  * Return value: the total number of colors in the palette
175  *
176  * Since: 2.1.0
177  */
178 unsigned int
hb_ot_color_palette_get_colors(hb_face_t * face,unsigned int palette_index,unsigned int start_offset,unsigned int * colors_count,hb_color_t * colors)179 hb_ot_color_palette_get_colors (hb_face_t     *face,
180 				unsigned int   palette_index,
181 				unsigned int   start_offset,
182 				unsigned int  *colors_count  /* IN/OUT.  May be NULL. */,
183 				hb_color_t    *colors        /* OUT.     May be NULL. */)
184 {
185   return face->table.CPAL->get_palette_colors (palette_index, start_offset, colors_count, colors);
186 }
187 
188 
189 /*
190  * COLR
191  */
192 
193 /**
194  * hb_ot_color_has_layers:
195  * @face: #hb_face_t to work upon
196  *
197  * Tests whether a face includes a `COLR` table
198  * with data according to COLRv0.
199  *
200  * Return value: `true` if data found, `false` otherwise
201  *
202  * Since: 2.1.0
203  */
204 hb_bool_t
hb_ot_color_has_layers(hb_face_t * face)205 hb_ot_color_has_layers (hb_face_t *face)
206 {
207   return face->table.COLR->has_v0_data ();
208 }
209 
210 /**
211  * hb_ot_color_has_paint:
212  * @face: #hb_face_t to work upon
213  *
214  * Tests where a face includes a `COLR` table
215  * with data according to COLRv1.
216  *
217  * Return value: `true` if data found, `false` otherwise
218  *
219  * Since: 7.0.0
220  */
221 hb_bool_t
hb_ot_color_has_paint(hb_face_t * face)222 hb_ot_color_has_paint (hb_face_t *face)
223 {
224   return face->table.COLR->has_v1_data ();
225 }
226 
227 /**
228  * hb_ot_color_glyph_has_paint:
229  * @face: #hb_face_t to work upon
230  * @glyph: The glyph index to query
231  *
232  * Tests where a face includes COLRv1 paint
233  * data for @glyph.
234  *
235  * Return value: `true` if data found, `false` otherwise
236  *
237  * Since: 7.0.0
238  */
239 hb_bool_t
hb_ot_color_glyph_has_paint(hb_face_t * face,hb_codepoint_t glyph)240 hb_ot_color_glyph_has_paint (hb_face_t      *face,
241                              hb_codepoint_t  glyph)
242 {
243   return face->table.COLR->has_paint_for_glyph (glyph);
244 }
245 
246 /**
247  * hb_ot_color_glyph_get_layers:
248  * @face: #hb_face_t to work upon
249  * @glyph: The glyph index to query
250  * @start_offset: offset of the first layer to retrieve
251  * @layer_count: (inout) (optional): Input = the maximum number of layers to return;
252  *         Output = the actual number of layers returned (may be zero)
253  * @layers: (out) (array length=layer_count) (nullable): The array of layers found
254  *
255  * Fetches a list of all color layers for the specified glyph index in the specified
256  * face. The list returned will begin at the offset provided.
257  *
258  * Return value: Total number of layers available for the glyph index queried
259  *
260  * Since: 2.1.0
261  */
262 unsigned int
hb_ot_color_glyph_get_layers(hb_face_t * face,hb_codepoint_t glyph,unsigned int start_offset,unsigned int * layer_count,hb_ot_color_layer_t * layers)263 hb_ot_color_glyph_get_layers (hb_face_t           *face,
264 			      hb_codepoint_t       glyph,
265 			      unsigned int         start_offset,
266 			      unsigned int        *layer_count, /* IN/OUT.  May be NULL. */
267 			      hb_ot_color_layer_t *layers /* OUT.     May be NULL. */)
268 {
269   return face->table.COLR->get_glyph_layers (glyph, start_offset, layer_count, layers);
270 }
271 
272 
273 /*
274  * SVG
275  */
276 
277 /**
278  * hb_ot_color_has_svg:
279  * @face: #hb_face_t to work upon.
280  *
281  * Tests whether a face includes any `SVG` glyph images.
282  *
283  * Return value: `true` if data found, `false` otherwise.
284  *
285  * Since: 2.1.0
286  */
287 hb_bool_t
hb_ot_color_has_svg(hb_face_t * face)288 hb_ot_color_has_svg (hb_face_t *face)
289 {
290   return face->table.SVG->has_data ();
291 }
292 
293 /**
294  * hb_ot_color_glyph_reference_svg:
295  * @face: #hb_face_t to work upon
296  * @glyph: a svg glyph index
297  *
298  * Fetches the SVG document for a glyph. The blob may be either plain text or gzip-encoded.
299  *
300  * If the glyph has no SVG document, the singleton empty blob is returned.
301  *
302  * Return value: (transfer full): An #hb_blob_t containing the SVG document of the glyph, if available
303  *
304  * Since: 2.1.0
305  */
306 hb_blob_t *
hb_ot_color_glyph_reference_svg(hb_face_t * face,hb_codepoint_t glyph)307 hb_ot_color_glyph_reference_svg (hb_face_t *face, hb_codepoint_t glyph)
308 {
309   return face->table.SVG->reference_blob_for_glyph (glyph);
310 }
311 
312 
313 /*
314  * PNG: CBDT or sbix
315  */
316 
317 /**
318  * hb_ot_color_has_png:
319  * @face: #hb_face_t to work upon
320  *
321  * Tests whether a face has PNG glyph images (either in `CBDT` or `sbix` tables).
322  *
323  * Return value: `true` if data found, `false` otherwise
324  *
325  * Since: 2.1.0
326  */
327 hb_bool_t
hb_ot_color_has_png(hb_face_t * face)328 hb_ot_color_has_png (hb_face_t *face)
329 {
330   return face->table.CBDT->has_data () || face->table.sbix->has_data ();
331 }
332 
333 /**
334  * hb_ot_color_glyph_reference_png:
335  * @font: #hb_font_t to work upon
336  * @glyph: a glyph index
337  *
338  * Fetches the PNG image for a glyph. This function takes a font object, not a face object,
339  * as input. To get an optimally sized PNG blob, the PPEM values must be set on the @font
340  * object. If PPEM is unset, the blob returned will be the largest PNG available.
341  *
342  * If the glyph has no PNG image, the singleton empty blob is returned.
343  *
344  * Return value: (transfer full): An #hb_blob_t containing the PNG image for the glyph, if available
345  *
346  * Since: 2.1.0
347  */
348 hb_blob_t *
hb_ot_color_glyph_reference_png(hb_font_t * font,hb_codepoint_t glyph)349 hb_ot_color_glyph_reference_png (hb_font_t *font, hb_codepoint_t  glyph)
350 {
351   hb_blob_t *blob = hb_blob_get_empty ();
352 
353   if (font->face->table.sbix->has_data ())
354     blob = font->face->table.sbix->reference_png (font, glyph, nullptr, nullptr, nullptr);
355 
356   if (!blob->length && font->face->table.CBDT->has_data ())
357     blob = font->face->table.CBDT->reference_png (font, glyph);
358 
359   return blob;
360 }
361 
362 
363 #endif
364