Files
EasyDesignCollection/lib/modules/sequence/export/export_fit.dart
T

73 lines
3.4 KiB
Dart
Raw Normal View History

/// 导出适配:固定 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 'package:sequence_diagram/export/export_geometry.dart';
export 'package:sequence_diagram/export/export_geometry.dart' show ExportFit, kExportContentArea, kExportPixelRatio, kExportSize, exportTokens;
import 'package:sequence_diagram/modules/sequence/layout/layout.dart';
import 'package:sequence_diagram/modules/sequence/layout/flow_layout.dart';
import 'package:sequence_diagram/model/models.dart';
import 'package:sequence_diagram/model/units.dart';
import 'package:sequence_diagram/modules/sequence/render/bar_painter.dart';
/// 计算内容在 [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.plotBottom + 8;
bounds = Rect.fromLTRB(12, BarPainter.phaseBandTop - 14, l.plotRight + 120, bottom);
} else if (mode == ViewMode.circular) {
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);
} else {
// 流程图:直接使用布局层给出的内容范围(含泳道、阶段栏与连线)。
final FlowLayout l = FlowLayout(project);
bounds = l.nodes.isEmpty ? const Rect.fromLTWH(0, 0, 800, 600) : l.bounds;
}
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) => switch (mode) {
ViewMode.bar => '条形时序视图',
ViewMode.circular => '圆周时序视图',
ViewMode.flow => '流程图视图',
};
/// 导出副标题:仅保留括号内的线型图例(与画布标题后方的说明一致)。
///
/// 更完整的视图语义(同心圆环 / 循环换算等)见应用内「帮助」。
String exportSubtitle(ViewMode mode) => switch (mode) {
ViewMode.bar => '(绿色实线 FS 严格顺序 / 琥珀虚线 SS 并行重叠)',
ViewMode.circular => '(绿色实线 FS 严格顺序 / 琥珀虚线 SS 并行重叠)',
// 流程图不重复线型说明,导出仅保留标题(与画布浮层一致)。
ViewMode.flow => '',
};
/// 导出图例(左下角固定)。
String exportLegend(Project project) =>
'时序图设计 · ${project.name} · 循环 ${tfmt(project.cycle.durationMs, project.units)} = 360°';