pub const LABEL_POINTS: f32 = 11.0;
pub const EXPORT_FONT_SCALE: f32 = 1.25;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ScalarLegendPanel {
pub pad: f32,
pub width: f32,
pub height: f32,
pub halo: f32,
pub bar_offset: f32,
pub bar_height: f32,
pub label_points: f32,
pub note_height: f32,
}
impl ScalarLegendPanel {
pub fn for_plot(plot_width: f32, font_scale: f32, has_note: bool) -> Self {
let scale = font_scale.max(0.75);
let note_height = if has_note { 16.0 * scale } else { 0.0 };
Self {
pad: 12.0 * scale,
width: (plot_width * 0.34).clamp(190.0, 300.0),
height: 56.0 * scale + note_height,
halo: 6.0 * scale,
bar_offset: 23.0 * scale,
bar_height: 7.0 * scale,
label_points: LABEL_POINTS * scale,
note_height,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LegendFootprint {
pub label_px: f32,
pub width_share: f32,
pub height_share: f32,
}
pub fn exported_footprint(
width_px: u32,
height_px: u32,
pixels_per_point: f32,
has_note: bool,
) -> LegendFootprint {
let ppp = pixels_per_point.max(0.5);
let (width, height) = (width_px.max(1) as f32, height_px.max(1) as f32);
let panel = ScalarLegendPanel::for_plot(width / ppp, EXPORT_FONT_SCALE, has_note);
LegendFootprint {
label_px: panel.label_points * ppp,
width_share: ((panel.pad + panel.width + panel.halo) * ppp / width).min(1.0),
height_share: ((panel.pad + panel.height + panel.halo) * ppp / height).min(1.0),
}
}
pub fn export_pixels_per_point_for_label(label_px: f32) -> f32 {
label_px / (LABEL_POINTS * EXPORT_FONT_SCALE)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_footprint_follows_the_pixels_per_point() {
let ppp = export_pixels_per_point_for_label(18.75);
let footprint = exported_footprint(1200, 750, ppp, false);
assert!((footprint.label_px - 18.75).abs() < 1e-4);
let panel = ScalarLegendPanel::for_plot(1200.0 / ppp, EXPORT_FONT_SCALE, false);
assert_eq!(panel.width, 0.34 * 1200.0 / ppp);
assert!(footprint.width_share > 0.3 && footprint.width_share < 0.45);
assert!(footprint.height_share > 0.1 && footprint.height_share < 0.25);
let noted = exported_footprint(1200, 750, ppp, true);
assert!(noted.height_share > footprint.height_share);
assert_eq!(noted.width_share, footprint.width_share);
}
}