-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiFont.cpp
More file actions
875 lines (789 loc) · 25.1 KB
/
Copy pathwiFont.cpp
File metadata and controls
875 lines (789 loc) · 25.1 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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
#include "wiFont.h"
#include "wiRenderer.h"
#include "wiResourceManager.h"
#include "wiHelper.h"
#include "shaders/ShaderInterop_Font.h"
#include "wiBacklog.h"
#include "wiTextureHelper.h"
#include "wiRectPacker.h"
#include "wiSpinLock.h"
#include "wiPlatform.h"
#include "wiEventHandler.h"
#include "wiTimer.h"
#include "wiUnorderedMap.h"
#include "wiUnorderedSet.h"
#include "wiVector.h"
#include "wiMath.h"
#include "Utility/liberation_sans.h"
#include "Utility/stb_truetype.h"
#include <fstream>
#include <mutex>
using namespace wi::enums;
using namespace wi::graphics;
namespace wi::font
{
namespace font_internal
{
enum DEPTH_TEST_MODE
{
DEPTH_TEST_OFF,
DEPTH_TEST_ON,
DEPTH_TEST_MODE_COUNT
};
static BlendState blendState;
static RasterizerState rasterizerState;
static DepthStencilState depthStencilStates[DEPTH_TEST_MODE_COUNT];
static Shader vertexShader;
static Shader pixelShader;
static PipelineState PSO[DEPTH_TEST_MODE_COUNT];
static thread_local wi::Canvas canvas;
static Texture texture;
struct FontStyle
{
std::string name;
wi::vector<uint8_t> fontBuffer; // only used if loaded from file, need to keep alive
stbtt_fontinfo fontInfo;
int ascent, descent, lineGap;
void Create(const std::string& newName, const uint8_t* data, size_t size)
{
name = newName;
int offset = stbtt_GetFontOffsetForIndex(data, 0);
if (!stbtt_InitFont(&fontInfo, data, offset))
{
wi::backlog::post("Failed to load font: " + name + " (file was unrecognized, it must be a .ttf file)");
}
stbtt_GetFontVMetrics(&fontInfo, &ascent, &descent, &lineGap);
}
void Create(const std::string& newName)
{
if (wi::helper::FileRead(newName, fontBuffer))
{
Create(newName, fontBuffer.data(), fontBuffer.size());
}
else
{
wi::backlog::post("Failed to load font: " + name + " (file could not be opened)");
}
}
};
static wi::vector<std::unique_ptr<FontStyle>> fontStyles;
struct Glyph
{
float x;
float y;
float width;
float height;
float tc_left;
float tc_right;
float tc_top;
float tc_bottom;
const FontStyle* fontStyle = nullptr;
};
static wi::unordered_map<int32_t, Glyph> glyph_lookup;
static wi::unordered_map<int32_t, wi::rectpacker::Rect> rect_lookup;
struct Bitmap
{
int width;
int height;
int xoff;
int yoff;
wi::vector<uint8_t> data;
};
static wi::unordered_map<int32_t, Bitmap> bitmap_lookup;
union GlyphHash
{
struct
{
uint32_t code : 16; // character code range supported: 0 - 65535
uint32_t height : 10; // height supported: 0 - 1023
uint32_t style : 5; // number of font styles supported: 0 - 31
uint32_t sdf : 1; // true or false
} bits;
uint32_t raw;
};
static_assert(sizeof(GlyphHash) == sizeof(uint32_t));
static wi::unordered_set<uint32_t> pendingGlyphs;
static std::mutex locker;
struct ParseStatus
{
Cursor cursor;
uint32_t quadCount = 0;
size_t last_word_begin = 0;
bool start_new_word = false;
};
static thread_local wi::vector<FontVertex> vertexList;
ParseStatus ParseText(const wchar_t* text, size_t text_length, const Params& params)
{
ParseStatus status;
status.cursor = params.cursor;
vertexList.clear();
const float whitespace_size = (float(params.size) + params.spacingX) * 0.25f;
const float tab_size = whitespace_size * 4;
const float linebreak_size = (float(params.size) + params.spacingY);
auto word_wrap = [&] {
status.start_new_word = true;
if (status.last_word_begin > 0 && params.h_wrap >= 0 && status.cursor.position.x >= params.h_wrap - 1)
{
// Word ended and wrap detected, push down last word by one line:
const float word_offset = vertexList[status.last_word_begin].pos.x;
for (size_t i = status.last_word_begin; i < status.quadCount * 4; ++i)
{
vertexList[i].pos.x -= word_offset;
vertexList[i].pos.y += linebreak_size;
}
status.cursor.position.x -= word_offset;
status.cursor.position.y += linebreak_size;
status.cursor.size.x = std::max(status.cursor.size.x, status.cursor.position.x);
status.cursor.size.y = std::max(status.cursor.size.y, status.cursor.position.y + linebreak_size);
}
};
status.cursor.size.y = status.cursor.position.y + linebreak_size;
for (size_t i = 0; i < text_length; ++i)
{
int code = (int)text[i];
GlyphHash hash;
hash.bits.code = text[i];
hash.bits.height = params.size;
hash.bits.style = (uint32_t)params.style;
hash.bits.sdf = params.isSDFRenderingEnabled() ? 1 : 0;
if (glyph_lookup.count(hash.raw) == 0)
{
// glyph not packed yet, so add to pending list:
std::scoped_lock lck(locker);
pendingGlyphs.insert(hash.raw);
continue;
}
if (code == '\n')
{
word_wrap();
status.cursor.position.x = 0;
status.cursor.position.y += linebreak_size;
}
else if (code == ' ')
{
word_wrap();
status.cursor.position.x += whitespace_size;
}
else if (code == '\t')
{
word_wrap();
status.cursor.position.x += tab_size;
}
else if (code == '\r')
{
}
else
{
const Glyph& glyph = glyph_lookup.at(hash.raw);
const float glyphWidth = glyph.width;
const float glyphHeight = glyph.height;
const float glyphOffsetX = glyph.x;
const float glyphOffsetY = glyph.y;
const float fontScale = stbtt_ScaleForPixelHeight(&glyph.fontStyle->fontInfo, (float)params.size);
const size_t vertexID = size_t(status.quadCount) * 4;
vertexList.resize(vertexID + 4);
status.quadCount++;
if (status.start_new_word)
{
status.last_word_begin = vertexID;
}
status.start_new_word = false;
const float left = status.cursor.position.x + glyphOffsetX;
const float right = left + glyphWidth;
const float top = status.cursor.position.y + glyphOffsetY;
const float bottom = top + glyphHeight;
vertexList[vertexID + 0].pos = float2(left, top);
vertexList[vertexID + 1].pos = float2(right, top);
vertexList[vertexID + 2].pos = float2(left, bottom);
vertexList[vertexID + 3].pos = float2(right, bottom);
float tc_left = glyph.tc_left;
float tc_right = glyph.tc_right;
float tc_top = glyph.tc_top;
float tc_bottom = glyph.tc_bottom;
if (params.isFlippedHorizontally())
{
std::swap(tc_left, tc_right);
}
if (params.isFlippedVertically())
{
std::swap(tc_top, tc_bottom);
}
vertexList[vertexID + 0].uv = float2(tc_left, tc_top);
vertexList[vertexID + 1].uv = float2(tc_right, tc_top);
vertexList[vertexID + 2].uv = float2(tc_left, tc_bottom);
vertexList[vertexID + 3].uv = float2(tc_right, tc_bottom);
int advance, lsb;
stbtt_GetCodepointHMetrics(&glyph.fontStyle->fontInfo, code, &advance, &lsb);
status.cursor.position.x += advance * fontScale;
status.cursor.position.x += params.spacingX;
if (text_length > 1 && i < text_length - 1 && text[i + 1])
{
int code_next = (int)text[i + 1];
int kern = stbtt_GetCodepointKernAdvance(&glyph.fontStyle->fontInfo, code, code_next);
status.cursor.position.x += kern * fontScale;
}
}
status.cursor.size.x = std::max(status.cursor.size.x, status.cursor.position.x);
status.cursor.size.y = std::max(status.cursor.size.y, status.cursor.position.y + linebreak_size);
}
word_wrap();
return status;
}
thread_local static std::string char_temp_buffer;
thread_local static std::wstring wchar_temp_buffer;
ParseStatus ParseText(const char* text, size_t text_length, const Params& params)
{
// the temp buffers are used to avoid allocations of string objects:
char_temp_buffer = text;
wi::helper::StringConvert(char_temp_buffer, wchar_temp_buffer);
return ParseText(wchar_temp_buffer.c_str(), wchar_temp_buffer.length(), params);
}
void CommitText(void* vertexList_GPU)
{
std::memcpy(vertexList_GPU, vertexList.data(), sizeof(FontVertex) * vertexList.size());
}
}
using namespace font_internal;
void LoadShaders()
{
wi::renderer::LoadShader(ShaderStage::VS, vertexShader, "fontVS.cso");
wi::renderer::LoadShader(ShaderStage::PS, pixelShader, "fontPS.cso");
for (int d = 0; d < DEPTH_TEST_MODE_COUNT; ++d)
{
PipelineStateDesc desc;
desc.vs = &vertexShader;
desc.ps = &pixelShader;
desc.bs = &blendState;
desc.dss = &depthStencilStates[d];
desc.rs = &rasterizerState;
desc.pt = PrimitiveTopology::TRIANGLESTRIP;
wi::graphics::GetDevice()->CreatePipelineState(&desc, &PSO[d]);
}
}
void Initialize()
{
wi::Timer timer;
// add default font if there is none yet:
if (fontStyles.empty())
{
wi::vector<uint8_t> data;
helper::Decompress(liberation_sans_zstd, sizeof(liberation_sans_zstd), data);
AddFontStyle("Liberation Sans", data.data(), data.size(), true);
}
RasterizerState rs;
rs.fill_mode = FillMode::SOLID;
rs.cull_mode = CullMode::NONE;
rs.front_counter_clockwise = true;
rs.depth_bias = 0;
rs.depth_bias_clamp = 0;
rs.slope_scaled_depth_bias = 0;
rs.depth_clip_enable = false;
rs.multisample_enable = false;
rs.antialiased_line_enable = false;
rasterizerState = rs;
BlendState bd;
bd.render_target[0].blend_enable = true;
bd.render_target[0].src_blend = Blend::ONE; // premultiplied blending
bd.render_target[0].dest_blend = Blend::INV_SRC_ALPHA;
bd.render_target[0].blend_op = BlendOp::ADD;
bd.render_target[0].src_blend_alpha = Blend::ONE;
bd.render_target[0].dest_blend_alpha = Blend::INV_SRC_ALPHA;
bd.render_target[0].blend_op_alpha = BlendOp::ADD;
bd.render_target[0].render_target_write_mask = ColorWrite::ENABLE_ALL;
bd.independent_blend_enable = false;
blendState = bd;
DepthStencilState dsd;
dsd.depth_enable = false;
dsd.stencil_enable = false;
depthStencilStates[DEPTH_TEST_OFF] = dsd;
dsd.depth_enable = true;
dsd.depth_write_mask = DepthWriteMask::ZERO;
dsd.depth_func = ComparisonFunc::GREATER_EQUAL;
depthStencilStates[DEPTH_TEST_ON] = dsd;
static wi::eventhandler::Handle handle1 = wi::eventhandler::Subscribe(wi::eventhandler::EVENT_RELOAD_SHADERS, [](uint64_t userdata) { LoadShaders(); });
LoadShaders();
wilog("wi::font Initialized (%d ms)", (int)std::round(timer.elapsed()));
}
void InvalidateAtlas()
{
texture = {};
glyph_lookup.clear();
rect_lookup.clear();
bitmap_lookup.clear();
}
void UpdateAtlas(float upscaling)
{
std::scoped_lock lck(locker);
upscaling = std::max(1.5f, upscaling); // add some minimum upscaling, especially for SDF
static float upscaling_prev = 1;
const float upscaling_rcp = 1.0f / upscaling;
if (upscaling_prev != upscaling)
{
// If upscaling changed (DPI change), clear glyph caches, they will need to be re-rendered:
InvalidateAtlas();
upscaling_prev = upscaling;
}
// If there are pending glyphs, render them and repack the atlas:
if (!pendingGlyphs.empty())
{
for (int32_t raw : pendingGlyphs)
{
GlyphHash hash;
hash.raw = raw;
const int code = (int)hash.bits.code;
const float height = (float)hash.bits.height;
const bool is_sdf = hash.bits.sdf ? true : false;
uint32_t style = hash.bits.style;
FontStyle* fontStyle = fontStyles[style].get();
int glyphIndex = stbtt_FindGlyphIndex(&fontStyle->fontInfo, code);
if (glyphIndex == 0)
{
// Try fallback to an other font style that has this character:
style = 0;
while (glyphIndex == 0 && style < fontStyles.size())
{
fontStyle = fontStyles[style].get();
glyphIndex = stbtt_FindGlyphIndex(&fontStyle->fontInfo, code);
style++;
}
}
float fontScaling = stbtt_ScaleForPixelHeight(&fontStyle->fontInfo, height * upscaling);
Bitmap& bitmap = bitmap_lookup[hash.raw];
bitmap.width = 0;
bitmap.height = 0;
bitmap.xoff = 0;
bitmap.yoff = 0;
if (is_sdf)
{
unsigned char* data = stbtt_GetGlyphSDF(
&fontStyle->fontInfo,
fontScaling,
glyphIndex,
(int)SDF::padding,
(unsigned char)SDF::onedge_value,
SDF::pixel_dist_scale,
&bitmap.width,
&bitmap.height,
&bitmap.xoff,
&bitmap.yoff
);
bitmap.data.resize(bitmap.width * bitmap.height);
if (data) std::memcpy(bitmap.data.data(), data, bitmap.data.size());
stbtt_FreeSDF(data, nullptr);
}
else
{
unsigned char* data = stbtt_GetGlyphBitmap(
&fontStyle->fontInfo,
fontScaling,
fontScaling,
glyphIndex,
&bitmap.width,
&bitmap.height,
&bitmap.xoff,
&bitmap.yoff
);
bitmap.data.resize(bitmap.width * bitmap.height);
if (data) std::memcpy(bitmap.data.data(), data, bitmap.data.size());
stbtt_FreeBitmap(data, nullptr);
}
wi::rectpacker::Rect rect = {};
rect.w = bitmap.width + 2;
rect.h = bitmap.height + 2;
rect.id = hash.raw;
rect_lookup[hash.raw] = rect;
Glyph& glyph = glyph_lookup[hash.raw];
glyph.x = float(bitmap.xoff) * upscaling_rcp;
glyph.y = (float(bitmap.yoff) + float(fontStyle->ascent) * fontScaling) * upscaling_rcp;
glyph.width = float(bitmap.width) * upscaling_rcp;
glyph.height = float(bitmap.height) * upscaling_rcp;
glyph.fontStyle = fontStyle;
}
pendingGlyphs.clear();
// Setup packer, this will allocate memory if needed:
static thread_local wi::rectpacker::State packer;
packer.clear();
for (auto& it : rect_lookup)
{
packer.add_rect(it.second);
}
// Perform packing and process the result if successful:
if (packer.pack(4096))
{
// Retrieve texture atlas dimensions:
const int atlasWidth = packer.width;
const int atlasHeight = packer.height;
const float inv_width = 1.0f / atlasWidth;
const float inv_height = 1.0f / atlasHeight;
// Create the CPU-side texture atlas and fill with transparency (0):
wi::vector<uint8_t> atlas(size_t(atlasWidth) * size_t(atlasHeight));
std::fill(atlas.begin(), atlas.end(), 0);
// Iterate all packed glyph rectangles:
for (auto& rect : packer.rects)
{
rect.x += 1;
rect.y += 1;
rect.w -= 2;
rect.h -= 2;
const int32_t hash = rect.id;
//const wchar_t code = codefromhash(hash);
//const int style = stylefromhash(hash);
//const float height = (float)heightfromhash(hash);
Glyph& glyph = glyph_lookup[hash];
Bitmap& bitmap = bitmap_lookup[hash];
for (int row = 0; row < bitmap.height; ++row)
{
uint8_t* dst = atlas.data() + rect.x + (rect.y + row) * atlasWidth;
uint8_t* src = bitmap.data.data() + row * bitmap.width;
std::memcpy(dst, src, bitmap.width);
}
// Compute texture coordinates for the glyph:
glyph.tc_left = float(rect.x);
glyph.tc_right = glyph.tc_left + float(rect.w);
glyph.tc_top = float(rect.y);
glyph.tc_bottom = glyph.tc_top + float(rect.h);
glyph.tc_left *= inv_width;
glyph.tc_right *= inv_width;
glyph.tc_top *= inv_height;
glyph.tc_bottom *= inv_height;
}
// Upload the CPU-side texture atlas bitmap to the GPU:
wi::texturehelper::CreateTexture(texture, atlas.data(), atlasWidth, atlasHeight, Format::R8_UNORM);
GetDevice()->SetName(&texture, "wi::font::texture");
}
else
{
assert(0); // rect packing failure
}
}
}
const Texture* GetAtlas()
{
return &texture;
}
int AddFontStyle(const std::string& fontName)
{
std::scoped_lock lck(locker);
for (size_t i = 0; i < fontStyles.size(); i++)
{
const FontStyle& fontStyle = *fontStyles[i];
if (fontStyle.name.compare(fontName) == 0)
{
return int(i);
}
}
fontStyles.push_back(std::make_unique<FontStyle>());
fontStyles.back()->Create(fontName);
InvalidateAtlas(); // invalidate atlas, in case there were missing glyphs, upon adding new font style they could become valid
return int(fontStyles.size() - 1);
}
int AddFontStyle(const std::string& fontName, const uint8_t* data, size_t size, bool copyData)
{
std::scoped_lock lck(locker);
for (size_t i = 0; i < fontStyles.size(); i++)
{
const FontStyle& fontStyle = *fontStyles[i];
if (fontStyle.name.compare(fontName) == 0)
{
return int(i);
}
}
fontStyles.push_back(std::make_unique<FontStyle>());
if (copyData)
{
fontStyles.back()->fontBuffer.resize(size);
std::memcpy(fontStyles.back()->fontBuffer.data(), data, size);
data = fontStyles.back()->fontBuffer.data();
}
fontStyles.back()->Create(fontName, data, size);
InvalidateAtlas(); // invalidate atlas, in case there were missing glyphs, upon adding new font style they could become valid
return int(fontStyles.size() - 1);
}
template<typename T>
Cursor Draw_internal(const T* text, size_t text_length, const Params& params, CommandList cmd)
{
if (text_length <= 0)
{
return Cursor();
}
ParseStatus status = ParseText(text, text_length, params);
if (status.quadCount > 0)
{
GraphicsDevice* device = wi::graphics::GetDevice();
GraphicsDevice::GPUAllocation mem = device->AllocateGPU(sizeof(FontVertex) * status.quadCount * 4, cmd);
if (!mem.IsValid())
{
return status.cursor;
}
CommitText(mem.data);
FontConstants font = {};
font.buffer_index = device->GetDescriptorIndex(&mem.buffer, SubresourceType::SRV);
font.buffer_offset = (uint32_t)mem.offset;
font.texture_index = device->GetDescriptorIndex(&texture, SubresourceType::SRV);
if (font.buffer_index < 0 || font.texture_index < 0)
{
return status.cursor;
}
device->EventBegin("Font", cmd);
device->BindPipelineState(&PSO[params.isDepthTestEnabled()], cmd);
using namespace wi::math;
XMFLOAT4 color = XMFLOAT4(1, 1, 1, 1);
float softness = 0;
float bolden = 0;
float hdr_scaling = 1;
uint32_t flags = 0;
if (params.isSDFRenderingEnabled())
{
flags |= FONT_FLAG_SDF_RENDERING;
}
if (params.isHDR10OutputMappingEnabled())
{
flags |= FONT_FLAG_OUTPUT_COLOR_SPACE_HDR10_ST2084;
}
if (params.isLinearOutputMappingEnabled())
{
flags |= FONT_FLAG_OUTPUT_COLOR_SPACE_LINEAR;
hdr_scaling = params.hdr_scaling;
}
XMFLOAT3 offset = XMFLOAT3(0, 0, 0);
float vertical_flip = params.customProjection == nullptr ? 1.0f : -1.0f;
if (params.h_align == WIFALIGN_CENTER)
offset.x -= status.cursor.size.x / 2;
else if (params.h_align == WIFALIGN_RIGHT)
offset.x -= status.cursor.size.x;
if (params.v_align == WIFALIGN_CENTER)
offset.y -= status.cursor.size.y / 2 * vertical_flip;
else if (params.v_align == WIFALIGN_BOTTOM)
offset.y -= status.cursor.size.y * vertical_flip;
XMMATRIX M = XMMatrixTranslation(offset.x, offset.y, offset.z);
M = M * XMMatrixScaling(params.scaling, params.scaling, params.scaling);
M = M * XMMatrixRotationZ(params.rotation);
if (params.customRotation != nullptr)
{
M = M * (*params.customRotation);
}
M = M * XMMatrixTranslation(params.position.x, params.position.y, params.position.z);
if (params.customProjection != nullptr)
{
M = XMMatrixScaling(1, -1, 1) * M; // reason: screen projection is Y down (like UV-space) and that is the common case for image rendering. But custom projections will use the "world space"
M = M * (*params.customProjection);
}
else
{
// Asserts will check that a proper canvas was set for this cmd with wi::image::SetCanvas()
// The canvas must be set to have dpi aware rendering
assert(canvas.width > 0);
assert(canvas.height > 0);
assert(canvas.dpi > 0);
M = M * canvas.GetProjection();
}
if (params.shadowColor.getA() > 0)
{
// font shadow render:
XMStoreFloat4x4(&font.transform, XMMatrixTranslation(params.shadow_offset_x, params.shadow_offset_y, 0) * M);
color = params.shadowColor;
color.x *= params.shadow_intensity;
color.y *= params.shadow_intensity;
color.z *= params.shadow_intensity;
font.color = pack_half4(color);
bolden = params.shadow_bolden;
softness = params.shadow_softness * 0.5f;
font.softness_bolden_hdrscaling = pack_half3(softness, bolden, hdr_scaling);
font.softness_bolden_hdrscaling.y |= flags << 16u;
device->BindDynamicConstantBuffer(font, CBSLOT_FONT, cmd);
device->DrawInstanced(4, status.quadCount, 0, 0, cmd);
}
// font base render:
XMStoreFloat4x4(&font.transform, M);
color = params.color;
color.x *= params.intensity;
color.y *= params.intensity;
color.z *= params.intensity;
font.color = pack_half4(color);
bolden = params.bolden;
softness = params.softness * 0.5f;
font.softness_bolden_hdrscaling = pack_half3(softness, bolden, hdr_scaling);
font.softness_bolden_hdrscaling.y |= flags << 16u;
device->BindDynamicConstantBuffer(font, CBSLOT_FONT, cmd);
device->DrawInstanced(4, status.quadCount, 0, 0, cmd);
device->EventEnd(cmd);
}
return status.cursor;
}
void SetCanvas(const wi::Canvas& current_canvas)
{
canvas = current_canvas;
}
Cursor Draw(const char* text, size_t text_length, const Params& params, CommandList cmd)
{
return Draw_internal(text, text_length, params, cmd);
}
Cursor Draw(const wchar_t* text, size_t text_length, const Params& params, CommandList cmd)
{
return Draw_internal(text, text_length, params, cmd);
}
Cursor Draw(const char* text, const Params& params, CommandList cmd)
{
return Draw_internal(text, strlen(text), params, cmd);
}
Cursor Draw(const wchar_t* text, const Params& params, CommandList cmd)
{
return Draw_internal(text, wcslen(text), params, cmd);
}
Cursor Draw(const std::string& text, const Params& params, CommandList cmd)
{
return Draw_internal(text.c_str(), text.length(), params, cmd);
}
Cursor Draw(const std::wstring& text, const Params& params, CommandList cmd)
{
return Draw_internal(text.c_str(), text.length(), params, cmd);
}
XMFLOAT2 TextSize(const char* text, size_t text_length, const Params& params)
{
if (text_length == 0)
{
return XMFLOAT2(0, 0);
}
return ParseText(text, text_length, params).cursor.size;
}
XMFLOAT2 TextSize(const wchar_t* text, size_t text_length, const Params& params)
{
if (text_length == 0)
{
return XMFLOAT2(0, 0);
}
return ParseText(text, text_length, params).cursor.size;
}
XMFLOAT2 TextSize(const char* text, const Params& params)
{
size_t text_length = strlen(text);
if (text_length == 0)
{
return XMFLOAT2(0, 0);
}
return ParseText(text, text_length, params).cursor.size;
}
XMFLOAT2 TextSize(const wchar_t* text, const Params& params)
{
size_t text_length = wcslen(text);
if (text_length == 0)
{
return XMFLOAT2(0, 0);
}
return ParseText(text, text_length, params).cursor.size;
}
XMFLOAT2 TextSize(const std::string& text, const Params& params)
{
if (text.empty())
{
return XMFLOAT2(0, 0);
}
return ParseText(text.c_str(), text.length(), params).cursor.size;
}
XMFLOAT2 TextSize(const std::wstring& text, const Params& params)
{
if (text.empty())
{
return XMFLOAT2(0, 0);
}
return ParseText(text.c_str(), text.length(), params).cursor.size;
}
Cursor TextCursor(const char* text, size_t text_length, const Params& params)
{
if (text_length == 0)
{
return {};
}
return ParseText(text, text_length, params).cursor;
}
Cursor TextCursor(const wchar_t* text, size_t text_length, const Params& params)
{
if (text_length == 0)
{
return {};
}
return ParseText(text, text_length, params).cursor;
}
Cursor TextCursor(const char* text, const Params& params)
{
size_t text_length = strlen(text);
if (text_length == 0)
{
return {};
}
return ParseText(text, text_length, params).cursor;
}
Cursor TextCursor(const wchar_t* text, const Params& params)
{
size_t text_length = wcslen(text);
if (text_length == 0)
{
return {};
}
return ParseText(text, text_length, params).cursor;
}
Cursor TextCursor(const std::string& text, const Params& params)
{
if (text.empty())
{
return {};
}
return ParseText(text.c_str(), text.length(), params).cursor;
}
Cursor TextCursor(const std::wstring& text, const Params& params)
{
if (text.empty())
{
return {};
}
return ParseText(text.c_str(), text.length(), params).cursor;
}
float TextWidth(const char* text, size_t text_length, const Params& params)
{
return TextSize(text, text_length, params).x;
}
float TextWidth(const wchar_t* text, size_t text_length, const Params& params)
{
return TextSize(text, text_length, params).x;
}
float TextWidth(const char* text, const Params& params)
{
return TextSize(text, params).x;
}
float TextWidth(const wchar_t* text, const Params& params)
{
return TextSize(text, params).x;
}
float TextWidth(const std::string& text, const Params& params)
{
return TextSize(text, params).x;
}
float TextWidth(const std::wstring& text, const Params& params)
{
return TextSize(text, params).x;
}
float TextHeight(const char* text, size_t text_length, const Params& params)
{
return TextSize(text, text_length, params).y;
}
float TextHeight(const wchar_t* text, size_t text_length, const Params& params)
{
return TextSize(text, text_length, params).y;
}
float TextHeight(const char* text, const Params& params)
{
return TextSize(text, params).y;
}
float TextHeight(const wchar_t* text, const Params& params)
{
return TextSize(text, params).y;
}
float TextHeight(const std::string& text, const Params& params)
{
return TextSize(text, params).y;
}
float TextHeight(const std::wstring& text, const Params& params)
{
return TextSize(text, params).y;
}
}