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
26 */
27
28#include "hb-open-type.hh"
29#include "hb-ot-color-colr-table.hh"
30#include "hb-ot-color-cpal-table.hh"
31#include "hb-ot.h"
32
33#include <stdlib.h>
34#include <string.h>
35
36#include "hb-ot-layout.hh"
37#include "hb-shaper.hh"
38
39#if 0
40HB_MARK_AS_FLAG_T (hb_ot_color_palette_flags_t)
41//HB_SHAPER_DATA_ENSURE_DECLARE(ot, face) Hmm?
42
43
44static inline const OT::COLR&
45_get_colr (hb_face_t *face)
46{
47 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::COLR);
48 return *(hb_ot_face_data (face)->colr.get ());
49}
50
51static inline const OT::CPAL&
52_get_cpal (hb_face_t *face)
53{
54 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::CPAL);
55 return *(hb_ot_face_data (face)->cpal.get ());
56}
57
58
59/**
60 * hb_ot_color_get_palette_count:
61 * @face: a font face.
62 *
63 * Returns: the number of color palettes in @face, or zero if @face has
64 * no colors.
65 *
66 * Since: REPLACEME
67 */
68unsigned int
69hb_ot_color_get_palette_count (hb_face_t *face)
70{
71 const OT::CPAL& cpal = _get_cpal (face);
72 return cpal.get_palette_count ();
73}
74
75
76/**
77 * hb_ot_color_get_palette_name_id:
78 * @face: a font face.
79 * @palette: the index of the color palette whose name is being requested.
80 *
81 * Retrieves the name id of a color palette. For example, a color font can
82 * have themed palettes like "Spring", "Summer", "Fall", and "Winter".
83 *
84 * Returns: an identifier within @face's `name` table.
85 * If the requested palette has no name, or if @face has no colors,
86 * or if @palette is not between 0 and hb_ot_color_get_palette_count(),
87 * the result is 0xFFFF. The implementation does not check whether
88 * the returned palette name id is actually in @face's `name` table.
89 *
90 * Since: REPLACEME
91 */
92unsigned int
93hb_ot_color_get_palette_name_id (hb_face_t *face, unsigned int palette)
94{
95 const OT::CPAL& cpal = _get_cpal (face);
96 return cpal.get_palette_name_id (palette);
97}
98
99
100/**
101 * hb_ot_color_get_palette_flags:
102 * @face: a font face
103 * @palette: the index of the color palette whose flags are being requested
104 *
105 * Returns: the flags for the requested color palette. If @face has no colors,
106 * or if @palette is not between 0 and hb_ot_color_get_palette_count(),
107 * the result is #HB_OT_COLOR_PALETTE_FLAG_DEFAULT.
108 *
109 * Since: REPLACEME
110 */
111hb_ot_color_palette_flags_t
112hb_ot_color_get_palette_flags (hb_face_t *face, unsigned int palette)
113{
114 const OT::CPAL& cpal = _get_cpal(face);
115 return cpal.get_palette_flags (palette);
116}
117
118
119/**
120 * hb_ot_color_get_palette_colors:
121 * @face: a font face.
122 * @palette: the index of the color palette whose colors
123 * are being requested.
124 * @start_offset: the index of the first color being requested.
125 * @color_count: (inout) (optional): on input, how many colors
126 * can be maximally stored into the @colors array;
127 * on output, how many colors were actually stored.
128 * @colors: (array length=color_count) (optional):
129 * an array of #hb_ot_color_t records. After calling
130 * this function, @colors will be filled with
131 * the palette colors. If @colors is NULL, the function
132 * will just return the number of total colors
133 * without storing any actual colors; this can be used
134 * for allocating a buffer of suitable size before calling
135 * hb_ot_color_get_palette_colors() a second time.
136 *
137 * Retrieves the colors in a color palette.
138 *
139 * Returns: the total number of colors in the palette. All palettes in
140 * a font have the same number of colors. If @face has no colors, or if
141 * @palette is not between 0 and hb_ot_color_get_palette_count(),
142 * the result is zero.
143 *
144 * Since: REPLACEME
145 */
146unsigned int
147hb_ot_color_get_palette_colors (hb_face_t *face,
148 unsigned int palette, /* default=0 */
149 unsigned int start_offset,
150 unsigned int *color_count /* IN/OUT */,
151 hb_ot_color_t *colors /* OUT */)
152{
153 const OT::CPAL& cpal = _get_cpal(face);
154 if (unlikely (palette >= cpal.numPalettes))
155 {
156 if (color_count) *color_count = 0;
157 return 0;
158 }
159
160 const OT::ColorRecord* crec = &cpal.offsetFirstColorRecord (&cpal);
161 crec += cpal.colorRecordIndices[palette];
162
163 unsigned int num_results = 0;
164 if (likely (color_count && colors))
165 {
166 for (unsigned int i = start_offset;
167 i < cpal.numPaletteEntries && num_results < *color_count; ++i)
168 {
169 hb_ot_color_t* result = &colors[num_results];
170 result->red = crec[i].red;
171 result->green = crec[i].green;
172 result->blue = crec[i].blue;
173 result->alpha = crec[i].alpha;
174 ++num_results;
175 }
176 }
177
178 if (likely (color_count)) *color_count = num_results;
179 return cpal.numPaletteEntries;
180}
181#endif
182