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
18class SkTestFont;
19
20struct 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    SkFontStyle fStyle;
29    sk_sp<SkTestFont> fCachedFont;
30};
31
32class SkTestFont : public SkRefCnt {
33public:
34    SkTestFont(const SkTestFontData& );
35    virtual ~SkTestFont();
36    int codeToIndex(SkUnichar charCode) const;
37    void init(const SkScalar* pts, const unsigned char* verbs);
38private:
39    const unsigned* fCharCodes;
40    const size_t fCharCodesCount;
41    const SkFixed* fWidths;
42    const SkPaint::FontMetrics& fMetrics;
43    const char* fName;
44    SkPath** fPaths;
45    friend class SkTestTypeface;
46    typedef SkRefCnt INHERITED;
47};
48
49
50class SkTestTypeface : public SkTypeface {
51public:
52    SkTestTypeface(sk_sp<SkTestFont>, const SkFontStyle& style);
53    void getAdvance(SkGlyph* glyph);
54    void getFontMetrics(SkPaint::FontMetrics* metrics);
55    void getMetrics(SkGlyph* glyph);
56    void getPath(SkGlyphID glyph, SkPath* path);
57protected:
58    SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&,
59                                           const SkDescriptor* desc) const override;
60    void onFilterRec(SkScalerContextRec* rec) const override;
61    std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override;
62
63    SkStreamAsset* onOpenStream(int* ttcIndex) const override {
64        return nullptr;
65    }
66
67    void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override;
68
69    int onCharsToGlyphs(const void* chars, Encoding encoding,
70                        uint16_t glyphs[], int glyphCount) const override;
71
72    int onCountGlyphs() const override {
73        return (int) fTestFont->fCharCodesCount;
74    }
75
76    int onGetUPEM() const override {
77        return 2048;
78    }
79
80    void onGetFamilyName(SkString* familyName) const override;
81    SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
82
83    int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
84                                     int coordinateCount) const override
85    {
86        return 0;
87    }
88
89    int onGetTableTags(SkFontTableTag tags[]) const override {
90        return 0;
91    }
92
93    size_t onGetTableData(SkFontTableTag tag, size_t offset,
94                          size_t length, void* data) const override {
95        return 0;
96    }
97private:
98    sk_sp<SkTestFont> fTestFont;
99    friend class SkTestScalerContext;
100};
101
102#endif
103