From 228f96c9d09272c8f677935a640e75e173b817a3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 22 Oct 2018 16:55:12 -0700 Subject: [PATCH] [color] Finish reviewing / revamping CPAL Now to hb_color_t. --- src/hb-ot-color-cpal-table.hh | 52 ++++++++++++++++++----------------- src/hb-ot-color.cc | 33 ++++------------------ src/hb-ot-color.h | 10 +++---- 3 files changed, 38 insertions(+), 57 deletions(-) diff --git a/src/hb-ot-color-cpal-table.hh b/src/hb-ot-color-cpal-table.hh index 37e0db36d..3f2165c46 100644 --- a/src/hb-ot-color-cpal-table.hh +++ b/src/hb-ot-color-cpal-table.hh @@ -133,17 +133,30 @@ struct CPAL inline unsigned int get_color_name_id (unsigned int color_index) const { return v1 ().get_color_name_id (this, color_index, numColors); } - bool - get_color_record_argb (unsigned int color_index, unsigned int palette_index, hb_color_t* color) const + inline 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 (color_index >= numColors || palette_index >= numPalettes)) - return false; - - /* No need for more range check as it is already done on #sanitize */ - const UnsizedArrayOf& color_records = this+colorRecordsZ; - if (color) - *color = color_records[colorRecordIndicesZ[palette_index] + color_index]; - return true; + if (unlikely (palette_index >= numPalettes)) + { + if (color_count) *color_count = 0; + return 0; + } + unsigned int start_index = colorRecordIndicesZ[palette_index]; + hb_array_t all_colors ((this+colorRecordsZ).arrayZ, numColorRecords); + hb_array_t palette_colors = all_colors.sub_array (start_index, + numColors); + if (color_count) + { + hb_array_t 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 (MAX (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; } private: @@ -157,21 +170,10 @@ struct CPAL inline bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); - if (unlikely (!(c->check_struct (this) && - (this+colorRecordsZ).sanitize (c, numColorRecords)))) - return_trace (false); - - /* TODO */ - /* Check for indices sanity so no need for doing it runtime */ - for (unsigned int i = 0; i < numPalettes; ++i) - if (unlikely (colorRecordIndicesZ[i] + numColors > numColorRecords)) - return_trace (false); - - /* If version is zero, we are done here; otherwise we need to check tail also */ - if (version == 0) - return_trace (true); - - return_trace (likely (v1 ().sanitize (c, this, numPalettes, numColors))); + return_trace (c->check_struct (this) && + (this+colorRecordsZ).sanitize (c, numColorRecords) && + colorRecordIndicesZ.sanitize (c, numPalettes) && + (version == 0 || v1 ().sanitize (c, this, numPalettes, numColors))); } protected: diff --git a/src/hb-ot-color.cc b/src/hb-ot-color.cc index 59f7da72f..229b6e66f 100644 --- a/src/hb-ot-color.cc +++ b/src/hb-ot-color.cc @@ -189,34 +189,13 @@ hb_ot_color_palette_get_flags (hb_face_t *face, * Since: REPLACEME */ unsigned int -hb_ot_color_palette_get_colors (hb_face_t *face, - unsigned int palette_index, /* default=0 */ - unsigned int start_offset, - unsigned int *colors_count /* IN/OUT. May be NULL. */, - hb_color_t *colors /* OUT. May be NULL. */) +hb_ot_color_palette_get_colors (hb_face_t *face, + unsigned int palette_index, + unsigned int start_offset, + unsigned int *colors_count /* IN/OUT. May be NULL. */, + hb_color_t *colors /* OUT. May be NULL. */) { - const OT::CPAL& cpal = _get_cpal(face); - if (unlikely (palette_index >= cpal.get_palette_count ())) - { - if (colors_count) *colors_count = 0; - return 0; - } - - unsigned int num_results = 0; - if (colors_count) - { - unsigned int platte_count; - platte_count = MIN(*colors_count, - cpal.get_color_count () - start_offset); - for (unsigned int i = 0; i < platte_count; i++) - { - if (cpal.get_color_record_argb(start_offset + i, palette_index, &colors[num_results])) - ++num_results; - } - } - - if (likely (colors_count)) *colors_count = num_results; - return cpal.get_color_count (); + return _get_cpal (face).get_palette_colors (palette_index, start_offset, colors_count, colors); } diff --git a/src/hb-ot-color.h b/src/hb-ot-color.h index 4d08eb065..02b76bffc 100644 --- a/src/hb-ot-color.h +++ b/src/hb-ot-color.h @@ -79,11 +79,11 @@ hb_ot_color_palette_get_flags (hb_face_t *face, unsigned int palette_index); HB_EXTERN unsigned int -hb_ot_color_palette_get_colors (hb_face_t *face, - 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. */); +hb_ot_color_palette_get_colors (hb_face_t *face, + 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. */); /*