1 // Copyright 2019 Google LLC.
2 #include "modules/skparagraph/include/TypefaceFontProvider.h"
3 #include <algorithm>
4 #include "include/core/SkFontMgr.h"
5 #include "include/core/SkString.h"
6 #include "include/core/SkTypeface.h"
7 #include "src/core/SkFontDescriptor.h"
8
9 namespace skia {
10 namespace textlayout {
11
onCountFamilies() const12 int TypefaceFontProvider::onCountFamilies() const { return fRegisteredFamilies.count(); }
13
onGetFamilyName(int index,SkString * familyName) const14 void TypefaceFontProvider::onGetFamilyName(int index, SkString* familyName) const {
15 SkASSERT(index < fRegisteredFamilies.count());
16 familyName->set(fFamilyNames[index]);
17 }
18
onMatchFamily(const char familyName[]) const19 SkFontStyleSet* TypefaceFontProvider::onMatchFamily(const char familyName[]) const {
20 auto found = fRegisteredFamilies.find(SkString(familyName));
21 if (found) {
22 return SkRef((*found).get());
23 }
24 return nullptr;
25 }
26
onMakeFromFontData(std::unique_ptr<SkFontData>) const27 sk_sp<SkTypeface> TypefaceFontProvider::onMakeFromFontData(std::unique_ptr<SkFontData>) const {
28 return nullptr;
29 }
30
registerTypeface(sk_sp<SkTypeface> typeface)31 size_t TypefaceFontProvider::registerTypeface(sk_sp<SkTypeface> typeface) {
32 if (typeface == nullptr) {
33 return 0;
34 }
35
36 SkString familyName;
37 typeface->getFamilyName(&familyName);
38
39 return registerTypeface(std::move(typeface), std::move(familyName));
40 }
41
registerTypeface(sk_sp<SkTypeface> typeface,const SkString & familyName)42 size_t TypefaceFontProvider::registerTypeface(sk_sp<SkTypeface> typeface, const SkString& familyName) {
43 if (familyName.size() == 0) {
44 return 0;
45 }
46
47 auto found = fRegisteredFamilies.find(familyName);
48 if (found == nullptr) {
49 found = fRegisteredFamilies.set(familyName, sk_make_sp<TypefaceFontStyleSet>(familyName));
50 fFamilyNames.emplace_back(familyName);
51 }
52
53 (*found)->appendTypeface(std::move(typeface));
54
55 return 1;
56 }
57
TypefaceFontStyleSet(const SkString & familyName)58 TypefaceFontStyleSet::TypefaceFontStyleSet(const SkString& familyName)
59 : fFamilyName(familyName) {}
60
count()61 int TypefaceFontStyleSet::count() { return fStyles.size(); }
62
getStyle(int index,SkFontStyle * style,SkString * name)63 void TypefaceFontStyleSet::getStyle(int index, SkFontStyle* style, SkString* name) {
64 SkASSERT(index < fStyles.count());
65 if (style) {
66 *style = fStyles[index]->fontStyle();
67 }
68 if (name) {
69 *name = fFamilyName;
70 }
71 }
72
createTypeface(int index)73 SkTypeface* TypefaceFontStyleSet::createTypeface(int index) {
74 SkASSERT(index < fStyles.count());
75 return SkRef(fStyles[index].get());
76 }
77
matchStyle(const SkFontStyle & pattern)78 SkTypeface* TypefaceFontStyleSet::matchStyle(const SkFontStyle& pattern) {
79 return this->matchStyleCSS3(pattern);
80 }
81
appendTypeface(sk_sp<SkTypeface> typeface)82 void TypefaceFontStyleSet::appendTypeface(sk_sp<SkTypeface> typeface) {
83 if (typeface.get() != nullptr) {
84 fStyles.emplace_back(std::move(typeface));
85 }
86 }
87
88 } // namespace textlayout
89 } // namespace skia
90