| 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 "include/private/base/SkOnce.h" |
| 10 |
#include "include/private/base/SkTArray.h" |
| 11 |
#include "modules/skparagraph/include/FontCollection.h" |
| 12 |
#include "modules/skparagraph/include/Paragraph.h" |
| 13 |
#include "modules/skparagraph/include/ParagraphBuilder.h" |
| 14 |
#include "modules/skparagraph/include/ParagraphStyle.h" |
| 15 |
#include "modules/skparagraph/include/TextStyle.h" |
| 16 |
|
| 17 |
namespace skia { |
| 18 |
namespace textlayout { |
| 19 |
|
| 20 |
class ParagraphBuilderImpl : public ParagraphBuilder { |
| 21 |
public: |
| 22 |
ParagraphBuilderImpl(const ParagraphStyle& style, |
| 23 |
sk_sp<FontCollection> fontCollection, |
| 24 |
sk_sp<SkUnicode> unicode); |
| 25 |
|
| 26 |
~ParagraphBuilderImpl() override; |
| 27 |
|
| 28 |
// Push a style to the stack. The corresponding text added with AddText will |
| 29 |
// use the top-most style. |
| 30 |
void pushStyle(const TextStyle& style) override; |
| 31 |
|
| 32 |
// Remove a style from the stack. Useful to apply different styles to chunks |
| 33 |
// of text such as bolding. |
| 34 |
// Example: |
| 35 |
// builder.PushStyle(normal_style); |
| 36 |
// builder.AddText("Hello this is normal. "); |
| 37 |
// |
| 38 |
// builder.PushStyle(bold_style); |
| 39 |
// builder.AddText("And this is BOLD. "); |
| 40 |
// |
| 41 |
// builder.Pop(); |
| 42 |
// builder.AddText(" Back to normal again."); |
| 43 |
void pop() override; |
| 44 |
|
| 45 |
TextStyle peekStyle() override; |
| 46 |
|
| 47 |
// Adds text to the builder. Forms the proper runs to use the upper-most style |
| 48 |
// on the style_stack. |
| 49 |
void addText(const std::u16string& text) override; |
| 50 |
|
| 51 |
// Adds text to the builder, using the top-most style on on the style_stack. |
| 52 |
void addText(const char* text) override; // Don't use this one - going away soon |
| 53 |
void addText(const char* text, size_t len) override; |
| 54 |
|
| 55 |
void addPlaceholder(const PlaceholderStyle& placeholderStyle) override; |
| 56 |
|
| 57 |
// Constructs a SkParagraph object that can be used to layout and paint the text to a SkCanvas. |
| 58 |
std::unique_ptr<Paragraph> Build() override; |
| 59 |
|
| 60 |
// Support for "Client" unicode |
| 61 |
SkSpan<char> getText() override; |
| 62 |
const ParagraphStyle& getParagraphStyle() const override; |
| 63 |
|
| 64 |
#if !defined(SK_DISABLE_LEGACY_CLIENT_UNICODE) && defined(SK_UNICODE_CLIENT_IMPLEMENTATION) |
| 65 |
void setWordsUtf8(std::vector<SkUnicode::Position> wordsUtf8) override; |
| 66 |
void setWordsUtf16(std::vector<SkUnicode::Position> wordsUtf16) override; |
| 67 |
|
| 68 |
void setGraphemeBreaksUtf8(std::vector<SkUnicode::Position> graphemesUtf8) override; |
| 69 |
void setGraphemeBreaksUtf16(std::vector<SkUnicode::Position> graphemesUtf16) override; |
| 70 |
|
| 71 |
void setLineBreaksUtf8(std::vector<SkUnicode::LineBreakBefore> lineBreaksUtf8) override; |
| 72 |
void setLineBreaksUtf16(std::vector<SkUnicode::LineBreakBefore> lineBreaksUtf16) override; |
| 73 |
|
| 74 |
std::tuple<std::vector<SkUnicode::Position>, |
| 75 |
std::vector<SkUnicode::Position>, |
| 76 |
std::vector<SkUnicode::LineBreakBefore>> |
| 77 |
getClientICUData() const override { |
| 78 |
return { fWordsUtf16, fGraphemeBreaksUtf8, fLineBreaksUtf8 }; |
| 79 |
} |
| 80 |
|
| 81 |
void SetUnicode(sk_sp<SkUnicode> unicode) override { |
| 82 |
fUnicode = std::move(unicode); |
| 83 |
} |
| 84 |
#endif |
| 85 |
// Support for Flutter optimization |
| 86 |
void Reset() override; |
| 87 |
|
| 88 |
static std::unique_ptr<ParagraphBuilder> make(const ParagraphStyle& style, |
| 89 |
sk_sp<FontCollection> fontCollection, |
| 90 |
sk_sp<SkUnicode> unicode); |
| 91 |
|
| 92 |
|
| 93 |
#if !defined(SK_DISABLE_LEGACY_PARAGRAPH_UNICODE) |
| 94 |
// Just until we fix all the code; calls icu::make inside |
| 95 |
static std::unique_ptr<ParagraphBuilder> make(const ParagraphStyle& style, |
| 96 |
sk_sp<FontCollection> fontCollection); |
| 97 |
#endif |
| 98 |
|
| 99 |
static bool RequiresClientICU(); |
| 100 |
protected: |
| 101 |
void startStyledBlock(); |
| 102 |
void endRunIfNeeded(); |
| 103 |
const TextStyle& internalPeekStyle(); |
| 104 |
void addPlaceholder(const PlaceholderStyle& placeholderStyle, bool lastOne); |
| 105 |
void finalize(); |
| 106 |
|
| 107 |
SkString fUtf8; |
| 108 |
skia_private::STArray<4, TextStyle, true> fTextStyles; |
| 109 |
skia_private::STArray<4, Block, true> fStyledBlocks; |
| 110 |
skia_private::STArray<4, Placeholder, true> fPlaceholders; |
| 111 |
sk_sp<FontCollection> fFontCollection; |
| 112 |
ParagraphStyle fParagraphStyle; |
| 113 |
|
| 114 |
sk_sp<SkUnicode> fUnicode; |
| 115 |
private: |
| 116 |
SkOnce fillUTF16MappingOnce; |
| 117 |
void ensureUTF16Mapping(); |
| 118 |
skia_private::TArray<TextIndex, true> fUTF8IndexForUTF16Index; |
| 119 |
skia_private::TArray<TextIndex, true> fUTF16IndexForUTF8Index; |
| 120 |
#if defined(SK_UNICODE_CLIENT_IMPLEMENTATION) |
| 121 |
bool fTextIsFinalized; |
| 122 |
bool fUsingClientInfo; |
| 123 |
std::vector<SkUnicode::Position> fWordsUtf16; |
| 124 |
std::vector<SkUnicode::Position> fGraphemeBreaksUtf8; |
| 125 |
std::vector<SkUnicode::LineBreakBefore> fLineBreaksUtf8; |
| 126 |
#endif |
| 127 |
}; |
| 128 |
} // namespace textlayout |
| 129 |
} // namespace skia |
| 130 |
|
| 131 |
#endif // ParagraphBuilderImpl_DEFINED |