Home | History | Annotate | Download | only in win
      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 
     15 class SkString;
     16 
     17 ////////////////////////////////////////////////////////////////////////////////
     18 // Factory
     19 
     20 IDWriteFactory* sk_get_dwrite_factory();
     21 
     22 ////////////////////////////////////////////////////////////////////////////////
     23 // String conversion
     24 
     25 /** Prefer to use this type to prevent template proliferation. */
     26 typedef SkAutoSTMalloc<16, WCHAR> SkSMallocWCHAR;
     27 
     28 /** Converts a utf8 string to a WCHAR string. */
     29 HRESULT sk_cstring_to_wchar(const char* skname, SkSMallocWCHAR* name);
     30 
     31 /** Converts a WCHAR string to a utf8 string. */
     32 HRESULT sk_wchar_to_skstring(WCHAR* name, SkString* skname);
     33 
     34 ////////////////////////////////////////////////////////////////////////////////
     35 // Locale
     36 
     37 void sk_get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* preferedLocale,
     38                        SkString* skname);
     39 
     40 typedef decltype(GetUserDefaultLocaleName)* SkGetUserDefaultLocaleNameProc;
     41 HRESULT SkGetGetUserDefaultLocaleNameProc(SkGetUserDefaultLocaleNameProc* proc);
     42 
     43 ////////////////////////////////////////////////////////////////////////////////
     44 // Table handling
     45 
     46 class AutoDWriteTable {
     47 public:
     48     AutoDWriteTable(IDWriteFontFace* fontFace, UINT32 beTag) : fFontFace(fontFace), fExists(FALSE) {
     49         // Any errors are ignored, user must check fExists anyway.
     50         fontFace->TryGetFontTable(beTag,
     51             reinterpret_cast<const void **>(&fData), &fSize, &fLock, &fExists);
     52     }
     53     ~AutoDWriteTable() {
     54         if (fExists) {
     55             fFontFace->ReleaseFontTable(fLock);
     56         }
     57     }
     58 
     59     const uint8_t* fData;
     60     UINT32 fSize;
     61     BOOL fExists;
     62 private:
     63     // Borrowed reference, the user must ensure the fontFace stays alive.
     64     IDWriteFontFace* fFontFace;
     65     void* fLock;
     66 };
     67 template<typename T> class AutoTDWriteTable : public AutoDWriteTable {
     68 public:
     69     static const UINT32 tag = DWRITE_MAKE_OPENTYPE_TAG(T::TAG0, T::TAG1, T::TAG2, T::TAG3);
     70     AutoTDWriteTable(IDWriteFontFace* fontFace) : AutoDWriteTable(fontFace, tag) { }
     71 
     72     const T* get() const { return reinterpret_cast<const T*>(fData); }
     73     const T* operator->() const { return reinterpret_cast<const T*>(fData); }
     74 };
     75 
     76 #endif
     77