aboutsummaryrefslogtreecommitdiffhomepage
path: root/dm/DMFontMgr.cpp
blob: d275c412c742b60653192ba8aa5a943a37b3e110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "DMFontMgr.h"
#include "SkFontDescriptor.h"
#include "sk_tool_utils.h"

namespace DM {

    static constexpr const char* kFamilyNames[] = {
        "Toy Liberation Sans",
        "Toy Liberation Serif",
        "Toy Liberation Mono",
    };

    FontStyleSet::FontStyleSet(int familyIndex) {
        using sk_tool_utils::create_portable_typeface;
        const char* familyName = kFamilyNames[familyIndex];

        fTypefaces[0] = create_portable_typeface(familyName, SkFontStyle::Normal());
        fTypefaces[1] = create_portable_typeface(familyName, SkFontStyle::Bold());
        fTypefaces[2] = create_portable_typeface(familyName, SkFontStyle::Italic());
        fTypefaces[3] = create_portable_typeface(familyName, SkFontStyle::BoldItalic());
    }

    int FontStyleSet::count() { return 4; }

    void FontStyleSet::getStyle(int index, SkFontStyle* style, SkString* name) {
        switch (index) {
            default:
            case  0: if (style) { *style = SkFontStyle::Normal(); }
                     if (name)  { *name = "Normal"; }
                     break;
            case  1: if (style) { *style = SkFontStyle::Bold(); }
                     if (name)  { *name = "Bold"; }
                     break;
            case  2: if (style) { *style = SkFontStyle::Italic(); }
                     if (name)  { *name = "Italic"; }
                     break;
            case  3: if (style) { *style = SkFontStyle::BoldItalic(); }
                     if (name)  { *name = "BoldItalic"; }
                     break;
        }
    }

    SkTypeface* FontStyleSet::createTypeface(int index) {
        return SkRef(fTypefaces[index].get());
    }

    SkTypeface* FontStyleSet::matchStyle(const SkFontStyle& pattern) {
        return this->matchStyleCSS3(pattern);
    }

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //

    FontMgr::FontMgr() {
        fFamilies[0] = sk_make_sp<FontStyleSet>(0);
        fFamilies[1] = sk_make_sp<FontStyleSet>(1);
        fFamilies[2] = sk_make_sp<FontStyleSet>(2);
    }

    int FontMgr::onCountFamilies() const { return SK_ARRAY_COUNT(fFamilies); }

    void FontMgr::onGetFamilyName(int index, SkString* familyName) const {
        *familyName = kFamilyNames[index];
    }

    SkFontStyleSet* FontMgr::onCreateStyleSet(int index) const {
        return SkRef(fFamilies[index].get());
    }

    SkFontStyleSet* FontMgr::onMatchFamily(const char familyName[]) const {
        if (familyName) {
            if (strstr(familyName,  "ans")) { return this->createStyleSet(0); }
            if (strstr(familyName, "erif")) { return this->createStyleSet(1); }
            if (strstr(familyName,  "ono")) { return this->createStyleSet(2); }
        }
        return this->createStyleSet(0);
    }

    SkTypeface* FontMgr::onMatchFamilyStyle(const char familyName[],
                                            const SkFontStyle& style) const {
        sk_sp<SkFontStyleSet> styleSet(this->matchFamily(familyName));
        return styleSet->matchStyle(style);
    }

    SkTypeface* FontMgr::onMatchFamilyStyleCharacter(const char familyName[],
                                                     const SkFontStyle& style,
                                                     const char* bcp47[],
                                                     int bcp47Count,
                                                     SkUnichar character) const {
        (void)bcp47;
        (void)bcp47Count;
        (void)character;
        return this->matchFamilyStyle(familyName, style);
    }

    SkTypeface* FontMgr::onMatchFaceStyle(const SkTypeface* tf,
                                          const SkFontStyle& style) const {
        SkString familyName;
        tf->getFamilyName(&familyName);
        return this->matchFamilyStyle(familyName.c_str(), style);
    }

    sk_sp<SkTypeface> FontMgr::onMakeFromData(sk_sp<SkData>, int ttcIndex) const {
        return nullptr;
    }

    sk_sp<SkTypeface> FontMgr::onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
                                                     int ttcIndex) const {
        return nullptr;
    }

    sk_sp<SkTypeface> FontMgr::onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
                                                    const SkFontArguments&) const {
        return nullptr;
    }

    sk_sp<SkTypeface> FontMgr::onMakeFromFontData(std::unique_ptr<SkFontData>) const {
        return nullptr;
    }

    sk_sp<SkTypeface> FontMgr::onMakeFromFile(const char path[], int ttcIndex) const {
        return nullptr;
    }

    sk_sp<SkTypeface> FontMgr::onLegacyMakeTypeface(const char familyName[],
                                                    SkFontStyle style) const {
        return sk_sp<SkTypeface>(this->matchFamilyStyle(familyName, style));
    }

} // namespace DM