2 * Copyright © 2016 Google, Inc.
3 * Copyright © 2018 Ebrahim Byagowi
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Google Author(s): Sascha Brawer
28 #ifndef HB_OT_COLOR_CPAL_TABLE_HH
29 #define HB_OT_COLOR_CPAL_TABLE_HH
31 #include "hb-open-type-private.hh"
35 * Following parts to be moved to a public header.
40 * ARGB data type for holding color values.
44 typedef uint32_t hb_ot_color_t;
48 * hb_ot_color_palette_flags_t:
49 * @HB_OT_COLOR_PALETTE_FLAG_DEFAULT: default indicating that there is nothing special to note about a color palette.
50 * @HB_OT_COLOR_PALETTE_FLAG_FOR_LIGHT_BACKGROUND: flag indicating that the color palette is suitable for rendering text on light background.
51 * @HB_OT_COLOR_PALETTE_FLAG_FOR_DARK_BACKGROUND: flag indicating that the color palette is suitable for rendering text on dark background.
55 typedef enum { /*< flags >*/
56 HB_OT_COLOR_PALETTE_FLAG_DEFAULT = 0x00000000u,
57 HB_OT_COLOR_PALETTE_FLAG_FOR_LIGHT_BACKGROUND = 0x00000001u,
58 HB_OT_COLOR_PALETTE_FLAG_FOR_DARK_BACKGROUND = 0x00000002u,
59 } hb_ot_color_palette_flags_t;
61 // HB_EXTERN unsigned int
62 // hb_ot_color_get_palette_count (hb_face_t *face);
64 // HB_EXTERN unsigned int
65 // hb_ot_color_get_palette_name_id (hb_face_t *face, unsigned int palette);
67 // HB_EXTERN hb_ot_color_palette_flags_t
68 // hb_ot_color_get_palette_flags (hb_face_t *face, unsigned int palette);
70 // HB_EXTERN unsigned int
71 // hb_ot_color_get_palette_colors (hb_face_t *face,
72 // unsigned int palette, /* default=0 */
73 // unsigned int start_offset,
74 // unsigned int *color_count /* IN/OUT */,
75 // hb_ot_color_t *colors /* OUT */);
82 * CPAL -- Color Palette
83 * https://docs.microsoft.com/en-us/typography/opentype/spec/cpal
85 #define HB_OT_TAG_CPAL HB_TAG('C','P','A','L')
96 sanitize (hb_sanitize_context_t *c, const void *base, unsigned int palettes) const
98 TRACE_SANITIZE (this);
99 return_trace (c->check_struct (this) &&
100 (base+paletteFlagsZ).sanitize (c, palettes) &&
101 (base+paletteLabelZ).sanitize (c, palettes) &&
102 (base+paletteEntryLabelZ).sanitize (c, palettes));
106 inline hb_ot_color_palette_flags_t
107 get_palette_flags (const void *base, unsigned int palette) const
109 // range checked at the CPAL caller
110 return (hb_ot_color_palette_flags_t) (uint32_t) (base+paletteFlagsZ)[palette];
114 get_palette_name_id (const void *base, unsigned int palette) const
116 // range checked at the CPAL caller
117 return (base+paletteLabelZ)[palette];
121 LOffsetTo<UnsizedArrayOf<HBUINT32> >
122 paletteFlagsZ; /* Offset from the beginning of CPAL table to
123 * the Palette Type Array. Set to 0 if no array
125 LOffsetTo<UnsizedArrayOf<HBUINT16> >
126 paletteLabelZ; /* Offset from the beginning of CPAL table to
127 * the Palette Labels Array. Set to 0 if no
128 * array is provided. */
129 LOffsetTo<UnsizedArrayOf<HBUINT16> >
130 paletteEntryLabelZ; /* Offset from the beginning of CPAL table to
131 * the Palette Entry Label Array. Set to 0
132 * if no array is provided. */
134 DEFINE_SIZE_STATIC (12);
137 typedef HBUINT32 BGRAColor;
141 static const hb_tag_t tableTag = HB_OT_TAG_CPAL;
143 inline bool sanitize (hb_sanitize_context_t *c) const
145 TRACE_SANITIZE (this);
146 if (unlikely (!(c->check_struct (this) && // it checks colorRecordIndices also
148 (this+colorRecordsZ).sanitize (c, numColorRecords))))
149 return_trace (false);
151 // Check for indices sanity so no need for doing it runtime
152 for (unsigned int i = 0; i < numPalettes; ++i)
153 if (unlikely (colorRecordIndicesZ[i] + numPaletteEntries > numColorRecords))
154 return_trace (false);
156 // If version is zero, we are done here; otherwise we need to check tail also
160 const CPALV1Tail &v1 = StructAfter<CPALV1Tail> (*this);
161 return_trace (likely (v1.sanitize (c, this, numPalettes)));
164 inline unsigned int get_size (void) const
166 return min_size + numPalettes * sizeof (HBUINT16);
169 inline hb_ot_color_palette_flags_t get_palette_flags (unsigned int palette) const
171 if (unlikely (version == 0 || palette >= numPalettes))
172 return HB_OT_COLOR_PALETTE_FLAG_DEFAULT;
174 const CPALV1Tail& cpal1 = StructAfter<CPALV1Tail> (*this);
175 return cpal1.get_palette_flags (this, palette);
178 inline unsigned int get_palette_name_id (unsigned int palette) const
180 if (unlikely (version == 0 || palette >= numPalettes))
183 const CPALV1Tail& cpal1 = StructAfter<CPALV1Tail> (*this);
184 return cpal1.get_palette_name_id (this, palette);
187 inline unsigned int get_palette_count () const
193 get_color_record_argb (unsigned int color_index, unsigned int palette) const
195 if (unlikely (color_index >= numPaletteEntries || palette >= numPalettes))
198 // No need for more range check as it is already done on #sanitize
199 const UnsizedArrayOf<BGRAColor>& color_records = this+colorRecordsZ;
200 return color_records[colorRecordIndicesZ[palette] + color_index];
204 HBUINT16 version; /* Table version number */
206 HBUINT16 numPaletteEntries; /* Number of palette entries in each palette. */
207 HBUINT16 numPalettes; /* Number of palettes in the table. */
208 HBUINT16 numColorRecords; /* Total number of color records, combined for
210 LOffsetTo<UnsizedArrayOf<BGRAColor> >
211 colorRecordsZ; /* Offset from the beginning of CPAL table to
212 * the first ColorRecord. */
213 UnsizedArrayOf<HBUINT16>
214 colorRecordIndicesZ; /* Index of each palette’s first color record in
215 * the combined color record array. */
218 DEFINE_SIZE_ARRAY (12, colorRecordIndicesZ);
224 #endif /* HB_OT_COLOR_CPAL_TABLE_HH */