diff --git a/ift/encoder/BUILD b/ift/encoder/BUILD index 0a1c1a03..ee2d58e4 100644 --- a/ift/encoder/BUILD +++ b/ift/encoder/BUILD @@ -130,6 +130,7 @@ cc_library( "@abseil-cpp//absl/status", "@abseil-cpp//absl/status:statusor", "@abseil-cpp//absl/strings", + "@abseil-cpp//absl/strings:str_format", "@harfbuzz", ], ) diff --git a/ift/encoder/closure_glyph_segmenter.cc b/ift/encoder/closure_glyph_segmenter.cc index 41fde2b3..e4c6ccc4 100644 --- a/ift/encoder/closure_glyph_segmenter.cc +++ b/ift/encoder/closure_glyph_segmenter.cc @@ -601,18 +601,9 @@ StatusOr> ToMergers( } static StatusOr ToFinalSegmentation( - SegmentationContext& context, Span mergers) { + SegmentationContext& context) { context.LogClosureStatistics(); - std::vector calculators; - for (const Merger& merger : mergers) { - if (!merger.Strategy().UseCosts()) { - continue; - } - for (const auto& profile : merger.Strategy().ProbabilityProfiles()) { - calculators.push_back(TRY(profile.Calculator())); - } - } - return context.ToGlyphSegmentation(calculators); + return context.ToGlyphSegmentation(); } Status ClosureGlyphSegmenter::ValidateInput( @@ -850,6 +841,19 @@ StatusOr ClosureGlyphSegmenter::CodepointToGlyphSegments( init_font_merging_brotli_quality_, resolver_, target_mode_)); if (target_mode_ == ift::config::UNICODE_RANGE) { + std::vector calculators; + for (const auto& [_, strategy] : merge_groups) { + if (!strategy.UseCosts()) { + continue; + } + for (const auto& profile : strategy.ProbabilityProfiles()) { + calculators.push_back(TRY(profile.Calculator())); + } + } + // Initial glyph probabilities are used at the end of segmentation to + // report probabilities of occurence for any broken glyphs. + TRYV(context.CacheInitialGlyphProbabilities(calculators)); + TRYV(MakeActivationConditionsDisjoint(context, disjoint_conditions_probability_threshold_, merge_groups, with_shared)); } @@ -903,7 +907,7 @@ StatusOr ClosureGlyphSegmenter::CodepointToGlyphSegments( if (target_mode_ == ift::config::UNICODE_RANGE) { TRYV(ValidateIncrementalGroupings(face, context)); } - return ToFinalSegmentation(context, mergers); + return ToFinalSegmentation(context); } // ### Iteratively merge segments and incrementally reprocess affected data. @@ -953,7 +957,7 @@ StatusOr ClosureGlyphSegmenter::CodepointToGlyphSegments( merger.LogMergedSizeHistogram(); } - return ToFinalSegmentation(context, mergers); + return ToFinalSegmentation(context); } TRYV(context.ReprocessChanged(std::move(*maybe_modified))); diff --git a/ift/encoder/closure_glyph_segmenter_test.cc b/ift/encoder/closure_glyph_segmenter_test.cc index c1a44f08..73784aeb 100644 --- a/ift/encoder/closure_glyph_segmenter_test.cc +++ b/ift/encoder/closure_glyph_segmenter_test.cc @@ -3138,6 +3138,46 @@ if (s2) then p2 )"); } +TEST_F(ClosureGlyphSegmenterTest, + UnicodeRange_InitialGlyphProbabilitiesCached) { + auto calc = std::make_shared( + std::vector>{ + {Segment({'f'}), 0.8}, + {Segment({'i'}), 0.7}, + {Segment({'f', 'i'}), 0.94}, + {Segment({0xc1}), 0.05}, + {Segment({0x106}), 0.01}, + {Segment({0xc1, 0x106}), 0.0595}, + }); + + std::vector segments = { + Segment({'f'}), Segment({'i'}), Segment({0xc1}), Segment({0x106})}; + + auto context = SegmentationContext::InitializeSegmentationContext( + roboto.get(), {'a'}, segments, FIND_CONDITIONS, CLOSURE_ONLY, 8, 8, + resolver, ift::config::UNICODE_RANGE); + ASSERT_TRUE(context.ok()) << context.status(); + + ASSERT_TRUE(context->CacheInitialGlyphProbabilities({calc.get()}).ok()); + const auto& initial_probs = context->InitialGlyphProbabilities(); + + // Glyph 74 is 'f' (P = 0.8), glyph 77 is 'i' (P = 0.7). + // Glyph 444 is 'fi' ligature which has condition (s0 AND s1) -> P = 0.56. + EXPECT_NEAR(initial_probs.at(74), 0.8, 1e-6); + EXPECT_NEAR(initial_probs.at(77), 0.7, 1e-6); + EXPECT_NEAR(initial_probs.at(444), 0.56, 1e-6); + + // Glyphs 117, 169, 667 are acute accent components with condition (s2 OR s3) + // -> P = 0.0595. + EXPECT_NEAR(initial_probs.at(117), 0.0595, 1e-6); + EXPECT_NEAR(initial_probs.at(169), 0.0595, 1e-6); + EXPECT_NEAR(initial_probs.at(667), 0.0595, 1e-6); + + // Glyphs 37 ('Á' / 0xc1) and 39 ('Ć' / 0x106) + EXPECT_NEAR(initial_probs.at(37), 0.05, 1e-6); + EXPECT_NEAR(initial_probs.at(39), 0.01, 1e-6); +} + TEST_F(ClosureGlyphSegmenterTest, UnicodeRange_InitialSegmentCodepointsAndFeatures) { ClosureGlyphSegmenter seg(8, 8, FIND_CONDITIONS, diff --git a/ift/encoder/segmentation_context.cc b/ift/encoder/segmentation_context.cc index ef8b7e72..1703161a 100644 --- a/ift/encoder/segmentation_context.cc +++ b/ift/encoder/segmentation_context.cc @@ -1,8 +1,17 @@ #include "ift/encoder/segmentation_context.h" +#include #include +#include +#include +#include +#include "absl/container/btree_map.h" #include "absl/status/status.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_format.h" +#include "absl/strings/str_join.h" +#include "ift/common/font_helper.h" #include "ift/common/int_set.h" #include "ift/common/try.h" #include "ift/encoder/activation_condition.h" @@ -28,10 +37,35 @@ using ift::common::SegmentSet; namespace ift::encoder { -Status SegmentationContext::ValidateSegmentation( - const GlyphSegmentation& segmentation, +Status SegmentationContext::CacheInitialGlyphProbabilities( absl::Span - probability_calculators) const { + probability_calculators) { + initial_glyph_probabilities_.clear(); + for (const freq::ProbabilityCalculator* calculator : + probability_calculators) { + calculator->ResetSegmentProbabilities( + segmentation_info_->Segments().size()); + } + + for (const auto& [cond, gids] : glyph_groupings.ConditionsAndGlyphs()) { + double max_prob = 0.0; + for (const freq::ProbabilityCalculator* calculator : + probability_calculators) { + double prob = + TRY(cond.Probability(segmentation_info_->Segments(), *calculator)); + if (prob > max_prob) { + max_prob = prob; + } + } + for (glyph_id_t gid : gids) { + initial_glyph_probabilities_[gid] = max_prob; + } + } + return absl::OkStatus(); +} + +Status SegmentationContext::ValidateSegmentation( + const GlyphSegmentation& segmentation) const { GlyphSet visited; const auto& initial_closure = segmentation.InitialFontGlyphClosure(); auto check_disjoint = [&](const GlyphSet& gids) -> Status { @@ -62,37 +96,21 @@ Status SegmentationContext::ValidateSegmentation( } } - for (const freq::ProbabilityCalculator* calculator : - probability_calculators) { - calculator->ResetSegmentProbabilities( - segmentation_info_->Segments().size()); - } - // In unicode range mode not all conditions get record into segmentation, so // check disjointness across the non-exclusive patches which were skipped. TRYV(check_disjoint(glyph_groupings.UnmappedGlyphs())); - size_t unreachable_glyph_count = glyph_groupings.UnmappedGlyphs().size(); + GlyphSet unreachable_glyphs = glyph_groupings.UnmappedGlyphs(); for (const auto& cond : glyph_groupings.OrderedConditions()) { if (cond.IsExclusive()) { continue; } const GlyphSet& gids = glyph_groupings.ConditionsAndGlyphs().at(cond); TRYV(check_disjoint(gids)); - unreachable_glyph_count += gids.size(); - - double max_prob = 0.0; - for (const freq::ProbabilityCalculator* calculator : - probability_calculators) { - double prob = - TRY(cond.Probability(segmentation_info_->Segments(), *calculator)); - if (prob > max_prob) { - max_prob = prob; - } - } - VLOG(0) << "Unreachable condition " << cond.ToString() << " => " - << gids.ToString() << " (max probability = " << max_prob << ")"; + unreachable_glyphs.union_set(gids); } - VLOG(0) << "Total unreachable glyphs: " << unreachable_glyph_count; + + LogUnreachableGlyphs(unreachable_glyphs); + LogSegmentCodepointCountHistogram(segmentation); } GlyphSet full_minus_initial = segmentation_info_->FullClosure(); @@ -129,6 +147,72 @@ Status SegmentationContext::ValidateSegmentation( return absl::OkStatus(); } +void SegmentationContext::LogUnreachableGlyphs( + const GlyphSet& unreachable_glyphs) const { + std::vector> ordered_glyphs; + ordered_glyphs.reserve(unreachable_glyphs.size()); + for (glyph_id_t gid : unreachable_glyphs) { + double initial_prob = 0.0; + if (auto it = initial_glyph_probabilities_.find(gid); + it != initial_glyph_probabilities_.end()) { + initial_prob = it->second; + } + ordered_glyphs.emplace_back(gid, initial_prob); + } + + std::sort(ordered_glyphs.begin(), ordered_glyphs.end(), + [](const auto& a, const auto& b) { + if (a.second != b.second) { + return a.second > b.second; + } + return a.first < b.first; + }); + + auto gid_to_unicode = common::FontHelper::GidToUnicodeMap(original_face.get()); + for (const auto& [gid, initial_prob] : ordered_glyphs) { + std::string cp_str; + auto cp_it = gid_to_unicode.find(gid); + if (cp_it != gid_to_unicode.end() && !cp_it->second.empty()) { + if (cp_it->second.size() == 1) { + cp_str = absl::StrFormat("U+%X, ", *cp_it->second.min()); + } else { + std::vector hex_cps; + for (uint32_t cp : cp_it->second) { + hex_cps.push_back(absl::StrFormat("U+%X", cp)); + } + cp_str = + absl::StrCat("{", absl::StrJoin(hex_cps, ", "), "}, "); + } + } + + std::string gid_str = absl::StrFormat("g%u, ", gid); + std::string prob_str = absl::StrFormat("%.3g%%", initial_prob * 100.0); + VLOG(0) << absl::StrFormat("Unreachable glyph %-8s%-10sP = %-9s", + gid_str, cp_str, prob_str); + } + VLOG(0) << "Total unreachable glyphs: " << unreachable_glyphs.size(); +} + +void SegmentationContext::LogSegmentCodepointCountHistogram( + const GlyphSegmentation& segmentation) const { + absl::btree_map codepoint_count_histogram; + for (const auto& segment : segmentation.Segments()) { + if (segment.codepoints.empty()) { + continue; + } + codepoint_count_histogram[segment.codepoints.size()]++; + } + + std::stringstream histogram_string; + histogram_string << "codepoint_count, segment_count" << std::endl; + for (const auto& [codepoint_count, segment_count] : + codepoint_count_histogram) { + histogram_string << codepoint_count << ", " << segment_count << std::endl; + } + VLOG(0) << "Segment Codepoint Count Histogram:" << std::endl + << histogram_string.str(); +} + Status SegmentationContext::ReprocessChanged(InvalidationSet modified) { segment_index_t last_merged_segment_index = modified.base_segment; diff --git a/ift/encoder/segmentation_context.h b/ift/encoder/segmentation_context.h index af965c5b..564a1350 100644 --- a/ift/encoder/segmentation_context.h +++ b/ift/encoder/segmentation_context.h @@ -90,6 +90,7 @@ class SegmentationContext { std::move(closure_cache), std::move(segmentation_info), resolver_, target_mode_, estimated_compression_ratio_, estimated_woff2_base_overhead_); + context.initial_glyph_probabilities_ = initial_glyph_probabilities_; TRYV(context.InitDependencyClosure()); return std::move(context); @@ -158,15 +159,24 @@ class SegmentationContext { // Convert the information in this context into a finalized GlyphSegmentation // representation. - absl::StatusOr ToGlyphSegmentation( - absl::Span - probability_calculators = {}) const { + absl::StatusOr ToGlyphSegmentation() const { GlyphSegmentation segmentation = TRY(glyph_groupings.ToGlyphSegmentation( *segmentation_info_, target_mode_ == config::UNICODE_RANGE)); - TRYV(ValidateSegmentation(segmentation, probability_calculators)); + TRYV(ValidateSegmentation(segmentation)); return segmentation; } + // Caches the initial segmentation probabilities per glyph based on the + // initial segment definitions and activation conditions. + absl::Status CacheInitialGlyphProbabilities( + absl::Span + probability_calculators); + + const absl::flat_hash_map& InitialGlyphProbabilities() + const { + return initial_glyph_probabilities_; + } + void LogClosureStatistics() const { uint64_t dep_graph_closures = 0; uint64_t dep_graph_inaccurate = 0; @@ -333,9 +343,13 @@ class SegmentationContext { * - Fully covers the full closure. */ absl::Status ValidateSegmentation( - const GlyphSegmentation& segmentation, - absl::Span - probability_calculators = {}) const; + const GlyphSegmentation& segmentation) const; + + void LogUnreachableGlyphs( + const ift::common::GlyphSet& unreachable_glyphs) const; + + void LogSegmentCodepointCountHistogram( + const GlyphSegmentation& segmentation) const; // If the merging strategy specifies a minimum cost threshold, this computes // the segment where we should stop computing optimized merges. This is @@ -422,6 +436,8 @@ class SegmentationContext { ift::config::ConditionAnalysisMode condition_analysis_mode_; ift::config::TargetMode target_mode_; std::shared_ptr resolver_; + + absl::flat_hash_map initial_glyph_probabilities_; }; } // namespace ift::encoder