Flutter Engine
The Flutter Engine
SkDWrite.cpp
Go to the documentation of this file.
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 */
8#if defined(SK_BUILD_FOR_WIN)
9
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
22static IDWriteFactory* gDWriteFactory = nullptr;
23
24static void release_dwrite_factory() {
25 if (gDWriteFactory) {
26 gDWriteFactory->Release();
27 }
28}
29
30static void create_dwrite_factory(IDWriteFactory** factory) {
31 using DWriteCreateFactoryProc = decltype(DWriteCreateFactory)*;
32 DWriteCreateFactoryProc dWriteCreateFactoryProc;
33
34 dWriteCreateFactoryProc = reinterpret_cast<DWriteCreateFactoryProc>(
35 GetProcAddress(LoadLibraryW(L"DWriteCore.dll"), "DWriteCoreCreateFactory"));
36
37 if (!dWriteCreateFactoryProc) {
38 dWriteCreateFactoryProc = reinterpret_cast<DWriteCreateFactoryProc>(
39 GetProcAddress(LoadLibraryW(L"dwrite.dll"), "DWriteCreateFactory"));
40 }
41
42 if (!dWriteCreateFactoryProc) {
43 HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
44 if (!IS_ERROR(hr)) {
45 hr = ERROR_PROC_NOT_FOUND;
46 }
47 HRVM(hr, "Could not get DWriteCreateFactory proc.");
48 }
49
50 HRVM(dWriteCreateFactoryProc(DWRITE_FACTORY_TYPE_SHARED,
51 __uuidof(IDWriteFactory),
52 reinterpret_cast<IUnknown**>(factory)),
53 "Could not create DirectWrite factory.");
54 atexit(release_dwrite_factory);
55}
56
57
58IDWriteFactory* sk_get_dwrite_factory() {
59 static SkOnce once;
60 once(create_dwrite_factory, &gDWriteFactory);
61 return gDWriteFactory;
62}
63
64////////////////////////////////////////////////////////////////////////////////
65// String conversion
66
67/** Converts a utf8 string to a WCHAR string. */
68HRESULT sk_cstring_to_wchar(const char* skname, SkSMallocWCHAR* name) {
69 int wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, nullptr, 0);
70 if (0 == wlen) {
71 HRM(HRESULT_FROM_WIN32(GetLastError()),
72 "Could not get length for wchar to utf-8 conversion.");
73 }
74 name->reset(wlen);
75 wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, name->get(), wlen);
76 if (0 == wlen) {
77 HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert wchar to utf-8.");
78 }
79 return S_OK;
80}
81
82/** Converts a WCHAR string to a utf8 string. */
83HRESULT sk_wchar_to_skstring(WCHAR* name, int nameLen, SkString* skname) {
84 int len = WideCharToMultiByte(CP_UTF8, 0, name, nameLen, nullptr, 0, nullptr, nullptr);
85 if (0 == len) {
86 if (nameLen <= 0) {
87 skname->reset();
88 return S_OK;
89 }
90 HRM(HRESULT_FROM_WIN32(GetLastError()),
91 "Could not get length for utf-8 to wchar conversion.");
92 }
93 skname->resize(len);
94
95 len = WideCharToMultiByte(CP_UTF8, 0, name, nameLen, skname->data(), len, nullptr, nullptr);
96 if (0 == len) {
97 HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert utf-8 to wchar.");
98 }
99 return S_OK;
100}
101
102////////////////////////////////////////////////////////////////////////////////
103// Locale
104
105HRESULT sk_get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* preferedLocale,
106 SkString* skname) {
107 UINT32 nameIndex = 0;
108 if (preferedLocale) {
109 // Ignore any errors and continue with index 0 if there is a problem.
110 BOOL nameExists = FALSE;
111 (void)names->FindLocaleName(preferedLocale, &nameIndex, &nameExists);
112 if (!nameExists) {
113 nameIndex = 0;
114 }
115 }
116
117 UINT32 nameLen;
118 HRM(names->GetStringLength(nameIndex, &nameLen), "Could not get name length.");
119
120 SkSMallocWCHAR name(nameLen + 1);
121 HRM(names->GetString(nameIndex, name.get(), nameLen + 1), "Could not get string.");
122
123 HR(sk_wchar_to_skstring(name.get(), nameLen, skname));
124 return S_OK;
125}
126
128 *proc = reinterpret_cast<SkGetUserDefaultLocaleNameProc>(
129 GetProcAddress(LoadLibraryW(L"Kernel32.dll"), "GetUserDefaultLocaleName")
130 );
131 if (!*proc) {
132 HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
133 if (!IS_ERROR(hr)) {
134 hr = ERROR_PROC_NOT_FOUND;
135 }
136 return hr;
137 }
138 return S_OK;
139}
140
141#if defined(__clang__)
142 #pragma clang diagnostic pop
143#endif
144
145#endif//defined(SK_BUILD_FOR_WIN)
HRESULT sk_wchar_to_skstring(WCHAR *name, int nameLen, SkString *skname)
HRESULT sk_get_locale_string(IDWriteLocalizedStrings *names, const WCHAR *preferedLocale, SkString *skname)
int(WINAPI * SkGetUserDefaultLocaleNameProc)(LPWSTR, int)
Definition: SkDWrite.h:44
HRESULT sk_cstring_to_wchar(const char *skname, SkSMallocWCHAR *name)
HRESULT SkGetGetUserDefaultLocaleNameProc(SkGetUserDefaultLocaleNameProc *proc)
IDWriteFactory * sk_get_dwrite_factory()
Definition: SkOnce.h:22
const char * data() const
Definition: SkString.h:132
void resize(size_t len)
Definition: SkString.cpp:374
void reset()
Definition: SkString.cpp:358
return FALSE
static const char *const names[]
Definition: symbols.cc:24
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
int BOOL
Definition: windows_types.h:37
WINBASEAPI _Check_return_ _Post_equals_last_error_ DWORD WINAPI GetLastError(VOID)