Skip to main content

lichtung_plot/theme/
annotation.rs

1//! Complete annotation geometry, separate from text, content and placement policy.
2
3use super::component::validated_component;
4use super::spacing::ExactLength;
5use super::{
6    LocalMetric, OpaqueColor, OutlineTheme, RelativeSpacing, ResolvedTheme, SolidStrokeTheme,
7    TextTheme,
8};
9use crate::layout::kernel::SpatialRounding;
10use crate::Result;
11
12/// One selected significance appearance, including exact-override provenance.
13pub(crate) struct EffectiveSignificance {
14    pub(crate) label: TextTheme,
15    pub(crate) exact_override: bool,
16    pub(crate) gap: ExactLength,
17    geometry: SignificanceGeometry,
18}
19
20impl ResolvedTheme {
21    pub(crate) fn effective_significance(
22        &self,
23        exact: Option<crate::SignificanceMarkStyle>,
24    ) -> Result<EffectiveSignificance> {
25        let mut label = self
26            .typography
27            .get_annotations()
28            .get_significance_label()
29            .try_copy()?;
30        if let Some(exact) = exact {
31            label = label
32                .font_size(exact.get_font_size())?
33                .weight(exact.get_weight())?;
34        }
35        let geometry = self.annotations.get_significance();
36        let em = LocalMetric::css_pixels(label.get_font_size())?;
37        Ok(EffectiveSignificance {
38            gap: exact.map_or_else(
39                || Ok(geometry.get_label_gap().product(em)),
40                |exact| ExactLength::pixels(exact.get_gap()),
41            )?,
42            geometry: geometry.clone(),
43            label,
44            exact_override: exact.is_some(),
45        })
46    }
47}
48
49impl EffectiveSignificance {
50    fn length(&self, spacing: &RelativeSpacing) -> Result<ExactLength> {
51        Ok(spacing.product(LocalMetric::css_pixels(self.label.get_font_size())?))
52    }
53
54    pub(crate) fn tick_height(&self) -> Result<ExactLength> {
55        self.length(self.geometry.get_tick_height())
56    }
57
58    pub(crate) fn data_gap(&self) -> Result<ExactLength> {
59        self.length(self.geometry.get_data_gap())
60    }
61
62    pub(crate) fn level_step(&self) -> Result<ExactLength> {
63        self.length(self.geometry.get_level_step())
64    }
65
66    pub(crate) fn panel_padding(&self) -> Result<ExactLength> {
67        let selected = self.length(self.geometry.get_panel_padding())?;
68        let ink =
69            ExactLength::pixels(f64::from(self.geometry.get_bracket_stroke().get_width()) / 2.0)?
70                .add_pixels(1.0 / 1024.0)?;
71        Ok(if selected.compare(&ink).is_lt() {
72            ink
73        } else {
74            selected
75        })
76    }
77
78    pub(crate) fn gap_pixels(&self) -> Result<f64> {
79        self.gap.pixels_rounded(SpatialRounding::NearestEven)
80    }
81
82    pub(crate) fn attached_reserve(&self, extent: f64) -> Result<f64> {
83        self.gap
84            .add_length(&self.length(self.geometry.get_attached_extra_clearance())?)
85            .add_pixels(extent)?
86            .pixels_rounded(SpatialRounding::Ceil)
87    }
88
89    pub(crate) fn bracket_style(
90        &self,
91        label: &crate::layout::ResolvedTextPlan,
92    ) -> Result<crate::render::backend::BracketStyle> {
93        Ok(crate::render::backend::BracketStyle {
94            color: self.geometry.get_bracket_stroke().get_color(),
95            width: self.geometry.get_bracket_stroke().get_width(),
96            label_color: label.style.paint,
97            font_family: label
98                .copy_stack()
99                .map_err(|error| crate::Error::Render(error.to_string()))?,
100            font_size: label.style.font_size,
101            line_height: label.line_height(),
102            weight: label.style.weight,
103            label_gap: self.gap_pixels()?,
104            tick_height: self
105                .tick_height()?
106                .pixels_rounded(SpatialRounding::NearestEven)?,
107        })
108    }
109}
110
111validated_component!(SignificanceGeometry {
112    borrow bracket_stroke: SolidStrokeTheme => get_bracket_stroke,
113    borrow label_gap: RelativeSpacing => get_label_gap,
114    borrow tick_height: RelativeSpacing => get_tick_height,
115    borrow data_gap: RelativeSpacing => get_data_gap,
116    borrow level_step: RelativeSpacing => get_level_step,
117    borrow panel_padding: RelativeSpacing => get_panel_padding,
118    borrow attached_extra_clearance: RelativeSpacing => get_attached_extra_clearance,
119} validate |value| { value.get_bracket_stroke().validate()?; });
120
121validated_component!(CellAnnotationGeometry {
122    borrow highlight_outline: OutlineTheme => get_highlight_outline,
123    borrow value_horizontal_padding: RelativeSpacing => get_value_horizontal_padding,
124    borrow value_vertical_padding: RelativeSpacing => get_value_vertical_padding,
125    borrow null_row_horizontal_padding: RelativeSpacing => get_null_row_horizontal_padding,
126    borrow null_row_vertical_padding: RelativeSpacing => get_null_row_vertical_padding,
127} validate |value| { value.get_highlight_outline().validate()?; });
128
129pub(crate) struct CellTextPadding {
130    horizontal: ExactLength,
131    vertical: ExactLength,
132}
133
134impl CellAnnotationGeometry {
135    pub(crate) fn value_padding(&self, em: f64) -> Result<CellTextPadding> {
136        let em = LocalMetric::css_pixels(em)?;
137        Ok(CellTextPadding {
138            horizontal: self.get_value_horizontal_padding().product(em),
139            vertical: self.get_value_vertical_padding().product(em),
140        })
141    }
142
143    pub(crate) fn null_row_padding(&self, em: f64) -> Result<CellTextPadding> {
144        let em = LocalMetric::css_pixels(em)?;
145        Ok(CellTextPadding {
146            horizontal: self.get_null_row_horizontal_padding().product(em),
147            vertical: self.get_null_row_vertical_padding().product(em),
148        })
149    }
150}
151
152impl CellTextPadding {
153    pub(crate) fn width(&self, text_width: f64) -> Result<f64> {
154        self.horizontal
155            .add_pixels(text_width)?
156            .pixels_rounded(SpatialRounding::Ceil)
157    }
158
159    pub(crate) fn height(&self, text_height: f64) -> Result<f64> {
160        self.vertical
161            .add_pixels(text_height)?
162            .pixels_rounded(SpatialRounding::Ceil)
163    }
164
165    pub(crate) fn available_width(&self, assigned: f64) -> Result<f64> {
166        ExactLength::pixels(assigned)?
167            .subtract(&self.horizontal)
168            .map_or(Ok(0.0), |remaining| {
169                remaining.pixels_rounded(SpatialRounding::Floor)
170            })
171    }
172}
173
174validated_component!(EstimateAnnotationGeometry {
175    borrow panel_inset: RelativeSpacing => get_panel_inset,
176    borrow guide_clearance: RelativeSpacing => get_guide_clearance,
177    borrow numeric_label_gap: RelativeSpacing => get_numeric_label_gap,
178} validate |_value| {});
179
180pub(crate) struct EstimateAnnotationLengths {
181    pub(crate) panel_inset: ExactLength,
182    pub(crate) guide_clearance: ExactLength,
183}
184
185impl EstimateAnnotationGeometry {
186    pub(crate) fn lengths(&self, em: f64) -> Result<EstimateAnnotationLengths> {
187        let em = LocalMetric::css_pixels(em)?;
188        Ok(EstimateAnnotationLengths {
189            panel_inset: self.get_panel_inset().product(em),
190            guide_clearance: self.get_guide_clearance().product(em),
191        })
192    }
193}
194
195validated_component!(ValueShelfSpacing {
196    borrow padding: RelativeSpacing => get_padding,
197    borrow gap: RelativeSpacing => get_gap,
198    borrow row_gap: RelativeSpacing => get_row_gap,
199} validate |_value| {});
200
201pub(crate) struct ValueShelfLengths {
202    pub(crate) padding: ExactLength,
203    pub(crate) gap: ExactLength,
204    pub(crate) row_gap: ExactLength,
205}
206
207impl ValueShelfSpacing {
208    pub(crate) fn lengths(&self, em: f64) -> Result<ValueShelfLengths> {
209        let em = LocalMetric::css_pixels(em)?;
210        Ok(ValueShelfLengths {
211            padding: self.get_padding().product(em),
212            gap: self.get_gap().product(em),
213            row_gap: self.get_row_gap().product(em),
214        })
215    }
216}
217
218validated_component!(AnnotationTheme {
219    borrow significance: SignificanceGeometry => get_significance,
220    borrow cells: CellAnnotationGeometry => get_cells,
221    borrow estimates: EstimateAnnotationGeometry => get_estimates,
222    borrow point_label_gap: RelativeSpacing => get_point_label_gap,
223    borrow panel_text_padding: RelativeSpacing => get_panel_text_padding,
224    borrow waterfall_shelf: ValueShelfSpacing => get_waterfall_shelf,
225    copy planning_backdrop: OpaqueColor => get_planning_backdrop,
226} validate |value| {
227    value.get_significance().validate()?;
228    value.get_cells().validate()?;
229    value.get_estimates().validate()?;
230    value.get_waterfall_shelf().validate()?;
231});
232
233impl AnnotationTheme {
234    pub(crate) fn builtin(foreground: super::Color, bracket_width: f32) -> Result<Self> {
235        let ratio = RelativeSpacing::ratio;
236        Self::new(
237            SignificanceGeometry::new(
238                SolidStrokeTheme::new(foreground, bracket_width)?,
239                ratio(8, 16)?,
240                ratio(7, 16)?,
241                ratio(6, 16)?,
242                ratio(26, 16)?,
243                ratio(2, 16)?,
244                ratio(2, 16)?,
245            )?,
246            CellAnnotationGeometry::new(
247                OutlineTheme::new(foreground, 2.5)?,
248                ratio(6, 11)?,
249                ratio(5, 11)?,
250                ratio(12, 11)?,
251                ratio(4, 11)?,
252            )?,
253            EstimateAnnotationGeometry::new(ratio(6, 14)?, ratio(6, 14)?, ratio(7, 14)?)?,
254            ratio(2, 10)?,
255            ratio(8, 14)?,
256            ValueShelfSpacing::new(ratio(4, 14)?, ratio(4, 14)?, ratio(4, 14)?)?,
257            OpaqueColor::rgb(255, 255, 255),
258        )
259    }
260
261    pub(crate) fn point_label_gap_length(&self, em: f64) -> Result<ExactLength> {
262        Ok(self
263            .get_point_label_gap()
264            .product(LocalMetric::css_pixels(em)?))
265    }
266}