svg-filters & basic parser #15

Merged
schrottkatze merged 67 commits from schrottkatze/iowo:svg-filters into main 2024-07-08 18:29:05 +00:00
6 changed files with 46 additions and 11 deletions
Showing only changes of commit e17fffb66b - Show all commits

View file

@ -1,10 +1,10 @@
mod color_matrix; mod color_matrix;
mod blend;
mod complex; mod complex;
mod gaussian_blur; mod gaussian_blur;
mod offset; mod offset;
mod blend {}
mod component_transfer {} mod component_transfer {}
mod composite {} mod composite {}
mod convolve_matrix {} 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!( assert_eq!(
doc.generate_svg(), doc.generate_svg(),
r#"<svg><filter id="blur"><feGaussianBlur stdDeviation="30 30" in="SourceGraphic"/></filter></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] #[test]
fn test_offset_simple() { fn test_offset_simple() {
let mut doc = SvgDocument::new(); let mut doc = SvgDocument::new();
let mut offset = doc.create_filter("offset"); let offset = doc.create_filter("offset");
offset.offset(StandardInput::SourceGraphic, 25., -25.); offset.offset(StandardInput::SourceGraphic, 25., -25.);
assert_eq!( assert_eq!(
doc.generate_svg(), doc.generate_svg(),
r#"<svg><filter id="offset"><feOffset dx="25" dy="-25" in="SourceGraphic"/></filter></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 std::fmt::{Debug, Display};
use petgraph::{data::Build, prelude::NodeIndex, prelude::*, visit::IntoNeighborsDirected}; use petgraph::{prelude::NodeIndex, prelude::*};
use crate::Node; use crate::Node;
use self::edge::Edge;
use super::nodes::standard_input::StandardInput; use super::nodes::standard_input::StandardInput;
#[derive(Debug)] #[derive(Debug)]
@ -141,9 +139,12 @@ impl FilterGraph {
} }
pub mod abstracted_inputs { 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}; use super::{FilterGraph, NodeInput};
@ -178,6 +179,20 @@ pub mod abstracted_inputs {
node_idx 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( pub fn composite_arithmetic(
&mut self, &mut self,
r#in: impl Into<NodeInput>, r#in: impl Into<NodeInput>,

View file

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