svg-filters: init

This commit is contained in:
Schrottkatze 2024-03-15 16:44:47 +01:00
parent 98850ee1e9
commit 69f0baf425
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
25 changed files with 445 additions and 1 deletions

View file

@ -0,0 +1,15 @@
[package]
name = "svg-filters"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
csscolorparser = "0.6.2"
indexmap = "2.2.5"
petgraph = "0.6.4"
quick-xml = { version = "0.31.0", features = ["serialize"] }
[lints]
workspace = true

View file

@ -0,0 +1,16 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}
pub mod types;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}

View file

@ -0,0 +1,20 @@
use std::collections::HashMap;
use petgraph::graphmap::DiGraphMap;
pub mod length;
pub mod nodes;
use self::{
length::{Coordinate, Length},
nodes::Node,
};
pub struct Filter<'a> {
graph: DiGraphMap<Node, &'a str>,
}
pub enum Edge<'a> {
Named(&'a str, u8),
/// For standard inputs such as SourceGraphic etc., which we'll just be representing as nodes for simplicity
Unnamed(u8),
}

View file

@ -0,0 +1,14 @@
pub struct Length(f32, Unit);
pub type Coordinate = Length;
pub enum Unit {
None,
Em,
Ex,
Px,
In,
Cm,
Mm,
Pt,
Pc,
}

View file

@ -0,0 +1,13 @@
use self::{primitives::Primitive, standard_input::StandardInput};
use super::length::{Coordinate, Length};
pub mod standard_input;
pub mod primitives;
pub enum Node {
/// We represent those as Nodes because they're essentially magical nodes
StandardInput(StandardInput),
Primitive(Primitive),
}

View file

@ -0,0 +1,51 @@
use crate::types::length::{Coordinate, Length};
mod blend;
mod color_matrix;
mod component_transfer;
mod composite;
mod convolve_matrix;
mod diffuse_lighting;
mod displacement_map;
mod flood;
mod gaussian_blur;
mod image;
mod merge;
mod morphology;
mod offset;
mod specular_lighting;
mod tile;
mod turbulence;
pub struct Primitive {
primitive: FePrimitive,
common_attrs: CommonAttrs,
}
struct CommonAttrs {
x: Coordinate,
y: Coordinate,
width: Length,
height: Length,
}
/// svg filter effects primitives
enum FePrimitive {
// 2 inputs
Blend(blend::Blend),
ColorMatrix(color_matrix::ColorMatrix),
ComponentTransfer(component_transfer::ComponentTransfer),
Composite(composite::Composite),
ConvolveMatrix(convolve_matrix::ConvolveMatrix),
DiffuseLighting(diffuse_lighting::DiffuseLighting),
DisplacementMap(displacement_map::DisplacementMap),
Flood(flood::Flood),
GaussianBlur(gaussian_blur::GaussianBlur),
Image(image::Image),
Merge(merge::Merge),
Morphology(morphology::Morphology),
Offset(offset::Offset),
SpecularLighting(specular_lighting::SpecularLighting),
Tile(tile::Tile),
Turbulence(turbulence::Turbulence),
}

View file

@ -0,0 +1,25 @@
/// [feBlend](https://www.w3.org/TR/SVG11/filters.html#feBlendElement)
pub struct Blend {
mode: BlendMode,
}
/// as according to https://drafts.fxtf.org/compositing-1/#blending
enum BlendMode {
Normal,
Multiply,
Screen,
Overlay,
Darken,
Lighten,
ColorDodge,
ColorBurn,
HardLight,
SoftLight,
Difference,
Exclusion,
Hue,
Saturation,
Color,
Luminosity,
}

View file

@ -0,0 +1,19 @@
/// [feColorMatrix](https://www.w3.org/TR/SVG11/filters.html#feColorMatrixElement)
pub struct ColorMatrix {
cm_type: ColorMatrixType,
}
enum ColorMatrixType {
Matrix {
values: [f32; 20],
},
Saturate {
// is the values attribute anyway tho
value: f32,
},
HueRotate {
// also the values attribute
degrees: f32,
},
LuminanceToAlpha,
}

View file

@ -0,0 +1,27 @@
/// [feComponentTransfer](https://www.w3.org/TR/SVG11/filters.html#feComponentTransferElement)
pub struct ComponentTransfer {
func_r: TransferFunction,
func_g: TransferFunction,
func_b: TransferFunction,
func_a: TransferFunction,
}
/// [transfer functions](https://www.w3.org/TR/SVG11/filters.html#transferFuncElements)
enum TransferFunction {
Identity,
Table {
table_values: Vec<f32>,
},
Discrete {
table_values: Vec<f32>,
},
Linear {
slope: f32,
intercept: f32,
},
Gamma {
amplitude: f32,
exponent: f32,
offset: f32,
},
}

View file

@ -0,0 +1,13 @@
/// [feComposite](https://www.w3.org/TR/SVG11/filters.html#feCompositeElement)
pub struct Composite {
operator: CompositeOperator,
}
enum CompositeOperator {
Over,
In,
Out,
Atop,
Xor,
Arithmetic { k1: f32, k2: f32, k3: f32, k4: f32 },
}

View file

@ -0,0 +1,18 @@
pub struct ConvolveMatrix {
order: (u16, u16),
// must be checked to be `order.0 * order.1`
kernel_matrix: Vec<f32>,
divisor: f32,
bias: f32,
target_x: i32,
target_y: i32,
edge_mode: EdgeMode,
kernel_unit_length: (f32, f32),
preserve_alpha: bool,
}
enum EdgeMode {
None,
Duplicate,
Wrap,
}

View file

@ -0,0 +1,2 @@
// TODO
pub struct DiffuseLighting;

View file

@ -0,0 +1,13 @@
/// [feDisplacementMap](https://www.w3.org/TR/SVG11/filters.html#feDisplacementMapElement)
pub struct DisplacementMap {
scale: f32,
x_channel_selector: Channel,
y_channel_selector: Channel,
}
enum Channel {
A,
R,
G,
B,
}

View file

@ -0,0 +1,7 @@
use csscolorparser::Color;
/// [feFlood](https://www.w3.org/TR/SVG11/filters.html#feFloodElement)
pub struct Flood {
flood_color: Color,
flood_opacity: f32,
}

View file

@ -0,0 +1,4 @@
/// [feGaussianBlur](https://www.w3.org/TR/SVG11/filters.html#feGaussianBlurElement)
pub struct GaussianBlur {
std_deviation: (f32, f32),
}

View file

@ -0,0 +1,2 @@
// TODO
pub struct Image;

View file

@ -0,0 +1,2 @@
// TODO
pub struct Merge;

View file

@ -0,0 +1,10 @@
/// [feMorphology](https://www.w3.org/TR/SVG11/filters.html#feMorphologyElement)
pub struct Morphology {
operator: Operator,
radius: (f32, f32),
}
enum Operator {
Erode,
Dilate,
}

View file

@ -0,0 +1,5 @@
/// [feOffset](https://www.w3.org/TR/SVG11/filters.html#feOffsetElement)
pub struct Offset {
dx: f32,
dy: f32,
}

View file

@ -0,0 +1,2 @@
// TODO
pub struct SpecularLighting;

View file

@ -0,0 +1,2 @@
/// [feTile](https://www.w3.org/TR/SVG11/filters.html#feTileElement)
pub struct Tile;

View file

@ -0,0 +1,18 @@
/// [feTurbulence](https://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement)
pub struct Turbulence {
base_frequency: (f32, f32),
num_octaves: (u16),
seed: u32,
stich_tiles: StitchTiles,
// attr name: type
noise_type: NoiseType,
}
enum StitchTiles {
Stitch,
NoStitch,
}
enum NoiseType {
Turbulence,
FractalNoise,
}

View file

@ -0,0 +1,10 @@
/// [svg filter effect standard input](https://www.w3.org/TR/SVG11/filters.html#FilterPrimitiveInAttribute)
/// technically not a node, but for implementation simplicity... yeah
pub enum StandardInput {
SourceGraphic,
SourceAlpha,
BackgroundImage,
BackgroundAlpha,
FillPaint,
StrokePaint,
}