Skip to content

Commit ebb3302

Browse files
committed
Add capability to convert from a GlyphSegmentation to an encoder config.
1 parent 65653d3 commit ebb3302

File tree

10 files changed

+404
-34
lines changed

10 files changed

+404
-34
lines changed

ift/encoder/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ cc_library(
1515
deps = [
1616
"//ift/proto",
1717
"//ift",
18+
"//util:encoder_config_cc_proto",
1819
"@abseil-cpp//absl/container:flat_hash_map",
1920
"@abseil-cpp//absl/container:flat_hash_set",
2021
"@abseil-cpp//absl/container:btree",

ift/encoder/closure_glyph_segmenter.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,9 @@ StatusOr<GlyphSegmentation> ClosureGlyphSegmenter::CodepointToGlyphSegments(
672672
context.ResetGroupings();
673673
TRYV(GroupGlyphs(context));
674674

675-
GlyphSegmentation segmentation(to_btree_set(context.initial_closure.get()),
676-
context.unmapped_glyphs);
675+
GlyphSegmentation segmentation(
676+
to_btree_set(context.initial_codepoints.get()),
677+
to_btree_set(context.initial_closure.get()), context.unmapped_glyphs);
677678
segmentation.CopySegments(context.segments);
678679

679680
TRYV(GlyphSegmentation::GroupsToSegmentation(

ift/encoder/glyph_segmentation.cc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,53 @@ GlyphSegmentation::ActivationConditionsToConditionEntries(
387387
return entries;
388388
}
389389

390+
template <typename ProtoType>
391+
ProtoType ToSetProto(const btree_set<uint32_t>& set) {
392+
ProtoType values;
393+
for (uint32_t v : set) {
394+
values.add_values(v);
395+
}
396+
return values;
397+
}
398+
399+
ActivationConditionProto GlyphSegmentation::ActivationCondition::ToConfigProto()
400+
const {
401+
ActivationConditionProto proto;
402+
403+
for (const auto& c : conditions()) {
404+
CodepointSets group = *proto.add_required_codepoint_sets() =
405+
ToSetProto<CodepointSets>(c);
406+
}
407+
proto.set_activated_patch(activated());
408+
409+
return proto;
410+
}
411+
412+
EncoderConfig GlyphSegmentation::ToConfigProto() const {
413+
EncoderConfig config;
414+
415+
uint32_t set_index = 0;
416+
for (const auto& s : Segments()) {
417+
if (!s.empty()) {
418+
(*config.mutable_codepoint_sets())[set_index++] =
419+
ToSetProto<Codepoints>(s);
420+
} else {
421+
set_index++;
422+
}
423+
}
424+
425+
for (const auto& [patch_id, gids] : GidSegments()) {
426+
(*config.mutable_glyph_patches())[patch_id] = ToSetProto<Glyphs>(gids);
427+
}
428+
429+
for (const auto& c : Conditions()) {
430+
*config.add_glyph_patch_conditions() = c.ToConfigProto();
431+
}
432+
433+
*config.mutable_initial_codepoints() =
434+
ToSetProto<Codepoints>(InitialFontCodepoints());
435+
436+
return config;
437+
}
438+
390439
} // namespace ift::encoder

ift/encoder/glyph_segmentation.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "common/hb_set_unique_ptr.h"
1313
#include "hb.h"
1414
#include "ift/encoder/condition.h"
15+
#include "util/encoder_config.pb.h"
1516

1617
namespace ift::encoder {
1718

@@ -25,8 +26,8 @@ typedef uint32_t glyph_id_t;
2526
*
2627
* A segmentation describes the groups of glyphs belong to each patch as well as
2728
* the conditions under which those patches should be loaded. A properly formed
28-
* segmentation should have an associated set of patches and conditions which will
29-
* satisfy the "glyph closure requirement", which is:
29+
* segmentation should have an associated set of patches and conditions which
30+
* will satisfy the "glyph closure requirement", which is:
3031
*
3132
* The set of glyphs contained in patches loaded for a font subset definition (a
3233
* set of Unicode codepoints and a set of layout feature tags) through the patch
@@ -111,6 +112,8 @@ class GlyphSegmentation {
111112
return conditions().size() == 1 && conditions().at(0).size() == 1;
112113
}
113114

115+
ActivationConditionProto ToConfigProto() const;
116+
114117
bool operator<(const ActivationCondition& other) const;
115118

116119
bool operator==(const ActivationCondition& other) const {
@@ -126,9 +129,11 @@ class GlyphSegmentation {
126129
patch_id_t activated_;
127130
};
128131

129-
GlyphSegmentation(absl::btree_set<glyph_id_t> init_font_glyphs,
132+
GlyphSegmentation(absl::btree_set<hb_codepoint_t> init_font_codepoints,
133+
absl::btree_set<glyph_id_t> init_font_glyphs,
130134
absl::btree_set<glyph_id_t> unmapped_glyphs)
131-
: init_font_glyphs_(init_font_glyphs),
135+
: init_font_codepoints_(init_font_codepoints),
136+
init_font_glyphs_(init_font_glyphs),
132137
unmapped_glyphs_(unmapped_glyphs) {}
133138

134139
/*
@@ -192,6 +197,15 @@ class GlyphSegmentation {
192197
return init_font_glyphs_;
193198
};
194199

200+
/*
201+
* These codepoints should be included in the initial font.
202+
*/
203+
const absl::btree_set<hb_codepoint_t>& InitialFontCodepoints() const {
204+
return init_font_codepoints_;
205+
};
206+
207+
EncoderConfig ToConfigProto() const;
208+
195209
static absl::Status GroupsToSegmentation(
196210
const absl::btree_map<absl::btree_set<segment_index_t>,
197211
absl::btree_set<glyph_id_t>>& and_glyph_groups,
@@ -203,6 +217,7 @@ class GlyphSegmentation {
203217
void CopySegments(const std::vector<common::hb_set_unique_ptr>& segments);
204218

205219
private:
220+
absl::btree_set<hb_codepoint_t> init_font_codepoints_;
206221
absl::btree_set<glyph_id_t> init_font_glyphs_;
207222
absl::btree_set<glyph_id_t> unmapped_glyphs_;
208223
absl::btree_set<ActivationCondition> conditions_;

0 commit comments

Comments
 (0)