@@ -1223,7 +1223,11 @@ VerticalOrientationType.Rotate or
1223
1223
// If the line is too long, insert a forced line break.
1224
1224
if ( textLine . ScaledLineAdvance > wrappingLength )
1225
1225
{
1226
- remaining . InsertAt ( 0 , textLine . SplitAt ( wrappingLength ) ) ;
1226
+ TextLine overflow = textLine . SplitAt ( wrappingLength ) ;
1227
+ if ( overflow != textLine )
1228
+ {
1229
+ remaining . InsertAt ( 0 , overflow ) ;
1230
+ }
1227
1231
}
1228
1232
}
1229
1233
@@ -1249,6 +1253,21 @@ VerticalOrientationType.Rotate or
1249
1253
// Add the final line.
1250
1254
if ( textLine . Count > 0 )
1251
1255
{
1256
+ if ( shouldWrap && ( breakWord || breakAll ) )
1257
+ {
1258
+ while ( textLine . ScaledLineAdvance > wrappingLength )
1259
+ {
1260
+ TextLine overflow = textLine . SplitAt ( wrappingLength ) ;
1261
+ if ( overflow == textLine )
1262
+ {
1263
+ break ;
1264
+ }
1265
+
1266
+ textLines . Add ( textLine . Finalize ( options ) ) ;
1267
+ textLine = overflow ;
1268
+ }
1269
+ }
1270
+
1252
1271
textLines . Add ( textLine . Finalize ( options ) ) ;
1253
1272
}
1254
1273
@@ -1342,29 +1361,40 @@ public TextLine SplitAt(int index)
1342
1361
return this ;
1343
1362
}
1344
1363
1345
- TextLine result = new ( ) ;
1346
- result . data . AddRange ( this . data . GetRange ( index , this . data . Count - index ) ) ;
1364
+ int count = this . data . Count - index ;
1365
+ TextLine result = new ( count ) ;
1366
+ result . data . AddRange ( this . data . GetRange ( index , count ) ) ;
1347
1367
RecalculateLineMetrics ( result ) ;
1348
1368
1349
- this . data . RemoveRange ( index , this . data . Count - index ) ;
1369
+ this . data . RemoveRange ( index , count ) ;
1350
1370
RecalculateLineMetrics ( this ) ;
1351
1371
return result ;
1352
1372
}
1353
1373
1354
1374
public TextLine SplitAt ( float length )
1355
1375
{
1356
- TextLine result = new ( ) ;
1357
- float advance = 0 ;
1358
- for ( int i = 0 ; i < this . data . Count ; i ++ )
1376
+ float advance = this . data [ 0 ] . ScaledAdvance ;
1377
+
1378
+ // Ensure at least one glyph is in the line.
1379
+ // trailing whitespace should be ignored as it is trimmed
1380
+ // on finalization.
1381
+ for ( int i = 1 ; i < this . data . Count ; i ++ )
1359
1382
{
1360
1383
GlyphLayoutData glyph = this . data [ i ] ;
1361
1384
advance += glyph . ScaledAdvance ;
1385
+ if ( CodePoint . IsWhiteSpace ( glyph . CodePoint ) )
1386
+ {
1387
+ continue ;
1388
+ }
1389
+
1362
1390
if ( advance >= length )
1363
1391
{
1364
- result . data . AddRange ( this . data . GetRange ( i , this . data . Count - i ) ) ;
1392
+ int count = this . data . Count - i ;
1393
+ TextLine result = new ( count ) ;
1394
+ result . data . AddRange ( this . data . GetRange ( i , count ) ) ;
1365
1395
RecalculateLineMetrics ( result ) ;
1366
1396
1367
- this . data . RemoveRange ( i , this . data . Count - i ) ;
1397
+ this . data . RemoveRange ( i , count ) ;
1368
1398
RecalculateLineMetrics ( this ) ;
1369
1399
return result ;
1370
1400
}
0 commit comments