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#include <stdlib.h>
41#include <string.h>
42
43
44/**
45 * SECTION:hb-ot-color
46 * @title: hb-ot-color
47 * @short_description: OpenType Color Fonts
48 * @include: hb-ot.h
49 *
50 * Functions for fetching color-font information from OpenType font faces.
51 *
52 * HarfBuzz supports `COLR`/`CPAL`, `sbix`, `CBDT`, and `SVG` color fonts.
53 **/
54
55
56/*
57 * CPAL
58 */
59
60
61/**
62 * hb_ot_color_has_palettes:
63 * @face: #hb_face_t to work upon
64 *
65 * Tests whether a face includes a `CPAL` color-palette table.
66 *
67 * Return value: true if data found, false otherwise
68 *
69 * Since: 2.1.0
70 */
71hb_bool_t
72hb_ot_color_has_palettes (hb_face_t *face)
73{
74 return face->table.CPAL->has_data ();
75}
76
77/**
78 * hb_ot_color_palette_get_count:
79 * @face: #hb_face_t to work upon
80 *
81 * Fetches the number of color palettes in a face.
82 *
83 * Return value: the number of palettes found
84 *
85 * Since: 2.1.0
86 */
87unsigned int
88hb_ot_color_palette_get_count (hb_face_t *face)
89{
90 return face->table.CPAL->get_palette_count ();
91}
92
93/**
94 * hb_ot_color_palette_get_name_id:
95 * @face: #hb_face_t to work upon
96 * @palette_index: The index of the color palette
97 *
98 * Fetches the `name` table Name ID that provides display names for
99 * a `CPAL` color palette.
100 *
101 * Palette display names can be generic (e.g., "Default") or provide
102 * specific, themed names (e.g., "Spring", "Summer", "Fall", and "Winter").
103 *
104 * Return value: the Named ID found for the palette.
105 * If the requested palette has no name the result is #HB_OT_NAME_ID_INVALID.
106 *
107 * Since: 2.1.0
108 */
109hb_ot_name_id_t
110hb_ot_color_palette_get_name_id (hb_face_t *face,
111 unsigned int palette_index)
112{
113 return face->table.CPAL->get_palette_name_id (palette_index);
114}
115
116/**
117 * hb_ot_color_palette_color_get_name_id:
118 * @face: #hb_face_t to work upon
119 * @color_index: The index of the color
120 *
121 * Fetches the `name` table Name ID that provides display names for
122 * the specificed color in a face's `CPAL` color palette.
123 *
124 * Display names can be generic (e.g., "Background") or specific
125 * (e.g., "Eye color").
126 *
127 * Return value: the Name ID found for the color.
128 *
129 * Since: 2.1.0
130 */
131hb_ot_name_id_t
132hb_ot_color_palette_color_get_name_id (hb_face_t *face,
133 unsigned int color_index)
134{
135 return face->table.CPAL->get_color_name_id (color_index);
136}
137
138/**
139 * hb_ot_color_palette_get_flags:
140 * @face: #hb_face_t to work upon
141 * @palette_index: The index of the color palette
142 *
143 * Fetches the flags defined for a color palette.
144 *
145 * Return value: the #hb_ot_color_palette_flags_t of the requested color palette
146 *
147 * Since: 2.1.0
148 */
149hb_ot_color_palette_flags_t
150hb_ot_color_palette_get_flags (hb_face_t *face,
151 unsigned int palette_index)
152{
153 return face->table.CPAL->get_palette_flags (palette_index);
154}
155
156/**
157 * hb_ot_color_palette_get_colors:
158 * @face: #hb_face_t to work upon
159 * @palette_index: the index of the color palette to query
160 * @start_offset: offset of the first color to retrieve
161 * @color_count: (inout) (optional): Input = the maximum number of colors to return;
162 * Output = the actual number of colors returned (may be zero)
163 * @colors: (out) (array length=color_count) (nullable): The array of #hb_color_t records found
164 *
165 * Fetches a list of the colors in a color palette.
166 *
167 * After calling this function, @colors will be filled with the palette
168 * colors. If @colors is NULL, the function will just return the number
169 * of total colors without storing any actual colors; this can be used
170 * for allocating a buffer of suitable size before calling
171 * hb_ot_color_palette_get_colors() a second time.
172 *
173 * Return value: the total number of colors in the palette
174 *
175 * Since: 2.1.0
176 */
177unsigned int
178hb_ot_color_palette_get_colors (hb_face_t *face,
179 unsigned int palette_index,
180 unsigned int start_offset,
181 unsigned int *colors_count /* IN/OUT. May be NULL. */,
182 hb_color_t *colors /* OUT. May be NULL. */)
183{
184 return face->table.CPAL->get_palette_colors (palette_index, start_offset, colors_count, colors);
185}
186
187
188/*
189 * COLR
190 */
191
192/**
193 * hb_ot_color_has_layers:
194 * @face: #hb_face_t to work upon
195 *
196 * Tests whether a face includes any `COLR` color layers.
197 *
198 * Return value: true if data found, false otherwise
199 *
200 * Since: 2.1.0
201 */
202hb_bool_t
203hb_ot_color_has_layers (hb_face_t *face)
204{
205 return face->table.COLR->has_data ();
206}
207
208/**
209 * hb_ot_color_glyph_get_layers:
210 * @face: #hb_face_t to work upon
211 * @glyph: The glyph index to query
212 * @start_offset: offset of the first layer to retrieve
213 * @layer_count: (inout) (optional): Input = the maximum number of layers to return;
214 * Output = the actual number of layers returned (may be zero)
215 * @layers: (out) (array length=layer_count) (nullable): The array of layers found
216 *
217 * Fetches a list of all color layers for the specified glyph index in the specified
218 * face. The list returned will begin at the offset provided.
219 *
220 * Return value: Total number of layers available for the glyph index queried
221 *
222 * Since: 2.1.0
223 */
224unsigned int
225hb_ot_color_glyph_get_layers (hb_face_t *face,
226 hb_codepoint_t glyph,
227 unsigned int start_offset,
228 unsigned int *layer_count, /* IN/OUT. May be NULL. */
229 hb_ot_color_layer_t *layers /* OUT. May be NULL. */)
230{
231 return face->table.COLR->get_glyph_layers (glyph, start_offset, layer_count, layers);
232}
233
234
235/*
236 * SVG
237 */
238
239/**
240 * hb_ot_color_has_svg:
241 * @face: #hb_face_t to work upon.
242 *
243 * Tests whether a face includes any `SVG` glyph images.
244 *
245 * Return value: true if data found, false otherwise.
246 *
247 * Since: 2.1.0
248 */
249hb_bool_t
250hb_ot_color_has_svg (hb_face_t *face)
251{
252 return face->table.SVG->has_data ();
253}
254
255/**
256 * hb_ot_color_glyph_reference_svg:
257 * @face: #hb_face_t to work upon
258 * @glyph: a svg glyph index
259 *
260 * Fetches the SVG document for a glyph. The blob may be either plain text or gzip-encoded.
261 *
262 * Return value: (transfer full): An #hb_blob_t containing the SVG document of the glyph, if available
263 *
264 * Since: 2.1.0
265 */
266hb_blob_t *
267hb_ot_color_glyph_reference_svg (hb_face_t *face, hb_codepoint_t glyph)
268{
269 return face->table.SVG->reference_blob_for_glyph (glyph);
270}
271
272
273/*
274 * PNG: CBDT or sbix
275 */
276
277/**
278 * hb_ot_color_has_png:
279 * @face: #hb_face_t to work upon
280 *
281 * Tests whether a face has PNG glyph images (either in `CBDT` or `sbix` tables).
282 *
283 * Return value: true if data found, false otherwise
284 *
285 * Since: 2.1.0
286 */
287hb_bool_t
288hb_ot_color_has_png (hb_face_t *face)
289{
290 return face->table.CBDT->has_data () || face->table.sbix->has_data ();
291}
292
293/**
294 * hb_ot_color_glyph_reference_png:
295 * @font: #hb_font_t to work upon
296 * @glyph: a glyph index
297 *
298 * Fetches the PNG image for a glyph. This function takes a font object, not a face object,
299 * as input. To get an optimally sized PNG blob, the UPEM value must be set on the @font
300 * object. If UPEM is unset, the blob returned will be the largest PNG available.
301 *
302 * Return value: (transfer full): An #hb_blob_t containing the PNG image for the glyph, if available
303 *
304 * Since: 2.1.0
305 */
306hb_blob_t *
307hb_ot_color_glyph_reference_png (hb_font_t *font, hb_codepoint_t glyph)
308{
309 hb_blob_t *blob = hb_blob_get_empty ();
310
311 if (font->face->table.sbix->has_data ())
312 blob = font->face->table.sbix->reference_png (font, glyph, nullptr, nullptr, nullptr);
313
314 if (!blob->length && font->face->table.CBDT->has_data ())
315 blob = font->face->table.CBDT->reference_png (font, glyph);
316
317 return blob;
318}
319
320
321#endif
322