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" |
11 #include "SkFontMgr.h" | 11 #include "SkFontMgr.h" |
12 #include "SkDescriptor.h" | 12 #include "SkDescriptor.h" |
13 #include "SkOSFile.h" | 13 #include "SkOSFile.h" |
14 #include "SkPaint.h" | 14 #include "SkPaint.h" |
15 #include "SkString.h" | 15 #include "SkString.h" |
16 #include "SkStream.h" | 16 #include "SkStream.h" |
17 #include "SkThread.h" | 17 #include "SkThread.h" |
18 #include "SkTSearch.h" | 18 #include "SkTSearch.h" |
19 #include "SkTypefaceCache.h" | 19 #include "SkTypefaceCache.h" |
20 #include "SkTArray.h" | 20 #include "SkTArray.h" |
21 | 21 |
22 #include <limits> | 22 #include <limits> |
23 | 23 |
24 #ifndef SK_FONT_FILE_PREFIX | 24 #ifndef SK_FONT_FILE_PREFIX |
25 # define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/" | 25 # define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/" |
26 #endif | 26 #endif |
27 | 27 |
28 bool find_name_and_attributes(SkStream* stream, SkString* name, | |
29 SkTypeface::Style* style, bool* isFixedPitch); | |
30 | |
31 /////////////////////////////////////////////////////////////////////////////// | 28 /////////////////////////////////////////////////////////////////////////////// |
32 | 29 |
33 /** The base SkTypeface implementation for the custom font manager. */ | 30 /** The base SkTypeface implementation for the custom font manager. */ |
34 class SkTypeface_Custom : public SkTypeface_FreeType { | 31 class SkTypeface_Custom : public SkTypeface_FreeType { |
35 public: | 32 public: |
36 SkTypeface_Custom(Style style, bool sysFont, bool isFixedPitch, const SkStri
ng familyName) | 33 SkTypeface_Custom(Style style, bool isFixedPitch, bool sysFont, const SkStri
ng familyName) |
37 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) | 34 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) |
38 , fIsSysFont(sysFont), fFamilyName(familyName) | 35 , fIsSysFont(sysFont), fFamilyName(familyName) |
39 { } | 36 { } |
40 | 37 |
41 bool isSysFont() const { return fIsSysFont; } | 38 bool isSysFont() const { return fIsSysFont; } |
42 | 39 |
43 virtual const char* getUniqueString() const = 0; | 40 virtual const char* getUniqueString() const = 0; |
44 | 41 |
45 protected: | 42 protected: |
46 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) cons
t SK_OVERRIDE { | 43 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) cons
t SK_OVERRIDE { |
47 desc->setFamilyName(fFamilyName.c_str()); | 44 desc->setFamilyName(fFamilyName.c_str()); |
48 desc->setFontFileName(this->getUniqueString()); | 45 desc->setFontFileName(this->getUniqueString()); |
49 *isLocal = !this->isSysFont(); | 46 *isLocal = !this->isSysFont(); |
50 } | 47 } |
51 | 48 |
52 private: | 49 private: |
53 bool fIsSysFont; | 50 bool fIsSysFont; |
54 SkString fFamilyName; | 51 SkString fFamilyName; |
55 | 52 |
56 typedef SkTypeface_FreeType INHERITED; | 53 typedef SkTypeface_FreeType INHERITED; |
57 }; | 54 }; |
58 | 55 |
59 /** The empty SkTypeface implementation for the custom font manager. | 56 /** The empty SkTypeface implementation for the custom font manager. |
60 * Used as the last resort fallback typeface. | 57 * Used as the last resort fallback typeface. |
61 */ | 58 */ |
62 class SkTypeface_Empty : public SkTypeface_Custom { | 59 class SkTypeface_Empty : public SkTypeface_Custom { |
63 public: | 60 public: |
64 SkTypeface_Empty() : INHERITED(SkTypeface::kNormal, true, false, SkString())
{} | 61 SkTypeface_Empty() : INHERITED(SkTypeface::kNormal, false, true, SkString())
{} |
65 | 62 |
66 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; } | 63 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; } |
67 | 64 |
68 protected: | 65 protected: |
69 virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; } | 66 virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; } |
70 | 67 |
71 private: | 68 private: |
72 typedef SkTypeface_Custom INHERITED; | 69 typedef SkTypeface_Custom INHERITED; |
73 }; | 70 }; |
74 | 71 |
75 /** The stream SkTypeface implementation for the custom font manager. */ | 72 /** The stream SkTypeface implementation for the custom font manager. */ |
76 class SkTypeface_Stream : public SkTypeface_Custom { | 73 class SkTypeface_Stream : public SkTypeface_Custom { |
77 public: | 74 public: |
78 SkTypeface_Stream(Style style, bool sysFont, SkStream* stream, | 75 SkTypeface_Stream(Style style, bool isFixedPitch, bool sysFont, const SkStri
ng familyName, |
79 bool isFixedPitch, const SkString familyName) | 76 SkStream* stream, int ttcIndex) |
80 : INHERITED(style, sysFont, isFixedPitch, familyName) | 77 : INHERITED(style, isFixedPitch, sysFont, familyName) |
81 , fStream(SkRef(stream)) | 78 , fStream(SkRef(stream)), fTtcIndex(ttcIndex) |
82 { } | 79 { } |
83 | 80 |
84 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; } | 81 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; } |
85 | 82 |
86 protected: | 83 protected: |
87 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { | 84 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { |
88 *ttcIndex = 0; | 85 *ttcIndex = 0; |
89 return fStream->duplicate(); | 86 return fStream->duplicate(); |
90 } | 87 } |
91 | 88 |
92 private: | 89 private: |
93 SkAutoTUnref<SkStream> fStream; | 90 SkAutoTUnref<SkStream> fStream; |
| 91 int fTtcIndex; |
94 | 92 |
95 typedef SkTypeface_Custom INHERITED; | 93 typedef SkTypeface_Custom INHERITED; |
96 }; | 94 }; |
97 | 95 |
98 /** The file SkTypeface implementation for the custom font manager. */ | 96 /** The file SkTypeface implementation for the custom font manager. */ |
99 class SkTypeface_File : public SkTypeface_Custom { | 97 class SkTypeface_File : public SkTypeface_Custom { |
100 public: | 98 public: |
101 SkTypeface_File(Style style, bool sysFont, const char path[], | 99 SkTypeface_File(Style style, bool isFixedPitch, bool sysFont, const SkString
familyName, |
102 bool isFixedPitch, const SkString familyName) | 100 const char path[]) |
103 : INHERITED(style, sysFont, isFixedPitch, familyName) | 101 : INHERITED(style, isFixedPitch, sysFont, familyName) |
104 , fPath(path) | 102 , fPath(path) |
105 { } | 103 { } |
106 | 104 |
107 virtual const char* getUniqueString() const SK_OVERRIDE { | 105 virtual const char* getUniqueString() const SK_OVERRIDE { |
108 const char* str = strrchr(fPath.c_str(), '/'); | 106 const char* str = strrchr(fPath.c_str(), '/'); |
109 if (str) { | 107 if (str) { |
110 str += 1; // skip the '/' | 108 str += 1; // skip the '/' |
111 } | 109 } |
112 return str; | 110 return str; |
113 } | 111 } |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 | 260 |
263 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const
SK_OVERRIDE { | 261 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const
SK_OVERRIDE { |
264 if (NULL == stream || stream->getLength() <= 0) { | 262 if (NULL == stream || stream->getLength() <= 0) { |
265 SkDELETE(stream); | 263 SkDELETE(stream); |
266 return NULL; | 264 return NULL; |
267 } | 265 } |
268 | 266 |
269 bool isFixedPitch; | 267 bool isFixedPitch; |
270 SkTypeface::Style style; | 268 SkTypeface::Style style; |
271 SkString name; | 269 SkString name; |
272 if (find_name_and_attributes(stream, &name, &style, &isFixedPitch)) { | 270 if (SkTypeface_FreeType::ScanFont(stream, ttcIndex, &name, &style, &isFi
xedPitch)) { |
273 return SkNEW_ARGS(SkTypeface_Stream, (style, false, stream, isFixedP
itch, name)); | 271 return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, na
me, |
| 272 stream, ttcIndex)); |
274 } else { | 273 } else { |
275 return NULL; | 274 return NULL; |
276 } | 275 } |
277 } | 276 } |
278 | 277 |
279 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const
SK_OVERRIDE { | 278 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const
SK_OVERRIDE { |
280 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); | 279 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); |
281 return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL; | 280 return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL; |
282 } | 281 } |
283 | 282 |
(...skipping 20 matching lines...) Expand all Loading... |
304 | 303 |
305 return SkSafeRef(tf); | 304 return SkSafeRef(tf); |
306 } | 305 } |
307 | 306 |
308 private: | 307 private: |
309 | 308 |
310 static bool get_name_and_style(const char path[], SkString* name, | 309 static bool get_name_and_style(const char path[], SkString* name, |
311 SkTypeface::Style* style, bool* isFixedPitch)
{ | 310 SkTypeface::Style* style, bool* isFixedPitch)
{ |
312 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); | 311 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); |
313 if (stream.get()) { | 312 if (stream.get()) { |
314 return find_name_and_attributes(stream, name, style, isFixedPitch); | 313 return SkTypeface_FreeType::ScanFont(stream, 0, name, style, isFixed
Pitch); |
315 } else { | 314 } else { |
316 SkDebugf("---- failed to open <%s> as a font\n", path); | 315 SkDebugf("---- failed to open <%s> as a font\n", path); |
317 return false; | 316 return false; |
318 } | 317 } |
319 } | 318 } |
320 | 319 |
321 void load_directory_fonts(const SkString& directory) { | 320 void load_directory_fonts(const SkString& directory) { |
322 SkOSFile::Iter iter(directory.c_str(), ".ttf"); | 321 SkOSFile::Iter iter(directory.c_str(), ".ttf"); |
323 SkString name; | 322 SkString name; |
324 | 323 |
325 while (iter.next(&name, false)) { | 324 while (iter.next(&name, false)) { |
326 SkString filename( | 325 SkString filename( |
327 SkOSPath::SkPathJoin(directory.c_str(), name.c_str())); | 326 SkOSPath::SkPathJoin(directory.c_str(), name.c_str())); |
328 | 327 |
329 bool isFixedPitch; | 328 bool isFixedPitch; |
330 SkString realname; | 329 SkString realname; |
331 SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialize
d warning | 330 SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialize
d warning |
332 | 331 |
333 if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixe
dPitch)) { | 332 if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixe
dPitch)) { |
334 SkDebugf("------ can't load <%s> as a font\n", filename.c_str())
; | 333 SkDebugf("------ can't load <%s> as a font\n", filename.c_str())
; |
335 continue; | 334 continue; |
336 } | 335 } |
337 | 336 |
338 SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, ( | 337 SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, ( |
339 style, | 338 style, |
| 339 isFixedPitch, |
340 true, // system-font (cannot de
lete) | 340 true, // system-font (cannot de
lete) |
341 filename.c_str(), | 341 realname, |
342 isFixedPitch, | 342 filename.c_str())); |
343 realname)); | |
344 | 343 |
345 SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_str())
; | 344 SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_str())
; |
346 if (NULL == addTo) { | 345 if (NULL == addTo) { |
347 addTo = new SkFontStyleSet_Custom(realname); | 346 addTo = new SkFontStyleSet_Custom(realname); |
348 fFamilies.push_back().reset(addTo); | 347 fFamilies.push_back().reset(addTo); |
349 } | 348 } |
350 addTo->appendTypeface(tf); | 349 addTo->appendTypeface(tf); |
351 } | 350 } |
352 | 351 |
353 SkOSFile::Iter dirIter(directory.c_str()); | 352 SkOSFile::Iter dirIter(directory.c_str()); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 } | 398 } |
400 | 399 |
401 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies; | 400 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies; |
402 SkFontStyleSet_Custom* gDefaultFamily; | 401 SkFontStyleSet_Custom* gDefaultFamily; |
403 SkTypeface* gDefaultNormal; | 402 SkTypeface* gDefaultNormal; |
404 }; | 403 }; |
405 | 404 |
406 SkFontMgr* SkFontMgr::Factory() { | 405 SkFontMgr* SkFontMgr::Factory() { |
407 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX); | 406 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX); |
408 } | 407 } |
OLD | NEW |