use crate::layout::AxisSystemPlan;
#[cfg(test)]
use crate::layout::PhysicalTextAlignment;
#[cfg(test)]
use crate::theme::TextTheme;
#[cfg(test)]
use crate::Error;
pub(crate) const MINIMUM_PANEL_SPAN: f64 = 1.0 / 1024.0;
#[derive(Clone, Copy, PartialEq)]
pub struct PixelRect {
pub x0: f64,
pub x1: f64,
pub y0: f64,
pub y1: f64,
}
impl std::fmt::Debug for PixelRect {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("PanelBox")
.field("x0", &self.x0)
.field("x1", &self.x1)
.field("y0", &self.y0)
.field("y1", &self.y1)
.finish()
}
}
impl PixelRect {
#[must_use]
pub fn new(x0: f64, x1: f64, y0: f64, y1: f64) -> Self {
Self { x0, x1, y0, y1 }
}
#[must_use]
pub fn as_tuple(self) -> (f64, f64, f64, f64) {
(self.x0, self.x1, self.y0, self.y1)
}
#[must_use]
pub fn width(self) -> f64 {
self.x1 - self.x0
}
#[must_use]
pub fn height(self) -> f64 {
self.y1 - self.y0
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub(crate) struct EdgeSpaceRequirements {
pub(crate) left: f64,
pub(crate) right: f64,
pub(crate) bottom: f64,
pub(crate) top: f64,
}
impl EdgeSpaceRequirements {
pub(crate) fn measure_header(
axes: &AxisSystemPlan,
theme: &crate::text::TextRequestContext<'_, '_>,
) -> Result<Self, crate::layout::fit::PlanError> {
let required = axes.requirements();
let clearance = theme.panel_edge_length()?;
let edge = |value| crate::theme::composition::hard(&clearance.add_pixels(value)?);
Ok(Self {
left: edge(required.left)?,
right: edge(required.right)?,
bottom: edge(required.bottom)?,
top: edge(required.top)?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CanvasInsets {
pub left: f64,
pub right: f64,
pub bottom: f64,
pub top: f64,
}
#[derive(Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub(crate) struct RootFramePlan {
pub(crate) root_text: crate::decorations::title_block::plan::RootTitleBlockPlan,
pub panel_box: PixelRect,
pub insets: CanvasInsets,
pub(crate) native_preference:
Option<crate::layout::interior::preference::CoordinateSizePreference>,
}
impl RootFramePlan {
pub(crate) fn height_for_body(
&self,
body: f64,
theme: &crate::text::TextRequestContext<'_, '_>,
) -> Result<f64, crate::Error> {
let padding = theme.root_padding_lengths()?;
let bottom = self
.root_text
.reserves
.bottom
.add_length(&padding.bottom)
.after_pixels(0.0)?;
let top = self
.root_text
.reserves
.top
.add_length(&padding.top)
.pixels_rounded(crate::layout::kernel::SpatialRounding::NearestEven)?;
let additional = (self.insets.bottom - bottom) + (self.insets.top - top);
crate::theme::composition::hard(
&self
.root_text
.reserves
.height_with_body(body + additional)?
.add_length(&padding.vertical()),
)
}
pub(crate) fn into_shared(self) -> std::sync::Arc<Self> {
std::sync::Arc::new(self)
}
pub(crate) fn measure(
canvas_size: (u32, u32),
theme: &crate::text::TextRequestContext<'_, '_>,
text: [Option<&crate::Text>; 3],
alignments: crate::decorations::title_block::plan::RootAlignments,
minimum_body: (f64, f64),
) -> Result<Self, crate::layout::fit::PlanError> {
let (canvas_width, canvas_height) = canvas_size;
let outer = PixelRect::new(0.0, f64::from(canvas_width), 0.0, f64::from(canvas_height));
let padding = theme.root_padding_lengths()?;
let plan = crate::decorations::title_block::plan::RootTitleBlockPlan::measure(
theme,
text,
outer,
&padding,
theme.root_inner_width(f64::from(canvas_width))?,
alignments,
minimum_body.1,
&crate::layout::fit::FitScope::root(),
)?;
if theme.root_outer_width(minimum_body.0)? > f64::from(canvas_width) {
return Err(crate::layout::fit::FitScope::root()
.failure(
crate::layout::kernel::FitKind::TrackOrResidual,
"root padding and complete content exceed the assigned root",
)
.into());
}
let panel_box = plan.reserves.body(outer, &padding)?;
let insets = CanvasInsets {
left: panel_box.x0,
right: outer.x1 - panel_box.x1,
bottom: panel_box.y0,
top: outer.y1 - panel_box.y1,
};
crate::layout::fit::measured(
crate::layout::kernel::FrameRequirements {
minimum_body,
natural_insets: [0.0; 4],
}
.assign_fit(panel_box, [0.0; 4], &crate::layout::fit::FitScope::root()),
)?;
Ok(Self {
panel_box,
insets,
root_text: plan,
native_preference: None,
})
}
}
#[cfg(test)]
fn try_wrap_text(
theme: &crate::text::TextRequestContext<'_, '_>,
text: &str,
appearance: &TextTheme,
maximum_width: f64,
alignment: PhysicalTextAlignment,
role: &str,
) -> Result<Vec<String>, Error> {
theme
.plan_text(text, appearance, maximum_width, alignment, role, false)
.and_then(|plan| {
plan.into_line_texts()
.map_err(|error| Error::Render(error.to_string()))
})
}
#[cfg(test)]
fn wrap_text(
theme: &crate::text::TextRequestContext<'_, '_>,
text: &str,
appearance: &TextTheme,
maximum_width: f64,
) -> Vec<String> {
try_wrap_text(
theme,
text,
appearance,
maximum_width,
PhysicalTextAlignment::Start,
"test text",
)
.unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::theme::Theme;
#[test]
fn measured_caption_wraps_without_shortening_and_rejects_impossible_fit() {
let theme = Theme::publication().resolve().unwrap();
let text = crate::text::TextService::default();
let theme = crate::text::TextRequestContext::new(&theme, &text);
let caption =
"diagnostic provenance with a deliberately long model and adjustment description";
let alignments = crate::decorations::title_block::plan::RootAlignments::resolve(
&theme, [None; 3], [None; 2],
);
let layout = RootFramePlan::measure(
(360, 240),
&theme,
[None, None, Some(&crate::Text::new(caption))],
alignments,
(MINIMUM_PANEL_SPAN, MINIMUM_PANEL_SPAN),
)
.unwrap();
let lines = wrap_text(&theme, caption, &theme.typography.root.caption, 328.0);
assert!(lines.len() > 1);
assert_eq!(lines.concat(), caption);
let plan = &layout.root_text;
assert!(
crate::theme::composition::hard(&plan.reserves.bottom).unwrap()
> theme.typography.root.caption.line_pitch_pixels()
);
assert!(RootFramePlan::measure(
(160, 120),
&theme,
[None, None, Some(&crate::Text::new(caption.repeat(8)))],
alignments,
(MINIMUM_PANEL_SPAN, MINIMUM_PANEL_SPAN),
)
.is_err());
}
}