1 // Copyright 2019 Google LLC. 2 #ifndef ParagraphBuilderImpl_DEFINED 3 #define ParagraphBuilderImpl_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/ParagraphBuilder.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 ParagraphBuilderImpl : public ParagraphBuilder { 20 public: 21 ParagraphBuilderImpl(const ParagraphStyle& style, 22 sk_sp<FontCollection> fontCollection, 23 std::unique_ptr<SkUnicode> unicode); 24 25 // Just until we fix all the code; calls icu::make inside 26 ParagraphBuilderImpl(const ParagraphStyle& style, sk_sp<FontCollection> fontCollection); 27 28 ~ParagraphBuilderImpl() override; 29 30 // Push a style to the stack. The corresponding text added with AddText will 31 // use the top-most style. 32 void pushStyle(const TextStyle& style) override; 33 34 // Remove a style from the stack. Useful to apply different styles to chunks 35 // of text such as bolding. 36 // Example: 37 // builder.PushStyle(normal_style); 38 // builder.AddText("Hello this is normal. "); 39 // 40 // builder.PushStyle(bold_style); 41 // builder.AddText("And this is BOLD. "); 42 // 43 // builder.Pop(); 44 // builder.AddText(" Back to normal again."); 45 void pop() override; 46 47 TextStyle peekStyle() override; 48 49 // Adds text to the builder. Forms the proper runs to use the upper-most style 50 // on the style_stack. 51 void addText(const std::u16string& text) override; 52 53 // Adds text to the builder, using the top-most style on on the style_stack. 54 void addText(const char* text) override; // Don't use this one - going away soon 55 void addText(const char* text, size_t len) override; 56 57 void addPlaceholder(const PlaceholderStyle& placeholderStyle) override; 58 59 void setParagraphStyle(const ParagraphStyle& style) override; 60 61 // Constructs a SkParagraph object that can be used to layout and paint the text to a SkCanvas. 62 std::unique_ptr<Paragraph> Build() override; 63 64 static std::unique_ptr<ParagraphBuilder> make(const ParagraphStyle& style, 65 sk_sp<FontCollection> fontCollection, 66 std::unique_ptr<SkUnicode> unicode); 67 68 // Just until we fix all the code; calls icu::make inside 69 static std::unique_ptr<ParagraphBuilder> make(const ParagraphStyle& style, 70 sk_sp<FontCollection> fontCollection); 71 private: 72 void endRunIfNeeded(); 73 void addPlaceholder(const PlaceholderStyle& placeholderStyle, bool lastOne); 74 75 SkString fUtf8; 76 std::stack<TextStyle> fTextStyles; 77 SkTArray<Block, true> fStyledBlocks; 78 SkTArray<Placeholder, true> fPlaceholders; 79 sk_sp<FontCollection> fFontCollection; 80 ParagraphStyle fParagraphStyle; 81 82 std::unique_ptr<SkUnicode> fUnicode; 83 }; 84 } // namespace textlayout 85 } // namespace skia 86 87 #endif // ParagraphBuilderImpl_DEFINED 88