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