Convert Util to a class with static methods

Also replaces `var` with `const` in all the relevant code.
This commit is contained in:
Jonas Jenwald 2019-08-11 13:56:15 +02:00
parent 7ee370a394
commit f6c4a1f080

View File

@ -627,22 +627,20 @@ function isEvalSupported() {
} }
} }
var Util = (function UtilClosure() { const rgbBuf = ['rgb(', 0, ',', 0, ',', 0, ')'];
function Util() {}
var rgbBuf = ['rgb(', 0, ',', 0, ',', 0, ')']; class Util {
// makeCssRgb() can be called thousands of times. Using ´rgbBuf` avoids
// makeCssRgb() can be called thousands of times. Using |rgbBuf| avoids
// creating many intermediate strings. // creating many intermediate strings.
Util.makeCssRgb = function Util_makeCssRgb(r, g, b) { static makeCssRgb(r, g, b) {
rgbBuf[1] = r; rgbBuf[1] = r;
rgbBuf[3] = g; rgbBuf[3] = g;
rgbBuf[5] = b; rgbBuf[5] = b;
return rgbBuf.join(''); return rgbBuf.join('');
}; }
// Concatenates two transformation matrices together and returns the result. // Concatenates two transformation matrices together and returns the result.
Util.transform = function Util_transform(m1, m2) { static transform(m1, m2) {
return [ return [
m1[0] * m2[0] + m1[2] * m2[1], m1[0] * m2[0] + m1[2] * m2[1],
m1[1] * m2[0] + m1[3] * m2[1], m1[1] * m2[0] + m1[3] * m2[1],
@ -651,44 +649,42 @@ var Util = (function UtilClosure() {
m1[0] * m2[4] + m1[2] * m2[5] + m1[4], m1[0] * m2[4] + m1[2] * m2[5] + m1[4],
m1[1] * m2[4] + m1[3] * m2[5] + m1[5] m1[1] * m2[4] + m1[3] * m2[5] + m1[5]
]; ];
}; }
// For 2d affine transforms // For 2d affine transforms
Util.applyTransform = function Util_applyTransform(p, m) { static applyTransform(p, m) {
var xt = p[0] * m[0] + p[1] * m[2] + m[4]; const xt = p[0] * m[0] + p[1] * m[2] + m[4];
var yt = p[0] * m[1] + p[1] * m[3] + m[5]; const yt = p[0] * m[1] + p[1] * m[3] + m[5];
return [xt, yt]; return [xt, yt];
}; }
Util.applyInverseTransform = function Util_applyInverseTransform(p, m) { static applyInverseTransform(p, m) {
var d = m[0] * m[3] - m[1] * m[2]; const d = m[0] * m[3] - m[1] * m[2];
var xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d; const xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
var yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d; const yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
return [xt, yt]; return [xt, yt];
}; }
// Applies the transform to the rectangle and finds the minimum axially // Applies the transform to the rectangle and finds the minimum axially
// aligned bounding box. // aligned bounding box.
Util.getAxialAlignedBoundingBox = static getAxialAlignedBoundingBox(r, m) {
function Util_getAxialAlignedBoundingBox(r, m) { const p1 = Util.applyTransform(r, m);
const p2 = Util.applyTransform(r.slice(2, 4), m);
var p1 = Util.applyTransform(r, m); const p3 = Util.applyTransform([r[0], r[3]], m);
var p2 = Util.applyTransform(r.slice(2, 4), m); const p4 = Util.applyTransform([r[2], r[1]], m);
var p3 = Util.applyTransform([r[0], r[3]], m);
var p4 = Util.applyTransform([r[2], r[1]], m);
return [ return [
Math.min(p1[0], p2[0], p3[0], p4[0]), Math.min(p1[0], p2[0], p3[0], p4[0]),
Math.min(p1[1], p2[1], p3[1], p4[1]), Math.min(p1[1], p2[1], p3[1], p4[1]),
Math.max(p1[0], p2[0], p3[0], p4[0]), Math.max(p1[0], p2[0], p3[0], p4[0]),
Math.max(p1[1], p2[1], p3[1], p4[1]) Math.max(p1[1], p2[1], p3[1], p4[1])
]; ];
}; }
Util.inverseTransform = function Util_inverseTransform(m) { static inverseTransform(m) {
var d = m[0] * m[3] - m[1] * m[2]; const d = m[0] * m[3] - m[1] * m[2];
return [m[3] / d, -m[1] / d, -m[2] / d, m[0] / d, return [m[3] / d, -m[1] / d, -m[2] / d, m[0] / d,
(m[2] * m[5] - m[4] * m[3]) / d, (m[4] * m[1] - m[5] * m[0]) / d]; (m[2] * m[5] - m[4] * m[3]) / d, (m[4] * m[1] - m[5] * m[0]) / d];
}; }
// Apply a generic 3d matrix M on a 3-vector v: // Apply a generic 3d matrix M on a 3-vector v:
// | a b c | | X | // | a b c | | X |
@ -696,44 +692,42 @@ var Util = (function UtilClosure() {
// | g h i | | Z | // | g h i | | Z |
// M is assumed to be serialized as [a,b,c,d,e,f,g,h,i], // M is assumed to be serialized as [a,b,c,d,e,f,g,h,i],
// with v as [X,Y,Z] // with v as [X,Y,Z]
Util.apply3dTransform = function Util_apply3dTransform(m, v) { static apply3dTransform(m, v) {
return [ return [
m[0] * v[0] + m[1] * v[1] + m[2] * v[2], m[0] * v[0] + m[1] * v[1] + m[2] * v[2],
m[3] * v[0] + m[4] * v[1] + m[5] * v[2], m[3] * v[0] + m[4] * v[1] + m[5] * v[2],
m[6] * v[0] + m[7] * v[1] + m[8] * v[2] m[6] * v[0] + m[7] * v[1] + m[8] * v[2]
]; ];
}; }
// This calculation uses Singular Value Decomposition. // This calculation uses Singular Value Decomposition.
// The SVD can be represented with formula A = USV. We are interested in the // The SVD can be represented with formula A = USV. We are interested in the
// matrix S here because it represents the scale values. // matrix S here because it represents the scale values.
Util.singularValueDecompose2dScale = static singularValueDecompose2dScale(m) {
function Util_singularValueDecompose2dScale(m) { const transpose = [m[0], m[2], m[1], m[3]];
var transpose = [m[0], m[2], m[1], m[3]];
// Multiply matrix m with its transpose. // Multiply matrix m with its transpose.
var a = m[0] * transpose[0] + m[1] * transpose[2]; const a = m[0] * transpose[0] + m[1] * transpose[2];
var b = m[0] * transpose[1] + m[1] * transpose[3]; const b = m[0] * transpose[1] + m[1] * transpose[3];
var c = m[2] * transpose[0] + m[3] * transpose[2]; const c = m[2] * transpose[0] + m[3] * transpose[2];
var d = m[2] * transpose[1] + m[3] * transpose[3]; const d = m[2] * transpose[1] + m[3] * transpose[3];
// Solve the second degree polynomial to get roots. // Solve the second degree polynomial to get roots.
var first = (a + d) / 2; const first = (a + d) / 2;
var second = Math.sqrt((a + d) * (a + d) - 4 * (a * d - c * b)) / 2; const second = Math.sqrt((a + d) * (a + d) - 4 * (a * d - c * b)) / 2;
var sx = first + second || 1; const sx = first + second || 1;
var sy = first - second || 1; const sy = first - second || 1;
// Scale values are the square roots of the eigenvalues. // Scale values are the square roots of the eigenvalues.
return [Math.sqrt(sx), Math.sqrt(sy)]; return [Math.sqrt(sx), Math.sqrt(sy)];
}; }
// Normalize rectangle rect=[x1, y1, x2, y2] so that (x1,y1) < (x2,y2) // Normalize rectangle rect=[x1, y1, x2, y2] so that (x1,y1) < (x2,y2)
// For coordinate systems whose origin lies in the bottom-left, this // For coordinate systems whose origin lies in the bottom-left, this
// means normalization to (BL,TR) ordering. For systems with origin in the // means normalization to (BL,TR) ordering. For systems with origin in the
// top-left, this means (TL,BR) ordering. // top-left, this means (TL,BR) ordering.
Util.normalizeRect = function Util_normalizeRect(rect) { static normalizeRect(rect) {
var r = rect.slice(0); // clone rect const r = rect.slice(0); // clone rect
if (rect[0] > rect[2]) { if (rect[0] > rect[2]) {
r[0] = rect[2]; r[0] = rect[2];
r[2] = rect[0]; r[2] = rect[0];
@ -743,20 +737,20 @@ var Util = (function UtilClosure() {
r[3] = rect[1]; r[3] = rect[1];
} }
return r; return r;
}; }
// Returns a rectangle [x1, y1, x2, y2] corresponding to the // Returns a rectangle [x1, y1, x2, y2] corresponding to the
// intersection of rect1 and rect2. If no intersection, returns 'false' // intersection of rect1 and rect2. If no intersection, returns 'false'
// The rectangle coordinates of rect1, rect2 should be [x1, y1, x2, y2] // The rectangle coordinates of rect1, rect2 should be [x1, y1, x2, y2]
Util.intersect = function Util_intersect(rect1, rect2) { static intersect(rect1, rect2) {
function compare(a, b) { function compare(a, b) {
return a - b; return a - b;
} }
// Order points along the axes // Order points along the axes
var orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare), const orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare);
orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare), const orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare);
result = []; const result = [];
rect1 = Util.normalizeRect(rect1); rect1 = Util.normalizeRect(rect1);
rect2 = Util.normalizeRect(rect2); rect2 = Util.normalizeRect(rect2);
@ -782,10 +776,8 @@ var Util = (function UtilClosure() {
} }
return result; return result;
}; }
}
return Util;
})();
const PDFStringTranslateTable = [ const PDFStringTranslateTable = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,