// 架构图模块测试:手绘识别(直线 / 箭头 / 矩形 / 圆 / 椭圆 / 三角形 / 菱形)、 // 数据模型 JSON 往返、示例场景完整性。 import 'dart:convert'; import 'dart:math' as math; import 'package:flutter/material.dart' hide Action; import 'package:flutter_test/flutter_test.dart'; import 'package:sequence_diagram/arch/arch_models.dart'; import 'package:sequence_diagram/arch/arch_painter.dart'; import 'package:sequence_diagram/arch/arch_recognizer.dart'; import 'package:sequence_diagram/arch/arch_samples.dart'; import 'package:sequence_diagram/model/models.dart'; import 'package:sequence_diagram/state/app_controller.dart'; void main() { group('手绘识别', () { test('直线:接近水平 → 吸附为水平线', () { final ArchRecognition? r = recognizeArchStroke( sketchLine(const Offset(20, 100), const Offset(220, 92), seed: 3), ); expect(r, isNotNull); expect(r!.type, ArchShapeType.line); expect(r.arrow, isFalse); expect((r.a!.dy - r.b!.dy).abs(), lessThan(0.001)); }); test('直线:斜线保持角度', () { final ArchRecognition? r = recognizeArchStroke( sketchLine(const Offset(20, 20), const Offset(200, 160), seed: 4), ); expect(r, isNotNull); expect(r!.type, ArchShapeType.line); expect(r.arrow, isFalse); expect(r.b!.dx - r.a!.dx, greaterThan(150)); }); test('箭头:箭杆 + 尖端折返 → 识别为箭头', () { final ArchRecognition? r = recognizeArchStroke( sketchArrow(const Offset(20, 140), const Offset(240, 60), seed: 5), ); expect(r, isNotNull); expect(r!.type, ArchShapeType.line); expect(r.arrow, isTrue); }); test('矩形:随手画的方块 → 扶正为矩形', () { const Rect target = Rect.fromLTWH(30, 40, 160, 110); final List pts = sketchPolyline( [ target.topLeft, target.topRight, target.bottomRight, target.bottomLeft, target.topLeft + const Offset(12, 0), ], seed: 6, ); final ArchRecognition? r = recognizeArchStroke(pts); expect(r, isNotNull); expect(r!.type, ArchShapeType.rect); expect((r.rect.left - target.left).abs(), lessThan(6)); expect((r.rect.width - target.width).abs(), lessThan(8)); expect((r.rect.height - target.height).abs(), lessThan(8)); }); test('圆形:随手画的圆 → 扶正为圆(宽高一致)', () { final List pts = sketchEllipse( Rect.fromCircle(center: const Offset(100, 100), radius: 70), seed: 7, ); final ArchRecognition? r = recognizeArchStroke(pts); expect(r, isNotNull); expect(r!.type, ArchShapeType.circle); expect((r.rect.width - r.rect.height).abs(), lessThan(0.01)); }); test('椭圆:长宽比保留', () { const Rect target = Rect.fromLTWH(20, 40, 200, 100); final ArchRecognition? r = recognizeArchStroke(sketchEllipse(target, seed: 8)); expect(r, isNotNull); expect(r!.type, ArchShapeType.ellipse); expect(r.rect.width / r.rect.height, greaterThan(1.6)); }); test('三角形:顶点朝上', () { const Rect target = Rect.fromLTWH(30, 30, 160, 120); final List pts = sketchPolyline( [ Offset(target.center.dx, target.top), target.bottomRight, target.bottomLeft, Offset(target.center.dx + 10, target.top), ], seed: 9, jitter: 1.4, ); final ArchRecognition? r = recognizeArchStroke(pts); expect(r, isNotNull); expect(r!.type, ArchShapeType.triangle); expect(r.dir, 0); }); test('菱形', () { const Rect target = Rect.fromLTWH(30, 40, 160, 120); final List pts = sketchPolyline( [ Offset(target.center.dx, target.top), Offset(target.right, target.center.dy), Offset(target.center.dx, target.bottom), Offset(target.left, target.center.dy), Offset(target.center.dx + 10, target.top), ], seed: 10, jitter: 1.4, ); final ArchRecognition? r = recognizeArchStroke(pts); expect(r, isNotNull); expect(r!.type, ArchShapeType.diamond); }); test('折线:L 形 → 识别为折线并扶正水平 / 垂直段', () { final ArchRecognition? r = recognizeArchStroke(sketchPolyline( [const Offset(20, 20), const Offset(180, 30), const Offset(190, 160)], seed: 11, )); expect(r, isNotNull); expect(r!.type, ArchShapeType.polyline); final List pts = r.pts!; expect(pts.length, 3); expect((pts[1].dy - pts[0].dy).abs(), lessThan(0.001)); expect((pts[2].dx - pts[1].dx).abs(), lessThan(0.001)); }); test('连线轨迹简化:多段折线 + 近轴段扶正', () { final List pts = sketchPolyline( [ const Offset(20, 20), const Offset(180, 30), const Offset(190, 160) ], seed: 11, ); final List v = simplifyArchPolyline(pts); expect(v.length, 3); expect((v[1].dy - v[0].dy).abs(), lessThan(0.001)); expect((v[2].dx - v[1].dx).abs(), lessThan(0.001)); }); test('曲线:平滑 S 形 → 识别为曲线', () { final ArchRecognition? r = recognizeArchStroke( sketchCurve(const Offset(30, 130), const Offset(200, 60), seed: 12), ); expect(r, isNotNull); expect(r!.type, ArchShapeType.curve); expect(r.pts!.length, greaterThanOrEqualTo(3)); }); test('云:多鼓包闭合曲线 → 识别为云', () { const Rect rect = Rect.fromLTWH(40, 40, 200, 130); final ArchShape proto = ArchShape(id: 'p', type: ArchShapeType.cloud, rect: rect); final List pts = archInkOutline( archShapePath(proto), seed: 21, jitter: 1.6, gapFrac: 0.06, ); final ArchRecognition? r = recognizeArchStroke(pts); expect(r, isNotNull); expect(r!.type, ArchShapeType.cloud); }); test('圆柱:柱体 + 顶盖一笔画 → 识别为圆柱', () { const Rect rect = Rect.fromLTWH(40, 40, 150, 150); final ArchShape proto = ArchShape(id: 'p', type: ArchShapeType.cylinder, rect: rect); final List pts = archInkOutline( archShapePath(proto), seed: 22, jitter: 1.5, gapFrac: 0.05, ); final ArchRecognition? r = recognizeArchStroke(pts); expect(r, isNotNull); expect(r!.type, ArchShapeType.cylinder); }); test('两笔组合:分开两笔画的矩形 → 合并识别为矩形', () { const Rect target = Rect.fromLTWH(40, 40, 160, 110); final List a = sketchPolyline( [target.topLeft, target.topRight, target.bottomRight], seed: 31, ); final List b = sketchPolyline( [target.bottomRight, target.bottomLeft, target.topLeft], seed: 32, ); final ArchRecognition? r = recognizeArchStrokes(>[a, b]); expect(r, isNotNull); expect(r!.type, ArchShapeType.rect); }); test('圆柱拼接:顶盖椭圆 + 柱体矩形 → 圆柱', () { const ArchRecognition lid = ArchRecognition( type: ArchShapeType.ellipse, rect: Rect.fromLTWH(40, 40, 140, 56), ); const ArchRecognition body = ArchRecognition( type: ArchShapeType.rect, rect: Rect.fromLTWH(40, 68, 140, 110), ); final ArchRecognition? r = archCylinderFromRecognitions(body, lid); expect(r, isNotNull); expect(r!.type, ArchShapeType.cylinder); expect(r.rect.top, 40); expect(archCylinderFromRecognitions(lid, lid), isNull); }); test('对照表样本全部识别成功', () { for (final (String, List) item in archRecognitionSheet()) { final ArchRecognition? r = recognizeArchStroke(item.$2); expect(r, isNotNull, reason: '样本「${item.$1}」应被识别'); } }); }); group('架构图模型', () { test('JSON 往返:图形 / 墨迹 / 连线 / 视口', () { final ArchDoc doc = ArchDoc( shapes: [ const ArchShape( id: 'S1', type: ArchShapeType.roundRect, rect: Rect.fromLTWH(10, 20, 120, 60), radius: 12, fill: 0x264CC9F0, stroke: 0xFF4CC9F0, text: 'PLC', ), const ArchShape( id: 'S2', type: ArchShapeType.line, rect: Rect.fromLTWH(0, 0, 100, 50), a: Offset(0, 0), b: Offset(100, 50), arrow: true, ), ], strokes: [ const ArchStroke( id: 'K1', pts: [Offset(1, 2), Offset(3, 4)], color: 0xFF64748B, ), ], connectors: [ const ArchConnector( id: 'C1', fromId: 'S1', from: Offset(0, 0), toId: 'S2', to: Offset(1, 1), label: 'EtherCAT', dash: true, ), ], zoom: 0.8, panX: 12, panY: -5, ); final ArchDoc back = ArchDoc.fromJson( jsonDecode(jsonEncode(doc.toJson())) as Map, ); expect(back.shapes.length, 2); expect(back.shapes[0].type, ArchShapeType.roundRect); expect(back.shapes[0].radius, 12); expect(back.shapes[0].text, 'PLC'); expect(back.shapes[1].arrow, isTrue); expect(back.strokes.single.pts.length, 2); expect(back.connectors.single.label, 'EtherCAT'); expect(back.connectors.single.dash, isTrue); expect(back.zoom, 0.8); expect(back.panX, 12); expect(back.panY, -5); }); test('工程文件往返保留架构图(暂不与时序图互通)', () { final Project p = Project.demo().copyWith(arch: archDemoDoneScene()); final Project back = Project.decode(p.encode()); expect(back.arch.shapes.length, p.arch.shapes.length); expect(back.arch.connectors.length, p.arch.connectors.length); expect(back.actions.length, p.actions.length); expect(back.name, p.name); }); test('JSON 往返:走线方式 / 网格 / 折线点', () { final ArchDoc doc = ArchDoc( shapes: [ const ArchShape( id: 'S1', type: ArchShapeType.polyline, rect: Rect.fromLTWH(0, 0, 100, 60), fill: 0, stroke: 0xFF4CC9F0, pts: [Offset(0, 0), Offset(60, 0), Offset(60, 60)], ), ], connectors: [ const ArchConnector( id: 'C1', from: Offset(0, 0), to: Offset(80, 80), route: ArchRoute.ortho, ), ], gridSnap: false, gridSize: 40, ); final ArchDoc back = ArchDoc.fromJson( jsonDecode(jsonEncode(doc.toJson())) as Map, ); expect(back.connectors.single.route, ArchRoute.ortho); expect(back.gridSnap, isFalse); expect(back.gridSize, 40); expect(back.shapes.single.type, ArchShapeType.polyline); expect(back.shapes.single.pts.length, 3); }); test('连线折点:JSON 往返 + 几何经过折点', () { final ArchDoc doc = ArchDoc( connectors: [ const ArchConnector( id: 'C1', from: Offset(0, 0), to: Offset(100, 0), mids: [Offset(40, 0), Offset(40, 60)], ), ], ); final ArchDoc back = ArchDoc.fromJson( jsonDecode(jsonEncode(doc.toJson())) as Map, ); expect(back.connectors.single.mids.length, 2); final ArchConnGeometry g = archConnectorGeometry(back, back.connectors.single); expect(g.pts.length, 4); expect(g.pts[1], const Offset(40, 0)); expect(g.pts[2], const Offset(40, 60)); }); test('连线吸附:开始 / 结束吸附到最近边缘点', () { const Rect rr = Rect.fromLTWH(100, 100, 200, 100); final ArchShape rect = ArchShape(id: 'S', type: ArchShapeType.rect, rect: rr); // 外部点 clamp 到最近边。 expect(archNearestBorderPoint(rect, const Offset(350, 140)), const Offset(300, 140)); // 内部点投影到最近一条边。 final Offset inside = archNearestBorderPoint(rect, const Offset(200, 130)); final bool onBorder = (inside.dx - rr.left).abs() < 0.01 || (inside.dx - rr.right).abs() < 0.01 || (inside.dy - rr.top).abs() < 0.01 || (inside.dy - rr.bottom).abs() < 0.01; expect(onBorder, isTrue); // 椭圆按径向投影到边界。 final ArchShape ell = ArchShape( id: 'E', type: ArchShapeType.ellipse, rect: const Rect.fromLTWH(0, 0, 200, 100)); expect(archNearestBorderPoint(ell, const Offset(300, 50)), const Offset(200, 50)); }); test('移动吸附:磁力对齐线与网格回退', () { const Rect a = Rect.fromLTWH(0, 0, 100, 60); const Rect b = Rect.fromLTWH(200, 0, 100, 60); final ArchDoc doc = ArchDoc( shapes: [ const ArchShape(id: 'A', type: ArchShapeType.rect, rect: a), const ArchShape(id: 'B', type: ArchShapeType.rect, rect: b), ], gridSnap: false, ); // 拖 B:左边缘离 A 的右边缘 3px(tol=6)→ 磁力吸附对齐 + 导引线。 final ArchMoveSnap snap = archSnapMoveDelta( doc: doc, movingIds: {'B'}, union: b.shift(const Offset(-97, 0)), // 左缘 103,靠近 A 右缘 100(差 3px) rawDelta: const Offset(-97, 0), tol: 6, gridSnap: false, ); expect(snap.guideX, 100); expect(snap.delta.dx, -100); // 网格回退:无对齐时按网格吸附主选中图形。 final ArchDoc grid = doc.copyWith(gridSnap: true, gridSize: 20); final ArchMoveSnap g2 = archSnapMoveDelta( doc: grid, movingIds: {'B'}, union: b.shift(const Offset(103, 7)), rawDelta: const Offset(103, 7), tol: 6, gridSnap: true, ); expect(g2.delta.dx, 100); expect(g2.delta.dy, 0); }); test('正交 / 极轴:位移约束', () { expect( archConstrainDelta( raw: const Offset(30, 10), ortho: true, polar: false, polarDeg: 15), const Offset(30, 0), ); expect( archConstrainDelta( raw: const Offset(10, 30), ortho: true, polar: false, polarDeg: 15), const Offset(0, 30), ); final Offset p = archConstrainDelta( raw: const Offset(100, 96.6), ortho: false, polar: true, polarDeg: 15); final double ang = math.atan2(p.dy, p.dx) * 180 / math.pi; expect((ang - 45).abs(), lessThan(0.01)); expect( (p.distance - math.sqrt(100 * 100 + 96.6 * 96.6)).abs(), lessThan(0.01), ); }); test('图层:JSON 往返 / 可见性 / 内容范围', () { final ArchDoc doc = ArchDoc( layers: [ const ArchLayer(id: 'L0', name: '默认', color: 0xFF4CC9F0), const ArchLayer( id: 'LY2', name: '电控', color: 0xFF34D399, visible: false, locked: true), ], shapes: [ const ArchShape( id: 'S1', type: ArchShapeType.rect, rect: Rect.fromLTWH(0, 0, 100, 60), layerId: 'L0'), const ArchShape( id: 'S2', type: ArchShapeType.rect, rect: Rect.fromLTWH(500, 0, 100, 60), layerId: 'LY2'), ], ); final ArchDoc back = ArchDoc.fromJson( jsonDecode(jsonEncode(doc.toJson())) as Map, ); expect(back.layers.length, 2); expect(back.layerById('LY2').visible, isFalse); expect(back.isLayerLocked('LY2'), isTrue); expect(back.isLayerVisible('L0'), isTrue); // 隐藏图层不计入内容范围。 expect(back.contentBounds().right, lessThan(200)); }); test('图层归属:新增对象自动进入活动图层', () { final AppController c = AppController(project: Project.demo()); addTearDown(c.dispose); c.addArchLayer(); final String active = c.arch.activeLayer; c.addArchShape(const ArchShape( id: 'S9', type: ArchShapeType.rect, rect: Rect.fromLTWH(0, 0, 10, 10), )); expect(c.arch.shapeById('S9')!.layerId, active); }); test('removeIds 连带删除吸附在图形上的连线', () { final ArchDoc doc = archDemoDoneScene().removeIds({'S4'}); expect(doc.shapeById('S4'), isNull); expect( doc.connectors .any((ArchConnector c) => c.fromId == 'S4' || c.toId == 'S4'), isFalse, ); }); test('连线端点:吸附图形时实时求边框交点', () { final ArchDoc doc = archDemoDoneScene(); final ArchConnector c = doc.connectors.firstWhere((ArchConnector x) => x.id == 'C4'); final (Offset a, Offset b) = archConnectorEnds(doc, c); final ArchShape s5 = doc.shapeById('S5')!; // 终点应落在视觉相机(S5)的边框上。 expect(s5.rect.inflate(2).contains(b), isTrue); expect((a - b).distance, greaterThan(10)); }); }); group('示例场景', () { test('手绘稿全部为墨迹(等待识别)', () { final ArchDoc ink = archDemoInkScene(); expect(ink.strokes.length, greaterThan(10)); expect(ink.shapes, isEmpty); expect(ink.connectors, isEmpty); }); test('完成图含图形 / 连线 / 标题', () { final ArchDoc done = archDemoDoneScene(); expect(done.shapes.length, greaterThan(8)); expect(done.connectors.length, 9); expect(done.shapes.any((ArchShape s) => s.text.contains('系统架构')), isTrue); }); }); group('对齐 / 均布', () { AppController makeController() { final AppController c = AppController(project: Project.demo()); addTearDown(c.dispose); c.addArchShape(const ArchShape( id: 'A1', type: ArchShapeType.rect, rect: Rect.fromLTWH(0, 0, 100, 60))); c.addArchShape(const ArchShape( id: 'A2', type: ArchShapeType.rect, rect: Rect.fromLTWH(220, 90, 60, 40))); c.addArchShape(const ArchShape( id: 'A3', type: ArchShapeType.circle, rect: Rect.fromLTWH(500, 300, 80, 80))); c.selectArchIds(['A1', 'A2', 'A3']); return c; } test('左对齐 / 顶对齐:全部对齐到选中范围边界', () { final AppController c = makeController(); c.alignArchSelection(ArchAlign.left); final double left = c.arch.shapeById('A1')!.bounds.left; for (final String id in ['A2', 'A3']) { expect(c.arch.shapeById(id)!.bounds.left, closeTo(left, 0.001)); } c.alignArchSelection(ArchAlign.top); final double top = c.arch.shapeById('A1')!.bounds.top; for (final String id in ['A2', 'A3']) { expect(c.arch.shapeById(id)!.bounds.top, closeTo(top, 0.001)); } }); test('横向 / 纵向均布:中心等距', () { final AppController c = makeController(); c.alignArchSelection(ArchAlign.distributeH); final List cx = [ for (final String id in ['A1', 'A2', 'A3']) c.arch.shapeById(id)!.bounds.center.dx, ]; expect((cx[1] - cx[0]) - (cx[2] - cx[1]), closeTo(0, 0.001)); c.alignArchSelection(ArchAlign.distributeV); final List cy = [ for (final String id in ['A1', 'A2', 'A3']) c.arch.shapeById(id)!.bounds.center.dy, ]; expect((cy[1] - cy[0]) - (cy[2] - cy[1]), closeTo(0, 0.001)); }); test('数量不足:仅提示不改动', () { final AppController c = AppController(project: Project.demo()); addTearDown(c.dispose); c.addArchShape(const ArchShape( id: 'B1', type: ArchShapeType.rect, rect: Rect.fromLTWH(0, 0, 40, 40))); c.addArchShape(const ArchShape( id: 'B2', type: ArchShapeType.rect, rect: Rect.fromLTWH(80, 80, 40, 40))); c.selectArchIds(['B1', 'B2']); final Rect before = c.arch.shapeById('B2')!.bounds; c.alignArchSelection(ArchAlign.distributeH); expect(c.status, contains('均布')); expect(c.arch.shapeById('B2')!.bounds, before); }); }); group('分文件保存', () { test('时序图 JSON 不含架构图部分;架构图独立序列化往返', () { final Project p = Project.demo().copyWith(arch: archDemoDoneScene()); final Map seq = jsonDecode(p.encode(includeArch: false)) as Map; final Map body = seq['project'] as Map; expect(body.containsKey('arch'), isFalse); // 默认(含架构图)仍写入:自动快照 / 旧文件兼容继续可用。 final Map full = jsonDecode(p.encode()) as Map; expect( (full['project'] as Map).containsKey('arch'), isTrue); // 独立架构图文档往返。 final ArchDoc back = ArchDoc.decode(p.arch.encode()); expect(back.shapes.length, p.arch.shapes.length); expect(back.connectors.length, p.arch.connectors.length); expect(back.layers.length, p.arch.layers.length); }); }); group('连线端点重吸附', () { test('端点可换绑到其它图形(吸附边框点)或脱离为自由端点', () { final ArchDoc doc = archDemoDoneScene(); final ArchConnector c4 = doc.connectors.firstWhere((ArchConnector x) => x.id == 'C4'); final ArchShape s5 = doc.shapeById('S5')!; // 换绑:起点吸附到 S5 的边框。 final Offset border = archNearestBorderPoint(s5, s5.rect.center); final ArchConnector moved = c4.copyWith(fromId: 'S5', from: border); expect(moved.fromId, 'S5'); expect(s5.rect.inflate(2).contains(moved.from), isTrue); // 脱离:显式置 null(哨兵语义),位置保留。 final ArchConnector free = c4.copyWith(fromId: null); expect(free.fromId, isNull); expect(free.from, c4.from); }); }); group('IT 图元', () { test('新增图元:标签 / 默认尺寸 / 路径 / JSON 往返', () { for (final ArchShapeType type in [ ArchShapeType.server, ArchShapeType.client, ArchShapeType.mobile, ArchShapeType.firewall, ArchShapeType.netSwitch, ArchShapeType.queue, ArchShapeType.symbol, ]) { expect(archShapeTypeLabel(type).isNotEmpty, isTrue); final Size d = archDefaultSize(type); expect(d.width, greaterThan(40), reason: archShapeTypeLabel(type)); final ArchShape s = ArchShape( id: 'X1', type: type, rect: const Rect.fromLTWH(0, 0, 160, 100), stroke: 0xFF4CC9F0, sw: 1.8, ); final Path path = archShapePath(s); expect(path.getBounds().isEmpty, isFalse, reason: archShapeTypeLabel(type)); final ArchShape back = ArchShape.fromJson(s.toJson()); expect(back.type, type); } }); }); group('尺寸编辑(识别后图形)', () { test('矩形宽 / 圆直径:直接缩放且中心保持', () { const ArchShape rect = ArchShape( id: 'D1', type: ArchShapeType.rect, rect: Rect.fromLTWH(100, 100, 200, 120), ); final ArchShape w = archShapeApplyDim(rect, ArchDimKind.width, 300); expect(w.rect.width, 300); expect(w.rect.center, rect.rect.center); expect(w.rect.height, 120); final ArchShape c = archShapeApplyDim( const ArchShape( id: 'D2', type: ArchShapeType.circle, rect: Rect.fromLTWH(0, 0, 80, 80), ), ArchDimKind.diameter, 120, ); expect(c.rect.width, 120); expect(c.rect.height, 120); }); test('直线 / 曲线长度:按比例缩放保持形状', () { const ArchShape line = ArchShape( id: 'L1', type: ArchShapeType.line, rect: Rect.fromLTWH(0, 0, 100, 0), a: Offset(0, 0), b: Offset(100, 0), ); expect(archShapeDimValue(line, ArchDimKind.length), 100); final ArchShape l2 = archShapeApplyDim(line, ArchDimKind.length, 250); expect(archShapeDimValue(l2, ArchDimKind.length), closeTo(250, 0.01)); expect(l2.rect.center, line.rect.center); const ArchShape curve = ArchShape( id: 'C1', type: ArchShapeType.curve, rect: Rect.fromLTWH(0, 0, 100, 50), pts: [Offset(0, 0), Offset(100, 0), Offset(100, 50)], ); expect(archShapeLength(curve), closeTo(150, 0.01)); final ArchShape c2 = archShapeApplyDim(curve, ArchDimKind.length, 300); expect(archShapeLength(c2), closeTo(300, 0.5)); final Rect b2 = archLinearBounds(c2); expect(b2.width / b2.height, closeTo(2.0, 0.01)); }); test('尺寸标注 JSON 往返', () { const ArchShape s = ArchShape( id: 'S1', type: ArchShapeType.rect, rect: Rect.fromLTWH(0, 0, 100, 80), dims: {ArchDimKind.width, ArchDimKind.height}, ); final ArchShape back = ArchShape.fromJson( jsonDecode(jsonEncode(s.toJson())) as Map); expect(back.dims.contains(ArchDimKind.width), isTrue); expect(back.dims.contains(ArchDimKind.height), isTrue); }); test('控制器:改值缩放 + 标注开关', () { final AppController c = AppController(project: Project.demo()); addTearDown(c.dispose); c.addArchShape(const ArchShape( id: 'S1', type: ArchShapeType.rect, rect: Rect.fromLTWH(10, 10, 100, 60), )); c.setArchShapeDim('S1', ArchDimKind.width, 260); expect(c.arch.shapeById('S1')!.rect.width, 260); c.toggleArchShapeDim('S1', ArchDimKind.width); expect( c.arch.shapeById('S1')!.dims.contains(ArchDimKind.width), isTrue); c.toggleArchShapeDim('S1', ArchDimKind.width); expect( c.arch.shapeById('S1')!.dims.contains(ArchDimKind.width), isFalse); }); test('默认画布 A0:空文档内容边界 1189×841', () { final Rect b = ArchDoc.empty.contentBounds(); expect(b.width, 1189); expect(b.height, 841); }); }); } // ===================== 手绘样本生成(确定性抖动) ===================== double _rnd(math.Random r, double amp) => (r.nextDouble() * 2 - 1) * amp; Offset _rot(Offset v, double deg) { final double a = deg * math.pi / 180; return Offset(v.dx * math.cos(a) - v.dy * math.sin(a), v.dx * math.sin(a) + v.dy * math.cos(a)); } /// 直线样本。 List sketchLine(Offset a, Offset b, {int seed = 1, double jitter = 1.5}) { final math.Random rnd = math.Random(seed); final List out = []; final Offset d = b - a; final double len = d.distance; final Offset u = d / len; final Offset n = Offset(-u.dy, u.dx); for (double t = 0; t <= len - 2; t += 6) { out.add(a + u * t + n * _rnd(rnd, jitter) + u * _rnd(rnd, jitter * 0.5)); } out.add(b); return out; } /// 箭头样本:箭杆 + 尖端折返的「>」形头部(同一笔)。 List sketchArrow(Offset a, Offset b, {int seed = 1, double jitter = 1.2}) { final math.Random rnd = math.Random(seed); final List out = []; final Offset d = b - a; final double len = d.distance; final Offset u = d / len; final Offset n = Offset(-u.dy, u.dx); for (double t = 0; t <= len - 4; t += 7) { out.add(a + u * t + n * _rnd(rnd, jitter)); } final double hs = math.min(40, len * 0.18); final Offset p1 = b + _rot(u, 155) * hs; final Offset p2 = b + _rot(u, -155) * hs; final Offset mid = (p1 + p2) / 2; out.add(b); for (int i = 1; i <= 4; i++) { out.add(Offset.lerp(b, p1, i / 4)! + n * _rnd(rnd, jitter * 0.5)); } for (int i = 1; i <= 4; i++) { out.add(i <= 2 ? Offset.lerp(p1, mid, i / 2)! : Offset.lerp(mid, p2, (i - 2) / 2)!); } return out; } /// 折线样本(矩形 / 三角形 / 菱形轮廓)。 List sketchPolyline( List pts, { int seed = 1, double step = 4, double jitter = 1.6, }) { final math.Random rnd = math.Random(seed); final List out = []; for (int i = 0; i < pts.length - 1; i++) { final Offset a = pts[i]; final Offset b = pts[i + 1]; final double len = (b - a).distance; if (len < 0.001) continue; final Offset u = (b - a) / len; final Offset n = Offset(-u.dy, u.dx); final int count = math.max(1, (len / step).floor()); for (int k = 0; k < count; k++) { out.add(a + u * (len * k / count) + n * _rnd(rnd, jitter) + u * _rnd(rnd, jitter * 0.4)); } } out.add(pts.last + Offset(_rnd(rnd, jitter), _rnd(rnd, jitter))); return out; } /// 曲线样本:平滑的三次贝塞尔(带抖动)。 List sketchCurve(Offset a, Offset b, {int seed = 1, double jitter = 1.6}) { final math.Random rnd = math.Random(seed); final Offset c1 = Offset(a.dx + 50, a.dy - 90); final Offset c2 = Offset(b.dx - 50, b.dy + 100); final List out = []; const int n = 40; for (int i = 0; i <= n; i++) { final double t = i / n; final double u = 1 - t; final Offset p = a * (u * u * u) + c1 * (3 * u * u * t) + c2 * (3 * u * t * t) + b * (t * t * t); out.add(p + Offset(_rnd(rnd, jitter), _rnd(rnd, jitter))); } return out; } /// 椭圆 / 圆样本(留收笔缺口)。 List sketchEllipse( Rect r, { int seed = 1, double jitter = 1.8, double gapFrac = 0.06, }) { final math.Random rnd = math.Random(seed); final List out = []; final int n = 90; final double stop = math.pi * 2 * (1 - gapFrac); for (int i = 0; i < n; i++) { final double a = stop * i / n; final Offset p = Offset( r.center.dx + r.width / 2 * math.cos(a), r.center.dy + r.height / 2 * math.sin(a), ); out.add(p + Offset(_rnd(rnd, jitter), _rnd(rnd, jitter))); } return out; }