@@ -7,7 +7,7 @@ use bytemuck::{Pod, Zeroable};
77use std:: sync:: Arc ;
88
99/// A rendering primitive.
10- #[ derive( Debug , Clone ) ]
10+ #[ derive( Debug , Clone , PartialEq ) ]
1111#[ non_exhaustive]
1212pub enum Primitive {
1313 /// A text primitive
@@ -147,10 +147,71 @@ impl Primitive {
147147 content : Box :: new ( self ) ,
148148 }
149149 }
150+
151+ pub fn bounds ( & self ) -> Rectangle {
152+ match self {
153+ Self :: Text {
154+ bounds,
155+ horizontal_alignment,
156+ vertical_alignment,
157+ ..
158+ } => {
159+ let mut bounds = * bounds;
160+
161+ bounds. x = match horizontal_alignment {
162+ alignment:: Horizontal :: Left => bounds. x ,
163+ alignment:: Horizontal :: Center => {
164+ bounds. x - bounds. width / 2.0
165+ }
166+ alignment:: Horizontal :: Right => bounds. x - bounds. width ,
167+ } ;
168+
169+ bounds. y = match vertical_alignment {
170+ alignment:: Vertical :: Top => bounds. y ,
171+ alignment:: Vertical :: Center => {
172+ bounds. y - bounds. height / 2.0
173+ }
174+ alignment:: Vertical :: Bottom => bounds. y - bounds. height ,
175+ } ;
176+
177+ bounds. expand ( 1.5 )
178+ }
179+ Self :: Quad { bounds, .. }
180+ | Self :: Image { bounds, .. }
181+ | Self :: Svg { bounds, .. } => bounds. expand ( 1.0 ) ,
182+ Self :: Clip { bounds, .. } => bounds. expand ( 1.0 ) ,
183+ Self :: SolidMesh { size, .. } | Self :: GradientMesh { size, .. } => {
184+ Rectangle :: with_size ( * size)
185+ }
186+ #[ cfg( feature = "tiny-skia" ) ]
187+ Self :: Fill { path, .. } | Self :: Stroke { path, .. } => {
188+ let bounds = path. bounds ( ) ;
189+
190+ Rectangle {
191+ x : bounds. x ( ) ,
192+ y : bounds. y ( ) ,
193+ width : bounds. width ( ) ,
194+ height : bounds. height ( ) ,
195+ }
196+ . expand ( 1.0 )
197+ }
198+ Self :: Group { primitives } => primitives
199+ . iter ( )
200+ . map ( Self :: bounds)
201+ . fold ( Rectangle :: with_size ( Size :: ZERO ) , |a, b| {
202+ Rectangle :: union ( & a, & b)
203+ } ) ,
204+ Self :: Translate {
205+ translation,
206+ content,
207+ } => content. bounds ( ) + * translation,
208+ Self :: Cache { content } => content. bounds ( ) ,
209+ }
210+ }
150211}
151212
152213/// A set of [`Vertex2D`] and indices representing a list of triangles.
153- #[ derive( Clone , Debug ) ]
214+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
154215pub struct Mesh2D < T > {
155216 /// The vertices of the mesh
156217 pub vertices : Vec < T > ,
@@ -162,15 +223,15 @@ pub struct Mesh2D<T> {
162223}
163224
164225/// A two-dimensional vertex.
165- #[ derive( Copy , Clone , Debug , Zeroable , Pod ) ]
226+ #[ derive( Copy , Clone , Debug , PartialEq , Zeroable , Pod ) ]
166227#[ repr( C ) ]
167228pub struct Vertex2D {
168229 /// The vertex position in 2D space.
169230 pub position : [ f32 ; 2 ] ,
170231}
171232
172233/// A two-dimensional vertex with a color.
173- #[ derive( Copy , Clone , Debug , Zeroable , Pod ) ]
234+ #[ derive( Copy , Clone , Debug , PartialEq , Zeroable , Pod ) ]
174235#[ repr( C ) ]
175236pub struct ColoredVertex2D {
176237 /// The vertex position in 2D space.
0 commit comments