Skip to main content

path_kit/
ops.rs

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