-
Notifications
You must be signed in to change notification settings - Fork 73
/
TextBlock.cs
2050 lines (1782 loc) · 71.2 KB
/
TextBlock.cs
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
// RichTextKit
// Copyright © 2019-2020 Topten Software. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this product except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Topten.RichTextKit.Utils;
namespace Topten.RichTextKit
{
/// <summary>
/// Represents a block of formatted, laid out and measurable text
/// </summary>
public class TextBlock : StyledText
{
/// <summary>
/// Constructor
/// </summary>
public TextBlock()
{
}
/// <summary>
/// The max width property sets the maximum width of a line, after which
/// the line will be wrapped onto the next line.
/// </summary>
/// <remarks>
/// This property can be set to null, in which case lines won't be wrapped.
/// </remarks>
public float? MaxWidth
{
get => _maxWidth;
set
{
if (value.HasValue && value.Value < 0)
value = 0;
if (_maxWidth != value)
{
_maxWidth = value;
InvalidateLayout();
}
}
}
/// <summary>
/// The maximum height of the TextBlock after which lines will be
/// truncated and the final line will be appended with an
/// ellipsis (`...`) character.
/// </summary>
/// <remarks>
/// This property can be set to null, in which case the vertical height of the text block
/// won't be capped.
/// </remarks>
public float? MaxHeight
{
get => _maxHeight;
set
{
if (value.HasValue && value.Value < 0)
value = 0;
if (value != _maxHeight)
{
_maxHeight = value;
InvalidateLayout();
}
}
}
/// <summary>
/// The maximum number of lines after which lines will be
/// truncated and the final line will be appended with an
/// ellipsis (`...`) character.
/// </summary>
/// <remarks>
/// This property can be set to null, in which case the vertical height of
/// the text block won't be capped.
/// </remarks>
public int? MaxLines
{
get => _maxLines;
set
{
if (value.HasValue && value.Value < 0)
value = 0;
if (value != _maxLines)
{
_maxLines = value;
InvalidateLayout();
}
}
}
/// <summary>
/// Sets the left, right or center alignment of the text block.
/// </summary>
/// <remarks>
/// Set this property to <see cref="TextAlignment.Auto"/> to align
/// the paragraph according to the <see cref="BaseDirection"/>.
///
/// * If the <see cref="MaxWidth"/> property has been set this will
/// be used for alignment calculations.
/// * If the <see cref="MaxWidth"/> property has not been set, the
/// width of the longest line will be used.
/// </remarks>
public TextAlignment Alignment
{
get => _textAlignment;
set
{
if (_textAlignment != value)
{
_textAlignment = value;
InvalidateLayout();
}
}
}
/// <summary>
/// The base directionality of this text block (whether text is laid out
/// left to right, or right to left)
/// </summary>
public TextDirection BaseDirection
{
get => _baseDirection;
set
{
if (_baseDirection != value)
{
_baseDirection = value;
InvalidateLayout();
}
}
}
/// <summary>
/// Clear the content of this text block
/// </summary>
public override void Clear()
{
// Reset everything
FontRun.Pool.Value.ReturnAndClear(_fontRuns);
TextLine.Pool.Value.ReturnAndClear(_lines);
_textShapingBuffers.Clear();
base.Clear();
}
/// <summary>
/// Split this text block at the specified code point index
/// </summary>
/// <param name="from">The code point index to copy from</param>
/// <param name="length">The number of code points to copy</param>
/// <returns>A new text block with the RHS split part of the text</returns>
public TextBlock Copy(int from, int length)
{
// Create a new text block with the same attributes as this one
var other = new TextBlock();
other.Alignment = this.Alignment;
other.BaseDirection = this.BaseDirection;
other.MaxWidth = this.MaxWidth;
other.MaxHeight = this.MaxHeight;
other.MaxLines = this.MaxLines;
// Copy text to the new paragraph
foreach (var subRun in _styleRuns.GetInterectingRuns(from, length))
{
var sr = _styleRuns[subRun.Index];
other.AddText(sr.CodePoints.SubSlice(subRun.Offset, subRun.Length), sr.Style);
}
return other;
}
/// <inheritdoc />
protected override void OnChanged()
{
InvalidateLayout();
base.OnChanged();
}
/// <summary>
/// Appends an ellipsis to this text block
/// </summary>
/// <remarks>
/// This method checks if the text block has already been truncated and if
/// not appends an ellipsis without changing the measured vertical layout of the
/// text block. The ellipsis only remains in effect until the block's layout
/// is recalculated.
///
/// The text block must have at least one line. If the block contains no text,
/// then use AddText("\n", style) to create a single line with an attached style
/// but no text.
///
/// The intended purpose of this is to included an ellipsis on this text block
/// when a following text block doesn't fit.
/// </remarks>
public void AddEllipsis()
{
// Make sure laid out
Layout();
// Already truncated?
if (_truncated)
return;
if (_lines.Count == 0)
throw new InvalidOperationException("Ellipsis can't be appended to a text block with no lines");
// Append the ellipsis
var line = _lines[_lines.Count - 1];
// Because adorning the line with ellipsis resets the XCoord of each font run
// to be left aligned, we need to move the glyphs to be left aligned too
for (int frIndex = 0; frIndex < line.Runs.Count; frIndex++)
{
var fr = line.Runs[frIndex];
fr.MoveGlyphs(-fr.XCoord, 0);
}
// Append the ellipsis to the line and relayout theline
AdornLineWithEllipsis(line, true);
// Work out the new x-alignment
var ta = ResolveTextAlignment();
float xAdjust = 0;
switch (ta)
{
case TextAlignment.Right:
xAdjust = (_maxWidth ?? _measuredWidth) - line.Width;
break;
case TextAlignment.Center:
xAdjust = ((_maxWidth ?? _measuredWidth) - line.Width) / 2;
break;
}
// Adjust the measured width if the adorned line is the widest line
if (line.Width > _measuredWidth)
_measuredWidth = line.Width;
// Reposition each run to the correct location
for (int frIndex = 0; frIndex < line.Runs.Count; frIndex++)
{
var fr = line.Runs[frIndex];
fr.Line = line;
fr.XCoord += xAdjust;
if (fr.RunKind == FontRunKind.Ellipsis)
{
// The ellipsis has it's xcoord setup, but it's glyphs have never
// been positioned so need to handle this a little differently
fr.MoveGlyphs(fr.XCoord, line.YCoord + line.BaseLine);
}
else
{
// Move the glyphs back to corectly aligned position
fr.MoveGlyphs(fr.XCoord, 0);
}
}
}
/// <summary>
/// Updates the internal layout of the text block
/// </summary>
/// <remarks>
/// Generally you don't need to call this method as the layout
/// will be automatically updated as needed.
/// </remarks>
public void Layout()
{
// Needed?
if (!_needsLayout)
return;
_needsLayout = false;
// Resolve max width/height
_maxWidthResolved = _maxWidth ?? float.MaxValue;
_maxHeightResolved = _maxHeight ?? float.MaxValue;
_maxLinesResolved = _maxLines ?? int.MaxValue;
// Reset layout state
_textShapingBuffers.Clear();
_fontRuns.Clear();
_lines.Clear();
_caretIndicies.Clear();
_wordBoundaryIndicies.Clear();
_measuredHeight = 0;
_measuredWidth = 0;
_leftOverhang = null;
_rightOverhang = null;
_truncated = false;
// Only layout if actually have some text
if (_codePoints.Length != 0)
{
// Build font runs
BuildFontRuns();
// Break font runs into lines
BreakLines();
// Finalize lines
FinalizeLines();
}
}
/// <summary>
/// Get all font runs for this text block
/// </summary>
public IReadOnlyList<FontRun> FontRuns
{
get
{
Layout();
return _fontRuns;
}
}
/// <summary>
/// Get all the lines for this text block
/// </summary>
public IReadOnlyList<TextLine> Lines
{
get
{
Layout();
return _lines;
}
}
/// <summary>
/// Paint this text block
/// </summary>
/// <param name="canvas">The Skia canvas to paint to</param>
/// <param name="options">Options controlling the paint operation</param>
public void Paint(SKCanvas canvas, TextPaintOptions options = null)
{
// Ensure have options
if (options == null)
options = TextPaintOptions.Default;
// Ensure layout done
Layout();
// Create context
var ctx = new PaintTextContext()
{
Canvas = canvas,
Options = options,
};
// Prepare selection
if (options.Selection.HasValue)
{
ctx.SelectionStart = options.Selection.Value.Minimum;
ctx.SelectionEnd = options.Selection.Value.Maximum;
ctx.PaintSelectionBackground = new SKPaint()
{
Color = options.SelectionColor,
IsStroke = false,
IsAntialias = false,
};
if (options.SelectionHandleScale != 0 && options.SelectionHandleColor.Alpha > 0)
{
ctx.SelectionHandleScale = options.SelectionHandleScale;
ctx.PaintSelectionHandle = new SKPaint()
{
Color = options.SelectionHandleColor,
IsStroke = false,
IsAntialias = true,
};
}
}
else
{
ctx.SelectionStart = -1;
ctx.SelectionEnd = -1;
}
// Paint each line
foreach (var l in _lines)
{
l.Paint(ctx);
}
// Clean up
ctx.PaintSelectionBackground?.Dispose();
}
/// <summary>
/// Paint this text block
/// </summary>
/// <param name="canvas">The Skia canvas to paint to</param>
/// <param name="position">The top left position within the canvas to draw at</param>
/// <param name="options">Options controlling the paint operation</param>
public void Paint(SKCanvas canvas, SKPoint position, TextPaintOptions options = null)
{
// Translate
canvas.Save();
canvas.Translate(position.X, position.Y);
// Paint it
Paint(canvas, options);
// Restore and done!
canvas.Restore();
}
/// <summary>
/// The total height of all lines.
/// </summary>
public float MeasuredHeight
{
get
{
Layout();
return _measuredHeight;
}
}
/// <summary>
/// The length of the displayed text (in code points)
/// </summary>
/// <remarks>
/// If the text is truncated, this is the index of the point
/// at which the ellipsis was inserted. If the text it not
/// truncated, is the length of all added text.
/// </remarks>
public int MeasuredLength
{
get
{
Layout();
if (_lines.Count == 0)
return 0;
return _lines[_lines.Count - 1].End;
}
}
/// <summary>
/// The number of lines in the text
/// </summary>
public int LineCount
{
get
{
Layout();
return _lines.Count;
}
}
/// <summary>
/// The width of the widest line of text.
/// </summary>
/// <remarks>
/// The returned width does not include any overhang.
/// </remarks>
public float MeasuredWidth
{
get
{
Layout();
return _measuredWidth;
}
}
/// <summary>
/// Indicates if the text was truncated due to max height or max lines
/// constraints
/// </summary>
public bool Truncated
{
get
{
Layout();
return _truncated;
}
}
/// <summary>
/// Gets the size of any unused space around the text.
/// </summary>
/// <remarks>
/// If MaxWidth is not set, the left and right padding will always be zero.
///
/// This property also returns a bottom padding amount if MaxHeight is set.
///
/// The returned top padding is always zero.
///
/// The return rectangle describes padding amounts for each edge - not
/// rectangle co-ordinates.
/// </remarks>
public SKRect MeasuredPadding
{
get
{
var r = new SKRect();
// Bottom padding?
if (_maxHeight.HasValue)
{
r.Bottom = _maxHeight.Value - _measuredHeight;
}
if (!_maxWidth.HasValue)
return r;
Layout();
switch (ResolveTextAlignment())
{
case TextAlignment.Left:
r.Left = 0;
r.Right = _maxWidthResolved - _measuredWidth;
return r;
case TextAlignment.Right:
r.Left = _maxWidthResolved - _measuredWidth;
r.Right = 0;
return r;
case TextAlignment.Center:
r.Left = (_maxWidthResolved - _measuredWidth) / 2;
r.Right = (_maxWidthResolved - _measuredWidth) / 2;
return r;
}
throw new InvalidOperationException();
}
}
/// <summary>
/// Gets the actual measured overhang in each direction based on the
/// fonts used, and the supplied text.
/// </summary>
/// <remarks>
/// The return rectangle describes overhang amounts for each edge - not
/// rectangle co-ordinates.
/// </remarks>
public SKRect MeasuredOverhang
{
get
{
Layout();
if (!_leftOverhang.HasValue)
{
var right = _maxWidth ?? MeasuredWidth;
float leftOverhang = 0;
float rightOverhang = 0;
foreach (var l in _lines)
{
l.UpdateOverhang(right, ref leftOverhang, ref rightOverhang);
}
_leftOverhang = leftOverhang;
_rightOverhang = rightOverhang;
}
return new SKRect(_leftOverhang.Value, 0, _rightOverhang.Value, 0);
}
}
/// <summary>
/// Hit test this block of text
/// </summary>
/// <param name="lineIndex">The line to be hit test</param>
/// <param name="x">The x-coordinate relative to top left of the block</param>
/// <returns>A HitTestResult</returns>
public HitTestResult HitTestLine(int lineIndex, float x)
{
return _lines[lineIndex].HitTest(x);
}
/// <summary>
/// Hit test this block of text
/// </summary>
/// <param name="x">The x-coordinate relative to top left of the block</param>
/// <param name="y">The x-coordinate relative to top left of the block</param>
/// <returns>A HitTestResult</returns>
public HitTestResult HitTest(float x, float y)
{
Layout();
var htr = new HitTestResult();
// Work out which line number we're over
htr.OverLine = -1;
htr.OverCodePointIndex = -1;
for (int i = 0; i < _lines.Count; i++)
{
var l = _lines[i];
if (y >= l.YCoord && y < l.YCoord + l.Height)
{
htr.OverLine = i;
}
}
// Work out the closest line
if (htr.OverLine >= 0)
{
htr.ClosestLine = htr.OverLine;
}
else if (y < 0)
{
htr.ClosestLine = 0;
}
else
{
htr.ClosestLine = _lines.Count - 1;
}
// Hit test each cluster
if (htr.ClosestLine >= 0 && htr.ClosestLine < _lines.Count)
{
// Hit test the line
var l = _lines[htr.ClosestLine];
l.HitTest(x, ref htr);
}
// If we're not over the line, we're also not over the character
if (htr.OverLine < 0)
htr.OverCodePointIndex = -1;
if (htr.ClosestCodePointIndex < 0)
{
if (htr.ClosestLine == _lines.Count - 1 && _lines.Count > 0)
htr.ClosestCodePointIndex = _lines[_lines.Count - 1].End - 1;
else
htr.ClosestCodePointIndex = 0;
}
return htr;
}
/// <summary>
/// Build map of all caret positions
/// </summary>
void BuildCaretIndicies()
{
Layout();
if (_caretIndicies.Count == 0)
{
foreach (var r in _lines.SelectMany(x => x.Runs))
{
for (int i = 0; i < r.Clusters.Length; i++)
{
_caretIndicies.Add(r.Clusters[i]);
}
}
_caretIndicies.Add(MeasuredLength);
_caretIndicies = _caretIndicies.OrderBy(x => x).Distinct().ToList();
}
}
/// <summary>
/// Retrieves a list of all valid caret positions
/// </summary>
public IReadOnlyList<int> CaretIndicies
{
get
{
BuildCaretIndicies();
return _caretIndicies;
}
}
/// <summary>
/// Retrieves a list of all valid caret positions
/// </summary>
public IReadOnlyList<int> WordBoundaryIndicies
{
get
{
// Find word boundaries (if not already done)
if (_wordBoundaryIndicies.Count == 0)
{
_wordBoundaryIndicies = WordBoundaryAlgorithm.FindWordBoundaries(_codePoints.AsSlice()).ToList();
}
return _wordBoundaryIndicies;
}
}
/// <summary>
/// Retrieves a list of the indicies of the first code point in each line
/// </summary>
public IReadOnlyList<int> LineIndicies
{
get
{
return _lines.Select(x => x.Start).ToList();
}
}
/// <summary>
/// Given a code point index, find the index in the CaretIndicies
/// </summary>
/// <param name="codePointIndex">The code point index to lookup</param>
/// <returns>The index in the code point idnex in the CaretIndicies array</returns>
public int LookupCaretIndex(int codePointIndex)
{
BuildCaretIndicies();
int index = _caretIndicies.BinarySearch(codePointIndex);
if (index < 0)
index = ~index;
return index;
}
/// <summary>
/// Calculates useful information for displaying a caret
/// </summary>
/// <remarks>
/// When altPosition is true, if the code point index indicates the first
/// code point after a line break, the returned caret position will be the
/// end of the previous line (instead of the start of the next line)
/// </remarks>
/// <param name="position">The caret position</param>
/// <returns>A CaretInfo struct</returns>
public CaretInfo GetCaretInfo(CaretPosition position)
{
// Empty text block?
if (_codePoints.Length == 0 || position.CodePointIndex < 0 || _lines.Count == 0)
{
return CaretInfo.None;
}
// Past the measured length?
if (position.CodePointIndex > MeasuredLength)
{
return CaretInfo.None;
}
// Look up the caret index
int cpii = LookupCaretIndex(position.CodePointIndex);
// Create caret info
var ci = new CaretInfo();
ci.CodePointIndex = _caretIndicies[cpii];
var frIndex = FindFontRunForCodePointIndex(position.CodePointIndex);
FontRun fr = null;
if (frIndex >= 0)
{
fr = _fontRuns[frIndex];
if (fr.Start == position.CodePointIndex && frIndex > 0)
{
var frPrior = _fontRuns[frIndex - 1];
if (frPrior.End == position.CodePointIndex)
{
if (position.AltPosition ||
(frPrior.Direction == TextDirection.RTL &&
frPrior.RunKind != FontRunKind.TrailingWhitespace))
{
fr = frPrior;
}
}
}
}
else
{
var lastLine = _lines[_lines.Count - 1];
if (lastLine.RunsInternal.Count > 0)
fr = lastLine.RunsInternal[lastLine.RunsInternal.Count - 1];
}
if (fr == null)
return CaretInfo.None;
// Setup caret coordinates
ci.CaretXCoord = ci.CodePointIndex < 0 ? 0 : fr.GetXCoordOfCodePointIndex(ci.CodePointIndex);
ci.CaretRectangle = CalculateCaretRectangle(ci, fr);
ci.LineIndex = _lines.IndexOf(fr.Line);
return ci;
}
SKRect CalculateCaretRectangle(CaretInfo ci, FontRun fr)
{
if (ci.CodePointIndex < 0)
return SKRect.Empty;
// Get the font run to be used for caret metrics
fr = GetFontRunForCaretMetrics(ci, fr);
// Setup the basic rectangle
var rect = new SKRect();
rect.Left = ci.CaretXCoord;
rect.Top = fr.Line.YCoord + fr.Line.BaseLine + fr.Ascent;
rect.Right = rect.Left;
rect.Bottom = fr.Line.YCoord + fr.Line.BaseLine + fr.Descent;
// Apply slant if italic
if (fr.Style.FontItalic)
{
rect.Left -= rect.Height / 14;
rect.Right = rect.Left + rect.Height / 5;
}
return rect;
}
/// <summary>
/// Internal helper to get the font run that should
/// be used for caret metrics.
/// </summary>
/// <remarks>
/// The returned font run is the font run of the previous
/// character, or the same character if the first font run
/// on the line.
/// </remarks>
/// <returns>The determined font run</returns>
FontRun GetFontRunForCaretMetrics(CaretInfo ci, FontRun fr)
{
// Same font run?
if (ci.CodePointIndex > fr.Start)
return fr;
// Try to get the previous font run in this line
var lineRuns = fr.Line.Runs as List<FontRun>;
int index = lineRuns.IndexOf(fr);
if (index <= 0)
return fr;
// Use the previous font run
return lineRuns[index - 1];
}
/// <summary>
/// Find the font run holding a code point index
/// </summary>
/// <param name="codePointIndex"></param>
/// <returns></returns>
public int FindFontRunForCodePointIndex(int codePointIndex)
{
// Past end of text?
if (codePointIndex > MeasuredLength)
return -1;
// Look up font run
int frIndex = FontRuns.BinarySearch(codePointIndex, (run, value) =>
{
return (run.End - 1) - codePointIndex;
});
if (frIndex < 0)
frIndex = ~frIndex;
if (frIndex == _fontRuns.Count)
frIndex = _fontRuns.Count - 1;
if (frIndex < 0)
return -1;
// Return the font run
var fr = _fontRuns[frIndex];
System.Diagnostics.Debug.Assert(codePointIndex >= fr.Start);
System.Diagnostics.Debug.Assert(codePointIndex <= fr.End);
return frIndex;
}
/// <summary>
/// Invalidate the layout
/// </summary>
void InvalidateLayout()
{
// Make sure style runs are valid (debug only)
_styleRuns.CheckValid(_codePoints.Length);
// Set layout flag
_needsLayout = true;
}
/// <summary>
/// Set if the current layout is dirty
/// </summary>
bool _needsLayout = true;
/// <summary>
/// Maximum width (wrap point, or null for no wrapping)
/// </summary>
float? _maxWidth;
/// <summary>
/// Width at which to wrap content
/// </summary>
float _maxWidthResolved = float.MaxValue;
/// <summary>
/// Maximum height (crop lines after this)
/// </summary>
float? _maxHeight;
/// <summary>
/// Maximum layout height
/// </summary>
float _maxHeightResolved = float.MaxValue;
/// <summary>
/// Maximum number of lines
/// </summary>
int? _maxLines;
/// <summary>
/// Maximum number of lines
/// </summary>
int _maxLinesResolved = int.MaxValue;
/// <summary>
/// Text alignment
/// </summary>
TextAlignment _textAlignment = TextAlignment.Auto;
/// <summary>
/// Base direction as set by user
/// </summary>
TextDirection _baseDirection = TextDirection.Auto;
/// <summary>
/// Base direction as resolved if auto
/// </summary>
TextDirection _resolvedBaseDirection;
/// <summary>
/// Re-usable buffers for text shaping results
/// </summary>
TextShaper.ResultBufferSet _textShapingBuffers = new TextShaper.ResultBufferSet();
/// <summary>
/// Reusable buffer for bidi data
/// </summary>
BidiData _bidiData = new BidiData();
/// <summary>
/// A list of font runs, after splitting by directionality, user styles and font fallback
/// </summary>
List<FontRun> _fontRuns = new List<FontRun>();
/// <summary>
/// Helper for splitting code into linebreaks
/// </summary>
LineBreaker _lineBreaker = new LineBreaker();
/// <summary>
/// The measured height
/// </summary>
float _measuredHeight;
/// <summary>
/// The measured width
/// </summary>
float _measuredWidth;
/// <summary>
/// The required left overhang
/// </summary>
float? _leftOverhang = null;
/// <summary>
/// The required left overhang
/// </summary>
float? _rightOverhang = null;
/// <summary>
/// Indicates if the text was truncated by max height/max lines limitations
/// </summary>
bool _truncated;
/// <summary>
/// The final laid out set of lines
/// </summary>
List<TextLine> _lines = new List<TextLine>();
/// <summary>
/// Calculated valid caret indicies
/// </summary>
List<int> _caretIndicies = new List<int>();
/// <summary>
/// Calculated word boundary caret indicies
/// </summary>