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
8#include "include/core/SkTypes.h"
9
10#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"
21#include "include/private/SkTemplates.h"
22#include "src/core/SkFontDescriptor.h"
23#include "src/core/SkOSFile.h"
24#include "src/core/SkTSearch.h"
25#include "src/core/SkTypefaceCache.h"
26#include "src/ports/SkFontHost_FreeType_common.h"
27#include "src/ports/SkFontMgr_android_parser.h"
28
29#include <algorithm>
30#include <limits>
31
32class SkData;
33
34class SkTypeface_Android : public SkTypeface_FreeType {
35public:
36 SkTypeface_Android(const SkFontStyle& style,
37 bool isFixedPitch,
38 const SkString& familyName)
39 : INHERITED(style, isFixedPitch)
40 , fFamilyName(familyName)
41 { }
42
43protected:
44 void onGetFamilyName(SkString* familyName) const override {
45 *familyName = fFamilyName;
46 }
47
48 SkString fFamilyName;
49
50private:
51 typedef SkTypeface_FreeType INHERITED;
52};
53
54class SkTypeface_AndroidSystem : public SkTypeface_Android {
55public:
56 SkTypeface_AndroidSystem(const SkString& pathName,
57 const bool cacheFontFiles,
58 int index,
59 const SkFixed* axes, int axesCount,
60 const SkFontStyle& style,
61 bool isFixedPitch,
62 const SkString& familyName,
63 const SkTArray<SkLanguage, true>& lang,
64 FontVariant variantStyle)
65 : INHERITED(style, isFixedPitch, familyName)
66 , fPathName(pathName)
67 , fIndex(index)
68 , fAxes(axes, axesCount)
69 , fLang(lang)
70 , fVariantStyle(variantStyle)
71 , fFile(cacheFontFiles ? sk_fopen(fPathName.c_str(), kRead_SkFILE_Flag) : nullptr) {
72 if (cacheFontFiles) {
73 SkASSERT(fFile);
74 }
75 }
76
77 std::unique_ptr<SkStreamAsset> makeStream() const {
78 if (fFile) {
79 sk_sp<SkData> data(SkData::MakeFromFILE(fFile));
80 return data ? std::make_unique<SkMemoryStream>(std::move(data)) : nullptr;
81 }
82 return SkStream::MakeFromFile(fPathName.c_str());
83 }
84
85 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const override {
86 SkASSERT(desc);
87 SkASSERT(serialize);
88 desc->setFamilyName(fFamilyName.c_str());
89 desc->setStyle(this->fontStyle());
90 *serialize = false;
91 }
92 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override {
93 *ttcIndex = fIndex;
94 return this->makeStream();
95 }
96 std::unique_ptr<SkFontData> onMakeFontData() const override {
97 return std::make_unique<SkFontData>(this->makeStream(), fIndex,
98 fAxes.begin(), fAxes.count());
99 }
100 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 }
116
117 const SkString fPathName;
118 int fIndex;
119 const SkSTArray<4, SkFixed, true> fAxes;
120 const SkSTArray<4, SkLanguage, true> fLang;
121 const FontVariant fVariantStyle;
122 SkAutoTCallVProc<FILE, sk_fclose> fFile;
123
124 typedef SkTypeface_Android INHERITED;
125};
126
127class SkTypeface_AndroidStream : public SkTypeface_Android {
128public:
129 SkTypeface_AndroidStream(std::unique_ptr<SkFontData> data,
130 const SkFontStyle& style,
131 bool isFixedPitch,
132 const SkString& familyName)
133 : INHERITED(style, isFixedPitch, familyName)
134 , fData(std::move(data))
135 { }
136
137 virtual void onGetFontDescriptor(SkFontDescriptor* desc,
138 bool* serialize) const override {
139 SkASSERT(desc);
140 SkASSERT(serialize);
141 desc->setFamilyName(fFamilyName.c_str());
142 *serialize = true;
143 }
144
145 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override {
146 *ttcIndex = fData->getIndex();
147 return fData->getStream()->duplicate();
148 }
149
150 std::unique_ptr<SkFontData> onMakeFontData() const override {
151 return std::make_unique<SkFontData>(*fData);
152 }
153
154 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
165private:
166 const std::unique_ptr<const SkFontData> fData;
167 typedef SkTypeface_Android INHERITED;
168};
169
170class SkFontStyleSet_Android : public SkFontStyleSet {
171 typedef SkTypeface_FreeType::Scanner Scanner;
172
173public:
174 explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& scanner,
175 const bool cacheFontFiles) {
176 const SkString* cannonicalFamilyName = nullptr;
177 if (family.fNames.count() > 0) {
178 cannonicalFamilyName = &family.fNames[0];
179 }
180 fFallbackFor = family.fFallbackFor;
181
182 // TODO? make this lazy
183 for (int i = 0; i < family.fFonts.count(); ++i) {
184 const FontFileInfo& fontFile = family.fFonts[i];
185
186 SkString pathName(family.fBasePath);
187 pathName.append(fontFile.fFileName);
188
189 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(pathName.c_str());
190 if (!stream) {
191 SkDEBUGF("Requested font file %s does not exist or cannot be opened.\n",
192 pathName.c_str());
193 continue;
194 }
195
196 const int ttcIndex = fontFile.fIndex;
197 SkString familyName;
198 SkFontStyle style;
199 bool isFixedWidth;
200 Scanner::AxisDefinitions axisDefinitions;
201 if (!scanner.scanFont(stream.get(), ttcIndex,
202 &familyName, &style, &isFixedWidth, &axisDefinitions))
203 {
204 SkDEBUGF("Requested font file %s exists, but is not a valid font.\n",
205 pathName.c_str());
206 continue;
207 }
208
209 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;
216 }
217 style = SkFontStyle(weight, style.width(), slant);
218
219 uint32_t variant = family.fVariant;
220 if (kDefault_FontVariant == variant) {
221 variant = kCompact_FontVariant | kElegant_FontVariant;
222 }
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.
227 if (cannonicalFamilyName != nullptr) {
228 familyName = *cannonicalFamilyName;
229 }
230
231 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
232 SkFontArguments::VariationPosition position = {
233 fontFile.fVariationDesignPosition.begin(),
234 fontFile.fVariationDesignPosition.count()
235 };
236 Scanner::computeAxisValues(axisDefinitions, position,
237 axisValues, familyName);
238
239 fStyles.push_back().reset(new SkTypeface_AndroidSystem(
240 pathName, cacheFontFiles, ttcIndex, axisValues.get(), axisDefinitions.count(),
241 style, isFixedWidth, familyName, family.fLanguages, variant));
242 }
243 }
244
245 int count() override {
246 return fStyles.count();
247 }
248 void getStyle(int index, SkFontStyle* style, SkString* name) override {
249 if (index < 0 || fStyles.count() <= index) {
250 return;
251 }
252 if (style) {
253 *style = fStyles[index]->fontStyle();
254 }
255 if (name) {
256 name->reset();
257 }
258 }
259 SkTypeface_AndroidSystem* createTypeface(int index) override {
260 if (index < 0 || fStyles.count() <= index) {
261 return nullptr;
262 }
263 return SkRef(fStyles[index].get());
264 }
265
266 SkTypeface_AndroidSystem* matchStyle(const SkFontStyle& pattern) override {
267 return static_cast<SkTypeface_AndroidSystem*>(this->matchStyleCSS3(pattern));
268 }
269
270private:
271 SkTArray<sk_sp<SkTypeface_AndroidSystem>> fStyles;
272 SkString fFallbackFor;
273
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:
292 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);
296 SkFontMgr_Android_Parser::GetCustomFontFamilies(
297 families, base, custom->fFontsXml, custom->fFallbackFontsXml);
298 }
299 if (!custom ||
300 (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSystemFontUse))
301 {
302 SkFontMgr_Android_Parser::GetSystemFontFamilies(families);
303 }
304 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem == custom->fSystemFontUse) {
305 SkString base(custom->fBasePath);
306 SkFontMgr_Android_Parser::GetCustomFontFamilies(
307 families, base, custom->fFontsXml, custom->fFallbackFontsXml);
308 }
309 this->buildNameToFamilyMap(families, custom ? custom->fIsolated : false);
310 this->findDefaultStyleSet();
311 families.deleteAll();
312 }
313
314protected:
315 /** Returns not how many families we have, but how many unique names
316 * exist among the families.
317 */
318 int onCountFamilies() const override {
319 return fNameToFamilyMap.count();
320 }
321
322 void onGetFamilyName(int index, SkString* familyName) const override {
323 if (index < 0 || fNameToFamilyMap.count() <= index) {
324 familyName->reset();
325 return;
326 }
327 familyName->set(fNameToFamilyMap[index].name);
328 }
329
330 SkFontStyleSet* onCreateStyleSet(int index) const override {
331 if (index < 0 || fNameToFamilyMap.count() <= index) {
332 return nullptr;
333 }
334 return SkRef(fNameToFamilyMap[index].styleSet);
335 }
336
337 SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
338 if (!familyName) {
339 return nullptr;
340 }
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 }
347 // 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 }
353 return nullptr;
354 }
355
356 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
357 const SkFontStyle& style) const override {
358 sk_sp<SkFontStyleSet> sset(this->matchFamily(familyName));
359 return sset->matchStyle(style);
360 }
361
362 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* typeface,
363 const SkFontStyle& style) const override {
364 for (int i = 0; i < fStyleSets.count(); ++i) {
365 for (int j = 0; j < fStyleSets[i]->fStyles.count(); ++j) {
366 if (fStyleSets[i]->fStyles[j].get() == typeface) {
367 return fStyleSets[i]->matchStyle(style);
368 }
369 }
370 }
371 return nullptr;
372 }
373
374 static sk_sp<SkTypeface_AndroidSystem> find_family_style_character(
375 const SkString& familyName,
376 const SkTArray<NameToFamily, true>& fallbackNameToFamilyMap,
377 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;
382 if (familyName != family->fFallbackFor) {
383 continue;
384 }
385 sk_sp<SkTypeface_AndroidSystem> face(family->matchStyle(style));
386
387 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 {
392 continue;
393 }
394
395 if (SkToBool(face->fVariantStyle & kElegant_FontVariant) != elegant) {
396 continue;
397 }
398
399 if (face->unicharToGlyph(character) != 0) {
400 return face;
401 }
402 }
403 return nullptr;
404 }
405
406 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
407 const SkFontStyle& style,
408 const char* bcp47[],
409 int bcp47Count,
410 SkUnichar character) const override
411 {
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
417 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 }
431
432 lang = lang.getParent();
433 }
434 }
435 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 }
442 }
443 }
444 return nullptr;
445 }
446
447 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);
450 }
451
452 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
453 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
454 return stream.get() ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr;
455 }
456
457 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,
458 int ttcIndex) const override {
459 bool isFixedPitch;
460 SkFontStyle style;
461 SkString name;
462 if (!fScanner.scanFont(stream.get(), ttcIndex, &name, &style, &isFixedPitch, nullptr)) {
463 return nullptr;
464 }
465 auto data = std::make_unique<SkFontData>(std::move(stream), ttcIndex, nullptr, 0);
466 return sk_sp<SkTypeface>(new SkTypeface_AndroidStream(std::move(data),
467 style, isFixedPitch, name));
468 }
469
470 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,
471 const SkFontArguments& args) const override {
472 using Scanner = SkTypeface_FreeType::Scanner;
473 bool isFixedPitch;
474 SkFontStyle style;
475 SkString name;
476 Scanner::AxisDefinitions axisDefinitions;
477 if (!fScanner.scanFont(stream.get(), args.getCollectionIndex(),
478 &name, &style, &isFixedPitch, &axisDefinitions))
479 {
480 return nullptr;
481 }
482
483 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
484 Scanner::computeAxisValues(axisDefinitions, args.getVariationDesignPosition(),
485 axisValues, name);
486
487 auto data = std::make_unique<SkFontData>(std::move(stream), args.getCollectionIndex(),
488 axisValues.get(), axisDefinitions.count());
489 return sk_sp<SkTypeface>(new SkTypeface_AndroidStream(std::move(data),
490 style, isFixedPitch, name));
491 }
492
493 sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData> data) const override {
494 SkStreamAsset* stream(data->getStream());
495 bool isFixedPitch;
496 SkFontStyle style;
497 SkString name;
498 if (!fScanner.scanFont(stream, data->getIndex(), &name, &style, &isFixedPitch, nullptr)) {
499 return nullptr;
500 }
501 return sk_sp<SkTypeface>(new SkTypeface_AndroidStream(std::move(data),
502 style, isFixedPitch, name));
503 }
504
505 sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override {
506 if (familyName) {
507 // On Android, we must return nullptr when we can't find the requested
508 // 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.
511 return sk_sp<SkTypeface>(this->onMatchFamilyStyle(familyName, style));
512 }
513 return sk_sp<SkTypeface>(fDefaultStyleSet->matchStyle(style));
514 }
515
516
517private:
518
519 SkTypeface_FreeType::Scanner fScanner;
520
521 SkTArray<sk_sp<SkFontStyleSet_Android>> fStyleSets;
522 sk_sp<SkFontStyleSet> fDefaultStyleSet;
523
524 SkTArray<NameToFamily, true> fNameToFamilyMap;
525 SkTArray<NameToFamily, true> fFallbackNameToFamilyMap;
526
527 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 }
549 void buildNameToFamilyMap(SkTDArray<FontFamily*> families, const bool isolated) {
550 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++);
556 }
557 );
558 }
559 }
560
561 void findDefaultStyleSet() {
562 SkASSERT(!fStyleSets.empty());
563
564 static const char* defaultNames[] = { "sans-serif" };
565 for (const char* defaultName : defaultNames) {
566 fDefaultStyleSet.reset(this->onMatchFamily(defaultName));
567 if (fDefaultStyleSet) {
568 break;
569 }
570 }
571 if (nullptr == fDefaultStyleSet) {
572 fDefaultStyleSet = fStyleSets[0];
573 }
574 SkASSERT(fDefaultStyleSet);
575 }
576
577 typedef SkFontMgr INHERITED;
578};
579
580#ifdef SK_DEBUG
581static char const * const gSystemFontUseStrings[] = {
582 "OnlyCustom", "PreferCustom", "PreferSystem"
583};
584#endif
585
586sk_sp<SkFontMgr> SkFontMgr_New_Android(const SkFontMgr_Android_CustomFonts* custom) {
587 if (custom) {
588 SkASSERT(0 <= custom->fSystemFontUse);
589 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings));
590 SkDEBUGF("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n",
591 gSystemFontUseStrings[custom->fSystemFontUse],
592 custom->fBasePath,
593 custom->fFontsXml,
594 custom->fFallbackFontsXml);
595 }
596 return sk_make_sp<SkFontMgr_Android>(custom);
597}
598