• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
14 namespace skia {
15 namespace textlayout {
16 
17 class ParagraphBuilder {
18 public:
ParagraphBuilder(const ParagraphStyle &,sk_sp<FontCollection>)19     ParagraphBuilder(const ParagraphStyle&, sk_sp<FontCollection>) { }
20 
21     virtual ~ParagraphBuilder() = default;
22 
23     // Push a style to the stack. The corresponding text added with AddText will
24     // use the top-most style.
25     virtual void pushStyle(const TextStyle& style) = 0;
26 
27     // Remove a style from the stack. Useful to apply different styles to chunks
28     // of text such as bolding.
29     // Example:
30     //   builder.PushStyle(normal_style);
31     //   builder.AddText("Hello this is normal. ");
32     //
33     //   builder.PushStyle(bold_style);
34     //   builder.AddText("And this is BOLD. ");
35     //
36     //   builder.Pop();
37     //   builder.AddText(" Back to normal again.");
38     virtual void pop() = 0;
39 
40     virtual TextStyle peekStyle() = 0;
41 
42     // Adds text to the builder. Forms the proper runs to use the upper-most style
43     // on the style_stack
44     virtual void addText(const std::u16string& text) = 0;
45 
46     // Adds text to the builder, using the top-most style on on the style_stack.
47     virtual void addText(const char* text) = 0; // Don't use this one - going away soon
48     virtual void addText(const char* text, size_t len) = 0;
49 
50     // Pushes the information required to leave an open space, where Flutter may
51     // draw a custom placeholder into.
52     // Internally, this method adds a single object replacement character (0xFFFC)
53     virtual void addPlaceholder(const PlaceholderStyle& placeholderStyle) = 0;
54 
55     virtual void setParagraphStyle(const ParagraphStyle& style) = 0;
56 
57     // Constructs a SkParagraph object that can be used to layout and paint the text to a SkCanvas.
58     virtual std::unique_ptr<Paragraph> Build() = 0;
59 
60 
61     // Just until we fix all the google3 code
62     static std::unique_ptr<ParagraphBuilder> make(const ParagraphStyle& style,
63                                                   sk_sp<FontCollection> fontCollection);
64 };
65 }  // namespace textlayout
66 }  // namespace skia
67 
68 #endif  // ParagraphBuilder_DEFINED
69