• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const 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     // Adds text to the builder, using the top-most style on on the style_stack.
48     void addText(const char* text) override; // Don't use this one - going away soon
49     void addText(const char* text, size_t len) override;
50 
51     void addPlaceholder(const PlaceholderStyle& placeholderStyle) override;
52 
53     void setParagraphStyle(const ParagraphStyle& style) override;
54 
55     // Constructs a SkParagraph object that can be used to layout and paint the text to a SkCanvas.
56     std::unique_ptr<Paragraph> Build() override;
57 
58 private:
59     void endRunIfNeeded();
60     void addPlaceholder(const PlaceholderStyle& placeholderStyle, bool lastOne);
61 
62     SkString fUtf8;
63     std::stack<TextStyle> fTextStyles;
64     SkTArray<Block, true> fStyledBlocks;
65     SkTArray<Placeholder, true> fPlaceholders;
66     sk_sp<FontCollection> fFontCollection;
67     ParagraphStyle fParagraphStyle;
68 };
69 }  // namespace textlayout
70 }  // namespace skia
71 
72 #endif  // ParagraphBuilderImpl_DEFINED
73