hb-ot-color.cc 5.7 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"
S
Sascha Brawer 已提交
31 32 33 34 35
#include "hb-ot.h"

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

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

39
#if 0
S
Sascha Brawer 已提交
40
HB_MARK_AS_FLAG_T (hb_ot_color_palette_flags_t)
41
//HB_SHAPER_DATA_ENSURE_DECLARE(ot, face) Hmm?
S
Sascha Brawer 已提交
42 43


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

S
Sascha Brawer 已提交
51 52 53
static inline const OT::CPAL&
_get_cpal (hb_face_t *face)
{
B
Behdad Esfahbod 已提交
54
  if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::CPAL);
55
  return *(hb_ot_face_data (face)->cpal.get ());
S
Sascha Brawer 已提交
56 57 58 59 60 61 62 63 64 65
}


/**
 * hb_ot_color_get_palette_count:
 * @face: a font face.
 *
 * Returns: the number of color palettes in @face, or zero if @face has
 * no colors.
 *
66
 * Since: REPLACEME
S
Sascha Brawer 已提交
67 68 69 70
 */
unsigned int
hb_ot_color_get_palette_count (hb_face_t *face)
{
71 72
  const OT::CPAL& cpal = _get_cpal (face);
  return cpal.get_palette_count ();
S
Sascha Brawer 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86
}


/**
 * hb_ot_color_get_palette_name_id:
 * @face: a font face.
 * @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(),
87 88
 * the result is 0xFFFF. The implementation does not check whether
 * the returned palette name id is actually in @face's `name` table.
S
Sascha Brawer 已提交
89
 *
90
 * Since: REPLACEME
S
Sascha Brawer 已提交
91 92 93 94
 */
unsigned int
hb_ot_color_get_palette_name_id (hb_face_t *face, unsigned int palette)
{
95 96
  const OT::CPAL& cpal = _get_cpal (face);
  return cpal.get_palette_name_id (palette);
S
Sascha Brawer 已提交
97 98 99 100 101 102 103 104 105 106 107 108
}


/**
 * hb_ot_color_get_palette_flags:
 * @face: a font face
 * @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.
 *
109
 * Since: REPLACEME
S
Sascha Brawer 已提交
110 111 112 113 114
 */
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);
115
  return cpal.get_palette_flags (palette);
S
Sascha Brawer 已提交
116 117 118 119 120 121 122 123 124 125 126 127
}


/**
 * 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.
128
 * @colors: (array length=color_count) (optional):
S
Sascha Brawer 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
 *                an array of #hb_ot_color_t records. After calling
 *                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.
 *
144
 * Since: REPLACEME
S
Sascha Brawer 已提交
145 146 147 148 149 150 151 152 153
 */
unsigned int
hb_ot_color_get_palette_colors (hb_face_t       *face,
				unsigned int     palette, /* default=0 */
				unsigned int     start_offset,
				unsigned int    *color_count /* IN/OUT */,
				hb_ot_color_t   *colors /* OUT */)
{
  const OT::CPAL& cpal = _get_cpal(face);
154
  if (unlikely (palette >= cpal.numPalettes))
S
Sascha Brawer 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  {
    if (color_count) *color_count = 0;
    return 0;
  }

  const OT::ColorRecord* crec = &cpal.offsetFirstColorRecord (&cpal);
  crec += cpal.colorRecordIndices[palette];

  unsigned int num_results = 0;
  if (likely (color_count && colors))
  {
    for (unsigned int i = start_offset;
	 i < cpal.numPaletteEntries && num_results < *color_count; ++i)
    {
      hb_ot_color_t* result = &colors[num_results];
      result->red = crec[i].red;
      result->green = crec[i].green;
      result->blue = crec[i].blue;
      result->alpha = crec[i].alpha;
      ++num_results;
    }
  }

  if (likely (color_count)) *color_count = num_results;
  return cpal.numPaletteEntries;
}
181
#endif