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