Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mabDc committed Mar 3, 2021
1 parent ec2ce30 commit f386484
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
15 changes: 10 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -86,13 +86,18 @@ class _SettingState extends State<Setting> {
shouldJustifyHeight: shouldJustifyHeight,
linkPattern: "<img",
linkText: (s) =>
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();
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 18 additions & 3 deletions lib/text_composition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 {
Expand Down Expand Up @@ -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("序号 行高 累计 宽度 内容");
Expand Down

0 comments on commit f386484

Please sign in to comment.