diff options
| author | cashmere <cashmere@autistici.org> | 2026-07-28 20:14:51 +0200 |
|---|---|---|
| committer | cashmere <cashmere@autistici.org> | 2026-07-28 20:14:51 +0200 |
| commit | 32438ce25f69de99d2523b98237f62cfa90890d7 (patch) | |
| tree | 3a712c347dec0889bdea3328b4c817c2d9392b27 /rust/crates/text/src/params.rs | |
| parent | f5a82152394fde4d8f740f664ab7737b82861618 (diff) | |
| download | opencut-32438ce25f69de99d2523b98237f62cfa90890d7.tar.gz | |
feat: port domain crates to Rust (session 1 complete)
All green: cargo test --workspace (~300 tests, 0 failures).
- timeline (45): placement, resize/ripple, split, snap, retime,
audio separation, scene helpers
- animation (73): bezier solver, channel sampling, keyframe ops,
split/clamp/clone, param definitions
- commands (9): Command trait, BatchCommand, history, 19 concrete
commands, freeze-frame port 1:1
- audio (22): audible collection, range mixing, mastering limiter,
rms/peak buckets
- text (19): cosmic-text layout/measure/raster, backgrounds,
decorations
- graphics (52): full CSS gradient parser, tiny-skia raster,
4 shape definitions
- effect-defs (16): blur definition, registry, pass resolution
- mask-defs (19): all 9 mask renderers via tiny-skia
- renderer (6): node model + scene builder port
- desktop: GPUI projects screen wired to ProjectStore
- scene: factory (web defaults: v32, Main Track, 1920x1080, 30fps)
Diffstat (limited to 'rust/crates/text/src/params.rs')
| -rw-r--r-- | rust/crates/text/src/params.rs | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/rust/crates/text/src/params.rs b/rust/crates/text/src/params.rs new file mode 100644 index 00000000..bf2d7702 --- /dev/null +++ b/rust/crates/text/src/params.rs @@ -0,0 +1,166 @@ +use scene::params::{ParamValue, ParamValues}; + +pub const MIN_FONT_SIZE: f64 = 5.0; +pub const MAX_FONT_SIZE: f64 = 300.0; +pub const DEFAULT_TEXT_COLOR: &str = "#000000"; +pub const FONT_SIZE_SCALE_REFERENCE: f64 = 90.0; +pub const CORNER_RADIUS_MIN: f64 = 0.0; +pub const CORNER_RADIUS_MAX: f64 = 100.0; +pub const DEFAULT_LETTER_SPACING: f64 = 0.0; +pub const DEFAULT_LINE_HEIGHT: f64 = 1.2; +pub const DEFAULT_BACKGROUND_ENABLED: bool = false; +pub const DEFAULT_BACKGROUND_COLOR: &str = "#000000"; +pub const DEFAULT_BACKGROUND_CORNER_RADIUS: f64 = 0.0; +pub const DEFAULT_BACKGROUND_PADDING_X: f64 = 30.0; +pub const DEFAULT_BACKGROUND_PADDING_Y: f64 = 42.0; +pub const DEFAULT_BACKGROUND_OFFSET_X: f64 = 0.0; +pub const DEFAULT_BACKGROUND_OFFSET_Y: f64 = 0.0; + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum TextAlign { + Left, + #[default] + Center, + Right, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum FontWeight { + #[default] + Normal, + Bold, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum FontStyle { + #[default] + Normal, + Italic, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum TextDecoration { + #[default] + None, + Underline, + LineThrough, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct TextLayoutParams { + pub content: String, + pub font_size: f64, + pub font_family: String, + pub font_weight: FontWeight, + pub font_style: FontStyle, + pub text_align: TextAlign, + pub text_decoration: TextDecoration, + pub letter_spacing: f64, + pub line_height: f64, +} + +impl Default for TextLayoutParams { + fn default() -> Self { + Self { + content: "Default text".to_string(), + font_size: 15.0, + font_family: "Arial".to_string(), + font_weight: FontWeight::Normal, + font_style: FontStyle::Normal, + text_align: TextAlign::Center, + text_decoration: TextDecoration::None, + letter_spacing: DEFAULT_LETTER_SPACING, + line_height: DEFAULT_LINE_HEIGHT, + } + } +} + +#[derive(Clone, Debug, PartialEq)] +pub struct TextBackground { + pub enabled: bool, + pub color: String, + pub corner_radius: f64, + pub padding_x: f64, + pub padding_y: f64, + pub offset_x: f64, + pub offset_y: f64, +} + +impl Default for TextBackground { + fn default() -> Self { + Self { + enabled: DEFAULT_BACKGROUND_ENABLED, + color: DEFAULT_BACKGROUND_COLOR.to_string(), + corner_radius: DEFAULT_BACKGROUND_CORNER_RADIUS, + padding_x: DEFAULT_BACKGROUND_PADDING_X, + padding_y: DEFAULT_BACKGROUND_PADDING_Y, + offset_x: DEFAULT_BACKGROUND_OFFSET_X, + offset_y: DEFAULT_BACKGROUND_OFFSET_Y, + } + } +} + +pub fn text_layout_params_from(params: &ParamValues) -> TextLayoutParams { + TextLayoutParams { + content: string_param(params, "content", "Default text"), + font_size: number_param(params, "fontSize", 15.0), + font_family: string_param(params, "fontFamily", "Arial"), + font_weight: match string_param(params, "fontWeight", "normal").as_str() { + "bold" => FontWeight::Bold, + _ => FontWeight::Normal, + }, + font_style: match string_param(params, "fontStyle", "normal").as_str() { + "italic" => FontStyle::Italic, + _ => FontStyle::Normal, + }, + text_align: match string_param(params, "textAlign", "center").as_str() { + "left" => TextAlign::Left, + "right" => TextAlign::Right, + _ => TextAlign::Center, + }, + text_decoration: match string_param(params, "textDecoration", "none").as_str() { + "underline" => TextDecoration::Underline, + "line-through" => TextDecoration::LineThrough, + _ => TextDecoration::None, + }, + letter_spacing: number_param(params, "letterSpacing", DEFAULT_LETTER_SPACING), + line_height: number_param(params, "lineHeight", DEFAULT_LINE_HEIGHT), + } +} + +pub fn text_background_from(params: &ParamValues) -> TextBackground { + TextBackground { + enabled: bool_param(params, "background.enabled", DEFAULT_BACKGROUND_ENABLED), + color: string_param(params, "background.color", DEFAULT_BACKGROUND_COLOR), + corner_radius: number_param( + params, + "background.cornerRadius", + DEFAULT_BACKGROUND_CORNER_RADIUS, + ), + padding_x: number_param(params, "background.paddingX", DEFAULT_BACKGROUND_PADDING_X), + padding_y: number_param(params, "background.paddingY", DEFAULT_BACKGROUND_PADDING_Y), + offset_x: number_param(params, "background.offsetX", DEFAULT_BACKGROUND_OFFSET_X), + offset_y: number_param(params, "background.offsetY", DEFAULT_BACKGROUND_OFFSET_Y), + } +} + +fn string_param(params: &ParamValues, key: &str, fallback: &str) -> String { + match params.get(key) { + Some(ParamValue::String(value)) => value.clone(), + _ => fallback.to_string(), + } +} + +fn number_param(params: &ParamValues, key: &str, fallback: f64) -> f64 { + match params.get(key) { + Some(ParamValue::Number(value)) => *value, + _ => fallback, + } +} + +fn bool_param(params: &ParamValues, key: &str, fallback: bool) -> bool { + match params.get(key) { + Some(ParamValue::Bool(value)) => *value, + _ => fallback, + } +} |
