sealed-surface 0.4.0

Non-extractable data surface: tables, timeseries, scatter, treemap as glyphs, never DOM text
Documentation
//! Optional look: type scale, chart style, composed appearance.
//!
//! [`Appearance::default`] uses compact 5×7 glyphs (same metrics as 0.2.0).
//! [`TypeScale::large`] is 2×; [`TypeScale::xlarge`] is 3× — opt-in only.

use crate::theme::{Rgb, Theme};

/// Glyph size and cell metrics.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum TypeScale {
    /// 5×7, row 18. Default (same metrics as 0.2.0).
    #[default]
    Compact,
    /// 5×7 at 2× (10×14). Opt-in; hard to read for long tables.
    Large,
    /// 5×7 at 3× (15×21).
    XLarge,
}

impl TypeScale {
    /// Compact 5×7 (default).
    #[must_use]
    pub const fn compact() -> Self {
        Self::Compact
    }

    /// 2× scale.
    #[must_use]
    pub const fn large() -> Self {
        Self::Large
    }

    /// 3× scale.
    #[must_use]
    pub const fn xlarge() -> Self {
        Self::XLarge
    }

    /// Integer scale of the 5×7 atlas.
    #[must_use]
    pub const fn pixel(self) -> u32 {
        match self {
            Self::Compact => 1,
            Self::Large => 2,
            Self::XLarge => 3,
        }
    }

    /// Advance including 1px (× scale) gap.
    #[must_use]
    pub const fn advance(self) -> u32 {
        6 * self.pixel()
    }

    /// Body row height.
    #[must_use]
    pub const fn row_height(self) -> u32 {
        match self {
            Self::Compact => 18,
            Self::Large => 28,
            Self::XLarge => 40,
        }
    }

    /// Sticky header height.
    #[must_use]
    pub const fn header_height(self) -> u32 {
        match self {
            Self::Compact => 20,
            Self::Large => 32,
            Self::XLarge => 44,
        }
    }

    /// Outer padding.
    #[must_use]
    pub const fn pad(self) -> u32 {
        match self {
            Self::Compact => 8,
            Self::Large => 12,
            Self::XLarge => 16,
        }
    }

    /// Horizontal cell padding.
    #[must_use]
    pub const fn cell_pad_x(self) -> u32 {
        match self {
            Self::Compact => 8,
            Self::Large => 12,
            Self::XLarge => 16,
        }
    }

    /// Auto column min width.
    #[must_use]
    pub const fn min_col(self) -> u32 {
        match self {
            Self::Compact => 56,
            Self::Large => 88,
            Self::XLarge => 120,
        }
    }

    /// Auto column max width.
    #[must_use]
    pub const fn max_col(self) -> u32 {
        match self {
            Self::Compact => 240,
            Self::Large => 360,
            Self::XLarge => 480,
        }
    }

    /// Y-axis label gutter for charts.
    #[must_use]
    pub const fn plot_left(self) -> u32 {
        self.advance().saturating_mul(7).saturating_add(self.pad())
    }

    /// X-axis label band.
    #[must_use]
    pub const fn plot_bottom(self) -> u32 {
        self.row_height().saturating_add(4)
    }
}

/// Where to draw series markers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum MarkerStyle {
    /// No vertex dots.
    None,
    /// Last sample only.
    Last,
    /// Hovered / nearest sample (default).
    #[default]
    Hover,
}

/// Timeseries look. Independent of table metrics.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct ChartStyle {
    /// Polyline thickness in pixels (1–4). Default 2.
    pub stroke_px: u8,
    /// Fill under the line.
    pub area_fill: bool,
    /// Area blend 0–255 against the plot background.
    pub area_alpha: u8,
    /// Target tick count (nice numbers may differ).
    pub tick_count: u8,
    /// 1–2–5 × 10^n ticks.
    pub nice_ticks: bool,
    /// Up to four series colors.
    pub series: [Rgb; 4],
    /// Plot well (distinct from page background).
    pub plot_background: Rgb,
    /// Vertex markers.
    pub markers: MarkerStyle,
}

impl Default for ChartStyle {
    fn default() -> Self {
        Self::from_theme(&Theme::default())
    }
}

impl ChartStyle {
    /// Derive a chart style from a theme (plot well + series palette).
    #[must_use]
    pub fn from_theme(theme: &Theme) -> Self {
        Self {
            stroke_px: 2,
            area_fill: true,
            area_alpha: 48,
            tick_count: 5,
            nice_ticks: true,
            series: [
                theme.series,
                Rgb::new(0x3e, 0xc8, 0xbf),
                theme.crosshair,
                Rgb::new(0xc0, 0x84, 0xfc),
            ],
            plot_background: Rgb::new(
                theme.background.r.saturating_add(14),
                theme.background.g.saturating_add(16),
                theme.background.b.saturating_add(22),
            ),
            markers: MarkerStyle::Hover,
        }
    }
}

/// Full look. Default is compact 5×7 + dark high-readability theme.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Appearance {
    /// Colors.
    pub theme: Theme,
    /// Glyph / cell metrics.
    pub type_scale: TypeScale,
    /// Timeseries stroke/fill/ticks.
    pub chart: ChartStyle,
}

impl Default for Appearance {
    fn default() -> Self {
        let theme = Theme::default();
        Self {
            chart: ChartStyle::from_theme(&theme),
            theme,
            type_scale: TypeScale::Compact,
        }
    }
}

impl Appearance {
    /// Compact 5×7 (same as [`Default`]).
    #[must_use]
    pub fn compact() -> Self {
        Self::default()
    }

    /// Light page, dark text, compact glyphs.
    #[must_use]
    pub fn light() -> Self {
        let theme = Theme::light();
        Self {
            chart: ChartStyle::from_theme(&theme),
            theme,
            type_scale: TypeScale::Compact,
        }
    }

    /// Near-max contrast (dark), compact glyphs.
    #[must_use]
    pub fn high_contrast() -> Self {
        let theme = Theme::high_contrast();
        Self {
            chart: ChartStyle::from_theme(&theme),
            theme,
            type_scale: TypeScale::Compact,
        }
    }

    /// 2× glyphs.
    #[must_use]
    pub fn large() -> Self {
        Self {
            type_scale: TypeScale::Large,
            ..Self::default()
        }
    }

    /// 3× glyphs.
    #[must_use]
    pub fn xlarge() -> Self {
        Self {
            type_scale: TypeScale::XLarge,
            ..Self::default()
        }
    }
}