• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * 2021.9.9 SkFontMgr on previewer of ohos.
7  *           Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved.
8  */
9 
10 #include "src/ports/SkFontMgr_preview.h"
11 
12 #include "src/core/SkTSearch.h"
13 
SkFontMgr_Preview()14 SkFontMgr_Preview::SkFontMgr_Preview()
15 {
16     SkTDArray<FontFamily*> families;
17     SkFontMgr_Config_Parser::GetInstance().GetSystemFontFamilies(families);
18     this->buildNameToFamilyMap(families);
19     this->findDefaultStyleSet();
20     families.deleteAll();
21 }
22 
onCountFamilies() const23 int SkFontMgr_Preview::onCountFamilies() const
24 {
25     return fNameToFamilyMap.count();
26 }
27 
onGetFamilyName(int index,SkString * familyName) const28 void SkFontMgr_Preview::onGetFamilyName(int index, SkString* familyName) const
29 {
30     if (index < 0 || fNameToFamilyMap.count() <= index) {
31         familyName->reset();
32         return;
33     }
34     familyName->set(fNameToFamilyMap[index].name);
35 }
36 
onCreateStyleSet(int index) const37 SkFontStyleSet* SkFontMgr_Preview::onCreateStyleSet(int index) const
38 {
39     if (index < 0 || fNameToFamilyMap.count() <= index) {
40         return nullptr;
41     }
42     return SkRef(fNameToFamilyMap[index].styleSet);
43 }
44 
onMatchFamily(const char familyName[]) const45 SkFontStyleSet* SkFontMgr_Preview::onMatchFamily(const char familyName[]) const
46 {
47     if (!familyName) {
48         return nullptr;
49     }
50     SkAutoAsciiToLC tolc(familyName);
51     for (int i = 0; i < fNameToFamilyMap.count(); ++i) {
52         if (fNameToFamilyMap[i].name.equals(tolc.lc())) {
53             return SkRef(fNameToFamilyMap[i].styleSet);
54         }
55     }
56     // TODO: eventually we should not need to name fallback families.
57     for (int i = 0; i < fFallbackNameToFamilyMap.count(); ++i) {
58         if (fFallbackNameToFamilyMap[i].name.equals(tolc.lc())) {
59             return SkRef(fFallbackNameToFamilyMap[i].styleSet);
60         }
61     }
62     return nullptr;
63 }
64 
onMatchFamilyStyle(const char familyName[],const SkFontStyle & style) const65 SkTypeface* SkFontMgr_Preview::onMatchFamilyStyle(const char familyName[], const SkFontStyle& style) const
66 {
67     sk_sp<SkFontStyleSet> sset(this->matchFamily(familyName));
68     return sset->matchStyle(style);
69 }
70 
onMatchFaceStyle(const SkTypeface * typeface,const SkFontStyle & style) const71 SkTypeface* SkFontMgr_Preview::onMatchFaceStyle(const SkTypeface* typeface, const SkFontStyle& style) const
72 {
73     for (int i = 0; i < fStyleSets.count(); ++i) {
74         for (int j = 0; j < fStyleSets[i]->fStyles.count(); ++j) {
75             if (fStyleSets[i]->fStyles[j].get() == typeface) {
76                 return fStyleSets[i]->matchStyle(style);
77             }
78         }
79     }
80     return nullptr;
81 }
82 
find_family_style_character(const SkString & familyName,const SkTArray<NameToFamily,true> & fallbackNameToFamilyMap,const SkFontStyle & style,bool elegant,const SkString & langTag,SkUnichar character)83 sk_sp<SkTypeface_PreviewSystem> SkFontMgr_Preview::find_family_style_character(
84     const SkString& familyName,
85     const SkTArray<NameToFamily, true>& fallbackNameToFamilyMap,
86     const SkFontStyle& style, bool elegant,
87     const SkString& langTag, SkUnichar character)
88 {
89     for (int i = 0; i < fallbackNameToFamilyMap.count(); ++i) {
90         SkFontStyleSet_Preview* family = fallbackNameToFamilyMap[i].styleSet;
91         if (familyName != family->fFallbackFor) {
92             continue;
93         }
94         sk_sp<SkTypeface_PreviewSystem> face(family->matchStyle(style));
95 
96         if (!langTag.isEmpty() &&
97             std::none_of(face->fLang.begin(), face->fLang.end(), [&](SkLanguage lang) {
98                 return lang.getTag().startsWith(langTag.c_str()); })) {
99             continue;
100         }
101 
102         if (SkToBool(face->fVariantStyle & kElegant_FontVariant) != elegant) {
103             continue;
104         }
105 
106         if (face->unicharToGlyph(character) != 0) {
107             return face;
108         }
109     }
110     return nullptr;
111 }
112 
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character) const113 SkTypeface* SkFontMgr_Preview::onMatchFamilyStyleCharacter(const char familyName[],
114                                                        const SkFontStyle& style,
115                                                        const char* bcp47[],
116                                                        int bcp47Count,
117                                                        SkUnichar character) const
118 {
119     // The variant 'elegant' is 'not squashed', 'compact' is 'stays in ascent/descent'.
120     // The variant 'default' means 'compact and elegant'.
121     // As a result, it is not possible to know the variant context from the font alone.
122     // TODO: add 'is_elegant' and 'is_compact' bits to 'style' request.
123     SkString familyNameString(familyName);
124     for (const SkString& currentFamilyName : { familyNameString, SkString() }) {
125         // The first time match anything elegant, second time anything not elegant.
126         for (int elegant = 2; elegant-- > 0;) {
127             for (int bcp47Index = bcp47Count; bcp47Index-- > 0;) {
128                 SkLanguage lang(bcp47[bcp47Index]);
129                 while (!lang.getTag().isEmpty()) {
130                     sk_sp<SkTypeface_PreviewSystem> matchingTypeface =
131                         find_family_style_character(currentFamilyName, fFallbackNameToFamilyMap,
132                                                     style, SkToBool(elegant),
133                                                     lang.getTag(), character);
134                     if (matchingTypeface) {
135                         return matchingTypeface.release();
136                     }
137                     lang = lang.getParent();
138                 }
139             }
140             sk_sp<SkTypeface_PreviewSystem> matchingTypeface =
141                 find_family_style_character(currentFamilyName, fFallbackNameToFamilyMap,
142                                             style, SkToBool(elegant),
143                                             SkString(), character);
144             if (matchingTypeface) {
145                 return matchingTypeface.release();
146             }
147         }
148     }
149     return nullptr;
150 }
151 
onMakeFromData(sk_sp<SkData> data,int ttcIndex) const152 sk_sp<SkTypeface> SkFontMgr_Preview::onMakeFromData(sk_sp<SkData> data, int ttcIndex) const
153 {
154     return this->makeFromStream(std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data))),
155                                 ttcIndex);
156 }
157 
onMakeFromFile(const char path[],int ttcIndex) const158 sk_sp<SkTypeface> SkFontMgr_Preview::onMakeFromFile(const char path[], int ttcIndex) const
159 {
160     std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
161     return stream.get() ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr;
162 }
163 
onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,int ttcIndex) const164 sk_sp<SkTypeface> SkFontMgr_Preview::onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream, int ttcIndex) const
165 {
166     bool isFixedPitch;
167     SkFontStyle style;
168     SkString name;
169     if (!fScanner.scanFont(stream.get(), ttcIndex, &name, &style, &isFixedPitch, nullptr)) {
170         return nullptr;
171     }
172     auto data = skstd::make_unique<SkFontData>(std::move(stream), ttcIndex, nullptr, 0);
173     return sk_sp<SkTypeface>(new SkTypeface_PreviewStream(std::move(data), style, isFixedPitch, name));
174 }
175 
onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,const SkFontArguments & args) const176 sk_sp<SkTypeface> SkFontMgr_Preview::onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,
177                                                       const SkFontArguments& args) const
178 {
179     using Scanner = SkTypeface_FreeType::Scanner;
180     bool isFixedPitch;
181     SkFontStyle style;
182     SkString name;
183     Scanner::AxisDefinitions axisDefinitions;
184     if (!fScanner.scanFont(stream.get(), args.getCollectionIndex(),
185                            &name, &style, &isFixedPitch, &axisDefinitions)) {
186         return nullptr;
187     }
188     SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
189     Scanner::computeAxisValues(axisDefinitions, args.getVariationDesignPosition(),
190                                axisValues, name);
191     auto data = skstd::make_unique<SkFontData>(std::move(stream), args.getCollectionIndex(),
192                                                axisValues.get(), axisDefinitions.count());
193     return sk_sp<SkTypeface>(new SkTypeface_PreviewStream(std::move(data), style, isFixedPitch, name));
194 }
195 
onMakeFromFontData(std::unique_ptr<SkFontData> data) const196 sk_sp<SkTypeface> SkFontMgr_Preview::onMakeFromFontData(std::unique_ptr<SkFontData> data) const
197 {
198     SkStreamAsset* stream(data->getStream());
199     bool isFixedPitch;
200     SkFontStyle style;
201     SkString name;
202     if (!fScanner.scanFont(stream, data->getIndex(), &name, &style, &isFixedPitch, nullptr)) {
203         return nullptr;
204     }
205     return sk_sp<SkTypeface>(new SkTypeface_PreviewStream(std::move(data), style, isFixedPitch, name));
206 }
207 
onLegacyMakeTypeface(const char familyName[],SkFontStyle style) const208 sk_sp<SkTypeface> SkFontMgr_Preview::onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const
209 {
210     if (familyName) {
211         return sk_sp<SkTypeface>(this->onMatchFamilyStyle(familyName, style));
212     }
213     return sk_sp<SkTypeface>(fDefaultStyleSet->matchStyle(style));
214 }
215 
addFamily(FontFamily & family,int familyIndex)216 void SkFontMgr_Preview::addFamily(FontFamily& family, int familyIndex)
217 {
218     SkTArray<NameToFamily, true>* nameToFamily = &fNameToFamilyMap;
219     if (family.fIsFallbackFont) {
220         nameToFamily = &fFallbackNameToFamilyMap;
221         if (0 == family.fNames.count()) {
222             SkString& fallbackName = family.fNames.push_back();
223             fallbackName.printf("%.2x##fallback", familyIndex);
224         }
225     }
226     sk_sp<SkFontStyleSet_Preview> newSet =
227         sk_make_sp<SkFontStyleSet_Preview>(family, fScanner);
228     if (0 == newSet->count()) {
229         return;
230     }
231     for (const SkString& name : family.fNames) {
232         nameToFamily->emplace_back(NameToFamily { name, newSet.get() });
233     }
234     fStyleSets.emplace_back(std::move(newSet));
235 }
236 
buildNameToFamilyMap(SkTDArray<FontFamily * > families)237 void SkFontMgr_Preview::buildNameToFamilyMap(SkTDArray<FontFamily*> families)
238 {
239     int familyIndex = 0;
240     for (FontFamily* family : families) {
241         addFamily(*family, familyIndex++);
242         family->fallbackFamilies.foreach([this, &familyIndex]
243             (SkString, std::unique_ptr<FontFamily>* fallbackFamily) {
244                 addFamily(*(*fallbackFamily).get(), familyIndex++);
245             }
246         );
247     }
248 }
249 
findDefaultStyleSet()250 void SkFontMgr_Preview::findDefaultStyleSet()
251 {
252     SkASSERT(!fStyleSets.empty());
253     static const char* defaultNames[] = { "sans-serif" };
254     for (const char* defaultName : defaultNames) {
255         fDefaultStyleSet.reset(this->onMatchFamily(defaultName));
256         if (fDefaultStyleSet) {
257             break;
258         }
259     }
260     if (nullptr == fDefaultStyleSet) {
261         fDefaultStyleSet = fStyleSets[0];
262     }
263     SkASSERT(fDefaultStyleSet);
264 }
265 
SkFontMgr_New_Preview()266 sk_sp<SkFontMgr> SkFontMgr_New_Preview()
267 {
268     return sk_make_sp<SkFontMgr_Preview>();
269 }