bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 8 | #include "SkFontDescriptor.h" |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 9 | #include "SkFontMgr.h" |
| 10 | #include "SkLazyPtr.h" |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 11 | #include "SkStream.h" |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 12 | #include "SkTypes.h" |
| 13 | |
| 14 | class SkFontStyle; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 15 | class SkTypeface; |
| 16 | |
| 17 | class SkEmptyFontStyleSet : public SkFontStyleSet { |
| 18 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 19 | int count() override { return 0; } |
| 20 | void getStyle(int, SkFontStyle*, SkString*) override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 21 | SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set"); |
| 22 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 23 | SkTypeface* createTypeface(int index) override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 24 | SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set"); |
| 25 | return NULL; |
| 26 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | SkTypeface* matchStyle(const SkFontStyle&) override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 28 | return NULL; |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | SkFontStyleSet* SkFontStyleSet::CreateEmpty() { |
| 33 | return SkNEW(SkEmptyFontStyleSet); |
| 34 | } |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | |
| 38 | class SkEmptyFontMgr : public SkFontMgr { |
| 39 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 40 | int onCountFamilies() const override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 41 | return 0; |
| 42 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | void onGetFamilyName(int index, SkString* familyName) const override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 44 | SkDEBUGFAIL("onGetFamilyName called with bad index"); |
| 45 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | SkFontStyleSet* onCreateStyleSet(int index) const override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 47 | SkDEBUGFAIL("onCreateStyleSet called with bad index"); |
| 48 | return NULL; |
| 49 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 50 | SkFontStyleSet* onMatchFamily(const char[]) const override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 51 | return SkFontStyleSet::CreateEmpty(); |
| 52 | } |
| 53 | |
| 54 | virtual SkTypeface* onMatchFamilyStyle(const char[], |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 55 | const SkFontStyle&) const override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 56 | return NULL; |
| 57 | } |
| 58 | virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], |
| 59 | const SkFontStyle& style, |
| 60 | const char* bcp47[], |
| 61 | int bcp47Count, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 62 | SkUnichar character) const override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 63 | return NULL; |
| 64 | } |
| 65 | virtual SkTypeface* onMatchFaceStyle(const SkTypeface*, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 66 | const SkFontStyle&) const override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 67 | return NULL; |
| 68 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 69 | SkTypeface* onCreateFromData(SkData*, int) const override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 70 | return NULL; |
| 71 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | SkTypeface* onCreateFromStream(SkStreamAsset* stream, int) const override { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 73 | SkDELETE(stream); |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 74 | return NULL; |
| 75 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 76 | SkTypeface* onCreateFromFile(const char[], int) const override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 77 | return NULL; |
| 78 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 79 | SkTypeface* onLegacyCreateTypeface(const char [], unsigned) const override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 80 | return NULL; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) { |
| 85 | if (NULL == fsset) { |
| 86 | fsset = SkFontStyleSet::CreateEmpty(); |
| 87 | } |
| 88 | return fsset; |
| 89 | } |
| 90 | |
| 91 | int SkFontMgr::countFamilies() const { |
| 92 | return this->onCountFamilies(); |
| 93 | } |
| 94 | |
| 95 | void SkFontMgr::getFamilyName(int index, SkString* familyName) const { |
| 96 | this->onGetFamilyName(index, familyName); |
| 97 | } |
| 98 | |
| 99 | SkFontStyleSet* SkFontMgr::createStyleSet(int index) const { |
| 100 | return emptyOnNull(this->onCreateStyleSet(index)); |
| 101 | } |
| 102 | |
| 103 | SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const { |
| 104 | return emptyOnNull(this->onMatchFamily(familyName)); |
| 105 | } |
| 106 | |
| 107 | SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[], |
| 108 | const SkFontStyle& fs) const { |
| 109 | return this->onMatchFamilyStyle(familyName, fs); |
| 110 | } |
| 111 | |
| 112 | SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style, |
| 113 | const char* bcp47[], int bcp47Count, |
| 114 | SkUnichar character) const { |
| 115 | return this->onMatchFamilyStyleCharacter(familyName, style, bcp47, bcp47Count, character); |
| 116 | } |
| 117 | |
| 118 | SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face, |
| 119 | const SkFontStyle& fs) const { |
| 120 | return this->onMatchFaceStyle(face, fs); |
| 121 | } |
| 122 | |
| 123 | SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const { |
| 124 | if (NULL == data) { |
| 125 | return NULL; |
| 126 | } |
| 127 | return this->onCreateFromData(data, ttcIndex); |
| 128 | } |
| 129 | |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 130 | SkTypeface* SkFontMgr::createFromStream(SkStreamAsset* stream, int ttcIndex) const { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 131 | if (NULL == stream) { |
| 132 | return NULL; |
| 133 | } |
| 134 | return this->onCreateFromStream(stream, ttcIndex); |
| 135 | } |
| 136 | |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 137 | SkTypeface* SkFontMgr::createFromFontData(SkFontData* data) const { |
| 138 | if (NULL == data) { |
| 139 | return NULL; |
| 140 | } |
| 141 | return this->onCreateFromFontData(data); |
| 142 | } |
| 143 | |
| 144 | // This implementation is temporary until it can be made pure virtual. |
| 145 | SkTypeface* SkFontMgr::onCreateFromFontData(SkFontData* data) const { |
| 146 | SkTypeface* ret = this->createFromStream(data->detachStream(), data->getIndex()); |
| 147 | delete data; |
| 148 | return ret; |
| 149 | } |
| 150 | |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 151 | SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const { |
| 152 | if (NULL == path) { |
| 153 | return NULL; |
| 154 | } |
| 155 | return this->onCreateFromFile(path, ttcIndex); |
| 156 | } |
| 157 | |
| 158 | SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[], |
| 159 | unsigned styleBits) const { |
| 160 | return this->onLegacyCreateTypeface(familyName, styleBits); |
| 161 | } |
| 162 | |
| 163 | // As a template argument this must have external linkage. |
| 164 | SkFontMgr* sk_fontmgr_create_default() { |
| 165 | SkFontMgr* fm = SkFontMgr::Factory(); |
| 166 | return fm ? fm : SkNEW(SkEmptyFontMgr); |
| 167 | } |
| 168 | |
| 169 | SK_DECLARE_STATIC_LAZY_PTR(SkFontMgr, singleton, sk_fontmgr_create_default); |
| 170 | |
| 171 | SkFontMgr* SkFontMgr::RefDefault() { |
| 172 | return SkRef(singleton.get()); |
| 173 | } |