blob: 1cdb1065670d65b0d34c04950862d75cc21878cd [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 */
7
bungeman51daa252014-06-05 13:38:45 -07008#ifndef SkDWrite_DEFINED
9#define SkDWrite_DEFINED
10
bungemanb2bed002015-02-20 12:45:44 -080011#include "SkFontStyle.h"
bungemanf3c15b72015-08-19 11:56:48 -070012#include "SkTemplates.h"
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000013
14#include <dwrite.h>
bungemanf5484442014-09-10 07:49:05 -070015#include <winsdkver.h>
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000016
17class SkString;
18
19////////////////////////////////////////////////////////////////////////////////
20// Factory
21
22IDWriteFactory* sk_get_dwrite_factory();
23
24////////////////////////////////////////////////////////////////////////////////
25// String conversion
26
27/** Prefer to use this type to prevent template proliferation. */
28typedef SkAutoSTMalloc<16, WCHAR> SkSMallocWCHAR;
29
30/** Converts a utf8 string to a WCHAR string. */
31HRESULT sk_cstring_to_wchar(const char* skname, SkSMallocWCHAR* name);
32
bungeman5e7b4f92014-08-25 10:16:01 -070033/** Converts a WCHAR string to a utf8 string.
34 * @param nameLen the number of WCHARs in the name.
35 */
36HRESULT sk_wchar_to_skstring(WCHAR* name, int nameLen, SkString* skname);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000037
38////////////////////////////////////////////////////////////////////////////////
39// Locale
40
Hal Canary8031b322018-03-29 13:20:30 -070041HRESULT sk_get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* preferedLocale,
42 SkString* skname);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000043
george8124bf02014-07-30 09:58:17 -070044typedef int (WINAPI *SkGetUserDefaultLocaleNameProc)(LPWSTR, int);
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000045HRESULT SkGetGetUserDefaultLocaleNameProc(SkGetUserDefaultLocaleNameProc* proc);
bungeman51daa252014-06-05 13:38:45 -070046
47////////////////////////////////////////////////////////////////////////////////
48// Table handling
49
50class AutoDWriteTable {
51public:
sammc98fc73c2015-06-30 07:09:49 -070052 AutoDWriteTable(IDWriteFontFace* fontFace, UINT32 beTag) : fExists(FALSE), fFontFace(fontFace) {
bungeman51daa252014-06-05 13:38:45 -070053 // Any errors are ignored, user must check fExists anyway.
54 fontFace->TryGetFontTable(beTag,
55 reinterpret_cast<const void **>(&fData), &fSize, &fLock, &fExists);
56 }
57 ~AutoDWriteTable() {
58 if (fExists) {
59 fFontFace->ReleaseFontTable(fLock);
60 }
61 }
62
63 const uint8_t* fData;
64 UINT32 fSize;
65 BOOL fExists;
66private:
67 // Borrowed reference, the user must ensure the fontFace stays alive.
68 IDWriteFontFace* fFontFace;
69 void* fLock;
70};
71template<typename T> class AutoTDWriteTable : public AutoDWriteTable {
72public:
73 static const UINT32 tag = DWRITE_MAKE_OPENTYPE_TAG(T::TAG0, T::TAG1, T::TAG2, T::TAG3);
74 AutoTDWriteTable(IDWriteFontFace* fontFace) : AutoDWriteTable(fontFace, tag) { }
75
76 const T* get() const { return reinterpret_cast<const T*>(fData); }
77 const T* operator->() const { return reinterpret_cast<const T*>(fData); }
78};
79
bungemanb2bed002015-02-20 12:45:44 -080080////////////////////////////////////////////////////////////////////////////////
81// Style conversion
82
83struct DWriteStyle {
84 explicit DWriteStyle(const SkFontStyle& pattern) {
bungemanb2bed002015-02-20 12:45:44 -080085 fWeight = (DWRITE_FONT_WEIGHT)pattern.weight();
86 fWidth = (DWRITE_FONT_STRETCH)pattern.width();
bungemanb4bb7d82016-04-27 10:21:04 -070087 switch (pattern.slant()) {
88 case SkFontStyle::kUpright_Slant: fSlant = DWRITE_FONT_STYLE_NORMAL ; break;
89 case SkFontStyle::kItalic_Slant: fSlant = DWRITE_FONT_STYLE_ITALIC ; break;
90 case SkFontStyle::kOblique_Slant: fSlant = DWRITE_FONT_STYLE_OBLIQUE; break;
91 default: SkASSERT(false); break;
92 }
bungemanb2bed002015-02-20 12:45:44 -080093 }
bungemanb2bed002015-02-20 12:45:44 -080094 DWRITE_FONT_WEIGHT fWeight;
95 DWRITE_FONT_STRETCH fWidth;
bungemanb4bb7d82016-04-27 10:21:04 -070096 DWRITE_FONT_STYLE fSlant;
bungemanb2bed002015-02-20 12:45:44 -080097};
98
bungeman51daa252014-06-05 13:38:45 -070099#endif