svg-filters: rework macro slightly

This commit is contained in:
Schrottkatze 2024-03-20 19:11:42 +01:00
parent aeeee54200
commit c31a158d9b
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
11 changed files with 52 additions and 96 deletions

View file

@ -12,23 +12,23 @@ pub mod util {
} }
macro_rules! gen_attrs { macro_rules! gen_attrs {
($($name:literal = $out:expr),+) => { ($($name:literal: $out:expr),+) => {
vec![ vec![
$(gen_attr!($name = $out)),+ $(gen_attr!($name = $out)),+
] ]
}; };
} ($($cond:expr => $name:literal: $out:expr),+) => {
macro_rules! gen_attrs_with_conds {
($($name:literal = $out:expr),+, $($cond:expr => $name_:literal = $out_:expr),+) => {
{ {
let mut r = gen_attrs![$($name = $out),+]; let mut r = Vec::new();
$(if $cond { $(if $cond {
r.push(gen_attr!($name_ = $out_)); r.push(gen_attr!($name = $out));
})+ })+
r r
} }
}; };
($other:ident; $($cond:expr => $name:literal: $out:expr),+) => {
$other.append(&mut gen_attrs![$($cond => $name: $out),+]);
};
} }
} }

View file

@ -5,7 +5,12 @@ use svg_filters::{
types::{ types::{
graph::edge::Edge, graph::edge::Edge,
nodes::{ nodes::{
primitives::{blend::BlendMode, color_matrix::ColorMatrixType}, primitives::{
blend::BlendMode,
color_matrix::ColorMatrixType,
turbulence::{NoiseType, StitchTiles, Turbulence},
FePrimitive,
},
standard_input::StandardInput, standard_input::StandardInput,
}, },
}, },
@ -15,11 +20,15 @@ use svg_filters::{
fn main() { fn main() {
let mut doc = SvgDocument::new(); 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.); noise.add_node(Node::simple(FePrimitive::Turbulence(Turbulence {
let offset1 = blend.offset(StandardInput::SourceGraphic, -100., 0.); base_frequency: (0.2, 0.2),
blend.blend(offset0, offset1, BlendMode::Multiply); num_octaves: 1,
seed: 2,
stitch_tiles: StitchTiles::NoStitch,
noise_type: NoiseType::FractalNoise,
})));
eprintln!("{}", doc.generate_svg_pretty()); eprintln!("{}", doc.generate_svg_pretty());
println!("{}", doc.generate_svg()); println!("{}", doc.generate_svg());

View file

@ -45,36 +45,12 @@ pub struct CommonAttrs {
impl From<CommonAttrs> for Vec<Attribute<'_>> { impl From<CommonAttrs> for Vec<Attribute<'_>> {
fn from(val: CommonAttrs) -> Self { fn from(val: CommonAttrs) -> Self {
let mut r = Vec::new(); gen_attrs![
if !val.x.is_zero() { !val.x.is_zero() => b"x": val.x,
r.push(Attribute { !val.y.is_zero() => b"y": val.y,
key: QName(b"x"), !val.width.is_zero() => b"width": val.width,
value: Cow::from(val.x.to_string().into_bytes()), !val.height.is_zero() => b"height": val.height
}); ]
}
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
} }
} }

View file

@ -1,6 +1,4 @@
use std::{borrow::Cow, fmt::Display}; use std::fmt::Display;
use quick_xml::{events::attributes::Attribute, name::QName};
use super::WriteElement; use super::WriteElement;
@ -29,7 +27,7 @@ impl WriteElement for Blend {
if let BlendMode::Normal = self.mode { if let BlendMode::Normal = self.mode {
Vec::new() Vec::new()
} else { } else {
gen_attrs![b"mode" = self.mode] gen_attrs![b"mode": self.mode]
} }
} }

View file

@ -16,7 +16,7 @@ impl WriteElement for ColorMatrix {
fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> { fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> {
match &self.cm_type { match &self.cm_type {
ColorMatrixType::Matrix(v) => gen_attrs![ ColorMatrixType::Matrix(v) => gen_attrs![
b"values" = v b"values": v
.iter() .iter()
.map(std::string::ToString::to_string) .map(std::string::ToString::to_string)
.reduce(|mut acc, e| { .reduce(|mut acc, e| {
@ -27,7 +27,7 @@ impl WriteElement for ColorMatrix {
.expect("fixed length arr should always work") .expect("fixed length arr should always work")
], ],
ColorMatrixType::Saturate(v) | ColorMatrixType::HueRotate(v) => { ColorMatrixType::Saturate(v) | ColorMatrixType::HueRotate(v) => {
gen_attrs![b"values" = v] gen_attrs![b"values": v]
} }
ColorMatrixType::LuminanceToAlpha => Vec::new(), ColorMatrixType::LuminanceToAlpha => Vec::new(),
} }

View file

@ -70,10 +70,10 @@ impl WriteElement for Composite {
// }, // },
// ]); // ]);
r.append(&mut gen_attrs![ r.append(&mut gen_attrs![
b"k1" = k1, b"k1": k1,
b"k2" = k2, b"k2": k2,
b"k3" = k3, b"k3": k3,
b"k4" = k4 b"k4": k4
]); ]);
} }

View file

@ -1,7 +1,4 @@
use std::borrow::Cow;
use csscolorparser::Color; use csscolorparser::Color;
use quick_xml::{events::attributes::Attribute, name::QName};
use super::WriteElement; use super::WriteElement;
@ -15,8 +12,8 @@ pub struct Flood {
impl WriteElement for Flood { impl WriteElement for Flood {
fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> { fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> {
gen_attrs![ gen_attrs![
b"flood-color" = self.flood_color.to_hex_string(), b"flood-color": self.flood_color.to_hex_string(),
b"flood-opacity" = self.flood_opacity b"flood-opacity": self.flood_opacity
] ]
} }

View file

@ -26,7 +26,7 @@ impl GaussianBlur {
impl WriteElement for GaussianBlur { impl WriteElement for GaussianBlur {
fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> { fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> {
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 { fn tag_name(&self) -> &'static str {

View file

@ -1,8 +1,5 @@
use std::{borrow::Cow, fmt::Display};
use quick_xml::{events::attributes::Attribute, name::QName};
use super::WriteElement; use super::WriteElement;
use std::fmt::Display;
/// [feMorphology](https://www.w3.org/TR/SVG11/filters.html#feMorphologyElement) /// [feMorphology](https://www.w3.org/TR/SVG11/filters.html#feMorphologyElement)
#[derive(Debug)] #[derive(Debug)]
@ -14,8 +11,8 @@ pub struct Morphology {
impl WriteElement for Morphology { impl WriteElement for Morphology {
fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> { fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> {
gen_attrs![ gen_attrs![
b"operator" = self.operator, b"operator": self.operator,
b"radius" = format!("{} {}", self.radius.0, self.radius.1) b"radius": format!("{} {}", self.radius.0, self.radius.1)
] ]
} }

View file

@ -1,7 +1,3 @@
use std::borrow::Cow;
use quick_xml::{events::attributes::Attribute, name::QName};
use super::WriteElement; use super::WriteElement;
/// [feOffset](https://www.w3.org/TR/SVG11/filters.html#feOffsetElement) /// [feOffset](https://www.w3.org/TR/SVG11/filters.html#feOffsetElement)
@ -19,7 +15,7 @@ impl Offset {
impl WriteElement for Offset { impl WriteElement for Offset {
fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> { fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> {
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 { fn tag_name(&self) -> &'static str {

View file

@ -14,32 +14,15 @@ pub struct Turbulence {
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) gen_attrs![
// ); r;
self.num_octaves != 1 => b"numOctaves": self.num_octaves,
// if self.num_octaves != 1 { self.seed != 0 => b"seed": self.seed,
// r.push(gen_attr!(b"numOctaves" = self.num_octaves)); self.stitch_tiles != StitchTiles::NoStitch => b"stitchTiles": "stitch",
// } self.noise_type != NoiseType::Turbulence => b"type": "fractalNoise"
];
// if self.seed != 0 { r
// 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"
]
} }
fn tag_name(&self) -> &'static str { fn tag_name(&self) -> &'static str {