SkFontConfigInterface.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 SkFontConfigInterface_DEFINED
  8. #define SkFontConfigInterface_DEFINED
  9. #include "include/core/SkFontStyle.h"
  10. #include "include/core/SkRefCnt.h"
  11. #include "include/core/SkStream.h"
  12. #include "include/core/SkTypeface.h"
  13. class SkFontMgr;
  14. /**
  15. * \class SkFontConfigInterface
  16. *
  17. * A simple interface for remotable font management.
  18. * The global instance can be found with RefGlobal().
  19. */
  20. class SK_API SkFontConfigInterface : public SkRefCnt {
  21. public:
  22. /**
  23. * Returns the global SkFontConfigInterface instance. If it is not
  24. * nullptr, calls ref() on it. The caller must balance this with a call to
  25. * unref(). The default SkFontConfigInterface is the result of calling
  26. * GetSingletonDirectInterface.
  27. */
  28. static sk_sp<SkFontConfigInterface> RefGlobal();
  29. /**
  30. * Replace the current global instance with the specified one.
  31. */
  32. static void SetGlobal(sk_sp<SkFontConfigInterface> fc);
  33. /**
  34. * This should be treated as private to the impl of SkFontConfigInterface.
  35. * Callers should not change or expect any particular values. It is meant
  36. * to be a union of possible storage types to aid the impl.
  37. */
  38. struct FontIdentity {
  39. FontIdentity() : fID(0), fTTCIndex(0) {}
  40. bool operator==(const FontIdentity& other) const {
  41. return fID == other.fID &&
  42. fTTCIndex == other.fTTCIndex &&
  43. fString == other.fString;
  44. }
  45. bool operator!=(const FontIdentity& other) const {
  46. return !(*this == other);
  47. }
  48. uint32_t fID;
  49. int32_t fTTCIndex;
  50. SkString fString;
  51. SkFontStyle fStyle;
  52. // If buffer is NULL, just return the number of bytes that would have
  53. // been written. Will pad contents to a multiple of 4.
  54. size_t writeToMemory(void* buffer = nullptr) const;
  55. // Recreate from a flattened buffer, returning the number of bytes read.
  56. size_t readFromMemory(const void* buffer, size_t length);
  57. };
  58. /**
  59. * Given a familyName and style, find the best match.
  60. *
  61. * If a match is found, return true and set its outFontIdentifier.
  62. * If outFamilyName is not null, assign the found familyName to it
  63. * (which may differ from the requested familyName).
  64. * If outStyle is not null, assign the found style to it
  65. * (which may differ from the requested style).
  66. *
  67. * If a match is not found, return false, and ignore all out parameters.
  68. */
  69. virtual bool matchFamilyName(const char familyName[],
  70. SkFontStyle requested,
  71. FontIdentity* outFontIdentifier,
  72. SkString* outFamilyName,
  73. SkFontStyle* outStyle) = 0;
  74. /**
  75. * Given a FontRef, open a stream to access its data, or return null
  76. * if the FontRef's data is not available. The caller is responsible for
  77. * deleting the stream when it is done accessing the data.
  78. */
  79. virtual SkStreamAsset* openStream(const FontIdentity&) = 0;
  80. /**
  81. * Return an SkTypeface for the given FontIdentity.
  82. *
  83. * The default implementation simply returns a new typeface built using data obtained from
  84. * openStream(), but derived classes may implement more complex caching schemes.
  85. */
  86. virtual sk_sp<SkTypeface> makeTypeface(const FontIdentity& identity) {
  87. return SkTypeface::MakeFromStream(std::unique_ptr<SkStreamAsset>(this->openStream(identity)),
  88. identity.fTTCIndex);
  89. }
  90. /**
  91. * Return a singleton instance of a direct subclass that calls into
  92. * libfontconfig. This does not affect the refcnt of the returned instance.
  93. */
  94. static SkFontConfigInterface* GetSingletonDirectInterface();
  95. typedef SkRefCnt INHERITED;
  96. };
  97. #endif