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" |
11 #include "SkFontMgr.h" | 11 #include "SkFontMgr.h" |
12 #include "SkFontMgr_android.h" | 12 #include "SkFontMgr_android.h" |
13 #include "SkFontStyle.h" | 13 #include "SkFontStyle.h" |
14 #include "SkStream.h" | 14 #include "SkStream.h" |
15 #include "SkTDArray.h" | 15 #include "SkTDArray.h" |
16 #include "SkTSearch.h" | 16 #include "SkTSearch.h" |
17 #include "SkTypeface.h" | 17 #include "SkTypeface.h" |
18 #include "SkTypeface_android.h" | 18 #include "SkTypeface_android.h" |
19 #include "SkTypefaceCache.h" | 19 #include "SkTypefaceCache.h" |
20 | 20 |
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(const SkFontStyle& style, |
31 const SkFontStyle& style, | |
32 bool isFixedPitch, | 31 bool isFixedPitch, |
33 const SkString& familyName) | 32 const SkString& familyName) |
34 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) | 33 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) |
35 , fIndex(index) | 34 , fFamilyName(familyName) |
36 , fFamilyName(familyName) { } | 35 { } |
37 | 36 |
38 protected: | 37 protected: |
39 void onGetFamilyName(SkString* familyName) const override { | 38 void onGetFamilyName(SkString* familyName) const override { |
40 *familyName = fFamilyName; | 39 *familyName = fFamilyName; |
41 } | 40 } |
42 | 41 |
43 int fIndex; | |
44 SkString fFamilyName; | 42 SkString fFamilyName; |
45 | 43 |
46 private: | 44 private: |
47 typedef SkTypeface_FreeType INHERITED; | 45 typedef SkTypeface_FreeType INHERITED; |
48 }; | 46 }; |
49 | 47 |
50 class SkTypeface_AndroidSystem : public SkTypeface_Android { | 48 class SkTypeface_AndroidSystem : public SkTypeface_Android { |
51 public: | 49 public: |
52 SkTypeface_AndroidSystem(const SkString& pathName, | 50 SkTypeface_AndroidSystem(const SkString& pathName, |
53 int index, | 51 int index, |
| 52 const SkFixed* axes, int axesCount, |
54 const SkFontStyle& style, | 53 const SkFontStyle& style, |
55 bool isFixedPitch, | 54 bool isFixedPitch, |
56 const SkString& familyName, | 55 const SkString& familyName, |
57 const SkLanguage& lang, | 56 const SkLanguage& lang, |
58 FontVariant variantStyle) | 57 FontVariant variantStyle) |
59 : INHERITED(index, style, isFixedPitch, familyName) | 58 : INHERITED(style, isFixedPitch, familyName) |
60 , fPathName(pathName) | 59 , fPathName(pathName) |
| 60 , fIndex(index) |
| 61 , fAxes(axes, axesCount) |
61 , fLang(lang) | 62 , fLang(lang) |
62 , fVariantStyle(variantStyle) { } | 63 , fVariantStyle(variantStyle) { } |
63 | 64 |
64 virtual void onGetFontDescriptor(SkFontDescriptor* desc, | 65 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) co
nst override { |
65 bool* serialize) const override { | |
66 SkASSERT(desc); | 66 SkASSERT(desc); |
67 SkASSERT(serialize); | 67 SkASSERT(serialize); |
68 desc->setFamilyName(fFamilyName.c_str()); | 68 desc->setFamilyName(fFamilyName.c_str()); |
69 desc->setFontIndex(fIndex); | |
70 *serialize = false; | 69 *serialize = false; |
71 } | 70 } |
72 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 71 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
73 *ttcIndex = fIndex; | 72 *ttcIndex = fIndex; |
74 return SkStream::NewFromFile(fPathName.c_str()); | 73 return SkStream::NewFromFile(fPathName.c_str()); |
75 } | 74 } |
| 75 SkFontData* onCreateFontData() const override { |
| 76 return new SkFontData(SkStream::NewFromFile(fPathName.c_str()), fIndex, |
| 77 fAxes.begin(), fAxes.count()); |
| 78 } |
76 | 79 |
77 const SkString fPathName; | 80 const SkString fPathName; |
| 81 int fIndex; |
| 82 const SkSTArray<4, SkFixed, true> fAxes; |
78 const SkLanguage fLang; | 83 const SkLanguage fLang; |
79 const FontVariant fVariantStyle; | 84 const FontVariant fVariantStyle; |
80 | 85 |
81 typedef SkTypeface_Android INHERITED; | 86 typedef SkTypeface_Android INHERITED; |
82 }; | 87 }; |
83 | 88 |
84 class SkTypeface_AndroidStream : public SkTypeface_Android { | 89 class SkTypeface_AndroidStream : public SkTypeface_Android { |
85 public: | 90 public: |
86 SkTypeface_AndroidStream(SkStreamAsset* stream, | 91 SkTypeface_AndroidStream(SkFontData* data, |
87 int index, | |
88 const SkFontStyle& style, | 92 const SkFontStyle& style, |
89 bool isFixedPitch, | 93 bool isFixedPitch, |
90 const SkString& familyName) | 94 const SkString& familyName) |
91 : INHERITED(index, style, isFixedPitch, familyName) | 95 : INHERITED(style, isFixedPitch, familyName) |
92 , fStream(stream) { } | 96 , fData(data) |
| 97 { } |
93 | 98 |
94 virtual void onGetFontDescriptor(SkFontDescriptor* desc, | 99 virtual void onGetFontDescriptor(SkFontDescriptor* desc, |
95 bool* serialize) const override { | 100 bool* serialize) const override { |
96 SkASSERT(desc); | 101 SkASSERT(desc); |
97 SkASSERT(serialize); | 102 SkASSERT(serialize); |
98 desc->setFamilyName(fFamilyName.c_str()); | 103 desc->setFamilyName(fFamilyName.c_str()); |
99 *serialize = true; | 104 *serialize = true; |
100 } | 105 } |
101 | 106 |
102 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 107 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
103 *ttcIndex = fIndex; | 108 *ttcIndex = fData->getIndex(); |
104 return fStream->duplicate(); | 109 return fData->duplicateStream(); |
| 110 } |
| 111 |
| 112 SkFontData* onCreateFontData() const override { |
| 113 return new SkFontData(*fData.get()); |
105 } | 114 } |
106 | 115 |
107 private: | 116 private: |
108 SkAutoTDelete<SkStreamAsset> fStream; | 117 const SkAutoTDelete<const SkFontData> fData; |
109 | |
110 typedef SkTypeface_Android INHERITED; | 118 typedef SkTypeface_Android INHERITED; |
111 }; | 119 }; |
112 | 120 |
113 class SkFontStyleSet_Android : public SkFontStyleSet { | 121 class SkFontStyleSet_Android : public SkFontStyleSet { |
| 122 typedef SkTypeface_FreeType::Scanner Scanner; |
| 123 |
114 public: | 124 public: |
115 explicit SkFontStyleSet_Android(const FontFamily& family, | 125 explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& sca
nner) { |
116 const SkTypeface_FreeType::Scanner& scanner) | |
117 { | |
118 const SkString* cannonicalFamilyName = NULL; | 126 const SkString* cannonicalFamilyName = NULL; |
119 if (family.fNames.count() > 0) { | 127 if (family.fNames.count() > 0) { |
120 cannonicalFamilyName = &family.fNames[0]; | 128 cannonicalFamilyName = &family.fNames[0]; |
121 } | 129 } |
122 // TODO? make this lazy | 130 // TODO? make this lazy |
123 for (int i = 0; i < family.fFonts.count(); ++i) { | 131 for (int i = 0; i < family.fFonts.count(); ++i) { |
124 const FontFileInfo& fontFile = family.fFonts[i]; | 132 const FontFileInfo& fontFile = family.fFonts[i]; |
125 | 133 |
126 SkString pathName(family.fBasePath); | 134 SkString pathName(family.fBasePath); |
127 pathName.append(fontFile.fFileName); | 135 pathName.append(fontFile.fFileName); |
128 | 136 |
129 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(pathName.c_str(
))); | 137 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(pathName.c_str(
))); |
130 if (!stream.get()) { | 138 if (!stream.get()) { |
131 SkDEBUGF(("Requested font file %s does not exist or cannot be op
ened.\n", | 139 SkDEBUGF(("Requested font file %s does not exist or cannot be op
ened.\n", |
132 pathName.c_str())); | 140 pathName.c_str())); |
133 continue; | 141 continue; |
134 } | 142 } |
135 | 143 |
136 const int ttcIndex = fontFile.fIndex; | 144 const int ttcIndex = fontFile.fIndex; |
137 SkString familyName; | 145 SkString familyName; |
138 SkFontStyle style; | 146 SkFontStyle style; |
139 bool isFixedWidth; | 147 bool isFixedWidth; |
140 if (!scanner.scanFont(stream.get(), ttcIndex, &familyName, &style, &
isFixedWidth)) { | 148 Scanner::AxisDefinitions axisDefinitions; |
| 149 if (!scanner.scanFont(stream.get(), ttcIndex, |
| 150 &familyName, &style, &isFixedWidth, &axisDefin
itions)) |
| 151 { |
141 SkDEBUGF(("Requested font file %s exists, but is not a valid fon
t.\n", | 152 SkDEBUGF(("Requested font file %s exists, but is not a valid fon
t.\n", |
142 pathName.c_str())); | 153 pathName.c_str())); |
143 continue; | 154 continue; |
144 } | 155 } |
145 | 156 |
146 int weight = fontFile.fWeight != 0 ? fontFile.fWeight : style.weight
(); | 157 int weight = fontFile.fWeight != 0 ? fontFile.fWeight : style.weight
(); |
147 SkFontStyle::Slant slant = style.slant(); | 158 SkFontStyle::Slant slant = style.slant(); |
148 switch (fontFile.fStyle) { | 159 switch (fontFile.fStyle) { |
149 case FontFileInfo::Style::kAuto: slant = style.slant(); break; | 160 case FontFileInfo::Style::kAuto: slant = style.slant(); break; |
150 case FontFileInfo::Style::kNormal: slant = SkFontStyle::kUpright
_Slant; break; | 161 case FontFileInfo::Style::kNormal: slant = SkFontStyle::kUpright
_Slant; break; |
151 case FontFileInfo::Style::kItalic: slant = SkFontStyle::kItalic_
Slant; break; | 162 case FontFileInfo::Style::kItalic: slant = SkFontStyle::kItalic_
Slant; break; |
152 default: SkASSERT(false); break; | 163 default: SkASSERT(false); break; |
153 } | 164 } |
154 style = SkFontStyle(weight, style.width(), slant); | 165 style = SkFontStyle(weight, style.width(), slant); |
155 | 166 |
156 const SkLanguage& lang = family.fLanguage; | 167 const SkLanguage& lang = family.fLanguage; |
157 uint32_t variant = family.fVariant; | 168 uint32_t variant = family.fVariant; |
158 if (kDefault_FontVariant == variant) { | 169 if (kDefault_FontVariant == variant) { |
159 variant = kCompact_FontVariant | kElegant_FontVariant; | 170 variant = kCompact_FontVariant | kElegant_FontVariant; |
160 } | 171 } |
161 | 172 |
162 // The first specified family name overrides the family name found i
n the font. | 173 // The first specified family name overrides the family name found i
n the font. |
163 // TODO: SkTypeface_AndroidSystem::onCreateFamilyNameIterator should
return | 174 // TODO: SkTypeface_AndroidSystem::onCreateFamilyNameIterator should
return |
164 // all of the specified family names in addition to the names found
in the font. | 175 // all of the specified family names in addition to the names found
in the font. |
165 if (cannonicalFamilyName != NULL) { | 176 if (cannonicalFamilyName != NULL) { |
166 familyName = *cannonicalFamilyName; | 177 familyName = *cannonicalFamilyName; |
167 } | 178 } |
168 | 179 |
| 180 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count()); |
| 181 for (int i = 0; i < axisDefinitions.count(); ++i) { |
| 182 const Scanner::AxisDefinition& axisDefinition = axisDefinitions[
i]; |
| 183 axisValues[i] = axisDefinition.fDefault; |
| 184 for (int j = 0; j < fontFile.fAxes.count(); ++j) { |
| 185 const FontFileInfo::Axis& axisSpecified = fontFile.fAxes[j]; |
| 186 if (axisDefinition.fTag == axisSpecified.fTag) { |
| 187 axisValues[i] = SkTPin(axisSpecified.fValue, axisDefinit
ion.fMinimum, |
| 188 axisDefinit
ion.fMaximum); |
| 189 if (axisValues[i] != axisSpecified.fValue) { |
| 190 SkDEBUGF(("Requested font axis value out of range: " |
| 191 "%s '%c%c%c%c' %f; pinned to %f.\n", |
| 192 familyName.c_str(), |
| 193 (axisDefinition.fTag >> 24) & 0xFF, |
| 194 (axisDefinition.fTag >> 16) & 0xFF, |
| 195 (axisDefinition.fTag >> 8) & 0xFF, |
| 196 (axisDefinition.fTag ) & 0xFF, |
| 197 SkFixedToDouble(axisSpecified.fValue), |
| 198 SkFixedToDouble(axisValues[i]))); |
| 199 } |
| 200 break; |
| 201 } |
| 202 } |
| 203 // TODO: warn on defaulted axis? |
| 204 } |
| 205 |
| 206 SkDEBUGCODE ( |
| 207 // Check for axis specified, but not matched in font. |
| 208 for (int i = 0; i < fontFile.fAxes.count(); ++i) { |
| 209 SkFourByteTag skTag = fontFile.fAxes[i].fTag; |
| 210 bool found = false; |
| 211 for (int j = 0; j < axisDefinitions.count(); ++j) { |
| 212 if (skTag == axisDefinitions[j].fTag) { |
| 213 found = true; |
| 214 break; |
| 215 } |
| 216 } |
| 217 if (!found) { |
| 218 SkDEBUGF(("Requested font axis not found: %s '%c%c%c%c'\
n", |
| 219 familyName.c_str(), |
| 220 (skTag >> 24) & 0xFF, |
| 221 (skTag >> 16) & 0xFF, |
| 222 (skTag >> 8) & 0xFF, |
| 223 (skTag ) & 0xFF)); |
| 224 } |
| 225 } |
| 226 ) |
| 227 |
169 fStyles.push_back().reset(SkNEW_ARGS(SkTypeface_AndroidSystem, | 228 fStyles.push_back().reset(SkNEW_ARGS(SkTypeface_AndroidSystem, |
170 (pathName, ttcIndex, | 229 (pathName, ttcIndex, |
| 230 axisValues.get(), axisDefiniti
ons.count(), |
171 style, isFixedWidth, familyNam
e, | 231 style, isFixedWidth, familyNam
e, |
172 lang, variant))); | 232 lang, variant))); |
173 } | 233 } |
174 } | 234 } |
175 | 235 |
176 int count() override { | 236 int count() override { |
177 return fStyles.count(); | 237 return fStyles.count(); |
178 } | 238 } |
179 void getStyle(int index, SkFontStyle* style, SkString* name) override { | 239 void getStyle(int index, SkFontStyle* style, SkString* name) override { |
180 if (index < 0 || fStyles.count() <= index) { | 240 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
{ | 464 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override
{ |
405 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path)); | 465 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path)); |
406 return stream.get() ? this->createFromStream(stream.detach(), ttcIndex)
: NULL; | 466 return stream.get() ? this->createFromStream(stream.detach(), ttcIndex)
: NULL; |
407 } | 467 } |
408 | 468 |
409 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t override { | 469 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t override { |
410 SkAutoTDelete<SkStreamAsset> stream(bareStream); | 470 SkAutoTDelete<SkStreamAsset> stream(bareStream); |
411 bool isFixedPitch; | 471 bool isFixedPitch; |
412 SkFontStyle style; | 472 SkFontStyle style; |
413 SkString name; | 473 SkString name; |
414 if (!fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch))
{ | 474 if (!fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch, N
ULL)) { |
415 return NULL; | 475 return NULL; |
416 } | 476 } |
417 return SkNEW_ARGS(SkTypeface_AndroidStream, (stream.detach(), ttcIndex, | 477 SkFontData* data(new SkFontData(stream.detach(), ttcIndex, NULL, 0)); |
418 style, isFixedPitch, name))
; | 478 return SkNEW_ARGS(SkTypeface_AndroidStream, (data, style, isFixedPitch,
name)); |
| 479 } |
| 480 |
| 481 SkTypeface* onCreateFromFontData(SkFontData* data) const override { |
| 482 SkStreamAsset* stream(data->getStream()); |
| 483 bool isFixedPitch; |
| 484 SkFontStyle style; |
| 485 SkString name; |
| 486 if (!fScanner.scanFont(stream, data->getIndex(), &name, &style, &isFixed
Pitch, NULL)) { |
| 487 return NULL; |
| 488 } |
| 489 return SkNEW_ARGS(SkTypeface_AndroidStream, (data, style, isFixedPitch,
name)); |
419 } | 490 } |
420 | 491 |
421 | 492 |
422 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], | 493 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
423 unsigned styleBits) const overrid
e { | 494 unsigned styleBits) const overrid
e { |
424 SkFontStyle style = SkFontStyle(styleBits); | 495 SkFontStyle style = SkFontStyle(styleBits); |
425 | 496 |
426 if (familyName) { | 497 if (familyName) { |
427 // On Android, we must return NULL when we can't find the requested | 498 // 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 | 499 // named typeface so that the system/app can provide their own recov
ery |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 { | 615 { |
545 gTestFontsXml = fontsXml; | 616 gTestFontsXml = fontsXml; |
546 gTestFallbackFontsXml = fallbackFontsXml; | 617 gTestFallbackFontsXml = fallbackFontsXml; |
547 gTestBasePath = basePath; | 618 gTestBasePath = basePath; |
548 SkASSERT(gTestFontsXml); | 619 SkASSERT(gTestFontsXml); |
549 SkASSERT(gTestFallbackFontsXml); | 620 SkASSERT(gTestFallbackFontsXml); |
550 SkASSERT(gTestBasePath); | 621 SkASSERT(gTestBasePath); |
551 SkDEBUGF(("Test BasePath: %s Fonts: %s FallbackFonts: %s\n", | 622 SkDEBUGF(("Test BasePath: %s Fonts: %s FallbackFonts: %s\n", |
552 gTestBasePath, gTestFontsXml, gTestFallbackFontsXml)); | 623 gTestBasePath, gTestFontsXml, gTestFallbackFontsXml)); |
553 } | 624 } |
OLD | NEW |