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