use crate::config::{AxisOptions, ChartTitleOptions, Config};
mod fit;
mod geometry;
mod nudge;
mod rect;
pub use fit::FitStrategy;
pub use geometry::{
LABEL_GAP, LEGEND_INSET, TitleBand, TitlePlacement, axis_anchor, axis_offset,
axis_title_offset_to_screen, axis_title_placement, axis_visibility_extent,
axis_visibility_rect, chart_title_placement, colorbar_rect, colorbar_title_placement,
label_origin, label_rect, legend_rect, point_on_rect_side, rect_axis_visibility_rect,
screen_offset_to_axis_title,
};
pub use nudge::{Element, NudgeReject, NudgeResult};
pub use rect::{ChartArea, DataArea, Rect, RectF};
#[derive(Debug, Clone, PartialEq)]
pub struct Margins {
pub top: f32,
pub bottom: f32,
pub left: f32,
pub right: f32,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Side {
Top,
Bottom,
Left,
Right,
}
#[derive(Debug, Clone, PartialEq)]
pub enum LayoutError {
OverflowHorizontal { required: f32, available: u32 },
OverflowVertical { required: f32, available: u32 },
EmptyDataArea,
TargetOutOfChartArea,
Infeasible,
}
pub(super) fn tick_contribution(axis: &AxisOptions) -> f32 {
axis.major_tick_length
}
pub(super) fn chart_title_contribution(title: &ChartTitleOptions) -> f32 {
title.top_margin
}
pub(super) fn colorbar_parts(cfg: &Config, side: &Side) -> Option<(f32, f32)> {
match cfg.colorbar.as_ref() {
Some(bar) if bar.visible && bar.side == *side => Some((
bar.axis.out_margin,
bar.gap_px + bar.thickness_px + tick_contribution(&bar.axis),
)),
_ => None,
}
}
pub(super) fn colorbar_contribution(cfg: &Config, side: &Side) -> f32 {
colorbar_parts(cfg, side).map_or(0.0, |(flex, fixed)| flex + fixed)
}
fn top_total(cfg: &Config) -> f32 {
chart_title_contribution(&cfg.chart_title)
+ cfg.top_x.out_margin
+ tick_contribution(&cfg.top_x)
+ colorbar_contribution(cfg, &Side::Top)
}
fn bottom_total(cfg: &Config) -> f32 {
cfg.bottom_x.out_margin
+ tick_contribution(&cfg.bottom_x)
+ colorbar_contribution(cfg, &Side::Bottom)
}
fn left_total(cfg: &Config) -> f32 {
cfg.left_y.out_margin + tick_contribution(&cfg.left_y) + colorbar_contribution(cfg, &Side::Left)
}
fn right_total(cfg: &Config) -> f32 {
cfg.right_y.out_margin
+ tick_contribution(&cfg.right_y)
+ colorbar_contribution(cfg, &Side::Right)
}
pub(super) fn axis_ref<'a>(cfg: &'a Config, side: &Side) -> &'a AxisOptions {
match side {
Side::Top => &cfg.top_x,
Side::Bottom => &cfg.bottom_x,
Side::Left => &cfg.left_y,
Side::Right => &cfg.right_y,
}
}
pub(super) fn axis_mut<'a>(cfg: &'a mut Config, side: &Side) -> &'a mut AxisOptions {
match side {
Side::Top => &mut cfg.top_x,
Side::Bottom => &mut cfg.bottom_x,
Side::Left => &mut cfg.left_y,
Side::Right => &mut cfg.right_y,
}
}
impl Config {
pub fn margins(&self) -> Margins {
Margins {
top: top_total(self),
bottom: bottom_total(self),
left: left_total(self),
right: right_total(self),
}
}
pub fn data_area(&self) -> Result<DataArea, LayoutError> {
let ca = &self.chart_area;
let m = self.margins();
let h_need = m.left + m.right;
if h_need > ca.width as f32 {
return Err(LayoutError::OverflowHorizontal {
required: h_need,
available: ca.width,
});
}
let v_need = m.top + m.bottom;
if v_need > ca.height as f32 {
return Err(LayoutError::OverflowVertical {
required: v_need,
available: ca.height,
});
}
let x = ca.x as f32 + m.left;
let y = ca.y as f32 + m.top;
let w = ca.width as f32 - h_need;
let h = ca.height as f32 - v_need;
if w <= 0.0 || h <= 0.0 {
return Err(LayoutError::EmptyDataArea);
}
Ok(DataArea(Rect {
x: x.floor() as u32,
y: y.floor() as u32,
width: w.floor() as u32,
height: h.floor() as u32,
}))
}
pub fn colorbar_band(&self, side: &Side) -> f32 {
colorbar_contribution(self, side)
}
pub fn validate(&self) -> Result<(), LayoutError> {
let _ = self.data_area()?;
for axis in [&self.top_x, &self.bottom_x, &self.left_y, &self.right_y] {
axis_dims_feasible(axis)?;
}
if let Some(bar) = self.colorbar.as_ref() {
axis_dims_feasible(&bar.axis)?;
if bar.thickness_px < 0.0 || bar.gap_px < 0.0 || bar.border_width < 0.0 {
return Err(LayoutError::Infeasible);
}
if !(bar.length_frac > 0.0 && bar.length_frac <= 1.0) {
return Err(LayoutError::Infeasible);
}
}
Ok(())
}
}
fn axis_dims_feasible(axis: &AxisOptions) -> Result<(), LayoutError> {
if axis.max <= axis.min {
return Err(LayoutError::Infeasible);
}
if axis.out_margin < 0.0
|| axis.major_tick_length < 0.0
|| axis.minor_tick_length < 0.0
|| axis.line_width < 0.0
{
return Err(LayoutError::Infeasible);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::{Side, colorbar_contribution};
use crate::config::Config;
use crate::default::{default_colorbar_options, default_config};
use crate::layout::LayoutError;
fn config_with_colorbar(side: Side) -> Config {
let mut cfg = default_config();
let mut bar = default_colorbar_options();
bar.side = side;
cfg.colorbar = Some(bar);
cfg
}
fn band(cfg: &Config) -> f32 {
let bar = cfg.colorbar.as_ref().expect("colourbar");
bar.gap_px + bar.thickness_px + bar.axis.out_margin + bar.axis.major_tick_length
}
#[test]
fn data_area_contained_in_chart_area() {
let cfg = default_config();
let da = cfg.data_area().unwrap();
let ca = &cfg.chart_area;
assert!(da.x >= ca.x);
assert!(da.y >= ca.y);
assert!(da.x + da.width <= ca.x + ca.width);
assert!(da.y + da.height <= ca.y + ca.height);
}
#[test]
fn margins_non_negative() {
let cfg = default_config();
let m = cfg.margins();
assert!(m.top >= 0.0);
assert!(m.bottom >= 0.0);
assert!(m.left >= 0.0);
assert!(m.right >= 0.0);
}
#[test]
fn the_colorbar_band_is_charged_to_its_own_side_only() {
for side in [Side::Top, Side::Bottom, Side::Left, Side::Right] {
let base = default_config();
let cfg = config_with_colorbar(side.clone());
let (before, after) = (base.margins(), cfg.margins());
let expected = band(&cfg);
let (grew, unchanged) = match side {
Side::Top => (
after.top - before.top,
[
after.bottom - before.bottom,
after.left - before.left,
after.right - before.right,
],
),
Side::Bottom => (
after.bottom - before.bottom,
[
after.top - before.top,
after.left - before.left,
after.right - before.right,
],
),
Side::Left => (
after.left - before.left,
[
after.top - before.top,
after.bottom - before.bottom,
after.right - before.right,
],
),
Side::Right => (
after.right - before.right,
[
after.top - before.top,
after.bottom - before.bottom,
after.left - before.left,
],
),
};
assert!(
(grew - expected).abs() < 1e-3,
"{side:?}: margin grew by {grew}, band is {expected}"
);
for delta in unchanged {
assert!(
delta.abs() < 1e-6,
"{side:?}: another side moved by {delta}"
);
}
let da = cfg.data_area().expect("data area");
let base_da = base.data_area().expect("data area");
match side {
Side::Left | Side::Right => {
assert!(da.width < base_da.width);
assert_eq!(da.height, base_da.height);
}
Side::Top | Side::Bottom => {
assert!(da.height < base_da.height);
assert_eq!(da.width, base_da.width);
}
}
}
}
#[test]
fn a_hidden_colorbar_reserves_nothing() {
let mut cfg = config_with_colorbar(Side::Right);
cfg.colorbar.as_mut().expect("colourbar").visible = false;
assert_eq!(cfg.margins(), default_config().margins());
assert_eq!(colorbar_contribution(&cfg, &Side::Right), 0.0);
}
#[test]
fn a_config_without_a_colorbar_is_unaffected() {
let cfg = default_config();
assert!(cfg.colorbar.is_none());
for side in [Side::Top, Side::Bottom, Side::Left, Side::Right] {
assert_eq!(colorbar_contribution(&cfg, &side), 0.0);
}
}
#[test]
fn validate_rejects_a_degenerate_colorbar() {
let good = config_with_colorbar(Side::Right);
assert!(good.validate().is_ok());
let mut reversed = good.clone();
reversed.colorbar.as_mut().unwrap().axis.max = reversed.colorbar.as_ref().unwrap().axis.min;
assert_eq!(reversed.validate(), Err(LayoutError::Infeasible));
let mut negative_margin = good.clone();
negative_margin.colorbar.as_mut().unwrap().axis.out_margin = -1.0;
assert_eq!(negative_margin.validate(), Err(LayoutError::Infeasible));
for frac in [0.0, -0.5, 1.5, f32::NAN] {
let mut bad = good.clone();
bad.colorbar.as_mut().unwrap().length_frac = frac;
assert_eq!(
bad.validate(),
Err(LayoutError::Infeasible),
"length_frac {frac} must be rejected"
);
}
}
}