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, Insets, Length, ResolvedInsets};
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::elements::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().into()))?,
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_padding: Insets => get_value_padding,
124    borrow null_row_padding: Insets => get_null_row_padding,
125} validate |value| {
126    value.get_highlight_outline().validate()?;
127    value.get_value_padding().validate_em_px()?;
128    value.get_null_row_padding().validate_em_px()?;
129});
130
131impl CellAnnotationGeometry {
132    pub(crate) fn value_lengths(&self, em: f64) -> Result<ResolvedInsets> {
133        self.get_value_padding()
134            .resolve(LocalMetric::css_pixels(em)?, None)
135    }
136    pub(crate) fn null_row_lengths(&self, em: f64) -> Result<ResolvedInsets> {
137        self.get_null_row_padding()
138            .resolve(LocalMetric::css_pixels(em)?, None)
139    }
140}
141
142validated_component!(EstimateAnnotationGeometry {
143    borrow panel_inset: Insets => get_panel_inset,
144    borrow guide_clearance: RelativeSpacing => get_guide_clearance,
145    borrow numeric_label_gap: RelativeSpacing => get_numeric_label_gap,
146} validate |value| {
147    let inset = value.get_panel_inset();
148    inset.validate_em_px()?;
149    if !inset.get_top().value().is_zero() || !inset.get_bottom().value().is_zero() {
150        return Err(crate::Error::Data("estimate panel inset accepts only left and right lengths".into()));
151    }
152});
153
154pub(crate) struct EstimateAnnotationLengths {
155    pub(crate) panel_inset: ResolvedInsets,
156    pub(crate) guide_clearance: ExactLength,
157}
158
159impl EstimateAnnotationGeometry {
160    pub(crate) fn lengths(&self, em: f64) -> Result<EstimateAnnotationLengths> {
161        let em = LocalMetric::css_pixels(em)?;
162        Ok(EstimateAnnotationLengths {
163            panel_inset: self.get_panel_inset().resolve(em, None)?,
164            guide_clearance: self.get_guide_clearance().product(em),
165        })
166    }
167}
168
169validated_component!(ValueShelfSpacing {
170    borrow padding: Insets => get_padding,
171    borrow gap: RelativeSpacing => get_gap,
172    borrow row_gap: RelativeSpacing => get_row_gap,
173} validate |value| { value.get_padding().validate_em_px()?; });
174
175pub(crate) struct ValueShelfLengths {
176    pub(crate) padding: ResolvedInsets,
177    pub(crate) gap: ExactLength,
178    pub(crate) row_gap: ExactLength,
179}
180
181impl ValueShelfSpacing {
182    pub(crate) fn lengths(&self, em: f64) -> Result<ValueShelfLengths> {
183        let em = LocalMetric::css_pixels(em)?;
184        Ok(ValueShelfLengths {
185            padding: self.get_padding().resolve(em, None)?,
186            gap: self.get_gap().product(em),
187            row_gap: self.get_row_gap().product(em),
188        })
189    }
190}
191
192validated_component!(AnnotationTheme {
193    borrow significance: SignificanceGeometry => get_significance,
194    borrow cells: CellAnnotationGeometry => get_cells,
195    borrow estimates: EstimateAnnotationGeometry => get_estimates,
196    borrow point_label_gap: RelativeSpacing => get_point_label_gap,
197    borrow panel_text_padding: Insets => get_panel_text_padding,
198    borrow waterfall_shelf: ValueShelfSpacing => get_waterfall_shelf,
199    copy planning_backdrop: OpaqueColor => get_planning_backdrop,
200} validate |value| {
201    value.get_significance().validate()?;
202    value.get_cells().validate()?;
203    value.get_estimates().validate()?;
204    value.get_waterfall_shelf().validate()?;
205    value.get_panel_text_padding().validate_em_px()?;
206});
207
208impl AnnotationTheme {
209    pub(crate) fn builtin(foreground: super::Color, bracket_width: f32) -> Result<Self> {
210        let ratio = RelativeSpacing::ratio;
211        Self::new(
212            SignificanceGeometry::new(
213                SolidStrokeTheme::new(foreground, bracket_width)?,
214                ratio(8, 16)?,
215                ratio(7, 16)?,
216                ratio(6, 16)?,
217                ratio(26, 16)?,
218                ratio(2, 16)?,
219                ratio(2, 16)?,
220            )?,
221            CellAnnotationGeometry::new(
222                OutlineTheme::new(foreground, 2.5)?,
223                Insets::symmetric(Length::Em(ratio(3, 11)?), Length::Em(ratio(5, 22)?)),
224                Insets::symmetric(Length::Em(ratio(6, 11)?), Length::Em(ratio(2, 11)?)),
225            )?,
226            EstimateAnnotationGeometry::new(
227                Insets::zero()
228                    .left(Length::Em(ratio(6, 14)?))
229                    .right(Length::Em(ratio(6, 14)?)),
230                ratio(6, 14)?,
231                ratio(7, 14)?,
232            )?,
233            ratio(2, 10)?,
234            Insets::all(Length::Em(ratio(8, 14)?)),
235            ValueShelfSpacing::new(
236                Insets::all(Length::Em(ratio(4, 14)?)),
237                ratio(4, 14)?,
238                ratio(4, 14)?,
239            )?,
240            OpaqueColor::rgb(255, 255, 255),
241        )
242    }
243
244    pub(crate) fn point_label_gap_length(&self, em: f64) -> Result<ExactLength> {
245        Ok(self
246            .get_point_label_gap()
247            .product(LocalMetric::css_pixels(em)?))
248    }
249}