svg-filters: blend node

This commit is contained in:
Schrottkatze 2024-03-19 15:15:36 +01:00
parent f59062cf88
commit e17fffb66b
No known key found for this signature in database
6 changed files with 46 additions and 11 deletions

View file

@ -1,10 +1,10 @@
mod color_matrix;
mod blend;
mod complex;
mod gaussian_blur;
mod offset;
mod blend {}
mod component_transfer {}
mod composite {}
mod convolve_matrix {}

View file

@ -0,0 +1,20 @@
use crate::{
codegen::SvgDocument,
types::nodes::{primitives::blend::BlendMode, standard_input::StandardInput},
};
#[test]
fn test_offset_blend() {
let mut doc = SvgDocument::new();
let blend = doc.create_filter("blend");
let offset0 = blend.offset(StandardInput::SourceGraphic, 100., 0.);
let offset1 = blend.offset(StandardInput::SourceGraphic, -100., 0.);
blend.blend(offset0, offset1, BlendMode::Multiply);
assert_eq!(
doc.generate_svg(),
r#"<svg><filter id="blend"><feOffset dx="-100" dy="0" in="SourceGraphic" result="r7"/><feOffset dx="100" dy="0" in="SourceGraphic" result="r6"/><feBlend mode="multiply" in="r6" in2="r7"/></filter></svg>"#
);
}

View file

@ -9,5 +9,5 @@ fn test_simple_blur() {
assert_eq!(
doc.generate_svg(),
r#"<svg><filter id="blur"><feGaussianBlur stdDeviation="30 30" in="SourceGraphic"/></filter></svg>"#
)
);
}

View file

@ -3,12 +3,12 @@ use crate::{codegen::SvgDocument, types::nodes::standard_input::StandardInput};
#[test]
fn test_offset_simple() {
let mut doc = SvgDocument::new();
let mut offset = doc.create_filter("offset");
let offset = doc.create_filter("offset");
offset.offset(StandardInput::SourceGraphic, 25., -25.);
assert_eq!(
doc.generate_svg(),
r#"<svg><filter id="offset"><feOffset dx="25" dy="-25" in="SourceGraphic"/></filter></svg>"#
)
);
}

View file

@ -1,11 +1,9 @@
use std::fmt::{Debug, Display};
use petgraph::{data::Build, prelude::NodeIndex, prelude::*, visit::IntoNeighborsDirected};
use petgraph::{prelude::NodeIndex, prelude::*};
use crate::Node;
use self::edge::Edge;
use super::nodes::standard_input::StandardInput;
#[derive(Debug)]
@ -141,9 +139,12 @@ impl FilterGraph {
}
pub mod abstracted_inputs {
use petgraph::{data::Build, prelude::NodeIndex};
use petgraph::prelude::NodeIndex;
use crate::{types::nodes::primitives::color_matrix::ColorMatrixType, Node};
use crate::{
types::nodes::primitives::{blend::BlendMode, color_matrix::ColorMatrixType},
Node,
};
use super::{FilterGraph, NodeInput};
@ -178,6 +179,20 @@ pub mod abstracted_inputs {
node_idx
}
pub fn blend(
&mut self,
r#in: impl Into<NodeInput>,
in2: impl Into<NodeInput>,
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<NodeInput>,

View file

@ -82,7 +82,7 @@ pub enum FePrimitive {
impl WriteElement for FePrimitive {
fn attrs(&self) -> std::vec::Vec<quick_xml::events::attributes::Attribute<'_>> {
match self {
FePrimitive::Blend(_) => todo!(),
FePrimitive::Blend(el) => el.attrs(),
FePrimitive::ColorMatrix(el) => el.attrs(),
FePrimitive::ComponentTransfer(_) => todo!(),
FePrimitive::Composite(el) => el.attrs(),
@ -103,7 +103,7 @@ impl WriteElement for FePrimitive {
fn tag_name(&self) -> &'static str {
match self {
FePrimitive::Blend(_) => todo!(),
FePrimitive::Blend(el) => el.tag_name(),
FePrimitive::ColorMatrix(el) => el.tag_name(),
FePrimitive::ComponentTransfer(_) => todo!(),
FePrimitive::Composite(el) => el.tag_name(),