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 "include/core/SkFontStyle.h" 12 #include "include/private/SkTemplates.h" 13 14 #include <dwrite.h> 15 #include <winsdkver.h> 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 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); 37 38 //////////////////////////////////////////////////////////////////////////////// 39 // Locale 40 41 HRESULT sk_get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* preferedLocale, 42 SkString* skname); 43 44 typedef int (WINAPI *SkGetUserDefaultLocaleNameProc)(LPWSTR, int); 45 HRESULT SkGetGetUserDefaultLocaleNameProc(SkGetUserDefaultLocaleNameProc* proc); 46 47 //////////////////////////////////////////////////////////////////////////////// 48 // Table handling 49 50 class AutoDWriteTable { 51 public: AutoDWriteTable(IDWriteFontFace * fontFace,UINT32 beTag)52 AutoDWriteTable(IDWriteFontFace* fontFace, UINT32 beTag) : fExists(FALSE), fFontFace(fontFace) { 53 // Any errors are ignored, user must check fExists anyway. 54 fontFace->TryGetFontTable(beTag, 55 reinterpret_cast<const void **>(&fData), &fSize, &fLock, &fExists); 56 } ~AutoDWriteTable()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); AutoTDWriteTable(IDWriteFontFace * fontFace)74 AutoTDWriteTable(IDWriteFontFace* fontFace) : AutoDWriteTable(fontFace, tag) { } 75 get()76 const T* get() const { return reinterpret_cast<const T*>(fData); } 77 const T* operator->() const { return reinterpret_cast<const T*>(fData); } 78 }; 79 80 //////////////////////////////////////////////////////////////////////////////// 81 // Style conversion 82 83 struct DWriteStyle { DWriteStyleDWriteStyle84 explicit DWriteStyle(const SkFontStyle& pattern) { 85 fWeight = (DWRITE_FONT_WEIGHT)pattern.weight(); 86 fWidth = (DWRITE_FONT_STRETCH)pattern.width(); 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 } 93 } 94 DWRITE_FONT_WEIGHT fWeight; 95 DWRITE_FONT_STRETCH fWidth; 96 DWRITE_FONT_STYLE fSlant; 97 }; 98 99 #endif 100