1 // Copyright 2019 Google LLC. 2 #ifndef FontResolver_DEFINED 3 #define FontResolver_DEFINED 4 5 #include <memory> 6 #include <set> 7 #include "include/core/SkFontMgr.h" 8 #include "include/core/SkRefCnt.h" 9 #include "include/private/SkTHash.h" 10 #include "modules/skparagraph/include/FontCollection.h" 11 #include "modules/skparagraph/include/TextStyle.h" 12 #include "modules/skparagraph/src/TextLine.h" 13 #include "src/core/SkSpan.h" 14 15 namespace skia { 16 namespace textlayout { 17 18 struct FontDescr { FontDescrFontDescr19 FontDescr() {} FontDescrFontDescr20 FontDescr(SkFont font, SkScalar height) 21 : fFont(font), fHeight(height), fStart(EMPTY_INDEX) {} 22 bool operator==(const FontDescr& a) const { 23 return this->fFont == a.fFont && this->fHeight == a.fHeight; 24 } 25 SkFont fFont; 26 SkScalar fHeight; 27 TextIndex fStart; 28 }; 29 30 class FontResolver { 31 public: 32 33 FontResolver() = default; 34 ~FontResolver() = default; 35 36 void findAllFontsForAllStyledBlocks(ParagraphImpl* master); 37 bool findNext(const char* codepoint, SkFont* font, SkScalar* height); 38 switches()39 const SkTArray<FontDescr>& switches() const { return fFontSwitches; } 40 41 private: 42 void findAllFontsForStyledBlock(const TextStyle& style, TextRange textRange); 43 FontDescr makeFont(sk_sp<SkTypeface> typeface, SkScalar size, SkScalar height); 44 size_t resolveAllCharactersByFont(const FontDescr& fontDescr); 45 void addResolvedWhitespacesToMapping(); 46 47 struct Hash { operatorHash48 uint32_t operator()(const FontDescr& key) const { 49 return SkTypeface::UniqueID(key.fFont.getTypeface()) + 50 SkScalarCeilToInt(key.fFont.getSize()) + 51 SkScalarCeilToInt(key.fHeight); 52 } 53 }; 54 55 SkUnichar firstUnresolved(); 56 57 sk_sp<FontCollection> fFontCollection; 58 SkSpan<const char> fText; 59 TextRange fTextRange; 60 SkSpan<Block> fStyles; 61 62 SkTArray<FontDescr> fFontSwitches; 63 FontDescr* fFontIterator; 64 SkTHashSet<FontDescr, Hash> fResolvedFonts; 65 FontDescr fFirstResolvedFont; 66 67 SkTHashMap<TextIndex, FontDescr> fFontMapping; 68 SkTArray<SkUnichar> fCodepoints; 69 SkTArray<const char*> fCharacters; 70 SkTArray<size_t> fUnresolvedIndexes; 71 SkTArray<SkUnichar> fUnresolvedCodepoints; 72 SkTHashMap<size_t, FontDescr> fWhitespaces; 73 size_t fUnresolved; 74 }; 75 } // namespace textlayout 76 } // namespace skia 77 78 #endif // FontResolver_DEFINED 79