| 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_FreeType_common.h" | 8 #include "SkFontHost_FreeType_common.h" |
| 9 #include "SkFontDescriptor.h" | 9 #include "SkFontDescriptor.h" |
| 10 #include "SkFontMgr.h" | 10 #include "SkFontMgr.h" |
| 11 #include "SkFontMgr_custom.h" |
| 11 #include "SkDescriptor.h" | 12 #include "SkDescriptor.h" |
| 12 #include "SkOSFile.h" | 13 #include "SkOSFile.h" |
| 13 #include "SkPaint.h" | 14 #include "SkPaint.h" |
| 14 #include "SkRTConf.h" | 15 #include "SkRTConf.h" |
| 15 #include "SkString.h" | 16 #include "SkString.h" |
| 16 #include "SkStream.h" | 17 #include "SkStream.h" |
| 17 #include "SkThread.h" | 18 #include "SkThread.h" |
| 18 #include "SkTSearch.h" | 19 #include "SkTSearch.h" |
| 19 #include "SkTypefaceCache.h" | 20 #include "SkTypefaceCache.h" |
| 20 #include "SkTArray.h" | 21 #include "SkTArray.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 *ttcIndex = this->getIndex(); | 81 *ttcIndex = this->getIndex(); |
| 81 return fStream->duplicate(); | 82 return fStream->duplicate(); |
| 82 } | 83 } |
| 83 | 84 |
| 84 private: | 85 private: |
| 85 const SkAutoTDelete<const SkStreamAsset> fStream; | 86 const SkAutoTDelete<const SkStreamAsset> fStream; |
| 86 | 87 |
| 87 typedef SkTypeface_Custom INHERITED; | 88 typedef SkTypeface_Custom INHERITED; |
| 88 }; | 89 }; |
| 89 | 90 |
| 90 // This configuration option is useful if we need to open and hold handles to | |
| 91 // all found system font data (e.g., for skfiddle, where the application can't | |
| 92 // access the filesystem to read fonts on demand) | |
| 93 | |
| 94 SK_CONF_DECLARE(bool, c_CustomTypefaceRetain, "fonts.customFont.retainAllData",
false, | |
| 95 "Retain the open stream for each found font on the system."); | |
| 96 | |
| 97 /** The file SkTypeface implementation for the custom font manager. */ | 91 /** The file SkTypeface implementation for the custom font manager. */ |
| 98 class SkTypeface_File : public SkTypeface_Custom { | 92 class SkTypeface_File : public SkTypeface_Custom { |
| 99 public: | 93 public: |
| 100 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont, | 94 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont, |
| 101 const SkString familyName, const char path[], int index) | 95 const SkString familyName, const char path[], int index) |
| 102 : INHERITED(style, isFixedPitch, sysFont, familyName, index) | 96 : INHERITED(style, isFixedPitch, sysFont, familyName, index) |
| 103 , fPath(path) | 97 , fPath(path) |
| 104 , fStream(c_CustomTypefaceRetain ? SkStream::NewFromFile(fPath.c_str())
: NULL) | |
| 105 { } | 98 { } |
| 106 | 99 |
| 107 protected: | 100 protected: |
| 108 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 101 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
| 109 *ttcIndex = this->getIndex(); | 102 *ttcIndex = this->getIndex(); |
| 110 if (fStream.get()) { | 103 return SkStream::NewFromFile(fPath.c_str()); |
| 111 return fStream->duplicate(); | |
| 112 } else { | |
| 113 return SkStream::NewFromFile(fPath.c_str()); | |
| 114 } | |
| 115 } | 104 } |
| 116 | 105 |
| 117 private: | 106 private: |
| 118 SkString fPath; | 107 SkString fPath; |
| 119 const SkAutoTDelete<SkStreamAsset> fStream; | |
| 120 | 108 |
| 121 typedef SkTypeface_Custom INHERITED; | 109 typedef SkTypeface_Custom INHERITED; |
| 122 }; | 110 }; |
| 123 | 111 |
| 124 /////////////////////////////////////////////////////////////////////////////// | 112 /////////////////////////////////////////////////////////////////////////////// |
| 125 | 113 |
| 126 /** | 114 /** |
| 127 * SkFontStyleSet_Custom | 115 * SkFontStyleSet_Custom |
| 128 * | 116 * |
| 129 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families. | 117 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families. |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 * to load fonts from any source by changing the initialization. | 194 * to load fonts from any source by changing the initialization. |
| 207 */ | 195 */ |
| 208 class SkFontMgr_Custom : public SkFontMgr { | 196 class SkFontMgr_Custom : public SkFontMgr { |
| 209 public: | 197 public: |
| 210 typedef SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> Families; | 198 typedef SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> Families; |
| 211 class SystemFontLoader { | 199 class SystemFontLoader { |
| 212 public: | 200 public: |
| 213 virtual ~SystemFontLoader() { } | 201 virtual ~SystemFontLoader() { } |
| 214 virtual void loadSystemFonts(const SkTypeface_FreeType::Scanner&, Famili
es*) const = 0; | 202 virtual void loadSystemFonts(const SkTypeface_FreeType::Scanner&, Famili
es*) const = 0; |
| 215 }; | 203 }; |
| 216 explicit SkFontMgr_Custom(const SystemFontLoader& loader) { | 204 explicit SkFontMgr_Custom(const SystemFontLoader& loader) : fDefaultFamily(N
ULL) { |
| 217 loader.loadSystemFonts(fScanner, &fFamilies); | 205 loader.loadSystemFonts(fScanner, &fFamilies); |
| 218 | 206 |
| 219 // Try to pick a default font. | 207 // Try to pick a default font. |
| 220 static const char* defaultNames[] = { | 208 static const char* defaultNames[] = { |
| 221 "Arial", "Verdana", "Times New Roman", "Droid Sans", NULL | 209 "Arial", "Verdana", "Times New Roman", "Droid Sans", NULL |
| 222 }; | 210 }; |
| 223 for (size_t i = 0; i < SK_ARRAY_COUNT(defaultNames); ++i) { | 211 for (size_t i = 0; i < SK_ARRAY_COUNT(defaultNames); ++i) { |
| 224 SkFontStyleSet_Custom* set = this->onMatchFamily(defaultNames[i]); | 212 SkFontStyleSet_Custom* set = this->onMatchFamily(defaultNames[i]); |
| 225 if (NULL == set) { | 213 if (NULL == set) { |
| 226 continue; | 214 continue; |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 continue; | 421 continue; |
| 434 } | 422 } |
| 435 SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str())); | 423 SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str())); |
| 436 load_directory_fonts(scanner, dirname, suffix, families); | 424 load_directory_fonts(scanner, dirname, suffix, families); |
| 437 } | 425 } |
| 438 } | 426 } |
| 439 | 427 |
| 440 SkString fBaseDirectory; | 428 SkString fBaseDirectory; |
| 441 }; | 429 }; |
| 442 | 430 |
| 431 SK_API SkFontMgr* SkFontMgr_New_Custom_Directory(const char* dir) { |
| 432 return new SkFontMgr_Custom(DirectorySystemFontLoader(dir)); |
| 433 } |
| 434 |
| 435 /////////////////////////////////////////////////////////////////////////////// |
| 436 |
| 443 struct SkEmbeddedResource { const uint8_t* data; size_t size; }; | 437 struct SkEmbeddedResource { const uint8_t* data; size_t size; }; |
| 444 struct SkEmbeddedResourceHeader { const SkEmbeddedResource* entries; int count;
}; | 438 struct SkEmbeddedResourceHeader { const SkEmbeddedResource* entries; int count;
}; |
| 445 | 439 |
| 446 class EmbeddedSystemFontLoader : public SkFontMgr_Custom::SystemFontLoader { | 440 class EmbeddedSystemFontLoader : public SkFontMgr_Custom::SystemFontLoader { |
| 447 public: | 441 public: |
| 448 EmbeddedSystemFontLoader(const SkEmbeddedResourceHeader* header) : fHeader(h
eader) { } | 442 EmbeddedSystemFontLoader(const SkEmbeddedResourceHeader* header) : fHeader(h
eader) { } |
| 449 | 443 |
| 450 void loadSystemFonts(const SkTypeface_FreeType::Scanner& scanner, | 444 void loadSystemFonts(const SkTypeface_FreeType::Scanner& scanner, |
| 451 SkFontMgr_Custom::Families* families) const override | 445 SkFontMgr_Custom::Families* families) const override |
| 452 { | 446 { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 addTo = new SkFontStyleSet_Custom(realname); | 502 addTo = new SkFontStyleSet_Custom(realname); |
| 509 families->push_back().reset(addTo); | 503 families->push_back().reset(addTo); |
| 510 } | 504 } |
| 511 addTo->appendTypeface(tf); | 505 addTo->appendTypeface(tf); |
| 512 } | 506 } |
| 513 } | 507 } |
| 514 | 508 |
| 515 const SkEmbeddedResourceHeader* fHeader; | 509 const SkEmbeddedResourceHeader* fHeader; |
| 516 }; | 510 }; |
| 517 | 511 |
| 518 #ifdef SK_EMBEDDED_FONTS | 512 SkFontMgr* SkFontMgr_New_Custom_Embedded(const SkEmbeddedResourceHeader* header)
{ |
| 519 | 513 return new SkFontMgr_Custom(EmbeddedSystemFontLoader(header)); |
| 520 extern "C" const SkEmbeddedResourceHeader SK_EMBEDDED_FONTS; | |
| 521 SkFontMgr* SkFontMgr::Factory() { | |
| 522 return new SkFontMgr_Custom(EmbeddedSystemFontLoader(&SK_EMBEDDED_FONTS)); | |
| 523 } | 514 } |
| 524 | |
| 525 #else | |
| 526 | |
| 527 #ifndef SK_FONT_FILE_PREFIX | |
| 528 # define SK_FONT_FILE_PREFIX "/usr/share/fonts/" | |
| 529 #endif | |
| 530 SkFontMgr* SkFontMgr::Factory() { | |
| 531 return new SkFontMgr_Custom(DirectorySystemFontLoader(SK_FONT_FILE_PREFIX)); | |
| 532 } | |
| 533 | |
| 534 #endif | |
| OLD | NEW |