• 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 & style,sk_sp<FontCollection> fontCollection)19     ParagraphBuilder(const ParagraphStyle& style, sk_sp<FontCollection> 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     // Converts to u16string before adding.
47     virtual void addText(const char* text) = 0;
48 
49     virtual void setParagraphStyle(const ParagraphStyle& style) = 0;
50 
51     // Constructs a SkParagraph object that can be used to layout and paint the text to a SkCanvas.
52     virtual std::unique_ptr<Paragraph> Build() = 0;
53 
54     static std::unique_ptr<ParagraphBuilder> make(const ParagraphStyle& style,
55                                                   sk_sp<FontCollection> fontCollection);
56 };
57 }  // namespace textlayout
58 }  // namespace skia
59 
60 #endif  // ParagraphBuilder_DEFINED
61