summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/skia/src/utils/win
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/third_party/skia/src/utils/win')
-rw-r--r--chromium/third_party/skia/src/utils/win/SkDWrite.cpp130
-rw-r--r--chromium/third_party/skia/src/utils/win/SkDWrite.h76
-rw-r--r--chromium/third_party/skia/src/utils/win/SkDWriteFontFileStream.h1
-rw-r--r--chromium/third_party/skia/src/utils/win/SkHRESULT.cpp15
4 files changed, 215 insertions, 7 deletions
diff --git a/chromium/third_party/skia/src/utils/win/SkDWrite.cpp b/chromium/third_party/skia/src/utils/win/SkDWrite.cpp
new file mode 100644
index 00000000000..87826b5194e
--- /dev/null
+++ b/chromium/third_party/skia/src/utils/win/SkDWrite.cpp
@@ -0,0 +1,130 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkDWrite.h"
+#include "SkHRESULT.h"
+#include "SkOnce.h"
+#include "SkString.h"
+
+#include <dwrite.h>
+
+static IDWriteFactory* gDWriteFactory = NULL;
+
+static void release_dwrite_factory() {
+ if (gDWriteFactory) {
+ gDWriteFactory->Release();
+ }
+}
+
+static void create_dwrite_factory(IDWriteFactory** factory) {
+ typedef decltype(DWriteCreateFactory)* DWriteCreateFactoryProc;
+ DWriteCreateFactoryProc dWriteCreateFactoryProc = reinterpret_cast<DWriteCreateFactoryProc>(
+ GetProcAddress(LoadLibraryW(L"dwrite.dll"), "DWriteCreateFactory"));
+
+ if (!dWriteCreateFactoryProc) {
+ HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
+ if (!IS_ERROR(hr)) {
+ hr = ERROR_PROC_NOT_FOUND;
+ }
+ HRVM(hr, "Could not get DWriteCreateFactory proc.");
+ }
+
+ HRVM(dWriteCreateFactoryProc(DWRITE_FACTORY_TYPE_SHARED,
+ __uuidof(IDWriteFactory),
+ reinterpret_cast<IUnknown**>(factory)),
+ "Could not create DirectWrite factory.");
+ atexit(release_dwrite_factory);
+}
+
+
+IDWriteFactory* sk_get_dwrite_factory() {
+ SK_DECLARE_STATIC_ONCE(once);
+ SkOnce(&once, create_dwrite_factory, &gDWriteFactory);
+
+ return gDWriteFactory;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// String conversion
+
+/** Converts a utf8 string to a WCHAR string. */
+HRESULT sk_cstring_to_wchar(const char* skname, SkSMallocWCHAR* name) {
+ int wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, NULL, 0);
+ if (0 == wlen) {
+ HRM(HRESULT_FROM_WIN32(GetLastError()),
+ "Could not get length for wchar to utf-8 conversion.");
+ }
+ name->reset(wlen);
+ wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, name->get(), wlen);
+ if (0 == wlen) {
+ HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert wchar to utf-8.");
+ }
+ return S_OK;
+}
+
+/** Converts a WCHAR string to a utf8 string. */
+HRESULT sk_wchar_to_skstring(WCHAR* name, SkString* skname) {
+ int len = WideCharToMultiByte(CP_UTF8, 0, name, -1, NULL, 0, NULL, NULL);
+ if (0 == len) {
+ HRM(HRESULT_FROM_WIN32(GetLastError()),
+ "Could not get length for utf-8 to wchar conversion.");
+ }
+ skname->resize(len - 1);
+
+ // TODO: remove after https://code.google.com/p/skia/issues/detail?id=1989 is fixed.
+ // If we resize to 0 then the skname points to gEmptyRec (the unique empty SkString::Rec).
+ // gEmptyRec is static const and on Windows this means the value is in a read only page.
+ // Writing to it in the following call to WideCharToMultiByte will cause an access violation.
+ if (1 == len) {
+ return S_OK;
+ }
+
+ len = WideCharToMultiByte(CP_UTF8, 0, name, -1, skname->writable_str(), len, NULL, NULL);
+ if (0 == len) {
+ HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert utf-8 to wchar.");
+ }
+ return S_OK;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// Locale
+
+void sk_get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* preferedLocale,
+ SkString* skname) {
+ UINT32 nameIndex = 0;
+ if (preferedLocale) {
+ // Ignore any errors and continue with index 0 if there is a problem.
+ BOOL nameExists;
+ names->FindLocaleName(preferedLocale, &nameIndex, &nameExists);
+ if (!nameExists) {
+ nameIndex = 0;
+ }
+ }
+
+ UINT32 nameLength;
+ HRVM(names->GetStringLength(nameIndex, &nameLength), "Could not get name length.");
+ nameLength += 1;
+
+ SkSMallocWCHAR name(nameLength);
+ HRVM(names->GetString(nameIndex, name.get(), nameLength), "Could not get string.");
+
+ HRV(sk_wchar_to_skstring(name.get(), skname));
+}
+
+HRESULT SkGetGetUserDefaultLocaleNameProc(SkGetUserDefaultLocaleNameProc* proc) {
+ *proc = reinterpret_cast<SkGetUserDefaultLocaleNameProc>(
+ GetProcAddress(LoadLibraryW(L"Kernel32.dll"), "GetUserDefaultLocaleName")
+ );
+ if (!*proc) {
+ HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
+ if (!IS_ERROR(hr)) {
+ hr = ERROR_PROC_NOT_FOUND;
+ }
+ return hr;
+ }
+ return S_OK;
+}
diff --git a/chromium/third_party/skia/src/utils/win/SkDWrite.h b/chromium/third_party/skia/src/utils/win/SkDWrite.h
new file mode 100644
index 00000000000..679447dfc61
--- /dev/null
+++ b/chromium/third_party/skia/src/utils/win/SkDWrite.h
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkDWrite_DEFINED
+#define SkDWrite_DEFINED
+
+#include "SkTemplates.h"
+
+#include <dwrite.h>
+
+class SkString;
+
+////////////////////////////////////////////////////////////////////////////////
+// Factory
+
+IDWriteFactory* sk_get_dwrite_factory();
+
+////////////////////////////////////////////////////////////////////////////////
+// String conversion
+
+/** Prefer to use this type to prevent template proliferation. */
+typedef SkAutoSTMalloc<16, WCHAR> SkSMallocWCHAR;
+
+/** Converts a utf8 string to a WCHAR string. */
+HRESULT sk_cstring_to_wchar(const char* skname, SkSMallocWCHAR* name);
+
+/** Converts a WCHAR string to a utf8 string. */
+HRESULT sk_wchar_to_skstring(WCHAR* name, SkString* skname);
+
+////////////////////////////////////////////////////////////////////////////////
+// Locale
+
+void sk_get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* preferedLocale,
+ SkString* skname);
+
+typedef decltype(GetUserDefaultLocaleName)* SkGetUserDefaultLocaleNameProc;
+HRESULT SkGetGetUserDefaultLocaleNameProc(SkGetUserDefaultLocaleNameProc* proc);
+
+////////////////////////////////////////////////////////////////////////////////
+// Table handling
+
+class AutoDWriteTable {
+public:
+ AutoDWriteTable(IDWriteFontFace* fontFace, UINT32 beTag) : fFontFace(fontFace), fExists(FALSE) {
+ // Any errors are ignored, user must check fExists anyway.
+ fontFace->TryGetFontTable(beTag,
+ reinterpret_cast<const void **>(&fData), &fSize, &fLock, &fExists);
+ }
+ ~AutoDWriteTable() {
+ if (fExists) {
+ fFontFace->ReleaseFontTable(fLock);
+ }
+ }
+
+ const uint8_t* fData;
+ UINT32 fSize;
+ BOOL fExists;
+private:
+ // Borrowed reference, the user must ensure the fontFace stays alive.
+ IDWriteFontFace* fFontFace;
+ void* fLock;
+};
+template<typename T> class AutoTDWriteTable : public AutoDWriteTable {
+public:
+ static const UINT32 tag = DWRITE_MAKE_OPENTYPE_TAG(T::TAG0, T::TAG1, T::TAG2, T::TAG3);
+ AutoTDWriteTable(IDWriteFontFace* fontFace) : AutoDWriteTable(fontFace, tag) { }
+
+ const T* get() const { return reinterpret_cast<const T*>(fData); }
+ const T* operator->() const { return reinterpret_cast<const T*>(fData); }
+};
+
+#endif
diff --git a/chromium/third_party/skia/src/utils/win/SkDWriteFontFileStream.h b/chromium/third_party/skia/src/utils/win/SkDWriteFontFileStream.h
index 5a56290c14c..ac98be6142a 100644
--- a/chromium/third_party/skia/src/utils/win/SkDWriteFontFileStream.h
+++ b/chromium/third_party/skia/src/utils/win/SkDWriteFontFileStream.h
@@ -68,6 +68,7 @@ public:
private:
explicit SkDWriteFontFileStreamWrapper(SkStream* stream);
+ virtual ~SkDWriteFontFileStreamWrapper() { }
ULONG fRefCount;
SkAutoTUnref<SkStream> fStream;
diff --git a/chromium/third_party/skia/src/utils/win/SkHRESULT.cpp b/chromium/third_party/skia/src/utils/win/SkHRESULT.cpp
index 32d9d4c35c3..495f074eb25 100644
--- a/chromium/third_party/skia/src/utils/win/SkHRESULT.cpp
+++ b/chromium/third_party/skia/src/utils/win/SkHRESULT.cpp
@@ -7,12 +7,13 @@
#include "SkTypes.h"
-#include "SKHRESULT.h"
+#include "SkHRESULT.h"
-void SkTraceHR(const char* file, unsigned long line,
- HRESULT hr, const char* msg) {
- SkDEBUGCODE(if (NULL != msg) SkDEBUGF(("%s\n", msg)));
- SkDEBUGF(("%s(%lu) : error 0x%x: ", file, line, hr));
+void SkTraceHR(const char* file, unsigned long line, HRESULT hr, const char* msg) {
+ if (NULL != msg) {
+ SkDebugf("%s\n", msg);
+ }
+ SkDebugf("%s(%lu) : error 0x%x: ", file, line, hr);
LPSTR errorText = NULL;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
@@ -27,9 +28,9 @@ void SkTraceHR(const char* file, unsigned long line,
);
if (NULL == errorText) {
- SkDEBUGF(("<unknown>\n"));
+ SkDebugf("<unknown>\n");
} else {
- SkDEBUGF(("%s", errorText));
+ SkDebugf("%s", errorText);
LocalFree(errorText);
errorText = NULL;
}