Skip to content

Commit

Permalink
libtxt: fixes to text style inheritance (flutter#4466)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jason-simmons authored Dec 16, 2017
1 parent fe07caf commit 0182ed9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
28 changes: 10 additions & 18 deletions third_party/txt/src/txt/paragraph_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions third_party/txt/src/txt/paragraph_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class ParagraphBuilder {
std::shared_ptr<FontCollection> font_collection_;
StyledRuns runs_;
ParagraphStyle paragraph_style_;
size_t paragraph_style_index_;

size_t PeekStyleIndex() const;

FXL_DISALLOW_COPY_AND_ASSIGN(ParagraphBuilder);
};
Expand Down
5 changes: 3 additions & 2 deletions third_party/txt/src/txt/styled_runs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}

Expand Down
3 changes: 1 addition & 2 deletions third_party/txt/src/txt/styled_runs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 0182ed9

Please sign in to comment.