Skip to main content

Crate path_kit

Crate path_kit 

Source
Expand description

§path-kit

基于 Skia PathKit 的 Rust 路径运算库,提供 safe 的 API 封装。 A Rust path operations library based on Skia PathKit with safe API wrappers.

§功能 / Features

  • 路径构建:线段、二次/三次贝塞尔、矩形、椭圆、圆、圆角矩形、RRect(四角独立半径) Path construction: lines, quadratic/cubic bezier, rect, oval, circle, round rect, RRect (per-corner radii)
  • 路径布尔运算:并集、交集、差集、异或 Path boolean operations: union, intersect, difference, xor
  • 路径简化、包围盒simplify, path.tight_bounds, pathops_tight_bounds Path simplification and tight bounds computation
  • 路径迭代:按动词遍历 Move/Line/Quad/Cubic/Close Path iteration over verbs and points
  • 描边:将路径转为描边轮廓 Stroke: convert path to stroked outline

§线程安全 / Thread safety

当前未保证 Send / Sync,请勿跨线程共享 PathRRectStrokeRec 等类型。 Send / Sync are not guaranteed; do not share Path, RRect, StrokeRec, etc. across threads.

§类型概览 / Types

类型说明
Path路径
Rect矩形
RRect圆角矩形(支持四角独立半径)
Point二维点
Direction绘制方向 Cw/Ccw
RectCorner矩形起始角
PathOp布尔运算类型
PathVerbItem路径迭代项
StrokeRec描边参数

§示例 / Examples

§路径布尔运算 / Path boolean ops

use path_kit::{Path, Rect, Direction, RectCorner, PathOp, path_op, OpBuilder};

let mut path1 = Path::new();
path1.add_rect(&Rect::new(0.0, 0.0, 100.0, 100.0), Direction::Cw, RectCorner::UpperLeft);

let mut path2 = Path::new();
path2.add_rect(&Rect::new(50.0, 50.0, 150.0, 150.0), Direction::Cw, RectCorner::UpperLeft);

let union = path_op(&path1, &path2, PathOp::Union).unwrap();

// 批量运算 / Batch operations (use add_ref to avoid clone when reusing paths)
let result = OpBuilder::new()
    .add_ref(&path1, PathOp::Union)
    .add_ref(&path2, PathOp::Union)
    .resolve()
    .unwrap();

§圆角矩形 RRect / Rounded rect with per-corner radii

use path_kit::{Path, Rect, RRect, Radii, Direction, RectCorner};

// 统一圆角 / Uniform radii
let rr = RRect::from_rect_xy(&Rect::new(0.0, 0.0, 100.0, 50.0), 10.0, 10.0);
let mut path = Path::new();
path.add_rrect(&rr, Direction::Cw);

// 四角独立半径 / Per-corner radii
let radii = [
    Radii { x: 10.0, y: 10.0 },
    Radii { x: 20.0, y: 10.0 },
    Radii { x: 10.0, y: 20.0 },
    Radii { x: 5.0, y: 5.0 },
];
let rr2 = RRect::from_rect_radii(&Rect::new(0.0, 0.0, 80.0, 60.0), &radii);
path.add_rrect(&rr2, Direction::Ccw);

§路径迭代 / Path iteration

use path_kit::{Path, PathVerbItem};

let mut path = Path::new();
path.move_to(0.0, 0.0).line_to(100.0, 0.0).line_to(100.0, 100.0).close();

for item in path.iter(false) {
    match item {
        PathVerbItem::Move(p) => println!("Move to {:?}", p),
        PathVerbItem::Line(from, to) => println!("Line {:?} -> {:?}", from, to),
        PathVerbItem::Quad(c, to) => println!("Quad {:?} -> {:?}", c, to),
        PathVerbItem::Cubic(c1, c2, to) => println!("Cubic -> {:?}", to),
        PathVerbItem::Close => println!("Close"),
        _ => {}
    }
}

§描边 / Stroke

use path_kit::{Path, StrokeRec};

let rec = StrokeRec::new_stroke(4.0, false);
let mut path = Path::new();
path.move_to(0.0, 0.0).line_to(100.0, 0.0);
let stroked = rec.apply_to_path(&path).unwrap();

§路径简化与包围盒 / Simplify and bounds

use path_kit::{Path, simplify, pathops_tight_bounds};

let mut path = Path::new();
path.move_to(0.0, 0.0)
    .line_to(100.0, 0.0)
    .line_to(100.0, 100.0)
    .line_to(50.0, 50.0)
    .line_to(0.0, 100.0)
    .close();

let simplified = simplify(&path).unwrap();
let bounds = pathops_tight_bounds(&path).unwrap();  // or path.tight_bounds() for infallible

Structs§

CornerPathEffect
圆角路径效果,将尖角变为圆角。 Corner path effect - rounds sharp corners.
DashPathEffect
虚线路径效果,将路径描边转为虚线。 Dash path effect for stroked paths (on/off intervals).
OpBuilder
路径操作构建器,用于批量执行路径布尔运算。 Path operation builder, optimized for unioning/combining many paths.
Path
路径的 safe 封装,对应 SkPath。Safe wrapper for SkPath.
PathIter
路径迭代器,按动词顺序遍历路径。Path iterator over verbs and points.
Point
二维点。A 2D point with x and y coordinates.
RRect
圆角矩形,支持四角独立半径。 Rounded rectangle with per-corner radii (each corner can have different x/y).
Radii
单角半径 (x, y)。Corner radii (x, y).
Rect
矩形,由 (left, top, right, bottom) 定义。 Rectangle defined by (left, top, right, bottom) coordinates.
StrokeRec
描边参数,描述如何将路径转为描边轮廓。 Stroke parameters - describes how to expand a path to a stroke outline.

Enums§

Direction
路径绘制方向。Direction for path winding.
PathOp
并集、交集、差集、异或等路径集合运算。Union, intersect, difference, xor, reverse difference.
PathVerb
路径动词,描述路径中的每个图元。Path verb - describes each element in the path.
PathVerbItem
单步迭代结果,含动词与关联点。Single iteration result with verb and points.
RectCorner
矩形起始角,指定 add_rect 时从哪个角开始绘制。 Rectangle start corner for add_rect (which corner to begin drawing from).
StrokeStyle
描边样式。Stroke style.

Functions§

path_op
对两个路径执行布尔运算。Performs boolean operation on two paths. Returns None if the operation fails.
pathops_tight_bounds
计算路径的紧密包围盒(pathops 实现)。Computes tight bounds using pathops algorithm.
simplify
简化路径,处理自相交等。Simplifies path (resolves self-intersections, etc.). Returns None if simplification fails.