[COLR/CPAL] Provide enough helper for rasterization (#855)

This commit is contained in:
Ebrahim Byagowi 2018-03-03 22:00:29 +03:30 committed by GitHub
parent 432758a7ac
commit 48ed15a2bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 6 deletions

View File

@ -39,12 +39,15 @@ namespace OT {
struct LayerRecord struct LayerRecord
{ {
friend struct COLR;
inline bool sanitize (hb_sanitize_context_t *c) const inline bool sanitize (hb_sanitize_context_t *c) const
{ {
TRACE_SANITIZE (this); TRACE_SANITIZE (this);
return_trace (c->check_struct (this)); return_trace (c->check_struct (this));
} }
protected:
GlyphID gID; /* Glyph ID of layer glyph */ GlyphID gID; /* Glyph ID of layer glyph */
HBUINT16 paletteIndex; /* Index value to use with a selected color palette */ HBUINT16 paletteIndex; /* Index value to use with a selected color palette */
public: public:
@ -53,12 +56,15 @@ struct LayerRecord
struct BaseGlyphRecord struct BaseGlyphRecord
{ {
friend struct COLR;
inline bool sanitize (hb_sanitize_context_t *c) const inline bool sanitize (hb_sanitize_context_t *c) const
{ {
TRACE_SANITIZE (this); TRACE_SANITIZE (this);
return_trace (c->check_struct (this)); return_trace (c->check_struct (this));
} }
protected:
GlyphID gID; /* Glyph ID of reference glyph */ GlyphID gID; /* Glyph ID of reference glyph */
HBUINT16 firstLayerIndex; /* Index to the layer record */ HBUINT16 firstLayerIndex; /* Index to the layer record */
HBUINT16 numLayers; /* Number of color layers associated with this glyph */ HBUINT16 numLayers; /* Number of color layers associated with this glyph */
@ -73,9 +79,44 @@ struct COLR
inline bool sanitize (hb_sanitize_context_t *c) const inline bool sanitize (hb_sanitize_context_t *c) const
{ {
TRACE_SANITIZE (this); TRACE_SANITIZE (this);
return_trace (c->check_struct (this) && if (!(c->check_struct (this) &&
c->check_array ((const void*) &layerRecordsOffset, sizeof (LayerRecord), numLayerRecords) && c->check_array ((const void*) &layerRecordsOffset, sizeof (LayerRecord), numLayerRecords) &&
c->check_array ((const void*) &baseGlyphRecords, sizeof (BaseGlyphRecord), numBaseGlyphRecords)); c->check_array ((const void*) &baseGlyphRecords, sizeof (BaseGlyphRecord), numBaseGlyphRecords)))
return_trace (false);
const BaseGlyphRecord* base_glyph_records = &baseGlyphRecords (this);
for (unsigned int i = 0; i < numBaseGlyphRecords; ++i)
if (base_glyph_records[i].firstLayerIndex +
base_glyph_records[i].numLayers > numLayerRecords)
return_trace (false);
/* XXX values of LayerRecord structs should be sanitized */
return_trace (true);
}
inline const bool get_base_glyph_record (
hb_codepoint_t glyph_id, unsigned int &first_layer, unsigned int &num_layers) const
{
/* TODO replace with bsearch */
const BaseGlyphRecord* base_glyph_records = &baseGlyphRecords (this);
unsigned int records = numBaseGlyphRecords;
for (unsigned int i = 0; i < records; ++i)
if (base_glyph_records[i].gID == glyph_id)
{
first_layer = base_glyph_records[i].firstLayerIndex;
num_layers = base_glyph_records[i].numLayers;
return true;
}
return false;
}
inline void get_layer_record (int layer,
hb_codepoint_t &glyph_id, unsigned int &palette_index) const
{
const LayerRecord* records = &layerRecordsOffset (this);
glyph_id = records[layer].gID;
palette_index = records[layer].paletteIndex;
} }
protected: protected:

View File

@ -42,12 +42,15 @@ namespace OT {
struct ColorRecord struct ColorRecord
{ {
friend struct CPAL;
inline bool sanitize (hb_sanitize_context_t *c) const inline bool sanitize (hb_sanitize_context_t *c) const
{ {
TRACE_SANITIZE (this); TRACE_SANITIZE (this);
return_trace (true); return_trace (true);
} }
protected:
HBUINT8 blue; HBUINT8 blue;
HBUINT8 green; HBUINT8 green;
HBUINT8 red; HBUINT8 red;
@ -74,14 +77,14 @@ struct CPALV1Tail
inline hb_ot_color_palette_flags_t inline hb_ot_color_palette_flags_t
get_palette_flags (const void *base, unsigned int palette) const get_palette_flags (const void *base, unsigned int palette) const
{ {
const HBUINT32* flags = (const HBUINT32*) (const void*) &paletteFlags (base); const HBUINT32* flags = &paletteFlags (base);
return (hb_ot_color_palette_flags_t) (uint32_t) flags[palette]; return (hb_ot_color_palette_flags_t) (uint32_t) flags[palette];
} }
inline unsigned int inline unsigned int
get_palette_name_id (const void *base, unsigned int palette) const get_palette_name_id (const void *base, unsigned int palette) const
{ {
const HBUINT16* name_ids = (const HBUINT16*) (const void*) &paletteLabel (base); const HBUINT16* name_ids = &paletteLabel (base);
return name_ids[palette]; return name_ids[palette];
} }
@ -148,9 +151,18 @@ struct CPAL
return numPalettes; return numPalettes;
} }
inline void get_color_record (int palette_index, uint8_t &r, uint8_t &g,
uint8_t &b, uint8_t &a) const
{
// We should check if palette_index is in range as it is not done on COLR sanitization
r = colorRecords[palette_index].red;
g = colorRecords[palette_index].green;
b = colorRecords[palette_index].blue;
a = colorRecords[palette_index].alpha;
}
protected: protected:
HBUINT16 version; HBUINT16 version;
/* Version 0 */ /* Version 0 */
HBUINT16 numPaletteEntries; HBUINT16 numPaletteEntries;
HBUINT16 numPalettes; HBUINT16 numPalettes;