[CPAL] Return 0xFFFF as name id for unnamed palettes

The name id 0 is used as Copyright notice. It's quite unlikely that a
font supplies a color palette with the exact same name as the font's
copyright notice, but the API should not prevent this.

Also, try to fix a problem with GObject introspection, where the
auto-generated Python bindings could not return palette colors.
This commit is contained in:
Sascha Brawer 2016-06-24 16:42:27 +02:00
parent d34d3ac985
commit 5967eaba72
2 changed files with 13 additions and 14 deletions

View File

@ -82,7 +82,8 @@ hb_ot_color_get_palette_count (hb_face_t *face)
* 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.
* the result is 0xFFFF. The implementation does not check whether
* the returned palette name id is actually in @face's `name` table.
*
* Since: 1.2.8
*/
@ -92,22 +93,20 @@ 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;
return 0xFFFF;
}
const OT::CPALV1Tail& cpal1 = OT::StructAfter<OT::CPALV1Tail>(cpal);
if (unlikely (&cpal1 == &OT::Null(OT::CPALV1Tail) ||
cpal1.paletteLabel.is_null())) {
return 0;
return 0xFFFF;
}
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;
return name_id;
}
@ -152,7 +151,7 @@ hb_ot_color_get_palette_flags (hb_face_t *face, unsigned int palette)
* @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):
* @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

View File

@ -135,19 +135,19 @@ 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);
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), ==, 0);
g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v0, 1), ==, 0);
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), ==, 0);
g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v0, 2), ==, 0xffff);
}
@ -155,11 +155,11 @@ 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, 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), ==, 0);
g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v1, 3), ==, 0xffff);
}
static void