blob: 0931fbf8c8b060a2af925672c44e1e402d5bb4c1 [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:
51 typedef SkTypeface_FreeType INHERITED;
52};
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
bungeman41868fe2015-05-20 09:21:04 -070085 virtual 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
124 typedef SkTypeface_Android INHERITED;
125};
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
137 virtual void onGetFontDescriptor(SkFontDescriptor* desc,
mtklein36352bf2015-03-25 18:17:31 -0700138 bool* serialize) const override {
bungeman8d84c992014-07-24 08:05:09 -0700139 SkASSERT(desc);
140 SkASSERT(serialize);
141 desc->setFamilyName(fFamilyName.c_str());
bungeman8d84c992014-07-24 08:05:09 -0700142 *serialize = true;
143 }
144
Ben Wagner4212a7d2019-02-25 14:27:46 -0500145 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override {
bungeman41868fe2015-05-20 09:21:04 -0700146 *ttcIndex = fData->getIndex();
Ben Wagner4212a7d2019-02-25 14:27:46 -0500147 return fData->getStream()->duplicate();
bungeman41868fe2015-05-20 09:21:04 -0700148 }
149
bungemanf93d7112016-09-16 06:24:20 -0700150 std::unique_ptr<SkFontData> onMakeFontData() const override {
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500151 return std::make_unique<SkFontData>(*fData);
bungeman8d84c992014-07-24 08:05:09 -0700152 }
153
Bruce Wangebf0cf52018-06-18 14:04:19 -0400154 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override {
155 std::unique_ptr<SkFontData> data = this->cloneFontData(args);
156 if (!data) {
157 return nullptr;
158 }
159 return sk_make_sp<SkTypeface_AndroidStream>(std::move(data),
160 this->fontStyle(),
161 this->isFixedPitch(),
162 fFamilyName);
163 }
164
bungeman8d84c992014-07-24 08:05:09 -0700165private:
bungemanf93d7112016-09-16 06:24:20 -0700166 const std::unique_ptr<const SkFontData> fData;
bungeman8d84c992014-07-24 08:05:09 -0700167 typedef SkTypeface_Android INHERITED;
168};
169
bungeman8d84c992014-07-24 08:05:09 -0700170class SkFontStyleSet_Android : public SkFontStyleSet {
bungeman41868fe2015-05-20 09:21:04 -0700171 typedef SkTypeface_FreeType::Scanner Scanner;
172
bungeman8d84c992014-07-24 08:05:09 -0700173public:
khushalsagarebc465b2016-02-12 12:42:48 -0800174 explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& scanner,
175 const bool cacheFontFiles) {
halcanary96fcdcc2015-08-27 07:41:13 -0700176 const SkString* cannonicalFamilyName = nullptr;
bungeman65fcd3d2014-08-06 11:12:20 -0700177 if (family.fNames.count() > 0) {
178 cannonicalFamilyName = &family.fNames[0];
179 }
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500180 fFallbackFor = family.fFallbackFor;
181
bungeman8d84c992014-07-24 08:05:09 -0700182 // TODO? make this lazy
tomhudsond3ddea22014-08-11 11:28:00 -0700183 for (int i = 0; i < family.fFonts.count(); ++i) {
184 const FontFileInfo& fontFile = family.fFonts[i];
bungeman8d84c992014-07-24 08:05:09 -0700185
bungeman7fa87cd2015-02-06 07:59:19 -0800186 SkString pathName(family.fBasePath);
187 pathName.append(fontFile.fFileName);
bungeman8d84c992014-07-24 08:05:09 -0700188
bungemanf93d7112016-09-16 06:24:20 -0700189 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(pathName.c_str());
190 if (!stream) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400191 SkDEBUGF("Requested font file %s does not exist or cannot be opened.\n",
192 pathName.c_str());
bungeman8d84c992014-07-24 08:05:09 -0700193 continue;
194 }
195
bungeman65fcd3d2014-08-06 11:12:20 -0700196 const int ttcIndex = fontFile.fIndex;
197 SkString familyName;
bungemana4c4a2d2014-10-20 13:33:19 -0700198 SkFontStyle style;
bungeman8d84c992014-07-24 08:05:09 -0700199 bool isFixedWidth;
bungeman41868fe2015-05-20 09:21:04 -0700200 Scanner::AxisDefinitions axisDefinitions;
201 if (!scanner.scanFont(stream.get(), ttcIndex,
202 &familyName, &style, &isFixedWidth, &axisDefinitions))
203 {
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400204 SkDEBUGF("Requested font file %s exists, but is not a valid font.\n",
205 pathName.c_str());
bungeman8d84c992014-07-24 08:05:09 -0700206 continue;
207 }
208
bungemane85a7542015-04-17 13:51:08 -0700209 int weight = fontFile.fWeight != 0 ? fontFile.fWeight : style.weight();
210 SkFontStyle::Slant slant = style.slant();
211 switch (fontFile.fStyle) {
212 case FontFileInfo::Style::kAuto: slant = style.slant(); break;
213 case FontFileInfo::Style::kNormal: slant = SkFontStyle::kUpright_Slant; break;
214 case FontFileInfo::Style::kItalic: slant = SkFontStyle::kItalic_Slant; break;
215 default: SkASSERT(false); break;
bungeman4b86bac2014-11-04 10:54:31 -0800216 }
bungemane85a7542015-04-17 13:51:08 -0700217 style = SkFontStyle(weight, style.width(), slant);
bungeman4b86bac2014-11-04 10:54:31 -0800218
djsollen3b625542014-08-14 06:29:02 -0700219 uint32_t variant = family.fVariant;
220 if (kDefault_FontVariant == variant) {
221 variant = kCompact_FontVariant | kElegant_FontVariant;
bungeman65fcd3d2014-08-06 11:12:20 -0700222 }
223
224 // The first specified family name overrides the family name found in the font.
225 // TODO: SkTypeface_AndroidSystem::onCreateFamilyNameIterator should return
226 // all of the specified family names in addition to the names found in the font.
halcanary96fcdcc2015-08-27 07:41:13 -0700227 if (cannonicalFamilyName != nullptr) {
bungeman65fcd3d2014-08-06 11:12:20 -0700228 familyName = *cannonicalFamilyName;
229 }
230
bungeman41868fe2015-05-20 09:21:04 -0700231 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
Ben Wagnerfc497342017-02-24 11:15:26 -0500232 SkFontArguments::VariationPosition position = {
233 fontFile.fVariationDesignPosition.begin(),
234 fontFile.fVariationDesignPosition.count()
235 };
236 Scanner::computeAxisValues(axisDefinitions, position,
bungeman47a1e962016-02-25 11:20:01 -0800237 axisValues, familyName);
bungeman41868fe2015-05-20 09:21:04 -0700238
bungemanf6c71072016-01-21 14:17:47 -0800239 fStyles.push_back().reset(new SkTypeface_AndroidSystem(
khushalsagarebc465b2016-02-12 12:42:48 -0800240 pathName, cacheFontFiles, ttcIndex, axisValues.get(), axisDefinitions.count(),
Ben Wagneraee878d2017-08-10 13:49:41 -0400241 style, isFixedWidth, familyName, family.fLanguages, variant));
bungeman8d84c992014-07-24 08:05:09 -0700242 }
243 }
244
mtklein36352bf2015-03-25 18:17:31 -0700245 int count() override {
bungeman8d84c992014-07-24 08:05:09 -0700246 return fStyles.count();
247 }
mtklein36352bf2015-03-25 18:17:31 -0700248 void getStyle(int index, SkFontStyle* style, SkString* name) override {
bungeman8d84c992014-07-24 08:05:09 -0700249 if (index < 0 || fStyles.count() <= index) {
250 return;
251 }
252 if (style) {
bungeman03675682016-08-18 14:36:02 -0700253 *style = fStyles[index]->fontStyle();
bungeman8d84c992014-07-24 08:05:09 -0700254 }
255 if (name) {
256 name->reset();
257 }
258 }
mtklein36352bf2015-03-25 18:17:31 -0700259 SkTypeface_AndroidSystem* createTypeface(int index) override {
bungeman8d84c992014-07-24 08:05:09 -0700260 if (index < 0 || fStyles.count() <= index) {
halcanary96fcdcc2015-08-27 07:41:13 -0700261 return nullptr;
bungeman8d84c992014-07-24 08:05:09 -0700262 }
263 return SkRef(fStyles[index].get());
264 }
265
mtklein36352bf2015-03-25 18:17:31 -0700266 SkTypeface_AndroidSystem* matchStyle(const SkFontStyle& pattern) override {
bungeman03675682016-08-18 14:36:02 -0700267 return static_cast<SkTypeface_AndroidSystem*>(this->matchStyleCSS3(pattern));
bungeman8d84c992014-07-24 08:05:09 -0700268 }
269
270private:
Brian Salomon343553a2018-09-05 15:41:23 -0400271 SkTArray<sk_sp<SkTypeface_AndroidSystem>> fStyles;
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500272 SkString fFallbackFor;
bungeman8d84c992014-07-24 08:05:09 -0700273
274 friend struct NameToFamily;
275 friend class SkFontMgr_Android;
276
277 typedef SkFontStyleSet INHERITED;
278};
279
280/** On Android a single family can have many names, but our API assumes unique names.
281 * Map names to the back end so that all names for a given family refer to the same
282 * (non-replicated) set of typefaces.
283 * SkTDict<> doesn't let us do index-based lookup, so we write our own mapping.
284 */
285struct NameToFamily {
286 SkString name;
287 SkFontStyleSet_Android* styleSet;
288};
289
290class SkFontMgr_Android : public SkFontMgr {
291public:
bungeman7fa87cd2015-02-06 07:59:19 -0800292 SkFontMgr_Android(const SkFontMgr_Android_CustomFonts* custom) {
293 SkTDArray<FontFamily*> families;
294 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem != custom->fSystemFontUse) {
295 SkString base(custom->fBasePath);
bungemanc5308542015-06-23 13:25:46 -0700296 SkFontMgr_Android_Parser::GetCustomFontFamilies(
297 families, base, custom->fFontsXml, custom->fFallbackFontsXml);
bungeman7fa87cd2015-02-06 07:59:19 -0800298 }
299 if (!custom ||
300 (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSystemFontUse))
301 {
bungemanc5308542015-06-23 13:25:46 -0700302 SkFontMgr_Android_Parser::GetSystemFontFamilies(families);
bungeman7fa87cd2015-02-06 07:59:19 -0800303 }
304 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem == custom->fSystemFontUse) {
305 SkString base(custom->fBasePath);
bungemanc5308542015-06-23 13:25:46 -0700306 SkFontMgr_Android_Parser::GetCustomFontFamilies(
307 families, base, custom->fFontsXml, custom->fFallbackFontsXml);
bungeman7fa87cd2015-02-06 07:59:19 -0800308 }
khushalsagarebc465b2016-02-12 12:42:48 -0800309 this->buildNameToFamilyMap(families, custom ? custom->fIsolated : false);
bungeman83b24ff2016-08-19 05:03:26 -0700310 this->findDefaultStyleSet();
bungeman7fa87cd2015-02-06 07:59:19 -0800311 families.deleteAll();
bungeman8d84c992014-07-24 08:05:09 -0700312 }
313
314protected:
315 /** Returns not how many families we have, but how many unique names
316 * exist among the families.
317 */
mtklein36352bf2015-03-25 18:17:31 -0700318 int onCountFamilies() const override {
bungeman8d84c992014-07-24 08:05:09 -0700319 return fNameToFamilyMap.count();
320 }
321
mtklein36352bf2015-03-25 18:17:31 -0700322 void onGetFamilyName(int index, SkString* familyName) const override {
bungeman8d84c992014-07-24 08:05:09 -0700323 if (index < 0 || fNameToFamilyMap.count() <= index) {
324 familyName->reset();
325 return;
326 }
327 familyName->set(fNameToFamilyMap[index].name);
328 }
329
mtklein36352bf2015-03-25 18:17:31 -0700330 SkFontStyleSet* onCreateStyleSet(int index) const override {
bungeman8d84c992014-07-24 08:05:09 -0700331 if (index < 0 || fNameToFamilyMap.count() <= index) {
halcanary96fcdcc2015-08-27 07:41:13 -0700332 return nullptr;
bungeman8d84c992014-07-24 08:05:09 -0700333 }
334 return SkRef(fNameToFamilyMap[index].styleSet);
335 }
336
mtklein36352bf2015-03-25 18:17:31 -0700337 SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
bungeman8d84c992014-07-24 08:05:09 -0700338 if (!familyName) {
halcanary96fcdcc2015-08-27 07:41:13 -0700339 return nullptr;
bungeman8d84c992014-07-24 08:05:09 -0700340 }
341 SkAutoAsciiToLC tolc(familyName);
342 for (int i = 0; i < fNameToFamilyMap.count(); ++i) {
343 if (fNameToFamilyMap[i].name.equals(tolc.lc())) {
344 return SkRef(fNameToFamilyMap[i].styleSet);
345 }
346 }
bungeman65fcd3d2014-08-06 11:12:20 -0700347 // TODO: eventually we should not need to name fallback families.
348 for (int i = 0; i < fFallbackNameToFamilyMap.count(); ++i) {
349 if (fFallbackNameToFamilyMap[i].name.equals(tolc.lc())) {
350 return SkRef(fFallbackNameToFamilyMap[i].styleSet);
351 }
352 }
halcanary96fcdcc2015-08-27 07:41:13 -0700353 return nullptr;
bungeman8d84c992014-07-24 08:05:09 -0700354 }
355
356 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
mtklein36352bf2015-03-25 18:17:31 -0700357 const SkFontStyle& style) const override {
Hal Canary67b39de2016-11-07 11:47:44 -0500358 sk_sp<SkFontStyleSet> sset(this->matchFamily(familyName));
bungeman8d84c992014-07-24 08:05:09 -0700359 return sset->matchStyle(style);
360 }
361
362 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* typeface,
mtklein36352bf2015-03-25 18:17:31 -0700363 const SkFontStyle& style) const override {
bungeman83b24ff2016-08-19 05:03:26 -0700364 for (int i = 0; i < fStyleSets.count(); ++i) {
365 for (int j = 0; j < fStyleSets[i]->fStyles.count(); ++j) {
Hal Canary67b39de2016-11-07 11:47:44 -0500366 if (fStyleSets[i]->fStyles[j].get() == typeface) {
bungeman83b24ff2016-08-19 05:03:26 -0700367 return fStyleSets[i]->matchStyle(style);
bungeman8d84c992014-07-24 08:05:09 -0700368 }
369 }
370 }
halcanary96fcdcc2015-08-27 07:41:13 -0700371 return nullptr;
bungeman8d84c992014-07-24 08:05:09 -0700372 }
373
bungeman13b9c952016-05-12 10:09:30 -0700374 static sk_sp<SkTypeface_AndroidSystem> find_family_style_character(
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500375 const SkString& familyName,
bungeman83b24ff2016-08-19 05:03:26 -0700376 const SkTArray<NameToFamily, true>& fallbackNameToFamilyMap,
bungemanc9232dc2014-11-10 13:29:33 -0800377 const SkFontStyle& style, bool elegant,
378 const SkString& langTag, SkUnichar character)
379 {
380 for (int i = 0; i < fallbackNameToFamilyMap.count(); ++i) {
381 SkFontStyleSet_Android* family = fallbackNameToFamilyMap[i].styleSet;
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500382 if (familyName != family->fFallbackFor) {
383 continue;
384 }
bungeman13b9c952016-05-12 10:09:30 -0700385 sk_sp<SkTypeface_AndroidSystem> face(family->matchStyle(style));
bungemanc20386e2014-10-23 07:08:05 -0700386
Ben Wagneraee878d2017-08-10 13:49:41 -0400387 if (!langTag.isEmpty() &&
388 std::none_of(face->fLang.begin(), face->fLang.end(), [&](SkLanguage lang){
389 return lang.getTag().startsWith(langTag.c_str());
390 }))
391 {
bungemanc9232dc2014-11-10 13:29:33 -0800392 continue;
393 }
394
395 if (SkToBool(face->fVariantStyle & kElegant_FontVariant) != elegant) {
396 continue;
397 }
398
Mike Reed65528ab2019-01-02 13:44:39 -0500399 if (face->unicharToGlyph(character) != 0) {
bungeman13b9c952016-05-12 10:09:30 -0700400 return face;
scroggo9a9a7b22016-05-12 06:22:30 -0700401 }
bungemanc20386e2014-10-23 07:08:05 -0700402 }
halcanary96fcdcc2015-08-27 07:41:13 -0700403 return nullptr;
bungemanc20386e2014-10-23 07:08:05 -0700404 }
bungemanc9232dc2014-11-10 13:29:33 -0800405
bungeman65fcd3d2014-08-06 11:12:20 -0700406 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
407 const SkFontStyle& style,
bungemanc20386e2014-10-23 07:08:05 -0700408 const char* bcp47[],
409 int bcp47Count,
mtklein36352bf2015-03-25 18:17:31 -0700410 SkUnichar character) const override
bungeman65fcd3d2014-08-06 11:12:20 -0700411 {
412 // The variant 'elegant' is 'not squashed', 'compact' is 'stays in ascent/descent'.
413 // The variant 'default' means 'compact and elegant'.
414 // As a result, it is not possible to know the variant context from the font alone.
415 // TODO: add 'is_elegant' and 'is_compact' bits to 'style' request.
416
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500417 SkString familyNameString(familyName);
418 for (const SkString& currentFamilyName : { familyNameString, SkString() }) {
419 // The first time match anything elegant, second time anything not elegant.
420 for (int elegant = 2; elegant --> 0;) {
421 for (int bcp47Index = bcp47Count; bcp47Index --> 0;) {
422 SkLanguage lang(bcp47[bcp47Index]);
423 while (!lang.getTag().isEmpty()) {
424 sk_sp<SkTypeface_AndroidSystem> matchingTypeface =
425 find_family_style_character(currentFamilyName, fFallbackNameToFamilyMap,
426 style, SkToBool(elegant),
427 lang.getTag(), character);
428 if (matchingTypeface) {
429 return matchingTypeface.release();
430 }
bungeman65fcd3d2014-08-06 11:12:20 -0700431
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500432 lang = lang.getParent();
433 }
bungeman65fcd3d2014-08-06 11:12:20 -0700434 }
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500435 sk_sp<SkTypeface_AndroidSystem> matchingTypeface =
436 find_family_style_character(currentFamilyName, fFallbackNameToFamilyMap,
437 style, SkToBool(elegant),
438 SkString(), character);
439 if (matchingTypeface) {
440 return matchingTypeface.release();
441 }
bungemanc20386e2014-10-23 07:08:05 -0700442 }
bungeman65fcd3d2014-08-06 11:12:20 -0700443 }
halcanary96fcdcc2015-08-27 07:41:13 -0700444 return nullptr;
bungeman65fcd3d2014-08-06 11:12:20 -0700445 }
446
Mike Reed59227392017-09-26 09:46:08 -0400447 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override {
448 return this->makeFromStream(std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data))),
449 ttcIndex);
bungeman8d84c992014-07-24 08:05:09 -0700450 }
451
Mike Reed59227392017-09-26 09:46:08 -0400452 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
bungemanf93d7112016-09-16 06:24:20 -0700453 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
Mike Reed59227392017-09-26 09:46:08 -0400454 return stream.get() ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr;
bungeman8d84c992014-07-24 08:05:09 -0700455 }
456
Mike Reed59227392017-09-26 09:46:08 -0400457 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,
458 int ttcIndex) const override {
bungeman8d84c992014-07-24 08:05:09 -0700459 bool isFixedPitch;
bungemana4c4a2d2014-10-20 13:33:19 -0700460 SkFontStyle style;
bungeman8d84c992014-07-24 08:05:09 -0700461 SkString name;
bungemanf93d7112016-09-16 06:24:20 -0700462 if (!fScanner.scanFont(stream.get(), ttcIndex, &name, &style, &isFixedPitch, nullptr)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700463 return nullptr;
bungeman8d84c992014-07-24 08:05:09 -0700464 }
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500465 auto data = std::make_unique<SkFontData>(std::move(stream), ttcIndex, nullptr, 0);
Mike Reed59227392017-09-26 09:46:08 -0400466 return sk_sp<SkTypeface>(new SkTypeface_AndroidStream(std::move(data),
467 style, isFixedPitch, name));
bungeman41868fe2015-05-20 09:21:04 -0700468 }
469
Mike Reed59227392017-09-26 09:46:08 -0400470 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,
471 const SkFontArguments& args) const override {
bungemanf6c71072016-01-21 14:17:47 -0800472 using Scanner = SkTypeface_FreeType::Scanner;
bungemanf6c71072016-01-21 14:17:47 -0800473 bool isFixedPitch;
474 SkFontStyle style;
475 SkString name;
476 Scanner::AxisDefinitions axisDefinitions;
Ben Wagnerfc497342017-02-24 11:15:26 -0500477 if (!fScanner.scanFont(stream.get(), args.getCollectionIndex(),
bungemanf93d7112016-09-16 06:24:20 -0700478 &name, &style, &isFixedPitch, &axisDefinitions))
bungemanf6c71072016-01-21 14:17:47 -0800479 {
480 return nullptr;
481 }
482
bungemanf6c71072016-01-21 14:17:47 -0800483 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
Ben Wagnerfc497342017-02-24 11:15:26 -0500484 Scanner::computeAxisValues(axisDefinitions, args.getVariationDesignPosition(),
485 axisValues, name);
bungemanf6c71072016-01-21 14:17:47 -0800486
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500487 auto data = std::make_unique<SkFontData>(std::move(stream), args.getCollectionIndex(),
bungemanf93d7112016-09-16 06:24:20 -0700488 axisValues.get(), axisDefinitions.count());
Mike Reed59227392017-09-26 09:46:08 -0400489 return sk_sp<SkTypeface>(new SkTypeface_AndroidStream(std::move(data),
490 style, isFixedPitch, name));
bungemanf6c71072016-01-21 14:17:47 -0800491 }
492
Mike Reed59227392017-09-26 09:46:08 -0400493 sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData> data) const override {
bungeman41868fe2015-05-20 09:21:04 -0700494 SkStreamAsset* stream(data->getStream());
495 bool isFixedPitch;
496 SkFontStyle style;
497 SkString name;
halcanary96fcdcc2015-08-27 07:41:13 -0700498 if (!fScanner.scanFont(stream, data->getIndex(), &name, &style, &isFixedPitch, nullptr)) {
499 return nullptr;
bungeman41868fe2015-05-20 09:21:04 -0700500 }
Mike Reed59227392017-09-26 09:46:08 -0400501 return sk_sp<SkTypeface>(new SkTypeface_AndroidStream(std::move(data),
502 style, isFixedPitch, name));
bungeman8d84c992014-07-24 08:05:09 -0700503 }
504
Mike Reed59227392017-09-26 09:46:08 -0400505 sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override {
bsalomon49f085d2014-09-05 13:34:00 -0700506 if (familyName) {
halcanary96fcdcc2015-08-27 07:41:13 -0700507 // On Android, we must return nullptr when we can't find the requested
bungeman8d84c992014-07-24 08:05:09 -0700508 // named typeface so that the system/app can provide their own recovery
509 // mechanism. On other platforms we'd provide a typeface from the
510 // default family instead.
Mike Reed59227392017-09-26 09:46:08 -0400511 return sk_sp<SkTypeface>(this->onMatchFamilyStyle(familyName, style));
bungeman8d84c992014-07-24 08:05:09 -0700512 }
Mike Reed59227392017-09-26 09:46:08 -0400513 return sk_sp<SkTypeface>(fDefaultStyleSet->matchStyle(style));
bungeman8d84c992014-07-24 08:05:09 -0700514 }
515
516
517private:
518
bungeman14df8332014-10-28 15:07:23 -0700519 SkTypeface_FreeType::Scanner fScanner;
520
Brian Salomon343553a2018-09-05 15:41:23 -0400521 SkTArray<sk_sp<SkFontStyleSet_Android>> fStyleSets;
bungeman83b24ff2016-08-19 05:03:26 -0700522 sk_sp<SkFontStyleSet> fDefaultStyleSet;
bungeman8d84c992014-07-24 08:05:09 -0700523
bungeman83b24ff2016-08-19 05:03:26 -0700524 SkTArray<NameToFamily, true> fNameToFamilyMap;
525 SkTArray<NameToFamily, true> fFallbackNameToFamilyMap;
bungeman8d84c992014-07-24 08:05:09 -0700526
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500527 void addFamily(FontFamily& family, const bool isolated, int familyIndex) {
528 SkTArray<NameToFamily, true>* nameToFamily = &fNameToFamilyMap;
529 if (family.fIsFallbackFont) {
530 nameToFamily = &fFallbackNameToFamilyMap;
531
532 if (0 == family.fNames.count()) {
533 SkString& fallbackName = family.fNames.push_back();
534 fallbackName.printf("%.2x##fallback", familyIndex);
535 }
536 }
537
538 sk_sp<SkFontStyleSet_Android> newSet =
539 sk_make_sp<SkFontStyleSet_Android>(family, fScanner, isolated);
540 if (0 == newSet->count()) {
541 return;
542 }
543
544 for (const SkString& name : family.fNames) {
545 nameToFamily->emplace_back(NameToFamily{name, newSet.get()});
546 }
547 fStyleSets.emplace_back(std::move(newSet));
548 }
khushalsagarebc465b2016-02-12 12:42:48 -0800549 void buildNameToFamilyMap(SkTDArray<FontFamily*> families, const bool isolated) {
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500550 int familyIndex = 0;
551 for (FontFamily* family : families) {
552 addFamily(*family, isolated, familyIndex++);
553 family->fallbackFamilies.foreach([this, isolated, &familyIndex]
554 (SkString, std::unique_ptr<FontFamily>* fallbackFamily) {
555 addFamily(*(*fallbackFamily).get(), isolated, familyIndex++);
bungeman65fcd3d2014-08-06 11:12:20 -0700556 }
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500557 );
bungeman8d84c992014-07-24 08:05:09 -0700558 }
559 }
560
bungeman83b24ff2016-08-19 05:03:26 -0700561 void findDefaultStyleSet() {
562 SkASSERT(!fStyleSets.empty());
bungeman8d84c992014-07-24 08:05:09 -0700563
bungeman83b24ff2016-08-19 05:03:26 -0700564 static const char* defaultNames[] = { "sans-serif" };
565 for (const char* defaultName : defaultNames) {
566 fDefaultStyleSet.reset(this->onMatchFamily(defaultName));
567 if (fDefaultStyleSet) {
568 break;
bungeman8d84c992014-07-24 08:05:09 -0700569 }
bungeman8d84c992014-07-24 08:05:09 -0700570 }
bungeman83b24ff2016-08-19 05:03:26 -0700571 if (nullptr == fDefaultStyleSet) {
572 fDefaultStyleSet = fStyleSets[0];
bungeman8d84c992014-07-24 08:05:09 -0700573 }
bungeman83b24ff2016-08-19 05:03:26 -0700574 SkASSERT(fDefaultStyleSet);
bungeman8d84c992014-07-24 08:05:09 -0700575 }
576
577 typedef SkFontMgr INHERITED;
578};
579
bungeman7fa87cd2015-02-06 07:59:19 -0800580#ifdef SK_DEBUG
581static char const * const gSystemFontUseStrings[] = {
582 "OnlyCustom", "PreferCustom", "PreferSystem"
583};
584#endif
Ben Wagner20d031a2017-01-11 13:54:39 -0500585
Ben Wagner3546ff12017-01-03 13:32:36 -0500586sk_sp<SkFontMgr> SkFontMgr_New_Android(const SkFontMgr_Android_CustomFonts* custom) {
bungeman7fa87cd2015-02-06 07:59:19 -0800587 if (custom) {
588 SkASSERT(0 <= custom->fSystemFontUse);
589 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings));
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400590 SkDEBUGF("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n",
591 gSystemFontUseStrings[custom->fSystemFontUse],
592 custom->fBasePath,
593 custom->fFontsXml,
594 custom->fFallbackFontsXml);
bungeman4e3523c2014-08-08 12:06:51 -0700595 }
Ben Wagner3546ff12017-01-03 13:32:36 -0500596 return sk_make_sp<SkFontMgr_Android>(custom);
bungeman8d84c992014-07-24 08:05:09 -0700597}