epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "SkFontHost.h" |
reed@google.com | 0fc17c3 | 2013-03-21 13:33:49 +0000 | [diff] [blame] | 9 | #include "SkFontHost_FreeType_common.h" |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 10 | #include "SkFontDescriptor.h" |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 11 | #include "SkFontMgr.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkDescriptor.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkOSFile.h" |
| 14 | #include "SkPaint.h" |
humper | ad5e9a5 | 2014-11-19 08:32:19 -0800 | [diff] [blame] | 15 | #include "SkRTConf.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkString.h" |
| 17 | #include "SkStream.h" |
| 18 | #include "SkThread.h" |
| 19 | #include "SkTSearch.h" |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 20 | #include "SkTypefaceCache.h" |
| 21 | #include "SkTArray.h" |
| 22 | |
| 23 | #include <limits> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | #ifndef SK_FONT_FILE_PREFIX |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 26 | # define SK_FONT_FILE_PREFIX "/usr/share/fonts/" |
bungeman@google.com | 2cf84ec | 2012-09-26 19:16:54 +0000 | [diff] [blame] | 27 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 31 | /** The base SkTypeface implementation for the custom font manager. */ |
| 32 | class SkTypeface_Custom : public SkTypeface_FreeType { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | public: |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 34 | SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch, |
bungeman | d71b757 | 2014-09-18 10:55:32 -0700 | [diff] [blame] | 35 | bool sysFont, const SkString familyName, int index) |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 36 | : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) |
bungeman | d71b757 | 2014-09-18 10:55:32 -0700 | [diff] [blame] | 37 | , fIsSysFont(sysFont), fFamilyName(familyName), fIndex(index) |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 38 | { } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 39 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | bool isSysFont() const { return fIsSysFont; } |
reed@google.com | 292b1d4 | 2013-03-22 17:21:59 +0000 | [diff] [blame] | 41 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | virtual const char* getUniqueString() const = 0; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 43 | |
reed@google.com | 5526ede | 2013-03-25 13:03:37 +0000 | [diff] [blame] | 44 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 45 | void onGetFamilyName(SkString* familyName) const SK_OVERRIDE { |
bungeman | b374d6a | 2014-09-17 07:48:59 -0700 | [diff] [blame] | 46 | *familyName = fFamilyName; |
| 47 | } |
| 48 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 49 | void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const SK_OVERRIDE { |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 50 | desc->setFamilyName(fFamilyName.c_str()); |
| 51 | desc->setFontFileName(this->getUniqueString()); |
bungeman | d71b757 | 2014-09-18 10:55:32 -0700 | [diff] [blame] | 52 | desc->setFontIndex(fIndex); |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 53 | *isLocal = !this->isSysFont(); |
| 54 | } |
reed@google.com | 5526ede | 2013-03-25 13:03:37 +0000 | [diff] [blame] | 55 | |
bungeman | d71b757 | 2014-09-18 10:55:32 -0700 | [diff] [blame] | 56 | int getIndex() const { return fIndex; } |
| 57 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | private: |
bungeman | d71b757 | 2014-09-18 10:55:32 -0700 | [diff] [blame] | 59 | const bool fIsSysFont; |
| 60 | const SkString fFamilyName; |
| 61 | const int fIndex; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 62 | |
reed@google.com | 032fbb8 | 2013-03-21 13:38:18 +0000 | [diff] [blame] | 63 | typedef SkTypeface_FreeType INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 66 | /** The empty SkTypeface implementation for the custom font manager. |
| 67 | * Used as the last resort fallback typeface. |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 68 | */ |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 69 | class SkTypeface_Empty : public SkTypeface_Custom { |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 70 | public: |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 71 | SkTypeface_Empty() : INHERITED(SkFontStyle(), false, true, SkString(), 0) {} |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 72 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 73 | const char* getUniqueString() const SK_OVERRIDE { return NULL; } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 74 | |
reed@google.com | 292b1d4 | 2013-03-22 17:21:59 +0000 | [diff] [blame] | 75 | protected: |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame^] | 76 | SkStreamAsset* onOpenStream(int*) const SK_OVERRIDE { return NULL; } |
reed@google.com | 292b1d4 | 2013-03-22 17:21:59 +0000 | [diff] [blame] | 77 | |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 78 | private: |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 79 | typedef SkTypeface_Custom INHERITED; |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 82 | /** The stream SkTypeface implementation for the custom font manager. */ |
| 83 | class SkTypeface_Stream : public SkTypeface_Custom { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | public: |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 85 | SkTypeface_Stream(const SkFontStyle& style, bool isFixedPitch, bool sysFont, |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame^] | 86 | const SkString familyName, SkStreamAsset* stream, int index) |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 87 | : INHERITED(style, isFixedPitch, sysFont, familyName, index) |
scroggo | e58898e | 2015-01-21 12:23:20 -0800 | [diff] [blame] | 88 | , fStream(stream) |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 89 | { } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 90 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 91 | const char* getUniqueString() const SK_OVERRIDE { return NULL; } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 92 | |
reed@google.com | 292b1d4 | 2013-03-22 17:21:59 +0000 | [diff] [blame] | 93 | protected: |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame^] | 94 | SkStreamAsset* onOpenStream(int* ttcIndex) const SK_OVERRIDE { |
bungeman | d71b757 | 2014-09-18 10:55:32 -0700 | [diff] [blame] | 95 | *ttcIndex = this->getIndex(); |
commit-bot@chromium.org | a2b44dc | 2014-03-24 21:42:01 +0000 | [diff] [blame] | 96 | return fStream->duplicate(); |
reed@google.com | 292b1d4 | 2013-03-22 17:21:59 +0000 | [diff] [blame] | 97 | } |
| 98 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | private: |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame^] | 100 | const SkAutoTDelete<const SkStreamAsset> fStream; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 101 | |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 102 | typedef SkTypeface_Custom INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
humper | ad5e9a5 | 2014-11-19 08:32:19 -0800 | [diff] [blame] | 105 | // This configuration option is useful if we need to open and hold handles to |
| 106 | // all found system font data (e.g., for skfiddle, where the application can't |
| 107 | // access the filesystem to read fonts on demand) |
| 108 | |
| 109 | SK_CONF_DECLARE(bool, c_CustomTypefaceRetain, "fonts.customFont.retainAllData", false, |
| 110 | "Retain the open stream for each found font on the system."); |
| 111 | |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 112 | /** The file SkTypeface implementation for the custom font manager. */ |
| 113 | class SkTypeface_File : public SkTypeface_Custom { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | public: |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 115 | SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont, |
| 116 | const SkString familyName, const char path[], int index) |
bungeman | d71b757 | 2014-09-18 10:55:32 -0700 | [diff] [blame] | 117 | : INHERITED(style, isFixedPitch, sysFont, familyName, index) |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 118 | , fPath(path) |
humper | ad5e9a5 | 2014-11-19 08:32:19 -0800 | [diff] [blame] | 119 | , fStream(c_CustomTypefaceRetain ? SkStream::NewFromFile(fPath.c_str()) : NULL) |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 120 | { } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 121 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 122 | const char* getUniqueString() const SK_OVERRIDE { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | const char* str = strrchr(fPath.c_str(), '/'); |
| 124 | if (str) { |
| 125 | str += 1; // skip the '/' |
| 126 | } |
| 127 | return str; |
| 128 | } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 129 | |
reed@google.com | 292b1d4 | 2013-03-22 17:21:59 +0000 | [diff] [blame] | 130 | protected: |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame^] | 131 | SkStreamAsset* onOpenStream(int* ttcIndex) const SK_OVERRIDE { |
bungeman | d71b757 | 2014-09-18 10:55:32 -0700 | [diff] [blame] | 132 | *ttcIndex = this->getIndex(); |
humper | ad5e9a5 | 2014-11-19 08:32:19 -0800 | [diff] [blame] | 133 | if (fStream.get()) { |
| 134 | return fStream->duplicate(); |
| 135 | } else { |
| 136 | return SkStream::NewFromFile(fPath.c_str()); |
| 137 | } |
reed@google.com | 292b1d4 | 2013-03-22 17:21:59 +0000 | [diff] [blame] | 138 | } |
| 139 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | private: |
| 141 | SkString fPath; |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 142 | const SkAutoTDelete<SkStreamAsset> fStream; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 143 | |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 144 | typedef SkTypeface_Custom INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 149 | /** |
| 150 | * SkFontStyleSet_Custom |
| 151 | * |
| 152 | * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families. |
| 153 | */ |
| 154 | class SkFontStyleSet_Custom : public SkFontStyleSet { |
| 155 | public: |
| 156 | explicit SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(familyName) { } |
| 157 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 158 | int count() SK_OVERRIDE { |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 159 | return fStyles.count(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 162 | void getStyle(int index, SkFontStyle* style, SkString* name) SK_OVERRIDE { |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 163 | SkASSERT(index < fStyles.count()); |
| 164 | bool bold = fStyles[index]->isBold(); |
| 165 | bool italic = fStyles[index]->isItalic(); |
| 166 | *style = SkFontStyle(bold ? SkFontStyle::kBold_Weight : SkFontStyle::kNormal_Weight, |
| 167 | SkFontStyle::kNormal_Width, |
| 168 | italic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant); |
| 169 | name->reset(); |
| 170 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 171 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 172 | SkTypeface* createTypeface(int index) SK_OVERRIDE { |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 173 | SkASSERT(index < fStyles.count()); |
| 174 | return SkRef(fStyles[index].get()); |
| 175 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 177 | static int match_score(const SkFontStyle& pattern, const SkFontStyle& candidate) { |
| 178 | int score = 0; |
| 179 | score += (pattern.width() - candidate.width()) * 100; |
| 180 | score += (pattern.isItalic() == candidate.isItalic()) ? 0 : 1000; |
| 181 | score += pattern.weight() - candidate.weight(); |
| 182 | return score; |
| 183 | } |
| 184 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 185 | SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE { |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 186 | if (0 == fStyles.count()) { |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | SkTypeface_Custom* closest = fStyles[0]; |
| 191 | int minScore = std::numeric_limits<int>::max(); |
| 192 | for (int i = 0; i < fStyles.count(); ++i) { |
| 193 | bool bold = fStyles[i]->isBold(); |
| 194 | bool italic = fStyles[i]->isItalic(); |
| 195 | SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight |
| 196 | : SkFontStyle::kNormal_Weight, |
| 197 | SkFontStyle::kNormal_Width, |
| 198 | italic ? SkFontStyle::kItalic_Slant |
| 199 | : SkFontStyle::kUpright_Slant); |
| 200 | |
| 201 | int score = match_score(pattern, style); |
| 202 | if (score < minScore) { |
| 203 | closest = fStyles[i]; |
| 204 | minScore = score; |
| 205 | } |
| 206 | } |
| 207 | return SkRef(closest); |
| 208 | } |
| 209 | |
| 210 | private: |
| 211 | SkTArray<SkAutoTUnref<SkTypeface_Custom>, true> fStyles; |
| 212 | SkString fFamilyName; |
| 213 | |
| 214 | void appendTypeface(SkTypeface_Custom* typeface) { |
| 215 | fStyles.push_back().reset(typeface); |
| 216 | } |
| 217 | |
| 218 | friend class SkFontMgr_Custom; |
| 219 | }; |
| 220 | |
| 221 | /** |
| 222 | * SkFontMgr_Custom |
| 223 | * |
| 224 | * This class is essentially a collection of SkFontStyleSet_Custom, |
| 225 | * one SkFontStyleSet_Custom for each family. This class may be modified |
| 226 | * to load fonts from any source by changing the initialization. |
| 227 | */ |
| 228 | class SkFontMgr_Custom : public SkFontMgr { |
| 229 | public: |
| 230 | explicit SkFontMgr_Custom(const char* dir) { |
| 231 | this->load_system_fonts(dir); |
| 232 | } |
| 233 | |
| 234 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 235 | int onCountFamilies() const SK_OVERRIDE { |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 236 | return fFamilies.count(); |
| 237 | } |
| 238 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 239 | void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE { |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 240 | SkASSERT(index < fFamilies.count()); |
| 241 | familyName->set(fFamilies[index]->fFamilyName); |
| 242 | } |
| 243 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 244 | SkFontStyleSet_Custom* onCreateStyleSet(int index) const SK_OVERRIDE { |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 245 | SkASSERT(index < fFamilies.count()); |
| 246 | return SkRef(fFamilies[index].get()); |
| 247 | } |
| 248 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 249 | SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const SK_OVERRIDE { |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 250 | for (int i = 0; i < fFamilies.count(); ++i) { |
| 251 | if (fFamilies[i]->fFamilyName.equals(familyName)) { |
| 252 | return SkRef(fFamilies[i].get()); |
| 253 | } |
| 254 | } |
| 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | virtual SkTypeface* onMatchFamilyStyle(const char familyName[], |
commit-bot@chromium.org | 967dee3 | 2014-02-04 22:35:01 +0000 | [diff] [blame] | 259 | const SkFontStyle& fontStyle) const SK_OVERRIDE |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 260 | { |
| 261 | SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName)); |
| 262 | return sset->matchStyle(fontStyle); |
| 263 | } |
| 264 | |
djsollen | 33068c1 | 2014-11-14 10:52:53 -0800 | [diff] [blame] | 265 | virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&, |
| 266 | const char* bcp47[], int bcp47Count, |
| 267 | SkUnichar character) const SK_OVERRIDE |
| 268 | { |
| 269 | return NULL; |
| 270 | } |
| 271 | |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 272 | virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember, |
commit-bot@chromium.org | 967dee3 | 2014-02-04 22:35:01 +0000 | [diff] [blame] | 273 | const SkFontStyle& fontStyle) const SK_OVERRIDE |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 274 | { |
| 275 | for (int i = 0; i < fFamilies.count(); ++i) { |
| 276 | for (int j = 0; j < fFamilies[i]->fStyles.count(); ++j) { |
| 277 | if (fFamilies[i]->fStyles[j] == familyMember) { |
| 278 | return fFamilies[i]->matchStyle(fontStyle); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | return NULL; |
| 283 | } |
| 284 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 285 | SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 286 | return this->createFromStream(new SkMemoryStream(data), ttcIndex); |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 287 | } |
| 288 | |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame^] | 289 | SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const SK_OVERRIDE { |
| 290 | SkAutoTDelete<SkStreamAsset> stream(bareStream); |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 291 | if (NULL == stream || stream->getLength() <= 0) { |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 292 | return NULL; |
| 293 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 294 | |
bungeman@google.com | fe74765 | 2013-03-25 19:36:11 +0000 | [diff] [blame] | 295 | bool isFixedPitch; |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 296 | SkFontStyle style; |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 297 | SkString name; |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 298 | if (fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) { |
bungeman | 3a21d61 | 2014-07-11 08:52:26 -0700 | [diff] [blame] | 299 | return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, name, |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame^] | 300 | stream.detach(), ttcIndex)); |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 301 | } else { |
| 302 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | } |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 304 | } |
| 305 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 306 | SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE { |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame^] | 307 | SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path)); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 308 | return stream.get() ? this->createFromStream(stream.detach(), ttcIndex) : NULL; |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
commit-bot@chromium.org | 967dee3 | 2014-02-04 22:35:01 +0000 | [diff] [blame] | 312 | unsigned styleBits) const SK_OVERRIDE |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 313 | { |
| 314 | SkTypeface::Style oldStyle = (SkTypeface::Style)styleBits; |
| 315 | SkFontStyle style = SkFontStyle(oldStyle & SkTypeface::kBold |
| 316 | ? SkFontStyle::kBold_Weight |
| 317 | : SkFontStyle::kNormal_Weight, |
| 318 | SkFontStyle::kNormal_Width, |
| 319 | oldStyle & SkTypeface::kItalic |
| 320 | ? SkFontStyle::kItalic_Slant |
| 321 | : SkFontStyle::kUpright_Slant); |
| 322 | SkTypeface* tf = NULL; |
| 323 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 324 | if (familyName) { |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 325 | tf = this->onMatchFamilyStyle(familyName, style); |
bungeman@google.com | 2cf84ec | 2012-09-26 19:16:54 +0000 | [diff] [blame] | 326 | } |
bungeman@google.com | 2cf84ec | 2012-09-26 19:16:54 +0000 | [diff] [blame] | 327 | |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 328 | if (NULL == tf) { |
| 329 | tf = gDefaultFamily->matchStyle(style); |
| 330 | } |
| 331 | |
| 332 | return SkSafeRef(tf); |
bungeman@google.com | 2cf84ec | 2012-09-26 19:16:54 +0000 | [diff] [blame] | 333 | } |
| 334 | |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 335 | private: |
bungeman@google.com | 2cf84ec | 2012-09-26 19:16:54 +0000 | [diff] [blame] | 336 | |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 337 | void load_directory_fonts(const SkString& directory, const char* suffix) { |
| 338 | SkOSFile::Iter iter(directory.c_str(), suffix); |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 339 | SkString name; |
| 340 | |
| 341 | while (iter.next(&name, false)) { |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 342 | SkString filename(SkOSPath::Join(directory.c_str(), name.c_str())); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 343 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(filename.c_str())); |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 344 | if (!stream.get()) { |
| 345 | SkDebugf("---- failed to open <%s>\n", filename.c_str()); |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 346 | continue; |
| 347 | } |
| 348 | |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 349 | int numFaces; |
| 350 | if (!fScanner.recognizedFont(stream, &numFaces)) { |
| 351 | SkDebugf("---- failed to open <%s> as a font\n", filename.c_str()); |
| 352 | continue; |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 353 | } |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 354 | |
| 355 | for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) { |
| 356 | bool isFixedPitch; |
| 357 | SkString realname; |
| 358 | SkFontStyle style = SkFontStyle(); // avoid uninitialized warning |
| 359 | if (!fScanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch)) { |
| 360 | SkDebugf("---- failed to open <%s> <%d> as a font\n", |
| 361 | filename.c_str(), faceIndex); |
| 362 | continue; |
| 363 | } |
| 364 | |
| 365 | SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, ( |
| 366 | style, |
| 367 | isFixedPitch, |
| 368 | true, // system-font (cannot delete) |
| 369 | realname, |
| 370 | filename.c_str(), 0)); |
| 371 | |
| 372 | SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_str()); |
| 373 | if (NULL == addTo) { |
| 374 | addTo = new SkFontStyleSet_Custom(realname); |
| 375 | fFamilies.push_back().reset(addTo); |
| 376 | } |
| 377 | addTo->appendTypeface(tf); |
| 378 | } |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | SkOSFile::Iter dirIter(directory.c_str()); |
| 382 | while (dirIter.next(&name, true)) { |
| 383 | if (name.startsWith(".")) { |
| 384 | continue; |
| 385 | } |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 386 | SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str())); |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 387 | load_directory_fonts(dirname, suffix); |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | void load_system_fonts(const char* dir) { |
| 392 | SkString baseDirectory(dir); |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 393 | load_directory_fonts(baseDirectory, ".ttf"); |
| 394 | load_directory_fonts(baseDirectory, ".ttc"); |
| 395 | load_directory_fonts(baseDirectory, ".otf"); |
| 396 | load_directory_fonts(baseDirectory, ".pfb"); |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 397 | |
| 398 | if (fFamilies.empty()) { |
| 399 | SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString()); |
| 400 | fFamilies.push_back().reset(family); |
| 401 | family->appendTypeface(SkNEW(SkTypeface_Empty)); |
| 402 | } |
| 403 | |
| 404 | // Try to pick a default font. |
| 405 | static const char* gDefaultNames[] = { |
commit-bot@chromium.org | ea163cc | 2014-03-13 16:24:49 +0000 | [diff] [blame] | 406 | "Arial", "Verdana", "Times New Roman", "Droid Sans", NULL |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 407 | }; |
| 408 | for (size_t i = 0; i < SK_ARRAY_COUNT(gDefaultNames); ++i) { |
| 409 | SkFontStyleSet_Custom* set = this->onMatchFamily(gDefaultNames[i]); |
| 410 | if (NULL == set) { |
| 411 | continue; |
| 412 | } |
| 413 | |
| 414 | SkTypeface* tf = set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight, |
| 415 | SkFontStyle::kNormal_Width, |
| 416 | SkFontStyle::kUpright_Slant)); |
| 417 | if (NULL == tf) { |
| 418 | continue; |
| 419 | } |
| 420 | |
| 421 | gDefaultFamily = set; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 422 | gDefaultNormal = tf; |
| 423 | break; |
| 424 | } |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 425 | if (NULL == gDefaultNormal) { |
| 426 | gDefaultFamily = fFamilies[0]; |
| 427 | gDefaultNormal = gDefaultFamily->fStyles[0]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 430 | |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 431 | SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies; |
| 432 | SkFontStyleSet_Custom* gDefaultFamily; |
| 433 | SkTypeface* gDefaultNormal; |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 434 | SkTypeface_FreeType::Scanner fScanner; |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 435 | }; |
reed@google.com | 070da5e | 2013-03-27 20:01:49 +0000 | [diff] [blame] | 436 | |
| 437 | SkFontMgr* SkFontMgr::Factory() { |
bungeman@google.com | b3d154d | 2013-11-11 15:53:29 +0000 | [diff] [blame] | 438 | return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX); |
reed@google.com | 070da5e | 2013-03-27 20:01:49 +0000 | [diff] [blame] | 439 | } |