From 02d8e9bac617a53f2a311c3ee609b4dd6057faa5 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Thu, 12 Nov 2026 22:55:53 +0000 Subject: [PATCH 1/2] Split cmap closure phase into multiple phases: - Bidi - Unicode Decomp - Unicode Comp - Variation Selectors - cmap unicode to glyph These are ordered to match the order in which harfbuzz shaping processes these things, by keeping them as separate phases cycles are eliminated (eg. unicode comp -> decomp -> comp). --- ift/dep_graph/BUILD | 1 + ift/dep_graph/dependency_graph.cc | 181 +++++++++----- ift/dep_graph/dependency_graph.h | 61 ++++- ift/dep_graph/dependency_graph_test.cc | 319 ++++++++++++++++++++++++- ift/dep_graph/pending_edge.h | 10 +- ift/dep_graph/traversal.h | 17 ++ ift/encoder/dependency_closure.cc | 27 +-- ift/encoder/dependency_closure.h | 2 +- ift/encoder/dependency_closure_test.cc | 58 +++++ 9 files changed, 579 insertions(+), 97 deletions(-) diff --git a/ift/dep_graph/BUILD b/ift/dep_graph/BUILD index 88b21e3a..28d2eb7c 100644 --- a/ift/dep_graph/BUILD +++ b/ift/dep_graph/BUILD @@ -76,6 +76,7 @@ cc_test( "//ift/freq:common", "@googletest//:gtest", "@googletest//:gtest_main", + "@harfbuzz", ], ) diff --git a/ift/dep_graph/dependency_graph.cc b/ift/dep_graph/dependency_graph.cc index b14732db..4b029ab0 100644 --- a/ift/dep_graph/dependency_graph.cc +++ b/ift/dep_graph/dependency_graph.cc @@ -118,9 +118,9 @@ class TraversalContext { hb_subset_depend_t* depend = nullptr; // Only edges from these tables will be followed. - flat_hash_set table_filter = {FontHelper::kCmap, FontHelper::kGlyf, - FontHelper::kGSUB, FontHelper::kCOLR, - FontHelper::kMATH, FontHelper::kCFF}; + flat_hash_set table_filter = { + std::begin(DependencyGraph::kClosurePhaseTable), + std::end(DependencyGraph::kClosurePhaseTable)}; // Only edges that originate from and end at glyphs from this filter will be // followed. @@ -226,16 +226,13 @@ class TraversalContext { // Traverse an edge with no special context and/or additional information // other than table tag - Status TraverseEdgeTo(Node source, Node dest, - std::optional table_tag = std::nullopt) { + Status TraverseEdgeTo(Node source, Node dest, hb_tag_t table_tag) { if (!ShouldFollow(dest, table_tag, std::nullopt)) { return absl::OkStatus(); } - TRYV(callback.Visit( - *this, - PendingEdge::Disjunctive( - source, dest, table_tag.value_or(HB_TAG(' ', ' ', ' ', ' '))))); + TRYV(callback.Visit(*this, + PendingEdge::Disjunctive(source, dest, table_tag))); Reached(dest); return absl::OkStatus(); } @@ -256,9 +253,11 @@ class TraversalContext { return absl::OkStatus(); } - PendingEdge edge = PendingEdge::Uvs(a, b, gid); + PendingEdge edge = + PendingEdge::Uvs(a, b, gid, DependencyGraph::kPhaseVariationSelectors); Node dest = Node::Glyph(gid); - return TraversePendingEdgeTo(dest, edge, FontHelper::kCmap); + return TraversePendingEdgeTo(dest, edge, + DependencyGraph::kPhaseVariationSelectors); } Status TraverseCompositionEdge(hb_codepoint_t a, hb_codepoint_t b, @@ -271,9 +270,11 @@ class TraversalContext { return absl::OkStatus(); } - PendingEdge edge = PendingEdge::UnicodeComposition(a, b, dest_unicode); + PendingEdge edge = PendingEdge::UnicodeComposition( + a, b, dest_unicode, DependencyGraph::kPhaseComposition); Node dest = Node::Unicode(dest_unicode); - return TraversePendingEdgeTo(dest, edge, FontHelper::kCmap); + return TraversePendingEdgeTo(dest, edge, + DependencyGraph::kPhaseComposition); } Status TraverseGsubEdgeTo(glyph_id_t source_gid, glyph_id_t dest_gid, @@ -420,13 +421,13 @@ class TraversalContext { void Pending(PendingEdge edge) { pending_edges_.push_back(edge); } - bool ShouldFollow(Node node, std::optional table_tag, + bool ShouldFollow(Node node, hb_tag_t table_tag, std::optional layout_feature) const { if (!node.Matches(node_type_filter)) { return false; } - if (table_tag.has_value() && !table_filter.contains(*table_tag)) { + if (!table_filter.contains(table_tag)) { return false; } @@ -458,7 +459,9 @@ class TraversalContext { Status DependencyGraph::ClosureState::Visit( const TraversalContext& context, const PendingEdge& edge) { - if (edge.table_tag == HB_TAG(' ', ' ', ' ', ' ')) { + if (edge.table_tag == kPhaseVariationSelectors) { + traversal.Visit(edge.dest, FontHelper::kCmap); + } else if (IsSpecialClosurePhase(edge.table_tag)) { traversal.Visit(edge.dest); } else if (edge.table_tag == FontHelper::kGSUB && edge.required_feature.has_value()) { @@ -771,6 +774,32 @@ StatusOr DependencyGraph::TraverseGraph( return std::move(context->callback.traversal); } +IntSet DependencyGraph::ActiveClosurePhases() const { + IntSet out; + flat_hash_set table_tags = FontHelper::GetTags(original_face_.get()); + for (uint32_t phase = 0; phase < kNumberOfClosurePhases; phase++) { + hb_tag_t table = kClosurePhaseTable[phase]; + if (table == kPhaseInit) { + out.insert(phase); + } else if (table == kPhaseVariationSelectors) { + if (!unicode_edges_.variation_selector.empty()) { + out.insert(phase); + } + } else if (table == kPhaseDecomposition) { + if (!unicode_edges_.decomposition.empty()) { + out.insert(phase); + } + } else if (table == kPhaseComposition) { + if (!unicode_edges_.composition.empty()) { + out.insert(phase); + } + } else if (table_tags.contains(table)) { + out.insert(phase); + } + } + return out; +} + StatusOr DependencyGraph::ClosureTraversal( const SegmentSet& start, TraversalMode mode) const { btree_set start_nodes; @@ -802,18 +831,20 @@ StatusOr DependencyGraph::ClosureTraversal( non_init_font_glyphs = segmentation_info_->NonInitFontGlyphs(); } - flat_hash_set table_tags = - FontHelper::GetTags(original_face_.get()); Traversal traversal_full; // Subsetting closure happens in phases which we need to mimic here: - // 1. Unicode closure (bidi) - // 2. Unicode to glyph (cmap + UVS) - // 3. GSUB glyph closure - // 4. MATH closure. - // 5. COLR closure - // 6. glyf closure - // 7. CFF closure + // 0. Init + Bidi mirroring (segment/init font -> unicode/feature, + // bidi unicode -> unicode) + // 1. Variation selectors (unicode -> glyph) + // 2. Unicode canonical decomposition (unicode -> unicode) + // 3. Unicode canonical composition (unicode -> unicode) + // 4. cmap nominal unicode -> glyph + // 5. MATH closure + // 6. GSUB glyph closure + // 7. COLR closure + // 8. glyf closure + // 9. CFF closure // // Reference for the phases and ordering: // _populate_gids_to_retain() from @@ -852,7 +883,7 @@ StatusOr DependencyGraph::ClosureTraversal( traversal_full.Visit(node); } - /* ### Phase 1 + 2: Unicode and Unicode to glyph */ + /* ### Phase 0: Init + Bidi ### */ { TraversalContext context = base_context; context.SetReached(filtered_nodes); @@ -863,13 +894,16 @@ StatusOr DependencyGraph::ClosureTraversal( } /* ### Remaining Phases ### */ - for (unsigned phase = 1; phase < DependencyGraph::kNumberOfClosurePhases; - phase++) { - if (table_tags.contains(DependencyGraph::kClosurePhaseTable[phase])) { - TRYV(ClosureSubTraversal(&base_context, phase, traversal_full)); + IntSet active_phases = ActiveClosurePhases(); + for (uint32_t phase : active_phases) { + if (phase == 0) { + continue; } + TRYV(ClosureSubTraversal(&base_context, phase, traversal_full)); } + traversal_full.PruneReachedPendingEdges(); + return traversal_full; } @@ -878,6 +912,13 @@ Status DependencyGraph::ClosureSubTraversal( Traversal& traversal_full) const { std::vector start_nodes; + if (DependencyGraph::kClosurePhaseStartNodes[phase_index] & + Node::NodeType::UNICODE) { + for (hb_codepoint_t u : traversal_full.ReachedCodepoints()) { + start_nodes.push_back(Node::Unicode(u)); + } + } + if (DependencyGraph::kClosurePhaseStartNodes[phase_index] & Node::NodeType::GLYPH) { for (glyph_id_t gid : traversal_full.ReachedGlyphs()) { @@ -905,52 +946,74 @@ Status DependencyGraph::ClosureSubTraversal( template Status DependencyGraph::HandleUnicodeOutgoingEdges( hb_codepoint_t unicode, TraversalContext* context) const { - { + if (context->table_filter.contains(FontHelper::kCmap)) { auto it = unicode_edges_.unicode_to_gid.find(unicode); if (it != unicode_edges_.unicode_to_gid.end()) { TRYV(context->TraverseEdgeTo(Node::Unicode(unicode), - Node::Glyph(it->second))); + Node::Glyph(it->second), + FontHelper::kCmap)); } } - auto vs_edges = unicode_edges_.variation_selector.find(unicode); - if (vs_edges != unicode_edges_.variation_selector.end()) { - for (VariationSelectorEdge edge : vs_edges->second) { - TRYV(context->TraverseUvsEdge(unicode, edge.unicode, edge.gid)); + if (context->table_filter.contains(kPhaseVariationSelectors)) { + auto vs_edges = unicode_edges_.variation_selector.find(unicode); + if (vs_edges != unicode_edges_.variation_selector.end()) { + for (VariationSelectorEdge edge : vs_edges->second) { + TRYV(context->TraverseUvsEdge(unicode, edge.unicode, edge.gid)); + } } } - auto comp_edges = unicode_edges_.composition.find(unicode); - if (comp_edges != unicode_edges_.composition.end()) { - for (const auto& edge : comp_edges->second) { - TRYV(context->TraverseCompositionEdge(unicode, edge.other_source, - edge.dest)); + if (context->table_filter.contains(kPhaseComposition)) { + auto comp_edges = unicode_edges_.composition.find(unicode); + if (comp_edges != unicode_edges_.composition.end()) { + for (const auto& edge : comp_edges->second) { + TRYV(context->TraverseCompositionEdge(unicode, edge.other_source, + edge.dest)); + } } } - auto decomp_edges = unicode_edges_.decomposition.find(unicode); - if (decomp_edges != unicode_edges_.decomposition.end()) { - for (hb_codepoint_t dest : decomp_edges->second) { - TRYV(context->TraverseEdgeTo(Node::Unicode(unicode), Node::Unicode(dest), - FontHelper::kCmap)); + if (context->table_filter.contains(kPhaseDecomposition)) { + auto decomp_edges = unicode_edges_.decomposition.find(unicode); + if (decomp_edges != unicode_edges_.decomposition.end()) { + for (hb_codepoint_t dest : decomp_edges->second) { + TRYV(context->TraverseEdgeTo(Node::Unicode(unicode), + Node::Unicode(dest), + kPhaseDecomposition)); + } } } - // The subsetter adds unicode bidi mirrors for any unicode codepoints, - // so add a dep graph edge for those if they exist: - auto unicode_funcs = hb_unicode_funcs_get_default(); - hb_codepoint_t mirror = hb_unicode_mirroring(unicode_funcs, unicode); - if (mirror != unicode) { - TRYV( - context->TraverseEdgeTo(Node::Unicode(unicode), Node::Unicode(mirror))); + if (context->table_filter.contains(kPhaseInit)) { + // The subsetter adds unicode bidi mirrors for any unicode codepoints, + // so add a dep graph edge for those if they exist: + auto unicode_funcs = hb_unicode_funcs_get_default(); + hb_codepoint_t mirror = hb_unicode_mirroring(unicode_funcs, unicode); + if (mirror != unicode) { + TRYV(context->TraverseEdgeTo(Node::Unicode(unicode), + Node::Unicode(mirror), kPhaseInit)); + } } return absl::OkStatus(); } +static bool HasGlyphTableFilter(const flat_hash_set& table_filter) { + return table_filter.contains(FontHelper::kMATH) || + table_filter.contains(FontHelper::kGSUB) || + table_filter.contains(FontHelper::kCOLR) || + table_filter.contains(FontHelper::kGlyf) || + table_filter.contains(FontHelper::kCFF); +} + template Status DependencyGraph::HandleGlyphOutgoingEdges( glyph_id_t gid, TraversalContext* context) const { + if (!HasGlyphTableFilter(context->table_filter)) { + return absl::OkStatus(); + } + hb_codepoint_t entry_count = hb_subset_depend_lookup_glyph( dependency_graph_.get(), gid, 0, nullptr, nullptr); for (unsigned i = 0; i < entry_count; i++) { @@ -981,6 +1044,10 @@ Status DependencyGraph::HandleGlyphOutgoingEdges( TRYV(context->TraverseEdgeTo(Node::Glyph(gid), dest, entry.table_tag)); } + if (!context->table_filter.contains(FontHelper::kGSUB)) { + return absl::OkStatus(); + } + auto it = context_glyph_implied_edges_.find(gid); if (it == context_glyph_implied_edges_.end()) { return absl::OkStatus(); @@ -1061,12 +1128,16 @@ template Status DependencyGraph::HandleSubsetDefinitionOutgoingEdges( Node source, const SubsetDefinition& subset_def, TraversalContext* context) const { + if (!context->table_filter.contains(kPhaseInit)) { + return absl::OkStatus(); + } + for (hb_codepoint_t u : subset_def.codepoints) { - TRYV(context->TraverseEdgeTo(source, Node::Unicode(u))); + TRYV(context->TraverseEdgeTo(source, Node::Unicode(u), kPhaseInit)); } for (hb_tag_t f : subset_def.feature_tags) { - TRYV(context->TraverseEdgeTo(source, Node::Feature(f))); + TRYV(context->TraverseEdgeTo(source, Node::Feature(f), kPhaseInit)); } return absl::OkStatus(); } diff --git a/ift/dep_graph/dependency_graph.h b/ift/dep_graph/dependency_graph.h index 1db2ff9a..dbe29de1 100644 --- a/ift/dep_graph/dependency_graph.h +++ b/ift/dep_graph/dependency_graph.h @@ -2,6 +2,7 @@ #define IFT_DEP_GRAPH_DEPENDENCY_GRAPH_H_ #include +#include #include #include "absl/container/btree_set.h" @@ -40,31 +41,60 @@ class TraversalContext; class DependencyGraph { private: public: - static constexpr uint32_t kNumberOfClosurePhases = 6; + // Special closure phase identifiers for non-table phases of closure. + // Use '#' prefix so they never collide with real OpenType table tags. + static constexpr hb_tag_t kPhaseInit = HB_TAG('#', 'i', 'n', 'i'); + static constexpr hb_tag_t kPhaseVariationSelectors = + HB_TAG('#', 'u', 'v', 's'); + static constexpr hb_tag_t kPhaseDecomposition = HB_TAG('#', 'd', 'e', 'c'); + static constexpr hb_tag_t kPhaseComposition = HB_TAG('#', 'c', 'm', 'p'); + + static constexpr bool IsSpecialClosurePhase(hb_tag_t tag) { + return tag == kPhaseInit || tag == kPhaseVariationSelectors || + tag == kPhaseDecomposition || tag == kPhaseComposition; + } + + static constexpr uint32_t kNumberOfClosurePhases = 10; // Reference for the phases and ordering: // _populate_gids_to_retain() from // https://github.com/harfbuzz/harfbuzz/blob/main/src/hb-subset-plan.cc#L439 static constexpr hb_tag_t kClosurePhaseTable[] = { - /* 0 */ common::FontHelper::kCmap, - /* 1 */ common::FontHelper::kMATH, - /* 2 */ common::FontHelper::kGSUB, - /* 3 */ common::FontHelper::kCOLR, - /* 4 */ common::FontHelper::kGlyf, - /* 5 */ common::FontHelper::kCFF, + /* 0 */ kPhaseInit, + /* 1 */ kPhaseVariationSelectors, + /* 2 */ kPhaseDecomposition, + /* 3 */ kPhaseComposition, + /* 4 */ common::FontHelper::kCmap, + /* 5 */ common::FontHelper::kMATH, + /* 6 */ common::FontHelper::kGSUB, + /* 7 */ common::FontHelper::kCOLR, + /* 8 */ common::FontHelper::kGlyf, + /* 9 */ common::FontHelper::kCFF, }; - - static constexpr hb_tag_t kClosurePhaseNodeFilter[] = { - /* cmap */ 0xFFFFFFFF, + static_assert(std::size(kClosurePhaseTable) == kNumberOfClosurePhases); + + static constexpr uint32_t kClosurePhaseNodeFilter[] = { + /* Init + Bidi */ Node::NodeType::SEGMENT | Node::NodeType::INIT_FONT | + Node::NodeType::UNICODE | Node::NodeType::FEATURE, + /* Variation Selectors */ Node::NodeType::UNICODE | + Node::NodeType::GLYPH, + /* Decomposition */ Node::NodeType::UNICODE, + /* Composition */ Node::NodeType::UNICODE, + /* cmap */ Node::NodeType::UNICODE | Node::NodeType::GLYPH, /* MATH */ Node::NodeType::GLYPH, /* GSUB */ Node::NodeType::GLYPH, /* COLR */ Node::NodeType::GLYPH, /* glyf */ Node::NodeType::GLYPH, /* CFF */ Node::NodeType::GLYPH, }; - - static constexpr hb_tag_t kClosurePhaseStartNodes[] = { - /* cmap */ Node::NodeType::SEGMENT, + static_assert(std::size(kClosurePhaseNodeFilter) == kNumberOfClosurePhases); + + static constexpr uint32_t kClosurePhaseStartNodes[] = { + /* Init + Bidi */ Node::NodeType::SEGMENT, + /* Variation Selectors */ Node::NodeType::UNICODE, + /* Decomposition */ Node::NodeType::UNICODE, + /* Composition */ Node::NodeType::UNICODE, + /* cmap */ Node::NodeType::UNICODE, /* MATH */ Node::NodeType::GLYPH, // For GSUB we also need to consider reached features as starting nodes // since those have outgoing GSUB edges. @@ -73,6 +103,7 @@ class DependencyGraph { /* glyf */ Node::NodeType::GLYPH, /* CFF */ Node::NodeType::GLYPH, }; + static_assert(std::size(kClosurePhaseStartNodes) == kNumberOfClosurePhases); enum TraversalMode { ENFORCE_CONTEXT, @@ -102,6 +133,10 @@ class DependencyGraph { const ift::common::CodepointSet* unicode_filter_ptr = nullptr, TraversalMode mode = ENFORCE_CONTEXT) const; + // Returns the set of closure phase indices (0..kNumberOfClosurePhases-1) + // that are active for this graph's font and unicode edges. + ift::common::IntSet ActiveClosurePhases() const; + const absl::flat_hash_set& FullFeatureSet() const { return full_feature_set_; } diff --git a/ift/dep_graph/dependency_graph_test.cc b/ift/dep_graph/dependency_graph_test.cc index 01721b64..3320e4f4 100644 --- a/ift/dep_graph/dependency_graph_test.cc +++ b/ift/dep_graph/dependency_graph_test.cc @@ -4,6 +4,8 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "hb-subset.h" +#include "hb.h" #include "ift/common/bazel_data_file_resolver.h" #include "ift/common/data_file_resolver.h" #include "ift/common/font_data.h" @@ -35,6 +37,7 @@ using ift::encoder::RequestedSegmentationInformation; using ift::encoder::Segment; using ift::encoder::SubsetDefinition; using ift::freq::ProbabilityBound; +using ::testing::Contains; using ::testing::ElementsAre; using ::testing::HasSubstr; using ::testing::UnorderedElementsAre; @@ -607,7 +610,9 @@ TEST_F(DependencyGraphTest, StronglyConnectedComponents_TopologicalSorting) { }); auto sccs_or = graph.StronglyConnectedComponents( - {FontHelper::kCmap, FontHelper::kGSUB, FontHelper::kGlyf}, 0xFFFFFFFF); + {DependencyGraph::kPhaseInit, FontHelper::kCmap, FontHelper::kGSUB, + FontHelper::kGlyf}, + 0xFFFFFFFF); ASSERT_TRUE(sccs_or.ok()) << sccs_or.status(); const std::vector topo_order = FlattenSccs(*sccs_or); glyph_id_t gid_a = *FontHelper::GetNominalGlyph(face.get(), 'a'); @@ -677,7 +682,9 @@ TEST_F(DependencyGraphTest, }); auto sccs_or = graph.StronglyConnectedComponents( - {FontHelper::kCmap, FontHelper::kGSUB, FontHelper::kGlyf}, 0xFFFFFFFF); + {DependencyGraph::kPhaseInit, FontHelper::kCmap, FontHelper::kGSUB, + FontHelper::kGlyf}, + 0xFFFFFFFF); ASSERT_TRUE(sccs_or.ok()) << sccs_or.status(); const std::vector topo_order = FlattenSccs(*sccs_or); @@ -704,7 +711,9 @@ TEST_F(DependencyGraphTest, }); auto sccs_or = graph.StronglyConnectedComponents( - {FontHelper::kCmap, FontHelper::kGSUB, FontHelper::kGlyf}, 0xFFFFFFFF); + {DependencyGraph::kPhaseInit, FontHelper::kCmap, FontHelper::kGSUB, + FontHelper::kGlyf}, + 0xFFFFFFFF); ASSERT_TRUE(sccs_or.ok()) << sccs_or.status(); const std::vector topo_order = FlattenSccs(*sccs_or); @@ -783,10 +792,18 @@ TEST_F(DependencyGraphTest, CollectIncomingEdges_TableFiltering) { EXPECT_NE(edges->find(Node::Glyph(gid_A)), edges->end()); EXPECT_EQ(edges->find(Node::Glyph(gid_smcp_A)), edges->end()); - // With GSUB, it should now be reachable + // With GSUB only, cmap edge to gid_A is excluded while smcp sub of A is + // included. edges = graph.CollectIncomingEdges({FontHelper::kGSUB}, 0xFFFFFFFF, 0xFFFFFFFF); ASSERT_TRUE(edges.ok()) << edges.status(); + EXPECT_EQ(edges->find(Node::Glyph(gid_A)), edges->end()); + EXPECT_NE(edges->find(Node::Glyph(gid_smcp_A)), edges->end()); + + // With both cmap and GSUB, both are reachable. + edges = graph.CollectIncomingEdges({FontHelper::kCmap, FontHelper::kGSUB}, + 0xFFFFFFFF, 0xFFFFFFFF); + ASSERT_TRUE(edges.ok()) << edges.status(); EXPECT_NE(edges->find(Node::Glyph(gid_A)), edges->end()); EXPECT_NE(edges->find(Node::Glyph(gid_smcp_A)), edges->end()); } @@ -881,8 +898,9 @@ TEST_F(DependencyGraphTest, StronglyConnectedComponents_NodeInclusionFilter) { }; auto sccs_or = graph.StronglyConnectedComponents( - {FontHelper::kCmap, FontHelper::kGSUB, FontHelper::kGlyf}, 0xFFFFFFFF, - &filter); + {DependencyGraph::kPhaseInit, FontHelper::kCmap, FontHelper::kGSUB, + FontHelper::kGlyf}, + 0xFFFFFFFF, &filter); ASSERT_TRUE(sccs_or.ok()) << sccs_or.status(); const std::vector topo_order = FlattenSccs(*sccs_or); @@ -933,6 +951,295 @@ TEST_F(DependencyGraphTest, CollectIncomingEdges_NodeInclusionFilter) { })); } +TEST_F(DependencyGraphTest, CollectIncomingEdges_UnicodePhases) { + Reconfigure(WithDefaultFeatures({}), { + /* 0 */ {{0xC1 /* Aacute */}}, + /* 1 */ {{'A'}}, + /* 2 */ {{0x301 /* acutecomb */}}, + /* 3 */ {{'('}}, + /* 4 */ {{')'}}, + }); + + glyph_id_t gid_A = *FontHelper::GetNominalGlyph(face.get(), 'A'); + + // Phase 0 (Init + Bidi): segment -> unicode/feature and bidi unicode -> + // unicode edges. + auto init_edges = graph.CollectIncomingEdges({DependencyGraph::kPhaseInit}, + 0xFFFFFFFF, 0xFFFFFFFF); + ASSERT_TRUE(init_edges.ok()) << init_edges.status(); + EXPECT_THAT(init_edges->at(Node::Unicode('(')), + UnorderedElementsAre(EdgeConditionsCnf{{Node::Segment(3)}}, + EdgeConditionsCnf{{Node::Unicode(')')}})); + EXPECT_THAT(init_edges->at(Node::Unicode(')')), + UnorderedElementsAre(EdgeConditionsCnf{{Node::Segment(4)}}, + EdgeConditionsCnf{{Node::Unicode('(')}})); + EXPECT_EQ(init_edges->at(Node::Unicode('A')), + (std::vector{{{Node::Segment(1)}}})); + EXPECT_EQ(init_edges->at(Node::Unicode(0xC1)), + (std::vector{{{Node::Segment(0)}}})); + EXPECT_EQ(init_edges->find(Node::Glyph(gid_A)), init_edges->end()); + + // Phase 2 (Decomposition): only unicode -> decomposed unicode edges. + auto decomp_edges = graph.CollectIncomingEdges( + {DependencyGraph::kPhaseDecomposition}, 0xFFFFFFFF, 0xFFFFFFFF); + ASSERT_TRUE(decomp_edges.ok()) << decomp_edges.status(); + EXPECT_EQ(decomp_edges->at(Node::Unicode('A')), + (std::vector{{{Node::Unicode(0xC1)}}})); + EXPECT_EQ(decomp_edges->at(Node::Unicode(0x301)), + (std::vector{{{Node::Unicode(0xC1)}}})); + EXPECT_EQ(decomp_edges->find(Node::Unicode(0xC1)), decomp_edges->end()); + EXPECT_EQ(decomp_edges->find(Node::Unicode(')')), decomp_edges->end()); + EXPECT_EQ(decomp_edges->find(Node::Glyph(gid_A)), decomp_edges->end()); + + // Phase 3 (Composition): only unicode -> composed unicode conjunctive edges. + auto comp_edges = graph.CollectIncomingEdges( + {DependencyGraph::kPhaseComposition}, 0xFFFFFFFF, 0xFFFFFFFF); + ASSERT_TRUE(comp_edges.ok()) << comp_edges.status(); + EXPECT_EQ(comp_edges->at(Node::Unicode(0xC1)), + (std::vector{ + {{Node::Unicode('A')}, {Node::Unicode(0x301)}}})); + EXPECT_EQ(comp_edges->find(Node::Unicode('A')), comp_edges->end()); + EXPECT_EQ(comp_edges->find(Node::Unicode(')')), comp_edges->end()); + EXPECT_EQ(comp_edges->find(Node::Glyph(gid_A)), comp_edges->end()); + + // Phase 4 (cmap): only nominal unicode -> glyph edges. + auto cmap_edges = + graph.CollectIncomingEdges({FontHelper::kCmap}, 0xFFFFFFFF, 0xFFFFFFFF); + ASSERT_TRUE(cmap_edges.ok()) << cmap_edges.status(); + EXPECT_EQ(cmap_edges->at(Node::Glyph(gid_A)), + (std::vector{{{Node::Unicode('A')}}})); + EXPECT_EQ(cmap_edges->find(Node::Unicode('A')), cmap_edges->end()); + EXPECT_EQ(cmap_edges->find(Node::Unicode(')')), cmap_edges->end()); + + // Phase 1 (Variation Selectors) with NotoSansJP: only UVS -> glyph edges. + Reconfigure(noto_sans_jp.get(), WithDefaultFeatures({}), + { + /* 0 */ {{0x4FAE}}, + /* 1 */ {{0xFE00}}, + /* 2 */ {{0xFA30}}, + }); + glyph_id_t gid_4fae = *FontHelper::GetNominalGlyph(noto_sans_jp.get(), 0x4FAE); + glyph_id_t gid_fa30 = *FontHelper::GetNominalGlyph(noto_sans_jp.get(), 0xFA30); + + auto uvs_edges = graph.CollectIncomingEdges( + {DependencyGraph::kPhaseVariationSelectors}, 0xFFFFFFFF, 0xFFFFFFFF); + ASSERT_TRUE(uvs_edges.ok()) << uvs_edges.status(); + EXPECT_THAT( + uvs_edges->at(Node::Glyph(gid_fa30)), + Contains(EdgeConditionsCnf{ + {Node::Unicode(0x4FAE)}, {Node::Unicode(0xFE00)}})); + EXPECT_EQ(uvs_edges->find(Node::Glyph(gid_4fae)), uvs_edges->end()); + + auto jp_cmap_edges = + graph.CollectIncomingEdges({FontHelper::kCmap}, 0xFFFFFFFF, 0xFFFFFFFF); + ASSERT_TRUE(jp_cmap_edges.ok()) << jp_cmap_edges.status(); + EXPECT_EQ(jp_cmap_edges->at(Node::Glyph(gid_4fae)), + (std::vector{{{Node::Unicode(0x4FAE)}}})); + EXPECT_EQ(jp_cmap_edges->at(Node::Glyph(gid_fa30)), + (std::vector{{{Node::Unicode(0xFA30)}}})); +} + +TEST_F(DependencyGraphTest, + StronglyConnectedComponents_NoCycleBetweenCompAndDecomp) { + Reconfigure(WithDefaultFeatures({}), { + {{0xC1 /* Aacute */}}, + {{'A'}}, + {{0x301 /* acutecomb */}}, + {{'('}}, + {{')'}}, + }); + + // In Decomposition alone, there are no cycles and Aacute precedes A and 0x301. + auto decomp_sccs = graph.StronglyConnectedComponents( + {DependencyGraph::kPhaseDecomposition}, Node::NodeType::UNICODE); + ASSERT_TRUE(decomp_sccs.ok()) << decomp_sccs.status(); + for (const auto& scc : *decomp_sccs) { + EXPECT_EQ(scc.size(), 1); + } + const std::vector decomp_order = FlattenSccs(*decomp_sccs); + EXPECT_LT(GetIndex(decomp_order, Node::Unicode(0xC1)), + GetIndex(decomp_order, Node::Unicode('A'))); + EXPECT_LT(GetIndex(decomp_order, Node::Unicode(0xC1)), + GetIndex(decomp_order, Node::Unicode(0x301))); + + // In Composition alone, there are no cycles and A and 0x301 precede Aacute. + auto comp_sccs = graph.StronglyConnectedComponents( + {DependencyGraph::kPhaseComposition}, Node::NodeType::UNICODE); + ASSERT_TRUE(comp_sccs.ok()) << comp_sccs.status(); + for (const auto& scc : *comp_sccs) { + EXPECT_EQ(scc.size(), 1); + } + const std::vector comp_order = FlattenSccs(*comp_sccs); + EXPECT_LT(GetIndex(comp_order, Node::Unicode('A')), + GetIndex(comp_order, Node::Unicode(0xC1))); + EXPECT_LT(GetIndex(comp_order, Node::Unicode(0x301)), + GetIndex(comp_order, Node::Unicode(0xC1))); + + // Init + Bidi has its own symmetric 2-node SCC between '(' and ')'. + auto init_sccs = graph.StronglyConnectedComponents( + {DependencyGraph::kPhaseInit}, + Node::NodeType::SEGMENT | Node::NodeType::UNICODE); + ASSERT_TRUE(init_sccs.ok()) << init_sccs.status(); + uint32_t bidi_cycles = 0; + for (const auto& scc : *init_sccs) { + if (scc.size() > 1) { + EXPECT_THAT(scc, UnorderedElementsAre(Node::Unicode('('), + Node::Unicode(')'))); + bidi_cycles++; + } + } + EXPECT_EQ(bidi_cycles, 1); + const std::vector init_order = FlattenSccs(*init_sccs); + EXPECT_LT(GetIndex(init_order, Node::Segment(3)), + GetIndex(init_order, Node::Unicode('('))); + EXPECT_LT(GetIndex(init_order, Node::Segment(4)), + GetIndex(init_order, Node::Unicode(')'))); + + // If Decomposition and Composition were combined, they would form a cycle. + auto combined_sccs = graph.StronglyConnectedComponents( + {DependencyGraph::kPhaseDecomposition, + DependencyGraph::kPhaseComposition}, + Node::NodeType::UNICODE); + ASSERT_TRUE(combined_sccs.ok()) << combined_sccs.status(); + uint32_t combined_cycles = 0; + for (const auto& scc : *combined_sccs) { + if (scc.size() > 1) { + EXPECT_THAT(scc, UnorderedElementsAre(Node::Unicode(0xC1), + Node::Unicode('A'), + Node::Unicode(0x301))); + combined_cycles++; + } + } + EXPECT_EQ(combined_cycles, 1); +} + +TEST_F(DependencyGraphTest, ClosureTraversal_DecompBeforeComp) { + Reconfigure(WithDefaultFeatures({}), { + {{0xE1 /* aacute */}}, + {{'e'}}, + {{'a'}}, + {{0x301 /* acutecomb */}}, + {{0xE9 /* eacute */}}, + }); + + auto r = graph.ClosureTraversal({Node::Unicode(0xE1), Node::Unicode('e')}); + ASSERT_TRUE(r.ok()) << r.status(); + EXPECT_EQ(r->ReachedCodepoints(), + (CodepointSet{0xE1, 'e', 'a', 0x301, 0xE9})); + + glyph_id_t gid_eacute = *FontHelper::GetNominalGlyph(face.get(), 0xE9); + EXPECT_TRUE(r->ReachedGlyphs().contains(gid_eacute)); + EXPECT_TRUE(r->TraversedTables().contains(FontHelper::kCmap)); + EXPECT_FALSE( + r->TraversedTables().contains(DependencyGraph::kPhaseDecomposition)); + EXPECT_FALSE( + r->TraversedTables().contains(DependencyGraph::kPhaseComposition)); +} + +TEST_F(DependencyGraphTest, ClosureTraversal_BidiBeforeDecomposition) { + // Keep 0x2329, 0x3008, 0x3009 while dropping 0x232A (bidi mirror of 0x2329). + // 0x2329 canonically decomposes to 0x3008, and 0x3008 <-> 0x3009 are bidi + // mirrors. + hb_subset_input_t* input = hb_subset_input_create_or_fail(); + ASSERT_NE(input, nullptr); + hb_subset_input_set_flags(input, HB_SUBSET_FLAGS_NO_BIDI_CLOSURE); + hb_set_t* unicodes = hb_subset_input_unicode_set(input); + hb_set_add(unicodes, 0x2329); + hb_set_add(unicodes, 0x3008); + hb_set_add(unicodes, 0x3009); + hb_face_unique_ptr subset = + ift::common::make_hb_face(hb_subset_or_fail(noto_sans_jp.get(), input)); + hb_subset_input_destroy(input); + ASSERT_NE(subset.get(), nullptr); + ift::common::hb_blob_unique_ptr blob = + ift::common::make_hb_blob(hb_face_reference_blob(subset.get())); + hb_face_unique_ptr subset_face = + ift::common::make_hb_face(hb_face_create(blob.get(), 0)); + + CodepointSet subset_cps = FontHelper::ToCodepointsSet(subset_face.get()); + ASSERT_TRUE(subset_cps.contains(0x2329)); + ASSERT_TRUE(subset_cps.contains(0x3008)); + ASSERT_TRUE(subset_cps.contains(0x3009)); + ASSERT_FALSE(subset_cps.contains(0x232A)); + + Reconfigure(subset_face.get(), WithDefaultFeatures({}), + { + {{0x2329}}, + {{0x3008}}, + {{0x3009}}, + }); + + // Starting from 0x2329: Bidi runs before Decomposition, so 0x3008 (reached + // only via Decomposition of 0x2329) must NOT trigger Bidi mirroring to + // 0x3009. + auto r = graph.ClosureTraversal({Node::Unicode(0x2329)}); + ASSERT_TRUE(r.ok()) << r.status(); + EXPECT_EQ(r->ReachedCodepoints(), (CodepointSet{0x2329, 0x3008})); + + // Starting from 0x3009: Bidi mirrors 0x3009 -> 0x3008. + r = graph.ClosureTraversal({Node::Unicode(0x3009)}); + ASSERT_TRUE(r.ok()) << r.status(); + EXPECT_EQ(r->ReachedCodepoints(), (CodepointSet{0x3009, 0x3008})); +} + +TEST_F(DependencyGraphTest, ClosureTraversal_UvsBeforeDecomposition) { + // 0xF901 canonically decomposes to 0x66F4, and (0x66F4, 0xE0101) maps via UVS + // to glyph 16687 (while nominal glyph for both 0xF901 and 0x66F4 is 6563). + Reconfigure(noto_sans_jp.get(), WithDefaultFeatures({}), + { + /* 0 */ {{0xF901}}, + /* 1 */ {{0xE0101}}, + /* 2 */ {{0x66F4}}, + }); + + // Starting from 0xF901 + 0xE0101: Phase 2 (Variation Selectors) runs before + // Phase 3 (Decomposition), so 0x66F4 (reached via Decomposition) must NOT + // activate UVS glyph 16687. + auto r = graph.ClosureTraversal({Node::Segment(0), Node::Segment(1)}); + ASSERT_TRUE(r.ok()) << r.status(); + EXPECT_TRUE(r->ReachedCodepoints().contains(0x66F4)); + EXPECT_TRUE(r->ReachedGlyphs().contains(6563)); + EXPECT_FALSE(r->ReachedGlyphs().contains(16687)); + + // Starting from 0x66F4 + 0xE0101 directly DOES reach UVS glyph 16687. + r = graph.ClosureTraversal({Node::Segment(2), Node::Segment(1)}); + ASSERT_TRUE(r.ok()) << r.status(); + EXPECT_TRUE(r->ReachedGlyphs().contains(16687)); +} + +TEST_F(DependencyGraphTest, ClosureTraversal_UvsPhaseAndPendingPruning) { + Reconfigure(noto_sans_jp.get(), WithDefaultFeatures({}), + { + /* 0 */ {{0x4FAE, 0xFA30}}, + /* 1 */ {{0xFE00}}, + }); + + glyph_id_t gid_fa30 = *FontHelper::GetNominalGlyph(noto_sans_jp.get(), 0xFA30); + + // Segment 0 reaches gid_fa30 via cmap (Phase 5) even though Phase 2 (UVS) + // leaves a pending edge (0x4FAE, 0xFE00) -> gid_fa30. That pending edge must + // be pruned because gid_fa30 was reached in Phase 5. + auto r = graph.ClosureTraversal({Node::Segment(0)}); + ASSERT_TRUE(r.ok()) << r.status(); + EXPECT_TRUE(r->ReachedGlyphs().contains(gid_fa30)); + EXPECT_FALSE(r->HasPendingEdges()); + + // When both base and variation selector are reached, UVS (Phase 2) reaches + // gid_fa30 directly without adding codepoint 0xFA30, and records kCmap table. + Reconfigure(noto_sans_jp.get(), WithDefaultFeatures({}), + { + /* 0 */ {{0x4FAE}}, + /* 1 */ {{0xFE00}}, + }); + r = graph.ClosureTraversal({Node::Segment(0), Node::Segment(1)}); + ASSERT_TRUE(r.ok()) << r.status(); + EXPECT_TRUE(r->ReachedGlyphs().contains(gid_fa30)); + EXPECT_FALSE(r->ReachedCodepoints().contains(0xFA30)); + EXPECT_TRUE(r->TraversedTables().contains(FontHelper::kCmap)); + EXPECT_FALSE(r->TraversedTables().contains( + DependencyGraph::kPhaseVariationSelectors)); +} + // TODO(garretrieger): // - basic math, CFF, and COLR tests. diff --git a/ift/dep_graph/pending_edge.h b/ift/dep_graph/pending_edge.h index 084f8f52..f930f4cb 100644 --- a/ift/dep_graph/pending_edge.h +++ b/ift/dep_graph/pending_edge.h @@ -19,17 +19,17 @@ struct PendingEdge { } static PendingEdge Uvs(hb_codepoint_t a, hb_codepoint_t b, - encoder::glyph_id_t gid) { - PendingEdge edge(Node::Unicode(a), Node::Glyph(gid), - HB_TAG('c', 'm', 'a', 'p')); + encoder::glyph_id_t gid, hb_tag_t table_tag) { + PendingEdge edge(Node::Unicode(a), Node::Glyph(gid), table_tag); edge.required_codepoints = std::make_pair(a, b); return edge; } static PendingEdge UnicodeComposition(hb_codepoint_t a, hb_codepoint_t b, - hb_codepoint_t dest_unicode) { + hb_codepoint_t dest_unicode, + hb_tag_t table_tag) { PendingEdge edge(Node::Unicode(a), Node::Unicode(dest_unicode), - HB_TAG('c', 'm', 'a', 'p')); + table_tag); edge.required_codepoints = std::make_pair(a, b); return edge; } diff --git a/ift/dep_graph/traversal.h b/ift/dep_graph/traversal.h index 52fd4af9..b8cc9b5c 100644 --- a/ift/dep_graph/traversal.h +++ b/ift/dep_graph/traversal.h @@ -1,6 +1,8 @@ #ifndef IFT_DEP_GRAPH_TRAVERSAL_H_ #define IFT_DEP_GRAPH_TRAVERSAL_H_ +#include + #include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_set.h" #include "ift/common/int_set.h" @@ -76,6 +78,21 @@ class Traversal { void AddPending(PendingEdge edge) { pending_edges_.push_back(edge); } + void PruneReachedPendingEdges() { + std::erase_if(pending_edges_, [this](const PendingEdge& edge) { + if (edge.dest.IsGlyph()) { + return reached_glyphs_.contains(edge.dest.Id()); + } + if (edge.dest.IsUnicode()) { + return reached_codepoints_.contains(edge.dest.Id()); + } + if (edge.dest.IsFeature()) { + return reached_feature_tags_.contains(edge.dest.Id()); + } + return false; + }); + } + bool HasPendingEdges() const { return !pending_edges_.empty(); } const std::vector& PendingEdges() const { diff --git a/ift/encoder/dependency_closure.cc b/ift/encoder/dependency_closure.cc index 18fdf65f..010aa2ca 100644 --- a/ift/encoder/dependency_closure.cc +++ b/ift/encoder/dependency_closure.cc @@ -528,6 +528,7 @@ Status DependencyClosure::PropagateConditions( ActivationCondition::Or(it_existing->second, *complete_condition); if (combined != it_existing->second) { it_existing->second = std::move(combined); + modified.insert(n); changed = true; } } @@ -611,19 +612,6 @@ StatusOr> DependencyClosure::InitFontNodes( return init_font_nodes; } -static IntSet ActiveClosurePhases(hb_face_t* face) { - IntSet out; - flat_hash_set table_tags = FontHelper::GetTags(face); - for (uint32_t phase = 0; phase < DependencyGraph::kNumberOfClosurePhases; - phase++) { - hb_tag_t table = DependencyGraph::kClosurePhaseTable[phase]; - if (table_tags.contains(table)) { - out.insert(phase); - } - } - return out; -} - StatusOr>&> DependencyClosure::IncomingEdgesForClosurePhase( uint32_t phase, bool full_recalc, const flat_hash_set* node_filter) { @@ -641,13 +629,13 @@ DependencyClosure::IncomingEdgesForClosurePhase( } else if (source_type_filter & Node::SEGMENT) { // Init font changes change the outgoing links on SEGMENT and/or INIT_FONT // nodes so if a phase includes these then a partial update is needed. Only - // needs to consider link types in the cmap phase (segment/init font -> - // unicode/feature, unicode -> unicode, unicode -> glyph) + // needs to consider link types in the Init phase (segment/init font -> + // unicode/feature, and bidi unicode -> unicode). update_type = Partial; source_type_filter = (Node::SEGMENT | Node::UNICODE | Node::INIT_FONT) & source_type_filter; dest_type_filter = - (Node::UNICODE | Node::FEATURE | Node::GLYPH) & dest_type_filter; + (Node::UNICODE | Node::FEATURE) & dest_type_filter; } if (update_type == None) { @@ -660,6 +648,11 @@ DependencyClosure::IncomingEdgesForClosurePhase( if (update_type == Full) { prev_incoming_edges = std::move(new_incoming_edges); } else { + if (node_filter != nullptr) { + for (const auto& n : *node_filter) { + prev_incoming_edges->erase(n); + } + } for (const auto& [n, edges] : new_incoming_edges) { prev_incoming_edges->insert_or_assign(n, std::move(edges)); } @@ -678,7 +671,7 @@ Status DependencyClosure::UpdateAllNodeConditions( *last_seen_full_closure_size_ != segmentation_info_->FullClosure().size(); last_seen_full_closure_size_ = segmentation_info_->FullClosure().size(); - IntSet phases = ActiveClosurePhases(original_face_.get()); + IntSet phases = graph_.ActiveClosurePhases(); flat_hash_set new_init_nodes; init_font_nodes_ = TRY(InitFontNodes(init_font_nodes_, new_init_nodes)); diff --git a/ift/encoder/dependency_closure.h b/ift/encoder/dependency_closure.h index 1cfe27c7..05603d91 100644 --- a/ift/encoder/dependency_closure.h +++ b/ift/encoder/dependency_closure.h @@ -235,7 +235,7 @@ class DependencyClosure { phase_node_condition_cache_ [dep_graph::DependencyGraph::kNumberOfClosurePhases]; - // Cache for CollectIncomingEdges results for non-cmap phases. + // Cache for CollectIncomingEdges results for non-Init phases. std::optional last_seen_full_closure_size_ = std::nullopt; absl::flat_hash_map< uint32_t, diff --git a/ift/encoder/dependency_closure_test.cc b/ift/encoder/dependency_closure_test.cc index 30476d15..cc32b8a9 100644 --- a/ift/encoder/dependency_closure_test.cc +++ b/ift/encoder/dependency_closure_test.cc @@ -479,6 +479,36 @@ TEST_F(DependencyClosureTest, ExtractAllGlyphConditions_BidiCycle) { EXPECT_EQ(conditions.at(13).ToString(), "if ((s0 OR s1)) then p0"); } +TEST_F(DependencyClosureTest, ExtractAllGlyphConditions_UnicodePhasesOrdering) { + Reconfigure(face.get(), WithDefaultFeatures(), + { + /* 0 */ {{0xC1 /* Aacute */}}, + /* 1 */ {{'A'}}, + /* 2 */ {{0x301 /* acutecomb */}}, + }); + + glyph_id_t gid_A = *FontHelper::GetNominalGlyph(face.get(), 'A'); + glyph_id_t gid_Aacute = *FontHelper::GetNominalGlyph(face.get(), 0xC1); + + auto conditions = dependency_closure->AllGlyphConditions(); + EXPECT_EQ(conditions.at(gid_A).ToString(), "if ((s0 OR s1)) then p0"); + EXPECT_EQ(conditions.at(gid_Aacute).ToString(), + "if ((s0 OR s1) AND (s0 OR s2)) then p0"); + + // 0xF901 decomposes to 0x66F4, and (0x66F4, 0xE0101) maps via UVS to glyph + // 16687. Because Variation Selectors precede Decomposition, s0 (0xF901) does + // not trigger glyph 16687. + Reconfigure(noto_sans_jp.get(), WithDefaultFeatures(), + { + /* 0 */ {{0xF901}}, + /* 1 */ {{0xE0101}}, + /* 2 */ {{0x66F4}}, + }); + + conditions = dependency_closure->AllGlyphConditions(); + EXPECT_EQ(conditions.at(16687).ToString(), "if (s1 AND s2) then p0"); +} + TEST_F(DependencyClosureTest, Exclusive) { Status s = CompareAnalysis({0}); ASSERT_TRUE(s.ok()) << s; @@ -1263,6 +1293,34 @@ TEST_F(DependencyClosureTest, InitFontChanged_Caching) { ASSERT_EQ(conditions3.at(69 /* a */).ToString(), "if (s0) then p0"); } +TEST_F(DependencyClosureTest, InitFontChanged_PreservesBidiEdges) { + Reconfigure(WithDefaultFeatures(), + { + /* 0 */ {{'a'}}, + /* 1 */ {{'('}}, + /* 2 */ {{')'}}, + }); + + auto conditions1 = dependency_closure->AllGlyphConditions(); + ASSERT_EQ(conditions1.at(69 /* a */).ToString(), "if (s0) then p0"); + ASSERT_EQ(conditions1.at(12 /* ( */).ToString(), "if ((s1 OR s2)) then p0"); + ASSERT_EQ(conditions1.at(13 /* ) */).ToString(), "if ((s1 OR s2)) then p0"); + + // Change init font to include 'a' (partial update on Init phase must + // preserve bidi edges between '(' and ')'). + Status s = segmentation_info->ReassignInitSubset(*closure_cache, + WithDefaultFeatures({'a'})); + ASSERT_TRUE(s.ok()) << s; + + s = dependency_closure->InitFontChanged(SegmentSet::all()); + ASSERT_TRUE(s.ok()) << s; + + auto conditions2 = dependency_closure->AllGlyphConditions(); + ASSERT_FALSE(conditions2.contains(69 /* a */)); + EXPECT_EQ(conditions2.at(12 /* ( */).ToString(), "if ((s1 OR s2)) then p0"); + EXPECT_EQ(conditions2.at(13 /* ) */).ToString(), "if ((s1 OR s2)) then p0"); +} + } // namespace ift::encoder // TODO(garretrieger): missing tests From c69559fa21fc367670171cb8d060a16000a086e0 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Fri, 13 Nov 2026 18:32:30 +0000 Subject: [PATCH 2/2] Update GlyphClosureCache to match unicode closure phasing in dep graph. --- ift/dep_graph/unicode_edges.cc | 1 + ift/dep_graph/unicode_edges.h | 1 + ift/encoder/BUILD | 2 + ift/encoder/glyph_closure_cache.cc | 119 ++++++++++++++++++++---- ift/encoder/glyph_closure_cache.h | 4 +- ift/encoder/glyph_closure_cache_test.cc | 89 ++++++++++++++++++ 6 files changed, 194 insertions(+), 22 deletions(-) diff --git a/ift/dep_graph/unicode_edges.cc b/ift/dep_graph/unicode_edges.cc index 3f070787..7406038c 100644 --- a/ift/dep_graph/unicode_edges.cc +++ b/ift/dep_graph/unicode_edges.cc @@ -196,6 +196,7 @@ void UnicodeEdges::ComputeUVSEdges( hb_set_unique_ptr vs_unicodes_hb = make_hb_set(); hb_face_collect_variation_selectors(face, vs_unicodes_hb.get()); CodepointSet vs_unicodes(vs_unicodes_hb.get()); + result.vs_unicodes = vs_unicodes; hb_font_unique_ptr font = make_hb_font(hb_font_create(face)); diff --git a/ift/dep_graph/unicode_edges.h b/ift/dep_graph/unicode_edges.h index bc90145c..9a499de3 100644 --- a/ift/dep_graph/unicode_edges.h +++ b/ift/dep_graph/unicode_edges.h @@ -36,6 +36,7 @@ struct UnicodeEdges { absl::flat_hash_map gid_to_unicode; absl::flat_hash_map gid_to_vs; + ift::common::CodepointSet vs_unicodes; static absl::StatusOr ComputeUnicodeDependencyEdges( hb_face_t* face, const ift::common::DataFileResolver& resolver); diff --git a/ift/encoder/BUILD b/ift/encoder/BUILD index 4698b15f..6dc75fc7 100644 --- a/ift/encoder/BUILD +++ b/ift/encoder/BUILD @@ -163,6 +163,7 @@ cc_library( "//ift/feature_registry", "@abseil-cpp//absl/container:flat_hash_map", "@abseil-cpp//absl/status", + "@harfbuzz", ], ) @@ -452,6 +453,7 @@ cc_test( "//ift/freq:common", "@googletest//:gtest", "@googletest//:gtest_main", + "@harfbuzz", ], ) diff --git a/ift/encoder/glyph_closure_cache.cc b/ift/encoder/glyph_closure_cache.cc index 1c33683e..2a3b52a4 100644 --- a/ift/encoder/glyph_closure_cache.cc +++ b/ift/encoder/glyph_closure_cache.cc @@ -1,5 +1,7 @@ #include "ift/encoder/glyph_closure_cache.h" +#include "hb-subset.h" +#include "hb.h" #include "ift/common/font_data.h" #include "ift/common/font_helper.h" #include "ift/common/hb_set_unique_ptr.h" @@ -18,20 +20,43 @@ using ift::common::CodepointSet; using ift::common::DataFileResolver; using ift::common::FontHelper; using ift::common::GlyphSet; +using ift::common::hb_blob_unique_ptr; using ift::common::hb_face_unique_ptr; using ift::common::hb_set_unique_ptr; +using ift::common::make_hb_blob; using ift::common::make_hb_face; +using ift::common::make_hb_face_builder; using ift::common::make_hb_set; using ift::common::SegmentSet; using ift::dep_graph::UnicodeEdges; namespace ift::encoder { +namespace { + +hb_face_unique_ptr RemoveCmapTable(hb_face_t* face) { + hb_face_unique_ptr builder = make_hb_face_builder(); + for (hb_tag_t tag : FontHelper::GetTags(face)) { + if (tag == FontHelper::kCmap) { + continue; + } + hb_blob_unique_ptr table_blob = + make_hb_blob(hb_face_reference_table(face, tag)); + hb_face_builder_add_table(builder.get(), tag, table_blob.get()); + } + return builder; +} + +} // namespace + StatusOr> GlyphClosureCache::Create( hb_face_t* face, const DataFileResolver& resolver) { - auto preprocessed_face = make_hb_face(hb_subset_preprocess(face)); + auto preprocessed_with_cmap = make_hb_face(hb_subset_preprocess(face)); auto unicode_edges = TRY(UnicodeEdges::ComputeUnicodeDependencyEdges( - preprocessed_face.get(), resolver)); + preprocessed_with_cmap.get(), resolver)); + auto without_cmap = RemoveCmapTable(preprocessed_with_cmap.get()); + auto preprocessed_face = + make_hb_face(hb_subset_preprocess(without_cmap.get())); return std::unique_ptr(new GlyphClosureCache( face, std::move(preprocessed_face), std::move(unicode_edges))); } @@ -96,33 +121,63 @@ StatusOr GlyphClosureCache::HasAdditionalConditions( return closure_glyphs.intersects(glyphs); } -CodepointSet GlyphClosureCache::UnicodeClosure( - const CodepointSet& unicodes) const { - CodepointSet queue = unicodes; - CodepointSet visited = unicodes; +CodepointSet GlyphClosureCache::UnicodeClosure(const CodepointSet& unicodes, + GlyphSet& uvs_glyphs) const { + // Follow DependencyGraph's ordered unicode closure phases: + // 1. Init + Bidi mirroring + CodepointSet closed = unicodes; + hb_unicode_funcs_t* unicode_funcs = hb_unicode_funcs_get_default(); + for (hb_codepoint_t u : unicodes) { + hb_codepoint_t mirror = hb_unicode_mirroring(unicode_funcs, u); + if (mirror != u && unicode_edges_.unicode_to_gid.contains(mirror)) { + closed.insert(mirror); + } + } - while (!queue.empty()) { - hb_codepoint_t u = *queue.min(); - queue.erase(u); + // 2. Variation selectors + CodepointSet active_vs = closed; + active_vs.intersect(unicode_edges_.vs_unicodes); + for (hb_codepoint_t vs : active_vs) { + auto it = unicode_edges_.variation_selector.find(vs); + if (it == unicode_edges_.variation_selector.end()) { + continue; + } + for (const auto& edge : it->second) { + if (closed.contains(edge.unicode)) { + uvs_glyphs.insert(edge.gid); + } + } + } + + // 3. Unicode canonical decomposition + CodepointSet decomp_queue = closed; + while (!decomp_queue.empty()) { + hb_codepoint_t u = *decomp_queue.min(); + decomp_queue.erase(u); - // outgoing decomp edges auto decomp = unicode_edges_.decomposition.find(u); if (decomp != unicode_edges_.decomposition.end()) { CodepointSet edges = decomp->second; - edges.subtract(visited); - queue.union_set(edges); - visited.union_set(edges); + edges.subtract(closed); + decomp_queue.union_set(edges); + closed.union_set(edges); } + } + + // 4. Unicode canonical composition + CodepointSet comp_queue = closed; + while (!comp_queue.empty()) { + hb_codepoint_t u = *comp_queue.min(); + comp_queue.erase(u); - // outgoing comp edges auto comp = unicode_edges_.composition.find(u); if (comp != unicode_edges_.composition.end()) { for (const auto& edge : comp->second) { - if (!visited.contains(edge.dest) && - visited.contains(edge.other_source)) { + if (!closed.contains(edge.dest) && + closed.contains(edge.other_source)) { // edge requirements are satisfied, we can follow it. - queue.insert(edge.dest); - visited.insert(edge.dest); + comp_queue.insert(edge.dest); + closed.insert(edge.dest); } // Note: if edge requirements are not satisfied, then this edge will be // rechecked later on if edge.other_source is visited, since edges for a @@ -131,7 +186,7 @@ CodepointSet GlyphClosureCache::UnicodeClosure( } } - return visited; + return closed; } StatusOr GlyphClosureCache::GlyphClosure( @@ -149,9 +204,33 @@ StatusOr GlyphClosureCache::GlyphClosure( return absl::InternalError("Closure subset configuration failed."); } + // Harfbuzz subsetter currently has only limited support for doing + // unicode closure (only bidi is supported), so we need to manually + // do the closure ourselves. In order to avoid subsetter from + // incorrectly mapping uvs glyphs back to nominal codepoints we + // manually do the cmap unicode -> glyph mapping and disable cmap + // in the harfbuzz subsetting operation. + // + // This "hack" can be removed later on if harfbuzz gains full support + // for unicode closure over unicode composition and decomposition. + GlyphSet uvs_glyphs; + CodepointSet closed_unicodes = UnicodeClosure(segment.codepoints, uvs_glyphs); + + GlyphSet initial_gids = std::move(uvs_glyphs); + for (hb_codepoint_t u : closed_unicodes) { + auto cmap_it = unicode_edges_.unicode_to_gid.find(u); + if (cmap_it != unicode_edges_.unicode_to_gid.end()) { + initial_gids.insert(cmap_it->second); + } + } + SubsetDefinition expanded_segment = segment; - expanded_segment.codepoints = UnicodeClosure(segment.codepoints); + expanded_segment.codepoints.clear(); + expanded_segment.gids.union_set(initial_gids); + hb_subset_input_set_flags( + // BIDI closure is disabled since we manually performed it already. + input, hb_subset_input_get_flags(input) | HB_SUBSET_FLAGS_NO_BIDI_CLOSURE); expanded_segment.ConfigureInput(input, preprocessed_face_.get()); hb_subset_plan_t* plan = hb_subset_plan_create_or_fail(preprocessed_face_.get(), input); diff --git a/ift/encoder/glyph_closure_cache.h b/ift/encoder/glyph_closure_cache.h index 070aefa8..372f97cf 100644 --- a/ift/encoder/glyph_closure_cache.h +++ b/ift/encoder/glyph_closure_cache.h @@ -67,8 +67,8 @@ class GlyphClosureCache { hb_face_t* Face() { return preprocessed_face_.get(); } private: - common::CodepointSet UnicodeClosure( - const common::CodepointSet& unicodes) const; + common::CodepointSet UnicodeClosure(const common::CodepointSet& unicodes, + common::GlyphSet& uvs_glyphs) const; ift::common::hb_face_unique_ptr original_face_; ift::common::hb_face_unique_ptr preprocessed_face_; diff --git a/ift/encoder/glyph_closure_cache_test.cc b/ift/encoder/glyph_closure_cache_test.cc index 93b83a26..4ab3afa7 100644 --- a/ift/encoder/glyph_closure_cache_test.cc +++ b/ift/encoder/glyph_closure_cache_test.cc @@ -3,6 +3,8 @@ #include #include "gtest/gtest.h" +#include "hb-subset.h" +#include "hb.h" #include "ift/common/bazel_data_file_resolver.h" #include "ift/common/font_data.h" #include "ift/common/font_helper.h" @@ -266,4 +268,91 @@ TEST_F(GlyphClosureCacheTest, CodepointsForGlyphs) { } } +TEST_F(GlyphClosureCacheTest, GlyphClosure_DecompBeforeComp) { + auto cache = GlyphClosureCache::Create(roboto.get(), *resolver); + ASSERT_TRUE(cache.ok()) << cache.status(); + + auto gids = (*cache)->GlyphClosure({0xE1 /* aacute */, 'e'}); + ASSERT_TRUE(gids.ok()) << gids.status(); + + auto gid_eacute = FontHelper::GetNominalGlyph(roboto.get(), 0xE9); + ASSERT_TRUE(gid_eacute.ok()) << gid_eacute.status(); + EXPECT_TRUE(gids->contains(*gid_eacute)); +} + +TEST_F(GlyphClosureCacheTest, GlyphClosure_BidiBeforeDecomposition) { + hb_face_unique_ptr noto_sans_jp = + from_file("ift/common/testdata/NotoSansJP-Regular.ttf"); + + // Keep 0x2329, 0x3008, 0x3009 while dropping 0x232A (bidi mirror of 0x2329). + // 0x2329 canonically decomposes to 0x3008, and 0x3008 <-> 0x3009 are bidi + // mirrors. + hb_subset_input_t* input = hb_subset_input_create_or_fail(); + ASSERT_NE(input, nullptr); + hb_subset_input_set_flags(input, HB_SUBSET_FLAGS_NO_BIDI_CLOSURE); + hb_set_t* unicodes = hb_subset_input_unicode_set(input); + hb_set_add(unicodes, 0x2329); + hb_set_add(unicodes, 0x3008); + hb_set_add(unicodes, 0x3009); + hb_face_unique_ptr subset = + ift::common::make_hb_face(hb_subset_or_fail(noto_sans_jp.get(), input)); + hb_subset_input_destroy(input); + ASSERT_NE(subset.get(), nullptr); + ift::common::hb_blob_unique_ptr blob = + ift::common::make_hb_blob(hb_face_reference_blob(subset.get())); + hb_face_unique_ptr subset_face = + ift::common::make_hb_face(hb_face_create(blob.get(), 0)); + + CodepointSet subset_cps = FontHelper::ToCodepointsSet(subset_face.get()); + ASSERT_TRUE(subset_cps.contains(0x2329)); + ASSERT_TRUE(subset_cps.contains(0x3008)); + ASSERT_TRUE(subset_cps.contains(0x3009)); + ASSERT_FALSE(subset_cps.contains(0x232A)); + + auto cache = GlyphClosureCache::Create(subset_face.get(), *resolver); + ASSERT_TRUE(cache.ok()) << cache.status(); + + uint32_t gid_2329 = + *FontHelper::GetNominalGlyph(subset_face.get(), 0x2329); + uint32_t gid_3008 = + *FontHelper::GetNominalGlyph(subset_face.get(), 0x3008); + uint32_t gid_3009 = + *FontHelper::GetNominalGlyph(subset_face.get(), 0x3009); + + // Starting from 0x2329: Bidi runs before Decomposition, so 0x3008 (reached + // only via Decomposition of 0x2329) must NOT trigger Bidi mirroring to + // 0x3009. + auto gids = (*cache)->GlyphClosure({0x2329}); + ASSERT_TRUE(gids.ok()) << gids.status(); + EXPECT_TRUE(gids->contains(gid_2329)); + EXPECT_TRUE(gids->contains(gid_3008)); + EXPECT_FALSE(gids->contains(gid_3009)); + + // Starting from 0x3009: Bidi mirrors 0x3009 -> 0x3008. + gids = (*cache)->GlyphClosure({0x3009}); + ASSERT_TRUE(gids.ok()) << gids.status(); + EXPECT_TRUE(gids->contains(gid_3009)); + EXPECT_TRUE(gids->contains(gid_3008)); +} + +TEST_F(GlyphClosureCacheTest, GlyphClosure_UvsBeforeDecomposition) { + hb_face_unique_ptr noto_sans_jp = + from_file("ift/common/testdata/NotoSansJP-Regular.ttf"); + + auto cache = GlyphClosureCache::Create(noto_sans_jp.get(), *resolver); + ASSERT_TRUE(cache.ok()) << cache.status(); + + // 0xF901 canonically decomposes to 0x66F4, and (0x66F4, 0xE0101) maps via UVS + // to glyph 16687 (while nominal glyph for both 0xF901 and 0x66F4 is 6563). + auto gids = (*cache)->GlyphClosure({0xF901, 0xE0101}); + ASSERT_TRUE(gids.ok()) << gids.status(); + EXPECT_TRUE(gids->contains(6563)); + EXPECT_FALSE(gids->contains(16687)); + + // Starting from 0x66F4 + 0xE0101 directly DOES reach UVS glyph 16687. + gids = (*cache)->GlyphClosure({0x66F4, 0xE0101}); + ASSERT_TRUE(gids.ok()) << gids.status(); + EXPECT_TRUE(gids->contains(16687)); +} + } // namespace ift::encoder \ No newline at end of file