-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Expand file tree
/
Copy pathCustomTextLayout.cpp
More file actions
1757 lines (1540 loc) · 74.3 KB
/
CustomTextLayout.cpp
File metadata and controls
1757 lines (1540 loc) · 74.3 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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "CustomTextLayout.h"
#include "CustomTextRenderer.h"
#include <wrl.h>
#include <wrl/client.h>
#include <VersionHelpers.h>
#include "BoxDrawingEffect.h"
using namespace Microsoft::Console::Render;
// Routine Description:
// - Creates a CustomTextLayout object for calculating which glyphs should be placed and where
// Arguments:
// - dxFontRenderData - The DirectWrite font render data for our layout
CustomTextLayout::CustomTextLayout(const gsl::not_null<DxFontRenderData*> fontRenderData) :
_fontRenderData{ fontRenderData },
_formatInUse{ fontRenderData->DefaultTextFormat().Get() },
_fontInUse{ fontRenderData->DefaultFontFace().Get() },
_numberSubstitution{},
_readingDirection{ DWRITE_READING_DIRECTION_LEFT_TO_RIGHT },
_runs{},
_breakpoints{},
_runIndex{ 0 },
_width{ gsl::narrow_cast<size_t>(fontRenderData->GlyphCell().width) },
_isEntireTextSimple{ false }
{
_localeName.resize(gsl::narrow_cast<size_t>(fontRenderData->DefaultTextFormat()->GetLocaleNameLength()) + 1); // +1 for null
THROW_IF_FAILED(fontRenderData->DefaultTextFormat()->GetLocaleName(_localeName.data(), gsl::narrow<UINT32>(_localeName.size())));
}
//Routine Description:
// - Resets this custom text layout to the freshly allocated state in terms of text analysis.
// Arguments:
// - <none>, modifies internal state
// Return Value:
// - S_OK or suitable memory management issue
[[nodiscard]] HRESULT STDMETHODCALLTYPE CustomTextLayout::Reset() noexcept
try
{
_runs.clear();
_breakpoints.clear();
_runIndex = 0;
_isEntireTextSimple = false;
_textClusterColumns.clear();
_text.clear();
_glyphScaleCorrections.clear();
_glyphClusters.clear();
_glyphIndices.clear();
_glyphDesignUnitAdvances.clear();
_glyphAdvances.clear();
_glyphOffsets.clear();
return S_OK;
}
CATCH_RETURN()
// Routine Description:
// - Appends text to this layout for analysis/processing.
// Arguments:
// - clusters - From the backing buffer, the text to be displayed clustered by the columns it should consume.
// Return Value:
// - S_OK or suitable memory management issue.
[[nodiscard]] HRESULT STDMETHODCALLTYPE CustomTextLayout::AppendClusters(const std::span<const ::Microsoft::Console::Render::Cluster> clusters)
try
{
_textClusterColumns.reserve(_textClusterColumns.size() + clusters.size());
for (const auto& cluster : clusters)
{
const auto cols = gsl::narrow<UINT16>(cluster.GetColumns());
const auto text = cluster.GetText();
// Push back the number of columns for this bit of text.
_textClusterColumns.push_back(cols);
// If there is more than one text character here, push 0s for the rest of the columns
// of the text run.
_textClusterColumns.resize(_textClusterColumns.size() + base::ClampSub(text.size(), 1u), gsl::narrow_cast<UINT16>(0u));
_text += text;
}
return S_OK;
}
CATCH_RETURN()
// Routine Description:
// - Figures out how many columns this layout should take. This will use the analyze step only.
// Arguments:
// - columns - The number of columns the layout should consume when done.
// Return Value:
// - S_OK or suitable DirectX/DirectWrite/Direct2D result code.
[[nodiscard]] HRESULT STDMETHODCALLTYPE CustomTextLayout::GetColumns(_Out_ UINT32* columns)
{
RETURN_HR_IF_NULL(E_INVALIDARG, columns);
*columns = 0;
_formatInUse = _fontRenderData->DefaultTextFormat().Get();
_fontInUse = _fontRenderData->DefaultFontFace().Get();
RETURN_IF_FAILED(_AnalyzeTextComplexity());
RETURN_IF_FAILED(_AnalyzeRuns());
RETURN_IF_FAILED(_ShapeGlyphRuns());
const auto totalAdvance = std::accumulate(_glyphAdvances.cbegin(), _glyphAdvances.cend(), 0.0f);
*columns = static_cast<UINT32>(ceil(totalAdvance / _width));
return S_OK;
}
// Routine Description:
// - Implements a drawing interface similarly to the default IDWriteTextLayout which will
// take the string from construction, analyze it for complexity, shape up the glyphs,
// and then draw the final product to the given renderer at the point and pass along
// the context information.
// - This specific class does the layout calculations and complexity analysis, not the
// final drawing. That's the renderer's job (passed in.)
// Arguments:
// - clientDrawingContext - Optional pointer to information that the renderer might need
// while attempting to graphically place the text onto the screen
// - renderer - The interface to be used for actually putting text onto the screen
// - originX - X pixel point of top left corner on final surface for drawing
// - originY - Y pixel point of top left corner on final surface for drawing
// Return Value:
// - S_OK or suitable DirectX/DirectWrite/Direct2D result code.
[[nodiscard]] HRESULT STDMETHODCALLTYPE CustomTextLayout::Draw(_In_opt_ void* clientDrawingContext,
_In_ IDWriteTextRenderer* renderer,
FLOAT originX,
FLOAT originY) noexcept
try
{
const auto drawingContext = static_cast<const DrawingContext*>(clientDrawingContext);
auto weight = _fontRenderData->DefaultFontWeight();
auto style = _fontRenderData->DefaultFontStyle();
const auto stretch = _fontRenderData->DefaultFontStretch();
if (drawingContext->useBoldFont)
{
// TODO: "relative" bold?
weight = DWRITE_FONT_WEIGHT_BOLD;
// Since we are setting the font weight according to the text attribute,
// make sure to tell the text format to ignore the user set font weight
_fontRenderData->InhibitUserWeight(true);
}
else
{
_fontRenderData->InhibitUserWeight(false);
}
if (drawingContext->useItalicFont || _fontRenderData->DidUserSetItalic())
{
style = DWRITE_FONT_STYLE_ITALIC;
}
_formatInUse = _fontRenderData->TextFormatWithAttribute(weight, style, stretch).Get();
_fontInUse = _fontRenderData->FontFaceWithAttribute(weight, style, stretch).Get();
RETURN_IF_FAILED(_AnalyzeTextComplexity());
RETURN_IF_FAILED(_AnalyzeRuns());
RETURN_IF_FAILED(_ShapeGlyphRuns());
RETURN_IF_FAILED(_CorrectGlyphRuns());
// Correcting box drawing has to come after both font fallback and
// the glyph run advance correction (which will apply a font size scaling factor).
// We need to know all the proposed X and Y dimension metrics to get this right.
RETURN_IF_FAILED(_CorrectBoxDrawing());
RETURN_IF_FAILED(_DrawGlyphRuns(clientDrawingContext, renderer, { originX, originY }));
return S_OK;
}
CATCH_RETURN()
// Routine Description:
// - Uses the internal text information and the analyzers/font information from construction
// to determine the complexity of the text. If the text is determined to be entirely simple,
// we'll have more chances to optimize the layout process.
// Arguments:
// - <none> - Uses internal state
// Return Value:
// - S_OK or suitable DirectWrite or STL error code
[[nodiscard]] HRESULT CustomTextLayout::_AnalyzeTextComplexity() noexcept
{
try
{
const auto textLength = gsl::narrow<UINT32>(_text.size());
auto isTextSimple = FALSE;
UINT32 uiLengthRead = 0;
// Start from the beginning.
const UINT32 glyphStart = 0;
_glyphIndices.resize(textLength);
const auto hr = _fontRenderData->Analyzer()->GetTextComplexity(
_text.c_str(),
textLength,
_fontInUse,
&isTextSimple,
&uiLengthRead,
&_glyphIndices.at(glyphStart));
RETURN_IF_FAILED(hr);
_isEntireTextSimple = isTextSimple && uiLengthRead == textLength;
}
CATCH_RETURN();
return S_OK;
}
// Routine Description:
// - Uses the internal text information and the analyzers/font information from construction
// to determine the complexity of the text inside this layout, compute the subsections (or runs)
// that contain similar property information, and stores that information internally.
// - We determine line breakpoints, bidirectional information, the script properties,
// number substitution, and font fallback properties in this function.
// Arguments:
// - <none> - Uses internal state
// Return Value:
// - S_OK or suitable DirectWrite or STL error code
[[nodiscard]] HRESULT CustomTextLayout::_AnalyzeRuns() noexcept
{
try
{
// We're going to need the text length in UINT32 format for the DWrite calls.
// Convert it once up front.
const auto textLength = gsl::narrow<UINT32>(_text.size());
// Initially start out with one result that covers the entire range.
// This result will be subdivided by the analysis processes.
_runs.resize(1);
auto& initialRun = _runs.front();
initialRun.textLength = textLength;
initialRun.bidiLevel = (_readingDirection == DWRITE_READING_DIRECTION_RIGHT_TO_LEFT);
// Allocate enough room to have one breakpoint per code unit.
_breakpoints.resize(_text.size());
if (!_isEntireTextSimple || _fontRenderData->DidUserSetAxes())
{
// Call each of the analyzers in sequence, recording their results.
RETURN_IF_FAILED(_fontRenderData->Analyzer()->AnalyzeLineBreakpoints(this, 0, textLength, this));
RETURN_IF_FAILED(_fontRenderData->Analyzer()->AnalyzeBidi(this, 0, textLength, this));
RETURN_IF_FAILED(_fontRenderData->Analyzer()->AnalyzeScript(this, 0, textLength, this));
RETURN_IF_FAILED(_fontRenderData->Analyzer()->AnalyzeNumberSubstitution(this, 0, textLength, this));
// Perform our custom font fallback analyzer that mimics the pattern of the real analyzers.
RETURN_IF_FAILED(_AnalyzeFontFallback(this, 0, textLength));
}
// Ensure that a font face is attached to every run
for (auto& run : _runs)
{
if (!run.fontFace)
{
run.fontFace = _fontInUse;
}
}
// Resequence the resulting runs in order before returning to caller.
_OrderRuns();
}
CATCH_RETURN();
return S_OK;
}
// Routine Description:
// - Uses the internal run analysis information (from the analyze step) to map and shape out
// the glyphs from the fonts. This is effectively a loop of _ShapeGlyphRun. See it for details.
// Arguments:
// - <none> - Uses internal state
// Return Value:
// - S_OK or suitable DirectWrite or STL error code
[[nodiscard]] HRESULT CustomTextLayout::_ShapeGlyphRuns() noexcept
{
try
{
// Shapes all the glyph runs in the layout.
const auto textLength = gsl::narrow<UINT32>(_text.size());
// Estimate the maximum number of glyph indices needed to hold a string.
const auto estimatedGlyphCount = _EstimateGlyphCount(textLength);
_glyphIndices.resize(estimatedGlyphCount);
_glyphOffsets.resize(estimatedGlyphCount);
_glyphAdvances.resize(estimatedGlyphCount);
_glyphClusters.resize(textLength);
UINT32 glyphStart = 0;
// Shape each run separately. This is needed whenever script, locale,
// or reading direction changes.
for (UINT32 runIndex = 0; runIndex < _runs.size(); ++runIndex)
{
LOG_IF_FAILED(_ShapeGlyphRun(runIndex, glyphStart));
}
_glyphIndices.resize(glyphStart);
_glyphOffsets.resize(glyphStart);
_glyphAdvances.resize(glyphStart);
}
CATCH_RETURN();
return S_OK;
}
// Routine Description:
// - Calculates the following information for any one particular run of text:
// 1. Indices (finding the ID number in each font for each glyph)
// 2. Offsets (the left/right or top/bottom spacing from the baseline origin for each glyph)
// 3. Advances (the width allowed for every glyph)
// 4. Clusters (the bunches of glyphs that represent a particular combined character)
// - A run is defined by the analysis step as a substring of the original text that has similar properties
// such that it can be processed together as a unit.
// Arguments:
// - runIndex - The ID number of the internal runs array to use while shaping
// - glyphStart - On input, which portion of the internal indices/offsets/etc. arrays to use
// to write the shaping information.
// - On output, the position that should be used by the next call as its start position
// Return Value:
// - S_OK or suitable DirectWrite or STL error code
[[nodiscard]] HRESULT CustomTextLayout::_ShapeGlyphRun(const UINT32 runIndex, UINT32& glyphStart) noexcept
{
try
{
// Shapes a single run of text into glyphs.
// Alternately, you could iteratively interleave shaping and line
// breaking to reduce the number glyphs held onto at once. It's simpler
// for this demonstration to just do shaping and line breaking as two
// separate processes, but realize that this does have the consequence that
// certain advanced fonts containing line specific features (like Gabriola)
// will shape as if the line is not broken.
Run& run = _runs.at(runIndex);
const auto textStart = run.textStart;
const auto textLength = run.textLength;
auto maxGlyphCount = gsl::narrow<UINT32>(_glyphIndices.size() - glyphStart);
UINT32 actualGlyphCount = 0;
run.glyphStart = glyphStart;
run.glyphCount = 0;
if (textLength == 0)
{
return S_FALSE; // Nothing to do..
}
// Allocate space for shaping to fill with glyphs and other information,
// with about as many glyphs as there are text characters. We'll actually
// need more glyphs than codepoints if they are decomposed into separate
// glyphs, or fewer glyphs than codepoints if multiple are substituted
// into a single glyph. In any case, the shaping process will need some
// room to apply those rules to even make that determination.
if (textLength > maxGlyphCount)
{
maxGlyphCount = _EstimateGlyphCount(textLength);
const auto totalGlyphsArrayCount = glyphStart + maxGlyphCount;
_glyphIndices.resize(totalGlyphsArrayCount);
}
if (_isEntireTextSimple && !_fontRenderData->DidUserSetFeatures())
{
// When the entire text is simple, we can skip GetGlyphs and directly retrieve glyph indices and
// advances(in font design unit). With the help of font metrics, we can calculate the actual glyph
// advances without the need of GetGlyphPlacements. This shortcut will significantly reduce the time
// needed for text analysis.
DWRITE_FONT_METRICS1 metrics;
run.fontFace->GetMetrics(&metrics);
// With simple text, there's only one run. The actual glyph count is the same as textLength.
_glyphDesignUnitAdvances.resize(textLength);
_glyphAdvances.resize(textLength);
auto designUnitsPerEm = metrics.designUnitsPerEm;
RETURN_IF_FAILED(_fontInUse->GetDesignGlyphAdvances(
textLength,
&_glyphIndices.at(glyphStart),
&_glyphDesignUnitAdvances.at(glyphStart),
run.isSideways));
for (size_t i = glyphStart; i < _glyphAdvances.size(); i++)
{
_glyphAdvances.at(i) = (float)_glyphDesignUnitAdvances.at(i) / designUnitsPerEm * _formatInUse->GetFontSize() * run.fontScale;
}
// Set all the clusters as sequential. In a simple run, we're going 1 to 1.
// Fill the clusters sequentially from 0 to N-1.
std::iota(_glyphClusters.begin(), _glyphClusters.end(), gsl::narrow_cast<unsigned short>(0));
run.glyphCount = textLength;
glyphStart += textLength;
return S_OK;
}
std::vector<DWRITE_SHAPING_TEXT_PROPERTIES> textProps(textLength);
std::vector<DWRITE_SHAPING_GLYPH_PROPERTIES> glyphProps(maxGlyphCount);
// Get the features to apply to the font
const auto& features = _fontRenderData->DefaultFontFeatures();
#pragma warning(suppress : 26492) // Don't use const_cast to cast away const or volatile (type.3).
const DWRITE_TYPOGRAPHIC_FEATURES typographicFeatures = { const_cast<DWRITE_FONT_FEATURE*>(features.data()), gsl::narrow<uint32_t>(features.size()) };
DWRITE_TYPOGRAPHIC_FEATURES const* typographicFeaturesPointer = &typographicFeatures;
const uint32_t fontFeatureLengths[] = { textLength };
// Get the glyphs from the text, retrying if needed.
auto tries = 0;
#pragma warning(suppress : 26485) // so we can pass in the fontFeatureLengths to GetGlyphs without the analyzer complaining
auto hr = S_OK;
do
{
hr = _fontRenderData->Analyzer()->GetGlyphs(
&_text.at(textStart),
textLength,
run.fontFace.Get(),
run.isSideways, // isSideways,
WI_IsFlagSet(run.bidiLevel, 1), // isRightToLeft
&run.script,
_localeName.data(),
(run.isNumberSubstituted) ? _numberSubstitution.Get() : nullptr,
&typographicFeaturesPointer, // features
&fontFeatureLengths[0], // featureLengths
1, // featureCount
maxGlyphCount, // maxGlyphCount
&_glyphClusters.at(textStart),
&textProps.at(0),
&_glyphIndices.at(glyphStart),
&glyphProps.at(0),
&actualGlyphCount);
tries++;
if (hr == E_NOT_SUFFICIENT_BUFFER)
{
// Try again using a larger buffer.
maxGlyphCount = _EstimateGlyphCount(maxGlyphCount);
const auto totalGlyphsArrayCount = glyphStart + maxGlyphCount;
glyphProps.resize(maxGlyphCount);
_glyphIndices.resize(totalGlyphsArrayCount);
}
else
{
break;
}
} while (tries < 2); // We'll give it two chances.
RETURN_IF_FAILED(hr);
// Get the placement of the all the glyphs.
_glyphAdvances.resize(std::max(gsl::narrow_cast<size_t>(glyphStart) + gsl::narrow_cast<size_t>(actualGlyphCount), _glyphAdvances.size()));
_glyphOffsets.resize(std::max(gsl::narrow_cast<size_t>(glyphStart) + gsl::narrow_cast<size_t>(actualGlyphCount), _glyphOffsets.size()));
const auto fontSizeFormat = _formatInUse->GetFontSize();
const auto fontSize = fontSizeFormat * run.fontScale;
hr = _fontRenderData->Analyzer()->GetGlyphPlacements(
&_text.at(textStart),
&_glyphClusters.at(textStart),
&textProps.at(0),
textLength,
&_glyphIndices.at(glyphStart),
&glyphProps.at(0),
actualGlyphCount,
run.fontFace.Get(),
fontSize,
run.isSideways,
(run.bidiLevel & 1), // isRightToLeft
&run.script,
_localeName.data(),
&typographicFeaturesPointer, // features
&fontFeatureLengths[0], // featureLengths
1, // featureCount
&_glyphAdvances.at(glyphStart),
&_glyphOffsets.at(glyphStart));
RETURN_IF_FAILED(hr);
// Set the final glyph count of this run and advance the starting glyph.
run.glyphCount = actualGlyphCount;
glyphStart += actualGlyphCount;
}
CATCH_RETURN();
return S_OK;
}
// Routine Description:
// - Adjusts the glyph information from shaping to fit the layout pattern required
// for our renderer.
// This is effectively a loop of _CorrectGlyphRun. See it for details.
// Arguments:
// - <none> - Uses internal state
// Return Value:
// - S_OK or suitable DirectWrite or STL error code
[[nodiscard]] HRESULT CustomTextLayout::_CorrectGlyphRuns() noexcept
{
try
{
// For simple text, there is no need to correct runs.
if (_isEntireTextSimple)
{
return S_OK;
}
// Correct each run separately. This is needed whenever script, locale,
// or reading direction changes.
for (UINT32 runIndex = 0; runIndex < _runs.size(); ++runIndex)
{
LOG_IF_FAILED(_CorrectGlyphRun(runIndex));
}
// If scale corrections were needed, we need to split the run.
for (auto& c : _glyphScaleCorrections)
{
// Split after the adjustment first so it
// takes a copy of all the run properties before we modify them.
// GH 4665: This is the other half of the potential future perf item.
// If glyphs needing the same scale are coalesced, we could
// break fewer times and have fewer runs.
// Example
// Text:
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
// LEN = 26
// Runs:
// ^0----^1---------^2-------
// Scale Factors:
// 1.0 1.0 1.0
// (arrows are run begin)
// 0: IDX = 0, LEN = 6
// 1: IDX = 6, LEN = 11
// 2: IDX = 17, LEN = 9
// From the scale correction... we get
// IDX = where the scale starts
// LEN = how long the scale adjustment runs
// SCALE = the scale factor.
// We need to split the run so the SCALE factor
// only applies from IDX to LEN.
// This is the index after the segment we're splitting.
const auto afterIndex = c.textIndex + c.textLength;
// If the after index is still within the text, split the back
// half off first so we don't apply the scale factor to anything
// after this glyph/run segment.
// Example relative to above sample state:
// Correction says: IDX = 12, LEN = 2, FACTOR = 0.8
// We must split off first at 14 to leave the existing factor from 14-16.
// (because the act of splitting copies all properties, we don't want to
// adjust the scale factor BEFORE splitting off the existing one.)
// Text:
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
// LEN = 26
// Runs:
// ^0----^1----xx^2-^3-------
// (xx is where we're going to put the correction when all is said and done.
// We're adjusting the scale of the "MN" text only.)
// Scale Factors:
// 1 1 1 1
// (arrows are run begin)
// 0: IDX = 0, LEN = 6
// 1: IDX = 6, LEN = 8
// 2: IDX = 14, LEN = 3
// 3: IDX = 17, LEN = 9
if (afterIndex < _text.size())
{
_SetCurrentRun(afterIndex);
_SplitCurrentRun(afterIndex);
}
// If it's after the text, don't bother. The correction will just apply
// from the begin point to the end of the text.
// Example relative to above sample state:
// Correction says: IDX = 24, LEN = 2
// Text:
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
// LEN = 26
// Runs:
// ^0----^1---------^2-----xx
// xx is where we're going to put the correction when all is said and done.
// We don't need to split off the back portion because there's nothing after the xx.
// Now split just this glyph off.
// Example versus the one above where we did already split the back half off..
// Correction says: IDX = 12, LEN = 2, FACTOR = 0.8
// Text:
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
// LEN = 26
// Runs:
// ^0----^1----^2^3-^4-------
// (The MN text has been broken into its own run, 2.)
// Scale Factors:
// 1 1 1 1 1
// (arrows are run begin)
// 0: IDX = 0, LEN = 6
// 1: IDX = 6, LEN = 6
// 2: IDX = 12, LEN = 2
// 2: IDX = 14, LEN = 3
// 3: IDX = 17, LEN = 9
_SetCurrentRun(c.textIndex);
_SplitCurrentRun(c.textIndex);
// Get the run with the one glyph and adjust the scale.
auto& run = _GetCurrentRun();
run.fontScale = c.scale;
// Correction says: IDX = 12, LEN = 2, FACTOR = 0.8
// Text:
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
// LEN = 26
// Runs:
// ^0----^1----^2^3-^4-------
// (We've now only corrected run 2, selecting only the MN to 0.8)
// Scale Factors:
// 1 1 .8 1 1
}
// Dump the glyph scale corrections now that we're done with them.
_glyphScaleCorrections.clear();
// Order the runs.
_OrderRuns();
}
CATCH_RETURN();
return S_OK;
}
// Routine Description:
// - Adjusts the advances for each glyph in the run so it fits within a fixed-column count of cells.
// Arguments:
// - runIndex - The ID number of the internal runs array to use while shaping.
// Return Value:
// - S_OK or suitable DirectWrite or STL error code
[[nodiscard]] HRESULT CustomTextLayout::_CorrectGlyphRun(const UINT32 runIndex) noexcept
try
{
const Run& run = _runs.at(runIndex);
if (run.textLength == 0)
{
return S_FALSE; // Nothing to do..
}
// We're going to walk through and check for advances that don't match the space that we expect to give out.
// Glyph Indices represents the number inside the selected font where the glyph image/paths are found.
// Text represents the original text we gave in.
// Glyph Clusters represents the map between Text and Glyph Indices.
// - There is one Glyph Clusters map column per character of text.
// - The value of the cluster at any given position is relative to the 0 index of this run.
// (a.k.a. it resets to 0 for every run)
// - If multiple Glyph Cluster map values point to the same index, then multiple text chars were used
// to create the same glyph cluster.
// - The delta between the value from one Glyph Cluster's value and the next one is how many
// Glyph Indices are consumed to make that cluster.
// We're going to walk the map to find what spans of text and glyph indices make one cluster.
const auto clusterMapBegin = _glyphClusters.cbegin() + run.textStart;
const auto clusterMapEnd = clusterMapBegin + run.textLength;
// Walk through every glyph in the run, collect them into clusters, then adjust them to fit in
// however many columns are expected for display by the text buffer.
#pragma warning(suppress : 26496) // clusterBegin is updated at the bottom of the loop but analysis isn't seeing it.
for (auto clusterBegin = clusterMapBegin; clusterBegin < clusterMapEnd; /* we will increment this inside the loop*/)
{
// One or more glyphs might belong to a single cluster.
// Consider the following examples:
// 1.
// U+00C1 is Á.
// That is text of length one.
// A given font might have a complete single glyph for this
// which will be mapped into the _glyphIndices array.
// _text[0] = Á
// _glyphIndices[0] = 153
// _glyphClusters[0] = 0
// _glyphClusters[1] = 1
// The delta between the value of Clusters 0 and 1 is 1.
// The number of times "0" is specified is once.
// This means that we've represented one text with one glyph.
// 2.
// U+0041 is A and U+0301 is a combining acute accent ´.
// That is a text length of two.
// A given font might have two glyphs for this
// which will be mapped into the _glyphIndices array.
// _text[0] = A
// _text[1] = ´ (U+0301, combining acute)
// _glyphIndices[0] = 153
// _glyphIndices[1] = 421
// _glyphClusters[0] = 0
// _glyphClusters[1] = 0
// _glyphClusters[2] = 2
// The delta between the value of Clusters 0/1 and 2 is 2.
// The number of times "0" is specified is twice.
// This means that we've represented two text with two glyphs.
// There are two more scenarios that can occur that get us into
// NxM territory (N text by M glyphs)
// 3.
// U+00C1 is Á.
// That is text of length one.
// A given font might represent this as two glyphs
// which will be mapped into the _glyphIndices array.
// _text[0] = Á
// _glyphIndices[0] = 153
// _glyphIndices[1] = 421
// _glyphClusters[0] = 0
// _glyphClusters[1] = 2
// The delta between the value of Clusters 0 and 1 is 2.
// The number of times "0" is specified is once.
// This means that we've represented one text with two glyphs.
// 4.
// U+0041 is A and U+0301 is a combining acute accent ´.
// That is a text length of two.
// A given font might represent this as one glyph
// which will be mapped into the _glyphIndices array.
// _text[0] = A
// _text[1] = ´ (U+0301, combining acute)
// _glyphIndices[0] = 984
// _glyphClusters[0] = 0
// _glyphClusters[1] = 0
// _glyphClusters[2] = 1
// The delta between the value of Clusters 0/1 and 2 is 1.
// The number of times "0" is specified is twice.
// This means that we've represented two text with one glyph.
// Finally, there's one more dimension.
// Due to supporting a specific coordinate system, the text buffer
// has told us how many columns it expects the text it gave us to take
// when displayed.
// That is stored in _textClusterColumns with one value for each
// character in the _text array.
// It isn't aware of how glyphs actually get mapped.
// So it's giving us a column count in terms of text characters
// but expecting it to be applied to all the glyphs in the cluster
// required to represent that text.
// We'll collect that up and use it at the end to adjust our drawing.
// Our goal below is to walk through and figure out...
// A. How many glyphs belong to this cluster?
// B. Which text characters belong with those glyphs?
// C. How many columns, in total, were we told we could use
// to draw the glyphs?
// This is the value under the beginning position in the map.
const auto clusterValue = *clusterBegin;
// Find the cluster end point inside the map.
// We want to walk forward in the map until it changes (or we reach the end).
const auto clusterEnd = std::find_if(clusterBegin, clusterMapEnd, [clusterValue](auto compareVal) -> bool { return clusterValue != compareVal; });
// The beginning of the text span is just how far the beginning of the cluster is into the map.
const auto clusterTextBegin = std::distance(_glyphClusters.cbegin(), clusterBegin);
// The distance from beginning to end is the cluster text length.
const auto clusterTextLength = std::distance(clusterBegin, clusterEnd);
// The beginning of the glyph span is just the original cluster value.
const auto clusterGlyphBegin = clusterValue + run.glyphStart;
// The difference between the value inside the end iterator and the original value is the glyph length.
// If the end iterator was off the end of the map, then it's the total run glyph count minus wherever we started.
const auto clusterGlyphLength = (clusterEnd != clusterMapEnd ? *clusterEnd : run.glyphCount) - clusterValue;
// Now we can specify the spans within the text-index and glyph-index based vectors
// that store our drawing metadata.
// All the text ones run [clusterTextBegin, clusterTextBegin + clusterTextLength)
// All the cluster ones run [clusterGlyphBegin, clusterGlyphBegin + clusterGlyphLength)
// Get how many columns we expected the glyph to have.
const auto columns = base::saturated_cast<UINT16>(std::accumulate(_textClusterColumns.cbegin() + clusterTextBegin,
_textClusterColumns.cbegin() + clusterTextBegin + clusterTextLength,
0u));
// Multiply into pixels to get the "advance" we expect this text/glyphs to take when drawn.
const auto advanceExpected = static_cast<float>(columns * _width);
// Sum up the advances across the entire cluster to find what the actual value is that we've been told.
const auto advanceActual = std::accumulate(_glyphAdvances.cbegin() + clusterGlyphBegin,
_glyphAdvances.cbegin() + clusterGlyphBegin + clusterGlyphLength,
0.0f);
// With certain font faces at certain sizes, the advances seem to be slightly more than
// the pixel grid; Cascadia Code at 13pt (though, 200% scale) had an advance of 10.000001.
// We don't want anything sub one hundredth of a cell to make us break up runs, because
// doing so results in suboptimal rendering.
// If what we expect is bigger than what we have... pad it out.
if ((advanceExpected - advanceActual) > 0.001f)
{
// Get the amount of space we have leftover.
const auto diff = advanceExpected - advanceActual;
// Move the X offset (pixels to the right from the left edge) by half the excess space
// so half of it will be left of the glyph and the other half on the right.
// Here we need to move every glyph in the cluster.
std::for_each(_glyphOffsets.begin() + clusterGlyphBegin,
_glyphOffsets.begin() + clusterGlyphBegin + clusterGlyphLength,
[halfDiff = diff / 2](DWRITE_GLYPH_OFFSET& offset) -> void { offset.advanceOffset += halfDiff; });
// Set the advance of the final glyph in the set to all excess space not consumed by the first few so
// we get the perfect width we want.
_glyphAdvances.at(static_cast<size_t>(clusterGlyphBegin) + clusterGlyphLength - 1) += diff;
}
// If what we expect is smaller than what we have... rescale the font size to get a smaller glyph to fit.
else if ((advanceExpected - advanceActual) < -0.001f)
{
const auto scaleProposed = advanceExpected / advanceActual;
// Store the glyph scale correction for future run breaking
// GH 4665: In theory, we could also store the length of the new run and coalesce
// in case two adjacent glyphs need the same scale factor.
_glyphScaleCorrections.push_back(ScaleCorrection{
gsl::narrow<UINT32>(clusterTextBegin),
gsl::narrow<UINT32>(clusterTextLength),
scaleProposed });
// Adjust all relevant advances by the scale factor.
std::for_each(_glyphAdvances.begin() + clusterGlyphBegin,
_glyphAdvances.begin() + clusterGlyphBegin + clusterGlyphLength,
[scaleProposed](float& advance) -> void { advance *= scaleProposed; });
}
clusterBegin = clusterEnd;
}
// Certain fonts, like Batang, contain glyphs for hidden control
// and formatting characters. So we'll want to explicitly force their
// advance to zero.
// I'm leaving this here for future reference, but I don't think we want invisible glyphs for this renderer.
//if (run.script.shapes & DWRITE_SCRIPT_SHAPES_NO_VISUAL)
//{
// std::fill(_glyphAdvances.begin() + glyphStart,
// _glyphAdvances.begin() + glyphStart + actualGlyphCount,
// 0.0f
// );
//}
return S_OK;
}
CATCH_RETURN();
// Routine Description:
// - Takes the analyzed and shaped textual information from the layout process and
// forwards it into the given renderer in a run-by-run fashion.
// Arguments:
// - clientDrawingContext - Optional pointer to information that the renderer might need
// while attempting to graphically place the text onto the screen
// - renderer - The interface to be used for actually putting text onto the screen
// - origin - pixel point of top left corner on final surface for drawing
// Return Value:
// - S_OK or suitable DirectX/DirectWrite/Direct2D result code.
[[nodiscard]] HRESULT CustomTextLayout::_DrawGlyphRuns(_In_opt_ void* clientDrawingContext,
IDWriteTextRenderer* renderer,
const D2D_POINT_2F origin) noexcept
{
RETURN_HR_IF_NULL(E_INVALIDARG, renderer);
try
{
// We're going to start from the origin given and walk to the right for each
// sub-run that was calculated by the layout analysis.
auto mutableOrigin = origin;
// Draw each run separately.
for (auto runIndex = 0; runIndex < gsl::narrow<INT32>(_runs.size()); ++runIndex)
{
// Get the run
const Run& run = _runs.at(runIndex);
if (!WI_IsFlagSet(run.bidiLevel, 1))
{
RETURN_IF_FAILED(_DrawGlyphRun(clientDrawingContext, renderer, mutableOrigin, run));
}
// This is the RTL behavior. We will advance to the last contiguous RTL run, draw that,
// and then keep on going backwards from there, and then move runIndex beyond.
// Let's say we have runs abcdEFGh, where runs EFG are RTL.
// Then we will draw them in the order abcdGFEh
else
{
const auto originalRunIndex = runIndex;
auto lastIndexRTL = runIndex;
// Step 1: Get to the last contiguous RTL run from here
while (lastIndexRTL < gsl::narrow<INT32>(_runs.size()) - 1) // only could ever advance if there's something left
{
const Run& nextRun = _runs.at(gsl::narrow_cast<size_t>(lastIndexRTL + 1));
if (WI_IsFlagSet(nextRun.bidiLevel, 1))
{
lastIndexRTL++;
}
else
{
break;
}
}
// Go from the last to the first and draw
for (runIndex = lastIndexRTL; runIndex >= originalRunIndex; runIndex--)
{
const Run& currentRun = _runs.at(runIndex);
RETURN_IF_FAILED(_DrawGlyphRun(clientDrawingContext, renderer, mutableOrigin, currentRun));
}
runIndex = lastIndexRTL; // and the for loop will take the increment to the last one
}
}
}
CATCH_RETURN();
return S_OK;
}
// Routine Description:
// - Draw the given run
// - The origin is updated to be after the run.
// Arguments:
// - clientDrawingContext - Optional pointer to information that the renderer might need
// while attempting to graphically place the text onto the screen
// - renderer - The interface to be used for actually putting text onto the screen
// - origin - pixel point of top left corner on final surface for drawing
// - run - the run to be drawn
[[nodiscard]] HRESULT CustomTextLayout::_DrawGlyphRun(_In_opt_ void* clientDrawingContext,
gsl::not_null<IDWriteTextRenderer*> renderer,
D2D_POINT_2F& mutableOrigin,
const Run& run) noexcept
{
try
{
// Prepare the glyph run and description objects by converting our
// internal storage representation into something that matches DWrite's structures.
DWRITE_GLYPH_RUN glyphRun;
glyphRun.bidiLevel = run.bidiLevel;
glyphRun.fontEmSize = _formatInUse->GetFontSize() * run.fontScale;
glyphRun.fontFace = run.fontFace.Get();
glyphRun.glyphAdvances = &_glyphAdvances.at(run.glyphStart);
glyphRun.glyphCount = run.glyphCount;
glyphRun.glyphIndices = &_glyphIndices.at(run.glyphStart);
glyphRun.glyphOffsets = &_glyphOffsets.at(run.glyphStart);
glyphRun.isSideways = false;
DWRITE_GLYPH_RUN_DESCRIPTION glyphRunDescription;
glyphRunDescription.clusterMap = _glyphClusters.data();
glyphRunDescription.localeName = _localeName.data();
glyphRunDescription.string = _text.data();
glyphRunDescription.stringLength = run.textLength;
glyphRunDescription.textPosition = run.textStart;
// Calculate the origin for the next run based on the amount of space
// that would be consumed. We are doing this calculation now, not after,
// because if the text is RTL then we need to advance immediately, before the
// write call since DirectX expects the origin to the RIGHT of the text for RTL.
const auto postOriginX = std::accumulate(_glyphAdvances.begin() + run.glyphStart,
_glyphAdvances.begin() + run.glyphStart + run.glyphCount,
mutableOrigin.x);
// Check for RTL, if it is, apply space adjustment.
if (WI_IsFlagSet(glyphRun.bidiLevel, 1))
{
mutableOrigin.x = postOriginX;
}
// Try to draw it
RETURN_IF_FAILED(renderer->DrawGlyphRun(clientDrawingContext,
mutableOrigin.x,
mutableOrigin.y,
DWRITE_MEASURING_MODE_NATURAL,
&glyphRun,
&glyphRunDescription,
run.drawingEffect.Get()));
// Either way, we should be at this point by the end of writing this sequence,
// whether it was LTR or RTL.
mutableOrigin.x = postOriginX;
}
CATCH_RETURN();
return S_OK;
}
// Routine Description:
// - Estimates the maximum number of glyph indices needed to hold a string of
// a given length. This is the formula given in the Uniscribe SDK and should
// cover most cases. Degenerate cases will require a reallocation.
// Arguments:
// - textLength - the number of wchar_ts in the original string
// Return Value:
// - An estimate of how many glyph spaces may be required in the shaping arrays
// to hold the data from a string of the given length.
[[nodiscard]] constexpr UINT32 CustomTextLayout::_EstimateGlyphCount(const UINT32 textLength) noexcept
{
// This formula is from https://docs.microsoft.com/en-us/windows/desktop/api/dwrite/nf-dwrite-idwritetextanalyzer-getglyphs