• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SkTestScalerContext_DEFINED
9 #define SkTestScalerContext_DEFINED
10 
11 #include "SkFixed.h"
12 #include "SkPaint.h"
13 #include "SkPath.h"
14 #include "SkRefCnt.h"
15 #include "SkTDArray.h"
16 #include "SkTypeface.h"
17 
18 class SkTestFont;
19 
20 struct SkTestFontData {
21     const SkScalar* fPoints;
22     const unsigned char* fVerbs;
23     const unsigned* fCharCodes;
24     const size_t fCharCodesCount;
25     const SkFixed* fWidths;
26     const SkPaint::FontMetrics& fMetrics;
27     const char* fName;
28     SkTypeface::Style fStyle;
29     sk_sp<SkTestFont> fCachedFont;
30 };
31 
32 class SkTestFont : public SkRefCnt {
33 public:
34     SkTestFont(const SkTestFontData& );
35     virtual ~SkTestFont();
36     int codeToIndex(SkUnichar charCode) const;
37     void init(const SkScalar* pts, const unsigned char* verbs);
38 #ifdef SK_DEBUG  // detect missing test font data
39     mutable unsigned char fDebugBits[16];
40     mutable SkUnichar fDebugOverage[8];
41     const char* fDebugName;
42     SkFontStyle fDebugStyle;
debugFontName()43     const char* debugFontName() const { return fName; }
44 #endif
45 private:
46     const unsigned* fCharCodes;
47     const size_t fCharCodesCount;
48     const SkFixed* fWidths;
49     const SkPaint::FontMetrics& fMetrics;
50     const char* fName;
51     SkPath** fPaths;
52     friend class SkTestTypeface;
53     typedef SkRefCnt INHERITED;
54 };
55 
56 
57 class SkTestTypeface : public SkTypeface {
58 public:
59     SkTestTypeface(sk_sp<SkTestFont>, const SkFontStyle& style);
60     void getAdvance(SkGlyph* glyph);
61     void getFontMetrics(SkPaint::FontMetrics* metrics);
62     void getMetrics(SkGlyph* glyph);
63     void getPath(SkGlyphID glyph, SkPath* path);
64 protected:
65     SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&,
66                                            const SkDescriptor* desc) const override;
67     void onFilterRec(SkScalerContextRec* rec) const override;
68     std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override;
69 
onOpenStream(int * ttcIndex)70     SkStreamAsset* onOpenStream(int* ttcIndex) const override {
71         return nullptr;
72     }
73 
74     void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override;
75 
76     int onCharsToGlyphs(const void* chars, Encoding encoding,
77                         uint16_t glyphs[], int glyphCount) const override;
78 
onCountGlyphs()79     int onCountGlyphs() const override {
80         return (int) fTestFont->fCharCodesCount;
81     }
82 
onGetUPEM()83     int onGetUPEM() const override {
84         return 2048;
85     }
86 
87     void onGetFamilyName(SkString* familyName) const override;
88     SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
89 
onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount)90     int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
91                                      int coordinateCount) const override
92     {
93         return 0;
94     }
95 
onGetTableTags(SkFontTableTag tags[])96     int onGetTableTags(SkFontTableTag tags[]) const override {
97         return 0;
98     }
99 
onGetTableData(SkFontTableTag tag,size_t offset,size_t length,void * data)100     size_t onGetTableData(SkFontTableTag tag, size_t offset,
101                           size_t length, void* data) const override {
102         return 0;
103     }
104 private:
105     sk_sp<SkTestFont> fTestFont;
106     friend class SkTestScalerContext;
107 };
108 
109 SkTypeface* CreateTestTypeface(const char* name, SkTypeface::Style style);
110 
111 #endif
112