• 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 #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     void Reset() override;
65 
66     static std::unique_ptr<ParagraphBuilder> make(const ParagraphStyle& style,
67                                                   sk_sp<FontCollection> fontCollection,
68                                                   std::unique_ptr<SkUnicode> unicode);
69 
70     // Just until we fix all the code; calls icu::make inside
71     static std::unique_ptr<ParagraphBuilder> make(const ParagraphStyle& style,
72                                                   sk_sp<FontCollection> fontCollection);
73 private:
74     void endRunIfNeeded();
75     void addPlaceholder(const PlaceholderStyle& placeholderStyle, bool lastOne);
76 
77     SkString fUtf8;
78     std::stack<TextStyle> fTextStyles;
79     SkTArray<Block, true> fStyledBlocks;
80     SkTArray<Placeholder, true> fPlaceholders;
81     sk_sp<FontCollection> fFontCollection;
82     ParagraphStyle fParagraphStyle;
83 
84     std::shared_ptr<SkUnicode> fUnicode;
85 };
86 }  // namespace textlayout
87 }  // namespace skia
88 
89 #endif  // ParagraphBuilderImpl_DEFINED
90