2017-11-06 17:02:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkFontDescriptor.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
#include "tools/fonts/TestFontMgr.h"
|
|
|
|
#include "tools/fonts/TestTypeface.h"
|
2018-05-31 18:50:58 +00:00
|
|
|
|
|
|
|
#ifdef SK_XML
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/fonts/TestSVGTypeface.h"
|
2018-05-31 18:50:58 +00:00
|
|
|
#endif
|
2017-11-06 17:02:47 +00:00
|
|
|
|
2018-07-10 23:40:15 +00:00
|
|
|
#include <vector>
|
2018-02-15 15:20:04 +00:00
|
|
|
|
2018-07-10 23:40:15 +00:00
|
|
|
namespace {
|
2018-02-15 15:20:04 +00:00
|
|
|
|
2018-07-10 23:40:15 +00:00
|
|
|
#include "test_font_monospace.inc"
|
|
|
|
#include "test_font_sans_serif.inc"
|
|
|
|
#include "test_font_serif.inc"
|
2019-03-20 16:08:46 +00:00
|
|
|
|
2018-07-10 23:40:15 +00:00
|
|
|
#include "test_font_index.inc"
|
2017-11-06 17:02:47 +00:00
|
|
|
|
2017-11-14 15:34:48 +00:00
|
|
|
class FontStyleSet final : public SkFontStyleSet {
|
|
|
|
public:
|
2019-03-20 16:08:46 +00:00
|
|
|
FontStyleSet(const char* familyName) : fFamilyName(familyName) {}
|
2018-07-10 23:40:15 +00:00
|
|
|
struct TypefaceEntry {
|
|
|
|
TypefaceEntry(sk_sp<SkTypeface> typeface, SkFontStyle style, const char* styleName)
|
2019-03-20 16:08:46 +00:00
|
|
|
: fTypeface(std::move(typeface)), fStyle(style), fStyleName(styleName) {}
|
2018-07-10 23:40:15 +00:00
|
|
|
sk_sp<SkTypeface> fTypeface;
|
2019-03-20 16:08:46 +00:00
|
|
|
SkFontStyle fStyle;
|
|
|
|
const char* fStyleName;
|
2018-07-10 23:40:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int count() override { return fTypefaces.size(); }
|
2017-11-09 18:45:10 +00:00
|
|
|
|
2017-11-14 15:34:48 +00:00
|
|
|
void getStyle(int index, SkFontStyle* style, SkString* name) override {
|
2019-03-20 16:08:46 +00:00
|
|
|
if (style) {
|
|
|
|
*style = fTypefaces[index].fStyle;
|
|
|
|
}
|
|
|
|
if (name) {
|
|
|
|
*name = fTypefaces[index].fStyleName;
|
|
|
|
}
|
2017-11-06 17:02:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 15:34:48 +00:00
|
|
|
SkTypeface* createTypeface(int index) override {
|
2018-07-10 23:40:15 +00:00
|
|
|
return SkRef(fTypefaces[index].fTypeface.get());
|
2017-11-09 18:45:10 +00:00
|
|
|
}
|
2017-11-06 17:02:47 +00:00
|
|
|
|
2017-11-14 15:34:48 +00:00
|
|
|
SkTypeface* matchStyle(const SkFontStyle& pattern) override {
|
2017-11-09 18:45:10 +00:00
|
|
|
return this->matchStyleCSS3(pattern);
|
2017-11-06 17:02:47 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 23:40:15 +00:00
|
|
|
SkString getFamilyName() { return fFamilyName; }
|
|
|
|
|
|
|
|
std::vector<TypefaceEntry> fTypefaces;
|
2019-03-20 16:08:46 +00:00
|
|
|
SkString fFamilyName;
|
2017-11-14 15:34:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FontMgr final : public SkFontMgr {
|
|
|
|
public:
|
|
|
|
FontMgr() {
|
2018-07-10 23:40:15 +00:00
|
|
|
for (const auto& sub : gSubFonts) {
|
2019-03-20 16:08:46 +00:00
|
|
|
sk_sp<TestTypeface> typeface =
|
|
|
|
sk_make_sp<TestTypeface>(sk_make_sp<SkTestFont>(sub.fFont), sub.fStyle);
|
2018-07-10 23:40:15 +00:00
|
|
|
bool defaultFamily = false;
|
|
|
|
if (&sub - gSubFonts == gDefaultFontIndex) {
|
2019-03-20 16:08:46 +00:00
|
|
|
defaultFamily = true;
|
2018-07-10 23:40:15 +00:00
|
|
|
fDefaultTypeface = typeface;
|
|
|
|
}
|
|
|
|
bool found = false;
|
|
|
|
for (const auto& family : fFamilies) {
|
|
|
|
if (family->getFamilyName().equals(sub.fFamilyName)) {
|
2019-03-20 16:08:46 +00:00
|
|
|
family->fTypefaces.emplace_back(
|
|
|
|
std::move(typeface), sub.fStyle, sub.fStyleName);
|
2018-07-10 23:40:15 +00:00
|
|
|
found = true;
|
|
|
|
if (defaultFamily) {
|
|
|
|
fDefaultFamily = family;
|
|
|
|
}
|
2018-12-11 14:54:31 +00:00
|
|
|
break;
|
2018-07-10 23:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
fFamilies.emplace_back(sk_make_sp<FontStyleSet>(sub.fFamilyName));
|
2019-03-20 16:08:46 +00:00
|
|
|
fFamilies.back()->fTypefaces.emplace_back(
|
|
|
|
// NOLINTNEXTLINE(bugprone-use-after-move)
|
|
|
|
std::move(typeface),
|
|
|
|
sub.fStyle,
|
|
|
|
sub.fStyleName);
|
2018-07-10 23:40:15 +00:00
|
|
|
if (defaultFamily) {
|
|
|
|
fDefaultFamily = fFamilies.back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 16:08:46 +00:00
|
|
|
#ifdef SK_XML
|
2018-07-10 23:40:15 +00:00
|
|
|
fFamilies.emplace_back(sk_make_sp<FontStyleSet>("Emoji"));
|
2019-03-20 16:08:46 +00:00
|
|
|
fFamilies.back()->fTypefaces.emplace_back(
|
|
|
|
TestSVGTypeface::Default(), SkFontStyle::Normal(), "Normal");
|
2019-03-08 23:05:47 +00:00
|
|
|
|
|
|
|
fFamilies.emplace_back(sk_make_sp<FontStyleSet>("Planet"));
|
2019-03-20 16:08:46 +00:00
|
|
|
fFamilies.back()->fTypefaces.emplace_back(
|
|
|
|
TestSVGTypeface::Planets(), SkFontStyle::Normal(), "Normal");
|
|
|
|
#endif
|
2017-11-09 18:45:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 18:50:58 +00:00
|
|
|
int onCountFamilies() const override { return fFamilies.size(); }
|
2017-11-06 17:02:47 +00:00
|
|
|
|
2017-11-14 15:34:48 +00:00
|
|
|
void onGetFamilyName(int index, SkString* familyName) const override {
|
2018-07-10 23:40:15 +00:00
|
|
|
*familyName = fFamilies[index]->getFamilyName();
|
2017-11-06 17:02:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 15:34:48 +00:00
|
|
|
SkFontStyleSet* onCreateStyleSet(int index) const override {
|
2018-05-31 18:50:58 +00:00
|
|
|
sk_sp<SkFontStyleSet> ref = fFamilies[index];
|
|
|
|
return ref.release();
|
2017-11-06 17:02:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 15:34:48 +00:00
|
|
|
SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
|
2017-11-06 17:02:47 +00:00
|
|
|
if (familyName) {
|
2019-03-20 16:08:46 +00:00
|
|
|
if (strstr(familyName, "ono")) {
|
|
|
|
return this->createStyleSet(0);
|
|
|
|
}
|
|
|
|
if (strstr(familyName, "ans")) {
|
|
|
|
return this->createStyleSet(1);
|
|
|
|
}
|
|
|
|
if (strstr(familyName, "erif")) {
|
|
|
|
return this->createStyleSet(2);
|
|
|
|
}
|
|
|
|
#ifdef SK_XML
|
|
|
|
if (strstr(familyName, "oji")) {
|
|
|
|
return this->createStyleSet(6);
|
|
|
|
}
|
|
|
|
if (strstr(familyName, "Planet")) {
|
|
|
|
return this->createStyleSet(7);
|
|
|
|
}
|
|
|
|
#endif
|
2017-11-06 17:02:47 +00:00
|
|
|
}
|
2018-07-10 23:40:15 +00:00
|
|
|
return nullptr;
|
2017-11-06 17:02:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
SkTypeface* onMatchFamilyStyle(const char familyName[],
|
2017-11-14 15:34:48 +00:00
|
|
|
const SkFontStyle& style) const override {
|
2017-11-06 17:02:47 +00:00
|
|
|
sk_sp<SkFontStyleSet> styleSet(this->matchFamily(familyName));
|
|
|
|
return styleSet->matchStyle(style);
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
|
2017-11-14 15:34:48 +00:00
|
|
|
const SkFontStyle& style,
|
2019-03-20 16:08:46 +00:00
|
|
|
const char* bcp47[],
|
|
|
|
int bcp47Count,
|
|
|
|
SkUnichar character) const override {
|
2017-11-06 17:02:47 +00:00
|
|
|
(void)bcp47;
|
|
|
|
(void)bcp47Count;
|
|
|
|
(void)character;
|
|
|
|
return this->matchFamilyStyle(familyName, style);
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
SkTypeface* onMatchFaceStyle(const SkTypeface* tf, const SkFontStyle& style) const override {
|
2017-11-06 17:02:47 +00:00
|
|
|
SkString familyName;
|
|
|
|
tf->getFamilyName(&familyName);
|
|
|
|
return this->matchFamilyStyle(familyName.c_str(), style);
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override { return nullptr; }
|
2017-11-14 15:34:48 +00:00
|
|
|
sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
|
|
|
|
int ttcIndex) const override {
|
2017-11-09 18:45:10 +00:00
|
|
|
return nullptr;
|
2017-11-06 17:02:47 +00:00
|
|
|
}
|
2017-11-14 15:34:48 +00:00
|
|
|
sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
|
|
|
|
const SkFontArguments&) const override {
|
2017-11-09 18:45:10 +00:00
|
|
|
return nullptr;
|
2017-11-06 17:02:47 +00:00
|
|
|
}
|
2017-11-14 15:34:48 +00:00
|
|
|
sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData>) const override {
|
2017-11-09 18:45:10 +00:00
|
|
|
return nullptr;
|
2017-11-06 17:02:47 +00:00
|
|
|
}
|
2017-11-14 15:34:48 +00:00
|
|
|
sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
|
2017-11-09 18:45:10 +00:00
|
|
|
return nullptr;
|
2017-11-06 17:02:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[],
|
2017-11-14 15:34:48 +00:00
|
|
|
SkFontStyle style) const override {
|
2018-07-10 23:40:15 +00:00
|
|
|
if (familyName == nullptr) {
|
|
|
|
return sk_sp<SkTypeface>(fDefaultFamily->matchStyle(style));
|
|
|
|
}
|
|
|
|
sk_sp<SkTypeface> typeface = sk_sp<SkTypeface>(this->matchFamilyStyle(familyName, style));
|
|
|
|
if (!typeface) {
|
|
|
|
typeface = fDefaultTypeface;
|
|
|
|
}
|
|
|
|
return typeface;
|
2017-11-06 17:02:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 15:34:48 +00:00
|
|
|
private:
|
2018-07-10 23:40:15 +00:00
|
|
|
std::vector<sk_sp<FontStyleSet>> fFamilies;
|
2019-03-20 16:08:46 +00:00
|
|
|
sk_sp<FontStyleSet> fDefaultFamily;
|
|
|
|
sk_sp<SkTypeface> fDefaultTypeface;
|
2017-11-14 15:34:48 +00:00
|
|
|
};
|
2019-03-20 16:08:46 +00:00
|
|
|
} // namespace
|
2017-11-14 15:34:48 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
namespace ToolUtils {
|
2018-02-20 22:06:07 +00:00
|
|
|
sk_sp<SkFontMgr> MakePortableFontMgr() { return sk_make_sp<FontMgr>(); }
|
2019-03-20 16:12:10 +00:00
|
|
|
} // namespace ToolUtils
|