aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/ports/SkFontMgr_custom.cpp
blob: 91a590827c0defb8c97430280e5f180ca5258966 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * Copyright 2006 The Android Open Source Project
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkFontArguments.h"
#include "SkFontDescriptor.h"
#include "SkFontHost_FreeType_common.h"
#include "SkFontMgr.h"
#include "SkFontMgr_custom.h"
#include "SkFontStyle.h"
#include "SkMakeUnique.h"
#include "SkRefCnt.h"
#include "SkStream.h"
#include "SkString.h"
#include "SkTArray.h"
#include "SkTemplates.h"
#include "SkTypeface.h"
#include "SkTypes.h"

#include <limits>
#include <memory>

class SkData;

SkTypeface_Custom::SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch,
                                     bool sysFont, const SkString familyName, int index)
    : INHERITED(style, isFixedPitch)
    , fIsSysFont(sysFont), fFamilyName(familyName), fIndex(index)
{ }

bool SkTypeface_Custom::isSysFont() const { return fIsSysFont; }

void SkTypeface_Custom::onGetFamilyName(SkString* familyName) const {
    *familyName = fFamilyName;
}

void SkTypeface_Custom::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
    desc->setFamilyName(fFamilyName.c_str());
    desc->setStyle(this->fontStyle());
    *isLocal = !this->isSysFont();
}

int SkTypeface_Custom::getIndex() const { return fIndex; }


SkTypeface_Empty::SkTypeface_Empty() : INHERITED(SkFontStyle(), false, true, SkString(), 0) {}

SkStreamAsset* SkTypeface_Empty::onOpenStream(int*) const { return nullptr; }


SkTypeface_Stream::SkTypeface_Stream(std::unique_ptr<SkFontData> fontData,
                                     const SkFontStyle& style, bool isFixedPitch, bool sysFont,
                                     const SkString familyName)
    : INHERITED(style, isFixedPitch, sysFont, familyName, fontData->getIndex())
    , fData(std::move(fontData))
{ }

SkStreamAsset* SkTypeface_Stream::onOpenStream(int* ttcIndex) const {
    *ttcIndex = fData->getIndex();
    return fData->getStream()->duplicate();
}

std::unique_ptr<SkFontData> SkTypeface_Stream::onMakeFontData() const {
    return skstd::make_unique<SkFontData>(*fData);
}


SkTypeface_File::SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
                                 const SkString familyName, const char path[], int index)
    : INHERITED(style, isFixedPitch, sysFont, familyName, index)
    , fPath(path)
{ }

SkStreamAsset* SkTypeface_File::onOpenStream(int* ttcIndex) const {
    *ttcIndex = this->getIndex();
    return SkStream::MakeFromFile(fPath.c_str()).release();
}

///////////////////////////////////////////////////////////////////////////////

SkFontStyleSet_Custom::SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(familyName) {}

void SkFontStyleSet_Custom::appendTypeface(sk_sp<SkTypeface_Custom> typeface) {
    fStyles.emplace_back(std::move(typeface));
}

int SkFontStyleSet_Custom::count() {
    return fStyles.count();
}

void SkFontStyleSet_Custom::getStyle(int index, SkFontStyle* style, SkString* name) {
    SkASSERT(index < fStyles.count());
    if (style) {
        *style = fStyles[index]->fontStyle();
    }
    if (name) {
        name->reset();
    }
}

SkTypeface* SkFontStyleSet_Custom::createTypeface(int index) {
    SkASSERT(index < fStyles.count());
    return SkRef(fStyles[index].get());
}

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

SkString SkFontStyleSet_Custom::getFamilyName() { return fFamilyName; }


SkFontMgr_Custom::SkFontMgr_Custom(const SystemFontLoader& loader) : fDefaultFamily(nullptr) {
    loader.loadSystemFonts(fScanner, &fFamilies);

    // Try to pick a default font.
    static const char* defaultNames[] = {
        "Arial", "Verdana", "Times New Roman", "Droid Sans", nullptr
    };
    for (size_t i = 0; i < SK_ARRAY_COUNT(defaultNames); ++i) {
        sk_sp<SkFontStyleSet_Custom> set(this->onMatchFamily(defaultNames[i]));
        if (nullptr == set) {
            continue;
        }

        sk_sp<SkTypeface> tf(set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight,
                                                         SkFontStyle::kNormal_Width,
                                                         SkFontStyle::kUpright_Slant)));
        if (nullptr == tf) {
            continue;
        }

        fDefaultFamily = set.get();
        break;
    }
    if (nullptr == fDefaultFamily) {
        fDefaultFamily = fFamilies[0].get();
    }
}

int SkFontMgr_Custom::onCountFamilies() const {
    return fFamilies.count();
}

void SkFontMgr_Custom::onGetFamilyName(int index, SkString* familyName) const {
    SkASSERT(index < fFamilies.count());
    familyName->set(fFamilies[index]->getFamilyName());
}

SkFontStyleSet_Custom* SkFontMgr_Custom::onCreateStyleSet(int index) const {
    SkASSERT(index < fFamilies.count());
    return SkRef(fFamilies[index].get());
}

SkFontStyleSet_Custom* SkFontMgr_Custom::onMatchFamily(const char familyName[]) const {
    for (int i = 0; i < fFamilies.count(); ++i) {
        if (fFamilies[i]->getFamilyName().equals(familyName)) {
            return SkRef(fFamilies[i].get());
        }
    }
    return nullptr;
}

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

SkTypeface* SkFontMgr_Custom::onMatchFamilyStyleCharacter(const char familyName[],
                                                          const SkFontStyle&,
                                                          const char* bcp47[], int bcp47Count,
                                                          SkUnichar character) const
{
    return nullptr;
}

SkTypeface* SkFontMgr_Custom::onMatchFaceStyle(const SkTypeface* familyMember,
                                               const SkFontStyle& fontStyle) const
{
    for (int i = 0; i < fFamilies.count(); ++i) {
        for (int j = 0; j < fFamilies[i]->fStyles.count(); ++j) {
            if (fFamilies[i]->fStyles[j].get() == familyMember) {
                return fFamilies[i]->matchStyle(fontStyle);
            }
        }
    }
    return nullptr;
}

SkTypeface* SkFontMgr_Custom::onCreateFromData(SkData* data, int ttcIndex) const {
    return this->createFromStream(new SkMemoryStream(sk_ref_sp(data)), ttcIndex);
}

SkTypeface* SkFontMgr_Custom::onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const {
    return this->createFromStream(bareStream, FontParameters().setCollectionIndex(ttcIndex));
}

SkTypeface* SkFontMgr_Custom::onCreateFromStream(SkStreamAsset* s,
                                                 const SkFontArguments& args) const
{
    using Scanner = SkTypeface_FreeType::Scanner;
    std::unique_ptr<SkStreamAsset> stream(s);
    bool isFixedPitch;
    SkFontStyle style;
    SkString name;
    Scanner::AxisDefinitions axisDefinitions;
    if (!fScanner.scanFont(stream.get(), args.getCollectionIndex(),
                            &name, &style, &isFixedPitch, &axisDefinitions))
    {
        return nullptr;
    }

    const SkFontArguments::VariationPosition position = args.getVariationDesignPosition();
    SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
    Scanner::computeAxisValues(axisDefinitions, position, axisValues, name);

    auto data = skstd::make_unique<SkFontData>(std::move(stream), args.getCollectionIndex(),
                                               axisValues.get(), axisDefinitions.count());
    return new SkTypeface_Stream(std::move(data), style, isFixedPitch, false, name);
}

SkTypeface* SkFontMgr_Custom::onCreateFromFontData(std::unique_ptr<SkFontData> data) const {
    bool isFixedPitch;
    SkFontStyle style;
    SkString name;
    if (!fScanner.scanFont(data->getStream(), data->getIndex(),
                            &name, &style, &isFixedPitch, nullptr))
    {
        return nullptr;
    }
    return new SkTypeface_Stream(std::move(data), style, isFixedPitch, false, name);
}

SkTypeface* SkFontMgr_Custom::onCreateFromFile(const char path[], int ttcIndex) const {
    std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
    return stream.get() ? this->createFromStream(stream.release(), ttcIndex) : nullptr;
}

SkTypeface* SkFontMgr_Custom::onLegacyCreateTypeface(const char familyName[],
                                                     SkFontStyle style) const
{
    SkTypeface* tf = nullptr;

    if (familyName) {
        tf = this->onMatchFamilyStyle(familyName, style);
    }

    if (nullptr == tf) {
        tf = fDefaultFamily->matchStyle(style);
    }

    return tf;
}