Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 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 "DMFontMgr.h" |
| 9 | #include "SkFontDescriptor.h" |
| 10 | #include "sk_tool_utils.h" |
| 11 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 12 | namespace { |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 13 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 14 | static constexpr const char* kFamilyNames[] = { |
| 15 | "Toy Liberation Sans", |
| 16 | "Toy Liberation Serif", |
| 17 | "Toy Liberation Mono", |
| 18 | }; |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 19 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 20 | class FontStyleSet final : public SkFontStyleSet { |
| 21 | public: |
| 22 | explicit FontStyleSet(int familyIndex) { |
Mike Klein | cb6940b | 2017-11-09 13:45:10 -0500 | [diff] [blame] | 23 | using sk_tool_utils::create_portable_typeface; |
| 24 | const char* familyName = kFamilyNames[familyIndex]; |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 25 | |
Mike Klein | cb6940b | 2017-11-09 13:45:10 -0500 | [diff] [blame] | 26 | 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 Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 32 | int count() override { return 4; } |
Mike Klein | cb6940b | 2017-11-09 13:45:10 -0500 | [diff] [blame] | 33 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 34 | void getStyle(int index, SkFontStyle* style, SkString* name) override { |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 35 | 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 Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 52 | SkTypeface* createTypeface(int index) override { |
Mike Klein | cb6940b | 2017-11-09 13:45:10 -0500 | [diff] [blame] | 53 | return SkRef(fTypefaces[index].get()); |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 56 | SkTypeface* matchStyle(const SkFontStyle& pattern) override { |
Mike Klein | cb6940b | 2017-11-09 13:45:10 -0500 | [diff] [blame] | 57 | return this->matchStyleCSS3(pattern); |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 60 | private: |
| 61 | sk_sp<SkTypeface> fTypefaces[4]; |
| 62 | }; |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 63 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 64 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // |
| 65 | |
| 66 | class FontMgr final : public SkFontMgr { |
| 67 | public: |
| 68 | FontMgr() { |
Mike Klein | cb6940b | 2017-11-09 13:45:10 -0500 | [diff] [blame] | 69 | 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 Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 74 | int onCountFamilies() const override { return SK_ARRAY_COUNT(fFamilies); } |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 75 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 76 | void onGetFamilyName(int index, SkString* familyName) const override { |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 77 | *familyName = kFamilyNames[index]; |
| 78 | } |
| 79 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 80 | SkFontStyleSet* onCreateStyleSet(int index) const override { |
Mike Klein | cb6940b | 2017-11-09 13:45:10 -0500 | [diff] [blame] | 81 | return SkRef(fFamilies[index].get()); |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 84 | SkFontStyleSet* onMatchFamily(const char familyName[]) const override { |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 85 | 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 Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 93 | |
| 94 | SkTypeface* onMatchFamilyStyle(const char familyName[], |
| 95 | const SkFontStyle& style) const override { |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 96 | sk_sp<SkFontStyleSet> styleSet(this->matchFamily(familyName)); |
| 97 | return styleSet->matchStyle(style); |
| 98 | } |
| 99 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 100 | SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], |
| 101 | const SkFontStyle& style, |
| 102 | const char* bcp47[], int bcp47Count, |
| 103 | SkUnichar character) const override { |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 104 | (void)bcp47; |
| 105 | (void)bcp47Count; |
| 106 | (void)character; |
| 107 | return this->matchFamilyStyle(familyName, style); |
| 108 | } |
| 109 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 110 | SkTypeface* onMatchFaceStyle(const SkTypeface* tf, |
| 111 | const SkFontStyle& style) const override { |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 112 | SkString familyName; |
| 113 | tf->getFamilyName(&familyName); |
| 114 | return this->matchFamilyStyle(familyName.c_str(), style); |
| 115 | } |
| 116 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 117 | 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 Klein | cb6940b | 2017-11-09 13:45:10 -0500 | [diff] [blame] | 132 | return nullptr; |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 135 | sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], |
| 136 | SkFontStyle style) const override { |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 137 | return sk_sp<SkTypeface>(this->matchFamilyStyle(familyName, style)); |
| 138 | } |
| 139 | |
Hal Canary | ccafca0 | 2017-11-14 10:34:48 -0500 | [diff] [blame] | 140 | private: |
| 141 | sk_sp<FontStyleSet> fFamilies[3]; |
| 142 | }; |
| 143 | } |
| 144 | |
| 145 | namespace DM { |
| 146 | sk_sp<SkFontMgr> MakeFontMgr() { return sk_make_sp<FontMgr>(); } |
Mike Klein | 3186821 | 2017-11-06 12:02:47 -0500 | [diff] [blame] | 147 | } // namespace DM |