blob: 9847c41eb0e81ef4aecc39852d7938da52a8c385 [file] [log] [blame]
bungeman@google.com72cf4fc2014-03-21 22:48:32 +00001/*
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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "include/core/SkTypes.h"
Mike Klein8f11d4d2018-01-24 12:42:55 -05008#if defined(SK_BUILD_FOR_WIN)
bungeman@google.com72cf4fc2014-03-21 22:48:32 +00009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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"
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000014
15#include <dwrite.h>
16
Brian Osman759802f2021-06-15 13:19:08 -040017#if defined(__clang__)
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wcast-function-type"
20#endif
21
halcanary96fcdcc2015-08-27 07:41:13 -070022static IDWriteFactory* gDWriteFactory = nullptr;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000023
mtklein1b818772014-06-02 11:26:59 -070024static void release_dwrite_factory() {
25 if (gDWriteFactory) {
26 gDWriteFactory->Release();
27 }
28}
29
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000030static 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.");
mtklein1b818772014-06-02 11:26:59 -070047 atexit(release_dwrite_factory);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000048}
49
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000050
51IDWriteFactory* sk_get_dwrite_factory() {
mtkleind9dd4282016-04-18 08:09:11 -070052 static SkOnce once;
53 once(create_dwrite_factory, &gDWriteFactory);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000054 return gDWriteFactory;
55}
56
57////////////////////////////////////////////////////////////////////////////////
58// String conversion
59
60/** Converts a utf8 string to a WCHAR string. */
61HRESULT sk_cstring_to_wchar(const char* skname, SkSMallocWCHAR* name) {
halcanary96fcdcc2015-08-27 07:41:13 -070062 int wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, nullptr, 0);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000063 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. */
bungeman5e7b4f92014-08-25 10:16:01 -070076HRESULT sk_wchar_to_skstring(WCHAR* name, int nameLen, SkString* skname) {
halcanary96fcdcc2015-08-27 07:41:13 -070077 int len = WideCharToMultiByte(CP_UTF8, 0, name, nameLen, nullptr, 0, nullptr, nullptr);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000078 if (0 == len) {
bungeman5e7b4f92014-08-25 10:16:01 -070079 if (nameLen <= 0) {
80 skname->reset();
81 return S_OK;
82 }
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000083 HRM(HRESULT_FROM_WIN32(GetLastError()),
84 "Could not get length for utf-8 to wchar conversion.");
85 }
bungeman5e7b4f92014-08-25 10:16:01 -070086 skname->resize(len);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000087
halcanary96fcdcc2015-08-27 07:41:13 -070088 len = WideCharToMultiByte(CP_UTF8, 0, name, nameLen, skname->writable_str(), len, nullptr, nullptr);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000089 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
Hal Canary8031b322018-03-29 13:20:30 -070098HRESULT sk_get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* preferedLocale,
99 SkString* skname) {
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000100 UINT32 nameIndex = 0;
101 if (preferedLocale) {
102 // Ignore any errors and continue with index 0 if there is a problem.
Hal Canary8031b322018-03-29 13:20:30 -0700103 BOOL nameExists = FALSE;
104 (void)names->FindLocaleName(preferedLocale, &nameIndex, &nameExists);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000105 if (!nameExists) {
106 nameIndex = 0;
107 }
108 }
109
bungeman5e7b4f92014-08-25 10:16:01 -0700110 UINT32 nameLen;
Hal Canary8031b322018-03-29 13:20:30 -0700111 HRM(names->GetStringLength(nameIndex, &nameLen), "Could not get name length.");
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000112
Hal Canary8031b322018-03-29 13:20:30 -0700113 SkSMallocWCHAR name(nameLen + 1);
114 HRM(names->GetString(nameIndex, name.get(), nameLen + 1), "Could not get string.");
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000115
Hal Canary8031b322018-03-29 13:20:30 -0700116 HR(sk_wchar_to_skstring(name.get(), nameLen, skname));
117 return S_OK;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000118}
119
120HRESULT 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}
mtklein1ee76512015-11-02 10:20:27 -0800133
Brian Osman759802f2021-06-15 13:19:08 -0400134#if defined(__clang__)
135 #pragma clang diagnostic pop
136#endif
137
Mike Klein8f11d4d2018-01-24 12:42:55 -0500138#endif//defined(SK_BUILD_FOR_WIN)