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