blob: 86c828511bf104f8efd2c74d33e9bdd3e62c00f1 [file] [log] [blame]
Ben Wagnerc912d612018-02-15 10:20:04 -05001/*
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
25class SkDescriptor;
26class SkFontDescriptor;
27class SkFontStyle;
28class SkGlyph;
29class SkPath;
30class SkScalerContext;
31class SkStreamAsset;
32class SkSVGDOM;
33class SkWStream;
34struct SkAdvancedTypefaceMetrics;
35struct SkScalerContextEffects;
36struct SkScalerContextRec;
37
38struct SkSVGTestTypefaceGlyphData {
39 const char* fSvgResourcePath;
40 SkPoint fOrigin;
41 SkScalar fAdvance;
42 SkUnichar fUnicode; //TODO: this limits to 1:1
43};
44
45class SkTestSVGTypeface : public SkTypeface {
46public:
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 };
74protected:
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 }
116private:
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