From 38ab13c67ea3d3b53b3bcfda953234d390d63be4 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Fri, 4 Jul 2025 22:31:04 +0000 Subject: [PATCH 01/12] [repacker] add support for splitting GSUB Ligature lookups. --- src/OT/Layout/GSUB/LigatureSet.hh | 4 +- src/graph/graph.hh | 3 +- src/graph/gsubgpos-graph.hh | 45 +++- src/graph/ligature-graph.hh | 367 ++++++++++++++++++++++++++++++ 4 files changed, 404 insertions(+), 15 deletions(-) create mode 100644 src/graph/ligature-graph.hh diff --git a/src/OT/Layout/GSUB/LigatureSet.hh b/src/OT/Layout/GSUB/LigatureSet.hh index 17ae38766..928c74a16 100644 --- a/src/OT/Layout/GSUB/LigatureSet.hh +++ b/src/OT/Layout/GSUB/LigatureSet.hh @@ -11,11 +11,11 @@ namespace GSUB_impl { template struct LigatureSet { - protected: + public: Array16OfOffset16To> ligature; /* Array LigatureSet tables * ordered by preference */ - public: + DEFINE_SIZE_ARRAY (2, ligature); bool sanitize (hb_sanitize_context_t *c) const diff --git a/src/graph/graph.hh b/src/graph/graph.hh index b24507ece..c4cedacbd 100644 --- a/src/graph/graph.hh +++ b/src/graph/graph.hh @@ -709,8 +709,9 @@ struct graph_t if (!r.table) return vertex_and_table_t (); - if (!r.table->sanitize (*(r.vertex), std::forward(ds)...)) + if (!r.table->sanitize (*(r.vertex), std::forward(ds)...)) { return vertex_and_table_t (); + } return r; } diff --git a/src/graph/gsubgpos-graph.hh b/src/graph/gsubgpos-graph.hh index 0f6d5662e..fee30b17a 100644 --- a/src/graph/gsubgpos-graph.hh +++ b/src/graph/gsubgpos-graph.hh @@ -24,12 +24,15 @@ * Google Author(s): Garret Rieger */ +#include "OT/Layout/GSUB/SubstLookupSubTable.hh" #include "graph.hh" #include "../hb-ot-layout-gsubgpos.hh" #include "../OT/Layout/GSUB/ExtensionSubst.hh" #include "gsubgpos-context.hh" +#include "hb-ot.h" #include "pairpos-graph.hh" #include "markbasepos-graph.hh" +#include "ligature-graph.hh" #ifndef GRAPH_GSUBGPOS_GRAPH_HH #define GRAPH_GSUBGPOS_GRAPH_HH @@ -120,12 +123,18 @@ struct Lookup : public OT::Lookup unsigned type = lookupType; bool is_ext = is_extension (c.table_tag); - if (c.table_tag != HB_OT_TAG_GPOS) + if (c.table_tag != HB_OT_TAG_GPOS && c.table_tag != HB_OT_TAG_GSUB) return true; - if (!is_ext && - type != OT::Layout::GPOS_impl::PosLookupSubTable::Type::Pair && - type != OT::Layout::GPOS_impl::PosLookupSubTable::Type::MarkBase) + bool supported_gsub = (c.table_tag == HB_OT_TAG_GSUB) && ( + type == OT::Layout::GSUB_impl::SubstLookupSubTable::Type::Ligature + ); + bool supported_gpos = (c.table_tag == HB_OT_TAG_GPOS) && ( + type == OT::Layout::GPOS_impl::PosLookupSubTable::Type::Pair || + type == OT::Layout::GPOS_impl::PosLookupSubTable::Type::MarkBase + ); + + if (!is_ext && !supported_gpos && !supported_gsub) return true; hb_vector_t>> all_new_subtables; @@ -150,15 +159,27 @@ struct Lookup : public OT::Lookup } hb_vector_t new_sub_tables; - switch (type) - { - case 2: - new_sub_tables = split_subtable (c, parent_index, subtable_index); break; - case 4: - new_sub_tables = split_subtable (c, parent_index, subtable_index); break; - default: - break; + + if (c.table_tag == HB_OT_TAG_GPOS) { + switch (type) + { + case 2: + new_sub_tables = split_subtable (c, parent_index, subtable_index); break; + case 4: + new_sub_tables = split_subtable (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 (c, parent_index, subtable_index); break; + default: + break; + } } + if (new_sub_tables.in_error ()) return false; if (!new_sub_tables) continue; hb_pair_t>* entry = all_new_subtables.push (); diff --git a/src/graph/ligature-graph.hh b/src/graph/ligature-graph.hh new file mode 100644 index 000000000..f552a131d --- /dev/null +++ b/src/graph/ligature-graph.hh @@ -0,0 +1,367 @@ +/* + * Copyright © 2025 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): Garret Rieger + */ + +#ifndef GRAPH_LIGATURE_GRAPH_HH +#define GRAPH_LIGATURE_GRAPH_HH + +#include "OT/Layout/GSUB/LigatureSet.hh" +#include "OT/Layout/types.hh" +#include "hb-ot-layout-common.hh" + +#include "graph.hh" +#include "../OT/Layout/GSUB/LigatureSubst.hh" +#include "../OT/Layout/GSUB/LigatureSubstFormat1.hh" +#include +#include + +namespace graph { + +struct LigatureSet : public OT::Layout::GSUB_impl::LigatureSet +{ + bool sanitize (graph_t::vertex_t& vertex) const + { + int64_t vertex_len = vertex.obj.tail - vertex.obj.head; + if (vertex_len < OT::Layout::GSUB_impl::LigatureSet::min_size) return false; + hb_barrier (); + + int64_t total_len = ligature.get_size() + OT::Layout::GSUB_impl::LigatureSet::min_size - ligature.len.get_size(); + if (vertex_len < total_len) { + return false; + } + return true; + } +}; + +struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1_2 +{ + bool sanitize (graph_t::vertex_t& vertex) const + { + int64_t vertex_len = vertex.obj.tail - vertex.obj.head; + unsigned min_size = OT::Layout::GSUB_impl::LigatureSubstFormat1_2::min_size; + if (vertex_len < min_size) return false; + hb_barrier (); + + return vertex_len >= + min_size + ligatureSet.get_size() - ligatureSet.len.get_size(); + } + + hb_vector_t split_subtables (gsubgpos_graph_context_t& c, + unsigned parent_index, + unsigned this_index) + { + auto split_points = compute_split_points(c, parent_index, this_index); + split_context_t split_context { + c, + this, + c.graph.duplicate_if_shared (parent_index, this_index), + }; + return actuate_subtable_split (split_context, split_points); + } + + private: + hb_vector_t 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 + // will happen if there is one, so we can check the estimate length of the + // LigatureSubstFormat1 -> Coverage offset length which is the sum of all data in the + // retained sub graph except for the coverage table itself. + const unsigned base_size = OT::Layout::GSUB_impl::LigatureSubstFormat1_2::min_size; + unsigned accumulated = base_size; + + unsigned ligature_index = 0; + hb_vector_t split_points; + for (unsigned i = 0; i < ligatureSet.len; i++) + { + accumulated += OT::HBUINT16::static_size; // for ligature set offset + accumulated += OT::Layout::GSUB_impl::LigatureSet::min_size; // for ligature set table + + auto liga_set = c.graph.as_table(this_index, &ligatureSet[i]); + if (!liga_set.table) { + return hb_vector_t {}; + } + + for (unsigned j = 0; j < liga_set.table->ligature.len; j++) + { + const unsigned liga_id = c.graph.index_for_offset (liga_set.index, &liga_set.table->ligature[j]); + const unsigned liga_size = c.graph.vertices_[liga_id].table_size (); + + accumulated += OT::HBUINT16::static_size; // for ligature offset + accumulated += liga_size; // for the ligature table + + + if (accumulated >= (1 << 16)) + { + split_points.push(ligature_index); + // We're going to split such that the current ligature will be in the new sub table. + // That means we'll have one ligature subst (base_base), one ligature set, and one liga table + accumulated = base_size + // for liga subst subtable + (OT::HBUINT16::static_size * 2) + // for liga set and liga offset + OT::Layout::GSUB_impl::LigatureSet::min_size + // for liga set subtable + liga_size; // for liga sub table + } + + ligature_index++; + } + } + + return split_points; + } + + + struct split_context_t + { + gsubgpos_graph_context_t& c; + LigatureSubstFormat1* thiz; + unsigned this_index; + + unsigned original_count () + { + unsigned total = 0; + for (unsigned i = 0; i < thiz->ligatureSet.len; i++) + { + auto liga_set = c.graph.as_table(this_index, &thiz->ligatureSet[i]); + if (!liga_set.table) { + return 0; + } + total += liga_set.table->ligature.len; + } + return total; + } + + unsigned clone_range (unsigned start, unsigned end) + { + return thiz->clone_range (c, this_index, start, end); + } + + bool shrink (unsigned count) + { + return thiz->shrink (c, this_index, original_count(), count); + } + }; + + std::pair new_liga_set(gsubgpos_graph_context_t& c, unsigned count) const { + unsigned prime_size = OT::Layout::GSUB_impl::LigatureSet::min_size + + count * SmallTypes::size; + + unsigned prime_id = c.create_node (prime_size); + if (prime_id == (unsigned) -1) return std::make_pair(-1, nullptr); + + LigatureSet* prime = (LigatureSet*) c.graph.object (prime_id).head; + prime->ligature.len = count; + return std::make_pair(prime_id, prime); + } + + unsigned clone_range (gsubgpos_graph_context_t& c, + unsigned this_index, + unsigned start, unsigned end) const + { + DEBUG_MSG (SUBSET_REPACK, nullptr, + " Cloning LigatureSubstFormat1 (%u) range [%u, %u).", this_index, start, end); + + // Create an oversized new liga subst, we'll adjust the size down later. We don't know + // the final size until we process it but we also need it to exist while we're processing + // so that nodes can be moved to it as needed. + unsigned prime_size = OT::Layout::GSUB_impl::LigatureSubstFormat1_2::min_size + + ligatureSet.get_size() - ligatureSet.len.get_size(); + + unsigned liga_subst_prime_id = c.create_node (prime_size); + if (liga_subst_prime_id == (unsigned) -1) return -1; + + LigatureSubstFormat1* liga_subst_prime = (LigatureSubstFormat1*) c.graph.object (liga_subst_prime_id).head; + liga_subst_prime->format = this->format; + liga_subst_prime->ligatureSet.len = this->ligatureSet.len; + + // Locate all liga sets with ligas between start and end. + // Clone or move them as needed. + unsigned count = 0; + unsigned liga_set_count = 0; + unsigned liga_set_start = -1; + unsigned liga_set_end = 0; // inclusive + for (unsigned i = 0; i < ligatureSet.len; i++) + { + auto liga_set_index = c.graph.index_for_offset(this_index, &ligatureSet[i]); + auto liga_set = c.graph.as_table(this_index, &ligatureSet[i]); + if (!liga_set.table) { + return -1; + } + unsigned num_ligas = liga_set.table->ligature.len; + + unsigned current_start = count; + unsigned current_end = count + num_ligas; + + if (current_start >= start && current_end <= end) { + // This liga set is fully contined within [start, end) + // We can move the entire ligaset to the new liga subset object. + liga_set_end = i; + if (i < liga_set_start) liga_set_start = i; + c.graph.move_child<> (this_index, + &ligatureSet[i], + liga_subst_prime_id, + &liga_subst_prime->ligatureSet[liga_set_count++]); + } + else if (current_start < end && start < current_end) + { + // This liga set partially overlaps [start, end). We'll need to create + // a new liga set sub table and move the intersecting ligas to it. + unsigned liga_count = std::min(end, current_end) - std::max(start, current_start); + auto result = new_liga_set(c, liga_count); + unsigned prime_id = result.first; + LigatureSet* prime = result.second; + if (prime_id == (unsigned) -1) return -1; + + unsigned new_index = 0; + for (unsigned j = std::max(start, current_start) - count; j < std::min(end, current_end) - count; j++) { + c.graph.move_child<> (liga_set_index, + &liga_set.table->ligature[j], + prime_id, + &prime->ligature[new_index++]); + } + + liga_set_end = i; + if (i < liga_set_start) liga_set_start = i; + c.graph.add_link(&liga_subst_prime->ligatureSet[liga_set_count++], liga_subst_prime_id, prime_id); + } + + count += num_ligas; + } + + c.graph.vertices_[liga_subst_prime_id].obj.tail -= (liga_subst_prime->ligatureSet.len - liga_set_count) * SmallTypes::size; + liga_subst_prime->ligatureSet.len = liga_set_count; + + unsigned coverage_id = c.graph.index_for_offset (this_index, &coverage); + if (!Coverage::clone_coverage (c, + coverage_id, + liga_subst_prime_id, + 2, + liga_set_start, liga_set_end)) + return -1; + + return liga_subst_prime_id; + } + + bool shrink (gsubgpos_graph_context_t& c, + unsigned this_index, + unsigned old_count, + unsigned count) + { + DEBUG_MSG (SUBSET_REPACK, nullptr, + " Shrinking LigatureSubstFormat1 (%u) to [0, %u).", + this_index, + count); + if (count >= old_count) + return true; + + unsigned new_liga_set_count = 0; + for (unsigned i = 0; i < ligatureSet.len; i++) + { + auto liga_set = c.graph.as_table(this_index, &ligatureSet[i]); + if (!liga_set.table) { + return false; + } + + unsigned num_ligas = liga_set.table->ligature.len; + if (num_ligas <= count) { + count -= num_ligas; + continue; + } + + if (count == 0) { + // drop this liga set an all subsequent ones. + new_liga_set_count = i; + break; + } + + // drop the trailing liga's from this set and all subsequent liga sets + new_liga_set_count = i + 1; + c.graph.vertices_[liga_set.index].obj.tail -= (num_ligas - count) * SmallTypes::size; + liga_set.table->ligature.len = count; + break; + } + + // Adjust liga set array + c.graph.vertices_[this_index].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 (this_index, &this->coverage); + if (!coverage) return false; + + unsigned coverage_size = coverage.vertex->table_size (); + auto new_coverage = + + hb_zip (coverage.table->iter (), hb_range ()) + | hb_filter ([&] (hb_pair_t 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); + } +}; + +struct LigatureSubst : public OT::Layout::GSUB_impl::LigatureSubst +{ + + hb_vector_t split_subtables (gsubgpos_graph_context_t& c, + unsigned parent_index, + unsigned this_index) + { + switch (u.format) { + case 1: + return ((LigatureSubstFormat1*)(&u.format1))->split_subtables (c, parent_index, this_index); +#ifndef HB_NO_BEYOND_64K + case 2: HB_FALLTHROUGH; + // Don't split 24bit Ligature Subs +#endif + default: + return hb_vector_t (); + } + } + + bool sanitize (graph_t::vertex_t& vertex) const + { + int64_t vertex_len = vertex.obj.tail - vertex.obj.head; + if (vertex_len < u.format.get_size ()) return false; + hb_barrier (); + + switch (u.format) { + case 1: + return ((LigatureSubstFormat1*)(&u.format1))->sanitize (vertex); +#ifndef HB_NO_BEYOND_64K + case 2: HB_FALLTHROUGH; +#endif + default: + // We don't handle format 2 here. + return false; + } + } +}; + +} + +#endif // GRAPH_LIGATURE_GRAPH_HH From 5b47710f560580cad7498da696b0bd47be6397b3 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Mon, 7 Jul 2025 20:15:30 +0000 Subject: [PATCH 02/12] [repacker] Start implementing test for liga splitting. --- src/graph/coverage-graph.hh | 22 +++++++ src/graph/graph.hh | 6 +- src/graph/gsubgpos-graph.hh | 26 +++++---- src/graph/ligature-graph.hh | 46 ++++++++++----- src/test-repacker.cc | 112 ++++++++++++++++++++++++++++++++++++ 5 files changed, 187 insertions(+), 25 deletions(-) diff --git a/src/graph/coverage-graph.hh b/src/graph/coverage-graph.hh index 61ca063e3..1dc882ccd 100644 --- a/src/graph/coverage-graph.hh +++ b/src/graph/coverage-graph.hh @@ -103,6 +103,28 @@ struct Coverage : public OT::Layout::Common::Coverage return (Coverage*) coverage_prime_vertex.obj.head; } + // Filter an existing coverage table to glyphs at indices [start, end) and replace it with the filtered version. + static bool filter_coverage (gsubgpos_graph_context_t& c, + unsigned existing_coverage, + unsigned start, unsigned end) { + unsigned coverage_size = c.graph.vertices_[existing_coverage].table_size (); + auto& coverage_v = c.graph.vertices_[existing_coverage]; + Coverage* coverage_table = (Coverage*) coverage_v.obj.head; + if (!coverage_table || !coverage_table->sanitize (coverage_v)) + return false; + + auto new_coverage = + + hb_zip (coverage_table->iter (), hb_range ()) + | hb_filter ([&] (hb_pair_t p) { + return p.second >= start && p.second < end; + }) + | hb_map_retains_sorting (hb_first) + ; + + return make_coverage (c, new_coverage, existing_coverage, coverage_size * 2 + 100); + } + + // Replace the coverage table at dest obj with one covering 'glyphs'. template static bool make_coverage (gsubgpos_graph_context_t& c, It glyphs, diff --git a/src/graph/graph.hh b/src/graph/graph.hh index c4cedacbd..09cadef3d 100644 --- a/src/graph/graph.hh +++ b/src/graph/graph.hh @@ -944,9 +944,11 @@ struct graph_t /* * Moves the child of old_parent_idx pointed to by old_offset to a new * vertex at the new_offset. + * + * Returns the id of the child node that was moved. */ template - void move_child (unsigned old_parent_idx, + unsigned move_child (unsigned old_parent_idx, const O* old_offset, unsigned new_parent_idx, const O* new_offset) @@ -970,6 +972,8 @@ struct graph_t old_v.remove_real_link (child_id, old_offset); child.remove_parent (old_parent_idx); + + return child_id; } /* diff --git a/src/graph/gsubgpos-graph.hh b/src/graph/gsubgpos-graph.hh index fee30b17a..0226264d8 100644 --- a/src/graph/gsubgpos-graph.hh +++ b/src/graph/gsubgpos-graph.hh @@ -126,15 +126,7 @@ struct Lookup : public OT::Lookup if (c.table_tag != HB_OT_TAG_GPOS && c.table_tag != HB_OT_TAG_GSUB) return true; - bool supported_gsub = (c.table_tag == HB_OT_TAG_GSUB) && ( - type == OT::Layout::GSUB_impl::SubstLookupSubTable::Type::Ligature - ); - bool supported_gpos = (c.table_tag == HB_OT_TAG_GPOS) && ( - type == OT::Layout::GPOS_impl::PosLookupSubTable::Type::Pair || - type == OT::Layout::GPOS_impl::PosLookupSubTable::Type::MarkBase - ); - - if (!is_ext && !supported_gpos && !supported_gsub) + if (!is_ext && !is_supported_gpos_type(type, c) && !is_supported_gsub_type(type, c)) return true; hb_vector_t>> all_new_subtables; @@ -153,8 +145,7 @@ struct Lookup : public OT::Lookup subtable_index = extension->get_subtable_index (c.graph, ext_subtable_index); type = extension->get_lookup_type (); - if (type != OT::Layout::GPOS_impl::PosLookupSubTable::Type::Pair - && type != OT::Layout::GPOS_impl::PosLookupSubTable::Type::MarkBase) + if (!is_supported_gpos_type(type, c) && !is_supported_gsub_type(type, c)) continue; } @@ -355,6 +346,19 @@ struct Lookup : public OT::Lookup } private: + bool is_supported_gsub_type(unsigned type, gsubgpos_graph_context_t& c) const { + return (c.table_tag == HB_OT_TAG_GSUB) && ( + type == OT::Layout::GSUB_impl::SubstLookupSubTable::Type::Ligature + ); + } + + bool is_supported_gpos_type(unsigned type, gsubgpos_graph_context_t& c) const { + return (c.table_tag == HB_OT_TAG_GPOS) && ( + type == OT::Layout::GPOS_impl::PosLookupSubTable::Type::Pair || + type == OT::Layout::GPOS_impl::PosLookupSubTable::Type::MarkBase + ); + } + unsigned extension_type (hb_tag_t table_tag) const { switch (table_tag) diff --git a/src/graph/ligature-graph.hh b/src/graph/ligature-graph.hh index f552a131d..7fbbe6f08 100644 --- a/src/graph/ligature-graph.hh +++ b/src/graph/ligature-graph.hh @@ -112,8 +112,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 accumulated += OT::HBUINT16::static_size; // for ligature offset accumulated += liga_size; // for the ligature table - - + if (accumulated >= (1 << 16)) { split_points.push(ligature_index); @@ -180,6 +179,8 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 unsigned this_index, unsigned start, unsigned end) const { + // TODO XXXX liga set's that are part of two clone_range's will not be pruned from the graph and will + // end up orphaned. Add a test for this case. DEBUG_MSG (SUBSET_REPACK, nullptr, " Cloning LigatureSubstFormat1 (%u) range [%u, %u).", this_index, start, end); @@ -196,6 +197,17 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 liga_subst_prime->format = this->format; liga_subst_prime->ligatureSet.len = this->ligatureSet.len; + // Create a place holder coverage prime id since we need to add virtual links to it while + // generating liga and liga sets. Afterwards it will be updated to have the correct coverage. + unsigned coverage_id = c.graph.index_for_offset (this_index, &coverage); + unsigned coverage_prime_id = c.graph.duplicate(coverage_id); + auto& coverage_prime_vertex = c.graph.vertices_[coverage_prime_id]; + auto* coverage_prime_link = c.graph.vertices_[liga_subst_prime_id].obj.real_links.push (); + coverage_prime_link->width = SmallTypes::size; + coverage_prime_link->objidx = coverage_prime_id; + coverage_prime_link->position = 2; + coverage_prime_vertex.add_parent (liga_subst_prime_id); + // Locate all liga sets with ligas between start and end. // Clone or move them as needed. unsigned count = 0; @@ -214,12 +226,13 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 unsigned current_start = count; unsigned current_end = count + num_ligas; + unsigned liga_set_prime_id; if (current_start >= start && current_end <= end) { // This liga set is fully contined within [start, end) // We can move the entire ligaset to the new liga subset object. liga_set_end = i; if (i < liga_set_start) liga_set_start = i; - c.graph.move_child<> (this_index, + liga_set_prime_id = c.graph.move_child<> (this_index, &ligatureSet[i], liga_subst_prime_id, &liga_subst_prime->ligatureSet[liga_set_count++]); @@ -230,21 +243,31 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 // a new liga set sub table and move the intersecting ligas to it. unsigned liga_count = std::min(end, current_end) - std::max(start, current_start); auto result = new_liga_set(c, liga_count); - unsigned prime_id = result.first; + liga_set_prime_id = result.first; LigatureSet* prime = result.second; - if (prime_id == (unsigned) -1) return -1; + if (liga_set_prime_id == (unsigned) -1) return -1; unsigned new_index = 0; for (unsigned j = std::max(start, current_start) - count; j < std::min(end, current_end) - count; j++) { c.graph.move_child<> (liga_set_index, &liga_set.table->ligature[j], - prime_id, + liga_set_prime_id, &prime->ligature[new_index++]); } liga_set_end = i; if (i < liga_set_start) liga_set_start = i; - c.graph.add_link(&liga_subst_prime->ligatureSet[liga_set_count++], liga_subst_prime_id, prime_id); + c.graph.add_link(&liga_subst_prime->ligatureSet[liga_set_count++], liga_subst_prime_id, liga_set_prime_id); + } + + // The new liga and all children set needs to have a virtual link to the new coverage table: + auto& liga_set_prime = c.graph.vertices_[liga_set_prime_id].obj; + liga_set_prime.virtual_links.clear(); + liga_set_prime.add_virtual_link(coverage_prime_id); + for (const auto& l : liga_set_prime.real_links) { + auto& liga = c.graph.vertices_[l.objidx]; + liga.obj.virtual_links.clear(); + liga.obj.add_virtual_link(coverage_prime_id); } count += num_ligas; @@ -253,12 +276,9 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 c.graph.vertices_[liga_subst_prime_id].obj.tail -= (liga_subst_prime->ligatureSet.len - liga_set_count) * SmallTypes::size; liga_subst_prime->ligatureSet.len = liga_set_count; - unsigned coverage_id = c.graph.index_for_offset (this_index, &coverage); - if (!Coverage::clone_coverage (c, - coverage_id, - liga_subst_prime_id, - 2, - liga_set_start, liga_set_end)) + if (!Coverage::filter_coverage (c, + coverage_prime_id, + liga_set_start, liga_set_end)) return -1; return liga_subst_prime_id; diff --git a/src/test-repacker.cc b/src/test-repacker.cc index 7f89fbfa3..e16127a86 100644 --- a/src/test-repacker.cc +++ b/src/test-repacker.cc @@ -24,6 +24,7 @@ * Google Author(s): Garret Rieger */ +#include #include #include "hb-repacker.hh" @@ -242,6 +243,31 @@ static unsigned add_pair_pos_1 (unsigned* pair_sets, return c->pop_pack (false); } +static void add_liga_set_header (char liga_count, + hb_serialize_context_t* c) +{ + uint8_t liga_count_bytes[] = { + (uint8_t) (0xFF & (liga_count >> 8)), + (uint8_t) (0xFF & liga_count), + }; + + start_object ((const char*) liga_count_bytes, 2, c); +} + +static void add_liga_header (unsigned coverage, unsigned liga_set_count, hb_serialize_context_t* c) +{ + uint8_t format[] = {0, 1}; + start_object ((const char*) format, 2, c); + add_offset(coverage, c); + + uint8_t liga_set_count_bytes[] = { + (uint8_t) (0xFF & (liga_set_count >> 8)), + (uint8_t) (0xFF & liga_set_count), + }; + + extend((const char *) liga_set_count_bytes, 2, c); +} + static unsigned add_pair_pos_2 (unsigned starting_class, unsigned coverage, unsigned class_def_1, uint16_t class_def_1_count, @@ -1568,6 +1594,68 @@ populate_serializer_with_large_mark_base_pos_1 (hb_serialize_context_t* c) c->end_serialize(); } +template +static void +populate_serializer_with_large_liga (hb_serialize_context_t* c) +{ + std::string large_string(100000, 'a'); + c->start_serialize (); + + unsigned liga_subst[liga_subst_count]; + + for (unsigned l = 0; l < liga_subst_count; l++) { + unsigned coverage = add_coverage(0, liga_set_count * liga_per_set_count - 1, c); + + 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++) + { + start_object (large_string.c_str(), liga_size, c); + add_virtual_offset(coverage, c); + liga[i * liga_per_set_count + j] = c->pop_pack (false); + } + + add_liga_set_header(liga_per_set_count, c); + add_virtual_offset(coverage, c); + for (unsigned j = 0; j < liga_per_set_count; j++) + { + add_offset(liga[i * liga_per_set_count + j], c); + } + + liga_set[i] = c->pop_pack(false); + } + + add_liga_header(coverage, liga_set_count, c); + for (unsigned i = 0; i < liga_set_count; i++) + { + add_offset(liga_set[i], c); + } + + liga_subst[l] = c->pop_pack(false); + } + + for (unsigned l = 0; l < liga_subst_count; l++) { + liga_subst[l] = add_extension(liga_subst[l], 4, c); + } + + start_lookup (7, liga_subst_count, 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; @@ -2113,6 +2201,29 @@ static void test_resolve_with_basic_mark_base_pos_1_split () free (expected_buffer); } +static void test_resolve_with_basic_liga_split () +{ + size_t buffer_size = 200000; + void* buffer = malloc (buffer_size); + assert (buffer); + hb_serialize_context_t c (buffer, buffer_size); + populate_serializer_with_large_liga<1, 1, 2, 40000>(&c); + + void* expected_buffer = malloc (buffer_size); + assert (expected_buffer); + hb_serialize_context_t e (expected_buffer, buffer_size); + populate_serializer_with_large_liga<2, 1, 1, 40000>(&e); + + run_resolve_overflow_test ("test_resolve_with_basic_liga_split", + 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; @@ -2294,6 +2405,7 @@ main (int argc, char **argv) test_resolve_with_pair_pos_2_split_with_device_tables (); test_resolve_with_close_to_limit_pair_pos_2_split (); test_resolve_with_basic_mark_base_pos_1_split (); + test_resolve_with_basic_liga_split (); // 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. From 96fedd88feb9c22a39f6dca1152d5ae558abc2d2 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Tue, 8 Jul 2025 01:14:14 +0000 Subject: [PATCH 03/12] [repacker] Fix handling of virtual links in liga table splitting. --- src/graph/graph.hh | 4 +++ src/graph/ligature-graph.hh | 60 ++++++++++++++++++++++++++++++------- src/test-repacker.cc | 13 +++++++- 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/src/graph/graph.hh b/src/graph/graph.hh index 09cadef3d..4a787f054 100644 --- a/src/graph/graph.hh +++ b/src/graph/graph.hh @@ -501,6 +501,9 @@ struct graph_t for (const auto &l : v.obj.real_links) { printf("%u, ", l.objidx); } + for (const auto &l : v.obj.virtual_links) { + printf("v%u, ", l.objidx); + } printf("]\n"); } } @@ -1284,6 +1287,7 @@ struct graph_t if (!DEBUG_ENABLED(SUBSET_REPACK)) return; DEBUG_MSG (SUBSET_REPACK, nullptr, "Graph is not fully connected."); + parents_invalid = true; update_parents(); diff --git a/src/graph/ligature-graph.hh b/src/graph/ligature-graph.hh index 7fbbe6f08..85fb0644e 100644 --- a/src/graph/ligature-graph.hh +++ b/src/graph/ligature-graph.hh @@ -142,7 +142,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 { unsigned total = 0; for (unsigned i = 0; i < thiz->ligatureSet.len; i++) - { + { auto liga_set = c.graph.as_table(this_index, &thiz->ligatureSet[i]); if (!liga_set.table) { return 0; @@ -174,7 +174,25 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 prime->ligature.len = count; return std::make_pair(prime_id, prime); } - + + void clear_virtual_links (gsubgpos_graph_context_t& c, unsigned node_index) const + { + auto& obj = c.graph.vertices_[node_index].obj; + for (const auto& l : obj.virtual_links) + { + auto& child = c.graph.vertices_[l.objidx]; + child.remove_parent(node_index); + } + obj.virtual_links.clear(); + } + + void add_virtual_link(gsubgpos_graph_context_t& c, unsigned from, unsigned to) const { + auto& from_obj = c.graph.vertices_[from].obj; + c.graph.vertices_[to].add_parent(from); + auto& link = *from_obj.virtual_links.push (); + link.objidx = to; + } + unsigned clone_range (gsubgpos_graph_context_t& c, unsigned this_index, unsigned start, unsigned end) const @@ -189,7 +207,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 // so that nodes can be moved to it as needed. unsigned prime_size = OT::Layout::GSUB_impl::LigatureSubstFormat1_2::min_size + ligatureSet.get_size() - ligatureSet.len.get_size(); - + unsigned liga_subst_prime_id = c.create_node (prime_size); if (liga_subst_prime_id == (unsigned) -1) return -1; @@ -258,18 +276,21 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 liga_set_end = i; if (i < liga_set_start) liga_set_start = i; c.graph.add_link(&liga_subst_prime->ligatureSet[liga_set_count++], liga_subst_prime_id, liga_set_prime_id); + } else { + // No intersection, so just skip + count += num_ligas; + continue; } // The new liga and all children set needs to have a virtual link to the new coverage table: auto& liga_set_prime = c.graph.vertices_[liga_set_prime_id].obj; - liga_set_prime.virtual_links.clear(); - liga_set_prime.add_virtual_link(coverage_prime_id); + clear_virtual_links(c, liga_set_prime_id); + add_virtual_link(c, liga_set_prime_id, coverage_prime_id); for (const auto& l : liga_set_prime.real_links) { - auto& liga = c.graph.vertices_[l.objidx]; - liga.obj.virtual_links.clear(); - liga.obj.add_virtual_link(coverage_prime_id); + clear_virtual_links(c, l.objidx); + add_virtual_link(c, l.objidx, coverage_prime_id); } - + count += num_ligas; } @@ -278,7 +299,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 if (!Coverage::filter_coverage (c, coverage_prime_id, - liga_set_start, liga_set_end)) + liga_set_start, liga_set_end + 1)) return -1; return liga_subst_prime_id; @@ -294,8 +315,9 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 this_index, count); if (count >= old_count) - return true; + return true; + hb_set_t retained_indices; unsigned new_liga_set_count = 0; for (unsigned i = 0; i < ligatureSet.len; i++) { @@ -304,6 +326,19 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 return false; } + // We need the virtual links to coverage removed from all descendants on this liga subst. + // If any are left when we try to mutate the coverage table later it will be unnessecarily + // duplicated. Code later on will re-add the virtual links as needed (via retained_indices). + clear_virtual_links(c, liga_set.index); + retained_indices.add(liga_set.index); + for (const auto& liga_offset : liga_set.table->ligature) { + unsigned liga_index = c.graph.index_for_offset(liga_set.index, &liga_offset); + if (liga_index != (unsigned) -1) { + clear_virtual_links(c, liga_index); + retained_indices.add(liga_index); + } + } + unsigned num_ligas = liga_set.table->ligature.len; if (num_ligas <= count) { count -= num_ligas; @@ -331,6 +366,9 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 auto coverage = c.graph.as_mutable_table (this_index, &this->coverage); if (!coverage) return false; + for (unsigned i : retained_indices.iter()) + add_virtual_link(c, i, coverage.index); + unsigned coverage_size = coverage.vertex->table_size (); auto new_coverage = + hb_zip (coverage.table->iter (), hb_range ()) diff --git a/src/test-repacker.cc b/src/test-repacker.cc index e16127a86..25171d21e 100644 --- a/src/test-repacker.cc +++ b/src/test-repacker.cc @@ -148,6 +148,17 @@ static unsigned add_extension (unsigned child, static unsigned add_coverage (unsigned start, unsigned end, hb_serialize_context_t* c) { + if (end - start == 0) { + uint8_t coverage[] = { + 0, 1, // format + 0, 1, // count + + (uint8_t) ((start >> 8) & 0xFF), + (uint8_t) (start & 0xFF), // glyph[0] + }; + return add_object ((char*) coverage, 6, c); + } + if (end - start == 1) { uint8_t coverage[] = { @@ -1607,7 +1618,7 @@ populate_serializer_with_large_liga (hb_serialize_context_t* c) unsigned liga_subst[liga_subst_count]; for (unsigned l = 0; l < liga_subst_count; l++) { - unsigned coverage = add_coverage(0, liga_set_count * liga_per_set_count - 1, c); + unsigned coverage = add_coverage(0, liga_set_count - 1, c); unsigned liga[liga_set_count * liga_per_set_count]; unsigned liga_set[liga_set_count]; From 4fa5788cfd068fa694b91e01b74a7c818c41bbac Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Tue, 8 Jul 2025 21:29:48 +0000 Subject: [PATCH 04/12] [repacker] fix bug with original count in liga splitting. --- src/graph/ligature-graph.hh | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/graph/ligature-graph.hh b/src/graph/ligature-graph.hh index 85fb0644e..3c837335c 100644 --- a/src/graph/ligature-graph.hh +++ b/src/graph/ligature-graph.hh @@ -77,11 +77,25 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 c, this, c.graph.duplicate_if_shared (parent_index, this_index), + total_number_ligas(c, this_index), }; return actuate_subtable_split (split_context, split_points); } private: + unsigned total_number_ligas(gsubgpos_graph_context_t& c, unsigned this_index) const { + unsigned total = 0; + for (unsigned i = 0; i < ligatureSet.len; i++) + { + auto liga_set = c.graph.as_table(this_index, &ligatureSet[i]); + if (!liga_set.table) { + return 0; + } + total += liga_set.table->ligature.len; + } + return total; + } + hb_vector_t compute_split_points(gsubgpos_graph_context_t& c, unsigned parent_index, unsigned this_index) const @@ -137,19 +151,11 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 gsubgpos_graph_context_t& c; LigatureSubstFormat1* thiz; unsigned this_index; + unsigned original_count_; unsigned original_count () { - unsigned total = 0; - for (unsigned i = 0; i < thiz->ligatureSet.len; i++) - { - auto liga_set = c.graph.as_table(this_index, &thiz->ligatureSet[i]); - if (!liga_set.table) { - return 0; - } - total += liga_set.table->ligature.len; - } - return total; + return original_count_; } unsigned clone_range (unsigned start, unsigned end) From 18c67b778955ad5019b4d140fc700c873972e59c Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Tue, 8 Jul 2025 22:05:39 +0000 Subject: [PATCH 05/12] [repacker] store liga counts before processing. Otherwise processing changes the counts when we need them later on. --- src/graph/ligature-graph.hh | 67 +++++++++++++++++++++---------------- src/graph/serialize.hh | 16 +++++---- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/src/graph/ligature-graph.hh b/src/graph/ligature-graph.hh index 3c837335c..6c75fe8aa 100644 --- a/src/graph/ligature-graph.hh +++ b/src/graph/ligature-graph.hh @@ -78,6 +78,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 this, c.graph.duplicate_if_shared (parent_index, this_index), total_number_ligas(c, this_index), + liga_counts(c, this_index), }; return actuate_subtable_split (split_context, split_points); } @@ -96,6 +97,16 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 return total; } + hb_vector_t liga_counts(gsubgpos_graph_context_t& c, unsigned this_index) const { + hb_vector_t result; + for (unsigned i = 0; i < ligatureSet.len; i++) + { + auto liga_set = c.graph.as_table(this_index, &ligatureSet[i]); + result.push(!liga_set.table ? 0 : liga_set.table->ligature.len); + } + return result; + } + hb_vector_t compute_split_points(gsubgpos_graph_context_t& c, unsigned parent_index, unsigned this_index) const @@ -152,6 +163,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 LigatureSubstFormat1* thiz; unsigned this_index; unsigned original_count_; + hb_vector_t liga_counts; unsigned original_count () { @@ -160,12 +172,12 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 unsigned clone_range (unsigned start, unsigned end) { - return thiz->clone_range (c, this_index, start, end); + return thiz->clone_range (c, this_index, liga_counts, start, end); } bool shrink (unsigned count) { - return thiz->shrink (c, this_index, original_count(), count); + return thiz->shrink (c, this_index, original_count(), liga_counts, count); } }; @@ -201,6 +213,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 unsigned clone_range (gsubgpos_graph_context_t& c, unsigned this_index, + hb_vector_t liga_counts, unsigned start, unsigned end) const { // TODO XXXX liga set's that are part of two clone_range's will not be pruned from the graph and will @@ -238,17 +251,24 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 unsigned liga_set_count = 0; unsigned liga_set_start = -1; unsigned liga_set_end = 0; // inclusive - for (unsigned i = 0; i < ligatureSet.len; i++) + for (unsigned i = 0; i < liga_counts.length; i++) { + unsigned num_ligas = liga_counts[i]; + + unsigned current_start = count; + unsigned current_end = count + num_ligas; + + if (current_start >= end || start >= current_end) { + // No intersection, so just skip + count += num_ligas; + continue; + } + auto liga_set_index = c.graph.index_for_offset(this_index, &ligatureSet[i]); auto liga_set = c.graph.as_table(this_index, &ligatureSet[i]); if (!liga_set.table) { return -1; } - unsigned num_ligas = liga_set.table->ligature.len; - - unsigned current_start = count; - unsigned current_end = count + num_ligas; unsigned liga_set_prime_id; if (current_start >= start && current_end <= end) { @@ -261,7 +281,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 liga_subst_prime_id, &liga_subst_prime->ligatureSet[liga_set_count++]); } - else if (current_start < end && start < current_end) + else { // This liga set partially overlaps [start, end). We'll need to create // a new liga set sub table and move the intersecting ligas to it. @@ -282,10 +302,6 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 liga_set_end = i; if (i < liga_set_start) liga_set_start = i; c.graph.add_link(&liga_subst_prime->ligatureSet[liga_set_count++], liga_subst_prime_id, liga_set_prime_id); - } else { - // No intersection, so just skip - count += num_ligas; - continue; } // The new liga and all children set needs to have a virtual link to the new coverage table: @@ -314,6 +330,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 bool shrink (gsubgpos_graph_context_t& c, unsigned this_index, unsigned old_count, + hb_vector_t liga_counts, unsigned count) { DEBUG_MSG (SUBSET_REPACK, nullptr, @@ -325,7 +342,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 hb_set_t retained_indices; unsigned new_liga_set_count = 0; - for (unsigned i = 0; i < ligatureSet.len; i++) + for (unsigned i = 0; i < liga_counts.length; i++) { auto liga_set = c.graph.as_table(this_index, &ligatureSet[i]); if (!liga_set.table) { @@ -345,23 +362,17 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 } } - unsigned num_ligas = liga_set.table->ligature.len; - if (num_ligas <= count) { - count -= num_ligas; - continue; - } - - if (count == 0) { - // drop this liga set an all subsequent ones. - new_liga_set_count = i; + unsigned num_ligas = liga_counts[i]; + if (num_ligas >= count) { + // drop the trailing liga's from this set and all subsequent liga sets + unsigned num_ligas_to_remove = num_ligas - count; + new_liga_set_count = i + 1; + c.graph.vertices_[liga_set.index].obj.tail -= num_ligas_to_remove * SmallTypes::size; + liga_set.table->ligature.len = count; break; + } else { + count -= num_ligas; } - - // drop the trailing liga's from this set and all subsequent liga sets - new_liga_set_count = i + 1; - c.graph.vertices_[liga_set.index].obj.tail -= (num_ligas - count) * SmallTypes::size; - liga_set.table->ligature.len = count; - break; } // Adjust liga set array diff --git a/src/graph/serialize.hh b/src/graph/serialize.hh index 06e4bf44d..fb8950e6f 100644 --- a/src/graph/serialize.hh +++ b/src/graph/serialize.hh @@ -172,8 +172,11 @@ void print_overflows (graph_t& graph, template inline void serialize_link_of_type (const hb_serialize_context_t::object_t::link_t& link, char* head, + unsigned size, hb_serialize_context_t* c) { + assert(link.position + link.width <= size); + OT::Offset* offset = reinterpret_cast*> (head + link.position); *offset = 0; c->add_link (*offset, @@ -187,6 +190,7 @@ serialize_link_of_type (const hb_serialize_context_t::object_t::link_t& link, inline void serialize_link (const hb_serialize_context_t::object_t::link_t& link, char* head, + unsigned size, hb_serialize_context_t* c) { switch (link.width) @@ -197,21 +201,21 @@ void serialize_link (const hb_serialize_context_t::object_t::link_t& link, case 4: if (link.is_signed) { - serialize_link_of_type (link, head, c); + serialize_link_of_type (link, head, size, c); } else { - serialize_link_of_type (link, head, c); + serialize_link_of_type (link, head, size, c); } return; case 2: if (link.is_signed) { - serialize_link_of_type (link, head, c); + serialize_link_of_type (link, head, size, c); } else { - serialize_link_of_type (link, head, c); + serialize_link_of_type (link, head, size, c); } return; case 3: - serialize_link_of_type (link, head, c); + serialize_link_of_type (link, head, size, c); return; default: // Unexpected link width. @@ -251,7 +255,7 @@ inline hb_blob_t* serialize (const graph_t& graph) // Only real links needs to be serialized. for (const auto& link : vertices[i].obj.real_links) - serialize_link (link, start, &c); + serialize_link (link, start, size, &c); // All duplications are already encoded in the graph, so don't // enable sharing during packing. From f5279200f3be3b85d64fac56b94f8d21ef9d0479 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Tue, 8 Jul 2025 22:46:33 +0000 Subject: [PATCH 06/12] [repacker] Add test which hits the move liga set pathway in clone_range(). --- src/test-repacker.cc | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/test-repacker.cc b/src/test-repacker.cc index 25171d21e..a6d112f19 100644 --- a/src/test-repacker.cc +++ b/src/test-repacker.cc @@ -1610,7 +1610,7 @@ template static void -populate_serializer_with_large_liga (hb_serialize_context_t* c) +populate_serializer_with_large_liga (hb_serialize_context_t* c, bool sequential_liga_sets) { std::string large_string(100000, 'a'); c->start_serialize (); @@ -1618,7 +1618,13 @@ populate_serializer_with_large_liga (hb_serialize_context_t* c) unsigned liga_subst[liga_subst_count]; for (unsigned l = 0; l < liga_subst_count; l++) { - unsigned coverage = add_coverage(0, liga_set_count - 1, c); + unsigned coverage_start = 0; + unsigned coverage_end = liga_set_count - 1; + if (sequential_liga_sets) { + coverage_start = l * liga_set_count; + coverage_end = (l + 1) * liga_set_count - 1; + } + unsigned coverage = add_coverage(coverage_start, coverage_end, c); unsigned liga[liga_set_count * liga_per_set_count]; unsigned liga_set[liga_set_count]; @@ -2218,12 +2224,35 @@ static void test_resolve_with_basic_liga_split () void* buffer = malloc (buffer_size); assert (buffer); hb_serialize_context_t c (buffer, buffer_size); - populate_serializer_with_large_liga<1, 1, 2, 40000>(&c); + populate_serializer_with_large_liga<1, 1, 2, 40000>(&c, false); void* expected_buffer = malloc (buffer_size); assert (expected_buffer); hb_serialize_context_t e (expected_buffer, buffer_size); - populate_serializer_with_large_liga<2, 1, 1, 40000>(&e); + populate_serializer_with_large_liga<2, 1, 1, 40000>(&e, false); + + run_resolve_overflow_test ("test_resolve_with_basic_liga_split", + c, + e, + 20, + true, + HB_TAG('G', 'S', 'U', 'B')); + free (buffer); + free (expected_buffer); +} + +static void test_resolve_with_liga_split_move () +{ + size_t buffer_size = 400000; + void* buffer = malloc (buffer_size); + assert (buffer); + hb_serialize_context_t c (buffer, buffer_size); + populate_serializer_with_large_liga<1, 6, 2, 16000>(&c, true); + + void* expected_buffer = malloc (buffer_size); + assert (expected_buffer); + hb_serialize_context_t e (expected_buffer, buffer_size); + populate_serializer_with_large_liga<3, 2, 2, 16000>(&e, true); run_resolve_overflow_test ("test_resolve_with_basic_liga_split", c, @@ -2417,6 +2446,7 @@ main (int argc, char **argv) test_resolve_with_close_to_limit_pair_pos_2_split (); test_resolve_with_basic_mark_base_pos_1_split (); test_resolve_with_basic_liga_split (); + test_resolve_with_liga_split_move (); // 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. From cad418860a728e4b124cc8d532ea7ecfd4db4083 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Wed, 9 Jul 2025 17:09:05 +0000 Subject: [PATCH 07/12] [repacker] fix bug where nodes with incoming virtual links could get orphaned. When attempting to fix overflows nodes that have incoming edges from more than one parent can be duplicated. However, if all of these incoming links are virtual except for one duplicating it will effectively orphan it (one of the copies will be pointed to only by virtual links). Since parent book keeping is expensive we don't want to separately track virtual/real parents so instead just don't duplicate nodes that have at least one incoming virtual link. --- src/graph/classdef-graph.hh | 2 +- src/graph/coverage-graph.hh | 2 +- src/graph/graph.hh | 61 ++++++++++++++++++++++++++++--------- src/graph/gsubgpos-graph.hh | 6 ++-- src/graph/ligature-graph.hh | 4 +-- src/graph/pairpos-graph.hh | 2 +- src/hb-repacker.hh | 11 ++++--- src/test-repacker.cc | 60 ++++++++++++++++++++++++++++++++++++ 8 files changed, 122 insertions(+), 26 deletions(-) diff --git a/src/graph/classdef-graph.hh b/src/graph/classdef-graph.hh index da6378820..d6c174e87 100644 --- a/src/graph/classdef-graph.hh +++ b/src/graph/classdef-graph.hh @@ -74,7 +74,7 @@ struct ClassDef : public OT::ClassDef class_def_link->width = SmallTypes::size; class_def_link->objidx = class_def_prime_id; class_def_link->position = link_position; - class_def_prime_vertex.add_parent (parent_id); + class_def_prime_vertex.add_parent (parent_id, false); return true; } diff --git a/src/graph/coverage-graph.hh b/src/graph/coverage-graph.hh index 1dc882ccd..2314020bf 100644 --- a/src/graph/coverage-graph.hh +++ b/src/graph/coverage-graph.hh @@ -98,7 +98,7 @@ struct Coverage : public OT::Layout::Common::Coverage coverage_link->width = SmallTypes::size; coverage_link->objidx = coverage_prime_id; coverage_link->position = link_position; - coverage_prime_vertex.add_parent (parent_id); + coverage_prime_vertex.add_parent (parent_id, false); return (Coverage*) coverage_prime_vertex.obj.head; } diff --git a/src/graph/graph.hh b/src/graph/graph.hh index 4a787f054..4fc528980 100644 --- a/src/graph/graph.hh +++ b/src/graph/graph.hh @@ -50,6 +50,7 @@ struct graph_t private: unsigned incoming_edges_ = 0; unsigned single_parent = (unsigned) -1; + bool has_incoming_virtual_edges_ = false; hb_hashmap_t parents; public: @@ -66,6 +67,11 @@ struct graph_t return parents.in_error (); } + bool has_incoming_virtual_edges () const + { + return has_incoming_virtual_edges_; + } + bool link_positions_valid (unsigned num_objects, bool removed_nil) { hb_set_t assigned_bytes; @@ -162,6 +168,7 @@ struct graph_t hb_swap (a.single_parent, b.single_parent); hb_swap (a.parents, b.parents); hb_swap (a.incoming_edges_, b.incoming_edges_); + hb_swap (a.has_incoming_virtual_edges_, b.has_incoming_virtual_edges_); hb_swap (a.start, b.start); hb_swap (a.end, b.end); hb_swap (a.priority, b.priority); @@ -207,13 +214,16 @@ struct graph_t void reset_parents () { incoming_edges_ = 0; + has_incoming_virtual_edges_ = false; single_parent = (unsigned) -1; parents.reset (); } - void add_parent (unsigned parent_index) + void add_parent (unsigned parent_index, bool is_virtual) { assert (parent_index != (unsigned) -1); + has_incoming_virtual_edges_ |= is_virtual; + if (incoming_edges_ == 0) { single_parent = parent_index; @@ -559,7 +569,7 @@ struct graph_t link->width = 2; link->objidx = child_id; link->position = (char*) offset - (char*) v.obj.head; - vertices_[child_id].add_parent (parent_id); + vertices_[child_id].add_parent (parent_id, false); } /* @@ -971,7 +981,7 @@ struct graph_t new_link->position = (const char*) new_offset - (const char*) new_v.obj.head; auto& child = vertices_[child_id]; - child.add_parent (new_parent_idx); + child.add_parent (new_parent_idx, false); old_v.remove_real_link (child_id, old_offset); child.remove_parent (old_parent_idx); @@ -1023,12 +1033,12 @@ struct graph_t for (const auto& l : child.obj.real_links) { clone->obj.real_links.push (l); - vertices_[l.objidx].add_parent (clone_idx); + vertices_[l.objidx].add_parent (clone_idx, false); } for (const auto& l : child.obj.virtual_links) { clone->obj.virtual_links.push (l); - vertices_[l.objidx].add_parent (clone_idx); + vertices_[l.objidx].add_parent (clone_idx, true); } check_success (!clone->obj.real_links.in_error ()); @@ -1081,10 +1091,15 @@ struct graph_t const auto& child = vertices_[child_idx]; unsigned links_to_child = child.incoming_edges_from_parent(parent_idx); - if (child.incoming_edges () <= links_to_child) + if (child.incoming_edges () <= links_to_child || child.has_incoming_virtual_edges()) { // Can't duplicate this node, doing so would orphan the original one as all remaining links // to child are from parent. + // + // We don't allow duplication of nodes with incoming virtual edges because we don't track + // the number of virtual vs real incoming edges. As a result we can't tell if a node + // with virtual edges may end up orphaned by duplication (ie. where one copy is only pointed + // to by virtual edges). DEBUG_MSG (SUBSET_REPACK, nullptr, " Not duplicating %u => %u", parent_idx, child_idx); return -1; @@ -1099,12 +1114,15 @@ struct graph_t if (parent_idx == clone_idx) parent_idx++; auto& parent = vertices_[parent_idx]; + unsigned count = 0; + unsigned num_real = parent.obj.real_links.length; for (auto& l : parent.obj.all_links_writer ()) { + count++; if (l.objidx != child_idx) continue; - reassign_link (l, parent_idx, clone_idx); + reassign_link (l, parent_idx, clone_idx, count > num_real); } return clone_idx; @@ -1137,10 +1155,15 @@ struct graph_t links_to_child += child.incoming_edges_from_parent(parent_idx); } - if (child.incoming_edges () <= links_to_child) + if (child.incoming_edges () <= links_to_child || child.has_incoming_virtual_edges()) { // Can't duplicate this node, doing so would orphan the original one as all remaining links // to child are from parent. + // + // We don't allow duplication of nodes with incoming virtual edges because we don't track + // the number of virtual vs real incoming edges. As a result we can't tell if a node + // with virtual edges may end up orphaned by duplication (ie. where one copy is only pointed + // to by virtual edges). DEBUG_MSG (SUBSET_REPACK, nullptr, " Not duplicating %u, ..., %u => %u", first_parent, last_parent, child_idx); return -1; } @@ -1154,12 +1177,15 @@ struct graph_t // duplicate shifts the root node idx, so if parent_idx was root update it. if (parent_idx == clone_idx) parent_idx++; auto& parent = vertices_[parent_idx]; + unsigned count = 0; + unsigned num_real = parent.obj.real_links.length; for (auto& l : parent.obj.all_links_writer ()) { + count++; if (l.objidx != child_idx) continue; - reassign_link (l, parent_idx, clone_idx); + reassign_link (l, parent_idx, clone_idx, count > num_real); } } @@ -1407,8 +1433,11 @@ struct graph_t for (unsigned p = 0; p < count; p++) { - for (auto& l : vertices_.arrayZ[p].obj.all_links ()) - vertices_[l.objidx].add_parent (p); + for (auto& l : vertices_.arrayZ[p].obj.real_links) + vertices_[l.objidx].add_parent (p, false); + + for (auto& l : vertices_.arrayZ[p].obj.virtual_links) + vertices_[l.objidx].add_parent (p, true); } for (unsigned i = 0; i < count; i++) @@ -1511,12 +1540,13 @@ struct graph_t */ void reassign_link (hb_serialize_context_t::object_t::link_t& link, unsigned parent_idx, - unsigned new_idx) + unsigned new_idx, + bool is_virtual) { unsigned old_idx = link.objidx; link.objidx = new_idx; vertices_[old_idx].remove_parent (parent_idx); - vertices_[new_idx].add_parent (parent_idx); + vertices_[new_idx].add_parent (parent_idx, is_virtual); } /* @@ -1530,13 +1560,16 @@ struct graph_t if (!id_map) return; for (unsigned i : subgraph) { + unsigned num_real = vertices_[i].obj.real_links.length; + unsigned count = 0; for (auto& link : vertices_[i].obj.all_links_writer ()) { + count++; const uint32_t *v; if (!id_map.has (link.objidx, &v)) continue; if (only_wide && !(link.width == 4 && !link.is_signed)) continue; - reassign_link (link, i, *v); + reassign_link (link, i, *v, count > num_real); } } } diff --git a/src/graph/gsubgpos-graph.hh b/src/graph/gsubgpos-graph.hh index 0226264d8..0e76edd55 100644 --- a/src/graph/gsubgpos-graph.hh +++ b/src/graph/gsubgpos-graph.hh @@ -238,7 +238,7 @@ struct Lookup : public OT::Lookup if (is_ext) { unsigned ext_id = create_extension_subtable (c, subtable_id, type); - c.graph.vertices_[subtable_id].add_parent (ext_id); + c.graph.vertices_[subtable_id].add_parent (ext_id, false); subtable_id = ext_id; } @@ -247,7 +247,7 @@ struct Lookup : public OT::Lookup link->objidx = subtable_id; link->position = (char*) &new_lookup->subTable[offset_index++] - (char*) new_lookup; - c.graph.vertices_[subtable_id].add_parent (this_index); + c.graph.vertices_[subtable_id].add_parent (this_index, false); } } @@ -338,7 +338,7 @@ struct Lookup : public OT::Lookup // Make extension point at the subtable. auto& ext_vertex = c.graph.vertices_[ext_index]; - ext_vertex.add_parent (lookup_index); + ext_vertex.add_parent (lookup_index, false); if (!existing_ext_index) subtable_vertex.remap_parent (lookup_index, ext_index); diff --git a/src/graph/ligature-graph.hh b/src/graph/ligature-graph.hh index 6c75fe8aa..99cc49368 100644 --- a/src/graph/ligature-graph.hh +++ b/src/graph/ligature-graph.hh @@ -206,7 +206,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 void add_virtual_link(gsubgpos_graph_context_t& c, unsigned from, unsigned to) const { auto& from_obj = c.graph.vertices_[from].obj; - c.graph.vertices_[to].add_parent(from); + c.graph.vertices_[to].add_parent(from, true); auto& link = *from_obj.virtual_links.push (); link.objidx = to; } @@ -243,7 +243,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 coverage_prime_link->width = SmallTypes::size; coverage_prime_link->objidx = coverage_prime_id; coverage_prime_link->position = 2; - coverage_prime_vertex.add_parent (liga_subst_prime_id); + coverage_prime_vertex.add_parent (liga_subst_prime_id, false); // Locate all liga sets with ligas between start and end. // Clone or move them as needed. diff --git a/src/graph/pairpos-graph.hh b/src/graph/pairpos-graph.hh index fd46861de..46da27406 100644 --- a/src/graph/pairpos-graph.hh +++ b/src/graph/pairpos-graph.hh @@ -423,7 +423,7 @@ struct PairPosFormat2 : public OT::Layout::GPOS_impl::PairPosFormat2_4width = SmallTypes::size; class_def_link->objidx = class_def_2_id; class_def_link->position = 10; - graph.vertices_[class_def_2_id].add_parent (pair_pos_prime_id); + graph.vertices_[class_def_2_id].add_parent (pair_pos_prime_id, false); graph.duplicate (pair_pos_prime_id, class_def_2_id); return pair_pos_prime_id; diff --git a/src/hb-repacker.hh b/src/hb-repacker.hh index cb4fdeead..11d47c9dc 100644 --- a/src/hb-repacker.hh +++ b/src/hb-repacker.hh @@ -266,7 +266,7 @@ bool _resolve_shared_overflow(const hb_vector_t& overf result = sorted_graph.duplicate(&parents, r.child); } - if (result == (unsigned) -1) return result; + if (result == (unsigned) -1) return false; if (parents.get_population() > 1) { // If the duplicated node has more than one parent pre-emptively raise it's priority to the maximum. @@ -283,7 +283,7 @@ bool _resolve_shared_overflow(const hb_vector_t& overf sorted_graph.vertices_[result].give_max_priority(); } - return result; + return true; } static inline @@ -302,8 +302,11 @@ bool _process_overflows (const hb_vector_t& overflows, { // The child object is shared, we may be able to eliminate the overflow // by duplicating it. - if (!_resolve_shared_overflow(overflows, i, sorted_graph)) continue; - return true; + if (_resolve_shared_overflow(overflows, i, sorted_graph)) + return true; + + // Sometimes we can't duplicate a node which looks shared because it's not actually shared + // (eg. all links from the same parent) in this case continue on to other resolution options. } if (child.is_leaf () && !priority_bumped_parents.has (r.parent)) diff --git a/src/test-repacker.cc b/src/test-repacker.cc index a6d112f19..750f8e591 100644 --- a/src/test-repacker.cc +++ b/src/test-repacker.cc @@ -558,6 +558,39 @@ populate_serializer_simple (hb_serialize_context_t* c) c->end_serialize(); } +static void +populate_serializer_virtual (hb_serialize_context_t* c, bool with_overflow) +{ + std::string large_string(50000, 'a'); + c->start_serialize (); + + unsigned obj_4, obj_5; + if (with_overflow) { + obj_5 = add_object("55555", 5, c); + obj_4 = add_object(large_string.c_str(), 50000, c); + } else { + obj_4 = add_object(large_string.c_str(), 50000, c); + obj_5 = add_object("55555", 5, c); + } + + start_object(large_string.c_str(), 20000, c); + add_offset(obj_5, c); + unsigned obj_3 = c->pop_pack(false); + + start_object("2", 2, c); + add_virtual_offset(obj_5, c); + unsigned obj_2 = c->pop_pack(false); + + // obj 1 + start_object("1", 1, c); + add_offset(obj_2, c); + add_offset(obj_3, c); + add_offset(obj_4, c); + c->pop_pack(false); + + c->end_serialize(); +} + static void populate_serializer_with_overflow (hb_serialize_context_t* c) { @@ -2408,6 +2441,32 @@ test_repack_last () free (expected_buffer); } +static void +test_dont_duplicate_virtual () +{ + size_t buffer_size = 200000; + void* buffer = malloc (buffer_size); + assert (buffer); + hb_serialize_context_t c (buffer, buffer_size); + populate_serializer_virtual (&c, true); + + void* expected_buffer = malloc (buffer_size); + assert (expected_buffer); + hb_serialize_context_t e (expected_buffer, buffer_size); + populate_serializer_virtual (&e, false); + + run_resolve_overflow_test ("test_dont_duplicate_virtual", + c, + e, + 20, + false, + HB_TAG('a', 'b', 'c', 'd'), + true); + + free (buffer); + free (expected_buffer); +} + // TODO(garretrieger): update will_overflow tests to check the overflows array. // TODO(garretrieger): add tests for priority raising. @@ -2447,6 +2506,7 @@ main (int argc, char **argv) test_resolve_with_basic_mark_base_pos_1_split (); test_resolve_with_basic_liga_split (); test_resolve_with_liga_split_move (); + test_dont_duplicate_virtual (); // 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. From e50d16f6f8e3f9bc0cbfe0320511d544b861745e Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Wed, 9 Jul 2025 23:48:45 +0000 Subject: [PATCH 08/12] [repacker] During liga splitting re-use orphaned liga sets. --- src/graph/graph.hh | 12 ++-- src/graph/gsubgpos-graph.hh | 18 +++--- src/graph/ligature-graph.hh | 44 +++++++++++++- src/test-repacker.cc | 115 +++++++++++++++++++++++++++++++++++- 4 files changed, 174 insertions(+), 15 deletions(-) diff --git a/src/graph/graph.hh b/src/graph/graph.hh index 4fc528980..fcabfb9e1 100644 --- a/src/graph/graph.hh +++ b/src/graph/graph.hh @@ -127,7 +127,9 @@ struct graph_t } } - bool equals (const vertex_t& other, + bool equals (unsigned this_index, + unsigned other_index, + const vertex_t& other, const graph_t& graph, const graph_t& other_graph, unsigned depth) const @@ -135,8 +137,10 @@ struct graph_t if (!(as_bytes () == other.as_bytes ())) { DEBUG_MSG (SUBSET_REPACK, nullptr, - "vertex [%lu] bytes != [%lu] bytes, depth = %u", + "vertex %u [%lu] bytes != %u [%lu] bytes, depth = %u", + this_index, (unsigned long) table_size (), + other_index, (unsigned long) other.table_size (), depth); @@ -418,7 +422,7 @@ struct graph_t link_a.bias != link_b.bias) return false; - if (!graph.vertices_[link_a.objidx].equals ( + if (!graph.vertices_[link_a.objidx].equals (link_a.objidx, link_b.objidx, other_graph.vertices_[link_b.objidx], graph, other_graph, depth + 1)) return false; @@ -500,7 +504,7 @@ struct graph_t bool operator== (const graph_t& other) const { - return root ().equals (other.root (), *this, other, 0); + return root ().equals (root_idx(), other.root_idx(), other.root (), *this, other, 0); } void print () const { diff --git a/src/graph/gsubgpos-graph.hh b/src/graph/gsubgpos-graph.hh index 0e76edd55..2c713b00d 100644 --- a/src/graph/gsubgpos-graph.hh +++ b/src/graph/gsubgpos-graph.hh @@ -24,7 +24,7 @@ * Google Author(s): Garret Rieger */ -#include "OT/Layout/GSUB/SubstLookupSubTable.hh" +#include "../OT/Layout/GSUB/SubstLookupSubTable.hh" #include "graph.hh" #include "../hb-ot-layout-gsubgpos.hh" #include "../OT/Layout/GSUB/ExtensionSubst.hh" @@ -203,14 +203,14 @@ struct Lookup : public OT::Lookup hb_vector_t>>& subtable_ids) { bool is_ext = is_extension (c.table_tag); - auto& v = c.graph.vertices_[this_index]; + auto* v = &c.graph.vertices_[this_index]; fix_existing_subtable_links (c, this_index, subtable_ids); unsigned new_subtable_count = 0; for (const auto& p : subtable_ids) new_subtable_count += p.second.length; - size_t new_size = v.table_size () + size_t new_size = v->table_size () + new_subtable_count * OT::Offset16::static_size; char* buffer = (char*) hb_calloc (1, new_size); if (!buffer) return false; @@ -219,10 +219,10 @@ struct Lookup : public OT::Lookup hb_free (buffer); return false; } - hb_memcpy (buffer, v.obj.head, v.table_size()); + hb_memcpy (buffer, v->obj.head, v->table_size()); - v.obj.head = buffer; - v.obj.tail = buffer + new_size; + v->obj.head = buffer; + v->obj.tail = buffer + new_size; Lookup* new_lookup = (Lookup*) buffer; @@ -240,9 +240,11 @@ struct Lookup : public OT::Lookup unsigned ext_id = create_extension_subtable (c, subtable_id, type); c.graph.vertices_[subtable_id].add_parent (ext_id, false); subtable_id = ext_id; + // the reference to v may have changed on adding a node, so reassign it. + v = &c.graph.vertices_[this_index]; } - auto* link = v.obj.real_links.push (); + auto* link = v->obj.real_links.push (); link->width = 2; link->objidx = subtable_id; link->position = (char*) &new_lookup->subTable[offset_index++] - @@ -252,7 +254,7 @@ struct Lookup : public OT::Lookup } // Repacker sort order depends on link order, which we've messed up so resort it. - v.obj.real_links.qsort (); + v->obj.real_links.qsort (); // The head location of the lookup has changed, invalidating the lookups map entry // in the context. Update the map. diff --git a/src/graph/ligature-graph.hh b/src/graph/ligature-graph.hh index 99cc49368..99bdacd11 100644 --- a/src/graph/ligature-graph.hh +++ b/src/graph/ligature-graph.hh @@ -211,13 +211,47 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 link.objidx = to; } + hb_pair_t current_liga_set_bounds (gsubgpos_graph_context_t& c, + unsigned liga_set_index, + const hb_serialize_context_t::object_t& liga_set) const + { + // Finds the actual liga indices present in the liga set currently. Takes + // into account those that have been removed by processing. + unsigned min_index = (unsigned) -1; + unsigned max_index = 0; + for (const auto& l : liga_set.real_links) { + if (l.position < 2) continue; + + unsigned liga_index = (l.position - 2) / 2; + min_index = hb_min(min_index, liga_index); + max_index = hb_max(max_index, liga_index); + } + return hb_pair(min_index, max_index + 1); + } + + void compact_liga_set (gsubgpos_graph_context_t& c, LigatureSet* table, hb_serialize_context_t::object_t& obj) const + { + if (table->ligature.len <= obj.real_links.length) return; + + // compact the remaining linked liga offsets into a continous array and shrink the node as needed. + unsigned to_remove = table->ligature.len - obj.real_links.length; + unsigned new_position = SmallTypes::size; + obj.real_links.qsort(); // for this to work we need to process links in order of position. + for (auto& l : obj.real_links) + { + l.position = new_position; + new_position += SmallTypes::size; + } + + table->ligature.len = obj.real_links.length; + obj.tail -= to_remove * SmallTypes::size; + } + unsigned clone_range (gsubgpos_graph_context_t& c, unsigned this_index, hb_vector_t liga_counts, unsigned start, unsigned end) const { - // TODO XXXX liga set's that are part of two clone_range's will not be pruned from the graph and will - // end up orphaned. Add a test for this case. DEBUG_MSG (SUBSET_REPACK, nullptr, " Cloning LigatureSubstFormat1 (%u) range [%u, %u).", this_index, start, end); @@ -270,6 +304,11 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 return -1; } + // Bounds may need to be adjusted if some ligas have been previously removed. + hb_pair_t liga_bounds = current_liga_set_bounds(c, liga_set_index, liga_set.vertex->obj); + current_start = hb_max(count + liga_bounds.first, current_start); + current_end = hb_min(count + liga_bounds.second, current_end); + unsigned liga_set_prime_id; if (current_start >= start && current_end <= end) { // This liga set is fully contined within [start, end) @@ -280,6 +319,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 &ligatureSet[i], liga_subst_prime_id, &liga_subst_prime->ligatureSet[liga_set_count++]); + compact_liga_set(c, liga_set.table, liga_set.vertex->obj); } else { diff --git a/src/test-repacker.cc b/src/test-repacker.cc index 750f8e591..51256b9e0 100644 --- a/src/test-repacker.cc +++ b/src/test-repacker.cc @@ -1706,6 +1706,95 @@ populate_serializer_with_large_liga (hb_serialize_context_t* c, bool sequential_ c->end_serialize(); } +static void +populate_serializer_with_large_liga_overlapping_clone_result (hb_serialize_context_t* c) +{ + std::string large_string(100000, 'a'); + c->start_serialize (); + + constexpr unsigned liga_size = 30000; + + unsigned liga[6]; + unsigned liga_subst[3]; + unsigned liga_set[2]; + + // LigaSubst 3 + unsigned coverage = add_coverage(1, 1, c); + for (int i = 1; i >= 0; i--) { + start_object (large_string.c_str(), liga_size, c); + add_virtual_offset(coverage, c); + liga[i] = c->pop_pack (false); + } + + add_liga_set_header(2, c); + add_virtual_offset(coverage, c); + for (unsigned i = 0; i < 2; i++) + add_offset(liga[i], c); + liga_set[0] = c->pop_pack(false); + + add_liga_header(coverage, 1, c); + add_offset(liga_set[0], c); + liga_subst[2] = c->pop_pack(false); + + // LigaSubst 2 + coverage = add_coverage(0, 1, c); + for (int i = 1; i >= 0; i--) { + start_object (large_string.c_str(), liga_size, c); + add_virtual_offset(coverage, c); + liga[i] = c->pop_pack (false); + } + + add_liga_set_header(1, c); + add_virtual_offset(coverage, c); + add_offset(liga[1], c); + liga_set[1] = c->pop_pack(false); + + add_liga_set_header(1, c); + add_virtual_offset(coverage, c); + add_offset(liga[0], c); + liga_set[0] = c->pop_pack(false); + + add_liga_header(coverage, 2, c); + add_offset(liga_set[0], c); + add_offset(liga_set[1], c); + liga_subst[1] = c->pop_pack(false); + + // LigaSubst 1 + coverage = add_coverage(0, 0, c); + for (int i = 1; i >= 0; i--) { + start_object (large_string.c_str(), liga_size, c); + add_virtual_offset(coverage, c); + liga[i] = c->pop_pack (false); + } + + add_liga_set_header(2, c); + add_virtual_offset(coverage, c); + for (unsigned i = 0; i < 2; i++) + add_offset(liga[i], c); + liga_set[0] = c->pop_pack(false); + + add_liga_header(coverage, 1, c); + add_offset(liga_set[0], c); + liga_subst[0] = c->pop_pack(false); + + for (int l = 2; l >= 0; l--) { + liga_subst[l] = add_extension(liga_subst[l], 4, c); + } + + start_lookup (7, 3, c); + for (unsigned l = 0; l < 3; 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; @@ -2287,7 +2376,30 @@ static void test_resolve_with_liga_split_move () hb_serialize_context_t e (expected_buffer, buffer_size); populate_serializer_with_large_liga<3, 2, 2, 16000>(&e, true); - run_resolve_overflow_test ("test_resolve_with_basic_liga_split", + run_resolve_overflow_test ("test_resolve_with_liga_split_move", + c, + e, + 20, + true, + HB_TAG('G', 'S', 'U', 'B')); + free (buffer); + free (expected_buffer); +} + +static void test_resolve_with_liga_split_overlapping_clone () +{ + size_t buffer_size = 400000; + void* buffer = malloc (buffer_size); + assert (buffer); + hb_serialize_context_t c (buffer, buffer_size); + populate_serializer_with_large_liga<1, 2, 3, 30000>(&c, true); + + void* expected_buffer = malloc (buffer_size); + assert (expected_buffer); + hb_serialize_context_t e (expected_buffer, buffer_size); + populate_serializer_with_large_liga_overlapping_clone_result(&e); + + run_resolve_overflow_test ("test_resolve_with_liga_split_overlapping_clone", c, e, 20, @@ -2507,6 +2619,7 @@ main (int argc, char **argv) test_resolve_with_basic_liga_split (); test_resolve_with_liga_split_move (); test_dont_duplicate_virtual (); + test_resolve_with_liga_split_overlapping_clone (); // 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. From 2dfaf2a22e5b806522760caa53b4944023c9974c Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Thu, 10 Jul 2025 01:00:20 +0000 Subject: [PATCH 09/12] [repacker] fix imports. --- src/graph/graph.hh | 5 ++--- src/graph/gsubgpos-graph.hh | 3 +-- src/graph/ligature-graph.hh | 16 +++++++--------- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/graph/graph.hh b/src/graph/graph.hh index fcabfb9e1..332f27169 100644 --- a/src/graph/graph.hh +++ b/src/graph/graph.hh @@ -137,7 +137,7 @@ struct graph_t if (!(as_bytes () == other.as_bytes ())) { DEBUG_MSG (SUBSET_REPACK, nullptr, - "vertex %u [%lu] bytes != %u [%lu] bytes, depth = %u", + "vertex %u [%lu bytes] != %u [%lu bytes], depth = %u", this_index, (unsigned long) table_size (), other_index, @@ -726,9 +726,8 @@ struct graph_t if (!r.table) return vertex_and_table_t (); - if (!r.table->sanitize (*(r.vertex), std::forward(ds)...)) { + if (!r.table->sanitize (*(r.vertex), std::forward(ds)...)) return vertex_and_table_t (); - } return r; } diff --git a/src/graph/gsubgpos-graph.hh b/src/graph/gsubgpos-graph.hh index 2c713b00d..77ba4ee18 100644 --- a/src/graph/gsubgpos-graph.hh +++ b/src/graph/gsubgpos-graph.hh @@ -24,12 +24,11 @@ * Google Author(s): Garret Rieger */ -#include "../OT/Layout/GSUB/SubstLookupSubTable.hh" #include "graph.hh" #include "../hb-ot-layout-gsubgpos.hh" #include "../OT/Layout/GSUB/ExtensionSubst.hh" +#include "../OT/Layout/GSUB/SubstLookupSubTable.hh" #include "gsubgpos-context.hh" -#include "hb-ot.h" #include "pairpos-graph.hh" #include "markbasepos-graph.hh" #include "ligature-graph.hh" diff --git a/src/graph/ligature-graph.hh b/src/graph/ligature-graph.hh index 99bdacd11..611ab686e 100644 --- a/src/graph/ligature-graph.hh +++ b/src/graph/ligature-graph.hh @@ -27,13 +27,11 @@ #ifndef GRAPH_LIGATURE_GRAPH_HH #define GRAPH_LIGATURE_GRAPH_HH -#include "OT/Layout/GSUB/LigatureSet.hh" -#include "OT/Layout/types.hh" -#include "hb-ot-layout-common.hh" - #include "graph.hh" #include "../OT/Layout/GSUB/LigatureSubst.hh" #include "../OT/Layout/GSUB/LigatureSubstFormat1.hh" +#include "../OT/Layout/GSUB/LigatureSet.hh" +#include "../OT/Layout/types.hh" #include #include @@ -181,16 +179,16 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 } }; - std::pair new_liga_set(gsubgpos_graph_context_t& c, unsigned count) const { + hb_pair_t new_liga_set(gsubgpos_graph_context_t& c, unsigned count) const { unsigned prime_size = OT::Layout::GSUB_impl::LigatureSet::min_size + count * SmallTypes::size; unsigned prime_id = c.create_node (prime_size); - if (prime_id == (unsigned) -1) return std::make_pair(-1, nullptr); + if (prime_id == (unsigned) -1) return hb_pair(-1, nullptr); LigatureSet* prime = (LigatureSet*) c.graph.object (prime_id).head; prime->ligature.len = count; - return std::make_pair(prime_id, prime); + return hb_pair(prime_id, prime); } void clear_virtual_links (gsubgpos_graph_context_t& c, unsigned node_index) const @@ -325,14 +323,14 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 { // This liga set partially overlaps [start, end). We'll need to create // a new liga set sub table and move the intersecting ligas to it. - unsigned liga_count = std::min(end, current_end) - std::max(start, current_start); + unsigned liga_count = hb_min(end, current_end) - hb_max(start, current_start); auto result = new_liga_set(c, liga_count); liga_set_prime_id = result.first; LigatureSet* prime = result.second; if (liga_set_prime_id == (unsigned) -1) return -1; unsigned new_index = 0; - for (unsigned j = std::max(start, current_start) - count; j < std::min(end, current_end) - count; j++) { + for (unsigned j = hb_max(start, current_start) - count; j < hb_min(end, current_end) - count; j++) { c.graph.move_child<> (liga_set_index, &liga_set.table->ligature[j], liga_set_prime_id, From 95dc11092fae24d2b116b603b1e496f4dd4e8070 Mon Sep 17 00:00:00 2001 From: Qunxin Liu Date: Thu, 10 Jul 2025 09:52:07 -0700 Subject: [PATCH 10/12] [subset] bug fix Don't downgrade to format1 when subsetting --- src/OT/Layout/GPOS/AnchorFormat3.hh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/OT/Layout/GPOS/AnchorFormat3.hh b/src/OT/Layout/GPOS/AnchorFormat3.hh index b683a0a0c..e08c380a0 100644 --- a/src/OT/Layout/GPOS/AnchorFormat3.hh +++ b/src/OT/Layout/GPOS/AnchorFormat3.hh @@ -94,7 +94,8 @@ struct AnchorFormat3 /* in case that all axes are pinned or no variations after instantiation, * both var_idxes will be mapped to HB_OT_LAYOUT_NO_VARIATIONS_INDEX */ if (x_varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX && - y_varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX) + y_varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX && + c->plan->normalized_coords) return_trace (c->serializer->check_assign (out->format, 1, HB_SERIALIZE_ERROR_INT_OVERFLOW)); if (!c->serializer->embed (xDeviceTable)) return_trace (false); From 971036888f8cdf3092c99f777beba35168699421 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 11 Jul 2025 00:30:48 -0600 Subject: [PATCH 11/12] [perf/hb-draw-compare] Default font-funcs2 to fontations Yay! --- perf/hb-draw-compare | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/perf/hb-draw-compare b/perf/hb-draw-compare index 61c4b0a5d..726cb74a5 100755 --- a/perf/hb-draw-compare +++ b/perf/hb-draw-compare @@ -18,7 +18,7 @@ variations= face_loader=ot shaper=ot font_funcs1=ot -font_funcs2=coretext +font_funcs2=fontations clear1=false clear2=false unicodes= From deb60ecf180c97fc42348a2388315d68f23aa12b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 11 Jul 2025 00:30:07 -0600 Subject: [PATCH 12/12] [fontations] Cache glyph_from_names hashmap on the face Fixes https://github.com/harfbuzz/harfbuzz/issues/5384 --- src/rust/font.rs | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/rust/font.rs b/src/rust/font.rs index e2180a2e0..cee949aab 100644 --- a/src/rust/font.rs +++ b/src/rust/font.rs @@ -5,7 +5,7 @@ use std::ffi::c_void; use std::mem::transmute; use std::ptr::null_mut; use std::sync::atomic::{AtomicPtr, AtomicU32, Ordering}; -use std::sync::{Mutex, OnceLock}; +use std::sync::{Arc, Mutex}; use skrifa::charmap::Charmap; use skrifa::charmap::MapVariant::Variant; @@ -35,7 +35,6 @@ struct FontationsData<'a> { outline_glyphs: OutlineGlyphCollection<'a>, color_glyphs: ColorGlyphCollection<'a>, glyph_names: GlyphNames<'a>, - glyph_from_names: OnceLock>, size: Size, vert_metrics: Option>, vert_origin: Option>, @@ -89,7 +88,6 @@ impl FontationsData<'_> { outline_glyphs, color_glyphs, glyph_names, - glyph_from_names: OnceLock::new(), size: Size::new(upem as f32), vert_metrics, vert_origin, @@ -980,8 +978,14 @@ extern "C" fn _hb_fontations_glyph_name( } } +static mut GLYPH_FROM_NAMES_KEY: hb_user_data_key_t = hb_user_data_key_t { unused: 0 }; + +extern "C" fn _hb_glyph_from_names_destroy(data: *mut c_void) { + let _ = unsafe { Arc::from_raw(data as *const HashMap) }; +} + extern "C" fn _hb_fontations_glyph_from_name( - _font: *mut hb_font_t, + font: *mut hb_font_t, font_data: *mut ::std::os::raw::c_void, name: *const ::std::os::raw::c_char, len: ::std::os::raw::c_int, @@ -992,13 +996,35 @@ extern "C" fn _hb_fontations_glyph_from_name( let name = unsafe { std::slice::from_raw_parts(name as *const u8, len as usize) }; let name = std::str::from_utf8(name).unwrap_or_default(); + let mut glyph_from_names: *mut c_void; - let glyph_from_names = data.glyph_from_names.get_or_init(|| { - data.glyph_names - .iter() - .map(|(gid, name)| (name.to_string(), gid.to_u32())) - .collect() - }); + let face = unsafe { hb_font_get_face(font) }; + loop { + glyph_from_names = unsafe { hb_face_get_user_data(face, &raw mut GLYPH_FROM_NAMES_KEY) }; + if !glyph_from_names.is_null() { + break; + } + // Create a new HashMap to store the glyph names + let mut glyph_from_names = HashMap::new(); + for (glyph_id, glyph_name) in data.glyph_names.iter() { + glyph_from_names.insert(glyph_name.to_string(), glyph_id.to_u32()); + } + + let glyph_from_names = Arc::new(glyph_from_names); + unsafe { + if hb_face_set_user_data( + face, + &raw mut GLYPH_FROM_NAMES_KEY, + Arc::into_raw(glyph_from_names) as *mut c_void, + Some(_hb_glyph_from_names_destroy), + true as hb_bool_t, + ) != false as hb_bool_t + { + break; + } + } + } + let glyph_from_names = unsafe { &*(glyph_from_names as *const HashMap) }; if let Some(glyph_id) = glyph_from_names.get(name) { unsafe { *glyph = *glyph_id;