1// Copyright 2019 Google LLC.
2#ifndef ParagraphBuilder_DEFINED
3#define ParagraphBuilder_DEFINED
4
5#include <memory>
6#include <stack>
7#include <string>
8#include <tuple>
9#include "modules/skparagraph/include/FontCollection.h"
10#include "modules/skparagraph/include/Paragraph.h"
11#ifdef OHOS_SUPPORT
12#include "modules/skparagraph/include/ParagraphLineFetcher.h"
13#endif
14#include "modules/skparagraph/include/ParagraphStyle.h"
15#include "modules/skparagraph/include/TextStyle.h"
16#include "modules/skunicode/include/SkUnicode.h"
17
18namespace skia {
19namespace textlayout {
20
21class ParagraphBuilder {
22public:
23    ParagraphBuilder(const ParagraphStyle&, sk_sp<FontCollection>) { }
24
25    virtual ~ParagraphBuilder() = default;
26
27    // Push a style to the stack. The corresponding text added with AddText will
28    // use the top-most style.
29    virtual void pushStyle(const TextStyle& style) = 0;
30
31    // Remove a style from the stack. Useful to apply different styles to chunks
32    // of text such as bolding.
33    // Example:
34    //   builder.PushStyle(normal_style);
35    //   builder.AddText("Hello this is normal. ");
36    //
37    //   builder.PushStyle(bold_style);
38    //   builder.AddText("And this is BOLD. ");
39    //
40    //   builder.Pop();
41    //   builder.AddText(" Back to normal again.");
42    virtual void pop() = 0;
43
44    virtual TextStyle peekStyle() = 0;
45
46    // Adds UTF16-encoded text to the builder. Forms the proper runs to use the upper-most style
47    // on the style_stack.
48    virtual void addText(const std::u16string& text) = 0;
49
50    // Adds UTF8-encoded text to the builder, using the top-most style on the style_stack.
51    virtual void addText(const char* text) = 0;
52    virtual void addText(const char* text, size_t len) = 0;
53
54    // Pushes the information required to leave an open space, where Flutter may
55    // draw a custom placeholder into.
56    // Internally, this method adds a single object replacement character (0xFFFC)
57    virtual void addPlaceholder(const PlaceholderStyle& placeholderStyle) = 0;
58
59    // Constructs a SkParagraph object that can be used to layout and paint the text to a SkCanvas.
60    virtual std::unique_ptr<Paragraph> Build() = 0;
61
62#ifdef OHOS_SUPPORT
63    virtual std::unique_ptr<ParagraphLineFetcher> buildLineFetcher() = 0;
64#endif
65    virtual SkSpan<char> getText() = 0;
66    virtual const ParagraphStyle& getParagraphStyle() const = 0;
67
68    // Mainly, support for "Client" unicode
69    virtual void setWordsUtf8(std::vector<SkUnicode::Position> wordsUtf8) = 0;
70    virtual void setWordsUtf16(std::vector<SkUnicode::Position> wordsUtf16) = 0;
71
72    virtual void setGraphemeBreaksUtf8(std::vector<SkUnicode::Position> graphemesUtf8) = 0;
73    virtual void setGraphemeBreaksUtf16(std::vector<SkUnicode::Position> graphemesUtf16) = 0;
74
75    virtual void setLineBreaksUtf8(std::vector<SkUnicode::LineBreakBefore> lineBreaksUtf8) = 0;
76    virtual void setLineBreaksUtf16(std::vector<SkUnicode::LineBreakBefore> lineBreaksUtf16) = 0;
77
78    virtual void SetUnicode(std::unique_ptr<SkUnicode> unicode) = 0;
79
80    // Resets this builder to its initial state, discarding any text, styles, placeholders that have
81    // been added, but keeping the initial ParagraphStyle.
82    virtual void Reset() = 0;
83
84    // Just until we fix all the google3 code
85    static std::unique_ptr<ParagraphBuilder> make(const ParagraphStyle& style,
86                                                  sk_sp<FontCollection> fontCollection);
87};
88}  // namespace textlayout
89}  // namespace skia
90
91#endif  // ParagraphBuilder_DEFINED
92