Skip to main content

freya_core/style/
text_shadow.rs

1use std::hash::Hash;
2
3use freya_engine::prelude::*;
4
5use crate::style::color::Color;
6
7/// A shadow cast behind text, with a [`Color`], an `(x, y)` offset and a blur sigma.
8///
9/// Build it with [`TextShadow::new`]:
10///
11/// ```
12/// # use freya::prelude::*;
13/// let shadow = TextShadow::new(Color::BLACK, (1.0, 1.0), 2.0);
14/// ```
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16#[derive(Copy, Clone, Debug, PartialEq, Default)]
17pub struct TextShadow {
18    pub color: Color,
19    pub offset: (f32, f32),
20    pub blur_sigma: f64,
21}
22
23impl Hash for TextShadow {
24    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
25        self.color.hash(state);
26        self.offset.0.to_bits().hash(state);
27        self.offset.1.to_bits().hash(state);
28        self.blur_sigma.to_bits().hash(state);
29    }
30}
31
32impl TextShadow {
33    /// Create a [`TextShadow`] with the given [`Color`], `(x, y)` offset and blur sigma.
34    pub fn new(color: Color, offset: (f32, f32), blur_sigma: f64) -> Self {
35        Self {
36            color,
37            offset,
38            blur_sigma,
39        }
40    }
41}
42
43impl From<TextShadow> for SkTextShadow {
44    fn from(value: TextShadow) -> Self {
45        let color: SkColor = value.color.into();
46        SkTextShadow::new(
47            color,
48            SkPoint::new(value.offset.0, value.offset.1),
49            value.blur_sigma,
50        )
51    }
52}