feat: 导出固定 1920×1080(标题恒定左上、内容自适应缩放)+ 安卓保存位置可配置

- 导出重设计(exe 与安卓一致):SVG/PNG 固定 1920×1080;标题恒定左上、图例恒定左下;内容按包围盒等比缩放居中(export_fit.dart 统一计算,不裁切不溢出);PNG 改为离屏 1920×1080 渲染

- 安卓保存位置可改:保存/打开对话框新增「更改位置」,基础目录可选文档/下载/图片 + 自定义子文件夹,设置持久化(save_location.json)

- 原生通道 seqstudio/docs 支持 base/folder 参数;新增 export_test.dart 回归(尺寸/标题/transform)

- 重建 dist(Windows)与签名 APK
This commit is contained in:
admin
2026-10-07 09:27:45 +08:00
parent 50d2303951
commit c1d9f27952
10 changed files with 533 additions and 120 deletions
+81
View File
@@ -0,0 +1,81 @@
/// 导出适配:固定 1920×1080 画布下,把时序图内容缩放到内容区并居中,
/// 供 PNG 渲染器与 SVG 写入器共用,保证两种导出几何完全一致。
///
/// 约定:
/// - 画布固定 1920×1080;
/// - 标题固定在左上角(标题区 0–128px);
/// - 内容区 = (48, 128) → (1872, 1016),图例固定左下角;
/// - 内容按自身包围盒等比缩放并居中到内容区,不裁切、不溢出。
library;
import 'dart:math' as math;
import 'package:flutter/material.dart' hide Action;
import '../layout/layout.dart';
import '../model/models.dart';
import '../model/units.dart';
import '../render/bar_painter.dart';
/// 导出画布尺寸(固定)。
const Size kExportSize = Size(1920, 1080);
/// 内容区(标题下方、图例上方)。
const Rect kExportContentArea = Rect.fromLTRB(48, 128, 1872, 1016);
/// 导出适配结果:内容变换 `scale` + `translate(tx, ty)`。
class ExportFit {
/// 构造适配结果。
const ExportFit(this.scale, this.tx, this.ty);
/// 缩放系数。
final double scale;
/// 水平平移。
final double tx;
/// 垂直平移。
final double ty;
/// SVG transform 属性值。
String get svgTransform =>
'translate(${tx.toStringAsFixed(1)} ${ty.toStringAsFixed(1)}) scale(${scale.toStringAsFixed(4)})';
}
/// 计算内容在 [area] 内的适配变换。
///
/// 先以 1920×1080 为参考画布求内容包围盒,再等比缩放并居中。
ExportFit computeExportFit(Project project, ViewMode mode, Rect area) {
Rect bounds;
if (mode == ViewMode.bar) {
final BarLayout l = BarLayout(project, kExportSize);
final int n = l.lanes.length;
final double bottom = n == 0 ? BarLayout.topPad + BarLayout.rowVisual : l.yFor(n) + BarLayout.rowVisual + 8;
bounds = Rect.fromLTRB(12, BarPainter.phaseBandTop - 14, l.plotRight + 120, bottom);
} else {
final CircularLayout l = CircularLayout(project, kExportSize);
final double rExt = l.tickR + 100;
bounds = Rect.fromLTRB(l.center.dx - rExt, l.center.dy - rExt, l.center.dx + rExt, l.center.dy + rExt);
}
if (bounds.width <= 1 || bounds.height <= 1) {
return const ExportFit(1, 0, 0);
}
final double s = math.min(area.width / bounds.width, area.height / bounds.height).clamp(0.2, 1.6);
final double tx = area.left + (area.width - bounds.width * s) / 2 - bounds.left * s;
final double ty = area.top + (area.height - bounds.height * s) / 2 - bounds.top * s;
return ExportFit(s, tx, ty);
}
/// 导出标题(与画布浮层一致)。
String exportTitle(ViewMode mode) =>
mode == ViewMode.bar ? '条形时序视图' : '圆周时序视图';
/// 导出副标题(与画布浮层一致)。
String exportSubtitle(Project project, ViewMode mode) {
if (mode == ViewMode.bar) {
return '箭头表示动作之间关联(绿色实线 FS 严格顺序 / 琥珀虚线 SS 并行重叠)';
}
return '同心圆环 = 动作层 · 1 圈 = 1 循环 = ${tfmt(project.cycle.durationMs, project.units)} = 360° = 100%';
}
/// 导出图例(左下角固定)。
String exportLegend(Project project) =>
'时序图设计器 · ${project.name} · 循环 ${tfmt(project.cycle.durationMs, project.units)} = 360°';
+97
View File
@@ -0,0 +1,97 @@
/// PNG 导出渲染器:把时序图渲染为固定 1920×1080 的位图。
///
/// 与 SVG 导出共用同一套适配计算([computeExportFit]),保证两种格式几何一致:
/// - 背景铺满 1920×1080;
/// - 标题固定在左上角、图例固定在左下角;
/// - 内容(条形 / 圆周)按自身包围盒等比缩放并居中到内容区。
library;
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart' hide Action;
import '../model/models.dart';
import '../render/bar_painter.dart';
import '../render/circular_painter.dart';
import '../theme/tokens.dart';
import 'export_fit.dart';
/// 渲染导出 PNG 并返回字节。
///
/// 参数:
/// - [project]:数据源;
/// - [tokens]:配色令牌;
/// - [criticalActions]/[criticalDeps]:关键路径高亮(与屏幕一致)。
Future<Uint8List> renderExportPng({
required Project project,
required Tokens tokens,
required Set<String> criticalActions,
required Set<String> criticalDeps,
}) async {
final ui.PictureRecorder recorder = ui.PictureRecorder();
final Canvas canvas = Canvas(recorder);
final double w = kExportSize.width;
final double h = kExportSize.height;
// 背景铺满。
canvas.drawRect(Rect.fromLTWH(0, 0, w, h), Paint()..color = tokens.bg);
// 标题:固定左上角。
final ViewMode mode = project.viewState.mode;
_text(canvas, exportTitle(mode), const Offset(48, 36),
color: tokens.text, size: 34, weight: FontWeight.w700);
_text(canvas, exportSubtitle(project, mode), const Offset(48, 88),
color: tokens.sub, size: 16, weight: FontWeight.w400);
// 内容:适配缩放并居中到内容区。
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,
tokens: tokens,
criticalActions: criticalActions,
criticalDeps: criticalDeps,
selectedIds: const <String>{},
).paint(canvas, kExportSize);
} else {
CircularPainter(
project: project,
tokens: tokens,
criticalActions: criticalActions,
criticalDeps: criticalDeps,
selectedIds: const <String>{},
).paint(canvas, kExportSize);
}
canvas.restore();
// 图例:固定左下角。
_text(canvas, exportLegend(project), Offset(48, h - 38),
color: tokens.sub, size: 15, weight: FontWeight.w400);
final ui.Image image = await recorder.endRecording().toImage(w.toInt(), h.toInt());
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));
}
+28 -21
View File
@@ -10,8 +10,10 @@
/// - 每个逻辑分组写成 `<g id="...">`(背景、标尺、动作、标签、依赖、图例),
/// 动作组与依赖组额外附带 `data-*` 属性,便于下游脚本解析。
///
/// 输出约定:
/// - 画布固定 2560×1440,通过 `viewBox` 支持无损缩放。
/// 输出约定(与 PNG 导出一致):
/// - 画布固定 1920×1080,通过 `viewBox` 支持无损缩放;
/// - 标题固定在左上角、图例固定在左下角;
/// - 时序图内容按自身包围盒等比缩放并居中到内容区,不裁切、不溢出。
/// - 所有文本内联 [_font] 字体栈,保证跨平台字形一致。
library;
@@ -22,6 +24,7 @@ import '../layout/layout.dart';
import '../model/models.dart';
import '../model/units.dart';
import '../theme/tokens.dart';
import 'export_fit.dart';
/// SVG 文档写入器。
///
@@ -51,28 +54,42 @@ class SvgWriter {
/// 生成完整 SVG 文档字符串。
///
/// 输出约定(与 PNG 导出一致):
/// - 画布固定 1920×1080;
/// - 标题固定在左上角、图例固定在左下角;
/// - 时序图内容按自身包围盒等比缩放并居中到内容区,不裁切、不溢出。
///
/// 返回:以 `<svg ...>` 开头、`</svg>` 结尾的字符串。
/// 副作用:会持续向内部缓冲区追加内容;重复调用不会自动清空缓冲区,
/// 因此约定每次导出都新建一个 [SvgWriter] 实例。
String build() {
// 固定逻辑画布尺寸;viewBox 与 width/height 相同,缩放时几何不失真。
final double w = 2560;
final double h = 1440;
_b.write('<svg xmlns="http://www.w3.org/2000/svg" width="$w" height="$h" viewBox="0 0 $w $h">');
const double w = 1920;
const double h = 1080;
_b.write('<svg xmlns="http://www.w3.org/2000/svg" width="1920" height="1080" viewBox="0 0 1920 1080">');
// 先写箭头 marker 定义,供后续依赖路径引用。
_defs();
// 背景层:铺满整张画布。
_b.write('<g id="background">');
_rect(0, 0, w, h, fill: _hex(tokens.bg));
_b.write('</g>');
// 按当前视图模式生成主体内容。
// 标题:固定左上角。
_b.write('<g id="title">');
_text(48, 60, exportTitle(mode), color: _hex(tokens.text), size: 34, weight: '700');
_text(48, 104, exportSubtitle(project, mode), color: _hex(tokens.sub), size: 16);
_b.write('</g>');
// 内容:适配缩放并居中到内容区。
final ExportFit fit = computeExportFit(project, mode, kExportContentArea);
_b.write('<g id="content" transform="${fit.svgTransform}">');
if (mode == ViewMode.bar) {
_barView(Size(w, h));
_barView(const Size(w, h));
} else {
_circularView(Size(w, h));
_circularView(const Size(w, h));
}
// 图例始终绘制在最上层。
_legend(w, h);
_b.write('</g>');
// 图例:固定左下角。
_b.write('<g id="legend">');
_text(48, h - 36, exportLegend(project), color: _hex(tokens.sub), size: 15);
_b.write('</g>');
_b.write('</svg>');
return _b.toString();
}
@@ -253,16 +270,6 @@ class SvgWriter {
}
}
/// 写入图例分组,展示工程名、循环时长与 360° 的等价关系。
///
/// 参数 [w]/[h] 为画布宽高,用于定位到左下角。
void _legend(double w, double h) {
_b.write('<g id="legend">');
_text(24, h - 28, '时序图设计器 · ${project.name} · 循环 ${tfmt(project.cycle.durationMs, project.units)} = 360°',
color: _hex(tokens.sub), size: 13);
_b.write('</g>');
}
/// 生成一段圆弧的 SVG `d` 路径字符串。
///
/// 参数 [c] 圆心、[r] 半径、[a0]/[a1] 起止角度(度)。