use crate::orchestrator::registration::AxisIntentRef;
use crate::spec::{AxisScale, PlotKind};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) enum RangeProvenance {
#[default]
Automatic,
Configured,
Shared,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RangePolicy {
ContinuousMargin,
AnchoredZero,
HistogramEdges,
QqFinite,
PointIntervalFinite,
MarginalSupport,
CategorySlots,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct AxisSemanticInputs {
pub(crate) text_layout: Option<crate::text::TextLayout>,
pub(crate) provenance: RangeProvenance,
pub(crate) scale: AxisScale,
pub(crate) policy: RangePolicy,
pub(crate) generation: crate::coordinates::TickGeneration,
pub(crate) category_labels: crate::coordinates::CategoryLabelSelection,
pub(crate) format: Option<std::sync::Arc<crate::numeric::format::NumericFormat>>,
}
impl AxisSemanticInputs {
pub(crate) fn new(
intent: AxisIntentRef<'_>,
provenance: RangeProvenance,
scale: AxisScale,
policy: RangePolicy,
) -> Self {
let (generation, category_labels, format) = match intent {
AxisIntentRef::Authored(spec) => (
spec.generation.clone(),
spec.category_labels.clone(),
if spec.exact_ticks.is_some() || policy == RangePolicy::CategorySlots {
None
} else {
spec.format.clone()
},
),
AxisIntentRef::MatrixVariable | AxisIntentRef::NativeAmplitude => (
crate::coordinates::TickGeneration::automatic(),
crate::coordinates::CategoryLabelSelection::all(),
None,
),
};
Self {
text_layout: None,
provenance,
scale,
policy,
generation,
category_labels,
format,
}
}
pub(crate) fn observe_feedback(
&self,
round: &mut crate::layout::kernel::FeedbackRound,
) -> Result<(), crate::Error> {
round.observe_count(self.provenance as usize)?;
round.observe_count(self.scale as usize)?;
round.observe_count(self.policy as usize)?;
round.observe_count(usize::from(self.format.is_some()))?;
if let Some(format) = &self.format {
format.observe_feedback(round)?;
}
self.category_labels.observe(round)?;
round.observe_text_layout(self.text_layout.as_ref())?;
self.generation.observe_feedback(round)
}
}
pub(crate) fn range_policies(kind: PlotKind) -> [RangePolicy; 2] {
match kind {
PlotKind::Bar | PlotKind::StackedBar | PlotKind::Density => {
[RangePolicy::ContinuousMargin, RangePolicy::AnchoredZero]
}
PlotKind::HorizontalBar | PlotKind::StackedHorizontalBar => {
[RangePolicy::AnchoredZero, RangePolicy::CategorySlots]
}
PlotKind::Histogram => [RangePolicy::HistogramEdges, RangePolicy::AnchoredZero],
PlotKind::Qq => [RangePolicy::QqFinite; 2],
PlotKind::PointInterval => [RangePolicy::PointIntervalFinite; 2],
PlotKind::Marginal => [RangePolicy::MarginalSupport; 2],
_ => [RangePolicy::ContinuousMargin; 2],
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct CoordinateLayoutInputs {
tick_layouts: [Option<crate::text::TextLayout>; 2],
pub(crate) axes: crate::theme::AxisTheme,
pub(crate) tick_label: crate::theme::TextTheme,
pub(crate) axis_title: crate::theme::TextTheme,
preferred_aspect: [u8; 2],
}
impl CoordinateLayoutInputs {
pub(crate) fn new(theme: &crate::theme::ResolvedTheme) -> Result<Self, crate::Error> {
let retain = |source: &crate::theme::TextTheme| -> Result<_, crate::Error> {
let stack = source
.stack
.try_copy()
.map_err(|error| crate::Error::Render(error.to_string().into()))?;
crate::theme::TextTheme::new(
stack,
source.get_color(),
source.get_font_size(),
source.get_weight(),
source.get_line_height(),
)
};
Ok(Self {
axes: theme.axes.clone(),
tick_layouts: theme.text_layouts.ticks.clone(),
tick_label: retain(&theme.typography.axes.tick_label)?,
axis_title: retain(&theme.typography.axes.axis_title)?,
preferred_aspect: [4, 3],
})
}
pub(crate) fn matches(
&self,
theme: &crate::theme::ResolvedTheme,
) -> Result<bool, crate::Error> {
Ok(self.axes.same_validated(&theme.axes)?
&& self.tick_layouts == theme.text_layouts.ticks
&& self.tick_label.same(&theme.typography.axes.tick_label)
&& self.axis_title.same(&theme.typography.axes.axis_title))
}
pub(crate) fn observe_feedback(
&self,
round: &mut crate::layout::kernel::FeedbackRound,
) -> Result<(), crate::Error> {
self.axes.observe_feedback(round)?;
for layout in &self.tick_layouts {
round.observe_text_layout(layout.as_ref())?;
}
for text in [&self.tick_label, &self.axis_title] {
round.observe_font_stack(&text.stack)?;
round.observe_number(text.font_size)?;
round.observe_count(text.weight as usize)?;
round.observe_number(text.line_height_multiplier)?;
for channel in [text.paint.red(), text.paint.green(), text.paint.blue()] {
round.observe_count(usize::from(channel))?;
}
round.observe_number(text.paint.get_alpha())?;
}
for value in self.preferred_aspect {
round.observe_count(usize::from(value))?;
}
Ok(())
}
}