svg-filters: new conditional attrs macro

This commit is contained in:
Schrottkatze 2024-03-19 18:43:30 +01:00
parent dc7d76dc26
commit aeeee54200
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
3 changed files with 61 additions and 41 deletions

View file

@ -1,5 +1,37 @@
#![feature(lint_reasons)] #![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 codegen;
pub mod types; pub mod types;
pub use types::nodes::Node; pub use types::nodes::Node;

View file

@ -4,23 +4,6 @@ use quick_xml::{events::attributes::Attribute, ElementWriter, Writer};
use super::CommonAttrs; 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 blend;
pub mod color_matrix; pub mod color_matrix;
pub mod component_transfer; pub mod component_transfer;
@ -114,7 +97,7 @@ impl WriteElement for FePrimitive {
FePrimitive::Offset(el) => el.attrs(), FePrimitive::Offset(el) => el.attrs(),
FePrimitive::SpecularLighting(_) => todo!(), FePrimitive::SpecularLighting(_) => todo!(),
FePrimitive::Tile(_) => 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::Offset(el) => el.tag_name(),
FePrimitive::SpecularLighting(_) => todo!(), FePrimitive::SpecularLighting(_) => todo!(),
FePrimitive::Tile(_) => todo!(), FePrimitive::Tile(_) => todo!(),
FePrimitive::Turbulence(_) => todo!(), FePrimitive::Turbulence(el) => el.tag_name(),
} }
} }
} }

View file

@ -3,38 +3,43 @@ use super::WriteElement;
/// [feTurbulence](https://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement) /// [feTurbulence](https://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement)
#[derive(Debug)] #[derive(Debug)]
pub struct Turbulence { pub struct Turbulence {
base_frequency: (f32, f32), pub base_frequency: (f32, f32),
num_octaves: u16, pub num_octaves: u16,
seed: u32, pub seed: u32,
stitch_tiles: StitchTiles, pub stitch_tiles: StitchTiles,
// attr name: type // attr name: type
noise_type: NoiseType, pub noise_type: NoiseType,
} }
impl WriteElement for Turbulence { impl WriteElement for Turbulence {
#[allow(clippy::str_to_string, reason = "in macro invocation")] #[allow(clippy::str_to_string, reason = "in macro invocation")]
fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> { fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> {
let mut r = gen_attrs!( // let mut r = gen_attrs!(
b"baseFrequency" = format!("{} {}", self.base_frequency.0, self.base_frequency.1) // b"baseFrequency" = format!("{} {}", self.base_frequency.0, self.base_frequency.1)
); // );
if self.num_octaves != 1 { // if self.num_octaves != 1 {
r.push(gen_attr!(b"numOctaves" = self.num_octaves)); // r.push(gen_attr!(b"numOctaves" = self.num_octaves));
} // }
if self.seed != 0 { // if self.seed != 0 {
r.push(gen_attr!(b"seed" = self.seed)); // r.push(gen_attr!(b"seed" = self.seed));
} // }
if self.stitch_tiles != StitchTiles::NoStitch { // if self.stitch_tiles != StitchTiles::NoStitch {
r.push(gen_attr!(b"stitchTiles" = "stitch")); // r.push(gen_attr!(b"stitchTiles" = "stitch"));
} // }
if self.noise_type != NoiseType::Turbulence { // if self.noise_type != NoiseType::Turbulence {
r.push(gen_attr!(b"type" = "fractalNoise")); // r.push(gen_attr!(b"type" = "fractalNoise"));
} // }
gen_attrs_with_conds![
r 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 { fn tag_name(&self) -> &'static str {