svg-filters: new conditional attrs macro

This commit is contained in:
Schrottkatze 2024-03-19 18:43:30 +01:00
parent dc7d76dc26
commit aeeee54200
No known key found for this signature in database
3 changed files with 61 additions and 41 deletions

View file

@ -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;

View file

@ -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(),
}
}
}

View file

@ -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<quick_xml::events::attributes::Attribute> {
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 {