svg-filters: init
This commit is contained in:
parent
98850ee1e9
commit
69f0baf425
25 changed files with 445 additions and 1 deletions
16
crates/svg-filters/src/lib.rs
Normal file
16
crates/svg-filters/src/lib.rs
Normal 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);
|
||||
}
|
||||
}
|
20
crates/svg-filters/src/types.rs
Normal file
20
crates/svg-filters/src/types.rs
Normal 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),
|
||||
}
|
14
crates/svg-filters/src/types/length.rs
Normal file
14
crates/svg-filters/src/types/length.rs
Normal 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,
|
||||
}
|
13
crates/svg-filters/src/types/nodes.rs
Normal file
13
crates/svg-filters/src/types/nodes.rs
Normal 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),
|
||||
}
|
51
crates/svg-filters/src/types/nodes/primitives.rs
Normal file
51
crates/svg-filters/src/types/nodes/primitives.rs
Normal 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),
|
||||
}
|
25
crates/svg-filters/src/types/nodes/primitives/blend.rs
Normal file
25
crates/svg-filters/src/types/nodes/primitives/blend.rs
Normal 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,
|
||||
}
|
|
@ -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,
|
||||
}
|
|
@ -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,
|
||||
},
|
||||
}
|
13
crates/svg-filters/src/types/nodes/primitives/composite.rs
Normal file
13
crates/svg-filters/src/types/nodes/primitives/composite.rs
Normal 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 },
|
||||
}
|
|
@ -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,
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
// TODO
|
||||
pub struct DiffuseLighting;
|
|
@ -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,
|
||||
}
|
7
crates/svg-filters/src/types/nodes/primitives/flood.rs
Normal file
7
crates/svg-filters/src/types/nodes/primitives/flood.rs
Normal 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,
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
/// [feGaussianBlur](https://www.w3.org/TR/SVG11/filters.html#feGaussianBlurElement)
|
||||
pub struct GaussianBlur {
|
||||
std_deviation: (f32, f32),
|
||||
}
|
2
crates/svg-filters/src/types/nodes/primitives/image.rs
Normal file
2
crates/svg-filters/src/types/nodes/primitives/image.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
// TODO
|
||||
pub struct Image;
|
2
crates/svg-filters/src/types/nodes/primitives/merge.rs
Normal file
2
crates/svg-filters/src/types/nodes/primitives/merge.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
// TODO
|
||||
pub struct Merge;
|
10
crates/svg-filters/src/types/nodes/primitives/morphology.rs
Normal file
10
crates/svg-filters/src/types/nodes/primitives/morphology.rs
Normal 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,
|
||||
}
|
5
crates/svg-filters/src/types/nodes/primitives/offset.rs
Normal file
5
crates/svg-filters/src/types/nodes/primitives/offset.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
/// [feOffset](https://www.w3.org/TR/SVG11/filters.html#feOffsetElement)
|
||||
pub struct Offset {
|
||||
dx: f32,
|
||||
dy: f32,
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
// TODO
|
||||
pub struct SpecularLighting;
|
2
crates/svg-filters/src/types/nodes/primitives/tile.rs
Normal file
2
crates/svg-filters/src/types/nodes/primitives/tile.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
/// [feTile](https://www.w3.org/TR/SVG11/filters.html#feTileElement)
|
||||
pub struct Tile;
|
18
crates/svg-filters/src/types/nodes/primitives/turbulence.rs
Normal file
18
crates/svg-filters/src/types/nodes/primitives/turbulence.rs
Normal 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,
|
||||
}
|
10
crates/svg-filters/src/types/nodes/standard_input.rs
Normal file
10
crates/svg-filters/src/types/nodes/standard_input.rs
Normal 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,
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue