From 919a3bb377ba4654153861b8bbc6b90065f14442 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Fri, 22 Mar 2024 16:47:21 +0100 Subject: [PATCH] svg-filters: seperate out abstractions and add flood abstraction --- crates/svg-filters/src/types/graph.rs | 154 +---------------- .../src/types/graph/abstracted_inputs.rs | 155 ++++++++++++++++++ crates/svg-filters/src/types/nodes.rs | 13 ++ .../svg-filters/src/types/nodes/primitives.rs | 6 +- .../src/types/nodes/primitives/flood.rs | 4 +- 5 files changed, 174 insertions(+), 158 deletions(-) create mode 100644 crates/svg-filters/src/types/graph/abstracted_inputs.rs diff --git a/crates/svg-filters/src/types/graph.rs b/crates/svg-filters/src/types/graph.rs index 524ba92..4b63a87 100644 --- a/crates/svg-filters/src/types/graph.rs +++ b/crates/svg-filters/src/types/graph.rs @@ -138,158 +138,6 @@ impl FilterGraph { } } -pub mod abstracted_inputs { - use petgraph::{data::Build, prelude::NodeIndex}; - - use crate::{ - types::nodes::primitives::{ - blend::BlendMode, color_matrix::ColorMatrixType, component_transfer::TransferFn, - }, - Node, - }; - - use super::{FilterGraph, NodeInput}; - - impl FilterGraph { - pub fn color_matrix( - &mut self, - r#in: impl Into, - cm_type: ColorMatrixType, - ) -> NodeIndex { - let node_idx = self.dag.add_node(Node::color_matrix(cm_type)); - self.dag - .add_edge(self.resolve_input(r#in.into()), node_idx, ()); - node_idx - } - - pub fn offset(&mut self, r#in: impl Into, dx: f32, dy: f32) -> NodeIndex { - let node_idx = self.dag.add_node(Node::offset(dx, dy)); - self.dag - .add_edge(self.resolve_input(r#in.into()), node_idx, ()); - node_idx - } - - pub fn gaussian_blur_xy( - &mut self, - r#in: impl Into, - x: u16, - y: u16, - ) -> NodeIndex { - let node_idx = self.dag.add_node(Node::gaussian_blur_xy(x, y)); - self.dag - .add_edge(self.resolve_input(r#in.into()), node_idx, ()); - node_idx - } - - pub fn blend( - &mut self, - r#in: impl Into, - in2: impl Into, - mode: BlendMode, - ) -> NodeIndex { - let node_idx = self.dag.add_node(Node::blend(mode)); - self.dag - .add_edge(self.resolve_input(r#in.into()), node_idx, ()); - self.dag - .add_edge(self.resolve_input(in2.into()), node_idx, ()); - node_idx - } - - pub fn composite_arithmetic( - &mut self, - r#in: impl Into, - in2: impl Into, - k1: f32, - k2: f32, - k3: f32, - k4: f32, - ) -> NodeIndex { - let node_idx = self - .dag - .add_node(Node::composite_arithmetic(k1, k2, k3, k4)); - self.dag - .add_edge(self.resolve_input(r#in.into()), node_idx, ()); - self.dag - .add_edge(self.resolve_input(in2.into()), node_idx, ()); - node_idx - } - - pub fn component_transfer_rgba( - &mut self, - r#in: impl Into, - r: TransferFn, - g: TransferFn, - b: TransferFn, - a: TransferFn, - ) -> NodeIndex { - let node_idx = self.dag.add_node(Node::component_transfer_rgba(r, g, b, a)); - self.dag - .add_edge(self.resolve_input(r#in.into()), node_idx, ()); - node_idx - } - pub fn component_transfer_rgb( - &mut self, - r#in: impl Into, - r: TransferFn, - g: TransferFn, - b: TransferFn, - ) -> NodeIndex { - let node_idx = self.dag.add_node(Node::component_transfer_rgb(r, g, b)); - self.dag - .add_edge(self.resolve_input(r#in.into()), node_idx, ()); - node_idx - } - pub fn component_transfer_r( - &mut self, - r#in: impl Into, - func: TransferFn, - ) -> NodeIndex { - let node_idx = self.dag.add_node(Node::component_transfer_r(func)); - self.dag - .add_edge(self.resolve_input(r#in.into()), node_idx, ()); - node_idx - } - pub fn component_transfer_g( - &mut self, - r#in: impl Into, - func: TransferFn, - ) -> NodeIndex { - let node_idx = self.dag.add_node(Node::component_transfer_g(func)); - self.dag - .add_edge(self.resolve_input(r#in.into()), node_idx, ()); - node_idx - } - pub fn component_transfer_b( - &mut self, - r#in: impl Into, - func: TransferFn, - ) -> NodeIndex { - let node_idx = self.dag.add_node(Node::component_transfer_b(func)); - self.dag - .add_edge(self.resolve_input(r#in.into()), node_idx, ()); - node_idx - } - pub fn component_transfer_a( - &mut self, - r#in: impl Into, - func: TransferFn, - ) -> NodeIndex { - let node_idx = self.dag.add_node(Node::component_transfer_a(func)); - self.dag - .add_edge(self.resolve_input(r#in.into()), node_idx, ()); - node_idx - } - pub fn component_transfer_single( - &mut self, - r#in: impl Into, - func: TransferFn, - ) -> NodeIndex { - let node_idx = self.dag.add_node(Node::component_transfer_single(func)); - self.dag - .add_edge(self.resolve_input(r#in.into()), node_idx, ()); - node_idx - } - } -} +pub mod abstracted_inputs; pub mod edge; diff --git a/crates/svg-filters/src/types/graph/abstracted_inputs.rs b/crates/svg-filters/src/types/graph/abstracted_inputs.rs new file mode 100644 index 0000000..ca576c5 --- /dev/null +++ b/crates/svg-filters/src/types/graph/abstracted_inputs.rs @@ -0,0 +1,155 @@ +use csscolorparser::Color; +use petgraph::{data::Build, prelude::NodeIndex}; + +use crate::{ + types::nodes::primitives::{ + blend::BlendMode, color_matrix::ColorMatrixType, component_transfer::TransferFn, + }, + Node, +}; + +use super::{FilterGraph, NodeInput}; + +impl FilterGraph { + pub fn color_matrix( + &mut self, + r#in: impl Into, + cm_type: ColorMatrixType, + ) -> NodeIndex { + let node_idx = self.dag.add_node(Node::color_matrix(cm_type)); + self.dag + .add_edge(self.resolve_input(r#in.into()), node_idx, ()); + node_idx + } + + pub fn offset(&mut self, r#in: impl Into, dx: f32, dy: f32) -> NodeIndex { + let node_idx = self.dag.add_node(Node::offset(dx, dy)); + self.dag + .add_edge(self.resolve_input(r#in.into()), node_idx, ()); + node_idx + } + + pub fn gaussian_blur_xy(&mut self, r#in: impl Into, x: u16, y: u16) -> NodeIndex { + let node_idx = self.dag.add_node(Node::gaussian_blur_xy(x, y)); + self.dag + .add_edge(self.resolve_input(r#in.into()), node_idx, ()); + node_idx + } + + pub fn blend( + &mut self, + r#in: impl Into, + in2: impl Into, + mode: BlendMode, + ) -> NodeIndex { + let node_idx = self.dag.add_node(Node::blend(mode)); + self.dag + .add_edge(self.resolve_input(r#in.into()), node_idx, ()); + self.dag + .add_edge(self.resolve_input(in2.into()), node_idx, ()); + node_idx + } + + pub fn composite_arithmetic( + &mut self, + r#in: impl Into, + in2: impl Into, + k1: f32, + k2: f32, + k3: f32, + k4: f32, + ) -> NodeIndex { + let node_idx = self + .dag + .add_node(Node::composite_arithmetic(k1, k2, k3, k4)); + self.dag + .add_edge(self.resolve_input(r#in.into()), node_idx, ()); + self.dag + .add_edge(self.resolve_input(in2.into()), node_idx, ()); + node_idx + } + + pub fn component_transfer_rgba( + &mut self, + r#in: impl Into, + r: TransferFn, + g: TransferFn, + b: TransferFn, + a: TransferFn, + ) -> NodeIndex { + let node_idx = self.dag.add_node(Node::component_transfer_rgba(r, g, b, a)); + self.dag + .add_edge(self.resolve_input(r#in.into()), node_idx, ()); + node_idx + } + pub fn component_transfer_rgb( + &mut self, + r#in: impl Into, + r: TransferFn, + g: TransferFn, + b: TransferFn, + ) -> NodeIndex { + let node_idx = self.dag.add_node(Node::component_transfer_rgb(r, g, b)); + self.dag + .add_edge(self.resolve_input(r#in.into()), node_idx, ()); + node_idx + } + pub fn component_transfer_r( + &mut self, + r#in: impl Into, + func: TransferFn, + ) -> NodeIndex { + let node_idx = self.dag.add_node(Node::component_transfer_r(func)); + self.dag + .add_edge(self.resolve_input(r#in.into()), node_idx, ()); + node_idx + } + pub fn component_transfer_g( + &mut self, + r#in: impl Into, + func: TransferFn, + ) -> NodeIndex { + let node_idx = self.dag.add_node(Node::component_transfer_g(func)); + self.dag + .add_edge(self.resolve_input(r#in.into()), node_idx, ()); + node_idx + } + pub fn component_transfer_b( + &mut self, + r#in: impl Into, + func: TransferFn, + ) -> NodeIndex { + let node_idx = self.dag.add_node(Node::component_transfer_b(func)); + self.dag + .add_edge(self.resolve_input(r#in.into()), node_idx, ()); + node_idx + } + pub fn component_transfer_a( + &mut self, + r#in: impl Into, + func: TransferFn, + ) -> NodeIndex { + let node_idx = self.dag.add_node(Node::component_transfer_a(func)); + self.dag + .add_edge(self.resolve_input(r#in.into()), node_idx, ()); + node_idx + } + pub fn component_transfer_single( + &mut self, + r#in: impl Into, + func: TransferFn, + ) -> NodeIndex { + let node_idx = self.dag.add_node(Node::component_transfer_single(func)); + self.dag + .add_edge(self.resolve_input(r#in.into()), node_idx, ()); + node_idx + } + + pub fn flood(&mut self, flood_color: Color, flood_opacity: f32) -> NodeIndex { + self.dag.add_node(Node::flood(flood_color, flood_opacity)) + } + + pub fn flood_opaque(&mut self, flood_color: Color) -> NodeIndex { + self.dag.add_node(Node::flood_opaque(flood_color)) + } +} diff --git a/crates/svg-filters/src/types/nodes.rs b/crates/svg-filters/src/types/nodes.rs index c9f9334..ba0340b 100644 --- a/crates/svg-filters/src/types/nodes.rs +++ b/crates/svg-filters/src/types/nodes.rs @@ -1,5 +1,6 @@ use std::borrow::Cow; +use csscolorparser::Color; use quick_xml::{events::attributes::Attribute, name::QName}; use self::{ @@ -8,6 +9,7 @@ use self::{ color_matrix::{ColorMatrix, ColorMatrixType}, component_transfer::{ComponentTransfer, TransferFn}, composite::{Composite, CompositeOperator}, + flood::Flood, gaussian_blur::GaussianBlur, offset::Offset, FePrimitive, @@ -194,4 +196,15 @@ impl Node { pub fn component_transfer_single(func: TransferFn) -> Self { Self::component_transfer_rgb(func.clone(), func.clone(), func) } + + pub fn flood(flood_color: Color, flood_opacity: f32) -> Self { + Self::simple(FePrimitive::Flood(Flood { + flood_color, + flood_opacity, + })) + } + + pub fn flood_opaque(flood_color: Color) -> Self { + Self::flood(flood_color, 1.) + } } diff --git a/crates/svg-filters/src/types/nodes/primitives.rs b/crates/svg-filters/src/types/nodes/primitives.rs index 5cdd232..a46ee9b 100644 --- a/crates/svg-filters/src/types/nodes/primitives.rs +++ b/crates/svg-filters/src/types/nodes/primitives.rs @@ -91,7 +91,7 @@ impl WriteElement for FePrimitive { FePrimitive::ConvolveMatrix(_) => todo!(), FePrimitive::DiffuseLighting(_) => todo!(), FePrimitive::DisplacementMap(_) => todo!(), - FePrimitive::Flood(_) => todo!(), + FePrimitive::Flood(el) => el.attrs(), FePrimitive::Image(_) => todo!(), FePrimitive::Merge(_) => todo!(), FePrimitive::Morphology(_) => todo!(), @@ -112,7 +112,7 @@ impl WriteElement for FePrimitive { FePrimitive::ConvolveMatrix(_) => todo!(), FePrimitive::DiffuseLighting(_) => todo!(), FePrimitive::DisplacementMap(_) => todo!(), - FePrimitive::Flood(_) => todo!(), + FePrimitive::Flood(el) => el.tag_name(), FePrimitive::Image(_) => todo!(), FePrimitive::Merge(_) => todo!(), FePrimitive::Morphology(_) => todo!(), @@ -139,7 +139,7 @@ impl WriteElement for FePrimitive { FePrimitive::ConvolveMatrix(_) => todo!(), FePrimitive::DiffuseLighting(_) => todo!(), FePrimitive::DisplacementMap(_) => todo!(), - FePrimitive::Flood(_) => todo!(), + FePrimitive::Flood(el) => el.element_writer(writer, common, inputs, output), FePrimitive::Image(_) => todo!(), FePrimitive::Merge(_) => todo!(), FePrimitive::Morphology(_) => todo!(), diff --git a/crates/svg-filters/src/types/nodes/primitives/flood.rs b/crates/svg-filters/src/types/nodes/primitives/flood.rs index 4daa3c8..df912ef 100644 --- a/crates/svg-filters/src/types/nodes/primitives/flood.rs +++ b/crates/svg-filters/src/types/nodes/primitives/flood.rs @@ -5,8 +5,8 @@ use super::WriteElement; /// [feFlood](https://www.w3.org/TR/SVG11/filters.html#feFloodElement) #[derive(Debug)] pub struct Flood { - flood_color: Color, - flood_opacity: f32, + pub flood_color: Color, + pub flood_opacity: f32, } impl WriteElement for Flood {