@@ -296,6 +296,7 @@ public bool ProcessMouseEvent(MouseEventArgs args)
296296 public override LayoutSize MeasureDOM ( LayoutConstraints constraints )
297297 {
298298 int targetWidth = Width ?? constraints . MaxWidth ;
299+ int contentWidth = Math . Max ( 0 , targetWidth - Margin . Left - Margin . Right ) ;
299300
300301 // Calculate content dimensions
301302 List < string > snapshot ;
@@ -305,22 +306,20 @@ public override LayoutSize MeasureDOM(LayoutConstraints constraints)
305306
306307 foreach ( var line in snapshot )
307308 {
308- // Split by embedded newlines to count actual rendered lines
309- var subLines = line . Split ( '\n ' ) ;
310- foreach ( var subLine in subLines )
311- {
312- int lineWidth = Parsing . MarkupParser . StripLength ( subLine ) ;
313- maxContentWidth = Math . Max ( maxContentWidth , lineWidth ) ;
309+ int lineWidth = Parsing . MarkupParser . StripLength ( line ) ;
310+ maxContentWidth = Math . Max ( maxContentWidth , lineWidth ) ;
314311
315- if ( _wrap && lineWidth > targetWidth && targetWidth > 0 )
316- {
317- // Estimate wrapped lines
318- totalLines += ( int ) Math . Ceiling ( ( double ) lineWidth / targetWidth ) ;
319- }
320- else
321- {
322- totalLines ++ ;
323- }
312+ if ( _wrap && contentWidth > 0 )
313+ {
314+ // Use actual word-wrap logic for accurate line count
315+ var wrappedLines = Parsing . MarkupParser . ParseLines ( line , contentWidth , Color . White , Color . Transparent ) ;
316+ totalLines += wrappedLines . Count ;
317+ }
318+ else
319+ {
320+ // Count explicit newlines
321+ var subLines = line . Split ( '\n ' ) ;
322+ totalLines += subLines . Length ;
324323 }
325324 }
326325
0 commit comments