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