fix(export): 纯白背景 + 2x 像素比渲染(3840×2160),导出更清晰

- 导出专用令牌 exportTokens:无论屏幕主题深浅,SVG/PNG 背景均为纯白、文字深色(印刷/工艺卡导向)

- PNG 按 2 倍像素比渲染:1920×1080 逻辑画布 → 3840×2160 像素,矢量内容在高分屏上锐利

- SVG 背景 rect 同步纯白;export_test 校验尺寸 3840×2160 与左上角纯白像素

- 重建 dist 与签名 APK
This commit is contained in:
admin
2026-10-07 09:44:33 +08:00
parent c1d9f27952
commit 2b04705ba8
4 changed files with 91 additions and 17 deletions
+54 -1
View File
@@ -16,10 +16,14 @@ import '../layout/layout.dart';
import '../model/models.dart'; import '../model/models.dart';
import '../model/units.dart'; import '../model/units.dart';
import '../render/bar_painter.dart'; import '../render/bar_painter.dart';
import '../theme/tokens.dart';
/// 导出画布尺寸(固定)。 /// 导出画布尺寸(固定,逻辑单位)。
const Size kExportSize = Size(1920, 1080); const Size kExportSize = Size(1920, 1080);
/// 导出渲染像素比:按 2 倍像素渲染(3840×2160),保证高分辨率屏幕下清晰锐利。
const double kExportPixelRatio = 2.0;
/// 内容区(标题下方、图例上方)。 /// 内容区(标题下方、图例上方)。
const Rect kExportContentArea = Rect.fromLTRB(48, 128, 1872, 1016); const Rect kExportContentArea = Rect.fromLTRB(48, 128, 1872, 1016);
@@ -79,3 +83,52 @@ String exportSubtitle(Project project, ViewMode mode) {
/// 导出图例(左下角固定)。 /// 导出图例(左下角固定)。
String exportLegend(Project project) => String exportLegend(Project project) =>
'时序图设计器 · ${project.name} · 循环 ${tfmt(project.cycle.durationMs, project.units)} = 360°'; '时序图设计器 · ${project.name} · 循环 ${tfmt(project.cycle.durationMs, project.units)} = 360°';
/// 导出专用令牌:始终使用浅色、**纯白背景**(印刷 / 工艺卡导向),
/// 文字、语义色与屏幕浅色主题保持一致。
///
/// 无论当前屏幕主题是深是浅,导出的 PNG / SVG 背景都是纯白、文字深色,
/// 避免「导出背景不是纯白」的问题。
Tokens exportTokens(Tokens base) {
return Tokens(
brightness: Brightness.light,
bg: Colors.white,
canvas1: Colors.white,
canvas2: Colors.white,
chrome: base.chrome,
panel: Colors.white,
panel2: Colors.white,
dialog: Colors.white,
line: base.line,
line2: base.line2,
text: base.text,
text2: base.text2,
sub: base.sub,
dim: base.dim,
accent: base.accent,
accent2: base.accent2,
ok: base.ok,
warn: base.warn,
danger: base.danger,
fs: base.fs,
ss: base.ss,
crit: base.crit,
chip: Colors.white,
dial: Colors.white,
arcText: base.arcText,
onAccent: base.onAccent,
ringA: base.ringA,
ringB: base.ringB,
ringEdge: base.ringEdge,
ringLabel: base.ringLabel,
info: Colors.white,
track: base.track,
sel: base.sel,
grid: base.grid,
shadow: base.shadow,
shadowOp: 0.10,
selOutline: base.selOutline,
overlay: base.overlay,
tailOp: base.tailOp,
);
}
+18 -9
View File
@@ -21,28 +21,35 @@ import 'export_fit.dart';
/// ///
/// 参数: /// 参数:
/// - [project]:数据源; /// - [project]:数据源;
/// - [tokens]:配色令牌; /// - [tokens]:配色令牌(内部转为导出专用纯白主题);
/// - [criticalActions]/[criticalDeps]:关键路径高亮(与屏幕一致)。 /// - [criticalActions]/[criticalDeps]:关键路径高亮(与屏幕一致)。
///
/// 输出:3840×2160 像素(1920×1080 逻辑画布 × 2 倍像素比),
/// 保证平板 / 2K / 4K 屏幕上查看清晰锐利。
Future<Uint8List> renderExportPng({ Future<Uint8List> renderExportPng({
required Project project, required Project project,
required Tokens tokens, required Tokens tokens,
required Set<String> criticalActions, required Set<String> criticalActions,
required Set<String> criticalDeps, required Set<String> criticalDeps,
}) async { }) async {
// 导出专用令牌:纯白背景、浅色文字。
final Tokens et = exportTokens(tokens);
final ui.PictureRecorder recorder = ui.PictureRecorder(); final ui.PictureRecorder recorder = ui.PictureRecorder();
final Canvas canvas = Canvas(recorder); final Canvas canvas = Canvas(recorder);
final double w = kExportSize.width; final double w = kExportSize.width;
final double h = kExportSize.height; final double h = kExportSize.height;
// 2 倍像素比:整体放大绘制,矢量内容保持锐利。
canvas.scale(kExportPixelRatio);
// 背景铺满。 // 背景铺满(纯白)。
canvas.drawRect(Rect.fromLTWH(0, 0, w, h), Paint()..color = tokens.bg); canvas.drawRect(Rect.fromLTWH(0, 0, w, h), Paint()..color = et.bg);
// 标题:固定左上角。 // 标题:固定左上角。
final ViewMode mode = project.viewState.mode; final ViewMode mode = project.viewState.mode;
_text(canvas, exportTitle(mode), const Offset(48, 36), _text(canvas, exportTitle(mode), const Offset(48, 36),
color: tokens.text, size: 34, weight: FontWeight.w700); color: et.text, size: 34, weight: FontWeight.w700);
_text(canvas, exportSubtitle(project, mode), const Offset(48, 88), _text(canvas, exportSubtitle(project, mode), const Offset(48, 88),
color: tokens.sub, size: 16, weight: FontWeight.w400); color: et.sub, size: 16, weight: FontWeight.w400);
// 内容:适配缩放并居中到内容区。 // 内容:适配缩放并居中到内容区。
final ExportFit fit = computeExportFit(project, mode, kExportContentArea); final ExportFit fit = computeExportFit(project, mode, kExportContentArea);
@@ -52,7 +59,7 @@ Future<Uint8List> renderExportPng({
if (mode == ViewMode.bar) { if (mode == ViewMode.bar) {
BarPainter( BarPainter(
project: project, project: project,
tokens: tokens, tokens: et,
criticalActions: criticalActions, criticalActions: criticalActions,
criticalDeps: criticalDeps, criticalDeps: criticalDeps,
selectedIds: const <String>{}, selectedIds: const <String>{},
@@ -60,7 +67,7 @@ Future<Uint8List> renderExportPng({
} else { } else {
CircularPainter( CircularPainter(
project: project, project: project,
tokens: tokens, tokens: et,
criticalActions: criticalActions, criticalActions: criticalActions,
criticalDeps: criticalDeps, criticalDeps: criticalDeps,
selectedIds: const <String>{}, selectedIds: const <String>{},
@@ -70,9 +77,11 @@ Future<Uint8List> renderExportPng({
// 图例:固定左下角。 // 图例:固定左下角。
_text(canvas, exportLegend(project), Offset(48, h - 38), _text(canvas, exportLegend(project), Offset(48, h - 38),
color: tokens.sub, size: 15, weight: FontWeight.w400); color: et.sub, size: 15, weight: FontWeight.w400);
final ui.Image image = await recorder.endRecording().toImage(w.toInt(), h.toInt()); final ui.Image image = await recorder
.endRecording()
.toImage((w * kExportPixelRatio).toInt(), (h * kExportPixelRatio).toInt());
final ByteData? data = await image.toByteData(format: ui.ImageByteFormat.png); final ByteData? data = await image.toByteData(format: ui.ImageByteFormat.png);
return data!.buffer.asUint8List(); return data!.buffer.asUint8List();
} }
+4 -1
View File
@@ -38,7 +38,10 @@ class SvgWriter {
/// - [mode]:视图模式,决定生成条形还是圆形布局。 /// - [mode]:视图模式,决定生成条形还是圆形布局。
/// ///
/// 构造时不生成任何内容;调用 [build] 才写出 SVG。 /// 构造时不生成任何内容;调用 [build] 才写出 SVG。
SvgWriter({required this.project, required this.tokens, required this.mode}); ///
/// [tokens] 会经 [exportTokens] 转为导出专用令牌(纯白背景、浅色文字)。
SvgWriter({required this.project, required Tokens tokens, required this.mode})
: tokens = exportTokens(tokens);
/// 数据源工程。 /// 数据源工程。
final Project project; final Project project;
+15 -6
View File
@@ -12,7 +12,7 @@ import 'package:sequence_diagram/theme/tokens.dart';
import 'helpers.dart'; import 'helpers.dart';
void main() { void main() {
test('svg export: fixed 1920x1080 + title + fit transform + legend', () { test('svg export: fixed 1920x1080 + white bg + title + fit transform + legend', () {
final Project p = demoTiming(); final Project p = demoTiming();
final String svg = SvgWriter(project: p, tokens: Tokens.light, mode: ViewMode.bar).build(); final String svg = SvgWriter(project: p, tokens: Tokens.light, mode: ViewMode.bar).build();
expect(svg, contains('width="1920" height="1080" viewBox="0 0 1920 1080"')); expect(svg, contains('width="1920" height="1080" viewBox="0 0 1920 1080"'));
@@ -21,24 +21,33 @@ void main() {
expect(svg, contains('<g id="content" transform="')); expect(svg, contains('<g id="content" transform="'));
expect(svg, contains('<g id="legend">')); expect(svg, contains('<g id="legend">'));
expect(svg, contains('装配线节拍')); expect(svg, contains('装配线节拍'));
// 背景纯白。
expect(svg, contains('fill="#ffffff"'));
final String circ = SvgWriter(project: p, tokens: Tokens.light, mode: ViewMode.circular).build(); final String circ = SvgWriter(project: p, tokens: Tokens.dark, mode: ViewMode.circular).build();
expect(circ, contains('圆周时序视图')); expect(circ, contains('圆周时序视图'));
expect(circ, contains('width="1920" height="1080"')); expect(circ, contains('width="1920" height="1080"'));
expect(circ, contains('fill="#ffffff"'));
}); });
test('png export: renders exactly 1920x1080', () async { test('png export: 3840x2160 pixels (2x) with pure white background', () async {
final Project p = demoTiming(); final Project p = demoTiming();
final Uint8List bytes = await renderExportPng( final Uint8List bytes = await renderExportPng(
project: p, project: p,
tokens: Tokens.light, tokens: Tokens.dark,
criticalActions: const <String>{'A1', 'A2'}, criticalActions: const <String>{'A1', 'A2'},
criticalDeps: const <String>{'A1->A2'}, criticalDeps: const <String>{'A1->A2'},
); );
final ui.ImmutableBuffer buf = await ui.ImmutableBuffer.fromUint8List(bytes); final ui.ImmutableBuffer buf = await ui.ImmutableBuffer.fromUint8List(bytes);
final ui.ImageDescriptor desc = await ui.ImageDescriptor.encoded(buf); final ui.ImageDescriptor desc = await ui.ImageDescriptor.encoded(buf);
expect(desc.width, 1920); expect(desc.width, 3840);
expect(desc.height, 1080); expect(desc.height, 2160);
// 左上角像素应为纯白。
final ui.Codec codec = await desc.instantiateCodec();
final ui.FrameInfo frame = await codec.getNextFrame();
final ByteData? rgba = await frame.image.toByteData(format: ui.ImageByteFormat.rawRgba);
final int first = rgba!.getUint32(0);
expect(first & 0xFFFFFF, 0xFFFFFF);
File('build/export_preview.png').writeAsBytesSync(bytes); File('build/export_preview.png').writeAsBytesSync(bytes);
}); });
} }