fix: cap watermark dimensions to prevent RangeError on large files

buildWatermark() calls Array.join() on a lines array whose size is
derived from tile dimensions divided by wmFontSize. For files with very
many lines the codeFontSize (and thus wmFontSize) approaches zero,
making charsPerLine and lineCount astronomically large and blowing past
JS's string length limit.

Fix by:
1. Clamping wmFontSize to a minimum of 1.0 to handle pathologically
   large files.
2. Capping charsPerLine at 400 and lineCount at 150 — the watermark is
   purely decorative so this cap has no visible impact.
This commit is contained in:
麻薯 2026-04-07 10:56:02 +01:00
parent f45e842370
commit b58ba41458

View File

@ -302,11 +302,12 @@ export class RepoRenderer {
// Watermark — tiled path text, 45° rotated, slightly larger than code font
if (!tile.watermark) {
const codeFontSize = (d.h / d.lines) * 0.65;
const wmFontSize = codeFontSize * 2.5;
// Clamp wmFontSize to avoid degenerate tiny values on files with huge line counts
const wmFontSize = Math.max(codeFontSize * 2.5, 1.0);
const wmLabel = `${this.repoName}/${d.path}`;
// Estimate how many repetitions to fill the area
const charsPerLine = Math.ceil(Math.max(d.w, d.h) * 1.5 / (wmFontSize * 0.5));
const lineCount = Math.ceil(Math.max(d.w, d.h) * 1.5 / (wmFontSize * 1.5));
// Estimate how many repetitions to fill the area; cap to prevent RangeError on massive tiles
const charsPerLine = Math.min(Math.ceil(Math.max(d.w, d.h) * 1.5 / (wmFontSize * 0.5)), 400);
const lineCount = Math.min(Math.ceil(Math.max(d.w, d.h) * 1.5 / (wmFontSize * 1.5)), 150);
const wmContent = buildWatermark(wmLabel, charsPerLine, lineCount);
const wm = new Text();