use std::collections::HashMap;
use lievisual::geometry::{Color, Point, Rect, Size};
use lievisual::scene::{Fill, LineCap, LineJoin, Stroke};
use lievisual::text::{
FontWeight, LineMetrics, RichSpan, TextAlign, TextBaseline, TextLayout, TextStyle,
layout_metrics, layout_text,
};
use crate::builder::ir::{
self, SceneGraph, SceneItem, StyleIntent,
common::{ArrowKind, ArrowSpec},
geograph::{GGNode, Geograph},
shape::{EdgeEnds, ShapeGeometry, ShapeKind},
unigraph::EdgeKind,
};
use crate::builder::theme;
const NODE_STROKE_WIDTH: f64 = 1.0;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum HAnchor {
Left,
Center,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum VAnchor {
InkMiddle,
InkTop,
InkBottom,
Baseline,
}
fn place_text(
layout: &TextLayout,
style: &mut TextStyle,
anchor: Point,
h: HAnchor,
v: VAnchor,
) -> Point {
style.align = TextAlign::Left;
style.baseline = TextBaseline::Top;
let cb = layout.cap_bounds();
let cap_h = cb.height().max(0.0);
let x = match h {
HAnchor::Left => anchor.x,
HAnchor::Center => anchor.x - layout.width / 2.0,
};
let y = match v {
VAnchor::InkMiddle => {
if cap_h > 0.0 {
anchor.y - (cb.min_y() + cap_h / 2.0)
} else {
anchor.y - layout.height / 2.0
}
}
VAnchor::InkTop => anchor.y - cb.min_y(),
VAnchor::InkBottom => {
let lb = layout.logical_bounds();
if lb.height() > 0.0 {
anchor.y - lb.max_y()
} else {
anchor.y - layout.height
}
}
VAnchor::Baseline => anchor.y - layout_metrics(layout).alphabetic_baseline,
};
Point::new(x, y)
}
fn line_metrics(layout: &TextLayout) -> LineMetrics {
layout
.lines
.first()
.map(|l| l.metrics)
.unwrap_or_else(|| LineMetrics {
line_height: 0.0,
baseline: 0.0,
block_min: 0.0,
block_max: 0.0,
content_block_min: 0.0,
content_block_max: 0.0,
cap_height: 0.0,
x_height: 0.0,
})
}
fn text_bg_rect(layout: &TextLayout, box_left_top: Point, pad_x: f64, pad_y: f64) -> Rect {
let lb = layout.logical_bounds();
let (top, bottom) = if lb.height() > 0.0 {
(box_left_top.y + lb.min_y(), box_left_top.y + lb.max_y())
} else {
(box_left_top.y, box_left_top.y + layout.height)
};
Rect::new(
box_left_top.x - pad_x,
top - pad_y,
box_left_top.x + layout.width + pad_x,
bottom + pad_y,
)
}
pub fn run(gg: &Geograph, _style: &StyleIntent) -> SceneGraph {
let mut items = Vec::new();
let is_pie = gg
.nodes
.iter()
.any(|n| matches!(n.detail, ir::common::NodeDetail::PieSlice { .. }));
if is_pie {
emit_pie(&mut items, gg);
return SceneGraph {
size: gg.size,
background: gg.background,
items,
};
}
let is_gitgraph = gg
.nodes
.iter()
.any(|n| matches!(n.detail, ir::common::NodeDetail::GitCommit { .. }));
if is_gitgraph {
emit_gitgraph(&mut items, gg);
return SceneGraph {
size: gg.size,
background: gg.background,
items,
};
}
let is_sequence = gg
.nodes
.iter()
.any(|n| n.role == ir::common::NodeRole::Lifeline);
if is_sequence {
emit_sequence(&mut items, gg);
return SceneGraph {
size: gg.size,
background: gg.background,
items,
};
}
let is_timeline = gg
.nodes
.iter()
.any(|n| matches!(n.detail, ir::common::NodeDetail::TimelineSection { .. }));
if is_timeline {
emit_timeline(&mut items, gg);
}
for c in &gg.containers {
let r = c.bounds;
let is_composite = c.kind == ir::common::ContainerKind::StateComposite;
let (fill_color, stroke_color, title_pos) = if is_composite {
(
theme::flowchart::SUBGRAPH_FILL,
theme::state::STROKE,
Point::new(r.min_x() + r.width() / 2.0, r.min_y() + 13.0),
)
} else {
(
theme::flowchart::SUBGRAPH_FILL,
theme::flowchart::SUBGRAPH_STROKE,
Point::new(r.min_x() + r.width() / 2.0, r.min_y() + 12.0),
)
};
let container_stroke = Stroke {
color: stroke_color,
width: 1.0,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
};
items.push(SceneItem::Shape {
geometry: ShapeGeometry::RoundedRect {
at: Point::new(r.min_x(), r.min_y()),
size: Size::new(r.width(), r.height()),
radius: if is_composite { 5.0 } else { 0.0 },
},
fill: Some(Fill::Solid(fill_color)),
stroke: Some(container_stroke.clone()),
name: None,
z: -1,
});
if is_composite {
let inner_top = r.min_y() + 26.0;
let inner_h = (r.max_y() - 4.0 - inner_top).max(0.0);
if inner_h > 0.0 {
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Rect {
at: Point::new(r.min_x(), inner_top),
size: Size::new(r.width(), inner_h),
},
fill: Some(Fill::Solid(lievisual::geometry::Color::WHITE)),
stroke: None,
name: None,
z: -1,
});
}
}
if let Some(title) = &c.title
&& !title.is_empty()
{
let mut title_style = theme::text_style(
theme::TEXT_COLOR,
theme::FONT_SIZE,
TextAlign::Center,
TextBaseline::Middle,
);
let spans = vec![RichSpan::new(title.clone(), title_style.clone())];
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut title_style,
title_pos,
HAnchor::Center,
VAnchor::InkMiddle,
);
items.push(SceneItem::Label {
text: spans,
position,
style: title_style,
anchor: ir::scenegraph::Anchor::Left,
z: theme::Z_BACKGROUND,
});
}
}
for n in &gg.nodes {
match &n.detail {
ir::common::NodeDetail::Class { .. } => {
emit_class_box(&mut items, n);
continue;
}
ir::common::NodeDetail::Entity { .. } => {
emit_entity_box(&mut items, n);
continue;
}
ir::common::NodeDetail::TimelineSection { .. } => continue,
ir::common::NodeDetail::SequenceNote { .. } => {}
ir::common::NodeDetail::PieSlice { .. } => {}
ir::common::NodeDetail::GitCommit { .. } => {}
ir::common::NodeDetail::None => {}
}
let (fill, stroke) = node_fill_stroke(n.shape);
match n.shape {
ShapeKind::EndDot | ShapeKind::DoubleCircle => {
let outer = Stroke {
color: theme::state::STROKE,
width: 1.5,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
};
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Ellipse {
center: n.center,
rx: n.size.width / 2.0,
ry: n.size.height / 2.0,
},
fill: None,
stroke: Some(outer),
name: None,
z: theme::Z_BACKGROUND,
});
let inner_r = (n.size.width / 2.0) * 0.6;
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Ellipse {
center: n.center,
rx: inner_r,
ry: inner_r,
},
fill: Some(Fill::Solid(theme::state::STROKE)),
stroke: None,
name: None,
z: theme::Z_BACKGROUND,
});
}
_ => {
let geometry = shape_to_geometry(n.shape, n.center, n.size);
items.push(SceneItem::Shape {
geometry,
fill: Some(fill),
stroke: Some(stroke.clone()),
name: None,
z: theme::Z_BACKGROUND,
});
if n.shape == ShapeKind::Subroutine {
let half_w = n.size.width / 2.0;
let half_h = n.size.height / 2.0;
for sx in [-1.0, 1.0] {
let x = n.center.x + sx * half_w;
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[
Point::new(x, n.center.y - half_h),
Point::new(x, n.center.y + half_h),
]),
stroke: stroke.clone(),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_BACKGROUND,
});
}
}
}
}
if let Some(label) = &n.label
&& !label.text.is_empty()
&& !matches!(n.shape, ShapeKind::Bar)
{
let mut style = theme::text_style(
theme::flowchart::TEXT,
theme::FONT_SIZE,
TextAlign::Center,
TextBaseline::Middle,
);
let spans = label.spans.clone();
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut style,
n.center,
HAnchor::Center,
VAnchor::InkMiddle,
);
items.push(SceneItem::Label {
text: spans,
position,
style,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_SERIES,
});
}
}
for e in &gg.edges {
let (color, width) = edge_style(e.kind, e.line_kind);
let dash_array = match e.line_kind {
ir::common::LineKind::Dotted => vec![3.0, 4.0],
_ => Vec::new(),
};
let color = if e.line_kind == ir::common::LineKind::Invisible {
Color::new(0, 0, 0, 0)
} else {
color
};
let stroke = Stroke {
color,
width,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array,
dash_offset: 0.0,
miter_limit: 4.0,
};
let ends = edge_ends(e.kind, e.arrow);
items.push(SceneItem::Edge {
path: e.route.clone(),
stroke: stroke.clone(),
ends,
z: theme::Z_SUBGRAPH,
});
let (cs, ct) = e.cardinality;
if let Some(c) = cs {
emit_cardinality(
&mut items,
e.route.start(),
e.route.first_direction(),
c,
&stroke,
1,
);
}
if let Some(c) = ct {
let d = e.route.last_direction();
emit_cardinality(
&mut items,
e.route.end(),
Point::new(-d.x, -d.y),
c,
&stroke,
1,
);
}
if let Some(t) = &e.cardinality_text.0 {
emit_cardinality_text(&mut items, e.route.start(), e.route.first_direction(), t, 2);
}
if let Some(t) = &e.cardinality_text.1 {
let d = e.route.last_direction();
emit_cardinality_text(&mut items, e.route.end(), Point::new(-d.x, -d.y), t, 2);
}
let label_bg = match e.kind {
EdgeKind::StateTransition => None,
EdgeKind::Flow => Some(theme::flowchart::EDGE_LABEL_BG),
EdgeKind::Generic => Some(theme::er::EDGE_LABEL_BG),
EdgeKind::ClassExtends
| EdgeKind::ClassComposition
| EdgeKind::ClassAggregation
| EdgeKind::ClassAssociation
| EdgeKind::ClassDependency
| EdgeKind::ClassRealization
| EdgeKind::ClassLink
| EdgeKind::ClassDashed => Some(theme::class::EDGE_LABEL_BG),
_ => Some(theme::flowchart::EDGE_LABEL_BG),
};
if let (Some(text), Some(anchor)) = (&e.label_text, &e.label_anchor)
&& !text.is_empty()
{
let mut ts = theme::text_style(
theme::flowchart::TEXT,
theme::FONT_SIZE,
TextAlign::Center,
TextBaseline::Middle,
);
let spans = vec![RichSpan::new(text.clone(), ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
*anchor,
HAnchor::Center,
VAnchor::InkMiddle,
);
if let Some(bg_color) = label_bg {
let bg = text_bg_rect(&layout, position, 4.0, 2.0);
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Rect {
at: Point::new(bg.min_x(), bg.min_y()),
size: Size::new(bg.width(), bg.height()),
},
fill: Some(Fill::Solid(bg_color)),
stroke: None,
name: Some("edge-label".to_string()),
z: theme::Z_SUBGRAPH,
});
}
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_SERIES,
});
}
}
SceneGraph {
size: gg.size,
background: gg.background,
items,
}
}
const CLASS_PAD: f64 = 12.0;
const ENTITY_PAD: f64 = 18.0;
const SMALL_FONT: f64 = theme::class::MEMBER_FONT_SIZE;
const ATTR_LINE_H: f64 = 24.0;
const SECTION_MIN_H: f64 = 18.0;
const ER_ATTR_GAP: f64 = 32.0;
fn emit_cardinality(
items: &mut Vec<SceneItem>,
p: Point,
away: Point,
card: ir::common::ErCardinality,
stroke: &Stroke,
z: i32,
) {
use ir::common::ErCardinality::*;
let perp = Point::new(-away.y, away.x);
const SHORT_HALF: f64 = 4.0;
const STEP: f64 = 6.0;
let at = |idx: f64| Point::new(p.x + away.x * idx * STEP, p.y + away.y * idx * STEP);
let short = |items: &mut Vec<SceneItem>, center: Point| {
let s = Point::new(
center.x - perp.x * SHORT_HALF,
center.y - perp.y * SHORT_HALF,
);
let e = Point::new(
center.x + perp.x * SHORT_HALF,
center.y + perp.y * SHORT_HALF,
);
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[s, e]),
stroke: stroke.clone(),
ends: (EdgeEnds::None, EdgeEnds::None),
z,
});
};
let circle = |items: &mut Vec<SceneItem>, center: Point| {
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Ellipse {
center,
rx: 3.0,
ry: 3.0,
},
fill: None,
stroke: Some(stroke.clone()),
name: None,
z,
});
};
let brace = |items: &mut Vec<SceneItem>, center: Point| {
let top = Point::new(center.x - perp.x * 4.0, center.y - perp.y * 4.0);
let mid = Point::new(center.x + away.x * 3.5, center.y + away.y * 3.5);
let bot = Point::new(center.x + perp.x * 4.0, center.y + perp.y * 4.0);
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Polygon {
points: vec![top, mid, bot],
},
fill: None,
stroke: Some(stroke.clone()),
name: None,
z,
});
};
match card {
ExactlyOne => {
short(items, at(0.0));
short(items, at(1.0));
}
ZeroOrOne => {
short(items, at(0.0));
circle(items, at(1.0));
}
OneOrMany => {
short(items, at(0.0));
brace(items, at(1.0));
}
ZeroOrMany => {
circle(items, at(0.0));
brace(items, at(1.0));
}
}
}
fn emit_cardinality_text(items: &mut Vec<SceneItem>, p: Point, dir: Point, text: &str, z: i32) {
let perp = Point::new(-dir.y, dir.x);
let pos = Point::new(p.x + perp.x * 12.0, p.y + perp.y * 12.0);
let mut ts = theme::text_style(
theme::class::TEXT,
theme::FONT_SIZE,
TextAlign::Center,
TextBaseline::Middle,
);
let spans = vec![RichSpan::new(text.to_string(), ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(&layout, &mut ts, pos, HAnchor::Center, VAnchor::InkMiddle);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Center,
z,
});
}
fn class_stroke() -> Stroke {
Stroke {
color: theme::class::STROKE,
width: 1.0,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
}
}
fn emit_class_box(items: &mut Vec<SceneItem>, n: &GGNode) {
let half_w = n.size.width / 2.0;
let half_h = n.size.height / 2.0;
let rect = Rect::new(
n.center.x - half_w,
n.center.y - half_h,
n.center.x + half_w,
n.center.y + half_h,
);
let stroke = class_stroke();
let ir::common::NodeDetail::Class {
annotation,
attrs,
methods,
} = &n.detail
else {
return;
};
let header_layout_h = n.label.as_ref().map(|l| l.layout.height).unwrap_or(20.0);
let ann_extra_h = annotation
.as_ref()
.map(|a| {
let ts = theme::text_style(
theme::class::TEXT,
SMALL_FONT,
TextAlign::Left,
TextBaseline::Top,
);
layout_text(&[RichSpan::new(format!("«{}»", a), ts)], None).height + 4.0
})
.unwrap_or(0.0);
let ann_h = ann_extra_h;
let header_h = header_layout_h + 24.0 + ann_h;
let attr_h = if attrs.is_empty() {
SECTION_MIN_H
} else {
attrs.len() as f64 * ATTR_LINE_H
};
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Rect {
at: Point::new(rect.min_x(), rect.min_y()),
size: n.size,
},
fill: Some(Fill::Solid(theme::class::FILL)),
stroke: Some(stroke.clone()),
name: None,
z: theme::Z_BACKGROUND,
});
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Rect {
at: Point::new(rect.min_x(), rect.min_y()),
size: Size::new(n.size.width, header_h),
},
fill: Some(Fill::Solid(theme::class::HEADER_FILL)),
stroke: Some(stroke.clone()),
name: None,
z: theme::Z_SUBGRAPH,
});
if let Some(ann) = annotation {
let mut ts = theme::text_style(
theme::class::TEXT,
SMALL_FONT,
TextAlign::Center,
TextBaseline::Middle,
);
let spans = vec![RichSpan::new(format!("«{}»", ann), ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
Point::new(n.center.x, rect.min_y() + 9.0),
HAnchor::Center,
VAnchor::InkMiddle,
);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_SERIES,
});
}
if let Some(label) = &n.label {
let name_y = rect.min_y() + header_h / 2.0 + if annotation.is_some() { 6.0 } else { 0.0 };
let mut ts = theme::text_style(
theme::class::TEXT,
SMALL_FONT,
TextAlign::Center,
TextBaseline::Middle,
)
.with_weight(FontWeight::Bold);
let spans = label.spans.clone();
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
Point::new(n.center.x, name_y),
HAnchor::Center,
VAnchor::InkMiddle,
);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_SERIES,
});
}
for dy in [rect.min_y() + header_h, rect.min_y() + header_h + attr_h] {
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Rect {
at: Point::new(rect.min_x(), dy),
size: Size::new(n.size.width, 1.0),
},
fill: Some(Fill::Solid(theme::class::SEPARATOR)),
stroke: None,
name: None,
z: theme::Z_SUBGRAPH,
});
}
let mut line_y = rect.min_y() + header_h + 4.0;
for a in attrs {
let mut ts = theme::text_style(
theme::class::TEXT,
SMALL_FONT,
TextAlign::Left,
TextBaseline::Top,
);
let spans = vec![RichSpan::new(a.clone(), ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
Point::new(rect.min_x() + CLASS_PAD, line_y + ATTR_LINE_H / 2.0),
HAnchor::Left,
VAnchor::InkMiddle,
);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Left,
z: theme::Z_SERIES,
});
line_y += ATTR_LINE_H;
}
line_y = rect.min_y() + header_h + attr_h + 4.0;
for m in methods {
let mut ts = theme::text_style(
theme::class::TEXT,
SMALL_FONT,
TextAlign::Left,
TextBaseline::Top,
);
let spans = vec![RichSpan::new(m.clone(), ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
Point::new(rect.min_x() + CLASS_PAD, line_y + ATTR_LINE_H / 2.0),
HAnchor::Left,
VAnchor::InkMiddle,
);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Left,
z: theme::Z_SERIES,
});
line_y += ATTR_LINE_H;
}
}
fn emit_entity_box(items: &mut Vec<SceneItem>, n: &GGNode) {
let half_w = n.size.width / 2.0;
let half_h = n.size.height / 2.0;
let rect = Rect::new(
n.center.x - half_w,
n.center.y - half_h,
n.center.x + half_w,
n.center.y + half_h,
);
let stroke = Stroke {
color: theme::er::STROKE,
width: 1.0,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
};
let ir::common::NodeDetail::Entity { attrs } = &n.detail else {
return;
};
let header_h = n.label.as_ref().map(|l| l.layout.height).unwrap_or(20.0) + 20.0;
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Rect {
at: Point::new(rect.min_x(), rect.min_y()),
size: n.size,
},
fill: Some(Fill::Solid(theme::er::FILL)),
stroke: Some(stroke.clone()),
name: None,
z: theme::Z_BACKGROUND,
});
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Rect {
at: Point::new(rect.min_x(), rect.min_y()),
size: Size::new(n.size.width, header_h),
},
fill: Some(Fill::Solid(theme::er::HEADER_FILL)),
stroke: None,
name: None,
z: theme::Z_SUBGRAPH,
});
if let Some(label) = &n.label {
let mut ts = theme::text_style(
theme::er::TEXT,
theme::FONT_SIZE,
TextAlign::Center,
TextBaseline::Middle,
);
let spans = label.spans.clone();
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
Point::new(n.center.x, rect.min_y() + header_h / 2.0),
HAnchor::Center,
VAnchor::InkMiddle,
);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_LABEL,
});
}
let attr_ts = theme::text_style(
theme::er::TEXT,
theme::FONT_SIZE,
TextAlign::Left,
TextBaseline::Alphabetic,
);
let mut type_max = 0.0f64;
let mut name_max = 0.0f64;
let mut constraint_max = 0.0f64;
for a in attrs {
let tl = layout_text(&[RichSpan::new(a.type_.clone(), attr_ts.clone())], None);
let nl = layout_text(&[RichSpan::new(a.name.clone(), attr_ts.clone())], None);
type_max = type_max.max(tl.width);
name_max = name_max.max(nl.width);
if let Some(c) = &a.constraint {
let cl = layout_text(&[RichSpan::new(c.clone(), attr_ts.clone())], None);
constraint_max = constraint_max.max(cl.width);
}
}
let type_x = rect.min_x() + ENTITY_PAD;
let name_x = type_x + type_max + ER_ATTR_GAP;
let constraint_x = if constraint_max > 0.0 {
name_x + name_max + ER_ATTR_GAP
} else {
name_x
};
let header_line_y = rect.min_y() + header_h;
let divider_stroke = Stroke {
color: theme::er::STROKE,
width: 1.0,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
};
if !attrs.is_empty() {
let div1_x = name_x - ER_ATTR_GAP / 2.0;
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[
Point::new(div1_x, header_line_y),
Point::new(div1_x, rect.max_y()),
]),
stroke: divider_stroke.clone(),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_LABEL,
});
if constraint_max > 0.0 {
let div2_x = constraint_x - ER_ATTR_GAP / 2.0;
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[
Point::new(div2_x, header_line_y),
Point::new(div2_x, rect.max_y()),
]),
stroke: divider_stroke.clone(),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_LABEL,
});
}
}
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[
Point::new(rect.min_x(), header_line_y),
Point::new(rect.max_x(), header_line_y),
]),
stroke: divider_stroke.clone(),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_LABEL,
});
let mut line_y = rect.min_y() + header_h + 6.0;
for (i, a) in attrs.iter().enumerate() {
let row_top = line_y - 6.0;
let row_bottom = row_top + ATTR_LINE_H;
let row_fill = if i % 2 == 0 {
theme::er::ATTR_ROW_LIGHT
} else {
theme::er::ATTR_ROW_DARK
};
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Rect {
at: Point::new(rect.min_x(), row_top),
size: Size::new(rect.width(), row_bottom - row_top),
},
fill: Some(Fill::Solid(row_fill)),
stroke: None,
name: None,
z: theme::Z_SUBGRAPH,
});
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[
Point::new(rect.min_x(), row_bottom),
Point::new(rect.max_x(), row_bottom),
]),
stroke: divider_stroke.clone(),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_SERIES,
});
let type_spans = vec![RichSpan::new(a.type_.clone(), attr_ts.clone())];
let name_spans = vec![RichSpan::new(a.name.clone(), attr_ts.clone())];
let type_layout = layout_text(&type_spans, None);
let name_layout = layout_text(&name_spans, None);
let constraint_layout = a
.constraint
.as_ref()
.map(|c| layout_text(&[RichSpan::new(c.clone(), attr_ts.clone())], None));
let max_ascent = [
&type_layout,
&name_layout,
constraint_layout.as_ref().unwrap_or(&type_layout),
]
.iter()
.map(|l| line_metrics(l).ascent() as f64)
.fold(0.0, f64::max);
let max_descent = [
&type_layout,
&name_layout,
constraint_layout.as_ref().unwrap_or(&type_layout),
]
.iter()
.map(|l| line_metrics(l).descent() as f64)
.fold(0.0, f64::max);
let common_height = max_ascent + max_descent;
let baseline_y = row_top + (ATTR_LINE_H - common_height) / 2.0 + max_ascent;
let type_ts = attr_ts.clone();
let name_ts = attr_ts.clone();
items.push(SceneItem::Label {
text: type_spans,
position: Point::new(type_x, baseline_y),
style: type_ts,
anchor: ir::scenegraph::Anchor::Left,
z: theme::Z_TITLE,
});
items.push(SceneItem::Label {
text: name_spans,
position: Point::new(name_x, baseline_y),
style: name_ts,
anchor: ir::scenegraph::Anchor::Left,
z: theme::Z_TITLE,
});
if let Some(c) = &a.constraint {
let constraint_ts = attr_ts.clone();
let constraint_spans = vec![RichSpan::new(c.clone(), constraint_ts.clone())];
items.push(SceneItem::Label {
text: constraint_spans,
position: Point::new(constraint_x, baseline_y),
style: constraint_ts,
anchor: ir::scenegraph::Anchor::Left,
z: theme::Z_TITLE,
});
}
line_y += ATTR_LINE_H;
}
}
fn auto_commit_label(node_id: &str) -> String {
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
for b in node_id.as_bytes() {
h ^= *b as u64;
h = h.wrapping_mul(0x0000_0100_0000_01b3);
}
h ^= h >> 30;
h = h.wrapping_mul(0xbf58_476d_1ce4_e5b9);
h ^= h >> 27;
h = h.wrapping_mul(0x94d0_49bb_1331_11eb);
h ^= h >> 31;
let seq = node_id.trim_start_matches('c');
format!("{seq}-{:07x}", h & 0x0fff_ffff)
}
fn emit_gitgraph(items: &mut Vec<SceneItem>, gg: &Geograph) {
use ir::common::NodeDetail;
use ir::geograph::RouteSegment;
let node_by_id: HashMap<&str, &GGNode> = gg.nodes.iter().map(|n| (n.id.as_str(), n)).collect();
let branches: Vec<(String, Vec<String>)> = gg
.containers
.iter()
.filter(|c| c.kind == ir::common::ContainerKind::GitBranch)
.map(|c| (c.title.clone().unwrap_or_default(), c.member_ids.clone()))
.collect();
let branch_row: HashMap<&str, usize> = branches
.iter()
.enumerate()
.map(|(i, (n, _))| (n.as_str(), i))
.collect();
let branch_color = |name: &str| -> Color {
let i = branch_row.get(name).copied().unwrap_or(0);
theme::gitgraph::BRANCH_COLORS[i % theme::gitgraph::BRANCH_COLORS.len()]
};
let git_stroke = |color: Color, width: f64| Stroke {
color,
width,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
};
let cr = theme::gitgraph::COMMIT_RADIUS;
for n in &gg.nodes {
let NodeDetail::GitCommit {
branch,
id,
tag,
commit_type,
is_merge,
} = &n.detail
else {
continue;
};
let color = branch_color(branch);
let line_y = n.center.y;
let is_highlight = commit_type.as_deref() == Some("HIGHLIGHT");
if is_highlight {
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Rect {
at: Point::new(n.center.x - 10.0, line_y - 10.0),
size: Size::new(20.0, 20.0),
},
fill: Some(Fill::Solid(theme::gitgraph::HIGHLIGHT_OUTER)),
stroke: None,
name: None,
z: theme::Z_SUBGRAPH,
});
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Rect {
at: Point::new(n.center.x - 6.0, line_y - 6.0),
size: Size::new(12.0, 12.0),
},
fill: Some(Fill::Solid(theme::gitgraph::MERGE_INNER)),
stroke: None,
name: None,
z: theme::Z_SUBGRAPH,
});
} else if *is_merge {
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Ellipse {
center: n.center,
rx: cr,
ry: cr,
},
fill: Some(Fill::Solid(color)),
stroke: None,
name: None,
z: theme::Z_SUBGRAPH,
});
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Ellipse {
center: n.center,
rx: cr * 0.6,
ry: cr * 0.6,
},
fill: Some(Fill::Solid(theme::gitgraph::MERGE_INNER)),
stroke: None,
name: None,
z: theme::Z_SUBGRAPH,
});
} else {
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Ellipse {
center: n.center,
rx: cr,
ry: cr,
},
fill: Some(Fill::Solid(color)),
stroke: None,
name: None,
z: theme::Z_SUBGRAPH,
});
}
let commit_label = id.clone().or_else(|| {
if *is_merge {
None
} else {
Some(auto_commit_label(&n.id))
}
});
if let Some(text) = commit_label {
let mut ts = theme::text_style(
theme::gitgraph::COMMIT_LABEL_FILL,
theme::gitgraph::LABEL_FONT,
TextAlign::Center,
TextBaseline::Alphabetic,
);
let spans = vec![RichSpan::new(text, ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
Point::new(n.center.x, line_y + 25.0),
HAnchor::Center,
VAnchor::Baseline,
);
let bg = text_bg_rect(&layout, position, 2.0, 2.0);
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Rect {
at: Point::new(bg.min_x(), bg.min_y()),
size: Size::new(bg.width(), bg.height()),
},
fill: Some(Fill::Solid(theme::gitgraph::COMMIT_LABEL_BKG)),
stroke: None,
name: None,
z: theme::Z_BACKGROUND,
});
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_SERIES,
});
}
if let Some(tag_text) = tag {
let mut ts = theme::text_style(
theme::gitgraph::TAG_LABEL_FILL,
theme::gitgraph::LABEL_FONT,
TextAlign::Center,
TextBaseline::Alphabetic,
);
let spans = vec![RichSpan::new(tag_text.clone(), ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
Point::new(n.center.x, line_y - 16.0),
HAnchor::Center,
VAnchor::Baseline,
);
let bg = text_bg_rect(&layout, position, 4.0, 2.0);
let x_left = bg.min_x();
let x_right = bg.max_x();
let y_top = bg.min_y();
let y_bottom = bg.max_y();
let tip = 6.0;
let cy = (y_top + y_bottom) / 2.0;
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Polygon {
points: vec![
Point::new(x_left - tip, cy), Point::new(x_left, y_top),
Point::new(x_right, y_top),
Point::new(x_right, y_bottom),
Point::new(x_left, y_bottom),
],
},
fill: Some(Fill::Solid(theme::gitgraph::TAG_BKG)),
stroke: Some(git_stroke(theme::gitgraph::TAG_BKG_STROKE, 1.0)),
name: None,
z: theme::Z_BACKGROUND,
});
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_SERIES,
});
}
}
let curve_r = theme::gitgraph::BRANCH_SPACING / 3.0;
for e in &gg.edges {
let (Some(child), Some(parent)) = (
node_by_id.get(e.source.as_str()),
node_by_id.get(e.target.as_str()),
) else {
continue;
};
let (
NodeDetail::GitCommit {
branch: cb,
is_merge: cm,
..
},
NodeDetail::GitCommit { branch: pb, .. },
) = (&child.detail, &parent.detail)
else {
continue;
};
if cb == pb {
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[parent.center, child.center]),
stroke: git_stroke(branch_color(cb), theme::gitgraph::LINE_WIDTH),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_BACKGROUND,
});
} else {
let color = if *cm {
branch_color(pb)
} else {
branch_color(cb)
};
let (pc, cc) = (parent.center, child.center);
let mut path = ir::geograph::RoutePath::new();
if cc.y >= pc.y {
let a = Point::new(pc.x, cc.y - curve_r);
let p1 = Point::new(pc.x, cc.y); let p2 = Point::new((cc.x - curve_r).max(pc.x), cc.y); path.push(RouteSegment::Line { from: pc, to: a });
path.push(RouteSegment::CubicBezier {
p0: a,
p1,
p2,
p3: cc,
});
} else {
let a = Point::new(cc.x - curve_r, pc.y);
let p1 = Point::new(cc.x, pc.y); let p2 = Point::new(cc.x, (pc.y - curve_r).max(cc.y)); path.push(RouteSegment::Line { from: pc, to: a });
path.push(RouteSegment::CubicBezier {
p0: a,
p1,
p2,
p3: cc,
});
}
items.push(SceneItem::Edge {
path,
stroke: git_stroke(color, theme::gitgraph::LINE_WIDTH),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_BACKGROUND,
});
}
}
let min_x = gg
.nodes
.iter()
.map(|n| n.center.x)
.fold(f64::INFINITY, f64::min);
let max_x = gg
.nodes
.iter()
.map(|n| n.center.x)
.fold(f64::NEG_INFINITY, f64::max);
let branch_dash = Stroke {
color: Color::new(51, 51, 51, 255), width: 1.0,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: vec![2.0, 2.0],
dash_offset: 0.0,
miter_limit: 4.0,
};
for (i, (name, members)) in branches.iter().enumerate() {
let color = theme::gitgraph::BRANCH_COLORS[i % theme::gitgraph::BRANCH_COLORS.len()];
let y = theme::gitgraph::TOP_MARGIN + i as f64 * theme::gitgraph::BRANCH_SPACING;
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[
Point::new(min_x - 10.0, y),
Point::new(max_x + 40.0, y),
]),
stroke: branch_dash.clone(),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_BACKGROUND,
});
let mut ts_label = theme::text_style(
Color::rgb(255, 255, 255),
theme::FONT_SIZE,
TextAlign::Center,
TextBaseline::Middle,
);
let spans = vec![RichSpan::new(name.clone(), ts_label.clone())];
let layout = layout_text(&spans, None);
let pad_x = 12.0;
let pad_y = 6.0;
let lw = layout.width + pad_x * 2.0;
let logical_h = layout.logical_bounds().height();
let lh = if logical_h > 0.0 {
logical_h
} else {
layout.height
} + pad_y * 2.0;
let lx = theme::gitgraph::LEFT_MARGIN - lw - 16.0;
let ly = y - lh / 2.0;
let label_pos = place_text(
&layout,
&mut ts_label,
Point::new(lx + lw / 2.0, y),
HAnchor::Center,
VAnchor::InkMiddle,
);
items.push(SceneItem::Shape {
geometry: ShapeGeometry::RoundedRect {
at: Point::new(lx, ly),
size: Size::new(lw, lh),
radius: 4.0,
},
fill: Some(Fill::Solid(color)),
stroke: Some(git_stroke(color, 1.0)),
name: None,
z: theme::Z_BACKGROUND,
});
items.push(SceneItem::Label {
text: spans,
position: label_pos,
style: ts_label,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_SERIES,
});
if let Some(last_id) = members.last()
&& let Some(last) = node_by_id.get(last_id.as_str())
{
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[
Point::new(last.center.x + cr + 4.0, y),
Point::new(last.center.x + cr + 40.0, y),
]),
stroke: git_stroke(Color::new(180, 180, 180, 255), 1.0),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_BACKGROUND,
});
}
}
}
fn emit_pie(items: &mut Vec<SceneItem>, gg: &Geograph) {
let mut data: Vec<(String, f64)> = Vec::new();
for n in &gg.nodes {
if let ir::common::NodeDetail::PieSlice { label, value } = &n.detail {
data.push((label.clone(), *value));
}
}
if data.is_empty() {
return;
}
let total: f64 = data.iter().map(|(_, v)| v).sum();
if total <= 0.0 {
return;
}
let center = Point::new(0.0, 0.0);
let radius = theme::pie::RADIUS;
if let Some(title) = &gg.title
&& !title.is_empty()
{
let mut ts = theme::text_style(
Color::BLACK,
theme::pie::TITLE_FONT,
TextAlign::Center,
TextBaseline::Alphabetic,
);
let spans = vec![RichSpan::new(title.clone(), ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
Point::new(center.x, center.y - theme::pie::TITLE_DY),
HAnchor::Center,
VAnchor::Baseline,
);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_SERIES,
});
}
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Ellipse {
center,
rx: theme::pie::OUTER_RADIUS,
ry: theme::pie::OUTER_RADIUS,
},
fill: None,
stroke: Some(Stroke {
color: theme::pie::OUTER_STROKE,
width: theme::pie::OUTER_STROKE_WIDTH,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
}),
name: None,
z: theme::Z_SUBGRAPH,
});
let slice_stroke = Stroke {
color: theme::pie::SLICE_STROKE,
width: theme::pie::SLICE_STROKE_WIDTH,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
};
let slice_ts = theme::text_style(
Color::BLACK,
theme::pie::LABEL_FONT,
TextAlign::Center,
TextBaseline::Middle,
);
let mut start = -std::f64::consts::FRAC_PI_2;
for (idx, (_label, value)) in data.iter().enumerate() {
let sweep = 2.0 * std::f64::consts::PI * value / total;
let color = theme::pie::COLORS[idx % theme::pie::COLORS.len()];
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Pie {
center,
radius,
start_angle: start,
end_angle: sweep,
},
fill: Some(Fill::Solid(color)),
stroke: Some(slice_stroke.clone()),
name: None,
z: theme::Z_BACKGROUND,
});
let mid = start + sweep / 2.0;
let lr = radius * theme::pie::LABEL_RADIUS_RATIO;
let lp = Point::new(center.x + lr * mid.cos(), center.y + lr * mid.sin());
let pct = format!("{}%", (value / total * 100.0).round() as i64);
let mut pct_ts = slice_ts.clone();
let spans = vec![RichSpan::new(pct, pct_ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut pct_ts,
lp,
HAnchor::Center,
VAnchor::InkMiddle,
);
items.push(SceneItem::Label {
text: spans,
position,
style: pct_ts,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_SERIES,
});
start += sweep;
}
let n = data.len() as f64;
let legend_x = center.x + radius + theme::pie::LEGEND_DX;
let legend_top = center.y - (n - 1.0) / 2.0 * theme::pie::LEGEND_ROW_H;
let legend_ts = theme::text_style(
Color::BLACK,
theme::pie::LABEL_FONT,
TextAlign::Left,
TextBaseline::Middle,
);
for (idx, (label, value)) in data.iter().enumerate() {
let color = theme::pie::COLORS[idx % theme::pie::COLORS.len()];
let row_y = legend_top + idx as f64 * theme::pie::LEGEND_ROW_H;
let swatch_top = row_y - theme::pie::LEGEND_SWATCH / 2.0;
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Rect {
at: Point::new(legend_x, swatch_top),
size: Size::new(theme::pie::LEGEND_SWATCH, theme::pie::LEGEND_SWATCH),
},
fill: Some(Fill::Solid(color)),
stroke: Some(Stroke {
color,
width: 1.0,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
}),
name: None,
z: theme::Z_SUBGRAPH,
});
let text = if gg.show_data {
format!("{} [{}]", label, *value as i64)
} else {
label.clone()
};
let mut ts = legend_ts.clone();
let spans = vec![RichSpan::new(text, ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
Point::new(legend_x + theme::pie::LEGEND_TEXT_DX, row_y),
HAnchor::Left,
VAnchor::InkMiddle,
);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Left,
z: theme::Z_SERIES,
});
}
}
fn emit_sequence(items: &mut Vec<SceneItem>, gg: &Geograph) {
let lifelines: Vec<&GGNode> = gg
.nodes
.iter()
.filter(|n| n.role == ir::common::NodeRole::Lifeline)
.collect();
if lifelines.is_empty() {
return;
}
let msg_bottom = gg
.edges
.iter()
.flat_map(|e| e.route.anchors())
.map(|p| p.y)
.fold(0.0f64, f64::max);
let note_bottom = gg
.nodes
.iter()
.filter(|n| matches!(n.detail, ir::common::NodeDetail::SequenceNote { .. }))
.map(|n| n.center.y + n.size.height / 2.0)
.fold(0.0f64, f64::max);
let content_bottom = msg_bottom.max(note_bottom) + 20.0;
let lifeline_stroke = Stroke {
color: theme::sequence::LIFELINE,
width: theme::sequence::LIFELINE_WIDTH,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: theme::sequence::LIFELINE_DASH.to_vec(),
dash_offset: 0.0,
miter_limit: 4.0,
};
for n in &lifelines {
let half_w = n.size.width / 2.0;
let half_h = n.size.height / 2.0;
let rect = Rect::new(
n.center.x - half_w,
n.center.y - half_h,
n.center.x + half_w,
n.center.y + half_h,
);
items.push(SceneItem::Shape {
geometry: ShapeGeometry::RoundedRect {
at: Point::new(rect.min_x(), rect.min_y()),
size: n.size,
radius: theme::sequence::ACTOR_RADIUS,
},
fill: Some(Fill::Solid(theme::sequence::ACTOR_FILL)),
stroke: Some(Stroke {
color: theme::sequence::ACTOR_STROKE,
width: 1.0,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
}),
name: None,
z: theme::Z_BACKGROUND,
});
if let Some(label) = &n.label {
let mut ts = theme::text_style(
theme::sequence::ACTOR_TEXT,
theme::FONT_SIZE,
TextAlign::Center,
TextBaseline::Middle,
);
let spans = label.spans.clone();
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
n.center,
HAnchor::Center,
VAnchor::InkMiddle,
);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_SERIES,
});
}
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[
Point::new(n.center.x, n.center.y + half_h),
Point::new(n.center.x, content_bottom),
]),
stroke: lifeline_stroke.clone(),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_BACKGROUND,
});
}
for n in &lifelines {
let half_w = n.size.width / 2.0;
let bottom_y = content_bottom;
items.push(SceneItem::Shape {
geometry: ShapeGeometry::RoundedRect {
at: Point::new(n.center.x - half_w, bottom_y),
size: n.size,
radius: theme::sequence::ACTOR_RADIUS,
},
fill: Some(Fill::Solid(theme::sequence::ACTOR_FILL)),
stroke: Some(Stroke {
color: theme::sequence::ACTOR_STROKE,
width: 1.0,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
}),
name: None,
z: theme::Z_BACKGROUND,
});
if let Some(label) = &n.label {
let mut ts = theme::text_style(
theme::sequence::ACTOR_TEXT,
theme::FONT_SIZE,
TextAlign::Center,
TextBaseline::Middle,
);
let spans = label.spans.clone();
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
Point::new(n.center.x, bottom_y + n.size.height / 2.0),
HAnchor::Center,
VAnchor::InkMiddle,
);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_SERIES,
});
}
}
for a in &gg.activations {
let y0 = a.y0.min(a.y1);
let y1 = a.y0.max(a.y1);
if (y1 - y0).abs() < 1.0 {
continue;
}
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Rect {
at: Point::new(a.x - theme::sequence::ACTIVATION_WIDTH / 2.0, y0),
size: Size::new(theme::sequence::ACTIVATION_WIDTH, y1 - y0),
},
fill: Some(Fill::Solid(theme::sequence::ACTIVATION_FILL)),
stroke: Some(Stroke {
color: theme::sequence::ACTIVATION_STROKE,
width: 1.0,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
}),
name: None,
z: theme::Z_BACKGROUND,
});
}
for e in &gg.edges {
let (color, _w) = edge_style(e.kind, e.line_kind);
let width = theme::sequence::MESSAGE_WIDTH;
let dash_array = match e.line_kind {
ir::common::LineKind::Dotted => theme::sequence::MESSAGE_DASH.to_vec(),
_ => Vec::new(),
};
let stroke = Stroke {
color,
width,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array,
dash_offset: 0.0,
miter_limit: 4.0,
};
let ends = edge_ends(e.kind, e.arrow);
items.push(SceneItem::Edge {
path: e.route.clone(),
stroke: stroke.clone(),
ends,
z: theme::Z_SUBGRAPH,
});
if let (Some(text), Some(anchor)) = (&e.label_text, &e.label_anchor)
&& !text.is_empty()
{
let mut ts = theme::text_style(
theme::sequence::TEXT,
theme::FONT_SIZE,
TextAlign::Center,
TextBaseline::Bottom,
);
let spans = vec![RichSpan::new(text.clone(), ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
*anchor,
HAnchor::Center,
VAnchor::InkBottom,
);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_SERIES,
});
}
}
for n in &gg.nodes {
let ir::common::NodeDetail::SequenceNote { text, .. } = &n.detail else {
continue;
};
let half_w = n.size.width / 2.0;
let half_h = n.size.height / 2.0;
let rect = Rect::new(
n.center.x - half_w,
n.center.y - half_h,
n.center.x + half_w,
n.center.y + half_h,
);
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Rect {
at: Point::new(rect.min_x(), rect.min_y()),
size: n.size,
},
fill: Some(Fill::Solid(theme::sequence::NOTE_FILL)),
stroke: Some(Stroke {
color: theme::sequence::NOTE_STROKE,
width: 1.0,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
}),
name: None,
z: theme::Z_BACKGROUND,
});
let mut ts = theme::text_style(
theme::sequence::NOTE_TEXT,
theme::FONT_SIZE,
TextAlign::Center,
TextBaseline::Middle,
);
let spans = vec![RichSpan::new(text.clone(), ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
n.center,
HAnchor::Center,
VAnchor::InkMiddle,
);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_SERIES,
});
}
for c in &gg.containers {
if c.kind != ir::common::ContainerKind::SequenceBlock {
continue;
}
let r = c.bounds;
let stroke = Stroke {
color: theme::sequence::BLOCK_STROKE,
width: 2.0,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: vec![2.0, 2.0],
dash_offset: 0.0,
miter_limit: 4.0,
};
items.push(SceneItem::Shape {
geometry: ShapeGeometry::RoundedRect {
at: Point::new(r.min_x(), r.min_y()),
size: Size::new(r.width(), r.height()),
radius: theme::NODE_RADIUS,
},
fill: None,
stroke: Some(stroke),
name: None,
z: -1,
});
if let Some(label) = &c.title {
let (head, tail) = split_block_label(label);
let head_ts = theme::text_style(
theme::sequence::BLOCK_TEXT,
theme::FONT_SIZE,
TextAlign::Left,
TextBaseline::Alphabetic,
);
let head_layout = layout_text(&[RichSpan::new(head.clone(), head_ts.clone())], None);
let tab_w = (head_layout.width + 20.0).max(40.0);
let tab_x = r.min_x();
let tab_y = r.min_y();
let tab_h = 20.0;
let chamfer_x = 8.4;
let chamfer_y = 7.0;
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Polygon {
points: vec![
Point::new(tab_x, tab_y),
Point::new(tab_x + tab_w, tab_y),
Point::new(tab_x + tab_w, tab_y + tab_h - chamfer_y),
Point::new(tab_x + tab_w - chamfer_x, tab_y + tab_h),
Point::new(tab_x, tab_y + tab_h),
],
},
fill: Some(Fill::Solid(theme::sequence::BLOCK_FILL)),
stroke: Some(Stroke {
color: theme::sequence::BLOCK_STROKE,
width: 1.0,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
}),
name: None,
z: -1,
});
let baseline_y = tab_y + 13.0;
items.push(SceneItem::Label {
text: vec![RichSpan::new(head, head_ts.clone())],
position: Point::new(tab_x + tab_w / 2.0, baseline_y),
style: head_ts.clone(),
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_BACKGROUND,
});
if let Some(tail) = tail {
items.push(SceneItem::Label {
text: vec![RichSpan::new(tail, head_ts.clone())],
position: Point::new(tab_x + tab_w + 6.0, baseline_y),
style: head_ts,
anchor: ir::scenegraph::Anchor::Left,
z: theme::Z_BACKGROUND,
});
}
}
}
for d in &gg.sequence_dividers {
let Some(c) = gg.containers.iter().find(|c| c.id == d.block_id) else {
continue;
};
let r = c.bounds;
let stroke = Stroke {
color: theme::sequence::BLOCK_STROKE,
width: 1.0,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: vec![3.0, 3.0],
dash_offset: 0.0,
miter_limit: 4.0,
};
let mut path = ir::geograph::RoutePath::new();
path.push(ir::geograph::RouteSegment::Line {
from: Point::new(r.min_x(), d.y),
to: Point::new(r.max_x(), d.y),
});
items.push(SceneItem::Edge {
path,
stroke,
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_BACKGROUND,
});
if !d.label.is_empty() {
let mut ts = theme::text_style(
theme::sequence::BLOCK_TEXT,
theme::FONT_SIZE * 0.85,
TextAlign::Center,
TextBaseline::Middle,
);
let spans = vec![RichSpan::new(d.label.clone(), ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
Point::new((r.min_x() + r.max_x()) / 2.0, d.y + 14.0),
HAnchor::Center,
VAnchor::InkMiddle,
);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_BACKGROUND,
});
}
}
}
fn split_block_label(label: &str) -> (String, Option<String>) {
match label.find(" [") {
Some(i) => (label[..i].to_string(), Some(label[i + 1..].to_string())),
None => (label.to_string(), None),
}
}
fn emit_timeline(items: &mut Vec<SceneItem>, gg: &Geograph) {
let sections: Vec<&GGNode> = gg
.nodes
.iter()
.filter(|n| matches!(n.detail, ir::common::NodeDetail::TimelineSection { .. }))
.collect();
if sections.is_empty() {
return;
}
use ir::common::NodeDetail;
let axis_stroke = Stroke {
color: theme::timeline::LINE,
width: theme::timeline::AXIS_WIDTH,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
};
let horizontal = if sections.len() >= 2 {
let a = sections[0].center;
let b = sections[1].center;
(b.y - a.y).abs() < (b.x - a.x).abs()
} else {
true
};
let axis_c = if horizontal {
sections[0].center.y
} else {
sections[0].center.x
};
let mut min_a = f64::INFINITY;
let mut max_a = f64::NEG_INFINITY;
for s in §ions {
let c = if horizontal { s.center.x } else { s.center.y };
min_a = min_a.min(c);
max_a = max_a.max(c);
}
let span_mid = (min_a + max_a) / 2.0;
let _ = span_mid;
if let Some(title) = &gg.title
&& !title.is_empty()
{
let mut ts = theme::text_style(
theme::timeline::TITLE,
theme::timeline::TITLE_SIZE,
TextAlign::Left,
TextBaseline::Alphabetic,
);
let sec_top_off = theme::timeline::SECTION_DY + theme::timeline::BLOCK_H / 2.0 + 78.0;
let axis_start_off = theme::timeline::BLOCK_W / 2.0 + theme::timeline::AXIS_HEAD;
let pos = if horizontal {
Point::new(min_a - axis_start_off, axis_c - sec_top_off)
} else {
Point::new(axis_c - sec_top_off, min_a - axis_start_off)
};
let spans = vec![RichSpan::new(title.clone(), ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(&layout, &mut ts, pos, HAnchor::Left, VAnchor::InkTop);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Left,
z: theme::Z_SERIES,
});
}
let arr = theme::timeline::ARROW_SIZE;
if horizontal {
let y = axis_c;
let x0 = min_a - theme::timeline::BLOCK_W / 2.0 - theme::timeline::AXIS_HEAD;
let x1 = max_a + theme::timeline::BLOCK_W / 2.0 + theme::timeline::AXIS_TAIL;
let tip = x1 - arr * 1.5;
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[Point::new(x0, y), Point::new(tip, y)]),
stroke: axis_stroke.clone(),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_BACKGROUND,
});
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[
Point::new(tip, y),
Point::new(tip - arr * 0.7, y - arr * 0.5),
]),
stroke: axis_stroke.clone(),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_BACKGROUND,
});
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[
Point::new(tip, y),
Point::new(tip - arr * 0.7, y + arr * 0.5),
]),
stroke: axis_stroke.clone(),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_BACKGROUND,
});
} else {
let x = axis_c;
let y0 = min_a - theme::timeline::BLOCK_W / 2.0 - theme::timeline::AXIS_HEAD;
let y1 = max_a + theme::timeline::BLOCK_W / 2.0 + theme::timeline::AXIS_TAIL;
let tip = y1 - arr * 1.5;
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[Point::new(x, y0), Point::new(x, tip)]),
stroke: axis_stroke.clone(),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_BACKGROUND,
});
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[
Point::new(x, tip),
Point::new(x - arr * 0.5, tip - arr * 0.7),
]),
stroke: axis_stroke.clone(),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_BACKGROUND,
});
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[
Point::new(x, tip),
Point::new(x + arr * 0.5, tip - arr * 0.7),
]),
stroke: axis_stroke.clone(),
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_BACKGROUND,
});
}
for (idx, s) in sections.iter().enumerate() {
let color = theme::timeline::block_color(idx);
let text_color = theme::timeline::block_text_color(idx);
let (cx, cy) = if horizontal {
(s.center.x, axis_c)
} else {
(axis_c, s.center.y)
};
let NodeDetail::TimelineSection { events } = &s.detail else {
continue;
};
let label_text = s.label.as_ref().map(|l| l.text.clone()).unwrap_or_default();
let sec_center = if horizontal {
Point::new(cx, cy - theme::timeline::SECTION_DY)
} else {
Point::new(cx - theme::timeline::SECTION_DY, cy)
};
emit_timeline_block(
items,
sec_center,
color,
text_color,
idx,
&label_text,
theme::timeline::BLOCK_H,
);
let mut last_block_bottom = if horizontal {
sec_center.y
} else {
sec_center.x
};
if let Some(period) = events.first() {
let per_center = if horizontal {
Point::new(cx, cy - theme::timeline::PERIOD_DY)
} else {
Point::new(cx - theme::timeline::PERIOD_DY, cy)
};
emit_timeline_block(
items,
per_center,
color,
text_color,
idx,
period,
theme::timeline::BLOCK_H,
);
last_block_bottom = if horizontal {
per_center.y
} else {
per_center.x
};
}
let mut last_event_bottom: Option<f64> = None;
for (j, ev) in events.iter().skip(1).enumerate() {
let off = theme::timeline::EVENT_DY
+ j as f64 * (theme::timeline::EVENT_H + theme::timeline::EVENT_GAP);
let ev_center = if horizontal {
Point::new(cx, cy + off)
} else {
Point::new(cx + off, cy)
};
emit_timeline_block(
items,
ev_center,
color,
text_color,
idx,
ev,
theme::timeline::EVENT_H,
);
last_event_bottom = Some(if horizontal { ev_center.y } else { ev_center.x });
}
if let Some(ev_bottom) = last_event_bottom {
let (from, to) = if horizontal {
(
Point::new(cx, last_block_bottom + theme::timeline::BLOCK_H / 2.0),
Point::new(
cx,
ev_bottom
+ theme::timeline::EVENT_H / 2.0
+ theme::timeline::CONNECTOR_TAIL,
),
)
} else {
(
Point::new(last_block_bottom + theme::timeline::BLOCK_H / 2.0, cy),
Point::new(
ev_bottom
+ theme::timeline::EVENT_H / 2.0
+ theme::timeline::CONNECTOR_TAIL,
cy,
),
)
};
let stroke = Stroke {
color: theme::timeline::LINE,
width: theme::timeline::CONNECTOR_W,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: vec![5.0, 5.0],
dash_offset: 0.0,
miter_limit: 4.0,
};
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[from, to]),
stroke,
ends: (EdgeEnds::None, EdgeEnds::Arrow),
z: theme::Z_GRID,
});
}
}
}
fn emit_timeline_block(
items: &mut Vec<SceneItem>,
center: Point,
color: Color,
text_color: Color,
idx: usize,
text: &str,
height: f64,
) {
let w = theme::timeline::BLOCK_W;
let at = Point::new(center.x - w / 2.0, center.y - height / 2.0);
let r = theme::timeline::BLOCK_RX.min(height / 2.0).min(w / 2.0);
let steps = 4;
let mut pts = vec![Point::new(at.x, at.y + height)];
for i in 0..=steps {
let t = std::f64::consts::PI * (1.0 + i as f64 / steps as f64); pts.push(Point::new(at.x + r + r * t.cos(), at.y + r + r * t.sin()));
}
for i in 0..=steps {
let t = std::f64::consts::PI * (1.5 + i as f64 / steps as f64); pts.push(Point::new(
at.x + w - r + r * t.cos(),
at.y + r + r * t.sin(),
));
}
pts.push(Point::new(at.x + w, at.y + height));
pts.push(Point::new(at.x, at.y + height));
items.push(SceneItem::Shape {
geometry: ShapeGeometry::Polygon { points: pts },
fill: Some(Fill::Solid(color)),
stroke: None,
name: None,
z: theme::Z_SERIES,
});
items.push(SceneItem::Edge {
path: ir::geograph::line_route(&[
Point::new(at.x, at.y + height),
Point::new(at.x + w, at.y + height),
]),
stroke: Stroke {
color: theme::timeline::block_line_color(idx),
width: theme::timeline::BLOCK_LINE_W,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
},
ends: (EdgeEnds::None, EdgeEnds::None),
z: theme::Z_SERIES,
});
let mut ts = theme::text_style(
text_color,
theme::timeline::BLOCK_TEXT_SIZE,
TextAlign::Center,
TextBaseline::Middle,
);
let spans = vec![RichSpan::new(text.to_string(), ts.clone())];
let layout = layout_text(&spans, None);
let position = place_text(
&layout,
&mut ts,
center,
HAnchor::Center,
VAnchor::InkMiddle,
);
items.push(SceneItem::Label {
text: spans,
position,
style: ts,
anchor: ir::scenegraph::Anchor::Center,
z: theme::Z_SERIES,
});
}
fn node_fill_stroke(shape: ShapeKind) -> (Fill, Stroke) {
let default_stroke = Stroke {
color: theme::flowchart::STROKE,
width: NODE_STROKE_WIDTH,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
dash_array: Vec::new(),
dash_offset: 0.0,
miter_limit: 4.0,
};
match shape {
ShapeKind::StartDot => (
Fill::Solid(theme::state::SPECIAL),
Stroke {
width: 0.0,
..default_stroke
},
),
ShapeKind::Bar => (
Fill::Solid(theme::state::SPECIAL),
Stroke {
width: 0.0,
..default_stroke
},
),
_ => (Fill::Solid(theme::flowchart::FILL), default_stroke),
}
}
fn edge_style(kind: EdgeKind, line: ir::common::LineKind) -> (Color, f64) {
let width = if line == ir::common::LineKind::Thick {
theme::EDGE_WIDTH_THICK
} else {
theme::EDGE_WIDTH
};
let color = match kind {
EdgeKind::ClassExtends
| EdgeKind::ClassComposition
| EdgeKind::ClassAggregation
| EdgeKind::ClassAssociation
| EdgeKind::ClassDependency
| EdgeKind::ClassRealization
| EdgeKind::ClassLink
| EdgeKind::ClassDashed => theme::class::EDGE,
_ => theme::flowchart::EDGE,
};
(color, width)
}
fn map_ends(kind: ArrowKind) -> EdgeEnds {
match kind {
ArrowKind::Arrow => EdgeEnds::Arrow,
ArrowKind::Circle => EdgeEnds::Circle,
ArrowKind::Cross => EdgeEnds::Cross,
ArrowKind::None => EdgeEnds::None,
}
}
fn edge_ends(kind: EdgeKind, arrow: ArrowSpec) -> (EdgeEnds, EdgeEnds) {
match kind {
EdgeKind::ClassExtends => (EdgeEnds::Triangle, EdgeEnds::None),
EdgeKind::ClassComposition => (EdgeEnds::DiamondFilled, EdgeEnds::None),
EdgeKind::ClassAggregation => (EdgeEnds::DiamondHollow, EdgeEnds::None),
EdgeKind::ClassAssociation => (EdgeEnds::None, EdgeEnds::TriangleFilled),
EdgeKind::ClassDependency | EdgeKind::ClassRealization => {
(EdgeEnds::None, EdgeEnds::ArrowOpen)
}
_ => (map_ends(arrow.start), map_ends(arrow.end)),
}
}
const PARALLELOGRAM_SKEW: f64 = 19.5;
const SUBROUTINE_BAR_GAP: f64 = 8.0;
fn shape_to_geometry(shape: ShapeKind, center: Point, size: Size) -> ShapeGeometry {
let half_w = size.width / 2.0;
let half_h = size.height / 2.0;
let tl = Point::new(center.x - half_w, center.y - half_h);
match shape {
ShapeKind::Rectangle | ShapeKind::Bar => ShapeGeometry::Rect { at: tl, size },
ShapeKind::Rounded => ShapeGeometry::RoundedRect {
at: tl,
size,
radius: theme::NODE_RADIUS,
},
ShapeKind::Subroutine => ShapeGeometry::Rect {
at: Point::new(tl.x + SUBROUTINE_BAR_GAP, tl.y),
size: Size::new(
(size.width - 2.0 * SUBROUTINE_BAR_GAP).max(0.0),
size.height,
),
},
ShapeKind::Stadium => ShapeGeometry::Stadium { at: tl, size },
ShapeKind::Diamond => ShapeGeometry::Polygon {
points: vec![
Point::new(center.x, center.y - half_h),
Point::new(center.x + half_w, center.y),
Point::new(center.x, center.y + half_h),
Point::new(center.x - half_w, center.y),
],
},
ShapeKind::Hexagon => {
let w = half_w * 0.5;
ShapeGeometry::Polygon {
points: vec![
Point::new(center.x - w, center.y - half_h),
Point::new(center.x + w, center.y - half_h),
Point::new(center.x + half_w, center.y),
Point::new(center.x + w, center.y + half_h),
Point::new(center.x - w, center.y + half_h),
Point::new(center.x - half_w, center.y),
],
}
}
ShapeKind::Circle | ShapeKind::StartDot | ShapeKind::EndDot | ShapeKind::DoubleCircle => {
ShapeGeometry::Ellipse {
center,
rx: half_w,
ry: half_h,
}
}
ShapeKind::Asymmetric => {
let skew = half_w * 0.25;
ShapeGeometry::Polygon {
points: vec![
Point::new(tl.x + skew, tl.y),
Point::new(tl.x + size.width, tl.y),
Point::new(tl.x + size.width - skew, tl.y + size.height),
Point::new(tl.x, tl.y + size.height),
],
}
}
ShapeKind::Parallelogram => {
let skew = PARALLELOGRAM_SKEW;
ShapeGeometry::Polygon {
points: vec![
Point::new(tl.x - skew, tl.y),
Point::new(tl.x + size.width - skew, tl.y),
Point::new(tl.x + size.width, tl.y + size.height),
Point::new(tl.x, tl.y + size.height),
],
}
}
ShapeKind::Trapezoid => {
let skew = PARALLELOGRAM_SKEW;
ShapeGeometry::Polygon {
points: vec![
Point::new(tl.x, tl.y),
Point::new(tl.x + size.width, tl.y),
Point::new(tl.x + size.width - skew, tl.y + size.height),
Point::new(tl.x - skew, tl.y + size.height),
],
}
}
ShapeKind::Cylinder => {
let ry = half_h * 0.29;
let top = center.y - half_h + ry;
let bottom = center.y + half_h - ry;
let mut pts = Vec::new();
let steps = 12;
for i in 0..=steps {
let t = std::f64::consts::PI * (i as f64 / steps as f64);
let x = center.x - half_w * (t.cos());
let y = top - ry * (t.sin());
pts.push(Point::new(x, y));
}
pts.push(Point::new(center.x + half_w, bottom));
for i in 0..=steps {
let t = std::f64::consts::PI * (i as f64 / steps as f64);
let x = center.x + half_w * (t.cos());
let y = bottom + ry * (t.sin());
pts.push(Point::new(x, y));
}
pts.push(Point::new(center.x - half_w, top));
ShapeGeometry::Polygon { points: pts }
}
ShapeKind::PieSlice => ShapeGeometry::Pie {
center,
radius: half_w.max(half_h),
start_angle: 0.0,
end_angle: std::f64::consts::FRAC_PI_2,
},
}
}