Enable the no-var rule in the src/core/evaluator.js file

These changes were made automatically, using `gulp lint --fix`.
This commit is contained in:
Jonas Jenwald 2021-05-06 09:39:21 +02:00
parent afb8c4fd25
commit f93c3b9aa7

View File

@ -12,7 +12,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* eslint-disable no-var */
import { import {
AbortException, AbortException,
@ -230,7 +229,7 @@ class PartialEvaluator {
} }
clone(newOptions = DefaultPartialEvaluatorOptions) { clone(newOptions = DefaultPartialEvaluatorOptions) {
var newEvaluator = Object.create(this); const newEvaluator = Object.create(this);
newEvaluator.options = newOptions; newEvaluator.options = newOptions;
return newEvaluator; return newEvaluator;
} }
@ -248,12 +247,12 @@ class PartialEvaluator {
processed.put(resources.objId); processed.put(resources.objId);
} }
var nodes = [resources], const nodes = [resources],
xref = this.xref; xref = this.xref;
while (nodes.length) { while (nodes.length) {
var node = nodes.shift(); const node = nodes.shift();
// First check the current resources for blend modes. // First check the current resources for blend modes.
var graphicStates = node.get("ExtGState"); const graphicStates = node.get("ExtGState");
if (graphicStates instanceof Dict) { if (graphicStates instanceof Dict) {
for (let graphicState of graphicStates.getRawValues()) { for (let graphicState of graphicStates.getRawValues()) {
if (graphicState instanceof Ref) { if (graphicState instanceof Ref) {
@ -294,7 +293,7 @@ class PartialEvaluator {
} }
} }
// Descend into the XObjects to look for more resources and blend modes. // Descend into the XObjects to look for more resources and blend modes.
var xObjects = node.get("XObject"); const xObjects = node.get("XObject");
if (!(xObjects instanceof Dict)) { if (!(xObjects instanceof Dict)) {
continue; continue;
} }
@ -322,7 +321,7 @@ class PartialEvaluator {
if (xObject.dict.objId) { if (xObject.dict.objId) {
processed.put(xObject.dict.objId); processed.put(xObject.dict.objId);
} }
var xResources = xObject.dict.get("Resources"); const xResources = xObject.dict.get("Resources");
if (!(xResources instanceof Dict)) { if (!(xResources instanceof Dict)) {
continue; continue;
} }
@ -386,9 +385,9 @@ class PartialEvaluator {
initialState, initialState,
localColorSpaceCache localColorSpaceCache
) { ) {
var dict = xobj.dict; const dict = xobj.dict;
var matrix = dict.getArray("Matrix"); const matrix = dict.getArray("Matrix");
var bbox = dict.getArray("BBox"); let bbox = dict.getArray("BBox");
if (Array.isArray(bbox) && bbox.length === 4) { if (Array.isArray(bbox) && bbox.length === 4) {
bbox = Util.normalizeRect(bbox); bbox = Util.normalizeRect(bbox);
} else { } else {
@ -402,7 +401,7 @@ class PartialEvaluator {
); );
operatorList.addOp(OPS.beginMarkedContentProps, ["OC", optionalContent]); operatorList.addOp(OPS.beginMarkedContentProps, ["OC", optionalContent]);
} }
var group = dict.get("Group"); const group = dict.get("Group");
if (group) { if (group) {
var groupOptions = { var groupOptions = {
matrix, matrix,
@ -412,8 +411,8 @@ class PartialEvaluator {
knockout: false, knockout: false,
}; };
var groupSubtype = group.get("S"); const groupSubtype = group.get("S");
var colorSpace = null; let colorSpace = null;
if (isName(groupSubtype, "Transparency")) { if (isName(groupSubtype, "Transparency")) {
groupOptions.isolated = group.get("I") || false; groupOptions.isolated = group.get("I") || false;
groupOptions.knockout = group.get("K") || false; groupOptions.knockout = group.get("K") || false;
@ -492,23 +491,23 @@ class PartialEvaluator {
localImageCache, localImageCache,
localColorSpaceCache, localColorSpaceCache,
}) { }) {
var dict = image.dict; const dict = image.dict;
const imageRef = dict.objId; const imageRef = dict.objId;
var w = dict.get("Width", "W"); const w = dict.get("Width", "W");
var h = dict.get("Height", "H"); const h = dict.get("Height", "H");
if (!(w && isNum(w)) || !(h && isNum(h))) { if (!(w && isNum(w)) || !(h && isNum(h))) {
warn("Image dimensions are missing, or not numbers."); warn("Image dimensions are missing, or not numbers.");
return undefined; return undefined;
} }
var maxImageSize = this.options.maxImageSize; const maxImageSize = this.options.maxImageSize;
if (maxImageSize !== -1 && w * h > maxImageSize) { if (maxImageSize !== -1 && w * h > maxImageSize) {
warn("Image exceeded maximum allowed size and was removed."); warn("Image exceeded maximum allowed size and was removed.");
return undefined; return undefined;
} }
var imageMask = dict.get("ImageMask", "IM") || false; const imageMask = dict.get("ImageMask", "IM") || false;
var imgData, args; let imgData, args;
if (imageMask) { if (imageMask) {
// This depends on a tmpCanvas being filled with the // This depends on a tmpCanvas being filled with the
// current fillStyle, such that processing the pixel // current fillStyle, such that processing the pixel
@ -516,14 +515,14 @@ class PartialEvaluator {
// complete PDFImage, only read the information needed // complete PDFImage, only read the information needed
// for later. // for later.
var width = dict.get("Width", "W"); const width = dict.get("Width", "W");
var height = dict.get("Height", "H"); const height = dict.get("Height", "H");
var bitStrideLength = (width + 7) >> 3; const bitStrideLength = (width + 7) >> 3;
var imgArray = image.getBytes( const imgArray = image.getBytes(
bitStrideLength * height, bitStrideLength * height,
/* forceClamped = */ true /* forceClamped = */ true
); );
var decode = dict.getArray("Decode", "D"); const decode = dict.getArray("Decode", "D");
imgData = PDFImage.createMask({ imgData = PDFImage.createMask({
imgArray, imgArray,
@ -545,10 +544,10 @@ class PartialEvaluator {
return undefined; return undefined;
} }
var softMask = dict.get("SMask", "SM") || false; const softMask = dict.get("SMask", "SM") || false;
var mask = dict.get("Mask") || false; const mask = dict.get("Mask") || false;
var SMALL_IMAGE_DIMENSIONS = 200; const SMALL_IMAGE_DIMENSIONS = 200;
// Inlining small images into the queue as RGB data // Inlining small images into the queue as RGB data
if (isInline && !softMask && !mask && w + h < SMALL_IMAGE_DIMENSIONS) { if (isInline && !softMask && !mask && w + h < SMALL_IMAGE_DIMENSIONS) {
const imageObj = new PDFImage({ const imageObj = new PDFImage({
@ -642,20 +641,20 @@ class PartialEvaluator {
stateManager, stateManager,
localColorSpaceCache localColorSpaceCache
) { ) {
var smaskContent = smask.get("G"); const smaskContent = smask.get("G");
var smaskOptions = { const smaskOptions = {
subtype: smask.get("S").name, subtype: smask.get("S").name,
backdrop: smask.get("BC"), backdrop: smask.get("BC"),
}; };
// The SMask might have a alpha/luminosity value transfer function -- // The SMask might have a alpha/luminosity value transfer function --
// we will build a map of integer values in range 0..255 to be fast. // we will build a map of integer values in range 0..255 to be fast.
var transferObj = smask.get("TR"); const transferObj = smask.get("TR");
if (isPDFFunction(transferObj)) { if (isPDFFunction(transferObj)) {
const transferFn = this._pdfFunctionFactory.create(transferObj); const transferFn = this._pdfFunctionFactory.create(transferObj);
var transferMap = new Uint8Array(256); const transferMap = new Uint8Array(256);
var tmp = new Float32Array(1); const tmp = new Float32Array(1);
for (var i = 0; i < 256; i++) { for (let i = 0; i < 256; i++) {
tmp[0] = i / 255; tmp[0] = i / 255;
transferFn(tmp, 0, tmp, 0); transferFn(tmp, 0, tmp, 0);
transferMap[i] = (tmp[0] * 255) | 0; transferMap[i] = (tmp[0] * 255) | 0;
@ -888,10 +887,10 @@ class PartialEvaluator {
const gStateRef = gState.objId; const gStateRef = gState.objId;
let isSimpleGState = true; let isSimpleGState = true;
// This array holds the converted/processed state data. // This array holds the converted/processed state data.
var gStateObj = []; const gStateObj = [];
var gStateKeys = gState.getKeys(); const gStateKeys = gState.getKeys();
var promise = Promise.resolve(); let promise = Promise.resolve();
for (var i = 0, ii = gStateKeys.length; i < ii; i++) { for (let i = 0, ii = gStateKeys.length; i < ii; i++) {
const key = gStateKeys[i]; const key = gStateKeys[i];
const value = gState.get(key); const value = gState.get(key);
switch (key) { switch (key) {
@ -1005,7 +1004,7 @@ class PartialEvaluator {
}); });
}; };
var fontRef, let fontRef,
xref = this.xref; xref = this.xref;
if (font) { if (font) {
// Loading by ref. // Loading by ref.
@ -1015,7 +1014,7 @@ class PartialEvaluator {
fontRef = font; fontRef = font;
} else { } else {
// Loading by name. // Loading by name.
var fontRes = resources.get("Font"); const fontRes = resources.get("Font");
if (fontRes) { if (fontRes) {
fontRef = fontRes.getRaw(fontName); fontRef = fontRes.getRaw(fontName);
} }
@ -1059,7 +1058,7 @@ class PartialEvaluator {
return this.fontCache.get(font.cacheKey); return this.fontCache.get(font.cacheKey);
} }
var fontCapability = createPromiseCapability(); const fontCapability = createPromiseCapability();
let preEvaluatedFont; let preEvaluatedFont;
try { try {
@ -1071,7 +1070,7 @@ class PartialEvaluator {
} }
const { descriptor, hash } = preEvaluatedFont; const { descriptor, hash } = preEvaluatedFont;
var fontRefIsRef = isRef(fontRef), let fontRefIsRef = isRef(fontRef),
fontID; fontID;
if (fontRefIsRef) { if (fontRefIsRef) {
fontID = `f${fontRef.toString()}`; fontID = `f${fontRef.toString()}`;
@ -1081,10 +1080,10 @@ class PartialEvaluator {
if (!descriptor.fontAliases) { if (!descriptor.fontAliases) {
descriptor.fontAliases = Object.create(null); descriptor.fontAliases = Object.create(null);
} }
var fontAliases = descriptor.fontAliases; const fontAliases = descriptor.fontAliases;
if (fontAliases[hash]) { if (fontAliases[hash]) {
var aliasFontRef = fontAliases[hash].aliasRef; const aliasFontRef = fontAliases[hash].aliasRef;
if (fontRefIsRef && aliasFontRef && this.fontCache.has(aliasFontRef)) { if (fontRefIsRef && aliasFontRef && this.fontCache.has(aliasFontRef)) {
this.fontCache.putAlias(fontRef, aliasFontRef); this.fontCache.putAlias(fontRef, aliasFontRef);
return this.fontCache.get(fontRef); return this.fontCache.get(fontRef);
@ -1137,7 +1136,7 @@ class PartialEvaluator {
this.translateFont(preEvaluatedFont) this.translateFont(preEvaluatedFont)
.then(translatedFont => { .then(translatedFont => {
if (translatedFont.fontType !== undefined) { if (translatedFont.fontType !== undefined) {
var xrefFontStats = xref.stats.fontTypes; const xrefFontStats = xref.stats.fontTypes;
xrefFontStats[translatedFont.fontType] = true; xrefFontStats[translatedFont.fontType] = true;
} }
@ -1160,13 +1159,13 @@ class PartialEvaluator {
try { try {
// error, but it's still nice to have font type reported // error, but it's still nice to have font type reported
var fontFile3 = descriptor && descriptor.get("FontFile3"); const fontFile3 = descriptor && descriptor.get("FontFile3");
var subtype = fontFile3 && fontFile3.get("Subtype"); const subtype = fontFile3 && fontFile3.get("Subtype");
var fontType = getFontType( const fontType = getFontType(
preEvaluatedFont.type, preEvaluatedFont.type,
subtype && subtype.name subtype && subtype.name
); );
var xrefFontStats = xref.stats.fontTypes; const xrefFontStats = xref.stats.fontTypes;
xrefFontStats[fontType] = true; xrefFontStats[fontType] = true;
} catch (ex) {} } catch (ex) {}
@ -1185,7 +1184,7 @@ class PartialEvaluator {
} }
buildPath(operatorList, fn, args, parsingText = false) { buildPath(operatorList, fn, args, parsingText = false) {
var lastIndex = operatorList.length - 1; const lastIndex = operatorList.length - 1;
if (!args) { if (!args) {
args = []; args = [];
} }
@ -1211,7 +1210,7 @@ class PartialEvaluator {
operatorList.addOp(OPS.restore, null); operatorList.addOp(OPS.restore, null);
} }
} else { } else {
var opArgs = operatorList.argsArray[lastIndex]; const opArgs = operatorList.argsArray[lastIndex];
opArgs[0].push(fn); opArgs[0].push(fn);
Array.prototype.push.apply(opArgs[1], args); Array.prototype.push.apply(opArgs[1], args);
} }
@ -1279,8 +1278,8 @@ class PartialEvaluator {
let pattern = patterns.get(name); let pattern = patterns.get(name);
if (pattern) { if (pattern) {
var dict = isStream(pattern) ? pattern.dict : pattern; const dict = isStream(pattern) ? pattern.dict : pattern;
var typeNum = dict.get("PatternType"); const typeNum = dict.get("PatternType");
if (typeNum === PatternType.TILING) { if (typeNum === PatternType.TILING) {
const color = cs.base ? cs.base.getRgb(args, 0) : null; const color = cs.base ? cs.base.getRgb(args, 0) : null;
@ -1296,8 +1295,8 @@ class PartialEvaluator {
localTilingPatternCache localTilingPatternCache
); );
} else if (typeNum === PatternType.SHADING) { } else if (typeNum === PatternType.SHADING) {
var shading = dict.get("Shading"); const shading = dict.get("Shading");
var matrix = dict.getArray("Matrix"); const matrix = dict.getArray("Matrix");
pattern = Pattern.parseShading( pattern = Pattern.parseShading(
shading, shading,
matrix, matrix,
@ -1433,22 +1432,22 @@ class PartialEvaluator {
throw new Error('getOperatorList: missing "operatorList" parameter'); throw new Error('getOperatorList: missing "operatorList" parameter');
} }
var self = this; const self = this;
var xref = this.xref; const xref = this.xref;
let parsingText = false; let parsingText = false;
const localImageCache = new LocalImageCache(); const localImageCache = new LocalImageCache();
const localColorSpaceCache = new LocalColorSpaceCache(); const localColorSpaceCache = new LocalColorSpaceCache();
const localGStateCache = new LocalGStateCache(); const localGStateCache = new LocalGStateCache();
const localTilingPatternCache = new LocalTilingPatternCache(); const localTilingPatternCache = new LocalTilingPatternCache();
var xobjs = resources.get("XObject") || Dict.empty; const xobjs = resources.get("XObject") || Dict.empty;
var patterns = resources.get("Pattern") || Dict.empty; const patterns = resources.get("Pattern") || Dict.empty;
var stateManager = new StateManager(initialState); const stateManager = new StateManager(initialState);
var preprocessor = new EvaluatorPreprocessor(stream, xref, stateManager); const preprocessor = new EvaluatorPreprocessor(stream, xref, stateManager);
var timeSlotManager = new TimeSlotManager(); const timeSlotManager = new TimeSlotManager();
function closePendingRestoreOPS(argument) { function closePendingRestoreOPS(argument) {
for (var i = 0, ii = preprocessor.savedStatesDepth; i < ii; i++) { for (let i = 0, ii = preprocessor.savedStatesDepth; i < ii; i++) {
operatorList.addOp(OPS.restore, []); operatorList.addOp(OPS.restore, []);
} }
} }
@ -1465,7 +1464,7 @@ class PartialEvaluator {
}; };
task.ensureNotTerminated(); task.ensureNotTerminated();
timeSlotManager.reset(); timeSlotManager.reset();
var stop, let stop,
operation = {}, operation = {},
i, i,
ii, ii,
@ -1480,8 +1479,8 @@ class PartialEvaluator {
if (!preprocessor.read(operation)) { if (!preprocessor.read(operation)) {
break; break;
} }
var args = operation.args; let args = operation.args;
var fn = operation.fn; let fn = operation.fn;
switch (fn | 0) { switch (fn | 0) {
case OPS.paintXObject: case OPS.paintXObject:
@ -1657,7 +1656,7 @@ class PartialEvaluator {
var arrLength = arr.length; var arrLength = arr.length;
var state = stateManager.state; var state = stateManager.state;
for (i = 0; i < arrLength; ++i) { for (i = 0; i < arrLength; ++i) {
var arrItem = arr[i]; const arrItem = arr[i];
if (isString(arrItem)) { if (isString(arrItem)) {
Array.prototype.push.apply( Array.prototype.push.apply(
combinedGlyphs, combinedGlyphs,
@ -2040,11 +2039,11 @@ class PartialEvaluator {
const WhitespaceRegexp = /\s/g; const WhitespaceRegexp = /\s/g;
var textContent = { const textContent = {
items: [], items: [],
styles: Object.create(null), styles: Object.create(null),
}; };
var textContentItem = { const textContentItem = {
initialized: false, initialized: false,
str: [], str: [],
totalWidth: 0, totalWidth: 0,
@ -2081,18 +2080,18 @@ class PartialEvaluator {
const SPACE_IN_FLOW_MIN_FACTOR = 0.3; const SPACE_IN_FLOW_MIN_FACTOR = 0.3;
const SPACE_IN_FLOW_MAX_FACTOR = 1.3; const SPACE_IN_FLOW_MAX_FACTOR = 1.3;
var self = this; const self = this;
var xref = this.xref; const xref = this.xref;
const showSpacedTextBuffer = []; const showSpacedTextBuffer = [];
// The xobj is parsed iff it's needed, e.g. if there is a `DO` cmd. // The xobj is parsed iff it's needed, e.g. if there is a `DO` cmd.
var xobjs = null; let xobjs = null;
const emptyXObjectCache = new LocalImageCache(); const emptyXObjectCache = new LocalImageCache();
const emptyGStateCache = new LocalGStateCache(); const emptyGStateCache = new LocalGStateCache();
var preprocessor = new EvaluatorPreprocessor(stream, xref, stateManager); const preprocessor = new EvaluatorPreprocessor(stream, xref, stateManager);
var textState; let textState;
function getCurrentTextTransform() { function getCurrentTextTransform() {
// 9.4.4 Text Space Details // 9.4.4 Text Space Details
@ -2166,7 +2165,7 @@ class PartialEvaluator {
textContentItem.textAdvanceScale = scaleCtmX * scaleLineX; textContentItem.textAdvanceScale = scaleCtmX * scaleLineX;
textContentItem.lastCharSize = textContentItem.lastCharSize || 0; textContentItem.lastCharSize = textContentItem.lastCharSize || 0;
var spaceWidth = (font.spaceWidth / 1000) * textState.fontSize; const spaceWidth = (font.spaceWidth / 1000) * textState.fontSize;
if (spaceWidth) { if (spaceWidth) {
textContentItem.spaceWidth = spaceWidth; textContentItem.spaceWidth = spaceWidth;
textContentItem.trackingSpaceMin = spaceWidth * TRACKING_SPACE_FACTOR; textContentItem.trackingSpaceMin = spaceWidth * TRACKING_SPACE_FACTOR;
@ -2215,7 +2214,7 @@ class PartialEvaluator {
// Replaces all whitespaces with standard spaces (0x20), to avoid // Replaces all whitespaces with standard spaces (0x20), to avoid
// alignment issues between the textLayer and the canvas if the text // alignment issues between the textLayer and the canvas if the text
// contains e.g. tabs (fixes issue6612.pdf). // contains e.g. tabs (fixes issue6612.pdf).
var i = 0, let i = 0,
ii = str.length, ii = str.length,
code; code;
while (i < ii && (code = str.charCodeAt(i)) >= 0x20 && code <= 0x7f) { while (i < ii && (code = str.charCodeAt(i)) >= 0x20 && code <= 0x7f) {
@ -2534,7 +2533,7 @@ class PartialEvaluator {
} }
} }
var timeSlotManager = new TimeSlotManager(); const timeSlotManager = new TimeSlotManager();
return new Promise(function promiseBody(resolve, reject) { return new Promise(function promiseBody(resolve, reject) {
const next = function (promise) { const next = function (promise) {
@ -2549,7 +2548,7 @@ class PartialEvaluator {
}; };
task.ensureNotTerminated(); task.ensureNotTerminated();
timeSlotManager.reset(); timeSlotManager.reset();
var stop, let stop,
operation = {}, operation = {},
args = []; args = [];
while (!(stop = timeSlotManager.check())) { while (!(stop = timeSlotManager.check())) {
@ -2562,7 +2561,7 @@ class PartialEvaluator {
break; break;
} }
textState = stateManager.state; textState = stateManager.state;
var fn = operation.fn; const fn = operation.fn;
args = operation.args; args = operation.args;
switch (fn | 0) { switch (fn | 0) {
@ -2972,14 +2971,14 @@ class PartialEvaluator {
const xref = this.xref; const xref = this.xref;
let cidToGidBytes; let cidToGidBytes;
// 9.10.2 // 9.10.2
var toUnicode = dict.get("ToUnicode") || baseDict.get("ToUnicode"); const toUnicode = dict.get("ToUnicode") || baseDict.get("ToUnicode");
var toUnicodePromise = toUnicode const toUnicodePromise = toUnicode
? this.readToUnicode(toUnicode) ? this.readToUnicode(toUnicode)
: Promise.resolve(undefined); : Promise.resolve(undefined);
if (properties.composite) { if (properties.composite) {
// CIDSystemInfo helps to match CID to glyphs // CIDSystemInfo helps to match CID to glyphs
var cidSystemInfo = dict.get("CIDSystemInfo"); const cidSystemInfo = dict.get("CIDSystemInfo");
if (isDict(cidSystemInfo)) { if (isDict(cidSystemInfo)) {
properties.cidSystemInfo = { properties.cidSystemInfo = {
registry: stringToPDFString(cidSystemInfo.get("Registry")), registry: stringToPDFString(cidSystemInfo.get("Registry")),
@ -2988,7 +2987,7 @@ class PartialEvaluator {
}; };
} }
var cidToGidMap = dict.get("CIDToGIDMap"); const cidToGidMap = dict.get("CIDToGIDMap");
if (isStream(cidToGidMap)) { if (isStream(cidToGidMap)) {
cidToGidBytes = cidToGidMap.getBytes(); cidToGidBytes = cidToGidMap.getBytes();
} }
@ -3000,9 +2999,9 @@ class PartialEvaluator {
// glyph mapping in the font. // glyph mapping in the font.
// TODO: Loading the built in encoding in the font would allow the // TODO: Loading the built in encoding in the font would allow the
// differences to be merged in here not require us to hold on to it. // differences to be merged in here not require us to hold on to it.
var differences = []; const differences = [];
var baseEncodingName = null; let baseEncodingName = null;
var encoding; let encoding;
if (dict.has("Encoding")) { if (dict.has("Encoding")) {
encoding = dict.get("Encoding"); encoding = dict.get("Encoding");
if (isDict(encoding)) { if (isDict(encoding)) {
@ -3012,10 +3011,10 @@ class PartialEvaluator {
: null; : null;
// Load the differences between the base and original // Load the differences between the base and original
if (encoding.has("Differences")) { if (encoding.has("Differences")) {
var diffEncoding = encoding.get("Differences"); const diffEncoding = encoding.get("Differences");
var index = 0; let index = 0;
for (var j = 0, jj = diffEncoding.length; j < jj; j++) { for (let j = 0, jj = diffEncoding.length; j < jj; j++) {
var data = xref.fetchIfRef(diffEncoding[j]); const data = xref.fetchIfRef(diffEncoding[j]);
if (isNum(data)) { if (isNum(data)) {
index = data; index = data;
} else if (isName(data)) { } else if (isName(data)) {
@ -3046,8 +3045,8 @@ class PartialEvaluator {
if (baseEncodingName) { if (baseEncodingName) {
properties.defaultEncoding = getEncoding(baseEncodingName); properties.defaultEncoding = getEncoding(baseEncodingName);
} else { } else {
var isSymbolicFont = !!(properties.flags & FontFlags.Symbolic); const isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);
var isNonsymbolicFont = !!(properties.flags & FontFlags.Nonsymbolic); const isNonsymbolicFont = !!(properties.flags & FontFlags.Nonsymbolic);
// According to "Table 114" in section "9.6.6.1 General" (under // According to "Table 114" in section "9.6.6.1 General" (under
// "9.6.6 Character Encoding") of the PDF specification, a Nonsymbolic // "9.6.6 Character Encoding") of the PDF specification, a Nonsymbolic
// font should use the `StandardEncoding` if no encoding is specified. // font should use the `StandardEncoding` if no encoding is specified.
@ -3284,7 +3283,7 @@ class PartialEvaluator {
} }
readToUnicode(toUnicode) { readToUnicode(toUnicode) {
var cmapObj = toUnicode; const cmapObj = toUnicode;
if (isName(cmapObj)) { if (isName(cmapObj)) {
return CMapFactory.create({ return CMapFactory.create({
encoding: cmapObj, encoding: cmapObj,
@ -3306,21 +3305,21 @@ class PartialEvaluator {
if (cmap instanceof IdentityCMap) { if (cmap instanceof IdentityCMap) {
return new IdentityToUnicodeMap(0, 0xffff); return new IdentityToUnicodeMap(0, 0xffff);
} }
var map = new Array(cmap.length); const map = new Array(cmap.length);
// Convert UTF-16BE // Convert UTF-16BE
// NOTE: cmap can be a sparse array, so use forEach instead of // NOTE: cmap can be a sparse array, so use forEach instead of
// `for(;;)` to iterate over all keys. // `for(;;)` to iterate over all keys.
cmap.forEach(function (charCode, token) { cmap.forEach(function (charCode, token) {
var str = []; const str = [];
for (var k = 0; k < token.length; k += 2) { for (let k = 0; k < token.length; k += 2) {
var w1 = (token.charCodeAt(k) << 8) | token.charCodeAt(k + 1); const w1 = (token.charCodeAt(k) << 8) | token.charCodeAt(k + 1);
if ((w1 & 0xf800) !== 0xd800) { if ((w1 & 0xf800) !== 0xd800) {
// w1 < 0xD800 || w1 > 0xDFFF // w1 < 0xD800 || w1 > 0xDFFF
str.push(w1); str.push(w1);
continue; continue;
} }
k += 2; k += 2;
var w2 = (token.charCodeAt(k) << 8) | token.charCodeAt(k + 1); const w2 = (token.charCodeAt(k) << 8) | token.charCodeAt(k + 1);
str.push(((w1 & 0x3ff) << 10) + (w2 & 0x3ff) + 0x10000); str.push(((w1 & 0x3ff) << 10) + (w2 & 0x3ff) + 0x10000);
} }
map[charCode] = String.fromCodePoint.apply(String, str); map[charCode] = String.fromCodePoint.apply(String, str);
@ -3351,9 +3350,9 @@ class PartialEvaluator {
// Extract the encoding from the CIDToGIDMap // Extract the encoding from the CIDToGIDMap
// Set encoding 0 to later verify the font has an encoding // Set encoding 0 to later verify the font has an encoding
var result = []; const result = [];
for (var j = 0, jj = glyphsData.length; j < jj; j++) { for (let j = 0, jj = glyphsData.length; j < jj; j++) {
var glyphID = (glyphsData[j++] << 8) | glyphsData[j]; const glyphID = (glyphsData[j++] << 8) | glyphsData[j];
const code = j >> 1; const code = j >> 1;
if (glyphID === 0 && !toUnicode.has(code)) { if (glyphID === 0 && !toUnicode.has(code)) {
continue; continue;
@ -3364,12 +3363,12 @@ class PartialEvaluator {
} }
extractWidths(dict, descriptor, properties) { extractWidths(dict, descriptor, properties) {
var xref = this.xref; const xref = this.xref;
var glyphsWidths = []; let glyphsWidths = [];
var defaultWidth = 0; let defaultWidth = 0;
var glyphsVMetrics = []; const glyphsVMetrics = [];
var defaultVMetrics; let defaultVMetrics;
var i, ii, j, jj, start, code, widths; let i, ii, j, jj, start, code, widths;
if (properties.composite) { if (properties.composite) {
defaultWidth = dict.has("DW") ? dict.get("DW") : 1000; defaultWidth = dict.has("DW") ? dict.get("DW") : 1000;
@ -3383,7 +3382,7 @@ class PartialEvaluator {
glyphsWidths[start++] = xref.fetchIfRef(code[j]); glyphsWidths[start++] = xref.fetchIfRef(code[j]);
} }
} else { } else {
var width = xref.fetchIfRef(widths[++i]); const width = xref.fetchIfRef(widths[++i]);
for (j = start; j <= code; j++) { for (j = start; j <= code; j++) {
glyphsWidths[j] = width; glyphsWidths[j] = width;
} }
@ -3392,7 +3391,7 @@ class PartialEvaluator {
} }
if (properties.vertical) { if (properties.vertical) {
var vmetrics = dict.getArray("DW2") || [880, -1000]; let vmetrics = dict.getArray("DW2") || [880, -1000];
defaultVMetrics = [vmetrics[1], defaultWidth * 0.5, vmetrics[0]]; defaultVMetrics = [vmetrics[1], defaultWidth * 0.5, vmetrics[0]];
vmetrics = dict.get("W2"); vmetrics = dict.get("W2");
if (vmetrics) { if (vmetrics) {
@ -3408,7 +3407,7 @@ class PartialEvaluator {
]; ];
} }
} else { } else {
var vmetric = [ const vmetric = [
xref.fetchIfRef(vmetrics[++i]), xref.fetchIfRef(vmetrics[++i]),
xref.fetchIfRef(vmetrics[++i]), xref.fetchIfRef(vmetrics[++i]),
xref.fetchIfRef(vmetrics[++i]), xref.fetchIfRef(vmetrics[++i]),
@ -3421,7 +3420,7 @@ class PartialEvaluator {
} }
} }
} else { } else {
var firstChar = properties.firstChar; const firstChar = properties.firstChar;
widths = dict.get("Widths"); widths = dict.get("Widths");
if (widths) { if (widths) {
j = firstChar; j = firstChar;
@ -3431,9 +3430,9 @@ class PartialEvaluator {
defaultWidth = parseFloat(descriptor.get("MissingWidth")) || 0; defaultWidth = parseFloat(descriptor.get("MissingWidth")) || 0;
} else { } else {
// Trying get the BaseFont metrics (see comment above). // Trying get the BaseFont metrics (see comment above).
var baseFontName = dict.get("BaseFont"); const baseFontName = dict.get("BaseFont");
if (isName(baseFontName)) { if (isName(baseFontName)) {
var metrics = this.getBaseFontMetrics(baseFontName.name); const metrics = this.getBaseFontMetrics(baseFontName.name);
glyphsWidths = this.buildCharCodeToWidth(metrics.widths, properties); glyphsWidths = this.buildCharCodeToWidth(metrics.widths, properties);
defaultWidth = metrics.defaultWidth; defaultWidth = metrics.defaultWidth;
@ -3442,10 +3441,10 @@ class PartialEvaluator {
} }
// Heuristic: detection of monospace font by checking all non-zero widths // Heuristic: detection of monospace font by checking all non-zero widths
var isMonospace = true; let isMonospace = true;
var firstWidth = defaultWidth; let firstWidth = defaultWidth;
for (var glyph in glyphsWidths) { for (const glyph in glyphsWidths) {
var glyphWidth = glyphsWidths[glyph]; const glyphWidth = glyphsWidths[glyph];
if (!glyphWidth) { if (!glyphWidth) {
continue; continue;
} }
@ -3470,7 +3469,7 @@ class PartialEvaluator {
isSerifFont(baseFontName) { isSerifFont(baseFontName) {
// Simulating descriptor flags attribute // Simulating descriptor flags attribute
var fontNameWoStyle = baseFontName.split("-")[0]; const fontNameWoStyle = baseFontName.split("-")[0];
return ( return (
fontNameWoStyle in getSerifFonts() || fontNameWoStyle in getSerifFonts() ||
fontNameWoStyle.search(/serif/gi) !== -1 fontNameWoStyle.search(/serif/gi) !== -1
@ -3478,12 +3477,12 @@ class PartialEvaluator {
} }
getBaseFontMetrics(name) { getBaseFontMetrics(name) {
var defaultWidth = 0; let defaultWidth = 0;
var widths = Object.create(null); let widths = Object.create(null);
var monospace = false; let monospace = false;
var stdFontMap = getStdFontMap(); const stdFontMap = getStdFontMap();
var lookupName = stdFontMap[name] || name; let lookupName = stdFontMap[name] || name;
var Metrics = getMetrics(); const Metrics = getMetrics();
if (!(lookupName in Metrics)) { if (!(lookupName in Metrics)) {
// Use default fonts for looking up font metrics if the passed // Use default fonts for looking up font metrics if the passed
@ -3494,7 +3493,7 @@ class PartialEvaluator {
lookupName = "Helvetica"; lookupName = "Helvetica";
} }
} }
var glyphWidths = Metrics[lookupName]; const glyphWidths = Metrics[lookupName];
if (isNum(glyphWidths)) { if (isNum(glyphWidths)) {
defaultWidth = glyphWidths; defaultWidth = glyphWidths;
@ -3511,10 +3510,10 @@ class PartialEvaluator {
} }
buildCharCodeToWidth(widthsByGlyphName, properties) { buildCharCodeToWidth(widthsByGlyphName, properties) {
var widths = Object.create(null); const widths = Object.create(null);
var differences = properties.differences; const differences = properties.differences;
var encoding = properties.defaultEncoding; const encoding = properties.defaultEncoding;
for (var charCode = 0; charCode < 256; charCode++) { for (let charCode = 0; charCode < 256; charCode++) {
if (charCode in differences && widthsByGlyphName[differences[charCode]]) { if (charCode in differences && widthsByGlyphName[differences[charCode]]) {
widths[charCode] = widthsByGlyphName[differences[charCode]]; widths[charCode] = widthsByGlyphName[differences[charCode]];
continue; continue;
@ -3528,20 +3527,20 @@ class PartialEvaluator {
} }
preEvaluateFont(dict) { preEvaluateFont(dict) {
var baseDict = dict; const baseDict = dict;
var type = dict.get("Subtype"); let type = dict.get("Subtype");
if (!isName(type)) { if (!isName(type)) {
throw new FormatError("invalid font Subtype"); throw new FormatError("invalid font Subtype");
} }
var composite = false; let composite = false;
var uint8array; let uint8array;
if (type.name === "Type0") { if (type.name === "Type0") {
// If font is a composite // If font is a composite
// - get the descendant font // - get the descendant font
// - set the type according to the descendant font // - set the type according to the descendant font
// - get the FontDescriptor from the descendant font // - get the FontDescriptor from the descendant font
var df = dict.get("DescendantFonts"); const df = dict.get("DescendantFonts");
if (!df) { if (!df) {
throw new FormatError("Descendant fonts are not specified"); throw new FormatError("Descendant fonts are not specified");
} }
@ -3557,10 +3556,10 @@ class PartialEvaluator {
composite = true; composite = true;
} }
var descriptor = dict.get("FontDescriptor"); const descriptor = dict.get("FontDescriptor");
if (descriptor) { if (descriptor) {
var hash = new MurmurHash3_64(); var hash = new MurmurHash3_64();
var encoding = baseDict.getRaw("Encoding"); const encoding = baseDict.getRaw("Encoding");
if (isName(encoding)) { if (isName(encoding)) {
hash.update(encoding.name); hash.update(encoding.name);
} else if (isRef(encoding)) { } else if (isRef(encoding)) {
@ -3573,11 +3572,11 @@ class PartialEvaluator {
hash.update(entry.toString()); hash.update(entry.toString());
} else if (Array.isArray(entry)) { } else if (Array.isArray(entry)) {
// 'Differences' array (fixes bug1157493.pdf). // 'Differences' array (fixes bug1157493.pdf).
var diffLength = entry.length, const diffLength = entry.length,
diffBuf = new Array(diffLength); diffBuf = new Array(diffLength);
for (var j = 0; j < diffLength; j++) { for (let j = 0; j < diffLength; j++) {
var diffEntry = entry[j]; const diffEntry = entry[j];
if (isName(diffEntry)) { if (isName(diffEntry)) {
diffBuf[j] = diffEntry.name; diffBuf[j] = diffEntry.name;
} else if (isNum(diffEntry) || isRef(diffEntry)) { } else if (isNum(diffEntry) || isRef(diffEntry)) {
@ -3593,9 +3592,9 @@ class PartialEvaluator {
const lastChar = dict.get("LastChar") || (composite ? 0xffff : 0xff); const lastChar = dict.get("LastChar") || (composite ? 0xffff : 0xff);
hash.update(`${firstChar}-${lastChar}`); hash.update(`${firstChar}-${lastChar}`);
var toUnicode = dict.get("ToUnicode") || baseDict.get("ToUnicode"); const toUnicode = dict.get("ToUnicode") || baseDict.get("ToUnicode");
if (isStream(toUnicode)) { if (isStream(toUnicode)) {
var stream = toUnicode.str || toUnicode; const stream = toUnicode.str || toUnicode;
uint8array = stream.buffer uint8array = stream.buffer
? new Uint8Array(stream.buffer.buffer, 0, stream.bufferLength) ? new Uint8Array(stream.buffer.buffer, 0, stream.bufferLength)
: new Uint8Array( : new Uint8Array(
@ -3608,7 +3607,7 @@ class PartialEvaluator {
hash.update(toUnicode.name); hash.update(toUnicode.name);
} }
var widths = dict.get("Widths") || baseDict.get("Widths"); const widths = dict.get("Widths") || baseDict.get("Widths");
if (widths) { if (widths) {
uint8array = new Uint8Array(new Uint32Array(widths).buffer); uint8array = new Uint8Array(new Uint32Array(widths).buffer);
hash.update(uint8array); hash.update(uint8array);
@ -3626,13 +3625,13 @@ class PartialEvaluator {
} }
async translateFont(preEvaluatedFont) { async translateFont(preEvaluatedFont) {
var baseDict = preEvaluatedFont.baseDict; const baseDict = preEvaluatedFont.baseDict;
var dict = preEvaluatedFont.dict; const dict = preEvaluatedFont.dict;
var composite = preEvaluatedFont.composite; const composite = preEvaluatedFont.composite;
var descriptor = preEvaluatedFont.descriptor; let descriptor = preEvaluatedFont.descriptor;
var type = preEvaluatedFont.type; const type = preEvaluatedFont.type;
var maxCharIndex = composite ? 0xffff : 0xff; const maxCharIndex = composite ? 0xffff : 0xff;
var properties; let properties;
const firstChar = dict.get("FirstChar") || 0; const firstChar = dict.get("FirstChar") || 0;
const lastChar = dict.get("LastChar") || maxCharIndex; const lastChar = dict.get("LastChar") || maxCharIndex;
@ -3647,18 +3646,18 @@ class PartialEvaluator {
// Before PDF 1.5 if the font was one of the base 14 fonts, having a // Before PDF 1.5 if the font was one of the base 14 fonts, having a
// FontDescriptor was not required. // FontDescriptor was not required.
// This case is here for compatibility. // This case is here for compatibility.
var baseFontName = dict.get("BaseFont"); let baseFontName = dict.get("BaseFont");
if (!isName(baseFontName)) { if (!isName(baseFontName)) {
throw new FormatError("Base font is not specified"); throw new FormatError("Base font is not specified");
} }
// Using base font name as a font name. // Using base font name as a font name.
baseFontName = baseFontName.name.replace(/[,_]/g, "-"); baseFontName = baseFontName.name.replace(/[,_]/g, "-");
var metrics = this.getBaseFontMetrics(baseFontName); const metrics = this.getBaseFontMetrics(baseFontName);
// Simulating descriptor flags attribute // Simulating descriptor flags attribute
var fontNameWoStyle = baseFontName.split("-")[0]; const fontNameWoStyle = baseFontName.split("-")[0];
var flags = const flags =
(this.isSerifFont(fontNameWoStyle) ? FontFlags.Serif : 0) | (this.isSerifFont(fontNameWoStyle) ? FontFlags.Serif : 0) |
(metrics.monospace ? FontFlags.FixedPitch : 0) | (metrics.monospace ? FontFlags.FixedPitch : 0) |
(getSymbolsFonts()[fontNameWoStyle] (getSymbolsFonts()[fontNameWoStyle]
@ -3702,8 +3701,8 @@ class PartialEvaluator {
// TODO Fill the width array depending on which of the base font this is // TODO Fill the width array depending on which of the base font this is
// a variant. // a variant.
var fontName = descriptor.get("FontName"); let fontName = descriptor.get("FontName");
var baseFont = dict.get("BaseFont"); let baseFont = dict.get("BaseFont");
// Some bad PDFs have a string as the font name. // Some bad PDFs have a string as the font name.
if (isString(fontName)) { if (isString(fontName)) {
fontName = Name.get(fontName); fontName = Name.get(fontName);
@ -3713,8 +3712,8 @@ class PartialEvaluator {
} }
if (type !== "Type3") { if (type !== "Type3") {
var fontNameStr = fontName && fontName.name; const fontNameStr = fontName && fontName.name;
var baseFontStr = baseFont && baseFont.name; const baseFontStr = baseFont && baseFont.name;
if (fontNameStr !== baseFontStr) { if (fontNameStr !== baseFontStr) {
info( info(
`The FontDescriptor's FontName is "${fontNameStr}" but ` + `The FontDescriptor's FontName is "${fontNameStr}" but ` +
@ -3891,22 +3890,22 @@ class TranslatedFont {
// When parsing Type3 glyphs, always ignore them if there are errors. // When parsing Type3 glyphs, always ignore them if there are errors.
// Compared to the parsing of e.g. an entire page, it doesn't really // Compared to the parsing of e.g. an entire page, it doesn't really
// make sense to only be able to render a Type3 glyph partially. // make sense to only be able to render a Type3 glyph partially.
var type3Options = Object.create(evaluator.options); const type3Options = Object.create(evaluator.options);
type3Options.ignoreErrors = false; type3Options.ignoreErrors = false;
var type3Evaluator = evaluator.clone(type3Options); const type3Evaluator = evaluator.clone(type3Options);
type3Evaluator.parsingType3Font = true; type3Evaluator.parsingType3Font = true;
const translatedFont = this.font, const translatedFont = this.font,
type3Dependencies = this.type3Dependencies; type3Dependencies = this.type3Dependencies;
var loadCharProcsPromise = Promise.resolve(); let loadCharProcsPromise = Promise.resolve();
var charProcs = this.dict.get("CharProcs"); const charProcs = this.dict.get("CharProcs");
var fontResources = this.dict.get("Resources") || resources; const fontResources = this.dict.get("Resources") || resources;
var charProcOperatorList = Object.create(null); const charProcOperatorList = Object.create(null);
for (const key of charProcs.getKeys()) { for (const key of charProcs.getKeys()) {
loadCharProcsPromise = loadCharProcsPromise.then(() => { loadCharProcsPromise = loadCharProcsPromise.then(() => {
var glyphStream = charProcs.get(key); const glyphStream = charProcs.get(key);
var operatorList = new OperatorList(); const operatorList = new OperatorList();
return type3Evaluator return type3Evaluator
.getOperatorList({ .getOperatorList({
stream: glyphStream, stream: glyphStream,
@ -4013,13 +4012,13 @@ class StateManager {
} }
save() { save() {
var old = this.state; const old = this.state;
this.stateStack.push(this.state); this.stateStack.push(this.state);
this.state = old.clone(); this.state = old.clone();
} }
restore() { restore() {
var prev = this.stateStack.pop(); const prev = this.stateStack.pop();
if (prev) { if (prev) {
this.state = prev; this.state = prev;
} }
@ -4047,7 +4046,7 @@ class TextState {
} }
setTextMatrix(a, b, c, d, e, f) { setTextMatrix(a, b, c, d, e, f) {
var m = this.textMatrix; const m = this.textMatrix;
m[0] = a; m[0] = a;
m[1] = b; m[1] = b;
m[2] = c; m[2] = c;
@ -4057,7 +4056,7 @@ class TextState {
} }
setTextLineMatrix(a, b, c, d, e, f) { setTextLineMatrix(a, b, c, d, e, f) {
var m = this.textLineMatrix; const m = this.textLineMatrix;
m[0] = a; m[0] = a;
m[1] = b; m[1] = b;
m[2] = c; m[2] = c;
@ -4067,13 +4066,13 @@ class TextState {
} }
translateTextMatrix(x, y) { translateTextMatrix(x, y) {
var m = this.textMatrix; const m = this.textMatrix;
m[4] = m[0] * x + m[2] * y + m[4]; m[4] = m[0] * x + m[2] * y + m[4];
m[5] = m[1] * x + m[3] * y + m[5]; m[5] = m[1] * x + m[3] * y + m[5];
} }
translateTextLineMatrix(x, y) { translateTextLineMatrix(x, y) {
var m = this.textLineMatrix; const m = this.textLineMatrix;
m[4] = m[0] * x + m[2] * y + m[4]; m[4] = m[0] * x + m[2] * y + m[4];
m[5] = m[1] * x + m[3] * y + m[5]; m[5] = m[1] * x + m[3] * y + m[5];
} }
@ -4084,7 +4083,7 @@ class TextState {
} }
clone() { clone() {
var clone = Object.create(this); const clone = Object.create(this);
clone.textMatrix = this.textMatrix.slice(); clone.textMatrix = this.textMatrix.slice();
clone.textLineMatrix = this.textLineMatrix.slice(); clone.textLineMatrix = this.textLineMatrix.slice();
clone.fontMatrix = this.fontMatrix.slice(); clone.fontMatrix = this.fontMatrix.slice();
@ -4276,26 +4275,26 @@ class EvaluatorPreprocessor {
// avoiding allocations where possible is worthwhile. // avoiding allocations where possible is worthwhile.
// //
read(operation) { read(operation) {
var args = operation.args; let args = operation.args;
while (true) { while (true) {
var obj = this.parser.getObj(); const obj = this.parser.getObj();
if (obj instanceof Cmd) { if (obj instanceof Cmd) {
var cmd = obj.cmd; const cmd = obj.cmd;
// Check that the command is valid // Check that the command is valid
var opSpec = EvaluatorPreprocessor.opMap[cmd]; const opSpec = EvaluatorPreprocessor.opMap[cmd];
if (!opSpec) { if (!opSpec) {
warn(`Unknown command "${cmd}".`); warn(`Unknown command "${cmd}".`);
continue; continue;
} }
var fn = opSpec.id; const fn = opSpec.id;
var numArgs = opSpec.numArgs; const numArgs = opSpec.numArgs;
var argsLength = args !== null ? args.length : 0; let argsLength = args !== null ? args.length : 0;
if (!opSpec.variableArgs) { if (!opSpec.variableArgs) {
// Postscript commands can be nested, e.g. /F2 /GS2 gs 5.711 Tf // Postscript commands can be nested, e.g. /F2 /GS2 gs 5.711 Tf
if (argsLength !== numArgs) { if (argsLength !== numArgs) {
var nonProcessedArgs = this.nonProcessedArgs; const nonProcessedArgs = this.nonProcessedArgs;
while (argsLength > numArgs) { while (argsLength > numArgs) {
nonProcessedArgs.push(args.shift()); nonProcessedArgs.push(args.shift());
argsLength--; argsLength--;