diff --git a/src/hb-face-builder.cc b/src/hb-face-builder.cc index e4b232b8d..7e0c1fb72 100644 --- a/src/hb-face-builder.cc +++ b/src/hb-face-builder.cc @@ -48,6 +48,7 @@ struct face_table_info_t struct hb_face_builder_data_t { hb_hashmap_t tables; + hb_vector_t sorted_tags; /* Keys of tables, kept sorted. */ }; static int compare_entries (const void* pa, const void* pb) @@ -74,6 +75,7 @@ _hb_face_builder_data_create () return nullptr; data->tables.init (); + data->sorted_tags.init (); return data; } @@ -87,6 +89,7 @@ _hb_face_builder_data_destroy (void *user_data) hb_blob_destroy (info.data); data->tables.fini (); + data->sorted_tags.fini (); hb_free (data); } @@ -182,18 +185,8 @@ _hb_face_builder_get_table_tags (const hb_face_t *face HB_UNUSED, return population; } - // Sort the tags. - hb_vector_t sorted_tags; - data->tables.keys () | hb_sink (sorted_tags); - if (unlikely (sorted_tags.in_error ())) - { - // Not much to do... - } - sorted_tags.qsort ([] (const hb_tag_t &a, const hb_tag_t &b) { - return (a > b) - (a < b); - }); - - auto array = sorted_tags.as_array ().sub_array (start_offset, table_count); + // Tags are kept sorted by hb_face_builder_add_table(). + auto array = data->sorted_tags.as_array ().sub_array (start_offset, table_count); if (table_tags) { auto out = hb_array (table_tags, *table_count); @@ -259,9 +252,20 @@ hb_face_builder_add_table (hb_face_t *face, hb_tag_t tag, hb_blob_t *blob) hb_face_builder_data_t *data = (hb_face_builder_data_t *) face->user_data; + // Keep the tags sorted here, such that get_table_tags() does not + // have to sort them on every call. + auto &sorted_tags = data->sorted_tags; + unsigned i; + bool is_new = !hb_bsearch_impl (&i, tag, + sorted_tags.arrayZ, sorted_tags.length, + sizeof (hb_tag_t), + _hb_cmp_operator); + /* numTables in the font file's table directory is 16bit. */ - if (unlikely (data->tables.get_population () >= 0xFFFFu && - !data->tables.has (tag))) + if (unlikely (is_new && data->tables.get_population () >= 0xFFFFu)) + return false; + + if (is_new && unlikely (!sorted_tags.alloc (sorted_tags.length + 1))) return false; hb_blob_t* previous = data->tables.get (tag).data; @@ -271,6 +275,15 @@ hb_face_builder_add_table (hb_face_t *face, hb_tag_t tag, hb_blob_t *blob) return false; } + if (is_new) + { + sorted_tags.resize (sorted_tags.length + 1); // Cannot fail; allocated above. + memmove (sorted_tags.arrayZ + i + 1, + sorted_tags.arrayZ + i, + (sorted_tags.length - 1 - i) * sorted_tags.item_size); + sorted_tags.arrayZ[i] = tag; + } + hb_blob_destroy (previous); return true; } diff --git a/test/api/test-get-table-tags.c b/test/api/test-get-table-tags.c index b417f0094..2fb1f2fd2 100644 --- a/test/api/test-get-table-tags.c +++ b/test/api/test-get-table-tags.c @@ -120,6 +120,53 @@ test_get_table_tags_builder_limit (void) hb_face_destroy (face); } +static void +test_get_table_tags_builder_unsorted (void) +{ + /* Tags added out of order, one of them twice; must come back + * sorted, each once, with any page size. */ + static const hb_tag_t added[] = { + HB_TAG ('m','a','x','p'), + HB_TAG ('G','S','U','B'), + HB_TAG ('h','e','a','d'), + HB_TAG ('z','z','z','z'), + HB_TAG ('h','e','a','d'), + HB_TAG ('c','m','a','p'), + HB_TAG ('G','D','E','F'), + }; + static const hb_tag_t expected[] = { + HB_TAG ('G','D','E','F'), + HB_TAG ('G','S','U','B'), + HB_TAG ('c','m','a','p'), + HB_TAG ('h','e','a','d'), + HB_TAG ('m','a','x','p'), + HB_TAG ('z','z','z','z'), + }; + const unsigned num_expected = sizeof (expected) / sizeof (expected[0]); + + hb_face_t *face = hb_face_builder_create (); + hb_blob_t *blob = hb_blob_create ("test", 4, HB_MEMORY_MODE_READONLY, NULL, NULL); + for (unsigned i = 0; i < sizeof (added) / sizeof (added[0]); i++) + g_assert_true (hb_face_builder_add_table (face, added[i], blob)); + hb_blob_destroy (blob); + + g_assert_cmpuint (num_expected, ==, hb_face_get_table_tags (face, 0, NULL, NULL)); + + hb_tag_t tags[4]; + unsigned offset = 0, count = sizeof (tags) / sizeof (tags[0]); + while (((void) hb_face_get_table_tags (face, offset, &count, tags), count)) + { + g_assert_cmpuint (offset + count, <=, num_expected); + for (unsigned i = 0; i < count; i++) + g_assert_cmpuint (tags[i], ==, expected[offset + i]); + offset += count; + count = sizeof (tags) / sizeof (tags[0]); + } + g_assert_cmpuint (offset, ==, num_expected); + + hb_face_destroy (face); +} + #ifdef HAVE_FREETYPE static void test_get_table_tags_ft (void) @@ -254,6 +301,7 @@ main (int argc, char **argv) hb_test_add (test_get_table_tags_default); hb_test_add (test_get_table_tags_builder); hb_test_add (test_get_table_tags_builder_limit); + hb_test_add (test_get_table_tags_builder_unsorted); #ifdef HAVE_FREETYPE hb_test_add (test_get_table_tags_ft); hb_test_add (test_get_table_tags_ft_stream);