-
Notifications
You must be signed in to change notification settings - Fork 15
/
mongol_paragraph.dart
1349 lines (1180 loc) · 41.3 KB
/
mongol_paragraph.dart
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 2014 The Flutter Authors.
// Copyright 2021 Suragch.
// All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:collection';
import 'dart:math' as math;
import 'dart:ui' as ui;
import 'package:characters/characters.dart';
import 'package:flutter/painting.dart';
import 'mongol_text_align.dart';
/// [MongolLineMetrics] stores the measurements and statistics of a single line in the
/// paragraph.
///
/// The measurements here are for the line as a whole, and represent the maximum
/// extent of the line instead of per-run or per-glyph metrics. For more detailed
/// metrics, [MongolParagraph.getBoxesForRange].
///
/// [MongolLineMetrics] should be obtained directly from the [MongolParagraph.computeLineMetrics]
/// method.
class MongolLineMetrics {
/// Creates a [MongolLineMetrics] object with only the specified values.
MongolLineMetrics({
required this.hardBreak,
required this.ascent,
required this.descent,
required this.unscaledAscent,
required this.height,
required this.width,
required this.top,
required this.baseline,
required this.lineNumber,
});
/// True if this line ends with an explicit line break (e.g. '\n') or is the end
/// of the paragraph. False otherwise.
final bool hardBreak;
/// The rise from the [baseline] as calculated from the font and style for this line.
///
/// This is the final computed ascent and can be impacted by the strut, width(horizontal layout height),
/// scaling, as well as outlying runs that are very tall.
///
/// The [ascent] is provided as a positive value, even though it is typically defined
/// in fonts as negative. This is to ensure the signage of operations with these
/// metrics directly reflects the intended signage of the value. For example,
/// the x coordinate of the right edge of the line is `baseline + ascent`.
final double ascent;
/// The drop from the [baseline] as calculated from the font and style for this line.
///
/// This is the final computed ascent and can be impacted by the strut, width(horizontal layout height),
/// scaling, as well as outlying runs that are very tall.
///
/// The x coordinate of the left edge of the line is `baseline - descent`.
final double descent;
/// The rise from the [baseline] as calculated from the font and style for this line
/// ignoring the [TextStyle.height].
///
/// The [unscaledAscent] is provided as a positive value, even though it is typically
/// defined in fonts as negative. This is to ensure the signage of operations with
/// these metrics directly reflects the intended signage of the value.
final double unscaledAscent;
/// Height of the line from the top edge of the topmost glyph to the bottom
/// edge of the bottommost glyph.
///
/// This is not the same as the height of the paragraph.
///
/// See also:
///
/// * [MongolParagraph.height], the max height passed in during layout.
final double height;
/// Total width of the line from the left edge to the right edge.
///
/// This is equivalent to `round(ascent + descent)`. This value is provided
/// separately due to rounding causing sub-pixel differences from the unrounded
/// values.
final double width;
/// The y coordinate of top edge of the line.
///
/// The bottom edge can be obtained with `top + height`.
final double top;
/// The x coordinate of the baseline for this line from the left of the paragraph.
///
/// The right edge of the paragraph up to and including this line may be obtained
/// through `baseline + ascent`.
final double baseline;
/// The number of this line in the overall paragraph, with the first line being
/// index zero.
///
/// For example, the first line is line 0, second line is line 1.
final int lineNumber;
@override
bool operator ==(Object other) {
if (other.runtimeType != runtimeType) {
return false;
}
return other is MongolLineMetrics &&
other.hardBreak == hardBreak &&
other.ascent == ascent &&
other.descent == descent &&
other.unscaledAscent == unscaledAscent &&
other.height == height &&
other.width == width &&
other.top == top &&
other.baseline == baseline &&
other.lineNumber == lineNumber;
}
@override
int get hashCode => Object.hash(hardBreak, ascent, descent, unscaledAscent,
height, width, top, baseline, lineNumber);
@override
String toString() {
return 'LineMetrics(hardBreak: $hardBreak, '
'ascent: $ascent, '
'descent: $descent, '
'unscaledAscent: $unscaledAscent, '
'height: $height, '
'width: $width, '
'top: $top, '
'baseline: $baseline, '
'lineNumber: $lineNumber)';
}
}
/// A paragraph of vertical Mongolian layout text.
///
/// This class is a replacement for the Paragraph class. Since Paragraph hands
/// all it's work down to the Flutter engine, this class also does the work
/// of line-wrapping and laying out the text.
///
/// The text is divided into a list of [_runs] where each run is a short
/// substring (usually a word or CJK/emoji character). Sometimes a run includes
/// multiple styles in which case [_rawStyledTextRuns] are used temporarily
/// before they can be combined into single [_runs] just based on words. The
/// [_runs] are then measured and layed out in [_lines] based on the given
/// constraints.
class MongolParagraph {
/// This class is created by the library, and should not be instantiated
/// or extended directly.
///
/// To create a [MongolParagraph] object, use a [MongolParagraphBuilder].
MongolParagraph._(
this._runs,
this._text,
this._maxLines,
this._ellipsis,
this._textAlign,
);
final String _text;
final List<_TextRun> _runs;
final int? _maxLines;
final _TextRun? _ellipsis;
final MongolTextAlign _textAlign;
double? _width;
double? _height;
double? _longestLine;
double? _minIntrinsicHeight;
double? _maxIntrinsicHeight;
/// The amount of horizontal space this paragraph occupies.
///
/// Valid only after [layout] has been called.
double get width => _width ?? 0;
/// The amount of vertical space this paragraph occupies.
///
/// Valid only after [layout] has been called.
double get height => _height ?? 0;
/// The distance from the top edge of the topmost glyph to the bottom edge of
/// the bottommost glyph in the paragraph.
///
/// Valid only after [layout] has been called.
double get longestLine => _longestLine ?? 0;
/// The minimum height that this paragraph could be without failing to paint
/// its contents within itself.
///
/// Valid only after [layout] has been called.
double get minIntrinsicHeight => _minIntrinsicHeight ?? 0;
/// Returns the smallest height beyond which increasing the height never
/// decreases the width.
///
/// Valid only after [layout] has been called.
double get maxIntrinsicHeight => _maxIntrinsicHeight ?? double.infinity;
/// The distance to the alphabetic baseline the same as for horizontal text.
double get alphabeticBaseline {
if (_runs.isEmpty) {
return 0.0;
}
return _runs.first.paragraph.alphabeticBaseline;
}
/// The distance to the ideographic baseline the same as for horizontal text.
double get ideographicBaseline {
if (_runs.isEmpty) {
return 0.0;
}
return _runs.first.paragraph.ideographicBaseline;
}
/// True if there is more horizontal content, but the text was truncated, either
/// because we reached `maxLines` lines of text or because the `maxLines` was
/// null, `ellipsis` was not null, and one of the lines exceeded the height
/// constraint.
///
/// See the discussion of the `maxLines` and `ellipsis` arguments at
/// [ParagraphStyle].
bool get didExceedMaxLines {
return _didExceedMaxLines;
}
bool _didExceedMaxLines = false;
/// Computes the size and position of each glyph in the paragraph.
///
/// The [MongolParagraphConstraints] control how tall the text is allowed
/// to be.
void layout(MongolParagraphConstraints constraints) =>
_layout(constraints.height);
void _layout(double height) {
if (height == _height) return;
_calculateLineBreaks(height);
_calculateWidth();
_height = height;
_calculateIntrinsicHeight();
}
final List<_LineInfo> _lines = [];
// Internally this method uses "width" and "height" naming with regard
// to a horizontal line of text. Rotation doesn't happen until drawing.
void _calculateLineBreaks(double maxLineLength) {
if (_runs.isEmpty) {
return;
}
if (_lines.isNotEmpty) {
_lines.clear();
_didExceedMaxLines = false;
}
// add run lengths until exceeds length
var start = 0;
var end = 0;
var lineWidth = 0.0;
var lineHeight = 0.0;
var runEndsWithNewLine = false;
for (var i = 0; i < _runs.length; i++) {
end = i;
final run = _runs[i];
final runWidth = run.width;
final runHeight = run.height;
if (lineWidth + runWidth > maxLineLength) {
_addLine(start, end, lineWidth, lineHeight);
lineWidth = runWidth;
lineHeight = runHeight;
start = end;
} else {
lineWidth += runWidth;
lineHeight = math.max(lineHeight, run.height);
}
runEndsWithNewLine = _runEndsWithNewLine(run);
if (runEndsWithNewLine) {
end = i + 1;
_addLine(start, end, lineWidth, lineHeight);
lineWidth = 0;
lineHeight = 0;
start = end;
}
if (_didExceedMaxLines) {
break;
}
}
end = _runs.length;
if (start < end) {
_addLine(start, end, lineWidth, lineHeight);
}
// add empty line with invalid run indexes for final newline char
if (runEndsWithNewLine) {
final height = _lines.last.bounds.height;
_addLine(-1, -1, 0, height);
}
}
bool _runEndsWithNewLine(_TextRun run) {
final index = run.end - 1;
return _text[index] == '\n';
}
void _addLine(int start, int end, double width, double height) {
if (_maxLines != null && _maxLines! <= _lines.length) {
_didExceedMaxLines = true;
return;
}
_didExceedMaxLines = false;
final bounds = Rect.fromLTRB(0, 0, width, height);
final lineInfo = _LineInfo(start, end, bounds);
_lines.add(lineInfo);
_longestLine = math.max(longestLine, lineInfo.bounds.width);
}
void _calculateWidth() {
var sum = 0.0;
for (final line in _lines) {
sum += line.bounds.height;
}
_width = sum;
}
// Internally this translates a horizontal run width to the vertical name
// that it is known as externally.
void _calculateIntrinsicHeight() {
var sum = 0.0;
var maxRunWidth = 0.0;
var maxLineEndsWithNewLine = 0.0;
var minLineEndsWithoutNewLine = double.infinity;
for (var index = 0; index < _lines.length; index++) {
final line = _lines[index];
_TextRun? lastRun;
for (var i = line.textRunStart; i < line.textRunEnd; i++) {
lastRun = _runs[i];
final width = lastRun.width;
maxRunWidth = math.max(width, maxRunWidth);
sum += width;
}
final bool endsWithNewLine;
if (lastRun != null) {
endsWithNewLine = _runEndsWithNewLine(lastRun);
} else {
endsWithNewLine = false;
}
final hasNextLine = index < _lines.length - 1;
if (hasNextLine && !endsWithNewLine) {
final nextLine = _lines[index + 1];
sum += _runs[nextLine.textRunStart].width;
minLineEndsWithoutNewLine = math.min(minLineEndsWithoutNewLine, sum);
} else {
maxLineEndsWithNewLine = math.max(maxLineEndsWithNewLine, sum);
}
sum = 0;
}
if (minLineEndsWithoutNewLine == double.infinity) {
minLineEndsWithoutNewLine = 0;
}
_minIntrinsicHeight = maxRunWidth;
_maxIntrinsicHeight =
math.max(minLineEndsWithoutNewLine, maxLineEndsWithNewLine);
}
/// Returns the text position closest to the given offset.
TextPosition getPositionForOffset(Offset offset) {
final encoded = _getPositionForOffset(offset.dx, offset.dy);
return TextPosition(
offset: encoded[0], affinity: TextAffinity.values[encoded[1]]);
}
// Both the line info and the text run are in horizontal orientation,
// but the [dx] and [dy] offsets are in vertical orientation.
List<int> _getPositionForOffset(double dx, double dy) {
const upstream = 0;
const downstream = 1;
if (_lines.isEmpty) {
return [0, downstream];
}
// find the line
_LineInfo? matchedLine;
var rightEdgeAfterRotation = 0.0;
var rotatedRunDx = 0.0;
var rotatedRunDy = 0.0;
for (var line in _lines) {
rightEdgeAfterRotation += line.bounds.bottom;
// todo rotatedRunDx always = 0 ?
// maybe rotatedRunDx = rightEdgeAfterRotation; before the previous line code?
rotatedRunDx = line.bounds.top;
if (dx <= rightEdgeAfterRotation) {
matchedLine = line;
break;
}
}
matchedLine ??= _lines.last;
// find the run in the line
_TextRun? matchedRun;
var bottomEdgeAfterRotating = 0.0;
for (var i = matchedLine.textRunStart; i < matchedLine.textRunEnd; i++) {
final run = _runs[i];
rotatedRunDy = bottomEdgeAfterRotating;
bottomEdgeAfterRotating += run.width;
if (dy <= bottomEdgeAfterRotating) {
matchedRun = run;
break;
}
}
if (matchedRun == null) {
final matchedRunIndex = matchedLine.textRunEnd - 1;
if (matchedRunIndex.isNegative) {
matchedRun = _runs.last;
} else {
matchedRun = _runs[matchedRunIndex];
}
}
// find the offset
final paragraphDx = dy - rotatedRunDy;
final paragraphDy = dx - rotatedRunDx;
final offset = Offset(paragraphDx, paragraphDy);
final runPosition = matchedRun.paragraph.getPositionForOffset(offset);
final textOffset = matchedRun.start + runPosition.offset;
// find the affinity
final lineEndCharOffset = matchedRun.end;
final textAffinity =
(textOffset == lineEndCharOffset) ? upstream : downstream;
return [textOffset, textAffinity];
}
/// Draws the precomputed text on a [canvas] one line at a time in vertical
/// lines that wrap from left to right.
void draw(Canvas canvas, Offset offset) {
final shouldDrawEllipsis = _didExceedMaxLines && _ellipsis != null;
// translate for the offset
canvas.save();
canvas.translate(offset.dx, offset.dy);
// rotate the canvas 90 degrees
canvas.rotate(math.pi / 2);
// loop through every line
for (var i = 0; i < _lines.length; i++) {
final line = _lines[i];
// translate for the line height
final dy = -line.bounds.height;
canvas.translate(0, dy);
// draw line
final isLastLine = i == _lines.length - 1;
_drawEachRunInCurrentLine(canvas, line, shouldDrawEllipsis, isLastLine);
}
canvas.restore();
}
void _drawEachRunInCurrentLine(
Canvas canvas, _LineInfo line, bool shouldDrawEllipsis, bool isLastLine) {
canvas.save();
var runSpacing = 0.0;
switch (_textAlign) {
case MongolTextAlign.top:
break;
case MongolTextAlign.center:
final offset = (_height! - line.bounds.width) / 2;
canvas.translate(offset, 0);
break;
case MongolTextAlign.bottom:
final offset = _height! - line.bounds.width;
canvas.translate(offset, 0);
break;
case MongolTextAlign.justify:
if (isLastLine) break;
final extraSpace = _height! - line.bounds.width;
final runsInLine = line.textRunEnd - line.textRunStart;
if (runsInLine <= 1) break;
runSpacing = extraSpace / (runsInLine - 1);
break;
}
final startIndex = line.textRunStart;
final endIndex = line.textRunEnd - 1;
for (var j = startIndex; j <= endIndex; j++) {
if (shouldDrawEllipsis && isLastLine && j == endIndex) {
if (maxIntrinsicHeight + _ellipsis!.height < height) {
final run = _runs[j];
run.draw(canvas, const Offset(0, 0));
canvas.translate(run.width, 0);
}
_ellipsis!.draw(canvas, const Offset(0, 0));
} else {
final run = _runs[j];
run.draw(canvas, const Offset(0, 0));
canvas.translate(run.width, 0);
}
canvas.translate(runSpacing, 0);
}
canvas.restore();
}
/// Returns a list of rects that enclose the given text range.
///
/// Coordinates of the Rect are relative to the upper-left corner of the
/// paragraph, where positive y values indicate down. Orientation is as
/// vertical Mongolian text with left to right line wrapping.
///
/// Note that this method behaves slightly differently than
/// Paragraph.getBoxesForRange. The Paragraph version returns List<TextBox>,
/// but TextBox doesn't accurately describe vertical text so Rect is used.
List<Rect> getBoxesForRange(int start, int end) {
final boxes = <Rect>[];
// The [start] index must be within the text range
final textLength = _text.length;
if (start < 0 || start > _text.length) {
return boxes;
}
// Allow the [end] index to be larger than the text length but don't use it
final effectiveEnd = math.min(textLength, end);
// Horizontal offset for the left side of the vertical rect
var dx = 0.0;
// loop through each line
for (var i = 0; i < _lines.length; i++) {
final line = _lines[i];
final lastRunIndex = line.textRunEnd - 1;
// return empty line for invalid run indexes
// (This happens when text ends with newline char.)
if (lastRunIndex < 0) {
if (end > textLength) {
boxes.add(_lineBoundsAsBox(line, dx));
}
continue;
}
final lineLastCharIndex = _runs[lastRunIndex].end - 1;
// skip empty lines before the selected range
if (lineLastCharIndex < start) {
// The line is horizontal but dx is for vertical orientation
dx += line.bounds.height;
continue;
}
final firstRunIndex = line.textRunStart;
final lineFirstCharIndex = _runs[firstRunIndex].start;
// If this is a full line then skip looping over the runs
// because the line size has already been cached.
if (lineFirstCharIndex >= start && lineLastCharIndex < effectiveEnd) {
boxes.add(_lineBoundsAsBox(line, dx));
} else {
// check the runs one at a time
final lineBox = _getBoxFromLine(line, start, effectiveEnd, dx);
// partial selections of grapheme clusters should return no boxes
if (lineBox != Rect.zero) {
boxes.add(lineBox);
}
// If this is the last line there we're finished
if (lineLastCharIndex >= effectiveEnd - 1) {
return boxes;
}
}
dx += line.bounds.height;
}
return boxes;
}
Rect _lineBoundsAsBox(_LineInfo line, double dx) {
final lineBounds = line.bounds;
return Rect.fromLTWH(dx, 0, lineBounds.height, lineBounds.width);
}
// Takes a single line and finds the box that includes the selected range
Rect _getBoxFromLine(_LineInfo line, int start, int end, double dx) {
var boxWidth = 0.0;
var boxHeight = 0.0;
// This is the vertical offset for the box in vertical line orientation
// It will only be non-zero if this is the first box.
var dy = 0.0;
// loop though every run in the line
for (var j = line.textRunStart; j < line.textRunEnd; j++) {
final run = _runs[j];
// skips runs that are after selected range
if (run.start >= end) {
break;
}
// skip runs that are before the selected range
if (run.end <= start) {
dy += run.width;
continue;
}
// The size of full intermediate runs has already been cached
if (run.start >= start && run.end <= end) {
boxWidth = math.max(boxWidth, run.height);
boxHeight += run.width;
if (run.end == end) {
break;
}
continue;
}
// The range selection is in middle of a run
final localStart = math.max(start, run.start) - run.start;
final localEnd = math.min(end, run.end) - run.start;
final textBoxes = run.paragraph.getBoxesForRange(localStart, localEnd);
// empty boxes occur for partial selections of a grapheme cluster
if (textBoxes.isEmpty) {
if (end <= run.end) {
break;
} else {
dy += run.width;
continue;
}
}
// handle orientation differences for emoji and CJK characters
final box = textBoxes.first;
double verticalWidth;
double verticalHeight;
if (run.isRotated) {
verticalWidth = box.right;
verticalHeight = box.bottom;
} else {
dy += box.left;
verticalWidth = box.bottom;
verticalHeight = box.right - box.left;
}
// update the rect size
boxWidth = math.max(boxWidth, verticalWidth);
boxHeight += verticalHeight;
// if this is the last run then we're finished
if (end <= run.end) {
break;
}
}
if (boxWidth == 0.0 || boxHeight == 0.0) {
return Rect.zero;
}
return Rect.fromLTWH(dx, dy, boxWidth, boxHeight);
}
/// Returns the [TextRange] of the word at the given [TextPosition].
///
/// The current implementation just returns the correct text run, which is
/// generally a word.
TextRange getWordBoundary(TextPosition position) {
final offset = position.offset;
if (offset >= _text.length) {
return TextRange(start: _text.length, end: offset);
}
final run = _getRunFromOffset(offset);
if (run == null) {
return TextRange.empty;
}
return _splitBreakCharactersFromRun(run, offset);
}
// runs can include break characters currently so split them from the returned
// range
TextRange _splitBreakCharactersFromRun(_TextRun run, int offset) {
var start = run.start;
var end = run.end;
final finalChar = _text[end - 1];
if (LineBreaker.isBreakChar(finalChar)) {
if (offset == end - 1) {
start = end - 1;
} else {
end = end - 1;
}
}
return TextRange(start: start, end: end);
}
_TextRun? _getRunFromOffset(int offset) {
if (offset >= _text.length) {
return null;
}
var min = 0;
var max = _runs.length - 1;
// do a binary search
while (min <= max) {
final guess = (max + min) ~/ 2;
if (offset >= _runs[guess].end) {
min = guess + 1;
continue;
} else if (offset < _runs[guess].start) {
max = guess - 1;
continue;
} else {
return _runs[guess];
}
}
return null;
}
/// Returns the [TextRange] of the line at the given [TextPosition].
///
/// The newline (if any) is NOT returned as part of the range.
/// https://github.com/flutter/flutter/issues/83392
///
/// Not valid until after layout.
///
/// This can potentially be expensive, since it needs to compute the line
/// metrics, so use it sparingly.
TextRange getLineBoundary(TextPosition position) {
final offset = position.offset;
if (offset > _text.length) {
return TextRange.empty;
}
var min = 0;
var max = _lines.length - 1;
var start = -1;
var end = -1;
// do a binary search
while (min <= max) {
final guess = (max + min) ~/ 2;
final line = _lines[guess];
start = _runs[line.textRunStart].start;
end = _runs[line.textRunEnd - 1].end;
if (offset >= end) {
min = guess + 1;
continue;
} else if (offset < start) {
max = guess - 1;
continue;
} else {
break;
}
}
// exclude newline character
if (end > start && _text[end - 1] == '\n') {
end--;
}
return TextRange(start: start, end: end);
}
/// Returns the full list of [MongolLineMetrics] that describe in detail the various
/// metrics of each laid out line.
///
/// Not valid until after layout.
///
/// This can potentially return a large amount of data, so it is not recommended
/// to repeatedly call this. Instead, cache the results.
List<MongolLineMetrics> computeLineMetrics() {
final List<MongolLineMetrics> metrics = <MongolLineMetrics>[];
for (int index = 0; index < _lines.length; index += 1) {
final line = _lines[index];
bool hardBreak = false;
double ascent = 0;
double descent = 0;
double unscaledAscent = 0;
double height = 0;
double width = 0;
double baseline = 0;
for (int j = line.textRunStart; j < line.textRunEnd; j += 1) {
final textRun = _runs[j];
final metricsList = textRun.paragraph.computeLineMetrics();
final runMetrics = metricsList.isEmpty ? null : metricsList.first;
// last textRun of this line
if (j == line.textRunEnd - 1) {
hardBreak = _runEndsWithNewLine(textRun);
}
ascent = math.max(runMetrics?.ascent ?? 0, ascent);
descent = math.max(runMetrics?.descent ?? 0, descent);
unscaledAscent =
math.max(runMetrics?.unscaledAscent ?? 0, unscaledAscent);
width = math.max(runMetrics?.height ?? 0, width);
height += runMetrics?.width ?? textRun.width;
final previousMetrics = index > 0 ? metrics[index - 1] : null;
final previousLineAscent = previousMetrics?.ascent ?? 0.0;
final previousLineBaseline = previousMetrics?.baseline ?? 0.0;
baseline = previousLineBaseline + previousLineAscent + descent;
}
// ends with new line
if (line.textRunStart == -1 && line.textRunEnd == -1) {
final previousMetrics = metrics[index - 1];
hardBreak = true;
ascent = previousMetrics.ascent;
descent = previousMetrics.descent;
unscaledAscent = previousMetrics.unscaledAscent;
height = previousMetrics.height;
width = previousMetrics.width;
baseline = previousMetrics.baseline + previousMetrics.ascent + descent;
}
double top = 0;
if (_textAlign == MongolTextAlign.center) {
top = (this.height - height) / 2;
} else if (_textAlign == MongolTextAlign.bottom) {
top = this.height - height;
}
final lineMetrics = MongolLineMetrics(
hardBreak: hardBreak,
ascent: ascent,
descent: descent,
unscaledAscent: unscaledAscent,
height: height,
width: width,
top: top,
baseline: baseline,
lineNumber: index,
);
metrics.add(lineMetrics);
}
return metrics;
}
/// Release the resources used by this object. The object is no longer usable
/// after this method is called.
void dispose() {
assert(!_disposed);
assert(() {
_disposed = true;
return true;
}());
// Is this slow? Is it necessary?
// Do we need to dispose anything else?
for (final run in _runs) {
run.paragraph.dispose();
}
_runs.clear();
}
bool _disposed = false;
/// Whether this reference to the underlying picture is [dispose]d.
///
/// This only returns a valid value if asserts are enabled, and must not be
/// used otherwise.
bool get debugDisposed {
bool? disposed;
assert(() {
disposed = _disposed;
return true;
}());
return disposed ??
(throw StateError(
'$runtimeType.debugDisposed is only available when asserts are enabled.'));
}
}
/// Layout constraints for [MongolParagraph] objects.
///
/// Instances of this class are typically used with [MongolParagraph.layout].
///
/// The only constraint that can be specified is the [height].
class MongolParagraphConstraints {
const MongolParagraphConstraints({
required this.height,
});
/// The height the paragraph should use when computing the positions of glyphs.
final double height;
@override
bool operator ==(Object other) {
if (other.runtimeType != runtimeType) return false;
return other is MongolParagraphConstraints && other.height == height;
}
@override
int get hashCode => height.hashCode;
@override
String toString() => '$runtimeType(height: $height)';
}
/// Builds a [MongolParagraph] containing text with the given styling
/// information.
///
/// To set the paragraph's style, pass an appropriately-configured
/// [ParagraphStyle] object to the [MongolParagraphBuilder] constructor.
///
/// Then, call combinations of [pushStyle], [addText], and [pop] to add styled
/// text to the object.
///
/// Finally, call [build] to obtain the constructed [MongolParagraph] object.
/// After this point, the builder is no longer usable.
///
/// After constructing a [MongolParagraph], call [MongolParagraph.layout] on
/// it and then paint it with [MongolParagraph.draw].
class MongolParagraphBuilder {
MongolParagraphBuilder(
ui.ParagraphStyle style, {
MongolTextAlign textAlign = MongolTextAlign.top,
@Deprecated(
'Use textScaler instead. '
'Use of textScaleFactor was deprecated in preparation for the upcoming nonlinear text scaling support. '
'This feature was deprecated after v3.12.0-2.0.pre.',
)
double textScaleFactor = 1.0,
TextScaler textScaler = TextScaler.noScaling,
int? maxLines,
String? ellipsis,
}) : _paragraphStyle = style,
_textAlign = textAlign,
_textScaler = textScaler == TextScaler.noScaling
? TextScaler.linear(textScaleFactor)
: textScaler,
_maxLines = maxLines,
_ellipsis = ellipsis;
ui.ParagraphStyle? _paragraphStyle;
final MongolTextAlign _textAlign;
final TextScaler _textScaler;
final int? _maxLines;
final String? _ellipsis;
//_TextRun? _ellipsisRun;
final _styleStack = _Stack<TextStyle>();
final _rawStyledTextRuns = <_RawStyledTextRun>[];
static final _defaultParagraphStyle = ui.ParagraphStyle(
textAlign: TextAlign.start,
textDirection: TextDirection.ltr,
);
static final _defaultTextStyle = ui.TextStyle(
color: const Color(0xFFFFFFFF),
textBaseline: TextBaseline.alphabetic,
);
/// Applies the given style to the added text until [pop] is called.
///
/// See [pop] for details.
void pushStyle(TextStyle style) {
if (_styleStack.isEmpty) {
_styleStack.push(style);
return;
}
final lastStyle = _styleStack.top;
_styleStack.push(lastStyle.merge(style));
}
/// Ends the effect of the most recent call to [pushStyle].
///
/// Internally, the paragraph builder maintains a stack of text styles. Text
/// added to the paragraph is affected by all the styles in the stack. Calling
/// [pop] removes the topmost style in the stack, leaving the remaining styles
/// in effect.
void pop() {
_styleStack.pop();
}
final _plainText = StringBuffer();
/// Adds the given text to the paragraph.
///
/// The text will be styled according to the current stack of text styles.
void addText(String text) {
_plainText.write(text);
final style = _styleStack.isEmpty ? null : _styleStack.top;
final breakSegments = BreakSegments(text);
for (final segment in breakSegments) {
_rawStyledTextRuns.add(_RawStyledTextRun(style, segment));
}
}
/// Applies the given paragraph style and returns a [MongolParagraph]
/// containing the added text and associated styling.
///
/// After calling this function, the paragraph builder object is invalid and
/// cannot be used further.
MongolParagraph build() {
_paragraphStyle ??= _defaultParagraphStyle;