Clean up code by using Iterator::collect() when constructing instance tables (#2918)
Some checks are pending
Editor: Dev & CI / build (push) Waiting to run
Editor: Dev & CI / cargo-deny (push) Waiting to run

* instances: `Iterator::collect()` instances

* instances: adjust nodes to use iterators

* fix warnings on master

* Bump MSRV

* Port the remaining usages

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Firestar99 2025-07-23 07:51:40 +02:00 committed by GitHub
parent 032f9bdf72
commit 890da6a3c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 964 additions and 985 deletions

View file

@ -2,7 +2,7 @@
name = "graphite-editor"
publish = false
version = "0.0.0"
rust-version = "1.85"
rust-version = "1.88"
authors = ["Graphite Authors <contact@graphite.rs>"]
edition = "2024"
readme = "../README.md"

View file

@ -6804,13 +6804,6 @@ impl From<DocumentNodePersistentMetadataPropertiesRow> for DocumentNodePersisten
}
}
#[derive(serde::Serialize, serde::Deserialize)]
enum NodePersistentMetadataVersions {
DocumentNodePersistentMetadataPropertiesRow(DocumentNodePersistentMetadataPropertiesRow),
NodePersistentMetadataInputNames(DocumentNodePersistentMetadataInputNames),
NodePersistentMetadata(DocumentNodePersistentMetadata),
}
fn deserialize_node_persistent_metadata<'de, D>(deserializer: D) -> Result<DocumentNodePersistentMetadata, D::Error>
where
D: serde::Deserializer<'de>,

View file

@ -1000,7 +1000,7 @@ impl ShapeState {
} else {
// Push both in and out handles into the correct position
for ((handle, sign), other_anchor) in handles.iter().zip([1., -1.]).zip(&anchor_positions) {
let Some(anchor_vector) = other_anchor.map(|position| (position - anchor_position)) else {
let Some(anchor_vector) = other_anchor.map(|position| position - anchor_position) else {
continue;
};

View file

@ -2,7 +2,7 @@
name = "graphite-wasm"
publish = false
version = "0.0.0"
rust-version = "1.85"
rust-version = "1.88"
authors = ["Graphite Authors <contact@graphite.rs>"]
edition = "2024"
readme = "../../README.md"

View file

@ -169,7 +169,7 @@ where
A::Item: Clone,
B::Item: Clone,
{
a.flat_map(move |i| (b.clone().map(move |j| (i.clone(), j))))
a.flat_map(move |i| b.clone().map(move |j| (i.clone(), j)))
}
/// A square (represented by its top left corner position and width/height of `square_size`) that is currently a candidate for targetting by the dart throwing process.

View file

@ -1086,7 +1086,7 @@ fn compute_dual(minor_graph: &MinorGraph) -> Result<DualGraph, BooleanError> {
let outer_face_key = if count != 1 {
#[cfg(feature = "logging")]
eprintln!("Found multiple outer faces: {areas:?}, falling back to area calculation");
let (key, _) = *areas.iter().max_by_key(|(_, area)| ((area.abs() * 1000.) as u64)).unwrap();
let (key, _) = *areas.iter().max_by_key(|(_, area)| (area.abs() * 1000.) as u64).unwrap();
*key
} else {
*windings

View file

@ -27,6 +27,24 @@ impl<T> Instances<T> {
}
}
pub fn new_instance(instance: Instance<T>) -> Self {
Self {
instance: vec![instance.instance],
transform: vec![instance.transform],
alpha_blending: vec![instance.alpha_blending],
source_node_id: vec![instance.source_node_id],
}
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
instance: Vec::with_capacity(capacity),
transform: Vec::with_capacity(capacity),
alpha_blending: Vec::with_capacity(capacity),
source_node_id: Vec::with_capacity(capacity),
}
}
pub fn push(&mut self, instance: Instance<T>) {
self.instance.push(instance.instance);
self.transform.push(instance.transform);
@ -161,6 +179,18 @@ unsafe impl<T: StaticType + 'static> StaticType for Instances<T> {
type Static = Instances<T>;
}
impl<T> FromIterator<Instance<T>> for Instances<T> {
fn from_iter<I: IntoIterator<Item = Instance<T>>>(iter: I) -> Self {
let iter = iter.into_iter();
let (lower, _) = iter.size_hint();
let mut instances = Self::with_capacity(lower);
for instance in iter {
instances.push(instance);
}
instances
}
}
fn one_daffine2_default() -> Vec<DAffine2> {
vec![DAffine2::IDENTITY]
}

View file

@ -182,7 +182,7 @@ where
A::Item: Clone,
B::Item: Clone,
{
a.flat_map(move |i| (b.clone().map(move |j| (i.clone(), j))))
a.flat_map(move |i| b.clone().map(move |j| (i.clone(), j)))
}
/// A square (represented by its top left corner position and width/height of `square_size`) that is currently a candidate for targetting by the dart throwing process.

View file

@ -337,7 +337,7 @@ impl VectorData {
/// Returns the number of linear segments connected to the given point.
pub fn connected_linear_segments(&self, point_id: PointId) -> usize {
self.segment_bezier_iter()
.filter(|(_, bez, start, end)| ((*start == point_id || *end == point_id) && matches!(bez.handles, BezierHandles::Linear)))
.filter(|(_, bez, start, end)| (*start == point_id || *end == point_id) && matches!(bez.handles, BezierHandles::Linear))
.count()
}

View file

@ -443,9 +443,9 @@ async fn round_corners(
#[default(5.)]
min_angle_threshold: Angle,
) -> VectorDataTable {
let mut result_table = VectorDataTable::default();
for source in source.instance_ref_iter() {
source
.instance_ref_iter()
.map(|source| {
let source_transform = *source.transform;
let source_transform_inverse = source_transform.inverse();
let source = source.instance;
@ -538,15 +538,14 @@ async fn round_corners(
result.upstream_graphic_group = upstream_graphic_group;
result_table.push(Instance {
Instance {
instance: result,
transform: source_transform,
alpha_blending: Default::default(),
source_node_id: None,
});
}
result_table
})
.collect()
}
#[node_macro::node(name("Merge by Distance"), category("Vector: Modifier"), path(graphene_core::vector))]
@ -558,25 +557,23 @@ pub fn merge_by_distance(
distance: PixelLength,
algorithm: MergeByDistanceAlgorithm,
) -> VectorDataTable {
let mut result_table = VectorDataTable::default();
match algorithm {
MergeByDistanceAlgorithm::Spatial => {
for mut vector_data_instance in vector_data.instance_iter() {
MergeByDistanceAlgorithm::Spatial => vector_data
.instance_iter()
.map(|mut vector_data_instance| {
vector_data_instance.instance.merge_by_distance_spatial(vector_data_instance.transform, distance);
result_table.push(vector_data_instance);
}
}
MergeByDistanceAlgorithm::Topological => {
for mut vector_data_instance in vector_data.instance_iter() {
vector_data_instance
})
.collect(),
MergeByDistanceAlgorithm::Topological => vector_data
.instance_iter()
.map(|mut vector_data_instance| {
vector_data_instance.instance.merge_by_distance_topological(distance);
result_table.push(vector_data_instance);
vector_data_instance
})
.collect(),
}
}
}
result_table
}
#[node_macro::node(category("Vector: Modifier"), path(graphene_core::vector))]
async fn box_warp(_: impl Ctx, vector_data: VectorDataTable, #[expose] rectangle: VectorDataTable) -> VectorDataTable {
@ -584,9 +581,9 @@ async fn box_warp(_: impl Ctx, vector_data: VectorDataTable, #[expose] rectangle
return vector_data;
};
let mut result_table = VectorDataTable::default();
for mut vector_data_instance in vector_data.instance_iter() {
vector_data
.instance_iter()
.map(|mut vector_data_instance| {
let vector_data_transform = vector_data_instance.transform;
let vector_data = vector_data_instance.instance;
@ -649,10 +646,9 @@ async fn box_warp(_: impl Ctx, vector_data: VectorDataTable, #[expose] rectangle
vector_data_instance.instance = result;
vector_data_instance.transform = DAffine2::IDENTITY;
vector_data_instance.source_node_id = None;
result_table.push(vector_data_instance);
}
result_table
vector_data_instance
})
.collect()
}
// Interpolate within a quadrilateral using normalized coordinates (0-1)
@ -679,9 +675,9 @@ async fn auto_tangents(
#[default(true)]
preserve_existing: bool,
) -> VectorDataTable {
let mut result_table = VectorDataTable::default();
for source in source.instance_ref_iter() {
source
.instance_ref_iter()
.map(|source| {
let transform = *source.transform;
let alpha_blending = *source.alpha_blending;
let source_node_id = *source.source_node_id;
@ -779,15 +775,14 @@ async fn auto_tangents(
result.append_subpath(softened_subpath, true);
}
result_table.push(Instance {
Instance {
instance: result,
transform,
alpha_blending,
source_node_id,
});
}
result_table
})
.collect()
}
// TODO: Fix issues and reenable
@ -904,9 +899,9 @@ async fn auto_tangents(
#[node_macro::node(category("Vector: Modifier"), path(graphene_core::vector))]
async fn bounding_box(_: impl Ctx, vector_data: VectorDataTable) -> VectorDataTable {
let mut result_table = VectorDataTable::default();
for mut vector_data_instance in vector_data.instance_iter() {
vector_data
.instance_iter()
.map(|mut vector_data_instance| {
let vector_data = vector_data_instance.instance;
let mut result = vector_data
@ -923,10 +918,9 @@ async fn bounding_box(_: impl Ctx, vector_data: VectorDataTable) -> VectorDataTa
vector_data_instance.instance = result;
vector_data_instance.source_node_id = None;
result_table.push(vector_data_instance);
}
result_table
vector_data_instance
})
.collect()
}
#[node_macro::node(category("Vector: Measure"), path(graphene_core::vector))]
@ -944,17 +938,13 @@ async fn dimensions(_: impl Ctx, vector_data: VectorDataTable) -> DVec2 {
/// This is useful in conjunction with nodes that repeat it, followed by the "Points to Polyline" node to string together a path of the points.
#[node_macro::node(category("Vector"), name("Coordinate to Point"), path(graphene_core::vector))]
async fn position_to_point(_: impl Ctx, coordinate: DVec2) -> VectorDataTable {
let mut result_table = VectorDataTable::default();
let mut point_domain = PointDomain::new();
point_domain.push(PointId::generate(), coordinate);
result_table.push(Instance {
VectorDataTable::new_instance(Instance {
instance: VectorData { point_domain, ..Default::default() },
..Default::default()
});
result_table
})
}
/// Creates a polyline from a series of vector points, replacing any existing segments and regions that may already exist.
@ -988,9 +978,9 @@ async fn points_to_polyline(_: impl Ctx, mut points: VectorDataTable, #[default(
#[node_macro::node(category("Vector: Modifier"), path(graphene_core::vector), properties("offset_path_properties"))]
async fn offset_path(_: impl Ctx, vector_data: VectorDataTable, distance: f64, join: StrokeJoin, #[default(4.)] miter_limit: f64) -> VectorDataTable {
let mut result_table = VectorDataTable::default();
for mut vector_data_instance in vector_data.instance_iter() {
vector_data
.instance_iter()
.map(|mut vector_data_instance| {
let vector_data_transform = vector_data_instance.transform;
let vector_data = vector_data_instance.instance;
@ -1024,17 +1014,16 @@ async fn offset_path(_: impl Ctx, vector_data: VectorDataTable, distance: f64, j
vector_data_instance.instance = result;
vector_data_instance.source_node_id = None;
result_table.push(vector_data_instance);
}
result_table
vector_data_instance
})
.collect()
}
#[node_macro::node(category("Vector: Modifier"), path(graphene_core::vector))]
async fn solidify_stroke(_: impl Ctx, vector_data: VectorDataTable) -> VectorDataTable {
let mut result_table = VectorDataTable::default();
for mut vector_data_instance in vector_data.instance_iter() {
vector_data
.instance_iter()
.map(|mut vector_data_instance| {
let vector_data = vector_data_instance.instance;
let stroke = vector_data.style.stroke().clone().unwrap_or_default();
@ -1080,10 +1069,9 @@ async fn solidify_stroke(_: impl Ctx, vector_data: VectorDataTable) -> VectorDat
vector_data_instance.instance = result;
vector_data_instance.source_node_id = None;
result_table.push(vector_data_instance);
}
result_table
vector_data_instance
})
.collect()
}
#[node_macro::node(category("Vector"), path(graphene_core::vector))]
@ -1154,9 +1142,9 @@ async fn sample_polyline(
adaptive_spacing: bool,
subpath_segment_lengths: Vec<f64>,
) -> VectorDataTable {
let mut result_table = VectorDataTable::default();
for mut vector_data_instance in vector_data.instance_iter() {
vector_data
.instance_iter()
.map(|mut vector_data_instance| {
let mut result = VectorData {
point_domain: Default::default(),
segment_domain: Default::default(),
@ -1204,10 +1192,9 @@ async fn sample_polyline(
}
vector_data_instance.instance = result;
result_table.push(vector_data_instance);
}
result_table
vector_data_instance
})
.collect()
}
/// Splits a path at a given progress from 0 to 1 along the path, creating two new subpaths from the original one (if the path is initially open) or one open subpath (if the path is initially closed).
@ -1404,9 +1391,9 @@ async fn poisson_disk_points(
) -> VectorDataTable {
let mut rng = rand::rngs::StdRng::seed_from_u64(seed.into());
let mut result_table = VectorDataTable::default();
for mut vector_data_instance in vector_data.instance_iter() {
vector_data
.instance_iter()
.map(|mut vector_data_instance| {
let mut result = VectorData::default();
let path_with_bounding_boxes: Vec<_> = vector_data_instance
@ -1435,11 +1422,9 @@ async fn poisson_disk_points(
result.style.set_stroke_transform(DAffine2::IDENTITY);
vector_data_instance.instance = result;
result_table.push(vector_data_instance);
}
result_table
vector_data_instance
})
.collect()
}
#[node_macro::node(category(""), path(graphene_core::vector))]
@ -1462,12 +1447,12 @@ async fn subpath_segment_lengths(_: impl Ctx, vector_data: VectorDataTable) -> V
#[node_macro::node(name("Spline"), category("Vector: Modifier"), path(graphene_core::vector))]
async fn spline(_: impl Ctx, vector_data: VectorDataTable) -> VectorDataTable {
let mut result_table = VectorDataTable::default();
for mut vector_data_instance in vector_data.instance_iter() {
vector_data
.instance_iter()
.filter_map(|mut vector_data_instance| {
// Exit early if there are no points to generate splines from.
if vector_data_instance.instance.point_domain.positions().is_empty() {
continue;
return None;
}
let mut segment_domain = SegmentDomain::default();
@ -1500,10 +1485,9 @@ async fn spline(_: impl Ctx, vector_data: VectorDataTable) -> VectorDataTable {
}
vector_data_instance.instance.segment_domain = segment_domain;
result_table.push(vector_data_instance);
}
result_table
Some(vector_data_instance)
})
.collect()
}
#[node_macro::node(category("Vector: Modifier"), path(graphene_core::vector))]
@ -1515,13 +1499,17 @@ async fn jitter_points(
amount: f64,
seed: SeedValue,
) -> VectorDataTable {
let mut result_table = VectorDataTable::default();
for mut vector_data_instance in vector_data.instance_iter() {
vector_data
.instance_iter()
.map(|mut vector_data_instance| {
let mut rng = rand::rngs::StdRng::seed_from_u64(seed.into());
let vector_data_transform = vector_data_instance.transform;
let inverse_transform = (vector_data_transform.matrix2.determinant() != 0.).then(|| vector_data_transform.inverse()).unwrap_or_default();
let inverse_transform = if vector_data_transform.matrix2.determinant() != 0. {
vector_data_transform.inverse()
} else {
Default::default()
};
let deltas = (0..vector_data_instance.instance.point_domain.positions().len())
.map(|_| {
@ -1560,10 +1548,9 @@ async fn jitter_points(
}
vector_data_instance.instance.style.set_stroke_transform(DAffine2::IDENTITY);
result_table.push(vector_data_instance);
}
result_table
vector_data_instance
})
.collect()
}
#[node_macro::node(category("Vector: Modifier"), path(graphene_core::vector))]
@ -1613,9 +1600,10 @@ async fn morph(_: impl Ctx, source: VectorDataTable, #[expose] target: VectorDat
let time = time.clamp(0., 1.);
let mut result_table = VectorDataTable::default();
for (source_instance, target_instance) in source.instance_iter().zip(target.instance_iter()) {
source
.instance_iter()
.zip(target.instance_iter())
.map(|(source_instance, target_instance)| {
let mut vector_data_instance = VectorData::default();
// Lerp styles
@ -1724,14 +1712,13 @@ async fn morph(_: impl Ctx, source: VectorDataTable, #[expose] target: VectorDat
vector_data_instance.append_bezpath(target_path);
}
result_table.push(Instance {
Instance {
instance: vector_data_instance,
alpha_blending: vector_data_alpha_blending,
..Default::default()
});
}
result_table
})
.collect()
}
fn bevel_algorithm(mut vector_data: VectorData, vector_data_transform: DAffine2, distance: f64) -> VectorData {
@ -1928,7 +1915,11 @@ fn bevel_algorithm(mut vector_data: VectorData, vector_data_transform: DAffine2,
next_bezier = handles_to_segment(start, BezierHandles::Linear, end);
}
let inverse_transform = (vector_data_transform.matrix2.determinant() != 0.).then(|| vector_data_transform.inverse()).unwrap_or_default();
let inverse_transform = if vector_data_transform.matrix2.determinant() != 0. {
vector_data_transform.inverse()
} else {
Default::default()
};
if index == 0 && next_index == 1 {
first_original_length = bezier.perimeter(DEFAULT_ACCURACY);
@ -2010,28 +2001,24 @@ fn bevel_algorithm(mut vector_data: VectorData, vector_data_transform: DAffine2,
#[node_macro::node(category("Vector: Modifier"), path(graphene_core::vector))]
fn bevel(_: impl Ctx, source: VectorDataTable, #[default(10.)] distance: Length) -> VectorDataTable {
let mut result_table = VectorDataTable::default();
for source_instance in source.instance_iter() {
result_table.push(Instance {
source
.instance_iter()
.map(|source_instance| Instance {
instance: bevel_algorithm(source_instance.instance, source_instance.transform, distance),
..source_instance
});
}
result_table
})
.collect()
}
#[node_macro::node(category("Vector: Modifier"), path(graphene_core::vector))]
fn close_path(_: impl Ctx, source: VectorDataTable) -> VectorDataTable {
let mut result_table = VectorDataTable::default();
for mut source_instance in source.instance_iter() {
source
.instance_iter()
.map(|mut source_instance| {
source_instance.instance.close_subpaths();
result_table.push(source_instance);
}
result_table
source_instance
})
.collect()
}
#[node_macro::node(category("Vector: Measure"), path(graphene_core::vector))]
@ -2162,14 +2149,6 @@ mod test {
}
}
fn vector_node_from_instances(data: Vec<Instance<VectorData>>) -> VectorDataTable {
let mut vector_data_table = VectorDataTable::default();
for instance in data {
vector_data_table.push(instance);
}
vector_data_table
}
#[tokio::test]
async fn repeat() {
let direction = DVec2::X * 1.5;
@ -2306,9 +2285,9 @@ mod test {
let bezpath = Rect::new(100., 100., 201., 201.).to_path(DEFAULT_ACCURACY);
let transform = DAffine2::from_scale(DVec2::new(2., 2.));
let instance = create_vector_data_instance(bezpath, transform);
let instances = (0..5).map(|_| instance.clone()).collect::<Vec<Instance<VectorData>>>();
let instances = (0..5).map(|_| instance.clone()).collect::<Instances<VectorData>>();
let length = super::path_length(Footprint::default(), vector_node_from_instances(instances)).await;
let length = super::path_length(Footprint::default(), instances).await;
// 101 (each rectangle edge length) * 4 (rectangle perimeter) * 2 (scale) * 5 (number of rows)
assert_eq!(length, 101. * 4. * 2. * 5.);

View file

@ -80,8 +80,7 @@ fn union<'a>(vector_data: impl DoubleEndedIterator<Item = InstanceRef<'a, Vector
// Reverse vector data so that the result style is the style of the first vector data
let mut vector_data_reversed = vector_data.rev();
let mut result_vector_data_table = VectorDataTable::default();
result_vector_data_table.push(vector_data_reversed.next().map(|x| x.to_instance_cloned()).unwrap_or_default());
let mut result_vector_data_table = VectorDataTable::new_instance(vector_data_reversed.next().map(|x| x.to_instance_cloned()).unwrap_or_default());
let mut first_instance = result_vector_data_table.instance_mut_iter().next().expect("Expected the one instance we just pushed");
// Loop over all vector data and union it with the result
@ -113,8 +112,7 @@ fn union<'a>(vector_data: impl DoubleEndedIterator<Item = InstanceRef<'a, Vector
fn subtract<'a>(vector_data: impl Iterator<Item = InstanceRef<'a, VectorData>>) -> VectorDataTable {
let mut vector_data = vector_data.into_iter();
let mut result_vector_data_table = VectorDataTable::default();
result_vector_data_table.push(vector_data.next().map(|x| x.to_instance_cloned()).unwrap_or_default());
let mut result_vector_data_table = VectorDataTable::new_instance(vector_data.next().map(|x| x.to_instance_cloned()).unwrap_or_default());
let mut first_instance = result_vector_data_table.instance_mut_iter().next().expect("Expected the one instance we just pushed");
let mut next_vector_data = vector_data.next();
@ -145,8 +143,7 @@ fn subtract<'a>(vector_data: impl Iterator<Item = InstanceRef<'a, VectorData>>)
fn intersect<'a>(vector_data: impl DoubleEndedIterator<Item = InstanceRef<'a, VectorData>>) -> VectorDataTable {
let mut vector_data = vector_data.rev();
let mut result_vector_data_table = VectorDataTable::default();
result_vector_data_table.push(vector_data.next().map(|x| x.to_instance_cloned()).unwrap_or_default());
let mut result_vector_data_table = VectorDataTable::new_instance(vector_data.next().map(|x| x.to_instance_cloned()).unwrap_or_default());
let mut first_instance = result_vector_data_table.instance_mut_iter().next().expect("Expected the one instance we just pushed");
let default = Instance::default();
@ -225,17 +222,20 @@ fn difference<'a>(vector_data: impl DoubleEndedIterator<Item = InstanceRef<'a, V
}
fn flatten_vector_data(graphic_group_table: &GraphicGroupTable) -> VectorDataTable {
let mut result_table = VectorDataTable::default();
for element in graphic_group_table.instance_ref_iter() {
graphic_group_table
.instance_ref_iter()
.flat_map(|element| {
match element.instance.clone() {
GraphicElement::VectorData(vector_data) => {
// Apply the parent group's transform to each element of vector data
for mut sub_vector_data in vector_data.instance_iter() {
vector_data
.instance_iter()
.map(|mut sub_vector_data| {
sub_vector_data.transform = *element.transform * sub_vector_data.transform;
result_table.push(sub_vector_data);
}
sub_vector_data
})
.collect::<Vec<_>>()
}
GraphicElement::RasterDataCPU(image) => {
let make_instance = |transform| {
@ -251,9 +251,7 @@ fn flatten_vector_data(graphic_group_table: &GraphicGroupTable) -> VectorDataTab
};
// Apply the parent group's transform to each element of raster data
for instance in image.instance_ref_iter() {
result_table.push(make_instance(*element.transform * *instance.transform));
}
image.instance_ref_iter().map(|instance| make_instance(*element.transform * *instance.transform)).collect::<Vec<_>>()
}
GraphicElement::RasterDataGPU(image) => {
let make_instance = |transform| {
@ -269,9 +267,7 @@ fn flatten_vector_data(graphic_group_table: &GraphicGroupTable) -> VectorDataTab
};
// Apply the parent group's transform to each element of raster data
for instance in image.instance_ref_iter() {
result_table.push(make_instance(*element.transform * *instance.transform));
}
image.instance_ref_iter().map(|instance| make_instance(*element.transform * *instance.transform)).collect::<Vec<_>>()
}
GraphicElement::GraphicGroup(mut graphic_group) => {
// Apply the parent group's transform to each element of inner group
@ -282,14 +278,11 @@ fn flatten_vector_data(graphic_group_table: &GraphicGroupTable) -> VectorDataTab
// Recursively flatten the inner group into vector data
let unioned = boolean_operation_on_vector_data_table(flatten_vector_data(&graphic_group).instance_ref_iter(), BooleanOperation::Union);
for element in unioned.instance_iter() {
result_table.push(element);
unioned.instance_iter().collect::<Vec<_>>()
}
}
}
}
result_table
})
.collect()
}
fn to_path(vector: &VectorData, transform: DAffine2) -> Vec<path_bool::PathSegment> {

View file

@ -8,9 +8,9 @@ use std::cmp::{max, min};
#[node_macro::node(category("Raster: Filter"))]
async fn dehaze(_: impl Ctx, image_frame: RasterDataTable<CPU>, strength: Percentage) -> RasterDataTable<CPU> {
let mut result_table = RasterDataTable::default();
for mut image_frame_instance in image_frame.instance_iter() {
image_frame
.instance_iter()
.map(|mut image_frame_instance| {
let image = image_frame_instance.instance;
// Prepare the image data for processing
let image_data = bytemuck::cast_vec(image.data.clone());
@ -32,10 +32,9 @@ async fn dehaze(_: impl Ctx, image_frame: RasterDataTable<CPU>, strength: Percen
image_frame_instance.instance = Raster::new_cpu(dehazed_image);
image_frame_instance.source_node_id = None;
result_table.push(image_frame_instance);
}
result_table
image_frame_instance
})
.collect()
}
// There is no real point in modifying these values because they do not change the final result all that much.

View file

@ -20,9 +20,9 @@ async fn blur(
/// Opt to incorrectly apply the filter with color calculations in gamma space for compatibility with the results from other software.
gamma: bool,
) -> RasterDataTable<CPU> {
let mut result_table = RasterDataTable::default();
for mut image_instance in image_frame.instance_iter() {
image_frame
.instance_iter()
.map(|mut image_instance| {
let image = image_instance.instance.clone();
// Run blur algorithm
@ -37,10 +37,9 @@ async fn blur(
image_instance.instance = blurred_image;
image_instance.source_node_id = None;
result_table.push(image_instance);
}
result_table
image_instance
})
.collect()
}
// 1D gaussian kernel

View file

@ -31,9 +31,9 @@ impl From<std::io::Error> for Error {
#[node_macro::node(category("Debug: Raster"))]
pub fn sample_image(ctx: impl ExtractFootprint + Clone + Send, image_frame: RasterDataTable<CPU>) -> RasterDataTable<CPU> {
let mut result_table = RasterDataTable::default();
for mut image_frame_instance in image_frame.instance_iter() {
image_frame
.instance_iter()
.filter_map(|mut image_frame_instance| {
let image_frame_transform = image_frame_instance.transform;
let image = image_frame_instance.instance;
@ -50,7 +50,7 @@ pub fn sample_image(ctx: impl ExtractFootprint + Clone + Send, image_frame: Rast
// If the image would not be visible, add nothing.
if size.x <= 0. || size.y <= 0. {
continue;
return None;
}
let image_buffer = ::image::Rgba32FImage::from_raw(image.width, image.height, data).expect("Failed to convert internal image format into image-rs data type.");
@ -90,10 +90,9 @@ pub fn sample_image(ctx: impl ExtractFootprint + Clone + Send, image_frame: Rast
image_frame_instance.transform = new_transform;
image_frame_instance.source_node_id = None;
image_frame_instance.instance = Raster::new_cpu(image);
result_table.push(image_frame_instance)
}
result_table
Some(image_frame_instance)
})
.collect()
}
#[node_macro::node(category("Raster: Channels"))]
@ -105,15 +104,16 @@ pub fn combine_channels(
#[expose] blue: RasterDataTable<CPU>,
#[expose] alpha: RasterDataTable<CPU>,
) -> RasterDataTable<CPU> {
let mut result_table = RasterDataTable::default();
let max_len = red.len().max(green.len()).max(blue.len()).max(alpha.len());
let red = red.instance_iter().map(Some).chain(std::iter::repeat(None)).take(max_len);
let green = green.instance_iter().map(Some).chain(std::iter::repeat(None)).take(max_len);
let blue = blue.instance_iter().map(Some).chain(std::iter::repeat(None)).take(max_len);
let alpha = alpha.instance_iter().map(Some).chain(std::iter::repeat(None)).take(max_len);
for (((red, green), blue), alpha) in red.zip(green).zip(blue).zip(alpha) {
red.zip(green)
.zip(blue)
.zip(alpha)
.filter_map(|(((red, green), blue), alpha)| {
// Turn any default zero-sized image instances into None
let red = red.filter(|i| i.instance.width > 0 && i.instance.height > 0);
let green = green.filter(|i| i.instance.width > 0 && i.instance.height > 0);
@ -122,7 +122,7 @@ pub fn combine_channels(
// Get this instance's transform and alpha blending mode from the first non-empty channel
let Some((transform, alpha_blending)) = [&red, &green, &blue, &alpha].iter().find_map(|i| i.as_ref()).map(|i| (i.transform, i.alpha_blending)) else {
continue;
return None;
};
// Get the common width and height of the channels, which must have equal dimensions
@ -138,9 +138,11 @@ pub fn combine_channels(
.flatten()
.any(|&(x, y)| channel_dimensions.iter().flatten().any(|&(other_x, other_y)| x != other_x || y != other_y))
{
continue;
return None;
}
let Some(&(width, height)) = channel_dimensions.iter().flatten().next() else { continue };
let Some(&(width, height)) = channel_dimensions.iter().flatten().next() else {
return None;
};
// Create a new image for this instance output
let mut image = Image::new(width, height, Color::TRANSPARENT);
@ -173,16 +175,14 @@ pub fn combine_channels(
}
}
// Add this instance to the result table
result_table.push(Instance {
Some(Instance {
instance: Raster::new_cpu(image),
transform,
alpha_blending,
source_node_id: None,
});
}
result_table
})
})
.collect()
}
#[node_macro::node(category("Raster"))]
@ -201,14 +201,14 @@ pub fn mask(
};
let stencil_size = DVec2::new(stencil_instance.instance.width as f64, stencil_instance.instance.height as f64);
let mut result_table = RasterDataTable::default();
for mut image_instance in image.instance_iter() {
image
.instance_iter()
.filter_map(|mut image_instance| {
let image_size = DVec2::new(image_instance.instance.width as f64, image_instance.instance.height as f64);
let mask_size = stencil_instance.transform.decompose_scale();
if mask_size == DVec2::ZERO {
continue;
return None;
}
// Transforms a point from the background image to the foreground image
@ -229,31 +229,26 @@ pub fn mask(
}
}
result_table.push(image_instance);
}
result_table
Some(image_instance)
})
.collect()
}
#[node_macro::node(category(""))]
pub fn extend_image_to_bounds(_: impl Ctx, image: RasterDataTable<CPU>, bounds: DAffine2) -> RasterDataTable<CPU> {
let mut result_table = RasterDataTable::default();
for mut image_instance in image.instance_iter() {
image
.instance_iter()
.map(|mut image_instance| {
let image_aabb = Bbox::unit().affine_transform(image_instance.transform).to_axis_aligned_bbox();
let bounds_aabb = Bbox::unit().affine_transform(bounds.transform()).to_axis_aligned_bbox();
if image_aabb.contains(bounds_aabb.start) && image_aabb.contains(bounds_aabb.end) {
result_table.push(image_instance);
continue;
return image_instance;
}
let image_data = &image_instance.instance.data;
let (image_width, image_height) = (image_instance.instance.width, image_instance.instance.height);
if image_width == 0 || image_height == 0 {
for image_instance in empty_image((), bounds, Color::TRANSPARENT).instance_iter() {
result_table.push(image_instance);
}
continue;
return empty_image((), bounds, Color::TRANSPARENT).instance_iter().next().unwrap();
}
let orig_image_scale = DVec2::new(image_width as f64, image_height as f64);
@ -282,10 +277,9 @@ pub fn extend_image_to_bounds(_: impl Ctx, image: RasterDataTable<CPU>, bounds:
image_instance.instance = Raster::new_cpu(new_image);
image_instance.transform = new_texture_to_layer_space;
image_instance.source_node_id = None;
result_table.push(image_instance);
}
result_table
image_instance
})
.collect()
}
#[node_macro::node(category("Debug: Raster"))]
@ -392,14 +386,11 @@ pub fn noise_pattern(
}
}
let mut result = RasterDataTable::default();
result.push(Instance {
return RasterDataTable::new_instance(Instance {
instance: Raster::new_cpu(image),
transform: DAffine2::from_translation(offset) * DAffine2::from_scale(size),
..Default::default()
});
return result;
}
};
noise.set_noise_type(Some(noise_type));
@ -457,14 +448,11 @@ pub fn noise_pattern(
}
}
let mut result = RasterDataTable::default();
result.push(Instance {
RasterDataTable::new_instance(Instance {
instance: Raster::new_cpu(image),
transform: DAffine2::from_translation(offset) * DAffine2::from_scale(size),
..Default::default()
});
result
})
}
#[node_macro::node(category("Raster: Pattern"))]
@ -502,20 +490,16 @@ pub fn mandelbrot(ctx: impl ExtractFootprint + Send) -> RasterDataTable<CPU> {
}
}
let image = Image {
RasterDataTable::new_instance(Instance {
instance: Raster::new_cpu(Image {
width,
height,
data,
..Default::default()
};
let mut result = RasterDataTable::default();
result.push(Instance {
instance: Raster::new_cpu(image),
}),
transform: DAffine2::from_translation(offset) * DAffine2::from_scale(size),
..Default::default()
});
result
})
}
#[inline(always)]

View file

@ -292,15 +292,12 @@ where
let rasterized = context.get_image_data(0., 0., resolution.x as f64, resolution.y as f64).unwrap();
let mut result = RasterDataTable::default();
let image = Image::from_image_data(&rasterized.data().0, resolution.x as u32, resolution.y as u32);
result.push(Instance {
RasterDataTable::new_instance(Instance {
instance: Raster::new_cpu(image),
transform: footprint.transform,
..Default::default()
});
result
})
}
#[node_macro::node(category(""))]

View file

@ -38,10 +38,10 @@ impl MaskType {
}
fn write_to_defs(self, svg_defs: &mut String, uuid: u64, svg_string: String) {
let id = format!("mask-{}", uuid);
let id = format!("mask-{uuid}");
match self {
Self::Clip => write!(svg_defs, r##"<clipPath id="{id}">{}</clipPath>"##, svg_string).unwrap(),
Self::Mask => write!(svg_defs, r##"<mask id="{id}" mask-type="alpha">{}</mask>"##, svg_string).unwrap(),
Self::Clip => write!(svg_defs, r##"<clipPath id="{id}">{svg_string}</clipPath>"##).unwrap(),
Self::Mask => write!(svg_defs, r##"<mask id="{id}" mask-type="alpha">{svg_string}</mask>"##).unwrap(),
}
}
}
@ -89,9 +89,9 @@ impl SvgRender {
.unwrap_or_default();
let matrix = format_transform_matrix(transform);
let transform = if matrix.is_empty() { String::new() } else { format!(r#" transform="{}""#, matrix) };
let transform = if matrix.is_empty() { String::new() } else { format!(r#" transform="{matrix}""#) };
let svg_header = format!(r#"<svg xmlns="http://www.w3.org/2000/svg" {}><defs>{defs}</defs><g{transform}>"#, view_box);
let svg_header = format!(r#"<svg xmlns="http://www.w3.org/2000/svg" {view_box}><defs>{defs}</defs><g{transform}>"#);
self.svg.insert(0, svg_header.into());
self.svg.push("</g></svg>".into());
}
@ -267,7 +267,7 @@ impl GraphicElementRendered for GraphicGroupTable {
mask_state = None;
}
let id = format!("mask-{}", uuid);
let id = format!("mask-{uuid}");
let selector = format!("url(#{id})");
attributes.push(mask_type.to_attribute(), selector);
@ -444,18 +444,18 @@ impl GraphicElementRendered for VectorDataTable {
let can_use_order = !instance.instance.style.fill().is_none() && mask_type == MaskType::Mask;
if !can_use_order {
let id = format!("alignment-{}", generate_uuid());
let mut vector_row = VectorDataTable::default();
let mut fill_instance = instance.instance.clone();
let mut fill_instance = instance.instance.clone();
fill_instance.style.clear_stroke();
fill_instance.style.set_fill(Fill::solid(Color::BLACK));
vector_row.push(Instance {
let vector_row = VectorDataTable::new_instance(Instance {
instance: fill_instance,
alpha_blending: *instance.alpha_blending,
transform: *instance.transform,
source_node_id: None,
});
push_id = Some((id, mask_type, vector_row));
}
}
@ -477,7 +477,7 @@ impl GraphicElementRendered for VectorDataTable {
let (x, y) = quad.top_left().into();
let (width, height) = (quad.bottom_right() - quad.top_left()).into();
write!(defs, r##"{}"##, svg.svg_defs).unwrap();
let rect = format!(r##"<rect x="{}" y="{}" width="{width}" height="{height}" fill="white" />"##, x, y);
let rect = format!(r##"<rect x="{x}" y="{y}" width="{width}" height="{height}" fill="white" />"##);
match mask_type {
MaskType::Clip => write!(defs, r##"<clipPath id="{id}">{}</clipPath>"##, svg.svg.to_svg_string()).unwrap(),
MaskType::Mask => write!(defs, r##"<mask id="{id}">{}{}</mask>"##, rect, svg.svg.to_svg_string()).unwrap(),
@ -564,13 +564,11 @@ impl GraphicElementRendered for VectorDataTable {
.stroke()
.is_some_and(|stroke| stroke.align == StrokeAlign::Outside && !instance.instance.style.fill().is_none());
if can_draw_aligned_stroke && !reorder_for_outside {
let mut vector_data = VectorDataTable::default();
let mut fill_instance = instance.instance.clone();
fill_instance.style.clear_stroke();
fill_instance.style.set_fill(Fill::solid(Color::BLACK));
vector_data.push(Instance {
let vector_data = VectorDataTable::new_instance(Instance {
instance: fill_instance,
alpha_blending: *instance.alpha_blending,
transform: *instance.transform,
@ -639,7 +637,11 @@ impl GraphicElementRendered for VectorDataTable {
let bounds = instance.instance.nonzero_bounding_box();
let bound_transform = DAffine2::from_scale_angle_translation(bounds[1] - bounds[0], 0., bounds[0]);
let inverse_parent_transform = (parent_transform.matrix2.determinant() != 0.).then(|| parent_transform.inverse()).unwrap_or_default();
let inverse_parent_transform = if parent_transform.matrix2.determinant() != 0. {
parent_transform.inverse()
} else {
Default::default()
};
let mod_points = inverse_parent_transform * multiplied_transform * bound_transform;
let start = mod_points.transform_point2(gradient.start);
@ -666,7 +668,11 @@ impl GraphicElementRendered for VectorDataTable {
});
// Vello does `element_transform * brush_transform` internally. We don't want element_transform to have any impact so we need to left multiply by the inverse.
// This makes the final internal brush transform equal to `parent_transform`, allowing you to stretch a gradient by transforming the parent folder.
let inverse_element_transform = (element_transform.matrix2.determinant() != 0.).then(|| element_transform.inverse()).unwrap_or_default();
let inverse_element_transform = if element_transform.matrix2.determinant() != 0. {
element_transform.inverse()
} else {
Default::default()
};
let brush_transform = kurbo::Affine::new((inverse_element_transform * parent_transform).to_cols_array());
scene.fill(peniko::Fill::NonZero, kurbo::Affine::new(element_transform.to_cols_array()), &fill, Some(brush_transform), &path);
}

View file

@ -2,7 +2,7 @@
name = "node-macro"
publish = false
version = "0.0.0"
rust-version = "1.85"
rust-version = "1.88"
authors = ["Graphite Authors <contact@graphite.rs>"]
edition = "2024"
readme = "../../README.md"
@ -26,4 +26,3 @@ proc-macro-error2 = "2"
[dev-dependencies]
graphene-core = { workspace = true }

View file

@ -2,7 +2,7 @@
name = "graphite-proc-macros"
publish = false
version = "0.0.0"
rust-version = "1.85"
rust-version = "1.88"
authors = ["Graphite Authors <contact@graphite.rs>"]
edition = "2024"
readme = "../README.md"