SkFontMgr_custom.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  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. #ifndef SkFontMgr_custom_DEFINED
  8. #define SkFontMgr_custom_DEFINED
  9. #include "include/core/SkFontMgr.h"
  10. #include "include/core/SkFontStyle.h"
  11. #include "include/core/SkRefCnt.h"
  12. #include "include/core/SkString.h"
  13. #include "include/core/SkTypes.h"
  14. #include "include/private/SkTArray.h"
  15. #include "src/ports/SkFontHost_FreeType_common.h"
  16. class SkData;
  17. class SkFontDescriptor;
  18. class SkStreamAsset;
  19. class SkTypeface;
  20. /** The base SkTypeface implementation for the custom font manager. */
  21. class SkTypeface_Custom : public SkTypeface_FreeType {
  22. public:
  23. SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch,
  24. bool sysFont, const SkString familyName, int index);
  25. bool isSysFont() const;
  26. protected:
  27. void onGetFamilyName(SkString* familyName) const override;
  28. void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override;
  29. int getIndex() const;
  30. private:
  31. const bool fIsSysFont;
  32. const SkString fFamilyName;
  33. const int fIndex;
  34. typedef SkTypeface_FreeType INHERITED;
  35. };
  36. /** The empty SkTypeface implementation for the custom font manager.
  37. * Used as the last resort fallback typeface.
  38. */
  39. class SkTypeface_Empty : public SkTypeface_Custom {
  40. public:
  41. SkTypeface_Empty() ;
  42. protected:
  43. std::unique_ptr<SkStreamAsset> onOpenStream(int*) const override;
  44. sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override;
  45. private:
  46. typedef SkTypeface_Custom INHERITED;
  47. };
  48. /** The stream SkTypeface implementation for the custom font manager. */
  49. class SkTypeface_Stream : public SkTypeface_Custom {
  50. public:
  51. SkTypeface_Stream(std::unique_ptr<SkFontData> fontData,
  52. const SkFontStyle& style, bool isFixedPitch, bool sysFont,
  53. const SkString familyName);
  54. protected:
  55. std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override;
  56. std::unique_ptr<SkFontData> onMakeFontData() const override;
  57. sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override;
  58. private:
  59. const std::unique_ptr<const SkFontData> fData;
  60. typedef SkTypeface_Custom INHERITED;
  61. };
  62. /** The file SkTypeface implementation for the custom font manager. */
  63. class SkTypeface_File : public SkTypeface_Custom {
  64. public:
  65. SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
  66. const SkString familyName, const char path[], int index);
  67. protected:
  68. std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override;
  69. sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override;
  70. private:
  71. SkString fPath;
  72. typedef SkTypeface_Custom INHERITED;
  73. };
  74. ///////////////////////////////////////////////////////////////////////////////
  75. /**
  76. * SkFontStyleSet_Custom
  77. *
  78. * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families.
  79. */
  80. class SkFontStyleSet_Custom : public SkFontStyleSet {
  81. public:
  82. explicit SkFontStyleSet_Custom(const SkString familyName);
  83. /** Should only be called during the inital build phase. */
  84. void appendTypeface(sk_sp<SkTypeface_Custom> typeface);
  85. int count() override;
  86. void getStyle(int index, SkFontStyle* style, SkString* name) override;
  87. SkTypeface* createTypeface(int index) override;
  88. SkTypeface* matchStyle(const SkFontStyle& pattern) override;
  89. SkString getFamilyName();
  90. private:
  91. SkTArray<sk_sp<SkTypeface_Custom>> fStyles;
  92. SkString fFamilyName;
  93. friend class SkFontMgr_Custom;
  94. };
  95. /**
  96. * SkFontMgr_Custom
  97. *
  98. * This class is essentially a collection of SkFontStyleSet_Custom,
  99. * one SkFontStyleSet_Custom for each family. This class may be modified
  100. * to load fonts from any source by changing the initialization.
  101. */
  102. class SkFontMgr_Custom : public SkFontMgr {
  103. public:
  104. typedef SkTArray<sk_sp<SkFontStyleSet_Custom>> Families;
  105. class SystemFontLoader {
  106. public:
  107. virtual ~SystemFontLoader() { }
  108. virtual void loadSystemFonts(const SkTypeface_FreeType::Scanner&, Families*) const = 0;
  109. };
  110. explicit SkFontMgr_Custom(const SystemFontLoader& loader);
  111. protected:
  112. int onCountFamilies() const override;
  113. void onGetFamilyName(int index, SkString* familyName) const override;
  114. SkFontStyleSet_Custom* onCreateStyleSet(int index) const override;
  115. SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const override;
  116. SkTypeface* onMatchFamilyStyle(const char familyName[],
  117. const SkFontStyle& fontStyle) const override;
  118. SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
  119. const char* bcp47[], int bcp47Count,
  120. SkUnichar character) const override;
  121. SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
  122. const SkFontStyle& fontStyle) const override;
  123. sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override;
  124. sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int ttcIndex) const override;
  125. sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const override;
  126. sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData> data) const override;
  127. sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override;
  128. sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override;
  129. private:
  130. Families fFamilies;
  131. SkFontStyleSet_Custom* fDefaultFamily;
  132. SkTypeface_FreeType::Scanner fScanner;
  133. };
  134. #endif