From a9170c4330628f66fa13dce29dff59eded1c82e3 Mon Sep 17 00:00:00 2001 From: Chad Brokaw Date: Mon, 7 Nov 2022 23:26:51 -0500 Subject: [PATCH] prevent bad encoding with only move-tos --- piet-gpu/src/samples.rs | 11 +++++++++++ piet-scene/src/scene/builder.rs | 20 +++++++------------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/piet-gpu/src/samples.rs b/piet-gpu/src/samples.rs index a52ba74..0cbfd57 100644 --- a/piet-gpu/src/samples.rs +++ b/piet-gpu/src/samples.rs @@ -13,6 +13,10 @@ pub fn render_funky_paths(sb: &mut SceneBuilder) { LineTo((0.0, 400.0).into()), LineTo((100.0, 400.0).into()), ]; + let only_movetos = [ + PathElement::MoveTo((0.0, 0.0).into()), + PathElement::MoveTo((100.0, 100.0).into()), + ]; let empty: [PathElement; 0] = []; sb.fill( Fill::NonZero, @@ -28,6 +32,13 @@ pub fn render_funky_paths(sb: &mut SceneBuilder) { None, empty, ); + sb.fill( + Fill::NonZero, + Affine::IDENTITY, + &Color::rgb8(0, 0, 255).into(), + None, + only_movetos, + ); sb.stroke( &simple_stroke(8.0), Affine::translate(100.0, 100.0), diff --git a/piet-scene/src/scene/builder.rs b/piet-scene/src/scene/builder.rs index 8007753..7100433 100644 --- a/piet-scene/src/scene/builder.rs +++ b/piet-scene/src/scene/builder.rs @@ -160,8 +160,8 @@ impl<'a> SceneBuilder<'a> { PathElement::Close => b.close_path(), } } + b.finish(); if b.n_pathseg != 0 { - b.path(); self.scene.n_path += 1; self.scene.n_pathseg += b.n_pathseg; true @@ -434,7 +434,7 @@ impl<'a> PathBuilder<'a> { self.state = PathState::Start; } - fn finish(&mut self) { + pub fn finish(&mut self) { if self.is_fill { self.close_path(); } @@ -442,17 +442,11 @@ impl<'a> PathBuilder<'a> { let new_len = self.pathseg_stream.len() - 8; self.pathseg_stream.truncate(new_len); } - if let Some(tag) = self.tag_stream.last_mut() { - *tag |= 4; + if self.n_pathseg != 0 { + if let Some(tag) = self.tag_stream.last_mut() { + *tag |= 4; + } + self.tag_stream.push(0x10); } } - - /// Finish encoding a path. - /// - /// Encode this after encoding path segments. - pub fn path(&mut self) { - self.finish(); - // maybe don't encode if path is empty? might throw off sync though - self.tag_stream.push(0x10); - } }