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