diff --git a/crates/processing_ffi/src/lib.rs b/crates/processing_ffi/src/lib.rs index afe8823..0288b04 100644 --- a/crates/processing_ffi/src/lib.rs +++ b/crates/processing_ffi/src/lib.rs @@ -1225,6 +1225,7 @@ pub unsafe extern "C" fn processing_text( z: 0.0, max_w: None, max_h: None, + stroke_weight: None, }, ) }); @@ -1257,6 +1258,7 @@ pub unsafe extern "C" fn processing_text_3d( z, max_w: None, max_h: None, + stroke_weight: None, }, ) }); @@ -1278,6 +1280,7 @@ pub extern "C" fn processing_text_int(graphics_id: u64, value: i32, x: f32, y: f z: 0.0, max_w: None, max_h: None, + stroke_weight: None, }, ) }); @@ -1299,6 +1302,7 @@ pub extern "C" fn processing_text_float(graphics_id: u64, value: f32, x: f32, y: z: 0.0, max_w: None, max_h: None, + stroke_weight: None, }, ) }); @@ -1332,6 +1336,7 @@ pub unsafe extern "C" fn processing_text_box( z: 0.0, max_w: Some(w), max_h: Some(h), + stroke_weight: None, }, ) }); diff --git a/crates/processing_node/src/lib.rs b/crates/processing_node/src/lib.rs index 2f2e959..8c02503 100644 --- a/crates/processing_node/src/lib.rs +++ b/crates/processing_node/src/lib.rs @@ -976,6 +976,7 @@ fn text_command(content: String, x: f64, y: f64, z: f64, max: Option<(f64, f64)> z: z as f32, max_w: max.map(|(w, _)| w as f32), max_h: max.map(|(_, h)| h as f32), + stroke_weight: None, } } diff --git a/crates/processing_pyo3/src/graphics.rs b/crates/processing_pyo3/src/graphics.rs index 01c95b1..c5a0435 100644 --- a/crates/processing_pyo3/src/graphics.rs +++ b/crates/processing_pyo3/src/graphics.rs @@ -1689,7 +1689,9 @@ impl Graphics { // --- Text --- - #[pyo3(signature = (content, x, y, *args, max_w=None, max_h=None))] + /// Draws text in the fill color; `stroke_weight` also outlines it in the stroke color. + #[pyo3(signature = (content, x, y, *args, max_w=None, max_h=None, stroke_weight=None))] + #[allow(clippy::too_many_arguments)] pub fn text( &self, content: &str, @@ -1698,6 +1700,7 @@ impl Graphics { args: &Bound<'_, pyo3::types::PyTuple>, max_w: Option, max_h: Option, + stroke_weight: Option, ) -> PyResult<()> { // text(content, x, y) or text(content, x, y, z) or text(content, x, y, max_w, max_h) let (z, mw, mh) = match args.len() { @@ -1732,6 +1735,7 @@ impl Graphics { z, max_w: mw, max_h: mh, + stroke_weight, }, ) .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) diff --git a/crates/processing_pyo3/src/lib.rs b/crates/processing_pyo3/src/lib.rs index 4fad888..e6fbbb2 100644 --- a/crates/processing_pyo3/src/lib.rs +++ b/crates/processing_pyo3/src/lib.rs @@ -2230,7 +2230,8 @@ pub mod processing_py { // --- Text --- #[pyfunction] - #[pyo3(pass_module, signature = (content, x, y, *args, max_w=None, max_h=None))] + #[pyo3(pass_module, signature = (content, x, y, *args, max_w=None, max_h=None, stroke_weight=None))] + #[allow(clippy::too_many_arguments)] fn text( module: &Bound<'_, PyModule>, content: &str, @@ -2239,8 +2240,9 @@ pub mod processing_py { args: &Bound<'_, PyTuple>, max_w: Option, max_h: Option, + stroke_weight: Option, ) -> PyResult<()> { - graphics!(module).text(content, x, y, args, max_w, max_h) + graphics!(module).text(content, x, y, args, max_w, max_h, stroke_weight) } #[pyfunction] diff --git a/crates/processing_render/src/render/command.rs b/crates/processing_render/src/render/command.rs index 7e19f61..179e078 100644 --- a/crates/processing_render/src/render/command.rs +++ b/crates/processing_render/src/render/command.rs @@ -760,6 +760,8 @@ pub enum DrawCommand { z: f32, max_w: Option, max_h: Option, + /// outlines the glyphs in the stroke color; text is not stroked otherwise + stroke_weight: Option, }, } diff --git a/crates/processing_render/src/render/mod.rs b/crates/processing_render/src/render/mod.rs index 288b236..38897ab 100644 --- a/crates/processing_render/src/render/mod.rs +++ b/crates/processing_render/src/render/mod.rs @@ -1361,6 +1361,7 @@ pub fn flush_draw_commands( z, max_w, max_h, + stroke_weight, } => { // rectMode applies to the bounding-box form let (x, y, max_w, max_h) = if let (Some(w), Some(h)) = (max_w, max_h) { @@ -1400,20 +1401,22 @@ pub fn flush_draw_commands( &p_material_handles, ); - add_stroke( - &mut res, - &mut batch, - &state, - |mesh, color, weight| { - // per-glyph fill colors don't apply to the stroke - let mut params = text_params.as_params(); - params.glyph_colors = None; - primitive::text::text_stroke( - mesh, &content, x, y, color, weight, ¶ms, &text_cx, - ); - }, - &p_material_handles, - ); + if let Some(weight) = stroke_weight { + add_stroke( + &mut res, + &mut batch, + &state, + |mesh, color, _| { + // per-glyph fill colors don't apply to the stroke + let mut params = text_params.as_params(); + params.glyph_colors = None; + primitive::text::text_stroke( + mesh, &content, x, y, color, weight, ¶ms, &text_cx, + ); + }, + &p_material_handles, + ); + } if z != 0.0 { state diff --git a/crates/processing_render/src/render/primitive/text.rs b/crates/processing_render/src/render/primitive/text.rs index 4039839..4ca8677 100644 --- a/crates/processing_render/src/render/primitive/text.rs +++ b/crates/processing_render/src/render/primitive/text.rs @@ -149,7 +149,7 @@ pub fn text( text_cx.with(|font_cx, layout_cx| { let layout = build_layout(font_cx, layout_cx, content, color, params); - let (base_x, base_y) = compute_text_origin(&layout, x, y, params.align_v); + let (base_x, base_y) = compute_text_origin(&layout, x, y, params); tessellate_layout( mesh, &layout, @@ -178,7 +178,7 @@ pub fn text_stroke( text_cx.with(|font_cx, layout_cx| { let layout = build_layout(font_cx, layout_cx, content, color, params); - let (base_x, base_y) = compute_text_origin(&layout, x, y, params.align_v); + let (base_x, base_y) = compute_text_origin(&layout, x, y, params); stroke_layout( mesh, &layout, @@ -248,7 +248,7 @@ pub fn text_bounds( let layout = build_layout(font_cx, layout_cx, content, Color::BLACK, params); // the text origin is the layout's top-left corner - let (box_x, box_y) = compute_text_origin(&layout, x, y, params.align_v); + let (box_x, box_y) = compute_text_origin(&layout, x, y, params); let width = layout.width(); let height = match params.max_h { Some(h) => layout.height().min(h), @@ -298,7 +298,7 @@ pub fn text_lines( } text_cx.with(|font_cx, layout_cx| { let layout = build_layout(font_cx, layout_cx, content, Color::BLACK, params); - let (base_x, base_y) = compute_text_origin(&layout, x, y, params.align_v); + let (base_x, base_y) = compute_text_origin(&layout, x, y, params); let mut result = Vec::new(); for line_idx in 0..layout.len() { @@ -343,7 +343,7 @@ pub fn text_glyph_rects( } text_cx.with(|font_cx, layout_cx| { let layout = build_layout(font_cx, layout_cx, content, Color::BLACK, params); - let (base_x, base_y) = compute_text_origin(&layout, x, y, params.align_v); + let (base_x, base_y) = compute_text_origin(&layout, x, y, params); let mut result = Vec::new(); for line_idx in 0..layout.len() { @@ -391,7 +391,7 @@ pub fn text_to_paths( text_cx.with(|font_cx, layout_cx| { let layout = build_layout(font_cx, layout_cx, content, Color::BLACK, params); - let (base_x, base_y) = compute_text_origin(&layout, x, y, params.align_v); + let (base_x, base_y) = compute_text_origin(&layout, x, y, params); extract_glyph_path_commands(&layout, base_x, base_y, params.max_h) }) } @@ -436,7 +436,7 @@ pub fn text_to_points( text_cx.with(|font_cx, layout_cx| { let layout = build_layout(font_cx, layout_cx, content, Color::BLACK, params); - let (base_x, base_y) = compute_text_origin(&layout, x, y, params.align_v); + let (base_x, base_y) = compute_text_origin(&layout, x, y, params); let glyph_paths = extract_glyph_lyon_paths(&layout, base_x, base_y, params.max_h); let step = sample_factor.max(0.001); @@ -523,7 +523,7 @@ pub fn text_to_model( text_cx.with(|font_cx, layout_cx| { let layout = build_layout(font_cx, layout_cx, content, Color::WHITE, params); - let (base_x, base_y) = compute_text_origin(&layout, x, y, params.align_v); + let (base_x, base_y) = compute_text_origin(&layout, x, y, params); let glyph_paths = extract_glyph_lyon_paths_yup(&layout, base_x, base_y, params.max_h); let half_depth = depth / 2.0; @@ -731,7 +731,14 @@ impl<'a> FillGeometryBuilder for Extrusion3DBuilder<'a> { /// Absolute position of the layout's top-left corner — parley's glyph /// positions are measured against it. `y` is the first line's baseline for /// `Baseline` align, otherwise the top/center/bottom of the text block. -fn compute_text_origin(layout: &Layout, x: f32, y: f32, align_v: TextAlignV) -> (f32, f32) { +fn compute_text_origin(layout: &Layout, x: f32, y: f32, params: &TextParams) -> (f32, f32) { + // without a box to align in, x is the anchor the lines hang from + let x_offset = match (params.max_w, params.align_h) { + (Some(_), _) | (None, TextAlignH::Left) => x, + (None, TextAlignH::Center) => x - layout.width() / 2.0, + (None, TextAlignH::Right) => x - layout.width(), + }; + let align_v = params.align_v; let total_height = layout.height(); let first_baseline = layout .get(0) @@ -745,7 +752,7 @@ fn compute_text_origin(layout: &Layout, x: f32, y: f32, align_v: TextAlig TextAlignV::Bottom => y - total_height, }; - (x, y_offset) + (x_offset, y_offset) } /// Extract text outlines, one `PathCommand` vec per contour. @@ -762,7 +769,7 @@ pub fn text_to_contours( text_cx.with(|font_cx, layout_cx| { let layout = build_layout(font_cx, layout_cx, content, Color::BLACK, params); - let (base_x, base_y) = compute_text_origin(&layout, x, y, params.align_v); + let (base_x, base_y) = compute_text_origin(&layout, x, y, params); let lyon_paths = extract_glyph_lyon_paths(&layout, base_x, base_y, params.max_h); let mut contours = Vec::new(); diff --git a/crates/processing_wasm/src/lib.rs b/crates/processing_wasm/src/lib.rs index a9d33b3..aa82977 100644 --- a/crates/processing_wasm/src/lib.rs +++ b/crates/processing_wasm/src/lib.rs @@ -847,6 +847,7 @@ pub fn js_text(graphics_id: u64, content: &str, x: f32, y: f32) -> Result<(), Js z: 0.0, max_w: None, max_h: None, + stroke_weight: None, }, )) } @@ -864,6 +865,7 @@ pub fn js_text_3d(graphics_id: u64, content: &str, x: f32, y: f32, z: f32) -> Re z, max_w: None, max_h: None, + stroke_weight: None, }, )) } @@ -881,6 +883,7 @@ pub fn js_text_int(graphics_id: u64, value: i32, x: f32, y: f32) -> Result<(), J z: 0.0, max_w: None, max_h: None, + stroke_weight: None, }, )) } @@ -898,6 +901,7 @@ pub fn js_text_float(graphics_id: u64, value: f32, x: f32, y: f32) -> Result<(), z: 0.0, max_w: None, max_h: None, + stroke_weight: None, }, )) } @@ -922,6 +926,7 @@ pub fn js_text_box( z: 0.0, max_w: Some(w), max_h: Some(h), + stroke_weight: None, }, )) } diff --git a/examples/midi.rs b/examples/midi.rs index 783bff1..3529850 100644 --- a/examples/midi.rs +++ b/examples/midi.rs @@ -136,6 +136,7 @@ fn sketch() -> error::Result<()> { z: 0.0, max_w: None, max_h: None, + stroke_weight: None, })?; } diff --git a/examples/text.rs b/examples/text.rs index 956a56d..c972cd0 100644 --- a/examples/text.rs +++ b/examples/text.rs @@ -42,6 +42,7 @@ fn sketch() -> error::Result<()> { z: 0.0, max_w: None, max_h: None, + stroke_weight: None, }, )?; @@ -56,6 +57,7 @@ fn sketch() -> error::Result<()> { z: 0.0, max_w: None, max_h: None, + stroke_weight: None, }, )?; @@ -70,6 +72,7 @@ fn sketch() -> error::Result<()> { z: 0.0, max_w: Some(300.0), max_h: Some(200.0), + stroke_weight: None, }, )?; @@ -91,6 +94,7 @@ fn sketch() -> error::Result<()> { z: 0.0, max_w: Some(200.0), max_h: None, + stroke_weight: None, }, )?;