blob: 4513a2f8d1f358c28b456b85f332fbeded05dc2b [file] [log] [blame]
Mike Klein31868212017-11-06 12:02:47 -05001/*
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
Mike Klein31868212017-11-06 12:02:47 -05008#include "SkFontDescriptor.h"
Ben Wagner483c7722018-02-20 17:06:07 -05009#include "SkTestFontMgr.h"
Mike Klein31868212017-11-06 12:02:47 -050010#include "sk_tool_utils.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050011#include "SkTestTypeface.h"
12#include "SkTestSVGTypeface.h"
Mike Klein31868212017-11-06 12:02:47 -050013
Hal Canaryccafca02017-11-14 10:34:48 -050014namespace {
Mike Klein31868212017-11-06 12:02:47 -050015
Hal Canaryccafca02017-11-14 10:34:48 -050016static constexpr const char* kFamilyNames[] = {
17 "Toy Liberation Sans",
18 "Toy Liberation Serif",
19 "Toy Liberation Mono",
Ben Wagner97182cc2018-02-15 10:20:04 -050020 "Emoji",
21};
22
23class JustOneTypefaceStyleSet final : public SkFontStyleSet {
24public:
25 explicit JustOneTypefaceStyleSet(sk_sp<SkTypeface> typeface) : fTypeface(std::move(typeface)) {}
26 int count() override { return 1; }
27
28 void getStyle(int index, SkFontStyle* style, SkString* name) override {
29 if (style) { *style = SkFontStyle::Normal(); }
30 if (name) { *name = "Normal"; }
31 }
32
33 SkTypeface* createTypeface(int index) override {
34 return SkRef(fTypeface.get());
35 }
36
37 SkTypeface* matchStyle(const SkFontStyle& pattern) override {
38 return this->matchStyleCSS3(pattern);
39 }
40private:
41 sk_sp<SkTypeface> fTypeface;
Hal Canaryccafca02017-11-14 10:34:48 -050042};
Mike Klein31868212017-11-06 12:02:47 -050043
Hal Canaryccafca02017-11-14 10:34:48 -050044class FontStyleSet final : public SkFontStyleSet {
45public:
46 explicit FontStyleSet(int familyIndex) {
Mike Kleincb6940b2017-11-09 13:45:10 -050047 using sk_tool_utils::create_portable_typeface;
48 const char* familyName = kFamilyNames[familyIndex];
Mike Klein31868212017-11-06 12:02:47 -050049
Mike Kleincb6940b2017-11-09 13:45:10 -050050 fTypefaces[0] = create_portable_typeface(familyName, SkFontStyle::Normal());
51 fTypefaces[1] = create_portable_typeface(familyName, SkFontStyle::Bold());
52 fTypefaces[2] = create_portable_typeface(familyName, SkFontStyle::Italic());
53 fTypefaces[3] = create_portable_typeface(familyName, SkFontStyle::BoldItalic());
54 }
55
Hal Canaryccafca02017-11-14 10:34:48 -050056 int count() override { return 4; }
Mike Kleincb6940b2017-11-09 13:45:10 -050057
Hal Canaryccafca02017-11-14 10:34:48 -050058 void getStyle(int index, SkFontStyle* style, SkString* name) override {
Mike Klein31868212017-11-06 12:02:47 -050059 switch (index) {
60 default:
61 case 0: if (style) { *style = SkFontStyle::Normal(); }
62 if (name) { *name = "Normal"; }
63 break;
64 case 1: if (style) { *style = SkFontStyle::Bold(); }
65 if (name) { *name = "Bold"; }
66 break;
67 case 2: if (style) { *style = SkFontStyle::Italic(); }
68 if (name) { *name = "Italic"; }
69 break;
70 case 3: if (style) { *style = SkFontStyle::BoldItalic(); }
71 if (name) { *name = "BoldItalic"; }
72 break;
73 }
74 }
75
Hal Canaryccafca02017-11-14 10:34:48 -050076 SkTypeface* createTypeface(int index) override {
Mike Kleincb6940b2017-11-09 13:45:10 -050077 return SkRef(fTypefaces[index].get());
Mike Klein31868212017-11-06 12:02:47 -050078 }
79
Hal Canaryccafca02017-11-14 10:34:48 -050080 SkTypeface* matchStyle(const SkFontStyle& pattern) override {
Mike Kleincb6940b2017-11-09 13:45:10 -050081 return this->matchStyleCSS3(pattern);
Mike Klein31868212017-11-06 12:02:47 -050082 }
83
Hal Canaryccafca02017-11-14 10:34:48 -050084private:
85 sk_sp<SkTypeface> fTypefaces[4];
86};
Mike Klein31868212017-11-06 12:02:47 -050087
Hal Canaryccafca02017-11-14 10:34:48 -050088// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
89
90class FontMgr final : public SkFontMgr {
91public:
92 FontMgr() {
Mike Kleincb6940b2017-11-09 13:45:10 -050093 fFamilies[0] = sk_make_sp<FontStyleSet>(0);
94 fFamilies[1] = sk_make_sp<FontStyleSet>(1);
95 fFamilies[2] = sk_make_sp<FontStyleSet>(2);
Ben Wagner97182cc2018-02-15 10:20:04 -050096 fFamilies[3] = sk_make_sp<JustOneTypefaceStyleSet>(SkTestSVGTypeface::Default());
Mike Kleincb6940b2017-11-09 13:45:10 -050097 }
98
Hal Canaryccafca02017-11-14 10:34:48 -050099 int onCountFamilies() const override { return SK_ARRAY_COUNT(fFamilies); }
Mike Klein31868212017-11-06 12:02:47 -0500100
Hal Canaryccafca02017-11-14 10:34:48 -0500101 void onGetFamilyName(int index, SkString* familyName) const override {
Mike Klein31868212017-11-06 12:02:47 -0500102 *familyName = kFamilyNames[index];
103 }
104
Hal Canaryccafca02017-11-14 10:34:48 -0500105 SkFontStyleSet* onCreateStyleSet(int index) const override {
Mike Kleincb6940b2017-11-09 13:45:10 -0500106 return SkRef(fFamilies[index].get());
Mike Klein31868212017-11-06 12:02:47 -0500107 }
108
Hal Canaryccafca02017-11-14 10:34:48 -0500109 SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
Mike Klein31868212017-11-06 12:02:47 -0500110 if (familyName) {
111 if (strstr(familyName, "ans")) { return this->createStyleSet(0); }
112 if (strstr(familyName, "erif")) { return this->createStyleSet(1); }
113 if (strstr(familyName, "ono")) { return this->createStyleSet(2); }
Ben Wagner97182cc2018-02-15 10:20:04 -0500114 if (strstr(familyName, "oji")) { return this->createStyleSet(3); }
Mike Klein31868212017-11-06 12:02:47 -0500115 }
116 return this->createStyleSet(0);
117 }
118
Hal Canaryccafca02017-11-14 10:34:48 -0500119
120 SkTypeface* onMatchFamilyStyle(const char familyName[],
121 const SkFontStyle& style) const override {
Mike Klein31868212017-11-06 12:02:47 -0500122 sk_sp<SkFontStyleSet> styleSet(this->matchFamily(familyName));
123 return styleSet->matchStyle(style);
124 }
125
Hal Canaryccafca02017-11-14 10:34:48 -0500126 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
127 const SkFontStyle& style,
128 const char* bcp47[], int bcp47Count,
129 SkUnichar character) const override {
Mike Klein31868212017-11-06 12:02:47 -0500130 (void)bcp47;
131 (void)bcp47Count;
132 (void)character;
133 return this->matchFamilyStyle(familyName, style);
134 }
135
Hal Canaryccafca02017-11-14 10:34:48 -0500136 SkTypeface* onMatchFaceStyle(const SkTypeface* tf,
137 const SkFontStyle& style) const override {
Mike Klein31868212017-11-06 12:02:47 -0500138 SkString familyName;
139 tf->getFamilyName(&familyName);
140 return this->matchFamilyStyle(familyName.c_str(), style);
141 }
142
Hal Canaryccafca02017-11-14 10:34:48 -0500143 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override {
144 return nullptr;
145 }
146 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
147 int ttcIndex) const override {
148 return nullptr;
149 }
150 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
151 const SkFontArguments&) const override {
152 return nullptr;
153 }
154 sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData>) const override {
155 return nullptr;
156 }
157 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
Mike Kleincb6940b2017-11-09 13:45:10 -0500158 return nullptr;
Mike Klein31868212017-11-06 12:02:47 -0500159 }
160
Hal Canaryccafca02017-11-14 10:34:48 -0500161 sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[],
162 SkFontStyle style) const override {
Mike Klein31868212017-11-06 12:02:47 -0500163 return sk_sp<SkTypeface>(this->matchFamilyStyle(familyName, style));
164 }
165
Hal Canaryccafca02017-11-14 10:34:48 -0500166private:
Ben Wagner97182cc2018-02-15 10:20:04 -0500167 sk_sp<SkFontStyleSet> fFamilies[4];
Hal Canaryccafca02017-11-14 10:34:48 -0500168};
169}
170
Ben Wagner483c7722018-02-20 17:06:07 -0500171namespace sk_tool_utils {
172sk_sp<SkFontMgr> MakePortableFontMgr() { return sk_make_sp<FontMgr>(); }
173} // namespace sk_tool_utils