Files
harfbuzz/test/api/test-subset-cmap.c
Behdad EsfahbodandClaude Opus 4.8 c3fb304549 [subset] Only visit non-default UVS mappings in the DMAP cmap closure
cmap_accelerator_t::closure_glyphs() in the DMAP path expanded, for
every retained variation selector, all of the selector's Default UVS
ranges and resolved each code point with get_variation_glyph(). Any
number of 11-byte selector records may share one Default UVS table and
a 4-byte range covers 256 code points, so the work was bounded by
selectors x covered code points rather than by the table size: a 223 KB
font with 20000 selectors on one 168-range table drove ~860M lookups
and ~49 s of CPU in hb_subset_plan_create_or_fail().

A default mapping can only ever resolve to the nominal glyph, which the
subset plan already retains with the unicode itself, so the closure only
needs the code points that carry a Non-Default UVS mapping in cmap or
DMAP; get_variation_glyph() still decides which table wins for each.
This is exactly what the non-DMAP path (NonDefaultUVS::closure_glyphs)
already does. A collect_variation_non_default_unicodes() accessor is
added alongside the existing collect_variation_unicodes() chain
(VariationSelectorRecord, CmapSubtableFormat14_15, CmapSubtable,
cmap::accelerator_t) and used from the DMAP closure, so the work is one
lookup per 5-byte mapping visited.

No public API or ABI change; hb_face_collect_variation_unicodes() keeps
expanding Default UVS ranges as before, and fonts without DMAP take the
unchanged first branch. The reproducer's plan drops from ~49 s to 0.01 s
with the same 257-glyph result. The separate, older per-record
DefaultUVS::copy() serialization cost (~5.2 s for this font, independent
of DMAP) is left in place.

Adds test_subset_DMAP_shared_default_uvs to test/api/test-subset-cmap.c.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-8RP452C0.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-08 14:36:19 -06:00

625 lines
23 KiB
C

/*
* Copyright © 2018 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): Roderick Sheeter
*/
#include "hb-test.h"
#include "hb-subset-test.h"
/* Unit tests for cmap subsetting */
static void
test_subset_cmap (void)
{
hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
hb_face_t *face_ac = hb_test_open_font_file ("fonts/Roboto-Regular.ac.ttf");
hb_set_t *codepoints = hb_set_create ();
hb_face_t *face_abc_subset;
hb_set_add (codepoints, 97);
hb_set_add (codepoints, 99);
face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('c','m','a','p'));
hb_face_destroy (face_abc_subset);
hb_face_destroy (face_abc);
hb_face_destroy (face_ac);
}
static void
check_default_uvs_range_limit (unsigned int format)
{
static const char maxp_data[] = {0, 0, 0x50, 0, 3, 1};
char cmap_data[] = {
0, 0, 0, 2, /* cmap header */
0, 0, 0, 5, 0, 0, 0, 48, /* UVS encoding record */
0, 3, 0, 10, 0, 0, 0, 20, /* nominal encoding record */
0, 12, 0, 0, 0, 0, 0, 28, /* format 12 header */
0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0x10, 0, 0, 0, 0x12, (char) 0xFF, 0, 0, 0, 1,
0, 14, 0, 0, 0, 37, 0, 0, 0, 1, /* format 14 or 15 header */
0, (char) 0xFE, 0, 0, 0, 0, 21, 0, 0, 0, 0,
0, 0, 0, 3, /* three adjacent 256-codepoint ranges */
0, 0x10, 0, (char) 0xFF,
0, 0x11, 0, (char) 0xFF,
0, 0x12, 0, (char) 0xFF,
};
cmap_data[49] = format;
hb_face_t *face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "maxp", maxp_data);
HB_FACE_ADD_TABLE (face, "cmap", cmap_data);
const unsigned int counts[] = {1, 255, 256, 257, 512, 513, 768};
for (unsigned int i = 0; i < G_N_ELEMENTS (counts); i++)
{
hb_set_t *codepoints = hb_set_create ();
hb_set_add_range (codepoints, 0x1000, 0x1000 + counts[i] - 1);
hb_set_add (codepoints, 0xFE00);
hb_face_t *subset = hb_subset_test_create_subset (
face, hb_subset_test_create_input (codepoints));
hb_set_clear (codepoints);
hb_face_collect_variation_unicodes (subset, 0xFE00, codepoints);
g_assert_cmpuint (hb_set_get_population (codepoints), ==, counts[i]);
hb_font_t *font = hb_font_create (subset);
hb_codepoint_t glyph;
g_assert_true (hb_font_get_variation_glyph (font, 0x1000 + counts[i] - 1,
0xFE00, &glyph));
g_assert_cmpuint (glyph, ==, counts[i]);
hb_font_destroy (font);
hb_set_destroy (codepoints);
hb_face_destroy (subset);
}
hb_face_destroy (face);
}
static void
test_subset_cmap_default_uvs_range_limit (void)
{
check_default_uvs_range_limit (14);
}
#ifndef HB_NO_BEYOND_64K
static void
test_subset_cmap15_default_uvs_range_limit (void)
{
check_default_uvs_range_limit (15);
}
#endif
static void
test_subset_cmap_non_consecutive_glyphs (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/Roboto-Regular.D7,D8,D9,DA,DE.ttf");
hb_set_t *codepoints = hb_set_create ();
hb_face_t *face_subset;
hb_set_add (codepoints, 0xD7);
hb_set_add (codepoints, 0xD8);
hb_set_add (codepoints, 0xD9);
hb_set_add (codepoints, 0xDA);
hb_set_add (codepoints, 0xDE);
face_subset = hb_subset_test_create_subset (face, hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
hb_subset_test_check (face, face_subset, HB_TAG ('c','m','a','p'));
hb_face_destroy (face_subset);
hb_face_destroy (face);
}
static void
test_subset_cmap_noop (void)
{
hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
hb_set_t *codepoints = hb_set_create();
hb_face_t *face_abc_subset;
hb_set_add (codepoints, 97);
hb_set_add (codepoints, 98);
hb_set_add (codepoints, 99);
face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('c','m','a','p'));
hb_face_destroy (face_abc_subset);
hb_face_destroy (face_abc);
}
static void
test_subset_cmap4_no_exceeding_maximum_codepoint (void)
{
hb_face_t *face_origin = hb_test_open_font_file ("fonts/Mplus1p-Regular.ttf");
hb_face_t *face_expected = hb_test_open_font_file ("fonts/Mplus1p-Regular-cmap4-testing.ttf");
hb_set_t *codepoints = hb_set_create ();
hb_face_t *face_subset;
hb_set_add (codepoints, 0x20);
hb_set_add (codepoints, 0x21);
hb_set_add (codepoints, 0x1d542);
hb_set_add (codepoints, 0x201a2);
face_subset = hb_subset_test_create_subset (face_origin, hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
hb_subset_test_check (face_expected, face_subset, HB_TAG ('c','m','a','p'));
hb_face_destroy (face_subset);
hb_face_destroy (face_expected);
hb_face_destroy (face_origin);
}
static void
test_subset_cmap_empty_tables (void)
{
hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
hb_face_t *face_empty = hb_test_open_font_file ("fonts/Roboto-Regular.empty.ttf");
hb_set_t *codepoints = hb_set_create ();
hb_face_t *face_abc_subset;
hb_set_add (codepoints, 100);
hb_set_add (codepoints, 101);
face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
hb_subset_test_check (face_empty, face_abc_subset, HB_TAG ('c','m','a','p'));
hb_face_destroy (face_abc_subset);
hb_face_destroy (face_abc);
hb_face_destroy (face_empty);
}
static void
test_subset_cmap_noto_color_emoji_noop (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/NotoColorEmoji.cmap.ttf");
hb_set_t *codepoints = hb_set_create ();
hb_face_t *face_subset;
hb_set_add (codepoints, 0x38);
hb_set_add (codepoints, 0x39);
hb_set_add (codepoints, 0xAE);
hb_set_add (codepoints, 0x2049);
hb_set_add (codepoints, 0x20E3);
hb_set_add (codepoints, 0xfe0f);
face_subset = hb_subset_test_create_subset (face, hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
hb_subset_test_check (face, face_subset, HB_TAG ('c','m','a','p'));
hb_face_destroy (face_subset);
hb_face_destroy (face);
}
static void
test_subset_cmap_noto_color_emoji_non_consecutive_glyphs (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/NotoColorEmoji.cmap.ttf");
hb_face_t *face_expected = hb_test_open_font_file ("fonts/NotoColorEmoji.cmap.38,AE,2049.ttf");
hb_set_t *codepoints = hb_set_create ();
hb_face_t *face_subset;
hb_set_add (codepoints, 0x38);
hb_set_add (codepoints, 0xAE);
hb_set_add (codepoints, 0x2049);
hb_set_add (codepoints, 0xfe0f);
face_subset = hb_subset_test_create_subset (face, hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
hb_subset_test_check (face_expected, face_subset, HB_TAG ('c','m','a','p'));
hb_face_destroy (face_subset);
hb_face_destroy (face_expected);
hb_face_destroy (face);
}
static void
test_subset_DMAP (void)
{
const char maxp_data[] = {
0, 0, 0x50, 0, 0, 7,
};
const char cmap_data[] = {
0, 0, 0, 2, /* cmap header */
0, 3, 0, 10, 0, 0, 0, 20, /* encoding records */
0, 0, 0, 5, 0, 0, 0, 48,
0, 12, 0, 0, 0, 0, 0, 28, /* format 12 header */
0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 'A', 0, 0, 0, 'B', 0, 0, 0, 1, /* format 12 group */
0, 14, 0, 0, 0, 40, 0, 0, 0, 1, /* format 14 header */
0, 0xFE, 0x0F, 0, 0, 0, 0, 0, 0, 0, 21, /* variation selector */
0, 0, 0, 3, /* non-default UVS count */
0, 0, 'A', 0, 3,
0, 0, 'B', 0, 3,
0, 0, 'C', 0, 3,
};
const char dmap_data[] = {
0, 0, 0, 2, /* DMAP header */
0, 3, 0, 10, 0, 0, 0, 20, /* encoding records */
0, 0, 0, 5, 0, 0, 0, 60,
0, 12, 0, 0, 0, 0, 0, 40, /* format 12 header */
0, 0, 0, 0, 0, 0, 0, 2,
0, 0, 0, 'A', 0, 0, 0, 'A', 0, 0, 0, 4, /* format 12 groups */
0, 0, 0, 'C', 0, 0, 0, 'C', 0, 0, 0, 5,
0, 14, 0, 0, 0, 38, 0, 0, 0, 1, /* format 14 header */
0, 0xFE, 0x0F, 0, 0, 0, 21, 0, 0, 0, 29, /* variation selector */
0, 0, 0, 1, 0, 0, 'B', 0, /* default UVS */
0, 0, 0, 1, 0, 0, 'A', 0, 6, /* non-default UVS */
};
hb_face_t *face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "maxp", maxp_data);
HB_FACE_ADD_TABLE (face, "cmap", cmap_data);
HB_FACE_ADD_TABLE (face, "DMAP", dmap_data);
hb_font_t *font = hb_font_create (face);
hb_codepoint_t glyph;
g_assert_true (hb_font_get_nominal_glyph (font, 'A', &glyph));
g_assert_cmpuint (glyph, ==, 4);
g_assert_true (hb_font_get_nominal_glyph (font, 'B', &glyph));
g_assert_cmpuint (glyph, ==, 2);
g_assert_true (hb_font_get_nominal_glyph (font, 'C', &glyph));
g_assert_cmpuint (glyph, ==, 5);
const hb_codepoint_t unicodes[] = {'A', 'B', 'C'};
hb_codepoint_t glyphs[3];
g_assert_cmpuint (hb_font_get_nominal_glyphs (font, 3,
unicodes, sizeof (unicodes[0]),
glyphs, sizeof (glyphs[0])), ==, 3);
g_assert_cmpuint (glyphs[0], ==, 4);
g_assert_cmpuint (glyphs[1], ==, 2);
g_assert_cmpuint (glyphs[2], ==, 5);
g_assert_true (hb_font_get_variation_glyph (font, 'A', 0xFE0F, &glyph));
g_assert_cmpuint (glyph, ==, 6);
g_assert_true (hb_font_get_variation_glyph (font, 'B', 0xFE0F, &glyph));
g_assert_cmpuint (glyph, ==, 2);
g_assert_true (hb_font_get_variation_glyph (font, 'C', 0xFE0F, &glyph));
g_assert_cmpuint (glyph, ==, 3);
hb_font_destroy (font);
hb_set_t *set = hb_set_create ();
hb_map_t *mapping = hb_map_create ();
hb_face_collect_nominal_glyph_mapping (face, mapping, set);
g_assert_cmpuint (hb_set_get_population (set), ==, 3);
g_assert_cmpuint (hb_map_get (mapping, 'A'), ==, 4);
g_assert_cmpuint (hb_map_get (mapping, 'B'), ==, 2);
g_assert_cmpuint (hb_map_get (mapping, 'C'), ==, 5);
hb_map_destroy (mapping);
hb_set_clear (set);
hb_face_collect_variation_selectors (face, set);
g_assert_true (hb_set_has (set, 0xFE0F));
hb_set_clear (set);
hb_face_collect_variation_unicodes (face, 0xFE0F, set);
g_assert_cmpuint (hb_set_get_population (set), ==, 3);
g_assert_true (hb_set_has (set, 'A'));
g_assert_true (hb_set_has (set, 'B'));
g_assert_true (hb_set_has (set, 'C'));
hb_set_destroy (set);
hb_set_t *codepoints = hb_set_create ();
hb_set_add (codepoints, 'A');
hb_set_add (codepoints, 'B');
hb_set_add (codepoints, 'C');
hb_set_add (codepoints, 0xFE0F);
hb_face_t *subset = hb_subset_test_create_subset (face,
hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
hb_blob_t *dmap = hb_face_reference_table (subset, HB_TAG ('D','M','A','P'));
g_assert_cmpuint (hb_blob_get_length (dmap), >, 0);
hb_blob_destroy (dmap);
font = hb_font_create (subset);
g_assert_true (hb_font_get_nominal_glyph (font, 'A', &glyph));
g_assert_cmpuint (glyph, ==, 3);
g_assert_true (hb_font_get_nominal_glyph (font, 'B', &glyph));
g_assert_cmpuint (glyph, ==, 1);
g_assert_true (hb_font_get_nominal_glyph (font, 'C', &glyph));
g_assert_cmpuint (glyph, ==, 4);
g_assert_true (hb_font_get_variation_glyph (font, 'A', 0xFE0F, &glyph));
g_assert_cmpuint (glyph, ==, 5);
g_assert_true (hb_font_get_variation_glyph (font, 'B', 0xFE0F, &glyph));
g_assert_cmpuint (glyph, ==, 1);
g_assert_true (hb_font_get_variation_glyph (font, 'C', 0xFE0F, &glyph));
g_assert_cmpuint (glyph, ==, 2);
hb_font_destroy (font);
hb_face_destroy (subset);
hb_face_destroy (face);
}
/* Many encoding records sharing one format 12 subtable whose group spans
* all of Unicode: the subsetter must collect the subtable once, and only
* up to the font's glyph count, instead of once per record. */
static void
test_subset_cmap_shared_subtable_many_records (void)
{
const unsigned num_records = 4000;
const char maxp_data[] = {
0, 0, 0x50, 0, 0, 3,
};
unsigned cmap_len = 4 + 8 * num_records + 16 + 12;
char *cmap_data = calloc (1, cmap_len);
g_assert_nonnull (cmap_data);
char *p = cmap_data;
*p++ = 0; *p++ = 0; /* version */
*p++ = num_records >> 8; *p++ = num_records & 0xFF; /* numTables */
unsigned subtable_offset = 4 + 8 * num_records;
for (unsigned i = 0; i < num_records; i++)
{
*p++ = 0; *p++ = 3; *p++ = 0; *p++ = 10; /* platform 3, encoding 10 */
*p++ = subtable_offset >> 24; *p++ = subtable_offset >> 16;
*p++ = subtable_offset >> 8; *p++ = subtable_offset;
}
const char subtable[] = {
0, 12, 0, 0, 0, 0, 0, 28, /* format 12 header */
0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 'A', 0, 0x10, 0xFF, 0xFF, 0, 0, 0, 1, /* U+0041..U+10FFFF -> 1... */
};
memcpy (p, subtable, sizeof (subtable));
hb_face_t *face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "maxp", maxp_data);
hb_blob_t *cmap_blob = hb_blob_create_or_fail (cmap_data, cmap_len,
HB_MEMORY_MODE_WRITABLE,
cmap_data, free);
hb_face_builder_add_table (face, HB_TAG ('c','m','a','p'), cmap_blob);
hb_blob_destroy (cmap_blob);
hb_set_t *codepoints = hb_set_create ();
hb_set_add (codepoints, 'A');
hb_face_t *subset = hb_subset_test_create_subset (face,
hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
hb_font_t *font = hb_font_create (subset);
hb_codepoint_t glyph;
g_assert_true (hb_font_get_nominal_glyph (font, 'A', &glyph));
g_assert_cmpuint (glyph, ==, 1);
g_assert_false (hb_font_get_nominal_glyph (font, 'B', &glyph));
hb_font_destroy (font);
hb_face_destroy (subset);
hb_face_destroy (face);
}
/* Several encoding records that repeat one (platformID, encodingID) pair,
* two sharing a subtable and one with its own copy: the subset must keep
* only the first record of the pair, so the output cmap has a single
* encoding record and still maps the retained code point. */
static void
test_subset_cmap_repeated_encoding_records (void)
{
const char maxp_data[] = {
0, 0, 0x50, 0, 0, 3,
};
const char cmap_data[] = {
0, 0, 0, 3, /* version, numTables = 3 */
0, 3, 0, 10, 0, 0, 0, 28, /* (3,10) -> subtable A */
0, 3, 0, 10, 0, 0, 0, 28, /* (3,10) -> subtable A */
0, 3, 0, 10, 0, 0, 0, 56, /* (3,10) -> subtable B */
/* subtable A at 28 */
0, 12, 0, 0, 0, 0, 0, 28,
0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 'A', 0, 0x10, 0xFF, 0xFF, 0, 0, 0, 1,
/* subtable B at 56 */
0, 12, 0, 0, 0, 0, 0, 28,
0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 'A', 0, 0x10, 0xFF, 0xFF, 0, 0, 0, 1,
};
hb_face_t *face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "maxp", maxp_data);
HB_FACE_ADD_TABLE (face, "cmap", cmap_data);
hb_set_t *codepoints = hb_set_create ();
hb_set_add (codepoints, 'A');
hb_face_t *subset = hb_subset_test_create_subset (face,
hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
hb_blob_t *out = hb_face_reference_table (subset, HB_TAG ('c','m','a','p'));
unsigned out_len = 0;
const char *out_data = hb_blob_get_data (out, &out_len);
g_assert_cmpuint (out_len, >=, 4);
unsigned num_tables = ((unsigned char) out_data[2] << 8) | (unsigned char) out_data[3];
g_assert_cmpuint (num_tables, ==, 1);
hb_blob_destroy (out);
hb_font_t *font = hb_font_create (subset);
hb_codepoint_t glyph;
g_assert_true (hb_font_get_nominal_glyph (font, 'A', &glyph));
g_assert_cmpuint (glyph, ==, 1);
hb_font_destroy (font);
hb_face_destroy (subset);
hb_face_destroy (face);
}
static void
write_be (unsigned char *p, unsigned int value, unsigned int size)
{
while (size--)
{
p[size] = value & 0xFF;
value >>= 8;
}
}
static void
test_subset_DMAP_shared_default_uvs (void)
{
/* 20000 variation selectors sharing one Default UVS table whose ranges
* cover U+0000..U+A7FF, plus an empty DMAP so the subsetter takes the
* DMAP closure path. Only the first selector has a non-default mapping.
* The glyph closure must not resolve every covered codepoint for every
* selector; that used to take tens of seconds. */
static const char maxp_data[] = {0, 0, 0x50, 0, 0, 3};
static const char dmap_data[] = {0, 0, 0, 0};
static const unsigned char cmap_start[] = {
0, 0, 0, 2, /* cmap header */
0, 0, 0, 5, 0, 0, 0, 48, /* UVS encoding record */
0, 3, 0, 10, 0, 0, 0, 20, /* nominal encoding record */
0, 13, 0, 0, 0, 0, 0, 28, /* format 13 header */
0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0xA8, 0, 0, 0, 0, 1, /* U+0000..U+A800 -> glyph 1 */
};
const unsigned int num_selectors = 20000;
const unsigned int num_ranges = 0xA8;
const unsigned int default_uvs = 10 + 11 * num_selectors;
const unsigned int non_default_uvs = default_uvs + 4 + 4 * num_ranges;
const unsigned int format14_length = non_default_uvs + 4 + 5;
const unsigned int cmap_length = sizeof (cmap_start) + format14_length;
unsigned char *cmap_data = g_malloc0 (cmap_length);
unsigned char *format14 = cmap_data + sizeof (cmap_start);
memcpy (cmap_data, cmap_start, sizeof (cmap_start));
write_be (format14 + 0, 14, 2); /* format 14 header */
write_be (format14 + 2, format14_length, 4);
write_be (format14 + 6, num_selectors, 4);
for (unsigned int i = 0; i < num_selectors; i++)
{
unsigned char *record = format14 + 10 + 11 * i; /* variation selector */
write_be (record + 0, 0xE0100 + i, 3);
write_be (record + 3, default_uvs, 4);
write_be (record + 7, i ? 0 : non_default_uvs, 4);
}
write_be (format14 + default_uvs, num_ranges, 4); /* default UVS */
for (unsigned int i = 0; i < num_ranges; i++)
{
write_be (format14 + default_uvs + 4 + 4 * i, i << 8, 3);
write_be (format14 + default_uvs + 4 + 4 * i + 3, 0xFF, 1);
}
write_be (format14 + non_default_uvs, 1, 4); /* non-default UVS */
write_be (format14 + non_default_uvs + 4, 0xA800, 3);
write_be (format14 + non_default_uvs + 7, 2, 2);
hb_face_t *face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "maxp", maxp_data);
HB_FACE_ADD_TABLE (face, "DMAP", dmap_data);
hb_blob_t *blob = hb_blob_create ((const char *) cmap_data, cmap_length,
HB_MEMORY_MODE_WRITABLE, cmap_data, g_free);
g_assert_true (hb_face_builder_add_table (face, HB_TAG ('c','m','a','p'), blob));
hb_blob_destroy (blob);
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_invert (hb_subset_input_unicode_set (input));
hb_subset_plan_t *plan = hb_subset_plan_create_or_fail (face, input);
g_assert_nonnull (plan);
/* notdef, the nominal glyph and the one non-default variant. */
g_assert_cmpuint (hb_map_get_population (hb_subset_plan_old_to_new_glyph_mapping (plan)), ==, 3);
g_assert_true (hb_map_has (hb_subset_plan_old_to_new_glyph_mapping (plan), 2));
hb_subset_plan_destroy (plan);
hb_subset_input_destroy (input);
hb_face_destroy (face);
}
#ifndef HB_NO_BEYOND_64K
static void
test_subset_cmap_format15 (void)
{
const char MAXP_data[] = "\x00\x00\x50\x00\x01\x00\x02";
const char cmap_data[] = {
0, 0, 0, 3, /* cmap header */
0, 0, 0, 4, 0, 0, 0, 28, /* encoding records */
0, 0, 0, 5, 0, 0, 0, 56,
0, 0, 0, 5, 0, 0, 0, 86,
0, 12, 0, 0, 0, 0, 0, 28, /* format 12 header */
0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 'A', 0, 0, 0, 'A', 0, 1, 0, 1, /* format 12 group */
0, 14, 0, 0, 0, 30, 0, 0, 0, 1, /* format 14 header */
0, 0xFE, 0x0F, 0, 0, 0, 0, 0, 0, 0, 21, /* variation selector */
0, 0, 0, 1, 0, 0, 'A', 0, 2, /* non-default UVS */
0, 15, 0, 0, 0, 31, 0, 0, 0, 1, /* format 15 header */
0, 0xFE, 0x0F, 0, 0, 0, 0, 0, 0, 0, 21, /* variation selector */
0, 0, 0, 1, 0, 0, 'A', 1, 0, 1, /* non-default UVS */
};
hb_face_t *face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "MAXP", MAXP_data);
HB_FACE_ADD_TABLE (face, "cmap", cmap_data);
hb_font_t *font = hb_font_create (face);
hb_codepoint_t glyph;
g_assert_true (hb_font_get_variation_glyph (font, 'A', 0xFE0F, &glyph));
g_assert_cmpuint (glyph, ==, 0x10001);
hb_font_destroy (font);
hb_set_t *set = hb_set_create ();
hb_face_collect_variation_selectors (face, set);
g_assert_true (hb_set_has (set, 0xFE0F));
hb_set_clear (set);
hb_face_collect_variation_unicodes (face, 0xFE0F, set);
g_assert_true (hb_set_has (set, 'A'));
hb_set_destroy (set);
hb_set_t *codepoints = hb_set_create ();
hb_set_add (codepoints, 'A');
hb_set_add (codepoints, 0xFE0F);
hb_face_t *subset = hb_subset_test_create_subset (face,
hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
font = hb_font_create (subset);
g_assert_true (hb_font_get_variation_glyph (font, 'A', 0xFE0F, &glyph));
g_assert_cmpuint (glyph, ==, 2);
hb_font_destroy (font);
hb_face_destroy (subset);
hb_face_destroy (face);
}
#endif
// TODO(rsheeter) test cmap to no codepoints
int
main (int argc, char **argv)
{
hb_test_init (&argc, &argv);
hb_test_add (test_subset_cmap);
hb_test_add (test_subset_cmap_noop);
hb_test_add (test_subset_cmap_default_uvs_range_limit);
hb_test_add (test_subset_cmap_non_consecutive_glyphs);
hb_test_add (test_subset_cmap4_no_exceeding_maximum_codepoint);
hb_test_add (test_subset_cmap_empty_tables);
hb_test_add (test_subset_cmap_noto_color_emoji_noop);
hb_test_add (test_subset_cmap_noto_color_emoji_non_consecutive_glyphs);
hb_test_add (test_subset_DMAP);
hb_test_add (test_subset_cmap_shared_subtable_many_records);
hb_test_add (test_subset_cmap_repeated_encoding_records);
hb_test_add (test_subset_DMAP_shared_default_uvs);
#ifndef HB_NO_BEYOND_64K
hb_test_add (test_subset_cmap_format15);
hb_test_add (test_subset_cmap15_default_uvs_range_limit);
#endif
return hb_test_run();
}