use lievisual::geometry::{Color, Size};
use lievisual::text::{RichSpan, TextAlign, TextBaseline, TextStyle, layout_text};
use crate::builder::ir::{self, common::*};
use crate::builder::theme;
const MIN_NODE_WIDTH: f64 = theme::NODE_MIN_W;
const MIN_NODE_HEIGHT: f64 = theme::NODE_MIN_H;
const NODE_PAD_X: f64 = theme::NODE_PAD_X;
const NODE_PAD_Y: f64 = theme::NODE_PAD_Y;
const FONT_SIZE: f64 = theme::FONT_SIZE;
fn text_style(size: f64, align: TextAlign, baseline: TextBaseline) -> TextStyle {
theme::text_style(Color::BLACK, size, align, baseline)
}
pub fn measure_all(mut ug: ir::Unigraph) -> ir::Unigraph {
ug.nodes = ug
.nodes
.into_iter()
.map(|mut n| {
let shape = n.shape;
let hint = n.size_hint.clone();
n.label = match &n.detail {
NodeDetail::None | NodeDetail::PieSlice { .. } | NodeDetail::GitCommit { .. } => {
measure_node_label(&n.label, shape, hint)
}
detail => measure_structured_label(&n.label, detail),
};
n
})
.collect();
for e in ug.edges.iter_mut() {
if let Some(text) = e.label_text.clone() {
let style = text_style(FONT_SIZE, TextAlign::Center, TextBaseline::Middle);
let layout = layout_text(&[RichSpan::new(text.clone(), style.clone())], None);
let size = Size::new(layout.width, layout.height);
e.label = Some(ir::common::MeasuredLabel {
text: text.clone(),
spans: vec![RichSpan::new(text, style)],
layout,
size,
});
}
}
ug
}
fn measure_node_label(
label: &LabelOrMeasured,
shape: ir::shape::ShapeKind,
size_hint: SizeHint,
) -> LabelOrMeasured {
if let LabelOrMeasured::Measured(_) = label {
return label.clone();
}
let LabelOrMeasured::Spec(spec) = label else {
return label.clone();
};
let text = spec.text.clone();
if let SizeHint::Fixed(fixed) = size_hint {
let style = text_style(FONT_SIZE, TextAlign::Center, TextBaseline::Middle);
let layout = layout_text(&[RichSpan::new(text.clone(), style.clone())], None);
return LabelOrMeasured::Measured(MeasuredLabel {
text: text.clone(),
spans: vec![RichSpan::new(text, style)],
layout,
size: fixed,
});
}
let text_style = text_style(FONT_SIZE, TextAlign::Center, TextBaseline::Middle);
let layout = layout_text(&[RichSpan::new(text.clone(), text_style.clone())], None);
let text_w = layout.width;
let text_h = layout.height;
let (pad_x, pad_y, square) = match size_hint {
SizeHint::Padded { pad_x, pad_y } => (pad_x, pad_y, false),
_ => shape_padding(shape),
};
let size = if shape == ir::shape::ShapeKind::Cylinder {
let w = text_w + 2.0 * pad_x;
let h = (text_h + 2.0 * pad_y) / 0.8;
Size::new(w, h)
} else if square {
let side = text_w.max(text_h) + 2.0 * pad_x.max(pad_y);
Size::new(side, side)
} else {
Size::new(text_w + 2.0 * pad_x, text_h + 2.0 * pad_y)
};
let size = Size::new(
size.width.max(MIN_NODE_WIDTH),
size.height.max(MIN_NODE_HEIGHT),
);
LabelOrMeasured::Measured(MeasuredLabel {
text: text.clone(),
spans: vec![RichSpan::new(text, text_style)],
layout,
size,
})
}
const CLASS_PAD: f64 = 12.0;
const ENTITY_PAD: f64 = 18.0;
const SMALL_FONT: f64 = theme::class::MEMBER_FONT_SIZE;
const SECTION_MIN_H: f64 = 18.0;
const ATTR_LINE_H: f64 = 24.0;
const ER_ATTR_GAP: f64 = 32.0;
fn measure_structured_label(label: &LabelOrMeasured, detail: &NodeDetail) -> LabelOrMeasured {
let LabelOrMeasured::Spec(spec) = label else {
return label.clone();
};
let name = spec.text.clone();
let header_style = text_style(SMALL_FONT, TextAlign::Center, TextBaseline::Middle);
let header_layout = layout_text(&[RichSpan::new(name.clone(), header_style.clone())], None);
let small_style = text_style(SMALL_FONT, TextAlign::Left, TextBaseline::Top);
let (width, height) = match detail {
NodeDetail::Class {
annotation,
attrs,
methods,
} => {
let ann_layout = annotation.as_ref().map(|a| {
layout_text(
&[RichSpan::new(format!("«{}»", a), small_style.clone())],
None,
)
});
let header_h = header_layout.height
+ 24.0
+ ann_layout.as_ref().map(|l| l.height + 4.0).unwrap_or(0.0);
let attr_h = if attrs.is_empty() {
SECTION_MIN_H
} else {
attrs.len() as f64 * ATTR_LINE_H
};
let method_h = if methods.is_empty() {
SECTION_MIN_H
} else {
methods.len() as f64 * ATTR_LINE_H
};
let mut max_w = header_layout.width + CLASS_PAD * 2.0;
if let Some(al) = &ann_layout {
max_w = max_w.max(al.width + CLASS_PAD * 2.0);
}
for line in attrs.iter().chain(methods.iter()) {
let l = layout_text(&[RichSpan::new(line.clone(), small_style.clone())], None);
max_w = max_w.max(l.width + CLASS_PAD * 2.0);
}
(max_w, header_h + attr_h + method_h)
}
NodeDetail::Entity { attrs } => {
let header_h = header_layout.height + 20.0;
let attr_h = (attrs.len() as f64 * ATTR_LINE_H).max(18.0);
let attr_style = text_style(FONT_SIZE, TextAlign::Left, TextBaseline::Top);
let mut type_w = 0.0f64;
let mut name_w = 0.0f64;
let mut constraint_w = 0.0f64;
for a in attrs {
let tl = layout_text(&[RichSpan::new(a.type_.clone(), attr_style.clone())], None);
let nl = layout_text(&[RichSpan::new(a.name.clone(), attr_style.clone())], None);
type_w = type_w.max(tl.width);
name_w = name_w.max(nl.width);
if let Some(c) = &a.constraint {
let cl = layout_text(&[RichSpan::new(c.clone(), attr_style.clone())], None);
constraint_w = constraint_w.max(cl.width);
}
}
let attrs_w = if attrs.is_empty() {
0.0
} else {
type_w
+ ER_ATTR_GAP
+ name_w
+ if constraint_w > 0.0 {
ER_ATTR_GAP + constraint_w
} else {
0.0
}
};
let max_w = header_layout.width.max(attrs_w) + ENTITY_PAD * 2.0;
(max_w, header_h + attr_h)
}
NodeDetail::TimelineSection { events } => {
let sec_w =
layout_text(&[RichSpan::new(name.clone(), small_style.clone())], None).width;
let mut max_ev = 0.0f64;
for ev in events {
let l = layout_text(&[RichSpan::new(ev.clone(), small_style.clone())], None);
max_ev = max_ev.max(l.width);
}
let events_h =
events.len() as f64 * (theme::timeline::BLOCK_H + theme::timeline::EVENT_GAP);
(
(sec_w.max(max_ev) + CLASS_PAD * 2.0).max(theme::timeline::BLOCK_W),
theme::timeline::BLOCK_H + events_h + 40.0,
)
}
NodeDetail::SequenceNote { text, .. } => {
let tl = layout_text(&[RichSpan::new(text.clone(), small_style.clone())], None);
(
tl.width + CLASS_PAD * 2.0,
tl.height.max(theme::timeline::BLOCK_H * 0.8) + CLASS_PAD * 2.0,
)
}
NodeDetail::None | NodeDetail::PieSlice { .. } | NodeDetail::GitCommit { .. } => {
let size = Size::new(
header_layout.width + 2.0 * NODE_PAD_X,
header_layout.height + 2.0 * NODE_PAD_Y,
);
return LabelOrMeasured::Measured(MeasuredLabel {
text: name.clone(),
spans: vec![RichSpan::new(name, header_style)],
layout: header_layout,
size,
});
}
};
LabelOrMeasured::Measured(MeasuredLabel {
text: name.clone(),
spans: vec![RichSpan::new(name, header_style)],
layout: header_layout,
size: Size::new(width, height),
})
}
fn shape_padding(shape: ir::shape::ShapeKind) -> (f64, f64, bool) {
use ir::shape::ShapeKind as S;
match shape {
S::Rectangle => (NODE_PAD_X, NODE_PAD_Y, false),
S::Rounded => (15.0, NODE_PAD_Y, false),
S::Circle | S::DoubleCircle | S::StartDot | S::EndDot => (7.5, 7.5, true),
S::Diamond => (27.0, 27.0, true),
S::Cylinder => (7.5, NODE_PAD_Y, false),
S::Stadium => (12.5, 7.5, false),
S::Subroutine => (15.5, 7.5, false),
S::Parallelogram | S::Trapezoid => (17.25, 7.5, false),
S::Asymmetric => (17.25, 7.5, false),
S::Hexagon => (25.0, 15.0, false),
S::Bar => (0.0, 0.0, false),
S::PieSlice => (0.0, 0.0, false),
}
}