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#ifndef LIB_TXT_SRC_PARAGRAPH_BUILDER_H_
18#define LIB_TXT_SRC_PARAGRAPH_BUILDER_H_
19
20#include <memory>
21#include <string>
22
23#include "flutter/fml/macros.h"
24#include "font_collection.h"
25#include "paragraph.h"
26#include "paragraph_style.h"
27#include "placeholder_run.h"
28#include "text_style.h"
29
30namespace txt {
31
32class ParagraphBuilder {
33 public:
34 static std::unique_ptr<ParagraphBuilder> CreateTxtBuilder(
35 const ParagraphStyle& style,
36 std::shared_ptr<FontCollection> font_collection);
37
38#if FLUTTER_ENABLE_SKSHAPER
39 static std::unique_ptr<ParagraphBuilder> CreateSkiaBuilder(
40 const ParagraphStyle& style,
41 std::shared_ptr<FontCollection> font_collection);
42#endif
43
44 virtual ~ParagraphBuilder() = default;
45
46 // Push a style to the stack. The corresponding text added with AddText will
47 // use the top-most style.
48 virtual void PushStyle(const TextStyle& style) = 0;
49
50 // Remove a style from the stack. Useful to apply different styles to chunks
51 // of text such as bolding.
52 // Example:
53 // builder.PushStyle(normal_style);
54 // builder.AddText("Hello this is normal. ");
55 //
56 // builder.PushStyle(bold_style);
57 // builder.AddText("And this is BOLD. ");
58 //
59 // builder.Pop();
60 // builder.AddText(" Back to normal again.");
61 virtual void Pop() = 0;
62
63 // Returns the last TextStyle on the stack.
64 virtual const TextStyle& PeekStyle() = 0;
65
66 // Adds text to the builder. Forms the proper runs to use the upper-most style
67 // on the style_stack_;
68 virtual void AddText(const std::u16string& text) = 0;
69
70 // Pushes the information requried to leave an open space, where Flutter may
71 // draw a custom placeholder into.
72 //
73 // Internally, this method adds a single object replacement character (0xFFFC)
74 // and emplaces a new PlaceholderRun instance to the vector of inline
75 // placeholders.
76 virtual void AddPlaceholder(PlaceholderRun& span) = 0;
77
78 // Constructs a Paragraph object that can be used to layout and paint the text
79 // to a SkCanvas.
80 virtual std::unique_ptr<Paragraph> Build() = 0;
81
82 protected:
83 ParagraphBuilder() = default;
84
85 private:
86 FML_DISALLOW_COPY_AND_ASSIGN(ParagraphBuilder);
87};
88
89} // namespace txt
90
91#endif // LIB_TXT_SRC_PARAGRAPH_BUILDER_H_
92