blob: 3b5d1589f2b471439d7bf11c02da14f0b913784d [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"
11
Hal Canaryccafca02017-11-14 10:34:48 -050012namespace {
Mike Klein31868212017-11-06 12:02:47 -050013
Hal Canaryccafca02017-11-14 10:34:48 -050014static constexpr const char* kFamilyNames[] = {
15 "Toy Liberation Sans",
16 "Toy Liberation Serif",
17 "Toy Liberation Mono",
18};
Mike Klein31868212017-11-06 12:02:47 -050019
Hal Canaryccafca02017-11-14 10:34:48 -050020class FontStyleSet final : public SkFontStyleSet {
21public:
22 explicit FontStyleSet(int familyIndex) {
Mike Kleincb6940b2017-11-09 13:45:10 -050023 using sk_tool_utils::create_portable_typeface;
24 const char* familyName = kFamilyNames[familyIndex];
Mike Klein31868212017-11-06 12:02:47 -050025
Mike Kleincb6940b2017-11-09 13:45:10 -050026 fTypefaces[0] = create_portable_typeface(familyName, SkFontStyle::Normal());
27 fTypefaces[1] = create_portable_typeface(familyName, SkFontStyle::Bold());
28 fTypefaces[2] = create_portable_typeface(familyName, SkFontStyle::Italic());
29 fTypefaces[3] = create_portable_typeface(familyName, SkFontStyle::BoldItalic());
30 }
31
Hal Canaryccafca02017-11-14 10:34:48 -050032 int count() override { return 4; }
Mike Kleincb6940b2017-11-09 13:45:10 -050033
Hal Canaryccafca02017-11-14 10:34:48 -050034 void getStyle(int index, SkFontStyle* style, SkString* name) override {
Mike Klein31868212017-11-06 12:02:47 -050035 switch (index) {
36 default:
37 case 0: if (style) { *style = SkFontStyle::Normal(); }
38 if (name) { *name = "Normal"; }
39 break;
40 case 1: if (style) { *style = SkFontStyle::Bold(); }
41 if (name) { *name = "Bold"; }
42 break;
43 case 2: if (style) { *style = SkFontStyle::Italic(); }
44 if (name) { *name = "Italic"; }
45 break;
46 case 3: if (style) { *style = SkFontStyle::BoldItalic(); }
47 if (name) { *name = "BoldItalic"; }
48 break;
49 }
50 }
51
Hal Canaryccafca02017-11-14 10:34:48 -050052 SkTypeface* createTypeface(int index) override {
Mike Kleincb6940b2017-11-09 13:45:10 -050053 return SkRef(fTypefaces[index].get());
Mike Klein31868212017-11-06 12:02:47 -050054 }
55
Hal Canaryccafca02017-11-14 10:34:48 -050056 SkTypeface* matchStyle(const SkFontStyle& pattern) override {
Mike Kleincb6940b2017-11-09 13:45:10 -050057 return this->matchStyleCSS3(pattern);
Mike Klein31868212017-11-06 12:02:47 -050058 }
59
Hal Canaryccafca02017-11-14 10:34:48 -050060private:
61 sk_sp<SkTypeface> fTypefaces[4];
62};
Mike Klein31868212017-11-06 12:02:47 -050063
Hal Canaryccafca02017-11-14 10:34:48 -050064// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
65
66class FontMgr final : public SkFontMgr {
67public:
68 FontMgr() {
Mike Kleincb6940b2017-11-09 13:45:10 -050069 fFamilies[0] = sk_make_sp<FontStyleSet>(0);
70 fFamilies[1] = sk_make_sp<FontStyleSet>(1);
71 fFamilies[2] = sk_make_sp<FontStyleSet>(2);
72 }
73
Hal Canaryccafca02017-11-14 10:34:48 -050074 int onCountFamilies() const override { return SK_ARRAY_COUNT(fFamilies); }
Mike Klein31868212017-11-06 12:02:47 -050075
Hal Canaryccafca02017-11-14 10:34:48 -050076 void onGetFamilyName(int index, SkString* familyName) const override {
Mike Klein31868212017-11-06 12:02:47 -050077 *familyName = kFamilyNames[index];
78 }
79
Hal Canaryccafca02017-11-14 10:34:48 -050080 SkFontStyleSet* onCreateStyleSet(int index) const override {
Mike Kleincb6940b2017-11-09 13:45:10 -050081 return SkRef(fFamilies[index].get());
Mike Klein31868212017-11-06 12:02:47 -050082 }
83
Hal Canaryccafca02017-11-14 10:34:48 -050084 SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
Mike Klein31868212017-11-06 12:02:47 -050085 if (familyName) {
86 if (strstr(familyName, "ans")) { return this->createStyleSet(0); }
87 if (strstr(familyName, "erif")) { return this->createStyleSet(1); }
88 if (strstr(familyName, "ono")) { return this->createStyleSet(2); }
89 }
90 return this->createStyleSet(0);
91 }
92
Hal Canaryccafca02017-11-14 10:34:48 -050093
94 SkTypeface* onMatchFamilyStyle(const char familyName[],
95 const SkFontStyle& style) const override {
Mike Klein31868212017-11-06 12:02:47 -050096 sk_sp<SkFontStyleSet> styleSet(this->matchFamily(familyName));
97 return styleSet->matchStyle(style);
98 }
99
Hal Canaryccafca02017-11-14 10:34:48 -0500100 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
101 const SkFontStyle& style,
102 const char* bcp47[], int bcp47Count,
103 SkUnichar character) const override {
Mike Klein31868212017-11-06 12:02:47 -0500104 (void)bcp47;
105 (void)bcp47Count;
106 (void)character;
107 return this->matchFamilyStyle(familyName, style);
108 }
109
Hal Canaryccafca02017-11-14 10:34:48 -0500110 SkTypeface* onMatchFaceStyle(const SkTypeface* tf,
111 const SkFontStyle& style) const override {
Mike Klein31868212017-11-06 12:02:47 -0500112 SkString familyName;
113 tf->getFamilyName(&familyName);
114 return this->matchFamilyStyle(familyName.c_str(), style);
115 }
116
Hal Canaryccafca02017-11-14 10:34:48 -0500117 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override {
118 return nullptr;
119 }
120 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
121 int ttcIndex) const override {
122 return nullptr;
123 }
124 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
125 const SkFontArguments&) const override {
126 return nullptr;
127 }
128 sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData>) const override {
129 return nullptr;
130 }
131 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
Mike Kleincb6940b2017-11-09 13:45:10 -0500132 return nullptr;
Mike Klein31868212017-11-06 12:02:47 -0500133 }
134
Hal Canaryccafca02017-11-14 10:34:48 -0500135 sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[],
136 SkFontStyle style) const override {
Mike Klein31868212017-11-06 12:02:47 -0500137 return sk_sp<SkTypeface>(this->matchFamilyStyle(familyName, style));
138 }
139
Hal Canaryccafca02017-11-14 10:34:48 -0500140private:
Jim Van Verthaf78a942018-03-23 14:39:42 +0000141 sk_sp<FontStyleSet> fFamilies[3];
Hal Canaryccafca02017-11-14 10:34:48 -0500142};
143}
144
Ben Wagner483c7722018-02-20 17:06:07 -0500145namespace sk_tool_utils {
146sk_sp<SkFontMgr> MakePortableFontMgr() { return sk_make_sp<FontMgr>(); }
147} // namespace sk_tool_utils