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
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -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});
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user