1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Sugiyama layout engine for component diagrams.
use std::{collections::HashMap, rc::Rc};
use log::debug;
use rust_sugiyama::configure::Config;
use orrery_core::{
draw::{self, Drawable},
geometry::{Insets, Point, Size},
identifier::Id,
semantic,
};
use crate::{
error::RenderError,
layout::{
component::{Component, Layout, LayoutRelation, adjust_positioned_contents_offset},
engines::{ComponentEngine, EmbeddedLayouts},
layer::{ContentStack, PositionedContent},
},
structure::{ComponentGraph, ContainmentScope},
};
/// Sugiyama-based layout engine for component diagrams.
///
/// Uses the Sugiyama algorithm for layered drawing of directed graphs.
/// Produces aesthetically pleasing layouts by minimizing edge
/// crossings between layers.
pub struct Engine {
/// Padding around text elements.
text_padding: f32,
/// Horizontal spacing between components.
horizontal_spacing: f32,
/// Vertical spacing between layers.
vertical_spacing: f32,
/// Container padding for nested components.
container_padding: Insets,
}
impl Engine {
/// Create a new Sugiyama component layout engine.
pub fn new() -> Self {
Self {
text_padding: 20.0,
horizontal_spacing: 50.0,
vertical_spacing: 80.0,
container_padding: Insets::uniform(20.0),
}
}
/// Set the text padding.
#[allow(dead_code)]
pub fn set_text_padding(&mut self, padding: f32) -> &mut Self {
self.text_padding = padding;
self
}
/// Set the horizontal spacing between components.
pub fn set_horizontal_spacing(&mut self, spacing: f32) -> &mut Self {
self.horizontal_spacing = spacing;
self
}
/// Set the vertical spacing between layers.
pub fn set_vertical_spacing(&mut self, spacing: f32) -> &mut Self {
self.vertical_spacing = spacing;
self
}
/// Set the padding inside container components.
pub fn set_container_padding(&mut self, padding: Insets) -> &mut Self {
self.container_padding = padding;
self
}
fn calculate_layout<'a>(
&self,
graph: &'a ComponentGraph<'a, '_>,
embedded_layouts: &EmbeddedLayouts<'a>,
) -> Result<ContentStack<Layout<'a>>, RenderError> {
let mut content_stack = ContentStack::<Layout<'a>>::new();
let mut positioned_content_sizes = HashMap::<Id, Size>::new();
for containment_scope in graph.containment_scopes() {
// Calculate component shapes - they contain all sizing information
let mut component_shapes = self.calculate_component_shapes(
graph,
containment_scope,
&positioned_content_sizes,
embedded_layouts,
)?;
// Extract sizes from shapes for position calculation
let component_sizes: HashMap<Id, Size> = component_shapes
.iter()
.map(|(idx, shape_with_text)| (*idx, shape_with_text.size()))
.collect();
// Calculate positions for components in this scope
let positions = self.positions(graph, containment_scope, &component_sizes)?;
// Build the final component list using the pre-configured shapes
let mut components: Vec<Component> = Vec::new();
for node in graph.scope_nodes(containment_scope) {
let position = *positions.get(&node.id()).ok_or_else(|| {
RenderError::Layout(format!("Position not found for node {node}"))
})?;
let shape_with_text = component_shapes.remove(&node.id()).ok_or_else(|| {
RenderError::Layout(format!("Shape not found for node {node}"))
})?;
components.push(Component::new(node, shape_with_text, position));
}
// Map node IDs to their component indices
let component_indices: HashMap<_, _> = components
.iter()
.enumerate()
.map(|(idx, component)| (component.node_id(), idx))
.collect();
// Build the list of relations between components
let relations: Vec<LayoutRelation> = graph
.scope_relations(containment_scope)
.filter_map(|relation| {
// Only include relations between visible components
// (not including relations within inner blocks)
if let (Some(&source_index), Some(&target_index)) = (
component_indices.get(&relation.source()),
component_indices.get(&relation.target()),
) {
Some(LayoutRelation::from_ast(
relation,
source_index,
target_index,
))
} else {
None
}
})
.collect();
let positioned_content = PositionedContent::new(Layout::new(components, relations));
if let Some(container) = containment_scope.container() {
// If this layer is a container, we need to adjust its size based on its contents
let size = positioned_content.layout_size();
positioned_content_sizes.insert(container, size);
}
content_stack.push(positioned_content);
}
adjust_positioned_contents_offset(&mut content_stack, graph)?;
Ok(content_stack)
}
/// Calculate component shapes with proper sizing and padding
fn calculate_component_shapes<'a>(
&self,
graph: &'a ComponentGraph<'a, '_>,
containment_scope: &ContainmentScope,
positioned_content_sizes: &HashMap<Id, Size>,
embedded_layouts: &EmbeddedLayouts<'a>,
) -> Result<HashMap<Id, draw::ShapeWithText<'a>>, RenderError> {
let mut component_shapes: HashMap<Id, draw::ShapeWithText<'a>> = HashMap::new();
// TODO: move it to the best place.
for node in graph.scope_nodes(containment_scope) {
let mut shape = draw::Shape::new(Rc::clone(node.shape_definition()));
shape.set_padding(self.container_padding);
let text = draw::Text::new(node.shape_definition().text(), node.display_text());
let mut shape_with_text = draw::ShapeWithText::new(shape, Some(text));
match node.block() {
semantic::Block::Diagram(_) => {
// Since we process in post-order (innermost to outermost),
// embedded diagram layouts should already be calculated and available
let layout = embedded_layouts.get(&node.id()).ok_or_else(|| {
RenderError::Layout(format!("Embedded layout not found for node {node}"))
})?;
let content_size = layout.calculate_size();
shape_with_text
.set_inner_content_size(content_size)
.map_err(|err| {
RenderError::Layout(format!(
"Failed to set content size for diagram block {node}: {err}"
))
})?;
}
semantic::Block::Scope(_) => {
let content_size =
*positioned_content_sizes.get(&node.id()).ok_or_else(|| {
RenderError::Layout(format!("Scope size not found for node {node}"))
})?;
shape_with_text
.set_inner_content_size(content_size)
.map_err(|err| {
RenderError::Layout(format!(
"Failed to set content size for scope block {node}: {err}"
))
})?;
}
semantic::Block::None => {
// No content to size, so don't call set_inner_content_size
}
};
component_shapes.insert(node.id(), shape_with_text);
}
Ok(component_shapes)
}
/// Calculate positions for components in a containment scope
fn positions<'a>(
&self,
graph: &ComponentGraph<'a, '_>,
containment_scope: &ContainmentScope,
component_sizes: &HashMap<Id, Size>,
) -> Result<HashMap<Id, Point>, RenderError> {
// Prepare layout
let mut positions = HashMap::new();
// Convert our graph to a format suitable for the Sugiyama algorithm
let mut edges = Vec::new();
let mut node_ids: HashMap<Id, u32> = HashMap::new();
// Get nodes for this containment scope
let scope_nodes: Vec<_> = graph.scope_nodes(containment_scope).collect();
// Map node IDs to u32 IDs for rust-sugiyama
for (i, node) in scope_nodes.iter().enumerate() {
let id = i as u32;
node_ids.insert(node.id(), id);
}
// Extract edges for this containment scope
for relation in graph.scope_relations(containment_scope) {
if let (Some(&source_id), Some(&target_id)) = (
node_ids.get(&relation.source()),
node_ids.get(&relation.target()),
) {
// Skip self-loops
if source_id != target_id {
edges.push((source_id, target_id));
}
}
}
if !edges.is_empty() {
debug!(
"Applying Sugiyama algorithm to graph with {} nodes and {} edges",
node_ids.len(),
edges.len()
);
// Calculate actual maximum component dimensions for adaptive spacing
let max_width = component_sizes
.values()
.map(|s| s.width())
.max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.unwrap_or(100.0);
let max_height = component_sizes
.values()
.map(|s| s.height())
.max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.unwrap_or(100.0);
// Calculate average node size for rust-sugiyama configuration
let avg_node_size = if !component_sizes.is_empty() {
component_sizes
.values()
.map(|s| (s.width() + s.height()) / 2.0)
.sum::<f32>()
/ component_sizes.len() as f32
} else {
100.0
};
// Create a bidirectional mapping between our original node IDs and sequential IDs
let id_to_node_id: HashMap<u32, Id> =
node_ids.iter().map(|(&node, &id)| (id, node)).collect();
// Try the rust_sugiyama crate with our sequential IDs, catching any panics
let layouts = std::panic::catch_unwind(move || {
// Configure with adaptive vertex spacing based on average component size
let config = Config {
minimum_length: 1,
vertex_spacing: (avg_node_size / 50.0).clamp(2.0, 5.0) as f64,
..Default::default()
};
rust_sugiyama::from_edges(&edges, &config)
});
// Process the layout results
match layouts {
// Success case with non-empty results
Ok(results) if !results.is_empty() => {
let (coords, _, _) = &results[0];
// Process coordinates safely
for &(id, (x, y)) in coords {
// Convert safely to u32 with bounds checking
let node_id = if (id as u64) <= (u32::MAX as u64) {
id as u32
} else {
debug!("Node ID {id} from rust-sugiyama result is out of valid range");
continue;
};
// Map the ID back to our original node Id
if let Some(&node_id) = id_to_node_id.get(&node_id) {
// Use adaptive spacing that accounts for actual component sizes
// Use a fraction of max size to avoid excessive spacing
let effective_h_spacing = self.horizontal_spacing + max_width * 0.5;
let effective_v_spacing = self.vertical_spacing + max_height * 0.5;
let x_pos = (x as f32) * effective_h_spacing;
let y_pos = (y as f32) * effective_v_spacing;
positions.insert(node_id, Point::new(x_pos, y_pos));
}
}
// If mapping failed for all nodes, return error
if positions.is_empty() {
return Err(RenderError::Layout(
"Failed to map any rust-sugiyama positions back to graph nodes"
.to_string(),
));
}
}
// Empty results case
Ok(results) if results.is_empty() => {
return Err(RenderError::Layout(
"Rust-sugiyama returned empty layout results".to_string(),
));
}
// Unexpected success case
Ok(_) => {
return Err(RenderError::Layout(
"Rust-sugiyama returned unexpected result format".to_string(),
));
}
// Error/panic case
Err(err) => {
let message = if let Some(panic_msg) = err.downcast_ref::<String>() {
format!("Rust-sugiyama layout engine panicked: {panic_msg}")
} else {
"Rust-sugiyama layout engine panicked with unknown error".to_string()
};
return Err(RenderError::Layout(message));
}
}
// Center the layout if we have positions
if !positions.is_empty() {
self.center_layout(&mut positions, component_sizes)?;
}
debug!(
"Layout generated with {} positioned nodes and positive coordinates",
positions.len(),
);
} else if !scope_nodes.is_empty() {
// No edges but we have nodes - arrange them horizontally with positive coordinates
debug!("Graph has no edges. Arranging nodes horizontally with positive coordinates.");
for (i, node) in scope_nodes.iter().enumerate() {
// For no-edge graphs, ensure adequate horizontal spacing and a margin from the top
let x =
self.horizontal_spacing * 0.8 + (i as f32) * (self.horizontal_spacing * 0.5);
positions.insert(node.id(), Point::new(x, self.vertical_spacing * 0.8));
}
}
Ok(positions)
}
fn center_layout(
&self,
positions: &mut HashMap<Id, Point>,
component_sizes: &HashMap<Id, Size>,
) -> Result<(), RenderError> {
if positions.is_empty() {
return Ok(());
}
// Calculate actual bounds considering component sizes
// Positions are component centers, so we need to account for half-widths/heights
let mut min_x = f32::MAX;
let mut min_y = f32::MAX;
for (node_id, point) in positions.iter() {
let size = component_sizes.get(node_id).ok_or_else(|| {
RenderError::Layout(format!("Component size not found for node {node_id}"))
})?;
let half_width = size.width() / 2.0;
let half_height = size.height() / 2.0;
// Calculate actual minimum bounds (left and top edges)
min_x = min_x.min(point.x() - half_width);
min_y = min_y.min(point.y() - half_height);
}
// Use minimal margin - the container shape provides the actual padding
// We just need to ensure coordinates are positive
let offset_x = min_x.min(0.0);
let offset_y = min_y.min(0.0);
for position in positions.values_mut() {
*position = position.sub_point(Point::new(offset_x, offset_y));
}
Ok(())
}
}
impl ComponentEngine for Engine {
fn calculate<'a>(
&self,
graph: &'a ComponentGraph<'a, '_>,
embedded_layouts: &EmbeddedLayouts<'a>,
) -> Result<ContentStack<Layout<'a>>, RenderError> {
self.calculate_layout(graph, embedded_layouts)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sugiyama_layout_basics() {
// Create a minimal engine and ensure it can be instantiated
let _engine = Engine::new();
}
}