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

136
Cargo.lock generated
View file

@ -237,6 +237,15 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "csscolorparser"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb2a7d3066da2de787b7f032c736763eb7ae5d355f81a68bab2675a96008b0bf"
dependencies = [
"phf",
]
[[package]]
name = "deranged"
version = "0.3.11"
@ -273,6 +282,12 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "eval"
version = "0.1.0"
@ -308,6 +323,12 @@ dependencies = [
"simd-adler32",
]
[[package]]
name = "fixedbitset"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "flate2"
version = "1.0.28"
@ -363,6 +384,12 @@ dependencies = [
"crunchy",
]
[[package]]
name = "hashbrown"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
[[package]]
name = "heck"
version = "0.4.1"
@ -388,6 +415,16 @@ dependencies = [
"tiff",
]
[[package]]
name = "indexmap"
version = "2.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b0b929d511467233429c45a44ac1dcaa21ba0f5ba11e4879e6ed28ddb4f9df4"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "ir"
version = "0.1.0"
@ -491,6 +528,12 @@ dependencies = [
"logos-codegen",
]
[[package]]
name = "memchr"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
[[package]]
name = "miniz_oxide"
version = "0.7.1"
@ -552,6 +595,58 @@ version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "caff54706df99d2a78a5a4e3455ff45448d81ef1bb63c22cd14052ca0e993a3f"
[[package]]
name = "petgraph"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9"
dependencies = [
"fixedbitset",
"indexmap",
]
[[package]]
name = "phf"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc"
dependencies = [
"phf_macros",
"phf_shared",
]
[[package]]
name = "phf_generator"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0"
dependencies = [
"phf_shared",
"rand",
]
[[package]]
name = "phf_macros"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b"
dependencies = [
"phf_generator",
"phf_shared",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "phf_shared"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b"
dependencies = [
"siphasher",
]
[[package]]
name = "png"
version = "0.17.10"
@ -589,6 +684,16 @@ dependencies = [
"bytemuck",
]
[[package]]
name = "quick-xml"
version = "0.31.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1004a344b30a54e2ee58d66a71b32d2db2feb0a31f9a2d302bf0536f15de2a33"
dependencies = [
"memchr",
"serde",
]
[[package]]
name = "quote"
version = "1.0.33"
@ -598,6 +703,21 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
[[package]]
name = "rayon"
version = "1.8.0"
@ -705,6 +825,12 @@ version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe"
[[package]]
name = "siphasher"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d"
[[package]]
name = "smallvec"
version = "1.11.2"
@ -726,6 +852,16 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "svg-filters"
version = "0.1.0"
dependencies = [
"csscolorparser",
"indexmap",
"petgraph",
"quick-xml",
]
[[package]]
name = "syn"
version = "2.0.41"

View file

@ -3,7 +3,7 @@ members = [
"crates/app",
"crates/eval",
"crates/ir",
"crates/lang",
"crates/lang", "crates/svg-filters",
]
resolver = "2"

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,
}