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 "SkFontConfigParser_android.h" | 8 #include "SkFontConfigParser_android.h" |
9 #include "SkFontDescriptor.h" | 9 #include "SkFontDescriptor.h" |
10 #include "SkFontHost_FreeType_common.h" | 10 #include "SkFontHost_FreeType_common.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include <limits> | 21 #include <limits> |
22 | 22 |
23 // For test only. | 23 // For test only. |
24 static const char* gTestFontsXml = NULL; | 24 static const char* gTestFontsXml = NULL; |
25 static const char* gTestFallbackFontsXml = NULL; | 25 static const char* gTestFallbackFontsXml = NULL; |
26 static const char* gTestBasePath = NULL; | 26 static const char* gTestBasePath = NULL; |
27 | 27 |
28 class SkTypeface_Android : public SkTypeface_FreeType { | 28 class SkTypeface_Android : public SkTypeface_FreeType { |
29 public: | 29 public: |
30 SkTypeface_Android(int index, | 30 SkTypeface_Android(int index, |
| 31 const SkTArray<SkFixed, true>& axes, |
31 const SkFontStyle& style, | 32 const SkFontStyle& style, |
32 bool isFixedPitch, | 33 bool isFixedPitch, |
33 const SkString& familyName) | 34 const SkString& familyName) |
34 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) | 35 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) |
35 , fIndex(index) | 36 , fIndex(index) |
36 , fFamilyName(familyName) { } | 37 , fFamilyName(familyName) |
| 38 , fAxes(axes) |
| 39 { } |
37 | 40 |
38 protected: | 41 protected: |
39 void onGetFamilyName(SkString* familyName) const override { | 42 void onGetFamilyName(SkString* familyName) const override { |
40 *familyName = fFamilyName; | 43 *familyName = fFamilyName; |
41 } | 44 } |
42 | 45 |
43 int fIndex; | 46 int fIndex; |
44 SkString fFamilyName; | 47 SkString fFamilyName; |
| 48 const SkTArray<SkFixed, true> fAxes; |
45 | 49 |
46 private: | 50 private: |
47 typedef SkTypeface_FreeType INHERITED; | 51 typedef SkTypeface_FreeType INHERITED; |
48 }; | 52 }; |
49 | 53 |
50 class SkTypeface_AndroidSystem : public SkTypeface_Android { | 54 class SkTypeface_AndroidSystem : public SkTypeface_Android { |
51 public: | 55 public: |
52 SkTypeface_AndroidSystem(const SkString& pathName, | 56 SkTypeface_AndroidSystem(const SkString& pathName, |
53 int index, | 57 int index, |
| 58 const SkTArray<SkFixed, true>& axes, |
54 const SkFontStyle& style, | 59 const SkFontStyle& style, |
55 bool isFixedPitch, | 60 bool isFixedPitch, |
56 const SkString& familyName, | 61 const SkString& familyName, |
57 const SkLanguage& lang, | 62 const SkLanguage& lang, |
58 FontVariant variantStyle) | 63 FontVariant variantStyle) |
59 : INHERITED(index, style, isFixedPitch, familyName) | 64 : INHERITED(index, axes, style, isFixedPitch, familyName) |
60 , fPathName(pathName) | 65 , fPathName(pathName) |
61 , fLang(lang) | 66 , fLang(lang) |
62 , fVariantStyle(variantStyle) { } | 67 , fVariantStyle(variantStyle) { } |
63 | 68 |
64 virtual void onGetFontDescriptor(SkFontDescriptor* desc, | 69 virtual void onGetFontDescriptor(SkFontDescriptor* desc, |
65 bool* serialize) const override { | 70 bool* serialize) const override { |
66 SkASSERT(desc); | 71 SkASSERT(desc); |
67 SkASSERT(serialize); | 72 SkASSERT(serialize); |
68 desc->setFamilyName(fFamilyName.c_str()); | 73 desc->setFamilyName(fFamilyName.c_str()); |
69 desc->setFontIndex(fIndex); | |
70 *serialize = false; | 74 *serialize = false; |
71 } | 75 } |
72 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 76 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
73 *ttcIndex = fIndex; | 77 *ttcIndex = fIndex; |
74 return SkStream::NewFromFile(fPathName.c_str()); | 78 return SkStream::NewFromFile(fPathName.c_str()); |
75 } | 79 } |
| 80 SkFontData* onCreateFontData() const override { |
| 81 return new SkFontData(SkStream::NewFromFile(fPathName.c_str()), fIndex, |
| 82 fAxes.count(), fAxes.begin()); |
| 83 } |
76 | 84 |
77 const SkString fPathName; | 85 const SkString fPathName; |
78 const SkLanguage fLang; | 86 const SkLanguage fLang; |
79 const FontVariant fVariantStyle; | 87 const FontVariant fVariantStyle; |
80 | 88 |
81 typedef SkTypeface_Android INHERITED; | 89 typedef SkTypeface_Android INHERITED; |
82 }; | 90 }; |
83 | 91 |
84 class SkTypeface_AndroidStream : public SkTypeface_Android { | 92 class SkTypeface_AndroidStream : public SkTypeface_Android { |
85 public: | 93 public: |
86 SkTypeface_AndroidStream(SkStreamAsset* stream, | 94 SkTypeface_AndroidStream(SkStreamAsset* stream, |
87 int index, | 95 int index, |
| 96 const SkTArray<SkFixed, true>& axes, |
88 const SkFontStyle& style, | 97 const SkFontStyle& style, |
89 bool isFixedPitch, | 98 bool isFixedPitch, |
90 const SkString& familyName) | 99 const SkString& familyName) |
91 : INHERITED(index, style, isFixedPitch, familyName) | 100 : INHERITED(index, axes, style, isFixedPitch, familyName) |
92 , fStream(stream) { } | 101 , fStream(stream) { } |
93 | 102 |
94 virtual void onGetFontDescriptor(SkFontDescriptor* desc, | 103 virtual void onGetFontDescriptor(SkFontDescriptor* desc, |
95 bool* serialize) const override { | 104 bool* serialize) const override { |
96 SkASSERT(desc); | 105 SkASSERT(desc); |
97 SkASSERT(serialize); | 106 SkASSERT(serialize); |
98 desc->setFamilyName(fFamilyName.c_str()); | 107 desc->setFamilyName(fFamilyName.c_str()); |
99 *serialize = true; | 108 *serialize = true; |
100 } | 109 } |
101 | 110 |
102 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 111 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
103 *ttcIndex = fIndex; | 112 *ttcIndex = fIndex; |
104 return fStream->duplicate(); | 113 return fStream->duplicate(); |
105 } | 114 } |
106 | 115 |
| 116 SkFontData* onCreateFontData() const override { |
| 117 return new SkFontData(fStream->duplicate(), fIndex, fAxes.count(), fAxes
.begin()); |
| 118 } |
| 119 |
107 private: | 120 private: |
108 SkAutoTDelete<SkStreamAsset> fStream; | 121 SkAutoTDelete<SkStreamAsset> fStream; |
109 | 122 |
110 typedef SkTypeface_Android INHERITED; | 123 typedef SkTypeface_Android INHERITED; |
111 }; | 124 }; |
112 | 125 |
113 class SkFontStyleSet_Android : public SkFontStyleSet { | 126 class SkFontStyleSet_Android : public SkFontStyleSet { |
114 public: | 127 public: |
115 explicit SkFontStyleSet_Android(const FontFamily& family, | 128 explicit SkFontStyleSet_Android(const FontFamily& family, |
116 const SkTypeface_FreeType::Scanner& scanner) | 129 const SkTypeface_FreeType::Scanner& scanner) |
(...skipping 13 matching lines...) Expand all Loading... |
130 if (!stream.get()) { | 143 if (!stream.get()) { |
131 SkDEBUGF(("Requested font file %s does not exist or cannot be op
ened.\n", | 144 SkDEBUGF(("Requested font file %s does not exist or cannot be op
ened.\n", |
132 pathName.c_str())); | 145 pathName.c_str())); |
133 continue; | 146 continue; |
134 } | 147 } |
135 | 148 |
136 const int ttcIndex = fontFile.fIndex; | 149 const int ttcIndex = fontFile.fIndex; |
137 SkString familyName; | 150 SkString familyName; |
138 SkFontStyle style; | 151 SkFontStyle style; |
139 bool isFixedWidth; | 152 bool isFixedWidth; |
140 if (!scanner.scanFont(stream.get(), ttcIndex, &familyName, &style, &
isFixedWidth)) { | 153 SkTArray<SkFixed, true> axes; |
| 154 if (!scanner.scanFont(stream.get(), ttcIndex, fontFile.fAxes.begin()
, fontFile.fAxes.count(), |
| 155 &familyName, &style, &isFixedWidth, &axes)) { |
141 SkDEBUGF(("Requested font file %s exists, but is not a valid fon
t.\n", | 156 SkDEBUGF(("Requested font file %s exists, but is not a valid fon
t.\n", |
142 pathName.c_str())); | 157 pathName.c_str())); |
143 continue; | 158 continue; |
144 } | 159 } |
145 | 160 |
146 int weight = fontFile.fWeight != 0 ? fontFile.fWeight : style.weight
(); | 161 int weight = fontFile.fWeight != 0 ? fontFile.fWeight : style.weight
(); |
147 SkFontStyle::Slant slant = style.slant(); | 162 SkFontStyle::Slant slant = style.slant(); |
148 switch (fontFile.fStyle) { | 163 switch (fontFile.fStyle) { |
149 case FontFileInfo::Style::kAuto: slant = style.slant(); break; | 164 case FontFileInfo::Style::kAuto: slant = style.slant(); break; |
150 case FontFileInfo::Style::kNormal: slant = SkFontStyle::kUpright
_Slant; break; | 165 case FontFileInfo::Style::kNormal: slant = SkFontStyle::kUpright
_Slant; break; |
151 case FontFileInfo::Style::kItalic: slant = SkFontStyle::kItalic_
Slant; break; | 166 case FontFileInfo::Style::kItalic: slant = SkFontStyle::kItalic_
Slant; break; |
152 default: SkASSERT(false); break; | 167 default: SkASSERT(false); break; |
153 } | 168 } |
154 style = SkFontStyle(weight, style.width(), slant); | 169 style = SkFontStyle(weight, style.width(), slant); |
155 | 170 |
156 const SkLanguage& lang = family.fLanguage; | 171 const SkLanguage& lang = family.fLanguage; |
157 uint32_t variant = family.fVariant; | 172 uint32_t variant = family.fVariant; |
158 if (kDefault_FontVariant == variant) { | 173 if (kDefault_FontVariant == variant) { |
159 variant = kCompact_FontVariant | kElegant_FontVariant; | 174 variant = kCompact_FontVariant | kElegant_FontVariant; |
160 } | 175 } |
161 | 176 |
162 // The first specified family name overrides the family name found i
n the font. | 177 // The first specified family name overrides the family name found i
n the font. |
163 // TODO: SkTypeface_AndroidSystem::onCreateFamilyNameIterator should
return | 178 // TODO: SkTypeface_AndroidSystem::onCreateFamilyNameIterator should
return |
164 // all of the specified family names in addition to the names found
in the font. | 179 // all of the specified family names in addition to the names found
in the font. |
165 if (cannonicalFamilyName != NULL) { | 180 if (cannonicalFamilyName != NULL) { |
166 familyName = *cannonicalFamilyName; | 181 familyName = *cannonicalFamilyName; |
167 } | 182 } |
168 | 183 |
169 fStyles.push_back().reset(SkNEW_ARGS(SkTypeface_AndroidSystem, | 184 fStyles.push_back().reset(SkNEW_ARGS(SkTypeface_AndroidSystem, |
170 (pathName, ttcIndex, | 185 (pathName, ttcIndex, axes, |
171 style, isFixedWidth, familyNam
e, | 186 style, isFixedWidth, familyNam
e, |
172 lang, variant))); | 187 lang, variant))); |
173 } | 188 } |
174 } | 189 } |
175 | 190 |
176 int count() override { | 191 int count() override { |
177 return fStyles.count(); | 192 return fStyles.count(); |
178 } | 193 } |
179 void getStyle(int index, SkFontStyle* style, SkString* name) override { | 194 void getStyle(int index, SkFontStyle* style, SkString* name) override { |
180 if (index < 0 || fStyles.count() <= index) { | 195 if (index < 0 || fStyles.count() <= index) { |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override
{ | 419 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override
{ |
405 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path)); | 420 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path)); |
406 return stream.get() ? this->createFromStream(stream.detach(), ttcIndex)
: NULL; | 421 return stream.get() ? this->createFromStream(stream.detach(), ttcIndex)
: NULL; |
407 } | 422 } |
408 | 423 |
409 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t override { | 424 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t override { |
410 SkAutoTDelete<SkStreamAsset> stream(bareStream); | 425 SkAutoTDelete<SkStreamAsset> stream(bareStream); |
411 bool isFixedPitch; | 426 bool isFixedPitch; |
412 SkFontStyle style; | 427 SkFontStyle style; |
413 SkString name; | 428 SkString name; |
414 if (!fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch))
{ | 429 SkTArray<SkFixed, true> axes; |
| 430 if (!fScanner.scanFont(stream, ttcIndex, NULL, 0, &name, &style, &isFixe
dPitch, &axes)) { |
415 return NULL; | 431 return NULL; |
416 } | 432 } |
417 return SkNEW_ARGS(SkTypeface_AndroidStream, (stream.detach(), ttcIndex, | 433 return SkNEW_ARGS(SkTypeface_AndroidStream, (stream.detach(), ttcIndex,
axes, |
418 style, isFixedPitch, name))
; | 434 style, isFixedPitch, name))
; |
419 } | 435 } |
420 | 436 |
| 437 SkTypeface* onCreateFromFontData(SkFontData* data) const override { |
| 438 SkAutoTDelete<SkStreamAsset> stream(data->transferStream()); |
| 439 bool isFixedPitch; |
| 440 SkFontStyle style; |
| 441 SkString name; |
| 442 SkTArray<SkFixed, true> axes; |
| 443 if (!fScanner.scanFont(stream, data->getIndex(), NULL, 0, &name, &style,
&isFixedPitch, &axes)) { |
| 444 return NULL; |
| 445 } |
| 446 return SkNEW_ARGS(SkTypeface_AndroidStream, (stream.detach(), data->getI
ndex(), |
| 447 SkTArray<SkFixed, true>(dat
a->getAxis(), data->getAxisCount()), |
| 448 style, isFixedPitch, name))
; |
| 449 } |
| 450 |
421 | 451 |
422 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], | 452 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
423 unsigned styleBits) const overrid
e { | 453 unsigned styleBits) const overrid
e { |
424 SkFontStyle style = SkFontStyle(styleBits); | 454 SkFontStyle style = SkFontStyle(styleBits); |
425 | 455 |
426 if (familyName) { | 456 if (familyName) { |
427 // On Android, we must return NULL when we can't find the requested | 457 // On Android, we must return NULL when we can't find the requested |
428 // named typeface so that the system/app can provide their own recov
ery | 458 // named typeface so that the system/app can provide their own recov
ery |
429 // mechanism. On other platforms we'd provide a typeface from the | 459 // mechanism. On other platforms we'd provide a typeface from the |
430 // default family instead. | 460 // default family instead. |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 { | 574 { |
545 gTestFontsXml = fontsXml; | 575 gTestFontsXml = fontsXml; |
546 gTestFallbackFontsXml = fallbackFontsXml; | 576 gTestFallbackFontsXml = fallbackFontsXml; |
547 gTestBasePath = basePath; | 577 gTestBasePath = basePath; |
548 SkASSERT(gTestFontsXml); | 578 SkASSERT(gTestFontsXml); |
549 SkASSERT(gTestFallbackFontsXml); | 579 SkASSERT(gTestFallbackFontsXml); |
550 SkASSERT(gTestBasePath); | 580 SkASSERT(gTestBasePath); |
551 SkDEBUGF(("Test BasePath: %s Fonts: %s FallbackFonts: %s\n", | 581 SkDEBUGF(("Test BasePath: %s Fonts: %s FallbackFonts: %s\n", |
552 gTestBasePath, gTestFontsXml, gTestFallbackFontsXml)); | 582 gTestBasePath, gTestFontsXml, gTestFallbackFontsXml)); |
553 } | 583 } |
OLD | NEW |