OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkFontHost.h" | 8 #include "SkFontHost.h" |
9 #include "SkFontHost_FreeType_common.h" | 9 #include "SkFontHost_FreeType_common.h" |
10 #include "SkFontDescriptor.h" | 10 #include "SkFontDescriptor.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 bool sysFont, const SkString familyName, int index) | 35 bool sysFont, const SkString familyName, int index) |
36 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) | 36 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) |
37 , fIsSysFont(sysFont), fFamilyName(familyName), fIndex(index) | 37 , fIsSysFont(sysFont), fFamilyName(familyName), fIndex(index) |
38 { } | 38 { } |
39 | 39 |
40 bool isSysFont() const { return fIsSysFont; } | 40 bool isSysFont() const { return fIsSysFont; } |
41 | 41 |
42 virtual const char* getUniqueString() const = 0; | 42 virtual const char* getUniqueString() const = 0; |
43 | 43 |
44 protected: | 44 protected: |
45 virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE { | 45 void onGetFamilyName(SkString* familyName) const SK_OVERRIDE { |
46 *familyName = fFamilyName; | 46 *familyName = fFamilyName; |
47 } | 47 } |
48 | 48 |
49 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) cons
t SK_OVERRIDE { | 49 void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const SK_OVE
RRIDE { |
50 desc->setFamilyName(fFamilyName.c_str()); | 50 desc->setFamilyName(fFamilyName.c_str()); |
51 desc->setFontFileName(this->getUniqueString()); | 51 desc->setFontFileName(this->getUniqueString()); |
52 desc->setFontIndex(fIndex); | 52 desc->setFontIndex(fIndex); |
53 *isLocal = !this->isSysFont(); | 53 *isLocal = !this->isSysFont(); |
54 } | 54 } |
55 | 55 |
56 int getIndex() const { return fIndex; } | 56 int getIndex() const { return fIndex; } |
57 | 57 |
58 private: | 58 private: |
59 const bool fIsSysFont; | 59 const bool fIsSysFont; |
60 const SkString fFamilyName; | 60 const SkString fFamilyName; |
61 const int fIndex; | 61 const int fIndex; |
62 | 62 |
63 typedef SkTypeface_FreeType INHERITED; | 63 typedef SkTypeface_FreeType INHERITED; |
64 }; | 64 }; |
65 | 65 |
66 /** The empty SkTypeface implementation for the custom font manager. | 66 /** The empty SkTypeface implementation for the custom font manager. |
67 * Used as the last resort fallback typeface. | 67 * Used as the last resort fallback typeface. |
68 */ | 68 */ |
69 class SkTypeface_Empty : public SkTypeface_Custom { | 69 class SkTypeface_Empty : public SkTypeface_Custom { |
70 public: | 70 public: |
71 SkTypeface_Empty() : INHERITED(SkFontStyle(), false, true, SkString(), 0) {} | 71 SkTypeface_Empty() : INHERITED(SkFontStyle(), false, true, SkString(), 0) {} |
72 | 72 |
73 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; } | 73 const char* getUniqueString() const SK_OVERRIDE { return NULL; } |
74 | 74 |
75 protected: | 75 protected: |
76 virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; } | 76 SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; } |
77 | 77 |
78 private: | 78 private: |
79 typedef SkTypeface_Custom INHERITED; | 79 typedef SkTypeface_Custom INHERITED; |
80 }; | 80 }; |
81 | 81 |
82 /** The stream SkTypeface implementation for the custom font manager. */ | 82 /** The stream SkTypeface implementation for the custom font manager. */ |
83 class SkTypeface_Stream : public SkTypeface_Custom { | 83 class SkTypeface_Stream : public SkTypeface_Custom { |
84 public: | 84 public: |
85 SkTypeface_Stream(const SkFontStyle& style, bool isFixedPitch, bool sysFont, | 85 SkTypeface_Stream(const SkFontStyle& style, bool isFixedPitch, bool sysFont, |
86 const SkString familyName, SkStream* stream, int index) | 86 const SkString familyName, SkStream* stream, int index) |
87 : INHERITED(style, isFixedPitch, sysFont, familyName, index) | 87 : INHERITED(style, isFixedPitch, sysFont, familyName, index) |
88 , fStream(SkRef(stream)) | 88 , fStream(SkRef(stream)) |
89 { } | 89 { } |
90 | 90 |
91 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; } | 91 const char* getUniqueString() const SK_OVERRIDE { return NULL; } |
92 | 92 |
93 protected: | 93 protected: |
94 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { | 94 SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { |
95 *ttcIndex = this->getIndex(); | 95 *ttcIndex = this->getIndex(); |
96 return fStream->duplicate(); | 96 return fStream->duplicate(); |
97 } | 97 } |
98 | 98 |
99 private: | 99 private: |
100 const SkAutoTUnref<const SkStream> fStream; | 100 const SkAutoTUnref<const SkStream> fStream; |
101 | 101 |
102 typedef SkTypeface_Custom INHERITED; | 102 typedef SkTypeface_Custom INHERITED; |
103 }; | 103 }; |
104 | 104 |
105 // This configuration option is useful if we need to open and hold handles to | 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 | 106 // all found system font data (e.g., for skfiddle, where the application can't |
107 // access the filesystem to read fonts on demand) | 107 // access the filesystem to read fonts on demand) |
108 | 108 |
109 SK_CONF_DECLARE(bool, c_CustomTypefaceRetain, "fonts.customFont.retainAllData",
false, | 109 SK_CONF_DECLARE(bool, c_CustomTypefaceRetain, "fonts.customFont.retainAllData",
false, |
110 "Retain the open stream for each found font on the system."); | 110 "Retain the open stream for each found font on the system."); |
111 | 111 |
112 /** The file SkTypeface implementation for the custom font manager. */ | 112 /** The file SkTypeface implementation for the custom font manager. */ |
113 class SkTypeface_File : public SkTypeface_Custom { | 113 class SkTypeface_File : public SkTypeface_Custom { |
114 public: | 114 public: |
115 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont, | 115 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont, |
116 const SkString familyName, const char path[], int index) | 116 const SkString familyName, const char path[], int index) |
117 : INHERITED(style, isFixedPitch, sysFont, familyName, index) | 117 : INHERITED(style, isFixedPitch, sysFont, familyName, index) |
118 , fPath(path) | 118 , fPath(path) |
119 , fStream(c_CustomTypefaceRetain ? SkStream::NewFromFile(fPath.c_str())
: NULL) | 119 , fStream(c_CustomTypefaceRetain ? SkStream::NewFromFile(fPath.c_str())
: NULL) |
120 { } | 120 { } |
121 | 121 |
122 virtual const char* getUniqueString() const SK_OVERRIDE { | 122 const char* getUniqueString() const SK_OVERRIDE { |
123 const char* str = strrchr(fPath.c_str(), '/'); | 123 const char* str = strrchr(fPath.c_str(), '/'); |
124 if (str) { | 124 if (str) { |
125 str += 1; // skip the '/' | 125 str += 1; // skip the '/' |
126 } | 126 } |
127 return str; | 127 return str; |
128 } | 128 } |
129 | 129 |
130 protected: | 130 protected: |
131 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { | 131 SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { |
132 *ttcIndex = this->getIndex(); | 132 *ttcIndex = this->getIndex(); |
133 if (fStream.get()) { | 133 if (fStream.get()) { |
134 return fStream->duplicate(); | 134 return fStream->duplicate(); |
135 } else { | 135 } else { |
136 return SkStream::NewFromFile(fPath.c_str()); | 136 return SkStream::NewFromFile(fPath.c_str()); |
137 } | 137 } |
138 } | 138 } |
139 | 139 |
140 private: | 140 private: |
141 SkString fPath; | 141 SkString fPath; |
142 const SkAutoTUnref<SkStreamAsset> fStream; | 142 const SkAutoTUnref<SkStreamAsset> fStream; |
143 | 143 |
144 typedef SkTypeface_Custom INHERITED; | 144 typedef SkTypeface_Custom INHERITED; |
145 }; | 145 }; |
146 | 146 |
147 /////////////////////////////////////////////////////////////////////////////// | 147 /////////////////////////////////////////////////////////////////////////////// |
148 | 148 |
149 /** | 149 /** |
150 * SkFontStyleSet_Custom | 150 * SkFontStyleSet_Custom |
151 * | 151 * |
152 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families. | 152 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families. |
153 */ | 153 */ |
154 class SkFontStyleSet_Custom : public SkFontStyleSet { | 154 class SkFontStyleSet_Custom : public SkFontStyleSet { |
155 public: | 155 public: |
156 explicit SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(fami
lyName) { } | 156 explicit SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(fami
lyName) { } |
157 | 157 |
158 virtual int count() SK_OVERRIDE { | 158 int count() SK_OVERRIDE { |
159 return fStyles.count(); | 159 return fStyles.count(); |
160 } | 160 } |
161 | 161 |
162 virtual void getStyle(int index, SkFontStyle* style, SkString* name) SK_OVER
RIDE { | 162 void getStyle(int index, SkFontStyle* style, SkString* name) SK_OVERRIDE { |
163 SkASSERT(index < fStyles.count()); | 163 SkASSERT(index < fStyles.count()); |
164 bool bold = fStyles[index]->isBold(); | 164 bool bold = fStyles[index]->isBold(); |
165 bool italic = fStyles[index]->isItalic(); | 165 bool italic = fStyles[index]->isItalic(); |
166 *style = SkFontStyle(bold ? SkFontStyle::kBold_Weight : SkFontStyle::kNo
rmal_Weight, | 166 *style = SkFontStyle(bold ? SkFontStyle::kBold_Weight : SkFontStyle::kNo
rmal_Weight, |
167 SkFontStyle::kNormal_Width, | 167 SkFontStyle::kNormal_Width, |
168 italic ? SkFontStyle::kItalic_Slant : SkFontStyle::
kUpright_Slant); | 168 italic ? SkFontStyle::kItalic_Slant : SkFontStyle::
kUpright_Slant); |
169 name->reset(); | 169 name->reset(); |
170 } | 170 } |
171 | 171 |
172 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE { | 172 SkTypeface* createTypeface(int index) SK_OVERRIDE { |
173 SkASSERT(index < fStyles.count()); | 173 SkASSERT(index < fStyles.count()); |
174 return SkRef(fStyles[index].get()); | 174 return SkRef(fStyles[index].get()); |
175 } | 175 } |
176 | 176 |
177 static int match_score(const SkFontStyle& pattern, const SkFontStyle& candid
ate) { | 177 static int match_score(const SkFontStyle& pattern, const SkFontStyle& candid
ate) { |
178 int score = 0; | 178 int score = 0; |
179 score += (pattern.width() - candidate.width()) * 100; | 179 score += (pattern.width() - candidate.width()) * 100; |
180 score += (pattern.isItalic() == candidate.isItalic()) ? 0 : 1000; | 180 score += (pattern.isItalic() == candidate.isItalic()) ? 0 : 1000; |
181 score += pattern.weight() - candidate.weight(); | 181 score += pattern.weight() - candidate.weight(); |
182 return score; | 182 return score; |
183 } | 183 } |
184 | 184 |
185 virtual SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE { | 185 SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE { |
186 if (0 == fStyles.count()) { | 186 if (0 == fStyles.count()) { |
187 return NULL; | 187 return NULL; |
188 } | 188 } |
189 | 189 |
190 SkTypeface_Custom* closest = fStyles[0]; | 190 SkTypeface_Custom* closest = fStyles[0]; |
191 int minScore = std::numeric_limits<int>::max(); | 191 int minScore = std::numeric_limits<int>::max(); |
192 for (int i = 0; i < fStyles.count(); ++i) { | 192 for (int i = 0; i < fStyles.count(); ++i) { |
193 bool bold = fStyles[i]->isBold(); | 193 bool bold = fStyles[i]->isBold(); |
194 bool italic = fStyles[i]->isItalic(); | 194 bool italic = fStyles[i]->isItalic(); |
195 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight | 195 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight |
(...skipping 29 matching lines...) Expand all Loading... |
225 * one SkFontStyleSet_Custom for each family. This class may be modified | 225 * one SkFontStyleSet_Custom for each family. This class may be modified |
226 * to load fonts from any source by changing the initialization. | 226 * to load fonts from any source by changing the initialization. |
227 */ | 227 */ |
228 class SkFontMgr_Custom : public SkFontMgr { | 228 class SkFontMgr_Custom : public SkFontMgr { |
229 public: | 229 public: |
230 explicit SkFontMgr_Custom(const char* dir) { | 230 explicit SkFontMgr_Custom(const char* dir) { |
231 this->load_system_fonts(dir); | 231 this->load_system_fonts(dir); |
232 } | 232 } |
233 | 233 |
234 protected: | 234 protected: |
235 virtual int onCountFamilies() const SK_OVERRIDE { | 235 int onCountFamilies() const SK_OVERRIDE { |
236 return fFamilies.count(); | 236 return fFamilies.count(); |
237 } | 237 } |
238 | 238 |
239 virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERR
IDE { | 239 void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE { |
240 SkASSERT(index < fFamilies.count()); | 240 SkASSERT(index < fFamilies.count()); |
241 familyName->set(fFamilies[index]->fFamilyName); | 241 familyName->set(fFamilies[index]->fFamilyName); |
242 } | 242 } |
243 | 243 |
244 virtual SkFontStyleSet_Custom* onCreateStyleSet(int index) const SK_OVERRIDE
{ | 244 SkFontStyleSet_Custom* onCreateStyleSet(int index) const SK_OVERRIDE { |
245 SkASSERT(index < fFamilies.count()); | 245 SkASSERT(index < fFamilies.count()); |
246 return SkRef(fFamilies[index].get()); | 246 return SkRef(fFamilies[index].get()); |
247 } | 247 } |
248 | 248 |
249 virtual SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const
SK_OVERRIDE { | 249 SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const SK_OVERR
IDE { |
250 for (int i = 0; i < fFamilies.count(); ++i) { | 250 for (int i = 0; i < fFamilies.count(); ++i) { |
251 if (fFamilies[i]->fFamilyName.equals(familyName)) { | 251 if (fFamilies[i]->fFamilyName.equals(familyName)) { |
252 return SkRef(fFamilies[i].get()); | 252 return SkRef(fFamilies[i].get()); |
253 } | 253 } |
254 } | 254 } |
255 return NULL; | 255 return NULL; |
256 } | 256 } |
257 | 257 |
258 virtual SkTypeface* onMatchFamilyStyle(const char familyName[], | 258 virtual SkTypeface* onMatchFamilyStyle(const char familyName[], |
259 const SkFontStyle& fontStyle) const S
K_OVERRIDE | 259 const SkFontStyle& fontStyle) const S
K_OVERRIDE |
(...skipping 15 matching lines...) Expand all Loading... |
275 for (int i = 0; i < fFamilies.count(); ++i) { | 275 for (int i = 0; i < fFamilies.count(); ++i) { |
276 for (int j = 0; j < fFamilies[i]->fStyles.count(); ++j) { | 276 for (int j = 0; j < fFamilies[i]->fStyles.count(); ++j) { |
277 if (fFamilies[i]->fStyles[j] == familyMember) { | 277 if (fFamilies[i]->fStyles[j] == familyMember) { |
278 return fFamilies[i]->matchStyle(fontStyle); | 278 return fFamilies[i]->matchStyle(fontStyle); |
279 } | 279 } |
280 } | 280 } |
281 } | 281 } |
282 return NULL; | 282 return NULL; |
283 } | 283 } |
284 | 284 |
285 virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OV
ERRIDE { | 285 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE { |
286 SkAutoTUnref<SkStream> stream(new SkMemoryStream(data)); | 286 SkAutoTUnref<SkStream> stream(new SkMemoryStream(data)); |
287 return this->createFromStream(stream, ttcIndex); | 287 return this->createFromStream(stream, ttcIndex); |
288 } | 288 } |
289 | 289 |
290 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const
SK_OVERRIDE { | 290 SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVER
RIDE { |
291 if (NULL == stream || stream->getLength() <= 0) { | 291 if (NULL == stream || stream->getLength() <= 0) { |
292 SkDELETE(stream); | 292 SkDELETE(stream); |
293 return NULL; | 293 return NULL; |
294 } | 294 } |
295 | 295 |
296 bool isFixedPitch; | 296 bool isFixedPitch; |
297 SkFontStyle style; | 297 SkFontStyle style; |
298 SkString name; | 298 SkString name; |
299 if (fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) { | 299 if (fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) { |
300 return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, na
me, | 300 return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, na
me, |
301 stream, ttcIndex)); | 301 stream, ttcIndex)); |
302 } else { | 302 } else { |
303 return NULL; | 303 return NULL; |
304 } | 304 } |
305 } | 305 } |
306 | 306 |
307 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const
SK_OVERRIDE { | 307 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERR
IDE { |
308 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); | 308 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); |
309 return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL; | 309 return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL; |
310 } | 310 } |
311 | 311 |
312 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], | 312 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
313 unsigned styleBits) const SK_OVER
RIDE | 313 unsigned styleBits) const SK_OVER
RIDE |
314 { | 314 { |
315 SkTypeface::Style oldStyle = (SkTypeface::Style)styleBits; | 315 SkTypeface::Style oldStyle = (SkTypeface::Style)styleBits; |
316 SkFontStyle style = SkFontStyle(oldStyle & SkTypeface::kBold | 316 SkFontStyle style = SkFontStyle(oldStyle & SkTypeface::kBold |
317 ? SkFontStyle::kBold_Weight | 317 ? SkFontStyle::kBold_Weight |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 | 431 |
432 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies; | 432 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies; |
433 SkFontStyleSet_Custom* gDefaultFamily; | 433 SkFontStyleSet_Custom* gDefaultFamily; |
434 SkTypeface* gDefaultNormal; | 434 SkTypeface* gDefaultNormal; |
435 SkTypeface_FreeType::Scanner fScanner; | 435 SkTypeface_FreeType::Scanner fScanner; |
436 }; | 436 }; |
437 | 437 |
438 SkFontMgr* SkFontMgr::Factory() { | 438 SkFontMgr* SkFontMgr::Factory() { |
439 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX); | 439 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX); |
440 } | 440 } |
OLD | NEW |