Skip to content

Commit 8b2be36

Browse files
committed
Formatting.
1 parent 1db6eec commit 8b2be36

11 files changed

+105
-74
lines changed

common/woff2.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ using woff2::WOFF2StringOut;
1515

1616
namespace common {
1717

18-
StatusOr<FontData> Woff2::EncodeWoff2(string_view font, bool glyf_transform, int quality) {
18+
StatusOr<FontData> Woff2::EncodeWoff2(string_view font, bool glyf_transform,
19+
int quality) {
1920
WOFF2Params params;
2021
params.brotli_quality = quality;
2122
params.allow_transforms = glyf_transform;

common/woff2.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ namespace common {
99

1010
struct Woff2 {
1111
static absl::StatusOr<FontData> EncodeWoff2(absl::string_view font,
12-
bool glyf_transform = true, int quality = 11);
12+
bool glyf_transform = true,
13+
int quality = 11);
1314
static absl::StatusOr<FontData> DecodeWoff2(absl::string_view font);
1415
};
1516

ift/encoder/candidate_merge.cc

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "common/font_helper.h"
1515
#include "common/int_set.h"
1616
#include "common/try.h"
17+
#include "common/woff2.h"
1718
#include "ift/encoder/activation_condition.h"
1819
#include "ift/encoder/glyph_groupings.h"
1920
#include "ift/encoder/requested_segmentation_information.h"
@@ -22,7 +23,6 @@
2223
#include "ift/encoder/subset_definition.h"
2324
#include "ift/encoder/types.h"
2425
#include "ift/glyph_keyed_diff.h"
25-
#include "common/woff2.h"
2626

2727
namespace ift::encoder {
2828

@@ -217,24 +217,26 @@ StatusOr<uint32_t> CandidateMerge::Woff2SizeOf(hb_face_t* original_face,
217217
return (double)woff2.size();
218218
}
219219

220-
static StatusOr<int64_t> InitFontDelta(const SegmentationContext& context, const SegmentSet& merged_segments) {
220+
static StatusOr<int64_t> InitFontDelta(const SegmentationContext& context,
221+
const SegmentSet& merged_segments) {
221222
int quality = context.GetMergeStrategy().BrotliQuality();
222223
SubsetDefinition init_def = context.SegmentationInfo().InitFontSegment();
223-
int64_t before = TRY(CandidateMerge::Woff2SizeOf(context.original_face.get(), init_def, quality));
224+
int64_t before = TRY(CandidateMerge::Woff2SizeOf(context.original_face.get(),
225+
init_def, quality));
224226

225227
for (segment_index_t s : merged_segments) {
226228
init_def.Union(context.SegmentationInfo().Segments().at(s).Definition());
227229
}
228230

229-
int64_t after = TRY(CandidateMerge::Woff2SizeOf(context.original_face.get(), init_def, quality));
231+
int64_t after = TRY(CandidateMerge::Woff2SizeOf(context.original_face.get(),
232+
init_def, quality));
230233

231234
return after - before;
232235
}
233236

234-
StatusOr<double> CandidateMerge::ComputeCostDelta(const SegmentationContext& context,
235-
const SegmentSet& merged_segments,
236-
std::optional<const Segment*> merged_segment,
237-
uint32_t new_patch_size) {
237+
StatusOr<double> CandidateMerge::ComputeCostDelta(
238+
const SegmentationContext& context, const SegmentSet& merged_segments,
239+
std::optional<const Segment*> merged_segment, uint32_t new_patch_size) {
238240
bool moving_to_init_font = !merged_segment.has_value();
239241
const uint32_t per_request_overhead =
240242
context.GetMergeStrategy().NetworkOverheadCost();
@@ -262,10 +264,12 @@ StatusOr<double> CandidateMerge::ComputeCostDelta(const SegmentationContext& con
262264
// "new_patch_size", add the associated cost.
263265
double p = (*merged_segment)->Probability();
264266
cost_delta += p * (new_patch_size + per_request_overhead);
265-
VLOG(1) << " cost_delta for merge of " << merged_segments.ToString() << " =";
267+
VLOG(1) << " cost_delta for merge of " << merged_segments.ToString()
268+
<< " =";
266269

267-
VLOG(1) << " + (" << p << " * " << (new_patch_size + per_request_overhead)
268-
<< ") -> " << cost_delta << " [merged patch]";
270+
VLOG(1) << " + (" << p << " * "
271+
<< (new_patch_size + per_request_overhead) << ") -> " << cost_delta
272+
<< " [merged patch]";
269273
} else {
270274
// Otherwise the merged segments are being moved to the init font, compute
271275
// the resulting size delta.
@@ -307,8 +311,10 @@ StatusOr<double> CandidateMerge::ComputeCostDelta(const SegmentationContext& con
307311
// the cost addition as usual.
308312
SegmentSet condition_segments = c.TriggeringSegments();
309313
condition_segments.subtract(merged_segments);
310-
ActivationCondition new_condition = ActivationCondition::and_segments(condition_segments, 0);
311-
double p = TRY(c.Probability(segments, *context.GetMergeStrategy().ProbabilityCalculator()));
314+
ActivationCondition new_condition =
315+
ActivationCondition::and_segments(condition_segments, 0);
316+
double p = TRY(c.Probability(
317+
segments, *context.GetMergeStrategy().ProbabilityCalculator()));
312318
double d = p * (size + per_request_overhead);
313319
VLOG(1) << " + " << d << " [modified patch " << c.ToString() << "]";
314320
cost_delta += d;
@@ -371,10 +377,12 @@ StatusOr<std::optional<CandidateMerge>> CandidateMerge::AssessMerge(
371377
// threshold on the segments to be merged that will allow us to quickly
372378
// discard merges which can't possibily beat the current best.
373379
unsigned segment_to_merge = segments_to_merge.min().value();
374-
const GlyphSet& glyphs = context.glyph_condition_set.GlyphsWithSegment(segment_to_merge);
380+
const GlyphSet& glyphs =
381+
context.glyph_condition_set.GlyphsWithSegment(segment_to_merge);
375382
unsigned segment_to_merge_size = 0;
376383
if (!glyphs.empty()) {
377-
segment_to_merge_size = TRY(context.patch_size_cache->GetPatchSize(glyphs));
384+
segment_to_merge_size =
385+
TRY(context.patch_size_cache->GetPatchSize(glyphs));
378386
}
379387
double threshold = best_merge_candidate->InertProbabilityThreshold(
380388
segment_to_merge_size, merged_segment.Probability());

ift/encoder/candidate_merge.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ struct CandidateMerge {
109109
network_overhead +
110110
BEST_CASE_MERGE_SIZE_DELTA;
111111
double total_base_size = base_size + network_overhead;
112-
double total_patch_size = patch_size > 0 ? patch_size + network_overhead : 0;
112+
double total_patch_size =
113+
patch_size > 0 ? patch_size + network_overhead : 0;
113114

114115
double numerator = merged_probability * best_case_merged_size -
115116
base_probability * total_base_size - cost_delta;
@@ -131,10 +132,10 @@ struct CandidateMerge {
131132
SegmentationContext& context, segment_index_t base_segment_index,
132133
const common::GlyphSet& glyphs);
133134

134-
static absl::StatusOr<double> ComputeCostDelta(const SegmentationContext& context,
135-
const common::SegmentSet& merged_segments,
136-
std::optional<const Segment*> merged_segment,
137-
uint32_t new_patch_size);
135+
static absl::StatusOr<double> ComputeCostDelta(
136+
const SegmentationContext& context,
137+
const common::SegmentSet& merged_segments,
138+
std::optional<const Segment*> merged_segment, uint32_t new_patch_size);
138139

139140
static absl::StatusOr<uint32_t> Woff2SizeOf(hb_face_t* original_face,
140141
const SubsetDefinition& def,

ift/encoder/closure_glyph_segmenter.cc

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,10 @@ Status CollectExclusiveCandidateMerges(
274274
continue;
275275
}
276276

277-
if (context.InertSegments().contains(*it) && context.glyph_condition_set.GlyphsWithSegment(*it).empty()) {
278-
// this segment is effectively a noop, it interacts with nothing and has no glyphs
279-
// so don't consider it for a merge.
277+
if (context.InertSegments().contains(*it) &&
278+
context.glyph_condition_set.GlyphsWithSegment(*it).empty()) {
279+
// this segment is effectively a noop, it interacts with nothing and has
280+
// no glyphs so don't consider it for a merge.
280281
continue;
281282
}
282283

@@ -509,41 +510,50 @@ static StatusOr<std::vector<Segment>> ToSegments(
509510
return segments;
510511
}
511512

512-
Status ClosureGlyphSegmenter::MoveSegmentsToInitFont(SegmentationContext& context) const {
513+
Status ClosureGlyphSegmenter::MoveSegmentsToInitFont(
514+
SegmentationContext& context) const {
513515
if (!context.GetMergeStrategy().InitFontMergeThreshold().has_value()) {
514-
return absl::FailedPreconditionError("Cannot be called when there is no merge threshold configured.");
516+
return absl::FailedPreconditionError(
517+
"Cannot be called when there is no merge threshold configured.");
515518
}
516519

517520
double threshold = *context.GetMergeStrategy().InitFontMergeThreshold();
518521

519-
VLOG(0) << "Checking if there are any segments which should be moved into the initial font.";
522+
VLOG(0) << "Checking if there are any segments which should be moved into "
523+
"the initial font.";
520524

521-
SubsetDefinition initial_segment = context.SegmentationInfo().InitFontSegment();
525+
SubsetDefinition initial_segment =
526+
context.SegmentationInfo().InitFontSegment();
522527
bool change_made;
523528
do {
524529
change_made = false;
525530
SegmentSet segments_to_move;
526-
for (const auto& [c, glyphs] : context.glyph_groupings.ConditionsAndGlyphs()) {
531+
for (const auto& [c, glyphs] :
532+
context.glyph_groupings.ConditionsAndGlyphs()) {
527533
SegmentSet candidate_segments = c.TriggeringSegments();
528534
if (!candidate_segments.intersects(context.ActiveSegments())) {
529-
// Only do this check for things involving active segments, this let's us skip
530-
// checks for conditions are are extremely unlikely to benefit from merging
531-
// into the init font.
535+
// Only do this check for things involving active segments, this let's
536+
// us skip checks for conditions are are extremely unlikely to benefit
537+
// from merging into the init font.
532538
continue;
533539
}
534540

535-
double delta = TRY(CandidateMerge::ComputeCostDelta(context, candidate_segments, std::nullopt, 0));
536-
if (delta >= threshold * (double) candidate_segments.size()) {
541+
double delta = TRY(CandidateMerge::ComputeCostDelta(
542+
context, candidate_segments, std::nullopt, 0));
543+
if (delta >= threshold * (double)candidate_segments.size()) {
537544
// Merging doesn't improve cost, skip.
538545
continue;
539546
}
540547

541-
// TODO(garretrieger): to get a more accurate picture we should consider comparing
548+
// TODO(garretrieger): to get a more accurate picture we should consider
549+
// comparing
542550
// to an updated init subset definition on each iteration.
543551
segments_to_move.union_set(candidate_segments);
544-
VLOG(0) << " Moving segments " << candidate_segments.ToString() << " into the initial font (cost delta = " << delta << ")";
552+
VLOG(0) << " Moving segments " << candidate_segments.ToString()
553+
<< " into the initial font (cost delta = " << delta << ")";
545554
for (segment_index_t s : segments_to_move) {
546-
initial_segment.Union(context.SegmentationInfo().Segments()[s].Definition());
555+
initial_segment.Union(
556+
context.SegmentationInfo().Segments()[s].Definition());
547557
}
548558

549559
TRYV(context.ReassignInitSubset(initial_segment, segments_to_move));
@@ -552,7 +562,8 @@ Status ClosureGlyphSegmenter::MoveSegmentsToInitFont(SegmentationContext& contex
552562
}
553563
} while (change_made);
554564

555-
VLOG(0) << "Initial font now has " << initial_segment.codepoints.size() << " codepoints.";
565+
VLOG(0) << "Initial font now has " << initial_segment.codepoints.size()
566+
<< " codepoints.";
556567
return absl::OkStatus();
557568
}
558569

@@ -579,9 +590,11 @@ StatusOr<GlyphSegmentation> ClosureGlyphSegmenter::CodepointToGlyphSegments(
579590
return context.ToGlyphSegmentation();
580591
}
581592

582-
// ### First phase of merging is to check for any patches which should be moved to the initial font
593+
// ### First phase of merging is to check for any patches which should be
594+
// moved to the initial font
583595
// (eg. cases where the probability of a patch is ~1.0).
584-
if (context.GetMergeStrategy().UseCosts() && context.GetMergeStrategy().InitFontMergeThreshold().has_value()) {
596+
if (context.GetMergeStrategy().UseCosts() &&
597+
context.GetMergeStrategy().InitFontMergeThreshold().has_value()) {
585598
TRYV(MoveSegmentsToInitFont(context));
586599
}
587600

@@ -668,9 +681,10 @@ StatusOr<SegmentationCost> ClosureGlyphSegmenter::TotalCost(
668681
segments.push_back(std::move(s));
669682
}
670683

671-
double init_font_size =
672-
TRY(CandidateMerge::Woff2SizeOf(original_face, segmentation.InitialFontSegment(), 11));
673-
double non_ift_font_size = TRY(CandidateMerge::Woff2SizeOf(original_face, non_ift, 11));
684+
double init_font_size = TRY(CandidateMerge::Woff2SizeOf(
685+
original_face, segmentation.InitialFontSegment(), 11));
686+
double non_ift_font_size =
687+
TRY(CandidateMerge::Woff2SizeOf(original_face, non_ift, 11));
674688

675689
// TODO(garretrieger): for the total cost we need to also add in the table
676690
// keyed patch costs

ift/encoder/closure_glyph_segmenter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class ClosureGlyphSegmenter {
6464
hb_face_t* original_face, const GlyphSegmentation& segmentation,
6565
const freq::ProbabilityCalculator& probability_calculator) const;
6666

67-
private:
68-
absl::Status MoveSegmentsToInitFont(SegmentationContext& context) const;
67+
private:
68+
absl::Status MoveSegmentsToInitFont(SegmentationContext& context) const;
6969
};
7070

7171
} // namespace ift::encoder

ift/encoder/closure_glyph_segmenter_test.cc

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,11 @@ TEST_F(ClosureGlyphSegmenterTest, NoGlyphSegments_CostMerging) {
784784

785785
auto segmentation = segmenter.CodepointToGlyphSegments(
786786
roboto.get(), {0x106 /* Cacute */},
787-
{{'A'}, {'B'}, {'C'},},
787+
{
788+
{'A'},
789+
{'B'},
790+
{'C'},
791+
},
788792
*MergeStrategy::CostBased(std::move(frequencies)));
789793
ASSERT_TRUE(segmentation.ok()) << segmentation.status();
790794

@@ -793,7 +797,7 @@ TEST_F(ClosureGlyphSegmenterTest, NoGlyphSegments_CostMerging) {
793797
std::vector<SubsetDefinition> expected_segments = {
794798
{'A', 'B'},
795799
{},
796-
{'C'}, // C glyph was already pulled in to the init font, so no merge
800+
{'C'}, // C glyph was already pulled in to the init font, so no merge
797801
};
798802
ASSERT_EQ(segmentation->Segments(), expected_segments);
799803

@@ -806,29 +810,28 @@ if (s0) then p0
806810
)");
807811
}
808812

809-
810813
TEST_F(ClosureGlyphSegmenterTest, InitFontMerging) {
811814
// In this test we enable merging of segments into the init font
812815
UnicodeFrequencies frequencies{
813816
{{'a', 'a'}, 100},
814-
{{'b', 'b'}, 50},
815-
{{'c', 'c'}, 50},
817+
{{'b', 'b'}, 50},
818+
{{'c', 'c'}, 50},
816819
{{'d', 'd'}, 100},
817820

818821
// b and c co-occur
819-
{{'b', 'c'}, 50},
822+
{{'b', 'c'}, 50},
820823
};
821824

822-
MergeStrategy strategy = *MergeStrategy::BigramCostBased(std::move(frequencies));
825+
MergeStrategy strategy =
826+
*MergeStrategy::BigramCostBased(std::move(frequencies));
823827
strategy.SetInitFontMergeThreshold(-75);
824828

825829
auto segmentation = segmenter.CodepointToGlyphSegments(
826-
roboto.get(), {},
827-
{{'a'}, {'d'}, {'b'}, {'c'}},
828-
strategy);
830+
roboto.get(), {}, {{'a'}, {'d'}, {'b'}, {'c'}}, strategy);
829831
ASSERT_TRUE(segmentation.ok()) << segmentation.status();
830832

831-
// 'a' and 'd' will be moved to the init font, leaving only two segments 'b', 'c'
833+
// 'a' and 'd' will be moved to the init font, leaving only two segments 'b',
834+
// 'c'
832835
std::vector<SubsetDefinition> expected_segments = {
833836
{},
834837
{},
@@ -848,16 +851,14 @@ TEST_F(ClosureGlyphSegmenterTest, InitFontMerging_CommonGlyphs) {
848851
UnicodeFrequencies frequencies{
849852
{{'A', 'A'}, 1},
850853
{{'C', 'C'}, 1},
851-
{{0x106, 0x106}, 100}, // Cacute (contains C glyph)
854+
{{0x106, 0x106}, 100}, // Cacute (contains C glyph)
852855
};
853856

854857
MergeStrategy strategy = *MergeStrategy::CostBased(std::move(frequencies));
855858
strategy.SetInitFontMergeThreshold(-75);
856859

857860
auto segmentation = segmenter.CodepointToGlyphSegments(
858-
roboto.get(), {},
859-
{{0x106}, {'A'}, {'C'}},
860-
strategy);
861+
roboto.get(), {}, {{0x106}, {'A'}, {'C'}}, strategy);
861862
ASSERT_TRUE(segmentation.ok()) << segmentation.status();
862863

863864
std::vector<SubsetDefinition> expected_segments = {
@@ -867,7 +868,8 @@ TEST_F(ClosureGlyphSegmenterTest, InitFontMerging_CommonGlyphs) {
867868
};
868869
ASSERT_EQ(segmentation->Segments(), expected_segments);
869870

870-
// C get's covered by the Cacute merge into int, so only A is left in the patch.
871+
// C get's covered by the Cacute merge into int, so only A is left in the
872+
// patch.
871873
ASSERT_EQ(segmentation->ToString(),
872874
R"(initial font: { gid0, gid39, gid117, gid700 }
873875
p0: { gid37 }

ift/encoder/requested_segmentation_information.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ namespace ift::encoder {
99
RequestedSegmentationInformation::RequestedSegmentationInformation(
1010
std::vector<Segment> segments, SubsetDefinition init_font_segment,
1111
GlyphClosureCache& closure_cache)
12-
: segments_(std::move(segments)),
13-
init_font_segment_() {
12+
: segments_(std::move(segments)), init_font_segment_() {
1413
ReassignInitSubset(closure_cache, std::move(init_font_segment), {});
1514
}
1615

ift/encoder/requested_segmentation_information.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class RequestedSegmentationInformation {
3333
return base_segment.Definition().codepoints.size();
3434
}
3535

36-
void ReassignInitSubset(GlyphClosureCache& closure_cache, SubsetDefinition new_def, const common::SegmentSet& removed_segments) {
36+
void ReassignInitSubset(GlyphClosureCache& closure_cache,
37+
SubsetDefinition new_def,
38+
const common::SegmentSet& removed_segments) {
3739
for (segment_index_t s : removed_segments) {
3840
segments_[s].Clear();
3941
}

0 commit comments

Comments
 (0)