SkDWrite.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2014 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 SkDWrite_DEFINED
  8. #define SkDWrite_DEFINED
  9. #include "include/core/SkFontStyle.h"
  10. #include "include/private/SkTemplates.h"
  11. #include <dwrite.h>
  12. #include <winsdkver.h>
  13. class SkString;
  14. ////////////////////////////////////////////////////////////////////////////////
  15. // Factory
  16. IDWriteFactory* sk_get_dwrite_factory();
  17. ////////////////////////////////////////////////////////////////////////////////
  18. // String conversion
  19. /** Prefer to use this type to prevent template proliferation. */
  20. typedef SkAutoSTMalloc<16, WCHAR> SkSMallocWCHAR;
  21. /** Converts a utf8 string to a WCHAR string. */
  22. HRESULT sk_cstring_to_wchar(const char* skname, SkSMallocWCHAR* name);
  23. /** Converts a WCHAR string to a utf8 string.
  24. * @param nameLen the number of WCHARs in the name.
  25. */
  26. HRESULT sk_wchar_to_skstring(WCHAR* name, int nameLen, SkString* skname);
  27. ////////////////////////////////////////////////////////////////////////////////
  28. // Locale
  29. HRESULT sk_get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* preferedLocale,
  30. SkString* skname);
  31. typedef int (WINAPI *SkGetUserDefaultLocaleNameProc)(LPWSTR, int);
  32. HRESULT SkGetGetUserDefaultLocaleNameProc(SkGetUserDefaultLocaleNameProc* proc);
  33. ////////////////////////////////////////////////////////////////////////////////
  34. // Table handling
  35. class AutoDWriteTable {
  36. public:
  37. AutoDWriteTable(IDWriteFontFace* fontFace, UINT32 beTag) : fExists(FALSE), fFontFace(fontFace) {
  38. // Any errors are ignored, user must check fExists anyway.
  39. fontFace->TryGetFontTable(beTag,
  40. reinterpret_cast<const void **>(&fData), &fSize, &fLock, &fExists);
  41. }
  42. ~AutoDWriteTable() {
  43. if (fExists) {
  44. fFontFace->ReleaseFontTable(fLock);
  45. }
  46. }
  47. const uint8_t* fData;
  48. UINT32 fSize;
  49. BOOL fExists;
  50. private:
  51. // Borrowed reference, the user must ensure the fontFace stays alive.
  52. IDWriteFontFace* fFontFace;
  53. void* fLock;
  54. };
  55. template<typename T> class AutoTDWriteTable : public AutoDWriteTable {
  56. public:
  57. static const UINT32 tag = DWRITE_MAKE_OPENTYPE_TAG(T::TAG0, T::TAG1, T::TAG2, T::TAG3);
  58. AutoTDWriteTable(IDWriteFontFace* fontFace) : AutoDWriteTable(fontFace, tag) { }
  59. const T* get() const { return reinterpret_cast<const T*>(fData); }
  60. const T* operator->() const { return reinterpret_cast<const T*>(fData); }
  61. };
  62. ////////////////////////////////////////////////////////////////////////////////
  63. // Style conversion
  64. struct DWriteStyle {
  65. explicit DWriteStyle(const SkFontStyle& pattern) {
  66. fWeight = (DWRITE_FONT_WEIGHT)pattern.weight();
  67. fWidth = (DWRITE_FONT_STRETCH)pattern.width();
  68. switch (pattern.slant()) {
  69. case SkFontStyle::kUpright_Slant: fSlant = DWRITE_FONT_STYLE_NORMAL ; break;
  70. case SkFontStyle::kItalic_Slant: fSlant = DWRITE_FONT_STYLE_ITALIC ; break;
  71. case SkFontStyle::kOblique_Slant: fSlant = DWRITE_FONT_STYLE_OBLIQUE; break;
  72. default: SkASSERT(false); break;
  73. }
  74. }
  75. DWRITE_FONT_WEIGHT fWeight;
  76. DWRITE_FONT_STRETCH fWidth;
  77. DWRITE_FONT_STYLE fSlant;
  78. };
  79. #endif