From 99a8a731bb25f66f6019ac03d0c002ea27738ba4 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Thu, 14 Dec 2017 09:43:49 -0800 Subject: [PATCH] libtxt: fix the paragraph level text direction passed to the ICU bidi API (flutter/engine#4455) Also includes some cleanup of the BidiRun struct --- .../third_party/txt/src/txt/paragraph.cc | 22 ++++++++--------- .../third_party/txt/src/txt/paragraph.h | 24 +++++++++---------- 2 files changed, 23 insertions(+), 23 deletions(-) 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 ec554f0ff1..8424fbd755 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paragraph.cc +++ b/engine/src/flutter/third_party/txt/src/txt/paragraph.cc @@ -257,9 +257,7 @@ bool Paragraph::ComputeLineBreaks() { return true; } -bool Paragraph::ComputeBidiRuns() { - bidi_runs_.clear(); - +bool Paragraph::ComputeBidiRuns(std::vector* result) { auto ubidi_closer = [](UBiDi* b) { ubidi_close(b); }; std::unique_ptr bidi(ubidi_open(), ubidi_closer); @@ -267,8 +265,8 @@ bool Paragraph::ComputeBidiRuns() { return false; UBiDiLevel paraLevel = (paragraph_style_.text_direction == TextDirection::rtl) - ? UBIDI_DEFAULT_RTL - : UBIDI_DEFAULT_LTR; + ? UBIDI_RTL + : UBIDI_LTR; UErrorCode status = U_ZERO_ERROR; ubidi_setPara(bidi.get(), reinterpret_cast(text_.data()), text_.size(), paraLevel, nullptr, &status); @@ -326,8 +324,8 @@ bool Paragraph::ComputeBidiRuns() { styled_run_iter--; const StyledRuns::Run& styled_run = styled_run_iter->second; size_t chunk_end = std::min(bidi_run_end, styled_run.end); - bidi_runs_.emplace_back(chunk_start, chunk_end, text_direction, - styled_run.style); + result->emplace_back(chunk_start, chunk_end, text_direction, + styled_run.style); chunk_start = chunk_end; } } @@ -344,10 +342,12 @@ void Paragraph::Layout(double width, bool force) { width_ = width; - if (!ComputeLineBreaks()) { + if (!ComputeLineBreaks()) + return; + + std::vector bidi_runs; + if (!ComputeBidiRuns(&bidi_runs)) return; - } - ComputeBidiRuns(); SkPaint paint; paint.setAntiAlias(true); @@ -389,7 +389,7 @@ void Paragraph::Layout(double width, bool force) { // Find the runs comprising this line. std::vector line_runs; - for (const BidiRun& bidi_run : bidi_runs_) { + for (const BidiRun& bidi_run : bidi_runs) { if (bidi_run.start < line_range.end && bidi_run.end > line_range.start) { line_runs.emplace_back(std::max(bidi_run.start, line_range.start), std::min(bidi_run.end, line_range.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 43b5ae9fe2..438531cef7 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paragraph.h +++ b/engine/src/flutter/third_party/txt/src/txt/paragraph.h @@ -78,16 +78,6 @@ class Paragraph { } }; - struct BidiRun { - BidiRun(size_t s, size_t e, TextDirection d, const TextStyle& st) - : start(s), end(e), direction(d), style(st) {} - size_t start, end; - TextDirection direction; - const TextStyle& style; - - bool is_rtl() const { return direction == TextDirection::rtl; } - }; - // Minikin Layout doLayout() and LineBreaker addStyleRun() 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 @@ -188,7 +178,6 @@ class Paragraph { // Starting data to layout. std::vector text_; StyledRuns runs_; - std::vector bidi_runs_; ParagraphStyle paragraph_style_; std::shared_ptr font_collection_; @@ -210,6 +199,17 @@ class Paragraph { std::vector line_baselines_; bool did_exceed_max_lines_; + struct BidiRun { + BidiRun(size_t s, size_t e, TextDirection d, const TextStyle& st) + : start(s), end(e), direction(d), style(st) {} + + const size_t start, end; + const TextDirection direction; + const TextStyle& style; + + bool is_rtl() const { return direction == TextDirection::rtl; } + }; + struct GlyphPosition { Range code_units; Range x_pos; @@ -284,7 +284,7 @@ class Paragraph { bool ComputeLineBreaks(); // Break the text into runs based on LTR/RTL text direction. - bool ComputeBidiRuns(); + bool ComputeBidiRuns(std::vector* result); // Calculate the starting X offset of a line based on the line's width and // alignment.