Skip to main content

skia_safe/core/
path_effect.rs

1//! Base class for objects in a [`crate::Paint`] that affect the geometry of a drawing primitive
2//! before it is transformed by the canvas' matrix and drawn.
3
4use crate::{Matrix, NativeFlattenable, Path, PathBuilder, Rect, StrokeRec, prelude::*};
5use sb::SkPathEffect_INHERITED;
6use skia_bindings::{self as sb, SkFlattenable, SkPathEffect, SkRefCntBase};
7use std::fmt;
8
9pub type PathEffect = RCHandle<SkPathEffect>;
10unsafe_send_sync!(PathEffect);
11require_type_equality!(SkPathEffect_INHERITED, SkFlattenable);
12
13impl NativeBase<SkRefCntBase> for SkPathEffect {}
14impl NativeBase<SkFlattenable> for SkPathEffect {}
15
16impl NativeRefCountedBase for SkPathEffect {
17    type Base = SkRefCntBase;
18}
19
20impl NativeFlattenable for SkPathEffect {
21    fn native_flattenable(&self) -> &SkFlattenable {
22        self.base()
23    }
24
25    fn native_deserialize(data: &[u8]) -> *mut Self {
26        unsafe { sb::C_SkPathEffect_Deserialize(data.as_ptr() as _, data.len()) }
27    }
28}
29
30impl fmt::Debug for PathEffect {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        f.debug_struct("PathEffect")
33            .field("needs_ctm", &self.needs_ctm())
34            .finish()
35    }
36}
37
38impl PathEffect {
39    /// Returns a path effect that applies each effect (`first` and `second`) to the original path,
40    /// and returns a path with the sum of these.
41    ///
42    /// `result = first(path) + second(path)`
43    ///
44    /// - `first` first effect
45    /// - `second` second effect
46    pub fn sum(first: impl Into<PathEffect>, second: impl Into<PathEffect>) -> PathEffect {
47        PathEffect::from_ptr(unsafe {
48            sb::C_SkPathEffect_MakeSum(first.into().into_ptr(), second.into().into_ptr())
49        })
50        .unwrap()
51    }
52
53    /// Returns a path effect that applies the inner effect to the path, and then applies the outer
54    /// effect to the result of the inner's.
55    ///
56    /// `result = outer(inner(path))`
57    ///
58    /// - `first` outer effect
59    /// - `second` inner effect
60    pub fn compose(first: impl Into<PathEffect>, second: impl Into<PathEffect>) -> PathEffect {
61        PathEffect::from_ptr(unsafe {
62            sb::C_SkPathEffect_MakeCompose(first.into().into_ptr(), second.into().into_ptr())
63        })
64        .unwrap()
65    }
66
67    /// Given a `src` path (input) and a stroke-rec (input and output), applies this effect to the
68    /// `src` path, returning the new path in `dst`, and returns true. If this effect cannot be
69    /// applied, returns false and ignores `dst` and the stroke-rec.
70    ///
71    /// The stroke-rec specifies the initial request for stroking (if any). The effect can treat
72    /// this as input only, or it can choose to change the rec as well. For example, the effect can
73    /// decide to change the stroke's width or join, or the effect can change the rec from stroke to
74    /// fill (or fill to stroke) in addition to returning a new (`dst`) path.
75    ///
76    /// If this method returns true, the caller will apply (as needed) the resulting stroke-rec to
77    /// `dst` and then draw.
78    ///
79    /// - `src` source path
80    /// - `stroke_rec` stroke record
81    /// - `cull_rect` cull rectangle
82    pub fn filter_path(
83        &self,
84        src: &Path,
85        stroke_rec: &StrokeRec,
86        cull_rect: impl AsRef<Rect>,
87    ) -> Option<(PathBuilder, StrokeRec)> {
88        let mut dst = PathBuilder::new();
89        let mut stroke_rec_r = stroke_rec.clone();
90        self.filter_path_inplace(&mut dst, src, &mut stroke_rec_r, cull_rect)
91            .then_some((dst, stroke_rec_r))
92    }
93
94    /// Applies this effect to the `src` path, returning the new path in `dst`. See
95    /// [`Self::filter_path()`] for the full contract.
96    ///
97    /// - `dst` destination path builder
98    /// - `src` source path
99    /// - `stroke_rec` stroke record
100    /// - `cull_rect` cull rectangle
101    pub fn filter_path_inplace(
102        &self,
103        dst: &mut PathBuilder,
104        src: &Path,
105        stroke_rec: &mut StrokeRec,
106        cull_rect: impl AsRef<Rect>,
107    ) -> bool {
108        unsafe {
109            self.native().filterPath(
110                dst.native_mut(),
111                src.native(),
112                stroke_rec.native_mut(),
113                cull_rect.as_ref().native(),
114                Matrix::new_identity().native(),
115            )
116        }
117    }
118
119    /// Applies this effect to the `src` path, returning the new path in `dst`. See
120    /// [`Self::filter_path()`] for the full contract.
121    ///
122    /// - `dst` destination path builder
123    /// - `src` source path
124    /// - `stroke_rec` stroke record
125    /// - `cull_rect` cull rectangle
126    /// - `ctm` current transformation matrix
127    pub fn filter_path_inplace_with_matrix(
128        &self,
129        dst: &mut PathBuilder,
130        src: &Path,
131        stroke_rec: &mut StrokeRec,
132        cull_rect: impl AsRef<Rect>,
133        ctm: &Matrix,
134    ) -> bool {
135        unsafe {
136            self.native().filterPath(
137                dst.native_mut(),
138                src.native(),
139                stroke_rec.native_mut(),
140                cull_rect.as_ref().native(),
141                ctm.native(),
142            )
143        }
144    }
145
146    /// Returns true if this path effect requires a valid CTM.
147    pub fn needs_ctm(&self) -> bool {
148        unsafe { self.native().needsCTM() }
149    }
150}