Skip to main content

path_kit/
corner_path_effect.rs

1//! 圆角路径效果。Corner path effect.
2
3use crate::bridge::ffi;
4use crate::path::Path;
5use crate::stroke_rec::StrokeRec;
6use cxx::UniquePtr;
7
8/// 圆角路径效果,将尖角变为圆角。
9/// Corner path effect - rounds sharp corners.
10pub struct CornerPathEffect {
11    inner: UniquePtr<ffi::PathEffectHolder>,
12}
13
14impl CornerPathEffect {
15    /// 创建圆角效果。Creates a corner effect.
16    ///
17    /// `radius`: 圆角半径,必须 > 0
18    /// `radius`: corner radius, must be > 0
19    pub fn new(radius: f32) -> Option<Self> {
20        if radius <= 0.0 {
21            return None;
22        }
23        let inner = ffi::corner_effect_make(radius);
24        if inner.is_null() {
25            return None;
26        }
27        Some(Self { inner })
28    }
29
30    /// 应用到路径。Applies effect to path.
31    ///
32    /// 失败时返回 `None`。Returns `None` on failure.
33    pub fn filter_path(&self, path: &Path, stroke_width: f32) -> Option<Path> {
34        let mut dst = Path::new();
35        let mut rec = StrokeRec::new_stroke(stroke_width, false);
36        let bounds = path.tight_bounds();
37        let cull: ffi::Rect = bounds.into();
38        let ok = ffi::path_effect_filter(
39            self.inner.as_ref().expect("PathEffect"),
40            dst.as_raw_pin_mut(),
41            path.as_raw(),
42            rec.pin_holder_mut(),
43            &cull,
44        );
45        if ok {
46            Some(dst)
47        } else {
48            None
49        }
50    }
51}