harfbuzz/src/hb-ot-color-cpal-table.hh

194 lines
6.4 KiB
C++
Raw Normal View History

2016-04-25 18:20:57 +02:00
/*
* Copyright © 2016 Google, Inc.
* Copyright © 2018 Ebrahim Byagowi
2016-04-25 18:20:57 +02: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
*/
#ifndef HB_OT_COLOR_CPAL_TABLE_HH
#define HB_OT_COLOR_CPAL_TABLE_HH
2016-04-25 18:20:57 +02:00
#include "hb-open-type.hh"
2018-05-01 17:16:46 +02:00
#include "hb-ot-color.h"
#include "hb-ot-name.h"
2016-04-25 18:20:57 +02:00
/*
* CPAL -- Color Palette
* https://docs.microsoft.com/en-us/typography/opentype/spec/cpal
*/
#define HB_OT_TAG_CPAL HB_TAG('C','P','A','L')
2018-02-28 10:49:40 +01:00
namespace OT {
2016-04-25 18:20:57 +02:00
struct CPALV1Tail
{
friend struct CPAL;
private:
hb_ot_color_palette_flags_t get_palette_flags (const void *base,
unsigned int palette_index,
unsigned int palette_count) const
{
if (!paletteFlagsZ) return HB_OT_COLOR_PALETTE_FLAG_DEFAULT;
return (hb_ot_color_palette_flags_t) (uint32_t)
(base+paletteFlagsZ).as_array (palette_count)[palette_index];
}
hb_ot_name_id_t get_palette_name_id (const void *base,
unsigned int palette_index,
unsigned int palette_count) const
{
if (!paletteLabelsZ) return HB_OT_NAME_ID_INVALID;
return (base+paletteLabelsZ).as_array (palette_count)[palette_index];
2016-04-25 18:20:57 +02:00
}
hb_ot_name_id_t get_color_name_id (const void *base,
unsigned int color_index,
unsigned int color_count) const
{
if (!colorLabelsZ) return HB_OT_NAME_ID_INVALID;
return (base+colorLabelsZ).as_array (color_count)[color_index];
2018-10-22 23:51:40 +02:00
}
public:
bool sanitize (hb_sanitize_context_t *c,
const void *base,
unsigned int palette_count,
unsigned int color_count) const
2018-10-22 23:51:40 +02:00
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this) &&
(!paletteFlagsZ || (base+paletteFlagsZ).sanitize (c, palette_count)) &&
(!paletteLabelsZ || (base+paletteLabelsZ).sanitize (c, palette_count)) &&
(!colorLabelsZ || (base+colorLabelsZ).sanitize (c, color_count)));
}
protected:
2019-01-18 00:17:04 +01:00
LNNOffsetTo<UnsizedArrayOf<HBUINT32> >
paletteFlagsZ; /* Offset from the beginning of CPAL table to
* the Palette Type Array. Set to 0 if no array
* is provided. */
2019-01-18 00:17:04 +01:00
LNNOffsetTo<UnsizedArrayOf<NameID> >
paletteLabelsZ; /* Offset from the beginning of CPAL table to
* the palette labels array. Set to 0 if no
* array is provided. */
2019-01-18 00:17:04 +01:00
LNNOffsetTo<UnsizedArrayOf<NameID> >
colorLabelsZ; /* Offset from the beginning of CPAL table to
* the color labels array. Set to 0
* if no array is provided. */
2016-04-25 18:20:57 +02:00
public:
DEFINE_SIZE_STATIC (12);
2016-04-25 18:20:57 +02:00
};
typedef HBUINT32 BGRAColor;
2016-04-25 18:20:57 +02:00
struct CPAL
{
static constexpr hb_tag_t tableTag = HB_OT_TAG_CPAL;
2016-04-25 18:20:57 +02:00
bool has_data () const { return numPalettes; }
unsigned int get_size () const
{ return min_size + numPalettes * sizeof (colorRecordIndicesZ[0]); }
2018-10-22 23:51:40 +02:00
unsigned int get_palette_count () const { return numPalettes; }
unsigned int get_color_count () const { return numColors; }
2016-04-25 18:20:57 +02:00
hb_ot_color_palette_flags_t get_palette_flags (unsigned int palette_index) const
2018-10-23 01:30:30 +02:00
{ return v1 ().get_palette_flags (this, palette_index, numPalettes); }
hb_ot_name_id_t get_palette_name_id (unsigned int palette_index) const
2018-10-23 01:30:30 +02:00
{ return v1 ().get_palette_name_id (this, palette_index, numPalettes); }
hb_ot_name_id_t get_color_name_id (unsigned int color_index) const
2018-10-23 01:30:30 +02:00
{ return v1 ().get_color_name_id (this, color_index, numColors); }
unsigned int get_palette_colors (unsigned int palette_index,
unsigned int start_offset,
unsigned int *color_count, /* IN/OUT. May be NULL. */
hb_color_t *colors /* OUT. May be NULL. */) const
{
if (unlikely (palette_index >= numPalettes))
{
if (color_count) *color_count = 0;
return 0;
}
unsigned int start_index = colorRecordIndicesZ[palette_index];
hb_array_t<const BGRAColor> all_colors ((this+colorRecordsZ).arrayZ, numColorRecords);
hb_array_t<const BGRAColor> palette_colors = all_colors.sub_array (start_index,
numColors);
if (color_count)
{
hb_array_t<const BGRAColor> segment_colors = palette_colors.sub_array (start_offset, *color_count);
/* Always return numColors colors per palette even if it has out-of-bounds start index. */
unsigned int count = MIN<unsigned int> (MAX<int> (numColors - start_offset, 0), *color_count);
*color_count = count;
for (unsigned int i = 0; i < count; i++)
colors[i] = segment_colors[i]; /* Bound-checked read. */
}
return numColors;
}
2018-10-23 01:30:30 +02:00
private:
const CPALV1Tail& v1 () const
2018-10-23 01:30:30 +02:00
{
if (version == 0) return Null(CPALV1Tail);
return StructAfter<CPALV1Tail> (*this);
}
public:
bool sanitize (hb_sanitize_context_t *c) const
2018-10-22 23:51:40 +02:00
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this) &&
(this+colorRecordsZ).sanitize (c, numColorRecords) &&
colorRecordIndicesZ.sanitize (c, numPalettes) &&
(version == 0 || v1 ().sanitize (c, this, numPalettes, numColors)));
2018-10-22 23:51:40 +02:00
}
protected:
HBUINT16 version; /* Table version number */
2016-04-25 18:20:57 +02:00
/* Version 0 */
2018-10-22 23:46:21 +02:00
HBUINT16 numColors; /* Number of colors in each palette. */
HBUINT16 numPalettes; /* Number of palettes in the table. */
HBUINT16 numColorRecords; /* Total number of color records, combined for
* all palettes. */
2019-01-18 00:17:04 +01:00
LNNOffsetTo<UnsizedArrayOf<BGRAColor> >
colorRecordsZ; /* Offset from the beginning of CPAL table to
* the first ColorRecord. */
UnsizedArrayOf<HBUINT16>
colorRecordIndicesZ; /* Index of each palettes first color record in
* the combined color record array. */
/*CPALV1Tail v1;*/
2016-04-25 18:20:57 +02:00
public:
DEFINE_SIZE_ARRAY (12, colorRecordIndicesZ);
2016-04-25 18:20:57 +02:00
};
} /* namespace OT */
#endif /* HB_OT_COLOR_CPAL_TABLE_HH */