(font, text, width)
| 425 | } |
| 426 | |
| 427 | wrapText(font, text, width) { |
| 428 | if (!this.measureCanvas) { |
| 429 | const [ el, ctx ] = this.makeCanvas(); |
| 430 | this.measureCanvas = el; |
| 431 | this.measureCtx = ctx; |
| 432 | } |
| 433 | |
| 434 | // todo: options lol |
| 435 | this.measureCtx.font = font; |
| 436 | |
| 437 | let lines = [], line = 0; |
| 438 | |
| 439 | let wrapNext = false; |
| 440 | for (const x of text.split(' ')) { |
| 441 | // if (!x) continue; |
| 442 | |
| 443 | const oldLine = lines[line]; |
| 444 | lines[line] = (lines[line] !== undefined ? (lines[line] + ' ') : '') + (x ?? ' '); |
| 445 | |
| 446 | const measure = this.measureCtx.measureText(lines[line]); |
| 447 | if (wrapNext || measure.width > width) { |
| 448 | if (wrapNext) wrapNext = false; |
| 449 | |
| 450 | lines[line] = oldLine; |
| 451 | line++; |
| 452 | lines.push(x); |
| 453 | } |
| 454 | |
| 455 | if (x.endsWith('\n')) wrapNext = true; |
| 456 | } |
| 457 | |
| 458 | return lines; |
| 459 | } |
| 460 | |
| 461 | measureWrapText(font, text, width) { |
| 462 |
no test coverage detected