Skip to content

Commit

Permalink
Fix duplicated points in SvgPath when unioning
Browse files Browse the repository at this point in the history
  • Loading branch information
fschutt committed Jan 19, 2024
1 parent 87ecc0f commit 5ca81aa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
20 changes: 20 additions & 0 deletions azul-core/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,26 @@ pub struct SvgPath {
}

impl SvgPath {

pub fn close(&mut self) {
let first = match self.items.as_ref().first() {
Some(s) => s,
None => return,
};
let last = match self.items.as_ref().last() {
Some(s) => s,
None => return,
};
if first.get_start() != last.get_end() {
let mut elements = self.items.as_slice().to_vec();
elements.push(SvgPathElement::Line(SvgLine {
start: last.get_end(),
end: first.get_start(),
}));
self.items = elements.into();
}
}

pub fn is_closed(&self) -> bool {
let first = self.items.as_ref().first();
let last = self.items.as_ref().last();
Expand Down
15 changes: 13 additions & 2 deletions azulc/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,14 @@ fn svg_multi_polygon_to_geo(poly: &SvgMultiPolygon) -> geo::MultiPolygon {
use geo::Intersects;

let linestrings = poly.rings.iter().map(|p| {
geo::LineString::new(p.items.iter().flat_map(|p| match p {

let mut p = p.clone();

if !p.is_closed() {
p.close();
}

let mut coords = p.items.iter().flat_map(|p| match p {
SvgPathElement::Line(l) => vec![
Coord { x: l.start.x as f64, y: l.start.y as f64 },
Coord { x: l.end.x as f64, y: l.end.y as f64 }
Expand All @@ -546,7 +553,11 @@ fn svg_multi_polygon_to_geo(poly: &SvgMultiPolygon) -> geo::MultiPolygon {
Coord { x: l.ctrl_2.x as f64, y: l.ctrl_2.y as f64 },
Coord { x: l.end.x as f64, y: l.end.y as f64 }
],
}.into_iter()).collect())
}.into_iter()).collect::<Vec<_>>();

coords.dedup();

geo::LineString::new(coords)
}).collect::<Vec<_>>();

let exterior_polys = linestrings.iter().filter(|ls| ls.is_cw()).cloned().collect::<Vec<geo::LineString<_>>>();
Expand Down

0 comments on commit 5ca81aa

Please sign in to comment.