mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 14:18:04 +08:00
Replace the IntoPaint trait with direct Graphic-typed node inputs (#4442)
* Let the Fill and Stroke paint inputs take Item<Graphic>, replacing the IntoPaint trait * Rename the FIll node's "fill" input to "paint" * Let the graphic-consuming nodes take List<Graphic> directly, relying on the embedding adapters * Remove outdated todo comments * Re-save the demo art
This commit is contained in:
@@ -376,7 +376,8 @@ fn position_value_raises_into_the_into_group_reducer() {
|
||||
assert_eq!(anchors.len(), 1, "The single position should group as one anchor point");
|
||||
}
|
||||
|
||||
// The 'Colors to Gradient' node turns an entire `List<Color>` wire into one gradient with those colors as its stops
|
||||
// The 'Colors to Gradient' node turns an entire color wire into one gradient with those colors as its stops,
|
||||
// reaching its `List<Graphic>` connector through the embedding adapter
|
||||
#[test]
|
||||
fn color_list_wraps_through_the_colors_to_gradient_node() {
|
||||
let color_node = ProtoNode::value(ConstructionArgs::Value(TaggedValue::Color(graphene_std::Color::WHITE).into()), vec![NodeId(0)]);
|
||||
@@ -384,20 +385,23 @@ fn color_list_wraps_through_the_colors_to_gradient_node() {
|
||||
let mut raise_node = ProtoNode::value(ConstructionArgs::Nodes(vec![NodeId(0)]), vec![NodeId(1)]);
|
||||
raise_node.identifier = ProtoNodeIdentifier::new("graphene_core::ops::ItemToListNode<Color>");
|
||||
|
||||
let mut colors_to_gradient_node = ProtoNode::value(ConstructionArgs::Nodes(vec![NodeId(1)]), vec![NodeId(2)]);
|
||||
let mut graphic_adapter = ProtoNode::value(ConstructionArgs::Nodes(vec![NodeId(1)]), vec![NodeId(2)]);
|
||||
graphic_adapter.identifier = ProtoNodeIdentifier::new("input_adapter<Graphic>");
|
||||
|
||||
let mut colors_to_gradient_node = ProtoNode::value(ConstructionArgs::Nodes(vec![NodeId(2)]), vec![NodeId(3)]);
|
||||
colors_to_gradient_node.identifier = ProtoNodeIdentifier::new("graphic_nodes::graphic::ColorsToGradientNode");
|
||||
|
||||
let network = ProtoNetwork {
|
||||
inputs: vec![],
|
||||
output: NodeId(2),
|
||||
nodes: vec![(NodeId(0), color_node), (NodeId(1), raise_node), (NodeId(2), colors_to_gradient_node)],
|
||||
output: NodeId(3),
|
||||
nodes: vec![(NodeId(0), color_node), (NodeId(1), raise_node), (NodeId(2), graphic_adapter), (NodeId(3), colors_to_gradient_node)],
|
||||
};
|
||||
let mut typing_context = TypingContext::new(&crate::node_registry::NODE_REGISTRY);
|
||||
typing_context.update(&network).expect("A List<Color> wire should resolve the node's List<Color> implementation");
|
||||
typing_context.update(&network).expect("A List<Color> wire should embed into the node's List<Graphic> connector");
|
||||
let tree = futures::executor::block_on(BorrowTree::new(network, &typing_context)).expect("The node constructor should instantiate");
|
||||
|
||||
let context: Context = None;
|
||||
let result: Option<Item<graphene_std::vector::Gradient>> = futures::executor::block_on(tree.eval(NodeId(2), context));
|
||||
let result: Option<Item<graphene_std::vector::Gradient>> = futures::executor::block_on(tree.eval(NodeId(3), context));
|
||||
let gradient = result.expect("The color list should arrive wrapped as a gradient");
|
||||
assert_eq!(gradient.element().len(), 1, "The single color should become the gradient's one stop");
|
||||
}
|
||||
|
||||
@@ -3,12 +3,9 @@
|
||||
//! while cover-specific data rides the inner `Item<Cover>`, reusing `ATTR_TRANSFORM` for the stroke-authoring space.
|
||||
|
||||
use crate::graphic::Graphic;
|
||||
use core_types::Color;
|
||||
use core_types::graphene_hash::CacheHash;
|
||||
use core_types::list::{ATTR_ALIGN, ATTR_APPEARANCE, ATTR_CAP, ATTR_DASH_OFFSET, ATTR_DASH_PATTERN, ATTR_JOIN, ATTR_JOIN_MITER_LIMIT, ATTR_PAINT, ATTR_TRANSFORM, ATTR_WEIGHT, Item, List};
|
||||
use raster_types::{CPU, GPU, Raster};
|
||||
use vector_types::vector::style::{DashPattern, Stroke};
|
||||
use vector_types::{Gradient, Vector};
|
||||
|
||||
/// The geometry-to-region operator a coverage applies before painting:
|
||||
/// the interior of the geometry (fill) or the region swept along its outline (stroke).
|
||||
@@ -275,95 +272,10 @@ pub fn stamp_coverage<T>(item: &mut Item<T>, coverage: Coverage, paint: Graphic,
|
||||
item.attribute_mut_or_insert_default::<Appearance>(ATTR_APPEARANCE).replace_or_insert(coverage, paint, placement);
|
||||
}
|
||||
|
||||
// ================
|
||||
// TRAIT: IntoPaint
|
||||
// ================
|
||||
|
||||
/// Converts the types accepted by a paint input into the canonical `Graphic` stored in the `ATTR_PAINT` attribute.
|
||||
/// `List<Graphic>` deliberately has no impl: a multi-element paint is a type error.
|
||||
pub trait IntoPaint: Clone + Send + Sync + Default + std::fmt::Debug + PartialEq + CacheHash + 'static {
|
||||
fn into_paint(self) -> Graphic;
|
||||
}
|
||||
|
||||
impl IntoPaint for Item<Graphic> {
|
||||
fn into_paint(self) -> Graphic {
|
||||
// Wrapping to keep the record's attributes would nest the paint as a group, changing how it renders
|
||||
self.into_element()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPaint for Item<Vector> {
|
||||
fn into_paint(self) -> Graphic {
|
||||
Graphic::VectorList(List::new_from_item(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPaint for Item<Raster<CPU>> {
|
||||
fn into_paint(self) -> Graphic {
|
||||
Graphic::RasterCPUList(List::new_from_item(self))
|
||||
}
|
||||
}
|
||||
|
||||
// No Item<Raster<GPU>> impl: GPU rasters have no Default, which the trait bounds require of the element
|
||||
|
||||
impl IntoPaint for Item<Color> {
|
||||
fn into_paint(self) -> Graphic {
|
||||
Graphic::ColorList(List::new_from_item(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPaint for Item<Gradient> {
|
||||
fn into_paint(self) -> Graphic {
|
||||
Graphic::GradientList(List::new_from_item(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPaint for Item<String> {
|
||||
fn into_paint(self) -> Graphic {
|
||||
Graphic::TextList(List::new_from_item(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPaint for List<Vector> {
|
||||
fn into_paint(self) -> Graphic {
|
||||
Graphic::VectorList(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPaint for List<Raster<CPU>> {
|
||||
fn into_paint(self) -> Graphic {
|
||||
Graphic::RasterCPUList(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPaint for List<Raster<GPU>> {
|
||||
fn into_paint(self) -> Graphic {
|
||||
Graphic::RasterGPUList(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPaint for List<Color> {
|
||||
fn into_paint(self) -> Graphic {
|
||||
Graphic::ColorList(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPaint for List<Gradient> {
|
||||
fn into_paint(self) -> Graphic {
|
||||
Graphic::GradientList(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPaint for List<String> {
|
||||
fn into_paint(self) -> Graphic {
|
||||
Graphic::TextList(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use core_types::list::ATTR_POSITION;
|
||||
use core_types::Color;
|
||||
use glam::{DAffine2, DVec2};
|
||||
use vector_types::vector::style::{StrokeAlign, StrokeCap, StrokeJoin};
|
||||
|
||||
@@ -470,24 +382,4 @@ mod tests {
|
||||
appearance.replace_or_insert(Coverage::new_fill(), solid_paint(Color::RED), CoverPlacement::Above);
|
||||
assert!(appearance.has_painted_cover(Cover::Fill));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_paint_becomes_one_graphic_holding_every_element() {
|
||||
let mut colors = List::new_from_element(Color::RED);
|
||||
colors.push(Item::new_from_element(Color::BLUE));
|
||||
|
||||
let paint = colors.into_paint();
|
||||
let Graphic::ColorList(inner) = &paint else { panic!("expected a color graphic") };
|
||||
assert_eq!(inner.len(), 2, "a list paint is one graphic holding all its elements");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn item_paint_keeps_its_attributes_on_the_inner_row() {
|
||||
let color = Item::new_from_element(Color::RED).with_attribute(ATTR_POSITION, 0.25_f64);
|
||||
|
||||
let paint = color.into_paint();
|
||||
let Graphic::ColorList(inner) = &paint else { panic!("expected a color graphic") };
|
||||
assert_eq!(inner.len(), 1);
|
||||
assert_eq!(inner.attribute::<f64>(ATTR_POSITION, 0), Some(&0.25));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -950,12 +950,8 @@ mod tests {
|
||||
assert!(!graphic_list.attribute_keys().any(|key| key == ATTR_EDITOR_LAYER_PATH));
|
||||
}
|
||||
|
||||
// Round-tripping through that wrapper must not collapse the items' distinct stamps onto item 0's
|
||||
#[test]
|
||||
fn round_trip_through_the_wrapper_preserves_per_item_layer_paths() {
|
||||
let flattened: List<Vector> = vector_list_stamped_with_layers([7, 9]).into_flattened_list();
|
||||
|
||||
let layers = (0..flattened.len())
|
||||
fn layer_stamps(flattened: &List<Vector>) -> Vec<Option<NodeId>> {
|
||||
(0..flattened.len())
|
||||
.map(|index| {
|
||||
flattened
|
||||
.attribute_cloned_or_default::<NodeIdPath>(ATTR_EDITOR_LAYER_PATH, index)
|
||||
@@ -964,9 +960,25 @@ mod tests {
|
||||
.next_back()
|
||||
.copied()
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
.collect()
|
||||
}
|
||||
|
||||
assert_eq!(layers, [Some(NodeId(7)), Some(NodeId(9))]);
|
||||
// Round-tripping through that wrapper must not collapse the items' distinct stamps onto item 0's
|
||||
#[test]
|
||||
fn round_trip_through_the_wrapper_preserves_per_item_layer_paths() {
|
||||
let flattened: List<Vector> = vector_list_stamped_with_layers([7, 9]).into_flattened_list();
|
||||
|
||||
assert_eq!(layer_stamps(&flattened), [Some(NodeId(7)), Some(NodeId(9))]);
|
||||
}
|
||||
|
||||
// The embedding adapter reaches the same flattened stamps as the wrapper, each item carrying its own inside its variant
|
||||
#[test]
|
||||
fn embedding_each_item_preserves_per_item_layer_paths() {
|
||||
let embedded: List<Graphic> = vector_list_stamped_with_layers([7, 9]).into_iter().map(|item| Item::new_from_element(Graphic::from(item))).collect();
|
||||
|
||||
let flattened: List<Vector> = embedded.into_flattened_list();
|
||||
|
||||
assert_eq!(layer_stamps(&flattened), [Some(NodeId(7)), Some(NodeId(9))]);
|
||||
}
|
||||
|
||||
// Flattening must not invent attributes that neither the parent graphic nor the child carried
|
||||
|
||||
@@ -8,7 +8,7 @@ pub use raster_types;
|
||||
pub use vector_types;
|
||||
|
||||
// Re-export commonly used types at the crate root
|
||||
pub use appearance::{Appearance, Cover, CoverPlacement, Coverage, FillAndStroke, IntoPaint, stamp_coverage};
|
||||
pub use appearance::{Appearance, Cover, CoverPlacement, Coverage, FillAndStroke, stamp_coverage};
|
||||
pub use artboard::Artboard;
|
||||
pub use graphic::{Graphic, IntoGraphicList, TryFromGraphic, Vector};
|
||||
|
||||
|
||||
@@ -1002,9 +1002,8 @@ pub async fn flatten_graphic(_: impl Ctx, content: List<Graphic>, fully_flatten:
|
||||
|
||||
/// Converts a `Graphic[]` into a `Vector[]` by deeply flattening any vector content it contains, and discarding any non-vector content.
|
||||
#[node_macro::node(category("Vector"))]
|
||||
pub async fn flatten_vector<T: IntoGraphicList>(_: impl Ctx, #[implementations(List<Graphic>, List<Vector>)] content: T) -> List<Vector> {
|
||||
let graphic_list = content.into_graphic_list();
|
||||
let mut output: List<Vector> = graphic_list.clone().into_flattened_list();
|
||||
pub async fn flatten_vector(_: impl Ctx, content: List<Graphic>) -> List<Vector> {
|
||||
let mut output: List<Vector> = content.clone().into_flattened_list();
|
||||
|
||||
// TODO: Replace this snapshot hack with per-layer metadata driven by each layer's Monitor node.
|
||||
// TODO: Flattening here erases the upstream `List<Graphic>` hierarchy that editor metadata collection walks
|
||||
@@ -1014,20 +1013,20 @@ pub async fn flatten_vector<T: IntoGraphicList>(_: impl Ctx, #[implementations(L
|
||||
// TODO: The cleaner fix is to drive each layer's metadata from its own Monitor's captured `(Context, List<Graphic>)`,
|
||||
// TODO: at which point this attribute (and the equivalents in Boolean Operation, Solidify Stroke, Combine Paths,
|
||||
// TODO: Morph, Rasterize) become unnecessary.
|
||||
if !output.is_empty() && !is_lone_anonymous_leaf(&graphic_list) {
|
||||
if !output.is_empty() && !is_lone_anonymous_leaf(&content) {
|
||||
// Item 0 carries a composed transform inherited from the flattened input, but the merged_layers
|
||||
// already holds the original transforms; pre-compensate by item 0's inverse so the renderer's
|
||||
// `upstream_footprint *= item_0_transform` recursion cancels out and leaves the originals intact.
|
||||
let mut graphic_list = graphic_list;
|
||||
let mut merged_layers = content;
|
||||
let item_0_transform: DAffine2 = output.attribute_cloned_or_default(ATTR_TRANSFORM, 0);
|
||||
if item_0_transform.matrix2.determinant().abs() > f64::EPSILON {
|
||||
let inverse = item_0_transform.inverse();
|
||||
for transform in graphic_list.iter_attribute_values_mut_or_default::<DAffine2>(ATTR_TRANSFORM) {
|
||||
for transform in merged_layers.iter_attribute_values_mut_or_default::<DAffine2>(ATTR_TRANSFORM) {
|
||||
*transform = inverse * *transform;
|
||||
}
|
||||
}
|
||||
|
||||
output.set_attribute(ATTR_EDITOR_MERGED_LAYERS, 0, graphic_list);
|
||||
output.set_attribute(ATTR_EDITOR_MERGED_LAYERS, 0, merged_layers);
|
||||
}
|
||||
|
||||
output
|
||||
@@ -1035,25 +1034,25 @@ pub async fn flatten_vector<T: IntoGraphicList>(_: impl Ctx, #[implementations(L
|
||||
|
||||
/// Converts a `Graphic[]` into a `Raster[]` by deeply flattening any raster content it contains, and discarding any non-raster content.
|
||||
#[node_macro::node(category("Raster"))]
|
||||
pub async fn flatten_raster<T: IntoGraphicList>(_: impl Ctx, #[implementations(List<Graphic>, List<Raster<CPU>>)] content: T) -> List<Raster<CPU>> {
|
||||
pub async fn flatten_raster(_: impl Ctx, content: List<Graphic>) -> List<Raster<CPU>> {
|
||||
content.into_flattened_list()
|
||||
}
|
||||
|
||||
/// Converts a `Graphic[]` into a `Color[]` by deeply flattening any color content it contains, and discarding any non-color content.
|
||||
#[node_macro::node(category("General"))]
|
||||
pub async fn flatten_color<T: IntoGraphicList>(_: impl Ctx, #[implementations(List<Graphic>, List<Color>)] content: T) -> List<Color> {
|
||||
pub async fn flatten_color(_: impl Ctx, content: List<Graphic>) -> List<Color> {
|
||||
content.into_flattened_list()
|
||||
}
|
||||
|
||||
/// Converts a `Graphic[]` into a `Gradient[]` by deeply flattening any gradient content it contains, and discarding any non-gradient content.
|
||||
#[node_macro::node(category("General"))]
|
||||
pub async fn flatten_gradient<T: IntoGraphicList>(_: impl Ctx, #[implementations(List<Graphic>, List<Gradient>)] content: T) -> List<Gradient> {
|
||||
pub async fn flatten_gradient(_: impl Ctx, content: List<Graphic>) -> List<Gradient> {
|
||||
content.into_flattened_list()
|
||||
}
|
||||
|
||||
/// Constructs a gradient from a `Color[]`, where each color becomes a gradient stop. A `position` attribute on the colors places their stops along the ramp and a `midpoint` attribute skews each transition, while colors carrying neither are distributed evenly across the 0 to 1 range.
|
||||
#[node_macro::node(category("Gradient"), name("Colors to Gradient"))]
|
||||
fn colors_to_gradient<T: IntoGraphicList>(_: impl Ctx, #[implementations(List<Graphic>, List<Color>)] colors: T) -> Item<Gradient> {
|
||||
fn colors_to_gradient(_: impl Ctx, colors: List<Graphic>) -> Item<Gradient> {
|
||||
Item::new_from_element(Gradient::from(colors.into_flattened_list::<Color>()))
|
||||
}
|
||||
|
||||
@@ -1071,6 +1070,11 @@ mod test {
|
||||
elements.into_iter().map(Item::new_from_element).collect()
|
||||
}
|
||||
|
||||
/// Stands in for the embedding adapter a compiled graph inserts ahead of a `List<Graphic>` connector.
|
||||
fn embed_colors(colors: List<Color>) -> List<Graphic> {
|
||||
colors.into_iter().map(|item| Item::new_from_element(Graphic::from(item))).collect()
|
||||
}
|
||||
|
||||
fn elements<T: Clone>(list: &List<T>) -> Vec<T> {
|
||||
list.iter_element_values().cloned().collect()
|
||||
}
|
||||
@@ -1196,7 +1200,7 @@ mod test {
|
||||
let colors = gradient_to_colors((), Item::new_from_element(gradient.clone()));
|
||||
assert_eq!(elements(&colors), [Color::RED, Color::GREEN, Color::BLUE], "every stop should come out as its color");
|
||||
|
||||
let restored = colors_to_gradient((), colors);
|
||||
let restored = colors_to_gradient((), embed_colors(colors));
|
||||
assert_eq!(
|
||||
restored.element(),
|
||||
&gradient,
|
||||
@@ -1209,7 +1213,7 @@ mod test {
|
||||
let gradient = Gradient::from(vec![Color::RED, Color::GREEN, Color::BLUE]);
|
||||
assert!(!gradient.has_position_attribute(), "even spacing is stored as the attribute's absence");
|
||||
|
||||
let restored = colors_to_gradient((), gradient_to_colors((), Item::new_from_element(gradient.clone())));
|
||||
let restored = colors_to_gradient((), embed_colors(gradient_to_colors((), Item::new_from_element(gradient.clone()))));
|
||||
assert_eq!(restored.element(), &gradient, "a default ramp should round trip without gaining attributes it never had");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ pub use graphene_application_io as application_io;
|
||||
pub use graphene_core;
|
||||
pub use graphene_core::debug;
|
||||
pub use graphic_nodes;
|
||||
pub use graphic_types::{Appearance, Artboard, Cover, CoverPlacement, Coverage, Graphic, IntoPaint, Vector, stamp_coverage};
|
||||
pub use graphic_types::{Appearance, Artboard, Cover, CoverPlacement, Coverage, Graphic, Vector, stamp_coverage};
|
||||
pub use math_nodes;
|
||||
pub use path_bool_nodes;
|
||||
pub use raster_nodes;
|
||||
|
||||
@@ -11,7 +11,7 @@ use core_types::math::bbox::Bbox;
|
||||
use core_types::ops::Convert;
|
||||
use core_types::transform::Footprint;
|
||||
#[cfg(target_family = "wasm")]
|
||||
use core_types::{ATTR_EDITOR_MERGED_LAYERS, ATTR_TRANSFORM, WasmNotSend};
|
||||
use core_types::{ATTR_EDITOR_MERGED_LAYERS, ATTR_TRANSFORM};
|
||||
use core_types::{Color, Ctx};
|
||||
pub use graph_craft::application_io::resource::{Resource, ResourceHash};
|
||||
pub use graph_craft::application_io::*;
|
||||
@@ -20,15 +20,9 @@ pub use graph_craft::document::value::RenderOutputType;
|
||||
pub use graphene_canvas_utils as canvas_utils;
|
||||
#[cfg(target_family = "wasm")]
|
||||
use graphic_types::Graphic;
|
||||
#[cfg(target_family = "wasm")]
|
||||
use graphic_types::IntoGraphicList;
|
||||
#[cfg(target_family = "wasm")]
|
||||
use graphic_types::Vector;
|
||||
use graphic_types::raster_types::Image;
|
||||
use graphic_types::raster_types::{CPU, GPU, Raster};
|
||||
#[cfg(target_family = "wasm")]
|
||||
use graphic_types::vector_types::gradient::Gradient;
|
||||
#[cfg(target_family = "wasm")]
|
||||
use rendering::{Render, RenderParams, RenderSvgSegmentList, SvgRender};
|
||||
|
||||
fn parse_headers(headers: &str) -> reqwest::header::HeaderMap {
|
||||
@@ -202,22 +196,7 @@ async fn create_canvas(_: impl Ctx) -> Item<CanvasHandle> {
|
||||
/// Renders a view of the input graphic within an area defined by the *Footprint*.
|
||||
#[cfg(target_family = "wasm")]
|
||||
#[node_macro::node(category(""))]
|
||||
async fn rasterize<T: WasmNotSend + Clone + 'n>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
List<Vector>,
|
||||
List<Raster<CPU>>,
|
||||
List<Graphic>,
|
||||
List<Color>,
|
||||
List<Gradient>,
|
||||
)]
|
||||
data: List<T>,
|
||||
footprint: Item<Footprint>,
|
||||
canvas: Item<CanvasHandle>,
|
||||
) -> List<Raster<CPU>>
|
||||
where
|
||||
List<T>: Render + Clone + graphic_types::IntoGraphicList,
|
||||
{
|
||||
async fn rasterize(_: impl Ctx, data: List<Graphic>, footprint: Item<Footprint>, canvas: Item<CanvasHandle>) -> List<Raster<CPU>> {
|
||||
let mut data = data;
|
||||
let mut canvas = canvas.into_element();
|
||||
use glam::{DAffine2, DVec2};
|
||||
@@ -231,7 +210,7 @@ where
|
||||
|
||||
// Snapshot the input as a List<Graphic> so the renderer can recurse into the original child layers
|
||||
// when collecting metadata, exposing their click targets to editor tools (same mechanism as Boolean Operation).
|
||||
let upstream_graphic_list = data.clone().into_graphic_list();
|
||||
let upstream_graphic_list = data.clone();
|
||||
|
||||
let mut render = SvgRender::new();
|
||||
let aabb = Bbox::from_transform(footprint.transform).to_axis_aligned_bbox();
|
||||
|
||||
@@ -19,11 +19,10 @@ pub use vector_types::vector::misc::BooleanOperation;
|
||||
|
||||
/// Combines the geometric forms of one or more closed paths into a new vector path that results from cutting or joining the paths by the chosen method.
|
||||
#[node_macro::node(category("Vector: Modifier"), memoize)]
|
||||
async fn boolean_operation<I: graphic_types::IntoGraphicList>(
|
||||
async fn boolean_operation(
|
||||
_: impl Ctx,
|
||||
/// The `List` of vector paths to perform the boolean operation on. Nested `List`s are automatically flattened.
|
||||
#[implementations(List<Graphic>, List<Vector>)]
|
||||
content: I,
|
||||
content: List<Graphic>,
|
||||
/// Which boolean operation to perform on the paths.
|
||||
///
|
||||
/// Union combines all paths while cutting out overlapping areas (even the interiors of a single path).
|
||||
@@ -33,7 +32,6 @@ async fn boolean_operation<I: graphic_types::IntoGraphicList>(
|
||||
operation: Item<BooleanOperation>,
|
||||
) -> Item<Vector> {
|
||||
let operation = operation.into_element();
|
||||
let content = content.into_graphic_list();
|
||||
|
||||
// The first index is the bottom of the stack
|
||||
let flattened = flatten_vector(&content);
|
||||
|
||||
@@ -15,7 +15,7 @@ use glam::{DAffine2, DMat2, DVec2};
|
||||
use graphic_types::Vector;
|
||||
use graphic_types::graphic::{bake_paint_transforms, is_paint_present};
|
||||
use graphic_types::raster_types::{CPU, GPU, Raster};
|
||||
use graphic_types::{Appearance, Cover, CoverPlacement, Coverage, Graphic, IntoGraphicList, IntoPaint, stamp_coverage};
|
||||
use graphic_types::{Appearance, Cover, CoverPlacement, Coverage, Graphic, IntoGraphicList, stamp_coverage};
|
||||
use kurbo::simplify::{SimplifyOptions, simplify_bezpath};
|
||||
use kurbo::{Affine, BezPath, DEFAULT_ACCURACY, Line, ParamCurve, ParamCurveArclen, PathEl, PathSeg, Shape};
|
||||
use rand::{Rng, SeedableRng};
|
||||
@@ -303,18 +303,12 @@ where
|
||||
|
||||
/// Applies a fill style to the vector content, giving an appearance to the area within the interior of the geometry.
|
||||
#[node_macro::node(category("Vector: Style"), path(graphene_core::vector), properties("fill_properties"))]
|
||||
async fn fill<V, F: IntoPaint + 'n + Send + 'static>(
|
||||
async fn fill<V>(
|
||||
_: impl Ctx,
|
||||
/// The content with vector paths to apply the fill style to.
|
||||
#[implementations(Vector, Vector, Vector, Vector, Vector, Vector, Graphic, Graphic, Graphic, Graphic, Graphic, Graphic)]
|
||||
#[implementations(Vector, Graphic)]
|
||||
content: Item<V>,
|
||||
/// The fill to paint the path with.
|
||||
#[default(Color::BLACK)]
|
||||
#[implementations(
|
||||
Item<Graphic>, List<Vector>, List<Color>, List<Gradient>, List<Raster<CPU>>, List<Raster<GPU>>,
|
||||
Item<Graphic>, List<Vector>, List<Color>, List<Gradient>, List<Raster<CPU>>, List<Raster<GPU>>,
|
||||
)]
|
||||
fill: F,
|
||||
#[default(Color::BLACK)] paint: Item<Graphic>,
|
||||
_backup_color: Item<Color>,
|
||||
#[default(Color::BLACK, Color::WHITE)] _backup_gradient: Item<Gradient>,
|
||||
_gradient_form: Item<GradientForm>,
|
||||
@@ -328,67 +322,85 @@ where
|
||||
let (_has_transform, _transform) = (_has_transform.into_element(), *_transform.element());
|
||||
|
||||
let mut content = content;
|
||||
let mut fill = fill.into_paint();
|
||||
// The paint is the element alone: keeping the wire envelope's attributes would nest the paint as a group, changing how it renders
|
||||
let mut paint = paint.into_element();
|
||||
|
||||
// Stamp the gradient styling inputs onto any gradient paint missing them, whether the paint arrived as a picker value or a wire
|
||||
if let Graphic::GradientList(gradient) = &mut fill {
|
||||
if gradient.iter_attribute_values::<GradientForm>(ATTR_GRADIENT_FORM).is_none() {
|
||||
for value in gradient.iter_attribute_values_mut_or_default::<GradientForm>(ATTR_GRADIENT_FORM) {
|
||||
*value = _gradient_form;
|
||||
}
|
||||
let (needs_form, needs_transform) = match &paint {
|
||||
Graphic::Gradient(item) => (item.attribute::<GradientForm>(ATTR_GRADIENT_FORM).is_none(), item.attribute::<DAffine2>(ATTR_TRANSFORM).is_none()),
|
||||
Graphic::GradientList(list) => (
|
||||
list.iter_attribute_values::<GradientForm>(ATTR_GRADIENT_FORM).is_none(),
|
||||
list.iter_attribute_values::<DAffine2>(ATTR_TRANSFORM).is_none(),
|
||||
),
|
||||
_ => (false, false),
|
||||
};
|
||||
|
||||
let stamped_transform = needs_transform.then(|| {
|
||||
// Without an explicit placement, derive one covering the paint target's bounding box (the CSS `auto` behavior)
|
||||
if _has_transform {
|
||||
return _transform;
|
||||
}
|
||||
|
||||
if gradient.iter_attribute_values::<DAffine2>(ATTR_TRANSFORM).is_none() {
|
||||
// Without an explicit placement, derive one covering the paint target's bounding box (the CSS `auto` behavior)
|
||||
let transform = if _has_transform {
|
||||
_transform
|
||||
} else {
|
||||
let mut bounds: Option<[DVec2; 2]> = None;
|
||||
content.for_each_vector_mut(|vector, _| {
|
||||
if let Some([min, max]) = vector.bounding_box() {
|
||||
bounds = Some(match bounds {
|
||||
Some([bmin, bmax]) => [bmin.min(min), bmax.max(max)],
|
||||
None => [min, max],
|
||||
});
|
||||
}
|
||||
let mut bounds: Option<[DVec2; 2]> = None;
|
||||
content.for_each_vector_mut(|vector, _| {
|
||||
if let Some([min, max]) = vector.bounding_box() {
|
||||
bounds = Some(match bounds {
|
||||
Some([bmin, bmax]) => [bmin.min(min), bmax.max(max)],
|
||||
None => [min, max],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Nudge a degenerate axis so the gradient transform stays invertible, matching the editor's `nonzero_bounding_box`
|
||||
let [min, mut max] = bounds.unwrap_or([DVec2::ZERO, DVec2::ONE]);
|
||||
if max.x - min.x < 1e-10 {
|
||||
max.x = min.x + 1.;
|
||||
}
|
||||
if max.y - min.y < 1e-10 {
|
||||
max.y = min.y + 1.;
|
||||
}
|
||||
initial_gradient_transform_for_bounding_box([min, max])
|
||||
};
|
||||
// Nudge a degenerate axis so the gradient transform stays invertible, matching the editor's `nonzero_bounding_box`
|
||||
let [min, mut max] = bounds.unwrap_or([DVec2::ZERO, DVec2::ONE]);
|
||||
if max.x - min.x < 1e-10 {
|
||||
max.x = min.x + 1.;
|
||||
}
|
||||
if max.y - min.y < 1e-10 {
|
||||
max.y = min.y + 1.;
|
||||
}
|
||||
initial_gradient_transform_for_bounding_box([min, max])
|
||||
});
|
||||
|
||||
for value in gradient.iter_attribute_values_mut_or_default::<DAffine2>(ATTR_TRANSFORM) {
|
||||
*value = transform;
|
||||
match &mut paint {
|
||||
Graphic::Gradient(item) => {
|
||||
if needs_form {
|
||||
item.set_attribute(ATTR_GRADIENT_FORM, _gradient_form);
|
||||
}
|
||||
if let Some(transform) = stamped_transform {
|
||||
item.set_attribute(ATTR_TRANSFORM, transform);
|
||||
}
|
||||
}
|
||||
Graphic::GradientList(list) => {
|
||||
if needs_form {
|
||||
for value in list.iter_attribute_values_mut_or_default::<GradientForm>(ATTR_GRADIENT_FORM) {
|
||||
*value = _gradient_form;
|
||||
}
|
||||
}
|
||||
if let Some(transform) = stamped_transform {
|
||||
for value in list.iter_attribute_values_mut_or_default::<DAffine2>(ATTR_TRANSFORM) {
|
||||
*value = transform;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// Appending follows the painter's algorithm: the most downstream paint node in the chain paints on top
|
||||
stamp_coverage(&mut content, Coverage::new_fill(), fill, CoverPlacement::Above);
|
||||
stamp_coverage(&mut content, Coverage::new_fill(), paint, CoverPlacement::Above);
|
||||
content
|
||||
}
|
||||
|
||||
/// 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"), path(graphene_core::vector), properties("stroke_properties"))]
|
||||
async fn stroke<V, P: IntoPaint + 'n + Send + 'static>(
|
||||
async fn stroke<V>(
|
||||
_: impl Ctx,
|
||||
/// The content with vector paths to apply the stroke style to.
|
||||
#[implementations(Vector, Vector, Vector, Vector, Vector, Vector, Graphic, Graphic, Graphic, Graphic, Graphic, Graphic)]
|
||||
#[implementations(Vector, Graphic)]
|
||||
content: Item<V>,
|
||||
/// The stroke paint.
|
||||
#[default(Color::BLACK)]
|
||||
#[implementations(
|
||||
Item<Graphic>, List<Vector>, List<Color>, List<Gradient>, List<Raster<CPU>>, List<Raster<GPU>>,
|
||||
Item<Graphic>, List<Vector>, List<Color>, List<Gradient>, List<Raster<CPU>>, List<Raster<GPU>>,
|
||||
)]
|
||||
paint: P,
|
||||
paint: Item<Graphic>,
|
||||
/// The stroke thickness.
|
||||
#[unit(" px")]
|
||||
#[default(2.)]
|
||||
@@ -433,7 +445,8 @@ where
|
||||
transform: DAffine2::IDENTITY,
|
||||
};
|
||||
|
||||
let paint = paint.into_paint();
|
||||
// The wire envelope is dropped for the same reason as in `fill` above
|
||||
let paint = paint.into_element();
|
||||
|
||||
// The coverage records the stroke's authoring space, so the item transform is composed in. Its translation
|
||||
// cancels out in every consumer, so it is cleared to let an otherwise-identity capture elide.
|
||||
@@ -1240,7 +1253,6 @@ async fn auto_tangents<V: MapVectorItems + 'n + Send>(
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: After the Graphic lowering refactor, measure a group as one enclosing box instead of one box per shape
|
||||
#[node_macro::node(category("Vector: Modifier"), path(core_types::vector))]
|
||||
async fn bounding_box<V: MapVectorItems + 'n + Send>(_: impl Ctx, #[implementations(Graphic, Vector)] content: Item<V>) -> Item<V> {
|
||||
V::map_vector_items(content, |content| {
|
||||
@@ -1260,7 +1272,6 @@ async fn bounding_box<V: MapVectorItems + 'n + Send>(_: impl Ctx, #[implementati
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: Accept graphic input once the Graphic lowering refactor gives group leaves a single Vector to measure
|
||||
#[node_macro::node(category("Vector: Measure"), path(core_types::vector))]
|
||||
async fn dimensions(_: impl Ctx, content: Item<Vector>) -> Item<DVec2> {
|
||||
let dimensions = content
|
||||
@@ -1714,7 +1725,6 @@ async fn separate_subpaths<V: ExpandVectorItems + 'n + Send>(_: impl Ctx, #[impl
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: Accept graphic input once the Graphic lowering refactor gives group leaves a single Vector to measure
|
||||
/// Determines if the subpath at the given index is closed, meaning its ends are connected together forming a loop.
|
||||
#[node_macro::node(name("Path is Closed"), category("Vector: Measure"), path(core_types::vector))]
|
||||
async fn path_is_closed(
|
||||
@@ -1751,9 +1761,9 @@ async fn map_points<V: MapVectorItems + 'n + Send>(
|
||||
|
||||
/// Combines every vector path across the input into a single compound path.
|
||||
#[node_macro::node(category("Vector"), path(graphene_core::vector))]
|
||||
pub async fn combine_paths<T: IntoGraphicList>(_: impl Ctx, #[implementations(List<Graphic>, List<Vector>)] content: T) -> Item<Vector> {
|
||||
let graphic_list = content.into_graphic_list();
|
||||
let flattened = graphic_list.clone().into_flattened_list::<Vector>();
|
||||
pub async fn combine_paths(_: impl Ctx, content: List<Graphic>) -> Item<Vector> {
|
||||
let graphic_list = content.clone();
|
||||
let flattened = content.into_flattened_list::<Vector>();
|
||||
|
||||
// Create a `List` with one empty `Vector` element, then get a mutable reference to it which we append flattened subpaths to
|
||||
let mut output_list = List::new_from_element(Vector::default());
|
||||
@@ -2154,7 +2164,6 @@ async fn cut_segments<V: MapVectorItems + 'n + Send>(_: impl Ctx, #[implementati
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: Accept graphic input once the Graphic lowering refactor gives group leaves a single Vector to measure
|
||||
/// Determines the position of a point on the path, given by its progression from 0 to 1 along the path.
|
||||
///
|
||||
/// If multiple subpaths make up the path, the whole number part of the progression value selects the subpath and the decimal part determines the position along it.
|
||||
@@ -2192,7 +2201,6 @@ async fn position_on_path(
|
||||
Item::new_from_element(position)
|
||||
}
|
||||
|
||||
// TODO: Accept graphic input once the Graphic lowering refactor gives group leaves a single Vector to measure
|
||||
/// Determines the angle of the tangent at a point on the path, given by its progression from 0 to 1 along the path.
|
||||
///
|
||||
/// If multiple subpaths make up the path, the whole number part of the progression value selects the subpath and the decimal part determines the position along it.
|
||||
@@ -2483,11 +2491,10 @@ async fn offset_points<V: MapVectorItems + 'n + Send>(
|
||||
///
|
||||
/// *Progression* morphs through all objects. Interpolation is linear unless *Path* geometry is provided to control the trajectory between key objects. The **Origins to Polyline** node may be used to create a path with anchor points corresponding to each object. Other nodes can modify its path segments.
|
||||
#[node_macro::node(category("Vector: Modifier"), path(core_types::vector))]
|
||||
async fn morph<I: IntoGraphicList>(
|
||||
async fn morph(
|
||||
_: impl Ctx,
|
||||
/// The vector objects to interpolate between. Mixed graphic content is deeply flattened to keep only vector elements.
|
||||
#[implementations(List<Graphic>, List<Vector>)]
|
||||
content: I,
|
||||
content: List<Graphic>,
|
||||
/// The fractional part `[0, 1)` traverses the morph uniformly along the path. If the control path has multiple subpaths, each added integer selects the next subpath.
|
||||
progression: Item<Progression>,
|
||||
/// Swap the direction of the progression between objects or along the control path.
|
||||
@@ -2736,9 +2743,9 @@ async fn morph<I: IntoGraphicList>(
|
||||
let (progression, reverse, distribution) = (progression.into_element(), reverse.into_element(), distribution.into_element());
|
||||
|
||||
// Preserve original `List<Graphic>` as upstream data so this group layer's nested layers can be edited by the tools.
|
||||
let mut graphic_list_content = content.clone().into_graphic_list();
|
||||
let mut graphic_list_content = content.clone();
|
||||
|
||||
// If the input isn't a List<Vector>, we convert it into one by flattening any List<Graphic> content.
|
||||
// Only vector content can interpolate, so the rest is discarded by flattening.
|
||||
let content = content.into_flattened_list::<Vector>();
|
||||
|
||||
// Not enough elements to interpolate between, so we return the input as-is
|
||||
@@ -3459,7 +3466,6 @@ fn close_path<V: MapVectorItems + Send + Sync + 'static>(_: impl Ctx, #[implemen
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: Accept graphic input once the Graphic lowering refactor gives group leaves a single Vector to measure
|
||||
#[node_macro::node(category("Vector: Measure"), path(core_types::vector))]
|
||||
fn point_inside(_: impl Ctx, source: Item<Vector>, point: Item<DVec2>) -> Item<bool> {
|
||||
let point = point.into_element();
|
||||
@@ -3476,7 +3482,6 @@ async fn list_length(_: impl Ctx, content: ListDyn) -> Item<f64> {
|
||||
Item::new_from_element(content.len() as f64)
|
||||
}
|
||||
|
||||
// TODO: Accept graphic input once the Graphic lowering refactor gives group leaves a single Vector to measure
|
||||
#[node_macro::node(category("Vector: Measure"), path(graphene_core::vector))]
|
||||
async fn count_points(_: impl Ctx, content: Item<Vector>) -> Item<f64> {
|
||||
let count = content.element().point_domain.positions().len() as f64;
|
||||
@@ -3484,7 +3489,6 @@ async fn count_points(_: impl Ctx, content: Item<Vector>) -> Item<f64> {
|
||||
Item::new_from_element(count)
|
||||
}
|
||||
|
||||
// TODO: Accept graphic input once the Graphic lowering refactor gives group leaves a single Vector to measure
|
||||
/// Retrieves the vec2 position (in local space) of the anchor point at the specified index within a vector element.
|
||||
/// If no value exists at that index, the position (0, 0) is returned.
|
||||
#[node_macro::node(category("Vector: Measure"), path(graphene_core::vector))]
|
||||
@@ -3513,7 +3517,6 @@ async fn index_points(
|
||||
Item::new_from_element(positions[index])
|
||||
}
|
||||
|
||||
// TODO: Accept graphic input once the Graphic lowering refactor gives group leaves a single Vector to measure
|
||||
#[node_macro::node(category("Vector: Measure"), path(core_types::vector))]
|
||||
async fn path_length(_: impl Ctx, source: Item<Vector>) -> Item<f64> {
|
||||
let transform: DAffine2 = source.attribute_cloned_or_default(ATTR_TRANSFORM);
|
||||
@@ -3529,7 +3532,6 @@ async fn path_length(_: impl Ctx, source: Item<Vector>) -> Item<f64> {
|
||||
Item::new_from_element(length)
|
||||
}
|
||||
|
||||
// TODO: Accept graphic input once the Graphic lowering refactor gives group leaves a single Vector to measure
|
||||
#[node_macro::node(category("Vector: Measure"), path(core_types::vector))]
|
||||
async fn area(ctx: impl Ctx + CloneVarArgs + ExtractAll, content: impl Node<Context<'static>, Output = Item<Vector>>) -> Item<f64> {
|
||||
let new_ctx = OwnedContextImpl::from(ctx).with_footprint(Footprint::default()).into_context();
|
||||
@@ -3542,7 +3544,6 @@ async fn area(ctx: impl Ctx + CloneVarArgs + ExtractAll, content: impl Node<Cont
|
||||
Item::new_from_element(area)
|
||||
}
|
||||
|
||||
// TODO: Accept graphic input once the Graphic lowering refactor gives group leaves a single Vector to measure
|
||||
#[node_macro::node(category("Vector: Measure"), path(core_types::vector))]
|
||||
async fn centroid(ctx: impl Ctx + CloneVarArgs + ExtractAll, content: impl Node<Context<'static>, Output = Item<Vector>>, centroid_type: Item<CentroidType>) -> Item<DVec2> {
|
||||
let centroid_type = centroid_type.into_element();
|
||||
@@ -3609,6 +3610,11 @@ mod test {
|
||||
List::new_from_element(Vector::from_bezpath(bezpath))
|
||||
}
|
||||
|
||||
/// Stands in for the embedding adapter a compiled graph inserts ahead of a `List<Graphic>` connector.
|
||||
fn embed_vectors(vectors: List<Vector>) -> List<Graphic> {
|
||||
vectors.into_iter().map(|item| Item::new_from_element(Graphic::from(item))).collect()
|
||||
}
|
||||
|
||||
fn vector_item_from_bezpath(bezpath: BezPath) -> Item<Vector> {
|
||||
Item::new_from_element(Vector::from_bezpath(bezpath))
|
||||
}
|
||||
@@ -3923,7 +3929,7 @@ mod test {
|
||||
|
||||
let morphed = super::morph(
|
||||
Footprint::default(),
|
||||
rectangles,
|
||||
embed_vectors(rectangles),
|
||||
Item::new_from_element(0.5),
|
||||
Item::new_from_element(false),
|
||||
Item::new_from_element(InterpolationDistribution::default()),
|
||||
@@ -3949,7 +3955,7 @@ mod test {
|
||||
v
|
||||
};
|
||||
|
||||
let solid_fill = |color: Color| Appearance::new_single(Coverage::new_fill(), List::new_from_element(color).into_paint());
|
||||
let solid_fill = |color: Color| Appearance::new_single(Coverage::new_fill(), Graphic::from(List::new_from_element(color)));
|
||||
let item_a = Item::new_from_element(rect())
|
||||
.with_attribute(ATTR_TRANSFORM, DAffine2::IDENTITY)
|
||||
.with_attribute(ATTR_APPEARANCE, solid_fill(Color::RED));
|
||||
@@ -3962,7 +3968,7 @@ mod test {
|
||||
|
||||
let morphed = super::morph(
|
||||
Footprint::default(),
|
||||
content,
|
||||
embed_vectors(content),
|
||||
Item::new_from_element(0.5),
|
||||
Item::new_from_element(false),
|
||||
Item::new_from_element(InterpolationDistribution::default()),
|
||||
@@ -4001,8 +4007,8 @@ mod test {
|
||||
// The two endpoints list their covers in opposite paint orders, which pairing by position would cross
|
||||
let appearance = |fill: Color, stroke: Color, stroke_placement| {
|
||||
let mut appearance = Appearance::default();
|
||||
appearance.replace_or_insert(Coverage::new_fill(), List::new_from_element(fill).into_paint(), CoverPlacement::Above);
|
||||
appearance.replace_or_insert(Coverage::new_stroke(&Stroke::new(4.)), List::new_from_element(stroke).into_paint(), stroke_placement);
|
||||
appearance.replace_or_insert(Coverage::new_fill(), Graphic::from(List::new_from_element(fill)), CoverPlacement::Above);
|
||||
appearance.replace_or_insert(Coverage::new_stroke(&Stroke::new(4.)), Graphic::from(List::new_from_element(stroke)), stroke_placement);
|
||||
appearance
|
||||
};
|
||||
|
||||
@@ -4018,7 +4024,7 @@ mod test {
|
||||
|
||||
let morphed = super::morph(
|
||||
Footprint::default(),
|
||||
content,
|
||||
embed_vectors(content),
|
||||
Item::new_from_element(0.5),
|
||||
Item::new_from_element(false),
|
||||
Item::new_from_element(InterpolationDistribution::default()),
|
||||
|
||||
@@ -400,7 +400,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_paint_color_default_parses_against_its_list_wire() {
|
||||
fn fill_paint_color_default_parses_against_its_graphic_wire() {
|
||||
let node_registry = core_types::registry::NODE_REGISTRY.lock().unwrap();
|
||||
let metadata_registry = core_types::registry::NODE_METADATA.lock().unwrap();
|
||||
|
||||
@@ -414,7 +414,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
*paint,
|
||||
TaggedValue::Color(Color::BLACK),
|
||||
"The paint input's `Color::BLACK` default should parse against its `List<Graphic>` wire type"
|
||||
"The paint input's `Color::BLACK` default should parse against its `Item<Graphic>` wire type"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user