lichtung-plot 0.2.0

Strongly typed scientific plotting with deterministic SVG and PNG output
Documentation
//! One complete embedded Plot title/subtitle/caption block in its Canvas cell.

use crate::decorations::title_block::plan::{
    RootAlignments, RootTitleBlockIntrinsics, RootTitleBlockPlan,
};
use crate::layout::fit::{self, FitOutcome, FitScope};
use crate::layout::kernel::FitKind;
use crate::layout::{AddressRole, PixelRect};
use crate::{text::TextAlignment, Error};

pub(super) struct PlotLocalTextPlan {
    pub(super) text: RootTitleBlockPlan,
    pub(super) body: PixelRect,
}

impl PlotLocalTextPlan {
    #[expect(
        clippy::too_many_arguments,
        reason = "local prose retains its own title intrinsics while reading the same occurrence's body intrinsic slot"
    )]
    pub(super) fn measure(
        registered: &crate::orchestrator::registration::PlotRegistration<'_>,
        intrinsic: &crate::orchestrator::root::PlotIntrinsics,
        operation: &crate::font::canonical::OperationContext,
        cell: PixelRect,
        theme: &crate::text::TextRequestContext<'_, '_>,
        panel_alignments: [Option<TextAlignment>; 2],
        intrinsics: RootTitleBlockIntrinsics,
        scope: &FitScope,
        ranges: [(f64, f64); 2],
    ) -> Result<FitOutcome<Self>, Error> {
        let sources = registered.source.prose();
        let present = sources.iter().any(Option::is_some);

        let padding = if present {
            theme.root_padding_lengths()?
        } else {
            crate::layout::ResolvedInsets::zero()?
        };
        let span = padding.inset(cell)?;
        let maximum_width = if present {
            theme.root_inner_width(cell.width())?
        } else {
            cell.width()
        };
        if span.width() <= 0.0 || span.height() <= 0.0 {
            return Ok(FitOutcome::Miss(
                scope.child(AddressRole::RootBody, 0)?.failure(
                    FitKind::TrackOrResidual,
                    "Canvas Plot local text leaves no content span",
                ),
            ));
        }
        if let FitOutcome::Miss(reason) =
            fit::outcome(intrinsics.check_width(maximum_width.min(span.width()), scope))?
        {
            return Ok(FitOutcome::Miss(reason));
        }
        let minimum = crate::layout::interior::intrinsic_body_minimum_for_ranges(
            registered,
            intrinsic,
            operation,
            theme,
            Some(ranges),
        )?;
        let alignments = RootAlignments::resolve(
            theme,
            registered.source.prose_alignments(),
            panel_alignments,
        );
        let text = match fit::outcome(RootTitleBlockPlan::measure(
            theme,
            sources,
            cell,
            &padding,
            maximum_width,
            alignments,
            minimum.1,
            scope,
        ))? {
            FitOutcome::Measured(text) => text,
            FitOutcome::Miss(reason) => return Ok(FitOutcome::Miss(reason)),
        };
        let reserved = text.reserves.body(cell, &padding)?;
        let body = PixelRect::new(cell.x0, cell.x1, reserved.y0, reserved.y1);
        if body.width() < minimum.0 || body.height() < minimum.1 {
            return Ok(FitOutcome::Miss(
                scope.child(AddressRole::RootBody, 0)?.failure(
                    FitKind::TrackOrResidual,
                    "Canvas Plot local title block leaves insufficient body space",
                ),
            ));
        }
        Ok(FitOutcome::Measured(Self { text, body }))
    }
}