blob: 17aa13d9f8aaff80a83078a2f14bd410ef1b7144 [file] [log] [blame]
bungeman8d84c992014-07-24 08:05:09 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
mtklein1ee76512015-11-02 10:20:27 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkData.h"
11#include "include/core/SkFontMgr.h"
12#include "include/core/SkFontStyle.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkRefCnt.h"
15#include "include/core/SkStream.h"
16#include "include/core/SkString.h"
17#include "include/ports/SkFontMgr_android.h"
18#include "include/private/SkFixed.h"
19#include "include/private/SkTArray.h"
20#include "include/private/SkTDArray.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/private/SkTemplates.h"
22#include "src/core/SkFontDescriptor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/core/SkOSFile.h"
Ben Wagner8bd6e8f2019-05-15 09:28:52 -040024#include "src/core/SkTSearch.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkTypefaceCache.h"
26#include "src/ports/SkFontHost_FreeType_common.h"
27#include "src/ports/SkFontMgr_android_parser.h"
bungeman8d84c992014-07-24 08:05:09 -070028
Ben Wagneraee878d2017-08-10 13:49:41 -040029#include <algorithm>
bungeman8d84c992014-07-24 08:05:09 -070030#include <limits>
bungeman8d84c992014-07-24 08:05:09 -070031
bungemanf20488b2015-07-29 11:49:40 -070032class SkData;
33
bungeman8d84c992014-07-24 08:05:09 -070034class SkTypeface_Android : public SkTypeface_FreeType {
35public:
bungeman41868fe2015-05-20 09:21:04 -070036 SkTypeface_Android(const SkFontStyle& style,
bungeman8d84c992014-07-24 08:05:09 -070037 bool isFixedPitch,
bungeman4b86bac2014-11-04 10:54:31 -080038 const SkString& familyName)
bungemane3aea102016-07-13 05:16:58 -070039 : INHERITED(style, isFixedPitch)
bungeman41868fe2015-05-20 09:21:04 -070040 , fFamilyName(familyName)
41 { }
bungeman8d84c992014-07-24 08:05:09 -070042
bungeman8d84c992014-07-24 08:05:09 -070043protected:
mtklein36352bf2015-03-25 18:17:31 -070044 void onGetFamilyName(SkString* familyName) const override {
bungemanb374d6a2014-09-17 07:48:59 -070045 *familyName = fFamilyName;
46 }
47
bungeman8d84c992014-07-24 08:05:09 -070048 SkString fFamilyName;
49
50private:
John Stiles7571f9e2020-09-02 22:42:33 -040051 using INHERITED = SkTypeface_FreeType;
bungeman8d84c992014-07-24 08:05:09 -070052};
53
54class SkTypeface_AndroidSystem : public SkTypeface_Android {
55public:
bungeman4b86bac2014-11-04 10:54:31 -080056 SkTypeface_AndroidSystem(const SkString& pathName,
khushalsagarebc465b2016-02-12 12:42:48 -080057 const bool cacheFontFiles,
bungeman8d84c992014-07-24 08:05:09 -070058 int index,
bungeman41868fe2015-05-20 09:21:04 -070059 const SkFixed* axes, int axesCount,
bungemana4c4a2d2014-10-20 13:33:19 -070060 const SkFontStyle& style,
bungeman8d84c992014-07-24 08:05:09 -070061 bool isFixedPitch,
bungeman4b86bac2014-11-04 10:54:31 -080062 const SkString& familyName,
Ben Wagneraee878d2017-08-10 13:49:41 -040063 const SkTArray<SkLanguage, true>& lang,
djsollen3b625542014-08-14 06:29:02 -070064 FontVariant variantStyle)
bungeman41868fe2015-05-20 09:21:04 -070065 : INHERITED(style, isFixedPitch, familyName)
bungeman65fcd3d2014-08-06 11:12:20 -070066 , fPathName(pathName)
bungeman41868fe2015-05-20 09:21:04 -070067 , fIndex(index)
68 , fAxes(axes, axesCount)
bungeman65fcd3d2014-08-06 11:12:20 -070069 , fLang(lang)
khushalsagarebc465b2016-02-12 12:42:48 -080070 , fVariantStyle(variantStyle)
71 , fFile(cacheFontFiles ? sk_fopen(fPathName.c_str(), kRead_SkFILE_Flag) : nullptr) {
72 if (cacheFontFiles) {
73 SkASSERT(fFile);
74 }
75 }
76
bungemanf93d7112016-09-16 06:24:20 -070077 std::unique_ptr<SkStreamAsset> makeStream() const {
khushalsagarebc465b2016-02-12 12:42:48 -080078 if (fFile) {
bungeman38d909e2016-08-02 14:40:46 -070079 sk_sp<SkData> data(SkData::MakeFromFILE(fFile));
Mike Kleinf46d5ca2019-12-11 10:45:01 -050080 return data ? std::make_unique<SkMemoryStream>(std::move(data)) : nullptr;
khushalsagarebc465b2016-02-12 12:42:48 -080081 }
bungemanf93d7112016-09-16 06:24:20 -070082 return SkStream::MakeFromFile(fPathName.c_str());
khushalsagarebc465b2016-02-12 12:42:48 -080083 }
bungeman8d84c992014-07-24 08:05:09 -070084
John Stiles1cf2c8d2020-08-13 22:58:04 -040085 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const override {
bungeman8d84c992014-07-24 08:05:09 -070086 SkASSERT(desc);
87 SkASSERT(serialize);
88 desc->setFamilyName(fFamilyName.c_str());
bungemanb8113782016-07-25 16:54:59 -070089 desc->setStyle(this->fontStyle());
bungeman8d84c992014-07-24 08:05:09 -070090 *serialize = false;
91 }
Ben Wagner4212a7d2019-02-25 14:27:46 -050092 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override {
bungeman8d84c992014-07-24 08:05:09 -070093 *ttcIndex = fIndex;
Ben Wagner4212a7d2019-02-25 14:27:46 -050094 return this->makeStream();
bungeman8d84c992014-07-24 08:05:09 -070095 }
bungemanf93d7112016-09-16 06:24:20 -070096 std::unique_ptr<SkFontData> onMakeFontData() const override {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050097 return std::make_unique<SkFontData>(this->makeStream(), fIndex,
bungemanf93d7112016-09-16 06:24:20 -070098 fAxes.begin(), fAxes.count());
bungeman41868fe2015-05-20 09:21:04 -070099 }
Bruce Wang37b61092018-06-20 16:43:02 -0400100 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override {
101 std::unique_ptr<SkFontData> data = this->cloneFontData(args);
102 if (!data) {
103 return nullptr;
104 }
105 return sk_make_sp<SkTypeface_AndroidSystem>(fPathName,
106 fFile,
107 fIndex,
108 data->getAxis(),
109 data->getAxisCount(),
110 this->fontStyle(),
111 this->isFixedPitch(),
112 fFamilyName,
113 fLang,
114 fVariantStyle);
115 }
bungeman8d84c992014-07-24 08:05:09 -0700116
bungeman65fcd3d2014-08-06 11:12:20 -0700117 const SkString fPathName;
bungeman41868fe2015-05-20 09:21:04 -0700118 int fIndex;
119 const SkSTArray<4, SkFixed, true> fAxes;
Ben Wagneraee878d2017-08-10 13:49:41 -0400120 const SkSTArray<4, SkLanguage, true> fLang;
djsollen3b625542014-08-14 06:29:02 -0700121 const FontVariant fVariantStyle;
khushalsagarebc465b2016-02-12 12:42:48 -0800122 SkAutoTCallVProc<FILE, sk_fclose> fFile;
bungeman8d84c992014-07-24 08:05:09 -0700123
John Stiles7571f9e2020-09-02 22:42:33 -0400124 using INHERITED = SkTypeface_Android;
bungeman8d84c992014-07-24 08:05:09 -0700125};
126
127class SkTypeface_AndroidStream : public SkTypeface_Android {
128public:
bungemanf93d7112016-09-16 06:24:20 -0700129 SkTypeface_AndroidStream(std::unique_ptr<SkFontData> data,
bungemana4c4a2d2014-10-20 13:33:19 -0700130 const SkFontStyle& style,
bungeman8d84c992014-07-24 08:05:09 -0700131 bool isFixedPitch,
bungeman4b86bac2014-11-04 10:54:31 -0800132 const SkString& familyName)
bungeman41868fe2015-05-20 09:21:04 -0700133 : INHERITED(style, isFixedPitch, familyName)
bungemanf93d7112016-09-16 06:24:20 -0700134 , fData(std::move(data))
bungeman41868fe2015-05-20 09:21:04 -0700135 { }
bungeman8d84c992014-07-24 08:05:09 -0700136
John Stiles1cf2c8d2020-08-13 22:58:04 -0400137 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const override {
bungeman8d84c992014-07-24 08:05:09 -0700138 SkASSERT(desc);
139 SkASSERT(serialize);
140 desc->setFamilyName(fFamilyName.c_str());
bungeman8d84c992014-07-24 08:05:09 -0700141 *serialize = true;
142 }
143
Ben Wagner4212a7d2019-02-25 14:27:46 -0500144 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override {
bungeman41868fe2015-05-20 09:21:04 -0700145 *ttcIndex = fData->getIndex();
Ben Wagner4212a7d2019-02-25 14:27:46 -0500146 return fData->getStream()->duplicate();
bungeman41868fe2015-05-20 09:21:04 -0700147 }
148
bungemanf93d7112016-09-16 06:24:20 -0700149 std::unique_ptr<SkFontData> onMakeFontData() const override {
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500150 return std::make_unique<SkFontData>(*fData);
bungeman8d84c992014-07-24 08:05:09 -0700151 }
152
Bruce Wangebf0cf52018-06-18 14:04:19 -0400153 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override {
154 std::unique_ptr<SkFontData> data = this->cloneFontData(args);
155 if (!data) {
156 return nullptr;
157 }
158 return sk_make_sp<SkTypeface_AndroidStream>(std::move(data),
159 this->fontStyle(),
160 this->isFixedPitch(),
161 fFamilyName);
162 }
163
bungeman8d84c992014-07-24 08:05:09 -0700164private:
bungemanf93d7112016-09-16 06:24:20 -0700165 const std::unique_ptr<const SkFontData> fData;
John Stiles7571f9e2020-09-02 22:42:33 -0400166 using INHERITED = SkTypeface_Android;
bungeman8d84c992014-07-24 08:05:09 -0700167};
168
bungeman8d84c992014-07-24 08:05:09 -0700169class SkFontStyleSet_Android : public SkFontStyleSet {
bungeman41868fe2015-05-20 09:21:04 -0700170 typedef SkTypeface_FreeType::Scanner Scanner;
171
bungeman8d84c992014-07-24 08:05:09 -0700172public:
khushalsagarebc465b2016-02-12 12:42:48 -0800173 explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& scanner,
174 const bool cacheFontFiles) {
halcanary96fcdcc2015-08-27 07:41:13 -0700175 const SkString* cannonicalFamilyName = nullptr;
bungeman65fcd3d2014-08-06 11:12:20 -0700176 if (family.fNames.count() > 0) {
177 cannonicalFamilyName = &family.fNames[0];
178 }
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500179 fFallbackFor = family.fFallbackFor;
180
bungeman8d84c992014-07-24 08:05:09 -0700181 // TODO? make this lazy
tomhudsond3ddea22014-08-11 11:28:00 -0700182 for (int i = 0; i < family.fFonts.count(); ++i) {
183 const FontFileInfo& fontFile = family.fFonts[i];
bungeman8d84c992014-07-24 08:05:09 -0700184
bungeman7fa87cd2015-02-06 07:59:19 -0800185 SkString pathName(family.fBasePath);
186 pathName.append(fontFile.fFileName);
bungeman8d84c992014-07-24 08:05:09 -0700187
bungemanf93d7112016-09-16 06:24:20 -0700188 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(pathName.c_str());
189 if (!stream) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400190 SkDEBUGF("Requested font file %s does not exist or cannot be opened.\n",
191 pathName.c_str());
bungeman8d84c992014-07-24 08:05:09 -0700192 continue;
193 }
194
bungeman65fcd3d2014-08-06 11:12:20 -0700195 const int ttcIndex = fontFile.fIndex;
196 SkString familyName;
bungemana4c4a2d2014-10-20 13:33:19 -0700197 SkFontStyle style;
bungeman8d84c992014-07-24 08:05:09 -0700198 bool isFixedWidth;
bungeman41868fe2015-05-20 09:21:04 -0700199 Scanner::AxisDefinitions axisDefinitions;
200 if (!scanner.scanFont(stream.get(), ttcIndex,
201 &familyName, &style, &isFixedWidth, &axisDefinitions))
202 {
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400203 SkDEBUGF("Requested font file %s exists, but is not a valid font.\n",
204 pathName.c_str());
bungeman8d84c992014-07-24 08:05:09 -0700205 continue;
206 }
207
bungemane85a7542015-04-17 13:51:08 -0700208 int weight = fontFile.fWeight != 0 ? fontFile.fWeight : style.weight();
209 SkFontStyle::Slant slant = style.slant();
210 switch (fontFile.fStyle) {
211 case FontFileInfo::Style::kAuto: slant = style.slant(); break;
212 case FontFileInfo::Style::kNormal: slant = SkFontStyle::kUpright_Slant; break;
213 case FontFileInfo::Style::kItalic: slant = SkFontStyle::kItalic_Slant; break;
214 default: SkASSERT(false); break;
bungeman4b86bac2014-11-04 10:54:31 -0800215 }
bungemane85a7542015-04-17 13:51:08 -0700216 style = SkFontStyle(weight, style.width(), slant);
bungeman4b86bac2014-11-04 10:54:31 -0800217
djsollen3b625542014-08-14 06:29:02 -0700218 uint32_t variant = family.fVariant;
219 if (kDefault_FontVariant == variant) {
220 variant = kCompact_FontVariant | kElegant_FontVariant;
bungeman65fcd3d2014-08-06 11:12:20 -0700221 }
222
223 // The first specified family name overrides the family name found in the font.
224 // TODO: SkTypeface_AndroidSystem::onCreateFamilyNameIterator should return
225 // all of the specified family names in addition to the names found in the font.
halcanary96fcdcc2015-08-27 07:41:13 -0700226 if (cannonicalFamilyName != nullptr) {
bungeman65fcd3d2014-08-06 11:12:20 -0700227 familyName = *cannonicalFamilyName;
228 }
229
bungeman41868fe2015-05-20 09:21:04 -0700230 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
Ben Wagnerfc497342017-02-24 11:15:26 -0500231 SkFontArguments::VariationPosition position = {
232 fontFile.fVariationDesignPosition.begin(),
233 fontFile.fVariationDesignPosition.count()
234 };
235 Scanner::computeAxisValues(axisDefinitions, position,
bungeman47a1e962016-02-25 11:20:01 -0800236 axisValues, familyName);
bungeman41868fe2015-05-20 09:21:04 -0700237
bungemanf6c71072016-01-21 14:17:47 -0800238 fStyles.push_back().reset(new SkTypeface_AndroidSystem(
khushalsagarebc465b2016-02-12 12:42:48 -0800239 pathName, cacheFontFiles, ttcIndex, axisValues.get(), axisDefinitions.count(),
Ben Wagneraee878d2017-08-10 13:49:41 -0400240 style, isFixedWidth, familyName, family.fLanguages, variant));
bungeman8d84c992014-07-24 08:05:09 -0700241 }
242 }
243
mtklein36352bf2015-03-25 18:17:31 -0700244 int count() override {
bungeman8d84c992014-07-24 08:05:09 -0700245 return fStyles.count();
246 }
mtklein36352bf2015-03-25 18:17:31 -0700247 void getStyle(int index, SkFontStyle* style, SkString* name) override {
bungeman8d84c992014-07-24 08:05:09 -0700248 if (index < 0 || fStyles.count() <= index) {
249 return;
250 }
251 if (style) {
bungeman03675682016-08-18 14:36:02 -0700252 *style = fStyles[index]->fontStyle();
bungeman8d84c992014-07-24 08:05:09 -0700253 }
254 if (name) {
255 name->reset();
256 }
257 }
mtklein36352bf2015-03-25 18:17:31 -0700258 SkTypeface_AndroidSystem* createTypeface(int index) override {
bungeman8d84c992014-07-24 08:05:09 -0700259 if (index < 0 || fStyles.count() <= index) {
halcanary96fcdcc2015-08-27 07:41:13 -0700260 return nullptr;
bungeman8d84c992014-07-24 08:05:09 -0700261 }
262 return SkRef(fStyles[index].get());
263 }
264
mtklein36352bf2015-03-25 18:17:31 -0700265 SkTypeface_AndroidSystem* matchStyle(const SkFontStyle& pattern) override {
bungeman03675682016-08-18 14:36:02 -0700266 return static_cast<SkTypeface_AndroidSystem*>(this->matchStyleCSS3(pattern));
bungeman8d84c992014-07-24 08:05:09 -0700267 }
268
269private:
Brian Salomon343553a2018-09-05 15:41:23 -0400270 SkTArray<sk_sp<SkTypeface_AndroidSystem>> fStyles;
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500271 SkString fFallbackFor;
bungeman8d84c992014-07-24 08:05:09 -0700272
273 friend struct NameToFamily;
274 friend class SkFontMgr_Android;
275
John Stiles7571f9e2020-09-02 22:42:33 -0400276 using INHERITED = SkFontStyleSet;
bungeman8d84c992014-07-24 08:05:09 -0700277};
278
279/** On Android a single family can have many names, but our API assumes unique names.
280 * Map names to the back end so that all names for a given family refer to the same
281 * (non-replicated) set of typefaces.
282 * SkTDict<> doesn't let us do index-based lookup, so we write our own mapping.
283 */
284struct NameToFamily {
285 SkString name;
286 SkFontStyleSet_Android* styleSet;
287};
288
289class SkFontMgr_Android : public SkFontMgr {
290public:
bungeman7fa87cd2015-02-06 07:59:19 -0800291 SkFontMgr_Android(const SkFontMgr_Android_CustomFonts* custom) {
292 SkTDArray<FontFamily*> families;
293 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem != custom->fSystemFontUse) {
294 SkString base(custom->fBasePath);
bungemanc5308542015-06-23 13:25:46 -0700295 SkFontMgr_Android_Parser::GetCustomFontFamilies(
296 families, base, custom->fFontsXml, custom->fFallbackFontsXml);
bungeman7fa87cd2015-02-06 07:59:19 -0800297 }
298 if (!custom ||
299 (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSystemFontUse))
300 {
bungemanc5308542015-06-23 13:25:46 -0700301 SkFontMgr_Android_Parser::GetSystemFontFamilies(families);
bungeman7fa87cd2015-02-06 07:59:19 -0800302 }
303 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem == custom->fSystemFontUse) {
304 SkString base(custom->fBasePath);
bungemanc5308542015-06-23 13:25:46 -0700305 SkFontMgr_Android_Parser::GetCustomFontFamilies(
306 families, base, custom->fFontsXml, custom->fFallbackFontsXml);
bungeman7fa87cd2015-02-06 07:59:19 -0800307 }
khushalsagarebc465b2016-02-12 12:42:48 -0800308 this->buildNameToFamilyMap(families, custom ? custom->fIsolated : false);
bungeman83b24ff2016-08-19 05:03:26 -0700309 this->findDefaultStyleSet();
bungeman7fa87cd2015-02-06 07:59:19 -0800310 families.deleteAll();
bungeman8d84c992014-07-24 08:05:09 -0700311 }
312
313protected:
314 /** Returns not how many families we have, but how many unique names
315 * exist among the families.
316 */
mtklein36352bf2015-03-25 18:17:31 -0700317 int onCountFamilies() const override {
bungeman8d84c992014-07-24 08:05:09 -0700318 return fNameToFamilyMap.count();
319 }
320
mtklein36352bf2015-03-25 18:17:31 -0700321 void onGetFamilyName(int index, SkString* familyName) const override {
bungeman8d84c992014-07-24 08:05:09 -0700322 if (index < 0 || fNameToFamilyMap.count() <= index) {
323 familyName->reset();
324 return;
325 }
326 familyName->set(fNameToFamilyMap[index].name);
327 }
328
mtklein36352bf2015-03-25 18:17:31 -0700329 SkFontStyleSet* onCreateStyleSet(int index) const override {
bungeman8d84c992014-07-24 08:05:09 -0700330 if (index < 0 || fNameToFamilyMap.count() <= index) {
halcanary96fcdcc2015-08-27 07:41:13 -0700331 return nullptr;
bungeman8d84c992014-07-24 08:05:09 -0700332 }
333 return SkRef(fNameToFamilyMap[index].styleSet);
334 }
335
mtklein36352bf2015-03-25 18:17:31 -0700336 SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
bungeman8d84c992014-07-24 08:05:09 -0700337 if (!familyName) {
halcanary96fcdcc2015-08-27 07:41:13 -0700338 return nullptr;
bungeman8d84c992014-07-24 08:05:09 -0700339 }
340 SkAutoAsciiToLC tolc(familyName);
341 for (int i = 0; i < fNameToFamilyMap.count(); ++i) {
342 if (fNameToFamilyMap[i].name.equals(tolc.lc())) {
343 return SkRef(fNameToFamilyMap[i].styleSet);
344 }
345 }
bungeman65fcd3d2014-08-06 11:12:20 -0700346 // TODO: eventually we should not need to name fallback families.
347 for (int i = 0; i < fFallbackNameToFamilyMap.count(); ++i) {
348 if (fFallbackNameToFamilyMap[i].name.equals(tolc.lc())) {
349 return SkRef(fFallbackNameToFamilyMap[i].styleSet);
350 }
351 }
halcanary96fcdcc2015-08-27 07:41:13 -0700352 return nullptr;
bungeman8d84c992014-07-24 08:05:09 -0700353 }
354
John Stiles1cf2c8d2020-08-13 22:58:04 -0400355 SkTypeface* onMatchFamilyStyle(const char familyName[],
356 const SkFontStyle& style) const override {
Hal Canary67b39de2016-11-07 11:47:44 -0500357 sk_sp<SkFontStyleSet> sset(this->matchFamily(familyName));
bungeman8d84c992014-07-24 08:05:09 -0700358 return sset->matchStyle(style);
359 }
360
John Stiles1cf2c8d2020-08-13 22:58:04 -0400361 SkTypeface* onMatchFaceStyle(const SkTypeface* typeface,
362 const SkFontStyle& style) const override {
bungeman83b24ff2016-08-19 05:03:26 -0700363 for (int i = 0; i < fStyleSets.count(); ++i) {
364 for (int j = 0; j < fStyleSets[i]->fStyles.count(); ++j) {
Hal Canary67b39de2016-11-07 11:47:44 -0500365 if (fStyleSets[i]->fStyles[j].get() == typeface) {
bungeman83b24ff2016-08-19 05:03:26 -0700366 return fStyleSets[i]->matchStyle(style);
bungeman8d84c992014-07-24 08:05:09 -0700367 }
368 }
369 }
halcanary96fcdcc2015-08-27 07:41:13 -0700370 return nullptr;
bungeman8d84c992014-07-24 08:05:09 -0700371 }
372
bungeman13b9c952016-05-12 10:09:30 -0700373 static sk_sp<SkTypeface_AndroidSystem> find_family_style_character(
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500374 const SkString& familyName,
bungeman83b24ff2016-08-19 05:03:26 -0700375 const SkTArray<NameToFamily, true>& fallbackNameToFamilyMap,
bungemanc9232dc2014-11-10 13:29:33 -0800376 const SkFontStyle& style, bool elegant,
377 const SkString& langTag, SkUnichar character)
378 {
379 for (int i = 0; i < fallbackNameToFamilyMap.count(); ++i) {
380 SkFontStyleSet_Android* family = fallbackNameToFamilyMap[i].styleSet;
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500381 if (familyName != family->fFallbackFor) {
382 continue;
383 }
bungeman13b9c952016-05-12 10:09:30 -0700384 sk_sp<SkTypeface_AndroidSystem> face(family->matchStyle(style));
bungemanc20386e2014-10-23 07:08:05 -0700385
Ben Wagneraee878d2017-08-10 13:49:41 -0400386 if (!langTag.isEmpty() &&
387 std::none_of(face->fLang.begin(), face->fLang.end(), [&](SkLanguage lang){
388 return lang.getTag().startsWith(langTag.c_str());
389 }))
390 {
bungemanc9232dc2014-11-10 13:29:33 -0800391 continue;
392 }
393
394 if (SkToBool(face->fVariantStyle & kElegant_FontVariant) != elegant) {
395 continue;
396 }
397
Mike Reed65528ab2019-01-02 13:44:39 -0500398 if (face->unicharToGlyph(character) != 0) {
bungeman13b9c952016-05-12 10:09:30 -0700399 return face;
scroggo9a9a7b22016-05-12 06:22:30 -0700400 }
bungemanc20386e2014-10-23 07:08:05 -0700401 }
halcanary96fcdcc2015-08-27 07:41:13 -0700402 return nullptr;
bungemanc20386e2014-10-23 07:08:05 -0700403 }
bungemanc9232dc2014-11-10 13:29:33 -0800404
John Stiles1cf2c8d2020-08-13 22:58:04 -0400405 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
406 const SkFontStyle& style,
407 const char* bcp47[],
408 int bcp47Count,
409 SkUnichar character) const override {
bungeman65fcd3d2014-08-06 11:12:20 -0700410 // The variant 'elegant' is 'not squashed', 'compact' is 'stays in ascent/descent'.
411 // The variant 'default' means 'compact and elegant'.
412 // As a result, it is not possible to know the variant context from the font alone.
413 // TODO: add 'is_elegant' and 'is_compact' bits to 'style' request.
414
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500415 SkString familyNameString(familyName);
416 for (const SkString& currentFamilyName : { familyNameString, SkString() }) {
417 // The first time match anything elegant, second time anything not elegant.
418 for (int elegant = 2; elegant --> 0;) {
419 for (int bcp47Index = bcp47Count; bcp47Index --> 0;) {
420 SkLanguage lang(bcp47[bcp47Index]);
421 while (!lang.getTag().isEmpty()) {
422 sk_sp<SkTypeface_AndroidSystem> matchingTypeface =
423 find_family_style_character(currentFamilyName, fFallbackNameToFamilyMap,
424 style, SkToBool(elegant),
425 lang.getTag(), character);
426 if (matchingTypeface) {
427 return matchingTypeface.release();
428 }
bungeman65fcd3d2014-08-06 11:12:20 -0700429
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500430 lang = lang.getParent();
431 }
bungeman65fcd3d2014-08-06 11:12:20 -0700432 }
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500433 sk_sp<SkTypeface_AndroidSystem> matchingTypeface =
434 find_family_style_character(currentFamilyName, fFallbackNameToFamilyMap,
435 style, SkToBool(elegant),
436 SkString(), character);
437 if (matchingTypeface) {
438 return matchingTypeface.release();
439 }
bungemanc20386e2014-10-23 07:08:05 -0700440 }
bungeman65fcd3d2014-08-06 11:12:20 -0700441 }
halcanary96fcdcc2015-08-27 07:41:13 -0700442 return nullptr;
bungeman65fcd3d2014-08-06 11:12:20 -0700443 }
444
Mike Reed59227392017-09-26 09:46:08 -0400445 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override {
446 return this->makeFromStream(std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data))),
447 ttcIndex);
bungeman8d84c992014-07-24 08:05:09 -0700448 }
449
Mike Reed59227392017-09-26 09:46:08 -0400450 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
bungemanf93d7112016-09-16 06:24:20 -0700451 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
John Stilesa008b0f2020-08-16 08:48:02 -0400452 return stream ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr;
bungeman8d84c992014-07-24 08:05:09 -0700453 }
454
Mike Reed59227392017-09-26 09:46:08 -0400455 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,
456 int ttcIndex) const override {
bungeman8d84c992014-07-24 08:05:09 -0700457 bool isFixedPitch;
bungemana4c4a2d2014-10-20 13:33:19 -0700458 SkFontStyle style;
bungeman8d84c992014-07-24 08:05:09 -0700459 SkString name;
bungemanf93d7112016-09-16 06:24:20 -0700460 if (!fScanner.scanFont(stream.get(), ttcIndex, &name, &style, &isFixedPitch, nullptr)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700461 return nullptr;
bungeman8d84c992014-07-24 08:05:09 -0700462 }
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500463 auto data = std::make_unique<SkFontData>(std::move(stream), ttcIndex, nullptr, 0);
Mike Reed59227392017-09-26 09:46:08 -0400464 return sk_sp<SkTypeface>(new SkTypeface_AndroidStream(std::move(data),
465 style, isFixedPitch, name));
bungeman41868fe2015-05-20 09:21:04 -0700466 }
467
Mike Reed59227392017-09-26 09:46:08 -0400468 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,
469 const SkFontArguments& args) const override {
bungemanf6c71072016-01-21 14:17:47 -0800470 using Scanner = SkTypeface_FreeType::Scanner;
bungemanf6c71072016-01-21 14:17:47 -0800471 bool isFixedPitch;
472 SkFontStyle style;
473 SkString name;
474 Scanner::AxisDefinitions axisDefinitions;
Ben Wagnerfc497342017-02-24 11:15:26 -0500475 if (!fScanner.scanFont(stream.get(), args.getCollectionIndex(),
bungemanf93d7112016-09-16 06:24:20 -0700476 &name, &style, &isFixedPitch, &axisDefinitions))
bungemanf6c71072016-01-21 14:17:47 -0800477 {
478 return nullptr;
479 }
480
bungemanf6c71072016-01-21 14:17:47 -0800481 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
Ben Wagnerfc497342017-02-24 11:15:26 -0500482 Scanner::computeAxisValues(axisDefinitions, args.getVariationDesignPosition(),
483 axisValues, name);
bungemanf6c71072016-01-21 14:17:47 -0800484
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500485 auto data = std::make_unique<SkFontData>(std::move(stream), args.getCollectionIndex(),
bungemanf93d7112016-09-16 06:24:20 -0700486 axisValues.get(), axisDefinitions.count());
Mike Reed59227392017-09-26 09:46:08 -0400487 return sk_sp<SkTypeface>(new SkTypeface_AndroidStream(std::move(data),
488 style, isFixedPitch, name));
bungemanf6c71072016-01-21 14:17:47 -0800489 }
490
Mike Reed59227392017-09-26 09:46:08 -0400491 sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData> data) const override {
bungeman41868fe2015-05-20 09:21:04 -0700492 SkStreamAsset* stream(data->getStream());
493 bool isFixedPitch;
494 SkFontStyle style;
495 SkString name;
halcanary96fcdcc2015-08-27 07:41:13 -0700496 if (!fScanner.scanFont(stream, data->getIndex(), &name, &style, &isFixedPitch, nullptr)) {
497 return nullptr;
bungeman41868fe2015-05-20 09:21:04 -0700498 }
Mike Reed59227392017-09-26 09:46:08 -0400499 return sk_sp<SkTypeface>(new SkTypeface_AndroidStream(std::move(data),
500 style, isFixedPitch, name));
bungeman8d84c992014-07-24 08:05:09 -0700501 }
502
Mike Reed59227392017-09-26 09:46:08 -0400503 sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override {
bsalomon49f085d2014-09-05 13:34:00 -0700504 if (familyName) {
halcanary96fcdcc2015-08-27 07:41:13 -0700505 // On Android, we must return nullptr when we can't find the requested
bungeman8d84c992014-07-24 08:05:09 -0700506 // named typeface so that the system/app can provide their own recovery
507 // mechanism. On other platforms we'd provide a typeface from the
508 // default family instead.
Mike Reed59227392017-09-26 09:46:08 -0400509 return sk_sp<SkTypeface>(this->onMatchFamilyStyle(familyName, style));
bungeman8d84c992014-07-24 08:05:09 -0700510 }
Mike Reed59227392017-09-26 09:46:08 -0400511 return sk_sp<SkTypeface>(fDefaultStyleSet->matchStyle(style));
bungeman8d84c992014-07-24 08:05:09 -0700512 }
513
514
515private:
516
bungeman14df8332014-10-28 15:07:23 -0700517 SkTypeface_FreeType::Scanner fScanner;
518
Brian Salomon343553a2018-09-05 15:41:23 -0400519 SkTArray<sk_sp<SkFontStyleSet_Android>> fStyleSets;
bungeman83b24ff2016-08-19 05:03:26 -0700520 sk_sp<SkFontStyleSet> fDefaultStyleSet;
bungeman8d84c992014-07-24 08:05:09 -0700521
bungeman83b24ff2016-08-19 05:03:26 -0700522 SkTArray<NameToFamily, true> fNameToFamilyMap;
523 SkTArray<NameToFamily, true> fFallbackNameToFamilyMap;
bungeman8d84c992014-07-24 08:05:09 -0700524
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500525 void addFamily(FontFamily& family, const bool isolated, int familyIndex) {
526 SkTArray<NameToFamily, true>* nameToFamily = &fNameToFamilyMap;
527 if (family.fIsFallbackFont) {
528 nameToFamily = &fFallbackNameToFamilyMap;
529
530 if (0 == family.fNames.count()) {
531 SkString& fallbackName = family.fNames.push_back();
532 fallbackName.printf("%.2x##fallback", familyIndex);
533 }
534 }
535
536 sk_sp<SkFontStyleSet_Android> newSet =
537 sk_make_sp<SkFontStyleSet_Android>(family, fScanner, isolated);
538 if (0 == newSet->count()) {
539 return;
540 }
541
542 for (const SkString& name : family.fNames) {
543 nameToFamily->emplace_back(NameToFamily{name, newSet.get()});
544 }
545 fStyleSets.emplace_back(std::move(newSet));
546 }
khushalsagarebc465b2016-02-12 12:42:48 -0800547 void buildNameToFamilyMap(SkTDArray<FontFamily*> families, const bool isolated) {
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500548 int familyIndex = 0;
549 for (FontFamily* family : families) {
550 addFamily(*family, isolated, familyIndex++);
John Stiles65b48272020-12-22 17:18:34 -0500551 for (const auto& [unused, fallbackFamily] : family->fallbackFamilies) {
552 addFamily(*fallbackFamily, isolated, familyIndex++);
553 }
bungeman8d84c992014-07-24 08:05:09 -0700554 }
555 }
556
bungeman83b24ff2016-08-19 05:03:26 -0700557 void findDefaultStyleSet() {
558 SkASSERT(!fStyleSets.empty());
bungeman8d84c992014-07-24 08:05:09 -0700559
bungeman83b24ff2016-08-19 05:03:26 -0700560 static const char* defaultNames[] = { "sans-serif" };
561 for (const char* defaultName : defaultNames) {
562 fDefaultStyleSet.reset(this->onMatchFamily(defaultName));
563 if (fDefaultStyleSet) {
564 break;
bungeman8d84c992014-07-24 08:05:09 -0700565 }
bungeman8d84c992014-07-24 08:05:09 -0700566 }
bungeman83b24ff2016-08-19 05:03:26 -0700567 if (nullptr == fDefaultStyleSet) {
568 fDefaultStyleSet = fStyleSets[0];
bungeman8d84c992014-07-24 08:05:09 -0700569 }
bungeman83b24ff2016-08-19 05:03:26 -0700570 SkASSERT(fDefaultStyleSet);
bungeman8d84c992014-07-24 08:05:09 -0700571 }
572
John Stiles7571f9e2020-09-02 22:42:33 -0400573 using INHERITED = SkFontMgr;
bungeman8d84c992014-07-24 08:05:09 -0700574};
575
bungeman7fa87cd2015-02-06 07:59:19 -0800576#ifdef SK_DEBUG
577static char const * const gSystemFontUseStrings[] = {
578 "OnlyCustom", "PreferCustom", "PreferSystem"
579};
580#endif
Ben Wagner20d031a2017-01-11 13:54:39 -0500581
Ben Wagner3546ff12017-01-03 13:32:36 -0500582sk_sp<SkFontMgr> SkFontMgr_New_Android(const SkFontMgr_Android_CustomFonts* custom) {
bungeman7fa87cd2015-02-06 07:59:19 -0800583 if (custom) {
584 SkASSERT(0 <= custom->fSystemFontUse);
585 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings));
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400586 SkDEBUGF("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n",
587 gSystemFontUseStrings[custom->fSystemFontUse],
588 custom->fBasePath,
589 custom->fFontsXml,
590 custom->fFallbackFontsXml);
bungeman4e3523c2014-08-08 12:06:51 -0700591 }
Ben Wagner3546ff12017-01-03 13:32:36 -0500592 return sk_make_sp<SkFontMgr_Android>(custom);
bungeman8d84c992014-07-24 08:05:09 -0700593}