From 50183917b0b00ebb1654e4168c9f772d1be76309 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Fri, 15 Dec 2017 16:21:40 -0800 Subject: [PATCH] libtxt: fixes to text style inheritance (flutter/engine#4466) * newly pushed styles should inherit from the top of the paragraph's style stack, not the most recently added style in StyledRuns * make the paragraph-level style a default that is not pushed onto the stack and can not be popped --- .../txt/src/txt/paragraph_builder.cc | 28 +++++++------------ .../txt/src/txt/paragraph_builder.h | 3 ++ .../third_party/txt/src/txt/styled_runs.cc | 5 ++-- .../third_party/txt/src/txt/styled_runs.h | 3 +- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/engine/src/flutter/third_party/txt/src/txt/paragraph_builder.cc b/engine/src/flutter/third_party/txt/src/txt/paragraph_builder.cc index c8a2fa1ed9..24be160d4a 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paragraph_builder.cc +++ b/engine/src/flutter/third_party/txt/src/txt/paragraph_builder.cc @@ -34,37 +34,29 @@ ParagraphBuilder::~ParagraphBuilder() = default; void ParagraphBuilder::SetParagraphStyle(const ParagraphStyle& style) { paragraph_style_ = style; - // Keep a default style to fall back to. - TextStyle text_style; - text_style.font_weight = paragraph_style_.font_weight; - text_style.font_style = paragraph_style_.font_style; - text_style.font_family = paragraph_style_.font_family; - text_style.font_size = paragraph_style_.font_size; - PushStyle(text_style); + paragraph_style_index_ = runs_.AddStyle(style.GetTextStyle()); + runs_.StartRun(paragraph_style_index_, text_.size()); } void ParagraphBuilder::PushStyle(const TextStyle& style) { - const size_t text_index = text_.size(); - runs_.EndRunIfNeeded(text_index); - const size_t style_index = runs_.AddStyle(style); - runs_.StartRun(style_index, text_index); + size_t style_index = runs_.AddStyle(style); style_stack_.push_back(style_index); + runs_.StartRun(style_index, text_.size()); } void ParagraphBuilder::Pop() { if (style_stack_.empty()) return; - const size_t text_index = text_.size(); - runs_.EndRunIfNeeded(text_index); style_stack_.pop_back(); - if (style_stack_.empty()) - return; - const size_t style_index = style_stack_.back(); - runs_.StartRun(style_index, text_index); + runs_.StartRun(PeekStyleIndex(), text_.size()); +} + +size_t ParagraphBuilder::PeekStyleIndex() const { + return style_stack_.size() ? style_stack_.back() : paragraph_style_index_; } const TextStyle& ParagraphBuilder::PeekStyle() const { - return runs_.PeekStyle(); + return runs_.GetStyle(PeekStyleIndex()); } void ParagraphBuilder::AddText(const std::u16string& text) { diff --git a/engine/src/flutter/third_party/txt/src/txt/paragraph_builder.h b/engine/src/flutter/third_party/txt/src/txt/paragraph_builder.h index f89c0c5ef4..3b834e5652 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paragraph_builder.h +++ b/engine/src/flutter/third_party/txt/src/txt/paragraph_builder.h @@ -78,6 +78,9 @@ class ParagraphBuilder { std::shared_ptr font_collection_; StyledRuns runs_; ParagraphStyle paragraph_style_; + size_t paragraph_style_index_; + + size_t PeekStyleIndex() const; FXL_DISALLOW_COPY_AND_ASSIGN(ParagraphBuilder); }; diff --git a/engine/src/flutter/third_party/txt/src/txt/styled_runs.cc b/engine/src/flutter/third_party/txt/src/txt/styled_runs.cc index 966817d088..ad76ccb110 100644 --- a/engine/src/flutter/third_party/txt/src/txt/styled_runs.cc +++ b/engine/src/flutter/third_party/txt/src/txt/styled_runs.cc @@ -47,11 +47,12 @@ size_t StyledRuns::AddStyle(const TextStyle& style) { return style_index; } -const TextStyle& StyledRuns::PeekStyle() const { - return styles_.back(); +const TextStyle& StyledRuns::GetStyle(size_t style_index) const { + return styles_[style_index]; } void StyledRuns::StartRun(size_t style_index, size_t start) { + EndRunIfNeeded(start); runs_.push_back(IndexedRun{style_index, start, start}); } diff --git a/engine/src/flutter/third_party/txt/src/txt/styled_runs.h b/engine/src/flutter/third_party/txt/src/txt/styled_runs.h index 0eb4b41d7f..62ad9b10ef 100644 --- a/engine/src/flutter/third_party/txt/src/txt/styled_runs.h +++ b/engine/src/flutter/third_party/txt/src/txt/styled_runs.h @@ -50,8 +50,7 @@ class StyledRuns { size_t AddStyle(const TextStyle& style); - // Returns the last TextStyle on the stack. - const TextStyle& PeekStyle() const; + const TextStyle& GetStyle(size_t style_index) const; void StartRun(size_t style_index, size_t start);