Skip to content

Commit

Permalink
fixed font size calculation for watermark if is page orientation is c…
Browse files Browse the repository at this point in the history
…hanged
  • Loading branch information
liborm85 committed Nov 23, 2024
1 parent ef36c8a commit 64f1b97
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fixed speed in Node.js if is fetching URL for image or font redirected
- Fixed aspect ratio for image with exif orientation tag
- Fixed font size calculation for watermark if is page orientation is changed

## 0.3.0-beta.12 - 2024-11-03

Expand Down
54 changes: 29 additions & 25 deletions src/LayoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,36 +261,40 @@ class LayoutBuilder {
return;
}

watermark.font = watermark.font || defaultStyle.font || 'Roboto';
watermark.fontSize = watermark.fontSize || 'auto';
watermark.color = watermark.color || 'black';
watermark.opacity = isNumber(watermark.opacity) ? watermark.opacity : 0.6;
watermark.bold = watermark.bold || false;
watermark.italics = watermark.italics || false;
watermark.angle = isValue(watermark.angle) ? watermark.angle : null;

if (watermark.angle === null) {
watermark.angle = Math.atan2(this.pageSize.height, this.pageSize.width) * -180 / Math.PI;
let pages = this.writer.context().pages;
for (let i = 0, l = pages.length; i < l; i++) {
pages[i].watermark = getWatermarkObject({ ...watermark }, pages[i].pageSize, pdfDocument, defaultStyle);
}

if (watermark.fontSize === 'auto') {
watermark.fontSize = getWatermarkFontSize(this.pageSize, watermark, pdfDocument);
}
function getWatermarkObject(watermark, pageSize, pdfDocument, defaultStyle) {
watermark.font = watermark.font || defaultStyle.font || 'Roboto';
watermark.fontSize = watermark.fontSize || 'auto';
watermark.color = watermark.color || 'black';
watermark.opacity = isNumber(watermark.opacity) ? watermark.opacity : 0.6;
watermark.bold = watermark.bold || false;
watermark.italics = watermark.italics || false;
watermark.angle = isValue(watermark.angle) ? watermark.angle : null;

if (watermark.angle === null) {
watermark.angle = Math.atan2(pageSize.height, pageSize.width) * -180 / Math.PI;
}

let watermarkObject = {
text: watermark.text,
font: pdfDocument.provideFont(watermark.font, watermark.bold, watermark.italics),
fontSize: watermark.fontSize,
color: watermark.color,
opacity: watermark.opacity,
angle: watermark.angle
};
if (watermark.fontSize === 'auto') {
watermark.fontSize = getWatermarkFontSize(pageSize, watermark, pdfDocument);
}

watermarkObject._size = getWatermarkSize(watermark, pdfDocument);
let watermarkObject = {
text: watermark.text,
font: pdfDocument.provideFont(watermark.font, watermark.bold, watermark.italics),
fontSize: watermark.fontSize,
color: watermark.color,
opacity: watermark.opacity,
angle: watermark.angle
};

let pages = this.writer.context().pages;
for (let i = 0, l = pages.length; i < l; i++) {
pages[i].watermark = watermarkObject;
watermarkObject._size = getWatermarkSize(watermark, pdfDocument);

return watermarkObject;
}

function getWatermarkSize(watermark, pdfDocument) {
Expand Down

0 comments on commit 64f1b97

Please sign in to comment.