2 * Copyright © 2016 Google, Inc.
3 * Copyright © 2018 Ebrahim Byagowi
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Google Author(s): Sascha Brawer
28 #include "hb-open-type-private.hh"
29 #include "hb-ot-color-colr-table.hh"
30 #include "hb-ot-color-cpal-table.hh"
36 #include "hb-ot-layout-private.hh"
37 #include "hb-shaper-private.hh"
40 HB_MARK_AS_FLAG_T (hb_ot_color_palette_flags_t)
41 //HB_SHAPER_DATA_ENSURE_DECLARE(ot, face) Hmm?
44 static inline const OT::COLR&
45 _get_colr (hb_face_t *face)
47 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::COLR);
48 hb_ot_layout_t * layout = hb_ot_layout_from_face (face);
49 return *(layout->colr.get ());
52 static inline const OT::CPAL&
53 _get_cpal (hb_face_t *face)
55 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::CPAL);
56 hb_ot_layout_t * layout = hb_ot_layout_from_face (face);
57 return *(layout->cpal.get ());
62 * hb_ot_color_get_palette_count:
65 * Returns: the number of color palettes in @face, or zero if @face has
71 hb_ot_color_get_palette_count (hb_face_t *face)
73 const OT::CPAL& cpal = _get_cpal (face);
74 return cpal.get_palette_count ();
79 * hb_ot_color_get_palette_name_id:
81 * @palette: the index of the color palette whose name is being requested.
83 * Retrieves the name id of a color palette. For example, a color font can
84 * have themed palettes like "Spring", "Summer", "Fall", and "Winter".
86 * Returns: an identifier within @face's `name` table.
87 * If the requested palette has no name, or if @face has no colors,
88 * or if @palette is not between 0 and hb_ot_color_get_palette_count(),
89 * the result is 0xFFFF. The implementation does not check whether
90 * the returned palette name id is actually in @face's `name` table.
95 hb_ot_color_get_palette_name_id (hb_face_t *face, unsigned int palette)
97 const OT::CPAL& cpal = _get_cpal (face);
98 return cpal.get_palette_name_id (palette);
103 * hb_ot_color_get_palette_flags:
105 * @palette: the index of the color palette whose flags are being requested
107 * Returns: the flags for the requested color palette. If @face has no colors,
108 * or if @palette is not between 0 and hb_ot_color_get_palette_count(),
109 * the result is #HB_OT_COLOR_PALETTE_FLAG_DEFAULT.
113 hb_ot_color_palette_flags_t
114 hb_ot_color_get_palette_flags (hb_face_t *face, unsigned int palette)
116 const OT::CPAL& cpal = _get_cpal(face);
117 return cpal.get_palette_flags (palette);
122 * hb_ot_color_get_palette_colors:
123 * @face: a font face.
124 * @palette: the index of the color palette whose colors
125 * are being requested.
126 * @start_offset: the index of the first color being requested.
127 * @color_count: (inout) (optional): on input, how many colors
128 * can be maximally stored into the @colors array;
129 * on output, how many colors were actually stored.
130 * @colors: (array length=color_count) (optional):
131 * an array of #hb_ot_color_t records. After calling
132 * this function, @colors will be filled with
133 * the palette colors. If @colors is NULL, the function
134 * will just return the number of total colors
135 * without storing any actual colors; this can be used
136 * for allocating a buffer of suitable size before calling
137 * hb_ot_color_get_palette_colors() a second time.
139 * Retrieves the colors in a color palette.
141 * Returns: the total number of colors in the palette. All palettes in
142 * a font have the same number of colors. If @face has no colors, or if
143 * @palette is not between 0 and hb_ot_color_get_palette_count(),
144 * the result is zero.
149 hb_ot_color_get_palette_colors (hb_face_t *face,
150 unsigned int palette, /* default=0 */
151 unsigned int start_offset,
152 unsigned int *color_count /* IN/OUT */,
153 hb_ot_color_t *colors /* OUT */)
155 const OT::CPAL& cpal = _get_cpal(face);
156 if (unlikely (palette >= cpal.numPalettes))
158 if (color_count) *color_count = 0;
162 const OT::ColorRecord* crec = &cpal.offsetFirstColorRecord (&cpal);
163 crec += cpal.colorRecordIndices[palette];
165 unsigned int num_results = 0;
166 if (likely (color_count && colors))
168 for (unsigned int i = start_offset;
169 i < cpal.numPaletteEntries && num_results < *color_count; ++i)
171 hb_ot_color_t* result = &colors[num_results];
172 result->red = crec[i].red;
173 result->green = crec[i].green;
174 result->blue = crec[i].blue;
175 result->alpha = crec[i].alpha;
180 if (likely (color_count)) *color_count = num_results;
181 return cpal.numPaletteEntries;