path_kit/lib.rs
1//! # path-kit
2//!
3//! 基于 Skia PathKit 的 Rust 路径运算库,提供 safe 的 API 封装。
4//! A Rust path operations library based on Skia PathKit with safe API wrappers.
5//!
6//! ## 功能 / Features
7//!
8//! - **路径构建**:线段、二次/三次贝塞尔、矩形、椭圆、圆、圆角矩形、RRect(四角独立半径)
9//! Path construction: lines, quadratic/cubic bezier, rect, oval, circle, round rect, RRect (per-corner radii)
10//! - **路径布尔运算**:并集、交集、差集、异或
11//! Path boolean operations: union, intersect, difference, xor
12//! - **路径简化、包围盒**:`simplify`, `path.tight_bounds`, `pathops_tight_bounds`
13//! Path simplification and tight bounds computation
14//! - **路径迭代**:按动词遍历 Move/Line/Quad/Cubic/Close
15//! Path iteration over verbs and points
16//! - **描边**:将路径转为描边轮廓
17//! Stroke: convert path to stroked outline
18//!
19//! ## 线程安全 / Thread safety
20//!
21//! 当前未保证 `Send` / `Sync`,请勿跨线程共享 `Path`、`RRect`、`StrokeRec` 等类型。
22//! `Send` / `Sync` are not guaranteed; do not share `Path`, `RRect`, `StrokeRec`, etc. across threads.
23//!
24//! ## 类型概览 / Types
25//!
26//! | 类型 | 说明 |
27//! |------|------|
28//! | [`Path`] | 路径 |
29//! | [`Rect`] | 矩形 |
30//! | [`RRect`] | 圆角矩形(支持四角独立半径) |
31//! | [`Point`] | 二维点 |
32//! | [`Direction`] | 绘制方向 Cw/Ccw |
33//! | [`RectCorner`] | 矩形起始角 |
34//! | [`PathOp`] | 布尔运算类型 |
35//! | [`PathVerbItem`] | 路径迭代项 |
36//! | [`StrokeRec`] | 描边参数 |
37//!
38//! ## 示例 / Examples
39//!
40//! ### 路径布尔运算 / Path boolean ops
41//!
42//! ```rust
43//! use path_kit::{Path, Rect, Direction, RectCorner, PathOp, path_op, OpBuilder};
44//!
45//! let mut path1 = Path::new();
46//! path1.add_rect(&Rect::new(0.0, 0.0, 100.0, 100.0), Direction::Cw, RectCorner::UpperLeft);
47//!
48//! let mut path2 = Path::new();
49//! path2.add_rect(&Rect::new(50.0, 50.0, 150.0, 150.0), Direction::Cw, RectCorner::UpperLeft);
50//!
51//! let union = path_op(&path1, &path2, PathOp::Union).unwrap();
52//!
53//! // 批量运算 / Batch operations (use add_ref to avoid clone when reusing paths)
54//! let result = OpBuilder::new()
55//! .add_ref(&path1, PathOp::Union)
56//! .add_ref(&path2, PathOp::Union)
57//! .resolve()
58//! .unwrap();
59//! ```
60//!
61//! ### 圆角矩形 RRect / Rounded rect with per-corner radii
62//!
63//! ```rust
64//! use path_kit::{Path, Rect, RRect, Radii, Direction, RectCorner};
65//!
66//! // 统一圆角 / Uniform radii
67//! let rr = RRect::from_rect_xy(&Rect::new(0.0, 0.0, 100.0, 50.0), 10.0, 10.0);
68//! let mut path = Path::new();
69//! path.add_rrect(&rr, Direction::Cw);
70//!
71//! // 四角独立半径 / Per-corner radii
72//! let radii = [
73//! Radii { x: 10.0, y: 10.0 },
74//! Radii { x: 20.0, y: 10.0 },
75//! Radii { x: 10.0, y: 20.0 },
76//! Radii { x: 5.0, y: 5.0 },
77//! ];
78//! let rr2 = RRect::from_rect_radii(&Rect::new(0.0, 0.0, 80.0, 60.0), &radii);
79//! path.add_rrect(&rr2, Direction::Ccw);
80//! ```
81//!
82//! ### 路径迭代 / Path iteration
83//!
84//! ```rust
85//! use path_kit::{Path, PathVerbItem};
86//!
87//! let mut path = Path::new();
88//! path.move_to(0.0, 0.0).line_to(100.0, 0.0).line_to(100.0, 100.0).close();
89//!
90//! for item in path.iter(false) {
91//! match item {
92//! PathVerbItem::Move(p) => println!("Move to {:?}", p),
93//! PathVerbItem::Line(from, to) => println!("Line {:?} -> {:?}", from, to),
94//! PathVerbItem::Quad(c, to) => println!("Quad {:?} -> {:?}", c, to),
95//! PathVerbItem::Cubic(c1, c2, to) => println!("Cubic -> {:?}", to),
96//! PathVerbItem::Close => println!("Close"),
97//! _ => {}
98//! }
99//! }
100//! ```
101//!
102//! ### 描边 / Stroke
103//!
104//! ```rust
105//! use path_kit::{Path, StrokeRec};
106//!
107//! let rec = StrokeRec::new_stroke(4.0, false);
108//! let mut path = Path::new();
109//! path.move_to(0.0, 0.0).line_to(100.0, 0.0);
110//! let stroked = rec.apply_to_path(&path).unwrap();
111//! ```
112//!
113//! ### 路径简化与包围盒 / Simplify and bounds
114//!
115//! ```rust
116//! use path_kit::{Path, simplify, pathops_tight_bounds};
117//!
118//! let mut path = Path::new();
119//! path.move_to(0.0, 0.0)
120//! .line_to(100.0, 0.0)
121//! .line_to(100.0, 100.0)
122//! .line_to(50.0, 50.0)
123//! .line_to(0.0, 100.0)
124//! .close();
125//!
126//! let simplified = simplify(&path).unwrap();
127//! let bounds = pathops_tight_bounds(&path).unwrap(); // or path.tight_bounds() for infallible
128//! ```
129
130/// PathKit FFI 绑定(仅内部使用,不对外暴露)。
131/// PathKit FFI bindings (internal only, not exposed to external users).
132#[doc(hidden)]
133#[allow(warnings)]
134mod pathkit {
135 pub use root::pk::*;
136 include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
137}
138
139mod corner_path_effect;
140mod dash_path_effect;
141mod direction;
142mod op_builder;
143mod ops;
144mod path;
145mod path_iter;
146mod path_op;
147mod point;
148mod rect;
149mod rect_corner;
150mod rrect;
151mod stroke_rec;
152
153pub use corner_path_effect::CornerPathEffect;
154pub use dash_path_effect::DashPathEffect;
155pub use direction::Direction;
156pub use op_builder::OpBuilder;
157pub use ops::{path_op, pathops_tight_bounds, simplify};
158pub use path::Path;
159pub use path_iter::{PathIter, PathVerb, PathVerbItem};
160pub use path_op::PathOp;
161pub use point::Point;
162pub use rect::Rect;
163pub use rect_corner::RectCorner;
164pub use rrect::{Radii, RRect};
165pub use stroke_rec::{StrokeRec, StrokeStyle};
166
167#[cfg(test)]
168mod tests;