• 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     SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
69         PerGlyphInfo,
70         const uint32_t* glyphIDs,
71         uint32_t glyphIDsCount) const override;
72 
onOpenStream(int * ttcIndex)73     SkStreamAsset* onOpenStream(int* ttcIndex) const override {
74         return nullptr;
75     }
76 
77     void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override;
78 
79     int onCharsToGlyphs(const void* chars, Encoding encoding,
80                         uint16_t glyphs[], int glyphCount) const override;
81 
onCountGlyphs()82     int onCountGlyphs() const override {
83         return (int) fTestFont->fCharCodesCount;
84     }
85 
onGetUPEM()86     int onGetUPEM() const override {
87         return 2048;
88     }
89 
90     void onGetFamilyName(SkString* familyName) const override;
91     SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
92 
onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount)93     int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
94                                      int coordinateCount) const override
95     {
96         return 0;
97     }
98 
onGetTableTags(SkFontTableTag tags[])99     int onGetTableTags(SkFontTableTag tags[]) const override {
100         return 0;
101     }
102 
onGetTableData(SkFontTableTag tag,size_t offset,size_t length,void * data)103     size_t onGetTableData(SkFontTableTag tag, size_t offset,
104                           size_t length, void* data) const override {
105         return 0;
106     }
107 private:
108     sk_sp<SkTestFont> fTestFont;
109     friend class SkTestScalerContext;
110 };
111 
112 SkTypeface* CreateTestTypeface(const char* name, SkTypeface::Style style);
113 
114 #endif
115