[repacker] bug fix for splitting shared subtables (#5686)

And fix splitting for ligatureSubst table with shared coverage table
This commit is contained in:
Qunxin Liu authored and GitHub committed 2025-12-19 21:42:20 -07:00
1 parent 5ef5240e94
commit babdb46be3
7 files changed
+265 -106

No files matched your search

+28 -18
View File
@@ -1097,24 +1097,6 @@ struct graph_t
return clone_idx;
}
/*
* Creates a copy of child and re-assigns the link from
* parent to the clone. The copy is a shallow copy, objects
* linked from child are not duplicated.
*
* Returns the index of the newly created duplicate.
*
* If the child_idx only has incoming edges from parent_idx, this
* will do nothing and return the original child_idx.
*/
unsigned duplicate_if_shared (unsigned parent_idx, unsigned child_idx)
{
unsigned new_idx = duplicate (parent_idx, child_idx);
if (new_idx == (unsigned) -1) return child_idx;
return new_idx;
}
/*
* Creates a copy of child and re-assigns the link from
* parent to the clone. The copy is a shallow copy, objects
@@ -1258,6 +1240,34 @@ struct graph_t
return clone_idx;
}
/*
* Creates a new child node and remap the old child to it.
*
* Returns the index of the newly created child.
*
*/
unsigned remap_child (unsigned parent_idx, unsigned old_child_idx)
{
unsigned new_child_idx = duplicate (old_child_idx);
if (new_child_idx == (unsigned) -1) return -1;
auto& parent = vertices_[parent_idx];
for (auto& l : parent.obj.real_links)
{
if (l.objidx != old_child_idx)
continue;
reassign_link (l, parent_idx, new_child_idx, false);
}
for (auto& l : parent.obj.virtual_links)
{
if (l.objidx != old_child_idx)
continue;
reassign_link (l, parent_idx, new_child_idx, true);
}
return new_child_idx;
}
/*
* Raises the sorting priority of all children.
*/
+1
View File
@@ -41,6 +41,7 @@ struct gsubgpos_graph_context_t
unsigned lookup_list_index;
hb_hashmap_t<unsigned, graph::Lookup*> lookups;
hb_hashmap_t<unsigned, unsigned> subtable_to_extension;
hb_hashmap_t<unsigned, hb_vector_t<unsigned>> split_subtables;
HB_INTERNAL gsubgpos_graph_context_t (hb_tag_t table_tag_,
graph_t& graph_);
+36 -27
View File
@@ -138,10 +138,8 @@ struct Lookup : public OT::Lookup
for (unsigned i = 0; i < subTable.len; i++)
{
unsigned subtable_index = c.graph.index_for_offset (this_index, &subTable[i]);
unsigned parent_index = this_index;
if (is_ext) {
unsigned ext_subtable_index = subtable_index;
parent_index = ext_subtable_index;
ExtensionFormat1<OT::Layout::GSUB_impl::ExtensionSubst>* extension =
(ExtensionFormat1<OT::Layout::GSUB_impl::ExtensionSubst>*)
c.graph.object (ext_subtable_index).head;
@@ -154,31 +152,43 @@ struct Lookup : public OT::Lookup
continue;
}
hb_vector_t<unsigned> new_sub_tables;
if (c.table_tag == HB_OT_TAG_GPOS) {
switch (type)
{
case 2:
new_sub_tables = split_subtable<PairPos> (c, parent_index, subtable_index); break;
case 4:
new_sub_tables = split_subtable<MarkBasePos> (c, parent_index, subtable_index); break;
default:
break;
}
} else if (c.table_tag == HB_OT_TAG_GSUB) {
switch (type)
{
case 4:
new_sub_tables = split_subtable<graph::LigatureSubst> (c, parent_index, subtable_index); break;
default:
break;
}
hb_vector_t<unsigned>* split_result;
if (c.split_subtables.has (subtable_index, &split_result))
{
if (split_result->length == 0)
continue;
all_new_subtables.push (hb_pair(i, *split_result));
}
else
{
hb_vector_t<unsigned> new_sub_tables;
if (new_sub_tables.in_error ()) return false;
if (!new_sub_tables) continue;
all_new_subtables.push (hb_pair (i, std::move (new_sub_tables)));
if (c.table_tag == HB_OT_TAG_GPOS) {
switch (type)
{
case 2:
new_sub_tables = split_subtable<PairPos> (c, subtable_index); break;
case 4:
new_sub_tables = split_subtable<MarkBasePos> (c, subtable_index); break;
default:
break;
}
} else if (c.table_tag == HB_OT_TAG_GSUB) {
switch (type)
{
case 4:
new_sub_tables = split_subtable<graph::LigatureSubst> (c, subtable_index); break;
default:
break;
}
}
if (new_sub_tables.in_error ()) return false;
c.split_subtables.set (subtable_index, new_sub_tables);
if (new_sub_tables)
all_new_subtables.push (hb_pair (i, std::move (new_sub_tables)));
}
}
if (all_new_subtables) {
@@ -190,14 +200,13 @@ struct Lookup : public OT::Lookup
template<typename T>
hb_vector_t<unsigned> split_subtable (gsubgpos_graph_context_t& c,
unsigned parent_idx,
unsigned objidx)
{
T* sub_table = (T*) c.graph.object (objidx).head;
if (!sub_table || !sub_table->sanitize (c.graph.vertices_[objidx]))
return hb_vector_t<unsigned> ();
return sub_table->split_subtables (c, parent_idx, objidx);
return sub_table->split_subtables (c, objidx);
}
bool add_sub_tables (gsubgpos_graph_context_t& c,
+19 -13
View File
@@ -67,14 +67,13 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
}
hb_vector_t<unsigned> split_subtables (gsubgpos_graph_context_t& c,
unsigned parent_index,
unsigned this_index)
{
auto split_points = compute_split_points(c, parent_index, this_index);
auto split_points = compute_split_points(c, this_index);
split_context_t split_context {
c,
this,
c.graph.duplicate_if_shared (parent_index, this_index),
this_index,
total_number_ligas(c, this_index),
liga_counts(c, this_index),
};
@@ -123,7 +122,6 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
}
hb_vector_t<unsigned> compute_split_points(gsubgpos_graph_context_t& c,
unsigned parent_index,
unsigned this_index) const
{
// For ligature subst coverage is always packed last, and as a result is where an overflow
@@ -441,26 +439,35 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
}
// Adjust liga set array
c.graph.vertices_[this_index].obj.tail -= (ligatureSet.len - new_liga_set_count) * SmallTypes::size;
auto& this_vertex = c.graph.vertices_[this_index];
this_vertex.obj.tail -= (ligatureSet.len - new_liga_set_count) * SmallTypes::size;
ligatureSet.len = new_liga_set_count;
// Coverage matches the number of liga sets so rebuild as needed
auto coverage = c.graph.as_mutable_table<Coverage> (this_index, &this->coverage);
if (!coverage) return false;
unsigned coverage_idx = c.graph.index_for_offset (this_index, &this->coverage);
if (coverage_idx == (unsigned) -1) return false;
auto& coverage_v = c.graph.vertices_[coverage_idx];
if (coverage_v.is_shared ())
{
coverage_idx = c.graph.remap_child (this_index, coverage_idx);
if (coverage_idx == (unsigned) -1) return false;
}
for (unsigned i : retained_indices.iter())
add_virtual_link(c, i, coverage.index);
add_virtual_link(c, i, coverage_idx);
unsigned coverage_size = coverage.vertex->table_size ();
unsigned coverage_size = coverage_v.table_size ();
Coverage* coverage_table = (Coverage*) coverage_v.obj.head;
auto new_coverage =
+ hb_zip (coverage.table->iter (), hb_range ())
+ hb_zip (coverage_table->iter (), hb_range ())
| hb_filter ([&] (hb_pair_t<unsigned, unsigned> p) {
return p.second < new_liga_set_count;
})
| hb_map_retains_sorting (hb_first)
;
return Coverage::make_coverage (c, new_coverage, coverage.index, coverage_size);
return Coverage::make_coverage (c, new_coverage, coverage_idx, coverage_size);
}
};
@@ -468,12 +475,11 @@ struct LigatureSubst : public OT::Layout::GSUB_impl::LigatureSubst
{
hb_vector_t<unsigned> split_subtables (gsubgpos_graph_context_t& c,
unsigned parent_index,
unsigned this_index)
{
switch (u.format.v) {
case 1:
return ((LigatureSubstFormat1*)(&u.format1))->split_subtables (c, parent_index, this_index);
return ((LigatureSubstFormat1*)(&u.format1))->split_subtables (c, this_index);
#ifndef HB_NO_BEYOND_64K
case 2: HB_FALLTHROUGH;
// Don't split 24bit Ligature Subs
+2 -4
View File
@@ -212,7 +212,6 @@ struct MarkBasePosFormat1 : public OT::Layout::GPOS_impl::MarkBasePosFormat1_2<S
}
hb_vector_t<unsigned> split_subtables (gsubgpos_graph_context_t& c,
unsigned parent_index,
unsigned this_index)
{
hb_set_t visited;
@@ -265,7 +264,7 @@ struct MarkBasePosFormat1 : public OT::Layout::GPOS_impl::MarkBasePosFormat1_2<S
split_context_t split_context {
c,
this,
c.graph.duplicate_if_shared (parent_index, this_index),
this_index,
std::move (class_to_info),
c.graph.vertices_[mark_array_id].position_to_index_map (),
};
@@ -478,12 +477,11 @@ struct MarkBasePosFormat1 : public OT::Layout::GPOS_impl::MarkBasePosFormat1_2<S
struct MarkBasePos : public OT::Layout::GPOS_impl::MarkBasePos
{
hb_vector_t<unsigned> split_subtables (gsubgpos_graph_context_t& c,
unsigned parent_index,
unsigned this_index)
{
switch (u.format.v) {
case 1:
return ((MarkBasePosFormat1*)(&u.format1))->split_subtables (c, parent_index, this_index);
return ((MarkBasePosFormat1*)(&u.format1))->split_subtables (c, this_index);
#ifndef HB_NO_BEYOND_64K
case 2: HB_FALLTHROUGH;
// Don't split 24bit MarkBasePos's.
+4 -7
View File
@@ -49,7 +49,6 @@ struct PairPosFormat1 : public OT::Layout::GPOS_impl::PairPosFormat1_3<SmallType
}
hb_vector_t<unsigned> split_subtables (gsubgpos_graph_context_t& c,
unsigned parent_index,
unsigned this_index)
{
hb_set_t visited;
@@ -84,7 +83,7 @@ struct PairPosFormat1 : public OT::Layout::GPOS_impl::PairPosFormat1_3<SmallType
split_context_t split_context {
c,
this,
c.graph.duplicate_if_shared (parent_index, this_index),
this_index,
};
return actuate_subtable_split<split_context_t> (split_context, split_points);
@@ -207,7 +206,6 @@ struct PairPosFormat2 : public OT::Layout::GPOS_impl::PairPosFormat2_4<SmallType
}
hb_vector_t<unsigned> split_subtables (gsubgpos_graph_context_t& c,
unsigned parent_index,
unsigned this_index)
{
const unsigned base_size = OT::Layout::GPOS_impl::PairPosFormat2_4<SmallTypes>::min_size;
@@ -291,7 +289,7 @@ struct PairPosFormat2 : public OT::Layout::GPOS_impl::PairPosFormat2_4<SmallType
split_context_t split_context {
c,
this,
c.graph.duplicate_if_shared (parent_index, this_index),
this_index,
class1_record_size,
total_value_len,
value_1_len,
@@ -607,14 +605,13 @@ struct PairPosFormat2 : public OT::Layout::GPOS_impl::PairPosFormat2_4<SmallType
struct PairPos : public OT::Layout::GPOS_impl::PairPos
{
hb_vector_t<unsigned> split_subtables (gsubgpos_graph_context_t& c,
unsigned parent_index,
unsigned this_index)
{
switch (u.format.v) {
case 1:
return ((PairPosFormat1*)(&u.format1))->split_subtables (c, parent_index, this_index);
return ((PairPosFormat1*)(&u.format1))->split_subtables (c, this_index);
case 2:
return ((PairPosFormat2*)(&u.format2))->split_subtables (c, parent_index, this_index);
return ((PairPosFormat2*)(&u.format2))->split_subtables (c, this_index);
#ifndef HB_NO_BEYOND_64K
case 3: HB_FALLTHROUGH;
case 4: HB_FALLTHROUGH;
+175 -37
View File
@@ -49,10 +49,11 @@ static void start_object(const char* tag,
static unsigned add_object(const char* tag,
unsigned len,
hb_serialize_context_t* c)
hb_serialize_context_t* c,
bool shared = false)
{
start_object (tag, len, c);
return c->pop_pack (false);
return c->pop_pack (shared);
}
@@ -114,7 +115,7 @@ static void start_lookup (int8_t type,
{
char lookup[] = {
0, (char)type, // type
0, 0, // flag
0, (char)2, // flag
0, (char)num_subtables, // num subtables
};
@@ -146,7 +147,8 @@ static unsigned add_extension (unsigned child,
// Adds coverage table fro [start, end]
static unsigned add_coverage (unsigned start, unsigned end,
hb_serialize_context_t* c)
hb_serialize_context_t* c,
bool shared = false)
{
if (end - start == 0) {
uint8_t coverage[] = {
@@ -156,7 +158,7 @@ static unsigned add_coverage (unsigned start, unsigned end,
(uint8_t) ((start >> 8) & 0xFF),
(uint8_t) (start & 0xFF), // glyph[0]
};
return add_object ((char*) coverage, 6, c);
return add_object ((char*) coverage, 6, c, shared);
}
if (end - start == 1)
@@ -171,7 +173,7 @@ static unsigned add_coverage (unsigned start, unsigned end,
(uint8_t) ((end >> 8) & 0xFF),
(uint8_t) (end & 0xFF), // glyph[1]
};
return add_object ((char*) coverage, 8, c);
return add_object ((char*) coverage, 8, c, shared);
}
uint8_t coverage[] = {
@@ -186,7 +188,7 @@ static unsigned add_coverage (unsigned start, unsigned end,
0, 0,
};
return add_object ((char*) coverage, 10, c);
return add_object ((char*) coverage, 10, c, shared);
}
@@ -1638,59 +1640,75 @@ populate_serializer_with_large_mark_base_pos_1 (hb_serialize_context_t* c)
c->end_serialize();
}
template<int liga_subst_count,
int liga_set_count,
int liga_per_set_count,
int liga_size>
static void
populate_serializer_with_large_liga (hb_serialize_context_t* c, bool sequential_liga_sets)
template<unsigned LigSubstCount,
unsigned LigaSetCount,
unsigned LigaPerSetCount,
unsigned LigaSize>
static void populate_serializer_with_large_ligsubst(hb_serialize_context_t* c,
bool sequential_liga_sets,
bool shared,
unsigned *liga_subst_id,
bool unique_lig_str = false)
{
std::string large_string(100000, 'a');
c->start_serialize<char> ();
char ch = 'a';
unsigned liga_subst[liga_subst_count];
for (unsigned l = 0; l < liga_subst_count; l++) {
unsigned liga[LigaSetCount * LigaPerSetCount];
unsigned liga_set[LigaSetCount];
for (unsigned l = 0; l < LigSubstCount; l++) {
unsigned coverage_start = 0;
unsigned coverage_end = liga_set_count - 1;
unsigned coverage_end = LigaSetCount - 1;
if (sequential_liga_sets) {
coverage_start = l * liga_set_count;
coverage_end = (l + 1) * liga_set_count - 1;
coverage_start = l * LigaSetCount;
coverage_end = (l + 1) * LigaSetCount - 1;
}
unsigned coverage = add_coverage(coverage_start, coverage_end, c);
unsigned coverage = add_coverage(coverage_start, coverage_end, c, shared);
unsigned liga[liga_set_count * liga_per_set_count];
unsigned liga_set[liga_set_count];
for (unsigned i = 0; i < liga_set_count; i++) {
for (unsigned j = 0; j < liga_per_set_count; j++)
for (unsigned i = 0; i < LigaSetCount; i++) {
for (unsigned j = 0; j < LigaPerSetCount; j++)
{
start_object (large_string.c_str(), liga_size, c);
std::string large_string(100000, ch);
start_object (large_string.c_str(), LigaSize, c);
if (unique_lig_str)
ch += 1;
add_virtual_offset(coverage, c);
liga[i * liga_per_set_count + j] = c->pop_pack (false);
liga[i * LigaPerSetCount + j] = c->pop_pack (shared);
}
add_liga_set_header(liga_per_set_count, c);
add_liga_set_header(LigaPerSetCount, c);
add_virtual_offset(coverage, c);
for (unsigned j = 0; j < liga_per_set_count; j++)
for (unsigned j = 0; j < LigaPerSetCount; j++)
{
add_offset(liga[i * liga_per_set_count + j], c);
add_offset(liga[i * LigaPerSetCount + j], c);
}
liga_set[i] = c->pop_pack(false);
liga_set[i] = c->pop_pack(shared);
}
add_liga_header(coverage, liga_set_count, c);
for (unsigned i = 0; i < liga_set_count; i++)
add_liga_header(coverage, LigaSetCount, c);
for (unsigned i = 0; i < LigaSetCount; i++)
{
add_offset(liga_set[i], c);
}
liga_subst[l] = c->pop_pack(false);
liga_subst_id[l] = c->pop_pack(shared);
}
for (unsigned l = 0; l < liga_subst_count; l++) {
liga_subst[l] = add_extension(liga_subst[l], 4, c);
for (unsigned l = 0; l < LigSubstCount; l++) {
liga_subst_id[l] = add_extension(liga_subst_id[l], 4, c);
}
}
template<unsigned liga_subst_count,
unsigned liga_set_count,
unsigned liga_per_set_count,
unsigned liga_size>
static void
populate_serializer_with_large_liga (hb_serialize_context_t* c, bool sequential_liga_sets)
{
c->start_serialize<char> ();
unsigned liga_subst[liga_subst_count];
populate_serializer_with_large_ligsubst<liga_subst_count, liga_set_count, liga_per_set_count, liga_size>(c, sequential_liga_sets, false, liga_subst);
start_lookup (7, liga_subst_count, c);
for (unsigned l = 0; l < liga_subst_count; l++) {
@@ -1795,6 +1813,78 @@ populate_serializer_with_large_liga_overlapping_clone_result (hb_serialize_conte
c->end_serialize();
}
template<unsigned liga_subst_count,
unsigned liga_set_count,
unsigned liga_per_set_count,
unsigned liga_size>
static void
populate_serializer_with_shared_large_liga (hb_serialize_context_t* c, bool sequential_liga_sets)
{
c->start_serialize<char> ();
unsigned lookups[2];
// Lookup
// LigSubst: shared with another Lookup table, needs splitting
unsigned liga_subst_idx[liga_subst_count];
populate_serializer_with_large_ligsubst<liga_subst_count, liga_set_count, liga_per_set_count, liga_size>(c, sequential_liga_sets, true, liga_subst_idx, true);
start_lookup (7, liga_subst_count, c);
for (unsigned l = 0; l < liga_subst_count; l++) {
add_offset(liga_subst_idx[l], c);
}
lookups[0] = finish_lookup (c);
// Lookup with 2 LigSubst tables
unsigned liga_subst[liga_subst_count + 1];
// LigSubst: small one, not shared, no split
populate_serializer_with_large_ligsubst<1, 1, 1, 10>(c, sequential_liga_sets, false, liga_subst);
// LigSubst: shared, needs splitting
populate_serializer_with_large_ligsubst<liga_subst_count, liga_set_count, liga_per_set_count, liga_size>(c, sequential_liga_sets, true, &liga_subst[1], true);
start_lookup (7, liga_subst_count + 1, c);
for (unsigned l = 0; l <= liga_subst_count; l++) {
add_offset(liga_subst[l], c);
}
lookups[1] = finish_lookup (c);
unsigned lookup_list = add_lookup_list (lookups, 2, c);
add_gsubgpos_header (lookup_list, c);
c->end_serialize();
}
template<unsigned liga_subst_count,
unsigned liga_set_count,
unsigned liga_per_set_count,
unsigned liga_size>
static void
populate_serializer_with_liga_shared_coverage (hb_serialize_context_t* c)
{
c->start_serialize<char> ();
unsigned liga_subst[liga_subst_count + 1];
// LigSubst: small one, no split, coverage shared
populate_serializer_with_large_ligsubst<1, 6, 2, 10>(c, true, true, liga_subst, true);
// LigSubst: shared coverage, needs splitting
populate_serializer_with_large_ligsubst<liga_subst_count, liga_set_count, liga_per_set_count, liga_size>(c, true, true, &liga_subst[1], true);
start_lookup (7, liga_subst_count + 1, c);
for (unsigned l = 0; l <= liga_subst_count; l++) {
add_offset(liga_subst[l], c);
}
unsigned lookup = finish_lookup (c);
unsigned lookup_list = add_lookup_list (&lookup, 1, c);
add_gsubgpos_header (lookup_list, c);
c->end_serialize();
}
static void test_sort_shortest ()
{
size_t buffer_size = 100;
@@ -2426,6 +2516,52 @@ static void test_resolve_with_liga_split_overlapping_clone ()
free (expected_buffer);
}
static void test_resolve_with_liga_split_shared_table ()
{
size_t buffer_size = 400000;
void* buffer = malloc (buffer_size);
hb_always_assert (buffer);
hb_serialize_context_t c (buffer, buffer_size);
populate_serializer_with_shared_large_liga<1, 6, 2, 16000>(&c, true);
void* expected_buffer = malloc (buffer_size);
hb_always_assert (expected_buffer);
hb_serialize_context_t e (expected_buffer, buffer_size);
populate_serializer_with_shared_large_liga<3, 2, 2, 16000>(&e, true);
run_resolve_overflow_test ("test_resolve_with_liga_split_shared_table",
c,
e,
20,
true,
HB_TAG('G', 'S', 'U', 'B'));
free (buffer);
free (expected_buffer);
}
static void test_resolve_with_liga_split_shared_coverage ()
{
size_t buffer_size = 400000;
void* buffer = malloc (buffer_size);
hb_always_assert (buffer);
hb_serialize_context_t c (buffer, buffer_size);
populate_serializer_with_liga_shared_coverage<1, 6, 2, 16000>(&c);
void* expected_buffer = malloc (buffer_size);
hb_always_assert (expected_buffer);
hb_serialize_context_t e (expected_buffer, buffer_size);
populate_serializer_with_liga_shared_coverage<3, 2, 2, 16000>(&e);
run_resolve_overflow_test ("test_resolve_with_liga_split_shared_coverage",
c,
e,
20,
true,
HB_TAG('G', 'S', 'U', 'B'));
free (buffer);
free (expected_buffer);
}
static void test_resolve_overflows_via_splitting_spaces ()
{
size_t buffer_size = 160000;
@@ -2637,6 +2773,8 @@ main (int argc, char **argv)
test_resolve_with_liga_split_move ();
test_dont_duplicate_virtual ();
test_resolve_with_liga_split_overlapping_clone ();
test_resolve_with_liga_split_shared_table ();
test_resolve_with_liga_split_shared_coverage ();
// TODO(grieger): have run overflow tests compare graph equality not final packed binary.
// TODO(grieger): split test where multiple subtables in one lookup are split to test link ordering.