hb-ot-color.cc 7.4 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 25 26 27
 *
 *  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.
 *
 * Google Author(s): Sascha Brawer
 */

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

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

37
#include "hb-ot-layout.hh"
S
Sascha Brawer 已提交
38 39


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

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

/**
55
 * hb_ot_color_has_cpal_data:
S
Sascha Brawer 已提交
56 57
 * @face: a font face.
 *
K
Khaled Hosny 已提交
58
 * Returns: whether CPAL table is available.
S
Sascha Brawer 已提交
59
 *
60
 * Since: REPLACEME
S
Sascha Brawer 已提交
61
 */
62 63
hb_bool_t
hb_ot_color_has_cpal_data (hb_face_t *face)
S
Sascha Brawer 已提交
64
{
65
  return &_get_cpal (face) != &Null(OT::CPAL);
S
Sascha Brawer 已提交
66 67 68
}

/**
69
 * hb_ot_color_has_colr_data:
S
Sascha Brawer 已提交
70 71
 * @face: a font face.
 *
K
Khaled Hosny 已提交
72
 * Returns: whether COLR table is available.
S
Sascha Brawer 已提交
73
 *
74
 * Since: REPLACEME
S
Sascha Brawer 已提交
75
 */
76 77
hb_bool_t
hb_ot_color_has_colr_data (hb_face_t *face)
S
Sascha Brawer 已提交
78
{
79
  return &_get_colr (face) != &Null(OT::COLR);
S
Sascha Brawer 已提交
80 81 82
}

/**
83 84
 * hb_ot_color_get_palette_count:
 * @face: a font face.
S
Sascha Brawer 已提交
85
 *
K
Khaled Hosny 已提交
86 87
 * Returns: the number of color palettes in @face, or zero if @face has
 * no colors.
S
Sascha Brawer 已提交
88
 *
89
 * Since: REPLACEME
S
Sascha Brawer 已提交
90
 */
91 92
unsigned int
hb_ot_color_get_palette_count (hb_face_t *face)
S
Sascha Brawer 已提交
93
{
94
  return _get_cpal (face).get_palette_count ();
S
Sascha Brawer 已提交
95 96
}

97 98
/**
 * hb_ot_color_get_palette_name_id:
E
Minor  
Ebrahim Byagowi 已提交
99
 * @face:    a font face.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 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.
 * If the requested palette has no name, or if @face has no colors,
 * or if @palette is not between 0 and hb_ot_color_get_palette_count(),
 * the result is 0xFFFF. The implementation does not check whether
 * the returned palette name id is actually in @face's `name` table.
 *
 * Since: REPLACEME
 */
hb_name_id_t
hb_ot_color_get_palette_name_id (hb_face_t *face, unsigned int palette)
{
  return _get_cpal (face).get_palette_name_id (palette);
}

/**
 * hb_ot_color_get_palette_entry_name_id:
 * @face: a font face.
 * @palette_entry:
 *
E
Minor  
Ebrahim Byagowi 已提交
124
 * Returns: Name ID associated with a palette entry, e.g. eye color
125 126 127 128 129 130 131 132 133
 *
 * Since: REPLACEME
 */
hb_name_id_t
hb_ot_color_get_palette_entry_name_id (hb_face_t *face, unsigned int palette_entry)
{
  return _get_cpal (face).get_palette_entry_name_id (palette_entry);
}

S
Sascha Brawer 已提交
134 135 136 137 138 139 140 141 142
/**
 * hb_ot_color_get_palette_colors:
 * @face:         a font face.
 * @palette:      the index of the color palette whose colors
 *                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 已提交
143
 * @colors: (array length=color_count) (out) (optional):
144
 *                an array of #hb_color_t records. After calling
S
Sascha Brawer 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158
 *                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
 *                hb_ot_color_get_palette_colors() a second time.
 *
 * Retrieves the colors in a color palette.
 *
 * Returns: the total number of colors in the palette. All palettes in
 * a font have the same number of colors. If @face has no colors, or if
 * @palette is not between 0 and hb_ot_color_get_palette_count(),
 * the result is zero.
 *
159
 * Since: REPLACEME
S
Sascha Brawer 已提交
160 161
 */
unsigned int
K
Khaled Hosny 已提交
162
hb_ot_color_get_palette_colors (hb_face_t      *face,
163
				unsigned int    palette,      /* default=0 */
K
Khaled Hosny 已提交
164
				unsigned int    start_offset,
E
Minor  
Ebrahim Byagowi 已提交
165
				unsigned int   *colors_count  /* IN/OUT.  May be NULL. */,
166
				hb_color_t     *colors        /* OUT.     May be NULL. */)
S
Sascha Brawer 已提交
167 168
{
  const OT::CPAL& cpal = _get_cpal(face);
K
Khaled Hosny 已提交
169
  if (unlikely (palette >= cpal.get_palette_count ()))
S
Sascha Brawer 已提交
170
  {
E
Minor  
Ebrahim Byagowi 已提交
171
    if (colors_count) *colors_count = 0;
S
Sascha Brawer 已提交
172 173 174
    return 0;
  }

K
Khaled Hosny 已提交
175
  unsigned int num_results = 0;
E
Minor  
Ebrahim Byagowi 已提交
176
  if (colors_count)
K
Khaled Hosny 已提交
177
  {
E
Minor  
Ebrahim Byagowi 已提交
178 179 180
    unsigned int platte_count;
    platte_count = MIN<unsigned int>(*colors_count,
				     cpal.get_palette_entries_count () - start_offset);
K
Khaled Hosny 已提交
181 182 183
    for (unsigned int i = 0; i < platte_count; i++)
    {
      if (cpal.get_color_record_argb(start_offset + i, palette, &colors[num_results]))
E
Minor  
Ebrahim Byagowi 已提交
184
	++num_results;
K
Khaled Hosny 已提交
185 186
    }
  }
S
Sascha Brawer 已提交
187

E
Minor  
Ebrahim Byagowi 已提交
188
  if (likely (colors_count)) *colors_count = num_results;
K
Khaled Hosny 已提交
189 190 191
  return cpal.get_palette_entries_count ();
}

192 193
/**
 * hb_ot_color_get_color_layers:
E
Minor  
Ebrahim Byagowi 已提交
194
 * @face: a font face.
195 196 197
 * @gid:
 * @start_offset:
 * @count:  (inout) (optional):
K
Khaled Hosny 已提交
198 199
 * @gids: (array length=count) (out) (optional):
 * @color_indices: (array length=count) (out) (optional):
200 201 202 203 204
 *
 * Returns:
 *
 * Since: REPLACEME
 */
K
Khaled Hosny 已提交
205 206 207 208
unsigned int
hb_ot_color_get_color_layers (hb_face_t        *face,
			      hb_codepoint_t    gid,
			      unsigned int      start_offset,
E
Minor  
Ebrahim Byagowi 已提交
209 210
			      unsigned int     *count         /* IN/OUT.  May be NULL. */,
			      hb_codepoint_t   *gids          /* OUT.     May be NULL. */,
K
Khaled Hosny 已提交
211 212 213
			      unsigned int     *color_indices /* OUT.     May be NULL. */)
{
  const OT::COLR& colr = _get_colr (face);
S
Sascha Brawer 已提交
214
  unsigned int num_results = 0;
K
Khaled Hosny 已提交
215 216
  unsigned int start_layer_index, num_layers = 0;
  if (colr.get_base_glyph_record (gid, &start_layer_index, &num_layers))
S
Sascha Brawer 已提交
217
  {
K
Khaled Hosny 已提交
218
    if (count)
S
Sascha Brawer 已提交
219
    {
K
Khaled Hosny 已提交
220 221 222 223 224 225 226
      unsigned int layer_count = MIN<unsigned int>(*count, num_layers - start_offset);
      for (unsigned int i = 0; i < layer_count; i++)
      {
	if (colr.get_layer_record (start_layer_index + start_offset + i,
				   &gids[num_results], &color_indices[num_results]))
	  ++num_results;
      }
S
Sascha Brawer 已提交
227 228 229
    }
  }

K
Khaled Hosny 已提交
230 231
  if (likely (count)) *count = num_results;
  return num_layers;
S
Sascha Brawer 已提交
232
}
233 234 235

/**
 * hb_ot_color_get_palette_flags:
E
Minor  
Ebrahim Byagowi 已提交
236
 * @face:    a font face
237 238 239 240 241 242
 * @palette: the index of the color palette whose flags are being requested
 *
 * Returns: the flags for the requested color palette.  If @face has no colors,
 * or if @palette is not between 0 and hb_ot_color_get_palette_count(),
 * the result is #HB_OT_COLOR_PALETTE_FLAG_DEFAULT.
 *
243
 * Since: REPLACEME
244 245 246 247 248 249 250
 */
hb_ot_color_palette_flags_t
hb_ot_color_get_palette_flags (hb_face_t *face, unsigned int palette)
{
  const OT::CPAL& cpal = _get_cpal(face);
  return cpal.get_palette_flags (palette);
}