SkFontMgr.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright 2013 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. #ifndef SkFontMgr_DEFINED
  8. #define SkFontMgr_DEFINED
  9. #include "include/core/SkFontArguments.h"
  10. #include "include/core/SkFontStyle.h"
  11. #include "include/core/SkRefCnt.h"
  12. #include "include/core/SkTypes.h"
  13. class SkData;
  14. class SkFontData;
  15. class SkStreamAsset;
  16. class SkString;
  17. class SkTypeface;
  18. class SK_API SkFontStyleSet : public SkRefCnt {
  19. public:
  20. virtual int count() = 0;
  21. virtual void getStyle(int index, SkFontStyle*, SkString* style) = 0;
  22. virtual SkTypeface* createTypeface(int index) = 0;
  23. virtual SkTypeface* matchStyle(const SkFontStyle& pattern) = 0;
  24. static SkFontStyleSet* CreateEmpty();
  25. protected:
  26. SkTypeface* matchStyleCSS3(const SkFontStyle& pattern);
  27. private:
  28. typedef SkRefCnt INHERITED;
  29. };
  30. class SK_API SkFontMgr : public SkRefCnt {
  31. public:
  32. int countFamilies() const;
  33. void getFamilyName(int index, SkString* familyName) const;
  34. SkFontStyleSet* createStyleSet(int index) const;
  35. /**
  36. * The caller must call unref() on the returned object.
  37. * Never returns NULL; will return an empty set if the name is not found.
  38. *
  39. * Passing nullptr as the parameter will return the default system family.
  40. * Note that most systems don't have a default system family, so passing nullptr will often
  41. * result in the empty set.
  42. *
  43. * It is possible that this will return a style set not accessible from
  44. * createStyleSet(int) due to hidden or auto-activated fonts.
  45. */
  46. SkFontStyleSet* matchFamily(const char familyName[]) const;
  47. /**
  48. * Find the closest matching typeface to the specified familyName and style
  49. * and return a ref to it. The caller must call unref() on the returned
  50. * object. Will return nullptr if no 'good' match is found.
  51. *
  52. * Passing |nullptr| as the parameter for |familyName| will return the
  53. * default system font.
  54. *
  55. * It is possible that this will return a style set not accessible from
  56. * createStyleSet(int) or matchFamily(const char[]) due to hidden or
  57. * auto-activated fonts.
  58. */
  59. SkTypeface* matchFamilyStyle(const char familyName[], const SkFontStyle&) const;
  60. /**
  61. * Use the system fallback to find a typeface for the given character.
  62. * Note that bcp47 is a combination of ISO 639, 15924, and 3166-1 codes,
  63. * so it is fine to just pass a ISO 639 here.
  64. *
  65. * Will return NULL if no family can be found for the character
  66. * in the system fallback.
  67. *
  68. * Passing |nullptr| as the parameter for |familyName| will return the
  69. * default system font.
  70. *
  71. * bcp47[0] is the least significant fallback, bcp47[bcp47Count-1] is the
  72. * most significant. If no specified bcp47 codes match, any font with the
  73. * requested character will be matched.
  74. */
  75. SkTypeface* matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
  76. const char* bcp47[], int bcp47Count,
  77. SkUnichar character) const;
  78. SkTypeface* matchFaceStyle(const SkTypeface*, const SkFontStyle&) const;
  79. /**
  80. * Create a typeface for the specified data and TTC index (pass 0 for none)
  81. * or NULL if the data is not recognized. The caller must call unref() on
  82. * the returned object if it is not null.
  83. */
  84. sk_sp<SkTypeface> makeFromData(sk_sp<SkData>, int ttcIndex = 0) const;
  85. /**
  86. * Create a typeface for the specified stream and TTC index
  87. * (pass 0 for none) or NULL if the stream is not recognized. The caller
  88. * must call unref() on the returned object if it is not null.
  89. */
  90. sk_sp<SkTypeface> makeFromStream(std::unique_ptr<SkStreamAsset>, int ttcIndex = 0) const;
  91. /* Experimental, API subject to change. */
  92. sk_sp<SkTypeface> makeFromStream(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const;
  93. /**
  94. * Create a typeface from the specified font data.
  95. * Will return NULL if the typeface could not be created.
  96. * The caller must call unref() on the returned object if it is not null.
  97. */
  98. sk_sp<SkTypeface> makeFromFontData(std::unique_ptr<SkFontData>) const;
  99. /**
  100. * Create a typeface for the specified fileName and TTC index
  101. * (pass 0 for none) or NULL if the file is not found, or its contents are
  102. * not recognized. The caller must call unref() on the returned object
  103. * if it is not null.
  104. */
  105. sk_sp<SkTypeface> makeFromFile(const char path[], int ttcIndex = 0) const;
  106. sk_sp<SkTypeface> legacyMakeTypeface(const char familyName[], SkFontStyle style) const;
  107. /** Return the default fontmgr. */
  108. static sk_sp<SkFontMgr> RefDefault();
  109. protected:
  110. virtual int onCountFamilies() const = 0;
  111. virtual void onGetFamilyName(int index, SkString* familyName) const = 0;
  112. virtual SkFontStyleSet* onCreateStyleSet(int index)const = 0;
  113. /** May return NULL if the name is not found. */
  114. virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const = 0;
  115. virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
  116. const SkFontStyle&) const = 0;
  117. virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
  118. const char* bcp47[], int bcp47Count,
  119. SkUnichar character) const = 0;
  120. virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
  121. const SkFontStyle&) const = 0;
  122. virtual sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const = 0;
  123. virtual sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
  124. int ttcIndex) const = 0;
  125. virtual sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
  126. const SkFontArguments&) const;
  127. virtual sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData>) const;
  128. virtual sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const = 0;
  129. virtual sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle) const = 0;
  130. private:
  131. /** Implemented by porting layer to return the default factory. */
  132. static sk_sp<SkFontMgr> Factory();
  133. typedef SkRefCnt INHERITED;
  134. };
  135. #endif