blob: 26700fa22e88443a9c2b4d476343deaf2738dfe1 [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
halcanary96fcdcc2015-08-27 07:41:13 -070017static IDWriteFactory* gDWriteFactory = nullptr;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000018
mtklein1b818772014-06-02 11:26:59 -070019static void release_dwrite_factory() {
20 if (gDWriteFactory) {
21 gDWriteFactory->Release();
22 }
23}
24
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000025static void create_dwrite_factory(IDWriteFactory** factory) {
26 typedef decltype(DWriteCreateFactory)* DWriteCreateFactoryProc;
27 DWriteCreateFactoryProc dWriteCreateFactoryProc = reinterpret_cast<DWriteCreateFactoryProc>(
28 GetProcAddress(LoadLibraryW(L"dwrite.dll"), "DWriteCreateFactory"));
29
30 if (!dWriteCreateFactoryProc) {
31 HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
32 if (!IS_ERROR(hr)) {
33 hr = ERROR_PROC_NOT_FOUND;
34 }
35 HRVM(hr, "Could not get DWriteCreateFactory proc.");
36 }
37
38 HRVM(dWriteCreateFactoryProc(DWRITE_FACTORY_TYPE_SHARED,
39 __uuidof(IDWriteFactory),
40 reinterpret_cast<IUnknown**>(factory)),
41 "Could not create DirectWrite factory.");
mtklein1b818772014-06-02 11:26:59 -070042 atexit(release_dwrite_factory);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000043}
44
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000045
46IDWriteFactory* sk_get_dwrite_factory() {
mtkleind9dd4282016-04-18 08:09:11 -070047 static SkOnce once;
48 once(create_dwrite_factory, &gDWriteFactory);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000049 return gDWriteFactory;
50}
51
52////////////////////////////////////////////////////////////////////////////////
53// String conversion
54
55/** Converts a utf8 string to a WCHAR string. */
56HRESULT sk_cstring_to_wchar(const char* skname, SkSMallocWCHAR* name) {
halcanary96fcdcc2015-08-27 07:41:13 -070057 int wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, nullptr, 0);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000058 if (0 == wlen) {
59 HRM(HRESULT_FROM_WIN32(GetLastError()),
60 "Could not get length for wchar to utf-8 conversion.");
61 }
62 name->reset(wlen);
63 wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, name->get(), wlen);
64 if (0 == wlen) {
65 HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert wchar to utf-8.");
66 }
67 return S_OK;
68}
69
70/** Converts a WCHAR string to a utf8 string. */
bungeman5e7b4f92014-08-25 10:16:01 -070071HRESULT sk_wchar_to_skstring(WCHAR* name, int nameLen, SkString* skname) {
halcanary96fcdcc2015-08-27 07:41:13 -070072 int len = WideCharToMultiByte(CP_UTF8, 0, name, nameLen, nullptr, 0, nullptr, nullptr);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000073 if (0 == len) {
bungeman5e7b4f92014-08-25 10:16:01 -070074 if (nameLen <= 0) {
75 skname->reset();
76 return S_OK;
77 }
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000078 HRM(HRESULT_FROM_WIN32(GetLastError()),
79 "Could not get length for utf-8 to wchar conversion.");
80 }
bungeman5e7b4f92014-08-25 10:16:01 -070081 skname->resize(len);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000082
halcanary96fcdcc2015-08-27 07:41:13 -070083 len = WideCharToMultiByte(CP_UTF8, 0, name, nameLen, skname->writable_str(), len, nullptr, nullptr);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000084 if (0 == len) {
85 HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert utf-8 to wchar.");
86 }
87 return S_OK;
88}
89
90////////////////////////////////////////////////////////////////////////////////
91// Locale
92
Hal Canary8031b322018-03-29 13:20:30 -070093HRESULT sk_get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* preferedLocale,
94 SkString* skname) {
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000095 UINT32 nameIndex = 0;
96 if (preferedLocale) {
97 // Ignore any errors and continue with index 0 if there is a problem.
Hal Canary8031b322018-03-29 13:20:30 -070098 BOOL nameExists = FALSE;
99 (void)names->FindLocaleName(preferedLocale, &nameIndex, &nameExists);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000100 if (!nameExists) {
101 nameIndex = 0;
102 }
103 }
104
bungeman5e7b4f92014-08-25 10:16:01 -0700105 UINT32 nameLen;
Hal Canary8031b322018-03-29 13:20:30 -0700106 HRM(names->GetStringLength(nameIndex, &nameLen), "Could not get name length.");
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000107
Hal Canary8031b322018-03-29 13:20:30 -0700108 SkSMallocWCHAR name(nameLen + 1);
109 HRM(names->GetString(nameIndex, name.get(), nameLen + 1), "Could not get string.");
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000110
Hal Canary8031b322018-03-29 13:20:30 -0700111 HR(sk_wchar_to_skstring(name.get(), nameLen, skname));
112 return S_OK;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000113}
114
115HRESULT SkGetGetUserDefaultLocaleNameProc(SkGetUserDefaultLocaleNameProc* proc) {
116 *proc = reinterpret_cast<SkGetUserDefaultLocaleNameProc>(
117 GetProcAddress(LoadLibraryW(L"Kernel32.dll"), "GetUserDefaultLocaleName")
118 );
119 if (!*proc) {
120 HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
121 if (!IS_ERROR(hr)) {
122 hr = ERROR_PROC_NOT_FOUND;
123 }
124 return hr;
125 }
126 return S_OK;
127}
mtklein1ee76512015-11-02 10:20:27 -0800128
Mike Klein8f11d4d2018-01-24 12:42:55 -0500129#endif//defined(SK_BUILD_FOR_WIN)