@@ -13,8 +13,6 @@ import 'package:flutter_layout_grid/flutter_layout_grid.dart';
13
13
/// Currently, nested tables are not supported.
14
14
///
15
15
///
16
- final RegExp _regExp = RegExp (r'[\w/\(\)]+' );
17
-
18
16
class TableHtmlExtension extends HtmlExtension {
19
17
const TableHtmlExtension ();
20
18
@@ -268,7 +266,6 @@ Widget _layoutCells(TableElement table, Map<StyledElement, InlineSpan> parsedCel
268
266
));
269
267
}
270
268
271
-
272
269
List <double > _getColWidths (List <StyledElement > children) {
273
270
final widths = < double > [];
274
271
for (final child in children) {
@@ -294,45 +291,58 @@ List<double> _getColWidths(List<StyledElement> children) {
294
291
List <double > _getColWidthsFromRow (TableRowLayoutElement row) {
295
292
List <double > widths = [];
296
293
for (final cell in row.children) {
297
- double minWidth = 0 ;
298
294
if (cell is TableCellElement ) {
299
- // Get entire text
300
- StringBuffer text = StringBuffer ();
295
+ WidthInfo info = WidthInfo ();
301
296
for (final child in cell.children) {
302
- text.write (_getText (child));
303
- }
304
-
305
- final words = _regExp.allMatches (text.toString ()).map ((match) => match.group (0 )! ).toList ();
306
- for (final word in words) {
307
- double wordWidth = TextPainter .computeWidth (
308
- text: TextSpan (
309
- text: word,
310
- style: TextStyle (
311
- fontSize: cell.style.fontSize? .value ?? 16 ,
312
- fontFamily: cell.style.fontFamily,
313
- fontWeight: cell.style.fontWeight,
314
- fontStyle: cell.style.fontStyle,
315
- )),
316
- textDirection: TextDirection .ltr,
317
- );
318
- if (wordWidth > minWidth) {
319
- minWidth = wordWidth;
320
- }
297
+ _getCellInfo (child, info);
321
298
}
299
+ double minWidth = info.requiredWidth + 32 ;
300
+ widths.add (minWidth);
322
301
}
323
- minWidth += 32 ;
324
- widths.add (minWidth);
325
302
}
326
303
return widths;
327
304
}
328
305
329
- String _getText (StyledElement element) {
330
- if (element is TextContentElement ) return element.text ?? '' ;
331
- StringBuffer buffer = StringBuffer ();
332
- for (final child in element.children) {
333
- buffer.write (_getText (child));
306
+ void _getCellInfo (StyledElement element, WidthInfo info) {
307
+ if (element is TextContentElement ) {
308
+ final regexp = RegExp (r'\s+|(?=[()/\-—])|(?<=[()/\-—])' );
309
+ final text = element.text;
310
+ if (text == null || text.isEmpty) return ;
311
+ final words = text.split (regexp).where ((word) => word.isNotEmpty).toList ();
312
+ for (final word in words) {
313
+ double wordWidth = TextPainter .computeWidth (
314
+ text: TextSpan (
315
+ text: word,
316
+ style: TextStyle (
317
+ fontSize: element.style.fontSize? .value ?? 16 ,
318
+ fontFamily: element.style.fontFamily,
319
+ fontWeight: element.style.fontWeight,
320
+ fontStyle: element.style.fontStyle,
321
+ )),
322
+ textDirection: TextDirection .ltr,
323
+ );
324
+ if (info.join && ! regexp.hasMatch (word[0 ])) {
325
+ info.width += wordWidth;
326
+ } else {
327
+ info.width = wordWidth;
328
+ }
329
+ if (info.width > info.requiredWidth) {
330
+ info.requiredWidth = info.width;
331
+ }
332
+ info.join = ! regexp.hasMatch (word[word.length - 1 ]);
333
+ }
334
+ info.join = ! regexp.hasMatch (text[text.length - 1 ]);
335
+ } else {
336
+ for (final child in element.children) {
337
+ _getCellInfo (child, info);
338
+ }
334
339
}
335
- return buffer.toString ();
340
+ }
341
+
342
+ class WidthInfo {
343
+ double width = 0 ;
344
+ double requiredWidth = 0 ;
345
+ bool join = false ;
336
346
}
337
347
338
348
Alignment _getCellAlignment (TableCellElement cell, TextDirection alignment) {
0 commit comments