Coverage Report

Created: 2024-09-14 07:19

/src/skia/tools/fonts/TestFontMgr.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2017 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
#include "include/core/SkPathBuilder.h"
9
#include "include/private/base/SkAssert.h"
10
#include "include/utils/SkCustomTypeface.h"
11
#include "src/core/SkFontDescriptor.h"
12
#include "tools/ToolUtils.h"
13
#include "tools/fonts/TestFontMgr.h"
14
#include "tools/fonts/TestTypeface.h"
15
16
#ifdef SK_XML
17
#include "tools/fonts/TestSVGTypeface.h"
18
#endif
19
20
#include <vector>
21
22
namespace {
23
24
class FontStyleSet final : public SkFontStyleSet {
25
public:
26
528
    FontStyleSet(const char* familyName) : fFamilyName(familyName) {}
27
    struct TypefaceEntry {
28
        TypefaceEntry(sk_sp<SkTypeface> typeface, SkFontStyle style, const char* styleName)
29
1.71k
                : fTypeface(std::move(typeface)), fStyle(style), fStyleName(styleName) {}
30
        sk_sp<SkTypeface> fTypeface;
31
        SkFontStyle       fStyle;
32
        const char*       fStyleName;
33
    };
34
35
328k
    int count() override { return fTypefaces.size(); }
36
37
1.28M
    void getStyle(int index, SkFontStyle* style, SkString* name) override {
38
1.28M
        if (style) {
39
1.28M
            *style = fTypefaces[index].fStyle;
40
1.28M
        }
41
1.28M
        if (name) {
42
0
            *name = fTypefaces[index].fStyleName;
43
0
        }
44
1.28M
    }
45
46
328k
    sk_sp<SkTypeface> createTypeface(int index) override {
47
328k
        return fTypefaces[index].fTypeface;
48
328k
    }
49
50
322k
    sk_sp<SkTypeface> matchStyle(const SkFontStyle& pattern) override {
51
322k
        return this->matchStyleCSS3(pattern);
52
322k
    }
53
54
0
    SkString getFamilyName() { return fFamilyName; }
55
56
    std::vector<TypefaceEntry> fTypefaces;
57
    SkString                   fFamilyName;
58
};
59
60
class FontMgr final : public SkFontMgr {
61
public:
62
66
    FontMgr() {
63
66
        auto&& list = TestTypeface::Typefaces();
64
396
        for (auto&& family : list.families) {
65
396
            auto&& ss = fFamilies.emplace_back(sk_make_sp<FontStyleSet>(family.name));
66
1.58k
            for (auto&& face : family.faces) {
67
1.58k
                ss->fTypefaces.emplace_back(face.typeface, face.typeface->fontStyle(), face.name);
68
1.58k
                if (face.isDefault) {
69
66
                    fDefaultFamily = ss;
70
66
                    fDefaultTypeface = face.typeface;
71
66
                }
72
1.58k
            }
73
396
        }
74
66
        if (!fDefaultFamily) {
75
0
            SkASSERTF(false, "expected TestTypeface to return a default");
76
0
            fDefaultFamily = fFamilies[0];
77
0
            fDefaultTypeface = fDefaultFamily->fTypefaces[0].fTypeface;
78
0
        }
79
80
66
#if defined(SK_ENABLE_SVG)
81
66
        fFamilies.emplace_back(sk_make_sp<FontStyleSet>("Emoji"));
82
66
        fFamilies.back()->fTypefaces.emplace_back(
83
66
                TestSVGTypeface::Default(), SkFontStyle::Normal(), "Normal");
84
85
66
        fFamilies.emplace_back(sk_make_sp<FontStyleSet>("Planet"));
86
66
        fFamilies.back()->fTypefaces.emplace_back(
87
66
                TestSVGTypeface::Planets(), SkFontStyle::Normal(), "Normal");
88
66
#endif
89
66
    }
90
91
6.54k
    int onCountFamilies() const override { return fFamilies.size(); }
92
93
0
    void onGetFamilyName(int index, SkString* familyName) const override {
94
0
        *familyName = fFamilies[index]->getFamilyName();
95
0
    }
96
97
6.91k
    sk_sp<SkFontStyleSet> onCreateStyleSet(int index) const override {
98
6.91k
        sk_sp<SkFontStyleSet> ref = fFamilies[index];
99
6.91k
        return ref;
100
6.91k
    }
101
102
507
    sk_sp<SkFontStyleSet> onMatchFamily(const char familyName[]) const override {
103
507
        if (familyName) {
104
370
            if (strstr(familyName, "ono")) {
105
0
                return this->createStyleSet(0);
106
0
            }
107
370
            if (strstr(familyName, "ans")) {
108
366
                return this->createStyleSet(1);
109
366
            }
110
4
            if (strstr(familyName, "erif")) {
111
0
                return this->createStyleSet(2);
112
0
            }
113
4
#if defined(SK_ENABLE_SVG)
114
4
            if (strstr(familyName, "oji")) {
115
0
                return this->createStyleSet(6);
116
0
            }
117
4
            if (strstr(familyName, "Planet")) {
118
0
                return this->createStyleSet(7);
119
0
            }
120
4
#endif
121
4
        }
122
141
        return nullptr;
123
507
    }
124
125
    sk_sp<SkTypeface> onMatchFamilyStyle(const char         familyName[],
126
507
                                         const SkFontStyle& style) const override {
127
507
        sk_sp<SkFontStyleSet> styleSet(this->matchFamily(familyName));
128
507
        return styleSet->matchStyle(style);
129
507
    }
130
131
    sk_sp<SkTypeface> onMatchFamilyStyleCharacter(const char         familyName[],
132
                                                  const SkFontStyle& style,
133
                                                  const char*        bcp47[],
134
                                                  int                bcp47Count,
135
137
                                                  SkUnichar          character) const override {
136
137
        (void)bcp47;
137
137
        (void)bcp47Count;
138
137
        (void)character;
139
137
        return this->matchFamilyStyle(familyName, style);
140
137
    }
141
142
0
    sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override { return nullptr; }
143
    sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
144
0
                                            int ttcIndex) const override {
145
0
        return nullptr;
146
0
    }
147
    sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
148
0
                                           const SkFontArguments&) const override {
149
0
        return nullptr;
150
0
    }
151
0
    sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
152
0
        return nullptr;
153
0
    }
154
155
    sk_sp<SkTypeface> onLegacyMakeTypeface(const char  familyName[],
156
322k
                                           SkFontStyle style) const override {
157
322k
        if (familyName == nullptr) {
158
321k
            return sk_sp<SkTypeface>(fDefaultFamily->matchStyle(style));
159
321k
        }
160
370
        sk_sp<SkTypeface> typeface = sk_sp<SkTypeface>(this->matchFamilyStyle(familyName, style));
161
370
        if (!typeface) {
162
4
            typeface = fDefaultTypeface;
163
4
        }
164
370
        return typeface;
165
322k
    }
166
167
private:
168
    std::vector<sk_sp<FontStyleSet>> fFamilies;
169
    sk_sp<FontStyleSet>              fDefaultFamily;
170
    sk_sp<SkTypeface>                fDefaultTypeface;
171
};
172
}  // namespace
173
174
namespace ToolUtils {
175
66
sk_sp<SkFontMgr> MakePortableFontMgr() { return sk_make_sp<FontMgr>(); }
176
}  // namespace ToolUtils