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 "SkAdvancedTypefaceMetrics.h" 12 #include "SkDescriptor.h" 13 #include "SkFontDescriptor.h" 14 #include "SkFontStyle.h" 15 #include "SkScalerContext.h" 16 #include "SkTypeface.h" 17 18 #include <thread> 19 20 class SkTypefaceProxy; 21 22 class SkRemoteScalerContext { 23 public: 24 virtual ~SkRemoteScalerContext() {} 25 // TODO: do metrics need effects? 26 virtual void generateFontMetrics( 27 const SkTypefaceProxy& tf, 28 const SkScalerContextRec& rec, 29 SkPaint::FontMetrics*) = 0; 30 virtual void generateMetrics( 31 const SkTypefaceProxy& tf, 32 const SkScalerContextRec& rec, 33 SkGlyph* glyph) = 0; 34 virtual void generateImage( 35 const SkTypefaceProxy& tf, 36 const SkScalerContextRec& rec, 37 const SkGlyph& glyph) = 0; 38 virtual void generatePath( 39 const SkTypefaceProxy& tf, 40 const SkScalerContextRec& rec, 41 SkGlyphID glyph, SkPath* path) = 0; 42 }; 43 44 class SkScalerContextProxy : public SkScalerContext { 45 public: 46 SkScalerContextProxy( 47 sk_sp<SkTypeface> tf, 48 const SkScalerContextEffects& effects, 49 const SkDescriptor* desc, 50 SkRemoteScalerContext* rsc); 51 52 protected: 53 unsigned generateGlyphCount(void) override { SK_ABORT("Should never be called."); return 0;} 54 uint16_t generateCharToGlyph(SkUnichar uni) override { 55 SK_ABORT("Should never be called."); 56 return 0; 57 } 58 void generateAdvance(SkGlyph* glyph) override { this->generateMetrics(glyph); } 59 void generateMetrics(SkGlyph* glyph) override; 60 void generateImage(const SkGlyph& glyph) override; 61 void generatePath(SkGlyphID glyphID, SkPath* path) override; 62 void generateFontMetrics(SkPaint::FontMetrics* metrics) override; 63 64 private: 65 SkTypefaceProxy* typefaceProxy(); 66 SkRemoteScalerContext* const fRemote; 67 typedef SkScalerContext INHERITED; 68 }; 69 70 class SkTypefaceProxy : public SkTypeface { 71 public: 72 SkTypefaceProxy( 73 SkFontID fontId, 74 std::thread::id threadId, 75 const SkFontStyle& style, 76 bool isFixed, 77 SkRemoteScalerContext* rsc) 78 : INHERITED{style, false} 79 , fFontId{fontId} 80 , fThreadId{threadId} 81 , fRsc{rsc} { } 82 SkFontID fontID() const {return fFontId;} 83 protected: 84 int onGetUPEM() const override { SK_ABORT("Should never be called."); return 0; } 85 SkStreamAsset* onOpenStream(int* ttcIndex) const override { 86 SK_ABORT("Should never be called."); 87 return nullptr; 88 } 89 std::unique_ptr<SkFontData> onMakeFontData() const override { 90 SK_ABORT("Should never be called."); 91 return nullptr; 92 } 93 int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], 94 int coordinateCount) const override { 95 SK_ABORT("Should never be called."); 96 return 0; 97 } 98 void onGetFamilyName(SkString* familyName) const override { 99 SK_ABORT("Should never be called."); 100 } 101 SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override { 102 SK_ABORT("Should never be called."); 103 return nullptr; 104 } 105 int onGetTableTags(SkFontTableTag tags[]) const override { 106 SK_ABORT("Should never be called."); 107 return 0; 108 } 109 size_t onGetTableData(SkFontTableTag, size_t offset, size_t length, void* data) const override { 110 SK_ABORT("Should never be called."); 111 return 0; 112 } 113 SkScalerContext* onCreateScalerContext(const SkScalerContextEffects& effects, 114 const SkDescriptor* desc) const override { 115 //std::cout << fFontId << fThreadId; 116 117 return new SkScalerContextProxy(sk_ref_sp(const_cast<SkTypefaceProxy*>(this)), effects, 118 desc, fRsc); 119 120 } 121 void onFilterRec(SkScalerContextRec* rec) const override { 122 // Add all the device information here. 123 //rec->fPost2x2[0][0] = 0.5f; 124 125 // This would be the best place to run the host SkTypeface_* onFilterRec. 126 // Can we move onFilterRec to the FongMgr, that way we don't need to cross the boundary to 127 // filter. 128 } 129 void onGetFontDescriptor(SkFontDescriptor*, bool*) const override { 130 SK_ABORT("Should never be called."); 131 } 132 std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override { 133 SK_ABORT("Should never be called."); 134 return nullptr; 135 } 136 int onCharsToGlyphs(const void* chars, Encoding, 137 uint16_t glyphs[], int glyphCount) const override { 138 SK_ABORT("Should never be called."); 139 return 0; 140 } 141 int onCountGlyphs() const override { 142 SK_ABORT("Should never be called."); 143 return 0; 144 } 145 146 void* onGetCTFontRef() const override { 147 SK_ABORT("Should never be called."); 148 return nullptr; 149 } 150 151 private: 152 const SkFontID fFontId; 153 const std::thread::id fThreadId; 154 SkRemoteScalerContext* const fRsc; 155 156 typedef SkTypeface INHERITED; 157 }; 158 159 #endif // SkRemoteTypeface_DEFINED 160