bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkFontConfigParser_android.h" |
| 9 | #include "SkFontDescriptor.h" |
| 10 | #include "SkFontHost_FreeType_common.h" |
| 11 | #include "SkFontMgr.h" |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 12 | #include "SkFontMgr_android.h" |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 13 | #include "SkFontStyle.h" |
| 14 | #include "SkStream.h" |
| 15 | #include "SkTDArray.h" |
| 16 | #include "SkTSearch.h" |
| 17 | #include "SkTypeface.h" |
bungeman | 4e3523c | 2014-08-08 12:06:51 -0700 | [diff] [blame] | 18 | #include "SkTypeface_android.h" |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 19 | #include "SkTypefaceCache.h" |
| 20 | |
| 21 | #include <limits> |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 22 | |
tomhudson | c773390 | 2014-08-13 10:41:25 -0700 | [diff] [blame] | 23 | // For test only. |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 24 | static const char* gTestFontsXml = NULL; |
| 25 | static const char* gTestFallbackFontsXml = NULL; |
| 26 | static const char* gTestBasePath = NULL; |
tomhudson | c773390 | 2014-08-13 10:41:25 -0700 | [diff] [blame] | 27 | |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 28 | class SkTypeface_Android : public SkTypeface_FreeType { |
| 29 | public: |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 30 | SkTypeface_Android(const SkFontStyle& style, |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 31 | bool isFixedPitch, |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 32 | const SkString& familyName) |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 33 | : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 34 | , fFamilyName(familyName) |
| 35 | { } |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 36 | |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 37 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | void onGetFamilyName(SkString* familyName) const override { |
bungeman | b374d6a | 2014-09-17 07:48:59 -0700 | [diff] [blame] | 39 | *familyName = fFamilyName; |
| 40 | } |
| 41 | |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 42 | SkString fFamilyName; |
| 43 | |
| 44 | private: |
| 45 | typedef SkTypeface_FreeType INHERITED; |
| 46 | }; |
| 47 | |
| 48 | class SkTypeface_AndroidSystem : public SkTypeface_Android { |
| 49 | public: |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 50 | SkTypeface_AndroidSystem(const SkString& pathName, |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 51 | int index, |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 52 | const SkFixed* axes, int axesCount, |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 53 | const SkFontStyle& style, |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 54 | bool isFixedPitch, |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 55 | const SkString& familyName, |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 56 | const SkLanguage& lang, |
djsollen | 3b62554 | 2014-08-14 06:29:02 -0700 | [diff] [blame] | 57 | FontVariant variantStyle) |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 58 | : INHERITED(style, isFixedPitch, familyName) |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 59 | , fPathName(pathName) |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 60 | , fIndex(index) |
| 61 | , fAxes(axes, axesCount) |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 62 | , fLang(lang) |
| 63 | , fVariantStyle(variantStyle) { } |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 64 | |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 65 | virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 66 | SkASSERT(desc); |
| 67 | SkASSERT(serialize); |
| 68 | desc->setFamilyName(fFamilyName.c_str()); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 69 | *serialize = false; |
| 70 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 71 | SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 72 | *ttcIndex = fIndex; |
| 73 | return SkStream::NewFromFile(fPathName.c_str()); |
| 74 | } |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 75 | SkFontData* onCreateFontData() const override { |
| 76 | return new SkFontData(SkStream::NewFromFile(fPathName.c_str()), fIndex, |
| 77 | fAxes.begin(), fAxes.count()); |
| 78 | } |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 79 | |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 80 | const SkString fPathName; |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 81 | int fIndex; |
| 82 | const SkSTArray<4, SkFixed, true> fAxes; |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 83 | const SkLanguage fLang; |
djsollen | 3b62554 | 2014-08-14 06:29:02 -0700 | [diff] [blame] | 84 | const FontVariant fVariantStyle; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 85 | |
| 86 | typedef SkTypeface_Android INHERITED; |
| 87 | }; |
| 88 | |
| 89 | class SkTypeface_AndroidStream : public SkTypeface_Android { |
| 90 | public: |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 91 | SkTypeface_AndroidStream(SkFontData* data, |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 92 | const SkFontStyle& style, |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 93 | bool isFixedPitch, |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 94 | const SkString& familyName) |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 95 | : INHERITED(style, isFixedPitch, familyName) |
| 96 | , fData(data) |
| 97 | { } |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 98 | |
| 99 | virtual void onGetFontDescriptor(SkFontDescriptor* desc, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 100 | bool* serialize) const override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 101 | SkASSERT(desc); |
| 102 | SkASSERT(serialize); |
| 103 | desc->setFamilyName(fFamilyName.c_str()); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 104 | *serialize = true; |
| 105 | } |
| 106 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 107 | SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 108 | *ttcIndex = fData->getIndex(); |
| 109 | return fData->duplicateStream(); |
| 110 | } |
| 111 | |
| 112 | SkFontData* onCreateFontData() const override { |
| 113 | return new SkFontData(*fData.get()); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | private: |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 117 | const SkAutoTDelete<const SkFontData> fData; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 118 | typedef SkTypeface_Android INHERITED; |
| 119 | }; |
| 120 | |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 121 | class SkFontStyleSet_Android : public SkFontStyleSet { |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 122 | typedef SkTypeface_FreeType::Scanner Scanner; |
| 123 | |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 124 | public: |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 125 | explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& scanner) { |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 126 | const SkString* cannonicalFamilyName = NULL; |
| 127 | if (family.fNames.count() > 0) { |
| 128 | cannonicalFamilyName = &family.fNames[0]; |
| 129 | } |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 130 | // TODO? make this lazy |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 131 | for (int i = 0; i < family.fFonts.count(); ++i) { |
| 132 | const FontFileInfo& fontFile = family.fFonts[i]; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 133 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 134 | SkString pathName(family.fBasePath); |
| 135 | pathName.append(fontFile.fFileName); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 136 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 137 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(pathName.c_str())); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 138 | if (!stream.get()) { |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 139 | SkDEBUGF(("Requested font file %s does not exist or cannot be opened.\n", |
| 140 | pathName.c_str())); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 141 | continue; |
| 142 | } |
| 143 | |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 144 | const int ttcIndex = fontFile.fIndex; |
| 145 | SkString familyName; |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 146 | SkFontStyle style; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 147 | bool isFixedWidth; |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 148 | Scanner::AxisDefinitions axisDefinitions; |
| 149 | if (!scanner.scanFont(stream.get(), ttcIndex, |
| 150 | &familyName, &style, &isFixedWidth, &axisDefinitions)) |
| 151 | { |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 152 | SkDEBUGF(("Requested font file %s exists, but is not a valid font.\n", |
| 153 | pathName.c_str())); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 154 | continue; |
| 155 | } |
| 156 | |
bungeman | e85a754 | 2015-04-17 13:51:08 -0700 | [diff] [blame] | 157 | int weight = fontFile.fWeight != 0 ? fontFile.fWeight : style.weight(); |
| 158 | SkFontStyle::Slant slant = style.slant(); |
| 159 | switch (fontFile.fStyle) { |
| 160 | case FontFileInfo::Style::kAuto: slant = style.slant(); break; |
| 161 | case FontFileInfo::Style::kNormal: slant = SkFontStyle::kUpright_Slant; break; |
| 162 | case FontFileInfo::Style::kItalic: slant = SkFontStyle::kItalic_Slant; break; |
| 163 | default: SkASSERT(false); break; |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 164 | } |
bungeman | e85a754 | 2015-04-17 13:51:08 -0700 | [diff] [blame] | 165 | style = SkFontStyle(weight, style.width(), slant); |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 166 | |
djsollen | 3b62554 | 2014-08-14 06:29:02 -0700 | [diff] [blame] | 167 | const SkLanguage& lang = family.fLanguage; |
| 168 | uint32_t variant = family.fVariant; |
| 169 | if (kDefault_FontVariant == variant) { |
| 170 | variant = kCompact_FontVariant | kElegant_FontVariant; |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // The first specified family name overrides the family name found in the font. |
| 174 | // TODO: SkTypeface_AndroidSystem::onCreateFamilyNameIterator should return |
| 175 | // all of the specified family names in addition to the names found in the font. |
| 176 | if (cannonicalFamilyName != NULL) { |
| 177 | familyName = *cannonicalFamilyName; |
| 178 | } |
| 179 | |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 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, axisDefinition.fMinimum, |
| 188 | axisDefinition.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 | |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 228 | fStyles.push_back().reset(SkNEW_ARGS(SkTypeface_AndroidSystem, |
bungeman | 07cfb20 | 2014-07-30 11:05:22 -0700 | [diff] [blame] | 229 | (pathName, ttcIndex, |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 230 | axisValues.get(), axisDefinitions.count(), |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 231 | style, isFixedWidth, familyName, |
| 232 | lang, variant))); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 236 | int count() override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 237 | return fStyles.count(); |
| 238 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 239 | void getStyle(int index, SkFontStyle* style, SkString* name) override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 240 | if (index < 0 || fStyles.count() <= index) { |
| 241 | return; |
| 242 | } |
| 243 | if (style) { |
| 244 | *style = this->style(index); |
| 245 | } |
| 246 | if (name) { |
| 247 | name->reset(); |
| 248 | } |
| 249 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 250 | SkTypeface_AndroidSystem* createTypeface(int index) override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 251 | if (index < 0 || fStyles.count() <= index) { |
| 252 | return NULL; |
| 253 | } |
| 254 | return SkRef(fStyles[index].get()); |
| 255 | } |
| 256 | |
| 257 | /** Find the typeface in this style set that most closely matches the given pattern. |
| 258 | * TODO: consider replacing with SkStyleSet_Indirect::matchStyle(); |
| 259 | * this simpler version using match_score() passes all our tests. |
| 260 | */ |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 261 | SkTypeface_AndroidSystem* matchStyle(const SkFontStyle& pattern) override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 262 | if (0 == fStyles.count()) { |
| 263 | return NULL; |
| 264 | } |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 265 | SkTypeface_AndroidSystem* closest = fStyles[0]; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 266 | int minScore = std::numeric_limits<int>::max(); |
| 267 | for (int i = 0; i < fStyles.count(); ++i) { |
| 268 | SkFontStyle style = this->style(i); |
| 269 | int score = match_score(pattern, style); |
| 270 | if (score < minScore) { |
| 271 | closest = fStyles[i]; |
| 272 | minScore = score; |
| 273 | } |
| 274 | } |
| 275 | return SkRef(closest); |
| 276 | } |
| 277 | |
| 278 | private: |
| 279 | SkFontStyle style(int index) { |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 280 | return fStyles[index]->fontStyle(); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 281 | } |
| 282 | static int match_score(const SkFontStyle& pattern, const SkFontStyle& candidate) { |
| 283 | int score = 0; |
| 284 | score += abs((pattern.width() - candidate.width()) * 100); |
| 285 | score += abs((pattern.isItalic() == candidate.isItalic()) ? 0 : 1000); |
| 286 | score += abs(pattern.weight() - candidate.weight()); |
| 287 | return score; |
| 288 | } |
| 289 | |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 290 | SkTArray<SkAutoTUnref<SkTypeface_AndroidSystem>, true> fStyles; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 291 | |
| 292 | friend struct NameToFamily; |
| 293 | friend class SkFontMgr_Android; |
| 294 | |
| 295 | typedef SkFontStyleSet INHERITED; |
| 296 | }; |
| 297 | |
| 298 | /** On Android a single family can have many names, but our API assumes unique names. |
| 299 | * Map names to the back end so that all names for a given family refer to the same |
| 300 | * (non-replicated) set of typefaces. |
| 301 | * SkTDict<> doesn't let us do index-based lookup, so we write our own mapping. |
| 302 | */ |
| 303 | struct NameToFamily { |
| 304 | SkString name; |
| 305 | SkFontStyleSet_Android* styleSet; |
| 306 | }; |
| 307 | |
| 308 | class SkFontMgr_Android : public SkFontMgr { |
| 309 | public: |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 310 | SkFontMgr_Android(const SkFontMgr_Android_CustomFonts* custom) { |
| 311 | SkTDArray<FontFamily*> families; |
| 312 | if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem != custom->fSystemFontUse) { |
| 313 | SkString base(custom->fBasePath); |
| 314 | SkFontConfigParser::GetCustomFontFamilies(families, base, |
| 315 | custom->fFontsXml, custom->fFallbackFontsXml); |
| 316 | } |
| 317 | if (!custom || |
| 318 | (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSystemFontUse)) |
| 319 | { |
| 320 | SkFontConfigParser::GetSystemFontFamilies(families); |
| 321 | } |
| 322 | if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem == custom->fSystemFontUse) { |
| 323 | SkString base(custom->fBasePath); |
| 324 | SkFontConfigParser::GetCustomFontFamilies(families, base, |
| 325 | custom->fFontsXml, custom->fFallbackFontsXml); |
| 326 | } |
| 327 | this->buildNameToFamilyMap(families); |
bungeman | 4e3523c | 2014-08-08 12:06:51 -0700 | [diff] [blame] | 328 | this->findDefaultFont(); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 329 | families.deleteAll(); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | protected: |
| 333 | /** Returns not how many families we have, but how many unique names |
| 334 | * exist among the families. |
| 335 | */ |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 336 | int onCountFamilies() const override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 337 | return fNameToFamilyMap.count(); |
| 338 | } |
| 339 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 340 | void onGetFamilyName(int index, SkString* familyName) const override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 341 | if (index < 0 || fNameToFamilyMap.count() <= index) { |
| 342 | familyName->reset(); |
| 343 | return; |
| 344 | } |
| 345 | familyName->set(fNameToFamilyMap[index].name); |
| 346 | } |
| 347 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 348 | SkFontStyleSet* onCreateStyleSet(int index) const override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 349 | if (index < 0 || fNameToFamilyMap.count() <= index) { |
| 350 | return NULL; |
| 351 | } |
| 352 | return SkRef(fNameToFamilyMap[index].styleSet); |
| 353 | } |
| 354 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 355 | SkFontStyleSet* onMatchFamily(const char familyName[]) const override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 356 | if (!familyName) { |
| 357 | return NULL; |
| 358 | } |
| 359 | SkAutoAsciiToLC tolc(familyName); |
| 360 | for (int i = 0; i < fNameToFamilyMap.count(); ++i) { |
| 361 | if (fNameToFamilyMap[i].name.equals(tolc.lc())) { |
| 362 | return SkRef(fNameToFamilyMap[i].styleSet); |
| 363 | } |
| 364 | } |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 365 | // TODO: eventually we should not need to name fallback families. |
| 366 | for (int i = 0; i < fFallbackNameToFamilyMap.count(); ++i) { |
| 367 | if (fFallbackNameToFamilyMap[i].name.equals(tolc.lc())) { |
| 368 | return SkRef(fFallbackNameToFamilyMap[i].styleSet); |
| 369 | } |
| 370 | } |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | virtual SkTypeface* onMatchFamilyStyle(const char familyName[], |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 375 | const SkFontStyle& style) const override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 376 | SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName)); |
| 377 | return sset->matchStyle(style); |
| 378 | } |
| 379 | |
| 380 | virtual SkTypeface* onMatchFaceStyle(const SkTypeface* typeface, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 381 | const SkFontStyle& style) const override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 382 | for (int i = 0; i < fFontStyleSets.count(); ++i) { |
| 383 | for (int j = 0; j < fFontStyleSets[i]->fStyles.count(); ++j) { |
| 384 | if (fFontStyleSets[i]->fStyles[j] == typeface) { |
| 385 | return fFontStyleSets[i]->matchStyle(style); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | return NULL; |
| 390 | } |
| 391 | |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 392 | static SkTypeface_AndroidSystem* find_family_style_character( |
| 393 | const SkTDArray<NameToFamily>& fallbackNameToFamilyMap, |
| 394 | const SkFontStyle& style, bool elegant, |
| 395 | const SkString& langTag, SkUnichar character) |
| 396 | { |
| 397 | for (int i = 0; i < fallbackNameToFamilyMap.count(); ++i) { |
| 398 | SkFontStyleSet_Android* family = fallbackNameToFamilyMap[i].styleSet; |
| 399 | SkAutoTUnref<SkTypeface_AndroidSystem> face(family->matchStyle(style)); |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 400 | |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 401 | if (!langTag.isEmpty() && !face->fLang.getTag().startsWith(langTag.c_str())) { |
| 402 | continue; |
| 403 | } |
| 404 | |
| 405 | if (SkToBool(face->fVariantStyle & kElegant_FontVariant) != elegant) { |
| 406 | continue; |
| 407 | } |
| 408 | |
| 409 | SkPaint paint; |
| 410 | paint.setTypeface(face); |
| 411 | paint.setTextEncoding(SkPaint::kUTF32_TextEncoding); |
| 412 | |
| 413 | uint16_t glyphID; |
| 414 | paint.textToGlyphs(&character, sizeof(character), &glyphID); |
| 415 | if (glyphID != 0) { |
| 416 | return face.detach(); |
| 417 | } |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 418 | } |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 419 | return NULL; |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 420 | } |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 421 | |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 422 | virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], |
| 423 | const SkFontStyle& style, |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 424 | const char* bcp47[], |
| 425 | int bcp47Count, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 426 | SkUnichar character) const override |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 427 | { |
| 428 | // The variant 'elegant' is 'not squashed', 'compact' is 'stays in ascent/descent'. |
| 429 | // The variant 'default' means 'compact and elegant'. |
| 430 | // As a result, it is not possible to know the variant context from the font alone. |
| 431 | // TODO: add 'is_elegant' and 'is_compact' bits to 'style' request. |
| 432 | |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 433 | // The first time match anything elegant, second time anything not elegant. |
| 434 | for (int elegant = 2; elegant --> 0;) { |
| 435 | for (int bcp47Index = bcp47Count; bcp47Index --> 0;) { |
| 436 | SkLanguage lang(bcp47[bcp47Index]); |
| 437 | while (!lang.getTag().isEmpty()) { |
| 438 | SkTypeface_AndroidSystem* matchingTypeface = |
| 439 | find_family_style_character(fFallbackNameToFamilyMap, |
| 440 | style, SkToBool(elegant), |
| 441 | lang.getTag(), character); |
| 442 | if (matchingTypeface) { |
| 443 | return matchingTypeface; |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 444 | } |
| 445 | |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 446 | lang = lang.getParent(); |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 447 | } |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 448 | } |
| 449 | SkTypeface_AndroidSystem* matchingTypeface = |
| 450 | find_family_style_character(fFallbackNameToFamilyMap, |
| 451 | style, SkToBool(elegant), |
| 452 | SkString(), character); |
| 453 | if (matchingTypeface) { |
| 454 | return matchingTypeface; |
| 455 | } |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 456 | } |
| 457 | return NULL; |
| 458 | } |
| 459 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 460 | SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 461 | return this->createFromStream(new SkMemoryStream(data), ttcIndex); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 462 | } |
| 463 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 464 | SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override { |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 465 | SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path)); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 466 | return stream.get() ? this->createFromStream(stream.detach(), ttcIndex) : NULL; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 467 | } |
| 468 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 469 | SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override { |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 470 | SkAutoTDelete<SkStreamAsset> stream(bareStream); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 471 | bool isFixedPitch; |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 472 | SkFontStyle style; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 473 | SkString name; |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 474 | if (!fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch, NULL)) { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 475 | return NULL; |
| 476 | } |
bungeman | 05773ed | 2015-05-13 10:57:09 -0700 | [diff] [blame^] | 477 | SkFontData* data(new SkFontData(stream.detach(), ttcIndex, NULL, 0)); |
| 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, &isFixedPitch, NULL)) { |
| 487 | return NULL; |
| 488 | } |
| 489 | return SkNEW_ARGS(SkTypeface_AndroidStream, (data, style, isFixedPitch, name)); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | |
| 493 | virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 494 | unsigned styleBits) const override { |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 495 | SkFontStyle style = SkFontStyle(styleBits); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 496 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 497 | if (familyName) { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 498 | // 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 recovery |
| 500 | // mechanism. On other platforms we'd provide a typeface from the |
| 501 | // default family instead. |
bungeman | 07cfb20 | 2014-07-30 11:05:22 -0700 | [diff] [blame] | 502 | return this->onMatchFamilyStyle(familyName, style); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 503 | } |
bungeman | 07cfb20 | 2014-07-30 11:05:22 -0700 | [diff] [blame] | 504 | return fDefaultFamily->matchStyle(style); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | |
| 508 | private: |
| 509 | |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 510 | SkTypeface_FreeType::Scanner fScanner; |
| 511 | |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 512 | SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets; |
| 513 | SkFontStyleSet* fDefaultFamily; |
| 514 | SkTypeface* fDefaultTypeface; |
| 515 | |
| 516 | SkTDArray<NameToFamily> fNameToFamilyMap; |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 517 | SkTDArray<NameToFamily> fFallbackNameToFamilyMap; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 518 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 519 | void buildNameToFamilyMap(SkTDArray<FontFamily*> families) { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 520 | for (int i = 0; i < families.count(); i++) { |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 521 | FontFamily& family = *families[i]; |
| 522 | |
| 523 | SkTDArray<NameToFamily>* nameToFamily = &fNameToFamilyMap; |
| 524 | if (family.fIsFallbackFont) { |
| 525 | nameToFamily = &fFallbackNameToFamilyMap; |
| 526 | |
| 527 | if (0 == family.fNames.count()) { |
| 528 | SkString& fallbackName = family.fNames.push_back(); |
| 529 | fallbackName.printf("%.2x##fallback", i); |
| 530 | } |
| 531 | } |
| 532 | |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 533 | SkFontStyleSet_Android* newSet = |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 534 | SkNEW_ARGS(SkFontStyleSet_Android, (family, fScanner)); |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 535 | if (0 == newSet->count()) { |
| 536 | SkDELETE(newSet); |
| 537 | continue; |
| 538 | } |
| 539 | fFontStyleSets.push_back().reset(newSet); |
| 540 | |
| 541 | for (int j = 0; j < family.fNames.count(); j++) { |
| 542 | NameToFamily* nextEntry = nameToFamily->append(); |
| 543 | SkNEW_PLACEMENT_ARGS(&nextEntry->name, SkString, (family.fNames[j])); |
| 544 | nextEntry->styleSet = newSet; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | void findDefaultFont() { |
| 550 | SkASSERT(!fFontStyleSets.empty()); |
| 551 | |
| 552 | static const char* gDefaultNames[] = { "sans-serif" }; |
| 553 | for (size_t i = 0; i < SK_ARRAY_COUNT(gDefaultNames); ++i) { |
| 554 | SkFontStyleSet* set = this->onMatchFamily(gDefaultNames[i]); |
| 555 | if (NULL == set) { |
| 556 | continue; |
| 557 | } |
| 558 | SkTypeface* tf = set->matchStyle(SkFontStyle()); |
| 559 | if (NULL == tf) { |
| 560 | continue; |
| 561 | } |
| 562 | fDefaultFamily = set; |
| 563 | fDefaultTypeface = tf; |
| 564 | break; |
| 565 | } |
| 566 | if (NULL == fDefaultTypeface) { |
| 567 | fDefaultFamily = fFontStyleSets[0]; |
| 568 | fDefaultTypeface = fDefaultFamily->createTypeface(0); |
| 569 | } |
| 570 | SkASSERT(fDefaultFamily); |
| 571 | SkASSERT(fDefaultTypeface); |
| 572 | } |
| 573 | |
| 574 | typedef SkFontMgr INHERITED; |
| 575 | }; |
| 576 | |
| 577 | /////////////////////////////////////////////////////////////////////////////// |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 578 | #ifdef SK_DEBUG |
| 579 | static char const * const gSystemFontUseStrings[] = { |
| 580 | "OnlyCustom", "PreferCustom", "PreferSystem" |
| 581 | }; |
| 582 | #endif |
| 583 | SkFontMgr* SkFontMgr_New_Android(const SkFontMgr_Android_CustomFonts* custom) { |
| 584 | if (custom) { |
| 585 | SkASSERT(0 <= custom->fSystemFontUse); |
| 586 | SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings)); |
| 587 | SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n", |
| 588 | gSystemFontUseStrings[custom->fSystemFontUse], |
| 589 | custom->fBasePath, |
| 590 | custom->fFontsXml, |
| 591 | custom->fFallbackFontsXml)); |
bungeman | 4e3523c | 2014-08-08 12:06:51 -0700 | [diff] [blame] | 592 | } |
| 593 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 594 | return SkNEW_ARGS(SkFontMgr_Android, (custom)); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 595 | } |
tomhudson | c773390 | 2014-08-13 10:41:25 -0700 | [diff] [blame] | 596 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 597 | SkFontMgr* SkFontMgr::Factory() { |
| 598 | // These globals exist so that Chromium can override the environment. |
| 599 | // TODO: these globals need to be removed, and Chromium use SkFontMgr_New_Android instead. |
| 600 | if ((gTestFontsXml || gTestFallbackFontsXml) && gTestBasePath) { |
| 601 | SkFontMgr_Android_CustomFonts custom = { |
| 602 | SkFontMgr_Android_CustomFonts::kOnlyCustom, |
| 603 | gTestBasePath, |
| 604 | gTestFontsXml, |
| 605 | gTestFallbackFontsXml |
| 606 | }; |
| 607 | return SkFontMgr_New_Android(&custom); |
| 608 | } |
| 609 | |
| 610 | return SkFontMgr_New_Android(NULL); |
tomhudson | c773390 | 2014-08-13 10:41:25 -0700 | [diff] [blame] | 611 | } |
| 612 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 613 | void SkUseTestFontConfigFile(const char* fontsXml, const char* fallbackFontsXml, |
| 614 | const char* basePath) |
| 615 | { |
| 616 | gTestFontsXml = fontsXml; |
| 617 | gTestFallbackFontsXml = fallbackFontsXml; |
| 618 | gTestBasePath = basePath; |
| 619 | SkASSERT(gTestFontsXml); |
| 620 | SkASSERT(gTestFallbackFontsXml); |
| 621 | SkASSERT(gTestBasePath); |
| 622 | SkDEBUGF(("Test BasePath: %s Fonts: %s FallbackFonts: %s\n", |
| 623 | gTestBasePath, gTestFontsXml, gTestFallbackFontsXml)); |
tomhudson | c773390 | 2014-08-13 10:41:25 -0700 | [diff] [blame] | 624 | } |