Skip to main content

path_kit/
ops.rs

1//! 路径运算函数。Path operation functions.
2
3use crate::path::Path;
4use crate::path_op::PathOp;
5use crate::pathkit;
6use crate::rect::Rect;
7
8/// 对两个路径执行布尔运算。Performs boolean operation on two paths.
9/// Returns None if the operation fails.
10pub fn path_op(path1: &Path, path2: &Path, op: PathOp) -> Option<Path> {
11    let mut result = Path::new();
12    let ok = unsafe {
13        pathkit::Op(
14            path1.as_raw() as *const _,
15            path2.as_raw() as *const _,
16            op.into(),
17            result.as_raw_mut() as *mut _,
18        )
19    };
20    if ok {
21        Some(result)
22    } else {
23        None
24    }
25}
26
27/// 简化路径,处理自相交等。Simplifies path (resolves self-intersections, etc.).
28/// Returns None if simplification fails.
29pub fn simplify(path: &Path) -> Option<Path> {
30    let mut result = Path::new();
31    let ok = unsafe {
32        pathkit::Simplify(path.as_raw() as *const _, result.as_raw_mut() as *mut _)
33    };
34    if ok {
35        Some(result)
36    } else {
37        None
38    }
39}
40
41/// 计算路径的紧密包围盒(pathops 实现)。Computes tight bounds using pathops algorithm.
42///
43/// 对含曲线路径可能比 `Path::tight_bounds` 更精确,但在解析失败时返回 None。
44/// More accurate for curved paths than `Path::tight_bounds`, but returns None on parse failure.
45pub fn pathops_tight_bounds(path: &Path) -> Option<Rect> {
46    let mut result = pathkit::SkRect {
47        fLeft: 0.0,
48        fTop: 0.0,
49        fRight: 0.0,
50        fBottom: 0.0,
51    };
52    let ok = unsafe { pathkit::TightBounds(path.as_raw() as *const _, &mut result as *mut _) };
53    if ok {
54        Some(result.into())
55    } else {
56        None
57    }
58}