• 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     using TypefaceVisitor = std::function<Resolved(sk_sp<SkTypeface> typeface)>;
72     void matchResolvedFonts(const TextStyle& textStyle, const TypefaceVisitor& visitor);
73 #ifdef SK_DEBUG
74     void printState();
75 #endif
76     void finish(const Block& block, SkScalar height, SkScalar& advanceX);
77 
beginLine()78     void beginLine() override {}
runInfo(const RunInfo &)79     void runInfo(const RunInfo&) override {}
commitRunInfo()80     void commitRunInfo() override {}
commitLine()81     void commitLine() override {}
82 
runBuffer(const RunInfo & info)83     Buffer runBuffer(const RunInfo& info) override {
84         fCurrentRun = std::make_shared<Run>(fParagraph,
85                                            info,
86                                            fCurrentText.start,
87                                            fHeight,
88                                            fUseHalfLeading,
89                                            fBaselineShift,
90                                            ++fUniqueRunId,
91                                            fAdvance.fX);
92         return fCurrentRun->newRunBuffer();
93     }
94 
95     void commitRunBuffer(const RunInfo&) override;
96 
97     TextRange clusteredText(GlyphRange& glyphs);
clusterIndex(GlyphIndex glyph)98     ClusterIndex clusterIndex(GlyphIndex glyph) {
99         return fCurrentText.start + fCurrentRun->fClusterIndexes[glyph];
100     }
101     void addFullyResolved();
102     void addUnresolvedWithRun(GlyphRange glyphRange);
103     void sortOutGlyphs(std::function<void(GlyphRange)>&& sortOutUnresolvedBLock);
104     ClusterRange normalizeTextRange(GlyphRange glyphRange);
105     void fillGaps(size_t);
106     BlockRange generateBlockRange(const Block& block, const TextRange& textRange);
107 
108     ParagraphImpl* fParagraph;
109     TextRange fCurrentText;
110     SkScalar fHeight;
111     bool fUseHalfLeading;
112     SkScalar fBaselineShift;
113     SkVector fAdvance;
114     size_t fUnresolvedGlyphs;
115     size_t fUniqueRunId;
116 
117     // TODO: Something that is not thead-safe since we don't need it
118     std::shared_ptr<Run> fCurrentRun;
119     std::deque<RunBlock> fUnresolvedBlocks;
120     std::vector<RunBlock> fResolvedBlocks;
121 
122     // Keeping all resolved typefaces
123     struct FontKey {
124 
FontKeyFontKey125         FontKey() {}
126 
FontKeyFontKey127         FontKey(SkUnichar unicode, SkFontStyle fontStyle, SkString locale)
128             : fUnicode(unicode), fFontStyle(fontStyle), fLocale(locale) { }
129         SkUnichar fUnicode;
130         SkFontStyle fFontStyle;
131         SkString fLocale;
132 
133         bool operator==(const FontKey& other) const;
134 
135         struct Hasher {
136             uint32_t operator()(const FontKey& key) const;
137         };
138     };
139     SkTHashMap<FontKey, sk_sp<SkTypeface>, FontKey::Hasher> fFallbackFonts;
140 };
141 
142 }  // namespace textlayout
143 }  // namespace skia
144 #endif
145