Skip to content

Commit

Permalink
优化;兼容 非等高 非等宽 字体
Browse files Browse the repository at this point in the history
  • Loading branch information
mabDc committed Mar 3, 2021
1 parent c5ec527 commit ec2ce30
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 112 deletions.
51 changes: 25 additions & 26 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import 'package:text_composition/text_composition.dart';

import 'first_chapter.dart';


///
/// 调试时修改162行查看详细的布局输出和绘图方式
/// `tc.getPageWidget(tc.pages[index], true, true/false),`
/// 调试时修改161行
/// * [useCanvas] 绘图方式(canvas/richText) [debug] 查看详细的布局输出
/// * 等宽字体 时 `richText``canvas`结果一致
/// * 非等宽字体 使用`canvas`才能两端对齐 [useCanvas] 应设为 [true]
///
main(List<String> args) {
runApp(MaterialApp(home: Setting()));
Expand All @@ -23,7 +24,10 @@ class Setting extends StatefulWidget {
}

class _SettingState extends State<Setting> {
var size = TextEditingController(text: '16'),
var pwidth = window.physicalSize.width,
pheight = window.physicalSize.height,
ratio = window.devicePixelRatio,
size = TextEditingController(text: '16'),
height = TextEditingController(text: '1.55'),
paragraph = TextEditingController(text: '10'),
shouldJustifyHeight = true,
Expand All @@ -32,19 +36,12 @@ class _SettingState extends State<Setting> {
TextComposition? tc;
@override
Widget build(BuildContext context) {
final pwidth = window.physicalSize.width;
final pheight = window.physicalSize.height;
final ratio = window.devicePixelRatio;
return Scaffold(
body: ListView(
padding: EdgeInsets.all(30),
children: [
Text("width / ratio: $pwidth / $ratio = ${pwidth / ratio}"),
Text("height / ratio: $pheight / $ratio = ${pheight / ratio}"),
OutlinedButton(
child: Text("重新获取屏幕大小"),
onPressed: () => setState(() {}),
),
TextField(
decoration: InputDecoration(labelText: "字号 size"),
controller: size,
Expand Down Expand Up @@ -73,11 +70,13 @@ class _SettingState extends State<Setting> {
OutlinedButton(
child: Text("开始或重新排版"),
onPressed: () {
pwidth = window.physicalSize.width;
pheight = window.physicalSize.height;
ratio = window.devicePixelRatio;
start = DateTime.now();
tc = TextComposition(
paragraphs: first_chapter,
style: TextStyle(
fontFamily: "jiali",
fontSize: double.tryParse(size.text),
height: double.tryParse(height.text),
),
Expand All @@ -88,8 +87,7 @@ class _SettingState extends State<Setting> {
linkPattern: "<img",
linkText: (s) =>
RegExp('src=".*/([^/]+)"').firstMatch(s)?.group(1) ?? "链接",
linkStyle: TextStyle(
color: Colors.cyan, fontStyle: FontStyle.italic, fontFamily: "jiali"),
linkStyle: TextStyle(color: Colors.cyan, fontStyle: FontStyle.italic),
onLinkTap: (s) => Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => Scaffold(
appBar: AppBar(),
Expand Down Expand Up @@ -144,22 +142,23 @@ class Page extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Column(
children: List.generate(
tc.boxHeight ~/ 10 + 2,
(index) => Container(
height: 10,
width: 20,
color: index % 2 == 0 ?null : Colors.cyan ,
child: Text(
index % 2 == 0 ? "" : ((index + 1) * 10).toString() ,
textAlign: TextAlign.end,
style: TextStyle(color: Colors.white, fontSize: 8),
children: List.generate(
tc.boxHeight ~/ 10 + 2,
(index) => Container(
height: 10,
width: 20,
color: index % 2 == 0 ? null : Colors.cyan,
child: Text(
index % 2 == 0 ? "" : ((index + 1) * 10).toString(),
textAlign: TextAlign.end,
style: TextStyle(color: Colors.white, fontSize: 10, height: 1),
),
),
),
)),
),
Column(
children: [
tc.getPageWidget(tc.pages[index], false, false),
tc.getPageWidget(tc.pages[index], true, false),
Container(
height: 20,
width: tc.boxWidth,
Expand Down
38 changes: 19 additions & 19 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,49 @@ packages:
name: async
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.5.0-nullsafety.3"
version: "2.5.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0-nullsafety.3"
version: "2.1.0"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0-nullsafety.5"
version: "1.1.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0-nullsafety.3"
version: "1.2.0"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0-nullsafety.3"
version: "1.1.0"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.15.0-nullsafety.5"
version: "1.15.0"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0-nullsafety.3"
version: "1.2.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -66,21 +66,21 @@ packages:
name: matcher
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.12.10-nullsafety.3"
version: "0.12.10"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.0-nullsafety.6"
version: "1.3.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.8.0-nullsafety.3"
version: "1.8.0"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -92,63 +92,63 @@ packages:
name: source_span
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.8.0-nullsafety.4"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.10.0-nullsafety.6"
version: "1.10.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0-nullsafety.3"
version: "2.1.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0-nullsafety.3"
version: "1.1.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0-nullsafety.3"
version: "1.2.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.2.19-nullsafety.6"
version: "0.2.19"
text_composition:
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "0.0.1"
version: "0.0.2"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.0-nullsafety.5"
version: "1.3.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0-nullsafety.5"
version: "2.1.0"
sdks:
dart: ">=2.12.0-0.0 <3.0.0"
flutter: ">=1.17.0"
5 changes: 0 additions & 5 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,3 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
fonts:
- family: jiali
fonts:
- asset: 腾祥嘉丽.ttf
1 change: 1 addition & 0 deletions example/windows/flutter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ add_custom_command(
${FLUTTER_TOOL_ENVIRONMENT}
"${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.bat"
windows-x64 $<CONFIG>
VERBATIM
)
add_custom_target(flutter_assemble DEPENDS
"${FLUTTER_LIBRARY}"
Expand Down
Binary file removed example/腾祥嘉丽.ttf
Binary file not shown.
Loading

0 comments on commit ec2ce30

Please sign in to comment.