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