/* * 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 "include/core/SkPathBuilder.h" #include "include/private/base/SkAssert.h" #include "include/utils/SkCustomTypeface.h" #include "src/core/SkFontDescriptor.h" #include "tools/ToolUtils.h" #include "tools/fonts/TestFontMgr.h" #include "tools/fonts/TestTypeface.h" #ifdef SK_XML #include "tools/fonts/TestSVGTypeface.h" #endif #include namespace { class FontStyleSet final : public SkFontStyleSet { public: FontStyleSet(const char* familyName) : fFamilyName(familyName) {} struct TypefaceEntry { TypefaceEntry(sk_sp typeface, SkFontStyle style, const char* styleName) : fTypeface(std::move(typeface)), fStyle(style), fStyleName(styleName) {} sk_sp fTypeface; SkFontStyle fStyle; const char* fStyleName; }; int count() override { return fTypefaces.size(); } void getStyle(int index, SkFontStyle* style, SkString* name) override { if (style) { *style = fTypefaces[index].fStyle; } if (name) { *name = fTypefaces[index].fStyleName; } } sk_sp createTypeface(int index) override { return fTypefaces[index].fTypeface; } sk_sp matchStyle(const SkFontStyle& pattern) override { return this->matchStyleCSS3(pattern); } SkString getFamilyName() { return fFamilyName; } std::vector fTypefaces; SkString fFamilyName; }; class FontMgr final : public SkFontMgr { public: FontMgr() { auto&& list = TestTypeface::Typefaces(); for (auto&& family : list.families) { auto&& ss = fFamilies.emplace_back(sk_make_sp(family.name)); for (auto&& face : family.faces) { ss->fTypefaces.emplace_back(face.typeface, face.typeface->fontStyle(), face.name); if (face.isDefault) { fDefaultFamily = ss; fDefaultTypeface = face.typeface; } } } if (!fDefaultFamily) { SkASSERTF(false, "expected TestTypeface to return a default"); fDefaultFamily = fFamilies[0]; fDefaultTypeface = fDefaultFamily->fTypefaces[0].fTypeface; } #if defined(SK_ENABLE_SVG) fFamilies.emplace_back(sk_make_sp("Emoji")); fFamilies.back()->fTypefaces.emplace_back( TestSVGTypeface::Default(), SkFontStyle::Normal(), "Normal"); fFamilies.emplace_back(sk_make_sp("Planet")); fFamilies.back()->fTypefaces.emplace_back( TestSVGTypeface::Planets(), SkFontStyle::Normal(), "Normal"); #endif } int onCountFamilies() const override { return fFamilies.size(); } void onGetFamilyName(int index, SkString* familyName) const override { *familyName = fFamilies[index]->getFamilyName(); } sk_sp onCreateStyleSet(int index) const override { sk_sp ref = fFamilies[index]; return ref; } sk_sp onMatchFamily(const char familyName[]) const override { if (familyName) { if (strstr(familyName, "ono")) { return this->createStyleSet(0); } if (strstr(familyName, "ans")) { return this->createStyleSet(1); } if (strstr(familyName, "erif")) { return this->createStyleSet(2); } #if defined(SK_ENABLE_SVG) if (strstr(familyName, "oji")) { return this->createStyleSet(6); } if (strstr(familyName, "Planet")) { return this->createStyleSet(7); } #endif } return nullptr; } sk_sp onMatchFamilyStyle(const char familyName[], const SkFontStyle& style) const override { sk_sp styleSet(this->matchFamily(familyName)); return styleSet->matchStyle(style); } sk_sp onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style, const char* bcp47[], int bcp47Count, SkUnichar character) const override { (void)bcp47; (void)bcp47Count; (void)character; return this->matchFamilyStyle(familyName, style); } sk_sp onMakeFromData(sk_sp, int ttcIndex) const override { return nullptr; } sk_sp onMakeFromStreamIndex(std::unique_ptr, int ttcIndex) const override { return nullptr; } sk_sp onMakeFromStreamArgs(std::unique_ptr, const SkFontArguments&) const override { return nullptr; } sk_sp onMakeFromFile(const char path[], int ttcIndex) const override { return nullptr; } sk_sp onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override { if (familyName == nullptr) { return sk_sp(fDefaultFamily->matchStyle(style)); } sk_sp typeface = sk_sp(this->matchFamilyStyle(familyName, style)); if (!typeface) { typeface = fDefaultTypeface; } return typeface; } private: std::vector> fFamilies; sk_sp fDefaultFamily; sk_sp fDefaultTypeface; }; } // namespace namespace ToolUtils { sk_sp MakePortableFontMgr() { return sk_make_sp(); } } // namespace ToolUtils