Ben Wagner | c912d61 | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 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 SkTestSVGTypeface_DEFINED |
| 9 | #define SkTestSVGTypeface_DEFINED |
| 10 | |
| 11 | #include "SkFontArguments.h" |
| 12 | #include "SkPaint.h" |
| 13 | #include "SkPoint.h" |
| 14 | #include "SkRect.h" |
| 15 | #include "SkRefCnt.h" |
| 16 | #include "SkScalar.h" |
| 17 | #include "SkString.h" |
| 18 | #include "SkTArray.h" |
| 19 | #include "SkTHash.h" |
| 20 | #include "SkTypeface.h" |
| 21 | #include "SkTypes.h" |
| 22 | |
| 23 | #include <memory> |
| 24 | |
| 25 | class SkDescriptor; |
| 26 | class SkFontDescriptor; |
| 27 | class SkFontStyle; |
| 28 | class SkGlyph; |
| 29 | class SkPath; |
| 30 | class SkScalerContext; |
| 31 | class SkStreamAsset; |
| 32 | class SkSVGDOM; |
| 33 | class SkWStream; |
| 34 | struct SkAdvancedTypefaceMetrics; |
| 35 | struct SkScalerContextEffects; |
| 36 | struct SkScalerContextRec; |
| 37 | |
| 38 | struct SkSVGTestTypefaceGlyphData { |
| 39 | const char* fSvgResourcePath; |
| 40 | SkPoint fOrigin; |
| 41 | SkScalar fAdvance; |
| 42 | SkUnichar fUnicode; //TODO: this limits to 1:1 |
| 43 | }; |
| 44 | |
| 45 | class SkTestSVGTypeface : public SkTypeface { |
| 46 | public: |
| 47 | SkTestSVGTypeface(const char* name, |
| 48 | int upem, |
| 49 | const SkPaint::FontMetrics& metrics, |
| 50 | const SkSVGTestTypefaceGlyphData* data, int dataCount, |
| 51 | const SkFontStyle& style); |
| 52 | ~SkTestSVGTypeface() override; |
| 53 | void getAdvance(SkGlyph* glyph) const; |
| 54 | void getFontMetrics(SkPaint::FontMetrics* metrics) const; |
| 55 | void getPath(SkGlyphID glyph, SkPath* path) const; |
| 56 | |
| 57 | static sk_sp<SkTestSVGTypeface> Default(); |
| 58 | void exportTtxCbdt(SkWStream*) const; |
| 59 | void exportTtxSbix(SkWStream*) const; |
| 60 | void exportTtxColr(SkWStream*) const; |
| 61 | |
| 62 | struct GlyfLayerInfo { |
| 63 | GlyfLayerInfo(int layerColorIndex, SkIRect bounds) |
| 64 | : fLayerColorIndex(layerColorIndex) |
| 65 | , fBounds(bounds) {} |
| 66 | int fLayerColorIndex; |
| 67 | SkIRect fBounds; |
| 68 | }; |
| 69 | struct GlyfInfo { |
| 70 | GlyfInfo() : fBounds(SkIRect::MakeEmpty()) {} |
| 71 | SkIRect fBounds; |
| 72 | SkTArray<GlyfLayerInfo> fLayers; |
| 73 | }; |
| 74 | protected: |
| 75 | void exportTtxCommon(SkWStream*, const char* type, const SkTArray<GlyfInfo>* = nullptr) const; |
| 76 | |
| 77 | SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&, |
| 78 | const SkDescriptor* desc) const override; |
| 79 | void onFilterRec(SkScalerContextRec* rec) const override; |
| 80 | std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override; |
| 81 | |
| 82 | SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
| 83 | return nullptr; |
| 84 | } |
| 85 | |
| 86 | void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override; |
| 87 | |
| 88 | int onCharsToGlyphs(const void* chars, Encoding encoding, |
| 89 | uint16_t glyphs[], int glyphCount) const override; |
| 90 | |
| 91 | int onCountGlyphs() const override { |
| 92 | return fGlyphs.count(); |
| 93 | } |
| 94 | |
| 95 | int onGetUPEM() const override { |
| 96 | return fUpem; |
| 97 | } |
| 98 | |
| 99 | void onGetFamilyName(SkString* familyName) const override; |
| 100 | SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; |
| 101 | |
| 102 | int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], |
| 103 | int coordinateCount) const override |
| 104 | { |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | int onGetTableTags(SkFontTableTag tags[]) const override { |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | size_t onGetTableData(SkFontTableTag tag, size_t offset, |
| 113 | size_t length, void* data) const override { |
| 114 | return 0; |
| 115 | } |
| 116 | private: |
| 117 | struct Glyph { |
| 118 | Glyph(sk_sp<SkSVGDOM> svg, const SkSVGTestTypefaceGlyphData& data); |
| 119 | ~Glyph(); |
| 120 | sk_sp<SkSVGDOM> fSvg; |
| 121 | SkPoint fOrigin; |
| 122 | SkScalar fAdvance; |
| 123 | }; |
| 124 | SkString fName; |
| 125 | int fUpem; |
| 126 | const SkPaint::FontMetrics fFontMetrics; |
| 127 | SkTArray<Glyph> fGlyphs; |
| 128 | SkTHashMap<SkUnichar, SkGlyphID> fCMap; |
| 129 | friend class SkTestSVGScalerContext; |
| 130 | }; |
| 131 | |
| 132 | #endif |