• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Google LLC.
2 #ifndef LineBreaker_DEFINED
3 #define LineBreaker_DEFINED
4 
5 #include <functional>  // std::function
6 #include <queue>
7 #include "include/core/SkSpan.h"
8 #include "modules/skparagraph/include/TextStyle.h"
9 #include "modules/skparagraph/src/ParagraphImpl.h"
10 #include "modules/skparagraph/src/Run.h"
11 
12 namespace skia {
13 namespace textlayout {
14 
15 class ParagraphImpl;
16 class OneLineShaper : public SkShaper::RunHandler {
17 public:
OneLineShaper(ParagraphImpl * paragraph)18     explicit OneLineShaper(ParagraphImpl* paragraph)
19         : fParagraph(paragraph)
20         , fHeight(0.0f)
21         , fUseHalfLeading(false)
22         , fBaselineShift(0.0f)
23         , fAdvance(SkPoint::Make(0.0f, 0.0f))
24         , fUnresolvedGlyphs(0)
25         , fUniqueRunId(paragraph->fRuns.size()){ }
26 
27     bool shape();
28 
unresolvedGlyphs()29     size_t unresolvedGlyphs() { return fUnresolvedGlyphs; }
30 
31 private:
32 
33     struct RunBlock {
RunBlockRunBlock34         RunBlock() : fRun(nullptr) { }
35 
36         // First unresolved block
RunBlockRunBlock37         explicit RunBlock(TextRange text) : fRun(nullptr), fText(text) { }
38 
RunBlockRunBlock39         RunBlock(std::shared_ptr<Run> run, TextRange text, GlyphRange glyphs, size_t score)
40             : fRun(std::move(run))
41             , fText(text)
42             , fGlyphs(glyphs) { }
43 
44         // Entire run comes as one block fully resolved
RunBlockRunBlock45         explicit RunBlock(std::shared_ptr<Run> run)
46             : fRun(std::move(run))
47             , fText(fRun->fTextRange)
48             , fGlyphs(GlyphRange(0, fRun->size())) { }
49 
50         std::shared_ptr<Run> fRun;
51         TextRange fText;
52         GlyphRange fGlyphs;
isFullyResolvedRunBlock53         bool isFullyResolved() { return fRun != nullptr && fGlyphs.width() == fRun->size(); }
54     };
55 
56     using ShapeVisitor =
57             std::function<SkScalar(TextRange textRange, SkSpan<Block>, SkScalar&, TextIndex, uint8_t)>;
58     bool iterateThroughShapingRegions(const ShapeVisitor& shape);
59 
60     using ShapeSingleFontVisitor =
61             std::function<void(Block, SkTArray<SkShaper::Feature>)>;
62     void iterateThroughFontStyles(
63             TextRange textRange, SkSpan<Block> styleSpan, const ShapeSingleFontVisitor& visitor);
64 
65     enum Resolved {
66         Nothing,
67         Something,
68         Everything
69     };
70 
71 #ifndef USE_SKIA_TXT
72     using TypefaceVisitor = std::function<Resolved(sk_sp<SkTypeface> typeface)>;
73 #else
74     using TypefaceVisitor = std::function<Resolved(std::shared_ptr<RSTypeface> typeface)>;
75 #endif
76     void matchResolvedFonts(const TextStyle& textStyle, const TypefaceVisitor& visitor);
77 
78 #ifdef OHOS_SUPPORT
79     bool isUnresolvedCombineGlyphRange(std::shared_ptr<Run> run, size_t glyphStart, size_t glyphEnd,
80         size_t charStart) const;
81     void splitUnresolvedBlockAndStageResolvedSubBlock(
82         std::deque<RunBlock>& stagedUnresolvedBlocks, const RunBlock& unresolvedBlock);
83     void shapeUnresolvedTextSeparatelyFromUnresolvedBlock(const TextStyle& textStyle, const TypefaceVisitor& visitor);
84 #endif
85 
86 #ifdef SK_DEBUG
87     void printState();
88 #endif
89     void finish(const Block& block, SkScalar height, SkScalar& advanceX);
90 
beginLine()91     void beginLine() override {}
runInfo(const RunInfo &)92     void runInfo(const RunInfo&) override {}
commitRunInfo()93     void commitRunInfo() override {}
commitLine()94     void commitLine() override {}
95 
runBuffer(const RunInfo & info)96     Buffer runBuffer(const RunInfo& info) override {
97         fCurrentRun = std::make_shared<Run>(fParagraph,
98                                            info,
99                                            fCurrentText.start,
100                                            fHeight,
101                                            fUseHalfLeading,
102                                            fBaselineShift,
103                                            ++fUniqueRunId,
104                                            fAdvance.fX);
105         return fCurrentRun->newRunBuffer();
106     }
107 
108     void commitRunBuffer(const RunInfo&) override;
109 
110     TextRange clusteredText(GlyphRange& glyphs);
clusterIndex(GlyphIndex glyph)111     ClusterIndex clusterIndex(GlyphIndex glyph) {
112         return fCurrentText.start + fCurrentRun->fClusterIndexes[glyph];
113     }
114     void addFullyResolved();
115     void addUnresolvedWithRun(GlyphRange glyphRange);
116     void sortOutGlyphs(std::function<void(GlyphRange)>&& sortOutUnresolvedBLock);
117     ClusterRange normalizeTextRange(GlyphRange glyphRange);
118     void fillGaps(size_t);
119     BlockRange generateBlockRange(const Block& block, const TextRange& textRange);
120 
121     ParagraphImpl* fParagraph;
122     TextRange fCurrentText;
123     SkScalar fHeight;
124     bool fUseHalfLeading;
125     SkScalar fBaselineShift;
126     SkVector fAdvance;
127     size_t fUnresolvedGlyphs;
128     size_t fUniqueRunId;
129 
130     // TODO: Something that is not thead-safe since we don't need it
131     std::shared_ptr<Run> fCurrentRun;
132     std::deque<RunBlock> fUnresolvedBlocks;
133     std::vector<RunBlock> fResolvedBlocks;
134 
135     // Keeping all resolved typefaces
136     struct FontKey {
137 
FontKeyFontKey138         FontKey() {}
139 
140 #ifndef USE_SKIA_TXT
FontKeyFontKey141         FontKey(SkUnichar unicode, SkFontStyle fontStyle, SkString locale)
142             : fUnicode(unicode), fFontStyle(fontStyle), fLocale(locale) { }
143 #else
FontKeyFontKey144         FontKey(SkUnichar unicode, RSFontStyle fontStyle, SkString locale)
145             : fUnicode(unicode), fFontStyle(fontStyle), fLocale(locale) { }
146 #endif
147 
148         SkUnichar fUnicode;
149 #ifndef USE_SKIA_TXT
150         SkFontStyle fFontStyle;
151 #else
152         RSFontStyle fFontStyle;
153 #endif
154         SkString fLocale;
155 
156         bool operator==(const FontKey& other) const;
157 
158         struct Hasher {
159             uint32_t operator()(const FontKey& key) const;
160         };
161     };
162 
163 #ifndef USE_SKIA_TXT
164     SkTHashMap<FontKey, sk_sp<SkTypeface>, FontKey::Hasher> fFallbackFonts;
165 #else
166     std::unordered_map<FontKey, std::shared_ptr<RSTypeface>, FontKey::Hasher> fFallbackFonts;
167 #endif
168 };
169 
170 }  // namespace textlayout
171 }  // namespace skia
172 #endif
173