Skip to main content

xpict_core/
metrics.rs

1//! Depiction proportions (xenopict / RDKit house style).
2//!
3//! Python host draw stack removed; this module is SoT.
4
5/// House bond length / CSS-px scale (was 20; ~1.5× for publication weight).
6pub const SCALE: f64 = 30.0;
7
8/// Bond length in drawing / CSS-px units (alias of [`SCALE`]).
9pub const BOND_PX: f64 = SCALE;
10
11/// Atom-label em as a fraction of bond length.
12///
13/// RDKit documents `baseFontSize = 0.6`, but MolDraw2D outlined heteroatom
14/// glyphs measure ~0.29 × bond in cap height (~0.45 em with Liberation Sans).
15pub const FONT_FRAC: f64 = 0.45;
16
17/// Liberation Sans Regular vertical stem width in em.
18pub const FONT_STEM_EM: f64 = 0.0933;
19
20/// Liberation Sans Bold vertical stem width in em (measured on H).
21///
22/// Reference measurement only — mol ink weight uses Regular + ``Shape::buffer``.
23pub const FONT_STEM_EM_BOLD: f64 = 0.144;
24
25/// Bond stroke as a fraction of bond length — tracks the label stem so ink
26/// weight matches letter stems when [`FONT_FRAC`] changes.
27pub const STROKE_FRAC: f64 = 0.042; // FONT_STEM_EM * FONT_FRAC, rounded
28
29/// RDKit `multipleBondOffset`.
30pub const OFFSET_FRAC: f64 = 0.15;
31
32/// Full wedge width at the fat end, as a fraction of bond length.
33pub const WEDGE_WIDTH_FRAC: f64 = 0.30;
34
35pub const PAD_FRAC: f64 = 1.0;
36pub const END_GAP_FRAC: f64 = 0.13;
37pub const CHAIN_END_GAP_FRAC: f64 = 0.0;
38pub const HASH_PER_BOND: i32 = 8;
39pub const HALO_FRAC: f64 = 2.0 * STROKE_FRAC;
40pub const LABEL_GAP_FRAC: f64 = 0.05;
41/// PlotDot outer radius as a fraction of bond length.
42///
43/// xenopict used 0.90; that reads ~25% too large at publication [`SCALE`].
44pub const SHADE_FRAC: f64 = 0.72;
45/// Shared PlotDot base when atom **and** bond scores paint (8/9 of [`SHADE_FRAC`]).
46pub const SHADE_BOTH_FRAC: f64 = SHADE_FRAC * 8.0 / 9.0;
47/// Fake-subscript scale for H-counts (RDKit ``DrawText::SUBS_SCALE``).
48pub const SUBSCRIPT_SCALE: f64 = 0.66;
49
50/// Fake-superscript scale for charges / caret markup.
51///
52/// Larger than RDKit's 0.66 so ``+`` / ``−`` stay readable on publication
53/// figures; H-count subscripts stay at [`SUBSCRIPT_SCALE`].
54pub const SUPERSCRIPT_SCALE: f64 = 0.90;
55
56/// Alias for [`SUBSCRIPT_SCALE`] (historical name used by older call sites).
57pub const SCRIPT_SCALE: f64 = SUBSCRIPT_SCALE;
58/// Dummy / R-group ``*`` height as a multiple of Liberation cap-height.
59///
60/// Custom five-point star (not the Liberation asterisk); ~2× letter height so
61/// attachment points read clearly next to heteroatom labels.
62pub const STAR_FRAC: f64 = 2.0;
63
64pub const STROKE_PX: f64 = STROKE_FRAC * BOND_PX;
65pub const OFFSET_PX: f64 = OFFSET_FRAC * BOND_PX;
66pub const WEDGE_HALF_PX: f64 = WEDGE_WIDTH_FRAC * BOND_PX * 0.5;
67pub const FONT_PX: f64 = FONT_FRAC * BOND_PX;
68pub const PAD_PX: f64 = PAD_FRAC * BOND_PX;
69pub const END_GAP_PX: f64 = END_GAP_FRAC * BOND_PX;
70pub const CHAIN_END_GAP_PX: f64 = CHAIN_END_GAP_FRAC * BOND_PX;
71pub const HALO_STROKE: f64 = HALO_FRAC * BOND_PX;
72pub const LABEL_GAP_PX: f64 = LABEL_GAP_FRAC * BOND_PX;
73/// Halo outline grow (half the label-gap air) + soft white fill.
74pub const HALO_GAP_PX: f64 = 0.5 * LABEL_GAP_PX;
75pub const HALO_OPACITY: f64 = 0.5;
76
77/// Absolute ink multiplier at user-facing ``weight = 1`` (house look).
78///
79/// Public ``weight`` is relative to this: default ``1`` → this multiplier;
80/// absolute floor stays ``1`` (Regular stem), so the user minimum is
81/// [`WEIGHT_MIN`] (= ``1 / WEIGHT_AT_ONE``).
82pub const WEIGHT_AT_ONE: f64 = 1.5;
83
84/// Lowest user-facing ``weight`` (maps to absolute ink ``1`` = Regular stem).
85pub const WEIGHT_MIN: f64 = 1.0 / WEIGHT_AT_ONE;
86
87/// Map user-facing mol ``weight`` → absolute ink multiplier (≥ 1).
88///
89/// Call this (or helpers that call it) with the public ``weight`` field — never
90/// with an already-converted absolute value.
91///
92/// # Panics
93/// Panics if ``weight`` is non-finite or ``< WEIGHT_MIN``.
94pub fn diagram_weight(weight: f64) -> f64 {
95    assert!(
96        weight.is_finite() && weight >= WEIGHT_MIN - 1e-12,
97        "mol weight must be finite and >= {WEIGHT_MIN}, got {weight}"
98    );
99    weight * WEIGHT_AT_ONE
100}
101
102/// Outward glyph buffer (px) so absolute ink stems grow past Regular.
103///
104/// ``grow = 0.5 × FONT_STEM_EM × FONT_PX × (ink - 1)`` where ``ink`` is
105/// [`diagram_weight`]; zero at [`WEIGHT_MIN`].
106///
107/// # Panics
108/// Panics if ``weight`` is invalid (see [`diagram_weight`]).
109pub fn label_weight_grow_px(weight: f64) -> f64 {
110    let ink = diagram_weight(weight);
111    0.5 * FONT_STEM_EM * FONT_PX * (ink - 1.0)
112}
113
114/// Extra bond↔label standoff (px) when absolute ink exceeds Regular.
115///
116/// Label buffer grow + half the extra bond stroke vs [`STROKE_PX`].
117///
118/// # Panics
119/// Panics if ``weight`` is invalid (see [`diagram_weight`]).
120pub fn label_weight_standoff_px(weight: f64) -> f64 {
121    label_weight_grow_px(weight) + 0.5 * (stroke_px_for_weight(weight) - STROKE_PX)
122}
123
124/// Bond stroke in drawing px for user-facing mol ``weight``.
125///
126/// # Panics
127/// Panics if ``weight`` is invalid (see [`diagram_weight`]).
128pub fn stroke_px_for_weight(weight: f64) -> f64 {
129    stroke_px_from_stem(FONT_STEM_EM) * diagram_weight(weight)
130}
131
132/// Bond stroke fraction from a stem width in em (rounded like [`STROKE_FRAC`]).
133pub fn stroke_frac_from_stem(stem_em: f64) -> f64 {
134    (stem_em * FONT_FRAC * 1000.0).round() / 1000.0
135}
136
137/// Bond stroke in drawing px keyed to a label stem.
138pub fn stroke_px_from_stem(stem_em: f64) -> f64 {
139    stroke_frac_from_stem(stem_em) * BOND_PX
140}
141
142/// Halo stroke width tracks active bond ink (2×, same ratio as [`HALO_FRAC`]).
143pub fn halo_stroke_from_stroke(stroke_px: f64) -> f64 {
144    2.0 * stroke_px
145}
146
147/// Halo stroke for mol ``weight``: [`HALO_STROKE`] × √ink (sublinear vs ink).
148///
149/// # Panics
150/// Panics if ``weight`` is invalid (see [`diagram_weight`]).
151pub fn halo_stroke_for_weight(weight: f64) -> f64 {
152    HALO_STROKE * diagram_weight(weight).sqrt()
153}
154
155/// Outer halo buffer for mol ``weight``: [`HALO_GAP_PX`] × √ink.
156///
157/// # Panics
158/// Panics if ``weight`` is invalid (see [`diagram_weight`]).
159pub fn halo_gap_for_weight(weight: f64) -> f64 {
160    HALO_GAP_PX * diagram_weight(weight).sqrt()
161}
162
163/// Dash count scaling with drawn bond length (hashed wedges).
164pub fn hash_count(length: f64) -> usize {
165    let n = (f64::from(HASH_PER_BOND) * length / BOND_PX).round() as i32;
166    n.clamp(5, 12) as usize
167}
168
169#[cfg(test)]
170mod tests {
171    use super::*;
172
173    #[test]
174    fn offset_matches_python_house_style() {
175        assert!((SCALE - 30.0).abs() < 1e-9);
176        assert!((OFFSET_PX - 4.5).abs() < 1e-9);
177        assert!((STROKE_PX - 1.26).abs() < 1e-9);
178        assert!((FONT_PX - 13.5).abs() < 1e-9);
179        assert!((SHADE_FRAC - 0.72).abs() < 1e-9);
180        assert!((SHADE_BOTH_FRAC - 0.64).abs() < 1e-9);
181    }
182
183    #[test]
184    fn stroke_tracks_font_stem() {
185        let expected = (FONT_STEM_EM * FONT_FRAC * 1000.0).round() / 1000.0;
186        assert!((STROKE_FRAC - expected).abs() < 1e-9);
187        assert!((stroke_px_from_stem(FONT_STEM_EM) - STROKE_PX).abs() < 1e-9);
188    }
189
190    #[test]
191    fn weight_scales_stroke_and_grow() {
192        // User weight 1 → house ink (WEIGHT_AT_ONE); WEIGHT_MIN → Regular stem.
193        assert!((diagram_weight(1.0) - WEIGHT_AT_ONE).abs() < 1e-9);
194        assert!((diagram_weight(WEIGHT_MIN) - 1.0).abs() < 1e-9);
195        assert!((stroke_px_for_weight(WEIGHT_MIN) - STROKE_PX).abs() < 1e-9);
196        assert!((stroke_px_for_weight(1.0) - WEIGHT_AT_ONE * STROKE_PX).abs() < 1e-9);
197        assert!((stroke_px_for_weight(2.0) - 2.0 * WEIGHT_AT_ONE * STROKE_PX).abs() < 1e-9);
198        assert!(label_weight_grow_px(WEIGHT_MIN).abs() < 1e-12);
199        let grow1 = label_weight_grow_px(1.0);
200        assert!((grow1 - 0.5 * FONT_STEM_EM * FONT_PX * (WEIGHT_AT_ONE - 1.0)).abs() < 1e-9);
201        assert!((halo_stroke_from_stroke(STROKE_PX) - 2.0 * STROKE_PX).abs() < 1e-9);
202        assert!((halo_stroke_for_weight(WEIGHT_MIN) - HALO_STROKE).abs() < 1e-9);
203        assert!((halo_stroke_for_weight(1.0) - HALO_STROKE * WEIGHT_AT_ONE.sqrt()).abs() < 1e-9);
204        // User weight whose absolute ink is 4 → halo ×2.
205        let w_abs4 = 4.0 / WEIGHT_AT_ONE;
206        assert!((halo_stroke_for_weight(w_abs4) - 2.0 * HALO_STROKE).abs() < 1e-9);
207        assert!((halo_gap_for_weight(w_abs4) - 2.0 * HALO_GAP_PX).abs() < 1e-9);
208        assert!(halo_stroke_for_weight(2.0) < halo_stroke_from_stroke(stroke_px_for_weight(2.0)));
209        assert!(label_weight_standoff_px(WEIGHT_MIN).abs() < 1e-12);
210        let stand1 = label_weight_standoff_px(1.0);
211        assert!(stand1 > label_weight_grow_px(1.0));
212        assert!(
213            (stand1 - (label_weight_grow_px(1.0) + 0.5 * (stroke_px_for_weight(1.0) - STROKE_PX)))
214                .abs()
215                < 1e-9
216        );
217    }
218
219    #[test]
220    #[should_panic(expected = "mol weight must be finite and >=")]
221    fn diagram_weight_rejects_below_min() {
222        let _ = diagram_weight(0.5);
223    }
224}