MCPcopy Create free account
hub / github.com/CanadaHonk/shadow / wrapText

Method wrapText

engine/renderer.js:427–459  ·  view source on GitHub ↗
(font, text, width)

Source from the content-addressed store, hash-verified

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

Callers 1

fillWrapTextMethod · 0.95

Calls 2

makeCanvasMethod · 0.95
measureTextMethod · 0.80

Tested by

no test coverage detected