From ab872e8c3f9f7d1eecf9080e4ce8451cc86ccc42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?charlotte=20=F0=9F=8C=B8?= Date: Tue, 1 Dec 2026 12:28:50 -0800 Subject: [PATCH] Drawing defaults. --- crates/processing_pyo3/src/graphics.rs | 9 ++-- crates/processing_pyo3/src/lib.rs | 4 +- .../processing_render/src/render/command.rs | 14 ++--- crates/processing_render/src/render/mod.rs | 52 ++++++++++++++++--- .../src/render/primitive/arc.rs | 4 +- .../src/render/primitive/mod.rs | 2 +- crates/processing_render/src/transform.rs | 5 +- 7 files changed, 69 insertions(+), 21 deletions(-) diff --git a/crates/processing_pyo3/src/graphics.rs b/crates/processing_pyo3/src/graphics.rs index 74b9041c..bfe35c0e 100644 --- a/crates/processing_pyo3/src/graphics.rs +++ b/crates/processing_pyo3/src/graphics.rs @@ -1617,10 +1617,13 @@ impl Graphics { h: f32, start: f32, stop: f32, - mode: &str, + mode: Option<&str>, ) -> PyResult<()> { - let mode = ArcMode::parse(mode) - .ok_or_else(|| PyValueError::new_err(format!("unknown arc mode: {mode:?}")))?; + let mode = match mode { + Some(mode) => ArcMode::parse(mode) + .ok_or_else(|| PyValueError::new_err(format!("unknown arc mode: {mode:?}")))?, + None => ArcMode::default(), + }; graphics_record_command( self.entity, DrawCommand::Arc { diff --git a/crates/processing_pyo3/src/lib.rs b/crates/processing_pyo3/src/lib.rs index feac7b7c..9f3d2b69 100644 --- a/crates/processing_pyo3/src/lib.rs +++ b/crates/processing_pyo3/src/lib.rs @@ -2546,7 +2546,7 @@ pub mod processing_py { } #[pyfunction] - #[pyo3(pass_module, signature = (cx, cy, w, h, start, stop, mode=processing::prelude::constants::OPEN))] + #[pyo3(pass_module, signature = (cx, cy, w, h, start, stop, mode=None))] fn arc( module: &Bound<'_, PyModule>, cx: f32, @@ -2555,7 +2555,7 @@ pub mod processing_py { h: f32, start: f32, stop: f32, - mode: &str, + mode: Option<&str>, ) -> PyResult<()> { graphics!(module).arc(cx, cy, w, h, start, stop, mode) } diff --git a/crates/processing_render/src/render/command.rs b/crates/processing_render/src/render/command.rs index 054b5677..ecb1abd1 100644 --- a/crates/processing_render/src/render/command.rs +++ b/crates/processing_render/src/render/command.rs @@ -197,18 +197,20 @@ impl StrokeJoinMode { #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] #[repr(u8)] pub enum ArcMode { + /// filled as a pie, stroked along the arc only: `arc()` with no mode #[default] - Open = 0, - Chord = 1, - Pie = 2, + Default = 0, + Open = 1, + Chord = 2, + Pie = 3, } impl From for ArcMode { fn from(v: u8) -> Self { match v { - 0 => Self::Open, - 1 => Self::Chord, - 2 => Self::Pie, + 1 => Self::Open, + 2 => Self::Chord, + 3 => Self::Pie, _ => Self::default(), } } diff --git a/crates/processing_render/src/render/mod.rs b/crates/processing_render/src/render/mod.rs index 249874d8..2dfb9012 100644 --- a/crates/processing_render/src/render/mod.rs +++ b/crates/processing_render/src/render/mod.rs @@ -328,7 +328,7 @@ pub fn flush_draw_commands( } } DrawCommand::Fill(color) => { - state.style.fill = Fill::Color(color); + state.style.fill = Fill::Color(in_range(color)); } DrawCommand::FillBuffer(buf_entity) => { state.style.fill = Fill::Buffer(buf_entity); @@ -337,7 +337,7 @@ pub fn flush_draw_commands( state.style.fill = Fill::None; } DrawCommand::StrokeColor(color) => { - state.style.stroke_color = Some(color); + state.style.stroke_color = Some(in_range(color)); } DrawCommand::NoStroke => { state.style.stroke_color = None; @@ -681,6 +681,9 @@ pub fn flush_draw_commands( stop, mode, } => { + let Some((start, stop)) = arc_angles(start, stop) else { + continue; + }; let (x, y, w, h) = apply_shape_mode(state.style.ellipse_mode, cx, cy, w, h); let cx = x + w / 2.0; let cy = y + h / 2.0; @@ -868,7 +871,7 @@ pub fn flush_draw_commands( } } DrawCommand::Tint(color) => { - state.style.tint_color = Some(color); + state.style.tint_color = Some(in_range(color)); } DrawCommand::NoTint => { state.style.tint_color = None; @@ -978,9 +981,14 @@ pub fn flush_draw_commands( DrawCommand::PushStyle => state.style.push(), DrawCommand::PopStyle => state.style.pop(), DrawCommand::Translate(v) => state.transform.apply(Affine3A::from_translation(v)), - DrawCommand::Rotate { angle, axis } => state - .transform - .apply(Affine3A::from_axis_angle(axis.normalize(), angle)), + DrawCommand::Rotate { angle, axis } => { + // a zero axis has no rotation + if let Some(axis) = axis.try_normalize() { + state + .transform + .apply(Affine3A::from_axis_angle(axis, angle)); + } + } DrawCommand::Scale(v) => state.transform.apply(Affine3A::from_scale(v)), DrawCommand::ShearX { angle } => state.transform.apply(transform::shear_x(angle)), DrawCommand::ShearY { angle } => state.transform.apply(transform::shear_y(angle)), @@ -1511,6 +1519,38 @@ fn start_batch( batch.current_mesh = Some(empty_mesh()); } +/// A color with its alpha in 0..=1 and no negative channel. +fn in_range(color: Color) -> Color { + let color = color.with_alpha(color.alpha().clamp(0.0, 1.0)); + let linear = color.to_linear(); + if linear.red < 0.0 || linear.green < 0.0 || linear.blue < 0.0 { + return LinearRgba::new( + linear.red.max(0.0), + linear.green.max(0.0), + linear.blue.max(0.0), + linear.alpha, + ) + .into(); + } + color +} + +/// The angles an arc sweeps, from `start` forwards; `None` when it has no extent. +fn arc_angles(start: f32, stop: f32) -> Option<(f32, f32)> { + use std::f32::consts::TAU; + let sweep = stop - start; + if sweep == 0.0 || !sweep.is_finite() { + return None; + } + // a stop before the start wraps around; more than a turn is one turn + let sweep = if sweep >= TAU { + TAU + } else { + sweep.rem_euclid(TAU) + }; + (sweep > 0.0).then_some((start, start + sweep)) +} + fn apply_shape_mode(mode: ShapeMode, a: f32, b: f32, c: f32, d: f32) -> (f32, f32, f32, f32) { match mode { ShapeMode::Corner => (a, b, c, d), diff --git a/crates/processing_render/src/render/primitive/arc.rs b/crates/processing_render/src/render/primitive/arc.rs index e91a901a..2aee0628 100644 --- a/crates/processing_render/src/render/primitive/arc.rs +++ b/crates/processing_render/src/render/primitive/arc.rs @@ -29,7 +29,7 @@ fn arc_path( let first_point = Point::new(cx + rx * start.cos(), cy + ry * start.sin()); match mode { - ArcMode::Pie if for_fill => { + ArcMode::Pie | ArcMode::Default if for_fill => { builder.begin(Point::new(cx, cy)); builder.line_to(first_point); } @@ -47,7 +47,7 @@ fn arc_path( let should_close = match mode { ArcMode::Pie => true, ArcMode::Chord => true, - ArcMode::Open => for_fill, + ArcMode::Open | ArcMode::Default => for_fill, }; builder.end(should_close); diff --git a/crates/processing_render/src/render/primitive/mod.rs b/crates/processing_render/src/render/primitive/mod.rs index e7b61f26..c8b4f12a 100644 --- a/crates/processing_render/src/render/primitive/mod.rs +++ b/crates/processing_render/src/render/primitive/mod.rs @@ -53,7 +53,7 @@ impl Default for StrokeConfig { fn default() -> Self { Self { line_cap: StrokeCapMode::Round, - line_join: StrokeJoinMode::Round, + line_join: StrokeJoinMode::Miter, } } } diff --git a/crates/processing_render/src/transform.rs b/crates/processing_render/src/transform.rs index 9661763d..adf26616 100644 --- a/crates/processing_render/src/transform.rs +++ b/crates/processing_render/src/transform.rs @@ -75,7 +75,10 @@ pub fn rotate_axis( let mut transform = transforms .get_mut(entity) .map_err(|_| ProcessingError::TransformNotFound)?; - transform.rotate(Quat::from_axis_angle(axis.normalize(), angle)); + // a zero axis has no rotation + if let Some(axis) = axis.try_normalize() { + transform.rotate(Quat::from_axis_angle(axis, angle)); + } Ok(()) }