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