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 | |
| 12 | namespace DM { |
| 13 | |
| 14 | static constexpr const char* kFamilyNames[] = { |
| 15 | "Toy Liberation Sans", |
| 16 | "Toy Liberation Serif", |
| 17 | "Toy Liberation Mono", |
| 18 | }; |
| 19 | |
| 20 | FontStyleSet::FontStyleSet(int familyIndex) : fFamilyName(kFamilyNames[familyIndex]) {} |
| 21 | |
| 22 | // Each font family has 4 styles: Normal, Bold, Italic, BoldItalic. |
| 23 | int FontStyleSet::count() { return 4; } |
| 24 | void FontStyleSet::getStyle(int index, SkFontStyle* style, SkString* name) { |
| 25 | switch (index) { |
| 26 | default: |
| 27 | case 0: if (style) { *style = SkFontStyle::Normal(); } |
| 28 | if (name) { *name = "Normal"; } |
| 29 | break; |
| 30 | case 1: if (style) { *style = SkFontStyle::Bold(); } |
| 31 | if (name) { *name = "Bold"; } |
| 32 | break; |
| 33 | case 2: if (style) { *style = SkFontStyle::Italic(); } |
| 34 | if (name) { *name = "Italic"; } |
| 35 | break; |
| 36 | case 3: if (style) { *style = SkFontStyle::BoldItalic(); } |
| 37 | if (name) { *name = "BoldItalic"; } |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | SkTypeface* FontStyleSet::matchStyle(const SkFontStyle& style) { |
| 43 | return this->matchStyleCSS3(style); |
| 44 | } |
| 45 | |
| 46 | SkTypeface* FontStyleSet::createTypeface(int index) { |
| 47 | SkFontStyle style; |
| 48 | this->getStyle(index, &style, nullptr); |
| 49 | |
| 50 | return sk_tool_utils::create_portable_typeface(fFamilyName, style).release(); |
| 51 | } |
| 52 | |
| 53 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // |
| 54 | |
| 55 | int FontMgr::onCountFamilies() const { return SK_ARRAY_COUNT(kFamilyNames); } |
| 56 | |
| 57 | void FontMgr::onGetFamilyName(int index, SkString* familyName) const { |
| 58 | *familyName = kFamilyNames[index]; |
| 59 | } |
| 60 | |
| 61 | SkFontStyleSet* FontMgr::onCreateStyleSet(int index) const { |
| 62 | return new FontStyleSet(index); |
| 63 | } |
| 64 | |
| 65 | SkFontStyleSet* FontMgr::onMatchFamily(const char familyName[]) const { |
| 66 | if (familyName) { |
| 67 | if (strstr(familyName, "ans")) { return this->createStyleSet(0); } |
| 68 | if (strstr(familyName, "erif")) { return this->createStyleSet(1); } |
| 69 | if (strstr(familyName, "ono")) { return this->createStyleSet(2); } |
| 70 | } |
| 71 | return this->createStyleSet(0); |
| 72 | } |
| 73 | |
| 74 | SkTypeface* FontMgr::onMatchFamilyStyle(const char familyName[], |
| 75 | const SkFontStyle& style) const { |
| 76 | sk_sp<SkFontStyleSet> styleSet(this->matchFamily(familyName)); |
| 77 | return styleSet->matchStyle(style); |
| 78 | } |
| 79 | |
| 80 | SkTypeface* FontMgr::onMatchFamilyStyleCharacter(const char familyName[], |
| 81 | const SkFontStyle& style, |
| 82 | const char* bcp47[], |
| 83 | int bcp47Count, |
| 84 | SkUnichar character) const { |
| 85 | (void)bcp47; |
| 86 | (void)bcp47Count; |
| 87 | (void)character; |
| 88 | return this->matchFamilyStyle(familyName, style); |
| 89 | } |
| 90 | |
| 91 | SkTypeface* FontMgr::onMatchFaceStyle(const SkTypeface* tf, |
| 92 | const SkFontStyle& style) const { |
| 93 | SkString familyName; |
| 94 | tf->getFamilyName(&familyName); |
| 95 | return this->matchFamilyStyle(familyName.c_str(), style); |
| 96 | } |
| 97 | |
| 98 | sk_sp<SkTypeface> FontMgr::onMakeFromData(sk_sp<SkData>, int ttcIndex) const { |
| 99 | return sk_sp<SkTypeface>(this->matchFamilyStyle("Sans", SkFontStyle::Normal())); |
| 100 | } |
| 101 | |
| 102 | sk_sp<SkTypeface> FontMgr::onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, |
| 103 | int ttcIndex) const { |
| 104 | return sk_sp<SkTypeface>(this->matchFamilyStyle("Sans", SkFontStyle::Normal())); |
| 105 | } |
| 106 | |
| 107 | sk_sp<SkTypeface> FontMgr::onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>, |
| 108 | const SkFontArguments&) const { |
| 109 | return sk_sp<SkTypeface>(this->matchFamilyStyle("Sans", SkFontStyle::Normal())); |
| 110 | } |
| 111 | |
| 112 | sk_sp<SkTypeface> FontMgr::onMakeFromFontData(std::unique_ptr<SkFontData>) const { |
| 113 | return sk_sp<SkTypeface>(this->matchFamilyStyle("Sans", SkFontStyle::Normal())); |
| 114 | } |
| 115 | |
| 116 | sk_sp<SkTypeface> FontMgr::onMakeFromFile(const char path[], int ttcIndex) const { |
| 117 | return sk_sp<SkTypeface>(this->matchFamilyStyle("Sans", SkFontStyle::Normal())); |
| 118 | } |
| 119 | |
| 120 | sk_sp<SkTypeface> FontMgr::onLegacyMakeTypeface(const char familyName[], |
| 121 | SkFontStyle style) const { |
| 122 | return sk_sp<SkTypeface>(this->matchFamilyStyle(familyName, style)); |
| 123 | } |
| 124 | |
| 125 | } // namespace DM |