diff --git a/engine/src/flutter/third_party/txt/src/txt/paragraph.cc b/engine/src/flutter/third_party/txt/src/txt/paragraph.cc index c92b4ede35..02a51c4cf8 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paragraph.cc +++ b/engine/src/flutter/third_party/txt/src/txt/paragraph.cc @@ -157,11 +157,13 @@ Paragraph::CodeUnitRun::CodeUnitRun(std::vector&& p, Range cu, Range x, size_t line, + const SkPaint::FontMetrics& metrics, TextDirection dir) : positions(std::move(p)), code_units(cu), x_pos(x), line_number(line), + font_metrics(metrics), direction(dir) {} Paragraph::Paragraph() { @@ -484,7 +486,6 @@ void Paragraph::Layout(double width, bool force) { blob_start += blob_len; } - std::vector glyph_positions; size_t code_unit_index; if (run.is_rtl()) { code_unit_index = text_count; @@ -496,6 +497,8 @@ void Paragraph::Layout(double width, bool force) { // Build a Skia text blob from each group of glyphs. for (const Range& glyph_blob : glyph_blobs) { + std::vector glyph_positions; + paint.setTypeface(GetTypefaceForGlyph(layout, glyph_blob.start)); const SkTextBlobBuilder::RunBuffer& blob_buffer = builder.allocRunPos(paint, glyph_blob.end - glyph_blob.start); @@ -578,24 +581,24 @@ void Paragraph::Layout(double width, bool force) { paint_records.emplace_back(run.style, SkPoint::Make(run_x_offset, 0), builder.make(), metrics, line_number, layout.getAdvance()); + + line_glyph_positions.insert(line_glyph_positions.end(), + glyph_positions.begin(), + glyph_positions.end()); + + // Add a record of glyph positions sorted by code unit index. + std::vector code_unit_positions(glyph_positions); + std::sort(code_unit_positions.begin(), code_unit_positions.end(), + [](const GlyphPosition& a, const GlyphPosition& b) { + return a.code_units.start < b.code_units.start; + }); + code_unit_runs_.emplace_back( + std::move(code_unit_positions), Range(run.start, run.end), + Range(glyph_positions.front().x_pos.start, + glyph_positions.back().x_pos.end), + line_number, metrics, run.direction); } - line_glyph_positions.insert(line_glyph_positions.end(), - glyph_positions.begin(), - glyph_positions.end()); - - // Add a record of glyph positions sorted by code unit index. - std::vector code_unit_positions(glyph_positions); - std::sort(code_unit_positions.begin(), code_unit_positions.end(), - [](const GlyphPosition& a, const GlyphPosition& b) { - return a.code_units.start < b.code_units.start; - }); - code_unit_runs_.emplace_back( - std::move(code_unit_positions), Range(run.start, run.end), - Range(glyph_positions.front().x_pos.start, - glyph_positions.back().x_pos.end), - line_number, run.direction); - run_x_offset += layout.getAdvance(); } @@ -625,6 +628,7 @@ void Paragraph::Layout(double width, bool force) { line_heights_.push_back((line_heights_.empty() ? 0 : line_heights_.back()) + round(max_line_spacing + max_descent)); + line_baselines_.push_back(line_heights_.back() - max_descent); y_offset += round(max_line_spacing + prev_max_descent); prev_max_descent = max_descent; @@ -897,9 +901,9 @@ std::vector Paragraph::GetRectsForRange(size_t start, if (run.code_units.end <= start) continue; - SkScalar top = - (run.line_number > 0) ? line_heights_[run.line_number - 1] : 0; - SkScalar bottom = line_heights_[run.line_number]; + double baseline = line_baselines_[run.line_number]; + SkScalar top = baseline + run.font_metrics.fAscent; + SkScalar bottom = baseline + run.font_metrics.fDescent; SkScalar left, right; if (run.code_units.start >= start && run.code_units.end <= end) { diff --git a/engine/src/flutter/third_party/txt/src/txt/paragraph.h b/engine/src/flutter/third_party/txt/src/txt/paragraph.h index 706b2751a2..7f18f3df7a 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paragraph.h +++ b/engine/src/flutter/third_party/txt/src/txt/paragraph.h @@ -206,6 +206,7 @@ class Paragraph { std::vector records_; std::vector line_heights_; + std::vector line_baselines_; bool did_exceed_max_lines_; struct GlyphPosition { @@ -232,12 +233,14 @@ class Paragraph { Range code_units; Range x_pos; size_t line_number; + SkPaint::FontMetrics font_metrics; TextDirection direction; CodeUnitRun(std::vector&& p, Range cu, Range x, size_t line, + const SkPaint::FontMetrics& metrics, TextDirection dir); }; diff --git a/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc b/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc index 5beb9cbbfd..8093552c44 100644 --- a/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc +++ b/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc @@ -968,7 +968,7 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(GetRectsForRangeParagraph)) { } EXPECT_EQ(boxes.size(), 1ull); EXPECT_FLOAT_EQ(boxes[0].rect.left(), 0); - EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0); + EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625); EXPECT_FLOAT_EQ(boxes[0].rect.right(), 28.417969); EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59); @@ -979,7 +979,7 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(GetRectsForRangeParagraph)) { } EXPECT_EQ(boxes.size(), 1ull); EXPECT_FLOAT_EQ(boxes[0].rect.left(), 56.835938); - EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0); + EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625); EXPECT_FLOAT_EQ(boxes[0].rect.right(), 177.44922); EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59); @@ -990,7 +990,7 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(GetRectsForRangeParagraph)) { } EXPECT_EQ(boxes.size(), 1ull); EXPECT_FLOAT_EQ(boxes[0].rect.left(), 177); - EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0); + EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625); EXPECT_FLOAT_EQ(boxes[0].rect.right(), 506.08984); EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59); @@ -1001,14 +1001,14 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(GetRectsForRangeParagraph)) { } EXPECT_EQ(boxes.size(), 4ull); EXPECT_FLOAT_EQ(boxes[0].rect.left(), 210.83594); - EXPECT_FLOAT_EQ(boxes[0].rect.top(), 59); + EXPECT_FLOAT_EQ(boxes[0].rect.top(), 59.40625); EXPECT_FLOAT_EQ(boxes[0].rect.right(), 463.44922); EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 118); // TODO(garyq): The following set of vals are definetly wrong and // end of paragraph handling needs to be fixed in a later patch. EXPECT_FLOAT_EQ(boxes[3].rect.left(), 0); - EXPECT_FLOAT_EQ(boxes[3].rect.top(), 236); + EXPECT_FLOAT_EQ(boxes[3].rect.top(), 236.40625); EXPECT_FLOAT_EQ(boxes[3].rect.right(), 142.08984); EXPECT_FLOAT_EQ(boxes[3].rect.bottom(), 295); @@ -1019,7 +1019,7 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(GetRectsForRangeParagraph)) { } EXPECT_EQ(boxes.size(), 1ull); EXPECT_FLOAT_EQ(boxes[0].rect.left(), 449.25391); - EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0); + EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625); EXPECT_FLOAT_EQ(boxes[0].rect.right(), 519.44922); EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59);