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 #include "paragraph_txt.h"
19
20 namespace txt {
21
ParagraphBuilderTxt(const ParagraphStyle & style,std::shared_ptr<FontCollection> font_collection)22 ParagraphBuilderTxt::ParagraphBuilderTxt(
23 const ParagraphStyle& style,
24 std::shared_ptr<FontCollection> font_collection)
25 : font_collection_(std::move(font_collection)) {
26 SetParagraphStyle(style);
27 }
28
29 ParagraphBuilderTxt::~ParagraphBuilderTxt() = default;
30
SetParagraphStyle(const ParagraphStyle & style)31 void ParagraphBuilderTxt::SetParagraphStyle(const ParagraphStyle& style) {
32 paragraph_style_ = style;
33 paragraph_style_index_ = runs_.AddStyle(style.GetTextStyle());
34 runs_.StartRun(paragraph_style_index_, text_.size());
35 }
36
PushStyle(const TextStyle & style)37 void ParagraphBuilderTxt::PushStyle(const TextStyle& style) {
38 size_t style_index = runs_.AddStyle(style);
39 style_stack_.push_back(style_index);
40 runs_.StartRun(style_index, text_.size());
41 }
42
Pop()43 void ParagraphBuilderTxt::Pop() {
44 if (style_stack_.empty()) {
45 return;
46 }
47 style_stack_.pop_back();
48 runs_.StartRun(PeekStyleIndex(), text_.size());
49 }
50
PeekStyleIndex() const51 size_t ParagraphBuilderTxt::PeekStyleIndex() const {
52 return style_stack_.size() ? style_stack_.back() : paragraph_style_index_;
53 }
54
PeekStyle()55 const TextStyle& ParagraphBuilderTxt::PeekStyle() {
56 return runs_.GetStyle(PeekStyleIndex());
57 }
58
AddText(const std::u16string & text)59 void ParagraphBuilderTxt::AddText(const std::u16string& text) {
60 text_.insert(text_.end(), text.begin(), text.end());
61 }
62
AddPlaceholder(PlaceholderRun & span)63 void ParagraphBuilderTxt::AddPlaceholder(PlaceholderRun& span) {
64 obj_replacement_char_indexes_.insert(text_.size());
65 runs_.StartRun(PeekStyleIndex(), text_.size());
66 AddText(std::u16string(1ull, objReplacementChar));
67 runs_.StartRun(PeekStyleIndex(), text_.size());
68 inline_placeholders_.push_back(span);
69 }
70
Build()71 std::unique_ptr<Paragraph> ParagraphBuilderTxt::Build() {
72 runs_.EndRunIfNeeded(text_.size());
73
74 std::unique_ptr<ParagraphTxt> paragraph = std::make_unique<ParagraphTxt>();
75 paragraph->SetText(std::move(text_), std::move(runs_));
76 paragraph->SetInlinePlaceholders(std::move(inline_placeholders_),
77 std::move(obj_replacement_char_indexes_));
78 paragraph->SetParagraphStyle(paragraph_style_);
79 paragraph->SetFontCollection(font_collection_);
80 SetParagraphStyle(paragraph_style_);
81 return paragraph;
82 }
83
84 } // namespace txt
85