Flutter Engine
paragraph_builder_txt.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "paragraph_builder_txt.h"
18 
19 #include "paragraph_txt.h"
20 
21 namespace txt {
22 
24  const ParagraphStyle& style,
25  std::shared_ptr<FontCollection> font_collection)
26  : font_collection_(std::move(font_collection)) {
27  SetParagraphStyle(style);
28 }
29 
31 
32 void ParagraphBuilderTxt::SetParagraphStyle(const ParagraphStyle& style) {
33  paragraph_style_ = style;
34  paragraph_style_index_ = runs_.AddStyle(style.GetTextStyle());
35  runs_.StartRun(paragraph_style_index_, text_.size());
36 }
37 
39  size_t style_index = runs_.AddStyle(style);
40  style_stack_.push_back(style_index);
41  runs_.StartRun(style_index, text_.size());
42 }
43 
45  if (style_stack_.empty()) {
46  return;
47  }
48  style_stack_.pop_back();
49  runs_.StartRun(PeekStyleIndex(), text_.size());
50 }
51 
52 size_t ParagraphBuilderTxt::PeekStyleIndex() const {
53  return style_stack_.size() ? style_stack_.back() : paragraph_style_index_;
54 }
55 
57  return runs_.GetStyle(PeekStyleIndex());
58 }
59 
60 void ParagraphBuilderTxt::AddText(const std::u16string& text) {
61  text_.insert(text_.end(), text.begin(), text.end());
62 }
63 
65  obj_replacement_char_indexes_.insert(text_.size());
66  runs_.StartRun(PeekStyleIndex(), text_.size());
67  AddText(std::u16string(1ull, objReplacementChar));
68  runs_.StartRun(PeekStyleIndex(), text_.size());
69  inline_placeholders_.push_back(span);
70 }
71 
72 std::unique_ptr<Paragraph> ParagraphBuilderTxt::Build() {
73  runs_.EndRunIfNeeded(text_.size());
74 
75  std::unique_ptr<ParagraphTxt> paragraph = std::make_unique<ParagraphTxt>();
76  paragraph->SetText(std::move(text_), std::move(runs_));
77  paragraph->SetInlinePlaceholders(std::move(inline_placeholders_),
78  std::move(obj_replacement_char_indexes_));
79  paragraph->SetParagraphStyle(paragraph_style_);
80  paragraph->SetFontCollection(font_collection_);
81  SetParagraphStyle(paragraph_style_);
82  return paragraph;
83 }
84 
85 } // namespace txt
size_t AddStyle(const TextStyle &style)
Definition: styled_runs.cc:44
ParagraphBuilderTxt(const ParagraphStyle &style, std::shared_ptr< FontCollection > font_collection)
void StartRun(size_t style_index, size_t start)
Definition: styled_runs.cc:54
TextStyle GetTextStyle() const
Definition: ref_ptr.h:252
virtual void Pop() override
virtual void AddPlaceholder(PlaceholderRun &span) override
void EndRunIfNeeded(size_t end)
Definition: styled_runs.cc:59
const TextStyle & GetStyle(size_t style_index) const
Definition: styled_runs.cc:50
virtual const TextStyle & PeekStyle() override
const int objReplacementChar
Definition: paragraph_txt.h:48
virtual void AddText(const std::u16string &text) override
virtual void PushStyle(const TextStyle &style) override
virtual std::unique_ptr< Paragraph > Build() override