From 4868e24e0e35d0052e6216af46842dd2fe3cc488 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Fri, 4 Aug 2017 16:46:24 -0700 Subject: [PATCH] Separate handling of letterspace/advance to fix multi-run text jitter, temp disable ligatures. Change-Id: Ia33b105fe8dc968cdcb365501842b4595b6e7922 --- engine/src/flutter/src/paragraph.cc | 39 +++++++++++++------ engine/src/flutter/src/paragraph.h | 2 + .../flutter/tests/txt/paragraph_unittests.cc | 30 +++++++------- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index 7c5fc79934..606855cd6f 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -109,6 +109,17 @@ void GetFontAndMinikinPaint(const TextStyle& style, // also produces more accurate layouts. paint->paintFlags |= style.round_char_advances ? 0x00 : minikin::LinearTextFlag; + // Disable ligatures + // TODO(garyq): Re-enable ligatures. + std::vector features; + hb_feature_t no_liga = {HB_TAG('l', 'i', 'g', 'a'), 0, 0, ~0u}; + hb_feature_t no_clig = {HB_TAG('c', 'l', 'i', 'g'), 0, 0, ~0u}; + char buff[HB_FEATURE_BUFFER_SIZE * 2]; + hb_feature_to_string(&no_liga, buff, HB_FEATURE_BUFFER_SIZE); + hb_feature_to_string(&no_clig, buff + HB_FEATURE_BUFFER_SIZE, + HB_FEATURE_BUFFER_SIZE); + paint->fontFeatureSettings = + std::string(buff) + "," + std::string(buff + HB_FEATURE_BUFFER_SIZE); } void GetPaint(const TextStyle& style, SkPaint* paint) { @@ -236,6 +247,7 @@ void Paragraph::Layout(double width, bool force) { double justify_spacing = 0.0f; double prev_word_pos = 0.0f; double prev_char_advance = 0.0f; + double current_x_position = previous_run_x_position; std::vector buffers; std::vector buffer_sizes; @@ -319,7 +331,7 @@ void Paragraph::Layout(double width, bool force) { const size_t layout_end = std::min(run.end, next_break); bool bidiFlags = paragraph_style_.rtl; - // NOTE: Minikin Layout doLayout() has an O(N^2) (according to + // Minikin Layout doLayout() has an O(N^2) (according to // benchmarks) time complexity where N is the total number of characters. // However, this is not significant for reasonably sized paragraphs. It is // currently recommended to break up very long paragraphs (10k+ @@ -339,8 +351,7 @@ void Paragraph::Layout(double width, bool force) { buffer_sizes = std::vector(); word_count = 0; double temp_line_spacing = 0; - double current_x_position = previous_run_x_position; - double run_width = 0.0f; + current_x_position = 0; while (blob_start < glyph_count) { const size_t blob_length = GetBlobLength(layout, blob_start); buffer_sizes.push_back(blob_length); @@ -356,6 +367,7 @@ void Paragraph::Layout(double width, bool force) { layout_end == next_break) { ++trailing_length; } + trailing_length = 0; buffers.push_back( &builder.allocRunPos(paint, blob_length - trailing_length)); @@ -363,19 +375,22 @@ void Paragraph::Layout(double width, bool force) { // TODO(garyq): Implement RTL. // Each Glyph/Letter. bool whitespace_ended = true; + float letter_spacing = 0; for (size_t blob_index = 0; blob_index < blob_length - trailing_length; ++blob_index) { const size_t glyph_index = blob_start + blob_index; buffers.back()->glyphs[blob_index] = layout.getGlyphId(glyph_index); const size_t pos_index = 2 * blob_index; - - current_x_position = layout.getX(glyph_index); - buffers.back()->pos[pos_index] = current_x_position; - glyph_single_line_position_x.push_back(current_x_position + - previous_run_x_position); + letter_spacing = run.style.letter_spacing == 0 + ? 0 + : layout.getX(glyph_index) - current_x_position; + buffers.back()->pos[pos_index] = current_x_position + letter_spacing; + glyph_single_line_position_x.push_back( + current_x_position + previous_run_x_position + letter_spacing); buffers.back()->pos[pos_index + 1] = layout.getY(glyph_index); + current_x_position += layout.getCharAdvance(glyph_index); // Check if the current Glyph is a whitespace and handle multiple // whitespaces in a row. if (whitespace_set_.count(layout.getGlyphId(glyph_index)) > 0) { @@ -390,10 +405,9 @@ void Paragraph::Layout(double width, bool force) { } prev_char_advance = layout.getCharAdvance(glyph_index); - run_width += layout.getCharAdvance(glyph_index); } blob_start += blob_length; - previous_run_x_position += current_x_position + prev_char_advance; + previous_run_x_position += current_x_position + letter_spacing; } // TODO(abarth): We could keep the same SkTextBlobBuilder as long as the @@ -405,8 +419,8 @@ void Paragraph::Layout(double width, bool force) { buffer_sizes.size() > 0) { JustifyLine(buffers, buffer_sizes, word_count, justify_spacing); } - records_.push_back( - PaintRecord{run.style, builder.make(), metrics, lines_, run_width}); + records_.push_back(PaintRecord{run.style, builder.make(), metrics, lines_, + layout.getAdvance()}); line_width += std::abs(records_[records_.size() - 1].text()->bounds().fRight + records_[records_.size() - 1].text()->bounds().fLeft); @@ -452,6 +466,7 @@ void Paragraph::Layout(double width, bool force) { prev_word_pos = 0; prev_char_advance = 0.0f; previous_run_x_position = 0.0f; + current_x_position = 0.0f; line_width = 0.0f; break_index += 1; lines_++; diff --git a/engine/src/flutter/src/paragraph.h b/engine/src/flutter/src/paragraph.h index 3aa6265ce6..3208c65d8c 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -31,6 +31,8 @@ #include "third_party/skia/include/core/SkRect.h" #include "third_party/skia/include/core/SkTextBlob.h" +#define HB_FEATURE_BUFFER_SIZE 128 + class SkCanvas; namespace txt { diff --git a/engine/src/flutter/tests/txt/paragraph_unittests.cc b/engine/src/flutter/tests/txt/paragraph_unittests.cc index a818c71514..8c45dbbd02 100644 --- a/engine/src/flutter/tests/txt/paragraph_unittests.cc +++ b/engine/src/flutter/tests/txt/paragraph_unittests.cc @@ -993,17 +993,17 @@ TEST_F(RenderTest, GetRectsForRangeParagraph) { EXPECT_EQ(rects.size(), 3ull); EXPECT_FLOAT_EQ(rects[0].left(), 56.835938); EXPECT_FLOAT_EQ(rects[0].top(), 0); - EXPECT_FLOAT_EQ(rects[0].right(), 153); + EXPECT_FLOAT_EQ(rects[0].right(), 153.07422); EXPECT_FLOAT_EQ(rects[0].bottom(), 58.59375); - EXPECT_FLOAT_EQ(rects[1].left(), 153); + EXPECT_FLOAT_EQ(rects[1].left(), 153.07422); EXPECT_FLOAT_EQ(rects[1].top(), 0); - EXPECT_FLOAT_EQ(rects[1].right(), 165); + EXPECT_FLOAT_EQ(rects[1].right(), 165.52344); EXPECT_FLOAT_EQ(rects[1].bottom(), 58.59375); - EXPECT_FLOAT_EQ(rects[2].left(), 165); + EXPECT_FLOAT_EQ(rects[2].left(), 165.52344); EXPECT_FLOAT_EQ(rects[2].top(), 0); - EXPECT_FLOAT_EQ(rects[2].right(), 177); + EXPECT_FLOAT_EQ(rects[2].right(), 177.97266); EXPECT_FLOAT_EQ(rects[2].bottom(), 58.59375); paint.setColor(SK_ColorGREEN); @@ -1012,19 +1012,19 @@ TEST_F(RenderTest, GetRectsForRangeParagraph) { GetCanvas()->drawRect(rects[i], paint); } EXPECT_EQ(rects.size(), 3ull); - EXPECT_FLOAT_EQ(rects[0].left(), 177); + EXPECT_FLOAT_EQ(rects[0].left(), 177.97266); EXPECT_FLOAT_EQ(rects[0].top(), 0); - EXPECT_FLOAT_EQ(rects[0].right(), 352); + EXPECT_FLOAT_EQ(rects[0].right(), 352.48438); EXPECT_FLOAT_EQ(rects[0].bottom(), 58.59375); - EXPECT_FLOAT_EQ(rects[1].left(), 352); + EXPECT_FLOAT_EQ(rects[1].left(), 352.48438); EXPECT_FLOAT_EQ(rects[1].top(), 0); - EXPECT_FLOAT_EQ(rects[1].right(), 364); + EXPECT_FLOAT_EQ(rects[1].right(), 364.93359); EXPECT_FLOAT_EQ(rects[1].bottom(), 58.59375); - EXPECT_FLOAT_EQ(rects[2].left(), 364); + EXPECT_FLOAT_EQ(rects[2].left(), 364.93359); EXPECT_FLOAT_EQ(rects[2].top(), 0); - EXPECT_FLOAT_EQ(rects[2].right(), 507); + EXPECT_FLOAT_EQ(rects[2].right(), 507.02344); EXPECT_FLOAT_EQ(rects[2].bottom(), 58.59375); paint.setColor(SK_ColorRED); @@ -1033,9 +1033,9 @@ TEST_F(RenderTest, GetRectsForRangeParagraph) { GetCanvas()->drawRect(rects[i], paint); } EXPECT_EQ(rects.size(), 17ull); - EXPECT_FLOAT_EQ(rects[0].left(), 210.83594); + EXPECT_FLOAT_EQ(rects[0].left(), 211.375); EXPECT_FLOAT_EQ(rects[0].top(), 58.59375); - EXPECT_FLOAT_EQ(rects[0].right(), 296); + EXPECT_FLOAT_EQ(rects[0].right(), 296.62891); EXPECT_FLOAT_EQ(rects[0].bottom(), 117.1875); // TODO(garyq): The following set of vals are definetly wrong and @@ -1051,9 +1051,9 @@ TEST_F(RenderTest, GetRectsForRangeParagraph) { GetCanvas()->drawRect(rects[i], paint); } EXPECT_EQ(rects.size(), 2ull); - EXPECT_FLOAT_EQ(rects[1].left(), 507); + EXPECT_FLOAT_EQ(rects[1].left(), 507.02344); EXPECT_FLOAT_EQ(rects[1].top(), 0); - EXPECT_FLOAT_EQ(rects[1].right(), 519.44922); + EXPECT_FLOAT_EQ(rects[1].right(), 519.47266); EXPECT_FLOAT_EQ(rects[1].bottom(), 58.59375); paint.setColor(SK_ColorRED);