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 data takes ownership of the font data.*/ | 378 /** @param stream does not take ownership of the reference, does take owners
hip of the stream.*/ |
379 SkTypeface_stream(SkFontData* data, const SkFontStyle& style, bool fixedWidt
h) | 379 SkTypeface_stream(const SkFontStyle& style, bool fixedWidth, int index, SkSt
reamAsset* stream) |
380 : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth) | 380 : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth) |
381 , fData(data) | 381 , fStream(stream) |
| 382 , fIndex(index) |
382 { }; | 383 { }; |
383 | 384 |
384 void onGetFamilyName(SkString* familyName) const override { | 385 void onGetFamilyName(SkString* familyName) const override { |
385 familyName->reset(); | 386 familyName->reset(); |
386 } | 387 } |
387 | 388 |
388 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const over
ride { | 389 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const over
ride { |
| 390 desc->setFontIndex(fIndex); |
389 *serialize = true; | 391 *serialize = true; |
390 } | 392 } |
391 | 393 |
392 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 394 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
393 *ttcIndex = fData->getIndex(); | 395 *ttcIndex = fIndex; |
394 return fData->duplicateStream(); | 396 return fStream->duplicate(); |
395 } | |
396 | |
397 SkFontData* onCreateFontData() const override { | |
398 return new SkFontData(*fData.get()); | |
399 } | 397 } |
400 | 398 |
401 private: | 399 private: |
402 const SkAutoTDelete<const SkFontData> fData; | 400 SkAutoTDelete<SkStreamAsset> fStream; |
| 401 int fIndex; |
403 | 402 |
404 typedef SkTypeface_FreeType INHERITED; | 403 typedef SkTypeface_FreeType INHERITED; |
405 }; | 404 }; |
406 | 405 |
407 class SkTypeface_fontconfig : public SkTypeface_FreeType { | 406 class SkTypeface_fontconfig : public SkTypeface_FreeType { |
408 public: | 407 public: |
409 /** @param pattern takes ownership of the reference. */ | 408 /** @param pattern takes ownership of the reference. */ |
410 static SkTypeface_fontconfig* Create(FcPattern* pattern) { | 409 static SkTypeface_fontconfig* Create(FcPattern* pattern) { |
411 return SkNEW_ARGS(SkTypeface_fontconfig, (pattern)); | 410 return SkNEW_ARGS(SkTypeface_fontconfig, (pattern)); |
412 } | 411 } |
413 mutable SkAutoFcPattern fPattern; | 412 mutable SkAutoFcPattern fPattern; |
414 | 413 |
415 void onGetFamilyName(SkString* familyName) const override { | 414 void onGetFamilyName(SkString* familyName) const override { |
416 *familyName = get_string(fPattern, FC_FAMILY); | 415 *familyName = get_string(fPattern, FC_FAMILY); |
417 } | 416 } |
418 | 417 |
419 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const over
ride { | 418 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const over
ride { |
420 FCLocker lock; | 419 FCLocker lock; |
421 desc->setFamilyName(get_string(fPattern, FC_FAMILY)); | 420 desc->setFamilyName(get_string(fPattern, FC_FAMILY)); |
422 desc->setFullName(get_string(fPattern, FC_FULLNAME)); | 421 desc->setFullName(get_string(fPattern, FC_FULLNAME)); |
423 desc->setPostscriptName(get_string(fPattern, FC_POSTSCRIPT_NAME)); | 422 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, NU
LL)) { | 825 if (!fScanner.scanFont(stream, ttcIndex, NULL, &style, &isFixedWidth)) { |
826 return NULL; | 826 return NULL; |
827 } | 827 } |
828 | 828 |
829 return SkNEW_ARGS(SkTypeface_stream, (new SkFontData(stream.detach(), tt
cIndex, NULL, 0), | 829 return SkNEW_ARGS(SkTypeface_stream, (style, isFixedWidth, ttcIndex, |
830 style, isFixedWidth)); | 830 static_cast<SkStreamAsset*>(stream
.detach()))); |
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 | |
858 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], | 841 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
859 unsigned styleBits) const overrid
e { | 842 unsigned styleBits) const overrid
e { |
860 bool bold = styleBits & SkTypeface::kBold; | 843 bool bold = styleBits & SkTypeface::kBold; |
861 bool italic = styleBits & SkTypeface::kItalic; | 844 bool italic = styleBits & SkTypeface::kItalic; |
862 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight | 845 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight |
863 : SkFontStyle::kNormal_Weight, | 846 : SkFontStyle::kNormal_Weight, |
864 SkFontStyle::kNormal_Width, | 847 SkFontStyle::kNormal_Width, |
865 italic ? SkFontStyle::kItalic_Slant | 848 italic ? SkFontStyle::kItalic_Slant |
866 : SkFontStyle::kUpright_Slant); | 849 : SkFontStyle::kUpright_Slant); |
867 SkAutoTUnref<SkTypeface> typeface(this->matchFamilyStyle(familyName, sty
le)); | 850 SkAutoTUnref<SkTypeface> typeface(this->matchFamilyStyle(familyName, sty
le)); |
868 if (typeface.get()) { | 851 if (typeface.get()) { |
869 return typeface.detach(); | 852 return typeface.detach(); |
870 } | 853 } |
871 | 854 |
872 return this->matchFamilyStyle(NULL, style); | 855 return this->matchFamilyStyle(NULL, style); |
873 } | 856 } |
874 }; | 857 }; |
875 | 858 |
876 SkFontMgr* SkFontMgr::Factory() { | 859 SkFontMgr* SkFontMgr::Factory() { |
877 return SkNEW(SkFontMgr_fontconfig); | 860 return SkNEW(SkFontMgr_fontconfig); |
878 } | 861 } |
OLD | NEW |