mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 18:25:34 +01:00
A new tool in the Pen tool's group, chosen with Alt+P (Option+P on macOS), draws paths of straight lines and circular arcs, as the Quick Pen help pages describe: https://helpx.adobe.com/illustrator/web/add-and-edit-artwork/draw-and-edit-paths/draw-with-quick-pen-tool.html https://helpx.adobe.com/illustrator/web/add-and-edit-artwork/draw-and-edit-paths/quick-pen-tool-keyboard-shortcuts.html - A click sets the start point; each click, or a press dragged to where it is let go, adds a line, a continuous arc (it leaves the last point in the path's direction) or a sharp-corner arc, as the tool's action says. Each piece is one undo step. - Alt (Option) pressed and let go on its own switches to the next action. C flips the last sharp-corner arc to the other side of its chord, and [ and ] make the arc size 15 degrees smaller or larger. - A click on the first point closes the path, a click on an end of an open path continues it, and Escape or Enter ends it. An edit made to the path from outside the tool also ends it. - path.quickPen {start | id, subpath?, keep?, pieces, closed?} draws the same path for agents and scripts. The action and arc size are kept with the preferences. The 180 degree default arc size, its 15 degree steps, the side an arc bulges to and Enter ending the path are VectorCraft's own rules. Part of #2660 (reported by earlvanze). Not yet: the on-canvas toolbar, dragging a path's ends to change its length, Ctrl+scroll for the arc size, and editing and deleting with the tool.
97 lines
4.2 KiB
Rust
97 lines
4.2 KiB
Rust
//! The Quick Pen tool's command (`path.quickPen`): a path of straight lines and circular arcs.
|
|
|
|
use vectorcraft_tools::quickpen::{self as qp, Piece, Stroke};
|
|
|
|
use super::common::{path_kind, path_mut, point_entry, usize_opt, within_input_bound};
|
|
use super::{Color, Paint, PathData, Result, Session, SubPath, Value, add_look, bad, json, number_or, object_param, switch_or};
|
|
|
|
const C: &str = "path.quickPen";
|
|
/// The most pieces one request takes.
|
|
const MAX_PIECES: usize = 10_000;
|
|
|
|
/// The `pieces` param.
|
|
fn pieces(p: &Value) -> Result<Vec<Piece>> {
|
|
let list = match p.get("pieces") {
|
|
None | Some(Value::Null) => return Ok(vec![]),
|
|
Some(Value::Array(list)) if list.len() <= MAX_PIECES => list,
|
|
Some(_) => return Err(bad(C, "`pieces` must be a list of at most 10000 pieces")),
|
|
};
|
|
list.iter()
|
|
.map(|v| {
|
|
let to = point_entry(v, C)?;
|
|
if !within_input_bound(to) {
|
|
return Err(bad(C, "x and y must be finite"));
|
|
}
|
|
let stroke = match v.get("action") {
|
|
None | Some(Value::Null) => Stroke::Line,
|
|
Some(a) => a.as_str().and_then(Stroke::parse).ok_or_else(|| {
|
|
let key = "action";
|
|
bad(C, format!("`{key}` must be one of {}", Stroke::ALL.map(Stroke::name).join(", ")))
|
|
})?,
|
|
};
|
|
let turn = number_or(v, "turn", qp::DEFAULT_TURN, C)?;
|
|
if !(qp::MIN_TURN..=qp::MAX_TURN).contains(&turn) {
|
|
let (key, min, max) = ("turn", qp::MIN_TURN, qp::MAX_TURN);
|
|
return Err(bad(C, format!("{key} must be a number from {min} to {max}")));
|
|
}
|
|
let flip = switch_or(v, "flip", false, C)?;
|
|
Ok(Piece { to, stroke, turn, flip })
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
pub(super) fn quick_pen(s: &mut Session, p: &Value) -> Result<Value> {
|
|
let pieces = pieces(p)?;
|
|
let closed = switch_or(p, "closed", false, C)?;
|
|
let start_given = p.get("start").is_some_and(|v| !v.is_null());
|
|
let either = "give either `start` for a new path or `id`, not both";
|
|
let Some(id) = object_param(p, "id", C)? else {
|
|
let start = match p.get("start") {
|
|
Some(v) if start_given => point_entry(v, C)?,
|
|
_ => return Err(bad(C, either)),
|
|
};
|
|
if !within_input_bound(start) {
|
|
return Err(bad(C, "x and y must be finite"));
|
|
}
|
|
if ["subpath", "keep"].iter().any(|key| p.get(key).is_some_and(|v| !v.is_null())) {
|
|
return Err(bad(C, "`subpath` and `keep` need `id`"));
|
|
}
|
|
let sp = qp::build(&SubPath::new(vec![vectorcraft_geom::Anchor::corner(start)], false), &pieces, closed);
|
|
let mut look = s.new_art_look(s.paint.fill.clone(), s.paint.stroke.clone(), s.paint.stroke_width);
|
|
let ap = &mut look.appearance;
|
|
if ap.stroke_paint().is_none() && ap.fill_paint().is_none() {
|
|
ap.set_stroke(Paint::solid(Color::BLACK));
|
|
}
|
|
return add_look(s, "Quick Pen", path_kind(PathData::single(sp)), look, None);
|
|
};
|
|
if start_given {
|
|
return Err(bad(C, either));
|
|
}
|
|
let si = usize_opt(p, "subpath", C)?.unwrap_or(0);
|
|
let d = &s.doc()?.doc;
|
|
if !d.is_editable(id) {
|
|
return Err(bad(C, "the path is locked or hidden"));
|
|
}
|
|
let path = d.node(id).and_then(|n| n.path_data()).ok_or_else(|| bad(C, "`id` is not a path"))?;
|
|
let sp = path.subpaths.get(si).ok_or_else(|| bad(C, "no such subpath"))?;
|
|
if sp.closed {
|
|
return Err(bad(C, "the subpath is closed"));
|
|
}
|
|
let n = sp.anchors.len();
|
|
let keep = usize_opt(p, "keep", C)?.unwrap_or(n);
|
|
if keep == 0 || keep > n {
|
|
let (key, min, max) = ("keep", 1, n);
|
|
return Err(bad(C, format!("{key} must be a number from {min} to {max}")));
|
|
}
|
|
let base = SubPath::new(sp.anchors.get_range(..keep).unwrap_or_default(), false);
|
|
let out = qp::build(&base, &pieces, closed);
|
|
let top = super::super::path::compound_or_self(d, id);
|
|
s.edit("Quick Pen", |d, sel| {
|
|
let slot = path_mut(d, id)?.subpaths.get_mut(si).ok_or_else(|| crate::EngineError::Other("no such subpath".into()))?;
|
|
*slot = out;
|
|
sel.set([top]);
|
|
Ok(())
|
|
})?;
|
|
Ok(json!({ "id": id.0 }))
|
|
}
|