bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 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 | |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 8 | #ifndef SkDWrite_DEFINED |
| 9 | #define SkDWrite_DEFINED |
| 10 | |
bungeman | b2bed00 | 2015-02-20 12:45:44 -0800 | [diff] [blame] | 11 | #include "SkFontStyle.h" |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 12 | #include "SkTemplates.h" |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 13 | |
| 14 | #include <dwrite.h> |
bungeman | f548444 | 2014-09-10 07:49:05 -0700 | [diff] [blame] | 15 | #include <winsdkver.h> |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 16 | |
| 17 | class SkString; |
| 18 | |
| 19 | //////////////////////////////////////////////////////////////////////////////// |
| 20 | // Factory |
| 21 | |
| 22 | IDWriteFactory* sk_get_dwrite_factory(); |
| 23 | |
| 24 | //////////////////////////////////////////////////////////////////////////////// |
| 25 | // String conversion |
| 26 | |
| 27 | /** Prefer to use this type to prevent template proliferation. */ |
| 28 | typedef SkAutoSTMalloc<16, WCHAR> SkSMallocWCHAR; |
| 29 | |
| 30 | /** Converts a utf8 string to a WCHAR string. */ |
| 31 | HRESULT sk_cstring_to_wchar(const char* skname, SkSMallocWCHAR* name); |
| 32 | |
bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame] | 33 | /** Converts a WCHAR string to a utf8 string. |
| 34 | * @param nameLen the number of WCHARs in the name. |
| 35 | */ |
| 36 | HRESULT sk_wchar_to_skstring(WCHAR* name, int nameLen, SkString* skname); |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 37 | |
| 38 | //////////////////////////////////////////////////////////////////////////////// |
| 39 | // Locale |
| 40 | |
Hal Canary | 8031b32 | 2018-03-29 13:20:30 -0700 | [diff] [blame] | 41 | HRESULT sk_get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* preferedLocale, |
| 42 | SkString* skname); |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 43 | |
george | 8124bf0 | 2014-07-30 09:58:17 -0700 | [diff] [blame] | 44 | typedef int (WINAPI *SkGetUserDefaultLocaleNameProc)(LPWSTR, int); |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 45 | HRESULT SkGetGetUserDefaultLocaleNameProc(SkGetUserDefaultLocaleNameProc* proc); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 46 | |
| 47 | //////////////////////////////////////////////////////////////////////////////// |
| 48 | // Table handling |
| 49 | |
| 50 | class AutoDWriteTable { |
| 51 | public: |
sammc | 98fc73c | 2015-06-30 07:09:49 -0700 | [diff] [blame] | 52 | AutoDWriteTable(IDWriteFontFace* fontFace, UINT32 beTag) : fExists(FALSE), fFontFace(fontFace) { |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 53 | // Any errors are ignored, user must check fExists anyway. |
| 54 | fontFace->TryGetFontTable(beTag, |
| 55 | reinterpret_cast<const void **>(&fData), &fSize, &fLock, &fExists); |
| 56 | } |
| 57 | ~AutoDWriteTable() { |
| 58 | if (fExists) { |
| 59 | fFontFace->ReleaseFontTable(fLock); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | const uint8_t* fData; |
| 64 | UINT32 fSize; |
| 65 | BOOL fExists; |
| 66 | private: |
| 67 | // Borrowed reference, the user must ensure the fontFace stays alive. |
| 68 | IDWriteFontFace* fFontFace; |
| 69 | void* fLock; |
| 70 | }; |
| 71 | template<typename T> class AutoTDWriteTable : public AutoDWriteTable { |
| 72 | public: |
| 73 | static const UINT32 tag = DWRITE_MAKE_OPENTYPE_TAG(T::TAG0, T::TAG1, T::TAG2, T::TAG3); |
| 74 | AutoTDWriteTable(IDWriteFontFace* fontFace) : AutoDWriteTable(fontFace, tag) { } |
| 75 | |
| 76 | const T* get() const { return reinterpret_cast<const T*>(fData); } |
| 77 | const T* operator->() const { return reinterpret_cast<const T*>(fData); } |
| 78 | }; |
| 79 | |
bungeman | b2bed00 | 2015-02-20 12:45:44 -0800 | [diff] [blame] | 80 | //////////////////////////////////////////////////////////////////////////////// |
| 81 | // Style conversion |
| 82 | |
| 83 | struct DWriteStyle { |
| 84 | explicit DWriteStyle(const SkFontStyle& pattern) { |
bungeman | b2bed00 | 2015-02-20 12:45:44 -0800 | [diff] [blame] | 85 | fWeight = (DWRITE_FONT_WEIGHT)pattern.weight(); |
| 86 | fWidth = (DWRITE_FONT_STRETCH)pattern.width(); |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 87 | switch (pattern.slant()) { |
| 88 | case SkFontStyle::kUpright_Slant: fSlant = DWRITE_FONT_STYLE_NORMAL ; break; |
| 89 | case SkFontStyle::kItalic_Slant: fSlant = DWRITE_FONT_STYLE_ITALIC ; break; |
| 90 | case SkFontStyle::kOblique_Slant: fSlant = DWRITE_FONT_STYLE_OBLIQUE; break; |
| 91 | default: SkASSERT(false); break; |
| 92 | } |
bungeman | b2bed00 | 2015-02-20 12:45:44 -0800 | [diff] [blame] | 93 | } |
bungeman | b2bed00 | 2015-02-20 12:45:44 -0800 | [diff] [blame] | 94 | DWRITE_FONT_WEIGHT fWeight; |
| 95 | DWRITE_FONT_STRETCH fWidth; |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 96 | DWRITE_FONT_STYLE fSlant; |
bungeman | b2bed00 | 2015-02-20 12:45:44 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 99 | #endif |