diff --git a/src/lib/svg-generator.js b/src/lib/svg-generator.js index 383254f..93545bb 100644 --- a/src/lib/svg-generator.js +++ b/src/lib/svg-generator.js @@ -476,6 +476,8 @@ function generatePostcardSVG(text, format, options = {}) { const trennXmm = (textLowerRightMm + addrLeftMm) / 2; // A6: 84 const paths = []; + let bodyLineCount = 0; + let bodyLineLimit = 1; let overflow = false; // Variabler (L-förmiger) Textfluss: oben schmal (bis Frankierzone), unten breit. @@ -500,12 +502,13 @@ function generatePostcardSVG(text, format, options = {}) { return out; } - // Text links (L-förmig). + // Text links (L-förmig). Nur DER Text kann „zu lang" sein (Nutzer kürzt ihn). + const baseline0 = mmToPx(14) + fontSizePx; + bodyLineLimit = Math.max(1, Math.floor((mmToPx(maxYmm) - baseline0) / lineHeightPx) + 1); if (body) { - const baseline0 = mmToPx(14) + fontSizePx; const bodyLines = flowVariabel(body, fontSizePx, baseline0, lineHeightPx); - const letzteBaseline = baseline0 + Math.max(0, bodyLines.length - 1) * lineHeightPx; - if (letzteBaseline > mmToPx(maxYmm)) overflow = true; + bodyLineCount = bodyLines.length; + if (bodyLineCount > bodyLineLimit) overflow = true; paths.push(...layoutTextToPaths({ lines: bodyLines, fontFileName: fontFile, fontSizePx, startX: mmToPx(textLeftMm), startY: baseline0, lineHeightPx, realisticHandwriting: realistic, @@ -523,14 +526,26 @@ function generatePostcardSVG(text, format, options = {}) { })); } - // Empfänger im sicheren Adressfeld (rechts unten). + // Empfänger im sicheren Adressfeld (rechts unten). Passt der Block nicht in die + // 46 mm hohe Zone, wird die Schrift automatisch verkleinert – so bleibt die + // Codierzone (unten) IMMER frei und es gibt keinen „Adresse zu lang"-Fehler. if (recipient) { - const recLines = wrapTextToLinesByWidth({ text: recipient, maxWidthPx: mmToPx(addrRightMm - addrLeftMm), fontFileName: fontFile, fontSizePx }); - const recBaseline0 = mmToPx(addrTopMm) + fontSizePx; - if (recBaseline0 + Math.max(0, recLines.length - 1) * lineHeightPx > mmToPx(maxYmm)) overflow = true; + const feldOben = mmToPx(addrTopMm); + const feldUnten = mmToPx(maxYmm); + const feldBreite = mmToPx(addrRightMm - addrLeftMm); + let recSize = fontSizePx; + let recLH = lineHeightPx; + let recLines = []; + for (const scale of [1, 0.9, 0.8, 0.72, 0.65, 0.6]) { + recSize = fontSizePx * scale; + recLH = lineHeightPx * scale; + recLines = wrapTextToLinesByWidth({ text: recipient, maxWidthPx: feldBreite, fontFileName: fontFile, fontSizePx: recSize }); + const blockUnten = feldOben + recSize + Math.max(0, recLines.length - 1) * recLH + recSize * 0.3; + if (blockUnten <= feldUnten) break; + } paths.push(...layoutTextToPaths({ - lines: recLines, fontFileName: fontFile, fontSizePx, - startX: mmToPx(addrLeftMm), startY: recBaseline0, lineHeightPx, realisticHandwriting: realistic, + lines: recLines, fontFileName: fontFile, fontSizePx: recSize, + startX: mmToPx(addrLeftMm), startY: feldOben + recSize, lineHeightPx: recLH, realisticHandwriting: realistic, })); } @@ -558,7 +573,7 @@ function generatePostcardSVG(text, format, options = {}) { ${pathElements} `; - return { svg: svg.trim(), overflow, lineCount: 0, lineLimit: 0 }; + return { svg: svg.trim(), overflow, lineCount: bodyLineCount, lineLimit: bodyLineLimit }; } /**