xref: /aosp_15_r20/external/skia/src/utils/SkOrderedFontMgr.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 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/utils/SkOrderedFontMgr.h"
9 
10 #include "include/core/SkData.h" // IWYU pragma: keep
11 #include "include/core/SkFontStyle.h"
12 #include "include/core/SkStream.h" // IWYU pragma: keep
13 #include "include/core/SkTypeface.h" // IWYU pragma: keep
14 
15 #include <utility>
16 
17 class SkString;
18 struct SkFontArguments;
19 
SkOrderedFontMgr()20 SkOrderedFontMgr::SkOrderedFontMgr() {}
~SkOrderedFontMgr()21 SkOrderedFontMgr::~SkOrderedFontMgr() {}
22 
append(sk_sp<SkFontMgr> fm)23 void SkOrderedFontMgr::append(sk_sp<SkFontMgr> fm) {
24     fList.push_back(std::move(fm));
25 }
26 
onCountFamilies() const27 int SkOrderedFontMgr::onCountFamilies() const {
28     int count = 0;
29     for (const auto& fm : fList) {
30         count += fm->countFamilies();
31     }
32     return count;
33 }
34 
onGetFamilyName(int index,SkString * familyName) const35 void SkOrderedFontMgr::onGetFamilyName(int index, SkString* familyName) const {
36     for (const auto& fm : fList) {
37         const int count = fm->countFamilies();
38         if (index < count) {
39             return fm->getFamilyName(index, familyName);
40         }
41         index -= count;
42     }
43 }
44 
onCreateStyleSet(int index) const45 sk_sp<SkFontStyleSet> SkOrderedFontMgr::onCreateStyleSet(int index) const {
46     for (const auto& fm : fList) {
47         const int count = fm->countFamilies();
48         if (index < count) {
49             return fm->createStyleSet(index);
50         }
51         index -= count;
52     }
53     return nullptr;
54 }
55 
onMatchFamily(const char familyName[]) const56 sk_sp<SkFontStyleSet> SkOrderedFontMgr::onMatchFamily(const char familyName[]) const {
57     for (const auto& fm : fList) {
58         if (auto fs = fm->matchFamily(familyName)) {
59             return fs;
60         }
61     }
62     return nullptr;
63 }
64 
onMatchFamilyStyle(const char family[],const SkFontStyle & style) const65 sk_sp<SkTypeface> SkOrderedFontMgr::onMatchFamilyStyle(const char family[],
66                                                        const SkFontStyle& style) const {
67     for (const auto& fm : fList) {
68         if (auto tf = fm->matchFamilyStyle(family, style)) {
69             return tf;
70         }
71     }
72     return nullptr;
73 }
74 
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar uni) const75 sk_sp<SkTypeface> SkOrderedFontMgr::onMatchFamilyStyleCharacter(
76     const char familyName[], const SkFontStyle& style,
77     const char* bcp47[], int bcp47Count,
78     SkUnichar uni) const
79 {
80     for (const auto& fm : fList) {
81         if (auto tf = fm->matchFamilyStyleCharacter(familyName, style, bcp47, bcp47Count, uni)) {
82             return tf;
83         }
84     }
85     return nullptr;
86 }
87 
88 // All of these are defined to fail by returning null
89 
onMakeFromData(sk_sp<SkData>,int ttcIndex) const90 sk_sp<SkTypeface> SkOrderedFontMgr::onMakeFromData(sk_sp<SkData>, int ttcIndex) const {
91     return nullptr;
92 }
93 
onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,int ttcIndex) const94 sk_sp<SkTypeface> SkOrderedFontMgr::onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
95                                                           int ttcIndex) const {
96     return nullptr;
97 }
98 
onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,const SkFontArguments &) const99 sk_sp<SkTypeface> SkOrderedFontMgr::onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
100                                                          const SkFontArguments&) const {
101     return nullptr;
102 }
103 
onMakeFromFile(const char path[],int ttcIndex) const104 sk_sp<SkTypeface> SkOrderedFontMgr::onMakeFromFile(const char path[], int ttcIndex) const {
105     return nullptr;
106 }
107 
onLegacyMakeTypeface(const char family[],SkFontStyle) const108 sk_sp<SkTypeface> SkOrderedFontMgr::onLegacyMakeTypeface(const char family[], SkFontStyle) const {
109     return nullptr;
110 }
111