From aeeee54200d126aeaa850878fe3ca406684242e6 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Tue, 19 Mar 2024 18:43:30 +0100 Subject: [PATCH] svg-filters: new conditional attrs macro --- crates/svg-filters/src/lib.rs | 32 ++++++++++++ .../svg-filters/src/types/nodes/primitives.rs | 21 +------- .../src/types/nodes/primitives/turbulence.rs | 49 ++++++++++--------- 3 files changed, 61 insertions(+), 41 deletions(-) diff --git a/crates/svg-filters/src/lib.rs b/crates/svg-filters/src/lib.rs index c3ddedc..e1e0272 100644 --- a/crates/svg-filters/src/lib.rs +++ b/crates/svg-filters/src/lib.rs @@ -1,5 +1,37 @@ #![feature(lint_reasons)] +#[macro_use] +pub mod util { + macro_rules! gen_attr { + ($name:literal = $out:expr) => { + quick_xml::events::attributes::Attribute { + key: quick_xml::name::QName($name), + value: std::borrow::Cow::from(($out).to_string().into_bytes()), + } + }; + } + + macro_rules! gen_attrs { + ($($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),+]; + $(if $cond { + r.push(gen_attr!($name_ = $out_)); + })+ + r + } + }; + } +} + pub mod codegen; pub mod types; pub use types::nodes::Node; diff --git a/crates/svg-filters/src/types/nodes/primitives.rs b/crates/svg-filters/src/types/nodes/primitives.rs index a596639..f3c101a 100644 --- a/crates/svg-filters/src/types/nodes/primitives.rs +++ b/crates/svg-filters/src/types/nodes/primitives.rs @@ -4,23 +4,6 @@ use quick_xml::{events::attributes::Attribute, ElementWriter, Writer}; use super::CommonAttrs; -macro_rules! gen_attr { - ($name:literal = $out:expr) => { - quick_xml::events::attributes::Attribute { - key: quick_xml::name::QName($name), - value: std::borrow::Cow::from(($out).to_string().into_bytes()), - } - }; -} - -macro_rules! gen_attrs { - ($($name:literal = $out:expr),+) => { - vec![ - $(gen_attr!($name = $out)),+ - ] - }; -} - pub mod blend; pub mod color_matrix; pub mod component_transfer; @@ -114,7 +97,7 @@ impl WriteElement for FePrimitive { FePrimitive::Offset(el) => el.attrs(), FePrimitive::SpecularLighting(_) => todo!(), FePrimitive::Tile(_) => todo!(), - FePrimitive::Turbulence(_) => todo!(), + FePrimitive::Turbulence(el) => el.attrs(), } } @@ -135,7 +118,7 @@ impl WriteElement for FePrimitive { FePrimitive::Offset(el) => el.tag_name(), FePrimitive::SpecularLighting(_) => todo!(), FePrimitive::Tile(_) => todo!(), - FePrimitive::Turbulence(_) => todo!(), + FePrimitive::Turbulence(el) => el.tag_name(), } } } diff --git a/crates/svg-filters/src/types/nodes/primitives/turbulence.rs b/crates/svg-filters/src/types/nodes/primitives/turbulence.rs index d538006..2cc59e5 100644 --- a/crates/svg-filters/src/types/nodes/primitives/turbulence.rs +++ b/crates/svg-filters/src/types/nodes/primitives/turbulence.rs @@ -3,38 +3,43 @@ use super::WriteElement; /// [feTurbulence](https://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement) #[derive(Debug)] pub struct Turbulence { - base_frequency: (f32, f32), - num_octaves: u16, - seed: u32, - stitch_tiles: StitchTiles, + pub base_frequency: (f32, f32), + pub num_octaves: u16, + pub seed: u32, + pub stitch_tiles: StitchTiles, // attr name: type - noise_type: NoiseType, + pub noise_type: NoiseType, } 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) - ); + // 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.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.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.stitch_tiles != StitchTiles::NoStitch { + // r.push(gen_attr!(b"stitchTiles" = "stitch")); + // } - if self.noise_type != NoiseType::Turbulence { - r.push(gen_attr!(b"type" = "fractalNoise")); - } - - r + // 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" + ] } fn tag_name(&self) -> &'static str {