From f386484e1c6c96a21ab5e7d3d141a30baa93a5d5 Mon Sep 17 00:00:00 2001 From: mabdc <747455334@qq.com> Date: Wed, 3 Mar 2021 15:55:19 +0800 Subject: [PATCH] fix --- example/lib/main.dart | 15 ++++++++++----- lib/text_composition.dart | 21 ++++++++++++++++++--- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 5c160e4..8dfa1b3 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -7,7 +7,7 @@ import 'package:text_composition/text_composition.dart'; import 'first_chapter.dart'; /// -/// 调试时修改161行 +/// 调试时修改`tc.getPageWidget(tc.pages[index], true, true),` /// * [useCanvas] 绘图方式(canvas/richText) [debug] 查看详细的布局输出 /// * 等宽字体 时 `richText`和`canvas`结果一致 /// * 非等宽字体 使用`canvas`才能两端对齐 [useCanvas] 应设为 [true] @@ -86,13 +86,18 @@ class _SettingState extends State { shouldJustifyHeight: shouldJustifyHeight, linkPattern: " - RegExp('src=".*/([^/]+)"').firstMatch(s)?.group(1) ?? "链接", - linkStyle: TextStyle(color: Colors.cyan, fontStyle: FontStyle.italic), + RegExp('(?<=src=".*)[^/\'"]+(?=[\'"])').stringMatch(s) ?? "链接", + linkStyle: TextStyle( + color: Colors.cyan, + fontStyle: FontStyle.italic, + fontSize: double.tryParse(size.text), + height: double.tryParse(height.text), + ), onLinkTap: (s) => Navigator.of(context).push(MaterialPageRoute( builder: (BuildContext context) => Scaffold( appBar: AppBar(), body: Image.network( - s.substring(s.indexOf("src=\"") + 5, s.lastIndexOf("\""))), + RegExp('(?<=src=")[^\'"]+').stringMatch(s) ?? ""), ))), ); end = DateTime.now(); @@ -158,7 +163,7 @@ class Page extends StatelessWidget { ), Column( children: [ - tc.getPageWidget(tc.pages[index], true, false), + tc.getPageWidget(tc.pages[index], true, true), Container( height: 20, width: tc.boxWidth, diff --git a/lib/text_composition.dart b/lib/text_composition.dart index 4dd2842..869814e 100644 --- a/lib/text_composition.dart +++ b/lib/text_composition.dart @@ -112,6 +112,8 @@ class TextComposition { for (var p in _paragraphs) { if (linkPattern != null && p.startsWith(linkPattern!)) { + tp.text = TextSpan(text: p, style: linkStyle); + tp.layout(); pageHeight += tp.height; lines.add(TextLine(text: p, link: true)); newParagraph(); @@ -123,7 +125,10 @@ class TextComposition { final textCount = tp.getPositionForOffset(offset).offset; if (p.length == textCount) { lines.add(TextLine( - text: p, width: tp.width, shouldJustifyWidth: tp.width > _boxWidth)); + text: p, + width: tp.width, + shouldJustifyWidth: tp.width > _boxWidth, + )); newParagraph(); break; } else { @@ -200,12 +205,22 @@ class TextComposition { /// * [useCanvas] 绘图方式(canvas/richText) [debug] 查看详细的布局输出 /// * 等宽字体 时 `richText`和`canvas`结果一致 - /// * 非等宽字体 使用`canvas`才能两端对齐 [useCanvas] 应设为 [true] + /// * 非等宽字体 使用`canvas`才能两端对齐 [useCanvas] 应设为 [true] Widget getPageWidget(TextPage page, [bool useCanvas = true, bool debug = false]) { print("****** [TextComposition page start] [${DateTime.now()}] ******"); final ts = TextSpan(style: style, children: getPageSpans(page)); - + if (debug) { + var paragraphJustifyHeight = paragraph.toDouble(); + var restJustifyHeight = 0; + if (shouldJustifyHeight && page.shouldJustifyHeight) { + final rest = boxHeight.floor() - page.height.floor(); + restJustifyHeight = rest % page.paragraphCount; + paragraphJustifyHeight += rest ~/ page.paragraphCount; + } + print( + "paragraphJustifyHeight $paragraphJustifyHeight restJustifyHeight $restJustifyHeight"); + final tp = TextPainter(text: ts, textDirection: TextDirection.ltr); var lastLineHeight = 0; print("序号 行高 累计 宽度 内容");