Make the stroke and path nodes' distances and angles typed lengths and angles

This commit is contained in:
Keavon Chambers committed 2026-11-26 02:35:57 -08:00
1 parent 63a8befe4b
commit fe505edeff
21 files changed
+976 -118

No files matched your search

@@ -0,0 +1,66 @@
use migration_core::{Inputs, Node, literal, literal_of, migration};
use serde::{Deserialize, Serialize};
const DISTANCE: usize = 1;
/// Frozen mirror of the `TaggedValue` variants this migration reads and writes.
#[derive(Serialize, Deserialize)]
enum Tagged {
None,
Number(f64),
Length(TypedLength),
}
#[derive(Serialize, Deserialize)]
struct TypedLength {
value: f64,
unit: LengthUnit,
}
#[derive(Serialize, Deserialize)]
enum LengthUnit {
Pixel,
}
/// The distance is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels.
#[migration("core_types::vector::BevelNode"@1 => @2)]
pub fn bevel_v1(node: &Node, inputs: &mut Inputs) {
let Some(slot) = node.inputs().get(DISTANCE) else { return };
let Some(Tagged::Number(pixels)) = literal_of::<Tagged>(&slot.input) else { return };
inputs.set(
DISTANCE as u32,
literal(Tagged::Length(TypedLength {
value: pixels,
unit: LengthUnit::Pixel,
})),
);
}
#[cfg(test)]
mod tests {
use super::*;
use migration_core::decl;
use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo};
fn contract() -> SourceContract {
SourceContract::new(2, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) })
}
#[test]
fn rewrites_history() {
check_commutation(&BevelV1, &contract());
check_undo(&BevelV1, &contract());
}
#[test]
fn a_number_of_pixels_becomes_a_length_in_pixels() {
let mut fixture = Fixture::new(&BevelV1);
let number = |number: f64| literal(Tagged::Number(number));
let bevel = fixture.add(decl!("core_types::vector::BevelNode"@1), vec![literal(Tagged::None), number(10.)]);
let migrated = fixture.migrate(&BevelV1).unwrap();
let distance = literal(Tagged::Length(TypedLength { value: 10., unit: LengthUnit::Pixel }));
assert_eq!(fixture.inputs(&migrated, bevel), [literal(Tagged::None), distance]);
}
}
@@ -0,0 +1,66 @@
use migration_core::{Inputs, Node, literal, literal_of, migration};
use serde::{Deserialize, Serialize};
const TOLERANCE: usize = 1;
/// Frozen mirror of the `TaggedValue` variants this migration reads and writes.
#[derive(Serialize, Deserialize)]
enum Tagged {
None,
Number(f64),
Length(TypedLength),
}
#[derive(Serialize, Deserialize)]
struct TypedLength {
value: f64,
unit: LengthUnit,
}
#[derive(Serialize, Deserialize)]
enum LengthUnit {
Pixel,
}
/// The tolerance is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels.
#[migration("core_types::vector::DecimateNode"@1 => @2)]
pub fn decimate_v1(node: &Node, inputs: &mut Inputs) {
let Some(slot) = node.inputs().get(TOLERANCE) else { return };
let Some(Tagged::Number(pixels)) = literal_of::<Tagged>(&slot.input) else { return };
inputs.set(
TOLERANCE as u32,
literal(Tagged::Length(TypedLength {
value: pixels,
unit: LengthUnit::Pixel,
})),
);
}
#[cfg(test)]
mod tests {
use super::*;
use migration_core::decl;
use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo};
fn contract() -> SourceContract {
SourceContract::new(2, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) })
}
#[test]
fn rewrites_history() {
check_commutation(&DecimateV1, &contract());
check_undo(&DecimateV1, &contract());
}
#[test]
fn a_number_of_pixels_becomes_a_length_in_pixels() {
let mut fixture = Fixture::new(&DecimateV1);
let number = |number: f64| literal(Tagged::Number(number));
let decimate = fixture.add(decl!("core_types::vector::DecimateNode"@1), vec![literal(Tagged::None), number(5.)]);
let migrated = fixture.migrate(&DecimateV1).unwrap();
let tolerance = literal(Tagged::Length(TypedLength { value: 5., unit: LengthUnit::Pixel }));
assert_eq!(fixture.inputs(&migrated, decimate), [literal(Tagged::None), tolerance]);
}
}
@@ -0,0 +1,66 @@
use migration_core::{Inputs, Node, literal, literal_of, migration};
use serde::{Deserialize, Serialize};
const MAX_DISTANCE: usize = 1;
/// Frozen mirror of the `TaggedValue` variants this migration reads and writes.
#[derive(Serialize, Deserialize)]
enum Tagged {
None,
Number(f64),
Length(TypedLength),
}
#[derive(Serialize, Deserialize)]
struct TypedLength {
value: f64,
unit: LengthUnit,
}
#[derive(Serialize, Deserialize)]
enum LengthUnit {
Pixel,
}
/// The max distance is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels.
#[migration("core_types::vector::JitterPointsNode"@1 => @2)]
pub fn jitter_points_v1(node: &Node, inputs: &mut Inputs) {
let Some(slot) = node.inputs().get(MAX_DISTANCE) else { return };
let Some(Tagged::Number(pixels)) = literal_of::<Tagged>(&slot.input) else { return };
inputs.set(
MAX_DISTANCE as u32,
literal(Tagged::Length(TypedLength {
value: pixels,
unit: LengthUnit::Pixel,
})),
);
}
#[cfg(test)]
mod tests {
use super::*;
use migration_core::decl;
use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo};
fn contract() -> SourceContract {
SourceContract::new(4, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) })
}
#[test]
fn rewrites_history() {
check_commutation(&JitterPointsV1, &contract());
check_undo(&JitterPointsV1, &contract());
}
#[test]
fn a_number_of_pixels_becomes_a_length_in_pixels() {
let mut fixture = Fixture::new(&JitterPointsV1);
let number = |number: f64| literal(Tagged::Number(number));
let jitter_points = fixture.add(decl!("core_types::vector::JitterPointsNode"@1), vec![literal(Tagged::None), number(5.), number(0.), number(1.)]);
let migrated = fixture.migrate(&JitterPointsV1).unwrap();
let max_distance = literal(Tagged::Length(TypedLength { value: 5., unit: LengthUnit::Pixel }));
assert_eq!(fixture.inputs(&migrated, jitter_points), [literal(Tagged::None), max_distance, number(0.), number(1.)]);
}
}
@@ -0,0 +1,12 @@
//! Migrations of the nodes in `core_types`.
mod bevel_v1;
mod decimate_v1;
mod jitter_points_v1;
mod offset_path_v1;
mod offset_points_v1;
mod round_corners_v1;
mod sample_polyline_v1;
mod scatter_points_v2;
mod simplify_v1;
mod weld_v1;
@@ -0,0 +1,66 @@
use migration_core::{Inputs, Node, literal, literal_of, migration};
use serde::{Deserialize, Serialize};
const DISTANCE: usize = 1;
/// Frozen mirror of the `TaggedValue` variants this migration reads and writes.
#[derive(Serialize, Deserialize)]
enum Tagged {
None,
Number(f64),
Length(TypedLength),
}
#[derive(Serialize, Deserialize)]
struct TypedLength {
value: f64,
unit: LengthUnit,
}
#[derive(Serialize, Deserialize)]
enum LengthUnit {
Pixel,
}
/// The distance is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels.
#[migration("core_types::vector::OffsetPathNode"@1 => @2)]
pub fn offset_path_v1(node: &Node, inputs: &mut Inputs) {
let Some(slot) = node.inputs().get(DISTANCE) else { return };
let Some(Tagged::Number(pixels)) = literal_of::<Tagged>(&slot.input) else { return };
inputs.set(
DISTANCE as u32,
literal(Tagged::Length(TypedLength {
value: pixels,
unit: LengthUnit::Pixel,
})),
);
}
#[cfg(test)]
mod tests {
use super::*;
use migration_core::decl;
use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo};
fn contract() -> SourceContract {
SourceContract::new(4, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) })
}
#[test]
fn rewrites_history() {
check_commutation(&OffsetPathV1, &contract());
check_undo(&OffsetPathV1, &contract());
}
#[test]
fn a_number_of_pixels_becomes_a_length_in_pixels() {
let mut fixture = Fixture::new(&OffsetPathV1);
let number = |number: f64| literal(Tagged::Number(number));
let offset_path = fixture.add(decl!("core_types::vector::OffsetPathNode"@1), vec![literal(Tagged::None), number(10.), number(0.), number(4.)]);
let migrated = fixture.migrate(&OffsetPathV1).unwrap();
let distance = literal(Tagged::Length(TypedLength { value: 10., unit: LengthUnit::Pixel }));
assert_eq!(fixture.inputs(&migrated, offset_path), [literal(Tagged::None), distance, number(0.), number(4.)]);
}
}
@@ -0,0 +1,66 @@
use migration_core::{Inputs, Node, literal, literal_of, migration};
use serde::{Deserialize, Serialize};
const DISTANCE: usize = 1;
/// Frozen mirror of the `TaggedValue` variants this migration reads and writes.
#[derive(Serialize, Deserialize)]
enum Tagged {
None,
Number(f64),
Length(TypedLength),
}
#[derive(Serialize, Deserialize)]
struct TypedLength {
value: f64,
unit: LengthUnit,
}
#[derive(Serialize, Deserialize)]
enum LengthUnit {
Pixel,
}
/// The distance is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels.
#[migration("core_types::vector::OffsetPointsNode"@1 => @2)]
pub fn offset_points_v1(node: &Node, inputs: &mut Inputs) {
let Some(slot) = node.inputs().get(DISTANCE) else { return };
let Some(Tagged::Number(pixels)) = literal_of::<Tagged>(&slot.input) else { return };
inputs.set(
DISTANCE as u32,
literal(Tagged::Length(TypedLength {
value: pixels,
unit: LengthUnit::Pixel,
})),
);
}
#[cfg(test)]
mod tests {
use super::*;
use migration_core::decl;
use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo};
fn contract() -> SourceContract {
SourceContract::new(2, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) })
}
#[test]
fn rewrites_history() {
check_commutation(&OffsetPointsV1, &contract());
check_undo(&OffsetPointsV1, &contract());
}
#[test]
fn a_number_of_pixels_becomes_a_length_in_pixels() {
let mut fixture = Fixture::new(&OffsetPointsV1);
let number = |number: f64| literal(Tagged::Number(number));
let offset_points = fixture.add(decl!("core_types::vector::OffsetPointsNode"@1), vec![literal(Tagged::None), number(10.)]);
let migrated = fixture.migrate(&OffsetPointsV1).unwrap();
let distance = literal(Tagged::Length(TypedLength { value: 10., unit: LengthUnit::Pixel }));
assert_eq!(fixture.inputs(&migrated, offset_points), [literal(Tagged::None), distance]);
}
}
@@ -0,0 +1,96 @@
use migration_core::{Inputs, Node, NodeInput, literal, literal_of, migration};
use serde::{Deserialize, Serialize};
const RADIUS: usize = 1;
const MIN_ANGLE_THRESHOLD: usize = 4;
/// Frozen mirror of the `TaggedValue` variants this migration reads and writes.
#[derive(Serialize, Deserialize)]
enum Tagged {
None,
Number(f64),
Length(TypedLength),
Angle(TypedAngle),
}
#[derive(Serialize, Deserialize)]
struct TypedLength {
value: f64,
unit: LengthUnit,
}
#[derive(Serialize, Deserialize)]
enum LengthUnit {
Pixel,
}
#[derive(Serialize, Deserialize)]
struct TypedAngle {
value: f64,
unit: AngleUnit,
}
#[derive(Serialize, Deserialize)]
enum AngleUnit {
Degree,
}
/// The radius is a length and the minimum angle threshold is an angle, which keep the numbers of pixels and degrees they held. Wired numbers still feed them, read as pixels and degrees.
#[migration("core_types::vector::RoundCornersNode"@1 => @2)]
pub fn round_corners_v1(node: &Node, inputs: &mut Inputs) {
let number_at = |slot: usize| match node.inputs().get(slot).and_then(|slot| literal_of::<Tagged>(&slot.input)) {
Some(Tagged::Number(number)) => Some(number),
_ => None,
};
if let Some(pixels) = number_at(RADIUS) {
inputs.set(RADIUS as u32, length(pixels));
}
if let Some(degrees) = number_at(MIN_ANGLE_THRESHOLD) {
inputs.set(MIN_ANGLE_THRESHOLD as u32, angle(degrees));
}
}
fn length(pixels: f64) -> NodeInput {
literal(Tagged::Length(TypedLength {
value: pixels,
unit: LengthUnit::Pixel,
}))
}
fn angle(degrees: f64) -> NodeInput {
literal(Tagged::Angle(TypedAngle {
value: degrees,
unit: AngleUnit::Degree,
}))
}
#[cfg(test)]
mod tests {
use super::*;
use migration_core::decl;
use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo};
fn contract() -> SourceContract {
SourceContract::new(5, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) })
}
#[test]
fn rewrites_history() {
check_commutation(&RoundCornersV1, &contract());
check_undo(&RoundCornersV1, &contract());
}
#[test]
fn numbers_of_pixels_and_degrees_become_a_length_and_an_angle() {
let mut fixture = Fixture::new(&RoundCornersV1);
let number = |number: f64| literal(Tagged::Number(number));
let round_corners = fixture.add(
decl!("core_types::vector::RoundCornersNode"@1),
vec![literal(Tagged::None), number(10.), number(0.5), number(100.), number(5.)],
);
let migrated = fixture.migrate(&RoundCornersV1).unwrap();
assert_eq!(fixture.inputs(&migrated, round_corners), [literal(Tagged::None), length(10.), number(0.5), number(100.), angle(5.)]);
}
}
@@ -0,0 +1,80 @@
use migration_core::{Inputs, Node, NodeInput, literal, literal_of, migration};
use serde::{Deserialize, Serialize};
const SEPARATION: usize = 2;
const START_OFFSET: usize = 4;
const STOP_OFFSET: usize = 5;
/// Frozen mirror of the `TaggedValue` variants this migration reads and writes.
#[derive(Serialize, Deserialize)]
enum Tagged {
None,
Number(f64),
Length(TypedLength),
}
#[derive(Serialize, Deserialize)]
struct TypedLength {
value: f64,
unit: LengthUnit,
}
#[derive(Serialize, Deserialize)]
enum LengthUnit {
Pixel,
}
/// The separation and start and stop offsets are lengths, which keep the numbers of pixels they held. Wired numbers still feed them, read as pixels.
#[migration("core_types::vector::SamplePolylineNode"@1 => @2)]
pub fn sample_polyline_v1(node: &Node, inputs: &mut Inputs) {
let number_at = |slot: usize| match node.inputs().get(slot).and_then(|slot| literal_of::<Tagged>(&slot.input)) {
Some(Tagged::Number(number)) => Some(number),
_ => None,
};
for slot in [SEPARATION, START_OFFSET, STOP_OFFSET] {
if let Some(pixels) = number_at(slot) {
inputs.set(slot as u32, length(pixels));
}
}
}
fn length(pixels: f64) -> NodeInput {
literal(Tagged::Length(TypedLength {
value: pixels,
unit: LengthUnit::Pixel,
}))
}
#[cfg(test)]
mod tests {
use super::*;
use migration_core::decl;
use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo};
fn contract() -> SourceContract {
SourceContract::new(7, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) })
}
#[test]
fn rewrites_history() {
check_commutation(&SamplePolylineV1, &contract());
check_undo(&SamplePolylineV1, &contract());
}
#[test]
fn numbers_of_pixels_become_lengths_in_pixels() {
let mut fixture = Fixture::new(&SamplePolylineV1);
let number = |number: f64| literal(Tagged::Number(number));
let sample_polyline = fixture.add(
decl!("core_types::vector::SamplePolylineNode"@1),
vec![literal(Tagged::None), number(0.), number(100.), number(100.), number(10.), number(20.), number(0.)],
);
let migrated = fixture.migrate(&SamplePolylineV1).unwrap();
assert_eq!(
fixture.inputs(&migrated, sample_polyline),
[literal(Tagged::None), number(0.), length(100.), number(100.), length(10.), length(20.), number(0.)]
);
}
}
@@ -0,0 +1,66 @@
use migration_core::{Inputs, Node, literal, literal_of, migration};
use serde::{Deserialize, Serialize};
const SEPARATION: usize = 1;
/// Frozen mirror of the `TaggedValue` variants this migration reads and writes.
#[derive(Serialize, Deserialize)]
enum Tagged {
None,
Number(f64),
Length(TypedLength),
}
#[derive(Serialize, Deserialize)]
struct TypedLength {
value: f64,
unit: LengthUnit,
}
#[derive(Serialize, Deserialize)]
enum LengthUnit {
Pixel,
}
/// The separation is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels.
#[migration("core_types::vector::ScatterPointsNode"@2 => @3)]
pub fn scatter_points_v2(node: &Node, inputs: &mut Inputs) {
let Some(slot) = node.inputs().get(SEPARATION) else { return };
let Some(Tagged::Number(pixels)) = literal_of::<Tagged>(&slot.input) else { return };
inputs.set(
SEPARATION as u32,
literal(Tagged::Length(TypedLength {
value: pixels,
unit: LengthUnit::Pixel,
})),
);
}
#[cfg(test)]
mod tests {
use super::*;
use migration_core::decl;
use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo};
fn contract() -> SourceContract {
SourceContract::new(3, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) })
}
#[test]
fn rewrites_history() {
check_commutation(&ScatterPointsV2, &contract());
check_undo(&ScatterPointsV2, &contract());
}
#[test]
fn a_number_of_pixels_becomes_a_length_in_pixels() {
let mut fixture = Fixture::new(&ScatterPointsV2);
let number = |number: f64| literal(Tagged::Number(number));
let scatter_points = fixture.add(decl!("core_types::vector::ScatterPointsNode"@2), vec![literal(Tagged::None), number(10.), number(0.)]);
let migrated = fixture.migrate(&ScatterPointsV2).unwrap();
let separation = literal(Tagged::Length(TypedLength { value: 10., unit: LengthUnit::Pixel }));
assert_eq!(fixture.inputs(&migrated, scatter_points), [literal(Tagged::None), separation, number(0.)]);
}
}
@@ -0,0 +1,66 @@
use migration_core::{Inputs, Node, literal, literal_of, migration};
use serde::{Deserialize, Serialize};
const TOLERANCE: usize = 1;
/// Frozen mirror of the `TaggedValue` variants this migration reads and writes.
#[derive(Serialize, Deserialize)]
enum Tagged {
None,
Number(f64),
Length(TypedLength),
}
#[derive(Serialize, Deserialize)]
struct TypedLength {
value: f64,
unit: LengthUnit,
}
#[derive(Serialize, Deserialize)]
enum LengthUnit {
Pixel,
}
/// The tolerance is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels.
#[migration("core_types::vector::SimplifyNode"@1 => @2)]
pub fn simplify_v1(node: &Node, inputs: &mut Inputs) {
let Some(slot) = node.inputs().get(TOLERANCE) else { return };
let Some(Tagged::Number(pixels)) = literal_of::<Tagged>(&slot.input) else { return };
inputs.set(
TOLERANCE as u32,
literal(Tagged::Length(TypedLength {
value: pixels,
unit: LengthUnit::Pixel,
})),
);
}
#[cfg(test)]
mod tests {
use super::*;
use migration_core::decl;
use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo};
fn contract() -> SourceContract {
SourceContract::new(2, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) })
}
#[test]
fn rewrites_history() {
check_commutation(&SimplifyV1, &contract());
check_undo(&SimplifyV1, &contract());
}
#[test]
fn a_number_of_pixels_becomes_a_length_in_pixels() {
let mut fixture = Fixture::new(&SimplifyV1);
let number = |number: f64| literal(Tagged::Number(number));
let simplify = fixture.add(decl!("core_types::vector::SimplifyNode"@1), vec![literal(Tagged::None), number(5.)]);
let migrated = fixture.migrate(&SimplifyV1).unwrap();
let tolerance = literal(Tagged::Length(TypedLength { value: 5., unit: LengthUnit::Pixel }));
assert_eq!(fixture.inputs(&migrated, simplify), [literal(Tagged::None), tolerance]);
}
}
@@ -0,0 +1,66 @@
use migration_core::{Inputs, Node, literal, literal_of, migration};
use serde::{Deserialize, Serialize};
const DISTANCE: usize = 1;
/// Frozen mirror of the `TaggedValue` variants this migration reads and writes.
#[derive(Serialize, Deserialize)]
enum Tagged {
None,
Number(f64),
Length(TypedLength),
}
#[derive(Serialize, Deserialize)]
struct TypedLength {
value: f64,
unit: LengthUnit,
}
#[derive(Serialize, Deserialize)]
enum LengthUnit {
Pixel,
}
/// The distance is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels.
#[migration("core_types::vector::WeldNode"@1 => @2)]
pub fn weld_v1(node: &Node, inputs: &mut Inputs) {
let Some(slot) = node.inputs().get(DISTANCE) else { return };
let Some(Tagged::Number(pixels)) = literal_of::<Tagged>(&slot.input) else { return };
inputs.set(
DISTANCE as u32,
literal(Tagged::Length(TypedLength {
value: pixels,
unit: LengthUnit::Pixel,
})),
);
}
#[cfg(test)]
mod tests {
use super::*;
use migration_core::decl;
use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo};
fn contract() -> SourceContract {
SourceContract::new(3, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) })
}
#[test]
fn rewrites_history() {
check_commutation(&WeldV1, &contract());
check_undo(&WeldV1, &contract());
}
#[test]
fn a_number_of_pixels_becomes_a_length_in_pixels() {
let mut fixture = Fixture::new(&WeldV1);
let number = |number: f64| literal(Tagged::Number(number));
let weld = fixture.add(decl!("core_types::vector::WeldNode"@1), vec![literal(Tagged::None), number(0.1), number(0.)]);
let migrated = fixture.migrate(&WeldV1).unwrap();
let distance = literal(Tagged::Length(TypedLength { value: 0.1, unit: LengthUnit::Pixel }));
assert_eq!(fixture.inputs(&migrated, weld), [literal(Tagged::None), distance, number(0.)]);
}
}
@@ -1,5 +1,7 @@
//! Migrations of the nodes in `graphene_core`.
mod pack_strips_v1;
mod passthrough_from_upload_texture_node;
mod renames;
mod stroke_v1;
mod vary_colors_from_assign_colors;
@@ -0,0 +1,73 @@
use migration_core::{Inputs, Node, NodeInput, literal, literal_of, migration};
use serde::{Deserialize, Serialize};
const SEPARATION: usize = 1;
const STRIP_MAX_LENGTH: usize = 2;
/// Frozen mirror of the `TaggedValue` variants this migration reads and writes.
#[derive(Serialize, Deserialize)]
enum Tagged {
None,
Number(f64),
Length(TypedLength),
}
#[derive(Serialize, Deserialize)]
struct TypedLength {
value: f64,
unit: LengthUnit,
}
#[derive(Serialize, Deserialize)]
enum LengthUnit {
Pixel,
}
/// The separation and strip max length are lengths, which keep the numbers of pixels they held. Wired numbers still feed them, read as pixels.
#[migration("graphene_core::vector::PackStripsNode"@1 => @2)]
pub fn pack_strips_v1(node: &Node, inputs: &mut Inputs) {
let number_at = |slot: usize| match node.inputs().get(slot).and_then(|slot| literal_of::<Tagged>(&slot.input)) {
Some(Tagged::Number(number)) => Some(number),
_ => None,
};
for slot in [SEPARATION, STRIP_MAX_LENGTH] {
if let Some(pixels) = number_at(slot) {
inputs.set(slot as u32, length(pixels));
}
}
}
fn length(pixels: f64) -> NodeInput {
literal(Tagged::Length(TypedLength {
value: pixels,
unit: LengthUnit::Pixel,
}))
}
#[cfg(test)]
mod tests {
use super::*;
use migration_core::decl;
use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo};
fn contract() -> SourceContract {
SourceContract::new(4, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) })
}
#[test]
fn rewrites_history() {
check_commutation(&PackStripsV1, &contract());
check_undo(&PackStripsV1, &contract());
}
#[test]
fn numbers_of_pixels_become_lengths_in_pixels() {
let mut fixture = Fixture::new(&PackStripsV1);
let number = |number: f64| literal(Tagged::Number(number));
let pack_strips = fixture.add(decl!("graphene_core::vector::PackStripsNode"@1), vec![literal(Tagged::None), number(10.), number(1000.), number(0.)]);
let migrated = fixture.migrate(&PackStripsV1).unwrap();
assert_eq!(fixture.inputs(&migrated, pack_strips), [literal(Tagged::None), length(10.), length(1000.), number(0.)]);
}
}
@@ -0,0 +1,79 @@
use migration_core::{Inputs, Node, NodeInput, literal, literal_of, migration};
use serde::{Deserialize, Serialize};
const WEIGHT: usize = 2;
const DASH_OFFSET: usize = 8;
/// Frozen mirror of the `TaggedValue` variants this migration reads and writes.
#[derive(Serialize, Deserialize)]
enum Tagged {
None,
Number(f64),
Length(TypedLength),
}
#[derive(Serialize, Deserialize)]
struct TypedLength {
value: f64,
unit: LengthUnit,
}
#[derive(Serialize, Deserialize)]
enum LengthUnit {
Pixel,
}
/// The weight and dash offset are lengths, which keep the numbers of pixels they held. Wired numbers still feed them, read as pixels.
#[migration("graphene_core::vector::StrokeNode"@1 => @2)]
pub fn stroke_v1(node: &Node, inputs: &mut Inputs) {
let number_at = |slot: usize| match node.inputs().get(slot).and_then(|slot| literal_of::<Tagged>(&slot.input)) {
Some(Tagged::Number(number)) => Some(number),
_ => None,
};
for slot in [WEIGHT, DASH_OFFSET] {
if let Some(pixels) = number_at(slot) {
inputs.set(slot as u32, length(pixels));
}
}
}
fn length(pixels: f64) -> NodeInput {
literal(Tagged::Length(TypedLength {
value: pixels,
unit: LengthUnit::Pixel,
}))
}
#[cfg(test)]
mod tests {
use super::*;
use migration_core::decl;
use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo};
fn contract() -> SourceContract {
SourceContract::new(9, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) })
}
#[test]
fn rewrites_history() {
check_commutation(&StrokeV1, &contract());
check_undo(&StrokeV1, &contract());
}
#[test]
fn numbers_of_pixels_become_lengths_in_pixels() {
let mut fixture = Fixture::new(&StrokeV1);
let number = |number: f64| literal(Tagged::Number(number));
let stroke = fixture.add(
decl!("graphene_core::vector::StrokeNode"@1),
vec![literal(Tagged::None), number(0.), number(2.), number(0.), number(0.), number(0.), number(4.), number(0.), number(5.)],
);
let migrated = fixture.migrate(&StrokeV1).unwrap();
assert_eq!(
fixture.inputs(&migrated, stroke),
[literal(Tagged::None), number(0.), length(2.), number(0.), number(0.), number(0.), number(4.), number(0.), length(5.)]
);
}
}
+1
View File
@@ -2,6 +2,7 @@
//! `cargo run migration new <identifier>` after bumping a node's `version(N)`.
mod blending_nodes;
mod core_types;
mod graphene_core;
mod graphene_std;
mod math_nodes;
@@ -15,6 +15,7 @@ use graph_craft::document::{NodeId, NodeInput};
use graph_craft::{ProtoNodeIdentifier, list};
use graphene_std::raster::BlendMode;
use graphene_std::text::{Font, TypesettingConfig};
use graphene_std::units::TypedLength;
use graphene_std::vector::style::{GradientForm, GradientHueDirection, GradientInterpolation, GradientSettings, GradientSpace, GradientSpread, PaintOrder, Stroke};
use graphene_std::vector::{Gradient, GradientRamp, Vector, VectorModification, VectorModificationType};
use graphene_std::{Artboard, Color, Graphic};
@@ -871,7 +872,7 @@ impl<'a> ModifyInputsContext<'a> {
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::PaintInput);
self.set_input_with_refresh(input_connector, NodeInput::value(color.map_or_else(TaggedValue::no_paint, TaggedValue::Color), false), true);
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::WeightInput);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::Number(stroke.weight), false), true);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::Length(TypedLength::pixels(stroke.weight)), false), true);
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::AlignInput);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::StrokeAlign(stroke.align), false), false);
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::CapInput);
@@ -883,7 +884,7 @@ impl<'a> ModifyInputsContext<'a> {
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::DashPatternInput);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::DashPattern(stroke.dash_lengths), false), true);
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::DashOffsetInput);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::Number(stroke.dash_offset), false), true);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::Length(TypedLength::pixels(stroke.dash_offset)), false), true);
}
/// Update the transform value of the upstream Transform node based a change to its existing value and the given parent transform.
@@ -2968,10 +2968,10 @@ pub(crate) fn sample_polyline_inspector(node_id: NodeId, context: &mut NodeInspe
let spacing = enum_choice::<PointSpacingType>()
.for_socket(ParameterWidgetsInfo::new(node_id, SpacingInput, true, context))
.inspector_row();
let separation = number_widget(ParameterWidgetsInfo::new(node_id, SeparationInput, true, context), NumberInput::default().min(0.).unit(" px"));
let separation = length_widget(ParameterWidgetsInfo::new(node_id, SeparationInput, true, context), NumberInput::default().min(0.));
let quantity = number_widget(ParameterWidgetsInfo::new(node_id, QuantityInput, true, context), NumberInput::default().min(2.).int());
let start_offset = number_widget(ParameterWidgetsInfo::new(node_id, StartOffsetInput, true, context), NumberInput::default().min(0.).unit(" px"));
let stop_offset = number_widget(ParameterWidgetsInfo::new(node_id, StopOffsetInput, true, context), NumberInput::default().min(0.).unit(" px"));
let start_offset = length_widget(ParameterWidgetsInfo::new(node_id, StartOffsetInput, true, context), NumberInput::default().min(0.));
let stop_offset = length_widget(ParameterWidgetsInfo::new(node_id, StopOffsetInput, true, context), NumberInput::default().min(0.));
let adaptive_spacing = bool_widget(ParameterWidgetsInfo::new(node_id, AdaptiveSpacingInput, true, context), CheckboxInput::default().disabled(is_quantity));
vec![
@@ -3799,7 +3799,7 @@ pub fn stroke_inspector(node_id: NodeId, context: &mut NodeInspectorContext) ->
ParameterWidgetsInfo::new(node_id, PaintInput, true, context),
crate::messages::layout::utility_types::widgets::button_widgets::ColorInput::default(),
);
let weight = number_widget(ParameterWidgetsInfo::new(node_id, WeightInput, true, context), NumberInput::default().unit(" px").min(0.));
let weight = length_widget(ParameterWidgetsInfo::new(node_id, WeightInput, true, context), NumberInput::default().min(0.));
let align = enum_choice::<StrokeAlign>().for_socket(ParameterWidgetsInfo::new(node_id, AlignInput, true, context)).inspector_row();
let cap = enum_choice::<StrokeCap>().for_socket(ParameterWidgetsInfo::new(node_id, CapInput, true, context)).inspector_row();
let join = enum_choice::<StrokeJoin>().for_socket(ParameterWidgetsInfo::new(node_id, JoinInput, true, context)).inspector_row();
@@ -3808,10 +3808,10 @@ pub fn stroke_inspector(node_id: NodeId, context: &mut NodeInspectorContext) ->
ParameterWidgetsInfo::new(node_id, MiterLimitInput, true, context),
NumberInput::default().min(0.).disabled(miter_limit_disabled),
);
let disabled_number_input = NumberInput::default().unit(" px").disabled(has_dash_lengths);
let disabled_number_input = NumberInput::default().disabled(has_dash_lengths);
let dash_lengths = dash_pattern_widget(ParameterWidgetsInfo::new(node_id, DashPatternInput, true, context), TextInput::default().centered(true));
let number_input = disabled_number_input;
let dash_offset = number_widget(ParameterWidgetsInfo::new(node_id, DashOffsetInput, true, context), number_input);
let dash_offset = length_widget(ParameterWidgetsInfo::new(node_id, DashOffsetInput, true, context), number_input);
vec![
color,
@@ -3828,8 +3828,7 @@ pub fn stroke_inspector(node_id: NodeId, context: &mut NodeInspectorContext) ->
pub fn offset_path_inspector(node_id: NodeId, context: &mut NodeInspectorContext) -> Vec<LayoutGroup> {
use graphene_std::vector::offset_path::*;
let number_input = NumberInput::default().unit(" px");
let distance = number_widget(ParameterWidgetsInfo::new(node_id, DistanceInput, true, context), number_input);
let distance = length_widget(ParameterWidgetsInfo::new(node_id, DistanceInput, true, context), NumberInput::default());
let join = enum_choice::<StrokeJoin>().for_socket(ParameterWidgetsInfo::new(node_id, JoinInput, true, context)).inspector_row();
@@ -683,11 +683,8 @@ pub fn get_text<'a>(
}
pub fn get_stroke_width(layer: LayerNodeIdentifier, network_interface: &NodeNetworkInterface) -> Option<f64> {
if let TaggedValue::Number(width) = NodeGraphLayer::new(layer, network_interface).parameter_value(graphene_std::vector::stroke::WeightInput)? {
Some(*width)
} else {
None
}
let weight = NodeGraphLayer::new(layer, network_interface).parameter_value(graphene_std::vector::stroke::WeightInput)?;
weight.as_length_pixels(network_interface.pixels_per_inch())
}
/// Subset of Stroke node inputs read for the control bar's stroke options popover.
@@ -729,10 +726,10 @@ pub fn get_stroke_options(layer: LayerNodeIdentifier, network_interface: &NodeNe
Some(TaggedValue::DashPattern(lengths)) => lengths.clone(),
_ => Vec::new(),
};
let dash_offset = match parameters.value(stroke::DashOffsetInput) {
Some(TaggedValue::Number(value)) => *value,
_ => 0.,
};
let dash_offset = parameters
.value(stroke::DashOffsetInput)
.and_then(|value| value.as_length_pixels(network_interface.pixels_per_inch()))
.unwrap_or_default();
Some(StrokeOptionsState {
align,
@@ -817,10 +814,11 @@ pub fn get_stroke_paint_order(layer: LayerNodeIdentifier, network_interface: &No
pub fn set_stroke_weight_for_selected_layers(weight: f64, document: &DocumentMessageHandler, responses: &mut VecDeque<Message>) {
for layer in paintable_selected_layers(document) {
if let Some(node_id) = get_stroke_id(layer, &document.network_interface) {
let input_connector = InputConnector::node(node_id, graphene_std::vector::stroke::WeightInput);
responses.add(NodeGraphMessage::SetInputValue {
node_id,
input_index: graphene_std::vector::stroke::WeightInput::INDEX,
value: TaggedValue::Number(weight).into(),
value: length_in_unit_of_input(&document.network_interface, &input_connector, weight).into(),
});
} else if weight > 0. {
let color = Some(Color::BLACK);
@@ -5,6 +5,7 @@ use crate::messages::tool::common_functionality::graph_modification_utils;
use graph_craft::document::value::TaggedValue;
use graphene_std::choice_type::ChoiceTypeStatic;
use graphene_std::core_types::misc::parse_f64;
use graphene_std::units::TypedLength;
use graphene_std::vector::style::{PaintOrder, StrokeAlign, StrokeCap, StrokeJoin};
/// All non-color stroke-related options surfaced in the control bar popover.
@@ -221,5 +222,5 @@ pub fn apply_dash_lengths(drawing: &mut DrawingToolState, lengths: Vec<f64>, doc
pub fn apply_dash_offset(drawing: &mut DrawingToolState, offset: f64, document: &DocumentMessageHandler, responses: &mut VecDeque<Message>) {
drawing.dash_offset = Some(offset);
graph_modification_utils::set_parameter_for_selected_layers(document, graphene_std::vector::stroke::DashOffsetInput, TaggedValue::Number(offset), responses);
graph_modification_utils::set_parameter_for_selected_layers(document, graphene_std::vector::stroke::DashOffsetInput, TaggedValue::Length(TypedLength::pixels(offset)), responses);
}
@@ -104,13 +104,13 @@ fn rank_0_content_promotes_through_the_layer_coercion_path() {
assert_eq!(stack.len(), 1, "The rank-0 content should contribute exactly one graphic to the stack");
}
/// Builds a network feeding the given content plus an f64 distance value into Offset Points, whose distance input is ranked `Item<f64>`.
/// Builds a network feeding the given content plus an f64 distance value into Offset Points, whose distance input is ranked `Item<Length>`.
fn offset_points_network(content: TaggedValue) -> ProtoNetwork {
let content_node = ProtoNode::value(ConstructionArgs::Value(content.into()), vec![NodeId(0)]);
let distance_node = ProtoNode::value(ConstructionArgs::Value(TaggedValue::Number(10.).into()), vec![NodeId(1)]);
let mut input_adapter_node = ProtoNode::value(ConstructionArgs::Nodes(vec![NodeId(1)]), vec![NodeId(2)]);
input_adapter_node.identifier = ProtoNodeIdentifier::new("input_adapter<f64>");
input_adapter_node.identifier = ProtoNodeIdentifier::new("input_adapter<Length>");
let mut offset_points_node = ProtoNode::value(ConstructionArgs::Nodes(vec![NodeId(0), NodeId(2)]), vec![NodeId(3)]);
offset_points_node.identifier = ProtoNodeIdentifier::new("core_types::vector::OffsetPointsNode");
+83 -95
View File
@@ -8,6 +8,7 @@ use core_types::bounds::{BoundingBox, RenderBoundingBox};
use core_types::list::{ATTR_APPEARANCE, ATTR_ID, ATTR_PATHS, Item, ItemAttributeValues, List, ListDyn, NodeIdPath};
use core_types::math::spread::spread_factors;
use core_types::transform::{Footprint, Transform};
use core_types::units::{Angle, Length};
use core_types::uuid::NodeId;
use core_types::{
ATTR_BLEND_MODE, ATTR_CLIPPING_MASK, ATTR_EDITOR_LAYER_PATH, ATTR_EDITOR_MERGED_LAYERS, ATTR_GRADIENT_FORM, ATTR_OPACITY, ATTR_OPACITY_FILL, ATTR_TRANSFORM, CloneVarArgs, Color, Context, Ctx,
@@ -499,7 +500,7 @@ where
}
/// Applies a stroke style to the vector content, giving an appearance to the area within the outline of the geometry.
#[node_macro::node(category("Vector: Style"), icon("NodeStroke"), path(graphene_core::vector), inspector("stroke_inspector"))]
#[node_macro::node(category("Vector: Style"), icon("NodeStroke"), path(graphene_core::vector), inspector("stroke_inspector"), version(2))]
fn stroke<V>(
_: impl Ctx,
/// The content with vector paths to apply the stroke style to.
@@ -509,10 +510,9 @@ fn stroke<V>(
#[default(Color::BLACK)]
paint: Item<Graphic>,
/// The stroke thickness.
#[unit(" px")]
#[hard(0..)]
#[default(2.)]
weight: Item<f64>,
weight: Item<Length>,
/// The alignment of stroke to the path's centerline or (for closed shapes) the inside or outside of the shape.
align: Item<StrokeAlign>,
/// The shape of the stroke at open endpoints.
@@ -526,20 +526,19 @@ fn stroke<V>(
/// The stroke dash pattern. Each length forms a distance in a pattern where the first length is a dash, the second is a gap, and so on. If the list is an odd length, the pattern repeats with solid-gap roles reversed.
dash_pattern: Item<DashPattern>,
/// The phase offset distance from the starting point of the dash pattern.
#[unit(" px")]
dash_offset: Item<f64>,
dash_offset: Item<Length>,
) -> Item<V>
where
Item<V>: VectorItemMut + Send,
{
let mut content = content;
let (weight, align, cap, join, miter_limit, dash_offset) = (
weight.into_element(),
weight.into_element().pixels(),
align.into_element(),
cap.into_element(),
join.into_element(),
miter_limit.into_element(),
dash_offset.into_element(),
dash_offset.into_element().pixels(),
);
let dash_lengths = dash_pattern.into_element().clamped_lengths();
@@ -572,14 +571,13 @@ fn dash_pattern(
Item::new_from_element(DashPattern::from(lengths))
}
#[node_macro::node(category("Vector: Path"), icon("NodeRoundCorners"), path(core_types::vector))]
#[node_macro::node(category("Vector: Path"), icon("NodeRoundCorners"), path(core_types::vector), version(2))]
fn round_corners<V: MapVectorItems + Send>(
_: impl Ctx,
#[implementations(Graphic, Vector)] source: Item<V>,
#[unit(" px")]
#[hard(0..)]
#[default(10.)]
radius: Item<f64>,
radius: Item<Length>,
#[range]
#[hard(0..1)]
#[default(0.5)]
@@ -589,14 +587,13 @@ fn round_corners<V: MapVectorItems + Send>(
#[hard(0..100)]
#[default(100.)]
edge_length_limit: Item<f64>,
#[unit("°")]
#[range]
#[hard(0..180)]
#[default(5.)]
min_angle_threshold: Item<f64>,
min_angle_threshold: Item<Angle>,
) -> Item<V> {
V::map_vector_items(source, |source| {
let (radius, roundness, edge_length_limit, min_angle_threshold) = (*radius.element(), *roundness.element(), *edge_length_limit.element(), *min_angle_threshold.element());
let (radius, roundness, edge_length_limit, min_angle_threshold) = (radius.element().pixels(), *roundness.element(), *edge_length_limit.element(), min_angle_threshold.element().radians());
let source_transform: DAffine2 = source.attribute_cloned_or_default(ATTR_TRANSFORM);
if !source_transform.matrix2.determinant().is_normal() {
return source;
@@ -618,7 +615,7 @@ fn round_corners<V: MapVectorItems + Send>(
document_bezpath.apply_affine(Affine::new(source_transform.to_cols_array()));
// A subpath without corners to round passes through as it was
let Some(mut rounded_subpath) = round_subpath_corners(&document_bezpath, radius, roundness, edge_length_limit, min_angle_threshold.to_radians()) else {
let Some(mut rounded_subpath) = round_subpath_corners(&document_bezpath, radius, roundness, edge_length_limit, min_angle_threshold) else {
rounded_subpaths.push(bezpath);
continue;
};
@@ -740,21 +737,20 @@ fn round_subpath_corners(subpath: &BezPath, radius: f64, roundness: f64, edge_le
}
/// Joins points lying within a distance of each other into one, connecting the paths they belong to.
#[node_macro::node(category("Vector: Path"), icon("NodeWeld"), path(core_types::vector))]
#[node_macro::node(category("Vector: Path"), icon("NodeWeld"), path(core_types::vector), version(2))]
fn weld<V: MapVectorItems + Send + Sync + 'static>(
_: impl Ctx,
/// The vector with points to join.
#[implementations(Graphic, Vector)]
content: Item<V>,
/// How close points must be to join.
#[unit(" px")]
#[default(0.1)]
#[hard(0.0001..)]
distance: Item<f64>,
distance: Item<Length>,
/// Which points may join: any within the distance, the two ends of each segment shorter than it, or only the ends of open path flows.
method: Item<WeldMethod>,
) -> Item<V> {
let (distance, method) = (*distance.element(), *method.element());
let (distance, method) = (distance.element().pixels(), *method.element());
V::map_vector_items(content, |content| {
let mut content = content;
@@ -1173,7 +1169,7 @@ fn bilinear_interpolate(t: DVec2, quad: &[DVec2; 4]) -> DVec2 {
tl * (1. - t.x) * (1. - t.y) + tr * t.x * (1. - t.y) + br * t.x * t.y + bl * (1. - t.x) * t.y
}
#[node_macro::node(category("Transform"), icon("NodePackStrips"), path(graphene_core::vector), footprint_invariant)]
#[node_macro::node(category("Transform"), icon("NodePackStrips"), path(graphene_core::vector), footprint_invariant, version(2))]
fn pack_strips<T: Send + Clone>(
_: impl Ctx,
#[implementations(
@@ -1183,19 +1179,15 @@ fn pack_strips<T: Send + Clone>(
List<Raster<GPU>>,
)]
elements: List<T>,
#[default(0.)]
#[unit(" px")]
separation: Item<f64>,
#[default(1000.)]
#[unit(" px")]
strip_max_length: Item<f64>,
#[default(0.)] separation: Item<Length>,
#[default(1000.)] strip_max_length: Item<Length>,
strip_direction: Item<RowsOrColumns>,
) -> List<T>
where
Graphic: From<List<T>>,
List<T>: BoundingBox,
{
let (separation, strip_max_length, strip_direction) = (separation.into_element(), strip_max_length.into_element(), strip_direction.into_element());
let (separation, strip_max_length, strip_direction) = (separation.into_element().pixels(), strip_max_length.into_element().pixels(), strip_direction.into_element());
// Packs shapes using bounds with Best-Fit Decreasing Height (BFDH) algorithm:
// - Sort shapes by cross-axis size (tallest first for rows, widest first for columns)
@@ -1772,11 +1764,11 @@ fn mesh_weld_tolerance(polygons: &[Vec<DVec2>]) -> f64 {
if diagonal.is_finite() && diagonal > 0. { (diagonal * 1e-6).max(1e-12) } else { 1e-6 }
}
#[node_macro::node(category("Vector: Path"), icon("NodeOffsetPath"), path(core_types::vector), inspector("offset_path_inspector"))]
#[node_macro::node(category("Vector: Path"), icon("NodeOffsetPath"), path(core_types::vector), inspector("offset_path_inspector"), version(2))]
fn offset_path<V: MapVectorItems + Send>(
_: impl Ctx,
#[implementations(Graphic, Vector)] content: Item<V>,
distance: Item<f64>,
distance: Item<Length>,
join: Item<StrokeJoin>,
#[hard(0..)]
#[default(4.)]
@@ -1784,7 +1776,7 @@ fn offset_path<V: MapVectorItems + Send>(
) -> Result<Item<V>, OffsetPathError> {
V::try_map_vector_items(content, |content| {
let mut content = content;
let (distance, join, miter_limit) = (*distance.element(), *join.element(), *miter_limit.element());
let (distance, join, miter_limit) = (distance.element().pixels(), *join.element(), *miter_limit.element());
let transform_attribute: DAffine2 = content.attribute_cloned_or_default(ATTR_TRANSFORM);
if !transform_attribute.matrix2.determinant().is_normal() {
@@ -2118,30 +2110,25 @@ pub fn combine_paths(_: impl Ctx, content: List<Graphic>) -> Item<Vector> {
}
/// Convert vector geometry into a polyline composed of evenly spaced points.
#[node_macro::node(category("Vector: Path"), icon("NodeSamplePolyline"), path(core_types::vector), inspector("sample_polyline_inspector"), memoize)]
#[node_macro::node(category("Vector: Path"), icon("NodeSamplePolyline"), path(core_types::vector), inspector("sample_polyline_inspector"), memoize, version(2))]
fn sample_polyline<V: MapVectorItems + Send>(
_: impl Ctx,
#[implementations(Graphic, Vector)] content: Item<V>,
spacing: Item<PointSpacingType>,
#[default(100.)]
#[hard(0..)]
#[unit(" px")]
separation: Item<f64>,
separation: Item<Length>,
#[default(100)]
#[hard(2..)]
quantity: Item<i64>,
#[hard(0..)]
#[unit(" px")]
start_offset: Item<f64>,
#[hard(0..)]
#[unit(" px")]
stop_offset: Item<f64>,
#[hard(0..)] start_offset: Item<Length>,
#[hard(0..)] stop_offset: Item<Length>,
adaptive_spacing: Item<bool>,
) -> Result<Item<V>, SampleCountError> {
V::try_map_vector_items(content, |content| {
let mut content = content;
let (spacing, separation, quantity) = (*spacing.element(), *separation.element(), *quantity.element());
let (start_offset, stop_offset, adaptive_spacing) = (*start_offset.element(), *stop_offset.element(), *adaptive_spacing.element());
let (spacing, separation, quantity) = (*spacing.element(), separation.element().pixels(), *quantity.element());
let (start_offset, stop_offset, adaptive_spacing) = (start_offset.element().pixels(), stop_offset.element().pixels(), *adaptive_spacing.element());
let pathseg_perimeter = |segment: PathSeg| {
if is_linear(segment) {
@@ -2216,19 +2203,18 @@ fn tolerance_floor(bezpath: &BezPath) -> f64 {
}
/// Simplifies vector paths by reducing the number of curve segments while preserving the overall shape within the given tolerance.
#[node_macro::node(category("Vector: Path"), icon("NodeSimplify"), path(core_types::vector))]
#[node_macro::node(category("Vector: Path"), icon("NodeSimplify"), path(core_types::vector), version(2))]
fn simplify<V: MapVectorItems + Send>(
_: impl Ctx,
/// The vector paths to simplify.
#[implementations(Graphic, Vector)]
content: Item<V>,
/// The maximum distance the simplified path may deviate from the original.
#[unit(" px")]
#[hard(0..)]
#[default(5.)]
tolerance: Item<f64>,
tolerance: Item<Length>,
) -> Item<V> {
let tolerance = *tolerance.element();
let tolerance = tolerance.element().pixels();
if tolerance <= 0. {
return content;
@@ -2271,21 +2257,20 @@ fn simplify<V: MapVectorItems + Send>(
}
/// Decimates vector paths into polylines by sampling any curves into line segments, then removing points that don't significantly contribute to the shape using the Ramer-Douglas-Peucker algorithm.
#[node_macro::node(category("Vector: Path"), icon("NodeDecimate"), path(core_types::vector))]
#[node_macro::node(category("Vector: Path"), icon("NodeDecimate"), path(core_types::vector), version(2))]
fn decimate<V: MapVectorItems + Send>(
_: impl Ctx,
/// The vector paths to decimate.
#[implementations(Graphic, Vector)]
content: Item<V>,
/// The maximum distance a point can deviate from the simplified path before it is kept.
#[unit(" px")]
#[hard(0..)]
#[default(5.)]
tolerance: Item<f64>,
tolerance: Item<Length>,
) -> Item<V> {
V::map_vector_items(content, |content| {
let mut content = content;
let tolerance = *tolerance.element();
let tolerance = tolerance.element().pixels();
// Tolerance of 0 means no simplification is possible, so return immediately
if tolerance <= 0. {
@@ -2554,16 +2539,15 @@ fn evaluate_path(
})
}
#[node_macro::node(category("Vector: Point"), icon("NodeScatterPoints"), path(core_types::vector), version(2), memoize)]
#[node_macro::node(category("Vector: Point"), icon("NodeScatterPoints"), path(core_types::vector), version(3), memoize)]
fn scatter_points<V: MapVectorItems + Send>(
_: impl Ctx,
#[implementations(Graphic, Vector)] content: Item<V>,
#[unit(" px")]
#[default(10.)]
#[range]
#[hard(0.01..)]
#[soft(1..100)]
separation: Item<f64>,
separation: Item<Length>,
#[hard(0..)] seed: Item<i64>,
) -> Result<Item<V>, PoissonDiskError> {
// One sampler for every vector in the content holds them all together to its limits on work
@@ -2571,7 +2555,7 @@ fn scatter_points<V: MapVectorItems + Send>(
V::try_map_vector_items(content, |content| {
let mut content = content;
let (separation, seed) = (*separation.element(), *seed.element());
let (separation, seed) = (separation.element().pixels(), *seed.element());
let content_paths = shared_paths_from_item_attributes(&content);
let bezpaths: Vec<_> = content.element().stroke_bezpath_iter(&content_paths).collect();
@@ -2732,16 +2716,15 @@ fn apply_point_deltas(vector: &mut Vector, paths: &List<Path>, anchors: &[usize]
}
/// Perturbs the positions of anchor points in vector geometry by random amounts and directions.
#[node_macro::node(category("Vector: Point"), icon("NodeJitterPoints"), path(core_types::vector))]
#[node_macro::node(category("Vector: Point"), icon("NodeJitterPoints"), path(core_types::vector), version(2))]
fn jitter_points<V: MapVectorItems + Send>(
_: impl Ctx,
/// The vector geometry with points to be jittered.
#[implementations(Graphic, Vector)]
content: Item<V>,
/// The maximum extent of the random distance each point can be offset.
#[unit(" px")]
#[default(5.)]
max_distance: Item<f64>,
max_distance: Item<Length>,
/// Seed used to determine unique variations on all randomized offsets.
#[hard(0..)]
seed: Item<i64>,
@@ -2751,7 +2734,7 @@ fn jitter_points<V: MapVectorItems + Send>(
) -> Item<V> {
V::map_vector_items(content, |content| {
let mut content = content;
let (max_distance, seed, along_normals) = (*max_distance.element(), *seed.element(), *along_normals.element());
let (max_distance, seed, along_normals) = (max_distance.element().pixels(), *seed.element(), *along_normals.element());
let mut rng = rand::rngs::StdRng::seed_from_u64(seed as u64);
let transform_attribute: DAffine2 = content.attribute_cloned_or_default(ATTR_TRANSFORM);
@@ -2784,7 +2767,7 @@ fn jitter_points<V: MapVectorItems + Send>(
/// Displaces anchor points along their normal direction (perpendicular to the path) by a set distance.
/// Points with 0 or 3+ segment connections have no well-defined normal and are left in place.
#[node_macro::node(category("Vector: Point"), icon("NodeOffsetPoints"), path(core_types::vector))]
#[node_macro::node(category("Vector: Point"), icon("NodeOffsetPoints"), path(core_types::vector), version(2))]
fn offset_points<V: MapVectorItems + Send>(
_: impl Ctx,
/// The vector geometry with points to be offset.
@@ -2792,12 +2775,11 @@ fn offset_points<V: MapVectorItems + Send>(
content: Item<V>,
/// The distance to offset each anchor point along its normal. Positive values move outward, negative values move inward.
#[default(10.)]
#[unit(" px")]
distance: Item<f64>,
distance: Item<Length>,
) -> Item<V> {
V::map_vector_items(content, |content| {
let mut content = content;
let distance = *distance.element();
let distance = distance.element().pixels();
let transform_attribute: DAffine2 = content.attribute_cloned_or_default(ATTR_TRANSFORM);
let inverse_linear = inverse_linear_or_repair(transform_attribute.matrix2);
@@ -3800,18 +3782,17 @@ fn bevel_algorithm(vector: &mut Vector, paths: &mut List<Path>, transform: DAffi
}
}
#[node_macro::node(category("Vector: Path"), icon("NodeBevel"), path(core_types::vector))]
#[node_macro::node(category("Vector: Path"), icon("NodeBevel"), path(core_types::vector), version(2))]
fn bevel<V: MapVectorItems + Send + Sync + 'static>(
_: impl Ctx,
#[implementations(Graphic, Vector)] source: Item<V>,
#[unit(" px")]
#[hard(0..)]
#[default(10.)]
distance: Item<f64>,
distance: Item<Length>,
) -> Item<V> {
V::map_vector_items(source, |source| {
let mut source = source;
let distance = *distance.element();
let distance = distance.element().pixels();
let transform: DAffine2 = source.attribute_cloned_or_default(ATTR_TRANSFORM);
edit_item_geometry(&mut source, |vector, paths| bevel_algorithm(vector, paths, transform, distance));
@@ -4029,6 +4010,10 @@ mod test {
Item::new_from_element(value)
}
fn pixels(pixels: f64) -> Item<Length> {
item(Length::from_pixels(pixels))
}
/// The paths riding an item, standing in for the empty weave when the item carries none.
fn item_paths<T>(item: &Item<T>) -> &List<Path> {
paths_from_item_attributes(item).unwrap_or_else(|| no_paths())
@@ -4109,7 +4094,7 @@ mod test {
// Offsetting closed triangles must not leave each subpath with a redundant start/end anchor (a Kurbo offset
// contour returns to approximately, not exactly, its start; that near-coincident point must close, not duplicate).
let delaunay = super::triangulate((), vector_item_from_points(&SQUARE_WITH_CENTER), item(false)).unwrap();
let offset = super::offset_path((), delaunay, item(0.5), item(StrokeJoin::Miter), item(4.)).unwrap();
let offset = super::offset_path((), delaunay, pixels(0.5), item(StrokeJoin::Miter), item(4.)).unwrap();
let paths = item_paths(&offset);
let result = offset.element();
@@ -4446,10 +4431,10 @@ mod test {
Footprint::default(),
vector_item_from_bezpath(path),
Item::new_from_element(PointSpacingType::Separation),
Item::new_from_element(30.),
pixels(30.),
Item::new_from_element(0),
Item::new_from_element(0.),
Item::new_from_element(0.),
pixels(0.),
pixels(0.),
Item::new_from_element(false),
)
.unwrap();
@@ -4466,10 +4451,10 @@ mod test {
Footprint::default(),
vector_item_from_bezpath(path),
Item::new_from_element(PointSpacingType::Separation),
Item::new_from_element(18.),
pixels(18.),
Item::new_from_element(0),
Item::new_from_element(45.),
Item::new_from_element(10.),
pixels(45.),
pixels(10.),
Item::new_from_element(true),
)
.unwrap();
@@ -4484,7 +4469,7 @@ mod test {
let poisson_points = super::scatter_points(
Footprint::default(),
vector_item_from_bezpath(ellipse_bezpath(DVec2::splat(-50.), DVec2::splat(50.))),
Item::new_from_element(10. * std::f64::consts::SQRT_2),
pixels(10. * std::f64::consts::SQRT_2),
Item::new_from_element(0),
)
.expect("the separation should fit the circle");
@@ -4798,7 +4783,7 @@ mod test {
#[test]
fn bevel_rect() {
let source = Rect::new(0., 0., 100., 100.).to_path(DEFAULT_ACCURACY);
let beveled = super::bevel(Footprint::default(), vector_item_from_bezpath(source), Item::new_from_element(2_f64.sqrt() * 10.));
let beveled = super::bevel(Footprint::default(), vector_item_from_bezpath(source), pixels(2_f64.sqrt() * 10.));
let paths = item_paths(&beveled);
let beveled = beveled.element();
@@ -4827,7 +4812,7 @@ mod test {
source.line_to(Point::ZERO);
source.push(curve.as_path_el());
let beveled = super::bevel((), vector_item_from_bezpath(source), Item::new_from_element(2_f64.sqrt() * 10.));
let beveled = super::bevel((), vector_item_from_bezpath(source), pixels(2_f64.sqrt() * 10.));
let paths = item_paths(&beveled);
let beveled = beveled.element();
@@ -4855,7 +4840,7 @@ mod test {
let transform = DAffine2::from_scale_angle_translation(DVec2::splat(10.), 1., DVec2::new(99., 77.));
let vector_item = Vector::item_from_bezpath(&source).with_attribute(ATTR_TRANSFORM, transform);
let beveled = super::bevel((), vector_item, Item::new_from_element(2_f64.sqrt() * 100.));
let beveled = super::bevel((), vector_item, pixels(2_f64.sqrt() * 100.));
let paths = item_paths(&beveled);
let beveled = beveled.element();
@@ -4879,7 +4864,7 @@ mod test {
source.line_to(Point::new(100., 100.));
source.line_to(Point::new(0., 100.));
let beveled = super::bevel(Footprint::default(), vector_item_from_bezpath(source), Item::new_from_element(999.));
let beveled = super::bevel(Footprint::default(), vector_item_from_bezpath(source), pixels(999.));
let paths = item_paths(&beveled);
let beveled = beveled.element();
@@ -4904,7 +4889,7 @@ mod test {
let subpath = BezPath::from_path_segments([line, point, curve].into_iter());
let beveled_item = super::bevel(Footprint::default(), vector_item_from_bezpath(subpath), Item::new_from_element(5.));
let beveled_item = super::bevel(Footprint::default(), vector_item_from_bezpath(subpath), pixels(5.));
let paths = item_paths(&beveled_item);
let beveled = beveled_item.element();
@@ -4952,7 +4937,7 @@ mod test {
}
fn round_corners_of(bezpath: BezPath, transform: DAffine2) -> Vec<BezPath> {
let rounded = super::round_corners((), create_vector_item(bezpath, transform), item(10.), item(0.5), item(100.), item(5.));
let rounded = super::round_corners((), create_vector_item(bezpath, transform), pixels(10.), item(0.5), item(100.), item(Angle::from_degrees(5.)));
rounded.element().stroke_bezpath_iter(item_paths(&rounded)).collect()
}
@@ -5067,7 +5052,7 @@ mod test {
// Sampling the welded diamond goes once around its closed contour, and once unwelded, along each open path on its own
#[test]
fn unwelded_paths_are_sampled_one_at_a_time() {
let sample = |content: Item<Vector>| super::sample_polyline((), content, item(PointSpacingType::Quantity), item(100.), item(5), item(0.), item(0.), item(false)).unwrap();
let sample = |content: Item<Vector>| super::sample_polyline((), content, item(PointSpacingType::Quantity), pixels(100.), item(5), pixels(0.), pixels(0.), item(false)).unwrap();
let polylines = |sampled: Item<Vector>| sampled.element().stroke_bezpath_iter(item_paths(&sampled)).collect::<Vec<_>>();
let welded = polylines(sample(welded_diamond()));
@@ -5132,7 +5117,7 @@ mod test {
#[test]
fn offset_path_reports_a_shape_too_large_to_clip() {
let star = vector_item_from_bezpath(vector_types::vector::algorithms::shapes::star_polygon_bezpath(DVec2::ZERO, 5, 1e20, 5e19).unwrap());
let offset = super::offset_path((), star, item(-1e18), item(StrokeJoin::Miter), item(4.));
let offset = super::offset_path((), star, pixels(-1e18), item(StrokeJoin::Miter), item(4.));
assert!(matches!(offset, Err(OffsetPathError::TooLarge)));
}
@@ -5141,25 +5126,28 @@ mod test {
fn collapsed_items_pass_through() {
let collapsed = create_vector_item(Rect::new(0., 0., 100., 100.).to_path(DEFAULT_ACCURACY), DAffine2::from_scale(DVec2::new(0., 1.)));
assert_eq!(super::round_corners((), collapsed.clone(), item(10.), item(0.5), item(100.), item(5.)), collapsed);
assert_eq!(super::offset_path((), collapsed.clone(), item(5.), item(StrokeJoin::Miter), item(4.)).unwrap(), collapsed);
assert_eq!(super::simplify((), collapsed.clone(), item(5.)), collapsed);
assert_eq!(super::decimate((), collapsed.clone(), item(5.)), collapsed);
assert_eq!(super::bevel((), collapsed.clone(), item(10.)), collapsed);
assert_eq!(
super::round_corners((), collapsed.clone(), pixels(10.), item(0.5), item(100.), item(Angle::from_degrees(5.))),
collapsed
);
assert_eq!(super::offset_path((), collapsed.clone(), pixels(5.), item(StrokeJoin::Miter), item(4.)).unwrap(), collapsed);
assert_eq!(super::simplify((), collapsed.clone(), pixels(5.)), collapsed);
assert_eq!(super::decimate((), collapsed.clone(), pixels(5.)), collapsed);
assert_eq!(super::bevel((), collapsed.clone(), pixels(10.)), collapsed);
}
// Kurbo drops a subpath that simplifies down to nothing, so one without length passes through
#[test]
fn simplify_keeps_a_subpath_without_length() {
let point = vector_item_from_bezpath(ellipse_bezpath(DVec2::splat(5.), DVec2::splat(5.)));
let simplified = super::simplify((), point, item(5.));
let simplified = super::simplify((), point, pixels(5.));
assert_eq!(simplified.element().stroke_bezpath_iter(item_paths(&simplified)).count(), 1);
}
#[test]
fn decimate_with_a_tolerance_finer_than_float_precision() {
let circle = vector_item_from_bezpath(ellipse_bezpath(DVec2::splat(-50.), DVec2::splat(50.)));
let decimated = super::decimate((), circle, item(1e-60));
let decimated = super::decimate((), circle, pixels(1e-60));
assert!(decimated.element().point_count() > 4);
}
@@ -5203,7 +5191,7 @@ mod test {
fn scatter_points_samples_each_vector_in_graphic_content() {
let shapes = [ellipse_bezpath(DVec2::splat(-50.), DVec2::splat(50.)), ellipse_bezpath(DVec2::new(200., -40.), DVec2::new(300., 40.))];
let vectors: List<Vector> = shapes.iter().cloned().map(vector_item_from_bezpath).collect();
let scattered = super::scatter_points((), Item::new_from_element(Graphic::VectorList(vectors)), item(10.), item(0)).expect("the shapes should scatter");
let scattered = super::scatter_points((), Item::new_from_element(Graphic::VectorList(vectors)), pixels(10.), item(0)).expect("the shapes should scatter");
let Graphic::VectorList(vectors) = scattered.element() else {
panic!("the content should stay a vector list")
@@ -5217,12 +5205,12 @@ mod test {
#[test]
fn scatter_points_on_empty_content_gives_no_points() {
let scattered = super::scatter_points((), vector_item_from_bezpath(BezPath::new()), item(10.), item(0)).expect("empty content should scatter nothing");
let scattered = super::scatter_points((), vector_item_from_bezpath(BezPath::new()), pixels(10.), item(0)).expect("empty content should scatter nothing");
assert_eq!(scattered.element().point_count(), 0);
}
fn scattered_points(bezpath: BezPath, separation: f64, seed: i64) -> Vec<(DVec2, PointId)> {
let scattered = super::scatter_points((), vector_item_from_bezpath(bezpath), item(separation), item(seed)).expect("the separation should fit the shape");
let scattered = super::scatter_points((), vector_item_from_bezpath(bezpath), pixels(separation), item(seed)).expect("the separation should fit the shape");
let points = &scattered.element().0;
points.iter_element_values().copied().zip(points.iter_attribute_values::<PointId>(ATTR_ID).unwrap().copied()).collect()
}
@@ -5290,7 +5278,7 @@ mod test {
#[test]
fn scatter_points_reports_a_separation_too_small_for_the_shape() {
let circle = |center: DVec2, radius: f64| vector_item_from_bezpath(ellipse_bezpath(center - radius, center + radius));
let scatter = |content, separation| super::scatter_points((), content, item(separation), item(0));
let scatter = |content, separation| super::scatter_points((), content, pixels(separation), item(0));
assert!(matches!(scatter(circle(DVec2::ZERO, 100.), 0.15), Err(PoissonDiskError::SeparationTooSmall { .. })));
assert!(matches!(scatter(circle(DVec2::ZERO, 100.), 0.01), Err(PoissonDiskError::ExtentTooLarge)));
@@ -5317,9 +5305,9 @@ mod test {
let transform = DAffine2::from_cols(DVec2::new(1e160, 0.), DVec2::ZERO, DVec2::ZERO);
let content = || create_vector_item(Rect::new(0., 0., 10., 10.).to_path(DEFAULT_ACCURACY), transform);
let jittered = super::jitter_points((), content(), item(5.), item(0), item(true));
let jittered = super::jitter_points((), content(), pixels(5.), item(0), item(true));
assert!(jittered.element().positions().all(|position| position.is_finite()));
let offset = super::offset_points((), content(), item(10.));
let offset = super::offset_points((), content(), pixels(10.));
assert!(offset.element().positions().all(|position| position.is_finite()));
}
@@ -5360,7 +5348,7 @@ mod test {
push_path(&mut paths, CurveType::Linear, [0, 1, 2, 3, 1], []);
push_path(&mut paths, CurveType::Linear, [4, 5, 6], []);
let beveled = super::bevel((), Item::new_from_element(vector).with_attribute(ATTR_PATHS, paths), item(10.));
let beveled = super::bevel((), Item::new_from_element(vector).with_attribute(ATTR_PATHS, paths), pixels(10.));
assert!(beveled.element().segment_iter(item_paths(&beveled)).count() > 6);
}
@@ -5376,7 +5364,7 @@ mod test {
#[test]
fn sample_polyline_reports_too_many_points() {
let line = || vector_item_from_bezpath(Line::new((0., 0.), (100., 0.)).to_path(DEFAULT_ACCURACY));
let sample = |spacing, separation, quantity| super::sample_polyline((), line(), item(spacing), item(separation), item(quantity), item(0.), item(0.), item(false));
let sample = |spacing, separation, quantity| super::sample_polyline((), line(), item(spacing), pixels(separation), item(quantity), pixels(0.), pixels(0.), item(false));
assert!(matches!(sample(PointSpacingType::Separation, 0., 100), Err(SampleCountError::SeparationTooSmall { limit: 10_000 })));
assert!(matches!(sample(PointSpacingType::Quantity, 100., 10_001), Err(SampleCountError::QuantityTooLarge { limit: 10_000 })));