Skip to main content

lichtung_plot/theme/
typography.rs

1//! Complete immutable typography construction and semantic role ownership.
2
3use crate::Error;
4
5use super::TextTheme;
6
7// Each input is retained once. Constructors and consuming edits validate the
8// entire group, including nested siblings, without copying font payloads.
9macro_rules! text_group {
10    (
11        $(#[$attribute:meta])* $name:ident {
12            $($field:ident: $type:ty => $get:ident),+ $(,)?
13        }
14        $(validate |$value:ident| $validation:block)?
15    ) => {
16        $(#[$attribute])*
17        #[derive(Clone, Debug, PartialEq)]
18        pub struct $name {
19            $(pub(crate) $field: $type),+
20        }
21
22        impl $name {
23            /// Construct all complete text roles and validate their invariants.
24            pub fn new($($field: $type),+) -> Result<Self, Error> {
25                let value = Self { $($field),+ };
26                value.validate()?;
27                Ok(value)
28            }
29
30            pub(crate) fn validate(&self) -> Result<(), Error> {
31                $(self.$field.validate()?;)+
32                $(let $value = self; $validation)?
33                Ok(())
34            }
35
36            $(
37                #[doc = concat!("Borrow the independent `", stringify!($field), "` text role.")]
38                pub fn $get(&self) -> &$type {
39                    &self.$field
40                }
41
42                #[doc = concat!("Replace only `", stringify!($field), "`, validating the complete role group.")]
43                pub fn $field(mut self, value: $type) -> Result<Self, Error> {
44                    self.$field = value;
45                    self.validate()?;
46                    Ok(self)
47                }
48            )+
49        }
50    };
51}
52
53text_group! {
54    /// Independent root and embedded Plot headings.
55    RootTextRoles {
56        title: TextTheme => get_title,
57        subtitle: TextTheme => get_subtitle,
58        caption: TextTheme => get_caption,
59    }
60}
61
62text_group! {
63    /// Complete coordinate-axis typography.
64    AxisTextRoles {
65        axis_title: TextTheme => get_axis_title,
66        tick_label: TextTheme => get_tick_label,
67    }
68}
69
70text_group! {
71    /// Complete discrete and continuous guide typography.
72    GuideTextRoles {
73        legend_title: TextTheme => get_legend_title,
74        legend_label: TextTheme => get_legend_label,
75    }
76}
77
78text_group! {
79    /// Independent facet, composition, and embedded-prose text roles.
80    CompositionTextRoles {
81        facet_header: TextTheme => get_facet_header,
82        composition_header: TextTheme => get_composition_header,
83        text_panel_title: TextTheme => get_text_panel_title,
84        body: TextTheme => get_body,
85    }
86}
87
88text_group! {
89    /// Complete opaque candidates selected before font planning.
90    ContrastTextRoles {
91        light: TextTheme => get_light,
92        dark: TextTheme => get_dark,
93    }
94    validate |value| {
95        if value.light.get_color().get_alpha() != 255 || value.dark.get_color().get_alpha() != 255 {
96            return Err(Error::Data("contrast text candidates must be opaque".into()));
97        }
98    }
99}
100
101text_group! {
102    /// Complete annotation text, independent of mark and axis chrome.
103    AnnotationTextRoles {
104        significance_label: TextTheme => get_significance_label,
105        estimate_detail: TextTheme => get_estimate_detail,
106        waterfall_total: TextTheme => get_waterfall_total,
107        point_layer_label: TextTheme => get_point_layer_label,
108        panel_text: TextTheme => get_panel_text,
109        contrast: ContrastTextRoles => get_contrast,
110    }
111}
112
113text_group! {
114    /// One complete retained typography value with all eighteen semantic roles.
115    TypographyTheme {
116        root: RootTextRoles => get_root,
117        axes: AxisTextRoles => get_axes,
118        guides: GuideTextRoles => get_guides,
119        composition: CompositionTextRoles => get_composition,
120        annotations: AnnotationTextRoles => get_annotations,
121    }
122}
123
124impl TypographyTheme {
125    pub(crate) fn with_font_stack(
126        mut self,
127        stack: crate::font::canonical::FontStack,
128    ) -> Result<Self, Error> {
129        let roles = [
130            &mut self.root.title,
131            &mut self.root.subtitle,
132            &mut self.root.caption,
133            &mut self.axes.axis_title,
134            &mut self.axes.tick_label,
135            &mut self.guides.legend_title,
136            &mut self.guides.legend_label,
137            &mut self.composition.facet_header,
138            &mut self.composition.composition_header,
139            &mut self.composition.text_panel_title,
140            &mut self.composition.body,
141            &mut self.annotations.significance_label,
142            &mut self.annotations.estimate_detail,
143            &mut self.annotations.waterfall_total,
144            &mut self.annotations.point_layer_label,
145            &mut self.annotations.panel_text,
146            &mut self.annotations.contrast.light,
147            &mut self.annotations.contrast.dark,
148        ];
149        let stacks =
150            std::iter::repeat_n(stack.clone(), roles.len() - 1).chain(std::iter::once(stack));
151        for (role, stack) in roles.into_iter().zip(stacks) {
152            *role = role.clone().stack(stack)?;
153        }
154        self.validate()?;
155        Ok(self)
156    }
157
158    /// Stable traversal of each retained font handle, exactly once.
159    pub(crate) fn roles(&self) -> [&TextTheme; 18] {
160        [
161            self.get_root().get_title(),
162            self.get_root().get_subtitle(),
163            self.get_root().get_caption(),
164            self.get_axes().get_axis_title(),
165            self.get_axes().get_tick_label(),
166            self.get_annotations().get_significance_label(),
167            self.get_annotations().get_estimate_detail(),
168            self.get_annotations().get_waterfall_total(),
169            self.get_annotations().get_point_layer_label(),
170            self.get_annotations().get_panel_text(),
171            self.get_annotations().get_contrast().get_light(),
172            self.get_annotations().get_contrast().get_dark(),
173            self.get_guides().get_legend_title(),
174            self.get_guides().get_legend_label(),
175            self.get_composition().get_facet_header(),
176            self.get_composition().get_composition_header(),
177            self.get_composition().get_text_panel_title(),
178            self.get_composition().get_body(),
179        ]
180    }
181}