text.dart
26.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file 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.
*/
import 'dart:math' as math;
import 'package:meta/meta.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/src/pdf/font/arabic.dart' as arabic;
import 'annotations.dart';
import 'document.dart';
import 'geometry.dart';
import 'multi_page.dart';
import 'text_style.dart';
import 'theme.dart';
import 'widget.dart';
enum TextAlign { left, right, center, justify }
enum TextDirection { ltr, rtl }
/// How overflowing text should be handled.
enum TextOverflow {
/// Clip the overflowing text to fix its container.
clip,
/// Render overflowing text outside of its container.
visible,
/// Span to the next page when possible.
span,
}
abstract class _Span {
_Span(this.style);
final TextStyle style;
var offset = PdfPoint.zero;
double get left;
double get top;
double get width;
double get height;
@override
String toString() {
return 'Span "offset:$offset';
}
void debugPaint(
Context context,
double textScaleFactor,
PdfRect? globalBox,
) {}
void paint(
Context context,
TextStyle style,
double textScaleFactor,
PdfPoint point,
);
}
class _TextDecoration {
_TextDecoration(this.style, this.annotation, this.startSpan, this.endSpan)
: assert(startSpan <= endSpan);
static const double _space = -0.15;
final TextStyle style;
final AnnotationBuilder? annotation;
final int startSpan;
final int endSpan;
PdfRect? _box;
PdfRect? _getBox(List<_Span> spans) {
if (_box != null) {
return _box;
}
final x1 = spans[startSpan].offset.x + spans[startSpan].left;
final x2 =
spans[endSpan].offset.x + spans[endSpan].left + spans[endSpan].width;
var y1 = spans[startSpan].offset.y + spans[startSpan].top;
var y2 = y1 + spans[startSpan].height;
for (var n = startSpan + 1; n <= endSpan; n++) {
final ny1 = spans[n].offset.y + spans[n].top;
final ny2 = ny1 + spans[n].height;
y1 = math.min(y1, ny1);
y2 = math.max(y2, ny2);
}
_box = PdfRect.fromLTRB(x1, y1, x2, y2);
return _box;
}
_TextDecoration copyWith({int? endSpan}) =>
_TextDecoration(style, annotation, startSpan, endSpan ?? this.endSpan);
void backgroundPaint(
Context context,
double textScaleFactor,
PdfRect? globalBox,
List<_Span> spans,
) {
final box = _getBox(spans);
if (annotation != null) {
final spanBox = PdfRect(
globalBox!.x + box!.left,
globalBox.top + box.bottom,
box.width,
box.height,
);
annotation!.build(context, spanBox);
}
if (style.background != null) {
final boundingBox = PdfRect(
globalBox!.x + box!.left,
globalBox.top + box.bottom,
box.width,
box.height,
);
style.background!.paint(context, boundingBox);
context.canvas.setFillColor(style.color);
}
}
void foregroundPaint(
Context context,
double textScaleFactor,
PdfRect? globalBox,
List<_Span> spans,
) {
if (style.decoration == null) {
return;
}
final box = _getBox(spans);
final font = style.font!.getFont(context);
final space =
_space * style.fontSize! * textScaleFactor * style.decorationThickness!;
context.canvas
..setStrokeColor(style.decorationColor ?? style.color)
..setLineWidth(style.decorationThickness! *
style.fontSize! *
textScaleFactor *
0.05);
if (style.decoration!.contains(TextDecoration.underline)) {
final base = -font!.descent * style.fontSize! * textScaleFactor / 2;
context.canvas.drawLine(
globalBox!.x + box!.left,
globalBox.top + box.bottom + base,
globalBox.x + box.right,
globalBox.top + box.bottom + base,
);
if (style.decorationStyle == TextDecorationStyle.double) {
context.canvas.drawLine(
globalBox.x + box.left,
globalBox.top + box.bottom + base + space,
globalBox.x + box.right,
globalBox.top + box.bottom + base + space,
);
}
context.canvas.strokePath();
}
if (style.decoration!.contains(TextDecoration.overline)) {
final base = style.fontSize! * textScaleFactor;
context.canvas.drawLine(
globalBox!.x + box!.left,
globalBox.top + box.bottom + base,
globalBox.x + box.right,
globalBox.top + box.bottom + base,
);
if (style.decorationStyle == TextDecorationStyle.double) {
context.canvas.drawLine(
globalBox.x + box.left,
globalBox.top + box.bottom + base - space,
globalBox.x + box.right,
globalBox.top + box.bottom + base - space,
);
}
context.canvas.strokePath();
}
if (style.decoration!.contains(TextDecoration.lineThrough)) {
final base = (1 - font!.descent) * style.fontSize! * textScaleFactor / 2;
context.canvas.drawLine(
globalBox!.x + box!.left,
globalBox.top + box.bottom + base,
globalBox.x + box.right,
globalBox.top + box.bottom + base,
);
if (style.decorationStyle == TextDecorationStyle.double) {
context.canvas.drawLine(
globalBox.x + box.left,
globalBox.top + box.bottom + base + space,
globalBox.x + box.right,
globalBox.top + box.bottom + base + space,
);
}
context.canvas.strokePath();
}
}
void debugPaint(
Context context,
double textScaleFactor,
PdfRect globalBox,
List<_Span> spans,
) {
final box = _getBox(spans)!;
context.canvas
..setLineWidth(.5)
..drawRect(
globalBox.x + box.x, globalBox.top + box.y, box.width, box.height)
..setStrokeColor(PdfColors.yellow)
..strokePath();
}
}
class _Word extends _Span {
_Word(
this.text,
TextStyle style,
this.metrics,
) : super(style);
final String text;
final PdfFontMetrics metrics;
@override
double get left => metrics.left;
@override
double get top => metrics.descent;
@override
double get width => metrics.width;
@override
double get height => metrics.maxHeight;
@override
String toString() {
return 'Word "$text" offset:$offset metrics:$metrics style:$style';
}
@override
void paint(
Context context,
TextStyle style,
double textScaleFactor,
PdfPoint point,
) {
context.canvas.drawString(
style.font!.getFont(context)!,
style.fontSize! * textScaleFactor,
text,
point.x + offset.x,
point.y + offset.y,
mode: style.renderingMode ?? PdfTextRenderingMode.fill,
charSpace: style.letterSpacing ?? 0,
);
}
@override
void debugPaint(
Context context,
double textScaleFactor,
PdfRect? globalBox,
) {
const deb = 5;
context.canvas
..setLineWidth(.5)
..drawRect(globalBox!.x + offset.x + metrics.left,
globalBox.top + offset.y + metrics.top, metrics.width, metrics.height)
..setStrokeColor(PdfColors.orange)
..strokePath()
..drawLine(
globalBox.x + offset.x - deb,
globalBox.top + offset.y,
globalBox.x + offset.x + metrics.right + deb,
globalBox.top + offset.y)
..setStrokeColor(PdfColors.deepPurple)
..strokePath();
}
}
class _WidgetSpan extends _Span {
_WidgetSpan(this.widget, TextStyle style) : super(style);
final Widget widget;
@override
double get left => 0;
@override
double get top => 0;
@override
double get width => widget.box!.width;
@override
double get height => widget.box!.height;
@override
PdfPoint get offset => widget.box!.offset;
@override
set offset(PdfPoint value) {
widget.box = PdfRect.fromPoints(value, widget.box!.size);
}
@override
String toString() {
return 'Widget "$widget" offset:$offset';
}
@override
void paint(
Context context,
TextStyle? style,
double textScaleFactor,
PdfPoint point,
) {
widget.box = PdfRect.fromPoints(
PdfPoint(
point.x + widget.box!.offset.x, point.y + widget.box!.offset.y),
widget.box!.size);
widget.paint(context);
}
@override
void debugPaint(
Context context,
double textScaleFactor,
PdfRect? globalBox,
) {
context.canvas
..setLineWidth(.5)
..drawRect(
globalBox!.x + offset.x, globalBox.top + offset.y, width, height)
..setStrokeColor(PdfColors.orange)
..strokePath();
}
}
typedef _VisitorCallback = bool Function(
InlineSpan span,
TextStyle? parentStyle,
AnnotationBuilder? annotation,
);
@immutable
abstract class InlineSpan {
const InlineSpan({this.style, this.baseline, this.annotation});
final TextStyle? style;
final double? baseline;
final AnnotationBuilder? annotation;
String toPlainText() {
final buffer = StringBuffer();
visitChildren((
InlineSpan span,
TextStyle? style,
AnnotationBuilder? annotation,
) {
if (span is TextSpan) {
buffer.write(span.text);
}
return true;
}, null, null);
return buffer.toString();
}
bool visitChildren(
_VisitorCallback visitor,
TextStyle? parentStyle,
AnnotationBuilder? annotation,
);
}
class WidgetSpan extends InlineSpan {
/// Creates a [WidgetSpan] with the given values.
const WidgetSpan({
required this.child,
double baseline = 0,
TextStyle? style,
AnnotationBuilder? annotation,
}) : super(style: style, baseline: baseline, annotation: annotation);
/// The widget to embed inline within text.
final Widget child;
/// Calls `visitor` on this [WidgetSpan]. There are no children spans to walk.
@override
bool visitChildren(
_VisitorCallback visitor,
TextStyle? parentStyle,
AnnotationBuilder? annotation,
) {
final _style = parentStyle?.merge(style);
final _a = this.annotation ?? annotation;
return visitor(this, _style, _a);
}
}
class TextSpan extends InlineSpan {
const TextSpan({
TextStyle? style,
this.text,
double baseline = 0,
this.children,
AnnotationBuilder? annotation,
}) : super(style: style, baseline: baseline, annotation: annotation);
final String? text;
final List<InlineSpan>? children;
@override
bool visitChildren(
_VisitorCallback visitor,
TextStyle? parentStyle,
AnnotationBuilder? annotation,
) {
final _style = parentStyle?.merge(style);
final _a = this.annotation ?? annotation;
if (text != null) {
if (!visitor(this, _style, _a)) {
return false;
}
}
if (children != null) {
for (var child in children!) {
if (!child.visitChildren(visitor, _style, _a)) {
return false;
}
}
}
return true;
}
}
class _Line {
const _Line(
this.parent,
this.firstSpan,
this.countSpan,
this.baseline,
this.wordsWidth,
this.textDirection,
this.justify,
);
final RichText parent;
final int firstSpan;
final int countSpan;
int get lastSpan => firstSpan + countSpan;
TextAlign get textAlign => parent._textAlign;
final double baseline;
final double wordsWidth;
final TextDirection textDirection;
final bool justify;
double get height {
final list = parent._spans.sublist(firstSpan, lastSpan);
return list.isEmpty
? 0
: list.reduce((a, b) => a.height > b.height ? a : b).height;
}
@override
String toString() =>
'$runtimeType $firstSpan-$lastSpan baseline: $baseline width:$wordsWidth';
void realign(double totalWidth) {
final spans = parent._spans.sublist(firstSpan, lastSpan);
var delta = 0.0;
switch (textAlign) {
case TextAlign.left:
break;
case TextAlign.right:
delta = totalWidth - wordsWidth;
break;
case TextAlign.center:
delta = (totalWidth - wordsWidth) / 2.0;
break;
case TextAlign.justify:
if (!justify) {
totalWidth = wordsWidth;
break;
}
delta = (totalWidth - wordsWidth) / (spans.length - 1);
var x = 0.0;
for (var span in spans) {
span.offset = span.offset.translate(x, -baseline);
x += delta;
}
return;
}
if (textDirection == TextDirection.rtl) {
for (var span in spans) {
span.offset = PdfPoint(
totalWidth - (span.offset.x + span.width) - delta,
span.offset.y - baseline,
);
}
return;
}
for (var span in spans) {
span.offset = span.offset.translate(delta, -baseline);
}
return;
}
}
class _RichTextContext extends WidgetContext {
var startOffset = 0.0;
var endOffset = 0.0;
var spanStart = 0;
var spanEnd = 0;
@override
void apply(_RichTextContext other) {
startOffset = other.startOffset;
endOffset = other.endOffset;
spanStart = other.spanStart;
spanEnd = other.spanEnd;
}
@override
WidgetContext clone() {
return _RichTextContext()..apply(this);
}
@override
String toString() =>
'$runtimeType Offset: $startOffset -> $endOffset Span: $spanStart -> $spanEnd';
}
class RichText extends Widget with SpanningWidget {
RichText({
required this.text,
this.textAlign,
this.textDirection,
this.softWrap,
this.tightBounds = false,
this.textScaleFactor = 1.0,
this.maxLines,
this.overflow = TextOverflow.visible,
});
static bool debug = false;
final InlineSpan text;
final TextAlign? textAlign;
late TextAlign _textAlign;
final TextDirection? textDirection;
final double textScaleFactor;
final bool? softWrap;
final bool tightBounds;
final int? maxLines;
final List<_Span> _spans = <_Span>[];
final List<_TextDecoration> _decorations = <_TextDecoration>[];
final _context = _RichTextContext();
final TextOverflow? overflow;
var _mustClip = false;
void _appendDecoration(bool append, _TextDecoration td) {
if (append && _decorations.isNotEmpty) {
final last = _decorations.last;
if (last.style == td.style && last.annotation == td.annotation) {
_decorations[_decorations.length - 1] =
last.copyWith(endSpan: td.endSpan);
return;
}
}
_decorations.add(td);
}
@override
void layout(Context context, BoxConstraints constraints,
{bool parentUsesSize = false}) {
_spans.clear();
_decorations.clear();
final theme = Theme.of(context);
final defaultstyle = theme.defaultTextStyle;
final _softWrap = softWrap ?? theme.softWrap;
final _maxLines = maxLines ?? theme.maxLines;
_textAlign = textAlign ?? theme.textAlign;
final _textDirection = textDirection ?? Directionality.of(context);
final _overflow = this.overflow ?? theme.overflow;
final constraintWidth = constraints.hasBoundedWidth
? constraints.maxWidth
: constraints.constrainWidth();
final constraintHeight = constraints.hasBoundedHeight
? constraints.maxHeight
: constraints.constrainHeight();
var offsetX = 0.0;
var offsetY = _context.startOffset;
var top = 0.0;
var bottom = 0.0;
final lines = <_Line>[];
var spanCount = 0;
var spanStart = 0;
var overflow = false;
text.visitChildren((
InlineSpan span,
TextStyle? style,
AnnotationBuilder? annotation,
) {
if (span is TextSpan) {
if (span.text == null) {
return true;
}
final font = style!.font!.getFont(context)!;
final space =
font.stringMetrics(' ') * (style.fontSize! * textScaleFactor);
final spanLines = (_textDirection == TextDirection.rtl
? arabic.convert(span.text!)
: span.text)!
.split('\n');
for (var line = 0; line < spanLines.length; line++) {
final words = spanLines[line].split(RegExp(r'\s'));
for (var index = 0; index < words.length; index++) {
final word = words[index];
if (word.isEmpty) {
offsetX += space.advanceWidth * style.wordSpacing! +
style.letterSpacing!;
continue;
}
final metrics = font.stringMetrics(word,
letterSpacing: style.letterSpacing! /
(style.fontSize! * textScaleFactor)) *
(style.fontSize! * textScaleFactor);
if (_softWrap &&
offsetX + metrics.width > constraintWidth + 0.00001) {
if (spanCount > 0 && metrics.width <= constraintWidth) {
overflow = true;
lines.add(_Line(
this,
spanStart,
spanCount,
bottom,
offsetX -
space.advanceWidth * style.wordSpacing! -
style.letterSpacing!,
_textDirection,
true,
));
spanStart += spanCount;
spanCount = 0;
offsetX = 0.0;
offsetY += bottom - top;
top = 0;
bottom = 0;
if (_maxLines != null && lines.length >= _maxLines) {
return false;
}
if (offsetY > constraintHeight) {
return false;
}
offsetY += style.lineSpacing! * textScaleFactor;
} else {
// One word Overflow, try to split it.
final pos = _splitWord(word, font, style, constraintWidth);
if (pos < word.length) {
words[index] = word.substring(0, pos);
words.insert(index + 1, word.substring(pos));
// Try again
index--;
continue;
}
}
}
final baseline = span.baseline! * textScaleFactor;
final mt = tightBounds ? metrics.top : metrics.descent;
final mb = tightBounds ? metrics.bottom : metrics.ascent;
top = math.min(top, mt + baseline);
bottom = math.max(bottom, mb + baseline);
final wd = _Word(
word,
style,
metrics,
);
wd.offset = PdfPoint(offsetX, -offsetY + baseline);
_spans.add(wd);
spanCount++;
_appendDecoration(
spanCount > 1,
_TextDecoration(
style,
annotation,
_spans.length - 1,
_spans.length - 1,
),
);
offsetX += metrics.advanceWidth +
space.advanceWidth * style.wordSpacing! +
style.letterSpacing!;
}
if (line < spanLines.length - 1) {
lines.add(_Line(
this,
spanStart,
spanCount,
bottom,
offsetX -
space.advanceWidth * style.wordSpacing! -
style.letterSpacing!,
_textDirection,
false,
));
spanStart += spanCount;
offsetX = 0.0;
if (spanCount > 0) {
offsetY += bottom - top;
} else {
offsetY += space.ascent + space.descent;
}
top = 0;
bottom = 0;
spanCount = 0;
if (_maxLines != null && lines.length >= _maxLines) {
return false;
}
if (offsetY > constraintHeight) {
return false;
}
offsetY += style.lineSpacing! * textScaleFactor;
}
}
offsetX -=
space.advanceWidth * style.wordSpacing! - style.letterSpacing!;
} else if (span is WidgetSpan) {
span.child.layout(
context,
BoxConstraints(
maxWidth: constraintWidth,
maxHeight: constraintHeight,
));
final ws = _WidgetSpan(
span.child,
style!,
);
if (offsetX + ws.width > constraintWidth && spanCount > 0) {
overflow = true;
lines.add(_Line(
this,
spanStart,
spanCount,
bottom,
offsetX,
_textDirection,
true,
));
spanStart += spanCount;
spanCount = 0;
if (_maxLines != null && lines.length > _maxLines) {
return false;
}
offsetX = 0.0;
offsetY += bottom - top;
top = 0;
bottom = 0;
if (offsetY > constraintHeight) {
return false;
}
offsetY += style.lineSpacing! * textScaleFactor;
}
final baseline = span.baseline! * textScaleFactor;
top = math.min(top, baseline);
bottom = math.max(
bottom,
ws.height + baseline,
);
ws.offset = PdfPoint(offsetX, -offsetY + baseline);
_spans.add(ws);
spanCount++;
_appendDecoration(
spanCount > 1,
_TextDecoration(
style,
annotation,
_spans.length - 1,
_spans.length - 1,
),
);
offsetX += ws.left + ws.width;
}
return true;
}, defaultstyle, null);
if (spanCount > 0) {
lines.add(_Line(
this,
spanStart,
spanCount,
bottom,
offsetX,
_textDirection,
false,
));
offsetY += bottom - top;
}
assert(!overflow || constraintWidth.isFinite);
var width = overflow ? constraintWidth : constraints.minWidth;
if (lines.isNotEmpty) {
if (!overflow) {
// Calculate the final width
for (final line in lines) {
width = math.max(width, line.wordsWidth);
}
}
// Realign all the lines
for (final line in lines) {
line.realign(width);
}
}
box = PdfRect(0, 0, constraints.constrainWidth(width),
constraints.constrainHeight(offsetY));
_context
..endOffset = offsetY - _context.startOffset
..spanEnd = _spans.length;
if (_overflow != TextOverflow.span) {
if (_overflow != TextOverflow.visible) {
_mustClip = true;
}
return;
}
if (offsetY > constraintHeight + 0.0001) {
_context.spanEnd -= lines.last.countSpan;
_context.endOffset -= lines.last.height;
}
for (var index = 0; index < _decorations.length; index++) {
final decoration = _decorations[index];
if (decoration.startSpan >= _context.spanEnd ||
decoration.endSpan < _context.spanStart) {
_decorations.removeAt(index);
index--;
}
}
}
@override
void debugPaint(Context context) {
context.canvas
..setStrokeColor(PdfColors.blue)
..setLineWidth(1)
..drawRect(
box!.x,
box!.y,
box!.width == double.infinity ? 1000 : box!.width,
box!.height == double.infinity ? 1000 : box!.height,
)
..strokePath();
}
@override
void paint(Context context) {
super.paint(context);
TextStyle? currentStyle;
PdfColor? currentColor;
if (_mustClip) {
context.canvas
..saveContext()
..drawBox(box!)
..clipPath();
}
for (var decoration in _decorations) {
assert(() {
if (Document.debug && RichText.debug) {
decoration.debugPaint(context, textScaleFactor, box!, _spans);
}
return true;
}());
decoration.backgroundPaint(
context,
textScaleFactor,
box,
_spans,
);
}
for (var span in _spans.sublist(_context.spanStart, _context.spanEnd)) {
assert(() {
if (Document.debug && RichText.debug) {
span.debugPaint(context, textScaleFactor, box);
}
return true;
}());
if (span.style != currentStyle) {
currentStyle = span.style;
if (currentStyle.color != currentColor) {
currentColor = currentStyle.color;
context.canvas.setFillColor(currentColor);
}
}
span.paint(
context,
currentStyle!,
textScaleFactor,
PdfPoint(box!.left, box!.top),
);
}
for (var decoration in _decorations) {
decoration.foregroundPaint(
context,
textScaleFactor,
box,
_spans,
);
}
if (_mustClip) {
context.canvas.restoreContext();
}
}
int _splitWord(String word, PdfFont font, TextStyle style, double maxWidth) {
var low = 0;
var high = word.length;
var pos = (low + high) ~/ 2;
while (low + 1 < high) {
final metrics = font.stringMetrics(word.substring(0, pos),
letterSpacing:
style.letterSpacing! / (style.fontSize! * textScaleFactor)) *
(style.fontSize! * textScaleFactor);
if (metrics.width > maxWidth) {
high = pos;
} else {
low = pos;
}
pos = (low + high) ~/ 2;
}
return math.max(1, pos);
}
@override
bool get canSpan => overflow == TextOverflow.span;
@override
bool get hasMoreWidgets => overflow == TextOverflow.span;
@override
void restoreContext(_RichTextContext context) {
_context.spanStart = context.spanEnd;
_context.startOffset = -context.endOffset;
}
@override
WidgetContext saveContext() {
return _context;
}
}
class Text extends RichText {
Text(
String text, {
TextStyle? style,
TextAlign? textAlign,
TextDirection? textDirection,
bool? softWrap,
bool tightBounds = false,
double textScaleFactor = 1.0,
int? maxLines,
TextOverflow? overflow,
}) : super(
text: TextSpan(text: text, style: style),
textAlign: textAlign,
softWrap: softWrap,
tightBounds: tightBounds,
textDirection: textDirection,
textScaleFactor: textScaleFactor,
maxLines: maxLines,
overflow: overflow,
);
}