Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 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 | |
| 15 | namespace skia { |
| 16 | namespace textlayout { |
| 17 | |
| 18 | class ParagraphBuilderImpl : public ParagraphBuilder { |
| 19 | public: |
| 20 | ParagraphBuilderImpl(ParagraphStyle style, sk_sp<FontCollection> fontCollection); |
| 21 | |
| 22 | ~ParagraphBuilderImpl() override; |
| 23 | |
| 24 | // Push a style to the stack. The corresponding text added with AddText will |
| 25 | // use the top-most style. |
| 26 | void pushStyle(const TextStyle& style) override; |
| 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 | void pop() override; |
| 40 | |
| 41 | TextStyle peekStyle() override; |
| 42 | |
| 43 | // Adds text to the builder. Forms the proper runs to use the upper-most style |
| 44 | // on the style_stack_; |
| 45 | void addText(const std::u16string& text) override; |
| 46 | |
| 47 | // Converts to u16string before adding. |
| 48 | void addText(const char* text) override; |
| 49 | |
| 50 | void setParagraphStyle(const ParagraphStyle& style) override; |
| 51 | |
| 52 | // Constructs a SkParagraph object that can be used to layout and paint the text to a SkCanvas. |
| 53 | std::unique_ptr<Paragraph> Build() override; |
| 54 | |
| 55 | private: |
| 56 | void endRunIfNeeded(); |
| 57 | |
| 58 | SkString fUtf8; |
| 59 | std::stack<TextStyle> fTextStyles; |
| 60 | std::vector<Block> fStyledBlocks; |
| 61 | sk_sp<FontCollection> fFontCollection; |
| 62 | ParagraphStyle fParagraphStyle; |
| 63 | }; |
| 64 | } // namespace textlayout |
| 65 | } // namespace skia |
| 66 | |
| 67 | #endif // ParagraphBuilderImpl_DEFINED |