diff --git a/lib/data.js b/lib/data.js index 7ca22e4..e3ae798 100644 --- a/lib/data.js +++ b/lib/data.js @@ -1,8 +1,5 @@ class Data { - constructor(data) { - if (data == null) { - data = []; - } + constructor(data = []) { this.data = data; this.pos = 0; this.length = this.data.length; @@ -88,11 +85,7 @@ class Data { readString(length) { const ret = []; - for ( - let i = 0, end = length, asc = 0 <= end; - asc ? i < end : i > end; - asc ? i++ : i-- - ) { + for (let i = 0; i < length; i++) { ret[i] = String.fromCharCode(this.readByte()); } @@ -186,11 +179,7 @@ class Data { read(bytes) { const buf = []; - for ( - let i = 0, end = bytes, asc = 0 <= end; - asc ? i < end : i > end; - asc ? i++ : i-- - ) { + for (let i = 0; i < bytes; i++) { buf.push(this.readByte()); } diff --git a/lib/font/afm.js b/lib/font/afm.js index cafeb02..d54aaab 100644 --- a/lib/font/afm.js +++ b/lib/font/afm.js @@ -181,11 +181,7 @@ class AFMFont { encodeText(text) { const res = []; - for ( - let i = 0, end = text.length, asc = 0 <= end; - asc ? i < end : i > end; - asc ? i++ : i-- - ) { + for (let i = 0, len = text.length; i < len; i++) { let char = text.charCodeAt(i); char = WIN_ANSI_MAP[char] || char; res.push(char.toString(16)); @@ -197,11 +193,7 @@ class AFMFont { glyphsForString(string) { const glyphs = []; - for ( - let i = 0, end = string.length, asc = 0 <= end; - asc ? i < end : i > end; - asc ? i++ : i-- - ) { + for (let i = 0, len = string.length; i < len; i++) { const charCode = string.charCodeAt(i); glyphs.push(this.characterToGlyph(charCode)); } diff --git a/lib/font/embedded.js b/lib/font/embedded.js index a185d2d..64f4548 100644 --- a/lib/font/embedded.js +++ b/lib/font/embedded.js @@ -60,15 +60,12 @@ class EmbeddedFont extends PDFFont { layout(text, features, onlyWidth) { // Skip the cache if any user defined features are applied - if (onlyWidth == null) { - onlyWidth = false; - } if (features) { return this.layoutRun(text, features); } - const glyphs = onlyWidth ? null : []; - const positions = onlyWidth ? null : []; + let glyphs = onlyWidth ? null : []; + let positions = onlyWidth ? null : []; let advanceWidth = 0; // Split the string by words to increase cache efficiency. @@ -83,8 +80,8 @@ class EmbeddedFont extends PDFFont { ) { const run = this.layoutCached(text.slice(last, ++index)); if (!onlyWidth) { - glyphs.push(...(run.glyphs || [])); - positions.push(...(run.positions || [])); + glyphs = glyphs.concat(run.glyphs); + positions = positions.concat(run.positions); } advanceWidth += run.advanceWidth; diff --git a/lib/gradient.js b/lib/gradient.js index c0e458d..f499f36 100644 --- a/lib/gradient.js +++ b/lib/gradient.js @@ -45,8 +45,7 @@ class PDFGradient { } embed(m) { - let asc, i; - let end, fn; + let fn; if (this.stops.length === 0) { return; } @@ -63,13 +62,9 @@ class PDFGradient { const encode = []; const stops = []; - for ( - i = 0, end = this.stops.length - 1, asc = 0 <= end; - asc ? i < end : i > end; - asc ? i++ : i-- - ) { + for (let i = 0, stopsLength = this.stops.length - 1; i < stopsLength; i++) { encode.push(0, 1); - if (i + 2 !== this.stops.length) { + if (i + 2 !== stopsLength) { bounds.push(this.stops[i + 1][0]); } diff --git a/lib/mixins/text.js b/lib/mixins/text.js index b1a1c58..1a00db2 100644 --- a/lib/mixins/text.js +++ b/lib/mixins/text.js @@ -81,16 +81,13 @@ export default { }, heightOfString(text, options) { - if (options == null) { - options = {}; - } const { x, y } = this; options = this._initOptions(options); options.height = Infinity; // don't break pages const lineGap = options.lineGap || this._lineGap || 0; - this._text(text, this.x, this.y, options, (line, options) => { + this._text(text, this.x, this.y, options, () => { return (this.y += this.currentLineHeight(true) + lineGap); }); @@ -205,22 +202,15 @@ export default { } // clone options object - options = (function() { - const opts = {}; - for (let k in options) { - const v = options[k]; - opts[k] = v; - } - return opts; - })(); + const result = Object.assign({}, options); // extend options with previous values for continued text if (this._textOptions) { for (let key in this._textOptions) { const val = this._textOptions[key]; if (key !== 'continued') { - if (options[key] == null) { - options[key] = val; + if (result[key] == null) { + result[key] = val; } } } @@ -235,20 +225,20 @@ export default { } // wrap to margins if no x or y position passed - if (options.lineBreak !== false) { - if (options.width == null) { - options.width = this.page.width - this.x - this.page.margins.right; + if (result.lineBreak !== false) { + if (result.width == null) { + result.width = this.page.width - this.x - this.page.margins.right; } } - if (!options.columns) { - options.columns = 0; + if (!result.columns) { + result.columns = 0; } - if (options.columnGap == null) { - options.columnGap = 18; + if (result.columnGap == null) { + result.columnGap = 18; } // 1/4 inch - return options; + return result; }, _line(text, options, wrapper) { @@ -431,8 +421,8 @@ export default { word, options.features ); - encoded.push(...(encodedWord || [])); - positions.push(...(positionsWord || [])); + encoded = encoded.concat(encodedWord); + positions = positions.concat(positionsWord); // add the word spacing to the end of the word // clone object because of cache diff --git a/lib/mixins/vector.js b/lib/mixins/vector.js index 4c6e853..1c657e5 100644 --- a/lib/mixins/vector.js +++ b/lib/mixins/vector.js @@ -195,11 +195,7 @@ export default { // calculate and render segments this.moveTo(ax, ay); - for ( - let segIdx = 0, end = numSegs, asc = 0 <= end; - asc ? segIdx < end : segIdx > end; - asc ? segIdx++ : segIdx-- - ) { + for (let segIdx = 0; segIdx < numSegs; segIdx++) { // starting control point const cp1x = ax + deltaCx; const cp1y = ay + deltaCy; diff --git a/lib/outline.js b/lib/outline.js index 2d6e423..c0d0c8b 100644 --- a/lib/outline.js +++ b/lib/outline.js @@ -40,9 +40,7 @@ class PDFOutline { } endOutline() { - let end; if (this.children.length > 0) { - let asc, i; if (this.options.expanded) { this.outlineData.Count = this.children.length; } @@ -52,11 +50,7 @@ class PDFOutline { this.outlineData.First = first.dictionary; this.outlineData.Last = last.dictionary; - for ( - i = 0, end = this.children.length, asc = 0 <= end; - asc ? i < end : i > end; - asc ? i++ : i-- - ) { + for (let i = 0, len = this.children.length; i < len; i++) { const child = this.children[i]; if (i > 0) { child.outlineData.Prev = this.children[i - 1].dictionary; diff --git a/lib/path.js b/lib/path.js index c9e6932..ddde05e 100644 --- a/lib/path.js +++ b/lib/path.js @@ -370,11 +370,7 @@ const arcToSegments = function(x, y, rx, ry, large, sweep, rotateX, ox, oy) { const segments = Math.ceil(Math.abs(th_arc / (Math.PI * 0.5 + 0.001))); const result = []; - for ( - let i = 0, end = segments, asc = 0 <= end; - asc ? i < end : i > end; - asc ? i++ : i-- - ) { + for (let i = 0; i < segments; i++) { const th2 = th0 + (i * th_arc) / segments; const th3 = th0 + ((i + 1) * th_arc) / segments; result[i] = [xc, yc, th2, th3, rx, ry, sin_th, cos_th];