Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/processing_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,7 @@ pub unsafe extern "C" fn processing_text(
z: 0.0,
max_w: None,
max_h: None,
stroke_weight: None,
},
)
});
Expand Down Expand Up @@ -1257,6 +1258,7 @@ pub unsafe extern "C" fn processing_text_3d(
z,
max_w: None,
max_h: None,
stroke_weight: None,
},
)
});
Expand All @@ -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,
},
)
});
Expand All @@ -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,
},
)
});
Expand Down Expand Up @@ -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,
},
)
});
Expand Down
1 change: 1 addition & 0 deletions crates/processing_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
6 changes: 5 additions & 1 deletion crates/processing_pyo3/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -1698,6 +1700,7 @@ impl Graphics {
args: &Bound<'_, pyo3::types::PyTuple>,
max_w: Option<f32>,
max_h: Option<f32>,
stroke_weight: Option<f32>,
) -> 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() {
Expand Down Expand Up @@ -1732,6 +1735,7 @@ impl Graphics {
z,
max_w: mw,
max_h: mh,
stroke_weight,
},
)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
Expand Down
6 changes: 4 additions & 2 deletions crates/processing_pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -2239,8 +2240,9 @@ pub mod processing_py {
args: &Bound<'_, PyTuple>,
max_w: Option<f32>,
max_h: Option<f32>,
stroke_weight: Option<f32>,
) -> 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]
Expand Down
2 changes: 2 additions & 0 deletions crates/processing_render/src/render/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,8 @@ pub enum DrawCommand {
z: f32,
max_w: Option<f32>,
max_h: Option<f32>,
/// outlines the glyphs in the stroke color; text is not stroked otherwise
stroke_weight: Option<f32>,
},
}

Expand Down
31 changes: 17 additions & 14 deletions crates/processing_render/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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, &params, &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, &params, &text_cx,
);
},
&p_material_handles,
);
}

if z != 0.0 {
state
Expand Down
29 changes: 18 additions & 11 deletions crates/processing_render/src/render/primitive/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Color>, x: f32, y: f32, align_v: TextAlignV) -> (f32, f32) {
fn compute_text_origin(layout: &Layout<Color>, 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)
Expand All @@ -745,7 +752,7 @@ fn compute_text_origin(layout: &Layout<Color>, 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.
Expand All @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions crates/processing_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
))
}
Expand All @@ -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,
},
))
}
Expand All @@ -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,
},
))
}
Expand All @@ -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,
},
))
}
Expand All @@ -922,6 +926,7 @@ pub fn js_text_box(
z: 0.0,
max_w: Some(w),
max_h: Some(h),
stroke_weight: None,
},
))
}
Expand Down
1 change: 1 addition & 0 deletions examples/midi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ fn sketch() -> error::Result<()> {
z: 0.0,
max_w: None,
max_h: None,
stroke_weight: None,
})?;
}

Expand Down
4 changes: 4 additions & 0 deletions examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn sketch() -> error::Result<()> {
z: 0.0,
max_w: None,
max_h: None,
stroke_weight: None,
},
)?;

Expand All @@ -56,6 +57,7 @@ fn sketch() -> error::Result<()> {
z: 0.0,
max_w: None,
max_h: None,
stroke_weight: None,
},
)?;

Expand All @@ -70,6 +72,7 @@ fn sketch() -> error::Result<()> {
z: 0.0,
max_w: Some(300.0),
max_h: Some(200.0),
stroke_weight: None,
},
)?;

Expand All @@ -91,6 +94,7 @@ fn sketch() -> error::Result<()> {
z: 0.0,
max_w: Some(200.0),
max_h: None,
stroke_weight: None,
},
)?;

Expand Down
Loading