-
Notifications
You must be signed in to change notification settings - Fork 529
Expand file tree
/
Copy pathSpriteFont.cpp
More file actions
732 lines (560 loc) · 23.8 KB
/
SpriteFont.cpp
File metadata and controls
732 lines (560 loc) · 23.8 KB
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//--------------------------------------------------------------------------------------
// File: SpriteFont.cpp
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//
// http://go.microsoft.com/fwlink/?LinkId=248929
//--------------------------------------------------------------------------------------
#include "pch.h"
#include <algorithm>
#include <vector>
#include "SpriteFont.h"
#include "DirectXHelpers.h"
#include "BinaryReader.h"
#include "LoaderHelpers.h"
using namespace DirectX;
using Microsoft::WRL::ComPtr;
// Internal SpriteFont implementation class.
class SpriteFont::Impl
{
public:
Impl(_In_ ID3D11Device* device,
_In_ BinaryReader* reader,
bool forceSRGB) noexcept(false);
Impl(_In_ ID3D11ShaderResourceView* texture,
_In_reads_(glyphCount) Glyph const* glyphs,
size_t glyphCount,
float lineSpacing) noexcept(false);
Impl(const Impl&) = delete;
Impl& operator=(const Impl&) = delete;
Impl(Impl&&) = default;
Impl& operator=(Impl&&) = default;
Glyph const* FindGlyph(wchar_t character) const;
void SetDefaultCharacter(wchar_t character);
template<typename TAction>
void ForEachGlyph(_In_z_ wchar_t const* text, TAction action, bool ignoreWhitespace) const;
const wchar_t* ConvertUTF8(_In_z_ const char *text) noexcept(false);
// Fields.
ComPtr<ID3D11ShaderResourceView> texture;
std::vector<Glyph> glyphs;
std::vector<uint32_t> glyphsIndex;
Glyph const* defaultGlyph;
float lineSpacing;
bool pixelAlignment;
private:
void CreateTextureResource(_In_ ID3D11Device* device,
uint32_t width, uint32_t height,
DXGI_FORMAT format,
uint32_t stride, uint32_t rows,
_In_reads_(stride * rows) const uint8_t* data) noexcept(false);
size_t utfBufferSize;
std::unique_ptr<wchar_t[]> utfBuffer;
};
// Constants.
const XMFLOAT2 SpriteFont::Float2Zero(0, 0);
static const char spriteFontMagic[] = "DXTKfont";
// Comparison operators make our sorted glyph vector work with std::binary_search and lower_bound.
namespace DirectX
{
static inline bool operator< (SpriteFont::Glyph const& left, SpriteFont::Glyph const& right) noexcept
{
return left.Character < right.Character;
}
static inline bool operator< (wchar_t left, SpriteFont::Glyph const& right) noexcept
{
return left < right.Character;
}
static inline bool operator< (SpriteFont::Glyph const& left, wchar_t right) noexcept
{
return left.Character < right;
}
}
// Reads a SpriteFont from the binary format created by the MakeSpriteFont utility.
_Use_decl_annotations_
SpriteFont::Impl::Impl(
ID3D11Device* device,
BinaryReader* reader,
bool forceSRGB) noexcept(false) :
defaultGlyph(nullptr),
lineSpacing(0),
pixelAlignment(false),
utfBufferSize(0)
{
if (!device || !reader)
throw std::invalid_argument("Direct3D device is null");
// Validate the header.
for (char const* magic = spriteFontMagic; *magic; magic++)
{
if (reader->Read<uint8_t>() != *magic)
{
DebugTrace("ERROR: SpriteFont provided with an invalid .spritefont file\n");
throw std::runtime_error("Not a MakeSpriteFont output binary");
}
}
// Read the glyph data.
auto glyphCount = reader->Read<uint32_t>();
auto glyphData = reader->ReadArray<Glyph>(glyphCount);
glyphs.assign(glyphData, glyphData + glyphCount);
glyphsIndex.reserve(glyphs.size());
for (auto& glyph : glyphs)
{
glyphsIndex.emplace_back(glyph.Character);
}
// Read font properties.
lineSpacing = reader->Read<float>();
SetDefaultCharacter(static_cast<wchar_t>(reader->Read<uint32_t>()));
// Read the texture data.
auto textureWidth = reader->Read<uint32_t>();
auto textureHeight = reader->Read<uint32_t>();
auto textureFormat = reader->Read<DXGI_FORMAT>();
auto textureStride = reader->Read<uint32_t>();
auto textureRows = reader->Read<uint32_t>();
const uint64_t dataSize = uint64_t(textureStride) * uint64_t(textureRows);
if (dataSize > UINT32_MAX)
{
DebugTrace("ERROR: SpriteFont provided with an invalid .spritefont file\n");
throw std::overflow_error("Invalid .spritefont file");
}
auto textureData = reader->ReadArray<uint8_t>(static_cast<size_t>(dataSize));
if (forceSRGB)
{
textureFormat = LoaderHelpers::MakeSRGB(textureFormat);
}
// Create the D3D texture.
CreateTextureResource(
device,
textureWidth, textureHeight,
textureFormat,
textureStride, textureRows,
textureData);
}
// Constructs a SpriteFont from arbitrary user specified glyph data.
_Use_decl_annotations_
SpriteFont::Impl::Impl(
ID3D11ShaderResourceView* itexture,
Glyph const* iglyphs,
size_t glyphCount,
float ilineSpacing) noexcept(false) :
texture(itexture),
glyphs(iglyphs, iglyphs + glyphCount),
defaultGlyph(nullptr),
lineSpacing(ilineSpacing),
pixelAlignment(false),
utfBufferSize(0)
{
if (!itexture || !iglyphs)
{
throw std::invalid_argument("Sprite sheet texture required");
}
if (!std::is_sorted(iglyphs, iglyphs + glyphCount))
{
throw std::runtime_error("Glyphs must be in ascending codepoint order");
}
glyphsIndex.reserve(glyphs.size());
for (auto& glyph : glyphs)
{
glyphsIndex.emplace_back(glyph.Character);
}
}
// Looks up the requested glyph, falling back to the default character if it is not in the font.
SpriteFont::Glyph const* SpriteFont::Impl::FindGlyph(wchar_t character) const
{
// Rather than use std::lower_bound (which includes a slow debug path when built for _DEBUG),
// we implement a binary search inline to ensure sufficient Debug build performance to be useful
// for text-heavy applications.
size_t lower = 0;
size_t higher = glyphs.size() - 1;
size_t index = higher / 2;
const size_t size = glyphs.size();
while (index < size)
{
const auto curChar = glyphsIndex[index];
if (curChar == character) { return &glyphs[index]; }
if (curChar < character)
{
lower = index + 1;
}
else
{
higher = index - 1;
}
if (higher < lower) { break; }
else if (higher - lower <= 4)
{
for (index = lower; index <= higher; index++)
{
if (glyphsIndex[index] == character)
{
return &glyphs[index];
}
}
}
index = lower + ((higher - lower) / 2);
}
if (defaultGlyph)
{
return defaultGlyph;
}
DebugTrace("ERROR: SpriteFont encountered a character not in the font (%u, %C), and no default glyph was provided\n", character, character);
throw std::runtime_error("Character not in font");
}
// Sets the missing-character fallback glyph.
void SpriteFont::Impl::SetDefaultCharacter(wchar_t character)
{
defaultGlyph = nullptr;
if (character)
{
defaultGlyph = FindGlyph(character);
}
}
// The core glyph layout algorithm, shared between DrawString and MeasureString.
template<typename TAction>
void SpriteFont::Impl::ForEachGlyph(_In_z_ wchar_t const* text, TAction action, bool ignoreWhitespace) const
{
float x = 0;
float y = 0;
for (; *text; text++)
{
const wchar_t character = *text;
switch (character)
{
case '\r':
// Skip carriage returns.
continue;
case '\n':
// New line.
x = 0;
y += lineSpacing;
break;
default:
// Output this character.
auto glyph = FindGlyph(character);
x += glyph->XOffset;
if (x < 0)
x = 0;
const float advance = float(glyph->Subrect.right) - float(glyph->Subrect.left) + glyph->XAdvance;
if (!ignoreWhitespace
|| !iswspace(character)
|| ((glyph->Subrect.right - glyph->Subrect.left) > 1)
|| ((glyph->Subrect.bottom - glyph->Subrect.top) > 1))
{
action(glyph, x, y, advance);
}
x += advance;
break;
}
}
}
_Use_decl_annotations_
void SpriteFont::Impl::CreateTextureResource(
ID3D11Device* device,
uint32_t width, uint32_t height,
DXGI_FORMAT format,
uint32_t stride, uint32_t rows,
const uint8_t* data) noexcept(false)
{
const uint64_t sliceBytes = uint64_t(stride) * uint64_t(rows);
if (sliceBytes > UINT32_MAX)
{
DebugTrace("ERROR: SpriteFont provided with an invalid .spritefont file\n");
throw std::overflow_error("Invalid .spritefont file");
}
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = width;
desc.Height = height;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = format;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
D3D11_SUBRESOURCE_DATA initData = { data, stride, static_cast<UINT>(sliceBytes) };
ComPtr<ID3D11Texture2D> texture2D;
ThrowIfFailed(
device->CreateTexture2D(&desc, &initData, &texture2D)
);
CD3D11_SHADER_RESOURCE_VIEW_DESC viewDesc(D3D11_SRV_DIMENSION_TEXTURE2D, format);
ThrowIfFailed(
device->CreateShaderResourceView(texture2D.Get(), &viewDesc, texture.ReleaseAndGetAddressOf())
);
SetDebugObjectName(texture.Get(), "DirectXTK:SpriteFont");
SetDebugObjectName(texture2D.Get(), "DirectXTK:SpriteFont");
}
const wchar_t* SpriteFont::Impl::ConvertUTF8(_In_z_ const char *text) noexcept(false)
{
if (!utfBuffer)
{
utfBufferSize = 1024;
utfBuffer.reset(new wchar_t[1024]);
}
int result = MultiByteToWideChar(CP_UTF8, 0, text, -1, utfBuffer.get(), static_cast<int>(utfBufferSize));
if (!result && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
{
// Compute required buffer size
result = MultiByteToWideChar(CP_UTF8, 0, text, -1, nullptr, 0);
utfBufferSize = AlignUp(static_cast<size_t>(result), 1024u);
utfBuffer.reset(new wchar_t[utfBufferSize]);
// Retry conversion
result = MultiByteToWideChar(CP_UTF8, 0, text, -1, utfBuffer.get(), static_cast<int>(utfBufferSize));
}
if (!result)
{
DebugTrace("ERROR: MultiByteToWideChar failed with error %u.\n", GetLastError());
throw std::system_error(std::error_code(static_cast<int>(GetLastError()), std::system_category()), "MultiByteToWideChar");
}
return utfBuffer.get();
}
// Construct from a binary file created by the MakeSpriteFont utility.
_Use_decl_annotations_
SpriteFont::SpriteFont(ID3D11Device* device, wchar_t const* fileName, bool forceSRGB)
{
BinaryReader reader(fileName);
pImpl = std::make_unique<Impl>(device, &reader, forceSRGB);
}
// Construct from a binary blob created by the MakeSpriteFont utility and already loaded into memory.
_Use_decl_annotations_
SpriteFont::SpriteFont(ID3D11Device* device, uint8_t const* dataBlob, size_t dataSize, bool forceSRGB)
{
BinaryReader reader(dataBlob, dataSize);
pImpl = std::make_unique<Impl>(device, &reader, forceSRGB);
}
// Construct from arbitrary user specified glyph data (for those not using the MakeSpriteFont utility).
_Use_decl_annotations_
SpriteFont::SpriteFont(ID3D11ShaderResourceView* texture, Glyph const* glyphs, size_t glyphCount, float lineSpacing)
: pImpl(std::make_unique<Impl>(texture, glyphs, glyphCount, lineSpacing))
{}
SpriteFont::SpriteFont(SpriteFont&&) noexcept = default;
SpriteFont& SpriteFont::operator= (SpriteFont&&) noexcept = default;
SpriteFont::~SpriteFont() = default;
// Wide-character / UTF-16LE
void XM_CALLCONV SpriteFont::DrawString(_In_ SpriteBatch* spriteBatch, _In_z_ wchar_t const* text, XMFLOAT2 const& position, FXMVECTOR color, float rotation, XMFLOAT2 const& origin, float scale, SpriteEffects effects, float layerDepth) const
{
DrawString(spriteBatch, text, XMLoadFloat2(&position), color, rotation, XMLoadFloat2(&origin), XMVectorReplicate(scale), effects, layerDepth);
}
void XM_CALLCONV SpriteFont::DrawString(_In_ SpriteBatch* spriteBatch, _In_z_ wchar_t const* text, XMFLOAT2 const& position, FXMVECTOR color, float rotation, XMFLOAT2 const& origin, XMFLOAT2 const& scale, SpriteEffects effects, float layerDepth) const
{
DrawString(spriteBatch, text, XMLoadFloat2(&position), color, rotation, XMLoadFloat2(&origin), XMLoadFloat2(&scale), effects, layerDepth);
}
void XM_CALLCONV SpriteFont::DrawString(_In_ SpriteBatch* spriteBatch, _In_z_ wchar_t const* text, FXMVECTOR position, FXMVECTOR color, float rotation, FXMVECTOR origin, float scale, SpriteEffects effects, float layerDepth) const
{
DrawString(spriteBatch, text, position, color, rotation, origin, XMVectorReplicate(scale), effects, layerDepth);
}
void XM_CALLCONV SpriteFont::DrawString(_In_ SpriteBatch* spriteBatch, _In_z_ wchar_t const* text, FXMVECTOR position, FXMVECTOR color, float rotation, FXMVECTOR origin, GXMVECTOR scale, SpriteEffects effects, float layerDepth) const
{
static_assert(SpriteEffects_FlipHorizontally == 1 &&
SpriteEffects_FlipVertically == 2, "If you change these enum values, the following tables must be updated to match");
// Lookup table indicates which way to move along each axis per SpriteEffects enum value.
static XMVECTORF32 axisDirectionTable[4] =
{
{ { { -1, -1, 0, 0 } } },
{ { { 1, -1, 0, 0 } } },
{ { { -1, 1, 0, 0 } } },
{ { { 1, 1, 0, 0 } } },
};
// Lookup table indicates which axes are mirrored for each SpriteEffects enum value.
static XMVECTORF32 axisIsMirroredTable[4] =
{
{ { { 0, 0, 0, 0 } } },
{ { { 1, 0, 0, 0 } } },
{ { { 0, 1, 0, 0 } } },
{ { { 1, 1, 0, 0 } } },
};
XMVECTOR baseOffset = origin;
// If the text is mirrored, offset the start position accordingly.
if (effects)
{
baseOffset = XMVectorNegativeMultiplySubtract(
MeasureString(text),
axisIsMirroredTable[effects & 3],
baseOffset);
}
// Draw each character in turn.
pImpl->ForEachGlyph(text, [&](Glyph const* glyph, float x, float y, float advance)
{
UNREFERENCED_PARAMETER(advance);
XMVECTOR offset = XMVectorMultiplyAdd(XMVectorSet(x, y + glyph->YOffset, 0, 0), axisDirectionTable[effects & 3], baseOffset);
if (effects)
{
// For mirrored characters, specify bottom and/or right instead of top left.
XMVECTOR glyphRect = XMConvertVectorIntToFloat(XMLoadInt4(reinterpret_cast<uint32_t const*>(&glyph->Subrect)), 0);
// xy = glyph width/height.
glyphRect = XMVectorSubtract(XMVectorSwizzle<2, 3, 0, 1>(glyphRect), glyphRect);
offset = XMVectorMultiplyAdd(glyphRect, axisIsMirroredTable[effects & 3], offset);
}
if (pImpl->pixelAlignment)
{
offset = XMVectorRound(offset);
}
spriteBatch->Draw(pImpl->texture.Get(), position, &glyph->Subrect, color, rotation, offset, scale, effects, layerDepth);
}, true);
}
XMVECTOR XM_CALLCONV SpriteFont::MeasureString(_In_z_ wchar_t const* text, bool ignoreWhitespace) const
{
XMVECTOR result = XMVectorZero();
pImpl->ForEachGlyph(text, [&](Glyph const* glyph, float x, float y, float advance)
{
UNREFERENCED_PARAMETER(advance);
const auto w = static_cast<float>(glyph->Subrect.right - glyph->Subrect.left);
auto h = static_cast<float>(glyph->Subrect.bottom - glyph->Subrect.top) + glyph->YOffset;
h = iswspace(wchar_t(glyph->Character)) ?
pImpl->lineSpacing :
std::max(h, pImpl->lineSpacing);
result = XMVectorMax(result, XMVectorSet(x + w, y + h, 0, 0));
}, ignoreWhitespace);
return result;
}
RECT SpriteFont::MeasureDrawBounds(_In_z_ wchar_t const* text, XMFLOAT2 const& position, bool ignoreWhitespace) const
{
RECT result = { LONG_MAX, LONG_MAX, 0, 0 };
pImpl->ForEachGlyph(text, [&](Glyph const* glyph, float x, float y, float advance) noexcept
{
const auto isWhitespace = iswspace(wchar_t(glyph->Character));
const auto w = static_cast<float>(glyph->Subrect.right - glyph->Subrect.left);
const auto h = isWhitespace ?
pImpl->lineSpacing :
static_cast<float>(glyph->Subrect.bottom - glyph->Subrect.top);
const float minX = position.x + x;
const float minY = position.y + y + (isWhitespace ? 0.0f : glyph->YOffset);
const float maxX = std::max(minX + advance, minX + w);
const float maxY = minY + h;
if (minX < float(result.left))
result.left = long(minX);
if (minY < float(result.top))
result.top = long(minY);
if (float(result.right) < maxX)
result.right = long(maxX);
if (float(result.bottom) < maxY)
result.bottom = long(maxY);
}, ignoreWhitespace);
if (result.left == LONG_MAX)
{
result.left = 0;
result.top = 0;
}
return result;
}
RECT XM_CALLCONV SpriteFont::MeasureDrawBounds(_In_z_ wchar_t const* text, FXMVECTOR position, bool ignoreWhitespace) const
{
XMFLOAT2 pos;
XMStoreFloat2(&pos, position);
return MeasureDrawBounds(text, pos, ignoreWhitespace);
}
// UTF-8
void XM_CALLCONV SpriteFont::DrawString(_In_ SpriteBatch* spriteBatch, _In_z_ char const* text, XMFLOAT2 const& position, FXMVECTOR color, float rotation, XMFLOAT2 const& origin, float scale, SpriteEffects effects, float layerDepth) const
{
DrawString(spriteBatch, pImpl->ConvertUTF8(text), XMLoadFloat2(&position), color, rotation, XMLoadFloat2(&origin), XMVectorReplicate(scale), effects, layerDepth);
}
void XM_CALLCONV SpriteFont::DrawString(_In_ SpriteBatch* spriteBatch, _In_z_ char const* text, XMFLOAT2 const& position, FXMVECTOR color, float rotation, XMFLOAT2 const& origin, XMFLOAT2 const& scale, SpriteEffects effects, float layerDepth) const
{
DrawString(spriteBatch, pImpl->ConvertUTF8(text), XMLoadFloat2(&position), color, rotation, XMLoadFloat2(&origin), XMLoadFloat2(&scale), effects, layerDepth);
}
void XM_CALLCONV SpriteFont::DrawString(_In_ SpriteBatch* spriteBatch, _In_z_ char const* text, FXMVECTOR position, FXMVECTOR color, float rotation, FXMVECTOR origin, float scale, SpriteEffects effects, float layerDepth) const
{
DrawString(spriteBatch, pImpl->ConvertUTF8(text), position, color, rotation, origin, XMVectorReplicate(scale), effects, layerDepth);
}
void XM_CALLCONV SpriteFont::DrawString(_In_ SpriteBatch* spriteBatch, _In_z_ char const* text, FXMVECTOR position, FXMVECTOR color, float rotation, FXMVECTOR origin, GXMVECTOR scale, SpriteEffects effects, float layerDepth) const
{
DrawString(spriteBatch, pImpl->ConvertUTF8(text), position, color, rotation, origin, scale, effects, layerDepth);
}
XMVECTOR XM_CALLCONV SpriteFont::MeasureString(_In_z_ char const* text, bool ignoreWhitespace) const
{
return MeasureString(pImpl->ConvertUTF8(text), ignoreWhitespace);
}
RECT SpriteFont::MeasureDrawBounds(_In_z_ char const* text, XMFLOAT2 const& position, bool ignoreWhitespace) const
{
return MeasureDrawBounds(pImpl->ConvertUTF8(text), position, ignoreWhitespace);
}
RECT XM_CALLCONV SpriteFont::MeasureDrawBounds(_In_z_ char const* text, FXMVECTOR position, bool ignoreWhitespace) const
{
XMFLOAT2 pos;
XMStoreFloat2(&pos, position);
return MeasureDrawBounds(pImpl->ConvertUTF8(text), pos, ignoreWhitespace);
}
// Spacing properties
float SpriteFont::GetLineSpacing() const noexcept
{
return pImpl->lineSpacing;
}
void SpriteFont::SetLineSpacing(float spacing) noexcept
{
pImpl->lineSpacing = spacing;
}
void SpriteFont::SetPixelAlignment(bool enable) noexcept
{
pImpl->pixelAlignment = enable;
}
// Font properties
wchar_t SpriteFont::GetDefaultCharacter() const noexcept
{
return static_cast<wchar_t>(pImpl->defaultGlyph ? pImpl->defaultGlyph->Character : 0);
}
void SpriteFont::SetDefaultCharacter(wchar_t character)
{
pImpl->SetDefaultCharacter(character);
}
bool SpriteFont::ContainsCharacter(wchar_t character) const
{
return std::binary_search(pImpl->glyphs.begin(), pImpl->glyphs.end(), character);
}
// Custom layout/rendering
SpriteFont::Glyph const* SpriteFont::FindGlyph(wchar_t character) const
{
return pImpl->FindGlyph(character);
}
void SpriteFont::GetSpriteSheet(ID3D11ShaderResourceView** texture) const
{
if (!texture)
return;
ThrowIfFailed(pImpl->texture.CopyTo(texture));
}
//--------------------------------------------------------------------------------------
// Adapters for /Zc:wchar_t- clients
#if defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED)
SpriteFont::SpriteFont(_In_ ID3D11Device* device, _In_z_ __wchar_t const* fileName, bool forceSRGB) :
SpriteFont(device, reinterpret_cast<const unsigned short*>(fileName), forceSRGB)
{}
void SpriteFont::DrawString(_In_ SpriteBatch* spriteBatch, _In_z_ __wchar_t const* text, XMFLOAT2 const& position, FXMVECTOR color, float rotation, XMFLOAT2 const& origin, float scale, SpriteEffects effects, float layerDepth) const
{
DrawString(spriteBatch, reinterpret_cast<const unsigned short*>(text), XMLoadFloat2(&position), color, rotation, XMLoadFloat2(&origin), XMVectorReplicate(scale), effects, layerDepth);
}
void SpriteFont::DrawString(_In_ SpriteBatch* spriteBatch, _In_z_ __wchar_t const* text, XMFLOAT2 const& position, FXMVECTOR color, float rotation, XMFLOAT2 const& origin, XMFLOAT2 const& scale, SpriteEffects effects, float layerDepth) const
{
DrawString(spriteBatch, reinterpret_cast<const unsigned short*>(text), XMLoadFloat2(&position), color, rotation, XMLoadFloat2(&origin), XMLoadFloat2(&scale), effects, layerDepth);
}
void SpriteFont::DrawString(_In_ SpriteBatch* spriteBatch, _In_z_ __wchar_t const* text, FXMVECTOR position, FXMVECTOR color, float rotation, FXMVECTOR origin, float scale, SpriteEffects effects, float layerDepth) const
{
DrawString(spriteBatch, reinterpret_cast<const unsigned short*>(text), position, color, rotation, origin, XMVectorReplicate(scale), effects, layerDepth);
}
void XM_CALLCONV SpriteFont::DrawString(_In_ SpriteBatch* spriteBatch, _In_z_ __wchar_t const* text, FXMVECTOR position, FXMVECTOR color, float rotation, FXMVECTOR origin, GXMVECTOR scale, SpriteEffects effects, float layerDepth) const
{
DrawString(spriteBatch, reinterpret_cast<const unsigned short*>(text), position, color, rotation, origin, scale, effects, layerDepth);
}
XMVECTOR SpriteFont::MeasureString(_In_z_ __wchar_t const* text, bool ignoreWhitespace) const
{
return MeasureString(reinterpret_cast<const unsigned short*>(text), ignoreWhitespace);
}
RECT SpriteFont::MeasureDrawBounds(_In_z_ __wchar_t const* text, XMFLOAT2 const& position, bool ignoreWhitespace) const
{
return MeasureDrawBounds(reinterpret_cast<const unsigned short*>(text), position, ignoreWhitespace);
}
RECT SpriteFont::MeasureDrawBounds(_In_z_ __wchar_t const* text, FXMVECTOR position, bool ignoreWhitespace) const
{
XMFLOAT2 pos;
XMStoreFloat2(&pos, position);
return MeasureDrawBounds(reinterpret_cast<const unsigned short*>(text), pos, ignoreWhitespace);
}
// Can't do this for GetDefaultCharacter since it only differs by return type.
void SpriteFont::SetDefaultCharacter(__wchar_t character)
{
pImpl->SetDefaultCharacter(static_cast<unsigned short>(character));
}
bool SpriteFont::ContainsCharacter(__wchar_t character) const
{
return ContainsCharacter(static_cast<unsigned short>(character));
}
SpriteFont::Glyph const* SpriteFont::FindGlyph(__wchar_t character) const
{
return pImpl->FindGlyph(static_cast<unsigned short>(character));
}
#endif // !_NATIVE_WCHAR_T_DEFINED