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