David PHAM-VAN

Fix text justify with multiple paragraphs

1 # Changelog 1 # Changelog
2 2
  3 +## 3.6.0
  4 +
  5 +- Fix text justify with multiple paragraphs
  6 +
3 ## 3.5.0 7 ## 3.5.0
4 8
5 - Add annotations [John Harris] 9 - Add annotations [John Harris]
@@ -483,6 +483,7 @@ class _Line { @@ -483,6 +483,7 @@ class _Line {
483 this.baseline, 483 this.baseline,
484 this.wordsWidth, 484 this.wordsWidth,
485 this.textDirection, 485 this.textDirection,
  486 + this.justify,
486 ); 487 );
487 488
488 final RichText parent; 489 final RichText parent;
@@ -499,6 +500,8 @@ class _Line { @@ -499,6 +500,8 @@ class _Line {
499 500
500 final TextDirection textDirection; 501 final TextDirection textDirection;
501 502
  503 + final bool justify;
  504 +
502 double get height => parent._spans 505 double get height => parent._spans
503 .sublist(firstSpan, lastSpan) 506 .sublist(firstSpan, lastSpan)
504 .reduce((a, b) => a.height > b.height ? a : b) 507 .reduce((a, b) => a.height > b.height ? a : b)
@@ -508,7 +511,7 @@ class _Line { @@ -508,7 +511,7 @@ class _Line {
508 String toString() => 511 String toString() =>
509 '$runtimeType $firstSpan-$lastSpan baseline: $baseline width:$wordsWidth'; 512 '$runtimeType $firstSpan-$lastSpan baseline: $baseline width:$wordsWidth';
510 513
511 - void realign(double totalWidth, bool isLast) { 514 + void realign(double totalWidth) {
512 final spans = parent._spans.sublist(firstSpan, lastSpan); 515 final spans = parent._spans.sublist(firstSpan, lastSpan);
513 516
514 var delta = 0.0; 517 var delta = 0.0;
@@ -522,7 +525,7 @@ class _Line { @@ -522,7 +525,7 @@ class _Line {
522 delta = (totalWidth - wordsWidth) / 2.0; 525 delta = (totalWidth - wordsWidth) / 2.0;
523 break; 526 break;
524 case TextAlign.justify: 527 case TextAlign.justify:
525 - if (isLast) { 528 + if (!justify) {
526 totalWidth = wordsWidth; 529 totalWidth = wordsWidth;
527 break; 530 break;
528 } 531 }
@@ -712,6 +715,7 @@ class RichText extends Widget with SpanningWidget { @@ -712,6 +715,7 @@ class RichText extends Widget with SpanningWidget {
712 space.advanceWidth * style.wordSpacing! - 715 space.advanceWidth * style.wordSpacing! -
713 style.letterSpacing!, 716 style.letterSpacing!,
714 _textDirection, 717 _textDirection,
  718 + true,
715 )); 719 ));
716 720
717 spanStart += spanCount; 721 spanStart += spanCount;
@@ -785,7 +789,9 @@ class RichText extends Widget with SpanningWidget { @@ -785,7 +789,9 @@ class RichText extends Widget with SpanningWidget {
785 offsetX - 789 offsetX -
786 space.advanceWidth * style.wordSpacing! - 790 space.advanceWidth * style.wordSpacing! -
787 style.letterSpacing!, 791 style.letterSpacing!,
788 - _textDirection)); 792 + _textDirection,
  793 + false,
  794 + ));
789 795
790 spanStart += spanCount; 796 spanStart += spanCount;
791 797
@@ -834,6 +840,7 @@ class RichText extends Widget with SpanningWidget { @@ -834,6 +840,7 @@ class RichText extends Widget with SpanningWidget {
834 bottom, 840 bottom,
835 offsetX, 841 offsetX,
836 _textDirection, 842 _textDirection,
  843 + true,
837 )); 844 ));
838 845
839 spanStart += spanCount; 846 spanStart += spanCount;
@@ -890,6 +897,7 @@ class RichText extends Widget with SpanningWidget { @@ -890,6 +897,7 @@ class RichText extends Widget with SpanningWidget {
890 bottom, 897 bottom,
891 offsetX, 898 offsetX,
892 _textDirection, 899 _textDirection,
  900 + false,
893 )); 901 ));
894 offsetY += bottom - top; 902 offsetY += bottom - top;
895 } 903 }
@@ -906,10 +914,9 @@ class RichText extends Widget with SpanningWidget { @@ -906,10 +914,9 @@ class RichText extends Widget with SpanningWidget {
906 } 914 }
907 915
908 // Realign all the lines 916 // Realign all the lines
909 - for (final line in lines.sublist(0, lines.length - 1)) {  
910 - line.realign(width, false); 917 + for (final line in lines) {
  918 + line.realign(width);
911 } 919 }
912 - lines.last.realign(width, true);  
913 } 920 }
914 921
915 box = PdfRect(0, 0, constraints.constrainWidth(width), 922 box = PdfRect(0, 0, constraints.constrainWidth(width),
@@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf 4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
5 repository: https://github.com/DavBfr/dart_pdf 5 repository: https://github.com/DavBfr/dart_pdf
6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues 6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues
7 -version: 3.5.0 7 +version: 3.6.0
8 8
9 environment: 9 environment:
10 sdk: ">=2.12.0 <3.0.0" 10 sdk: ">=2.12.0 <3.0.0"
@@ -340,6 +340,23 @@ void main() { @@ -340,6 +340,23 @@ void main() {
340 ); 340 );
341 }); 341 });
342 342
  343 + test('Text Widgets Justify multiple paragraphs', () {
  344 + const para =
  345 + 'This is the first paragraph with a small nice text.\nHere is a new line.\nAnother one.\nAnd finally a long paragraph to finish this test with a three lines text that finishes well.';
  346 +
  347 + pdf.addPage(
  348 + Page(
  349 + build: (Context context) => SizedBox(
  350 + width: 200,
  351 + child: Text(
  352 + para,
  353 + textAlign: TextAlign.justify,
  354 + ),
  355 + ),
  356 + ),
  357 + );
  358 + });
  359 +
343 tearDownAll(() async { 360 tearDownAll(() async {
344 final file = File('widgets-text.pdf'); 361 final file = File('widgets-text.pdf');
345 await file.writeAsBytes(await pdf.save()); 362 await file.writeAsBytes(await pdf.save());