1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//! SVG filter types.
use std::cell::RefCell;
use std::rc::Rc;
use strict_num::PositiveF32;
use svgtypes::AspectRatio;
use crate::{BlendMode, Color, Group, ImageRendering, NonZeroF32, NonZeroRect, Opacity, Units};
/// A filter element.
///
/// `filter` element in the SVG.
#[derive(Clone, Debug)]
pub struct Filter {
/// Element's ID.
///
/// Taken from the SVG itself.
/// Used only during SVG writing. `resvg` doesn't rely on this property.
pub id: String,
/// Region coordinate system units.
///
/// `filterUnits` in the SVG.
pub units: Units,
/// Content coordinate system units.
///
/// `primitiveUnits` in the SVG.
pub primitive_units: Units,
/// Filter region.
///
/// `x`, `y`, `width` and `height` in the SVG.
pub rect: NonZeroRect,
/// A list of filter primitives.
pub primitives: Vec<Primitive>,
}
/// An alias for a shared `Filter`.
pub type SharedFilter = Rc<RefCell<Filter>>;
/// A filter primitive element.
#[derive(Clone, Debug)]
pub struct Primitive {
/// `x` coordinate of the filter subregion.
pub x: Option<f32>,
/// `y` coordinate of the filter subregion.
pub y: Option<f32>,
/// The filter subregion width.
pub width: Option<f32>,
/// The filter subregion height.
pub height: Option<f32>,
/// Color interpolation mode.
///
/// `color-interpolation-filters` in the SVG.
pub color_interpolation: ColorInterpolation,
/// Assigned name for this filter primitive.
///
/// `result` in the SVG.
pub result: String,
/// Filter primitive kind.
pub kind: Kind,
}
/// A filter kind.
#[allow(missing_docs)]
#[derive(Clone, Debug)]
pub enum Kind {
Blend(Blend),
ColorMatrix(ColorMatrix),
ComponentTransfer(ComponentTransfer),
Composite(Composite),
ConvolveMatrix(ConvolveMatrix),
DiffuseLighting(DiffuseLighting),
DisplacementMap(DisplacementMap),
DropShadow(DropShadow),
Flood(Flood),
GaussianBlur(GaussianBlur),
Image(Image),
Merge(Merge),
Morphology(Morphology),
Offset(Offset),
SpecularLighting(SpecularLighting),
Tile(Tile),
Turbulence(Turbulence),
}
impl Kind {
/// Checks that `FilterKind` has a specific input.
pub fn has_input(&self, input: &Input) -> bool {
match self {
Kind::Blend(ref fe) => fe.input1 == *input || fe.input2 == *input,
Kind::ColorMatrix(ref fe) => fe.input == *input,
Kind::ComponentTransfer(ref fe) => fe.input == *input,
Kind::Composite(ref fe) => fe.input1 == *input || fe.input2 == *input,
Kind::ConvolveMatrix(ref fe) => fe.input == *input,
Kind::DiffuseLighting(ref fe) => fe.input == *input,
Kind::DisplacementMap(ref fe) => fe.input1 == *input || fe.input2 == *input,
Kind::DropShadow(ref fe) => fe.input == *input,
Kind::Flood(_) => false,
Kind::GaussianBlur(ref fe) => fe.input == *input,
Kind::Image(_) => false,
Kind::Merge(ref fe) => fe.inputs.iter().any(|i| i == input),
Kind::Morphology(ref fe) => fe.input == *input,
Kind::Offset(ref fe) => fe.input == *input,
Kind::SpecularLighting(ref fe) => fe.input == *input,
Kind::Tile(ref fe) => fe.input == *input,
Kind::Turbulence(_) => false,
}
}
}
/// Identifies input for a filter primitive.
#[allow(missing_docs)]
#[derive(Clone, PartialEq, Debug)]
pub enum Input {
SourceGraphic,
SourceAlpha,
Reference(String),
}
/// A color interpolation mode.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum ColorInterpolation {
SRGB,
LinearRGB,
}
impl Default for ColorInterpolation {
fn default() -> Self {
ColorInterpolation::LinearRGB
}
}
/// A blend filter primitive.
///
/// `feBlend` element in the SVG.
#[derive(Clone, Debug)]
pub struct Blend {
/// Identifies input for the given filter primitive.
///
/// `in` in the SVG.
pub input1: Input,
/// Identifies input for the given filter primitive.
///
/// `in2` in the SVG.
pub input2: Input,
/// A blending mode.
///
/// `mode` in the SVG.
pub mode: BlendMode,
}
/// A color matrix filter primitive.
///
/// `feColorMatrix` element in the SVG.
#[derive(Clone, Debug)]
pub struct ColorMatrix {
/// Identifies input for the given filter primitive.
///
/// `in` in the SVG.
pub input: Input,
/// A matrix kind.
///
/// `type` in the SVG.
pub kind: ColorMatrixKind,
}
/// A color matrix filter primitive kind.
#[derive(Clone, Debug)]
#[allow(missing_docs)]
pub enum ColorMatrixKind {
Matrix(Vec<f32>), // Guarantee to have 20 numbers.
Saturate(PositiveF32),
HueRotate(f32),
LuminanceToAlpha,
}
impl Default for ColorMatrixKind {
fn default() -> Self {
ColorMatrixKind::Matrix(vec![
1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0,
])
}
}
/// A component-wise remapping filter primitive.
///
/// `feComponentTransfer` element in the SVG.
#[derive(Clone, Debug)]
pub struct ComponentTransfer {
/// Identifies input for the given filter primitive.
///
/// `in` in the SVG.
pub input: Input,
/// `feFuncR` in the SVG.
pub func_r: TransferFunction,
/// `feFuncG` in the SVG.
pub func_g: TransferFunction,
/// `feFuncB` in the SVG.
pub func_b: TransferFunction,
/// `feFuncA` in the SVG.
pub func_a: TransferFunction,
}
/// A transfer function used by `FeComponentTransfer`.
///
/// <https://www.w3.org/TR/SVG11/filters.html#transferFuncElements>
#[derive(Clone, Debug)]
pub enum TransferFunction {
/// Keeps a component as is.
Identity,
/// Applies a linear interpolation to a component.
///
/// The number list can be empty.
Table(Vec<f32>),
/// Applies a step function to a component.
///
/// The number list can be empty.
Discrete(Vec<f32>),
/// Applies a linear shift to a component.
#[allow(missing_docs)]
Linear { slope: f32, intercept: f32 },
/// Applies an exponential shift to a component.
#[allow(missing_docs)]
Gamma {
amplitude: f32,
exponent: f32,
offset: f32,
},
}
/// A composite filter primitive.
///
/// `feComposite` element in the SVG.
#[derive(Clone, Debug)]
pub struct Composite {
/// Identifies input for the given filter primitive.
///
/// `in` in the SVG.
pub input1: Input,
/// Identifies input for the given filter primitive.
///
/// `in2` in the SVG.
pub input2: Input,
/// A compositing operation.
///
/// `operator` in the SVG.
pub operator: CompositeOperator,
}
/// An images compositing operation.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum CompositeOperator {
Over,
In,
Out,
Atop,
Xor,
Arithmetic { k1: f32, k2: f32, k3: f32, k4: f32 },
}
/// A matrix convolution filter primitive.
///
/// `feConvolveMatrix` element in the SVG.
#[derive(Clone, Debug)]
pub struct ConvolveMatrix {
/// Identifies input for the given filter primitive.
///
/// `in` in the SVG.
pub input: Input,
/// A convolve matrix.
pub matrix: ConvolveMatrixData,
/// A matrix divisor.
///
/// `divisor` in the SVG.
pub divisor: NonZeroF32,
/// A kernel matrix bias.
///
/// `bias` in the SVG.
pub bias: f32,
/// An edges processing mode.
///
/// `edgeMode` in the SVG.
pub edge_mode: EdgeMode,
/// An alpha preserving flag.
///
/// `preserveAlpha` in the SVG.
pub preserve_alpha: bool,
}
/// A convolve matrix representation.
///
/// Used primarily by [`ConvolveMatrix`].
#[derive(Clone, Debug)]
pub struct ConvolveMatrixData {
/// Returns a matrix's X target.
///
/// `targetX` in the SVG.
pub target_x: u32,
/// Returns a matrix's Y target.
///
/// `targetY` in the SVG.
pub target_y: u32,
/// Returns a number of columns in the matrix.
///
/// Part of the `order` attribute in the SVG.
pub columns: u32,
/// Returns a number of rows in the matrix.
///
/// Part of the `order` attribute in the SVG.
pub rows: u32,
/// The actual matrix.
pub data: Vec<f32>,
}
impl ConvolveMatrixData {
/// Creates a new `ConvolveMatrixData`.
///
/// Returns `None` when:
///
/// - `columns` * `rows` != `data.len()`
/// - `target_x` >= `columns`
/// - `target_y` >= `rows`
pub fn new(
target_x: u32,
target_y: u32,
columns: u32,
rows: u32,
data: Vec<f32>,
) -> Option<Self> {
if (columns * rows) as usize != data.len() || target_x >= columns || target_y >= rows {
return None;
}
Some(ConvolveMatrixData {
target_x,
target_y,
columns,
rows,
data,
})
}
/// Returns a matrix value at the specified position.
///
/// # Panics
///
/// - When position is out of bounds.
pub fn get(&self, x: u32, y: u32) -> f32 {
self.data[(y * self.columns + x) as usize]
}
}
/// An edges processing mode.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum EdgeMode {
None,
Duplicate,
Wrap,
}
/// A displacement map filter primitive.
///
/// `feDisplacementMap` element in the SVG.
#[derive(Clone, Debug)]
pub struct DisplacementMap {
/// Identifies input for the given filter primitive.
///
/// `in` in the SVG.
pub input1: Input,
/// Identifies input for the given filter primitive.
///
/// `in2` in the SVG.
pub input2: Input,
/// Scale factor.
///
/// `scale` in the SVG.
pub scale: f32,
/// Indicates a source color channel along the X-axis.
///
/// `xChannelSelector` in the SVG.
pub x_channel_selector: ColorChannel,
/// Indicates a source color channel along the Y-axis.
///
/// `yChannelSelector` in the SVG.
pub y_channel_selector: ColorChannel,
}
/// A color channel.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum ColorChannel {
R,
G,
B,
A,
}
/// A drop shadow filter primitive.
///
/// This is essentially `feGaussianBlur`, `feOffset` and `feFlood` joined together.
///
/// `feDropShadow` element in the SVG.
#[derive(Clone, Debug)]
pub struct DropShadow {
/// Identifies input for the given filter primitive.
///
/// `in` in the SVG.
pub input: Input,
/// The amount to offset the input graphic along the X-axis.
pub dx: f32,
/// The amount to offset the input graphic along the Y-axis.
pub dy: f32,
/// A standard deviation along the X-axis.
///
/// `stdDeviation` in the SVG.
pub std_dev_x: PositiveF32,
/// A standard deviation along the Y-axis.
///
/// `stdDeviation` in the SVG.
pub std_dev_y: PositiveF32,
/// A flood color.
///
/// `flood-color` in the SVG.
pub color: Color,
/// A flood opacity.
///
/// `flood-opacity` in the SVG.
pub opacity: Opacity,
}
/// A flood filter primitive.
///
/// `feFlood` element in the SVG.
#[derive(Clone, Copy, Debug)]
pub struct Flood {
/// A flood color.
///
/// `flood-color` in the SVG.
pub color: Color,
/// A flood opacity.
///
/// `flood-opacity` in the SVG.
pub opacity: Opacity,
}
/// A Gaussian blur filter primitive.
///
/// `feGaussianBlur` element in the SVG.
#[derive(Clone, Debug)]
pub struct GaussianBlur {
/// Identifies input for the given filter primitive.
///
/// `in` in the SVG.
pub input: Input,
/// A standard deviation along the X-axis.
///
/// `stdDeviation` in the SVG.
pub std_dev_x: PositiveF32,
/// A standard deviation along the Y-axis.
///
/// `stdDeviation` in the SVG.
pub std_dev_y: PositiveF32,
}
/// An image filter primitive.
///
/// `feImage` element in the SVG.
#[derive(Clone, Debug)]
pub struct Image {
/// Value of the `preserveAspectRatio` attribute.
pub aspect: AspectRatio,
/// Rendering method.
///
/// `image-rendering` in SVG.
pub rendering_mode: ImageRendering,
/// Image data.
pub data: ImageKind,
}
/// Kind of the `feImage` data.
#[derive(Clone, Debug)]
pub enum ImageKind {
/// An image data.
Image(crate::ImageKind),
/// An SVG node.
Use(Box<Group>),
}
/// A diffuse lighting filter primitive.
///
/// `feDiffuseLighting` element in the SVG.
#[derive(Clone, Debug)]
pub struct DiffuseLighting {
/// Identifies input for the given filter primitive.
///
/// `in` in the SVG.
pub input: Input,
/// A surface scale.
///
/// `surfaceScale` in the SVG.
pub surface_scale: f32,
/// A diffuse constant.
///
/// `diffuseConstant` in the SVG.
pub diffuse_constant: f32,
/// A lighting color.
///
/// `lighting-color` in the SVG.
pub lighting_color: Color,
/// A light source.
pub light_source: LightSource,
}
/// A specular lighting filter primitive.
///
/// `feSpecularLighting` element in the SVG.
#[derive(Clone, Debug)]
pub struct SpecularLighting {
/// Identifies input for the given filter primitive.
///
/// `in` in the SVG.
pub input: Input,
/// A surface scale.
///
/// `surfaceScale` in the SVG.
pub surface_scale: f32,
/// A specular constant.
///
/// `specularConstant` in the SVG.
pub specular_constant: f32,
/// A specular exponent.
///
/// Should be in 1..128 range.
///
/// `specularExponent` in the SVG.
pub specular_exponent: f32,
/// A lighting color.
///
/// `lighting-color` in the SVG.
pub lighting_color: Color,
/// A light source.
pub light_source: LightSource,
}
/// A light source kind.
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug)]
pub enum LightSource {
DistantLight(DistantLight),
PointLight(PointLight),
SpotLight(SpotLight),
}
/// A distant light source.
///
/// `feDistantLight` element in the SVG.
#[derive(Clone, Copy, Debug)]
pub struct DistantLight {
/// Direction angle for the light source on the XY plane (clockwise),
/// in degrees from the x axis.
///
/// `azimuth` in the SVG.
pub azimuth: f32,
/// Direction angle for the light source from the XY plane towards the z axis, in degrees.
///
/// `elevation` in the SVG.
pub elevation: f32,
}
/// A point light source.
///
/// `fePointLight` element in the SVG.
#[derive(Clone, Copy, Debug)]
pub struct PointLight {
/// X location for the light source.
///
/// `x` in the SVG.
pub x: f32,
/// Y location for the light source.
///
/// `y` in the SVG.
pub y: f32,
/// Z location for the light source.
///
/// `z` in the SVG.
pub z: f32,
}
/// A spot light source.
///
/// `feSpotLight` element in the SVG.
#[derive(Clone, Copy, Debug)]
pub struct SpotLight {
/// X location for the light source.
///
/// `x` in the SVG.
pub x: f32,
/// Y location for the light source.
///
/// `y` in the SVG.
pub y: f32,
/// Z location for the light source.
///
/// `z` in the SVG.
pub z: f32,
/// X point at which the light source is pointing.
///
/// `pointsAtX` in the SVG.
pub points_at_x: f32,
/// Y point at which the light source is pointing.
///
/// `pointsAtY` in the SVG.
pub points_at_y: f32,
/// Z point at which the light source is pointing.
///
/// `pointsAtZ` in the SVG.
pub points_at_z: f32,
/// Exponent value controlling the focus for the light source.
///
/// `specularExponent` in the SVG.
pub specular_exponent: PositiveF32,
/// A limiting cone which restricts the region where the light is projected.
///
/// `limitingConeAngle` in the SVG.
pub limiting_cone_angle: Option<f32>,
}
/// A merge filter primitive.
///
/// `feMerge` element in the SVG.
#[derive(Clone, Debug)]
pub struct Merge {
/// List of input layers that should be merged.
///
/// List of `feMergeNode`'s in the SVG.
pub inputs: Vec<Input>,
}
/// A morphology filter primitive.
///
/// `feMorphology` element in the SVG.
#[derive(Clone, Debug)]
pub struct Morphology {
/// Identifies input for the given filter primitive.
///
/// `in` in the SVG.
pub input: Input,
/// A filter operator.
///
/// `operator` in the SVG.
pub operator: MorphologyOperator,
/// A filter radius along the X-axis.
///
/// A value of zero disables the effect of the given filter primitive.
///
/// `radius` in the SVG.
pub radius_x: PositiveF32,
/// A filter radius along the Y-axis.
///
/// A value of zero disables the effect of the given filter primitive.
///
/// `radius` in the SVG.
pub radius_y: PositiveF32,
}
/// A morphology operation.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum MorphologyOperator {
Erode,
Dilate,
}
/// An offset filter primitive.
///
/// `feOffset` element in the SVG.
#[derive(Clone, Debug)]
pub struct Offset {
/// Identifies input for the given filter primitive.
///
/// `in` in the SVG.
pub input: Input,
/// The amount to offset the input graphic along the X-axis.
pub dx: f32,
/// The amount to offset the input graphic along the Y-axis.
pub dy: f32,
}
/// A tile filter primitive.
///
/// `feTile` element in the SVG.
#[derive(Clone, Debug)]
pub struct Tile {
/// Identifies input for the given filter primitive.
///
/// `in` in the SVG.
pub input: Input,
}
/// A turbulence generation filter primitive.
///
/// `feTurbulence` element in the SVG.
#[derive(Clone, Copy, Debug)]
pub struct Turbulence {
/// Identifies the base frequency for the noise function.
///
/// `baseFrequency` in the SVG.
pub base_frequency_x: PositiveF32,
/// Identifies the base frequency for the noise function.
///
/// `baseFrequency` in the SVG.
pub base_frequency_y: PositiveF32,
/// Identifies the number of octaves for the noise function.
///
/// `numOctaves` in the SVG.
pub num_octaves: u32,
/// The starting number for the pseudo random number generator.
///
/// `seed` in the SVG.
pub seed: i32,
/// Smooth transitions at the border of tiles.
///
/// `stitchTiles` in the SVG.
pub stitch_tiles: bool,
/// Indicates whether the filter primitive should perform a noise or turbulence function.
///
/// `type` in the SVG.
pub kind: TurbulenceKind,
}
/// A turbulence kind for the `feTurbulence` filter.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum TurbulenceKind {
FractalNoise,
Turbulence,
}