OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkDWrite.h" | 8 #include "SkDWrite.h" |
9 #include "SkHRESULT.h" | 9 #include "SkHRESULT.h" |
10 #include "SkOnce.h" | 10 #include "SkOnce.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 } | 60 } |
61 name->reset(wlen); | 61 name->reset(wlen); |
62 wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, name->get(), wlen); | 62 wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, name->get(), wlen); |
63 if (0 == wlen) { | 63 if (0 == wlen) { |
64 HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert wchar to utf-
8."); | 64 HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert wchar to utf-
8."); |
65 } | 65 } |
66 return S_OK; | 66 return S_OK; |
67 } | 67 } |
68 | 68 |
69 /** Converts a WCHAR string to a utf8 string. */ | 69 /** Converts a WCHAR string to a utf8 string. */ |
70 HRESULT sk_wchar_to_skstring(WCHAR* name, SkString* skname) { | 70 HRESULT sk_wchar_to_skstring(WCHAR* name, int nameLen, SkString* skname) { |
71 int len = WideCharToMultiByte(CP_UTF8, 0, name, -1, NULL, 0, NULL, NULL); | 71 int len = WideCharToMultiByte(CP_UTF8, 0, name, nameLen, NULL, 0, NULL, NULL
); |
72 if (0 == len) { | 72 if (0 == len) { |
| 73 if (nameLen <= 0) { |
| 74 skname->reset(); |
| 75 return S_OK; |
| 76 } |
73 HRM(HRESULT_FROM_WIN32(GetLastError()), | 77 HRM(HRESULT_FROM_WIN32(GetLastError()), |
74 "Could not get length for utf-8 to wchar conversion."); | 78 "Could not get length for utf-8 to wchar conversion."); |
75 } | 79 } |
76 skname->resize(len - 1); | 80 skname->resize(len); |
77 | 81 |
78 // TODO: remove after https://code.google.com/p/skia/issues/detail?id=1989 i
s fixed. | 82 len = WideCharToMultiByte(CP_UTF8, 0, name, nameLen, skname->writable_str(),
len, NULL, NULL); |
79 // If we resize to 0 then the skname points to gEmptyRec (the unique empty S
kString::Rec). | |
80 // gEmptyRec is static const and on Windows this means the value is in a rea
d only page. | |
81 // Writing to it in the following call to WideCharToMultiByte will cause an
access violation. | |
82 if (1 == len) { | |
83 return S_OK; | |
84 } | |
85 | |
86 len = WideCharToMultiByte(CP_UTF8, 0, name, -1, skname->writable_str(), len,
NULL, NULL); | |
87 if (0 == len) { | 83 if (0 == len) { |
88 HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert utf-8 to wcha
r."); | 84 HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert utf-8 to wcha
r."); |
89 } | 85 } |
90 return S_OK; | 86 return S_OK; |
91 } | 87 } |
92 | 88 |
93 //////////////////////////////////////////////////////////////////////////////// | 89 //////////////////////////////////////////////////////////////////////////////// |
94 // Locale | 90 // Locale |
95 | 91 |
96 void sk_get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* preferedL
ocale, | 92 void sk_get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* preferedL
ocale, |
97 SkString* skname) { | 93 SkString* skname) { |
98 UINT32 nameIndex = 0; | 94 UINT32 nameIndex = 0; |
99 if (preferedLocale) { | 95 if (preferedLocale) { |
100 // Ignore any errors and continue with index 0 if there is a problem. | 96 // Ignore any errors and continue with index 0 if there is a problem. |
101 BOOL nameExists; | 97 BOOL nameExists; |
102 names->FindLocaleName(preferedLocale, &nameIndex, &nameExists); | 98 names->FindLocaleName(preferedLocale, &nameIndex, &nameExists); |
103 if (!nameExists) { | 99 if (!nameExists) { |
104 nameIndex = 0; | 100 nameIndex = 0; |
105 } | 101 } |
106 } | 102 } |
107 | 103 |
108 UINT32 nameLength; | 104 UINT32 nameLen; |
109 HRVM(names->GetStringLength(nameIndex, &nameLength), "Could not get name len
gth."); | 105 HRVM(names->GetStringLength(nameIndex, &nameLen), "Could not get name length
."); |
110 nameLength += 1; | |
111 | 106 |
112 SkSMallocWCHAR name(nameLength); | 107 SkSMallocWCHAR name(nameLen+1); |
113 HRVM(names->GetString(nameIndex, name.get(), nameLength), "Could not get str
ing."); | 108 HRVM(names->GetString(nameIndex, name.get(), nameLen+1), "Could not get stri
ng."); |
114 | 109 |
115 HRV(sk_wchar_to_skstring(name.get(), skname)); | 110 HRV(sk_wchar_to_skstring(name.get(), nameLen, skname)); |
116 } | 111 } |
117 | 112 |
118 HRESULT SkGetGetUserDefaultLocaleNameProc(SkGetUserDefaultLocaleNameProc* proc)
{ | 113 HRESULT SkGetGetUserDefaultLocaleNameProc(SkGetUserDefaultLocaleNameProc* proc)
{ |
119 *proc = reinterpret_cast<SkGetUserDefaultLocaleNameProc>( | 114 *proc = reinterpret_cast<SkGetUserDefaultLocaleNameProc>( |
120 GetProcAddress(LoadLibraryW(L"Kernel32.dll"), "GetUserDefaultLocaleName"
) | 115 GetProcAddress(LoadLibraryW(L"Kernel32.dll"), "GetUserDefaultLocaleName"
) |
121 ); | 116 ); |
122 if (!*proc) { | 117 if (!*proc) { |
123 HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); | 118 HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); |
124 if (!IS_ERROR(hr)) { | 119 if (!IS_ERROR(hr)) { |
125 hr = ERROR_PROC_NOT_FOUND; | 120 hr = ERROR_PROC_NOT_FOUND; |
126 } | 121 } |
127 return hr; | 122 return hr; |
128 } | 123 } |
129 return S_OK; | 124 return S_OK; |
130 } | 125 } |
OLD | NEW |