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