David PHAM-VAN

Implement properly RichText.softWrap

@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 * Add SizedBox Widget 5 * Add SizedBox Widget
6 * Fix RichText Widget word spacing 6 * Fix RichText Widget word spacing
7 * Improve Theme and TextStyle 7 * Improve Theme and TextStyle
  8 +* Implement properly RichText.softWrap
8 9
9 # 1.3.7 10 # 1.3.7
10 * Add Pdf Creation date 11 * Add Pdf Creation date
@@ -98,11 +98,10 @@ class RichText extends Widget { @@ -98,11 +98,10 @@ class RichText extends Widget {
98 RichText( 98 RichText(
99 {@required this.text, 99 {@required this.text,
100 this.textAlign = TextAlign.left, 100 this.textAlign = TextAlign.left,
101 - bool softWrap = true, 101 + this.softWrap = true,
102 this.textScaleFactor = 1.0, 102 this.textScaleFactor = 1.0,
103 - int maxLines})  
104 - : maxLines = !softWrap ? 1 : maxLines,  
105 - assert(text != null); 103 + this.maxLines})
  104 + : assert(text != null);
106 105
107 static const bool debug = false; 106 static const bool debug = false;
108 107
@@ -112,6 +111,8 @@ class RichText extends Widget { @@ -112,6 +111,8 @@ class RichText extends Widget {
112 111
113 final double textScaleFactor; 112 final double textScaleFactor;
114 113
  114 + final bool softWrap;
  115 +
115 final int maxLines; 116 final int maxLines;
116 117
117 final List<_Word> _words = <_Word>[]; 118 final List<_Word> _words = <_Word>[];
@@ -183,16 +184,57 @@ class RichText extends Widget { @@ -183,16 +184,57 @@ class RichText extends Widget {
183 final PdfFontMetrics space = 184 final PdfFontMetrics space =
184 font.stringMetrics(' ') * (style.fontSize * textScaleFactor); 185 font.stringMetrics(' ') * (style.fontSize * textScaleFactor);
185 186
186 - for (String word in span.text.split(RegExp(r'\s'))) {  
187 - if (word.isEmpty) {  
188 - offsetX += space.advanceWidth * style.wordSpacing;  
189 - continue;  
190 - } 187 + final List<String> spanLines = span.text.split('\n');
  188 + for (int line = 0; line < spanLines.length; line++) {
  189 + for (String word in spanLines[line].split(RegExp(r'\s'))) {
  190 + if (word.isEmpty) {
  191 + offsetX += space.advanceWidth * style.wordSpacing;
  192 + continue;
  193 + }
  194 +
  195 + final PdfFontMetrics metrics =
  196 + font.stringMetrics(word) * (style.fontSize * textScaleFactor);
  197 +
  198 + if (offsetX + metrics.width > constraintWidth && wCount > 0) {
  199 + width = math.max(
  200 + width,
  201 + _realignLine(
  202 + _words.sublist(lineStart),
  203 + constraintWidth,
  204 + offsetX - space.advanceWidth * style.wordSpacing,
  205 + false,
  206 + bottom));
  207 +
  208 + lineStart += wCount;
  209 +
  210 + if (maxLines != null && ++lines > maxLines) {
  211 + break;
  212 + }
  213 +
  214 + offsetX = 0.0;
  215 + offsetY += bottom - top + style.lineSpacing;
  216 + top = null;
  217 + bottom = null;
  218 +
  219 + if (offsetY > constraintHeight) {
  220 + return false;
  221 + }
  222 + wCount = 0;
  223 + }
  224 +
  225 + top = math.min(top ?? metrics.top, metrics.top);
  226 + bottom = math.max(bottom ?? metrics.bottom, metrics.bottom);
  227 +
  228 + final _Word wd = _Word(word, style, metrics, span.annotation);
  229 + wd.offset = PdfPoint(offsetX, -offsetY);
191 230
192 - final PdfFontMetrics metrics =  
193 - font.stringMetrics(word) * (style.fontSize * textScaleFactor); 231 + _words.add(wd);
  232 + wCount++;
  233 + offsetX +=
  234 + metrics.advanceWidth + space.advanceWidth * style.wordSpacing;
  235 + }
194 236
195 - if (offsetX + metrics.width > constraintWidth && wCount > 0) { 237 + if (softWrap && line < spanLines.length - 1) {
196 width = math.max( 238 width = math.max(
197 width, 239 width,
198 _realignLine( 240 _realignLine(
@@ -201,13 +243,19 @@ class RichText extends Widget { @@ -201,13 +243,19 @@ class RichText extends Widget {
201 offsetX - space.advanceWidth * style.wordSpacing, 243 offsetX - space.advanceWidth * style.wordSpacing,
202 false, 244 false,
203 bottom)); 245 bottom));
  246 +
204 lineStart += wCount; 247 lineStart += wCount;
  248 +
205 if (maxLines != null && ++lines > maxLines) { 249 if (maxLines != null && ++lines > maxLines) {
206 break; 250 break;
207 } 251 }
208 252
209 offsetX = 0.0; 253 offsetX = 0.0;
210 - offsetY += bottom - top + style.lineSpacing; 254 + if (wCount > 0) {
  255 + offsetY += bottom - top + style.lineSpacing;
  256 + } else {
  257 + offsetY += space.ascent + space.descent + style.lineSpacing;
  258 + }
211 top = null; 259 top = null;
212 bottom = null; 260 bottom = null;
213 261
@@ -216,17 +264,6 @@ class RichText extends Widget { @@ -216,17 +264,6 @@ class RichText extends Widget {
216 } 264 }
217 wCount = 0; 265 wCount = 0;
218 } 266 }
219 -  
220 - top = math.min(top ?? metrics.top, metrics.top);  
221 - bottom = math.max(bottom ?? metrics.bottom, metrics.bottom);  
222 -  
223 - final _Word wd = _Word(word, style, metrics, span.annotation);  
224 - wd.offset = PdfPoint(offsetX, -offsetY);  
225 -  
226 - _words.add(wd);  
227 - wCount++;  
228 - offsetX +=  
229 - metrics.advanceWidth + space.advanceWidth * style.wordSpacing;  
230 } 267 }
231 268
232 offsetX -= space.advanceWidth * style.wordSpacing; 269 offsetX -= space.advanceWidth * style.wordSpacing;