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