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 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 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 | } |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 217 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 218 | if (!found) { |
| 219 | SkDEBUGF(("Requested font axis not found: %s '%c%c%c%c'\n", |
| 220 | familyName.c_str(), (skTag >> 24) & 0xFF, |
| 221 | (skTag >> 16) & 0xFF, (skTag >> 8) & 0xFF, (skTag)&0xFF)); |
| 222 | } |
| 223 | }) |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 224 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 225 | fStyles.push_back().reset(new SkTypeface_AndroidSystem( |
| 226 | pathName, ttcIndex, axisValues.get(), axisDefinitions.count(), style, |
| 227 | isFixedWidth, familyName, lang, variant)); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 231 | int count() override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 232 | return fStyles.count(); |
| 233 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 234 | void getStyle(int index, SkFontStyle* style, SkString* name) override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 235 | if (index < 0 || fStyles.count() <= index) { |
| 236 | return; |
| 237 | } |
| 238 | if (style) { |
| 239 | *style = this->style(index); |
| 240 | } |
| 241 | if (name) { |
| 242 | name->reset(); |
| 243 | } |
| 244 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 245 | SkTypeface_AndroidSystem* createTypeface(int index) override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 246 | if (index < 0 || fStyles.count() <= index) { |
| 247 | return NULL; |
| 248 | } |
| 249 | return SkRef(fStyles[index].get()); |
| 250 | } |
| 251 | |
| 252 | /** Find the typeface in this style set that most closely matches the given pattern. |
| 253 | * TODO: consider replacing with SkStyleSet_Indirect::matchStyle(); |
| 254 | * this simpler version using match_score() passes all our tests. |
| 255 | */ |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 256 | SkTypeface_AndroidSystem* matchStyle(const SkFontStyle& pattern) override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 257 | if (0 == fStyles.count()) { |
| 258 | return NULL; |
| 259 | } |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 260 | SkTypeface_AndroidSystem* closest = fStyles[0]; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 261 | int minScore = std::numeric_limits<int>::max(); |
| 262 | for (int i = 0; i < fStyles.count(); ++i) { |
| 263 | SkFontStyle style = this->style(i); |
| 264 | int score = match_score(pattern, style); |
| 265 | if (score < minScore) { |
| 266 | closest = fStyles[i]; |
| 267 | minScore = score; |
| 268 | } |
| 269 | } |
| 270 | return SkRef(closest); |
| 271 | } |
| 272 | |
| 273 | private: |
| 274 | SkFontStyle style(int index) { |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 275 | return fStyles[index]->fontStyle(); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 276 | } |
| 277 | static int match_score(const SkFontStyle& pattern, const SkFontStyle& candidate) { |
| 278 | int score = 0; |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 279 | score += SkTAbs((pattern.width() - candidate.width()) * 100); |
| 280 | score += SkTAbs((pattern.isItalic() == candidate.isItalic()) ? 0 : 1000); |
| 281 | score += SkTAbs(pattern.weight() - candidate.weight()); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 282 | return score; |
| 283 | } |
| 284 | |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 285 | SkTArray<SkAutoTUnref<SkTypeface_AndroidSystem>, true> fStyles; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 286 | |
| 287 | friend struct NameToFamily; |
| 288 | friend class SkFontMgr_Android; |
| 289 | |
| 290 | typedef SkFontStyleSet INHERITED; |
| 291 | }; |
| 292 | |
| 293 | /** On Android a single family can have many names, but our API assumes unique names. |
| 294 | * Map names to the back end so that all names for a given family refer to the same |
| 295 | * (non-replicated) set of typefaces. |
| 296 | * SkTDict<> doesn't let us do index-based lookup, so we write our own mapping. |
| 297 | */ |
| 298 | struct NameToFamily { |
| 299 | SkString name; |
| 300 | SkFontStyleSet_Android* styleSet; |
| 301 | }; |
| 302 | |
| 303 | class SkFontMgr_Android : public SkFontMgr { |
| 304 | public: |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 305 | SkFontMgr_Android(const SkFontMgr_Android_CustomFonts* custom) { |
| 306 | SkTDArray<FontFamily*> families; |
| 307 | if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem != custom->fSystemFontUse) { |
| 308 | SkString base(custom->fBasePath); |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 309 | SkFontMgr_Android_Parser::GetCustomFontFamilies( |
| 310 | families, base, custom->fFontsXml, custom->fFallbackFontsXml); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 311 | } |
| 312 | if (!custom || |
| 313 | (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSystemFontUse)) |
| 314 | { |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 315 | SkFontMgr_Android_Parser::GetSystemFontFamilies(families); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 316 | } |
| 317 | if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem == custom->fSystemFontUse) { |
| 318 | SkString base(custom->fBasePath); |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 319 | SkFontMgr_Android_Parser::GetCustomFontFamilies( |
| 320 | families, base, custom->fFontsXml, custom->fFallbackFontsXml); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 321 | } |
| 322 | this->buildNameToFamilyMap(families); |
bungeman | 4e3523c | 2014-08-08 12:06:51 -0700 | [diff] [blame] | 323 | this->findDefaultFont(); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 324 | families.deleteAll(); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | protected: |
| 328 | /** Returns not how many families we have, but how many unique names |
| 329 | * exist among the families. |
| 330 | */ |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 331 | int onCountFamilies() const override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 332 | return fNameToFamilyMap.count(); |
| 333 | } |
| 334 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 335 | void onGetFamilyName(int index, SkString* familyName) const override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 336 | if (index < 0 || fNameToFamilyMap.count() <= index) { |
| 337 | familyName->reset(); |
| 338 | return; |
| 339 | } |
| 340 | familyName->set(fNameToFamilyMap[index].name); |
| 341 | } |
| 342 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 343 | SkFontStyleSet* onCreateStyleSet(int index) const override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 344 | if (index < 0 || fNameToFamilyMap.count() <= index) { |
| 345 | return NULL; |
| 346 | } |
| 347 | return SkRef(fNameToFamilyMap[index].styleSet); |
| 348 | } |
| 349 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 350 | SkFontStyleSet* onMatchFamily(const char familyName[]) const override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 351 | if (!familyName) { |
| 352 | return NULL; |
| 353 | } |
| 354 | SkAutoAsciiToLC tolc(familyName); |
| 355 | for (int i = 0; i < fNameToFamilyMap.count(); ++i) { |
| 356 | if (fNameToFamilyMap[i].name.equals(tolc.lc())) { |
| 357 | return SkRef(fNameToFamilyMap[i].styleSet); |
| 358 | } |
| 359 | } |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 360 | // TODO: eventually we should not need to name fallback families. |
| 361 | for (int i = 0; i < fFallbackNameToFamilyMap.count(); ++i) { |
| 362 | if (fFallbackNameToFamilyMap[i].name.equals(tolc.lc())) { |
| 363 | return SkRef(fFallbackNameToFamilyMap[i].styleSet); |
| 364 | } |
| 365 | } |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 366 | return NULL; |
| 367 | } |
| 368 | |
| 369 | virtual SkTypeface* onMatchFamilyStyle(const char familyName[], |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 370 | const SkFontStyle& style) const override { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 371 | SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName)); |
| 372 | return sset->matchStyle(style); |
| 373 | } |
| 374 | |
| 375 | virtual SkTypeface* onMatchFaceStyle(const SkTypeface* typeface, |
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 | for (int i = 0; i < fFontStyleSets.count(); ++i) { |
| 378 | for (int j = 0; j < fFontStyleSets[i]->fStyles.count(); ++j) { |
| 379 | if (fFontStyleSets[i]->fStyles[j] == typeface) { |
| 380 | return fFontStyleSets[i]->matchStyle(style); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | return NULL; |
| 385 | } |
| 386 | |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 387 | static SkTypeface_AndroidSystem* find_family_style_character( |
| 388 | const SkTDArray<NameToFamily>& fallbackNameToFamilyMap, |
| 389 | const SkFontStyle& style, bool elegant, |
| 390 | const SkString& langTag, SkUnichar character) |
| 391 | { |
| 392 | for (int i = 0; i < fallbackNameToFamilyMap.count(); ++i) { |
| 393 | SkFontStyleSet_Android* family = fallbackNameToFamilyMap[i].styleSet; |
| 394 | SkAutoTUnref<SkTypeface_AndroidSystem> face(family->matchStyle(style)); |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 395 | |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 396 | if (!langTag.isEmpty() && !face->fLang.getTag().startsWith(langTag.c_str())) { |
| 397 | continue; |
| 398 | } |
| 399 | |
| 400 | if (SkToBool(face->fVariantStyle & kElegant_FontVariant) != elegant) { |
| 401 | continue; |
| 402 | } |
| 403 | |
bungeman | 24a104d | 2015-07-30 10:31:57 -0700 | [diff] [blame] | 404 | SkPaint paint; |
| 405 | paint.setTypeface(face); |
| 406 | paint.setTextEncoding(SkPaint::kUTF32_TextEncoding); |
| 407 | |
| 408 | uint16_t glyphID; |
| 409 | paint.textToGlyphs(&character, sizeof(character), &glyphID); |
| 410 | if (glyphID != 0) { |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 411 | return face.detach(); |
| 412 | } |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 413 | } |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 414 | return NULL; |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 415 | } |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 416 | |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 417 | virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], |
| 418 | const SkFontStyle& style, |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 419 | const char* bcp47[], |
| 420 | int bcp47Count, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 421 | SkUnichar character) const override |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 422 | { |
| 423 | // The variant 'elegant' is 'not squashed', 'compact' is 'stays in ascent/descent'. |
| 424 | // The variant 'default' means 'compact and elegant'. |
| 425 | // As a result, it is not possible to know the variant context from the font alone. |
| 426 | // TODO: add 'is_elegant' and 'is_compact' bits to 'style' request. |
| 427 | |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 428 | // The first time match anything elegant, second time anything not elegant. |
| 429 | for (int elegant = 2; elegant --> 0;) { |
| 430 | for (int bcp47Index = bcp47Count; bcp47Index --> 0;) { |
| 431 | SkLanguage lang(bcp47[bcp47Index]); |
| 432 | while (!lang.getTag().isEmpty()) { |
| 433 | SkTypeface_AndroidSystem* matchingTypeface = |
| 434 | find_family_style_character(fFallbackNameToFamilyMap, |
| 435 | style, SkToBool(elegant), |
| 436 | lang.getTag(), character); |
| 437 | if (matchingTypeface) { |
| 438 | return matchingTypeface; |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 439 | } |
| 440 | |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 441 | lang = lang.getParent(); |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 442 | } |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 443 | } |
| 444 | SkTypeface_AndroidSystem* matchingTypeface = |
| 445 | find_family_style_character(fFallbackNameToFamilyMap, |
| 446 | style, SkToBool(elegant), |
| 447 | SkString(), character); |
| 448 | if (matchingTypeface) { |
| 449 | return matchingTypeface; |
| 450 | } |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 451 | } |
| 452 | return NULL; |
| 453 | } |
| 454 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 455 | SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 456 | return this->createFromStream(new SkMemoryStream(data), ttcIndex); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 457 | } |
| 458 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 459 | SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override { |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 460 | SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path)); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 461 | return stream.get() ? this->createFromStream(stream.detach(), ttcIndex) : NULL; |
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* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override { |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 465 | SkAutoTDelete<SkStreamAsset> stream(bareStream); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 466 | bool isFixedPitch; |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 467 | SkFontStyle style; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 468 | SkString name; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 469 | if (!fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch, NULL)) { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 470 | return NULL; |
| 471 | } |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 472 | SkFontData* data(new SkFontData(stream.detach(), ttcIndex, NULL, 0)); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 473 | return new SkTypeface_AndroidStream(data, style, isFixedPitch, name); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | SkTypeface* onCreateFromFontData(SkFontData* data) const override { |
| 477 | SkStreamAsset* stream(data->getStream()); |
| 478 | bool isFixedPitch; |
| 479 | SkFontStyle style; |
| 480 | SkString name; |
| 481 | if (!fScanner.scanFont(stream, data->getIndex(), &name, &style, &isFixedPitch, NULL)) { |
| 482 | return NULL; |
| 483 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 484 | return new SkTypeface_AndroidStream(data, style, isFixedPitch, name); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | |
| 488 | virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 489 | unsigned styleBits) const override { |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 490 | SkFontStyle style = SkFontStyle(styleBits); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 491 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 492 | if (familyName) { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 493 | // On Android, we must return NULL when we can't find the requested |
| 494 | // named typeface so that the system/app can provide their own recovery |
| 495 | // mechanism. On other platforms we'd provide a typeface from the |
| 496 | // default family instead. |
bungeman | 07cfb20 | 2014-07-30 11:05:22 -0700 | [diff] [blame] | 497 | return this->onMatchFamilyStyle(familyName, style); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 498 | } |
bungeman | 07cfb20 | 2014-07-30 11:05:22 -0700 | [diff] [blame] | 499 | return fDefaultFamily->matchStyle(style); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | |
| 503 | private: |
| 504 | |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 505 | SkTypeface_FreeType::Scanner fScanner; |
| 506 | |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 507 | SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets; |
| 508 | SkFontStyleSet* fDefaultFamily; |
| 509 | SkTypeface* fDefaultTypeface; |
| 510 | |
| 511 | SkTDArray<NameToFamily> fNameToFamilyMap; |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 512 | SkTDArray<NameToFamily> fFallbackNameToFamilyMap; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 513 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 514 | void buildNameToFamilyMap(SkTDArray<FontFamily*> families) { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 515 | for (int i = 0; i < families.count(); i++) { |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 516 | FontFamily& family = *families[i]; |
| 517 | |
| 518 | SkTDArray<NameToFamily>* nameToFamily = &fNameToFamilyMap; |
| 519 | if (family.fIsFallbackFont) { |
| 520 | nameToFamily = &fFallbackNameToFamilyMap; |
| 521 | |
| 522 | if (0 == family.fNames.count()) { |
| 523 | SkString& fallbackName = family.fNames.push_back(); |
| 524 | fallbackName.printf("%.2x##fallback", i); |
| 525 | } |
| 526 | } |
| 527 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 528 | SkFontStyleSet_Android* newSet = new SkFontStyleSet_Android(family, fScanner); |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 529 | if (0 == newSet->count()) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 530 | delete newSet; |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 531 | continue; |
| 532 | } |
| 533 | fFontStyleSets.push_back().reset(newSet); |
| 534 | |
| 535 | for (int j = 0; j < family.fNames.count(); j++) { |
| 536 | NameToFamily* nextEntry = nameToFamily->append(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 537 | new (&nextEntry->name) SkString(family.fNames[j]); |
bungeman | 65fcd3d | 2014-08-06 11:12:20 -0700 | [diff] [blame] | 538 | nextEntry->styleSet = newSet; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | void findDefaultFont() { |
| 544 | SkASSERT(!fFontStyleSets.empty()); |
| 545 | |
| 546 | static const char* gDefaultNames[] = { "sans-serif" }; |
| 547 | for (size_t i = 0; i < SK_ARRAY_COUNT(gDefaultNames); ++i) { |
| 548 | SkFontStyleSet* set = this->onMatchFamily(gDefaultNames[i]); |
| 549 | if (NULL == set) { |
| 550 | continue; |
| 551 | } |
| 552 | SkTypeface* tf = set->matchStyle(SkFontStyle()); |
| 553 | if (NULL == tf) { |
| 554 | continue; |
| 555 | } |
| 556 | fDefaultFamily = set; |
| 557 | fDefaultTypeface = tf; |
| 558 | break; |
| 559 | } |
| 560 | if (NULL == fDefaultTypeface) { |
| 561 | fDefaultFamily = fFontStyleSets[0]; |
| 562 | fDefaultTypeface = fDefaultFamily->createTypeface(0); |
| 563 | } |
| 564 | SkASSERT(fDefaultFamily); |
| 565 | SkASSERT(fDefaultTypeface); |
| 566 | } |
| 567 | |
| 568 | typedef SkFontMgr INHERITED; |
| 569 | }; |
| 570 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 571 | #ifdef SK_DEBUG |
| 572 | static char const * const gSystemFontUseStrings[] = { |
| 573 | "OnlyCustom", "PreferCustom", "PreferSystem" |
| 574 | }; |
| 575 | #endif |
| 576 | SkFontMgr* SkFontMgr_New_Android(const SkFontMgr_Android_CustomFonts* custom) { |
| 577 | if (custom) { |
| 578 | SkASSERT(0 <= custom->fSystemFontUse); |
| 579 | SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings)); |
| 580 | SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n", |
| 581 | gSystemFontUseStrings[custom->fSystemFontUse], |
| 582 | custom->fBasePath, |
| 583 | custom->fFontsXml, |
| 584 | custom->fFallbackFontsXml)); |
bungeman | 4e3523c | 2014-08-08 12:06:51 -0700 | [diff] [blame] | 585 | } |
| 586 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 587 | return new SkFontMgr_Android(custom); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 588 | } |