| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 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 "SkDataTable.h" | 8 #include "SkDataTable.h" |
| 9 #include "SkFontDescriptor.h" | 9 #include "SkFontDescriptor.h" |
| 10 #include "SkFontHost_FreeType_common.h" | 10 #include "SkFontHost_FreeType_common.h" |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 }; | 368 }; |
| 369 int width = map_ranges(style.width(), widthRanges, SK_ARRAY_COUNT(widthRange
s)); | 369 int width = map_ranges(style.width(), widthRanges, SK_ARRAY_COUNT(widthRange
s)); |
| 370 | 370 |
| 371 FcPatternAddInteger(pattern, FC_WEIGHT, weight); | 371 FcPatternAddInteger(pattern, FC_WEIGHT, weight); |
| 372 FcPatternAddInteger(pattern, FC_WIDTH, width); | 372 FcPatternAddInteger(pattern, FC_WIDTH, width); |
| 373 FcPatternAddInteger(pattern, FC_SLANT, style.isItalic() ? FC_SLANT_ITALIC :
FC_SLANT_ROMAN); | 373 FcPatternAddInteger(pattern, FC_SLANT, style.isItalic() ? FC_SLANT_ITALIC :
FC_SLANT_ROMAN); |
| 374 } | 374 } |
| 375 | 375 |
| 376 class SkTypeface_stream : public SkTypeface_FreeType { | 376 class SkTypeface_stream : public SkTypeface_FreeType { |
| 377 public: | 377 public: |
| 378 /** @param stream does not take ownership of the reference, does take owners
hip of the stream.*/ | 378 /** @param data takes ownership of the font data.*/ |
| 379 SkTypeface_stream(const SkFontStyle& style, bool fixedWidth, int index, SkSt
reamAsset* stream) | 379 SkTypeface_stream(SkFontData* data, const SkFontStyle& style, bool fixedWidt
h) |
| 380 : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth) | 380 : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth) |
| 381 , fStream(stream) | 381 , fData(data) |
| 382 , fIndex(index) | |
| 383 { }; | 382 { }; |
| 384 | 383 |
| 385 void onGetFamilyName(SkString* familyName) const override { | 384 void onGetFamilyName(SkString* familyName) const override { |
| 386 familyName->reset(); | 385 familyName->reset(); |
| 387 } | 386 } |
| 388 | 387 |
| 389 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const over
ride { | 388 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const over
ride { |
| 390 desc->setFontIndex(fIndex); | |
| 391 *serialize = true; | 389 *serialize = true; |
| 392 } | 390 } |
| 393 | 391 |
| 394 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 392 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
| 395 *ttcIndex = fIndex; | 393 *ttcIndex = fData->getIndex(); |
| 396 return fStream->duplicate(); | 394 return fData->duplicateStream(); |
| 395 } |
| 396 |
| 397 SkFontData* onCreateFontData() const override { |
| 398 return new SkFontData(*fData.get()); |
| 397 } | 399 } |
| 398 | 400 |
| 399 private: | 401 private: |
| 400 SkAutoTDelete<SkStreamAsset> fStream; | 402 const SkAutoTDelete<const SkFontData> fData; |
| 401 int fIndex; | |
| 402 | 403 |
| 403 typedef SkTypeface_FreeType INHERITED; | 404 typedef SkTypeface_FreeType INHERITED; |
| 404 }; | 405 }; |
| 405 | 406 |
| 406 class SkTypeface_fontconfig : public SkTypeface_FreeType { | 407 class SkTypeface_fontconfig : public SkTypeface_FreeType { |
| 407 public: | 408 public: |
| 408 /** @param pattern takes ownership of the reference. */ | 409 /** @param pattern takes ownership of the reference. */ |
| 409 static SkTypeface_fontconfig* Create(FcPattern* pattern) { | 410 static SkTypeface_fontconfig* Create(FcPattern* pattern) { |
| 410 return SkNEW_ARGS(SkTypeface_fontconfig, (pattern)); | 411 return SkNEW_ARGS(SkTypeface_fontconfig, (pattern)); |
| 411 } | 412 } |
| 412 mutable SkAutoFcPattern fPattern; | 413 mutable SkAutoFcPattern fPattern; |
| 413 | 414 |
| 414 void onGetFamilyName(SkString* familyName) const override { | 415 void onGetFamilyName(SkString* familyName) const override { |
| 415 *familyName = get_string(fPattern, FC_FAMILY); | 416 *familyName = get_string(fPattern, FC_FAMILY); |
| 416 } | 417 } |
| 417 | 418 |
| 418 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const over
ride { | 419 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const over
ride { |
| 419 FCLocker lock; | 420 FCLocker lock; |
| 420 desc->setFamilyName(get_string(fPattern, FC_FAMILY)); | 421 desc->setFamilyName(get_string(fPattern, FC_FAMILY)); |
| 421 desc->setFullName(get_string(fPattern, FC_FULLNAME)); | 422 desc->setFullName(get_string(fPattern, FC_FULLNAME)); |
| 422 desc->setPostscriptName(get_string(fPattern, FC_POSTSCRIPT_NAME)); | 423 desc->setPostscriptName(get_string(fPattern, FC_POSTSCRIPT_NAME)); |
| 423 desc->setFontIndex(get_int(fPattern, FC_INDEX, 0)); | |
| 424 *serialize = false; | 424 *serialize = false; |
| 425 } | 425 } |
| 426 | 426 |
| 427 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 427 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
| 428 FCLocker lock; | 428 FCLocker lock; |
| 429 *ttcIndex = get_int(fPattern, FC_INDEX, 0); | 429 *ttcIndex = get_int(fPattern, FC_INDEX, 0); |
| 430 return SkStream::NewFromFile(get_string(fPattern, FC_FILE)); | 430 return SkStream::NewFromFile(get_string(fPattern, FC_FILE)); |
| 431 } | 431 } |
| 432 | 432 |
| 433 virtual ~SkTypeface_fontconfig() { | 433 virtual ~SkTypeface_fontconfig() { |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 815 | 815 |
| 816 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t override { | 816 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t override { |
| 817 SkAutoTDelete<SkStreamAsset> stream(bareStream); | 817 SkAutoTDelete<SkStreamAsset> stream(bareStream); |
| 818 const size_t length = stream->getLength(); | 818 const size_t length = stream->getLength(); |
| 819 if (length <= 0 || (1u << 30) < length) { | 819 if (length <= 0 || (1u << 30) < length) { |
| 820 return NULL; | 820 return NULL; |
| 821 } | 821 } |
| 822 | 822 |
| 823 SkFontStyle style; | 823 SkFontStyle style; |
| 824 bool isFixedWidth = false; | 824 bool isFixedWidth = false; |
| 825 if (!fScanner.scanFont(stream, ttcIndex, NULL, &style, &isFixedWidth)) { | 825 if (!fScanner.scanFont(stream, ttcIndex, NULL, &style, &isFixedWidth, NU
LL)) { |
| 826 return NULL; | 826 return NULL; |
| 827 } | 827 } |
| 828 | 828 |
| 829 return SkNEW_ARGS(SkTypeface_stream, (style, isFixedWidth, ttcIndex, | 829 return SkNEW_ARGS(SkTypeface_stream, (new SkFontData(stream.detach(), tt
cIndex, NULL, 0), |
| 830 static_cast<SkStreamAsset*>(stream
.detach()))); | 830 style, isFixedWidth)); |
| 831 } | 831 } |
| 832 | 832 |
| 833 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { | 833 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { |
| 834 return this->createFromStream(SkNEW_ARGS(SkMemoryStream, (data)), ttcInd
ex); | 834 return this->createFromStream(SkNEW_ARGS(SkMemoryStream, (data)), ttcInd
ex); |
| 835 } | 835 } |
| 836 | 836 |
| 837 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override
{ | 837 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override
{ |
| 838 return this->createFromStream(SkStream::NewFromFile(path), ttcIndex); | 838 return this->createFromStream(SkStream::NewFromFile(path), ttcIndex); |
| 839 } | 839 } |
| 840 | 840 |
| 841 SkTypeface* onCreateFromFontData(SkFontData* fontData) const override { |
| 842 SkStreamAsset* stream(fontData->getStream()); |
| 843 const size_t length = stream->getLength(); |
| 844 if (length <= 0 || (1u << 30) < length) { |
| 845 return NULL; |
| 846 } |
| 847 |
| 848 const int ttcIndex = fontData->getIndex(); |
| 849 SkFontStyle style; |
| 850 bool isFixedWidth = false; |
| 851 if (!fScanner.scanFont(stream, ttcIndex, NULL, &style, &isFixedWidth, NU
LL)) { |
| 852 return NULL; |
| 853 } |
| 854 |
| 855 return SkNEW_ARGS(SkTypeface_stream, (fontData, style, isFixedWidth)); |
| 856 } |
| 857 |
| 841 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], | 858 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
| 842 unsigned styleBits) const overrid
e { | 859 unsigned styleBits) const overrid
e { |
| 843 bool bold = styleBits & SkTypeface::kBold; | 860 bool bold = styleBits & SkTypeface::kBold; |
| 844 bool italic = styleBits & SkTypeface::kItalic; | 861 bool italic = styleBits & SkTypeface::kItalic; |
| 845 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight | 862 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight |
| 846 : SkFontStyle::kNormal_Weight, | 863 : SkFontStyle::kNormal_Weight, |
| 847 SkFontStyle::kNormal_Width, | 864 SkFontStyle::kNormal_Width, |
| 848 italic ? SkFontStyle::kItalic_Slant | 865 italic ? SkFontStyle::kItalic_Slant |
| 849 : SkFontStyle::kUpright_Slant); | 866 : SkFontStyle::kUpright_Slant); |
| 850 SkAutoTUnref<SkTypeface> typeface(this->matchFamilyStyle(familyName, sty
le)); | 867 SkAutoTUnref<SkTypeface> typeface(this->matchFamilyStyle(familyName, sty
le)); |
| 851 if (typeface.get()) { | 868 if (typeface.get()) { |
| 852 return typeface.detach(); | 869 return typeface.detach(); |
| 853 } | 870 } |
| 854 | 871 |
| 855 return this->matchFamilyStyle(NULL, style); | 872 return this->matchFamilyStyle(NULL, style); |
| 856 } | 873 } |
| 857 }; | 874 }; |
| 858 | 875 |
| 859 SkFontMgr* SkFontMgr::Factory() { | 876 SkFontMgr* SkFontMgr::Factory() { |
| 860 return SkNEW(SkFontMgr_fontconfig); | 877 return SkNEW(SkFontMgr_fontconfig); |
| 861 } | 878 } |
| OLD | NEW |