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 "hb-ot-color-cbdt-table.hh"
35 #include "hb-ot-color-colr-table.hh"
36 #include "hb-ot-color-cpal-table.hh"
37 #include "hb-ot-color-sbix-table.hh"
38 #include "hb-ot-color-svg-table.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  * Return value: the total number of colors in the palette
171  *
172  * Since: 2.1.0
173  */
174 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)175 hb_ot_color_palette_get_colors (hb_face_t     *face,
176 				unsigned int   palette_index,
177 				unsigned int   start_offset,
178 				unsigned int  *colors_count  /* IN/OUT.  May be NULL. */,
179 				hb_color_t    *colors        /* OUT.     May be NULL. */)
180 {
181   return face->table.CPAL->get_palette_colors (palette_index, start_offset, colors_count, colors);
182 }
183 
184 
185 /*
186  * COLR
187  */
188 
189 /**
190  * hb_ot_color_has_layers:
191  * @face: #hb_face_t to work upon
192  *
193  * Tests whether a face includes any `COLR` color layers.
194  *
195  * Return value: %true if data found, %false otherwise
196  *
197  * Since: 2.1.0
198  */
199 hb_bool_t
hb_ot_color_has_layers(hb_face_t * face)200 hb_ot_color_has_layers (hb_face_t *face)
201 {
202   return face->table.COLR->has_data ();
203 }
204 
205 /**
206  * hb_ot_color_glyph_get_layers:
207  * @face: #hb_face_t to work upon
208  * @glyph: The glyph index to query
209  * @start_offset: offset of the first layer to retrieve
210  * @layer_count: (inout) (optional): Input = the maximum number of layers to return;
211  *         Output = the actual number of layers returned (may be zero)
212  * @layers: (out) (array length=layer_count) (nullable): The array of layers found
213  *
214  * Fetches a list of all color layers for the specified glyph index in the specified
215  * face. The list returned will begin at the offset provided.
216  *
217  * Return value: Total number of layers available for the glyph index queried
218  *
219  * Since: 2.1.0
220  */
221 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)222 hb_ot_color_glyph_get_layers (hb_face_t           *face,
223 			      hb_codepoint_t       glyph,
224 			      unsigned int         start_offset,
225 			      unsigned int        *layer_count, /* IN/OUT.  May be NULL. */
226 			      hb_ot_color_layer_t *layers /* OUT.     May be NULL. */)
227 {
228   return face->table.COLR->get_glyph_layers (glyph, start_offset, layer_count, layers);
229 }
230 
231 
232 /*
233  * SVG
234  */
235 
236 /**
237  * hb_ot_color_has_svg:
238  * @face: #hb_face_t to work upon.
239  *
240  * Tests whether a face includes any `SVG` glyph images.
241  *
242  * Return value: %true if data found, %false otherwise.
243  *
244  * Since: 2.1.0
245  */
246 hb_bool_t
hb_ot_color_has_svg(hb_face_t * face)247 hb_ot_color_has_svg (hb_face_t *face)
248 {
249   return face->table.SVG->has_data ();
250 }
251 
252 /**
253  * hb_ot_color_glyph_reference_svg:
254  * @face: #hb_face_t to work upon
255  * @glyph: a svg glyph index
256  *
257  * Fetches the SVG document for a glyph. The blob may be either plain text or gzip-encoded.
258  *
259  * If the glyph has no SVG document, the singleton empty blob is returned.
260  *
261  * Return value: (transfer full): An #hb_blob_t containing the SVG document of the glyph, if available
262  *
263  * Since: 2.1.0
264  */
265 hb_blob_t *
hb_ot_color_glyph_reference_svg(hb_face_t * face,hb_codepoint_t glyph)266 hb_ot_color_glyph_reference_svg (hb_face_t *face, hb_codepoint_t glyph)
267 {
268   return face->table.SVG->reference_blob_for_glyph (glyph);
269 }
270 
271 
272 /*
273  * PNG: CBDT or sbix
274  */
275 
276 /**
277  * hb_ot_color_has_png:
278  * @face: #hb_face_t to work upon
279  *
280  * Tests whether a face has PNG glyph images (either in `CBDT` or `sbix` tables).
281  *
282  * Return value: %true if data found, %false otherwise
283  *
284  * Since: 2.1.0
285  */
286 hb_bool_t
hb_ot_color_has_png(hb_face_t * face)287 hb_ot_color_has_png (hb_face_t *face)
288 {
289   return face->table.CBDT->has_data () || face->table.sbix->has_data ();
290 }
291 
292 /**
293  * hb_ot_color_glyph_reference_png:
294  * @font: #hb_font_t to work upon
295  * @glyph: a glyph index
296  *
297  * Fetches the PNG image for a glyph. This function takes a font object, not a face object,
298  * as input. To get an optimally sized PNG blob, the UPEM value must be set on the @font
299  * object. If UPEM is unset, the blob returned will be the largest PNG available.
300  *
301  * If the glyph has no PNG image, the singleton empty blob is returned.
302  *
303  * Return value: (transfer full): An #hb_blob_t containing the PNG image for the glyph, if available
304  *
305  * Since: 2.1.0
306  */
307 hb_blob_t *
hb_ot_color_glyph_reference_png(hb_font_t * font,hb_codepoint_t glyph)308 hb_ot_color_glyph_reference_png (hb_font_t *font, hb_codepoint_t  glyph)
309 {
310   hb_blob_t *blob = hb_blob_get_empty ();
311 
312   if (font->face->table.sbix->has_data ())
313     blob = font->face->table.sbix->reference_png (font, glyph, nullptr, nullptr, nullptr);
314 
315   if (!blob->length && font->face->table.CBDT->has_data ())
316     blob = font->face->table.CBDT->reference_png (font, glyph);
317 
318   return blob;
319 }
320 
321 
322 #endif
323