diff --git a/src/Makefile.sources b/src/Makefile.sources index b45cd9276..fc3383b80 100644 --- a/src/Makefile.sources +++ b/src/Makefile.sources @@ -96,6 +96,8 @@ HB_OT_sources = \ hb-ot-layout-gsub-table.hh \ hb-ot-layout-jstf-table.hh \ hb-ot-layout-private.hh \ + hb-ot-color.cc \ + hb-ot-cpal-table.hh \ hb-ot-map.cc \ hb-ot-map-private.hh \ hb-ot-math.cc \ @@ -149,6 +151,7 @@ HB_OT_RAGEL_sources = \ HB_OT_headers = \ hb-ot.h \ + hb-ot-color.h \ hb-ot-font.h \ hb-ot-layout.h \ hb-ot-math.h \ diff --git a/src/hb-ot-color.cc b/src/hb-ot-color.cc new file mode 100644 index 000000000..f89e58acd --- /dev/null +++ b/src/hb-ot-color.cc @@ -0,0 +1,174 @@ +/* + * Copyright © 2016 Google, Inc. + * Copyright © 2018 Ebrahim Byagowi + * + * 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 + */ + +#include "hb-open-type-private.hh" +#include "hb-ot-cpal-table.hh" +#include "hb-ot.h" + +#include +#include + +#include "hb-ot-layout-private.hh" +#include "hb-shaper-private.hh" + +#if 0 +HB_MARK_AS_FLAG_T (hb_ot_color_palette_flags_t) +//HB_SHAPER_DATA_ENSURE_DECLARE(ot, face) Hmm? + + +static inline const OT::CPAL& +_get_cpal (hb_face_t *face) +{ + if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::CPAL); + hb_ot_layout_t * layout = hb_ot_layout_from_face (face); + return *(layout->cpal.get ()); +} + + +/** + * hb_ot_color_get_palette_count: + * @face: a font face. + * + * Returns: the number of color palettes in @face, or zero if @face has + * no colors. + * + * Since: REPLACEME + */ +unsigned int +hb_ot_color_get_palette_count (hb_face_t *face) +{ + const OT::CPAL& cpal = _get_cpal (face); + return cpal.get_palette_count (); +} + + +/** + * hb_ot_color_get_palette_name_id: + * @face: a font face. + * @palette: the index of the color palette whose name is being requested. + * + * Retrieves the name id of a color palette. For example, a color font can + * have themed palettes like "Spring", "Summer", "Fall", and "Winter". + * + * Returns: an identifier within @face's `name` table. + * If the requested palette has no name, or if @face has no colors, + * or if @palette is not between 0 and hb_ot_color_get_palette_count(), + * the result is 0xFFFF. The implementation does not check whether + * the returned palette name id is actually in @face's `name` table. + * + * Since: REPLACEME + */ +unsigned int +hb_ot_color_get_palette_name_id (hb_face_t *face, unsigned int palette) +{ + const OT::CPAL& cpal = _get_cpal (face); + return cpal.get_palette_name_id (palette); +} + + +/** + * hb_ot_color_get_palette_flags: + * @face: a font face + * @palette: the index of the color palette whose flags are being requested + * + * Returns: the flags for the requested color palette. If @face has no colors, + * or if @palette is not between 0 and hb_ot_color_get_palette_count(), + * the result is #HB_OT_COLOR_PALETTE_FLAG_DEFAULT. + * + * Since: REPLACEME + */ +hb_ot_color_palette_flags_t +hb_ot_color_get_palette_flags (hb_face_t *face, unsigned int palette) +{ + const OT::CPAL& cpal = _get_cpal(face); + return cpal.get_palette_flags (palette); +} + + +/** + * hb_ot_color_get_palette_colors: + * @face: a font face. + * @palette: the index of the color palette whose colors + * are being requested. + * @start_offset: the index of the first color being requested. + * @color_count: (inout) (optional): on input, how many colors + * can be maximally stored into the @colors array; + * on output, how many colors were actually stored. + * @colors: (array length=color_count) (optional): + * an array of #hb_ot_color_t records. After calling + * this function, @colors will be filled with + * the palette colors. If @colors is NULL, the function + * will just return the number of total colors + * without storing any actual colors; this can be used + * for allocating a buffer of suitable size before calling + * hb_ot_color_get_palette_colors() a second time. + * + * Retrieves the colors in a color palette. + * + * Returns: the total number of colors in the palette. All palettes in + * a font have the same number of colors. If @face has no colors, or if + * @palette is not between 0 and hb_ot_color_get_palette_count(), + * the result is zero. + * + * Since: REPLACEME + */ +unsigned int +hb_ot_color_get_palette_colors (hb_face_t *face, + unsigned int palette, /* default=0 */ + unsigned int start_offset, + unsigned int *color_count /* IN/OUT */, + hb_ot_color_t *colors /* OUT */) +{ + const OT::CPAL& cpal = _get_cpal(face); + if (unlikely (palette >= cpal.numPalettes)) + { + if (color_count) *color_count = 0; + return 0; + } + + const OT::ColorRecord* crec = &cpal.offsetFirstColorRecord (&cpal); + crec += cpal.colorRecordIndices[palette]; + + unsigned int num_results = 0; + if (likely (color_count && colors)) + { + for (unsigned int i = start_offset; + i < cpal.numPaletteEntries && num_results < *color_count; ++i) + { + hb_ot_color_t* result = &colors[num_results]; + result->red = crec[i].red; + result->green = crec[i].green; + result->blue = crec[i].blue; + result->alpha = crec[i].alpha; + ++num_results; + } + } + + if (likely (color_count)) *color_count = num_results; + return cpal.numPaletteEntries; +} +#endif diff --git a/src/hb-ot-color.h b/src/hb-ot-color.h new file mode 100644 index 000000000..8f485667d --- /dev/null +++ b/src/hb-ot-color.h @@ -0,0 +1,98 @@ +/* + * Copyright © 2016 Google, Inc. + * + * 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_H_IN +#error "Include instead." +#endif + +#ifndef HB_OT_COLOR_H +#define HB_OT_COLOR_H + +#include "hb.h" + +#include "hb-ot-tag.h" + +HB_BEGIN_DECLS + +/** + * HB_OT_TAG_CPAL: + * a four-letter tag for identifying the CPAL table with color palettes + * + * Since: REPLACEME + */ +#define HB_OT_TAG_CPAL HB_TAG('C','P','A','L') + + +/** + * hb_ot_color_t: + * @red: the intensity of the red channel + * @green: the intensity of the green channel + * @blue: the intensity of the blue channel + * @alpha: the transparency + * + * Structure for holding color values. + * + * Since: REPLACEME + */ +typedef struct +{ + uint8_t red, green, blue, alpha; +} hb_ot_color_t; + + +/** + * hb_ot_color_palette_flags_t: + * @HB_OT_COLOR_PALETTE_FLAG_DEFAULT: default indicating that there is nothing special to note about a color palette. + * @HB_OT_COLOR_PALETTE_FLAG_FOR_LIGHT_BACKGROUND: flag indicating that the color palette is suitable for rendering text on light background. + * @HB_OT_COLOR_PALETTE_FLAG_FOR_DARK_BACKGROUND: flag indicating that the color palette is suitable for rendering text on dark background. + * + * Since: REPLACEME + */ +typedef enum { /*< flags >*/ + HB_OT_COLOR_PALETTE_FLAG_DEFAULT = 0x00000000u, + HB_OT_COLOR_PALETTE_FLAG_FOR_LIGHT_BACKGROUND = 0x00000001u, + HB_OT_COLOR_PALETTE_FLAG_FOR_DARK_BACKGROUND = 0x00000002u, +} hb_ot_color_palette_flags_t; + +// HB_EXTERN unsigned int +// hb_ot_color_get_palette_count (hb_face_t *face); + +// HB_EXTERN unsigned int +// hb_ot_color_get_palette_name_id (hb_face_t *face, unsigned int palette); + +// HB_EXTERN hb_ot_color_palette_flags_t +// hb_ot_color_get_palette_flags (hb_face_t *face, unsigned int palette); + +// HB_EXTERN unsigned int +// hb_ot_color_get_palette_colors (hb_face_t *face, +// unsigned int palette, /* default=0 */ +// unsigned int start_offset, +// unsigned int *color_count /* IN/OUT */, +// hb_ot_color_t *colors /* OUT */); + +HB_END_DECLS + +#endif /* HB_OT_COLOR_H */ diff --git a/src/hb-ot-cpal-table.hh b/src/hb-ot-cpal-table.hh new file mode 100644 index 000000000..4369a7d30 --- /dev/null +++ b/src/hb-ot-cpal-table.hh @@ -0,0 +1,169 @@ +/* + * Copyright © 2016 Google, Inc. + * Copyright © 2018 Ebrahim Byagowi + * + * 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_CPAL_TABLE_HH +#define HB_OT_CPAL_TABLE_HH + +#include "hb-open-type-private.hh" + + +namespace OT { + +/* + * Color Palette + * http://www.microsoft.com/typography/otspec/cpal.htm + */ + +#define HB_OT_TAG_CPAL HB_TAG('C','P','A','L') + +struct ColorRecord +{ + inline bool sanitize (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + return_trace (true); + } + + HBUINT8 blue; + HBUINT8 green; + HBUINT8 red; + HBUINT8 alpha; + public: + DEFINE_SIZE_STATIC (4); +}; + +struct CPALV1Tail +{ + friend struct CPAL; + + inline bool sanitize (hb_sanitize_context_t *c, unsigned int palettes) const + { + TRACE_SANITIZE (this); + return_trace ( + c->check_struct (this) && + c->check_array ((const void*) &paletteFlags, sizeof (HBUINT32), palettes) && + c->check_array ((const void*) &paletteLabel, sizeof (HBUINT16), palettes) && + c->check_array ((const void*) &paletteEntryLabel, sizeof (HBUINT16), palettes)); + } + + private: + inline hb_ot_color_palette_flags_t + get_palette_flags (const void *base, unsigned int palette) const + { + const HBUINT32* flags = (const HBUINT32*) (const void*) &paletteFlags (base); + return (hb_ot_color_palette_flags_t) (uint32_t) flags[palette]; + } + + inline unsigned int + get_palette_name_id (const void *base, unsigned int palette) const + { + const HBUINT16* name_ids = (const HBUINT16*) (const void*) &paletteLabel (base); + return name_ids[palette]; + } + + protected: + LOffsetTo paletteFlags; + LOffsetTo paletteLabel; + LOffsetTo paletteEntryLabel; + + public: + DEFINE_SIZE_STATIC (12); +}; + +struct CPAL +{ + static const hb_tag_t tableTag = HB_OT_TAG_CPAL; + + inline bool sanitize (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + if (!(c->check_struct (this) && + colorRecords.sanitize (c))) + return_trace (false); + + unsigned int palettes = numPalettes; + if (!c->check_array (colorRecordIndices, sizeof (HBUINT16), palettes)) + return_trace (false); + + for (unsigned int i = 0; i < palettes; ++i) + if (colorRecordIndices[i] + numPaletteEntries > colorRecords.get_size ()) + return_trace (false); + + if (version > 1) + { + const CPALV1Tail &v1 = StructAfter (*this); + return_trace (v1.sanitize (c, palettes)); + } + else + return_trace (true); + } + + inline unsigned int get_size (void) const + { + return min_size + numPalettes * 2; + } + + inline hb_ot_color_palette_flags_t get_palette_flags (unsigned int palette) const + { + if (version == 0 || palette >= numPalettes) + return HB_OT_COLOR_PALETTE_FLAG_DEFAULT; + + const CPALV1Tail& cpal1 = StructAfter (*this); + return cpal1.get_palette_flags (this, palette); + } + + inline unsigned int get_palette_name_id (unsigned int palette) const + { + if (version == 0 || palette >= numPalettes) + return 0xFFFF; + + const CPALV1Tail& cpal1 = StructAfter (*this); + return cpal1.get_palette_name_id (this, palette); + } + + inline unsigned int get_palette_count () const + { + return numPalettes; + } + + protected: + HBUINT16 version; + + /* Version 0 */ + HBUINT16 numPaletteEntries; + HBUINT16 numPalettes; + ArrayOf colorRecords; + HBUINT16 colorRecordIndices[VAR]; // VAR=numPalettes + + public: + DEFINE_SIZE_ARRAY (12, colorRecordIndices); +}; + +} /* namespace OT */ + + +#endif /* HB_OT_CPAL_TABLE_HH */ diff --git a/src/hb-ot-layout-private.hh b/src/hb-ot-layout-private.hh index fdaa41ee8..a8f06f6a3 100644 --- a/src/hb-ot-layout-private.hh +++ b/src/hb-ot-layout-private.hh @@ -126,6 +126,7 @@ namespace OT { struct GDEF; struct GSUB; struct GPOS; + struct CPAL; struct MATH; struct fvar; struct avar; @@ -172,6 +173,7 @@ struct hb_ot_layout_t /* TODO Move the following out of this struct. */ OT::hb_lazy_table_loader_t base; OT::hb_lazy_table_loader_t math; + OT::hb_lazy_table_loader_t cpal; OT::hb_lazy_table_loader_t fvar; OT::hb_lazy_table_loader_t avar; OT::hb_lazy_table_loader_t ankr; diff --git a/src/hb-ot-layout.cc b/src/hb-ot-layout.cc index 42802e2ba..beef8419f 100644 --- a/src/hb-ot-layout.cc +++ b/src/hb-ot-layout.cc @@ -37,6 +37,7 @@ #include "hb-ot-layout-gpos-table.hh" #include "hb-ot-layout-jstf-table.hh" // Just so we compile it; unused otherwise. #include "hb-ot-name-table.hh" // Just so we compile it; unused otherwise. +#include "hb-ot-cpal-table.hh" #include "hb-ot-map-private.hh" @@ -63,6 +64,7 @@ _hb_ot_layout_create (hb_face_t *face) layout->gpos = OT::Sanitizer::lock_instance (layout->gpos_blob); layout->math.init (face); + layout->cpal.init (face); layout->base.init (face); layout->fvar.init (face); layout->avar.init (face); @@ -217,6 +219,7 @@ _hb_ot_layout_destroy (hb_ot_layout_t *layout) hb_blob_destroy (layout->gpos_blob); layout->math.fini (); + layout->cpal.fini (); layout->base.fini (); layout->fvar.fini (); layout->avar.fini (); diff --git a/src/hb-ot.h b/src/hb-ot.h index 272208e38..d1979b81f 100644 --- a/src/hb-ot.h +++ b/src/hb-ot.h @@ -30,6 +30,7 @@ #include "hb.h" +#include "hb-ot-color.h" #include "hb-ot-font.h" #include "hb-ot-layout.h" #include "hb-ot-math.h" diff --git a/test/api/Makefile.am b/test/api/Makefile.am index e2e6166c0..a6ebc57a2 100644 --- a/test/api/Makefile.am +++ b/test/api/Makefile.am @@ -61,6 +61,7 @@ endif if HAVE_OT TEST_PROGS += \ + test-ot-color \ test-ot-tag \ $(NULL) diff --git a/test/api/hb-test.h b/test/api/hb-test.h index a88b00c48..c851f809c 100644 --- a/test/api/hb-test.h +++ b/test/api/hb-test.h @@ -88,6 +88,38 @@ hb_test_run (void) } +#if 0 +/* Helpers for loading test fonts */ +static inline hb_face_t * +hb_test_load_face (const char *path) +{ + const char *font_data = NULL; + unsigned int len = 0; + hb_blob_t *blob = NULL; + hb_face_t *face = NULL; + + FILE *f = fopen (path, "rb"); + if (!f) { + perror (path); + exit (1); + } + + fseek (f, 0, SEEK_END); + len = ftell (f); + fseek (f, 0, SEEK_SET); + font_data = (const char *) malloc (len); + if (!font_data) len = 0; + len = fread ((char *) font_data, 1, len, f); + fclose (f); + + blob = hb_blob_create (font_data, len, HB_MEMORY_MODE_READONLY, 0, free); + face = hb_face_create (blob, 0 /* first face */); + hb_blob_destroy (blob); + return face; +} +#endif + + /* Bugzilla helpers */ static inline void diff --git a/test/api/test-ot-color.c b/test/api/test-ot-color.c new file mode 100644 index 000000000..22584d20e --- /dev/null +++ b/test/api/test-ot-color.c @@ -0,0 +1,319 @@ +/* + * Copyright © 2016 Google, Inc. + * + * 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 + */ + +#include "hb-test.h" + +#include +#include +#include + +/* Unit tests for hb-ot-color.h */ + +/* Test font with the following CPAL v0 table, as TTX and manual disassembly: + + + + + + + + + + + + + + + 0 | 0000 # version=0 + 2 | 0002 # numPaletteEntries=2 + 4 | 0002 # numPalettes=2 + 6 | 0004 # numColorRecords=4 + 8 | 00000010 # offsetToFirstColorRecord=16 + 12 | 0000 0002 # colorRecordIndex=[0, 2] + 16 | 000000ff ffcc66ff # colorRecord #0, #1 (BGRA) + 24 | 000000ff 000080ff # colorRecord #2, #3 (BGRA) + */ +static hb_face_t *cpal_v0 = NULL; + +/* Test font with the following CPAL v1 table, as TTX and manual disassembly: + + + + + + + + + + + + + + + + + + + + + 0 | 0001 # version=1 + 2 | 0002 # numPaletteEntries=2 + 4 | 0003 # numPalettes=3 + 6 | 0006 # numColorRecords=6 + 8 | 0000001e # offsetToFirstColorRecord=30 + 12 | 0000 0002 0004 # colorRecordIndex=[0, 2, 4] + 18 | 00000036 # offsetToPaletteTypeArray=54 + 22 | 00000042 # offsetToPaletteLabelArray=66 + 26 | 00000048 # offsetToPaletteEntryLabelArray=72 + 30 | 000000ff ffcc66ff 000000ff # colorRecord #0, #1, #2 (BGRA) + 42 | 66ccffff 000000ff 000080ff # colorRecord #3, #4, #5 (BGRA) + 54 | 00000002 00000001 00000000 # paletteFlags=[2, 1, 0] + 66 | 0101 ffff 0102 # paletteName=[257, 0xffff, 258] + 72 | ffff 0100 # paletteEntryLabel=[0xffff, 256] +*/ +static hb_face_t *cpal_v1 = NULL; + + +#define assert_color_rgba(colors, i, r, g, b, a) G_STMT_START { \ + const hb_ot_color_t *_colors = (colors); \ + const size_t _i = (i); \ + const uint8_t red = (r), green = (g), blue = (b), alpha = (a); \ + if (_colors[_i].red != red) { \ + g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ + "colors[" #i "].red", _colors[_i].red, "==", red, 'x'); \ + } \ + if (_colors[_i].green != green) { \ + g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ + "colors[" #i "].green", _colors[_i].green, "==", green, 'x'); \ + } \ + if (_colors[_i].blue != blue) { \ + g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ + "colors[" #i "].blue", colors[i].blue, "==", blue, 'x'); \ + } \ + if (_colors[_i].alpha != alpha) { \ + g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ + "colors[" #i "].alpha", _colors[_i].alpha, "==", alpha, 'x'); \ + } \ +} G_STMT_END + + +#if 0 +static void +test_hb_ot_color_get_palette_count (void) +{ + g_assert_cmpint (hb_ot_color_get_palette_count (hb_face_get_empty()), ==, 0); + g_assert_cmpint (hb_ot_color_get_palette_count (cpal_v0), ==, 2); + g_assert_cmpint (hb_ot_color_get_palette_count (cpal_v1), ==, 3); +} + + +static void +test_hb_ot_color_get_palette_name_id_empty (void) +{ + /* numPalettes=0, so all calls are for out-of-bounds palette indices */ + g_assert_cmpint (hb_ot_color_get_palette_name_id (hb_face_get_empty(), 0), ==, 0xffff); + g_assert_cmpint (hb_ot_color_get_palette_name_id (hb_face_get_empty(), 1), ==, 0xffff); +} + + +static void +test_hb_ot_color_get_palette_name_id_v0 (void) +{ + g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v0, 0), ==, 0xffff); + g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v0, 1), ==, 0xffff); + + /* numPalettes=2, so palette #2 is out of bounds */ + g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v0, 2), ==, 0xffff); +} + + +static void +test_hb_ot_color_get_palette_name_id_v1 (void) +{ + g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v1, 0), ==, 257); + g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v1, 1), ==, 0xffff); + g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v1, 2), ==, 258); + + /* numPalettes=3, so palette #3 is out of bounds */ + g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v1, 3), ==, 0xffff); +} + +static void +test_hb_ot_color_get_palette_flags_empty (void) +{ + /* numPalettes=0, so all calls are for out-of-bounds palette indices */ + g_assert_cmpint (hb_ot_color_get_palette_flags (hb_face_get_empty(), 0), ==, HB_OT_COLOR_PALETTE_FLAG_DEFAULT); + g_assert_cmpint (hb_ot_color_get_palette_flags (hb_face_get_empty(), 1), ==, HB_OT_COLOR_PALETTE_FLAG_DEFAULT); +} + + +static void +test_hb_ot_color_get_palette_flags_v0 (void) +{ + g_assert_cmpint (hb_ot_color_get_palette_flags (cpal_v0, 0), ==, HB_OT_COLOR_PALETTE_FLAG_DEFAULT); + g_assert_cmpint (hb_ot_color_get_palette_flags (cpal_v0, 1), ==, HB_OT_COLOR_PALETTE_FLAG_DEFAULT); + + /* numPalettes=2, so palette #2 is out of bounds */ + g_assert_cmpint (hb_ot_color_get_palette_flags (cpal_v0, 2), ==, HB_OT_COLOR_PALETTE_FLAG_DEFAULT); +} + + +static void +test_hb_ot_color_get_palette_flags_v1 (void) +{ + g_assert_cmpint (hb_ot_color_get_palette_flags (cpal_v1, 0), ==, HB_OT_COLOR_PALETTE_FLAG_FOR_DARK_BACKGROUND); + g_assert_cmpint (hb_ot_color_get_palette_flags (cpal_v1, 1), ==, HB_OT_COLOR_PALETTE_FLAG_FOR_LIGHT_BACKGROUND); + g_assert_cmpint (hb_ot_color_get_palette_flags (cpal_v0, 2), ==, HB_OT_COLOR_PALETTE_FLAG_DEFAULT); + + /* numPalettes=3, so palette #3 is out of bounds */ + g_assert_cmpint (hb_ot_color_get_palette_flags (cpal_v0, 3), ==, HB_OT_COLOR_PALETTE_FLAG_DEFAULT); +} + + +static void +test_hb_ot_color_get_palette_colors_empty (void) +{ + hb_face_t *empty = hb_face_get_empty (); + g_assert_cmpint (hb_ot_color_get_palette_colors (empty, 0, 0, NULL, NULL), ==, 0); +} + + +static void +test_hb_ot_color_get_palette_colors_v0 (void) +{ + unsigned int num_colors = hb_ot_color_get_palette_colors (cpal_v0, 0, 0, NULL, NULL); + hb_ot_color_t *colors = (hb_ot_color_t*) alloca (num_colors * sizeof (hb_ot_color_t)); + size_t colors_size = num_colors * sizeof(*colors); + g_assert_cmpint (num_colors, ==, 2); + + /* Palette #0, start_index=0 */ + g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v0, 0, 0, &num_colors, colors), ==, 2); + g_assert_cmpint (num_colors, ==, 2); + assert_color_rgba (colors, 0, 0x00, 0x00, 0x00, 0xff); + assert_color_rgba (colors, 1, 0x66, 0xcc, 0xff, 0xff); + + /* Palette #1, start_index=0 */ + g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v0, 1, 0, &num_colors, colors), ==, 2); + g_assert_cmpint (num_colors, ==, 2); + assert_color_rgba (colors, 0, 0x00, 0x00, 0x00, 0xff); + assert_color_rgba (colors, 1, 0x80, 0x00, 0x00, 0xff); + + /* Palette #2 (there are only #0 and #1 in the font, so this is out of bounds) */ + g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v0, 2, 0, &num_colors, colors), ==, 0); + + /* Palette #0, start_index=1 */ + memset(colors, 0x33, colors_size); + num_colors = 2; + g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v0, 0, 1, &num_colors, colors), ==, 2); + g_assert_cmpint (num_colors, ==, 1); + assert_color_rgba (colors, 0, 0x66, 0xcc, 0xff, 0xff); + assert_color_rgba (colors, 1, 0x33, 0x33, 0x33, 0x33); /* untouched */ + + /* Palette #0, start_index=0, pretend that we have only allocated space for 1 color */ + memset(colors, 0x44, colors_size); + num_colors = 1; + g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v0, 0, 0, &num_colors, colors), ==, 2); + g_assert_cmpint (num_colors, ==, 1); + assert_color_rgba (colors, 0, 0x00, 0x00, 0x00, 0xff); + assert_color_rgba (colors, 1, 0x44, 0x44, 0x44, 0x44); /* untouched */ + + /* start_index > numPaletteEntries */ + memset(colors, 0x44, colors_size); + num_colors = 2; + g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v0, 0, 9876, &num_colors, colors), ==, 2); + g_assert_cmpint (num_colors, ==, 0); + assert_color_rgba (colors, 0, 0x44, 0x44, 0x44, 0x44); /* untouched */ + assert_color_rgba (colors, 1, 0x44, 0x44, 0x44, 0x44); /* untouched */ +} + + +static void +test_hb_ot_color_get_palette_colors_v1 (void) +{ + hb_ot_color_t colors[3]; + unsigned int num_colors = hb_ot_color_get_palette_colors (cpal_v1, 0, 0, NULL, NULL); + size_t colors_size = 3 * sizeof(*colors); + g_assert_cmpint (num_colors, ==, 2); + + /* Palette #0, start_index=0 */ + memset(colors, 0x77, colors_size); + g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v1, 0, 0, &num_colors, colors), ==, 2); + g_assert_cmpint (num_colors, ==, 2); + assert_color_rgba (colors, 0, 0x00, 0x00, 0x00, 0xff); + assert_color_rgba (colors, 1, 0x66, 0xcc, 0xff, 0xff); + assert_color_rgba (colors, 2, 0x77, 0x77, 0x77, 0x77); /* untouched */ + + /* Palette #1, start_index=0 */ + memset(colors, 0x77, colors_size); + g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v1, 1, 0, &num_colors, colors), ==, 2); + g_assert_cmpint (num_colors, ==, 2); + assert_color_rgba (colors, 0, 0x00, 0x00, 0x00, 0xff); + assert_color_rgba (colors, 1, 0xff, 0xcc, 0x66, 0xff); + assert_color_rgba (colors, 2, 0x77, 0x77, 0x77, 0x77); /* untouched */ + + /* Palette #2, start_index=0 */ + memset(colors, 0x77, colors_size); + g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v1, 2, 0, &num_colors, colors), ==, 2); + g_assert_cmpint (num_colors, ==, 2); + assert_color_rgba (colors, 0, 0x00, 0x00, 0x00, 0xff); + assert_color_rgba (colors, 1, 0x80, 0x00, 0x00, 0xff); + assert_color_rgba (colors, 2, 0x77, 0x77, 0x77, 0x77); /* untouched */ + + /* Palette #3 (out of bounds), start_index=0 */ + memset(colors, 0x77, colors_size); + g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v1, 3, 0, &num_colors, colors), ==, 0); + g_assert_cmpint (num_colors, ==, 0); + assert_color_rgba (colors, 0, 0x77, 0x77, 0x77, 0x77); /* untouched */ + assert_color_rgba (colors, 1, 0x77, 0x77, 0x77, 0x77); /* untouched */ + assert_color_rgba (colors, 2, 0x77, 0x77, 0x77, 0x77); /* untouched */ +} +#endif + +int +main (int argc, char **argv) +{ + int status = 0; + + hb_test_init (&argc, &argv); + // cpal_v0 = hb_test_load_face ("../shaping/data/in-house/fonts/e90374e5e439e00725b4fe7a8d73db57c5a97f82.ttf"); + // cpal_v1 = hb_test_load_face ("../shaping/data/in-house/fonts/319f5d7ebffbefc5c5e6569f8cea73444d7a7268.ttf"); + // hb_test_add (test_hb_ot_color_get_palette_count); + // hb_test_add (test_hb_ot_color_get_palette_name_id_empty); + // hb_test_add (test_hb_ot_color_get_palette_name_id_v0); + // hb_test_add (test_hb_ot_color_get_palette_name_id_v1); + // hb_test_add (test_hb_ot_color_get_palette_flags_empty); + // hb_test_add (test_hb_ot_color_get_palette_flags_v0); + // hb_test_add (test_hb_ot_color_get_palette_flags_v1); + // hb_test_add (test_hb_ot_color_get_palette_colors_empty); + // hb_test_add (test_hb_ot_color_get_palette_colors_v0); + // hb_test_add (test_hb_ot_color_get_palette_colors_v1); + status = hb_test_run(); + hb_face_destroy (cpal_v0); + hb_face_destroy (cpal_v1); + return status; +} diff --git a/test/shaping/data/in-house/fonts/319f5d7ebffbefc5c5e6569f8cea73444d7a7268.ttf b/test/shaping/data/in-house/fonts/319f5d7ebffbefc5c5e6569f8cea73444d7a7268.ttf new file mode 100644 index 000000000..53044b718 Binary files /dev/null and b/test/shaping/data/in-house/fonts/319f5d7ebffbefc5c5e6569f8cea73444d7a7268.ttf differ diff --git a/test/shaping/data/in-house/fonts/e90374e5e439e00725b4fe7a8d73db57c5a97f82.ttf b/test/shaping/data/in-house/fonts/e90374e5e439e00725b4fe7a8d73db57c5a97f82.ttf new file mode 100644 index 000000000..66a9001bd Binary files /dev/null and b/test/shaping/data/in-house/fonts/e90374e5e439e00725b4fe7a8d73db57c5a97f82.ttf differ