TestFontMgr.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright 2017 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "src/core/SkFontDescriptor.h"
  8. #include "tools/ToolUtils.h"
  9. #include "tools/fonts/TestFontMgr.h"
  10. #include "tools/fonts/TestTypeface.h"
  11. #ifdef SK_XML
  12. #include "tools/fonts/TestSVGTypeface.h"
  13. #endif
  14. #include <vector>
  15. namespace {
  16. #include "test_font_monospace.inc"
  17. #include "test_font_sans_serif.inc"
  18. #include "test_font_serif.inc"
  19. #include "test_font_index.inc"
  20. class FontStyleSet final : public SkFontStyleSet {
  21. public:
  22. FontStyleSet(const char* familyName) : fFamilyName(familyName) {}
  23. struct TypefaceEntry {
  24. TypefaceEntry(sk_sp<SkTypeface> typeface, SkFontStyle style, const char* styleName)
  25. : fTypeface(std::move(typeface)), fStyle(style), fStyleName(styleName) {}
  26. sk_sp<SkTypeface> fTypeface;
  27. SkFontStyle fStyle;
  28. const char* fStyleName;
  29. };
  30. int count() override { return fTypefaces.size(); }
  31. void getStyle(int index, SkFontStyle* style, SkString* name) override {
  32. if (style) {
  33. *style = fTypefaces[index].fStyle;
  34. }
  35. if (name) {
  36. *name = fTypefaces[index].fStyleName;
  37. }
  38. }
  39. SkTypeface* createTypeface(int index) override {
  40. return SkRef(fTypefaces[index].fTypeface.get());
  41. }
  42. SkTypeface* matchStyle(const SkFontStyle& pattern) override {
  43. return this->matchStyleCSS3(pattern);
  44. }
  45. SkString getFamilyName() { return fFamilyName; }
  46. std::vector<TypefaceEntry> fTypefaces;
  47. SkString fFamilyName;
  48. };
  49. class FontMgr final : public SkFontMgr {
  50. public:
  51. FontMgr() {
  52. for (const auto& sub : gSubFonts) {
  53. sk_sp<TestTypeface> typeface =
  54. sk_make_sp<TestTypeface>(sk_make_sp<SkTestFont>(sub.fFont), sub.fStyle);
  55. bool defaultFamily = false;
  56. if (&sub - gSubFonts == gDefaultFontIndex) {
  57. defaultFamily = true;
  58. fDefaultTypeface = typeface;
  59. }
  60. bool found = false;
  61. for (const auto& family : fFamilies) {
  62. if (family->getFamilyName().equals(sub.fFamilyName)) {
  63. family->fTypefaces.emplace_back(
  64. std::move(typeface), sub.fStyle, sub.fStyleName);
  65. found = true;
  66. if (defaultFamily) {
  67. fDefaultFamily = family;
  68. }
  69. break;
  70. }
  71. }
  72. if (!found) {
  73. fFamilies.emplace_back(sk_make_sp<FontStyleSet>(sub.fFamilyName));
  74. fFamilies.back()->fTypefaces.emplace_back(
  75. // NOLINTNEXTLINE(bugprone-use-after-move)
  76. std::move(typeface),
  77. sub.fStyle,
  78. sub.fStyleName);
  79. if (defaultFamily) {
  80. fDefaultFamily = fFamilies.back();
  81. }
  82. }
  83. }
  84. #ifdef SK_XML
  85. fFamilies.emplace_back(sk_make_sp<FontStyleSet>("Emoji"));
  86. fFamilies.back()->fTypefaces.emplace_back(
  87. TestSVGTypeface::Default(), SkFontStyle::Normal(), "Normal");
  88. fFamilies.emplace_back(sk_make_sp<FontStyleSet>("Planet"));
  89. fFamilies.back()->fTypefaces.emplace_back(
  90. TestSVGTypeface::Planets(), SkFontStyle::Normal(), "Normal");
  91. #endif
  92. }
  93. int onCountFamilies() const override { return fFamilies.size(); }
  94. void onGetFamilyName(int index, SkString* familyName) const override {
  95. *familyName = fFamilies[index]->getFamilyName();
  96. }
  97. SkFontStyleSet* onCreateStyleSet(int index) const override {
  98. sk_sp<SkFontStyleSet> ref = fFamilies[index];
  99. return ref.release();
  100. }
  101. SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
  102. if (familyName) {
  103. if (strstr(familyName, "ono")) {
  104. return this->createStyleSet(0);
  105. }
  106. if (strstr(familyName, "ans")) {
  107. return this->createStyleSet(1);
  108. }
  109. if (strstr(familyName, "erif")) {
  110. return this->createStyleSet(2);
  111. }
  112. #ifdef SK_XML
  113. if (strstr(familyName, "oji")) {
  114. return this->createStyleSet(6);
  115. }
  116. if (strstr(familyName, "Planet")) {
  117. return this->createStyleSet(7);
  118. }
  119. #endif
  120. }
  121. return nullptr;
  122. }
  123. SkTypeface* onMatchFamilyStyle(const char familyName[],
  124. const SkFontStyle& style) const override {
  125. sk_sp<SkFontStyleSet> styleSet(this->matchFamily(familyName));
  126. return styleSet->matchStyle(style);
  127. }
  128. SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
  129. const SkFontStyle& style,
  130. const char* bcp47[],
  131. int bcp47Count,
  132. SkUnichar character) const override {
  133. (void)bcp47;
  134. (void)bcp47Count;
  135. (void)character;
  136. return this->matchFamilyStyle(familyName, style);
  137. }
  138. SkTypeface* onMatchFaceStyle(const SkTypeface* tf, const SkFontStyle& style) const override {
  139. SkString familyName;
  140. tf->getFamilyName(&familyName);
  141. return this->matchFamilyStyle(familyName.c_str(), style);
  142. }
  143. sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override { return nullptr; }
  144. sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
  145. int ttcIndex) const override {
  146. return nullptr;
  147. }
  148. sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
  149. const SkFontArguments&) const override {
  150. return nullptr;
  151. }
  152. sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData>) const override {
  153. return nullptr;
  154. }
  155. sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
  156. return nullptr;
  157. }
  158. sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[],
  159. SkFontStyle style) const override {
  160. if (familyName == nullptr) {
  161. return sk_sp<SkTypeface>(fDefaultFamily->matchStyle(style));
  162. }
  163. sk_sp<SkTypeface> typeface = sk_sp<SkTypeface>(this->matchFamilyStyle(familyName, style));
  164. if (!typeface) {
  165. typeface = fDefaultTypeface;
  166. }
  167. return typeface;
  168. }
  169. private:
  170. std::vector<sk_sp<FontStyleSet>> fFamilies;
  171. sk_sp<FontStyleSet> fDefaultFamily;
  172. sk_sp<SkTypeface> fDefaultTypeface;
  173. };
  174. } // namespace
  175. namespace ToolUtils {
  176. sk_sp<SkFontMgr> MakePortableFontMgr() { return sk_make_sp<FontMgr>(); }
  177. } // namespace ToolUtils