-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathDXTextPainter.cpp
556 lines (468 loc) · 13.6 KB
/
DXTextPainter.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "DXTextPainter.h"
#include "FontLoader.h"
#include "CCCommon.h"
using namespace Microsoft::WRL;
using namespace Windows::UI::Core;
using namespace Windows::Foundation;
using namespace D2D1;
using namespace Platform;
using namespace Windows::Storage;
using namespace Windows::System;
using namespace Windows::Storage::Streams;
// helper functions to get the proper resolution scale
// (even if invalid value is received)
static int GetResolutionScaleInt()
{
int resolutionScale = (int)Windows::Graphics::Display::DisplayProperties::ResolutionScale;
// if <= - consider as 100%
if (resolutionScale <= 0)
resolutionScale = 100;
return resolutionScale;
}
static float GetResolutionScale()
{
return (float)GetResolutionScaleInt() / 100.0;
}
DXTextPainter::DXTextPainter()
: m_fontLoader()
, m_dwriteFactory()
, m_fontCollection()
, m_bUseCustomFont(false)
, m_bIsFontChanged(false)
{
}
void DXTextPainter:: Initialize(
_In_ ID2D1DeviceContext* d2dContext,
_In_ IWICImagingFactory2* wicFactory,
_In_ IDWriteFactory1* dwriteFactory
)
{
m_d2dContext = d2dContext;
m_wicFactory = wicFactory;
m_dwriteFactory = dwriteFactory;
m_fontName = L"Arial";
m_fontSize = 24;
m_bUseCustomFont = false;
m_bIsFontChanged = false;
wchar_t localeName[LOCALE_NAME_MAX_LENGTH] = {0};
GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH);
m_locale = ref new Platform::String(localeName);
}
bool DXTextPainter::SetFont(Platform::String^ fontName , UINT nSize)
{
bool bReCreate = false;
std::wstring wstrFontName(fontName->Data());
std::wstring suffix = wstrFontName.substr(wstrFontName.rfind(L'.') == std::wstring::npos ? wstrFontName.length() : wstrFontName.rfind(L'.') + 1);
if(nSize!=0 && nSize != m_fontSize)
{
m_fontSize = nSize;
bReCreate = true;
}
FLOAT fntSize = (FLOAT)m_fontSize;
fntSize /= GetResolutionScale();
wchar_t localeName[LOCALE_NAME_MAX_LENGTH] = {0};
GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH);
Platform::String^ refLocalName = ref new String(localeName);
if(m_locale != refLocalName)
{
m_locale = ref new Platform::String(localeName);
bReCreate = true;
}
if(suffix == L"" && (suffix.length() == 0))
{
//use sysstem font
m_bUseCustomFont = false;
m_fontName = ref new Platform::String(wstrFontName.c_str());
}else{
// use custom font
m_bUseCustomFont = true;
int pos = wstrFontName.find_last_of(L'/')+1;
std::wstring wfontName(wstrFontName.substr(pos,wstrFontName.length()-pos-4));
std::wstring oldFontName(m_fontName->Data());
if( oldFontName != wstrFontName )
{
m_bIsFontChanged = true;
}
m_fontName = ref new String(wfontName.c_str());
//create FontLoader if not exsit
if(nullptr == m_fontLoader){
m_fontLoader = new FontLoader(m_dwriteFactory.Get());
m_fontLoader->LoadFont(wstrFontName);
DX::ThrowIfFailed(
m_dwriteFactory->RegisterFontFileLoader(m_fontLoader.Get())
);
DX::ThrowIfFailed(
m_dwriteFactory->RegisterFontCollectionLoader(m_fontLoader.Get())
);
}else{
if(m_bIsFontChanged)
{
// Unregister the font loader from DirectWrite factory
DX::ThrowIfFailed(
m_dwriteFactory->UnregisterFontCollectionLoader(m_fontLoader.Get())
);
DX::ThrowIfFailed(
m_dwriteFactory->UnregisterFontFileLoader(m_fontLoader.Get())
);
m_fontLoader = nullptr;
m_fontCollection = nullptr;
m_fontLoader = new FontLoader(m_dwriteFactory.Get());
m_fontLoader->LoadFont(wstrFontName);
DX::ThrowIfFailed(
m_dwriteFactory->RegisterFontFileLoader(m_fontLoader.Get())
);
DX::ThrowIfFailed(
m_dwriteFactory->RegisterFontCollectionLoader(m_fontLoader.Get())
);
}
}
size_t fontCollectionKey = 0;
DX::ThrowIfFailed(
m_dwriteFactory->CreateCustomFontCollection(
m_fontLoader.Get(),
&fontCollectionKey,
sizeof(size_t),
&m_fontCollection
)
);
}
if(bReCreate && m_TextFormat){
//release old one
m_TextFormat = nullptr;
}
if(m_TextFormat == nullptr)
{
DX::ThrowIfFailed(
m_dwriteFactory->CreateTextFormat(
m_fontName->Data(),
nullptr, // Proper font collection will be set upon drawing
DWRITE_FONT_WEIGHT_LIGHT,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
fntSize,
m_locale->Data(),
&m_TextFormat
)
);
}
return true;
}
Platform::Array<byte>^ DXTextPainter::DrawTextToImage(Platform::String^ text, Windows::Foundation::Size* tSize, TextAlignment alignment)
{
if(text->Length() == 0){
return nullptr;
}
//set text alignment and paragraph alignment
switch (alignment)
{
case TextAlignment::TextAlignmentLeft: // left
DX::ThrowIfFailed(
m_TextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING)
);
DX::ThrowIfFailed(
m_TextFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR)
);
break;
case TextAlignment::TextAlignmentRight: // right
DX::ThrowIfFailed(
m_TextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_TRAILING)
);
DX::ThrowIfFailed(
m_TextFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR)
);
break;
case TextAlignment::TextAlignmentCenter: // center
DX::ThrowIfFailed(
m_TextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER)
);
DX::ThrowIfFailed(
m_TextFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER)
);
break;
}
bool isShouldAdjustBounds = false;
if(tSize->Width <= 0)
{
tSize->Width = FLT_MAX;
isShouldAdjustBounds = true;
}
if(m_textLayout)
{
//release old one
m_textLayout = nullptr;
}
DX::ThrowIfFailed(
m_dwriteFactory->CreateTextLayout(
text->Data(),
text->Length(),
m_TextFormat.Get(),
tSize->Width,
FLT_MAX,
&m_textLayout
)
);
//Here invalid!
DWRITE_TEXT_RANGE fullRange = {0, text->Length()};
DX::ThrowIfFailed(
m_textLayout->SetFontFamilyName(m_fontName->Data(),fullRange)
);
if(m_bUseCustomFont){
DX::ThrowIfFailed(
m_textLayout->SetFontCollection(m_fontCollection.Get(),fullRange)
);
}
m_bIsFontChanged = false;
DWRITE_TEXT_METRICS metrics;
m_textLayout->GetMetrics(&metrics);
Windows::Foundation::Size newSize(metrics.width, metrics.height);
// layoutTextSize is used to contain text size without ResolutionScale
// otherwise it is possible that due to floating point accuracy problem
// the provided size to the layout will be less than required
// and the text will be divided in 2 lines instead 1, for example.
Windows::Foundation::Size layoutTextSize(metrics.width, metrics.height);
// allow resolution scale usage
newSize.Width *= GetResolutionScale();
newSize.Height *= GetResolutionScale();
if(isShouldAdjustBounds)
{
tSize->Width = newSize.Width;
tSize->Height = newSize.Height;
}
else
{
// Originally provided tSize (provided with an original resolution)
// will be used - so layoutTextSize should be calculated.
layoutTextSize.Width = tSize->Width / GetResolutionScale();
// adjust height just in case if it is provided
if (tSize->Height > 0)
{
layoutTextSize.Height = tSize->Height / GetResolutionScale();
}
}
if(tSize->Height <=0)
{
tSize->Height = newSize.Height;
}
else
{
layoutTextSize.Height = tSize->Height / GetResolutionScale();
}
m_textLayout->SetMaxWidth(layoutTextSize.Width);
m_textLayout->SetMaxHeight(layoutTextSize.Height);
if(m_whiteBrush == nullptr){
DX::ThrowIfFailed(
m_d2dContext->CreateSolidColorBrush(ColorF(ColorF::White), &m_whiteBrush)
);
}
if(!PrepareBitmap((UINT)ceilf(tSize->Width),(UINT)ceilf(tSize->Height)))
{
return nullptr;
}
//Render
m_d2dContext->SetTarget(m_d2dTargetBitmap.Get());
m_d2dContext->BeginDraw();
m_d2dContext->SetTransform(D2D1::Matrix3x2F::Identity());
m_d2dContext->DrawTextLayout(
Point2F(0, 0),
m_textLayout.Get(),
m_whiteBrush.Get()
);
// We ignore the HRESULT returned as we want to application to handle the
// error when it uses Direct2D next.
m_d2dContext->EndDraw();
if ( m_d2dTargetBitmap == nullptr)
{
return nullptr;
}
ComPtr<IStream> stream;
auto refStream = ref new InMemoryRandomAccessStream();
DX::ThrowIfFailed(
CreateStreamOverRandomAccessStream(reinterpret_cast<IUnknown*>(refStream) ,IID_PPV_ARGS(&stream))
);
GUID wicFormat = GUID_ContainerFormatPng;
SaveBitmapToStream(m_d2dTargetBitmap, m_wicFactory, m_d2dContext, wicFormat, stream.Get());
refStream->Seek(0);
ComPtr<IStream> stream2;
DX::ThrowIfFailed(
CreateStreamOverRandomAccessStream(reinterpret_cast<IUnknown*>(refStream) ,IID_PPV_ARGS(&stream2))
);
ComPtr<IWICBitmapDecoder> wicBitmapDecoder;
DX::ThrowIfFailed(
m_wicFactory->CreateDecoderFromStream(stream2.Get(),nullptr,WICDecodeMetadataCacheOnDemand,&wicBitmapDecoder)
);
ComPtr<IWICBitmapFrameDecode> wicBitmapFrame;
DX::ThrowIfFailed(
wicBitmapDecoder->GetFrame(0, &wicBitmapFrame)
);
ComPtr<IWICFormatConverter> wicFormatConverter;
DX::ThrowIfFailed(
m_wicFactory->CreateFormatConverter(&wicFormatConverter)
);
DX::ThrowIfFailed(
wicFormatConverter->Initialize(
wicBitmapFrame.Get(),
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
nullptr,
0.0,
WICBitmapPaletteTypeCustom // the BGRA format has no palette so this value is ignored
)
);
double dpiX = 96.0f;
double dpiY = 96.0f;
DX::ThrowIfFailed(
wicFormatConverter->GetResolution(&dpiX, &dpiY)
);
UINT uiWidth = (UINT)tSize->Width;
UINT uiHeight = (UINT)tSize->Height;
DX::ThrowIfFailed(
wicFormatConverter->GetSize(&uiWidth, &uiHeight)
);
UINT cbStride = uiWidth * 4;
UINT cbBufferSize = cbStride * uiHeight;
Platform::Array<byte>^ pixelBuffer = ref new Platform::Array<byte>(cbBufferSize);
if (true)
{
WICRect rc;
rc.X = 0;
rc.Y = 0;
rc.Width = uiWidth;
rc.Height = uiHeight;
DX::ThrowIfFailed(
wicFormatConverter->CopyPixels(&rc, cbStride, cbBufferSize,pixelBuffer->Data)
);
// ensure that last bottom/right lines will not be lost - return the same size
// (which is used to draw text)
tSize->Width = (float)rc.Width;
tSize->Height = (float)rc.Height;
}
return pixelBuffer;
}
bool DXTextPainter::PrepareBitmap(UINT nWidth, UINT nHeight)
{
if((nWidth == 0) || (nHeight == 0))
{
return false;
}
ComPtr<IWICBitmapDecoder> wicBitmapDecoder;
double dpiX = 96.0f;
double dpiY = 96.0f;
D2D1_SIZE_U size = D2D1::SizeU(nWidth,nHeight);
BYTE* initData = new BYTE[nWidth * nHeight * 4 ];
ZeroMemory(initData,nWidth * nHeight * 4);
if(m_d2dTargetBitmap)
{
// release old one
m_d2dTargetBitmap = nullptr;
}
DX::ThrowIfFailed(
m_d2dContext->CreateBitmap(size,
initData,
nWidth*4,
BitmapProperties1(D2D1_BITMAP_OPTIONS_TARGET,
PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_PREMULTIPLIED
),
static_cast<float>(dpiX),
static_cast<float>(dpiY),
0
),
&m_d2dTargetBitmap
)
);
//release initData
if(initData){
delete[]initData;
}
initData = 0;
return true;
}
void DXTextPainter::SaveBitmapToStream( _In_ ComPtr<ID2D1Bitmap1> d2dBitmap, _In_ ComPtr<IWICImagingFactory2> wicFactory2, _In_ ComPtr<ID2D1DeviceContext> d2dContext, _In_ REFGUID wicFormat, _In_ IStream* stream )
{
// Create and initialize WIC Bitmap Encoder.
ComPtr<IWICBitmapEncoder> wicBitmapEncoder;
DX::ThrowIfFailed(
wicFactory2->CreateEncoder(
wicFormat,
nullptr, // No preferred codec vendor.
&wicBitmapEncoder
)
);
DX::ThrowIfFailed(
wicBitmapEncoder->Initialize(
stream,
WICBitmapEncoderNoCache
)
);
// Create and initialize WIC Frame Encoder.
ComPtr<IWICBitmapFrameEncode> wicFrameEncode;
DX::ThrowIfFailed(
wicBitmapEncoder->CreateNewFrame(
&wicFrameEncode,
nullptr // No encoder options.
)
);
DX::ThrowIfFailed(
wicFrameEncode->Initialize(nullptr)
);
// Retrieve D2D Device.
ComPtr<ID2D1Device> d2dDevice;
d2dContext->GetDevice(&d2dDevice);
// Create IWICImageEncoder.
ComPtr<IWICImageEncoder> imageEncoder;
DX::ThrowIfFailed(
wicFactory2->CreateImageEncoder(
d2dDevice.Get(),
&imageEncoder
)
);
DX::ThrowIfFailed(
imageEncoder->WriteFrame(
d2dBitmap.Get(),
wicFrameEncode.Get(),
nullptr // Use default WICImageParameter options.
)
);
DX::ThrowIfFailed(
wicFrameEncode->Commit()
);
DX::ThrowIfFailed(
wicBitmapEncoder->Commit()
);
// Flush all memory buffers to the next-level storage object.
DX::ThrowIfFailed(
stream->Commit(STGC_DEFAULT)
);
}
DXTextPainter::~DXTextPainter()
{
if (m_fontLoader != nullptr)
{
// Unregister the font loader from DirectWrite factory
DX::ThrowIfFailed(
m_dwriteFactory->UnregisterFontCollectionLoader(m_fontLoader.Get())
);
DX::ThrowIfFailed(
m_dwriteFactory->UnregisterFontFileLoader(m_fontLoader.Get())
);
}
}