diff --git a/src/hb-ot-cmap-table.hh b/src/hb-ot-cmap-table.hh index 8bad1da72..cf7d78474 100644 --- a/src/hb-ot-cmap-table.hh +++ b/src/hb-ot-cmap-table.hh @@ -615,10 +615,16 @@ struct CmapSubtableFormat4 unsigned int count = this->segCount; if (count && this->startCount[count - 1] == 0xFFFFu) count--; /* Skip sentinel segment. */ + hb_codepoint_t next = 0; for (unsigned int i = 0; i < count; i++) { - hb_codepoint_t start = this->startCount[i]; + /* Segments must be sorted and disjoint; never walk a codepoint twice, + * so that the total work is bounded regardless of segment count. */ + hb_codepoint_t start = hb_max ((hb_codepoint_t) this->startCount[i], next); hb_codepoint_t end = this->endCount[i]; + if (unlikely (start > end)) + continue; + next = end + 1; unsigned int rangeOffset = this->idRangeOffset[i]; out->add_range(start, end); if (rangeOffset == 0) @@ -656,10 +662,16 @@ struct CmapSubtableFormat4 unsigned count = this->segCount; if (count && this->startCount[count - 1] == 0xFFFFu) count--; /* Skip sentinel segment. */ + hb_codepoint_t next = 0; for (unsigned i = 0; i < count; i++) { - hb_codepoint_t start = this->startCount[i]; + /* Segments must be sorted and disjoint; never walk a codepoint twice, + * so that the total work is bounded regardless of segment count. */ + hb_codepoint_t start = hb_max ((hb_codepoint_t) this->startCount[i], next); hb_codepoint_t end = this->endCount[i]; + if (unlikely (start > end)) + continue; + next = end + 1; unsigned rangeOffset = this->idRangeOffset[i]; if (rangeOffset == 0) { diff --git a/test/api/test-collect-unicodes.c b/test/api/test-collect-unicodes.c index 735ff7c1d..fe0c77104 100644 --- a/test/api/test-collect-unicodes.c +++ b/test/api/test-collect-unicodes.c @@ -107,6 +107,71 @@ test_collect_unicodes_format4_with_range_offsets_and_deltas (void) hb_face_destroy (face); } +static void +test_collect_unicodes_format4_overlapping_segments (void) +{ + /* Segments overlap and one lies entirely inside an earlier one; each + * codepoint must be collected once, from the first segment covering it. */ + const char cmap_data[60] = { + 0, 0, 0, 1, /* cmap header */ + 0, 0, 0, 3, 0, 0, 0, 12, /* encoding records */ + /* cmap 4 */ + 0, 4, /* format */ + 0, 48, /* length */ + 0, 0, /* lang */ + 0, 8, /* segcountx2 */ + 0, 0, /* search range */ + 0, 0, /* entry selector */ + 0, 0, /* range shift */ + 0, 0x45, /* end code [0] */ + 0, 0x47, /* end code [1] */ + 0, 0x47, /* end code [2] */ + -1, -1, /* end code [3] */ + 0, 0, /* pad */ + 0, 0x41, /* start code [0] */ + 0, 0x43, /* start code [1] */ + 0, 0x40, /* start code [2] */ + -1, -1, /* start code [3] */ + 0, 0, /* id delta [0] */ + 1, 0, /* id delta [1] */ + 2, 0, /* id delta [2] */ + 0, 1, /* id delta [3] */ + 0, 0, /* id range offset [0] */ + 0, 0, /* id range offset [1] */ + 0, 0, /* id range offset [2] */ + 0, 0, /* id range offset [3] */ + }; + + hb_blob_t* cmap = hb_blob_create(cmap_data, 60, HB_MEMORY_MODE_READONLY, 0, 0); + hb_face_t* face = hb_face_builder_create(); + hb_face_builder_add_table(face, HB_TAG('c', 'm', 'a', 'p'), cmap); + hb_blob_destroy(cmap); + + hb_set_t *codepoints = hb_set_create(); + hb_map_t *mapping = hb_map_create(); + hb_codepoint_t cp; + + hb_face_collect_unicodes (face, codepoints); + + g_assert_cmpuint (hb_set_get_population (codepoints), ==, 7); + g_assert_cmpuint (hb_set_get_min (codepoints), ==, 0x41); + g_assert_cmpuint (hb_set_get_max (codepoints), ==, 0x47); + + hb_set_clear(codepoints); + hb_face_collect_nominal_glyph_mapping(face, mapping, codepoints); + + g_assert_cmpuint (hb_set_get_population (codepoints), ==, 7); + g_assert_cmpuint (hb_map_get_population (mapping), ==, 7); + for (cp = 0x41; cp <= 0x45; cp++) + g_assert_cmpuint (hb_map_get(mapping, cp), ==, cp); + g_assert_cmpuint (hb_map_get(mapping, 0x46), ==, 0x146); + g_assert_cmpuint (hb_map_get(mapping, 0x47), ==, 0x147); + + hb_map_destroy(mapping); + hb_set_destroy (codepoints); + hb_face_destroy (face); +} + static void test_collect_unicodes_format12_notdef (void) { @@ -275,6 +340,7 @@ main (int argc, char **argv) hb_test_add (test_collect_unicodes_format12_clamped); hb_test_add (test_collect_unicodes_format13); hb_test_add (test_collect_unicodes_format4_with_range_offsets_and_deltas); + hb_test_add (test_collect_unicodes_format4_overlapping_segments); return hb_test_run(); }