-
Notifications
You must be signed in to change notification settings - Fork 35
/
font.h
157 lines (133 loc) · 4.2 KB
/
font.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Part of WinLamb - Win32 API Lambda Library
* https://github.com/rodrigocfd/winlamb
* Copyright 2017-present Rodrigo Cesar de Freitas Dias
* This library is released under the MIT License
*/
#pragma once
#include <system_error>
#include <VersionHelpers.h>
#include "wnd.h"
#include "internals/enable_bitmask_operators.h"
namespace wl {
// Wrapper to HFONT handle.
class font final {
private:
HFONT _hFont = nullptr;
public:
// Can be combined with bitmask operators.
enum class deco : BYTE {
NONE = 0b00000000,
BOLD = 0b00000001,
ITALIC = 0b00000010,
STRIKEOUT = 0b00000100,
UNDERLINE = 0b00001000
};
~font() {
this->destroy();
}
font() = default;
font(font&& other) noexcept : _hFont{other._hFont} {
other._hFont = nullptr;
}
HFONT hfont() const noexcept {
return this->_hFont;
}
font& operator=(font&& other) noexcept {
this->destroy();
std::swap(this->_hFont, other._hFont);
return *this;
}
font& destroy() noexcept {
if (this->_hFont) {
DeleteObject(this->_hFont);
this->_hFont = nullptr;
}
return *this;
}
font& create(const LOGFONT& lf) {
this->destroy();
this->_hFont = CreateFontIndirectW(&lf);
if (!this->_hFont) {
throw std::system_error(GetLastError(), std::system_category(),
"CreateFontIndirect failed");
}
return *this;
}
font& create(const wchar_t* fontName, BYTE size, deco style = deco::NONE) {
this->destroy();
LOGFONT lf{};
lstrcpyW(lf.lfFaceName, fontName);
lf.lfHeight = -(size + 3);
auto hasDeco = [=](deco yourDeco) noexcept -> BOOL {
return (static_cast<BYTE>(style) &
static_cast<BYTE>(yourDeco)) != 0 ? TRUE : FALSE;
};
lf.lfWeight = hasDeco(deco::BOLD) ? FW_BOLD : FW_DONTCARE;
lf.lfItalic = hasDeco(deco::ITALIC);
lf.lfUnderline = hasDeco(deco::UNDERLINE);
lf.lfStrikeOut = hasDeco(deco::STRIKEOUT);
return this->create(lf);
}
// Sets the font on the given control.
void set_on(wnd& control) const noexcept {
SendMessageW(control.hwnd(), WM_SETFONT,
reinterpret_cast<WPARAM>(_hFont), TRUE);
}
// Sets the font on the given control.
void set_on(std::initializer_list<std::reference_wrapper<wnd>> ctrls) const noexcept {
for (wnd& ctrl : ctrls) {
this->set_on(ctrl);
}
}
// Create the same exact font used by UI, like Tahoma or Segoe UI.
font& create_ui() {
NONCLIENTMETRICS ncm{};
ncm.cbSize = sizeof(ncm);
if (!IsWindowsVistaOrGreater()) {
ncm.cbSize -= sizeof(ncm.iBorderWidth);
}
SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0);
return this->create(ncm.lfMenuFont); // Tahoma/Segoe
}
public:
class util final {
private:
util() = delete;
public:
// Applies default UI font on all children of the window.
static void set_ui_on_children(HWND hParent) {
static font oneFont; // keep one single font instance for all windows
if (!oneFont._hFont) oneFont.create_ui();
SendMessageW(hParent, WM_SETFONT,
reinterpret_cast<WPARAM>(oneFont._hFont),
MAKELPARAM(FALSE, 0));
EnumChildWindows(hParent, [](HWND hWnd, LPARAM lp) noexcept -> BOOL {
SendMessageW(hWnd, WM_SETFONT,
reinterpret_cast<WPARAM>(reinterpret_cast<HFONT>(lp)),
MAKELPARAM(FALSE, 0)); // will run on each child
return TRUE;
}, reinterpret_cast<LPARAM>(oneFont._hFont));
}
// Checks if the font is currently installed.
static bool exists(const wchar_t* fontName) {
// http://cboard.cprogramming.com/windows-programming/90066-how-determine-if-font-support-unicode.html
bool isInstalled = false;
HDC hdc = GetDC(nullptr);
if (!hdc) {
throw std::system_error(GetLastError(), std::system_category(),
"GetDC failed when checking if font exists");
}
EnumFontFamiliesW(hdc, fontName,
[](const LOGFONT* lpelf, const TEXTMETRIC* lpntm, DWORD fontType, LPARAM lp) noexcept -> int {
bool* pIsInstalled = reinterpret_cast<bool*>(lp);
*pIsInstalled = true; // if we're here, font does exist
return 0;
}, reinterpret_cast<LPARAM>(&isInstalled));
ReleaseDC(nullptr, hdc);
return isInstalled;
}
};
};
ENABLE_BITMASK_OPERATORS(font::deco);
}//namespace wl