//! How a screenshot or recording is drawn. //! //! `[recording.style]` resolves through [`crate::profile::Settings`] into the //! session. SVG and raster output share the same style. //! Regenerate the default golden with `TUI_TEST_UPDATE_GOLDEN=1` after intentional changes. use std::path::{Path, PathBuf}; use serde::{Deserialize, Serialize}; use crate::profile::Rgb; /// The default CSS font stack for SVG and raster output. pub const DEFAULT_FONT_FAMILY: &str = "'Cascadia Code','JetBrains Mono','Fira Code',Menlo,Consolas,'DejaVu Sans Mono',monospace"; const DEFAULT_FONT_SIZE: f32 = 17.0; const MAX_FONT_SIZE: f32 = 1_000.0; const MAX_LENGTH: f32 = 10_000.0; const MAX_PADDING: u32 = 10_000; const CELL_W_RATIO: f32 = 10.0 / DEFAULT_FONT_SIZE; const CELL_H_RATIO: f32 = 21.0 / DEFAULT_FONT_SIZE; /// Font families, with optional overrides for bold and italic text. #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(default, deny_unknown_fields)] pub struct FontFamilies { /// The family for unstyled text, and the fallback for the other three. pub family: String, pub bold: Option, pub italic: Option, pub bold_italic: Option, /// Extra font files, relative to the config file. /// Unavailable fonts fall back to bundled and system fonts. /// Raster output loads each canonical path once, with a total 64 MiB limit. /// Font data above this limit causes an error. pub files: Vec, } impl Default for FontFamilies { fn default() -> Self { Self { family: DEFAULT_FONT_FAMILY.to_string(), bold: None, italic: None, bold_italic: None, files: Vec::new(), } } } impl FontFamilies { /// Bold italic falls back through bold, italic, then the base family. pub fn resolve(&self, bold: bool, italic: bool) -> &str { let pick = match (bold, italic) { (true, true) => self .bold_italic .as_deref() .or(self.bold.as_deref()) .or(self.italic.as_deref()), (true, false) => self.bold.as_deref(), (false, true) => self.italic.as_deref(), (false, false) => None, }; pick.unwrap_or(&self.family) } pub fn named(&self) -> impl Iterator { std::iter::once(self.family.as_str()).chain( [ self.bold.as_deref(), self.italic.as_deref(), self.bold_italic.as_deref(), ] .into_iter() .flatten(), ) } pub fn resolve_paths(&mut self, config_dir: &Path) { for file in &mut self.files { if file.is_relative() { *file = config_dir.join(&*file); } } } } /// The window chrome drawn around the grid. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(default, deny_unknown_fields)] pub struct WindowStyle { /// Draw the title bar, divider, and controls above the grid. pub title_bar: bool, /// Draw the three controls when the title bar is visible. pub traffic_lights: bool, pub background: Rgb, pub foreground: Rgb, pub divider: Rgb, } impl WindowStyle { pub fn traffic_lights(&self) -> &[Rgb] { const LIGHTS: [Rgb; 3] = [ Rgb::new(236, 106, 94), Rgb::new(244, 191, 79), Rgb::new(97, 197, 84), ]; if self.title_bar && self.traffic_lights { &LIGHTS } else { &[] } } } impl Default for WindowStyle { fn default() -> Self { Self { title_bar: true, traffic_lights: true, background: Rgb::new(217, 217, 232), foreground: Rgb::new(65, 65, 69), divider: Rgb::new(0, 0, 0), } } } /// A panel border. A zero width disables it. #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] #[serde(default, deny_unknown_fields)] pub struct BorderStyle { pub width: f32, pub color: Rgb, /// Corner radius of the panel, which the border follows. pub radius: f32, } impl Default for BorderStyle { fn default() -> Self { Self { width: 0.0, color: Rgb::new(0, 0, 0), radius: 8.0, } } } /// The drop shadow under the panel. #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] #[serde(default, deny_unknown_fields)] pub struct ShadowStyle { pub enabled: bool, pub color: Rgb, /// How far below the panel the shadow sits. pub offset: f32, /// How far beyond the panel's edges it reaches. pub spread: f32, } impl Default for ShadowStyle { fn default() -> Self { Self { enabled: true, color: Rgb::new(8, 8, 18), offset: 5.0, spread: 7.0, } } } impl ShadowStyle { /// `(spread, offset, alpha)` layers, largest first, shared by both renderers. fn layers(&self) -> [(f32, f32, u8); 4] { let spread_step = self.spread * 2.0 / 7.0; let offset_step = self.offset / 5.0; std::array::from_fn(|index| { let step = index as f32; ( self.spread - step * spread_step, self.offset - step * offset_step, 18 + index as u8 * 2, ) }) } } /// One number covers every side. A table sets individual sides. /// Omitted sides inherit file settings, then the canvas or content defaults. /// /// ```toml /// canvas_padding = 24 /// /// # or /// [recording.style.canvas_padding] /// bottom = 48 /// ``` #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(untagged)] pub enum Padding { Uniform(u32), Sides(PaddingSides), } #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(default, deny_unknown_fields)] pub struct PaddingSides { pub top: Option, pub right: Option, pub bottom: Option, pub left: Option, } impl Padding { fn side(self, pick: fn(PaddingSides) -> Option, fallback: u32) -> u32 { match self { Self::Uniform(value) => value, Self::Sides(sides) => pick(sides).unwrap_or(fallback), } } fn each(self, fallback: Sides) -> [(&'static str, u32); 4] { [ ("top", self.side(|s| s.top, fallback.top)), ("right", self.side(|s| s.right, fallback.right)), ("bottom", self.side(|s| s.bottom, fallback.bottom)), ("left", self.side(|s| s.left, fallback.left)), ] } } /// The default gap on each side, for a `Padding` that names none. #[derive(Debug, Clone, Copy)] struct Sides { top: u32, right: u32, bottom: u32, left: u32, } /// Around the window. const CANVAS_SIDES: Sides = Sides { top: 24, right: 24, bottom: 24, left: 24, }; /// Between the window and the grid inside it. Wider at the sides than at the /// top and bottom because a character sits tight in its cell horizontally and /// the rows already carry their own leading. const CONTENT_SIDES: Sides = Sides { top: 8, right: 15, bottom: 14, left: 15, }; /// Cursor animation in raster recordings. Screenshots use the visible phase. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(default, deny_unknown_fields)] pub struct CursorStyle { /// Allow the application's blinking cursor to animate. pub blink: bool, /// Duration of each visible or hidden phase, before playback speed scaling. pub blink_interval_ms: u64, } impl Default for CursorStyle { fn default() -> Self { Self { blink: true, blink_interval_ms: 500, } } } impl CursorStyle { pub(crate) fn validate(&self) -> Result<(), String> { if self.blink_interval_ms == 0 { return Err("cursor.blink_interval_ms must be greater than zero".into()); } Ok(()) } } /// Everything about how output is drawn. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(default, deny_unknown_fields)] pub struct Style { pub font: FontFamilies, /// Grid font size in pixels. Cell width and height follow it. pub font_size: f32, /// Title bar font size in pixels. pub title_font_size: f32, /// Background outside the panel, separate from the title bar and terminal. pub canvas_background: Rgb, /// The width of that area on every side of the panel. pub canvas_padding: Padding, /// The gap between the window and the grid drawn inside it. pub content_padding: Padding, pub window: WindowStyle, pub border: BorderStyle, pub shadow: ShadowStyle, pub cursor: CursorStyle, } impl Default for Style { fn default() -> Self { Self { font: FontFamilies::default(), font_size: DEFAULT_FONT_SIZE, title_font_size: 13.0, canvas_background: Rgb::new(104, 103, 170), canvas_padding: Padding::Uniform(CANVAS_SIDES.top), content_padding: Padding::Sides(PaddingSides::default()), window: WindowStyle::default(), border: BorderStyle::default(), shadow: ShadowStyle::default(), cursor: CursorStyle::default(), } } } impl Style { /// Reject non-finite values, invalid sizes, and excessive dimensions. pub fn validate(&self) -> Result<(), String> { self.cursor.validate()?; for (name, value) in [ ("font_size", self.font_size), ("title_font_size", self.title_font_size), ] { if !value.is_finite() || value <= 0.0 { return Err(format!("{name} must be finite and greater than zero")); } if value > MAX_FONT_SIZE { return Err(format!("{name} must not exceed {MAX_FONT_SIZE}")); } } for (name, value) in [ ("border.width", self.border.width), ("border.radius", self.border.radius), ("shadow.offset", self.shadow.offset), ("shadow.spread", self.shadow.spread), ] { if !value.is_finite() || value < 0.0 { return Err(format!("{name} must be finite and not negative")); } if value > MAX_LENGTH { return Err(format!("{name} must not exceed {MAX_LENGTH}")); } } for (name, padding, fallback) in [ ("canvas_padding", self.canvas_padding, CANVAS_SIDES), ("content_padding", self.content_padding, CONTENT_SIDES), ] { for (side, value) in padding.each(fallback) { if value > MAX_PADDING { return Err(format!("{name} {side} must not exceed {MAX_PADDING}")); } } } // A positive font size can still be too small to draw with: a // subnormal one leaves cells that round to nothing. if self.cell_width() < 1.0 || self.cell_height() < 1.0 { return Err("font_size is too small to draw a cell".to_string()); } if self.font.family.trim().is_empty() { return Err("font.family must name a font".to_string()); } for (name, family) in [ ("font.bold", &self.font.bold), ("font.italic", &self.font.italic), ("font.bold_italic", &self.font.bold_italic), ] { if family.as_deref().is_some_and(|f| f.trim().is_empty()) { return Err(format!("{name} must name a font when it is set")); } } Ok(()) } pub fn cell_width(&self) -> f32 { self.font_size * CELL_W_RATIO } pub fn cell_height(&self) -> f32 { self.font_size * CELL_H_RATIO } /// Where a glyph sits inside its cell, measured from the cell's top. pub fn baseline(&self) -> f32 { (self.cell_height() - self.font_size) / 2.0 + self.font_size * 0.78 } /// The gap around the window, resolved per side. pub fn canvas_top(&self) -> u32 { self.canvas_padding.side(|s| s.top, CANVAS_SIDES.top) } pub fn canvas_right(&self) -> u32 { self.canvas_padding.side(|s| s.right, CANVAS_SIDES.right) } pub fn canvas_bottom(&self) -> u32 { self.canvas_padding.side(|s| s.bottom, CANVAS_SIDES.bottom) } pub fn canvas_left(&self) -> u32 { self.canvas_padding.side(|s| s.left, CANVAS_SIDES.left) } /// What the canvas grows by to hold the window, or `None` past `u32`. pub fn canvas_horizontal(&self) -> Option { self.canvas_left().checked_add(self.canvas_right()) } pub fn canvas_vertical(&self) -> Option { self.canvas_top().checked_add(self.canvas_bottom()) } pub fn content_top(&self) -> f32 { self.content_padding.side(|s| s.top, CONTENT_SIDES.top) as f32 } pub fn content_right(&self) -> f32 { self.content_padding.side(|s| s.right, CONTENT_SIDES.right) as f32 } pub fn content_bottom(&self) -> f32 { self.content_padding .side(|s| s.bottom, CONTENT_SIDES.bottom) as f32 } pub fn content_left(&self) -> f32 { self.content_padding.side(|s| s.left, CONTENT_SIDES.left) as f32 } /// The height the title bar occupies, zero when it is not drawn. pub fn header_height(&self) -> f32 { if self.window.title_bar { 34.0 } else { 0.0 } } /// The height of the line under the title bar. pub fn divider_height(&self) -> f32 { if self.window.title_bar { 1.0 } else { 0.0 } } /// The shadow's stacked rectangles, empty when it is turned off. pub fn shadow_layers(&self) -> Vec<(f32, f32, u8)> { if self.shadow.enabled { self.shadow.layers().to_vec() } else { Vec::new() } } } #[cfg(test)] mod tests { use super::*; /// The default has to reproduce the constants the renderer used to hold, /// or every existing screenshot and recording shifts. #[test] fn the_default_reproduces_the_original_geometry() { let style = Style::default(); assert_eq!(style.cell_width(), 10.0); assert_eq!(style.cell_height(), 21.0); assert_eq!(style.font_size, 17.0); assert_eq!(style.baseline(), (21.0 - 17.0) / 2.0 + 17.0 * 0.78); assert_eq!(style.header_height(), 34.0); assert_eq!(style.divider_height(), 1.0); assert_eq!(style.canvas_top(), 24); assert_eq!(style.border.radius, 8.0); assert_eq!(style.border.width, 0.0, "no border was drawn before"); assert_eq!(style.shadow_layers().len(), 4); } /// One knob moves the grid: the ratios are what keep a larger font from /// overflowing cells sized for the old one. #[test] fn cell_geometry_follows_the_font_size() { let style = Style { font_size: 34.0, ..Style::default() }; assert_eq!(style.cell_width(), 20.0); assert_eq!(style.cell_height(), 42.0); } #[test] fn a_variant_font_falls_back_to_the_family() { let plain = FontFamilies::default(); assert_eq!(plain.resolve(true, true), DEFAULT_FONT_FAMILY); let mixed = FontFamilies { family: "Regular".into(), bold: Some("Bold".into()), ..FontFamilies::default() }; assert_eq!(mixed.resolve(false, false), "Regular"); assert_eq!(mixed.resolve(true, false), "Bold"); assert_eq!(mixed.resolve(false, true), "Regular"); assert_eq!( mixed.resolve(true, true), "Bold", "bold italic falls through to bold before giving up on the variant" ); } #[test] fn turning_the_title_bar_off_removes_its_height() { let style = Style { window: WindowStyle { title_bar: false, ..WindowStyle::default() }, ..Style::default() }; assert_eq!(style.header_height(), 0.0); assert_eq!(style.divider_height(), 0.0); } #[test] fn a_disabled_shadow_draws_no_layers() { let style = Style { shadow: ShadowStyle { enabled: false, ..ShadowStyle::default() }, ..Style::default() }; assert!(style.shadow_layers().is_empty()); } #[test] fn font_files_resolve_against_the_config_that_named_them() { // Built by joining rather than written out: a literal "/opt/x" is // relative on Windows, which has no drive letter for it, and a // literal expectation would compare separators byte for byte. let config_dir = std::env::temp_dir().join("config"); let absolute = std::env::temp_dir().join("Absolute.ttf"); assert!(absolute.is_absolute(), "the fixture is absolute everywhere"); let mut font = FontFamilies { files: vec![ PathBuf::from("fonts").join("Berkeley.ttf"), absolute.clone(), ], ..FontFamilies::default() }; font.resolve_paths(&config_dir); assert_eq!( font.files[0], config_dir.join("fonts").join("Berkeley.ttf"), "a repository carrying its own font renders the same wherever it sits" ); assert_eq!(font.files[1], absolute, "an absolute path is left alone"); } #[test] fn every_named_family_is_reported_for_loading() { let font = FontFamilies { family: "Regular".into(), bold: Some("Bold".into()), bold_italic: Some("BoldItalic".into()), ..FontFamilies::default() }; let named: Vec<&str> = font.named().collect(); assert_eq!(named, ["Regular", "Bold", "BoldItalic"]); } #[test] fn the_config_spelling_matches_the_rest_of_the_file() { // Colors spells bright_black, so Style spells font_size. One config // file should not carry two naming conventions. let style: Style = toml::from_str( "font_size = 20\ncanvas_padding = 8\n\ [font]\nfamily = \"Berkeley Mono\"\nbold_italic = \"Berkeley Mono Oblique\"\n\ [window]\ntitle_bar = false\n\ [border]\nwidth = 2\ncolor = \"#ff0000\"\n\ [shadow]\nenabled = false\noffset = 2\n", ) .unwrap(); assert_eq!(style.font_size, 20.0); assert_eq!(style.font.resolve(true, true), "Berkeley Mono Oblique"); assert!(!style.window.title_bar); assert_eq!(style.border.width, 2.0); assert!(!style.shadow.enabled); assert_eq!(style.shadow.offset, 2.0); assert_eq!( style.canvas_background, Style::default().canvas_background, "anything unnamed keeps its default" ); } #[test] fn a_kebab_case_key_is_rejected() { let error = toml::from_str::