svg-filters: get svg generation working!!!!

This commit is contained in:
Schrottkatze 2024-03-16 00:35:23 +01:00
parent 01b1880089
commit a42ec014e5
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
7 changed files with 237 additions and 85 deletions

View file

@ -62,18 +62,18 @@ impl WriteElement for FePrimitive {
fn attrs(&self) -> std::vec::Vec<quick_xml::events::attributes::Attribute<'_>> {
match self {
FePrimitive::Blend(_) => todo!(),
FePrimitive::ColorMatrix(cm) => cm.attrs(),
FePrimitive::ColorMatrix(el) => el.attrs(),
FePrimitive::ComponentTransfer(_) => todo!(),
FePrimitive::Composite(_) => todo!(),
FePrimitive::Composite(el) => el.attrs(),
FePrimitive::ConvolveMatrix(_) => todo!(),
FePrimitive::DiffuseLighting(_) => todo!(),
FePrimitive::DisplacementMap(_) => todo!(),
FePrimitive::Flood(_) => todo!(),
FePrimitive::GaussianBlur(_) => todo!(),
FePrimitive::GaussianBlur(el) => el.attrs(),
FePrimitive::Image(_) => todo!(),
FePrimitive::Merge(_) => todo!(),
FePrimitive::Morphology(_) => todo!(),
FePrimitive::Offset(_) => todo!(),
FePrimitive::Offset(el) => el.attrs(),
FePrimitive::SpecularLighting(_) => todo!(),
FePrimitive::Tile(_) => todo!(),
FePrimitive::Turbulence(_) => todo!(),
@ -83,18 +83,18 @@ impl WriteElement for FePrimitive {
fn tag_name(&self) -> &'static str {
match self {
FePrimitive::Blend(_) => todo!(),
FePrimitive::ColorMatrix(cm) => cm.tag_name(),
FePrimitive::ColorMatrix(el) => el.tag_name(),
FePrimitive::ComponentTransfer(_) => todo!(),
FePrimitive::Composite(_) => todo!(),
FePrimitive::Composite(el) => el.tag_name(),
FePrimitive::ConvolveMatrix(_) => todo!(),
FePrimitive::DiffuseLighting(_) => todo!(),
FePrimitive::DisplacementMap(_) => todo!(),
FePrimitive::Flood(_) => todo!(),
FePrimitive::GaussianBlur(_) => todo!(),
FePrimitive::GaussianBlur(el) => el.tag_name(),
FePrimitive::Image(_) => todo!(),
FePrimitive::Merge(_) => todo!(),
FePrimitive::Morphology(_) => todo!(),
FePrimitive::Offset(_) => todo!(),
FePrimitive::Offset(el) => el.tag_name(),
FePrimitive::SpecularLighting(_) => todo!(),
FePrimitive::Tile(_) => todo!(),
FePrimitive::Turbulence(_) => todo!(),

View file

@ -1,3 +1,9 @@
use std::borrow::Cow;
use quick_xml::{events::attributes::Attribute, name::QName};
use super::WriteElement;
/// [feComposite](https://www.w3.org/TR/SVG11/filters.html#feCompositeElement)
#[derive(Debug)]
pub struct Composite {
@ -25,3 +31,50 @@ pub enum CompositeOperator {
Xor,
Arithmetic { k1: f32, k2: f32, k3: f32, k4: f32 },
}
impl WriteElement for Composite {
fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> {
let (op_name, vals) = match self.operator {
CompositeOperator::Over => ("over", None),
CompositeOperator::In => ("in", None),
CompositeOperator::Out => ("out", None),
CompositeOperator::Atop => ("atop", None),
CompositeOperator::Xor => ("xor", None),
CompositeOperator::Arithmetic { k1, k2, k3, k4 } => {
("arithmetic", Some([k1, k2, k3, k4]))
}
};
let mut r = vec![Attribute {
key: QName(b"operator"),
value: Cow::from(op_name.as_bytes()),
}];
if let Some([k1, k2, k3, k4]) = vals {
r.append(&mut vec![
Attribute {
key: QName(b"k1"),
value: Cow::from(k1.to_string().into_bytes()),
},
Attribute {
key: QName(b"k2"),
value: Cow::from(k2.to_string().into_bytes()),
},
Attribute {
key: QName(b"k3"),
value: Cow::from(k3.to_string().into_bytes()),
},
Attribute {
key: QName(b"k4"),
value: Cow::from(k4.to_string().into_bytes()),
},
]);
}
r
}
fn tag_name(&self) -> &'static str {
"feComposite"
}
}

View file

@ -1,3 +1,9 @@
use std::borrow::Cow;
use quick_xml::{events::attributes::Attribute, name::QName};
use super::WriteElement;
/// [feGaussianBlur](https://www.w3.org/TR/SVG11/filters.html#feGaussianBlurElement)
#[derive(Debug)]
pub struct GaussianBlur {
@ -17,3 +23,18 @@ impl GaussianBlur {
}
}
}
impl WriteElement for GaussianBlur {
fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> {
vec![Attribute {
key: QName(b"stdDeviation"),
value: Cow::from(
format!("{} {}", self.std_deviation.0, self.std_deviation.1).into_bytes(),
),
}]
}
fn tag_name(&self) -> &'static str {
"feGaussianBlur"
}
}

View file

@ -1,3 +1,9 @@
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)
#[derive(Debug)]
pub struct Offset {
@ -10,3 +16,22 @@ impl Offset {
Self { dx, dy }
}
}
impl WriteElement for Offset {
fn attrs(&self) -> Vec<quick_xml::events::attributes::Attribute> {
vec![
Attribute {
key: QName(b"dx"),
value: Cow::from(self.dx.to_string().into_bytes()),
},
Attribute {
key: QName(b"dy"),
value: Cow::from(self.dy.to_string().into_bytes()),
},
]
}
fn tag_name(&self) -> &'static str {
"feOffset"
}
}