Support CPAL table

This commit is contained in:
Sascha Brawer 2016-04-25 18:20:57 +02:00
parent 07461d06d2
commit d34d3ac985
14 changed files with 804 additions and 1 deletions

6
NEWS
View File

@ -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
====================================

View File

@ -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)

View File

@ -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 \

213
src/hb-ot-color.cc Normal file
View File

@ -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 <stdlib.h>
#include <string.h>
#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<OT::CPAL>::sanitize (face->reference_table (HB_OT_TAG_CPAL));
layout->cpal = OT::Sanitizer<OT::CPAL>::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<OT::CPALV1Tail>(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<OT::CPALV1Tail>(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<uint32_t> (flags [palette]);
return static_cast<hb_ot_color_palette_flags_t> (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;
}

99
src/hb-ot-color.h Normal file
View File

@ -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 <hb-ot.h> 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 */

121
src/hb-ot-cpal-table.hh Normal file
View File

@ -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<ULONG, ULONG> paletteFlags;
OffsetTo<USHORT, ULONG> paletteLabel;
OffsetTo<USHORT, ULONG> 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<CPALV1Tail>(*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<ColorRecord, ULONG> offsetFirstColorRecord;
USHORT colorRecordIndices[VAR]; // VAR=numPalettes
public:
DEFINE_SIZE_ARRAY (12, colorRecordIndices);
};
} /* namespace OT */
#endif /* HB_OT_CPAL_TABLE_HH */

View File

@ -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;

View File

@ -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<OT::GPOS>::sanitize (face->reference_table (HB_OT_TAG_GPOS));
layout->gpos = OT::Sanitizer<OT::GPOS>::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);
}

View File

@ -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"

View File

@ -44,6 +44,7 @@ endif
if HAVE_OT
TEST_PROGS += \
test-ot-color \
test-ot-tag \
$(NULL)
endif

View File

@ -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

318
test/api/test-ot-color.c Normal file
View File

@ -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 <hb-ot.h>
#include <stdlib.h>
#include <stdio.h>
/* Unit tests for hb-ot-color.h */
/* Test font with the following CPAL v0 table, as TTX and manual disassembly:
<CPAL>
<version value="0"/>
<numPaletteEntries value="2"/>
<palette index="0">
<color index="0" value="#000000FF"/>
<color index="1" value="#66CCFFFF"/>
</palette>
<palette index="1">
<color index="0" value="#000000FF"/>
<color index="1" value="#800000FF"/>
</palette>
</CPAL>
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:
<CPAL>
<version value="1"/>
<numPaletteEntries value="2"/>
<palette index="0" label="257" type="2">
<color index="0" value="#000000FF"/>
<color index="1" value="#66CCFFFF"/>
</palette>
<palette index="1" label="65535" type="1">
<color index="0" value="#000000FF"/>
<color index="1" value="#FFCC66FF"/>
</palette>
<palette index="2" label="258" type="0">
<color index="0" value="#000000FF"/>
<color index="1" value="#800000FF"/>
</palette>
<paletteEntryLabels>
<label index="0" value="65535"/>
<label index="1" value="256"/>
</paletteEntryLabels>
</CPAL>
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;
}