-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathtext.dart
1095 lines (987 loc) · 32.9 KB
/
text.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 2013 The Flutter Authors. 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:async';
import 'dart:math' show max;
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'diagram_step.dart';
const String _textEllipsis = 'text_ellipsis';
const String _textFadeMaxLines = 'text_fade_max_lines';
const String _textFadeSoftWrap = 'text_fade_soft_wrap';
const String _textRich = 'text_rich';
const String _textBorder = 'text_border';
const String _textGradient = 'text_gradient';
class TextDiagram extends StatelessWidget with DiagramMetadata {
const TextDiagram(this.name, {super.key});
@override
final String name;
@override
Widget build(BuildContext context) {
Widget returnWidget;
switch (name) {
case _textEllipsis:
returnWidget = Container(
width: 100,
decoration: BoxDecoration(border: Border.all()),
child: const Text(
overflow: TextOverflow.ellipsis,
'Hello Ruth, how are you?',
),
);
break;
case _textFadeMaxLines:
returnWidget = Container(
width: 100,
decoration: BoxDecoration(border: Border.all()),
child: const Text(
overflow: TextOverflow.fade,
maxLines: 1,
'Hello Ruth, how are you?',
),
);
break;
case _textFadeSoftWrap:
returnWidget = Container(
width: 100,
decoration: BoxDecoration(border: Border.all()),
child: const Text(
overflow: TextOverflow.fade,
softWrap: false,
'Hello Ruth, how are you?',
),
);
break;
case _textRich:
returnWidget = const Text.rich(
TextSpan(
text: 'Hello', // default text style
children: <TextSpan>[
TextSpan(
text: ' beautiful ',
style: TextStyle(fontStyle: FontStyle.italic),
),
TextSpan(
text: 'world',
style: TextStyle(fontWeight: FontWeight.bold),
),
],
),
);
break;
case _textBorder:
returnWidget = ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 200),
child: Stack(
children: <Widget>[
// Stroked text as border.
Text(
'Greetings, planet!',
style: TextStyle(
fontSize: 40,
foreground:
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 6
..color = Colors.blue[700]!,
),
),
// Solid text as fill.
Text(
'Greetings, planet!',
style: TextStyle(fontSize: 40, color: Colors.grey[300]),
),
],
),
);
break;
case _textGradient:
returnWidget = ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 200),
child: Stack(
children: <Widget>[
// Gradient text.
Text(
'Greetings, planet!',
style: TextStyle(
fontSize: 40,
foreground:
Paint()
..shader = ui.Gradient.linear(
const Offset(0, 20),
const Offset(150, 20),
<Color>[Colors.red, Colors.yellow],
),
),
),
],
),
);
break;
default:
returnWidget = const Text('Error');
break;
}
return ConstrainedBox(
key: UniqueKey(),
constraints: BoxConstraints.tight(const Size(240.0, 140.0)),
child: Container(
alignment: FractionalOffset.center,
padding: const EdgeInsets.all(5.0),
color: Colors.white,
child: returnWidget,
),
);
}
}
class TextHeightDiagram extends StatelessWidget with DiagramMetadata {
const TextHeightDiagram(this.name, {super.key});
@override
final String name;
@override
Widget build(BuildContext context) {
final TextPainter textPainter = TextPainter(
text: const TextSpan(
text: ' AaBbGgJj ',
style: TextStyle(fontSize: 100, color: Colors.black),
),
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,
);
return Container(
width: 800,
height: 200,
color: Colors.white,
child: CustomPaint(
size: const Size(1000, 300),
painter: TextDiagramPainter(textPainter),
),
);
}
}
class TextDiagramPainter extends CustomPainter {
const TextDiagramPainter(this.textPainter);
final TextPainter textPainter;
static const int largeIndex = 99;
@override
void paint(Canvas canvas, Size size) {
textPainter.layout();
final List<TextBox> boxes = textPainter.getBoxesForSelection(
const TextSelection(baseOffset: 0, extentOffset: largeIndex),
);
final Paint paint = Paint();
paint.color = Colors.black;
paint.strokeWidth = 3.5;
const double top = 0;
final double bottom = textPainter.height;
final double baseline = textPainter.computeDistanceToActualBaseline(
TextBaseline.alphabetic,
);
final double ratio = 100.0 / textPainter.height;
final double emTop = baseline - (baseline - top) * ratio;
final double emBottom = baseline + (bottom - baseline) * ratio;
final double width = boxes[boxes.length - 1].right;
final Offset baseOffset = Offset(
(size.width - width) / 2,
(size.height - textPainter.height) / 2,
);
textPainter.paint(canvas, baseOffset);
// Baseline
canvas.drawLine(
baseOffset + Offset(0, baseline),
baseOffset + Offset(width, baseline),
paint,
);
paint.color = Colors.blue[900]!;
// Top
canvas.drawLine(baseOffset, baseOffset + Offset(width, top), paint);
// Bottom
canvas.drawLine(
baseOffset + Offset(0, bottom),
baseOffset + Offset(width, bottom),
paint,
);
paint.strokeWidth = 2;
paint.color = Colors.red[900]!;
// emTop
canvas.drawLine(
baseOffset + Offset(0, emTop),
baseOffset + Offset(width, emTop),
paint,
);
// emBottom
canvas.drawLine(
baseOffset + Offset(0, emBottom),
baseOffset + Offset(width, emBottom),
paint,
);
paint.strokeWidth = 2;
paint.style = PaintingStyle.stroke;
Path path = Path();
path.moveTo(baseOffset.dx + width + 10, baseOffset.dy + emTop);
path.lineTo(baseOffset.dx + width + 25, baseOffset.dy + emTop);
path.lineTo(baseOffset.dx + width + 25, baseOffset.dy + emBottom);
path.lineTo(baseOffset.dx + width + 10, baseOffset.dy + emBottom);
canvas.drawPath(path, paint);
paint.color = Colors.blue[900]!;
path = Path();
path.moveTo(baseOffset.dx - 10, baseOffset.dy + top);
path.lineTo(baseOffset.dx - 25, baseOffset.dy + top);
path.lineTo(baseOffset.dx - 25, baseOffset.dy + bottom);
path.lineTo(baseOffset.dx - 10, baseOffset.dy + bottom);
canvas.drawPath(path, paint);
const double margin = 8;
TextPainter label = TextPainter(
text: const TextSpan(
text: 'Font metrics\ndefault height',
style: TextStyle(fontSize: 12, color: Colors.black),
),
textAlign: TextAlign.right,
textDirection: TextDirection.ltr,
);
label.layout();
label.paint(
canvas,
baseOffset +
Offset(
-25.0 - (paint.strokeWidth + margin) - label.width,
(top + bottom) / 2 - 16,
),
);
paint.color = Colors.red[900]!;
label = TextPainter(
text: const TextSpan(
text: 'Font Size\n(EM-square)',
style: TextStyle(fontSize: 12, color: Colors.black),
),
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,
);
label.layout();
label.paint(
canvas,
baseOffset + Offset(width + 25 + margin, (emTop + emBottom) / 2 - 16),
);
paint.color = Colors.black;
// Baseline label
label = TextPainter(
text: const TextSpan(
text: 'Alphabetic Baseline',
style: TextStyle(fontSize: 11, color: Colors.black),
),
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,
);
label.layout();
label.paint(canvas, baseOffset + Offset(0, baseline + 3));
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
final TextDiagramPainter diagramPainter = oldDelegate as TextDiagramPainter;
return textPainter != diagramPainter.textPainter;
}
}
// Height values comparison.
class TextHeightComparison extends TextHeightDiagram with DiagramMetadata {
const TextHeightComparison(super.name, {super.key});
@override
Widget build(BuildContext context) {
double totalHeight = 70.0 + 10;
for (final double h in <double>[1, 1, 1.15, 2, 3]) {
totalHeight += 70 * h + 30;
}
return Container(
width: 600,
height: totalHeight,
color: Colors.white,
child: Column(
children: <Widget>[
Container(
width: 600,
height: 70,
color: const Color.fromARGB(255, 180, 180, 180),
child: const Center(
child: Text(
'Roboto, fontSize:50',
style: TextStyle(fontSize: 30, fontWeight: FontWeight.w700),
),
),
),
const CustomPaint(
size: Size(600, 70.0 + 30 + 10),
painter: TextHeightComparisonPainter('Axy', null, 0),
),
const CustomPaint(
size: Size(600, 70.0 * 1.0 + 30),
painter: TextHeightComparisonPainter('Axy', 1, 1),
),
const CustomPaint(
size: Size(600, 70.0 * 1.15 + 30),
painter: TextHeightComparisonPainter('Axy', 1.15, 2),
),
const CustomPaint(
size: Size(600, 70.0 * 2.0 + 30),
painter: TextHeightComparisonPainter('Axy', 2, 3),
),
const CustomPaint(
size: Size(600, 70.0 * 3.0 + 30),
painter: TextHeightComparisonPainter('Axy', 3, 4),
),
],
),
);
}
}
class TextHeightComparisonPainter extends CustomPainter {
const TextHeightComparisonPainter(this.text, this.height, this.index);
final String text;
final double? height;
final int index;
static const int largeIndex = 99;
@override
void paint(Canvas canvas, Size size) {
final Paint paint = Paint();
if (index.isEven) {
paint.color = const Color.fromARGB(255, 235, 235, 235);
} else {
paint.color = const Color.fromARGB(255, 250, 250, 250);
}
canvas.drawRect(Rect.fromLTRB(0, 0, size.width, size.height), paint);
final TextPainter textPainter = TextPainter(
text: TextSpan(
text: 'height:$height, $text',
style: TextStyle(fontSize: 50, height: height, color: Colors.black),
),
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,
);
textPainter.layout();
final List<TextBox> boxes = textPainter.getBoxesForSelection(
const TextSelection(baseOffset: 0, extentOffset: largeIndex),
);
paint.color = Colors.black;
paint.strokeWidth = 3.5;
const double top = 0;
final double bottom = textPainter.height;
final double baseline = textPainter.computeDistanceToActualBaseline(
TextBaseline.alphabetic,
);
final double width = boxes[boxes.length - 1].right;
final Offset baseOffset = Offset(
(size.width - width) / 2 + 30,
(size.height - textPainter.height) / 2,
);
textPainter.paint(canvas, baseOffset);
paint.color = Colors.red[900]!;
// Top
canvas.drawLine(baseOffset, baseOffset + Offset(width, top), paint);
// Bottom
canvas.drawLine(
baseOffset + Offset(0, bottom),
baseOffset + Offset(width, bottom),
paint,
);
// Baseline
paint.color = Colors.black;
paint.strokeWidth = 2.5;
canvas.drawLine(
baseOffset + Offset(0, baseline),
baseOffset + Offset(width, baseline),
paint,
);
paint.strokeWidth = 2;
paint.style = PaintingStyle.stroke;
paint.color = Colors.blue[900]!;
final Path path = Path();
path.moveTo(baseOffset.dx - 10, baseOffset.dy + top);
path.lineTo(baseOffset.dx - 25, baseOffset.dy + top);
path.lineTo(baseOffset.dx - 25, baseOffset.dy + bottom);
path.lineTo(baseOffset.dx - 10, baseOffset.dy + bottom);
canvas.drawPath(path, paint);
TextPainter label = TextPainter(
text: TextSpan(
text: '${bottom - top}px',
style: const TextStyle(fontSize: 20, color: Colors.black),
),
textAlign: TextAlign.right,
textDirection: TextDirection.ltr,
);
label.layout();
label.paint(
canvas,
baseOffset + Offset(-25.0 - 80, (top + bottom) / 2 - 10),
);
paint.color = Colors.black;
// Baseline label
label = TextPainter(
text: const TextSpan(
text: 'Alphabetic Baseline',
style: TextStyle(fontSize: 9, color: Colors.black),
),
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,
);
label.layout();
label.paint(canvas, baseOffset + Offset(0, baseline + 1));
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
final TextHeightComparisonPainter diagramPainter =
oldDelegate as TextHeightComparisonPainter;
return text != diagramPainter.text || height != diagramPainter.height;
}
}
/// Side-by-side comparison of paragraphs with different text height
/// configuration combinations.
class TextHeightBreakdown extends TextHeightDiagram with DiagramMetadata {
const TextHeightBreakdown(super.name, {super.key});
static const double _height = 4;
static const double _fontSize = 85;
static const String _text = ' Axy ';
@override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
height: 500,
child: const Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Center(
child: Text(
'Roboto, fontSize: $_fontSize, height: $_height',
style: TextStyle(fontSize: 30, fontWeight: FontWeight.w700),
),
),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: <Widget>[
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
// This must be the first child of the column for the Row
// elements to be properly baseline-aligned.
TextHeightBreakdownRow(
text: _text,
backgroundColor: Colors.transparent,
style: TextStyle(
fontSize: _fontSize,
height: _height,
color: Colors.black,
leadingDistribution:
TextLeadingDistribution.proportional,
),
paintHeightIndicator: true,
paintCaptions: true,
paintLeadingIndicator: true,
),
SizedBox(height: 30),
// This column has the height indicator so the glyph is not
// centered. This is a hack to offset the "Configuration 1"
// caption so it looks more aligned with the glyph.
Text(
' Configuration 1',
textScaler: TextScaler.linear(1.5),
),
],
),
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextHeightBreakdownRow(
text: _text,
backgroundColor: Colors.transparent,
style: TextStyle(
fontSize: _fontSize,
height: _height,
color: Colors.black,
leadingDistribution:
TextLeadingDistribution.proportional,
),
textHeightBehavior: TextHeightBehavior(
applyHeightToFirstAscent: false,
),
paintCaptions: true,
),
SizedBox(height: 30),
Text(
'Configuration 2',
textScaler: TextScaler.linear(1.5),
),
],
),
],
),
Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: <Widget>[
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextHeightBreakdownRow(
text: _text,
backgroundColor: Colors.transparent,
style: TextStyle(
fontSize: _fontSize,
height: _height,
color: Colors.black,
leadingDistribution: TextLeadingDistribution.even,
),
paintLeadingIndicator: true,
),
SizedBox(height: 30),
Text(
'Configuration 3',
textScaler: TextScaler.linear(1.5),
),
],
),
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextHeightBreakdownRow(
text: _text,
backgroundColor: Colors.transparent,
style: TextStyle(
fontSize: _fontSize,
height: _height,
color: Colors.black,
leadingDistribution: TextLeadingDistribution.even,
),
textHeightBehavior: TextHeightBehavior(
applyHeightToLastDescent: false,
),
),
SizedBox(height: 30),
Text(
'Configuration 4',
textScaler: TextScaler.linear(1.5),
),
],
),
],
),
],
),
],
),
);
}
}
class TextHeightBreakdownRow extends LeafRenderObjectWidget {
const TextHeightBreakdownRow({
super.key,
required this.text,
required this.backgroundColor,
required this.style,
this.textHeightBehavior,
this.paintHeightIndicator = false,
this.paintLeadingIndicator = false,
this.paintCaptions = false,
});
final String text;
final TextStyle style;
final Color backgroundColor;
final TextHeightBehavior? textHeightBehavior;
final bool paintHeightIndicator;
final bool paintLeadingIndicator;
final bool paintCaptions;
@override
RenderTextHeightBreakdown createRenderObject(BuildContext context) {
return RenderTextHeightBreakdown(
text: TextSpan(text: text, style: style),
backgroundColor: backgroundColor,
textDirection: Directionality.of(context),
textHeightBehavior: textHeightBehavior,
paintHeightIndicator: paintHeightIndicator,
paintLeadingIndicator: paintLeadingIndicator,
paintCaptions: paintCaptions,
);
}
@override
void updateRenderObject(
BuildContext context,
RenderTextHeightBreakdown renderObject,
) {
renderObject
..text = TextSpan(text: text, style: style)
..backgroundColor = backgroundColor
..textDirection = Directionality.of(context)
..textHeightBehavior = textHeightBehavior
..paintHeightIndicator = paintHeightIndicator
..paintLeadingIndicator = paintLeadingIndicator
..paintCaptions = paintCaptions;
}
}
class RenderTextHeightBreakdown extends RenderBox
with RenderObjectWithChildMixin<RenderBox> {
RenderTextHeightBreakdown({
required TextDirection textDirection,
required TextSpan text,
required Color backgroundColor,
required TextHeightBehavior? textHeightBehavior,
required bool paintHeightIndicator,
required bool paintCaptions,
required bool paintLeadingIndicator,
}) : _backgroundColor = backgroundColor,
_paintHeightIndicator = paintHeightIndicator,
_paintLeadingIndicator = paintLeadingIndicator,
_paintCaptions = paintCaptions,
_textPainter = TextPainter(
textDirection: textDirection,
text: text,
textHeightBehavior: textHeightBehavior,
);
final TextPainter _textPainter;
TextDirection get textDirection => _textPainter.textDirection!;
set textDirection(TextDirection value) {
if (_textPainter.textDirection == value) {
return;
}
_textPainter.textDirection = value;
markNeedsLayout();
}
TextSpan get text => _textPainter.text! as TextSpan;
set text(TextSpan? value) {
if (_textPainter.text == value) {
return;
}
_textPainter.text = value;
markNeedsLayout();
}
Color get backgroundColor => _backgroundColor;
Color _backgroundColor;
set backgroundColor(Color value) {
if (value == _backgroundColor) {
return;
}
_backgroundColor = value;
markNeedsPaint();
}
TextHeightBehavior? get textHeightBehavior => _textPainter.textHeightBehavior;
set textHeightBehavior(TextHeightBehavior? newValue) {
if (textHeightBehavior == newValue) {
return;
}
_textPainter.textHeightBehavior = newValue;
markNeedsLayout();
}
bool get paintHeightIndicator => _paintHeightIndicator;
bool _paintHeightIndicator;
set paintHeightIndicator(bool value) {
if (value == _paintHeightIndicator) {
return;
}
_paintHeightIndicator = value;
markNeedsPaint();
}
bool get paintLeadingIndicator => _paintLeadingIndicator;
bool _paintLeadingIndicator;
set paintLeadingIndicator(bool value) {
if (value == _paintLeadingIndicator) {
return;
}
_paintLeadingIndicator = value;
markNeedsPaint();
}
bool get paintCaptions => _paintCaptions;
bool _paintCaptions;
set paintCaptions(bool value) {
if (value == _paintCaptions) {
return;
}
_paintCaptions = value;
markNeedsPaint();
}
late final TextPainter heightCaptionTextPainter = TextPainter(
textDirection: textDirection,
textAlign: TextAlign.center,
);
static final TextStyle heightCaptionTextStyle = TextStyle(
fontSize: 20,
color: Colors.blue[900],
);
static const Offset fontMetricsLabelPadding = Offset(0, 3);
static const double heightCaptionBracketMinX = -25;
@override
Size computeDryLayout(BoxConstraints constraints) {
_textPainter.layout(minWidth: constraints.minWidth);
if (paintHeightIndicator) {
heightCaptionTextPainter.text = TextSpan(
text: 'Text Height:\n${_textPainter.height} px',
style: heightCaptionTextStyle,
);
heightCaptionTextPainter.layout(minWidth: constraints.minWidth);
return constraints.constrain(
Size(
heightCaptionTextPainter.width +
_textPainter.width +
fontMetricsLabelPadding.dx -
heightCaptionBracketMinX,
max(heightCaptionTextPainter.height, _textPainter.height),
),
);
}
return constraints.constrain(_textPainter.size);
}
@override
double? computeDistanceToActualBaseline(TextBaseline baseline) =>
_textPainter.computeDistanceToActualBaseline(baseline);
@override
void performLayout() {
size = computeDryLayout(constraints);
assert(size.height == _textPainter.height);
}
@override
void paint(PaintingContext context, Offset offset) {
final Paint paint = Paint();
final Canvas canvas = context.canvas;
paint.color = backgroundColor;
// Paint the background color.
canvas.drawRect(offset & size, paint);
final Offset lineBoxOrigin =
paintHeightIndicator
// Left-align the height caption. Ideally this should also be centered.
? Offset(
heightCaptionTextPainter.width +
fontMetricsLabelPadding.dx -
heightCaptionBracketMinX,
0,
) +
offset
: (size - _textPainter.size as Offset) / 2 + offset;
assert(lineBoxOrigin.dy == offset.dy);
// Paint the text. Layout is done in performLayout.
_textPainter.paint(canvas, lineBoxOrigin);
// Change the coordinate space to the line box (i.e. in textPainter
// coordinates), paint the guides and labels.
canvas.save();
canvas.translate(lineBoxOrigin.dx, lineBoxOrigin.dy);
final List<TextBox> boxes = _textPainter.getBoxesForSelection(
TextSelection(baseOffset: 0, extentOffset: text.text!.length),
);
assert(boxes.length == 1);
final Rect glyphsBox = boxes.first.toRect();
paint.strokeWidth = 7.5;
paint.color = Colors.blue[900]!;
final double guideWidth = max(
size.width - lineBoxOrigin.dx,
glyphsBox.right,
);
// Top guide
canvas.drawLine(Offset.zero, Offset(guideWidth, 0), paint);
// Bottom guide
canvas.drawLine(
Offset(0, _textPainter.height),
Offset(guideWidth, _textPainter.height),
paint,
);
paint.strokeWidth = 2.5;
paint.color = Colors.red[900]!;
// Glyph ascent guide
canvas.drawLine(
Offset(0, glyphsBox.top),
Offset(guideWidth, glyphsBox.top),
paint,
);
// Glyph descent guide
canvas.drawLine(
Offset(0, glyphsBox.bottom),
Offset(guideWidth, glyphsBox.bottom),
paint,
);
// Baseline
paint.strokeWidth = 2.5;
paint.color = Colors.black;
final double baseline = _textPainter.computeDistanceToActualBaseline(
TextBaseline.alphabetic,
);
canvas.drawLine(Offset(0, baseline), Offset(guideWidth, baseline), paint);
if (paintHeightIndicator) {
_doPaintHeightIndicator(canvas, _textPainter.size);
}
if (paintCaptions) {
_doPaintCaptions(canvas, _textPainter.size, glyphsBox, baseline);
}
if (paintLeadingIndicator) {
_doPaintLeadingIndicators(
canvas,
Size(guideWidth, _textPainter.height),
glyphsBox,
);
}
canvas.restore();
}
void _doPaintLeadingIndicators(Canvas canvas, Size size, Rect glyphsBox) {
final TextStyle textStyle = TextStyle(fontSize: 9, color: Colors.blue[900]);
final TextPainter labelPainter = TextPainter(
text: TextSpan(text: 'Top Leading', style: textStyle),
textAlign: TextAlign.right,
textDirection: textDirection,
);
final Paint paint = Paint();
paint.strokeWidth = 2;
paint.style = PaintingStyle.stroke;
paint.color = Colors.blue[900]!;
void drawPointedLine(Offset start, Offset end) {
final Path path = Path();
final double pointDirection = end.dy > start.dy ? -1 : 1;
const double arrowScale = 5;
final Offset arrowPoint1 = end + Offset(1, pointDirection) * arrowScale;
final Offset arrowPoint2 = end + Offset(-1, pointDirection) * arrowScale;
path.moveTo(end.dx, end.dy);
path.lineTo(arrowPoint1.dx, arrowPoint1.dy);
path.moveTo(end.dx, end.dy);
path.lineTo(arrowPoint2.dx, arrowPoint2.dy);
path.moveTo(end.dx, end.dy);
path.lineTo(start.dx, start.dy);
canvas.drawPath(path, paint);
}
// Top Leading Area
if (glyphsBox.top > 1) {
labelPainter.layout();
final Rect topLeadingArea = Offset.zero & Size(size.width, glyphsBox.top);
final Rect topTextRect = Rect.fromCenter(
center: topLeadingArea.center,
width: labelPainter.width,
height: labelPainter.height,
);
labelPainter.paint(canvas, topTextRect.topLeft);
drawPointedLine(
-fontMetricsLabelPadding + topTextRect.topCenter,
fontMetricsLabelPadding * 2 + topLeadingArea.topCenter,
);
drawPointedLine(
fontMetricsLabelPadding + topTextRect.bottomCenter,
-fontMetricsLabelPadding + topLeadingArea.bottomCenter,
);
}
if (glyphsBox.bottom >= size.height - 1) {
return;
}
// Bottom Leading Area
labelPainter.text = TextSpan(text: 'Bottom Leading', style: textStyle);
labelPainter.layout();
final Rect bottomLeadingArea =
Offset(0, glyphsBox.bottom) &
Size(size.width, size.height - glyphsBox.bottom);
final Rect bottomTextRect = Rect.fromCenter(
center: bottomLeadingArea.center,
width: labelPainter.width,
height: labelPainter.height,
);
labelPainter.paint(canvas, bottomTextRect.topLeft);
drawPointedLine(
-fontMetricsLabelPadding + bottomTextRect.topCenter,
fontMetricsLabelPadding + bottomLeadingArea.topCenter,
);
drawPointedLine(
fontMetricsLabelPadding + bottomTextRect.bottomCenter,
fontMetricsLabelPadding * -2 + bottomLeadingArea.bottomCenter,
);
}
void _doPaintHeightIndicator(Canvas canvas, Size size) {
final Paint paint = Paint();
// Height bracket & label
paint.strokeWidth = 2;
paint.style = PaintingStyle.stroke;
paint.color = Colors.blue[900]!;
final Path path = Path();
path.moveTo(-10, 0);
path.lineTo(heightCaptionBracketMinX, 0);
path.lineTo(heightCaptionBracketMinX, size.height);
path.lineTo(-10, size.height);
canvas.drawPath(path, paint);
// Layout is done in performLayout.
final Offset origin =