@@ -194,73 +194,71 @@ impl PixState {
194194 angle_radians = angle. map ( Scalar :: to_radians) ;
195195 }
196196
197- let mut render_text = |mut color : Color , outline : u8 | -> PixResult < ( u32 , u32 ) > {
198- s. push ( ) ;
197+ let mut render_text =
198+ |pos : PointI2 , mut color : Color , outline : u8 | -> PixResult < Rect < i32 > > {
199+ s. push ( ) ;
199200
200- // Make sure to offset the text if an outline was drawn
201- if stroke. is_some ( ) && stroke_weight > 0 && outline == 0 {
202- pos += i32:: from ( stroke_weight) ;
203- }
204- if disabled {
205- color = color. blended ( colors. background , 0.38 ) ;
206- }
207-
208- let ( w, h) = if wrap_width. is_some ( ) {
209- s. renderer . text (
210- pos,
211- text,
212- wrap_width,
213- angle,
214- center,
215- flipped,
216- Some ( color) ,
217- outline,
218- ) ?
219- } else {
220- let mut x = pos. x ( ) ;
221- let mut y = pos. y ( ) ;
222- let ( mut total_width, mut total_height) = ( 0 , 0 ) ;
223- for line in text. split ( '\n' ) {
224- let ( line_width, line_height) = s. renderer . size_of ( line, wrap_width) ?;
225- let rect = rect ! [ 0 , 0 , clamp_size( line_width) , clamp_size( line_height) ] ;
226- let bounding_box =
227- angle_radians. map_or ( rect, |angle| rect. rotated ( angle, center) ) ;
228- x -= bounding_box. x ( ) ;
229- y -= bounding_box. y ( ) ;
201+ if disabled {
202+ color = color. blended ( colors. background , 0.38 ) ;
203+ }
204+ let ( w, h) = if wrap_width. is_some ( ) {
230205 s. renderer . text (
231- point ! [ x , y ] ,
232- line ,
206+ pos ,
207+ text ,
233208 wrap_width,
234209 angle,
235210 center,
236211 flipped,
237212 Some ( color) ,
238213 outline,
239- ) ?;
240- total_width += bounding_box. width ( ) as u32 ;
241- total_height += bounding_box. height ( ) as u32 ;
242- y += bounding_box. height ( ) ;
243- }
244- ( total_width, total_height)
214+ ) ?
215+ } else {
216+ let mut x = pos. x ( ) ;
217+ let mut y = pos. y ( ) ;
218+ let ( mut total_width, mut total_height) = ( 0 , 0 ) ;
219+ for line in text. split ( '\n' ) {
220+ let ( line_width, line_height) = s. renderer . size_of ( line, wrap_width) ?;
221+ let rect = rect ! [ 0 , 0 , clamp_size( line_width) , clamp_size( line_height) ] ;
222+ let bounding_box =
223+ angle_radians. map_or ( rect, |angle| rect. rotated ( angle, center) ) ;
224+ x -= bounding_box. x ( ) ;
225+ y -= bounding_box. y ( ) ;
226+ s. renderer . text (
227+ point ! [ x, y] ,
228+ line,
229+ wrap_width,
230+ angle,
231+ center,
232+ flipped,
233+ Some ( color) ,
234+ outline,
235+ ) ?;
236+ total_width += bounding_box. width ( ) as u32 ;
237+ total_height += bounding_box. height ( ) as u32 ;
238+ y += bounding_box. height ( ) ;
239+ }
240+ ( total_width, total_height)
241+ } ;
242+ let rect = rect ! [ pos, clamp_size( w) , clamp_size( h) ] ;
243+
244+ s. pop ( ) ;
245+ Ok ( rect)
245246 } ;
246- let rect = rect ! [ pos, clamp_size( w) , clamp_size( h) ] ;
247247
248- // Only advance the cursor if we're not drawing a text outline
249- if outline == 0 {
250- s. advance_cursor ( rect. size ( ) ) ;
248+ let rect = {
249+ let stroke_rect = match stroke {
250+ Some ( stroke) if stroke_weight > 0 => Some ( render_text ( pos, stroke, stroke_weight) ?) ,
251+ _ => None ,
252+ } ;
253+ if stroke_rect. is_some ( ) {
254+ pos += i32:: from ( stroke_weight) ;
251255 }
252-
253- s. pop ( ) ;
254- Ok ( ( w, h) )
255- } ;
256-
257- let stroke_size = match stroke {
258- Some ( stroke) if stroke_weight > 0 => Some ( render_text ( stroke, stroke_weight) ?) ,
259- _ => None ,
256+ let text_rect = render_text ( pos, fill, 0 ) ?;
257+ stroke_rect. unwrap_or ( text_rect)
260258 } ;
261- let size = render_text ( fill , 0 ) ? ;
259+ s . advance_cursor ( rect . size ( ) ) ;
262260
263- Ok ( stroke_size . unwrap_or ( size ) )
261+ Ok ( ( rect . width ( ) as u32 , rect . height ( ) as u32 ) )
264262 }
265263
266264 /// Draw bulleted text to the current canvas.
0 commit comments