Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ift/encoder/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
Expand Down
30 changes: 17 additions & 13 deletions ift/encoder/closure_glyph_segmenter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -601,18 +601,9 @@ StatusOr<std::vector<Merger>> ToMergers(
}

static StatusOr<GlyphSegmentation> ToFinalSegmentation(
SegmentationContext& context, Span<const Merger> mergers) {
SegmentationContext& context) {
context.LogClosureStatistics();
std::vector<const ProbabilityCalculator*> 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(
Expand Down Expand Up @@ -850,6 +841,19 @@ StatusOr<GlyphSegmentation> ClosureGlyphSegmenter::CodepointToGlyphSegments(
init_font_merging_brotli_quality_, resolver_, target_mode_));

if (target_mode_ == ift::config::UNICODE_RANGE) {
std::vector<const ProbabilityCalculator*> 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));
}

Expand Down Expand Up @@ -903,7 +907,7 @@ StatusOr<GlyphSegmentation> 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.
Expand Down Expand Up @@ -953,7 +957,7 @@ StatusOr<GlyphSegmentation> ClosureGlyphSegmenter::CodepointToGlyphSegments(
merger.LogMergedSizeHistogram();
}

return ToFinalSegmentation(context, mergers);
return ToFinalSegmentation(context);
}

TRYV(context.ReprocessChanged(std::move(*maybe_modified)));
Expand Down
40 changes: 40 additions & 0 deletions ift/encoder/closure_glyph_segmenter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3138,6 +3138,46 @@ if (s2) then p2
)");
}

TEST_F(ClosureGlyphSegmenterTest,
UnicodeRange_InitialGlyphProbabilitiesCached) {
auto calc = std::make_shared<MockProbabilityCalculator>(
std::vector<std::pair<Segment, double>>{
{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<Segment> 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,
Expand Down
132 changes: 108 additions & 24 deletions ift/encoder/segmentation_context.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#include "ift/encoder/segmentation_context.h"

#include <algorithm>
#include <cstdint>
#include <sstream>
#include <utility>
#include <vector>

#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"
Expand All @@ -28,10 +37,35 @@ using ift::common::SegmentSet;

namespace ift::encoder {

Status SegmentationContext::ValidateSegmentation(
const GlyphSegmentation& segmentation,
Status SegmentationContext::CacheInitialGlyphProbabilities(
absl::Span<const freq::ProbabilityCalculator* const>
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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -129,6 +147,72 @@ Status SegmentationContext::ValidateSegmentation(
return absl::OkStatus();
}

void SegmentationContext::LogUnreachableGlyphs(
const GlyphSet& unreachable_glyphs) const {
std::vector<std::pair<glyph_id_t, double>> 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<std::string> 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<size_t, uint32_t> 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;

Expand Down
30 changes: 23 additions & 7 deletions ift/encoder/segmentation_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -158,15 +159,24 @@ class SegmentationContext {

// Convert the information in this context into a finalized GlyphSegmentation
// representation.
absl::StatusOr<GlyphSegmentation> ToGlyphSegmentation(
absl::Span<const freq::ProbabilityCalculator* const>
probability_calculators = {}) const {
absl::StatusOr<GlyphSegmentation> 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<const freq::ProbabilityCalculator* const>
probability_calculators);

const absl::flat_hash_map<glyph_id_t, double>& InitialGlyphProbabilities()
const {
return initial_glyph_probabilities_;
}

void LogClosureStatistics() const {
uint64_t dep_graph_closures = 0;
uint64_t dep_graph_inaccurate = 0;
Expand Down Expand Up @@ -333,9 +343,13 @@ class SegmentationContext {
* - Fully covers the full closure.
*/
absl::Status ValidateSegmentation(
const GlyphSegmentation& segmentation,
absl::Span<const freq::ProbabilityCalculator* const>
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
Expand Down Expand Up @@ -422,6 +436,8 @@ class SegmentationContext {
ift::config::ConditionAnalysisMode condition_analysis_mode_;
ift::config::TargetMode target_mode_;
std::shared_ptr<ift::common::DataFileResolver> resolver_;

absl::flat_hash_map<glyph_id_t, double> initial_glyph_probabilities_;
};

} // namespace ift::encoder
Expand Down
Loading