gfxDWriteCommon.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef GFX_DWRITECOMMON_H
  6. #define GFX_DWRITECOMMON_H
  7. // Mozilla includes
  8. #include "nscore.h"
  9. #include "nsIServiceManager.h"
  10. #include "nsCOMPtr.h"
  11. #include "cairo-features.h"
  12. #include "gfxFontConstants.h"
  13. #include "nsTArray.h"
  14. #include "gfxWindowsPlatform.h"
  15. #include "nsIUUIDGenerator.h"
  16. #include <windows.h>
  17. #include <dwrite.h>
  18. static inline DWRITE_FONT_STRETCH
  19. DWriteFontStretchFromStretch(int16_t aStretch)
  20. {
  21. switch (aStretch) {
  22. case NS_FONT_STRETCH_ULTRA_CONDENSED:
  23. return DWRITE_FONT_STRETCH_ULTRA_CONDENSED;
  24. case NS_FONT_STRETCH_EXTRA_CONDENSED:
  25. return DWRITE_FONT_STRETCH_EXTRA_CONDENSED;
  26. case NS_FONT_STRETCH_CONDENSED:
  27. return DWRITE_FONT_STRETCH_CONDENSED;
  28. case NS_FONT_STRETCH_SEMI_CONDENSED:
  29. return DWRITE_FONT_STRETCH_SEMI_CONDENSED;
  30. case NS_FONT_STRETCH_NORMAL:
  31. return DWRITE_FONT_STRETCH_NORMAL;
  32. case NS_FONT_STRETCH_SEMI_EXPANDED:
  33. return DWRITE_FONT_STRETCH_SEMI_EXPANDED;
  34. case NS_FONT_STRETCH_EXPANDED:
  35. return DWRITE_FONT_STRETCH_EXPANDED;
  36. case NS_FONT_STRETCH_EXTRA_EXPANDED:
  37. return DWRITE_FONT_STRETCH_EXTRA_EXPANDED;
  38. case NS_FONT_STRETCH_ULTRA_EXPANDED:
  39. return DWRITE_FONT_STRETCH_ULTRA_EXPANDED;
  40. default:
  41. return DWRITE_FONT_STRETCH_UNDEFINED;
  42. }
  43. }
  44. static inline int16_t
  45. FontStretchFromDWriteStretch(DWRITE_FONT_STRETCH aStretch)
  46. {
  47. switch (aStretch) {
  48. case DWRITE_FONT_STRETCH_ULTRA_CONDENSED:
  49. return NS_FONT_STRETCH_ULTRA_CONDENSED;
  50. case DWRITE_FONT_STRETCH_EXTRA_CONDENSED:
  51. return NS_FONT_STRETCH_EXTRA_CONDENSED;
  52. case DWRITE_FONT_STRETCH_CONDENSED:
  53. return NS_FONT_STRETCH_CONDENSED;
  54. case DWRITE_FONT_STRETCH_SEMI_CONDENSED:
  55. return NS_FONT_STRETCH_SEMI_CONDENSED;
  56. case DWRITE_FONT_STRETCH_NORMAL:
  57. return NS_FONT_STRETCH_NORMAL;
  58. case DWRITE_FONT_STRETCH_SEMI_EXPANDED:
  59. return NS_FONT_STRETCH_SEMI_EXPANDED;
  60. case DWRITE_FONT_STRETCH_EXPANDED:
  61. return NS_FONT_STRETCH_EXPANDED;
  62. case DWRITE_FONT_STRETCH_EXTRA_EXPANDED:
  63. return NS_FONT_STRETCH_EXTRA_EXPANDED;
  64. case DWRITE_FONT_STRETCH_ULTRA_EXPANDED:
  65. return NS_FONT_STRETCH_ULTRA_EXPANDED;
  66. default:
  67. return NS_FONT_STRETCH_NORMAL;
  68. }
  69. }
  70. struct ffReferenceKey
  71. {
  72. FallibleTArray<uint8_t> *mArray;
  73. nsID mGUID;
  74. };
  75. class gfxDWriteFontFileLoader : public IDWriteFontFileLoader
  76. {
  77. public:
  78. gfxDWriteFontFileLoader()
  79. {
  80. }
  81. // IUnknown interface
  82. IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject)
  83. {
  84. if (iid == __uuidof(IDWriteFontFileLoader)) {
  85. *ppObject = static_cast<IDWriteFontFileLoader*>(this);
  86. return S_OK;
  87. } else if (iid == __uuidof(IUnknown)) {
  88. *ppObject = static_cast<IUnknown*>(this);
  89. return S_OK;
  90. } else {
  91. return E_NOINTERFACE;
  92. }
  93. }
  94. IFACEMETHOD_(ULONG, AddRef)()
  95. {
  96. return 1;
  97. }
  98. IFACEMETHOD_(ULONG, Release)()
  99. {
  100. return 1;
  101. }
  102. // IDWriteFontFileLoader methods
  103. /**
  104. * Important! Note the key here -has- to be a pointer to a uint64_t.
  105. */
  106. virtual HRESULT STDMETHODCALLTYPE
  107. CreateStreamFromKey(void const* fontFileReferenceKey,
  108. UINT32 fontFileReferenceKeySize,
  109. OUT IDWriteFontFileStream** fontFileStream);
  110. /**
  111. * Gets the singleton loader instance. Note that when using this font
  112. * loader, the key must be a pointer to a unint64_t.
  113. */
  114. static IDWriteFontFileLoader* Instance()
  115. {
  116. if (!mInstance) {
  117. mInstance = new gfxDWriteFontFileLoader();
  118. gfxWindowsPlatform::GetPlatform()->GetDWriteFactory()->
  119. RegisterFontFileLoader(mInstance);
  120. }
  121. return mInstance;
  122. }
  123. /**
  124. * Creates a IDWriteFontFile and IDWriteFontFileStream from aFontData.
  125. * aFontData will be empty on return as it swaps out the data.
  126. *
  127. * @param aFontData the font data for the custom font file
  128. * @param aFontFile out param for the created font file
  129. * @param aFontFileStream out param for the corresponding stream
  130. * @return HRESULT of internal calls
  131. */
  132. static HRESULT CreateCustomFontFile(FallibleTArray<uint8_t>& aFontData,
  133. IDWriteFontFile** aFontFile,
  134. IDWriteFontFileStream** aFontFileStream);
  135. private:
  136. static IDWriteFontFileLoader* mInstance;
  137. };
  138. #endif /* GFX_DWRITECOMMON_H */