2026-10-07 09:27:45 +08:00
|
|
|
|
/// PNG 导出渲染器:把时序图渲染为固定 1920×1080 的位图。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 与 SVG 导出共用同一套适配计算([computeExportFit]),保证两种格式几何一致:
|
|
|
|
|
|
/// - 背景铺满 1920×1080;
|
|
|
|
|
|
/// - 标题固定在左上角、图例固定在左下角;
|
|
|
|
|
|
/// - 内容(条形 / 圆周)按自身包围盒等比缩放并居中到内容区。
|
|
|
|
|
|
library;
|
|
|
|
|
|
|
|
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
import 'dart:ui' as ui;
|
|
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart' hide Action;
|
|
|
|
|
|
|
2026-11-22 10:02:48 +08:00
|
|
|
|
import 'package:sequence_diagram/model/models.dart';
|
|
|
|
|
|
import 'package:sequence_diagram/modules/sequence/render/bar_painter.dart';
|
|
|
|
|
|
import 'package:sequence_diagram/modules/sequence/render/circular_painter.dart';
|
|
|
|
|
|
import 'package:sequence_diagram/modules/sequence/render/flow_painter.dart';
|
|
|
|
|
|
import 'package:sequence_diagram/theme/tokens.dart';
|
2026-10-07 09:27:45 +08:00
|
|
|
|
import 'export_fit.dart';
|
|
|
|
|
|
|
|
|
|
|
|
/// 渲染导出 PNG 并返回字节。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 参数:
|
|
|
|
|
|
/// - [project]:数据源;
|
2026-10-07 09:44:33 +08:00
|
|
|
|
/// - [tokens]:配色令牌(内部转为导出专用纯白主题);
|
2026-10-07 09:27:45 +08:00
|
|
|
|
/// - [criticalActions]/[criticalDeps]:关键路径高亮(与屏幕一致)。
|
2026-10-07 09:44:33 +08:00
|
|
|
|
///
|
|
|
|
|
|
/// 输出:3840×2160 像素(1920×1080 逻辑画布 × 2 倍像素比),
|
|
|
|
|
|
/// 保证平板 / 2K / 4K 屏幕上查看清晰锐利。
|
2026-10-07 09:27:45 +08:00
|
|
|
|
Future<Uint8List> renderExportPng({
|
|
|
|
|
|
required Project project,
|
|
|
|
|
|
required Tokens tokens,
|
|
|
|
|
|
required Set<String> criticalActions,
|
|
|
|
|
|
required Set<String> criticalDeps,
|
|
|
|
|
|
}) async {
|
2026-10-07 09:44:33 +08:00
|
|
|
|
// 导出专用令牌:纯白背景、浅色文字。
|
|
|
|
|
|
final Tokens et = exportTokens(tokens);
|
2026-10-07 09:27:45 +08:00
|
|
|
|
final ui.PictureRecorder recorder = ui.PictureRecorder();
|
|
|
|
|
|
final Canvas canvas = Canvas(recorder);
|
|
|
|
|
|
final double w = kExportSize.width;
|
|
|
|
|
|
final double h = kExportSize.height;
|
2026-10-07 09:44:33 +08:00
|
|
|
|
// 2 倍像素比:整体放大绘制,矢量内容保持锐利。
|
|
|
|
|
|
canvas.scale(kExportPixelRatio);
|
2026-10-07 09:27:45 +08:00
|
|
|
|
|
2026-10-07 09:44:33 +08:00
|
|
|
|
// 背景铺满(纯白)。
|
|
|
|
|
|
canvas.drawRect(Rect.fromLTWH(0, 0, w, h), Paint()..color = et.bg);
|
2026-10-07 09:27:45 +08:00
|
|
|
|
|
|
|
|
|
|
// 标题:固定左上角。
|
|
|
|
|
|
final ViewMode mode = project.viewState.mode;
|
|
|
|
|
|
_text(canvas, exportTitle(mode), const Offset(48, 36),
|
2026-10-07 09:44:33 +08:00
|
|
|
|
color: et.text, size: 34, weight: FontWeight.w700);
|
2026-10-09 15:38:21 +08:00
|
|
|
|
_text(canvas, exportSubtitle(mode), const Offset(48, 88),
|
2026-10-07 09:44:33 +08:00
|
|
|
|
color: et.sub, size: 16, weight: FontWeight.w400);
|
2026-10-07 09:27:45 +08:00
|
|
|
|
|
|
|
|
|
|
// 内容:适配缩放并居中到内容区。
|
|
|
|
|
|
final ExportFit fit = computeExportFit(project, mode, kExportContentArea);
|
|
|
|
|
|
canvas.save();
|
|
|
|
|
|
canvas.translate(fit.tx, fit.ty);
|
|
|
|
|
|
canvas.scale(fit.scale);
|
|
|
|
|
|
if (mode == ViewMode.bar) {
|
|
|
|
|
|
BarPainter(
|
|
|
|
|
|
project: project,
|
2026-10-07 09:44:33 +08:00
|
|
|
|
tokens: et,
|
2026-10-07 09:27:45 +08:00
|
|
|
|
criticalActions: criticalActions,
|
|
|
|
|
|
criticalDeps: criticalDeps,
|
|
|
|
|
|
selectedIds: const <String>{},
|
|
|
|
|
|
).paint(canvas, kExportSize);
|
2026-10-09 13:59:05 +08:00
|
|
|
|
} else if (mode == ViewMode.flow) {
|
|
|
|
|
|
FlowchartPainter(
|
|
|
|
|
|
project: project,
|
|
|
|
|
|
tokens: et,
|
|
|
|
|
|
criticalActions: criticalActions,
|
|
|
|
|
|
criticalDeps: criticalDeps,
|
|
|
|
|
|
selectedIds: const <String>{},
|
|
|
|
|
|
).paint(canvas, kExportSize);
|
2026-10-07 09:27:45 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
CircularPainter(
|
|
|
|
|
|
project: project,
|
2026-10-07 09:44:33 +08:00
|
|
|
|
tokens: et,
|
2026-10-07 09:27:45 +08:00
|
|
|
|
criticalActions: criticalActions,
|
|
|
|
|
|
criticalDeps: criticalDeps,
|
|
|
|
|
|
selectedIds: const <String>{},
|
|
|
|
|
|
).paint(canvas, kExportSize);
|
|
|
|
|
|
}
|
|
|
|
|
|
canvas.restore();
|
|
|
|
|
|
|
|
|
|
|
|
// 图例:固定左下角。
|
|
|
|
|
|
_text(canvas, exportLegend(project), Offset(48, h - 38),
|
2026-10-07 09:44:33 +08:00
|
|
|
|
color: et.sub, size: 15, weight: FontWeight.w400);
|
2026-10-07 09:27:45 +08:00
|
|
|
|
|
2026-10-07 09:44:33 +08:00
|
|
|
|
final ui.Image image = await recorder
|
|
|
|
|
|
.endRecording()
|
|
|
|
|
|
.toImage((w * kExportPixelRatio).toInt(), (h * kExportPixelRatio).toInt());
|
2026-10-07 09:27:45 +08:00
|
|
|
|
final ByteData? data = await image.toByteData(format: ui.ImageByteFormat.png);
|
|
|
|
|
|
return data!.buffer.asUint8List();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 用统一字体绘制单行文本。
|
|
|
|
|
|
void _text(Canvas canvas, String s, Offset at,
|
|
|
|
|
|
{required Color color, required double size, required FontWeight weight}) {
|
|
|
|
|
|
final TextPainter tp = TextPainter(
|
|
|
|
|
|
text: TextSpan(
|
|
|
|
|
|
text: s,
|
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
|
color: color,
|
|
|
|
|
|
fontSize: size,
|
|
|
|
|
|
fontWeight: weight,
|
|
|
|
|
|
fontFamily: 'Microsoft YaHei UI',
|
|
|
|
|
|
fontFamilyFallback: const <String>['Microsoft YaHei', 'Noto Sans SC', 'sans-serif'],
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
textDirection: ui.TextDirection.ltr,
|
|
|
|
|
|
)..layout();
|
|
|
|
|
|
tp.paint(canvas, Offset(at.dx, at.dy - tp.height / 2));
|
|
|
|
|
|
}
|