1use super::{component::validated_component, spacing::ExactLength};
4use super::{LocalMetric, RelativeSpacing, ResolvedTheme};
5use crate::layout::{
6 kernel::SpatialRounding, GridGap, GridGapPixels, Insets, Length, ResolvedInsets,
7};
8use crate::{text::TextAlignment, Result};
9
10macro_rules! spacing_group {
11 ($name:ident, $lengths:ident; $($field:ident => $get:ident: $metric:literal),+ $(,)?) => {
12 #[derive(Clone, Debug, PartialEq, Eq)]
13 #[allow(clippy::struct_field_names, reason = "field names follow the authoritative complete composition schema")]
14 #[doc = concat!("Complete nonnegative owner-local `", stringify!($name), "` relationships.")]
15 pub struct $name { $($field: RelativeSpacing),+ }
16 impl $name {
17 #[allow(clippy::unnecessary_wraps, reason = "all supplied immutable relationships already validate nonnegative spacing")]
18 pub fn new($($field: RelativeSpacing),+) -> Result<Self> { Ok(Self { $($field),+ }) }
20 $(
21 #[doc = concat!("Borrow the exact `", stringify!($field), "` relationship.")]
22 pub fn $get(&self) -> &RelativeSpacing { &self.$field }
23 #[allow(clippy::unnecessary_wraps, reason = "immutable nonnegative relationships have no additional sibling constraint")]
24 #[doc = concat!("Replace only the exact `", stringify!($field), "` relationship.")]
25 pub fn $field(mut self, value: RelativeSpacing) -> Result<Self> {
26 self.$field = value; Ok(self)
27 }
28 )+
29 fn lengths(&self, metrics: &[LocalMetric]) -> Result<$lengths> {
30 Ok($lengths { $($field: self.$get().product(metrics[$metric])),+ })
31 }
32 }
33 #[derive(Debug)]
34 #[allow(clippy::struct_field_names, reason = "measured products retain their authoritative source field names")]
35 pub(crate) struct $lengths { $(pub(crate) $field: ExactLength),+ }
36 };
37}
38
39spacing_group!(RootTitleSpacing, RootTitleLengths;
40 title_subtitle_gap => get_title_subtitle_gap: 0,
41 title_block_to_content_clearance => get_title_block_to_content_clearance: 0,
42 caption_to_content_clearance => get_caption_to_content_clearance: 1);
43validated_component!(TextPanelSpacing {
44 borrow padding: Insets => get_padding,
45 borrow gap: RelativeSpacing => get_gap,
46} validate |value| { value.get_padding().validate_em_px()?; });
47impl Eq for TextPanelSpacing {}
48
49validated_component!(#[expect(clippy::struct_field_names, reason = "authored header padding fields retain their public role names")]
50HeaderSpacing {
51 borrow column_header_padding: Insets => get_column_header_padding,
52 borrow row_header_padding: Insets => get_row_header_padding,
53 borrow row_title_padding: Insets => get_row_title_padding,
54} validate |value| {
55 value.get_column_header_padding().validate_em_px()?;
56 value.get_row_header_padding().validate_em_px()?;
57 value.get_row_title_padding().validate_em_px()?;
58});
59impl Eq for HeaderSpacing {}
60
61validated_component!(FacetStripSpacing {
62 borrow padding: Insets => get_padding,
63 borrow content_clearance: RelativeSpacing => get_content_clearance,
64} validate |value| { value.get_padding().validate_em_px()?; });
65impl Eq for FacetStripSpacing {}
66
67pub(crate) struct TextPanelLengths {
68 pub(crate) padding: ResolvedInsets,
69 pub(crate) gap: ExactLength,
70}
71pub(crate) struct HeaderLengths {
72 pub(crate) column_header: ResolvedInsets,
73 pub(crate) row_header: ResolvedInsets,
74 pub(crate) row_title: ResolvedInsets,
75}
76pub(crate) struct FacetStripLengths {
77 pub(crate) padding: ResolvedInsets,
78 pub(crate) content_clearance: ExactLength,
79}
80
81#[derive(Clone, Debug, PartialEq, Eq)]
83pub struct CompositionTheme {
84 root_title_alignment: TextAlignment,
85 root_subtitle_alignment: Option<TextAlignment>,
86 root_caption_alignment: TextAlignment,
87 root_title_block: RootTitleSpacing,
88 root_padding: Insets,
89 panel_edge_clearance: Insets,
90 text_panel: TextPanelSpacing,
91 facet_gap: GridGap,
92 canvas_grid_gap: GridGap,
93 matrix_gap: GridGap,
94 marginal_gap: GridGap,
95 headers: HeaderSpacing,
96 facet_strip: FacetStripSpacing,
97}
98
99macro_rules! owned_fields {
100 ($($field:ident => $get:ident: $ty:ty),+ $(,)?) => { $(
101 #[doc = concat!("Borrow the complete `", stringify!($field), "` relationship value.")]
102 pub fn $get(&self) -> &$ty { &self.$field }
103 #[allow(clippy::unnecessary_wraps, reason = "complete validated inputs have no additional cross-field constraint")]
104 #[doc = concat!("Replace only `", stringify!($field), "`.")]
105 pub fn $field(mut self, value: $ty) -> Result<Self> { self.$field = value; Ok(self) }
106 )+ };
107}
108macro_rules! directional_fields {
109 ($($field:ident => $get:ident: $ty:ty),+ $(,)?) => { $(
110 #[doc = concat!("Borrow the original ", stringify!($field), ".")]
111 pub fn $get(&self) -> &$ty { &self.$field }
112 #[doc = concat!("Replace ", stringify!($field), " with owner-local em or CSS pixel lengths.")]
113 pub fn $field(mut self,value:$ty) -> Result<Self> {
114 value.validate_em_px()?;
115 self.$field=value;
116 Ok(self)
117 }
118 )+ };
119}
120macro_rules! copy_fields {
121 ($($field:ident => $get:ident: $ty:ty),+ $(,)?) => { $(
122 #[doc = concat!("Inspect `", stringify!($field), "`.")]
123 pub fn $get(&self) -> $ty { self.$field }
124 #[allow(clippy::unnecessary_wraps, reason = "closed alignment inputs have no invalid states")]
125 #[doc = concat!("Replace only `", stringify!($field), "`.")]
126 pub fn $field(mut self, value: $ty) -> Result<Self> { self.$field = value; Ok(self) }
127 )+ };
128}
129
130impl CompositionTheme {
131 #[expect(
132 clippy::too_many_arguments,
133 reason = "the authoritative constructor accepts complete validated role groups and closed alignment values"
134 )]
135 pub fn new(
137 root_title_alignment: TextAlignment,
138 root_subtitle_alignment: Option<TextAlignment>,
139 root_caption_alignment: TextAlignment,
140 root_title_block: RootTitleSpacing,
141 root_padding: Insets,
142 panel_edge_clearance: Insets,
143 text_panel: TextPanelSpacing,
144 facet_gap: GridGap,
145 canvas_grid_gap: GridGap,
146 matrix_gap: GridGap,
147 marginal_gap: GridGap,
148 headers: HeaderSpacing,
149 facet_strip: FacetStripSpacing,
150 ) -> Result<Self> {
151 root_padding.validate_em_px()?;
152 panel_edge_clearance.validate_em_px()?;
153 facet_gap.validate_em_px()?;
154 canvas_grid_gap.validate_em_px()?;
155 matrix_gap.validate_em_px()?;
156 marginal_gap.validate_em_px()?;
157 Ok(Self {
158 root_title_alignment,
159 root_subtitle_alignment,
160 root_caption_alignment,
161 root_title_block,
162 root_padding,
163 panel_edge_clearance,
164 text_panel,
165 facet_gap,
166 canvas_grid_gap,
167 matrix_gap,
168 marginal_gap,
169 headers,
170 facet_strip,
171 })
172 }
173 copy_fields!(root_title_alignment => get_root_title_alignment: TextAlignment,
174 root_subtitle_alignment => get_root_subtitle_alignment: Option<TextAlignment>,
175 root_caption_alignment => get_root_caption_alignment: TextAlignment);
176 owned_fields!(root_title_block => get_root_title_block: RootTitleSpacing,
177 text_panel => get_text_panel: TextPanelSpacing, headers => get_headers: HeaderSpacing,
178 facet_strip => get_facet_strip: FacetStripSpacing);
179
180 directional_fields!(root_padding => get_root_padding: Insets,
181 panel_edge_clearance => get_panel_edge_clearance: Insets,
182 facet_gap => get_facet_gap: GridGap,
183 canvas_grid_gap => get_canvas_grid_gap: GridGap,
184 matrix_gap => get_matrix_gap: GridGap,
185 marginal_gap => get_marginal_gap: GridGap);
186
187 pub(crate) fn builtin(t: u32, s: u32, c: u32, a: u32) -> Result<Self> {
188 let ratio = |n, d| RelativeSpacing::ratio(n, d);
189 Self::new(
190 TextAlignment::Center,
191 None,
192 TextAlignment::Start,
193 RootTitleSpacing::new(ratio(0, 1)?, ratio(14, t.max(s))?, ratio(12, c)?)?,
194 Insets::zero()
195 .left(Length::Em(ratio(16, a)?))
196 .right(Length::Em(ratio(16, a)?))
197 .bottom(Length::Em(ratio(8, a)?))
198 .top(Length::Em(ratio(12, a)?)),
199 Insets::all(Length::Em(ratio(4, 16)?)),
200 TextPanelSpacing::new(Insets::all(Length::Em(ratio(16, a)?)), ratio(12, t.max(a))?)?,
201 GridGap::all(Length::Em(ratio(12, s)?)),
202 GridGap::all(Length::Em(ratio(12, a)?)),
203 GridGap::all(Length::Em(ratio(12, a)?)),
204 GridGap::all(Length::Em(ratio(12, a)?)),
205 HeaderSpacing::new(
206 Insets::zero()
207 .top(Length::Em(ratio(8, t)?))
208 .bottom(Length::Em(ratio(8, t)?)),
209 Insets::zero()
210 .left(Length::Em(ratio(10, t)?))
211 .right(Length::Em(ratio(10, t)?)),
212 Insets::symmetric(Length::Em(ratio(8, t)?), Length::Em(ratio(4, t)?)),
213 )?,
214 FacetStripSpacing::new(
215 Insets::zero()
216 .left(Length::Em(ratio(8, s)?))
217 .right(Length::Em(ratio(8, s)?)),
218 ratio(8, s)?,
219 )?,
220 )
221 }
222 pub(crate) fn same(&self, other: &Self) -> bool {
223 self == other
224 }
225}
226
227pub(crate) fn hard(value: &ExactLength) -> Result<f64> {
228 value.pixels_rounded(SpatialRounding::Ceil)
229}
230pub(crate) fn capacity(width: f64, reserve: &ExactLength) -> Result<f64> {
231 Ok(ExactLength::pixels(width.max(0.0))?
232 .subtract(reserve)
233 .map(|value| value.pixels_rounded(SpatialRounding::Floor))
234 .transpose()?
235 .unwrap_or(-1.0))
236}
237
238impl ResolvedTheme {
239 pub(crate) fn panel_edge_lengths(&self) -> Result<ResolvedInsets> {
240 self.composition.get_panel_edge_clearance().resolve(
241 LocalMetric::css_pixels(self.typography.axes.tick_label.get_font_size())?,
242 None,
243 )
244 }
245 pub(crate) fn root_inner_width(&self, outer: f64) -> Result<f64> {
246 capacity(outer, &self.root_padding_lengths()?.horizontal())
247 }
248 pub(crate) fn root_outer_width(&self, inner: f64) -> Result<f64> {
249 hard(
250 &self
251 .root_padding_lengths()?
252 .horizontal()
253 .add_pixels(inner)?,
254 )
255 }
256 pub(crate) fn root_outer_height(&self, inner: &ExactLength) -> Result<f64> {
257 hard(&self.root_padding_lengths()?.vertical().add_length(inner))
258 }
259 pub(crate) fn root_padding_lengths(&self) -> Result<ResolvedInsets> {
260 self.composition.get_root_padding().resolve(
261 LocalMetric::css_pixels(self.typography.composition.body.get_font_size())?,
262 None,
263 )
264 }
265 pub(crate) fn root_title_lengths(&self, sizes: [f64; 3]) -> Result<RootTitleLengths> {
266 self.composition.get_root_title_block().lengths(&[
267 LocalMetric::css_pixels(sizes[0].max(sizes[1]))?,
268 LocalMetric::css_pixels(sizes[2])?,
269 ])
270 }
271 pub(crate) fn text_panel_lengths(&self, title: f64, body: f64) -> Result<TextPanelLengths> {
272 let source = self.composition.get_text_panel();
273 Ok(TextPanelLengths {
274 padding: source
275 .get_padding()
276 .resolve(LocalMetric::css_pixels(body)?, None)?,
277 gap: source
278 .get_gap()
279 .product(LocalMetric::css_pixels(body.max(title))?),
280 })
281 }
282 pub(crate) fn header_lengths(&self, selected_size: f64) -> Result<HeaderLengths> {
283 let source = self.composition.get_headers();
284 let em = LocalMetric::css_pixels(selected_size)?;
285 Ok(HeaderLengths {
286 column_header: source.get_column_header_padding().resolve(em, None)?,
287 row_header: source.get_row_header_padding().resolve(em, None)?,
288 row_title: source.get_row_title_padding().resolve(em, None)?,
289 })
290 }
291 pub(crate) fn facet_strip_lengths(&self) -> Result<FacetStripLengths> {
292 let source = self.composition.get_facet_strip();
293 let em = LocalMetric::css_pixels(self.typography.composition.facet_header.get_font_size())?;
294 Ok(FacetStripLengths {
295 padding: source.get_padding().resolve(em, None)?,
296 content_clearance: source.get_content_clearance().product(em),
297 })
298 }
299 pub(crate) fn canvas_gaps(&self) -> Result<GridGapPixels> {
300 self.composition
301 .get_canvas_grid_gap()
302 .pixels(LocalMetric::css_pixels(
303 self.typography.composition.body.get_font_size(),
304 )?)
305 }
306 pub(crate) fn matrix_gaps(&self) -> Result<GridGapPixels> {
307 self.composition
308 .get_matrix_gap()
309 .pixels(LocalMetric::css_pixels(
310 self.typography.composition.body.get_font_size(),
311 )?)
312 }
313 pub(crate) fn marginal_gaps(&self) -> Result<GridGapPixels> {
314 self.composition
315 .get_marginal_gap()
316 .pixels(LocalMetric::css_pixels(
317 self.typography.composition.body.get_font_size(),
318 )?)
319 }
320 pub(crate) fn facet_gaps(&self) -> Result<GridGapPixels> {
321 self.composition
322 .get_facet_gap()
323 .pixels(LocalMetric::css_pixels(
324 self.typography.composition.facet_header.get_font_size(),
325 )?)
326 }
327}
328
329impl super::TextTheme {
330 pub(crate) fn line_pitch_exact(&self) -> Result<ExactLength> {
331 Ok(RelativeSpacing::new(self.get_line_height())?
332 .product(LocalMetric::css_pixels(self.get_font_size())?))
333 }
334}
335
336#[cfg(test)]
337mod tests;