| bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 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 |  | 
|  | 8 | #include "SkDWrite.h" | 
|  | 9 | #include "SkHRESULT.h" | 
|  | 10 | #include "SkOnce.h" | 
|  | 11 | #include "SkString.h" | 
|  | 12 |  | 
|  | 13 | #include <dwrite.h> | 
|  | 14 |  | 
|  | 15 | static IDWriteFactory* gDWriteFactory = NULL; | 
|  | 16 |  | 
| mtklein | 1b81877 | 2014-06-02 11:26:59 -0700 | [diff] [blame] | 17 | static void release_dwrite_factory() { | 
|  | 18 | if (gDWriteFactory) { | 
|  | 19 | gDWriteFactory->Release(); | 
|  | 20 | } | 
|  | 21 | } | 
|  | 22 |  | 
| bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 23 | static void create_dwrite_factory(IDWriteFactory** factory) { | 
|  | 24 | typedef decltype(DWriteCreateFactory)* DWriteCreateFactoryProc; | 
|  | 25 | DWriteCreateFactoryProc dWriteCreateFactoryProc = reinterpret_cast<DWriteCreateFactoryProc>( | 
|  | 26 | GetProcAddress(LoadLibraryW(L"dwrite.dll"), "DWriteCreateFactory")); | 
|  | 27 |  | 
|  | 28 | if (!dWriteCreateFactoryProc) { | 
|  | 29 | HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); | 
|  | 30 | if (!IS_ERROR(hr)) { | 
|  | 31 | hr = ERROR_PROC_NOT_FOUND; | 
|  | 32 | } | 
|  | 33 | HRVM(hr, "Could not get DWriteCreateFactory proc."); | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | HRVM(dWriteCreateFactoryProc(DWRITE_FACTORY_TYPE_SHARED, | 
|  | 37 | __uuidof(IDWriteFactory), | 
|  | 38 | reinterpret_cast<IUnknown**>(factory)), | 
|  | 39 | "Could not create DirectWrite factory."); | 
| mtklein | 1b81877 | 2014-06-02 11:26:59 -0700 | [diff] [blame] | 40 | atexit(release_dwrite_factory); | 
| bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
| bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 43 |  | 
|  | 44 | IDWriteFactory* sk_get_dwrite_factory() { | 
|  | 45 | SK_DECLARE_STATIC_ONCE(once); | 
| mtklein | 1b81877 | 2014-06-02 11:26:59 -0700 | [diff] [blame] | 46 | SkOnce(&once, create_dwrite_factory, &gDWriteFactory); | 
| bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 47 |  | 
|  | 48 | return gDWriteFactory; | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | //////////////////////////////////////////////////////////////////////////////// | 
|  | 52 | // String conversion | 
|  | 53 |  | 
|  | 54 | /** Converts a utf8 string to a WCHAR string. */ | 
|  | 55 | HRESULT sk_cstring_to_wchar(const char* skname, SkSMallocWCHAR* name) { | 
|  | 56 | int wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, NULL, 0); | 
|  | 57 | if (0 == wlen) { | 
|  | 58 | HRM(HRESULT_FROM_WIN32(GetLastError()), | 
|  | 59 | "Could not get length for wchar to utf-8 conversion."); | 
|  | 60 | } | 
|  | 61 | name->reset(wlen); | 
|  | 62 | wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, name->get(), wlen); | 
|  | 63 | if (0 == wlen) { | 
|  | 64 | HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert wchar to utf-8."); | 
|  | 65 | } | 
|  | 66 | return S_OK; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | /** Converts a WCHAR string to a utf8 string. */ | 
| bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame^] | 70 | HRESULT sk_wchar_to_skstring(WCHAR* name, int nameLen, SkString* skname) { | 
|  | 71 | int len = WideCharToMultiByte(CP_UTF8, 0, name, nameLen, NULL, 0, NULL, NULL); | 
| bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 72 | if (0 == len) { | 
| bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame^] | 73 | if (nameLen <= 0) { | 
|  | 74 | skname->reset(); | 
|  | 75 | return S_OK; | 
|  | 76 | } | 
| bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 77 | HRM(HRESULT_FROM_WIN32(GetLastError()), | 
|  | 78 | "Could not get length for utf-8 to wchar conversion."); | 
|  | 79 | } | 
| bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame^] | 80 | skname->resize(len); | 
| bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 81 |  | 
| bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame^] | 82 | len = WideCharToMultiByte(CP_UTF8, 0, name, nameLen, skname->writable_str(), len, NULL, NULL); | 
| bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 83 | if (0 == len) { | 
|  | 84 | HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert utf-8 to wchar."); | 
|  | 85 | } | 
|  | 86 | return S_OK; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | //////////////////////////////////////////////////////////////////////////////// | 
|  | 90 | // Locale | 
|  | 91 |  | 
|  | 92 | void sk_get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* preferedLocale, | 
|  | 93 | SkString* skname) { | 
|  | 94 | UINT32 nameIndex = 0; | 
|  | 95 | if (preferedLocale) { | 
|  | 96 | // Ignore any errors and continue with index 0 if there is a problem. | 
|  | 97 | BOOL nameExists; | 
|  | 98 | names->FindLocaleName(preferedLocale, &nameIndex, &nameExists); | 
|  | 99 | if (!nameExists) { | 
|  | 100 | nameIndex = 0; | 
|  | 101 | } | 
|  | 102 | } | 
|  | 103 |  | 
| bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame^] | 104 | UINT32 nameLen; | 
|  | 105 | HRVM(names->GetStringLength(nameIndex, &nameLen), "Could not get name length."); | 
| bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 106 |  | 
| bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame^] | 107 | SkSMallocWCHAR name(nameLen+1); | 
|  | 108 | HRVM(names->GetString(nameIndex, name.get(), nameLen+1), "Could not get string."); | 
| bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 109 |  | 
| bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame^] | 110 | HRV(sk_wchar_to_skstring(name.get(), nameLen, skname)); | 
| bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 111 | } | 
|  | 112 |  | 
|  | 113 | HRESULT SkGetGetUserDefaultLocaleNameProc(SkGetUserDefaultLocaleNameProc* proc) { | 
|  | 114 | *proc = reinterpret_cast<SkGetUserDefaultLocaleNameProc>( | 
|  | 115 | GetProcAddress(LoadLibraryW(L"Kernel32.dll"), "GetUserDefaultLocaleName") | 
|  | 116 | ); | 
|  | 117 | if (!*proc) { | 
|  | 118 | HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); | 
|  | 119 | if (!IS_ERROR(hr)) { | 
|  | 120 | hr = ERROR_PROC_NOT_FOUND; | 
|  | 121 | } | 
|  | 122 | return hr; | 
|  | 123 | } | 
|  | 124 | return S_OK; | 
|  | 125 | } |