/
stdafx.cpp
500 lines (464 loc) · 15.2 KB
/
stdafx.cpp
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// This is the plex precompiled header cc.
// Do not edit this file by hand.
#include "stdafx.h"
#pragma comment(lib, "dwrite.lib")
#pragma comment(lib, "shcore.lib")
#pragma comment(lib, "mf.lib")
#pragma comment(lib, "mfplat.lib")
#pragma comment(lib, "strmiids.lib")
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dcomp.lib")
#pragma comment(lib, "mfuuid.lib")
namespace plx {
plx::ComPtr<ID2D1Geometry> CreateD2D1Geometry(
plx::ComPtr<ID2D1Factory2> d2d1_factory,
const D2D1_ELLIPSE& ellipse) {
plx::ComPtr<ID2D1EllipseGeometry> geometry;
auto hr = d2d1_factory->CreateEllipseGeometry(ellipse, geometry.GetAddressOf());
if (hr != S_OK)
throw plx::ComException(__LINE__, hr);
return geometry;
}
plx::ComPtr<ID2D1Geometry> CreateD2D1Geometry(
plx::ComPtr<ID2D1Factory2> d2d1_factory,
const D2D1_ROUNDED_RECT& rect) {
plx::ComPtr<ID2D1RoundedRectangleGeometry> geometry;
auto hr = d2d1_factory->CreateRoundedRectangleGeometry(rect, geometry.GetAddressOf());
if (hr != S_OK)
throw plx::ComException(__LINE__, hr);
return geometry;
}
plx::ComPtr<IDCompositionDesktopDevice> CreateDCoDevice2(
plx::ComPtr<ID2D1Device> device2D) {
plx::ComPtr<IDCompositionDesktopDevice> device;
auto hr = DCompositionCreateDevice2(device2D.Get(),
__uuidof(device),
reinterpret_cast<void **>(device.GetAddressOf()));
if (hr != S_OK)
throw plx::ComException(__LINE__, hr);
return device;
}
plx::ComPtr<IDCompositionSurface> CreateDCoSurface(
plx::ComPtr<IDCompositionDesktopDevice> device, unsigned int w, unsigned int h) {
plx::ComPtr<IDCompositionSurface> surface;
auto hr = device->CreateSurface(w, h,
DXGI_FORMAT_B8G8R8A8_UNORM,
DXGI_ALPHA_MODE_PREMULTIPLIED,
surface.GetAddressOf());
if (hr != S_OK)
throw plx::ComException(__LINE__, hr);
return surface;
}
plx::ComPtr<IDCompositionVisual2> CreateDCoVisual(plx::ComPtr<IDCompositionDesktopDevice> device) {
plx::ComPtr<IDCompositionVisual2> visual;
auto hr = device->CreateVisual(visual.GetAddressOf());
if (hr != S_OK)
throw plx::ComException(__LINE__, hr);
return visual;
}
plx::ComPtr<IDCompositionTarget> CreateDCoWindowTarget(
plx::ComPtr<IDCompositionDesktopDevice> device, HWND window) {
plx::ComPtr<IDCompositionTarget> target;
auto hr = device->CreateTargetForHwnd(window, TRUE, target.GetAddressOf());
if (hr != S_OK)
throw plx::ComException(__LINE__, hr);
return target;
}
plx::ComPtr<IDWriteFactory> CreateDWriteFactory() {
plx::ComPtr<IDWriteFactory> factory;
auto hr = ::DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(factory.GetAddressOf()));
if (hr != S_OK)
throw plx::ComException(__LINE__, hr);
return factory;
}
plx::ComPtr<ID2D1Factory2> CreateD2D1FactoryST(D2D1_DEBUG_LEVEL debug_level) {
D2D1_FACTORY_OPTIONS options = {};
options.debugLevel = debug_level;
plx::ComPtr<ID2D1Factory2> factory;
auto hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
options,
factory.GetAddressOf());
if (hr != S_OK)
throw plx::ComException(__LINE__, hr);
return factory;
}
plx::ComPtr<ID2D1Device> CreateDeviceD2D1(plx::ComPtr<ID3D11Device> device3D,
plx::ComPtr<ID2D1Factory2> factoryD2D1) {
plx::ComPtr<IDXGIDevice3> dxgi_dev;
device3D.As(&dxgi_dev);
plx::ComPtr<ID2D1Device> device2D;
auto hr = factoryD2D1->CreateDevice(dxgi_dev.Get(),
device2D.GetAddressOf());
if (hr != S_OK)
throw plx::ComException(__LINE__, hr);
return device2D;
}
plx::ComPtr<ID3D11Device> CreateDeviceD3D11(int extra_flags) {
auto flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT |
D3D11_CREATE_DEVICE_SINGLETHREADED;
flags |= extra_flags;
plx::ComPtr<ID3D11Device> device;
auto hr = D3D11CreateDevice(nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
flags,
nullptr, 0,
D3D11_SDK_VERSION,
device.GetAddressOf(),
nullptr,
nullptr);
if (hr != S_OK)
throw plx::ComException(__LINE__, hr);
return device;
}
plx::ComPtr<ID2D1DeviceContext> CreateDCoDeviceCtx(
plx::ComPtr<IDCompositionSurface> surface,
const plx::DPI& dpi, const D2D1_SIZE_F& extra_offset) {
plx::ComPtr<ID2D1DeviceContext> dc;
POINT offset;
auto hr = surface->BeginDraw(nullptr,
__uuidof(dc),
reinterpret_cast<void **>(dc.GetAddressOf()),
&offset);
if (hr != S_OK)
throw plx::ComException(__LINE__, hr);
dc->SetDpi(float(dpi.get_dpi_x()), float(dpi.get_dpi_y()));
auto matrix = D2D1::Matrix3x2F::Translation(
dpi.to_logical_x(offset.x) + extra_offset.width,
dpi.to_logical_y(offset.y) + extra_offset.height);
dc->SetTransform(matrix);
return dc;
}
ItRange<uint8_t*> RangeFromBytes(void* start, size_t count) {
auto s = reinterpret_cast<uint8_t*>(start);
return ItRange<uint8_t*>(s, s + count);
}
ItRange<const uint8_t*> RangeFromBytes(const void* start, size_t count) {
auto s = reinterpret_cast<const uint8_t*>(start);
return ItRange<const uint8_t*>(s, s + count);
}
ItRange<const uint8_t*> RangeFromString(const std::string& str) {
auto s = reinterpret_cast<const uint8_t*>(&str.front());
return ItRange<const uint8_t*>(s, s + str.size());
}
ItRange<uint8_t*> RangeFromString(std::string& str) {
auto s = reinterpret_cast<uint8_t*>(&str.front());
return ItRange<uint8_t*>(s, s + str.size());
}
ItRange<const uint16_t*> RangeFromString(const std::wstring& str) {
auto s = reinterpret_cast<const uint16_t*>(&str.front());
return ItRange<const uint16_t*>(s, s + str.size());
}
ItRange<uint16_t*> RangeFromString(std::wstring& str) {
auto s = reinterpret_cast<uint16_t*>(&str.front());
return ItRange<uint16_t*>(s, s + str.size());
}
plx::ComPtr<IDWriteTextFormat> CreateDWriteSystemTextFormat(
plx::ComPtr<IDWriteFactory> dw_factory,
const wchar_t* font_family, float size,
const plx::FontWSSParams& params) {
plx::ComPtr<IDWriteTextFormat> format;
auto hr = dw_factory->CreateTextFormat(font_family, nullptr,
params.weight, params.style, params.stretch,
size, L"",
format.GetAddressOf());
if (hr != S_OK)
throw plx::ComException(__LINE__, hr);
return format;
}
plx::FilePath GetAppDataPath(bool roaming) {
auto folder = roaming? FOLDERID_RoamingAppData : FOLDERID_LocalAppData;
wchar_t* path = nullptr;
auto hr = ::SHGetKnownFolderPath(folder, 0, nullptr, &path);
if (hr != S_OK)
throw plx::IOException(__LINE__, L"<appdata folder>");
auto fp = FilePath(path);
::CoTaskMemFree(path);
return fp;
}
char* HexASCII(uint8_t byte, char* out) {
*out++ = HexASCIITable[(byte >> 4) & 0x0F];
*out++ = HexASCIITable[byte & 0x0F];
return out;
}
std::string HexASCIIStr(const plx::Range<const uint8_t>& r, char separator) {
if (r.empty())
return std::string();
std::string str((r.size() * 3) - 1, separator);
char* data = &str[0];
for (size_t ix = 0; ix != r.size(); ++ix) {
data = plx::HexASCII(r[ix], data);
++data;
}
return str;
}
short NextInt(char value) {
return short(value);
}
int NextInt(short value) {
return int(value);
}
long long NextInt(int value) {
return long long(value);
}
long long NextInt(long value) {
return long long(value);
}
long long NextInt(long long value) {
return value;
}
short NextInt(unsigned char value) {
return short(value);
}
int NextInt(unsigned short value) {
return int(value);
}
long long NextInt(unsigned int value) {
return long long(value);
}
long long NextInt(unsigned long value) {
return long long(value);
}
long long NextInt(unsigned long long value) {
if (static_cast<long long>(value) < 0LL)
throw plx::OverflowException(__LINE__, plx::OverflowKind::Positive);
return long long(value);
}
plx::ComPtr<IDWriteTextLayout> CreateDWTextLayout(
plx::ComPtr<IDWriteFactory> dw_factory, plx::ComPtr<IDWriteTextFormat> format,
const plx::Range<const wchar_t>& text, const D2D1_SIZE_F& size) {
plx::ComPtr<IDWriteTextLayout> layout;
auto hr = dw_factory->CreateTextLayout(
text.start(), plx::To<UINT32>(text.size()),
format.Get(),
size.width, size.height,
layout.GetAddressOf());
if (hr != S_OK)
throw plx::ComException(__LINE__, hr);
return layout;
}
std::string DecodeString(plx::Range<const char>& range) {
if (range.empty())
return std::string();
if (range[0] != '\"') {
auto r = plx::RangeFromBytes(range.start(), 1);
throw plx::CodecException(__LINE__, &r);
}
std::string s;
for (;;) {
auto text_start = range.start();
while (range.advance(1) > 0) {
auto c = range.front();
if (c < 32) {
throw plx::CodecException(__LINE__, nullptr);
} else {
switch (c) {
case '\"' : break;
case '\\' : goto escape;
default: continue;
}
}
s.append(++text_start, range.start());
range.advance(1);
return s;
}
// Reached the end of range before a (").
throw plx::CodecException(__LINE__, nullptr);
escape:
s.append(++text_start, range.start());
if (range.advance(1) <= 0)
throw plx::CodecException(__LINE__, nullptr);
switch (range.front()) {
case '\"': s.push_back('\"'); break;
case '\\': s.push_back('\\'); break;
case '/': s.push_back('/'); break;
case 'b': s.push_back('\b'); break;
case 'f': s.push_back('\f'); break;
case 'n': s.push_back('\n'); break;
case 'r': s.push_back('\r'); break;
case 't': s.push_back('\t'); break; //$$ missing \u (unicode).
default: {
auto r = plx::RangeFromBytes(range.start() - 1, 2);
throw plx::CodecException(__LINE__, &r);
}
}
}
}
namespace JsonImp {
template <typename StrT>
bool Consume(plx::Range<const char>& r, StrT&& str) {
auto c = r.starts_with(plx::RangeFromLitStr(str));
if (c) {
r.advance(c);
return true;
}
else {
return (c != 0);
}
}
bool IsNumber(plx::Range<const char>&r) {
if ((r.front() >= '0') && (r.front() <= '9'))
return true;
if ((r.front() == '-') || (r.front() == '+'))
return true;
if (r.front() == '.')
return true;
return false;
}
plx::JsonValue ParseArray(plx::Range<const char>& range) {
if (range.empty())
throw plx::CodecException(__LINE__, NULL);
if (range.front() != '[')
throw plx::CodecException(__LINE__, NULL);
JsonValue value(plx::JsonType::ARRAY);
range.advance(1);
for (;!range.empty();) {
range = plx::SkipWhitespace(range);
if (range.front() == ',') {
if (range.advance(1) <= 0)
break;
range = plx::SkipWhitespace(range);
}
if (range.front() == ']') {
range.advance(1);
return value;
}
value.push_back(ParseJsonValue(range));
}
auto r = plx::RangeFromBytes(range.start(), range.size());
throw plx::CodecException(__LINE__, &r);
}
plx::JsonValue ParseNumber(plx::Range<const char>& range) {
size_t pos = 0;
auto num = plx::StringFromRange(range);
auto iv = std::stoll(num, &pos);
if ((range[pos] != 'e') && (range[pos] != 'E') && (range[pos] != '.')) {
range.advance(pos);
return iv;
}
auto dv = std::stod(num, &pos);
range.advance(pos);
return dv;
}
plx::JsonValue ParseObject(plx::Range<const char>& range) {
if (range.empty())
throw plx::CodecException(__LINE__, NULL);
if (range.front() != '{')
throw plx::CodecException(__LINE__, NULL);
JsonValue obj(plx::JsonType::OBJECT);
range.advance(1);
for (;!range.empty();) {
if (range.front() == '}') {
range.advance(1);
return obj;
}
range = plx::SkipWhitespace(range);
auto key = plx::DecodeString(range);
range = plx::SkipWhitespace(range);
if (range.front() != ':')
throw plx::CodecException(__LINE__, nullptr);
if (range.advance(1) <= 0)
throw plx::CodecException(__LINE__, nullptr);
range = plx::SkipWhitespace(range);
obj[key] = ParseJsonValue(range);
range = plx::SkipWhitespace(range);
if (range.front() == ',') {
if (range.advance(1) <= 0)
break;
range = plx::SkipWhitespace(range);
}
}
throw plx::CodecException(__LINE__, nullptr);
}
}
plx::JsonValue ParseJsonValue(plx::Range<const char>& range) {
range = plx::SkipWhitespace(range);
if (range.empty())
throw plx::CodecException(__LINE__, NULL);
if (range.front() == '{')
return JsonImp::ParseObject(range);
if (range.front() == '\"')
return plx::DecodeString(range);
if (range.front() == '[')
return JsonImp::ParseArray(range);
if (JsonImp::Consume(range, "true"))
return true;
if (JsonImp::Consume(range, "false"))
return false;
if (JsonImp::Consume(range, "null"))
return nullptr;
if (JsonImp::IsNumber(range))
return JsonImp::ParseNumber(range);
auto r = plx::RangeFromBytes(range.start(), range.size());
throw plx::CodecException(__LINE__, &r);
}
namespace impl_v {
int vsnprintf(char* buffer, size_t size,
const char* format, va_list arguments) {
int length = _vsprintf_p(buffer, size, format, arguments);
if (length < 0) {
if (size > 0)
buffer[0] = 0;
return _vscprintf_p(format, arguments);
}
return length;
}
}
std::string StringPrintf(const char* fmt, ...) {
int fmt_size = 128;
std::unique_ptr<char> mem;
va_list args;
va_start(args, fmt);
while (true) {
mem.reset(new char[fmt_size]);
int sz = impl_v::vsnprintf(mem.get(), fmt_size, fmt, args);
if (sz < fmt_size)
break;
fmt_size = sz + 1;
}
va_end(args);
return std::string(mem.get());
}
plx::JsonValue JsonFromFile(plx::File& cfile) {
if (!cfile.is_valid())
throw plx::IOException(__LINE__, L"<json file>");
auto size = cfile.size_in_bytes();
plx::Range<uint8_t> r(0, plx::To<size_t>(size));
auto mem = plx::HeapRange(r);
if (cfile.read(r, 0) != size)
throw plx::IOException(__LINE__, L"<json file>");
plx::Range<const char> json(reinterpret_cast<char*>(r.start()),
reinterpret_cast<char*>(r.end()));
return plx::ParseJsonValue(json);
}
std::wstring UTF16FromUTF8(const plx::Range<const uint8_t>& utf8, bool strict) {
if (utf8.empty())
return std::wstring();
// Get length and validate string.
const int utf16_len = ::MultiByteToWideChar(
CP_UTF8, strict ? MB_ERR_INVALID_CHARS : 0,
reinterpret_cast<const char*>(utf8.start()),
plx::To<int>(utf8.size()),
NULL,
0);
if (utf16_len == 0) {
throw plx::CodecException(__LINE__, nullptr);
}
std::wstring utf16;
utf16.resize(utf16_len);
// Now do the conversion without validation.
if (!::MultiByteToWideChar(
CP_UTF8, 0,
reinterpret_cast<const char*>(utf8.start()),
plx::To<int>(utf8.size()),
&utf16[0],
utf16_len)) {
throw plx::CodecException(__LINE__, nullptr);
}
return utf16;
}
}