mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-06 08:13:40 -10:00
529 lines
22 KiB
Rust
529 lines
22 KiB
Rust
//! Writes each layer's path onto what it renders, so what's rendered can be traced back to its layer.
|
|
|
|
use crate::node_registry::NODE_REGISTRY;
|
|
use graph_craft::document::value::TaggedValue;
|
|
use graph_craft::document::{DocumentNode, DocumentNodeImplementation, DocumentNodeMetadata, NodeId, NodeInput, NodeNetwork};
|
|
use graph_craft::{ProtoNodeIdentifier, Type, concrete, list};
|
|
use graphene_std::graphic::{as_graphic, extend, write_layer_path};
|
|
use graphene_std::list::NodeIdPath;
|
|
use graphene_std::{Artboard, Graphic, NodeParameter};
|
|
use std::collections::HashMap;
|
|
use std::hash::{DefaultHasher, Hash, Hasher};
|
|
|
|
/// What a layer adds to the document, which is what its thumbnail shows and what its path is written onto.
|
|
pub enum LayerContent {
|
|
/// What the Extend node the layer ends in is given to add, with the path of the network the node is in.
|
|
Added { network_path: Vec<NodeId>, extend: NodeId },
|
|
/// Everything the layer outputs, from the node at this path.
|
|
Output(Vec<NodeId>),
|
|
}
|
|
|
|
/// The content of the layer at this path, unless it's hidden or passes through what's below it and so has none of its own.
|
|
pub fn layer_content(layer_path: &[NodeId], network: &NodeNetwork) -> Option<LayerContent> {
|
|
let output = network.output_source(layer_path, 0)?;
|
|
if !output.starts_with(layer_path) {
|
|
return None;
|
|
}
|
|
|
|
let (node_id, network_path) = output.split_last()?;
|
|
let node = network.nested_network(network_path)?.nodes.get(node_id)?;
|
|
if node.implementation != DocumentNodeImplementation::ProtoNode(extend::IDENTIFIER) {
|
|
return Some(LayerContent::Output(output));
|
|
}
|
|
|
|
Some(LayerContent::Added {
|
|
network_path: network_path.to_vec(),
|
|
extend: *node_id,
|
|
})
|
|
}
|
|
|
|
impl LayerContent {
|
|
/// The path of the node producing the content once compiled.
|
|
pub fn source(&self, network: &NodeNetwork) -> Option<Vec<NodeId>> {
|
|
match self {
|
|
Self::Added { network_path, extend } => {
|
|
let added = network.nested_network(network_path)?.nodes.get(extend)?.inputs.get(extend::NewInput::INDEX)?;
|
|
network.input_source(network_path, added)
|
|
}
|
|
Self::Output(output) => Some(output.clone()),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Puts a node after each layer's content that writes the layer's path onto it.
|
|
/// A layer has none if its content may be of a type that can't carry the path.
|
|
pub fn write_layer_paths(network: &mut NodeNetwork, layer_paths: &[Vec<NodeId>]) {
|
|
let mut carries_layer_path = HashMap::new();
|
|
|
|
// O(layers * depth)
|
|
for layer_path in layer_paths {
|
|
let Some((layer, parent_path)) = layer_path.split_last() else { continue };
|
|
let Some(content) = layer_content(layer_path, network) else { continue };
|
|
|
|
// An Extend node nested deeper than the layer may be in a network that other layers share.
|
|
// So the writer goes after the layer's output, and the Extend node only decides if the content can carry the path.
|
|
let content = match content {
|
|
LayerContent::Added { network_path, extend } if network_path.len() > layer_path.len() => LayerContent::Output([network_path.as_slice(), &[extend]].concat()),
|
|
content => content,
|
|
};
|
|
|
|
let Some(source) = content.source(network) else { continue };
|
|
if !source_can_carry_layer_path(source, network, &mut carries_layer_path) {
|
|
continue;
|
|
}
|
|
|
|
match content {
|
|
// The writer goes between the Extend node and what it's given to add, inside the layer or beside it if the layer is that node
|
|
LayerContent::Added { network_path, extend } => {
|
|
let writer = layer_path_writer_id(extend);
|
|
let layer_beside = if network_path.len() == layer_path.len() { Vec::new() } else { vec![*layer] };
|
|
let Some(holder) = network_mut(network, &network_path) else { continue };
|
|
let Some(added) = holder.nodes.get_mut(&extend).and_then(|node| node.inputs.get_mut(extend::NewInput::INDEX)) else {
|
|
continue;
|
|
};
|
|
|
|
// Every layer made from the same network shares the writer
|
|
if *added == NodeInput::node(writer, 0) {
|
|
continue;
|
|
}
|
|
|
|
let content = std::mem::replace(added, NodeInput::node(writer, 0));
|
|
holder.nodes.insert(writer, layer_path_writer(content, layer_beside));
|
|
}
|
|
// The writer goes beside the layer, taking over everything its output feeds
|
|
LayerContent::Output(_) => {
|
|
let writer = layer_path_writer_id(*layer);
|
|
let Some(holder) = network_mut(network, parent_path) else { continue };
|
|
if holder.nodes.contains_key(&writer) {
|
|
continue;
|
|
}
|
|
|
|
let (output, written) = (NodeInput::node(*layer, 0), NodeInput::node(writer, 0));
|
|
let inputs = holder.nodes.values_mut().flat_map(|node| &mut node.inputs).chain(&mut holder.exports);
|
|
inputs.filter(|input| **input == output).for_each(|input| *input = written.clone());
|
|
|
|
holder.nodes.insert(writer, layer_path_writer(output, vec![*layer]));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Replaces the network's export with the stack of what these layers add, in the given order, giving back the root node making it, or `None` if they add nothing.
|
|
/// An artboard holds its own content, so when any of the layers adds one, only the artboards are stacked.
|
|
pub fn export_layers(network: &mut NodeNetwork, layers: &[NodeId]) -> Option<NodeId> {
|
|
// O(layers * depth)
|
|
let mut added = Vec::new();
|
|
for &layer in layers {
|
|
let Some(content) = layer_content(&[layer], network) else { continue };
|
|
let Some(source) = content.source(network) else { continue };
|
|
let artboard = outputs_artboards(source, network);
|
|
|
|
let Some(root_input) = surface_layer_content(network, layer, content) else { continue };
|
|
added.push((layer, root_input, artboard));
|
|
}
|
|
|
|
let artboards = added.iter().any(|&(_, _, artboard)| artboard);
|
|
let seed = if artboards { list!(Artboard) } else { list!(Graphic) };
|
|
|
|
let mut stack = NodeInput::type_default(seed, false);
|
|
for (layer, root_input, _) in added.into_iter().filter(|&(_, _, artboard)| artboard == artboards) {
|
|
// Extend joins lists of one type, so what other layers add is converted to graphics as the base input of 'Merge' does
|
|
let content = if artboards {
|
|
root_input
|
|
} else {
|
|
let as_graphic_id = exported_layer_node_id("as graphic", layer);
|
|
network.nodes.insert(as_graphic_id, proto_node(as_graphic::IDENTIFIER, vec![root_input]));
|
|
NodeInput::node(as_graphic_id, 0)
|
|
};
|
|
|
|
let extend_id = exported_layer_node_id("extend", layer);
|
|
network.nodes.insert(extend_id, proto_node(extend::IDENTIFIER, vec![stack, content]));
|
|
stack = NodeInput::node(extend_id, 0);
|
|
}
|
|
|
|
let stack_node = stack.as_node();
|
|
network.exports = vec![stack];
|
|
stack_node
|
|
}
|
|
|
|
/// Makes what the layer adds an output of the layer node at the root, giving back the input that refers to it there.
|
|
fn surface_layer_content(network: &mut NodeNetwork, layer: NodeId, content: LayerContent) -> Option<NodeInput> {
|
|
let LayerContent::Added { network_path, extend } = content else {
|
|
return Some(NodeInput::node(layer, 0));
|
|
};
|
|
|
|
let mut passed_on = network.nested_network(&network_path)?.nodes.get(&extend)?.inputs.get(extend::NewInput::INDEX)?.clone();
|
|
|
|
// Each network from the Extend node's out to the layer's gives it as an extra export, which layers made from the same network share
|
|
for depth in (1..=network_path.len()).rev() {
|
|
let holder = network_mut(network, &network_path[..depth])?;
|
|
let output_index = match holder.exports.iter().position(|export| *export == passed_on) {
|
|
Some(index) => index,
|
|
None => {
|
|
holder.exports.push(passed_on);
|
|
holder.exports.len() - 1
|
|
}
|
|
};
|
|
passed_on = NodeInput::node(network_path[depth - 1], output_index);
|
|
}
|
|
|
|
Some(passed_on)
|
|
}
|
|
|
|
/// Whether every type the proto node at this path may output is an artboard type, looking through nodes that output what their first input is given.
|
|
fn outputs_artboards(mut source: Vec<NodeId>, network: &NodeNetwork) -> bool {
|
|
let is_artboard = |ty: &Type| matches!(ty.nested_type(), Type::Item(element) | Type::List(element) if **element == concrete!(Artboard));
|
|
|
|
loop {
|
|
let Some((node_id, network_path)) = source.split_last() else { return false };
|
|
let Some(node) = network.nested_network(network_path).and_then(|network| network.nodes.get(node_id)) else {
|
|
return false;
|
|
};
|
|
let DocumentNodeImplementation::ProtoNode(identifier) = &node.implementation else { return false };
|
|
let Some(implementations) = NODE_REGISTRY.get(identifier) else { return false };
|
|
|
|
if implementations.keys().all(|types| is_artboard(&types.return_value)) {
|
|
return true;
|
|
}
|
|
|
|
let passes_first_input_on = |types: &graphene_std::NodeIOTypes| types.inputs.first().is_some_and(|input| input.nested_type() == types.return_value.nested_type());
|
|
if !implementations.keys().all(passes_first_input_on) {
|
|
return false;
|
|
}
|
|
let Some(input_source) = node.inputs.first().and_then(|input| network.input_source(network_path, input)) else {
|
|
return false;
|
|
};
|
|
source = input_source;
|
|
}
|
|
}
|
|
|
|
/// The ID of a node that `export_layers` adds at the root for the layer with this ID.
|
|
fn exported_layer_node_id(purpose: &str, layer: NodeId) -> NodeId {
|
|
let mut hasher = DefaultHasher::new();
|
|
"exported layer".hash(&mut hasher);
|
|
purpose.hash(&mut hasher);
|
|
layer.hash(&mut hasher);
|
|
NodeId(hasher.finish())
|
|
}
|
|
|
|
fn proto_node(identifier: ProtoNodeIdentifier, inputs: Vec<NodeInput>) -> DocumentNode {
|
|
DocumentNode {
|
|
inputs,
|
|
implementation: DocumentNodeImplementation::ProtoNode(identifier),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
/// How the layer path writer fares with what a proto node outputs.
|
|
#[derive(Clone, Copy)]
|
|
enum CarriesLayerPath {
|
|
Always,
|
|
/// The node outputs the type its one input is given, so that decides it.
|
|
AsItsInput,
|
|
Never,
|
|
}
|
|
|
|
/// Whether the layer path writer can take everything the proto node at this path may output.
|
|
fn source_can_carry_layer_path(mut source: Vec<NodeId>, network: &NodeNetwork, carries_layer_path: &mut HashMap<ProtoNodeIdentifier, CarriesLayerPath>) -> bool {
|
|
loop {
|
|
let Some((node_id, network_path)) = source.split_last() else { return false };
|
|
let Some(node) = network.nested_network(network_path).and_then(|network| network.nodes.get(node_id)) else {
|
|
return false;
|
|
};
|
|
let DocumentNodeImplementation::ProtoNode(identifier) = &node.implementation else { return false };
|
|
|
|
match *carries_layer_path.entry(identifier.clone()).or_insert_with(|| output_can_carry_layer_path(identifier)) {
|
|
CarriesLayerPath::Always => return true,
|
|
CarriesLayerPath::Never => return false,
|
|
CarriesLayerPath::AsItsInput => {
|
|
let Some(input_source) = node.inputs.first().and_then(|input| network.input_source(network_path, input)) else {
|
|
return false;
|
|
};
|
|
source = input_source;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn output_can_carry_layer_path(identifier: &ProtoNodeIdentifier) -> CarriesLayerPath {
|
|
let (Some(outputs), Some(writers)) = (NODE_REGISTRY.get(identifier), NODE_REGISTRY.get(&write_layer_path::IDENTIFIER)) else {
|
|
return CarriesLayerPath::Never;
|
|
};
|
|
|
|
let writes_type = |output: &graph_craft::Type| {
|
|
let content = |writer: &graphene_std::NodeIOTypes| writer.inputs.get(write_layer_path::ContentInput::INDEX).is_some_and(|content| content.nested_type() == output);
|
|
writers.keys().any(content)
|
|
};
|
|
if outputs.keys().all(|output| writes_type(output.return_value.nested_type())) {
|
|
return CarriesLayerPath::Always;
|
|
}
|
|
|
|
let as_its_input = |output: &graphene_std::NodeIOTypes| matches!(output.inputs.as_slice(), [input] if input.nested_type() == output.return_value.nested_type());
|
|
if outputs.keys().all(as_its_input) {
|
|
CarriesLayerPath::AsItsInput
|
|
} else {
|
|
CarriesLayerPath::Never
|
|
}
|
|
}
|
|
|
|
/// The network at this path, which is either an entry of the definitions table or held by its node.
|
|
fn network_mut<'a>(network: &'a mut NodeNetwork, path: &[NodeId]) -> Option<&'a mut NodeNetwork> {
|
|
// The last table entry along the path, which holds any networks after it
|
|
let mut table_entry = None;
|
|
let mut current = &*network;
|
|
for (index, node_id) in path.iter().enumerate() {
|
|
current = match ¤t.nodes.get(node_id)?.implementation {
|
|
DocumentNodeImplementation::Definition(id) => {
|
|
table_entry = Some((*id, index + 1));
|
|
network.definitions.get(id)?
|
|
}
|
|
DocumentNodeImplementation::Network(held) => held,
|
|
_ => return None,
|
|
};
|
|
}
|
|
|
|
let (mut current, held_path) = match table_entry {
|
|
Some((id, held_from)) => (network.definitions.get_mut(&id)?, &path[held_from..]),
|
|
None => (network, path),
|
|
};
|
|
for node_id in held_path {
|
|
let DocumentNodeImplementation::Network(held) = &mut current.nodes.get_mut(node_id)?.implementation else {
|
|
return None;
|
|
};
|
|
current = held;
|
|
}
|
|
|
|
Some(current)
|
|
}
|
|
|
|
/// The ID of the node writing the layer path onto what the node with this ID outputs or is given, which stays the same between compilations.
|
|
pub fn layer_path_writer_id(node_id: NodeId) -> NodeId {
|
|
let mut hasher = DefaultHasher::new();
|
|
"layer path writer".hash(&mut hasher);
|
|
node_id.hash(&mut hasher);
|
|
NodeId(hasher.finish())
|
|
}
|
|
|
|
fn layer_path_writer(content: NodeInput, layer_beside: Vec<NodeId>) -> DocumentNode {
|
|
DocumentNode {
|
|
inputs: vec![
|
|
content,
|
|
// The writer is directly inside the layer or beside it, so removing only its own ID gives the path
|
|
NodeInput::value(TaggedValue::Integer(1), false),
|
|
NodeInput::value(TaggedValue::NodeIdPath(NodeIdPath::from(layer_beside)), false),
|
|
NodeInput::Reflection(DocumentNodeMetadata::DocumentNodePath),
|
|
],
|
|
implementation: DocumentNodeImplementation::ProtoNode(write_layer_path::IDENTIFIER),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use graph_craft::document::NetworkId;
|
|
|
|
#[test]
|
|
fn content_that_cannot_carry_a_layer_path_is_left_alone() {
|
|
let path_node = DocumentNode {
|
|
inputs: vec![NodeInput::Reflection(DocumentNodeMetadata::DocumentNodePath)],
|
|
implementation: DocumentNodeImplementation::ProtoNode(graphene_std::graphic::path_of_subgraph::IDENTIFIER),
|
|
..Default::default()
|
|
};
|
|
let mut network = NodeNetwork {
|
|
exports: vec![NodeInput::node(NodeId(1), 0)],
|
|
nodes: [(NodeId(1), path_node)].into_iter().collect(),
|
|
..Default::default()
|
|
};
|
|
let unchanged = network.clone();
|
|
|
|
write_layer_paths(&mut network, &[vec![NodeId(1)]]);
|
|
|
|
assert_eq!(network, unchanged);
|
|
}
|
|
|
|
fn definition_node(id: NetworkId) -> DocumentNode {
|
|
DocumentNode {
|
|
implementation: DocumentNodeImplementation::Definition(id),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
/// A network shaped like 'Merge', where an Extend node adds what the node beside it gives.
|
|
fn merge_like(added: DocumentNode) -> NodeNetwork {
|
|
let extend = proto_node(extend::IDENTIFIER, vec![NodeInput::value(TaggedValue::None, false), NodeInput::node(NodeId(2), 0)]);
|
|
NodeNetwork {
|
|
exports: vec![NodeInput::node(NodeId(1), 0)],
|
|
nodes: [(NodeId(1), extend), (NodeId(2), added)].into_iter().collect(),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
// One network is shared by a layer at the root and by a layer inside another layer, so the one writer inside it has to suit both
|
|
#[test]
|
|
fn a_network_shared_by_layers_at_different_depths_writes_each_layer_path() {
|
|
let (merge, group) = (NetworkId(1), NetworkId(2));
|
|
let rectangle = proto_node(graphene_std::vector::generator_nodes::rectangle::IDENTIFIER, Vec::new());
|
|
let group_network = NodeNetwork {
|
|
exports: vec![NodeInput::node(NodeId(30), 0)],
|
|
nodes: [(NodeId(30), definition_node(merge))].into_iter().collect(),
|
|
..Default::default()
|
|
};
|
|
let network = NodeNetwork {
|
|
exports: vec![NodeInput::node(NodeId(10), 0)],
|
|
nodes: [(NodeId(10), definition_node(merge)), (NodeId(20), definition_node(group))].into_iter().collect(),
|
|
definitions: [(merge, merge_like(rectangle)), (group, group_network)].into_iter().collect(),
|
|
..Default::default()
|
|
};
|
|
|
|
// The outer layer comes first in one order and last in the other
|
|
let orders = [
|
|
[vec![NodeId(20)], vec![NodeId(10)], vec![NodeId(20), NodeId(30)]],
|
|
[vec![NodeId(10)], vec![NodeId(20), NodeId(30)], vec![NodeId(20)]],
|
|
];
|
|
let written = orders.map(|layer_paths| {
|
|
let mut network = network.clone();
|
|
write_layer_paths(&mut network, &layer_paths);
|
|
network
|
|
});
|
|
assert_eq!(written[0], written[1], "The order the layers are given in should not change what's written");
|
|
|
|
// The writer writes the path of the layer holding its network, whichever layer that is
|
|
let writer = &written[0].definitions[&merge].nodes[&layer_path_writer_id(NodeId(1))];
|
|
let (levels_up, layer_beside) = (writer.input(write_layer_path::LevelsUpInput), writer.input(write_layer_path::LayerBesideInput));
|
|
assert_eq!(levels_up, Some(&NodeInput::value(TaggedValue::Integer(1), false)));
|
|
assert_eq!(layer_beside, Some(&NodeInput::value(TaggedValue::NodeIdPath(NodeIdPath::from(Vec::new())), false)));
|
|
}
|
|
|
|
// Content that already writes its own layer path, like the 'Merge' networks of older documents, gets no second writer
|
|
#[test]
|
|
fn a_network_writing_its_own_layer_path_is_left_alone() {
|
|
let write_attribute = proto_node(graphene_std::graphic::write_attribute::IDENTIFIER, Vec::new());
|
|
let mut network = NodeNetwork {
|
|
exports: vec![NodeInput::node(NodeId(10), 0)],
|
|
nodes: [(NodeId(10), definition_node(NetworkId(1)))].into_iter().collect(),
|
|
definitions: [(NetworkId(1), merge_like(write_attribute))].into_iter().collect(),
|
|
..Default::default()
|
|
};
|
|
let unchanged = network.clone();
|
|
|
|
write_layer_paths(&mut network, &[vec![NodeId(10)]]);
|
|
|
|
assert_eq!(network, unchanged);
|
|
}
|
|
|
|
// The writer has to take every graphic type, or nodes taking any graphic, like these, lose the layer path of everything they output
|
|
#[test]
|
|
fn nodes_taking_any_graphic_carry_a_layer_path() {
|
|
for identifier in [graphene_std::transform_nodes::transform::IDENTIFIER, graphene_std::blending_nodes::opacity::IDENTIFIER] {
|
|
assert!(matches!(output_can_carry_layer_path(&identifier), CarriesLayerPath::Always), "{identifier:?} should carry a layer path");
|
|
}
|
|
}
|
|
|
|
/// What the exported stack's Extend nodes are given at the root, bottom up, each with whether it's converted to a graphic, and what the stack starts from.
|
|
fn exported_stack(network: &NodeNetwork) -> (Vec<(NodeInput, bool)>, NodeInput) {
|
|
let mut added = Vec::new();
|
|
let mut stack = network.exports[0].clone();
|
|
while let Some(extend_node) = stack.as_node().and_then(|node_id| network.nodes.get(&node_id)) {
|
|
assert_eq!(extend_node.implementation, DocumentNodeImplementation::ProtoNode(extend::IDENTIFIER));
|
|
|
|
let content = &extend_node.inputs[extend::NewInput::INDEX];
|
|
let as_graphic_node = content.as_node().map(|node_id| &network.nodes[&node_id]);
|
|
added.push(match as_graphic_node {
|
|
Some(node) if node.implementation == DocumentNodeImplementation::ProtoNode(as_graphic::IDENTIFIER) => (node.inputs[0].clone(), true),
|
|
_ => (content.clone(), false),
|
|
});
|
|
|
|
stack = extend_node.inputs[extend::BaseInput::INDEX].clone();
|
|
}
|
|
|
|
added.reverse();
|
|
(added, stack)
|
|
}
|
|
|
|
#[test]
|
|
fn exported_layers_stack_as_graphics_in_the_order_given() {
|
|
let merge = NetworkId(1);
|
|
let rectangle = || proto_node(graphene_std::vector::generator_nodes::rectangle::IDENTIFIER, Vec::new());
|
|
let mut network = NodeNetwork {
|
|
exports: vec![NodeInput::node(NodeId(20), 0)],
|
|
nodes: [(NodeId(10), definition_node(merge)), (NodeId(20), definition_node(merge)), (NodeId(30), rectangle())]
|
|
.into_iter()
|
|
.collect(),
|
|
definitions: [(merge, merge_like(rectangle()))].into_iter().collect(),
|
|
..Default::default()
|
|
};
|
|
|
|
assert!(export_layers(&mut network, &[NodeId(20), NodeId(30), NodeId(10)]).is_some());
|
|
|
|
// The network both 'Merge' layers are made from gives what its Extend node adds as one extra export, while the plain node gives its output
|
|
assert_eq!(network.definitions[&merge].exports, vec![NodeInput::node(NodeId(1), 0), NodeInput::node(NodeId(2), 0)]);
|
|
let (added, start) = exported_stack(&network);
|
|
assert_eq!(start, NodeInput::type_default(list!(Graphic), false));
|
|
assert_eq!(
|
|
added,
|
|
[(NodeInput::node(NodeId(20), 1), true), (NodeInput::node(NodeId(30), 0), true), (NodeInput::node(NodeId(10), 1), true)]
|
|
);
|
|
}
|
|
|
|
// A layer made from a network holding a 'Merge' node passes what that node's Extend node adds out through both networks
|
|
#[test]
|
|
fn a_nested_extend_nodes_content_is_exported_through_each_network_around_it() {
|
|
let (merge, outer) = (NetworkId(1), NetworkId(2));
|
|
let rectangle = proto_node(graphene_std::vector::generator_nodes::rectangle::IDENTIFIER, Vec::new());
|
|
let outer_network = NodeNetwork {
|
|
exports: vec![NodeInput::node(NodeId(30), 0)],
|
|
nodes: [(NodeId(30), definition_node(merge))].into_iter().collect(),
|
|
..Default::default()
|
|
};
|
|
let mut network = NodeNetwork {
|
|
exports: vec![NodeInput::node(NodeId(10), 0)],
|
|
nodes: [(NodeId(10), definition_node(outer))].into_iter().collect(),
|
|
definitions: [(merge, merge_like(rectangle)), (outer, outer_network)].into_iter().collect(),
|
|
..Default::default()
|
|
};
|
|
|
|
assert!(export_layers(&mut network, &[NodeId(10)]).is_some());
|
|
|
|
assert_eq!(network.definitions[&merge].exports[1], NodeInput::node(NodeId(2), 0));
|
|
assert_eq!(network.definitions[&outer].exports[1], NodeInput::node(NodeId(30), 1));
|
|
assert_eq!(exported_stack(&network).0, [(NodeInput::node(NodeId(10), 1), true)]);
|
|
}
|
|
|
|
// An artboard holds its own content, so the other layers copied with it are left out
|
|
#[test]
|
|
fn exported_artboards_leave_out_the_other_layers() {
|
|
let (merge, artboard) = (NetworkId(1), NetworkId(2));
|
|
let rectangle = proto_node(graphene_std::vector::generator_nodes::rectangle::IDENTIFIER, Vec::new());
|
|
let create_artboard = proto_node(graphene_std::artboard::create_artboard::IDENTIFIER, Vec::new());
|
|
let mut network = NodeNetwork {
|
|
exports: vec![NodeInput::node(NodeId(20), 0)],
|
|
nodes: [(NodeId(10), definition_node(merge)), (NodeId(20), definition_node(artboard))].into_iter().collect(),
|
|
definitions: [(merge, merge_like(rectangle)), (artboard, merge_like(create_artboard))].into_iter().collect(),
|
|
..Default::default()
|
|
};
|
|
|
|
assert!(export_layers(&mut network, &[NodeId(10), NodeId(20)]).is_some());
|
|
|
|
let (added, start) = exported_stack(&network);
|
|
assert_eq!(start, NodeInput::type_default(list!(Artboard), false));
|
|
assert_eq!(added, [(NodeInput::node(NodeId(20), 1), false)]);
|
|
}
|
|
|
|
#[test]
|
|
fn hidden_layers_export_nothing() {
|
|
let rectangle = DocumentNode {
|
|
visible: false,
|
|
..proto_node(graphene_std::vector::generator_nodes::rectangle::IDENTIFIER, Vec::new())
|
|
};
|
|
let mut network = NodeNetwork {
|
|
exports: vec![NodeInput::node(NodeId(10), 0)],
|
|
nodes: [(NodeId(10), rectangle)].into_iter().collect(),
|
|
..Default::default()
|
|
};
|
|
|
|
assert_eq!(export_layers(&mut network, &[NodeId(10)]), None);
|
|
assert_eq!(exported_stack(&network), (Vec::new(), NodeInput::type_default(list!(Graphic), false)));
|
|
}
|
|
}
|