1pub const SCALE: f64 = 30.0;
7
8pub const BOND_PX: f64 = SCALE;
10
11pub const FONT_FRAC: f64 = 0.45;
16
17pub const FONT_STEM_EM: f64 = 0.0933;
19
20pub const FONT_STEM_EM_BOLD: f64 = 0.144;
24
25pub const STROKE_FRAC: f64 = 0.042; pub const OFFSET_FRAC: f64 = 0.15;
31
32pub 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;
41pub const SHADE_FRAC: f64 = 0.72;
45pub const SHADE_BOTH_FRAC: f64 = SHADE_FRAC * 8.0 / 9.0;
47pub const SUBSCRIPT_SCALE: f64 = 0.66;
49
50pub const SUPERSCRIPT_SCALE: f64 = 0.90;
55
56pub const SCRIPT_SCALE: f64 = SUBSCRIPT_SCALE;
58pub 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;
73pub const HALO_GAP_PX: f64 = 0.5 * LABEL_GAP_PX;
75pub const HALO_OPACITY: f64 = 0.5;
76
77pub const WEIGHT_AT_ONE: f64 = 1.5;
83
84pub const WEIGHT_MIN: f64 = 1.0 / WEIGHT_AT_ONE;
86
87pub 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
102pub 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
114pub 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
124pub fn stroke_px_for_weight(weight: f64) -> f64 {
129 stroke_px_from_stem(FONT_STEM_EM) * diagram_weight(weight)
130}
131
132pub fn stroke_frac_from_stem(stem_em: f64) -> f64 {
134 (stem_em * FONT_FRAC * 1000.0).round() / 1000.0
135}
136
137pub fn stroke_px_from_stem(stem_em: f64) -> f64 {
139 stroke_frac_from_stem(stem_em) * BOND_PX
140}
141
142pub fn halo_stroke_from_stroke(stroke_px: f64) -> f64 {
144 2.0 * stroke_px
145}
146
147pub fn halo_stroke_for_weight(weight: f64) -> f64 {
152 HALO_STROKE * diagram_weight(weight).sqrt()
153}
154
155pub fn halo_gap_for_weight(weight: f64) -> f64 {
160 HALO_GAP_PX * diagram_weight(weight).sqrt()
161}
162
163pub 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 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 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}