Browse Source

Merge pull request #4895 from p01/Small_optimizations_1

Small optimizations 1
Yury Delendik 11 years ago
parent
commit
b2d8e73d54
  1. 57
      src/core/evaluator.js
  2. 6
      src/core/parser.js
  3. 137
      src/display/canvas.js
  4. 18
      src/shared/util.js

57
src/core/evaluator.js

@ -576,11 +576,11 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
var dict = (isStream(pattern) ? pattern.dict : pattern); var dict = (isStream(pattern) ? pattern.dict : pattern);
var typeNum = dict.get('PatternType'); var typeNum = dict.get('PatternType');
if (typeNum == TILING_PATTERN) { if (typeNum === TILING_PATTERN) {
var color = cs.base ? cs.base.getRgb(args, 0) : null; var color = cs.base ? cs.base.getRgb(args, 0) : null;
return this.handleTilingType(fn, color, resources, pattern, return this.handleTilingType(fn, color, resources, pattern,
dict, operatorList); dict, operatorList);
} else if (typeNum == SHADING_PATTERN) { } else if (typeNum === SHADING_PATTERN) {
var shading = dict.get('Shading'); var shading = dict.get('Shading');
var matrix = dict.get('Matrix'); var matrix = dict.get('Matrix');
pattern = Pattern.parseShading(shading, matrix, xref, resources); pattern = Pattern.parseShading(shading, matrix, xref, resources);
@ -954,7 +954,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
} }
var glyphUnicode = glyph.unicode; var glyphUnicode = glyph.unicode;
if (glyphUnicode in NormalizedUnicodes) { if (NormalizedUnicodes[glyphUnicode] !== undefined) {
glyphUnicode = NormalizedUnicodes[glyphUnicode]; glyphUnicode = NormalizedUnicodes[glyphUnicode];
} }
glyphUnicode = reverseIfRtl(glyphUnicode); glyphUnicode = reverseIfRtl(glyphUnicode);
@ -1486,7 +1486,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
var composite = false; var composite = false;
var uint8array; var 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
@ -1552,7 +1552,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
var properties; var properties;
if (!descriptor) { if (!descriptor) {
if (type.name == 'Type3') { if (type.name === 'Type3') {
// FontDescriptor is only required for Type3 fonts when the document // FontDescriptor is only required for Type3 fonts when the document
// is a tagged pdf. Create a barbebones one to get by. // is a tagged pdf. Create a barbebones one to get by.
descriptor = new Dict(null); descriptor = new Dict(null);
@ -2081,7 +2081,7 @@ var EvaluatorPreprocessor = (function EvaluatorPreprocessorClosure() {
} }
if (!isCmd(obj)) { if (!isCmd(obj)) {
// argument // argument
if (obj !== null && obj !== undefined) { if (obj !== null) {
args.push((obj instanceof Dict ? obj.getAll() : obj)); args.push((obj instanceof Dict ? obj.getAll() : obj));
assert(args.length <= 33, 'Too many arguments'); assert(args.length <= 33, 'Too many arguments');
} }
@ -2097,37 +2097,32 @@ var EvaluatorPreprocessor = (function EvaluatorPreprocessorClosure() {
} }
var fn = opSpec.id; var fn = opSpec.id;
var numArgs = opSpec.numArgs;
// Some post script commands can be nested, e.g. /F2 /GS2 gs 5.711 Tf
if (!opSpec.variableArgs && args.length !== opSpec.numArgs) { if (!opSpec.variableArgs) {
while (args.length > opSpec.numArgs) { // Some post script commands can be nested, e.g. /F2 /GS2 gs 5.711 Tf
this.nonProcessedArgs.push(args.shift()); if (args.length !== numArgs) {
} var nonProcessedArgs = this.nonProcessedArgs;
while (args.length > numArgs) {
while (args.length < opSpec.numArgs && this.nonProcessedArgs.length) { nonProcessedArgs.push(args.shift());
args.unshift(this.nonProcessedArgs.pop()); }
while (args.length < numArgs && nonProcessedArgs.length !== 0) {
args.unshift(nonProcessedArgs.pop());
}
} }
}
// Validate the number of arguments for the command if (args.length < numArgs) {
if (opSpec.variableArgs) {
if (args.length > opSpec.numArgs) {
info('Command ' + fn + ': expected [0,' + opSpec.numArgs +
'] args, but received ' + args.length + ' args');
}
} else {
if (args.length < opSpec.numArgs) {
// If we receive too few args, it's not possible to possible // If we receive too few args, it's not possible to possible
// to execute the command, so skip the command // to execute the command, so skip the command
info('Command ' + fn + ': because expected ' + info('Command ' + fn + ': because expected ' +
opSpec.numArgs + ' args, but received ' + args.length + numArgs + ' args, but received ' + args.length +
' args; skipping'); ' args; skipping');
args = []; args = [];
continue; continue;
} else if (args.length > opSpec.numArgs) {
info('Command ' + fn + ': expected ' + opSpec.numArgs +
' args, but received ' + args.length + ' args');
} }
} else if (args.length > numArgs) {
info('Command ' + fn + ': expected [0,' + numArgs +
'] args, but received ' + args.length + ' args');
} }
// TODO figure out how to type-check vararg functions // TODO figure out how to type-check vararg functions
@ -2172,9 +2167,9 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
// have been found at index. // have been found at index.
for (var i = 0; i < count; i++) { for (var i = 0; i < count; i++) {
var arg = argsArray[index + 4 * i + 2]; var arg = argsArray[index + 4 * i + 2];
var imageMask = arg.length == 1 && arg[0]; var imageMask = arg.length === 1 && arg[0];
if (imageMask && imageMask.width == 1 && imageMask.height == 1 && if (imageMask && imageMask.width === 1 && imageMask.height === 1 &&
(!imageMask.data.length || (imageMask.data.length == 1 && (!imageMask.data.length || (imageMask.data.length === 1 &&
imageMask.data[0] === 0))) { imageMask.data[0] === 0))) {
fnArray[index + 4 * i + 2] = OPS.paintSolidColorImageMask; fnArray[index + 4 * i + 2] = OPS.paintSolidColorImageMask;
continue; continue;

6
src/core/parser.js

@ -758,19 +758,19 @@ var Lexer = (function LexerClosure() {
// command // command
var str = String.fromCharCode(ch); var str = String.fromCharCode(ch);
var knownCommands = this.knownCommands; var knownCommands = this.knownCommands;
var knownCommandFound = knownCommands && (str in knownCommands); var knownCommandFound = knownCommands && knownCommands[str] !== undefined;
while ((ch = this.nextChar()) >= 0 && !specialChars[ch]) { while ((ch = this.nextChar()) >= 0 && !specialChars[ch]) {
// stop if known command is found and next character does not make // stop if known command is found and next character does not make
// the str a command // the str a command
var possibleCommand = str + String.fromCharCode(ch); var possibleCommand = str + String.fromCharCode(ch);
if (knownCommandFound && !(possibleCommand in knownCommands)) { if (knownCommandFound && knownCommands[possibleCommand] === undefined) {
break; break;
} }
if (str.length === 128) { if (str.length === 128) {
error('Command token too long: ' + str.length); error('Command token too long: ' + str.length);
} }
str = possibleCommand; str = possibleCommand;
knownCommandFound = knownCommands && (str in knownCommands); knownCommandFound = knownCommands && knownCommands[str] !== undefined;
} }
if (str === 'true') { if (str === 'true') {
return true; return true;

137
src/display/canvas.js

@ -394,6 +394,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
// Defines the time the executeOperatorList is going to be executing // Defines the time the executeOperatorList is going to be executing
// before it stops and shedules a continue of execution. // before it stops and shedules a continue of execution.
var EXECUTION_TIME = 15; var EXECUTION_TIME = 15;
// Defines the number of steps before checking the execution time
var EXECUTION_STEPS = 10;
function CanvasGraphics(canvasCtx, commonObjs, objs, imageLayer) { function CanvasGraphics(canvasCtx, commonObjs, objs, imageLayer) {
this.ctx = canvasCtx; this.ctx = canvasCtx;
@ -599,49 +601,54 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
} }
} }
function composeSMaskBackdrop(bytes, r0, g0, b0) {
var length = bytes.length;
for (var i = 3; i < length; i += 4) {
var alpha = bytes[i];
if (alpha === 0) {
bytes[i - 3] = r0;
bytes[i - 2] = g0;
bytes[i - 1] = b0;
} else if (alpha < 255) {
var alpha_ = 255 - alpha;
bytes[i - 3] = (bytes[i - 3] * alpha + r0 * alpha_) >> 8;
bytes[i - 2] = (bytes[i - 2] * alpha + g0 * alpha_) >> 8;
bytes[i - 1] = (bytes[i - 1] * alpha + b0 * alpha_) >> 8;
}
}
}
function composeSMaskAlpha(maskData, layerData) {
var length = maskData.length;
var scale = 1 / 255;
for (var i = 3; i < length; i += 4) {
var alpha = maskData[i];
layerData[i] = (layerData[i] * alpha * scale) | 0;
}
}
function composeSMaskLuminosity(maskData, layerData) {
var length = maskData.length;
for (var i = 3; i < length; i += 4) {
var y = ((maskData[i - 3] * 77) + // * 0.3 / 255 * 0x10000
(maskData[i - 2] * 152) + // * 0.59 ....
(maskData[i - 1] * 28)) | 0; // * 0.11 ....
layerData[i] = (layerData[i] * y) >> 16;
}
}
function genericComposeSMask(maskCtx, layerCtx, width, height, function genericComposeSMask(maskCtx, layerCtx, width, height,
subtype, backdrop) { subtype, backdrop) {
var addBackdropFn; var hasBackdrop = backdrop !== undefined;
if (backdrop) { var r0 = hasBackdrop ? backdrop[0] : 0;
addBackdropFn = function (r0, g0, b0, bytes) { var g0 = hasBackdrop ? backdrop[1] : 0;
var length = bytes.length; var b0 = hasBackdrop ? backdrop[2] : 0;
for (var i = 3; i < length; i += 4) {
var alpha = bytes[i] / 255;
if (alpha === 0) {
bytes[i - 3] = r0;
bytes[i - 2] = g0;
bytes[i - 1] = b0;
} else if (alpha < 1) {
var alpha_ = 1 - alpha;
bytes[i - 3] = (bytes[i - 3] * alpha + r0 * alpha_) | 0;
bytes[i - 2] = (bytes[i - 2] * alpha + g0 * alpha_) | 0;
bytes[i - 1] = (bytes[i - 1] * alpha + b0 * alpha_) | 0;
}
}
}.bind(null, backdrop[0], backdrop[1], backdrop[2]);
} else {
addBackdropFn = function () {};
}
var composeFn; var composeFn;
if (subtype === 'Luminosity') { if (subtype === 'Luminosity') {
composeFn = function (maskDataBytes, layerDataBytes) { composeFn = composeSMaskLuminosity;
var length = maskDataBytes.length;
for (var i = 3; i < length; i += 4) {
var y = ((maskDataBytes[i - 3] * 77) + // * 0.3 / 255 * 0x10000
(maskDataBytes[i - 2] * 152) + // * 0.59 ....
(maskDataBytes[i - 1] * 28)) | 0; // * 0.11 ....
layerDataBytes[i] = (layerDataBytes[i] * y) >> 16;
}
};
} else { } else {
composeFn = function (maskDataBytes, layerDataBytes) { composeFn = composeSMaskAlpha;
var length = maskDataBytes.length;
for (var i = 3; i < length; i += 4) {
var alpha = maskDataBytes[i];
layerDataBytes[i] = (layerDataBytes[i] * alpha / 255) | 0;
}
};
} }
// processing image in chunks to save memory // processing image in chunks to save memory
@ -652,7 +659,9 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var maskData = maskCtx.getImageData(0, row, width, chunkHeight); var maskData = maskCtx.getImageData(0, row, width, chunkHeight);
var layerData = layerCtx.getImageData(0, row, width, chunkHeight); var layerData = layerCtx.getImageData(0, row, width, chunkHeight);
addBackdropFn(maskData.data); if (hasBackdrop) {
composeSMaskBackdrop(maskData.data, r0, g0, b0);
}
composeFn(maskData.data, layerData.data); composeFn(maskData.data, layerData.data);
maskCtx.putImageData(layerData, 0, row); maskCtx.putImageData(layerData, 0, row);
@ -726,18 +735,21 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var argsArrayLen = argsArray.length; var argsArrayLen = argsArray.length;
// Sometimes the OperatorList to execute is empty. // Sometimes the OperatorList to execute is empty.
if (argsArrayLen == i) { if (argsArrayLen === i) {
return i; return i;
} }
var endTime = Date.now() + EXECUTION_TIME; var chunkOperations = (argsArrayLen - i > EXECUTION_STEPS &&
typeof continueCallback === 'function');
var endTime = chunkOperations ? Date.now() + EXECUTION_TIME : 0;
var steps = 0;
var commonObjs = this.commonObjs; var commonObjs = this.commonObjs;
var objs = this.objs; var objs = this.objs;
var fnId; var fnId;
while (true) { while (true) {
if (stepper && i === stepper.nextBreakPoint) { if (stepper !== undefined && i === stepper.nextBreakPoint) {
stepper.breakIt(i, continueCallback); stepper.breakIt(i, continueCallback);
return i; return i;
} }
@ -750,16 +762,13 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var deps = argsArray[i]; var deps = argsArray[i];
for (var n = 0, nn = deps.length; n < nn; n++) { for (var n = 0, nn = deps.length; n < nn; n++) {
var depObjId = deps[n]; var depObjId = deps[n];
var common = depObjId.substring(0, 2) == 'g_'; var common = depObjId[0] === 'g' && depObjId[1] === '_';
var objsPool = common ? commonObjs : objs;
// If the promise isn't resolved yet, add the continueCallback // If the promise isn't resolved yet, add the continueCallback
// to the promise and bail out. // to the promise and bail out.
if (!common && !objs.isResolved(depObjId)) { if (!objsPool.isResolved(depObjId)) {
objs.get(depObjId, continueCallback); objsPool.get(depObjId, continueCallback);
return i;
}
if (common && !commonObjs.isResolved(depObjId)) {
commonObjs.get(depObjId, continueCallback);
return i; return i;
} }
} }
@ -768,15 +777,18 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
i++; i++;
// If the entire operatorList was executed, stop as were done. // If the entire operatorList was executed, stop as were done.
if (i == argsArrayLen) { if (i === argsArrayLen) {
return i; return i;
} }
// If the execution took longer then a certain amount of time and // If the execution took longer then a certain amount of time and
// `continueCallback` is specified, interrupt the execution. // `continueCallback` is specified, interrupt the execution.
if (continueCallback && Date.now() > endTime) { if (chunkOperations && ++steps > EXECUTION_STEPS) {
continueCallback(); if (Date.now() > endTime) {
return i; continueCallback();
return i;
}
steps = 0;
} }
// If the operatorList isn't executed completely yet OR the execution // If the operatorList isn't executed completely yet OR the execution
@ -935,18 +947,15 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var old = this.current; var old = this.current;
this.stateStack.push(old); this.stateStack.push(old);
this.current = old.clone(); this.current = old.clone();
if (this.current.activeSMask) { this.current.activeSMask = null;
this.current.activeSMask = null;
}
}, },
restore: function CanvasGraphics_restore() { restore: function CanvasGraphics_restore() {
var prev = this.stateStack.pop(); if (this.stateStack.length !== 0) {
if (prev) { if (this.current.activeSMask !== null) {
if (this.current.activeSMask) {
this.endSMaskGroup(); this.endSMaskGroup();
} }
this.current = prev; this.current = this.stateStack.pop();
this.ctx.restore(); this.ctx.restore();
} }
}, },
@ -1502,7 +1511,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
// Color // Color
getColorN_Pattern: function CanvasGraphics_getColorN_Pattern(IR) { getColorN_Pattern: function CanvasGraphics_getColorN_Pattern(IR) {
var pattern; var pattern;
if (IR[0] == 'TilingPattern') { if (IR[0] === 'TilingPattern') {
var color = IR[1]; var color = IR[1];
pattern = new TilingPattern(IR, color, this.ctx, this.objs, pattern = new TilingPattern(IR, color, this.ctx, this.objs,
this.commonObjs, this.baseTransform); this.commonObjs, this.baseTransform);
@ -1578,13 +1587,13 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
this.save(); this.save();
this.baseTransformStack.push(this.baseTransform); this.baseTransformStack.push(this.baseTransform);
if (matrix && isArray(matrix) && 6 == matrix.length) { if (isArray(matrix) && 6 === matrix.length) {
this.transform.apply(this, matrix); this.transform.apply(this, matrix);
} }
this.baseTransform = this.ctx.mozCurrentTransform; this.baseTransform = this.ctx.mozCurrentTransform;
if (bbox && isArray(bbox) && 4 == bbox.length) { if (isArray(bbox) && 4 === bbox.length) {
var width = bbox[2] - bbox[0]; var width = bbox[2] - bbox[0];
var height = bbox[3] - bbox[1]; var height = bbox[3] - bbox[1];
this.rectangle(bbox[0], bbox[1], width, height); this.rectangle(bbox[0], bbox[1], width, height);
@ -1736,7 +1745,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
matrix) { matrix) {
this.save(); this.save();
if (rect && isArray(rect) && 4 == rect.length) { if (isArray(rect) && 4 === rect.length) {
var width = rect[2] - rect[0]; var width = rect[2] - rect[0];
var height = rect[3] - rect[1]; var height = rect[3] - rect[1];
this.rectangle(rect[0], rect[1], width, height); this.rectangle(rect[0], rect[1], width, height);
@ -2054,7 +2063,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
consumePath: function CanvasGraphics_consumePath() { consumePath: function CanvasGraphics_consumePath() {
var ctx = this.ctx; var ctx = this.ctx;
if (this.pendingClip) { if (this.pendingClip) {
if (this.pendingClip == EO_CLIP) { if (this.pendingClip === EO_CLIP) {
if (ctx.mozFillRule !== undefined) { if (ctx.mozFillRule !== undefined) {
ctx.mozFillRule = 'evenodd'; ctx.mozFillRule = 'evenodd';
ctx.clip(); ctx.clip();

18
src/shared/util.js

@ -911,15 +911,15 @@ function isBool(v) {
} }
function isInt(v) { function isInt(v) {
return typeof v == 'number' && ((v | 0) == v); return typeof v === 'number' && ((v | 0) === v);
} }
function isNum(v) { function isNum(v) {
return typeof v == 'number'; return typeof v === 'number';
} }
function isString(v) { function isString(v) {
return typeof v == 'string'; return typeof v === 'string';
} }
function isNull(v) { function isNull(v) {
@ -931,7 +931,7 @@ function isName(v) {
} }
function isCmd(v, cmd) { function isCmd(v, cmd) {
return v instanceof Cmd && (!cmd || v.cmd == cmd); return v instanceof Cmd && (cmd === undefined || v.cmd === cmd);
} }
function isDict(v, type) { function isDict(v, type) {
@ -942,7 +942,7 @@ function isDict(v, type) {
return true; return true;
} }
var dictType = v.get('Type'); var dictType = v.get('Type');
return isName(dictType) && dictType.name == type; return isName(dictType) && dictType.name === type;
} }
function isArray(v) { function isArray(v) {
@ -950,13 +950,11 @@ function isArray(v) {
} }
function isStream(v) { function isStream(v) {
return typeof v == 'object' && v !== null && v !== undefined && return typeof v === 'object' && v !== null && v.getBytes !== undefined;
('getBytes' in v);
} }
function isArrayBuffer(v) { function isArrayBuffer(v) {
return typeof v == 'object' && v !== null && v !== undefined && return typeof v === 'object' && v !== null && v.byteLength !== undefined;
('byteLength' in v);
} }
function isRef(v) { function isRef(v) {
@ -1298,7 +1296,7 @@ PDFJS.createPromiseCapability = createPromiseCapability;
HandlerManager.scheduleHandlers(this); HandlerManager.scheduleHandlers(this);
return nextPromise; return nextPromise;
}, },
catch: function Promise_catch(onReject) { catch: function Promise_catch(onReject) {
return this.then(undefined, onReject); return this.then(undefined, onReject);
} }

Loading…
Cancel
Save