From d34d3ac985a6c8c848ae49635b648a72e0c8f30d Mon Sep 17 00:00:00 2001 From: Sascha Brawer Date: Mon, 25 Apr 2016 18:20:57 +0200 Subject: [PATCH] Support CPAL table --- NEWS | 6 +- src/Makefile.am | 3 + src/Makefile.sources | 3 + src/hb-ot-color.cc | 213 ++++++++++++++ src/hb-ot-color.h | 99 +++++++ src/hb-ot-cpal-table.hh | 121 ++++++++ src/hb-ot-layout-private.hh | 3 + src/hb-ot-layout.cc | 7 + src/hb-ot.h | 1 + test/api/Makefile.am | 1 + test/api/hb-test.h | 30 ++ test/api/test-ot-color.c | 318 +++++++++++++++++++++ .../319f5d7ebffbefc5c5e6569f8cea73444d7a7268.ttf | Bin 0 -> 2128 bytes .../e90374e5e439e00725b4fe7a8d73db57c5a97f82.ttf | Bin 0 -> 1948 bytes 14 files changed, 804 insertions(+), 1 deletion(-) create mode 100644 src/hb-ot-color.cc create mode 100644 src/hb-ot-color.h create mode 100644 src/hb-ot-cpal-table.hh create mode 100644 test/api/test-ot-color.c create mode 100644 test/shaping/fonts/sha1sum/319f5d7ebffbefc5c5e6569f8cea73444d7a7268.ttf create mode 100644 test/shaping/fonts/sha1sum/e90374e5e439e00725b4fe7a8d73db57c5a97f82.ttf diff --git a/NEWS b/NEWS index e62b198..992c64b 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,8 @@ +Overview of changes leading to 1.2.8 +Not yet released +==================================== +- Implemented 'CPAL' table in hb-ot-color. + Overview of changes leading to 1.2.7 Monday, May 2, 2016 ==================================== @@ -9,7 +14,6 @@ Monday, May 2, 2016 - Unbreak build on Windows CE. - Make 'glyf' table loading lazy in hb-ot-font. - Overview of changes leading to 1.2.6 Friday, April 8, 2016 ==================================== diff --git a/src/Makefile.am b/src/Makefile.am index 8cfe4ac..49e349b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -312,6 +312,9 @@ dist_check_SCRIPTS = \ check_PROGRAMS = \ test-ot-tag \ $(NULL) +test_ot_color_SOURCES = hb-ot-color.cc +test_ot_color_CPPFLAGS = $(HBCFLAGS) -DMAIN +test_ot_color_LDADD = libharfbuzz.la $(HBLIBS) test_ot_tag_SOURCES = hb-ot-tag.cc test_ot_tag_CPPFLAGS = $(HBCFLAGS) -DMAIN test_ot_tag_LDADD = libharfbuzz.la $(HBLIBS) diff --git a/src/Makefile.sources b/src/Makefile.sources index ac80683..a40de85 100644 --- a/src/Makefile.sources +++ b/src/Makefile.sources @@ -67,6 +67,8 @@ HB_NODIST_headers = \ HB_FALLBACK_sources = hb-fallback-shape.cc HB_OT_sources = \ + hb-ot-color.cc \ + hb-ot-cpal-table.hh \ hb-ot-font.cc \ hb-ot-layout.cc \ hb-ot-layout-common-private.hh \ @@ -109,6 +111,7 @@ HB_OT_sources = \ HB_OT_headers = \ hb-ot.h \ + hb-ot-color.h \ hb-ot-font.h \ hb-ot-layout.h \ hb-ot-shape.h \ diff --git a/src/hb-ot-color.cc b/src/hb-ot-color.cc new file mode 100644 index 0000000..58aa412 --- /dev/null +++ b/src/hb-ot-color.cc @@ -0,0 +1,213 @@ +/* + * 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-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" + +HB_MARK_AS_FLAG_T (hb_ot_color_palette_flags_t) +HB_SHAPER_DATA_ENSURE_DECLARE(ot, face) + + +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); + if (!layout->cpal) { + layout->cpal_blob = OT::Sanitizer::sanitize (face->reference_table (HB_OT_TAG_CPAL)); + layout->cpal = OT::Sanitizer::lock_instance (layout->cpal_blob); + } + + return *layout->cpal; +} + + +/** + * 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: 1.2.8 + */ +unsigned int +hb_ot_color_get_palette_count (hb_face_t *face) +{ + const OT::CPAL& cpal = _get_cpal(face); + return &cpal != &OT::Null(OT::CPAL) ? cpal.numPalettes : 0; +} + + +/** + * 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 zero. + * + * Since: 1.2.8 + */ +unsigned int +hb_ot_color_get_palette_name_id (hb_face_t *face, unsigned int palette) +{ + const OT::CPAL& cpal = _get_cpal(face); + if (unlikely (&cpal == &OT::Null(OT::CPAL) || cpal.version == 0 || + palette >= cpal.numPalettes)) { + return 0; + } + + const OT::CPALV1Tail& cpal1 = OT::StructAfter(cpal); + if (unlikely (&cpal1 == &OT::Null(OT::CPALV1Tail) || + cpal1.paletteLabel.is_null())) { + return 0; + } + + const OT::USHORT* name_ids = &cpal1.paletteLabel (&cpal); + const OT::USHORT name_id = name_ids [palette]; + + // According to the OpenType CPAL specification, 0xFFFF means name-less. + // We map 0xFFFF to 0 because zero is far more commonly used to indicate + // "no value". + return likely (name_id != 0xffff) ? name_id : 0; +} + + +/** + * 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: 1.2.8 + */ +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); + if (unlikely (&cpal == &OT::Null(OT::CPAL) || cpal.version == 0 || + palette >= cpal.numPalettes)) { + return HB_OT_COLOR_PALETTE_FLAG_DEFAULT; + } + + const OT::CPALV1Tail& cpal1 = OT::StructAfter(cpal); + if (unlikely (&cpal1 == &OT::Null(OT::CPALV1Tail) || + cpal1.paletteFlags.is_null())) { + return HB_OT_COLOR_PALETTE_FLAG_DEFAULT; + } + + const OT::ULONG* flags = &cpal1.paletteFlags(&cpal); + const uint32_t flag = static_cast (flags [palette]); + return static_cast (flag); +} + + +/** + * 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: (out caller-allocates) (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: 1.2.8 + */ +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 (&cpal == &OT::Null(OT::CPAL) || + palette >= cpal.numPalettes)) + { + if (color_count) *color_count = 0; + return 0; + } + + const OT::ColorRecord* crec = &cpal.offsetFirstColorRecord (&cpal); + if (unlikely (crec == &OT::Null(OT::ColorRecord))) + { + if (color_count) *color_count = 0; + return 0; + } + 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; +} diff --git a/src/hb-ot-color.h b/src/hb-ot-color.h new file mode 100644 index 0000000..2fe799f --- /dev/null +++ b/src/hb-ot-color.h @@ -0,0 +1,99 @@ +/* + * 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: 1.2.8 + */ +#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: 1.2.8 + */ +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: 1.2.8 + */ +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 0000000..3287346 --- /dev/null +++ b/src/hb-ot-cpal-table.hh @@ -0,0 +1,121 @@ +/* + * 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_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 + { + // We do not enforce alpha != 0 because zero alpha is bogus but harmless. + TRACE_SANITIZE (this); + return_trace (true); + } + + public: + BYTE blue; + BYTE green; + BYTE red; + BYTE alpha; + DEFINE_SIZE_STATIC (4); +}; + +struct CPALV1Tail +{ + inline bool sanitize (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + return_trace (paletteFlags.sanitize (c, this) && + paletteLabel.sanitize (c, this) && + paletteEntryLabel.sanitize (c, this)); + } + + public: + OffsetTo paletteFlags; + OffsetTo paletteLabel; + OffsetTo paletteEntryLabel; + 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 (unlikely (!(c->check_struct (this) && + offsetFirstColorRecord.sanitize (c, this)))) { + return_trace (false); + } + for (unsigned int i = 0; i < numPalettes; ++i) { + if (unlikely (colorRecordIndices[i] + numPaletteEntries > numColorRecords)) { + return_trace (false); + } + } + if (version > 1) { + const CPALV1Tail &v1 = StructAfter(*this); + return_trace (v1.sanitize (c)); + } else { + return_trace (true); + } + } + + inline unsigned int get_size (void) const { + return min_size + numPalettes * 2; + } + + public: + USHORT version; + + /* Version 0 */ + USHORT numPaletteEntries; + USHORT numPalettes; + USHORT numColorRecords; + OffsetTo offsetFirstColorRecord; + USHORT 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 778b2c4..30c8717 100644 --- a/src/hb-ot-layout-private.hh +++ b/src/hb-ot-layout-private.hh @@ -124,6 +124,7 @@ namespace OT { struct GDEF; struct GSUB; struct GPOS; + struct CPAL; } struct hb_ot_layout_lookup_accelerator_t @@ -152,10 +153,12 @@ struct hb_ot_layout_t hb_blob_t *gdef_blob; hb_blob_t *gsub_blob; hb_blob_t *gpos_blob; + hb_blob_t *cpal_blob; const struct OT::GDEF *gdef; const struct OT::GSUB *gsub; const struct OT::GPOS *gpos; + const struct OT::CPAL *cpal; unsigned int gsub_lookup_count; unsigned int gpos_lookup_count; diff --git a/src/hb-ot-layout.cc b/src/hb-ot-layout.cc index 29749bc..73f993b 100644 --- a/src/hb-ot-layout.cc +++ b/src/hb-ot-layout.cc @@ -31,6 +31,7 @@ #include "hb-open-type-private.hh" #include "hb-ot-layout-private.hh" +#include "hb-ot-cpal-table.hh" #include "hb-ot-layout-gdef-table.hh" #include "hb-ot-layout-gsub-table.hh" #include "hb-ot-layout-gpos-table.hh" @@ -60,6 +61,10 @@ _hb_ot_layout_create (hb_face_t *face) layout->gpos_blob = OT::Sanitizer::sanitize (face->reference_table (HB_OT_TAG_GPOS)); layout->gpos = OT::Sanitizer::lock_instance (layout->gpos_blob); + // The CPAL table is rarely accessed, so we only try to load it in _get_cpal. + layout->cpal_blob = NULL; + layout->cpal = NULL; + { /* * The ugly business of blacklisting individual fonts' tables happen here! @@ -131,6 +136,8 @@ _hb_ot_layout_destroy (hb_ot_layout_t *layout) hb_blob_destroy (layout->gsub_blob); hb_blob_destroy (layout->gpos_blob); + if (layout->cpal_blob) hb_blob_destroy (layout->cpal_blob); + free (layout); } diff --git a/src/hb-ot.h b/src/hb-ot.h index 47c92a5..0c0cf8d 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-tag.h" diff --git a/test/api/Makefile.am b/test/api/Makefile.am index d7d40af..895d154 100644 --- a/test/api/Makefile.am +++ b/test/api/Makefile.am @@ -44,6 +44,7 @@ endif if HAVE_OT TEST_PROGS += \ + test-ot-color \ test-ot-tag \ $(NULL) endif diff --git a/test/api/hb-test.h b/test/api/hb-test.h index 4d41218..9ce6adc 100644 --- a/test/api/hb-test.h +++ b/test/api/hb-test.h @@ -86,6 +86,36 @@ hb_test_run (void) } +/* 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; +} + + /* 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 0000000..7378a59 --- /dev/null +++ b/test/api/test-ot-color.c @@ -0,0 +1,318 @@ +/* + * 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 != r) { \ + 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 + + +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), ==, 0); + g_assert_cmpint (hb_ot_color_get_palette_name_id (hb_face_get_empty(), 1), ==, 0); +} + + +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), ==, 0); + g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v0, 1), ==, 0); + + /* numPalettes=2, so palette #2 is out of bounds */ + g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v0, 2), ==, 0); +} + + +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), ==, 0); + 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), ==, 0); +} + +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 */ +} + + +int +main (int argc, char **argv) +{ + int status = 0; + + hb_test_init (&argc, &argv); + cpal_v0 = hb_test_load_face ("../shaping/fonts/sha1sum/e90374e5e439e00725b4fe7a8d73db57c5a97f82.ttf"); + cpal_v1 = hb_test_load_face ("../shaping/fonts/sha1sum/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/fonts/sha1sum/319f5d7ebffbefc5c5e6569f8cea73444d7a7268.ttf b/test/shaping/fonts/sha1sum/319f5d7ebffbefc5c5e6569f8cea73444d7a7268.ttf new file mode 100644 index 0000000000000000000000000000000000000000..53044b7185c3edd56d71200a00d4e28764d65bac GIT binary patch literal 2128 zcmah~%WoS+82@J0Str3wX-MJ_6|=S@w<^?+#BmMK}cK>q7ocHNL1y*0U;r7twax1Bq&1s2}N+Is=}qH%Wr1a!A)9q>~FsJb9ZKm z5z$H7BZW$d%L_}?LT8Dbze0LC@%H4xpSQEOz&`-La3MK&5f~oLv%t+4lgm?r138MV z^>Q-Qm+@p_!*;=6OXc+vwf}Jj{C)7FX}waS9`YmJZSXIrbG6N{$Ksz5@i2gA41J^d zgSD5S`xg+)K%oA{XW$*s(S#-SxFui{3`nh+Gf7bMYZ!5 zW>=|?y(w%gM`_LKi3PVjzq&|ok!G5Th8pgm<~tC}*z6Ji5wr&O5nT~+WrYt>B}jk+ z(1Ge&C3aya@x0{{dd&ug0GJQJ@@u0us?o;`a=Ky;vFnP4DAeRVf^NXU2JdUsspAjr+&>V37NLs_OLc{vaQS#$rO%*`cb?4MYP0 zq3rDF>JJ2X9^0kLHErK+m#oS>-=37b~vG|kwwqrs+$PSCn7SLqZLZMp^WJ)3q>Cw*wqNYX-I z*fb-RZrU^l{jE52juzCCqwj5+Q9JY7G^aK;VABc>v4l-K$j9EZ=_cx7pV_q9Aa8-Z z9@9nV*w2;?>SZJLpbpRj2T`qf0SR4ZrGnW}a}>kCI>+Ime(>XlSR*QUz)j!_P3 zi$zUZRJ5{DG0NM4&} zM%0{MGs?aw5wLS}sv#1}!F;3>c? hz%=h}ngVa$MJ!;VJ%L3O1|Y^vlW~B}3z=L}`ycjUWYquw literal 0 HcmV?d00001 diff --git a/test/shaping/fonts/sha1sum/e90374e5e439e00725b4fe7a8d73db57c5a97f82.ttf b/test/shaping/fonts/sha1sum/e90374e5e439e00725b4fe7a8d73db57c5a97f82.ttf new file mode 100644 index 0000000000000000000000000000000000000000..66a9001bd78d2b68c8df7d40107bb82f686abf4e GIT binary patch literal 1948 zcmah}OKclO82)G0UdK^z+mOUnRLpv9xusA)64!Y+4;r_r(nKzbm6l7BY@GE&a$?JN z9u^mKLqSL!K!_eVz=cR$x%CDTIiMUsLYz1k#Hnf#5|8DZ*-dehR-N6M|NY$ApBNFH zp*AU0%iLaABrjbe@+^RRA@kO?1?%v)KF}r5+V$MrO+Z+9J_78znOm9>6zKPGbl=X! zhDw1lbl5KF)%=E0quyUHf_?~kqG&W~G)N)nPk=sKth5SWPNqL0;&E(VGL7}FclVYd z`x`q|0z>_Y&%!?_s0L0>S$=3&r18^&&pdI-6*w?S*0#)kQh zH~)ml1$+K&t=edQwLSM8k@_d(*JSR<|NVpIe^$;+JRwC9dD4&5ue5CV+CBxWQGTds zpgqI|7YjDZLu(X1f2RNi_@VS<%j{E;95>CATQX#qj%g`;nSBUp7vJL|+j$$ZWg22{ z2pikbwmu3hsHOS4S$dN+%ThGtaDbc-z?Ko%WBvfR2J$i86MkiekB}u$Y!`MPzIC^R zJvd2xZ@GkAb2ftm^atCv^O28Q^f3dU?pdSkzM{bkxr66|?Eb&sut|{yQVv*u@I&?k zZq%z3qba(Dkdes6DLyIN{IUVR28zT$w)*9`}+ol zBM}H8gDAuCp!a8~Dy6*SuVx!_mw95WsWmH9Bnb!X~vs_2y z)lO$8y74-tU(k)tg4GjYj<@E8?Lf`4Zkf;NwmiQbpB+^_-&sm$UvMF?dg&MR#a<}@ z8)ujvDF^5HU#1;gp$jzY;I0lGH@!q_4qv5nRCRDK_-zO8p?>-Rca6Ech*P1@9h^}w z^Eo&t9~1Xc=qNPGq7LpNofRD1O@o*ewvO7t_kw@qvxhFT2bpTERWBDyO>JKriYHRq zT1(3rjeN<_X6nX{S&wSjswOoWTHS1z^=)%KnyFT*bz^hWSTu`U6{DVr#^dp{_*c_U zMOPfz9kbpjS2s1Ob4q$FR;k&njmKg;J3G-eJ9xenqYOG#!~a;PG8L&rP5ig}*h3Vj z1ipZVuC{RI$e;%0p<#fUfxbaIWCBJ(XW>Qb@X?Tz2@9dIjdLA(B31=k*lyx6Xc3Yk zZGmfmn}AMSK51IQyt@m(>1SA7IbnC&?@5h2GLHsotRk+~5$XALiC#3(n{iB^7-|(y z6y4~oT=XM$G|R28(Y0qSF@6=snZjJ^R$?}pOsEy3W!B?1NC+E-`{cnaeVII#C8mSC nvp&E+_!sk(iyJ4WuE4p(&PqO!Wqn_;fMd5U=eC2Ci;43;LZU~_ literal 0 HcmV?d00001 -- 2.7.4