use crate::overlay::{OverlayFill, OverlayFillKind, ShadowLayer};
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct BackdropEffects {
pub blur: f32,
pub saturation: f32,
pub brightness: f32,
pub hue_shift: f32,
}
impl Default for BackdropEffects {
fn default() -> Self {
Self {
blur: 0.0,
saturation: 1.0,
brightness: 1.0,
hue_shift: 0.0,
}
}
}
impl BackdropEffects {
pub fn is_active(&self) -> bool {
self.blur > 0.0
}
pub fn with_blur(mut self, blur: f32) -> Self {
self.blur = blur;
self
}
pub fn with_filters(mut self, saturation: f32, brightness: f32, hue_shift: f32) -> Self {
self.saturation = saturation;
self.brightness = brightness;
self.hue_shift = hue_shift;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum OverlayBlend {
#[default]
SourceOver,
Additive,
Multiply,
Screen,
Clear,
Source,
Destination,
DestinationOver,
SourceIn,
DestinationIn,
SourceOut,
DestinationOut,
SourceAtop,
DestinationAtop,
Xor,
Plus,
Overlay,
Darken,
Lighten,
ColourDodge,
ColourBurn,
HardLight,
SoftLight,
Difference,
Exclusion,
Hue,
Saturation,
Colour,
Luminosity,
}
impl OverlayBlend {
pub fn reads_destination(&self) -> bool {
matches!(
self,
Self::Overlay
| Self::Darken
| Self::Lighten
| Self::ColourDodge
| Self::ColourBurn
| Self::HardLight
| Self::SoftLight
| Self::Difference
| Self::Exclusion
| Self::Hue
| Self::Saturation
| Self::Colour
| Self::Luminosity
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum OverlayEdge {
#[default]
AntiAliased,
Crisp,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct OverlayStyle {
pub fill: OverlayFill,
pub shadows: Vec<ShadowLayer>,
pub inner_shadows: Vec<ShadowLayer>,
pub backdrop: BackdropEffects,
pub tint: [f32; 4],
pub opacity: f32,
pub blend: OverlayBlend,
pub colour_matrix: Option<crate::overlay::ColourMatrix>,
pub edge: OverlayEdge,
}
impl Default for OverlayStyle {
fn default() -> Self {
Self {
fill: OverlayFill::none(),
shadows: Vec::new(),
inner_shadows: Vec::new(),
backdrop: BackdropEffects::default(),
tint: [1.0, 1.0, 1.0, 1.0],
opacity: 1.0,
blend: OverlayBlend::SourceOver,
colour_matrix: None,
edge: OverlayEdge::AntiAliased,
}
}
}
impl OverlayStyle {
pub fn solid(colour: impl Into<crate::colour::Colour>) -> Self {
Self {
fill: OverlayFill::Solid(colour.into()),
..Default::default()
}
}
pub fn with_fill(mut self, fill: OverlayFill) -> Self {
self.fill = fill;
self
}
pub fn with_shadows(mut self, shadows: Vec<ShadowLayer>) -> Self {
self.shadows = shadows;
self
}
pub fn with_inner_shadows(mut self, shadows: Vec<ShadowLayer>) -> Self {
self.inner_shadows = shadows;
self
}
pub fn with_backdrop(mut self, backdrop: BackdropEffects) -> Self {
self.backdrop = backdrop;
self
}
pub fn with_tint(mut self, tint: [f32; 4]) -> Self {
self.tint = tint;
self
}
pub fn with_opacity(mut self, opacity: f32) -> Self {
self.opacity = opacity;
self
}
pub fn with_blend(mut self, blend: OverlayBlend) -> Self {
self.blend = blend;
self
}
pub fn with_colour_matrix(mut self, matrix: crate::overlay::ColourMatrix) -> Self {
self.colour_matrix = Some(matrix);
self
}
pub fn with_edge(mut self, edge: OverlayEdge) -> Self {
self.edge = edge;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct OverlayStyleSupport {
pub fill: &'static [OverlayFillKind],
pub backdrop: bool,
pub shadows: bool,
pub crisp_edges: bool,
}
const ALL_FILLS: &[OverlayFillKind] = &[
OverlayFillKind::Solid,
OverlayFillKind::Gradient,
OverlayFillKind::Texture,
OverlayFillKind::Custom,
];
impl OverlayStyleSupport {
pub const ALL: Self = Self {
fill: ALL_FILLS,
backdrop: true,
shadows: true,
crisp_edges: true,
};
pub const NONE: Self = Self {
fill: &[],
backdrop: false,
shadows: false,
crisp_edges: false,
};
pub const fn for_shape(shape: &crate::overlay::OverlayShape) -> Self {
Self {
crisp_edges: !matches!(shape, crate::overlay::OverlayShape::Vector { .. }),
..Self::ALL
}
}
pub const fn for_polyline(closed: bool) -> Self {
Self {
fill: if closed { ALL_FILLS } else { &[] },
backdrop: false,
shadows: true,
crisp_edges: false,
}
}
pub const fn for_glyphs() -> Self {
Self {
fill: ALL_FILLS,
backdrop: false,
shadows: true,
crisp_edges: false,
}
}
pub const fn for_mesh() -> Self {
Self {
fill: ALL_FILLS,
backdrop: false,
shadows: false,
crisp_edges: true,
}
}
pub fn draws_fill(&self, fill: &OverlayFill) -> bool {
let kind = fill.kind();
self.fill.iter().any(|k| *k == kind)
}
pub fn inert_fields(&self, style: &OverlayStyle) -> Vec<&'static str> {
let mut out = Vec::new();
if style.fill != OverlayFill::none() && !self.draws_fill(&style.fill) {
out.push("fill");
}
if !self.backdrop && style.backdrop.is_active() {
out.push("backdrop");
}
if !self.shadows && !style.shadows.is_empty() {
out.push("shadows");
}
if !self.shadows && !style.inner_shadows.is_empty() {
out.push("inner_shadows");
}
if !self.crisp_edges && style.edge == OverlayEdge::Crisp {
out.push("edge");
}
out
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::colour::Colour;
use crate::overlay::{OverlayShape, ShadowLayer};
#[test]
fn vector_shapes_draw_every_cell() {
let style = OverlayStyle::solid(Colour::linear(1.0, 0.0, 0.0, 1.0))
.with_inner_shadows(vec![ShadowLayer::default()])
.with_backdrop(BackdropEffects::default().with_blur(4.0));
let analytic = OverlayStyleSupport::for_shape(&OverlayShape::Circle);
assert!(analytic.inert_fields(&style).is_empty());
let vector = OverlayStyleSupport::for_shape(&OverlayShape::Vector {
subpaths: Vec::new(),
fill_rule: crate::overlay::FillRule::NonZero,
stroke: None,
inverse: false,
});
assert!(vector.inert_fields(&style).is_empty());
assert!(vector.draws_fill(&OverlayFill::texture(crate::overlay::OverlayTextureId(0))));
}
#[test]
fn glyphs_draw_every_fill_kind() {
let glyphs = OverlayStyleSupport::for_glyphs();
assert!(glyphs.draws_fill(&OverlayFill::texture(crate::overlay::OverlayTextureId(0))));
assert!(glyphs.draws_fill(&OverlayFill::sweep_gradient(
Colour::WHITE,
Colour::BLACK,
0.0,
1.0
)));
}
#[test]
fn a_default_style_is_never_inert() {
let style = OverlayStyle::default();
assert!(
OverlayStyleSupport::for_glyphs()
.inert_fields(&style)
.is_empty()
);
assert!(OverlayStyleSupport::NONE.inert_fields(&style).is_empty());
}
#[test]
fn crisp_edges_are_reported_where_they_are_drawn() {
let style = OverlayStyle::solid(Colour::WHITE).with_edge(OverlayEdge::Crisp);
let rect = OverlayShape::Rect { corner_radius: 0.0 };
assert!(
OverlayStyleSupport::for_shape(&rect)
.inert_fields(&style)
.is_empty()
);
let vector = OverlayShape::Vector {
subpaths: Vec::new(),
fill_rule: crate::overlay::FillRule::NonZero,
stroke: None,
inverse: false,
};
assert_eq!(
OverlayStyleSupport::for_shape(&vector).inert_fields(&style),
vec!["edge"]
);
assert_eq!(
OverlayStyleSupport::for_glyphs().inert_fields(&style),
vec!["edge"]
);
assert!(
OverlayStyleSupport::for_mesh()
.inert_fields(&style)
.is_empty()
);
}
#[test]
fn a_mesh_draws_no_shadow_layers() {
let style = OverlayStyle::solid(Colour::WHITE).with_shadows(vec![ShadowLayer::default()]);
assert_eq!(
OverlayStyleSupport::for_mesh().inert_fields(&style),
vec!["shadows"]
);
assert!(
OverlayStyleSupport::for_mesh().draws_fill(&OverlayFill::custom(
crate::overlay::CustomFillId(0),
[0.0; 16]
))
);
}
}