ilja
Committed by David PHAM-VAN

Add support for hyphenation

@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 - Add Flutter's Logical Pixel constant 5 - Add Flutter's Logical Pixel constant
6 - Add support for existing reference objects 6 - Add support for existing reference objects
7 - Update barcode golden pdf 7 - Update barcode golden pdf
  8 +- Add support for hyphenation [ilja]
8 9
9 ## 3.10.7 10 ## 3.10.7
10 11
@@ -658,6 +658,8 @@ class RichTextContext extends WidgetContext { @@ -658,6 +658,8 @@ class RichTextContext extends WidgetContext {
658 '$runtimeType Offset: $startOffset -> $endOffset Span: $spanStart -> $spanEnd'; 658 '$runtimeType Offset: $startOffset -> $endOffset Span: $spanStart -> $spanEnd';
659 } 659 }
660 660
  661 +typedef Hyphenation = List<String> Function(String word);
  662 +
661 class RichText extends Widget with SpanningWidget { 663 class RichText extends Widget with SpanningWidget {
662 RichText({ 664 RichText({
663 required this.text, 665 required this.text,
@@ -668,6 +670,7 @@ class RichText extends Widget with SpanningWidget { @@ -668,6 +670,7 @@ class RichText extends Widget with SpanningWidget {
668 this.textScaleFactor = 1.0, 670 this.textScaleFactor = 1.0,
669 this.maxLines, 671 this.maxLines,
670 this.overflow = TextOverflow.visible, 672 this.overflow = TextOverflow.visible,
  673 + this.hyphenation,
671 }); 674 });
672 675
673 static bool debug = false; 676 static bool debug = false;
@@ -700,6 +703,8 @@ class RichText extends Widget with SpanningWidget { @@ -700,6 +703,8 @@ class RichText extends Widget with SpanningWidget {
700 703
701 List<InlineSpan>? _preprocessed; 704 List<InlineSpan>? _preprocessed;
702 705
  706 + final Hyphenation? hyphenation;
  707 +
703 void _appendDecoration(bool append, _TextDecoration td) { 708 void _appendDecoration(bool append, _TextDecoration td) {
704 if (append && _decorations.isNotEmpty) { 709 if (append && _decorations.isNotEmpty) {
705 final last = _decorations.last; 710 final last = _decorations.last;
@@ -946,6 +951,32 @@ class RichText extends Widget with SpanningWidget { @@ -946,6 +951,32 @@ class RichText extends Widget with SpanningWidget {
946 951
947 if (_softWrap && 952 if (_softWrap &&
948 offsetX + metrics.width > constraintWidth + 0.00001) { 953 offsetX + metrics.width > constraintWidth + 0.00001) {
  954 + if (hyphenation != null) {
  955 + final syllables = hyphenation!(word);
  956 + if (syllables.length > 1) {
  957 + var fits = '';
  958 + for (var syllable in syllables) {
  959 + if (offsetX +
  960 + ((font.stringMetrics('$fits$syllable-',
  961 + letterSpacing: style.letterSpacing! /
  962 + (style.fontSize! *
  963 + textScaleFactor)) *
  964 + (style.fontSize! * textScaleFactor))
  965 + .width) >
  966 + constraintWidth + 0.00001) {
  967 + break;
  968 + }
  969 + fits += syllable;
  970 + }
  971 + if (fits.isNotEmpty) {
  972 + words[index] = '$fits-';
  973 + words.insert(index + 1, word.substring(fits.length));
  974 + index--;
  975 + continue;
  976 + }
  977 + }
  978 + }
  979 +
949 if (spanCount > 0 && metrics.width <= constraintWidth) { 980 if (spanCount > 0 && metrics.width <= constraintWidth) {
950 overflow = true; 981 overflow = true;
951 lines.add(_Line( 982 lines.add(_Line(