Skip to main content

xpict_core/
debug_box.rs

1//! Debug bounding-box overlays for layout / margin inspection.
2//!
3//! Not for publication figures — a diagnostic aid for boxes and margins.
4//! When [`crate::scene::MoleculeIn::debug_box`] (or cascading opts) is on,
5//! depict emits stroked rectangles for label ink bounds and the mol canvas.
6//! Compose adds a document-level box around the full scene. Atom/bond stroke
7//! primitives are never wrapped. Strokes use partial opacity so stacked
8//! boxes remain distinguishable where they overlap.
9
10use crate::scene::Primitive;
11
12/// Stroke color for debug rectangles (high-contrast, not chem ink).
13pub const DEBUG_BOX_STROKE: &str = "#e91e63";
14/// Hairline in house SCALE units (scales with diagram scale).
15pub const DEBUG_BOX_STROKE_PX: f64 = 1.0;
16pub const DEBUG_BOX_DASH: &str = "4 2";
17/// Partial alpha so overlapping label / mol / scene borders stay readable.
18pub const DEBUG_BOX_OPACITY: f64 = 0.45;
19
20/// Axis-aligned debug rectangle as a closed path primitive.
21pub fn debug_rect(x: f64, y: f64, w: f64, h: f64, class: &str) -> Primitive {
22    let x2 = x + w;
23    let y2 = y + h;
24    Primitive::Path {
25        d: format!("M {:.2} {:.2} H {:.2} V {:.2} H {:.2} Z", x, y, x2, y2, x),
26        stroke: Some(DEBUG_BOX_STROKE.into()),
27        fill: Some("none".into()),
28        stroke_width: DEBUG_BOX_STROKE_PX,
29        opacity: DEBUG_BOX_OPACITY,
30        stroke_dasharray: Some(DEBUG_BOX_DASH.into()),
31        stroke_linecap: Some("butt".into()),
32        class: Some(class.into()),
33        data_text: None,
34    }
35}
36
37/// Label ink AABB → debug rect (`debug-box debug-box-label …`).
38pub fn label_debug_rect(
39    min_x: f64,
40    min_y: f64,
41    max_x: f64,
42    max_y: f64,
43    atom_index: i32,
44) -> Primitive {
45    debug_rect(
46        min_x,
47        min_y,
48        (max_x - min_x).max(0.0),
49        (max_y - min_y).max(0.0),
50        &format!("debug-box debug-box-label atom-{atom_index}"),
51    )
52}
53
54/// Mol viewport / canvas box.
55pub fn mol_debug_rect(width: f64, height: f64) -> Primitive {
56    debug_rect(0.0, 0.0, width, height, "debug-box debug-box-mol")
57}
58
59/// Full scene / document box.
60pub fn scene_debug_rect(width: f64, height: f64) -> Primitive {
61    debug_rect(0.0, 0.0, width, height, "debug-box debug-box-scene")
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn debug_rect_is_closed_path() {
70        let p = debug_rect(1.0, 2.0, 10.0, 20.0, "debug-box");
71        match p {
72            Primitive::Path {
73                d,
74                fill: Some(f),
75                stroke: Some(s),
76                opacity,
77                class: Some(c),
78                ..
79            } => {
80                assert!(d.ends_with('Z') || d.ends_with("Z"));
81                assert_eq!(f, "none");
82                assert_eq!(s, DEBUG_BOX_STROKE);
83                assert!(
84                    (opacity - DEBUG_BOX_OPACITY).abs() < 1e-9 && opacity < 1.0,
85                    "debug borders need alpha so overlaps are visible; got {opacity}"
86                );
87                assert!(c.contains("debug-box"));
88            }
89            _ => panic!("expected path"),
90        }
91    }
92}