2016-04-25 16:20:57 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright © 2016 Google, Inc.
|
2018-02-27 19:26:17 +00:00
|
|
|
|
* Copyright © 2018 Ebrahim Byagowi
|
2016-04-25 16:20:57 +00:00
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2018-03-03 19:13:23 +00:00
|
|
|
|
#ifndef HB_OT_COLOR_CPAL_TABLE_HH
|
|
|
|
|
#define HB_OT_COLOR_CPAL_TABLE_HH
|
2016-04-25 16:20:57 +00:00
|
|
|
|
|
2018-08-26 05:36:36 +00:00
|
|
|
|
#include "hb-open-type.hh"
|
2018-05-01 15:16:46 +00:00
|
|
|
|
#include "hb-ot-color.h"
|
2018-10-21 04:56:40 +00:00
|
|
|
|
#include "hb-ot-name.h"
|
2016-04-25 16:20:57 +00:00
|
|
|
|
|
2018-03-07 14:51:42 +00:00
|
|
|
|
|
|
|
|
|
/*
|
2018-10-21 04:56:40 +00:00
|
|
|
|
* CPAL -- Color Palette
|
|
|
|
|
* https://docs.microsoft.com/en-us/typography/opentype/spec/cpal
|
2018-03-07 14:51:42 +00:00
|
|
|
|
*/
|
2018-10-21 04:56:40 +00:00
|
|
|
|
#define HB_OT_TAG_CPAL HB_TAG('C','P','A','L')
|
2018-03-07 14:51:42 +00:00
|
|
|
|
|
2018-04-12 09:08:19 +00:00
|
|
|
|
|
2018-02-28 09:49:40 +00:00
|
|
|
|
namespace OT {
|
|
|
|
|
|
|
|
|
|
|
2016-04-25 16:20:57 +00:00
|
|
|
|
struct CPALV1Tail
|
|
|
|
|
{
|
2018-02-27 19:26:17 +00:00
|
|
|
|
friend struct CPAL;
|
|
|
|
|
|
2018-04-09 20:23:50 +00:00
|
|
|
|
inline bool
|
2018-10-21 06:14:16 +00:00
|
|
|
|
sanitize (hb_sanitize_context_t *c, const void *base,
|
2018-10-22 21:43:12 +00:00
|
|
|
|
unsigned int palette_count, unsigned int paletteEntries) const
|
2016-04-25 16:20:57 +00:00
|
|
|
|
{
|
|
|
|
|
TRACE_SANITIZE (this);
|
2018-04-09 20:23:50 +00:00
|
|
|
|
return_trace (c->check_struct (this) &&
|
2018-10-22 21:43:12 +00:00
|
|
|
|
(base+paletteFlagsZ).sanitize (c, palette_count) &&
|
|
|
|
|
(base+paletteLabelZ).sanitize (c, palette_count) &&
|
2018-10-21 06:14:16 +00:00
|
|
|
|
(base+paletteEntryLabelZ).sanitize (c, paletteEntries));
|
2018-02-27 19:26:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
inline hb_ot_color_palette_flags_t
|
2018-10-22 21:43:12 +00:00
|
|
|
|
get_palette_flags (const void *base, unsigned int palette_index,
|
2018-10-21 06:14:16 +00:00
|
|
|
|
unsigned int palettes_count) const
|
2018-02-27 19:26:17 +00:00
|
|
|
|
{
|
2018-10-22 21:43:12 +00:00
|
|
|
|
if (unlikely (palette_index >= palettes_count))
|
2018-10-21 06:14:16 +00:00
|
|
|
|
return HB_OT_COLOR_PALETTE_FLAG_DEFAULT;
|
|
|
|
|
|
2018-10-22 21:43:12 +00:00
|
|
|
|
return (hb_ot_color_palette_flags_t) (uint32_t) (base+paletteFlagsZ)[palette_index];
|
2018-02-27 19:26:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline unsigned int
|
2018-10-22 21:43:12 +00:00
|
|
|
|
get_palette_name_id (const void *base, unsigned int palette_index,
|
2018-10-21 06:14:16 +00:00
|
|
|
|
unsigned int palettes_count) const
|
2018-02-27 19:26:17 +00:00
|
|
|
|
{
|
2018-10-22 21:43:12 +00:00
|
|
|
|
if (unlikely (palette_index >= palettes_count))
|
2018-10-21 06:14:16 +00:00
|
|
|
|
return HB_NAME_ID_INVALID;
|
|
|
|
|
|
2018-10-22 21:43:12 +00:00
|
|
|
|
return (base+paletteLabelZ)[palette_index];
|
2016-04-25 16:20:57 +00:00
|
|
|
|
}
|
2018-02-27 19:26:17 +00:00
|
|
|
|
|
2018-10-21 06:14:16 +00:00
|
|
|
|
inline unsigned int
|
|
|
|
|
get_palette_entry_name_id (const void *base, unsigned int palette_entry,
|
|
|
|
|
unsigned int palettes_entries_count) const
|
|
|
|
|
{
|
|
|
|
|
if (unlikely (palette_entry >= palettes_entries_count))
|
|
|
|
|
return HB_NAME_ID_INVALID;
|
|
|
|
|
|
|
|
|
|
return (base+paletteEntryLabelZ)[palette_entry];
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-27 19:26:17 +00:00
|
|
|
|
protected:
|
2018-09-15 17:43:33 +00:00
|
|
|
|
LOffsetTo<UnsizedArrayOf<HBUINT32>, false>
|
2018-04-09 20:23:50 +00:00
|
|
|
|
paletteFlagsZ; /* Offset from the beginning of CPAL table to
|
|
|
|
|
* the Palette Type Array. Set to 0 if no array
|
|
|
|
|
* is provided. */
|
2018-09-15 17:43:33 +00:00
|
|
|
|
LOffsetTo<UnsizedArrayOf<HBUINT16>, false>
|
2018-04-09 20:23:50 +00:00
|
|
|
|
paletteLabelZ; /* Offset from the beginning of CPAL table to
|
|
|
|
|
* the Palette Labels Array. Set to 0 if no
|
|
|
|
|
* array is provided. */
|
2018-10-21 06:14:16 +00:00
|
|
|
|
LOffsetTo<UnsizedArrayOf<HBUINT16>, false>
|
|
|
|
|
paletteEntryLabelZ; /* Offset from the beginning of CPAL table to
|
2018-04-09 20:23:50 +00:00
|
|
|
|
* the Palette Entry Label Array. Set to 0
|
|
|
|
|
* if no array is provided. */
|
2016-04-25 16:20:57 +00:00
|
|
|
|
public:
|
2018-10-21 06:14:16 +00:00
|
|
|
|
DEFINE_SIZE_STATIC (12);
|
2016-04-25 16:20:57 +00:00
|
|
|
|
};
|
|
|
|
|
|
2018-03-06 13:11:08 +00:00
|
|
|
|
typedef HBUINT32 BGRAColor;
|
|
|
|
|
|
2016-04-25 16:20:57 +00:00
|
|
|
|
struct CPAL
|
|
|
|
|
{
|
|
|
|
|
static const hb_tag_t tableTag = HB_OT_TAG_CPAL;
|
|
|
|
|
|
2018-10-22 02:23:11 +00:00
|
|
|
|
inline bool has_data (void) const { return numPalettes; }
|
|
|
|
|
|
2016-04-25 16:20:57 +00:00
|
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
|
{
|
|
|
|
|
TRACE_SANITIZE (this);
|
2018-10-21 08:16:51 +00:00
|
|
|
|
if (unlikely (!(c->check_struct (this) && /* it checks colorRecordIndices also
|
|
|
|
|
* See #get_size */
|
2018-04-18 07:39:37 +00:00
|
|
|
|
(this+colorRecordsZ).sanitize (c, numColorRecords))))
|
2018-02-27 19:26:17 +00:00
|
|
|
|
return_trace (false);
|
|
|
|
|
|
2018-10-21 08:16:51 +00:00
|
|
|
|
/* Check for indices sanity so no need for doing it runtime */
|
2018-03-06 13:11:08 +00:00
|
|
|
|
for (unsigned int i = 0; i < numPalettes; ++i)
|
2018-04-18 07:39:37 +00:00
|
|
|
|
if (unlikely (colorRecordIndicesZ[i] + numPaletteEntries > numColorRecords))
|
2018-04-11 12:43:20 +00:00
|
|
|
|
return_trace (false);
|
2018-02-27 19:26:17 +00:00
|
|
|
|
|
2018-10-21 08:16:51 +00:00
|
|
|
|
/* If version is zero, we are done here; otherwise we need to check tail also */
|
2018-03-01 06:57:18 +00:00
|
|
|
|
if (version == 0)
|
2016-04-25 16:20:57 +00:00
|
|
|
|
return_trace (true);
|
2018-03-01 06:57:18 +00:00
|
|
|
|
|
|
|
|
|
const CPALV1Tail &v1 = StructAfter<CPALV1Tail> (*this);
|
2018-10-21 06:14:16 +00:00
|
|
|
|
return_trace (likely (v1.sanitize (c, this, numPalettes, numPaletteEntries)));
|
2016-04-25 16:20:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-27 19:26:17 +00:00
|
|
|
|
inline unsigned int get_size (void) const
|
|
|
|
|
{
|
2018-03-06 13:11:08 +00:00
|
|
|
|
return min_size + numPalettes * sizeof (HBUINT16);
|
2016-04-25 16:20:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-22 21:43:12 +00:00
|
|
|
|
inline hb_ot_color_palette_flags_t get_palette_flags (unsigned int palette_index) const
|
2018-02-27 19:26:17 +00:00
|
|
|
|
{
|
2018-10-21 06:14:16 +00:00
|
|
|
|
if (unlikely (version == 0))
|
2018-02-27 19:26:17 +00:00
|
|
|
|
return HB_OT_COLOR_PALETTE_FLAG_DEFAULT;
|
|
|
|
|
|
|
|
|
|
const CPALV1Tail& cpal1 = StructAfter<CPALV1Tail> (*this);
|
2018-10-22 21:43:12 +00:00
|
|
|
|
return cpal1.get_palette_flags (this, palette_index, numPalettes);
|
2018-02-27 19:26:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-22 21:43:12 +00:00
|
|
|
|
inline unsigned int get_palette_name_id (unsigned int palette_index) const
|
2018-02-27 19:26:17 +00:00
|
|
|
|
{
|
2018-10-21 06:14:16 +00:00
|
|
|
|
if (unlikely (version == 0))
|
2018-10-21 04:56:40 +00:00
|
|
|
|
return HB_NAME_ID_INVALID;
|
2018-02-27 19:26:17 +00:00
|
|
|
|
|
|
|
|
|
const CPALV1Tail& cpal1 = StructAfter<CPALV1Tail> (*this);
|
2018-10-22 21:43:12 +00:00
|
|
|
|
return cpal1.get_palette_name_id (this, palette_index, numPalettes);
|
2018-02-27 19:26:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-21 06:14:16 +00:00
|
|
|
|
inline unsigned int get_palette_entry_name_id (unsigned int palette_entry) const
|
2018-02-27 19:26:17 +00:00
|
|
|
|
{
|
2018-10-21 06:14:16 +00:00
|
|
|
|
if (unlikely (version == 0))
|
|
|
|
|
return HB_NAME_ID_INVALID;
|
|
|
|
|
|
|
|
|
|
const CPALV1Tail& cpal1 = StructAfter<CPALV1Tail> (*this);
|
|
|
|
|
return cpal1.get_palette_entry_name_id (this, palette_entry, numPaletteEntries);
|
2018-02-27 19:26:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-21 06:14:16 +00:00
|
|
|
|
inline unsigned int get_palette_count () const
|
|
|
|
|
{ return numPalettes; }
|
|
|
|
|
|
2018-05-01 15:16:46 +00:00
|
|
|
|
inline unsigned int get_palette_entries_count () const
|
2018-10-21 06:14:16 +00:00
|
|
|
|
{ return numPaletteEntries; }
|
2018-05-01 15:16:46 +00:00
|
|
|
|
|
|
|
|
|
bool
|
2018-10-22 21:43:12 +00:00
|
|
|
|
get_color_record_argb (unsigned int color_index, unsigned int palette_index, hb_color_t* color) const
|
2018-03-03 18:30:29 +00:00
|
|
|
|
{
|
2018-10-22 21:43:12 +00:00
|
|
|
|
if (unlikely (color_index >= numPaletteEntries || palette_index >= numPalettes))
|
2018-05-01 15:16:46 +00:00
|
|
|
|
return false;
|
2018-03-06 13:11:08 +00:00
|
|
|
|
|
2018-10-21 08:16:51 +00:00
|
|
|
|
/* No need for more range check as it is already done on #sanitize */
|
2018-04-18 07:39:37 +00:00
|
|
|
|
const UnsizedArrayOf<BGRAColor>& color_records = this+colorRecordsZ;
|
2018-05-01 15:16:46 +00:00
|
|
|
|
if (color)
|
2018-10-22 21:43:12 +00:00
|
|
|
|
*color = color_records[colorRecordIndicesZ[palette_index] + color_index];
|
2018-05-01 15:16:46 +00:00
|
|
|
|
return true;
|
2018-03-03 18:30:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-27 19:26:17 +00:00
|
|
|
|
protected:
|
2018-04-09 20:23:50 +00:00
|
|
|
|
HBUINT16 version; /* Table version number */
|
2016-04-25 16:20:57 +00:00
|
|
|
|
/* Version 0 */
|
2018-04-09 20:23:50 +00:00
|
|
|
|
HBUINT16 numPaletteEntries; /* Number of palette entries in each palette. */
|
|
|
|
|
HBUINT16 numPalettes; /* Number of palettes in the table. */
|
|
|
|
|
HBUINT16 numColorRecords; /* Total number of color records, combined for
|
|
|
|
|
* all palettes. */
|
2018-09-15 17:43:33 +00:00
|
|
|
|
LOffsetTo<UnsizedArrayOf<BGRAColor>, false>
|
2018-04-09 20:23:50 +00:00
|
|
|
|
colorRecordsZ; /* Offset from the beginning of CPAL table to
|
|
|
|
|
* the first ColorRecord. */
|
|
|
|
|
UnsizedArrayOf<HBUINT16>
|
|
|
|
|
colorRecordIndicesZ; /* Index of each palette’s first color record in
|
|
|
|
|
* the combined color record array. */
|
|
|
|
|
/*CPALV1Tail v1;*/
|
2016-04-25 16:20:57 +00:00
|
|
|
|
public:
|
2018-04-09 20:23:50 +00:00
|
|
|
|
DEFINE_SIZE_ARRAY (12, colorRecordIndicesZ);
|
2016-04-25 16:20:57 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} /* namespace OT */
|
|
|
|
|
|
|
|
|
|
|
2018-03-03 19:13:23 +00:00
|
|
|
|
#endif /* HB_OT_COLOR_CPAL_TABLE_HH */
|