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