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