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