Use Math.hypot, instead of Math.sqrt with manual squaring (#12973)

When the PDF.js project started `Math.hypot` didn't exist yet, and until recently we still supported browsers (IE 11) without a native `Math.hypot` implementation; please see this compatibility information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot#browser_compatibility

Furthermore, somewhat recently there were performance improvements of `Math.hypot` in Firefox; see https://bugzilla.mozilla.org/show_bug.cgi?id=1648820

Finally, this patch also replaces a couple of multiplications with the exponentiation operator.
This commit is contained in:
Jonas Jenwald 2021-02-10 12:28:49 +01:00 committed by GitHub
parent 3a2c259b57
commit 31098c404d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 23 additions and 27 deletions

View file

@ -1576,7 +1576,7 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
},
setTextMatrix: function CanvasGraphics_setTextMatrix(a, b, c, d, e, f) {
this.current.textMatrix = [a, b, c, d, e, f];
this.current.textMatrixScale = Math.sqrt(a * a + b * b);
this.current.textMatrixScale = Math.hypot(a, b);
this.current.x = this.current.lineX = 0;
this.current.y = this.current.lineY = 0;
@ -2455,12 +2455,14 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
ctx.scale(1 / width, -1 / height);
const currentTransform = ctx.mozCurrentTransformInverse;
const a = currentTransform[0],
b = currentTransform[1];
let widthScale = Math.max(Math.sqrt(a * a + b * b), 1);
const c = currentTransform[2],
d = currentTransform[3];
let heightScale = Math.max(Math.sqrt(c * c + d * d), 1);
let widthScale = Math.max(
Math.hypot(currentTransform[0], currentTransform[1]),
1
);
let heightScale = Math.max(
Math.hypot(currentTransform[2], currentTransform[3]),
1
);
let imgToPaint, tmpCanvas, tmpCtx;
// typeof check is needed due to node.js support, see issue #8489