use super::{component::validated_component, spacing::ExactLength};
use super::{LocalMetric, RelativeSpacing, ResolvedTheme};
use crate::layout::{
kernel::SpatialRounding, GridGap, GridGapPixels, Insets, Length, ResolvedInsets,
};
use crate::{text::TextAlignment, Result};
macro_rules! spacing_group {
($name:ident, $lengths:ident; $($field:ident => $get:ident: $metric:literal),+ $(,)?) => {
#[derive(Clone, Debug, PartialEq, Eq)]
#[allow(clippy::struct_field_names, reason = "field names follow the authoritative complete composition schema")]
#[doc = concat!("Complete nonnegative owner-local `", stringify!($name), "` relationships.")]
pub struct $name { $($field: RelativeSpacing),+ }
impl $name {
#[allow(clippy::unnecessary_wraps, reason = "all supplied immutable relationships already validate nonnegative spacing")]
pub fn new($($field: RelativeSpacing),+) -> Result<Self> { Ok(Self { $($field),+ }) }
$(
#[doc = concat!("Borrow the exact `", stringify!($field), "` relationship.")]
pub fn $get(&self) -> &RelativeSpacing { &self.$field }
#[allow(clippy::unnecessary_wraps, reason = "immutable nonnegative relationships have no additional sibling constraint")]
#[doc = concat!("Replace only the exact `", stringify!($field), "` relationship.")]
pub fn $field(mut self, value: RelativeSpacing) -> Result<Self> {
self.$field = value; Ok(self)
}
)+
fn lengths(&self, metrics: &[LocalMetric]) -> Result<$lengths> {
Ok($lengths { $($field: self.$get().product(metrics[$metric])),+ })
}
}
#[derive(Debug)]
#[allow(clippy::struct_field_names, reason = "measured products retain their authoritative source field names")]
pub(crate) struct $lengths { $(pub(crate) $field: ExactLength),+ }
};
}
spacing_group!(RootTitleSpacing, RootTitleLengths;
title_subtitle_gap => get_title_subtitle_gap: 0,
title_block_to_content_clearance => get_title_block_to_content_clearance: 0,
caption_to_content_clearance => get_caption_to_content_clearance: 1);
validated_component!(TextPanelSpacing {
borrow padding: Insets => get_padding,
borrow gap: RelativeSpacing => get_gap,
} validate |value| { value.get_padding().validate_em_px()?; });
impl Eq for TextPanelSpacing {}
validated_component!(#[expect(clippy::struct_field_names, reason = "authored header padding fields retain their public role names")]
HeaderSpacing {
borrow column_header_padding: Insets => get_column_header_padding,
borrow row_header_padding: Insets => get_row_header_padding,
borrow row_title_padding: Insets => get_row_title_padding,
} validate |value| {
value.get_column_header_padding().validate_em_px()?;
value.get_row_header_padding().validate_em_px()?;
value.get_row_title_padding().validate_em_px()?;
});
impl Eq for HeaderSpacing {}
validated_component!(FacetStripSpacing {
borrow padding: Insets => get_padding,
borrow content_clearance: RelativeSpacing => get_content_clearance,
} validate |value| { value.get_padding().validate_em_px()?; });
impl Eq for FacetStripSpacing {}
pub(crate) struct TextPanelLengths {
pub(crate) padding: ResolvedInsets,
pub(crate) gap: ExactLength,
}
pub(crate) struct HeaderLengths {
pub(crate) column_header: ResolvedInsets,
pub(crate) row_header: ResolvedInsets,
pub(crate) row_title: ResolvedInsets,
}
pub(crate) struct FacetStripLengths {
pub(crate) padding: ResolvedInsets,
pub(crate) content_clearance: ExactLength,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CompositionTheme {
root_title_alignment: TextAlignment,
root_subtitle_alignment: Option<TextAlignment>,
root_caption_alignment: TextAlignment,
root_title_block: RootTitleSpacing,
root_padding: Insets,
panel_edge_clearance: Insets,
text_panel: TextPanelSpacing,
facet_gap: GridGap,
canvas_grid_gap: GridGap,
matrix_gap: GridGap,
marginal_gap: GridGap,
headers: HeaderSpacing,
facet_strip: FacetStripSpacing,
}
macro_rules! owned_fields {
($($field:ident => $get:ident: $ty:ty),+ $(,)?) => { $(
#[doc = concat!("Borrow the complete `", stringify!($field), "` relationship value.")]
pub fn $get(&self) -> &$ty { &self.$field }
#[allow(clippy::unnecessary_wraps, reason = "complete validated inputs have no additional cross-field constraint")]
#[doc = concat!("Replace only `", stringify!($field), "`.")]
pub fn $field(mut self, value: $ty) -> Result<Self> { self.$field = value; Ok(self) }
)+ };
}
macro_rules! directional_fields {
($($field:ident => $get:ident: $ty:ty),+ $(,)?) => { $(
#[doc = concat!("Borrow the original ", stringify!($field), ".")]
pub fn $get(&self) -> &$ty { &self.$field }
#[doc = concat!("Replace ", stringify!($field), " with owner-local em or CSS pixel lengths.")]
pub fn $field(mut self,value:$ty) -> Result<Self> {
value.validate_em_px()?;
self.$field=value;
Ok(self)
}
)+ };
}
macro_rules! copy_fields {
($($field:ident => $get:ident: $ty:ty),+ $(,)?) => { $(
#[doc = concat!("Inspect `", stringify!($field), "`.")]
pub fn $get(&self) -> $ty { self.$field }
#[allow(clippy::unnecessary_wraps, reason = "closed alignment inputs have no invalid states")]
#[doc = concat!("Replace only `", stringify!($field), "`.")]
pub fn $field(mut self, value: $ty) -> Result<Self> { self.$field = value; Ok(self) }
)+ };
}
impl CompositionTheme {
#[expect(
clippy::too_many_arguments,
reason = "the authoritative constructor accepts complete validated role groups and closed alignment values"
)]
pub fn new(
root_title_alignment: TextAlignment,
root_subtitle_alignment: Option<TextAlignment>,
root_caption_alignment: TextAlignment,
root_title_block: RootTitleSpacing,
root_padding: Insets,
panel_edge_clearance: Insets,
text_panel: TextPanelSpacing,
facet_gap: GridGap,
canvas_grid_gap: GridGap,
matrix_gap: GridGap,
marginal_gap: GridGap,
headers: HeaderSpacing,
facet_strip: FacetStripSpacing,
) -> Result<Self> {
root_padding.validate_em_px()?;
panel_edge_clearance.validate_em_px()?;
facet_gap.validate_em_px()?;
canvas_grid_gap.validate_em_px()?;
matrix_gap.validate_em_px()?;
marginal_gap.validate_em_px()?;
Ok(Self {
root_title_alignment,
root_subtitle_alignment,
root_caption_alignment,
root_title_block,
root_padding,
panel_edge_clearance,
text_panel,
facet_gap,
canvas_grid_gap,
matrix_gap,
marginal_gap,
headers,
facet_strip,
})
}
copy_fields!(root_title_alignment => get_root_title_alignment: TextAlignment,
root_subtitle_alignment => get_root_subtitle_alignment: Option<TextAlignment>,
root_caption_alignment => get_root_caption_alignment: TextAlignment);
owned_fields!(root_title_block => get_root_title_block: RootTitleSpacing,
text_panel => get_text_panel: TextPanelSpacing, headers => get_headers: HeaderSpacing,
facet_strip => get_facet_strip: FacetStripSpacing);
directional_fields!(root_padding => get_root_padding: Insets,
panel_edge_clearance => get_panel_edge_clearance: Insets,
facet_gap => get_facet_gap: GridGap,
canvas_grid_gap => get_canvas_grid_gap: GridGap,
matrix_gap => get_matrix_gap: GridGap,
marginal_gap => get_marginal_gap: GridGap);
pub(crate) fn builtin(t: u32, s: u32, c: u32, a: u32) -> Result<Self> {
let ratio = |n, d| RelativeSpacing::ratio(n, d);
Self::new(
TextAlignment::Center,
None,
TextAlignment::Start,
RootTitleSpacing::new(ratio(0, 1)?, ratio(14, t.max(s))?, ratio(12, c)?)?,
Insets::zero()
.left(Length::Em(ratio(16, a)?))
.right(Length::Em(ratio(16, a)?))
.bottom(Length::Em(ratio(8, a)?))
.top(Length::Em(ratio(12, a)?)),
Insets::all(Length::Em(ratio(4, 16)?)),
TextPanelSpacing::new(Insets::all(Length::Em(ratio(16, a)?)), ratio(12, t.max(a))?)?,
GridGap::all(Length::Em(ratio(12, s)?)),
GridGap::all(Length::Em(ratio(12, a)?)),
GridGap::all(Length::Em(ratio(12, a)?)),
GridGap::all(Length::Em(ratio(12, a)?)),
HeaderSpacing::new(
Insets::zero()
.top(Length::Em(ratio(8, t)?))
.bottom(Length::Em(ratio(8, t)?)),
Insets::zero()
.left(Length::Em(ratio(10, t)?))
.right(Length::Em(ratio(10, t)?)),
Insets::symmetric(Length::Em(ratio(8, t)?), Length::Em(ratio(4, t)?)),
)?,
FacetStripSpacing::new(
Insets::zero()
.left(Length::Em(ratio(8, s)?))
.right(Length::Em(ratio(8, s)?)),
ratio(8, s)?,
)?,
)
}
pub(crate) fn same(&self, other: &Self) -> bool {
self == other
}
}
pub(crate) fn hard(value: &ExactLength) -> Result<f64> {
value.pixels_rounded(SpatialRounding::Ceil)
}
pub(crate) fn capacity(width: f64, reserve: &ExactLength) -> Result<f64> {
Ok(ExactLength::pixels(width.max(0.0))?
.subtract(reserve)
.map(|value| value.pixels_rounded(SpatialRounding::Floor))
.transpose()?
.unwrap_or(-1.0))
}
impl ResolvedTheme {
pub(crate) fn panel_edge_lengths(&self) -> Result<ResolvedInsets> {
self.composition.get_panel_edge_clearance().resolve(
LocalMetric::css_pixels(self.typography.axes.tick_label.get_font_size())?,
None,
)
}
pub(crate) fn root_inner_width(&self, outer: f64) -> Result<f64> {
capacity(outer, &self.root_padding_lengths()?.horizontal())
}
pub(crate) fn root_outer_width(&self, inner: f64) -> Result<f64> {
hard(
&self
.root_padding_lengths()?
.horizontal()
.add_pixels(inner)?,
)
}
pub(crate) fn root_outer_height(&self, inner: &ExactLength) -> Result<f64> {
hard(&self.root_padding_lengths()?.vertical().add_length(inner))
}
pub(crate) fn root_padding_lengths(&self) -> Result<ResolvedInsets> {
self.composition.get_root_padding().resolve(
LocalMetric::css_pixels(self.typography.composition.body.get_font_size())?,
None,
)
}
pub(crate) fn root_title_lengths(&self, sizes: [f64; 3]) -> Result<RootTitleLengths> {
self.composition.get_root_title_block().lengths(&[
LocalMetric::css_pixels(sizes[0].max(sizes[1]))?,
LocalMetric::css_pixels(sizes[2])?,
])
}
pub(crate) fn text_panel_lengths(&self, title: f64, body: f64) -> Result<TextPanelLengths> {
let source = self.composition.get_text_panel();
Ok(TextPanelLengths {
padding: source
.get_padding()
.resolve(LocalMetric::css_pixels(body)?, None)?,
gap: source
.get_gap()
.product(LocalMetric::css_pixels(body.max(title))?),
})
}
pub(crate) fn header_lengths(&self, selected_size: f64) -> Result<HeaderLengths> {
let source = self.composition.get_headers();
let em = LocalMetric::css_pixels(selected_size)?;
Ok(HeaderLengths {
column_header: source.get_column_header_padding().resolve(em, None)?,
row_header: source.get_row_header_padding().resolve(em, None)?,
row_title: source.get_row_title_padding().resolve(em, None)?,
})
}
pub(crate) fn facet_strip_lengths(&self) -> Result<FacetStripLengths> {
let source = self.composition.get_facet_strip();
let em = LocalMetric::css_pixels(self.typography.composition.facet_header.get_font_size())?;
Ok(FacetStripLengths {
padding: source.get_padding().resolve(em, None)?,
content_clearance: source.get_content_clearance().product(em),
})
}
pub(crate) fn canvas_gaps(&self) -> Result<GridGapPixels> {
self.composition
.get_canvas_grid_gap()
.pixels(LocalMetric::css_pixels(
self.typography.composition.body.get_font_size(),
)?)
}
pub(crate) fn matrix_gaps(&self) -> Result<GridGapPixels> {
self.composition
.get_matrix_gap()
.pixels(LocalMetric::css_pixels(
self.typography.composition.body.get_font_size(),
)?)
}
pub(crate) fn marginal_gaps(&self) -> Result<GridGapPixels> {
self.composition
.get_marginal_gap()
.pixels(LocalMetric::css_pixels(
self.typography.composition.body.get_font_size(),
)?)
}
pub(crate) fn facet_gaps(&self) -> Result<GridGapPixels> {
self.composition
.get_facet_gap()
.pixels(LocalMetric::css_pixels(
self.typography.composition.facet_header.get_font_size(),
)?)
}
}
impl super::TextTheme {
pub(crate) fn line_pitch_exact(&self) -> Result<ExactLength> {
Ok(RelativeSpacing::new(self.get_line_height())?
.product(LocalMetric::css_pixels(self.get_font_size())?))
}
}
#[cfg(test)]
mod tests;