1use crate::layout::kernel::{FeedbackRound, SpatialRounding};
4use crate::{Error, Result};
5
6use super::spacing::ExactLength;
7use super::{LocalMetric, RelativeSpacing, StrokeTheme};
8
9pub(crate) fn stroke_style(stroke: &StrokeTheme) -> Result<crate::render::backend::LineStyle> {
10 stroke.validate()?;
11 let width = crate::layout::kernel::LayoutScalar::from_css_pixels(
12 f64::from(stroke.get_width()),
13 SpatialRounding::NearestEven,
14 )?;
15 if width.quanta() == 0 {
16 return Err(Error::Render("axis stroke collapses in CSS quanta".into()));
17 }
18 Ok(stroke.backend_style())
19}
20
21#[derive(Clone, Debug, PartialEq, Eq)]
23pub struct FrameSides {
24 bits: u8,
25}
26
27macro_rules! side_field {
28 ($field:ident, $get:ident, $bit:literal) => {
29 #[doc = concat!("Inspect whether the physical `", stringify!($field), "` side is present.")]
30 pub fn $get(&self) -> bool {
31 self.bits & $bit != 0
32 }
33 #[doc = concat!("Replace only the `", stringify!($field), "` side; at least one side must remain.")]
34 pub fn $field(mut self, value: bool) -> Result<Self> {
35 self.bits = if value {
36 self.bits | $bit
37 } else {
38 self.bits & !$bit
39 };
40 self.validate()?;
41 Ok(self)
42 }
43 };
44}
45
46impl FrameSides {
47 #[expect(
48 clippy::fn_params_excessive_bools,
49 reason = "the complete constructor explicitly names the four independent frame sides"
50 )]
51 pub fn new(top: bool, right: bool, bottom: bool, left: bool) -> Result<Self> {
53 let value = Self {
54 bits: u8::from(top)
55 | (u8::from(right) << 1)
56 | (u8::from(bottom) << 2)
57 | (u8::from(left) << 3),
58 };
59 value.validate()?;
60 Ok(value)
61 }
62
63 fn validate(&self) -> Result<()> {
64 if self.bits != 0 && self.bits & !15 == 0 {
65 Ok(())
66 } else {
67 Err(Error::Data(
68 "a present frame requires at least one side".into(),
69 ))
70 }
71 }
72
73 side_field!(top, get_top, 1);
74 side_field!(right, get_right, 2);
75 side_field!(bottom, get_bottom, 4);
76 side_field!(left, get_left, 8);
77
78 pub(crate) fn drawing_order(&self) -> [bool; 4] {
80 [
81 self.get_bottom(),
82 self.get_right(),
83 self.get_top(),
84 self.get_left(),
85 ]
86 }
87}
88
89#[derive(Clone, Debug, PartialEq)]
91pub struct FrameTheme {
92 pub(crate) sides: FrameSides,
93 pub(crate) stroke: StrokeTheme,
94}
95
96impl FrameTheme {
97 pub fn new(sides: FrameSides, stroke: StrokeTheme) -> Result<Self> {
99 let value = Self { sides, stroke };
100 value.validate()?;
101 Ok(value)
102 }
103 fn validate(&self) -> Result<()> {
104 self.sides.validate()?;
105 self.stroke.validate()
106 }
107 pub fn get_sides(&self) -> &FrameSides {
109 &self.sides
110 }
111 pub fn get_stroke(&self) -> &StrokeTheme {
113 &self.stroke
114 }
115 pub fn sides(self, sides: FrameSides) -> Result<Self> {
117 Self::new(sides, self.stroke)
118 }
119 pub fn stroke(self, stroke: StrokeTheme) -> Result<Self> {
121 Self::new(self.sides, stroke)
122 }
123}
124
125#[derive(Clone, Debug, PartialEq)]
127pub struct TickTheme {
128 pub(crate) stroke: StrokeTheme,
129 pub(crate) length: u32,
130}
131
132impl TickTheme {
133 pub fn new(stroke: StrokeTheme, length: u32) -> Result<Self> {
135 let value = Self { stroke, length };
136 value.validate()?;
137 Ok(value)
138 }
139 fn validate(&self) -> Result<()> {
140 self.stroke.validate()?;
141 if self.length == 0 {
142 return Err(Error::Data(
143 "tick length must be positive CSS pixels".into(),
144 ));
145 }
146 Ok(())
147 }
148 pub fn get_stroke(&self) -> &StrokeTheme {
150 &self.stroke
151 }
152 pub fn get_length(&self) -> u32 {
154 self.length
155 }
156 pub fn stroke(self, stroke: StrokeTheme) -> Result<Self> {
158 Self::new(stroke, self.length)
159 }
160 pub fn length(self, length: u32) -> Result<Self> {
162 Self::new(self.stroke, length)
163 }
164}
165
166macro_rules! axis_spacing {
167 ($($field:ident => $get:ident : $metric:literal),+ $(,)?) => {
168 #[derive(Clone, Debug, PartialEq, Eq)]
169 pub struct AxisSpacing { $(pub(crate) $field: RelativeSpacing),+ }
171
172 impl AxisSpacing {
173 pub fn new($($field: RelativeSpacing),+) -> Result<Self> { Ok(Self { $($field),+ }) }
175 $(
176 #[doc = concat!("Borrow the exact `", stringify!($field), "` relationship.")]
177 pub fn $get(&self) -> &RelativeSpacing { &self.$field }
178 #[doc = concat!("Replace only the `", stringify!($field), "` relationship.")]
179 pub fn $field(mut self, value: RelativeSpacing) -> Result<Self> {
180 self.$field = value;
181 self.revalidated()
182 }
183 )+
184 fn revalidated(self) -> Result<Self> { Self::new($(self.$field),+) }
185 pub(crate) fn relationships(&self) -> [&RelativeSpacing; 6] { [$(&self.$field),+] }
186 pub(crate) fn lengths(&self, tick: f64, title: f64) -> Result<AxisLengths> {
187 let metrics = [LocalMetric::css_pixels(tick)?, LocalMetric::css_pixels(title)?];
188 Ok(AxisLengths { $($field: self.$get().product(metrics[$metric])),+ })
189 }
190 }
191
192 #[derive(Debug)]
194 pub(crate) struct AxisLengths { $(pub(crate) $field: ExactLength),+ }
195 };
196}
197
198axis_spacing! {
199 x_tick_label_gap => get_x_tick_label_gap: 0,
200 y_tick_label_gap => get_y_tick_label_gap: 0,
201 x_title_tick_label_gap => get_x_title_tick_label_gap: 1,
202 x_title_without_tick_labels_gap => get_x_title_without_tick_labels_gap: 1,
203 y_title_tick_label_gap => get_y_title_tick_label_gap: 1,
204 y_title_edge_padding => get_y_title_edge_padding: 1,
205}
206
207#[derive(Clone, Debug, PartialEq)]
209pub struct AxisTheme {
210 pub(crate) frame: Option<FrameTheme>,
211 pub(crate) cell_frame: Option<FrameTheme>,
212 pub(crate) ticks: Option<TickTheme>,
213 pub(crate) grid: Option<StrokeTheme>,
214 pub(crate) spacing: AxisSpacing,
215}
216
217pub(crate) struct AxisChrome<'a> {
219 pub(crate) frame: Option<&'a FrameTheme>,
220 pub(crate) grid: Option<&'a StrokeTheme>,
221 ticks: Option<&'a TickTheme>,
222}
223
224impl AxisChrome<'_> {
225 pub(crate) fn outward(
226 &self,
227 side: crate::layout::AxisSide,
228 present_ticks: bool,
229 ) -> Result<ExactLength> {
230 let frame = self.frame.filter(|frame| match side {
231 crate::layout::AxisSide::Bottom => frame.get_sides().get_bottom(),
232 crate::layout::AxisSide::Left => frame.get_sides().get_left(),
233 });
234 let mut length = ExactLength::pixels(
235 frame.map_or(0.0, |frame| f64::from(frame.get_stroke().get_width()) / 2.0),
236 )?;
237 if let Some(ticks) = self.ticks.filter(|_| present_ticks) {
238 length = length
239 .add_pixels(f64::from(ticks.get_length()))?
240 .add_pixels(f64::from(ticks.get_stroke().get_width()) / 2.0)?;
241 }
242 Ok(length)
243 }
244}
245
246impl AxisTheme {
247 pub fn new(
249 frame: Option<FrameTheme>,
250 cell_frame: Option<FrameTheme>,
251 ticks: Option<TickTheme>,
252 grid: Option<StrokeTheme>,
253 spacing: AxisSpacing,
254 ) -> Result<Self> {
255 let value = Self {
256 frame,
257 cell_frame,
258 ticks,
259 grid,
260 spacing,
261 };
262 value.validate()?;
263 Ok(value)
264 }
265 pub(crate) fn validate(&self) -> Result<()> {
266 if let Some(value) = self.get_frame() {
267 value.validate()?;
268 }
269 if let Some(value) = self.get_cell_frame() {
270 value.validate()?;
271 }
272 if let Some(value) = self.get_ticks() {
273 value.validate()?;
274 }
275 if let Some(value) = self.get_grid() {
276 value.validate()?;
277 }
278 Ok(())
279 }
280 pub fn get_frame(&self) -> Option<&FrameTheme> {
282 self.frame.as_ref()
283 }
284 pub fn get_cell_frame(&self) -> Option<&FrameTheme> {
286 self.cell_frame.as_ref()
287 }
288 pub fn get_ticks(&self) -> Option<&TickTheme> {
290 self.ticks.as_ref()
291 }
292 pub fn get_grid(&self) -> Option<&StrokeTheme> {
294 self.grid.as_ref()
295 }
296 pub fn get_spacing(&self) -> &AxisSpacing {
298 &self.spacing
299 }
300 pub(crate) fn coordinate_chrome(&self) -> AxisChrome<'_> {
301 AxisChrome {
302 frame: self.get_frame(),
303 grid: self.get_grid(),
304 ticks: self.get_ticks(),
305 }
306 }
307 pub(crate) fn cell_axis_chrome(&self) -> AxisChrome<'_> {
310 AxisChrome {
311 frame: None,
312 grid: None,
313 ticks: self.get_ticks(),
314 }
315 }
316 pub fn frame(self, frame: Option<FrameTheme>) -> Result<Self> {
318 Self::new(frame, self.cell_frame, self.ticks, self.grid, self.spacing)
319 }
320 pub fn cell_frame(self, cell_frame: Option<FrameTheme>) -> Result<Self> {
322 Self::new(self.frame, cell_frame, self.ticks, self.grid, self.spacing)
323 }
324 pub fn ticks(self, ticks: Option<TickTheme>) -> Result<Self> {
326 Self::new(self.frame, self.cell_frame, ticks, self.grid, self.spacing)
327 }
328 pub fn grid(self, grid: Option<StrokeTheme>) -> Result<Self> {
330 Self::new(self.frame, self.cell_frame, self.ticks, grid, self.spacing)
331 }
332 pub fn spacing(self, spacing: AxisSpacing) -> Result<Self> {
334 Self::new(self.frame, self.cell_frame, self.ticks, self.grid, spacing)
335 }
336
337 pub(crate) fn same_validated(&self, other: &Self) -> Result<bool> {
338 self.validate()?;
339 other.validate()?;
340
341 if self.frame != other.frame
342 || self.cell_frame != other.cell_frame
343 || self.ticks != other.ticks
344 || self.grid != other.grid
345 {
346 return Ok(false);
347 }
348 for (left, right) in self
349 .get_spacing()
350 .relationships()
351 .into_iter()
352 .zip(other.get_spacing().relationships())
353 {
354 if !left.same(right) {
355 return Ok(false);
356 }
357 }
358 Ok(true)
359 }
360
361 pub(crate) fn observe_feedback(&self, round: &mut FeedbackRound) -> Result<()> {
362 for stroke in [
363 self.get_frame().map(FrameTheme::get_stroke),
364 self.get_cell_frame().map(FrameTheme::get_stroke),
365 self.get_ticks().map(TickTheme::get_stroke),
366 self.get_grid(),
367 ] {
368 round.observe_count(usize::from(stroke.is_some()))?;
369 if let Some(stroke) = stroke {
370 round.observe_number(f64::from(stroke.get_width()))?;
371 round.observe_count(stroke.get_pattern() as usize)?;
372 for channel in [
373 stroke.get_color().red(),
374 stroke.get_color().green(),
375 stroke.get_color().blue(),
376 ] {
377 round.observe_count(usize::from(channel))?;
378 }
379 round.observe_number(stroke.get_color().get_alpha())?;
380 }
381 }
382 for frame in [self.get_frame(), self.get_cell_frame()]
383 .into_iter()
384 .flatten()
385 {
386 for side in frame.get_sides().drawing_order() {
387 round.observe_count(usize::from(side))?;
388 }
389 }
390 if let Some(ticks) = self.get_ticks() {
391 round.observe_count(ticks.get_length() as usize)?;
392 }
393 for spacing in self.get_spacing().relationships() {
394 spacing.observe_feedback(round)?;
395 }
396 Ok(())
397 }
398}
399
400impl AxisLengths {
401 pub(crate) fn before(value: &ExactLength, anchor: f64) -> Result<f64> {
402 Self::pixels(value.before(anchor)?)
403 }
404 pub(crate) fn hard(value: &ExactLength) -> Result<f64> {
405 Self::pixels(value.round(SpatialRounding::Ceil)?)
406 }
407
408 #[expect(
409 clippy::cast_precision_loss,
410 reason = "the checked quanta bound guarantees exact integer conversion"
411 )]
412 fn pixels(value: crate::layout::kernel::LayoutScalar) -> Result<f64> {
413 if value.quanta().unsigned_abs() > (1_u64 << 53) {
414 return Err(Error::Render(
415 "axis length cannot retain exact CSS quanta".into(),
416 ));
417 }
418 Ok(value.quanta() as f64 / 1024.0)
419 }
420}