diff --git a/crates/svg-filters/src/lib.rs b/crates/svg-filters/src/lib.rs index e1e0272..2afb148 100644 --- a/crates/svg-filters/src/lib.rs +++ b/crates/svg-filters/src/lib.rs @@ -12,23 +12,23 @@ pub mod util { } macro_rules! gen_attrs { - ($($name:literal = $out:expr),+) => { + ($($name:literal: $out:expr),+) => { vec![ $(gen_attr!($name = $out)),+ ] }; - } - - macro_rules! gen_attrs_with_conds { - ($($name:literal = $out:expr),+, $($cond:expr => $name_:literal = $out_:expr),+) => { - { - let mut r = gen_attrs![$($name = $out),+]; + ($($cond:expr => $name:literal: $out:expr),+) => { + { + let mut r = Vec::new(); $(if $cond { - r.push(gen_attr!($name_ = $out_)); + r.push(gen_attr!($name = $out)); })+ r } }; + ($other:ident; $($cond:expr => $name:literal: $out:expr),+) => { + $other.append(&mut gen_attrs![$($cond => $name: $out),+]); + }; } } diff --git a/crates/svg-filters/src/main.rs b/crates/svg-filters/src/main.rs index 1bf86c8..2fc8df3 100644 --- a/crates/svg-filters/src/main.rs +++ b/crates/svg-filters/src/main.rs @@ -5,7 +5,12 @@ use svg_filters::{ types::{ graph::edge::Edge, nodes::{ - primitives::{blend::BlendMode, color_matrix::ColorMatrixType}, + primitives::{ + blend::BlendMode, + color_matrix::ColorMatrixType, + turbulence::{NoiseType, StitchTiles, Turbulence}, + FePrimitive, + }, standard_input::StandardInput, }, }, @@ -15,11 +20,15 @@ use svg_filters::{ fn main() { let mut doc = SvgDocument::new(); - let blend = doc.create_filter("blend"); + let noise = doc.create_filter("noise"); - let offset0 = blend.offset(StandardInput::SourceGraphic, 100., 0.); - let offset1 = blend.offset(StandardInput::SourceGraphic, -100., 0.); - blend.blend(offset0, offset1, BlendMode::Multiply); + noise.add_node(Node::simple(FePrimitive::Turbulence(Turbulence { + base_frequency: (0.2, 0.2), + num_octaves: 1, + seed: 2, + stitch_tiles: StitchTiles::NoStitch, + noise_type: NoiseType::FractalNoise, + }))); eprintln!("{}", doc.generate_svg_pretty()); println!("{}", doc.generate_svg()); diff --git a/crates/svg-filters/src/types/nodes.rs b/crates/svg-filters/src/types/nodes.rs index 656574d..2edc63b 100644 --- a/crates/svg-filters/src/types/nodes.rs +++ b/crates/svg-filters/src/types/nodes.rs @@ -45,36 +45,12 @@ pub struct CommonAttrs { impl From for Vec> { fn from(val: CommonAttrs) -> Self { - let mut r = Vec::new(); - if !val.x.is_zero() { - r.push(Attribute { - key: QName(b"x"), - value: Cow::from(val.x.to_string().into_bytes()), - }); - } - - if !val.y.is_zero() { - r.push(Attribute { - key: QName(b"y"), - value: Cow::from(val.y.to_string().into_bytes()), - }); - } - - if !val.width.is_zero() { - r.push(Attribute { - key: QName(b"width"), - value: Cow::from(val.width.to_string().into_bytes()), - }); - } - - if !val.height.is_zero() { - r.push(Attribute { - key: QName(b"height"), - value: Cow::from(val.height.to_string().into_bytes()), - }); - } - - r + gen_attrs![ + !val.x.is_zero() => b"x": val.x, + !val.y.is_zero() => b"y": val.y, + !val.width.is_zero() => b"width": val.width, + !val.height.is_zero() => b"height": val.height + ] } } diff --git a/crates/svg-filters/src/types/nodes/primitives/blend.rs b/crates/svg-filters/src/types/nodes/primitives/blend.rs index 1887042..3e28401 100644 --- a/crates/svg-filters/src/types/nodes/primitives/blend.rs +++ b/crates/svg-filters/src/types/nodes/primitives/blend.rs @@ -1,6 +1,4 @@ -use std::{borrow::Cow, fmt::Display}; - -use quick_xml::{events::attributes::Attribute, name::QName}; +use std::fmt::Display; use super::WriteElement; @@ -29,7 +27,7 @@ impl WriteElement for Blend { if let BlendMode::Normal = self.mode { Vec::new() } else { - gen_attrs![b"mode" = self.mode] + gen_attrs![b"mode": self.mode] } } diff --git a/crates/svg-filters/src/types/nodes/primitives/color_matrix.rs b/crates/svg-filters/src/types/nodes/primitives/color_matrix.rs index 31e4130..3a31b7d 100644 --- a/crates/svg-filters/src/types/nodes/primitives/color_matrix.rs +++ b/crates/svg-filters/src/types/nodes/primitives/color_matrix.rs @@ -16,7 +16,7 @@ impl WriteElement for ColorMatrix { fn attrs(&self) -> Vec { match &self.cm_type { ColorMatrixType::Matrix(v) => gen_attrs![ - b"values" = v + b"values": v .iter() .map(std::string::ToString::to_string) .reduce(|mut acc, e| { @@ -27,7 +27,7 @@ impl WriteElement for ColorMatrix { .expect("fixed length arr should always work") ], ColorMatrixType::Saturate(v) | ColorMatrixType::HueRotate(v) => { - gen_attrs![b"values" = v] + gen_attrs![b"values": v] } ColorMatrixType::LuminanceToAlpha => Vec::new(), } diff --git a/crates/svg-filters/src/types/nodes/primitives/composite.rs b/crates/svg-filters/src/types/nodes/primitives/composite.rs index b877fe8..45f639c 100644 --- a/crates/svg-filters/src/types/nodes/primitives/composite.rs +++ b/crates/svg-filters/src/types/nodes/primitives/composite.rs @@ -70,10 +70,10 @@ impl WriteElement for Composite { // }, // ]); r.append(&mut gen_attrs![ - b"k1" = k1, - b"k2" = k2, - b"k3" = k3, - b"k4" = k4 + b"k1": k1, + b"k2": k2, + b"k3": k3, + b"k4": k4 ]); } diff --git a/crates/svg-filters/src/types/nodes/primitives/flood.rs b/crates/svg-filters/src/types/nodes/primitives/flood.rs index 3342ef7..4daa3c8 100644 --- a/crates/svg-filters/src/types/nodes/primitives/flood.rs +++ b/crates/svg-filters/src/types/nodes/primitives/flood.rs @@ -1,7 +1,4 @@ -use std::borrow::Cow; - use csscolorparser::Color; -use quick_xml::{events::attributes::Attribute, name::QName}; use super::WriteElement; @@ -15,8 +12,8 @@ pub struct Flood { impl WriteElement for Flood { fn attrs(&self) -> Vec { gen_attrs![ - b"flood-color" = self.flood_color.to_hex_string(), - b"flood-opacity" = self.flood_opacity + b"flood-color": self.flood_color.to_hex_string(), + b"flood-opacity": self.flood_opacity ] } diff --git a/crates/svg-filters/src/types/nodes/primitives/gaussian_blur.rs b/crates/svg-filters/src/types/nodes/primitives/gaussian_blur.rs index e255df4..cd18f72 100644 --- a/crates/svg-filters/src/types/nodes/primitives/gaussian_blur.rs +++ b/crates/svg-filters/src/types/nodes/primitives/gaussian_blur.rs @@ -26,7 +26,7 @@ impl GaussianBlur { impl WriteElement for GaussianBlur { fn attrs(&self) -> Vec { - gen_attrs![b"stdDeviation" = format!("{} {}", self.std_deviation.0, self.std_deviation.1)] + gen_attrs![b"stdDeviation": format!("{} {}", self.std_deviation.0, self.std_deviation.1)] } fn tag_name(&self) -> &'static str { diff --git a/crates/svg-filters/src/types/nodes/primitives/morphology.rs b/crates/svg-filters/src/types/nodes/primitives/morphology.rs index f9087d6..f6b7bbf 100644 --- a/crates/svg-filters/src/types/nodes/primitives/morphology.rs +++ b/crates/svg-filters/src/types/nodes/primitives/morphology.rs @@ -1,8 +1,5 @@ -use std::{borrow::Cow, fmt::Display}; - -use quick_xml::{events::attributes::Attribute, name::QName}; - use super::WriteElement; +use std::fmt::Display; /// [feMorphology](https://www.w3.org/TR/SVG11/filters.html#feMorphologyElement) #[derive(Debug)] @@ -14,8 +11,8 @@ pub struct Morphology { impl WriteElement for Morphology { fn attrs(&self) -> Vec { gen_attrs![ - b"operator" = self.operator, - b"radius" = format!("{} {}", self.radius.0, self.radius.1) + b"operator": self.operator, + b"radius": format!("{} {}", self.radius.0, self.radius.1) ] } diff --git a/crates/svg-filters/src/types/nodes/primitives/offset.rs b/crates/svg-filters/src/types/nodes/primitives/offset.rs index fa17dca..7ccf2fd 100644 --- a/crates/svg-filters/src/types/nodes/primitives/offset.rs +++ b/crates/svg-filters/src/types/nodes/primitives/offset.rs @@ -1,7 +1,3 @@ -use std::borrow::Cow; - -use quick_xml::{events::attributes::Attribute, name::QName}; - use super::WriteElement; /// [feOffset](https://www.w3.org/TR/SVG11/filters.html#feOffsetElement) @@ -19,7 +15,7 @@ impl Offset { impl WriteElement for Offset { fn attrs(&self) -> Vec { - gen_attrs![b"dx" = self.dx, b"dy" = self.dy] + gen_attrs![b"dx": self.dx, b"dy": self.dy] } fn tag_name(&self) -> &'static str { diff --git a/crates/svg-filters/src/types/nodes/primitives/turbulence.rs b/crates/svg-filters/src/types/nodes/primitives/turbulence.rs index 2cc59e5..8fc4ae9 100644 --- a/crates/svg-filters/src/types/nodes/primitives/turbulence.rs +++ b/crates/svg-filters/src/types/nodes/primitives/turbulence.rs @@ -14,32 +14,15 @@ pub struct Turbulence { impl WriteElement for Turbulence { #[allow(clippy::str_to_string, reason = "in macro invocation")] fn attrs(&self) -> Vec { - // let mut r = gen_attrs!( - // b"baseFrequency" = format!("{} {}", self.base_frequency.0, self.base_frequency.1) - // ); - - // if self.num_octaves != 1 { - // r.push(gen_attr!(b"numOctaves" = self.num_octaves)); - // } - - // if self.seed != 0 { - // r.push(gen_attr!(b"seed" = self.seed)); - // } - - // if self.stitch_tiles != StitchTiles::NoStitch { - // r.push(gen_attr!(b"stitchTiles" = "stitch")); - // } - - // if self.noise_type != NoiseType::Turbulence { - // r.push(gen_attr!(b"type" = "fractalNoise")); - // } - gen_attrs_with_conds![ - b"baseFrequency" = format!("{} {}", self.base_frequency.0, self.base_frequency.1), - self.num_octaves != 1 => b"numOctaves" = self.num_octaves, - self.seed != 0 => b"seed" = self.seed, - self.stitch_tiles != StitchTiles::NoStitch => b"stitchTiles" = "stitch", - self.noise_type != NoiseType::Turbulence => b"type" = "fractalNoise" - ] + let mut r = gen_attrs![b"baseFrequency": format!("{} {}", self.base_frequency.0, self.base_frequency.1)]; + gen_attrs![ + r; + self.num_octaves != 1 => b"numOctaves": self.num_octaves, + self.seed != 0 => b"seed": self.seed, + self.stitch_tiles != StitchTiles::NoStitch => b"stitchTiles": "stitch", + self.noise_type != NoiseType::Turbulence => b"type": "fractalNoise" + ]; + r } fn tag_name(&self) -> &'static str {