hb-ot-color.cc 7.1 KB
Newer Older
S
Sascha Brawer 已提交
1 2
/*
 * Copyright © 2016  Google, Inc.
3
 * Copyright © 2018  Ebrahim Byagowi
S
Sascha Brawer 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
B
Behdad Esfahbod 已提交
25
 * Google Author(s): Sascha Brawer, Behdad Esfahbod
S
Sascha Brawer 已提交
26 27
 */

28
#include "hb-open-type.hh"
29
#include "hb-ot-color-cbdt-table.hh"
30 31
#include "hb-ot-color-colr-table.hh"
#include "hb-ot-color-cpal-table.hh"
32 33
#include "hb-ot-color-sbix-table.hh"
#include "hb-ot-color-svg-table.hh"
K
Khaled Hosny 已提交
34
#include "hb-ot-face.hh"
S
Sascha Brawer 已提交
35 36 37 38 39
#include "hb-ot.h"

#include <stdlib.h>
#include <string.h>

40
#include "hb-ot-layout.hh"
S
Sascha Brawer 已提交
41 42


E
Ebrahim Byagowi 已提交
43 44 45
static inline const OT::COLR&
_get_colr (hb_face_t *face)
{
B
Behdad Esfahbod 已提交
46
  if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::COLR);
K
Khaled Hosny 已提交
47
  return *(hb_ot_face_data (face)->COLR.get ());
E
Ebrahim Byagowi 已提交
48 49
}

S
Sascha Brawer 已提交
50 51 52
static inline const OT::CPAL&
_get_cpal (hb_face_t *face)
{
B
Behdad Esfahbod 已提交
53
  if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::CPAL);
K
Khaled Hosny 已提交
54
  return *(hb_ot_face_data (face)->CPAL.get ());
S
Sascha Brawer 已提交
55 56
}

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#if 0
static inline const OT::CBDT_accelerator_t&
_get_cbdt (hb_face_t *face)
{
  if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::CBDT_accelerator_t);
  return *(hb_ot_face_data (face)->CBDT.get ());
}

static inline const OT::sbix&
_get_sbix (hb_face_t *face)
{
  if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::sbix);
  return *(hb_ot_face_data (face)->sbix.get ());
}

static inline const OT::SVG&
_get_svg (hb_face_t *face)
{
  if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::SVG);
  return *(hb_ot_face_data (face)->SVG.get ());
}
#endif

B
Behdad Esfahbod 已提交
80

B
Behdad Esfahbod 已提交
81 82
/*
 * CPAL
S
Sascha Brawer 已提交
83
 */
B
Behdad Esfahbod 已提交
84

S
Sascha Brawer 已提交
85 86

/**
B
Behdad Esfahbod 已提交
87
 * hb_ot_color_has_palettes:
S
Sascha Brawer 已提交
88 89
 * @face: a font face.
 *
B
Behdad Esfahbod 已提交
90
 * Returns: whether CPAL table is available.
S
Sascha Brawer 已提交
91
 *
92
 * Since: REPLACEME
S
Sascha Brawer 已提交
93
 */
94
hb_bool_t
B
Behdad Esfahbod 已提交
95
hb_ot_color_has_palettes (hb_face_t *face)
S
Sascha Brawer 已提交
96
{
B
Behdad Esfahbod 已提交
97
  return _get_cpal (face).has_data ();
S
Sascha Brawer 已提交
98 99 100
}

/**
B
Behdad Esfahbod 已提交
101
 * hb_ot_color_palette_get_count:
102
 * @face: a font face.
S
Sascha Brawer 已提交
103
 *
K
Khaled Hosny 已提交
104 105
 * Returns: the number of color palettes in @face, or zero if @face has
 * no colors.
S
Sascha Brawer 已提交
106
 *
107
 * Since: REPLACEME
S
Sascha Brawer 已提交
108
 */
109
unsigned int
B
Behdad Esfahbod 已提交
110
hb_ot_color_palette_get_count (hb_face_t *face)
S
Sascha Brawer 已提交
111
{
112
  return _get_cpal (face).get_palette_count ();
S
Sascha Brawer 已提交
113 114
}

115
/**
B
Behdad Esfahbod 已提交
116
 * hb_ot_color_palette_get_name_id:
E
Minor  
Ebrahim Byagowi 已提交
117
 * @face:    a font face.
118 119 120 121 122 123
 * @palette: the index of the color palette whose name is being requested.
 *
 * Retrieves the name id of a color palette. For example, a color font can
 * have themed palettes like "Spring", "Summer", "Fall", and "Winter".
 *
 * Returns: an identifier within @face's `name` table.
B
Behdad Esfahbod 已提交
124
 * If the requested palette has no name the result is #HB_NAME_ID_INVALID.
125 126 127 128
 *
 * Since: REPLACEME
 */
hb_name_id_t
B
Behdad Esfahbod 已提交
129 130
hb_ot_color_palette_get_name_id (hb_face_t *face,
				 unsigned int palette_index)
131
{
B
Behdad Esfahbod 已提交
132
  return _get_cpal (face).get_palette_name_id (palette_index);
133 134 135
}

/**
B
Behdad Esfahbod 已提交
136
 * hb_ot_color_palette_color_get_name_id:
137
 * @face: a font face.
B
Behdad Esfahbod 已提交
138
 * @color_index:
139
 *
E
Minor  
Ebrahim Byagowi 已提交
140
 * Returns: Name ID associated with a palette entry, e.g. eye color
141 142 143 144
 *
 * Since: REPLACEME
 */
hb_name_id_t
B
Behdad Esfahbod 已提交
145 146
hb_ot_color_palette_color_get_name_id (hb_face_t *face,
				       unsigned int color_index)
147
{
B
Behdad Esfahbod 已提交
148
  return _get_cpal (face).get_color_name_id (color_index);
149 150
}

S
Sascha Brawer 已提交
151
/**
B
Behdad Esfahbod 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
 * hb_ot_color_palette_get_flags:
 * @face:    a font face
 * @palette_index: the index of the color palette whose flags are being requested
 *
 * Returns: the flags for the requested color palette.
 *
 * Since: REPLACEME
 */
hb_ot_color_palette_flags_t
hb_ot_color_palette_get_flags (hb_face_t *face,
			       unsigned int palette_index)
{
  const OT::CPAL& cpal = _get_cpal(face);
  return cpal.get_palette_flags (palette_index);
}

/**
 * hb_ot_color_palette_get_colors:
S
Sascha Brawer 已提交
170
 * @face:         a font face.
B
Behdad Esfahbod 已提交
171
 * @palette_index:the index of the color palette whose colors
S
Sascha Brawer 已提交
172 173 174 175 176
 *                are being requested.
 * @start_offset: the index of the first color being requested.
 * @color_count:  (inout) (optional): on input, how many colors
 *                can be maximally stored into the @colors array;
 *                on output, how many colors were actually stored.
K
Khaled Hosny 已提交
177
 * @colors: (array length=color_count) (out) (optional):
178
 *                an array of #hb_color_t records. After calling
S
Sascha Brawer 已提交
179 180 181 182 183
 *                this function, @colors will be filled with
 *                the palette colors. If @colors is NULL, the function
 *                will just return the number of total colors
 *                without storing any actual colors; this can be used
 *                for allocating a buffer of suitable size before calling
B
Behdad Esfahbod 已提交
184
 *                hb_ot_color_palette_get_colors() a second time.
S
Sascha Brawer 已提交
185 186 187
 *
 * Retrieves the colors in a color palette.
 *
B
Behdad Esfahbod 已提交
188
 * Returns: the total number of colors in the palette.
S
Sascha Brawer 已提交
189
 *
190
 * Since: REPLACEME
S
Sascha Brawer 已提交
191 192
 */
unsigned int
B
Behdad Esfahbod 已提交
193 194
hb_ot_color_palette_get_colors (hb_face_t      *face,
				unsigned int    palette_index,      /* default=0 */
K
Khaled Hosny 已提交
195
				unsigned int    start_offset,
E
Minor  
Ebrahim Byagowi 已提交
196
				unsigned int   *colors_count  /* IN/OUT.  May be NULL. */,
197
				hb_color_t     *colors        /* OUT.     May be NULL. */)
S
Sascha Brawer 已提交
198 199
{
  const OT::CPAL& cpal = _get_cpal(face);
B
Behdad Esfahbod 已提交
200
  if (unlikely (palette_index >= cpal.get_palette_count ()))
S
Sascha Brawer 已提交
201
  {
E
Minor  
Ebrahim Byagowi 已提交
202
    if (colors_count) *colors_count = 0;
S
Sascha Brawer 已提交
203 204 205
    return 0;
  }

K
Khaled Hosny 已提交
206
  unsigned int num_results = 0;
E
Minor  
Ebrahim Byagowi 已提交
207
  if (colors_count)
K
Khaled Hosny 已提交
208
  {
E
Minor  
Ebrahim Byagowi 已提交
209 210
    unsigned int platte_count;
    platte_count = MIN<unsigned int>(*colors_count,
B
Behdad Esfahbod 已提交
211
				     cpal.get_color_count () - start_offset);
K
Khaled Hosny 已提交
212 213
    for (unsigned int i = 0; i < platte_count; i++)
    {
B
Behdad Esfahbod 已提交
214
      if (cpal.get_color_record_argb(start_offset + i, palette_index, &colors[num_results]))
E
Minor  
Ebrahim Byagowi 已提交
215
	++num_results;
K
Khaled Hosny 已提交
216 217
    }
  }
S
Sascha Brawer 已提交
218

E
Minor  
Ebrahim Byagowi 已提交
219
  if (likely (colors_count)) *colors_count = num_results;
B
Behdad Esfahbod 已提交
220
  return cpal.get_color_count ();
K
Khaled Hosny 已提交
221 222
}

B
Behdad Esfahbod 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

/*
 * COLR
 */

/**
 * hb_ot_color_has_layers:
 * @face: a font face.
 *
 * Returns: whether COLR table is available.
 *
 * Since: REPLACEME
 */
hb_bool_t
hb_ot_color_has_layers (hb_face_t *face)
{
  return _get_colr (face).has_data ();
}

242
/**
B
Behdad Esfahbod 已提交
243
 * hb_ot_color_glyph_get_layers:
E
Minor  
Ebrahim Byagowi 已提交
244
 * @face: a font face.
B
Behdad Esfahbod 已提交
245
 * @glyph:
246 247
 * @start_offset:
 * @count:  (inout) (optional):
B
Behdad Esfahbod 已提交
248
 * @layers: (array length=count) (out) (optional):
249 250 251 252 253
 *
 * Returns:
 *
 * Since: REPLACEME
 */
K
Khaled Hosny 已提交
254
unsigned int
B
Behdad Esfahbod 已提交
255 256 257 258 259
hb_ot_color_glyph_get_layers (hb_face_t           *face,
			      hb_codepoint_t       glyph,
			      unsigned int         start_offset,
			      unsigned int        *count, /* IN/OUT.  May be NULL. */
			      hb_ot_color_layer_t *layers /* OUT.     May be NULL. */)
K
Khaled Hosny 已提交
260 261
{
  const OT::COLR& colr = _get_colr (face);
B
Behdad Esfahbod 已提交
262
  return colr.get_glyph_layers (glyph, start_offset, count, layers);
S
Sascha Brawer 已提交
263
}