• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "include/core/SkTypes.h"
8 #if defined(SK_BUILD_FOR_WIN)
9 
10 #include "include/core/SkString.h"
11 #include "include/private/SkOnce.h"
12 #include "src/utils/win/SkDWrite.h"
13 #include "src/utils/win/SkHRESULT.h"
14 
15 #include <dwrite.h>
16 
17 #if defined(__clang__)
18     #pragma clang diagnostic push
19     #pragma clang diagnostic ignored "-Wcast-function-type"
20 #endif
21 
22 static IDWriteFactory* gDWriteFactory = nullptr;
23 
release_dwrite_factory()24 static void release_dwrite_factory() {
25     if (gDWriteFactory) {
26         gDWriteFactory->Release();
27     }
28 }
29 
create_dwrite_factory(IDWriteFactory ** factory)30 static void create_dwrite_factory(IDWriteFactory** factory) {
31     typedef decltype(DWriteCreateFactory)* DWriteCreateFactoryProc;
32     DWriteCreateFactoryProc dWriteCreateFactoryProc = reinterpret_cast<DWriteCreateFactoryProc>(
33         GetProcAddress(LoadLibraryW(L"dwrite.dll"), "DWriteCreateFactory"));
34 
35     if (!dWriteCreateFactoryProc) {
36         HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
37         if (!IS_ERROR(hr)) {
38             hr = ERROR_PROC_NOT_FOUND;
39         }
40         HRVM(hr, "Could not get DWriteCreateFactory proc.");
41     }
42 
43     HRVM(dWriteCreateFactoryProc(DWRITE_FACTORY_TYPE_SHARED,
44                                  __uuidof(IDWriteFactory),
45                                  reinterpret_cast<IUnknown**>(factory)),
46          "Could not create DirectWrite factory.");
47     atexit(release_dwrite_factory);
48 }
49 
50 
sk_get_dwrite_factory()51 IDWriteFactory* sk_get_dwrite_factory() {
52     static SkOnce once;
53     once(create_dwrite_factory, &gDWriteFactory);
54     return gDWriteFactory;
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 // String conversion
59 
60 /** Converts a utf8 string to a WCHAR string. */
sk_cstring_to_wchar(const char * skname,SkSMallocWCHAR * name)61 HRESULT sk_cstring_to_wchar(const char* skname, SkSMallocWCHAR* name) {
62     int wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, nullptr, 0);
63     if (0 == wlen) {
64         HRM(HRESULT_FROM_WIN32(GetLastError()),
65             "Could not get length for wchar to utf-8 conversion.");
66     }
67     name->reset(wlen);
68     wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, name->get(), wlen);
69     if (0 == wlen) {
70         HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert wchar to utf-8.");
71     }
72     return S_OK;
73 }
74 
75 /** Converts a WCHAR string to a utf8 string. */
sk_wchar_to_skstring(WCHAR * name,int nameLen,SkString * skname)76 HRESULT sk_wchar_to_skstring(WCHAR* name, int nameLen, SkString* skname) {
77     int len = WideCharToMultiByte(CP_UTF8, 0, name, nameLen, nullptr, 0, nullptr, nullptr);
78     if (0 == len) {
79         if (nameLen <= 0) {
80             skname->reset();
81             return S_OK;
82         }
83         HRM(HRESULT_FROM_WIN32(GetLastError()),
84             "Could not get length for utf-8 to wchar conversion.");
85     }
86     skname->resize(len);
87 
88     len = WideCharToMultiByte(CP_UTF8, 0, name, nameLen, skname->writable_str(), len, nullptr, nullptr);
89     if (0 == len) {
90         HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert utf-8 to wchar.");
91     }
92     return S_OK;
93 }
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 // Locale
97 
sk_get_locale_string(IDWriteLocalizedStrings * names,const WCHAR * preferedLocale,SkString * skname)98 HRESULT sk_get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* preferedLocale,
99                              SkString* skname) {
100     UINT32 nameIndex = 0;
101     if (preferedLocale) {
102         // Ignore any errors and continue with index 0 if there is a problem.
103         BOOL nameExists = FALSE;
104         (void)names->FindLocaleName(preferedLocale, &nameIndex, &nameExists);
105         if (!nameExists) {
106             nameIndex = 0;
107         }
108     }
109 
110     UINT32 nameLen;
111     HRM(names->GetStringLength(nameIndex, &nameLen), "Could not get name length.");
112 
113     SkSMallocWCHAR name(nameLen + 1);
114     HRM(names->GetString(nameIndex, name.get(), nameLen + 1), "Could not get string.");
115 
116     HR(sk_wchar_to_skstring(name.get(), nameLen, skname));
117     return S_OK;
118 }
119 
SkGetGetUserDefaultLocaleNameProc(SkGetUserDefaultLocaleNameProc * proc)120 HRESULT SkGetGetUserDefaultLocaleNameProc(SkGetUserDefaultLocaleNameProc* proc) {
121     *proc = reinterpret_cast<SkGetUserDefaultLocaleNameProc>(
122         GetProcAddress(LoadLibraryW(L"Kernel32.dll"), "GetUserDefaultLocaleName")
123     );
124     if (!*proc) {
125         HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
126         if (!IS_ERROR(hr)) {
127             hr = ERROR_PROC_NOT_FOUND;
128         }
129         return hr;
130     }
131     return S_OK;
132 }
133 
134 #if defined(__clang__)
135     #pragma clang diagnostic pop
136 #endif
137 
138 #endif//defined(SK_BUILD_FOR_WIN)
139