mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 19:36:16 +00:00
Merge pull request #8997 from dotCooCoo/feat/smooth-tool-smoothness
Smooth: Smooth Tool Options' Smoothness sets how strongly a drag smooths
This commit is contained in:
@@ -129,7 +129,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Smooth",
|
||||
[],
|
||||
None,
|
||||
"{points: [[x,y]…] (pen values after x and y are checked as path.freehand checks them and do not change the smoothing), radius?: pt (12; from 0.001 to 1000), fidelity?: pt (2.5; from 0.000078125 to about 3195, the Smooth tool's 0.05 to 100 px at the highest and the lowest zoom: the tool sends its Fidelity in screen pixels divided by the zoom, kept within the range)} smooth selected paths near the drag, keeping a corner where the path turns by more than 57.5° at the anchor and over radius and twice radius on either side, and leaving a subpath as it is when the result would not be smoother (docs/mcp.md) → {ids, before, after}. A radius or fidelity outside its range is refused with an error, and nothing changes",
|
||||
"{points: [[x,y]…] (pen values after x and y are checked as path.freehand checks them and do not change the smoothing), radius?: pt (12; from 0.001 to 1000), fidelity?: pt (2.5; from 0.000078125 to about 3195, the Smooth tool's 0.05 to 100 px at the highest and the lowest zoom: the tool sends its Fidelity in screen pixels divided by the zoom, kept within the range), smoothness?: % (50; 0 to 100: how far along the path each point is averaged before the refit, in proportion: none at 0, twice as far as at 50 at 100)} smooth selected paths near the drag, keeping a corner where the path turns by more than 57.5° at the anchor and over radius and twice radius on either side, and leaving a subpath as it is when the result would not be smoother (docs/mcp.md) → {ids, before, after}. A radius, fidelity or smoothness outside its range is refused with an error, and nothing changes",
|
||||
has_selection,
|
||||
smooth_region
|
||||
),
|
||||
|
||||
@@ -12,10 +12,16 @@ use super::{
|
||||
// ---------- Smooth ----------
|
||||
|
||||
/// The spread (points) of the Gaussian the Smooth tool smooths a path along its length with before
|
||||
/// refitting it, as a share of the tool's reach: a third, so each point is averaged with the stretch
|
||||
/// of path within the reach on either side of it. VectorCraft's own value.
|
||||
/// refitting it at the default Smoothness ([`SMOOTH_SMOOTHNESS`]), as a share of the tool's reach: a
|
||||
/// third, so each point is averaged with the stretch of path within the reach on either side of it.
|
||||
/// The spread grows in proportion to the Smoothness, from none at 0% to twice this at 100%.
|
||||
/// VectorCraft's own value.
|
||||
const SMOOTH_SPREAD: f64 = 1.0 / 3.0;
|
||||
|
||||
/// The Smoothness (percent) `path.smoothRegion` smooths with when the drag leaves it out. VectorCraft's
|
||||
/// own value.
|
||||
const SMOOTH_SMOOTHNESS: f64 = 50.0;
|
||||
|
||||
/// The degrees by which a path must turn at an anchor, and over the tool's reach and twice its reach
|
||||
/// on either side of it, for the Smooth tool to keep the anchor as a corner ([`smooth_corners`]): a
|
||||
/// turn of more than 57.5°, an angle inside the corner of less than 122.5°. VectorCraft's own value,
|
||||
@@ -929,7 +935,11 @@ pub(super) fn smooth_region(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if !(lo..=hi).contains(&tol) {
|
||||
return Err(bad(C, format!("`fidelity` must be from {lo} to {hi} pt")));
|
||||
}
|
||||
let sigma = radius * SMOOTH_SPREAD;
|
||||
let smoothness = number_or(p, "smoothness", SMOOTH_SMOOTHNESS, C)?;
|
||||
if !(0.0..=100.0).contains(&smoothness) {
|
||||
return Err(bad(C, "`smoothness` must be from 0 to 100"));
|
||||
}
|
||||
let sigma = radius * SMOOTH_SPREAD * smoothness / SMOOTH_SMOOTHNESS;
|
||||
let st = s.doc()?;
|
||||
let mut plan = vec![];
|
||||
let (mut before, mut after) = (0, 0);
|
||||
|
||||
@@ -1802,6 +1802,64 @@ fn smooth_region_refits_at_the_tolerance_sent_at_any_zoom() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A line from (100, 200) to (400, 200) jagged by up to 6 pt either way, the same every time.
|
||||
fn jagged_line_anchors() -> Vec<serde_json::Value> {
|
||||
let mut seed = 7u64;
|
||||
(0..=60)
|
||||
.map(|i| {
|
||||
seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
|
||||
let noise = ((seed >> 11) as f64 / (1u64 << 53) as f64) * 2.0 - 1.0;
|
||||
json!({"x": 100.0 + f64::from(i) * 5.0, "y": 200.0 + 6.0 * noise})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The path a `path.smoothRegion` drag along [`jagged_line_anchors`] leaves with `smoothness` (left
|
||||
/// out for null), and the mean distance of its points from the line y = 200.
|
||||
fn smoothed_jagged_line(smoothness: serde_json::Value) -> (PathData, f64) {
|
||||
let mut s = session();
|
||||
let id = smooth_test_path(&mut s, &jagged_line_anchors(), false);
|
||||
let drag: Vec<_> = (0..=64).map(|i| json!([90.0 + f64::from(i) * 5.0, 200.0])).collect();
|
||||
s.execute("path.smoothRegion", &json!({"points": drag, "radius": 12, "fidelity": 2.5, "smoothness": smoothness})).unwrap();
|
||||
let p = path(&s, id);
|
||||
let samples = smooth_test_samples(&p);
|
||||
let off = samples.iter().map(|q| (q.y - 200.0).abs()).sum::<f64>() / samples.len() as f64;
|
||||
(p, off)
|
||||
}
|
||||
|
||||
/// Smooth Tool Options' Smoothness: the higher it is, the smoother the path a drag leaves, from
|
||||
/// 0% to 100%. Left out, a drag smooths at 50%.
|
||||
#[test]
|
||||
fn a_higher_smoothness_leaves_a_smoother_path() {
|
||||
let original = {
|
||||
let mut s = session();
|
||||
let id = smooth_test_path(&mut s, &jagged_line_anchors(), false);
|
||||
let samples = smooth_test_samples(&path(&s, id));
|
||||
samples.iter().map(|q| (q.y - 200.0).abs()).sum::<f64>() / samples.len() as f64
|
||||
};
|
||||
let offs: Vec<f64> = [0.0, 25.0, 50.0, 75.0, 100.0].iter().map(|&x| smoothed_jagged_line(json!(x)).1).collect();
|
||||
assert!(offs[0] < original, "even 0% refits within Fidelity: {offs:?}, {original} before");
|
||||
assert!(offs.windows(2).all(|w| w[1] < w[0]), "each step up smooths more: {offs:?}");
|
||||
assert_eq!(smoothed_jagged_line(json!(null)).0, smoothed_jagged_line(json!(50)).0, "left out is 50%");
|
||||
}
|
||||
|
||||
/// `path.smoothRegion` refuses a Smoothness outside 0% to 100% and changes nothing.
|
||||
#[test]
|
||||
fn smooth_region_refuses_a_smoothness_outside_0_to_100() {
|
||||
let mut s = session();
|
||||
let id = smooth_test_path(&mut s, &jagged_line_anchors(), false);
|
||||
let drag = json!({"points": [[90, 200], [250, 200], [410, 200]], "radius": 12, "fidelity": 2.5});
|
||||
for x in [json!(-1), json!(-0.001), json!(100.5), json!(101), json!(1e300)] {
|
||||
refuses_with(&mut s, "path.smoothRegion", &with(&drag, json!({"smoothness": x})), "`smoothness` must be from 0 to 100");
|
||||
}
|
||||
for x in [json!(0), json!(100)] {
|
||||
let before = path(&s, id);
|
||||
s.execute("path.smoothRegion", &with(&drag, json!({"smoothness": x}))).unwrap_or_else(|e| panic!("smoothness {x}: {e}"));
|
||||
assert_ne!(path(&s, id), before, "smoothness {x} smoothed nothing");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// A drag of the Smooth tool along a line jagged by up to 6 pt leaves no corner between a curve and
|
||||
/// a line: every anchor between the path's ends turns by at most 1°.
|
||||
#[test]
|
||||
@@ -3484,7 +3542,7 @@ fn smooth_and_join_refuse_a_reach_or_fidelity_outside_its_range() {
|
||||
}
|
||||
|
||||
/// The Eraser, Path Eraser, Smooth, Join and Blob Brush commands refuse a size, angle, roundness,
|
||||
/// width, radius, fidelity or tolerance given as text or as another value that is not a number, and change
|
||||
/// width, radius, fidelity, smoothness or tolerance given as text or as another value that is not a number, and change
|
||||
/// nothing; null is read as the key left out.
|
||||
#[test]
|
||||
fn drawing_commands_refuse_a_size_given_as_text() {
|
||||
@@ -3499,6 +3557,7 @@ fn drawing_commands_refuse_a_size_given_as_text() {
|
||||
("path.eraseSegments", "width"),
|
||||
("path.smoothRegion", "radius"),
|
||||
("path.smoothRegion", "fidelity"),
|
||||
("path.smoothRegion", "smoothness"),
|
||||
("path.joinScrub", "tolerance"),
|
||||
("path.blob", "size"),
|
||||
("path.blob", "fidelity"),
|
||||
|
||||
@@ -473,6 +473,54 @@ fn alt_smoothing_takes_the_smooth_tools_fidelity() {
|
||||
}
|
||||
}
|
||||
|
||||
/// The Smoothness an Alt smoothing drag sends with `path.smoothRegion` on a selected path.
|
||||
fn alt_smoothness(s: &mut Session) -> Option<f64> {
|
||||
use vectorcraft_tools::Action;
|
||||
let mods = Mods { alt: true, ..Default::default() };
|
||||
let mut last = vec![];
|
||||
for (kind, x, y) in [(PointerKind::Down, 50.0, 150.0), (PointerKind::Drag, 150.0, 150.0), (PointerKind::Up, 250.0, 160.0)] {
|
||||
let ev = PointerEvent { mods, ..PointerEvent::new(kind, x, y) };
|
||||
last = s.with_tool_cx(ViewInfo::default(), |t, cx| t.pointer(cx, &ev));
|
||||
}
|
||||
last.iter().find_map(|a| match a {
|
||||
Action::Exec(c, v) if c == "path.smoothRegion" => v["smoothness"].as_f64(),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Smooth Tool Options' Smoothness is kept with the preferences (50% until it is set), and Alt
|
||||
/// smoothing with the Pencil, Paintbrush and Blob Brush uses it. A Smoothness outside 0 to 100 is
|
||||
/// an error that sets nothing.
|
||||
#[test]
|
||||
fn the_smooth_tools_smoothness_is_kept_and_alt_smoothing_takes_it() {
|
||||
let mut s = session();
|
||||
s.execute("shape.rectangle", &json!({"x": 100, "y": 100, "width": 100, "height": 100})).unwrap();
|
||||
select(&mut s, "smooth");
|
||||
assert_eq!(s.tool_options()["smoothness"], json!(50.0));
|
||||
for bad in [json!(-1), json!(100.5), json!("30"), json!(null)] {
|
||||
let e = s.set_tool_option_cmd(&json!({"key": "smoothness", "value": bad})).unwrap_err();
|
||||
assert_eq!(e, "tool `smooth`: `smoothness` must be a number from 0 to 100", "{bad}");
|
||||
}
|
||||
assert_eq!(s.tool_options()["smoothness"], json!(50.0));
|
||||
s.set_tool_option("smoothness", &json!(80));
|
||||
assert_eq!(s.prefs.tool_settings["smooth"]["smoothness"], json!(80.0));
|
||||
for id in ["pencil", "paintbrush", "blobBrush"] {
|
||||
select(&mut s, id);
|
||||
if id == "pencil" {
|
||||
s.set_tool_option("altSmooth", &json!(true));
|
||||
}
|
||||
assert_eq!(alt_smoothness(&mut s), Some(80.0), "{id}");
|
||||
assert!(s.tool_options().get("smoothness").is_none(), "{id} has no Smoothness of its own");
|
||||
}
|
||||
s.set_tool_option_cmd(&json!({"tool": "smooth", "key": "smoothness", "value": 10})).unwrap();
|
||||
assert_eq!(alt_smoothness(&mut s), Some(10.0), "the Blob Brush takes the new Smoothness");
|
||||
let saved = serde_json::to_string(&s.prefs).unwrap();
|
||||
let mut next = session();
|
||||
next.apply_prefs(serde_json::from_str(&saved).unwrap());
|
||||
select(&mut next, "smooth");
|
||||
assert_eq!(next.tool_options()["smoothness"], json!(10.0));
|
||||
}
|
||||
|
||||
/// `tool.setOption` with a value of the wrong type, a number out of range, an unknown value or an
|
||||
/// option the tool doesn't have is an error naming the tool and the option. Nothing is set, also
|
||||
/// when another option given with it is good, and nothing is saved or recorded for undo.
|
||||
|
||||
@@ -524,12 +524,17 @@ impl Session {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Alt switches the Pencil, Paintbrush or Blob Brush to Smooth, using Smooth Tool Options' Fidelity.
|
||||
// Alt switches the Pencil, Paintbrush or Blob Brush to Smooth, using Smooth Tool Options' Fidelity
|
||||
// and Smoothness.
|
||||
if matches!(t.id(), "pencil" | "paintbrush" | "blobBrush")
|
||||
&& wanted("smooth")
|
||||
&& let Some(v) = prefs.tool_settings.get("smooth").and_then(|s| s.get("fidelity"))
|
||||
&& let Some(saved) = prefs.tool_settings.get("smooth")
|
||||
{
|
||||
t.set_option("smoothFidelity", v);
|
||||
for (key, alt) in [("fidelity", "smoothFidelity"), ("smoothness", "smoothSmoothness")] {
|
||||
if let Some(v) = saved.get(key) {
|
||||
t.set_option(alt, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -85,6 +85,9 @@ const BAND_WORK_PER_CALL: usize = 20_000;
|
||||
const PREVIEW_FIDELITY_PX: f64 = 0.5;
|
||||
/// The Smooth tool's Fidelity (screen pixels) until Smooth Tool Options sets another.
|
||||
const SMOOTH_FIDELITY: f64 = 2.5;
|
||||
/// The Smooth tool's Smoothness (percent) until Smooth Tool Options sets another: the strength
|
||||
/// `path.smoothRegion` smooths with when a drag leaves it out. VectorCraft's own value.
|
||||
const SMOOTH_SMOOTHNESS: f64 = 50.0;
|
||||
/// How far the pointer goes from the press point, in screen pixels, before a Shift-drag of the
|
||||
/// Eraser takes the 45° step it lies nearest to.
|
||||
const ERASER_SHIFT_PX: f64 = 3.0;
|
||||
@@ -182,6 +185,11 @@ pub struct GestureTool {
|
||||
/// The Smooth tool's Fidelity (screen pixels), used when Alt switches the Pencil, Paintbrush or
|
||||
/// Blob Brush to Smooth. The engine sets it from Smooth Tool Options (`smoothFidelity`).
|
||||
smooth_fidelity: f64,
|
||||
/// Smooth Tool Options' Smoothness (percent, 0 to 100): how strongly a Smooth drag smooths the
|
||||
/// path before it is refitted. The Smooth tool's own (`smoothness`), and for the Pencil,
|
||||
/// Paintbrush and Blob Brush the one an Alt smoothing drag uses, which the engine sets from
|
||||
/// Smooth Tool Options (`smoothSmoothness`).
|
||||
smoothness: f64,
|
||||
/// Active drag is smoothing with the Pencil, Paintbrush or Blob Brush (Alt held).
|
||||
smoothing: bool,
|
||||
/// The pointer has moved from the press point, by any distance (a move too short to add a
|
||||
@@ -907,6 +915,7 @@ impl GestureTool {
|
||||
live_preview: true,
|
||||
live_fit: true,
|
||||
smooth_fidelity: SMOOTH_FIDELITY,
|
||||
smoothness: SMOOTH_SMOOTHNESS,
|
||||
smoothing: false,
|
||||
moved: false,
|
||||
marquee: false,
|
||||
@@ -1590,7 +1599,7 @@ impl GestureTool {
|
||||
if pts.len() >= 2 && has_selected_smooth_path(cx) {
|
||||
return vec![Action::Exec(
|
||||
"path.smoothRegion".into(),
|
||||
json!({"points": points_json(pts), "radius": radius, "fidelity": smooth_tolerance(cx, self.smooth_fidelity)}),
|
||||
json!({"points": points_json(pts), "radius": radius, "fidelity": smooth_tolerance(cx, self.smooth_fidelity), "smoothness": self.smoothness}),
|
||||
)];
|
||||
}
|
||||
return vec![];
|
||||
@@ -1655,7 +1664,7 @@ impl GestureTool {
|
||||
"smooth" if pts.len() >= 2 && has_selected_smooth_path(cx) => {
|
||||
vec![Action::Exec(
|
||||
"path.smoothRegion".into(),
|
||||
json!({"points": points_json(pts), "radius": radius, "fidelity": smooth_tolerance(cx, self.fidelity)}),
|
||||
json!({"points": points_json(pts), "radius": radius, "fidelity": smooth_tolerance(cx, self.fidelity), "smoothness": self.smoothness}),
|
||||
)]
|
||||
}
|
||||
"pathEraser" if has_sel => vec![Action::Exec("path.eraseSegments".into(), json!({"points": points_json(pts), "width": reach(cx, 8.0)}))],
|
||||
@@ -2089,7 +2098,7 @@ impl Tool for GestureTool {
|
||||
"closeWithin": self.close_within
|
||||
})
|
||||
}
|
||||
"smooth" => json!({"fidelity": self.fidelity}),
|
||||
"smooth" => json!({"fidelity": self.fidelity, "smoothness": self.smoothness}),
|
||||
"blobBrush" => {
|
||||
json!({
|
||||
"fidelity": self.fidelity,
|
||||
@@ -2142,6 +2151,11 @@ impl Tool for GestureTool {
|
||||
"smoothFidelity" if matches!(self.id, "pencil" | "paintbrush" | "blobBrush") => {
|
||||
self.smooth_fidelity = v.as_f64().unwrap_or(self.smooth_fidelity).clamp(super::min_fidelity("smooth"), 100.0)
|
||||
}
|
||||
// A value that is not a number from 0 to 100 leaves the Smoothness as it is.
|
||||
"smoothness" if self.id == "smooth" => self.smoothness = v.as_f64().filter(|n| (0.0..=100.0).contains(n)).unwrap_or(self.smoothness),
|
||||
"smoothSmoothness" if matches!(self.id, "pencil" | "paintbrush" | "blobBrush") => {
|
||||
self.smoothness = v.as_f64().filter(|n| (0.0..=100.0).contains(n)).unwrap_or(self.smoothness)
|
||||
}
|
||||
"keepSelected" => self.keep_selected = v.as_bool().unwrap_or(self.keep_selected),
|
||||
"mergeOnlySelection" => self.merge_only_selection = v.as_bool().unwrap_or(self.merge_only_selection),
|
||||
"editWithin" => self.edit_within = v.as_f64().unwrap_or(self.edit_within).clamp(0.0, 100.0),
|
||||
@@ -2149,17 +2163,19 @@ impl Tool for GestureTool {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
/// The options the tool shows ([`Self::options`]), and the Smooth tool's Fidelity that the
|
||||
/// Pencil, Paintbrush and Blob Brush smooth with while Alt is held (`smoothFidelity`).
|
||||
/// The options the tool shows ([`Self::options`]), and the Smooth tool's Fidelity and Smoothness
|
||||
/// that the Pencil, Paintbrush and Blob Brush smooth with while Alt is held (`smoothFidelity`,
|
||||
/// `smoothSmoothness`).
|
||||
fn check_option(&self, key: &str, v: &Value) -> Result<(), String> {
|
||||
use crate::option_check as c;
|
||||
let smooth_store = key == "smoothFidelity" && matches!(self.id, "pencil" | "paintbrush" | "blobBrush");
|
||||
let smooth_store = matches!(key, "smoothFidelity" | "smoothSmoothness") && matches!(self.id, "pencil" | "paintbrush" | "blobBrush");
|
||||
if !smooth_store && self.options().get(key).is_none() {
|
||||
return Err(c::unknown(key));
|
||||
}
|
||||
match key {
|
||||
"fidelity" => c::number(key, v, super::min_fidelity(self.id), 100.0),
|
||||
"smoothFidelity" => c::number(key, v, super::min_fidelity("smooth"), 100.0),
|
||||
"smoothness" | "smoothSmoothness" => c::number(key, v, 0.0, 100.0),
|
||||
"size" => c::number(key, v, 0.1, 1000.0),
|
||||
"sizeVariation" => c::number(key, v, 0.0, 1000.0),
|
||||
"angle" => c::number(key, v, -180.0, 180.0),
|
||||
|
||||
@@ -690,7 +690,10 @@ fn blob_brush_alt_switches_to_the_smooth_tool() {
|
||||
let acts = drag_held(&mut t, &cx, &stroke, alt);
|
||||
assert!(acts[..acts.len() - 1].iter().all(|a| a.is_empty()), "{acts:?}");
|
||||
let v = smoothed(&acts).unwrap_or_else(|| panic!("{acts:?}"));
|
||||
assert_eq!(v, json!({"points": [[50.0, 150.0], [150.0, 150.0], [250.0, 160.0]], "radius": cx.tol(12.0), "fidelity": cx.tol(SMOOTH_FIDELITY)}));
|
||||
assert_eq!(
|
||||
v,
|
||||
json!({"points": [[50.0, 150.0], [150.0, 150.0], [250.0, 160.0]], "radius": cx.tol(12.0), "fidelity": cx.tol(SMOOTH_FIDELITY), "smoothness": SMOOTH_SMOOTHNESS})
|
||||
);
|
||||
// Mid-drag the cursor stays the Smooth tool's when Alt is let go, and the line is thin with
|
||||
// no length label.
|
||||
t.pointer(&cx, &PointerEvent::new(PointerKind::Down, 50.0, 150.0).with_mods(alt));
|
||||
@@ -1783,6 +1786,50 @@ fn alt_smoothing_uses_the_smooth_tools_fidelity() {
|
||||
}
|
||||
}
|
||||
|
||||
/// The Smooth tool sends Smooth Tool Options' Smoothness (50% until it is set) with each drag, and
|
||||
/// an Alt smoothing drag with the Pencil, Paintbrush or Blob Brush sends the one the engine gives it
|
||||
/// (`smoothSmoothness`). A value that is not a number from 0 to 100 is refused and sets nothing.
|
||||
#[test]
|
||||
fn smooth_drags_send_the_smoothness() {
|
||||
let (d, id) = doc_with_rect();
|
||||
let mut s = Selection::default();
|
||||
s.set([id]);
|
||||
let p = paint();
|
||||
let mut cx = cx(&d, &s, &p);
|
||||
cx.smart_guides = false;
|
||||
let pts = [(50.0, 150.0), (150.0, 150.0), (250.0, 160.0)];
|
||||
let smoothness = |acts: &[Vec<Action>]| match acts.last().and_then(|a| a.first()) {
|
||||
Some(Action::Exec(c, v)) if c == "path.smoothRegion" => v["smoothness"].as_f64(),
|
||||
a => panic!("expected path.smoothRegion, got: {a:?}"),
|
||||
};
|
||||
let mut t = GestureTool::new("smooth");
|
||||
assert_eq!(t.options()["smoothness"], json!(50.0));
|
||||
assert_eq!(smoothness(&drag(&mut t, &cx, &pts, Mods::default())), Some(50.0));
|
||||
for bad in [json!(-1), json!(100.5), json!("30"), json!(true)] {
|
||||
assert_eq!(t.check_option("smoothness", &bad), Err("`smoothness` must be a number from 0 to 100".into()), "{bad}");
|
||||
t.set_option("smoothness", &bad);
|
||||
assert_eq!(t.options()["smoothness"], json!(50.0), "{bad}");
|
||||
}
|
||||
for ok in [0.0, 75.0, 100.0] {
|
||||
assert_eq!(t.check_option("smoothness", &json!(ok)), Ok(()));
|
||||
t.set_option("smoothness", &json!(ok));
|
||||
assert_eq!(smoothness(&drag(&mut t, &cx, &pts, Mods::default())), Some(ok));
|
||||
}
|
||||
let alt = Mods { alt: true, ..Default::default() };
|
||||
for (id, alt_smooth) in [("pencil", true), ("blobBrush", false), ("paintbrush", false)] {
|
||||
let mut t = GestureTool::new(id);
|
||||
t.set_option("altSmooth", &json!(alt_smooth));
|
||||
assert_eq!(smoothness(&drag_held(&mut t, &cx, &pts, alt)), Some(50.0), "{id}");
|
||||
assert_eq!(t.check_option("smoothSmoothness", &json!(101)), Err("`smoothSmoothness` must be a number from 0 to 100".into()), "{id}");
|
||||
t.set_option("smoothSmoothness", &json!(101));
|
||||
t.set_option("smoothness", &json!(20.0));
|
||||
assert_eq!(smoothness(&drag_held(&mut t, &cx, &pts, alt)), Some(50.0), "{id}: only smoothSmoothness sets it");
|
||||
t.set_option("smoothSmoothness", &json!(20.0));
|
||||
assert_eq!(smoothness(&drag_held(&mut t, &cx, &pts, alt)), Some(20.0), "{id}");
|
||||
assert!(t.options().get("smoothness").is_none() && t.options().get("smoothSmoothness").is_none(), "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The Smooth tool's Fidelity is in screen pixels: the Smooth tool and an Alt smoothing drag with
|
||||
/// the Pencil, Paintbrush or Blob Brush send it in points at Fidelity / zoom, so a drag smooths as
|
||||
/// much on screen at any zoom.
|
||||
|
||||
@@ -63,7 +63,7 @@ const ROWS: &[Row] = &[
|
||||
// Pencil Tool Options' Alt key toggles to Smooth Tool, Live preview and Live curve fitting (the
|
||||
// Paintbrush has none of them).
|
||||
(&["pencil"], None, &["altSmooth", "livePreview", "liveFit"]),
|
||||
(&["smooth"], None, &["fidelity"]),
|
||||
(&["smooth"], None, &["fidelity", "smoothness"]),
|
||||
(&["blobBrush", "eraser"], None, &["size", "sizeMode", "sizeVariation"]),
|
||||
(&["blobBrush"], None, &["fidelity", "keepSelected", "mergeOnlySelection"]),
|
||||
// Eraser Tool Options' tip shape.
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//! (Live curve fitting, on: its preview fits the stroke while it is drawn).
|
||||
//!
|
||||
//! Fields: `tool`; `fidelity` (screen pixels for the Pencil, Paintbrush and Smooth, points for the
|
||||
//! Blob Brush), `fill` (Pencil and
|
||||
//! Blob Brush), `smoothness` (percent, 0 to 100; Smooth), `fill` (Pencil and
|
||||
//! Paintbrush), `keepSelected` (Pencil, Paintbrush and Blob Brush), `mergeOnlySelection` (Blob
|
||||
//! Brush), `closeWithin` and `editWithin` (screen pixels, 0 turns it off; Pencil and Paintbrush),
|
||||
//! `angle` (degrees) and `roundness` (percent; Blob Brush and Eraser), `livePreview` (Pencil: the stroke shows
|
||||
@@ -45,8 +45,9 @@ pub const KIND: &str = "freehandOptions";
|
||||
pub const TOOLS: [&str; 5] = ["pencil", "paintbrush", "smooth", "blobBrush", "eraser"];
|
||||
|
||||
/// The options the dialog edits, in the order it shows them (a tool has those it reports).
|
||||
const KEYS: [&str; 18] = [
|
||||
const KEYS: [&str; 19] = [
|
||||
"fidelity",
|
||||
"smoothness",
|
||||
"angle",
|
||||
"angleMode",
|
||||
"angleVariation",
|
||||
@@ -227,6 +228,12 @@ fn params(tool: &str, present: &[&str]) -> Vec<Param> {
|
||||
.full_width(),
|
||||
);
|
||||
}
|
||||
if has("smoothness") {
|
||||
// The Smooth tool's Smoothness: a slider from 0% to 100% with the field at its end, in whole percent.
|
||||
rows.push(
|
||||
Param::new("smoothness", tl!("Smoothness:"), ParamKind::range(0.0, 100.0, 50.0, SliderScale::Linear)).decimals(0).unit(UnitKind::Percent),
|
||||
);
|
||||
}
|
||||
if eraser {
|
||||
rows.push(variation_param("angle", tl!("Angle:"), UnitKind::Degrees, 0, (-180.0, 180.0), (0.0, 180.0), &Variation::ALL));
|
||||
rows.push(variation_param("roundness", tl!("Roundness:"), UnitKind::Percent, 0, (1.0, 100.0), (0.0, 100.0), &Variation::ALL));
|
||||
|
||||
@@ -43,6 +43,32 @@ fn the_fidelity_keys_step_finely_at_the_low_end() {
|
||||
assert_eq!((fidelity_step(0.035, 1.0), fidelity_step(0.035, -1.0), fidelity_step(1.5, 1.0), fidelity_step(1.5, -1.0)), (0.04, 0.03, 2.0, 1.0));
|
||||
}
|
||||
|
||||
/// Smooth Tool Options shows Smoothness under Fidelity, at 50% until it is set, and OK sets it from 0% to 100%; a
|
||||
/// Smoothness past 100% is refused and sets nothing. The other freehand tools have no Smoothness.
|
||||
#[test]
|
||||
fn smooth_tool_options_set_the_smoothness() {
|
||||
let mut app = app();
|
||||
let smooth = app.session.tool_options_of("smooth");
|
||||
assert_eq!(smooth["smoothness"], json!(50.0));
|
||||
let present: Vec<&str> = KEYS.iter().copied().filter(|key| smooth.get(*key).is_some()).collect();
|
||||
let keys: Vec<String> = params("smooth", &present).iter().map(|p| p.key.to_string()).collect();
|
||||
let (fidelity, smoothness) = (keys.iter().position(|k| k == "fidelity"), keys.iter().position(|k| k == "smoothness"));
|
||||
assert!(fidelity.is_some() && fidelity < smoothness, "{keys:?}");
|
||||
for value in [30.0, 0.0, 100.0] {
|
||||
set_through_dialog(&mut app, "smooth", &[("smoothness", json!(value))]);
|
||||
assert_eq!(app.session.tool_options_of("smooth")["smoothness"], json!(value));
|
||||
}
|
||||
let key = app.run("tool.options", json!({"tool": "smooth"})).unwrap()["dialog"].as_str().unwrap().to_owned();
|
||||
let (ctx, id) = (app.ui_ctx.clone().expect("a window"), open_id(&app, &key));
|
||||
let host = app.dialogs().expect("a window");
|
||||
assert!(host.with(|host| host.set_json_field(&ctx, id, "smoothness", json!(120))).is_err(), "120% is refused");
|
||||
host.with(|host| host.cancel_by_id(&ctx, id));
|
||||
assert_eq!(app.session.tool_options_of("smooth")["smoothness"], json!(100.0));
|
||||
for tool in ["pencil", "paintbrush", "blobBrush", "eraser"] {
|
||||
assert!(app.session.tool_options_of(tool).get("smoothness").is_none(), "{tool}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Each tool's dialog, opened as the user opens it, stores the values set in it as the tool's options: the field
|
||||
/// keys, the units (Liquify's percent is a fraction) and the companion keys of the variation rows.
|
||||
#[test]
|
||||
@@ -60,7 +86,7 @@ fn ok_sets_each_tools_options() {
|
||||
json!({"fidelity": 6.5, "fill": true, "altSmooth": true, "closeWithin": 0.0, "editWithin": 20.0, "liveFit": false}),
|
||||
),
|
||||
("paintbrush", json!({"fidelity": 3, "keepSelected": false}), "paintbrush", json!({"fidelity": 3.0, "keepSelected": false})),
|
||||
("smooth", json!({"fidelity": 5}), "smooth", json!({"fidelity": 5.0})),
|
||||
("smooth", json!({"fidelity": 5, "smoothness": 75}), "smooth", json!({"fidelity": 5.0, "smoothness": 75.0})),
|
||||
(
|
||||
"blobBrush",
|
||||
json!({"angle": 45, "roundness": 0, "sizeMode": "pressure", "sizeVariation": 6, "fidelity": 0.01, "keepSelected": true, "mergeOnlySelection": true}),
|
||||
|
||||
+12
-3
@@ -7251,7 +7251,7 @@ in `path.convertAnchor`; `t` in `path.insertAnchor`; `fidelity` and the `within`
|
||||
`brush.freehand`, which runs it); `size`, `sizeVariation`, `angle`, `angleVariation`, `roundness` and
|
||||
`roundnessVariation` in `path.eraseRegion`;
|
||||
`size`, `sizeVariation`, `angle`, `roundness` and `fidelity` in `path.blob`; `width` in `path.eraseSegments`;
|
||||
`radius` and `fidelity` in `path.smoothRegion`; `tolerance` in `path.joinScrub`; `dx` and `dy` in `path.moveAnchors`;
|
||||
`radius`, `fidelity` and `smoothness` in `path.smoothRegion`; `tolerance` in `path.joinScrub`; `dx` and `dy` in `path.moveAnchors`;
|
||||
and `dx`, `dy`, `tol` and `radius` in `path.reshape`. A null is read as a key left out. `path.convertAnchor` refuses
|
||||
an `x` without a `y`, or a `y` without an `x`, with "x and y must be given together". `path.freehand`,
|
||||
`path.curvature`, `path.knife`, `path.eraseRegion`, `path.eraseSegments`, `path.smoothRegion`, `path.joinScrub` and
|
||||
@@ -7401,6 +7401,14 @@ own, and the corner keeps its place and the directions the path arrives and leav
|
||||
`fidelity`. A call that changes no anchor returns `{ids: [], before: 0, after: 0}` and adds no undo step. The 57.5°
|
||||
threshold is VectorCraft's own value.
|
||||
|
||||
`path.smoothRegion`'s `smoothness` (0 to 100, 50 when left out) sets how strongly the path is smoothed before it is
|
||||
refitted within `fidelity`: each point is averaged with the stretch of path around it, over a stretch that grows in
|
||||
proportion to `smoothness`, so a higher value leaves a smoother path. At 0 the path is only refitted within
|
||||
`fidelity`, and 100 averages over twice the stretch that 50 does. A `smoothness` outside 0 to 100 is refused with
|
||||
"`smoothness` must be from 0 to 100" and changes nothing. The default and the way the percentage scales the smoothing
|
||||
are VectorCraft's own. The Smooth tool sends its Smooth Tool Options' Smoothness (`tool.setOption {tool: "smooth",
|
||||
key: "smoothness"}`, 0 to 100, 50 by default) with each drag.
|
||||
|
||||
`path.smoothRegion` keeps a subpath's result only when it is smoother. It measures the subpath and the result along
|
||||
lines between points on each curved segment, spaced about `step` apart along the segment's control polygon, where
|
||||
`step` is an eighth of `radius` or 1/512 of the diagonal of the box around the subpath's anchors and handles,
|
||||
@@ -13910,8 +13918,9 @@ The second call returns the error ``tool `smooth`: `fidelity` must be a number f
|
||||
|
||||
The Pencil's, the Paintbrush's and the Smooth tool's `fidelity` is in screen pixels: the tools send `path.freehand` and
|
||||
`path.smoothRegion` the option divided by the zoom, so a stroke keeps as many points and a Smooth drag smooths as much
|
||||
at any zoom. The Smooth tool's `fidelity` (0.05 to 100 px, 2.5 by default) is also the one an Alt smoothing drag with
|
||||
the Pencil, Paintbrush or Blob Brush uses. The Blob Brush's `fidelity` is in points.
|
||||
at any zoom. The Smooth tool's `fidelity` (0.05 to 100 px, 2.5 by default) and `smoothness` (0 to 100, 50 by default)
|
||||
are also the ones an Alt smoothing drag with the Pencil, Paintbrush or Blob Brush uses. The Blob Brush's `fidelity` is
|
||||
in points.
|
||||
|
||||
`tool.options {tool}` opens what double-clicking the tool's button opens (its params doc lists every tool), in the
|
||||
desktop app. The Flare tool's are its Flare Tool Options (dialog `flareOptions`): `diameter` and `pathLength` (pt),
|
||||
|
||||
@@ -1460,6 +1460,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
Curvature point markers are circular, with no direction handles. While drawing, its own point feedback replaces generic selection markers; existing paths use the selection overlay, including partial selections and Hide Edges. Anchor Size applies, and Direct Selection and Pen retain their handles. The real egui-frame regressions in `crates/ui-egui/src/canvas.rs` verify marker positions, counts and unchanged documents (#1236, in part); the existing-path interpolation request remains open. The curve through the points (`curvature_spline` in `crates/tools/src/draw2.rs`) makes each stretch of three points follow the circle through them: a smooth point's handles lie along that circle's tangent, each `chord / (3 cos²(α/2))` long (α between handle and chord, capped at 90°), and the ends of an open path mirror their neighbour's tangent in their chord, so a J or a U curves evenly, a turn tighter than 30° comes to a point, and uneven spacing doesn't loop; the rubber band, a dragged or inserted point and `path.convertAnchor` to smooth use the same rule, while the Pencil's fitting keeps its own; `tests/draw2.rs` checks that the rubber band is the path the click makes (#1236, in part). The Smooth tool smooths only the stretch of path under the drag (#1547, fixes #1512). A path the Smooth tool changes stays selected whole, and the anchor points and segments selected on it with Direct Selection are deselected. Selected paths the drag does not change keep their selected anchors, and undo restores the earlier selection (`tests/draw2.rs`, `smoothing_a_path_drops_its_direct_selected_anchors`). While the Smooth tool drags with a path selected that it can smooth, a dashed outline shows the band it smooths: everything within its radius of the drag (12 screen pixels, VectorCraft's own value), round at both ends; Alt smoothing with the Pencil, Paintbrush and Blob Brush shows the same band.
|
||||
While Alt is held during a Paintbrush stroke or before a press with no path selected that the Smooth tool can act on, the Paintbrush shows a closing cursor (a crosshair with a ring), and so does the Pencil with Alt key toggles to Smooth Tool off when Alt is pressed after the drag starts; releasing the mouse button with Alt still held closes the path. Before a Paintbrush press with such a path selected, Alt shows the Smooth cursor, and holding Alt at the press smooths that path with the Smooth tool's radius and Smooth Tool Options' Fidelity. A Pencil drag started with Shift or Alt held draws a straight line instead, which stays open.
|
||||
Smooth Tool Options' Fidelity is in pixels, as [the help page](https://help.adobe.com/archive/en_US/illustrator/cs3/illustrator_cs3_help.pdf#page=79) gives it: the Smooth tool, and an Alt smoothing drag with the Pencil, Paintbrush or Blob Brush, refits the path at Fidelity divided by the zoom, so a drag smooths a path as much on screen at 25 % as at 800 %. Reading the pixels as screen pixels is VectorCraft's own rule, as for the Pencil and the Paintbrush (#5729); the range (0.05 to 100 px) and the default (2.5 px) are VectorCraft's own values, and a saved Fidelity keeps its number (`crates/tools/src/draw2/gesture/tests.rs` test `smooth_fidelity_is_in_screen_pixels_at_any_zoom`, `crates/engine/src/tests/draw2.rs` test `smooth_region_refits_at_the_tolerance_sent_at_any_zoom`).
|
||||
Smooth Tool Options has Smoothness under Fidelity, from 0% to 100%, where a higher value gives a smoother path, as [the help page](https://web.archive.org/web/20210601162814/https://helpx.adobe.com/pdf/illustrator_reference.pdf#page=164) describes the setting: the Smooth tool, and an Alt smoothing drag with the Pencil, Paintbrush or Blob Brush, sends it as `path.smoothRegion`'s `smoothness`, and a Smoothness outside 0 to 100 is an error in `tool.setOption` and `path.smoothRegion`. The default (50%) and the way the percentage scales the smoothing (none before the refit at 0%, twice the default's stretch at 100%) are VectorCraft's own (`crates/engine/src/tests/draw2.rs` test `a_higher_smoothness_leaves_a_smoother_path`, `crates/engine/src/tests/toolsettings.rs` test `the_smooth_tools_smoothness_is_kept_and_alt_smoothing_takes_it`).
|
||||
With Shift held at the press, the Pencil draws a straight line from the press point to the pointer at 45° steps from the Constrain Angle, as [Draw straight lines with the Pencil tool](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/draw-shapes/draw-straight-lines-with-the-pencil-tool.html) describes; pressed on an end of a selected open path, the line continues the path from that end (part of #3799). With Option/Alt held at the press and Alt key toggles to Smooth Tool off, the Pencil draws a straight line at any angle, and it stays open when Alt is still held at the release; Shift and Alt together keep the 45° steps (#3799). While Shift or Alt is held before the press, the Pencil shows a straight-line cursor (a crosshair with a short level line), as the page describes. Over an end of a selected open path, where the stroke continues the path, freehand or as a straight line, it shows a path-continuation cursor (a crosshair with a short line rising to the right), as [Extend paths with the Pencil tool](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/draw-shapes/extend-paths-with-the-pencil-tool.html) describes, and again while a continuing stroke ends near an open end of another selected path, where the release connects the two ([Connect two paths with the Pencil tool](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/draw-shapes/connect-two-paths-with-the-pencil-tool.html)). A continuing stroke, a straight line too, that ends within Close Paths When Ends Are Within of the path's other end shows the closing cursor, and the release closes the path; Alt held at the release closes a continuing freehand stroke too (a straight line stays open, as above).
|
||||
- **Shapes:** all shape tools (including Flare) and the line, arc, spiral and grid tools. Space held while dragging out a shape, line, arc, spiral or grid moves it at its current size. While a grid is dragged out, Up/Down change its rows (concentric dividers) and Left/Right its columns (radial dividers). A click with a grid tool asks for the size, the divider counts and the grid options (#1377, in part): a skew for each set of dividers, Use Outside Rectangle As Frame or Create Compound Path From Ellipses, and Fill Grid; they are parameters of `shape.rectangularGrid` and `shape.polarGrid`, stay as the tool's options and apply to drags too. A shape just drawn stays adjustable with its tool: its Live Shape widgets drag, a press on its centre point (Show Center) moves the selection with the Selection tool's move arrow, snapping and Alt copy, and a press in the rotate zone just outside a bounding-box corner turns it about the centre with the Selection tool's rotate cursor (#5477; tests `a_shape_tool_moves_the_selection_by_its_centre_point_and_turns_it_from_a_corner` and, through the engine, `shapetools.rs`). A drag on one of its eight bounding-box handles scales it as the Selection tool's handles do, with their cursors and snapping, instead of drawing a new shape from the handle. A shape tool takes a press as a handle's only on the drawn handle square (VectorCraft's own rule: 3 screen points from its centre along each axis at the default Anchor Size, a half point more or less per step; the Selection tool takes 5), so a press just beside a handle still draws (tests `a_shape_tool_scales_the_selection_by_its_bounding_box_handles`, `a_shape_tool_draws_from_a_press_just_beside_a_handle` and `a_shape_tool_scales_the_shape_just_drawn_by_its_bounding_box_handle`). With such a tool, a double-click on a corner widget of a selected live rectangle or polygon shows and selects the Transform panel, and the tool, the selection and the document stay as they were (test `a_double_click_on_a_corner_widget_shows_the_transform_panel`). The curve a skew percentage follows and the direction of the radial skew are our own (the public documentation gives neither), and the dialogs have no reference point locator. An ellipse's Pie Start and End Angle and Invert Pie (Transform and Properties panels' Ellipse Properties, `object.setLiveShape {pieStart, pieEnd, invertPie}`) cut it as a pie (#812). With the bounding box showing, the Selection tool shows a selected ellipse's two pie widgets just outside it; dragging one sets its angle in one undo step and double-clicking one makes the ellipse whole again. A live polygon's Polygon Properties there: side count, angle, corner type and radius, radius, side length and Make Sides Equal once an uneven scale has made its sides differ (`object.setLiveShape {sides, polygonAngle, polygonRadius, sideLength, makeSidesEqual}`, part of #812); a star is a plain path, as in Illustrator. The Flare Tool Options last as the tool's options: the next flare dragged out uses them, a click's dialog shows them and double-clicking the tool opens them (part of #812).
|
||||
- **Shapes:** all shape tools (including Flare) and the line, arc, spiral and grid tools. Space held while dragging out a shape, line, arc, spiral or grid moves it at its current size. The Flare tool's center drag turns the rays by 0.1 radian at each drag update in which the pointer's document y grows, and back by as much at each one in which it falls, Shift held keeps their turn, and Space held during either of its drags moves the flare. While a grid is dragged out, Up/Down change its rows (concentric dividers) and Left/Right its columns (radial dividers). A click with a grid tool asks for the size, the divider counts and the grid options (#1377, in part): a skew for each set of dividers, Use Outside Rectangle As Frame or Create Compound Path From Ellipses, and Fill Grid; they are parameters of `shape.rectangularGrid` and `shape.polarGrid`, stay as the tool's options and apply to drags too. The curve a skew percentage follows and the direction of the radial skew are our own (the public documentation gives neither), and the dialogs have no reference point locator. An ellipse's Pie Start and End Angle and Invert Pie (Transform and Properties panels' Ellipse Properties, `object.setLiveShape {pieStart, pieEnd, invertPie}`) cut it as a pie (#812). With the bounding box showing, the Selection tool shows a selected ellipse's two pie widgets just outside it; dragging one sets its angle in one undo step and double-clicking one makes the ellipse whole again. A live polygon's Polygon Properties there: side count, angle, corner type and radius, radius, side length and Make Sides Equal once an uneven scale has made its sides differ (`object.setLiveShape {sides, polygonAngle, polygonRadius, sideLength, makeSidesEqual}`, part of #812); a star is a plain path, as in Illustrator. The Flare Tool Options last as the tool's options: the next flare dragged out uses them, a click's dialog shows them and double-clicking the tool opens them (part of #812).
|
||||
|
||||
Reference in New Issue
Block a user