Skip to main content

SmoothRect

Struct SmoothRect 

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

矩形便捷封装。

Implementations§

Source§

impl SmoothRect

Source

pub fn new(width: f64, height: f64) -> Self

创建矩形便捷封装。

宽高为非有限数、负数或零时会退化为单点闭合路径。

Examples found in repository?
examples/demo/main.rs (line 28)
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 with_radius(self, radius: f64) -> Self

设置矩形角半径。

负数或非有限数会按 0 处理,过大的半径会 clamp 到短边的一半。

Examples found in repository?
examples/demo/main.rs (line 29)
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 with_smoothing(self, smoothing: f64) -> Self

设置 Sketch-like smoothing。

有限数会 clamp 到 [0, 1],非有限数会按 0 处理。

Examples found in repository?
examples/demo/main.rs (line 30)
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 to_path(&self) -> SmoothPath

生成矩形 smooth corner 路径。

Examples found in repository?
examples/demo/main.rs (line 31)
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 SmoothRect

Source§

fn clone(&self) -> SmoothRect

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 SmoothRect

Source§

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

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

impl PartialEq for SmoothRect

Source§

fn eq(&self, other: &SmoothRect) -> 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 Copy for SmoothRect

Source§

impl StructuralPartialEq for SmoothRect

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.