1 /* 2 * Copyright 2014 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 TestSVGTypeface_DEFINED 9 #define TestSVGTypeface_DEFINED 10 11 #include "include/core/SkFontArguments.h" 12 #include "include/core/SkFontMetrics.h" 13 #include "include/core/SkPaint.h" 14 #include "include/core/SkPoint.h" 15 #include "include/core/SkRect.h" 16 #include "include/core/SkRefCnt.h" 17 #include "include/core/SkScalar.h" 18 #include "include/core/SkStream.h" 19 #include "include/core/SkString.h" 20 #include "include/core/SkTypeface.h" 21 #include "include/core/SkTypes.h" 22 #include "include/pathops/SkPathOps.h" 23 #include "include/private/SkMutex.h" 24 #include "include/private/SkTArray.h" 25 #include "include/private/SkTHash.h" 26 #include "src/core/SkSpan.h" 27 28 #include <memory> 29 30 class SkCanvas; 31 class SkDescriptor; 32 class SkFontDescriptor; 33 class SkFontStyle; 34 class SkGlyph; 35 class SkPath; 36 class SkScalerContext; 37 class SkSVGDOM; 38 class SkWStream; 39 struct SkAdvancedTypefaceMetrics; 40 struct SkScalerContextEffects; 41 struct SkScalerContextRec; 42 43 struct SkSVGTestTypefaceGlyphData { 44 const char* fSvgResourcePath; 45 SkPoint fOrigin; // y-down 46 SkScalar fAdvance; 47 SkUnichar fUnicode; // TODO: this limits to 1:1 48 }; 49 50 class TestSVGTypeface : public SkTypeface { 51 public: 52 TestSVGTypeface(const char* name, 53 int upem, 54 const SkFontMetrics& metrics, 55 SkSpan<const SkSVGTestTypefaceGlyphData> data, 56 const SkFontStyle& style); 57 ~TestSVGTypeface() override; 58 void getAdvance(SkGlyph* glyph) const; 59 void getFontMetrics(SkFontMetrics* metrics) const; 60 61 static sk_sp<TestSVGTypeface> Default(); 62 static sk_sp<TestSVGTypeface> Planets(); 63 void exportTtxCbdt(SkWStream*, SkSpan<unsigned> strikeSizes) const; 64 void exportTtxSbix(SkWStream*, SkSpan<unsigned> strikeSizes) const; 65 void exportTtxColr(SkWStream*) const; 66 virtual bool getPathOp(SkColor, SkPathOp*) const = 0; 67 68 struct GlyfLayerInfo { GlyfLayerInfoGlyfLayerInfo69 GlyfLayerInfo(int layerColorIndex, SkIRect bounds) 70 : fLayerColorIndex(layerColorIndex), fBounds(bounds) {} 71 int fLayerColorIndex; 72 SkIRect fBounds; 73 }; 74 struct GlyfInfo { GlyfInfoGlyfInfo75 GlyfInfo() : fBounds(SkIRect::MakeEmpty()) {} 76 SkIRect fBounds; 77 SkTArray<GlyfLayerInfo> fLayers; 78 }; 79 80 protected: 81 void exportTtxCommon(SkWStream*, const char* type, const SkTArray<GlyfInfo>* = nullptr) const; 82 83 SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&, 84 const SkDescriptor* desc) const override; 85 void onFilterRec(SkScalerContextRec* rec) const override; 86 void getGlyphToUnicodeMap(SkUnichar*) const override; 87 std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override; 88 onOpenStream(int * ttcIndex)89 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override { return nullptr; } 90 onMakeClone(const SkFontArguments & args)91 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override { 92 return sk_ref_sp(this); 93 } 94 95 void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override; 96 97 void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override; 98 getPostScriptGlyphNames(SkString *)99 void getPostScriptGlyphNames(SkString*) const override {} 100 onCountGlyphs()101 int onCountGlyphs() const override { return fGlyphCount; } 102 onGetUPEM()103 int onGetUPEM() const override { return fUpem; } 104 105 void onGetFamilyName(SkString* familyName) const override; 106 SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; 107 onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount)108 int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], 109 int coordinateCount) const override { 110 return 0; 111 } 112 onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],int parameterCount)113 int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[], 114 int parameterCount) const override { 115 return 0; 116 } 117 onGetTableTags(SkFontTableTag tags[])118 int onGetTableTags(SkFontTableTag tags[]) const override { return 0; } 119 onGetTableData(SkFontTableTag tag,size_t offset,size_t length,void * data)120 size_t onGetTableData(SkFontTableTag tag, 121 size_t offset, 122 size_t length, 123 void* data) const override { 124 return 0; 125 } 126 127 private: 128 struct Glyph { 129 Glyph(); 130 ~Glyph(); 131 SkPoint fOrigin; 132 SkScalar fAdvance; 133 const char* fResourcePath; 134 135 SkSize size() const; 136 void render(SkCanvas*) const; 137 138 private: 139 // Lazily parses the SVG from fResourcePath, and manages mutex locking. 140 template <typename Fn> void withSVG(Fn&&) const; 141 142 // The mutex guards lazy parsing of the SVG, but also predates that. 143 // Must be SkSVGDOM::render() is not thread safe? 144 // If not, an SkOnce is enough here. 145 mutable SkMutex fSvgMutex; 146 mutable bool fParsedSvg = false; 147 mutable sk_sp<SkSVGDOM> fSvg; 148 }; 149 150 SkString fName; 151 int fUpem; 152 const SkFontMetrics fFontMetrics; 153 std::unique_ptr<Glyph[]> fGlyphs; 154 int fGlyphCount; 155 SkTHashMap<SkUnichar, SkGlyphID> fCMap; 156 friend class SkTestSVGScalerContext; 157 }; 158 159 #endif 160