Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 766f53c

Browse files
jason-simmonsfluttergithubbot
authored andcommitted
[libtxt] Assign a unique ID to each glyph cluster within a line (#15742)
1 parent 7fcc986 commit 766f53c

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

third_party/txt/src/txt/paragraph_txt.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ void ParagraphTxt::Layout(double width) {
796796

797797
double run_x_offset = 0;
798798
double justify_x_offset = 0;
799+
size_t cluster_unique_id = 0;
799800
std::vector<PaintRecord> paint_records;
800801

801802
for (auto line_run_it = line_runs.begin(); line_run_it != line_runs.end();
@@ -956,19 +957,20 @@ void ParagraphTxt::Layout(double width) {
956957
float grapheme_advance =
957958
glyph_advance / grapheme_code_unit_counts.size();
958959

959-
glyph_positions.emplace_back(run_x_offset + glyph_x_offset,
960-
grapheme_advance,
961-
run.start() + glyph_code_units.start,
962-
grapheme_code_unit_counts[0], cluster);
960+
glyph_positions.emplace_back(
961+
run_x_offset + glyph_x_offset, grapheme_advance,
962+
run.start() + glyph_code_units.start,
963+
grapheme_code_unit_counts[0], cluster_unique_id);
963964

964965
// Compute positions for the additional graphemes in the ligature.
965966
for (size_t i = 1; i < grapheme_code_unit_counts.size(); ++i) {
966967
glyph_positions.emplace_back(
967968
glyph_positions.back().x_pos.end, grapheme_advance,
968969
glyph_positions.back().code_units.start +
969970
grapheme_code_unit_counts[i - 1],
970-
grapheme_code_unit_counts[i], cluster);
971+
grapheme_code_unit_counts[i], cluster_unique_id);
971972
}
973+
cluster_unique_id++;
972974

973975
bool at_word_start = false;
974976
bool at_word_end = false;

third_party/txt/tests/paragraph_unittests.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,37 @@ TEST_F(ParagraphTest, GetGlyphPositionAtCoordinateSegfault) {
175175
ASSERT_TRUE(Snapshot());
176176
}
177177

178+
// Check that GetGlyphPositionAtCoordinate computes correct text positions for
179+
// a paragraph containing multiple styled runs.
180+
TEST_F(ParagraphTest, GetGlyphPositionAtCoordinateMultiRun) {
181+
txt::ParagraphStyle paragraph_style;
182+
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
183+
184+
txt::TextStyle text_style;
185+
text_style.font_families = std::vector<std::string>(1, "Ahem");
186+
text_style.color = SK_ColorBLACK;
187+
text_style.font_size = 10;
188+
builder.PushStyle(text_style);
189+
builder.AddText(u"A");
190+
text_style.font_size = 20;
191+
builder.PushStyle(text_style);
192+
builder.AddText(u"B");
193+
text_style.font_size = 30;
194+
builder.PushStyle(text_style);
195+
builder.AddText(u"C");
196+
197+
auto paragraph = BuildParagraph(builder);
198+
paragraph->Layout(GetTestCanvasWidth());
199+
200+
paragraph->Paint(GetCanvas(), 10.0, 15.0);
201+
202+
ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(2.0, 5.0).position, 0ull);
203+
ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(12.0, 5.0).position, 1ull);
204+
ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(32.0, 5.0).position, 2ull);
205+
206+
ASSERT_TRUE(Snapshot());
207+
}
208+
178209
TEST_F(ParagraphTest, LineMetricsParagraph1) {
179210
const char* text = "Hello! What is going on?\nSecond line \nthirdline";
180211
auto icu_text = icu::UnicodeString::fromUTF8(text);

0 commit comments

Comments
 (0)