Skip to main content

path_kit/
corner_path_effect.rs

1//! 圆角路径效果。Corner path effect.
2//!
3//! ⚠️ **实验性 / Experimental**: sk_sp 生命周期与 Rust 集成尚不完善,使用时需注意。
4//! ⚠️ **Experimental**: sk_sp lifecycle integration with Rust is incomplete; use with caution.
5
6use crate::path::Path;
7use crate::pathkit;
8
9/// 圆角路径效果,将尖角变为圆角。
10/// Corner path effect - rounds sharp corners.
11pub struct CornerPathEffect {
12    inner: pathkit::sk_sp<pathkit::SkPathEffect>,
13}
14
15impl CornerPathEffect {
16    /// 创建圆角效果。Creates a corner effect.
17    ///
18    /// `radius`: 圆角半径,必须 > 0
19    /// `radius`: corner radius, must be > 0
20    pub fn new(radius: f32) -> Option<Self> {
21        if radius <= 0.0 {
22            return None;
23        }
24        let inner = unsafe { pathkit::SkCornerPathEffect_Make(radius) };
25        if inner.fPtr.is_null() {
26            return None;
27        }
28        Some(Self { inner })
29    }
30
31    /// 应用到路径。Applies effect to path.
32    pub fn filter_path(&self, path: &Path, stroke_width: f32) -> Option<Path> {
33        let mut dst = Path::new();
34        let mut rec = unsafe {
35            pathkit::SkStrokeRec::new(pathkit::SkStrokeRec_InitStyle::kHairline_InitStyle)
36        };
37        unsafe {
38            rec.setStrokeStyle(stroke_width, false);
39        }
40        let bounds = path.tight_bounds();
41        let cull: pathkit::SkRect = bounds.into();
42        let ok = unsafe {
43            pathkit::SkPathEffect_filterPath(
44                self.inner.fPtr,
45                dst.as_raw_mut() as *mut _,
46                path.as_raw() as *const _,
47                &mut rec as *mut _,
48                &cull as *const _,
49            )
50        };
51        if ok {
52            Some(dst)
53        } else {
54            None
55        }
56    }
57}