2020-02-20 17:22:07 -05:00
|
|
|
// pathfinder/content/src/effects.rs
|
|
|
|
//
|
|
|
|
// Copyright © 2020 The Pathfinder Project Developers.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Special effects that can be applied to layers.
|
|
|
|
|
|
|
|
use pathfinder_color::ColorF;
|
2020-03-26 23:01:17 -04:00
|
|
|
use pathfinder_geometry::line_segment::LineSegment2F;
|
|
|
|
use pathfinder_geometry::vector::Vector2F;
|
|
|
|
use pathfinder_simd::default::F32x2;
|
2020-02-20 17:22:07 -05:00
|
|
|
|
|
|
|
/// This intentionally does not precisely match what Core Graphics does (a
|
|
|
|
/// Lanczos function), because we don't want any ringing artefacts.
|
|
|
|
pub static DEFRINGING_KERNEL_CORE_GRAPHICS: DefringingKernel =
|
|
|
|
DefringingKernel([0.033165660, 0.102074051, 0.221434336, 0.286651906]);
|
|
|
|
pub static DEFRINGING_KERNEL_FREETYPE: DefringingKernel =
|
|
|
|
DefringingKernel([0.0, 0.031372549, 0.301960784, 0.337254902]);
|
|
|
|
|
|
|
|
/// Should match macOS 10.13 High Sierra.
|
|
|
|
pub static STEM_DARKENING_FACTORS: [f32; 2] = [0.0121, 0.0121 * 1.25];
|
|
|
|
|
|
|
|
/// Should match macOS 10.13 High Sierra.
|
|
|
|
pub const MAX_STEM_DARKENING_AMOUNT: [f32; 2] = [0.3, 0.3];
|
|
|
|
|
|
|
|
/// This value is a subjective cutoff. Above this ppem value, no stem darkening is performed.
|
|
|
|
pub const MAX_STEM_DARKENING_PIXELS_PER_EM: f32 = 72.0;
|
|
|
|
|
|
|
|
/// The shader that should be used when compositing this layer onto its destination.
|
2020-03-26 23:01:17 -04:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
2020-02-20 17:22:07 -05:00
|
|
|
pub enum Filter {
|
2020-03-19 19:44:45 -04:00
|
|
|
/// No special filter.
|
|
|
|
None,
|
2020-02-25 21:37:23 -05:00
|
|
|
|
2020-03-26 23:01:17 -04:00
|
|
|
/// Converts a linear gradient to a radial one.
|
|
|
|
RadialGradient {
|
|
|
|
/// The line that the circles lie along.
|
|
|
|
line: LineSegment2F,
|
|
|
|
/// The radii of the circles at the two endpoints.
|
|
|
|
radii: F32x2,
|
|
|
|
/// The origin of the linearized gradient in the texture.
|
|
|
|
uv_origin: Vector2F,
|
|
|
|
},
|
|
|
|
|
2020-04-03 01:03:42 -04:00
|
|
|
PatternFilter(PatternFilter),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Shaders applicable to patterns.
|
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
|
|
|
pub enum PatternFilter {
|
2020-02-20 17:22:07 -05:00
|
|
|
/// Performs postprocessing operations useful for monochrome text.
|
|
|
|
Text {
|
|
|
|
/// The foreground color of the text.
|
|
|
|
fg_color: ColorF,
|
|
|
|
/// The background color of the text.
|
|
|
|
bg_color: ColorF,
|
|
|
|
/// The kernel used for defringing, if subpixel AA is enabled.
|
|
|
|
defringing_kernel: Option<DefringingKernel>,
|
|
|
|
/// Whether gamma correction is used when compositing.
|
|
|
|
///
|
|
|
|
/// If this is enabled, stem darkening is advised.
|
|
|
|
gamma_correction: bool,
|
|
|
|
},
|
2020-02-26 21:43:41 -05:00
|
|
|
|
|
|
|
/// A blur operation in one direction, either horizontal or vertical.
|
|
|
|
///
|
|
|
|
/// To produce a full Gaussian blur, perform two successive blur operations, one in each
|
|
|
|
/// direction.
|
|
|
|
Blur {
|
|
|
|
direction: BlurDirection,
|
|
|
|
sigma: f32,
|
|
|
|
},
|
2020-02-20 17:22:07 -05:00
|
|
|
}
|
|
|
|
|
2020-02-22 17:39:03 -05:00
|
|
|
/// Blend modes that can be applied to individual paths.
|
2020-02-20 18:52:40 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
|
|
|
pub enum BlendMode {
|
2020-03-19 19:44:45 -04:00
|
|
|
// Porter-Duff, supported by GPU blender
|
2020-02-20 18:52:40 -05:00
|
|
|
Clear,
|
2020-03-19 19:44:45 -04:00
|
|
|
Copy,
|
|
|
|
SrcIn,
|
|
|
|
SrcOut,
|
2020-02-21 01:22:15 -05:00
|
|
|
SrcOver,
|
|
|
|
SrcAtop,
|
2020-03-19 19:44:45 -04:00
|
|
|
DestIn,
|
|
|
|
DestOut,
|
|
|
|
DestOver,
|
|
|
|
DestAtop,
|
2020-02-21 01:22:15 -05:00
|
|
|
Xor,
|
|
|
|
Lighter,
|
2020-02-25 00:23:17 -05:00
|
|
|
|
2020-03-19 19:44:45 -04:00
|
|
|
// Others, unsupported by GPU blender
|
|
|
|
Darken,
|
|
|
|
Lighten,
|
2020-02-25 00:23:17 -05:00
|
|
|
Multiply,
|
|
|
|
Screen,
|
|
|
|
HardLight,
|
|
|
|
Overlay,
|
2020-02-25 14:37:42 -05:00
|
|
|
ColorDodge,
|
|
|
|
ColorBurn,
|
2020-02-25 17:39:06 -05:00
|
|
|
SoftLight,
|
2020-02-25 17:55:58 -05:00
|
|
|
Difference,
|
|
|
|
Exclusion,
|
2020-02-22 17:39:03 -05:00
|
|
|
Hue,
|
|
|
|
Saturation,
|
|
|
|
Color,
|
|
|
|
Luminosity,
|
2020-02-20 18:52:40 -05:00
|
|
|
}
|
|
|
|
|
2020-02-20 17:22:07 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
|
|
|
pub struct DefringingKernel(pub [f32; 4]);
|
|
|
|
|
2020-02-26 21:43:41 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
|
|
|
pub enum BlurDirection {
|
|
|
|
X,
|
|
|
|
Y,
|
|
|
|
}
|
|
|
|
|
2020-03-19 19:44:45 -04:00
|
|
|
impl Default for BlendMode {
|
2020-02-20 17:22:07 -05:00
|
|
|
#[inline]
|
2020-03-19 19:44:45 -04:00
|
|
|
fn default() -> BlendMode {
|
|
|
|
BlendMode::SrcOver
|
2020-02-20 17:22:07 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-20 18:52:40 -05:00
|
|
|
|
2020-03-19 19:44:45 -04:00
|
|
|
impl Default for Filter {
|
2020-02-20 18:52:40 -05:00
|
|
|
#[inline]
|
2020-03-19 19:44:45 -04:00
|
|
|
fn default() -> Filter {
|
|
|
|
Filter::None
|
2020-02-20 18:52:40 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-20 21:15:20 -05:00
|
|
|
|
|
|
|
impl BlendMode {
|
|
|
|
/// Whether the backdrop is irrelevant when applying this blend mode (i.e. destination blend
|
2020-02-21 01:22:15 -05:00
|
|
|
/// factor is zero when source alpha is one).
|
2020-02-20 21:15:20 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn occludes_backdrop(self) -> bool {
|
|
|
|
match self {
|
2020-02-21 01:22:15 -05:00
|
|
|
BlendMode::SrcOver | BlendMode::Clear => true,
|
|
|
|
BlendMode::DestOver |
|
|
|
|
BlendMode::DestOut |
|
|
|
|
BlendMode::SrcAtop |
|
|
|
|
BlendMode::Xor |
|
|
|
|
BlendMode::Lighter |
|
|
|
|
BlendMode::Lighten |
|
2020-02-25 14:37:42 -05:00
|
|
|
BlendMode::Darken |
|
2020-03-19 19:44:45 -04:00
|
|
|
BlendMode::Copy |
|
|
|
|
BlendMode::SrcIn |
|
|
|
|
BlendMode::DestIn |
|
|
|
|
BlendMode::SrcOut |
|
|
|
|
BlendMode::DestAtop |
|
|
|
|
BlendMode::Multiply |
|
|
|
|
BlendMode::Screen |
|
|
|
|
BlendMode::HardLight |
|
|
|
|
BlendMode::Overlay |
|
|
|
|
BlendMode::ColorDodge |
|
|
|
|
BlendMode::ColorBurn |
|
|
|
|
BlendMode::SoftLight |
|
|
|
|
BlendMode::Difference |
|
|
|
|
BlendMode::Exclusion |
|
|
|
|
BlendMode::Hue |
|
|
|
|
BlendMode::Saturation |
|
|
|
|
BlendMode::Color |
|
|
|
|
BlendMode::Luminosity => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// True if this blend mode does not preserve destination areas outside the source.
|
|
|
|
pub fn is_destructive(self) -> bool {
|
|
|
|
match self {
|
|
|
|
BlendMode::Clear |
|
|
|
|
BlendMode::Copy |
|
|
|
|
BlendMode::SrcIn |
|
|
|
|
BlendMode::DestIn |
|
|
|
|
BlendMode::SrcOut |
|
|
|
|
BlendMode::DestAtop => true,
|
|
|
|
BlendMode::SrcOver |
|
|
|
|
BlendMode::DestOver |
|
|
|
|
BlendMode::DestOut |
|
|
|
|
BlendMode::SrcAtop |
|
|
|
|
BlendMode::Xor |
|
|
|
|
BlendMode::Lighter |
|
|
|
|
BlendMode::Lighten |
|
|
|
|
BlendMode::Darken |
|
2020-02-25 00:23:17 -05:00
|
|
|
BlendMode::Multiply |
|
|
|
|
BlendMode::Screen |
|
|
|
|
BlendMode::HardLight |
|
|
|
|
BlendMode::Overlay |
|
2020-02-25 14:37:42 -05:00
|
|
|
BlendMode::ColorDodge |
|
|
|
|
BlendMode::ColorBurn |
|
2020-02-25 17:39:06 -05:00
|
|
|
BlendMode::SoftLight |
|
2020-02-25 17:55:58 -05:00
|
|
|
BlendMode::Difference |
|
|
|
|
BlendMode::Exclusion |
|
2020-02-22 17:39:03 -05:00
|
|
|
BlendMode::Hue |
|
|
|
|
BlendMode::Saturation |
|
|
|
|
BlendMode::Color |
|
|
|
|
BlendMode::Luminosity => false,
|
2020-02-20 21:15:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|