From e835cea03c5d6eeba2d76b52206516dcc2a6b628 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Thu, 27 Jan 2022 09:40:52 -0800 Subject: Add line dash API --- graphics/src/widget/canvas/path.rs | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'graphics/src/widget/canvas/path.rs') diff --git a/graphics/src/widget/canvas/path.rs b/graphics/src/widget/canvas/path.rs index 4e4fd734..b52c1f90 100644 --- a/graphics/src/widget/canvas/path.rs +++ b/graphics/src/widget/canvas/path.rs @@ -7,7 +7,11 @@ mod builder; pub use arc::Arc; pub use builder::Builder; +use crate::canvas::LineDash; + use iced_native::{Point, Size}; +use lyon::path::iterator::PathIterator; +use lyon_algorithms::walk::{walk_along_path, RepeatedPattern}; /// An immutable set of points that may or may not be connected. /// @@ -66,3 +70,43 @@ impl Path { } } } + +pub(super) fn dashed(path: &Path, line_dash: LineDash) -> Path { + let segments_odd = line_dash.segments.len() % 2 == 1; + + let segments = segments_odd + .then(|| [&line_dash.segments[..], &line_dash.segments[..]].concat()) + .unwrap_or(line_dash.segments); + + let mut points = vec![]; + + walk_along_path( + path.raw().iter().flattened(0.01), + 0.0, + &mut RepeatedPattern { + callback: |position: lyon_algorithms::math::Point, + _tangent, + _distance| { + points.push(Point { + x: position.x, + y: position.y, + }); + true + }, + index: line_dash.offset, + intervals: &segments, + }, + ); + + Path::new(|builder| { + for (idx, point) in points.into_iter().enumerate() { + let is_even = idx % 2 == 0; + + if is_even { + builder.move_to(point); + } else { + builder.line_to(point); + } + } + }) +} -- cgit From 730c57ba67e20e2179418961e50fdc24acfdb677 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Thu, 27 Jan 2022 10:01:03 -0800 Subject: Remove vec allocation --- graphics/src/widget/canvas/path.rs | 71 +++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 35 deletions(-) (limited to 'graphics/src/widget/canvas/path.rs') diff --git a/graphics/src/widget/canvas/path.rs b/graphics/src/widget/canvas/path.rs index b52c1f90..bc258b67 100644 --- a/graphics/src/widget/canvas/path.rs +++ b/graphics/src/widget/canvas/path.rs @@ -72,41 +72,42 @@ impl Path { } pub(super) fn dashed(path: &Path, line_dash: LineDash) -> Path { - let segments_odd = line_dash.segments.len() % 2 == 1; - - let segments = segments_odd - .then(|| [&line_dash.segments[..], &line_dash.segments[..]].concat()) - .unwrap_or(line_dash.segments); - - let mut points = vec![]; - - walk_along_path( - path.raw().iter().flattened(0.01), - 0.0, - &mut RepeatedPattern { - callback: |position: lyon_algorithms::math::Point, - _tangent, - _distance| { - points.push(Point { - x: position.x, - y: position.y, - }); - true - }, - index: line_dash.offset, - intervals: &segments, - }, - ); - Path::new(|builder| { - for (idx, point) in points.into_iter().enumerate() { - let is_even = idx % 2 == 0; - - if is_even { - builder.move_to(point); - } else { - builder.line_to(point); - } - } + let segments_odd = line_dash.segments.len() % 2 == 1; + + let segments = segments_odd + .then(|| { + [&line_dash.segments[..], &line_dash.segments[..]].concat() + }) + .unwrap_or(line_dash.segments); + + let mut draw_line = false; + + walk_along_path( + path.raw().iter().flattened(0.01), + 0.0, + &mut RepeatedPattern { + callback: |position: lyon_algorithms::math::Point, + _tangent, + _distance| { + let point = Point { + x: position.x, + y: position.y, + }; + + if draw_line { + builder.line_to(point); + } else { + builder.move_to(point); + } + + draw_line = !draw_line; + + true + }, + index: line_dash.offset, + intervals: &segments, + }, + ); }) } -- cgit From f56c8a7361ceb215bce68e88bd6ce402e2694693 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Feb 2022 17:18:05 +0700 Subject: Ask for a slice of segments instead of ownership in `LineDash` --- graphics/src/widget/canvas/path.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'graphics/src/widget/canvas/path.rs') diff --git a/graphics/src/widget/canvas/path.rs b/graphics/src/widget/canvas/path.rs index bc258b67..cb7e5035 100644 --- a/graphics/src/widget/canvas/path.rs +++ b/graphics/src/widget/canvas/path.rs @@ -71,15 +71,11 @@ impl Path { } } -pub(super) fn dashed(path: &Path, line_dash: LineDash) -> Path { +pub(super) fn dashed(path: &Path, line_dash: LineDash<'_>) -> Path { Path::new(|builder| { - let segments_odd = line_dash.segments.len() % 2 == 1; - - let segments = segments_odd - .then(|| { - [&line_dash.segments[..], &line_dash.segments[..]].concat() - }) - .unwrap_or(line_dash.segments); + let segments_odd = (line_dash.segments.len() % 2 == 1).then(|| { + [&line_dash.segments[..], &line_dash.segments[..]].concat() + }); let mut draw_line = false; @@ -106,7 +102,10 @@ pub(super) fn dashed(path: &Path, line_dash: LineDash) -> Path { true }, index: line_dash.offset, - intervals: &segments, + intervals: segments_odd + .as_ref() + .map(Vec::as_slice) + .unwrap_or(line_dash.segments), }, ); }) -- cgit From bace264bfe5e3cb5046867bd411e54969a637c79 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Feb 2022 17:19:48 +0700 Subject: Access `lyon_algorithms` indirectly through `lyon` --- graphics/src/widget/canvas/path.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'graphics/src/widget/canvas/path.rs') diff --git a/graphics/src/widget/canvas/path.rs b/graphics/src/widget/canvas/path.rs index cb7e5035..1728f060 100644 --- a/graphics/src/widget/canvas/path.rs +++ b/graphics/src/widget/canvas/path.rs @@ -10,8 +10,8 @@ pub use builder::Builder; use crate::canvas::LineDash; use iced_native::{Point, Size}; +use lyon::algorithms::walk::{walk_along_path, RepeatedPattern}; use lyon::path::iterator::PathIterator; -use lyon_algorithms::walk::{walk_along_path, RepeatedPattern}; /// An immutable set of points that may or may not be connected. /// @@ -83,7 +83,7 @@ pub(super) fn dashed(path: &Path, line_dash: LineDash<'_>) -> Path { path.raw().iter().flattened(0.01), 0.0, &mut RepeatedPattern { - callback: |position: lyon_algorithms::math::Point, + callback: |position: lyon::algorithms::math::Point, _tangent, _distance| { let point = Point { -- cgit