export const PROOF_WATERMARK_TEXT = 'MUSTER – nicht verkaufsfähig'; const WATERMARK_COLOR = '#808080'; const WATERMARK_OPACITY = 0.5; const WATERMARK_FONT = 'Helvetica-Bold'; // The text runs along the label's diagonal; this share of it is covered. const DIAGONAL_COVERAGE = 0.8; // Never taller than this share of the label's height. const MAX_HEIGHT_SHARE = 0.5; const MEASURE_SIZE_PT = 10; /** * Draws the proof stamp "MUSTER – nicht verkaufsfähig" over one label: grey, * half opaque, rotated along the label's diagonal and centred. Called once * per label, after its content. The graphics state is restored, so nothing * leaks into the next label. */ export function drawProofWatermark( doc: PDFKit.PDFDocument, originX: number, originY: number, widthPt: number, heightPt: number, ): void { const centerX = originX + widthPt / 2; const centerY = originY + heightPt / 2; const angleDeg = (Math.atan2(heightPt, widthPt) * 180) / Math.PI; doc.save(); doc.font(WATERMARK_FONT).fontSize(MEASURE_SIZE_PT); const widthAtMeasureSize = doc.widthOfString(PROOF_WATERMARK_TEXT); const targetWidth = Math.hypot(widthPt, heightPt) * DIAGONAL_COVERAGE; const fontSize = Math.min( (MEASURE_SIZE_PT * targetWidth) / widthAtMeasureSize, heightPt * MAX_HEIGHT_SHARE, ); const textWidth = (widthAtMeasureSize * fontSize) / MEASURE_SIZE_PT; doc.fillColor(WATERMARK_COLOR).fillOpacity(WATERMARK_OPACITY); doc.rotate(-angleDeg, { origin: [centerX, centerY] }); doc.fontSize(fontSize); doc.text( PROOF_WATERMARK_TEXT, centerX - textWidth / 2, centerY - fontSize / 2, { lineBreak: false }, ); doc.restore(); }