Imported Upstream version 1.8.1
[platform/upstream/harfbuzz.git] / src / hb-ot-color.cc
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-private.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-private.hh"
37 #include "hb-shaper-private.hh"
38
39 #if 0
40 HB_MARK_AS_FLAG_T (hb_ot_color_palette_flags_t)
41 //HB_SHAPER_DATA_ENSURE_DECLARE(ot, face) Hmm?
42
43
44 static 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   hb_ot_layout_t * layout = hb_ot_layout_from_face (face);
49   return *(layout->colr.get ());
50 }
51
52 static inline const OT::CPAL&
53 _get_cpal (hb_face_t *face)
54 {
55   if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::CPAL);
56   hb_ot_layout_t * layout = hb_ot_layout_from_face (face);
57   return *(layout->cpal.get ());
58 }
59
60
61 /**
62  * hb_ot_color_get_palette_count:
63  * @face: a font face.
64  *
65  * Returns: the number of color palettes in @face, or zero if @face has
66  * no colors.
67  *
68  * Since: REPLACEME
69  */
70 unsigned int
71 hb_ot_color_get_palette_count (hb_face_t *face)
72 {
73   const OT::CPAL& cpal = _get_cpal (face);
74   return cpal.get_palette_count ();
75 }
76
77
78 /**
79  * hb_ot_color_get_palette_name_id:
80  * @face: a font face.
81  * @palette: the index of the color palette whose name is being requested.
82  *
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".
85  *
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.
91  *
92  * Since: REPLACEME
93  */
94 unsigned int
95 hb_ot_color_get_palette_name_id (hb_face_t *face, unsigned int palette)
96 {
97   const OT::CPAL& cpal = _get_cpal (face);
98   return cpal.get_palette_name_id (palette);
99 }
100
101
102 /**
103  * hb_ot_color_get_palette_flags:
104  * @face: a font face
105  * @palette: the index of the color palette whose flags are being requested
106  *
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.
110  *
111  * Since: REPLACEME
112  */
113 hb_ot_color_palette_flags_t
114 hb_ot_color_get_palette_flags (hb_face_t *face, unsigned int palette)
115 {
116   const OT::CPAL& cpal = _get_cpal(face);
117   return cpal.get_palette_flags (palette);
118 }
119
120
121 /**
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.
138  *
139  * Retrieves the colors in a color palette.
140  *
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.
145  *
146  * Since: REPLACEME
147  */
148 unsigned int
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 */)
154 {
155   const OT::CPAL& cpal = _get_cpal(face);
156   if (unlikely (palette >= cpal.numPalettes))
157   {
158     if (color_count) *color_count = 0;
159     return 0;
160   }
161
162   const OT::ColorRecord* crec = &cpal.offsetFirstColorRecord (&cpal);
163   crec += cpal.colorRecordIndices[palette];
164
165   unsigned int num_results = 0;
166   if (likely (color_count && colors))
167   {
168     for (unsigned int i = start_offset;
169          i < cpal.numPaletteEntries && num_results < *color_count; ++i)
170     {
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;
176       ++num_results;
177     }
178   }
179
180   if (likely (color_count)) *color_count = num_results;
181   return cpal.numPaletteEntries;
182 }
183 #endif