Skip to main content

SmoothPath

Struct SmoothPath 

Source
pub struct SmoothPath { /* private fields */ }
Expand description

平滑路径。

Implementations§

Source§

impl SmoothPath

Source

pub fn new() -> Self

创建空路径。

Source

pub fn commands(&self) -> &[PathCommand]

返回底层路径命令。

Examples found in repository?
examples/demo/main.rs (line 51)
14fn main() {
15    let config = match parse_args(env::args().skip(1)) {
16        Ok(Some(config)) => config,
17        Ok(None) => return,
18        Err(message) => {
19            eprintln!("参数错误:{message}");
20            eprintln!("运行 `cargo run --example demo -- --help` 查看用法。");
21            process::exit(2);
22        }
23    };
24
25    let drawable_width = drawable_dimension(config.width, config.border_width);
26    let drawable_height = drawable_dimension(config.height, config.border_width);
27    let radius = effective_radius(&config);
28    let path = SmoothRect::new(drawable_width, drawable_height)
29        .with_radius(radius)
30        .with_smoothing(config.smoothing)
31        .to_path();
32    let path_data = path.to_svg_path_with_precision(config.precision);
33
34    if let Some(output) = config.output.as_ref() {
35        let svg = render_svg(&config, &path_data);
36        if let Err(error) = fs::write(output, svg) {
37            eprintln!("写入 SVG 文件失败:{}:{error}", output.display());
38            process::exit(1);
39        }
40        println!("已生成 SVG 文件:{}", output.display());
41    } else if config.svg {
42        println!("{}", render_svg(&config, &path_data));
43    } else {
44        println!("SVG 宽度:{}", format_number(config.width));
45        println!("SVG 高度:{}", format_number(config.height));
46        println!("路径宽度:{}", format_number(drawable_width));
47        println!("路径高度:{}", format_number(drawable_height));
48        println!("半径:{}", format_number(radius));
49        println!("平滑:{}", format_number(config.smoothing));
50        println!("边框:{}", format_number(config.border_width));
51        println!("命令数:{}", path.commands().len());
52        println!("SVG path:{path_data}");
53    }
54}
Source

pub fn cubics(&self) -> Vec<CubicSegment>

提取路径中的所有 cubic 段。

Source

pub fn move_to(&mut self, point: Point)

追加 MoveTo 命令。

Source

pub fn line_to(&mut self, point: Point)

追加 LineTo 命令。

Source

pub fn cubic_to(&mut self, ctrl1: Point, ctrl2: Point, to: Point)

追加 CubicTo 命令。

Source

pub fn close(&mut self)

追加闭合路径命令。

Source

pub fn export_with<F>(&self, formatter: &F) -> F::Output
where F: PathFormatter + ?Sized,

使用指定 formatter 导出路径。

这是输出层的主要扩展点:调用方可以实现 PathFormatter,把同一组 PathCommand 转换成 SVG、Godot、Canvas 或自定义函数调用格式。

Source

pub fn to_svg_path(&self) -> String

以默认 6 位小数输出 SVG path data。

Source

pub fn to_svg_path_with_precision(&self, precision: usize) -> String

按指定小数位数输出 SVG path data,最多保留 12 位。

Examples found in repository?
examples/demo/main.rs (line 32)
14fn main() {
15    let config = match parse_args(env::args().skip(1)) {
16        Ok(Some(config)) => config,
17        Ok(None) => return,
18        Err(message) => {
19            eprintln!("参数错误:{message}");
20            eprintln!("运行 `cargo run --example demo -- --help` 查看用法。");
21            process::exit(2);
22        }
23    };
24
25    let drawable_width = drawable_dimension(config.width, config.border_width);
26    let drawable_height = drawable_dimension(config.height, config.border_width);
27    let radius = effective_radius(&config);
28    let path = SmoothRect::new(drawable_width, drawable_height)
29        .with_radius(radius)
30        .with_smoothing(config.smoothing)
31        .to_path();
32    let path_data = path.to_svg_path_with_precision(config.precision);
33
34    if let Some(output) = config.output.as_ref() {
35        let svg = render_svg(&config, &path_data);
36        if let Err(error) = fs::write(output, svg) {
37            eprintln!("写入 SVG 文件失败:{}:{error}", output.display());
38            process::exit(1);
39        }
40        println!("已生成 SVG 文件:{}", output.display());
41    } else if config.svg {
42        println!("{}", render_svg(&config, &path_data));
43    } else {
44        println!("SVG 宽度:{}", format_number(config.width));
45        println!("SVG 高度:{}", format_number(config.height));
46        println!("路径宽度:{}", format_number(drawable_width));
47        println!("路径高度:{}", format_number(drawable_height));
48        println!("半径:{}", format_number(radius));
49        println!("平滑:{}", format_number(config.smoothing));
50        println!("边框:{}", format_number(config.border_width));
51        println!("命令数:{}", path.commands().len());
52        println!("SVG path:{path_data}");
53    }
54}

Trait Implementations§

Source§

impl Clone for SmoothPath

Source§

fn clone(&self) -> SmoothPath

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SmoothPath

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SmoothPath

Source§

fn default() -> SmoothPath

Returns the “default value” for a type. Read more
Source§

impl PartialEq for SmoothPath

Source§

fn eq(&self, other: &SmoothPath) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for SmoothPath

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.