Comparing version 0.1.59 to 0.1.60
{ | ||
"name": "pdfmake", | ||
"version": "0.1.59", | ||
"version": "0.1.60", | ||
"description": "Client/server side PDF printing in pure JavaScript", | ||
@@ -17,8 +17,8 @@ "main": "src/printer.js", | ||
"devDependencies": { | ||
"@babel/core": "^7.5.5", | ||
"@babel/plugin-transform-modules-commonjs": "^7.5.0", | ||
"@babel/preset-env": "^7.5.5", | ||
"@babel/core": "^7.6.0", | ||
"@babel/plugin-transform-modules-commonjs": "^7.6.0", | ||
"@babel/preset-env": "^7.6.0", | ||
"babel-loader": "^8.0.6", | ||
"brfs": "^2.0.2", | ||
"eslint-plugin-jsdoc": "^15.7.2", | ||
"eslint-plugin-jsdoc": "^15.9.2", | ||
"expose-loader": "^0.7.5", | ||
@@ -34,7 +34,7 @@ "fancy-log": "^1.3.3", | ||
"rewire": "^4.0.1", | ||
"sinon": "^7.3.2", | ||
"sinon": "^7.4.2", | ||
"string-replace-webpack-plugin": "^0.1.3", | ||
"transform-loader": "^0.2.4", | ||
"uglifyjs-webpack-plugin": "^2.1.3", | ||
"webpack": "^4.38.0" | ||
"uglifyjs-webpack-plugin": "^2.2.0", | ||
"webpack": "^4.40.2" | ||
}, | ||
@@ -41,0 +41,0 @@ "scripts": { |
@@ -13,2 +13,4 @@ 'use strict'; | ||
var isArray = require('./helpers').isArray; | ||
var isUndefined = require('./helpers').isUndefined; | ||
var isNull = require('./helpers').isNull; | ||
var pack = require('./helpers').pack; | ||
@@ -258,2 +260,3 @@ var offsetVector = require('./helpers').offsetVector; | ||
watermark.font = watermark.font || defaultStyle.font || 'Roboto'; | ||
watermark.fontSize = watermark.fontSize || 'auto'; | ||
watermark.color = watermark.color || 'black'; | ||
@@ -263,11 +266,23 @@ watermark.opacity = watermark.opacity || 0.6; | ||
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; | ||
} | ||
if (watermark.fontSize === 'auto') { | ||
watermark.fontSize = getWatermarkFontSize(this.pageSize, watermark, fontProvider); | ||
} | ||
var watermarkObject = { | ||
text: watermark.text, | ||
font: fontProvider.provideFont(watermark.font, watermark.bold, watermark.italics), | ||
size: getSize(this.pageSize, watermark, fontProvider), | ||
fontSize: watermark.fontSize, | ||
color: watermark.color, | ||
opacity: watermark.opacity | ||
opacity: watermark.opacity, | ||
angle: watermark.angle | ||
}; | ||
watermarkObject._size = getWatermarkSize(watermark, fontProvider); | ||
var pages = this.writer.context().pages; | ||
@@ -278,10 +293,21 @@ for (var i = 0, l = pages.length; i < l; i++) { | ||
function getSize(pageSize, watermark, fontProvider) { | ||
var width = pageSize.width; | ||
var height = pageSize.height; | ||
var targetWidth = Math.sqrt(width * width + height * height) * 0.8; /* page diagonal * sample factor */ | ||
function getWatermarkSize(watermark, fontProvider) { | ||
var textTools = new TextTools(fontProvider); | ||
var styleContextStack = new StyleContextStack(null, { font: watermark.font, bold: watermark.bold, italics: watermark.italics }); | ||
var size; | ||
styleContextStack.push({ | ||
fontSize: watermark.fontSize | ||
}); | ||
var size = textTools.sizeOfString(watermark.text, styleContextStack); | ||
var rotatedSize = textTools.sizeOfRotatedText(watermark.text, watermark.angle, styleContextStack); | ||
return { size: size, rotatedSize: rotatedSize }; | ||
} | ||
function getWatermarkFontSize(pageSize, watermark, fontProvider) { | ||
var textTools = new TextTools(fontProvider); | ||
var styleContextStack = new StyleContextStack(null, { font: watermark.font, bold: watermark.bold, italics: watermark.italics }); | ||
var rotatedSize; | ||
/** | ||
@@ -299,9 +325,14 @@ * Binary search the best font size. | ||
}); | ||
size = textTools.sizeOfString(watermark.text, styleContextStack); | ||
if (size.width > targetWidth) { | ||
rotatedSize = textTools.sizeOfRotatedText(watermark.text, watermark.angle, styleContextStack); | ||
if (rotatedSize.width > pageSize.width) { | ||
b = c; | ||
c = (a + b) / 2; | ||
} else if (size.width < targetWidth) { | ||
a = c; | ||
c = (a + b) / 2; | ||
} else if (rotatedSize.width < pageSize.width) { | ||
if (rotatedSize.height > pageSize.height) { | ||
b = c; | ||
c = (a + b) / 2; | ||
} else { | ||
a = c; | ||
c = (a + b) / 2; | ||
} | ||
} | ||
@@ -313,3 +344,3 @@ styleContextStack.pop(); | ||
*/ | ||
return { size: size, fontSize: c }; | ||
return c; | ||
} | ||
@@ -316,0 +347,0 @@ }; |
@@ -495,10 +495,9 @@ /*eslint no-unused-vars: ["error", {"args": "none"}]*/ | ||
var angle = Math.atan2(pdfKitDoc.page.height, pdfKitDoc.page.width) * -180 / Math.PI; | ||
pdfKitDoc.rotate(angle, { origin: [pdfKitDoc.page.width / 2, pdfKitDoc.page.height / 2] }); | ||
pdfKitDoc.rotate(watermark.angle, { origin: [pdfKitDoc.page.width / 2, pdfKitDoc.page.height / 2] }); | ||
var x = pdfKitDoc.page.width / 2 - watermark.size.size.width / 2; | ||
var y = pdfKitDoc.page.height / 2 - watermark.size.size.height / 4; | ||
var x = pdfKitDoc.page.width / 2 - watermark._size.size.width / 2; | ||
var y = pdfKitDoc.page.height / 2 - watermark._size.size.height / 2; | ||
pdfKitDoc._font = watermark.font; | ||
pdfKitDoc.fontSize(watermark.size.fontSize); | ||
pdfKitDoc.fontSize(watermark.fontSize); | ||
pdfKitDoc.text(watermark.text, x, y, { lineBreak: false }); | ||
@@ -505,0 +504,0 @@ |
@@ -178,3 +178,3 @@ 'use strict'; | ||
bottomBorder = cellAbove.border ? cellAbove.border[3] : this.layout.defaultBorder; | ||
if (cellAbove.borderColor) { | ||
if (bottomBorder && cellAbove.borderColor) { | ||
borderColor = cellAbove.borderColor[3]; | ||
@@ -188,3 +188,3 @@ } | ||
topBorder = currentCell.border ? currentCell.border[1] : this.layout.defaultBorder; | ||
if (borderColor == null && currentCell.borderColor) { | ||
if (topBorder && borderColor == null && currentCell.borderColor) { | ||
borderColor = currentCell.borderColor[1]; | ||
@@ -200,3 +200,3 @@ } | ||
rowBottomBorder = rowCellAbove && rowCellAbove.border ? rowCellAbove.border[3] : this.layout.defaultBorder; | ||
if (rowCellAbove && rowCellAbove.borderColor) { | ||
if (rowBottomBorder && rowCellAbove && rowCellAbove.borderColor) { | ||
borderColor = rowCellAbove.borderColor[3]; | ||
@@ -284,3 +284,5 @@ } | ||
if (cellBefore && cellBefore.borderColor) { | ||
borderColor = cellBefore.borderColor[2]; | ||
if (cellBefore.border ? cellBefore.border[2] : this.layout.defaultBorder) { | ||
borderColor = cellBefore.borderColor[2]; | ||
} | ||
} | ||
@@ -293,3 +295,5 @@ } | ||
if (currentCell && currentCell.borderColor) { | ||
borderColor = currentCell.borderColor[0]; | ||
if (currentCell.border ? currentCell.border[0] : this.layout.defaultBorder) { | ||
borderColor = currentCell.borderColor[0]; | ||
} | ||
} | ||
@@ -301,3 +305,5 @@ } | ||
if (rowCellBeforeAbove.borderColor) { | ||
borderColor = rowCellBeforeAbove.borderColor[2]; | ||
if (rowCellBeforeAbove.border ? rowCellBeforeAbove.border[2] : this.layout.defaultBorder) { | ||
borderColor = rowCellBeforeAbove.borderColor[2]; | ||
} | ||
} | ||
@@ -309,3 +315,5 @@ } | ||
if (rowCurrentCellAbove.borderColor) { | ||
borderColor = rowCurrentCellAbove.borderColor[2]; | ||
if (rowCurrentCellAbove.border ? rowCurrentCellAbove.border[2] : this.layout.defaultBorder) { | ||
borderColor = rowCurrentCellAbove.borderColor[2]; | ||
} | ||
} | ||
@@ -312,0 +320,0 @@ } |
@@ -100,2 +100,19 @@ 'use strict'; | ||
/** | ||
* Returns size of the specified rotated string (without breaking it) using the current style | ||
* | ||
* @param {string} text text to be measured | ||
* @param {number} angle | ||
* @param {object} styleContextStack current style stack | ||
* @returns {object} size of the specified string | ||
*/ | ||
TextTools.prototype.sizeOfRotatedText = function (text, angle, styleContextStack) { | ||
var angleRad = angle * Math.PI / -180; | ||
var size = this.sizeOfString(text, styleContextStack); | ||
return { | ||
width: Math.abs(size.height * Math.sin(angleRad)) + Math.abs(size.width * Math.cos(angleRad)), | ||
height: Math.abs(size.width * Math.sin(angleRad)) + Math.abs(size.height * Math.cos(angleRad)) | ||
}; | ||
} | ||
TextTools.prototype.widthOfString = function (text, font, fontSize, characterSpacing, fontFeatures) { | ||
@@ -102,0 +119,0 @@ return widthOfString(text, font, fontSize, characterSpacing, fontFeatures); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
11521685
72864