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