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 e04c087 commit 13dcebd
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 @@ -3,6 +3,7 @@
## Unreleased

- Fixed aspect ratio for image with exif orientation tag
- Fixed font size calculation for watermark if is page orientation is changed

## 0.2.15 - 2024-11-02

Expand Down
54 changes: 29 additions & 25 deletions src/layoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,36 +252,40 @@ LayoutBuilder.prototype.addWatermark = function (watermark, fontProvider, defaul
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 = !isUndefined(watermark.angle) && !isNull(watermark.angle) ? watermark.angle : null;

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

if (watermark.fontSize === 'auto') {
watermark.fontSize = getWatermarkFontSize(this.pageSize, watermark, fontProvider);
}
function getWatermarkObject(watermark, pageSize, fontProvider, 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 = !isUndefined(watermark.angle) && !isNull(watermark.angle) ? watermark.angle : null;

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

var watermarkObject = {
text: watermark.text,
font: fontProvider.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, fontProvider);
}

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

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

return watermarkObject;
}

function getWatermarkSize(watermark, fontProvider) {
Expand Down

0 comments on commit 13dcebd

Please sign in to comment.