blob: ceebe0b7a0203f19369d1c1e9527d91503f2338c [file] [log] [blame]
Sascha Brawerd34d3ac2016-04-25 18:20:57 +02001/*
2 * Copyright © 2016 Google, Inc.
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +03303 * Copyright © 2018 Ebrahim Byagowi
Sascha Brawerd34d3ac2016-04-25 18:20:57 +02004 *
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"
Ebrahim Byagowic446c232018-03-03 22:43:23 +033029#include "hb-ot-color-colr-table.hh"
30#include "hb-ot-color-cpal-table.hh"
Sascha Brawerd34d3ac2016-04-25 18:20:57 +020031#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
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +033039#if 0
Sascha Brawerd34d3ac2016-04-25 18:20:57 +020040HB_MARK_AS_FLAG_T (hb_ot_color_palette_flags_t)
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +033041//HB_SHAPER_DATA_ENSURE_DECLARE(ot, face) Hmm?
Sascha Brawerd34d3ac2016-04-25 18:20:57 +020042
43
Ebrahim Byagowif671f7f2018-02-28 13:19:40 +033044static inline const OT::COLR&
45_get_colr (hb_face_t *face)
46{
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 ());
50}
51
Sascha Brawerd34d3ac2016-04-25 18:20:57 +020052static inline const OT::CPAL&
53_get_cpal (hb_face_t *face)
54{
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +033055 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::CPAL);
Sascha Brawerd34d3ac2016-04-25 18:20:57 +020056 hb_ot_layout_t * layout = hb_ot_layout_from_face (face);
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +033057 return *(layout->cpal.get ());
Sascha Brawerd34d3ac2016-04-25 18:20:57 +020058}
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 *
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +033068 * Since: REPLACEME
Sascha Brawerd34d3ac2016-04-25 18:20:57 +020069 */
70unsigned int
71hb_ot_color_get_palette_count (hb_face_t *face)
72{
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +033073 const OT::CPAL& cpal = _get_cpal (face);
74 return cpal.get_palette_count ();
Sascha Brawerd34d3ac2016-04-25 18:20:57 +020075}
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(),
Sascha Brawer5967eab2016-06-24 16:42:27 +020089 * the result is 0xFFFF. The implementation does not check whether
90 * the returned palette name id is actually in @face's `name` table.
Sascha Brawerd34d3ac2016-04-25 18:20:57 +020091 *
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +033092 * Since: REPLACEME
Sascha Brawerd34d3ac2016-04-25 18:20:57 +020093 */
94unsigned int
95hb_ot_color_get_palette_name_id (hb_face_t *face, unsigned int palette)
96{
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +033097 const OT::CPAL& cpal = _get_cpal (face);
98 return cpal.get_palette_name_id (palette);
Sascha Brawerd34d3ac2016-04-25 18:20:57 +020099}
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 *
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +0330111 * Since: REPLACEME
Sascha Brawerd34d3ac2016-04-25 18:20:57 +0200112 */
113hb_ot_color_palette_flags_t
114hb_ot_color_get_palette_flags (hb_face_t *face, unsigned int palette)
115{
116 const OT::CPAL& cpal = _get_cpal(face);
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +0330117 return cpal.get_palette_flags (palette);
Sascha Brawerd34d3ac2016-04-25 18:20:57 +0200118}
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.
Sascha Brawer5967eab2016-06-24 16:42:27 +0200130 * @colors: (array length=color_count) (optional):
Sascha Brawerd34d3ac2016-04-25 18:20:57 +0200131 * 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 *
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +0330146 * Since: REPLACEME
Sascha Brawerd34d3ac2016-04-25 18:20:57 +0200147 */
148unsigned int
149hb_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);
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +0330156 if (unlikely (palette >= cpal.numPalettes))
Sascha Brawerd34d3ac2016-04-25 18:20:57 +0200157 {
158 if (color_count) *color_count = 0;
159 return 0;
160 }
161
162 const OT::ColorRecord* crec = &cpal.offsetFirstColorRecord (&cpal);
Sascha Brawerd34d3ac2016-04-25 18:20:57 +0200163 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}
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +0330183#endif