1 /* 2 * Copyright 2018 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkRemoteTypeface_DEFINED 9 #define SkRemoteTypeface_DEFINED 10 11 #include "include/core/SkFontArguments.h" 12 #include "include/core/SkFontParameters.h" 13 #include "include/core/SkFontStyle.h" 14 #include "include/core/SkRefCnt.h" 15 #include "include/core/SkString.h" 16 #include "include/core/SkTypeface.h" 17 #include "include/core/SkTypes.h" 18 #include "include/private/chromium/SkChromeRemoteGlyphCache.h" 19 #include "src/core/SkScalerContext.h" 20 21 #include <cstddef> 22 #include <cstdint> 23 #include <memory> 24 #include <optional> 25 26 class SkArenaAlloc; 27 class SkDescriptor; 28 class SkDrawable; 29 class SkFontDescriptor; 30 class SkGlyph; 31 class SkPath; 32 class SkReadBuffer; 33 class SkStreamAsset; 34 class SkTypefaceProxy; 35 class SkWriteBuffer; 36 struct SkAdvancedTypefaceMetrics; 37 struct SkFontMetrics; 38 39 class SkScalerContextProxy : public SkScalerContext { 40 public: 41 SkScalerContextProxy(sk_sp<SkTypeface> tf, 42 const SkScalerContextEffects& effects, 43 const SkDescriptor* desc, 44 sk_sp<SkStrikeClient::DiscardableHandleManager> manager); 45 46 protected: 47 GlyphMetrics generateMetrics(const SkGlyph&, SkArenaAlloc*) override; 48 void generateImage(const SkGlyph&, void*) override; 49 bool generatePath(const SkGlyph& glyph, SkPath* path, bool* modified) override; 50 sk_sp<SkDrawable> generateDrawable(const SkGlyph&) override; 51 void generateFontMetrics(SkFontMetrics* metrics) override; 52 SkTypefaceProxy* getProxyTypeface() const; 53 54 private: 55 sk_sp<SkStrikeClient::DiscardableHandleManager> fDiscardableManager; 56 using INHERITED = SkScalerContext; 57 }; 58 59 // SkTypefaceProxyPrototype is the serialization format for SkTypefaceProxy. 60 class SkTypefaceProxyPrototype { 61 public: 62 static std::optional<SkTypefaceProxyPrototype> MakeFromBuffer(SkReadBuffer& buffer); 63 explicit SkTypefaceProxyPrototype(const SkTypeface& typeface); 64 SkTypefaceProxyPrototype(SkTypefaceID typefaceID, 65 int glyphCount, 66 int32_t styleValue, 67 bool isFixedPitch, 68 bool glyphMaskNeedsCurrentColor); 69 70 void flatten(SkWriteBuffer&buffer) const; serverTypefaceID()71 SkTypefaceID serverTypefaceID() const { return fServerTypefaceID; } 72 73 private: 74 friend class SkTypefaceProxy; style()75 SkFontStyle style() const { 76 SkFontStyle style; 77 style.fValue = fStyleValue; 78 return style; 79 } 80 const SkTypefaceID fServerTypefaceID; 81 const int fGlyphCount; 82 const int32_t fStyleValue; 83 const bool fIsFixedPitch; 84 // Used for COLRv0 or COLRv1 fonts that may need the 0xFFFF special palette 85 // index to represent foreground color. This information needs to be on here 86 // to determine how this typeface can be cached. 87 const bool fGlyphMaskNeedsCurrentColor; 88 }; 89 90 class SkTypefaceProxy : public SkTypeface { 91 public: 92 SkTypefaceProxy(const SkTypefaceProxyPrototype& prototype, 93 sk_sp<SkStrikeClient::DiscardableHandleManager> manager, 94 bool isLogging = true); 95 96 SkTypefaceProxy(SkTypefaceID typefaceID, 97 int glyphCount, 98 const SkFontStyle& style, 99 bool isFixedPitch, 100 bool glyphMaskNeedsCurrentColor, 101 sk_sp<SkStrikeClient::DiscardableHandleManager> manager, 102 bool isLogging = true); 103 remoteTypefaceID()104 SkTypefaceID remoteTypefaceID() const {return fTypefaceID;} 105 glyphCount()106 int glyphCount() const {return fGlyphCount;} 107 isLogging()108 bool isLogging() const {return fIsLogging;} 109 110 protected: onGetUPEM()111 int onGetUPEM() const override { SK_ABORT("Should never be called."); } onOpenStream(int * ttcIndex)112 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override { 113 SK_ABORT("Should never be called."); 114 } onMakeClone(const SkFontArguments & args)115 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override { 116 SK_ABORT("Should never be called."); 117 } onGlyphMaskNeedsCurrentColor()118 bool onGlyphMaskNeedsCurrentColor() const override { 119 return fGlyphMaskNeedsCurrentColor; 120 } onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount)121 int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], 122 int coordinateCount) const override { 123 SK_ABORT("Should never be called."); 124 } onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],int parameterCount)125 int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[], 126 int parameterCount) const override { 127 SK_ABORT("Should never be called."); 128 } onGetFamilyName(SkString * familyName)129 void onGetFamilyName(SkString* familyName) const override { 130 // Used by SkStrikeCache::DumpMemoryStatistics. 131 *familyName = ""; 132 } onGetPostScriptName(SkString *)133 bool onGetPostScriptName(SkString*) const override { 134 SK_ABORT("Should never be called."); 135 } onCreateFamilyNameIterator()136 SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override { 137 SK_ABORT("Should never be called."); 138 } onGetTableTags(SkFontTableTag tags[])139 int onGetTableTags(SkFontTableTag tags[]) const override { 140 SK_ABORT("Should never be called."); 141 } onGetTableData(SkFontTableTag,size_t offset,size_t length,void * data)142 size_t onGetTableData(SkFontTableTag, size_t offset, size_t length, void* data) const override { 143 SK_ABORT("Should never be called."); 144 } onCreateScalerContext(const SkScalerContextEffects & effects,const SkDescriptor * desc)145 std::unique_ptr<SkScalerContext> onCreateScalerContext( 146 const SkScalerContextEffects& effects, const SkDescriptor* desc) const override 147 { 148 return std::make_unique<SkScalerContextProxy>( 149 sk_ref_sp(const_cast<SkTypefaceProxy*>(this)), effects, desc, fDiscardableManager); 150 } onFilterRec(SkScalerContextRec * rec)151 void onFilterRec(SkScalerContextRec* rec) const override { 152 // The rec filtering is already applied by the server when generating 153 // the glyphs. 154 } onGetFontDescriptor(SkFontDescriptor *,bool *)155 void onGetFontDescriptor(SkFontDescriptor*, bool*) const override { 156 SK_ABORT("Should never be called."); 157 } getGlyphToUnicodeMap(SkUnichar *)158 void getGlyphToUnicodeMap(SkUnichar*) const override { 159 SK_ABORT("Should never be called."); 160 } 161 getPostScriptGlyphNames(SkString *)162 void getPostScriptGlyphNames(SkString*) const override { 163 SK_ABORT("Should never be called."); 164 } 165 onGetAdvancedMetrics()166 std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override { 167 SK_ABORT("Should never be called."); 168 } onCharsToGlyphs(const SkUnichar * chars,int count,SkGlyphID glyphs[])169 void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override { 170 SK_ABORT("Should never be called."); 171 } onCountGlyphs()172 int onCountGlyphs() const override { 173 return this->glyphCount(); 174 } 175 onGetCTFontRef()176 void* onGetCTFontRef() const override { 177 SK_ABORT("Should never be called."); 178 } 179 180 private: 181 const SkTypefaceID fTypefaceID; 182 const int fGlyphCount; 183 const bool fIsLogging; 184 const bool fGlyphMaskNeedsCurrentColor; 185 sk_sp<SkStrikeClient::DiscardableHandleManager> fDiscardableManager; 186 }; 187 188 #endif // SkRemoteTypeface_DEFINED 189