|
281 | 281 | // Split each line into words |
282 | 282 | const words = line.split(/(?:(?!\u00A0+)\s+)/); |
283 | 283 |
|
284 | | - // Handle word wrapping within each line |
285 | | - return words.reduce((result: { words: string[]; width?: number }[], item) => { |
286 | | - const currentLine = result[result.length - 1]; |
287 | | - const itemWidth = getStringWidth(item, style) || 0; |
288 | | -
|
289 | | - if ( |
290 | | - currentLine && |
291 | | - (width == null || scaleToFit || (currentLine.width || 0) + itemWidth + spaceWidth < width) |
292 | | - ) { |
293 | | - // Word can be added to an existing line |
294 | | - currentLine.words.push(item); |
295 | | - currentLine.width = currentLine.width || 0; |
296 | | - currentLine.width += itemWidth + spaceWidth; |
297 | | - } else { |
298 | | - // Add first word to line or word is too long to scaleToFit on existing line |
299 | | - const newLine = { words: [item], width: itemWidth }; |
300 | | - result.push(newLine); |
301 | | - } |
302 | | -
|
303 | | - return result; |
304 | | - }, []); |
| 284 | + if (width == null) { |
| 285 | + // No width specified, only use explicit line breaks (if used) |
| 286 | + return [{ words }]; |
| 287 | + } else { |
| 288 | + // Handle word wrapping within each line |
| 289 | + return words.reduce((result: { words: string[]; width?: number }[], item) => { |
| 290 | + const currentLine = result[result.length - 1]; |
| 291 | + const itemWidth = getStringWidth(item, style) || 0; |
| 292 | +
|
| 293 | + if ( |
| 294 | + currentLine && |
| 295 | + (width == null || |
| 296 | + scaleToFit || |
| 297 | + (currentLine.width || 0) + itemWidth + spaceWidth < width) |
| 298 | + ) { |
| 299 | + // Word can be added to an existing line |
| 300 | + currentLine.words.push(item); |
| 301 | + currentLine.width = currentLine.width || 0; |
| 302 | + currentLine.width += itemWidth + spaceWidth; |
| 303 | + } else { |
| 304 | + // Add first word to line or word is too long to scaleToFit on existing line |
| 305 | + const newLine = { words: [item], width: itemWidth }; |
| 306 | + result.push(newLine); |
| 307 | + } |
| 308 | +
|
| 309 | + return result; |
| 310 | + }, []); |
| 311 | + } |
305 | 312 | }); |
306 | 313 | }); |
307 | 314 |
|
|
0 commit comments