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