forked from katzen-cafe/iowo
svg-filters: rework macro slightly
This commit is contained in:
parent
aeeee54200
commit
c31a158d9b
11 changed files with 52 additions and 96 deletions
|
@ -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),+) => {
|
||||
($($cond:expr => $name:literal: $out:expr),+) => {
|
||||
{
|
||||
let mut r = gen_attrs![$($name = $out),+];
|
||||
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),+]);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -45,36 +45,12 @@ pub struct CommonAttrs {
|
|||
|
||||
impl From<CommonAttrs> for Vec<Attribute<'_>> {
|
||||
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
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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]
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ impl WriteElement for ColorMatrix {
|
|||
fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> {
|
||||
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(),
|
||||
}
|
||||
|
|
|
@ -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
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
@ -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<quick_xml::events::attributes::Attribute> {
|
||||
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
|
||||
]
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ impl GaussianBlur {
|
|||
|
||||
impl WriteElement for GaussianBlur {
|
||||
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 {
|
||||
|
|
|
@ -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<quick_xml::events::attributes::Attribute> {
|
||||
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)
|
||||
]
|
||||
}
|
||||
|
||||
|
|
|
@ -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<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 {
|
||||
|
|
|
@ -14,32 +14,15 @@ pub struct Turbulence {
|
|||
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)
|
||||
// );
|
||||
|
||||
// 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 {
|
||||
|
|
Loading…
Reference in a new issue