import 'dart:math' as math; import '../domain/graph_definition.dart'; import 'graph_ranks.dart'; import 'layout_models.dart'; /// Places definition nodes in topological columns. /// /// Nodes keep definition order inside a rank, each column is as wide as its /// widest measured node, and columns are top-aligned. A node is centred in its /// column, so heterogeneous widths and text-expanded heights stay separated by /// [GraphLayoutConfig.rowGap] inside a rank and [GraphLayoutConfig.columnGap] /// between ranks. Nothing is clipped to a fixed box: the supplied measured /// width and height are used verbatim and the returned bounds include them. /// /// [nodes] and [edges] normally come from `graphNodes`/`graphEdges`. Edges whose /// source or target is not one of [nodes] are ignored, so an unknown target can /// never invent a node. A measurement is required for every node; a missing one /// is an error rather than a made-up size. Extra measurement entries are /// ignored because they cannot become nodes. GraphLayout layoutGraphNodes({ required List nodes, required List edges, required Map extents, required GraphLayoutConfig config, }) { _validateNodeKeys(nodes); final ranks = assignGraphRanks(nodes, edges); final measured = {}; final missing = []; for (final node in nodes) { final extent = extents[node.key]; if (extent == null) { missing.add(node.key); } else { measured[node.key] = extent; } } if (missing.isNotEmpty) { throw ArgumentError.value( missing, 'extents', 'missing measured extents for graph nodes', ); } final columns = List>.generate( ranks.rankCount, (_) => [], ); for (final node in nodes) { columns[ranks.rankOf[node.key]!].add(node); } final columnWidths = [ for (final column in columns) column.fold( 0, (width, node) => math.max(width, measured[node.key]!.width), ), ]; final columnHeights = [ for (final column in columns) _columnHeight(column, measured, config.rowGap), ]; final columnX = []; var nextX = config.padding; for (var rank = 0; rank < columns.length; rank += 1) { columnX.add(nextX); nextX += columnWidths[rank]; if (rank + 1 < columns.length) nextX += config.columnGap; } final boxes = []; for (var rank = 0; rank < columns.length; rank += 1) { var nextY = config.padding; for (final node in columns[rank]) { final extent = measured[node.key]!; boxes.add( GraphNodeBox( key: node.key, terminal: node.terminal, stageIndex: node.stageIndex, rank: rank, x: columnX[rank] + (columnWidths[rank] - extent.width) / 2, y: nextY, width: extent.width, height: extent.height, ), ); nextY += extent.height + config.rowGap; } } final width = config.padding * 2 + columnWidths.fold(0, (sum, column) => sum + column) + config.columnGap * math.max(0, columns.length - 1); final height = config.padding * 2 + columnHeights.fold( 0, (tallest, column) => math.max(tallest, column), ); return GraphLayout( nodes: boxes, width: width, height: height, rankCount: ranks.rankCount, ranks: ranks.rankOf, ); } void _validateNodeKeys(List nodes) { final seen = {}; for (final node in nodes) { if (node.key.isEmpty) { throw ArgumentError.value( node.key, 'nodes', 'node key must not be empty', ); } if (!seen.add(node.key)) { throw ArgumentError.value(node.key, 'nodes', 'duplicate graph node key'); } } } double _columnHeight( List column, Map measured, double rowGap, ) { if (column.isEmpty) return 0; var height = 0.0; for (final node in column) { height += measured[node.key]!.height; } return height + rowGap * (column.length - 1); }