2015-11-10 10:24:15 +09:00
|
|
|
/* Copyright 2015 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-04-02 21:25:33 +09:00
|
|
|
import {
|
2017-12-13 22:51:45 +09:00
|
|
|
assert, CMapCompressionType, removeNullCharacters, stringToBytes,
|
2018-06-04 19:36:26 +09:00
|
|
|
unreachable, Util, warn
|
2017-04-02 21:25:33 +09:00
|
|
|
} from '../shared/util';
|
2015-11-22 01:32:47 +09:00
|
|
|
|
2017-07-24 07:09:18 +09:00
|
|
|
const DEFAULT_LINK_REL = 'noopener noreferrer nofollow';
|
|
|
|
const SVG_NS = 'http://www.w3.org/2000/svg';
|
2017-01-24 01:34:27 +09:00
|
|
|
|
2017-05-06 01:12:26 +09:00
|
|
|
class DOMCanvasFactory {
|
|
|
|
create(width, height) {
|
2017-07-20 21:04:54 +09:00
|
|
|
if (width <= 0 || height <= 0) {
|
|
|
|
throw new Error('invalid canvas size');
|
|
|
|
}
|
2017-05-06 01:12:26 +09:00
|
|
|
let canvas = document.createElement('canvas');
|
|
|
|
let context = canvas.getContext('2d');
|
2017-01-28 02:58:39 +09:00
|
|
|
canvas.width = width;
|
|
|
|
canvas.height = height;
|
2017-02-07 06:19:56 +09:00
|
|
|
return {
|
2017-04-25 23:17:18 +09:00
|
|
|
canvas,
|
|
|
|
context,
|
2017-02-07 06:19:56 +09:00
|
|
|
};
|
2017-05-06 01:12:26 +09:00
|
|
|
}
|
2017-01-28 02:58:39 +09:00
|
|
|
|
2017-05-06 01:12:26 +09:00
|
|
|
reset(canvasAndContext, width, height) {
|
2017-07-20 21:04:54 +09:00
|
|
|
if (!canvasAndContext.canvas) {
|
|
|
|
throw new Error('canvas is not specified');
|
|
|
|
}
|
|
|
|
if (width <= 0 || height <= 0) {
|
|
|
|
throw new Error('invalid canvas size');
|
|
|
|
}
|
2017-05-06 01:12:26 +09:00
|
|
|
canvasAndContext.canvas.width = width;
|
|
|
|
canvasAndContext.canvas.height = height;
|
|
|
|
}
|
2017-01-28 02:58:39 +09:00
|
|
|
|
2017-05-06 01:12:26 +09:00
|
|
|
destroy(canvasAndContext) {
|
2017-07-20 21:04:54 +09:00
|
|
|
if (!canvasAndContext.canvas) {
|
|
|
|
throw new Error('canvas is not specified');
|
|
|
|
}
|
2017-01-28 02:58:39 +09:00
|
|
|
// Zeroing the width and height cause Firefox to release graphics
|
|
|
|
// resources immediately, which can greatly reduce memory consumption.
|
2017-05-06 01:12:26 +09:00
|
|
|
canvasAndContext.canvas.width = 0;
|
|
|
|
canvasAndContext.canvas.height = 0;
|
|
|
|
canvasAndContext.canvas = null;
|
|
|
|
canvasAndContext.context = null;
|
2017-01-28 02:58:39 +09:00
|
|
|
}
|
2017-05-06 01:12:26 +09:00
|
|
|
}
|
2017-01-28 02:58:39 +09:00
|
|
|
|
2017-05-06 00:55:02 +09:00
|
|
|
class DOMCMapReaderFactory {
|
|
|
|
constructor({ baseUrl = null, isCompressed = false, }) {
|
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
this.isCompressed = isCompressed;
|
2017-02-12 23:54:41 +09:00
|
|
|
}
|
|
|
|
|
2017-05-06 00:55:02 +09:00
|
|
|
fetch({ name, }) {
|
2017-09-28 19:29:01 +09:00
|
|
|
if (!this.baseUrl) {
|
2018-02-18 00:57:24 +09:00
|
|
|
return Promise.reject(new Error(
|
|
|
|
'The CMap "baseUrl" parameter must be specified, ensure that ' +
|
|
|
|
'the "cMapUrl" and "cMapPacked" API parameters are provided.'));
|
2017-09-28 19:29:01 +09:00
|
|
|
}
|
2017-05-06 00:55:02 +09:00
|
|
|
if (!name) {
|
|
|
|
return Promise.reject(new Error('CMap name must be specified.'));
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let url = this.baseUrl + name + (this.isCompressed ? '.bcmap' : '');
|
2017-02-12 23:54:41 +09:00
|
|
|
|
2017-05-06 00:55:02 +09:00
|
|
|
let request = new XMLHttpRequest();
|
|
|
|
request.open('GET', url, true);
|
2017-03-26 01:43:51 +09:00
|
|
|
|
2017-05-06 00:55:02 +09:00
|
|
|
if (this.isCompressed) {
|
|
|
|
request.responseType = 'arraybuffer';
|
|
|
|
}
|
|
|
|
request.onreadystatechange = () => {
|
|
|
|
if (request.readyState !== XMLHttpRequest.DONE) {
|
|
|
|
return;
|
2017-02-12 23:54:41 +09:00
|
|
|
}
|
2017-05-06 00:55:02 +09:00
|
|
|
if (request.status === 200 || request.status === 0) {
|
|
|
|
let data;
|
|
|
|
if (this.isCompressed && request.response) {
|
|
|
|
data = new Uint8Array(request.response);
|
|
|
|
} else if (!this.isCompressed && request.responseText) {
|
|
|
|
data = stringToBytes(request.responseText);
|
2017-03-28 19:08:44 +09:00
|
|
|
}
|
2017-05-06 00:55:02 +09:00
|
|
|
if (data) {
|
|
|
|
resolve({
|
|
|
|
cMapData: data,
|
|
|
|
compressionType: this.isCompressed ?
|
|
|
|
CMapCompressionType.BINARY : CMapCompressionType.NONE,
|
|
|
|
});
|
|
|
|
return;
|
2017-02-12 23:54:41 +09:00
|
|
|
}
|
2017-05-06 00:55:02 +09:00
|
|
|
}
|
|
|
|
reject(new Error('Unable to load ' +
|
|
|
|
(this.isCompressed ? 'binary ' : '') +
|
|
|
|
'CMap at: ' + url));
|
|
|
|
};
|
2017-02-12 23:54:41 +09:00
|
|
|
|
2017-05-06 00:55:02 +09:00
|
|
|
request.send(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-02-12 23:54:41 +09:00
|
|
|
|
2017-07-24 07:09:18 +09:00
|
|
|
class DOMSVGFactory {
|
|
|
|
create(width, height) {
|
|
|
|
assert(width > 0 && height > 0, 'Invalid SVG dimensions');
|
|
|
|
|
|
|
|
let svg = document.createElementNS(SVG_NS, 'svg:svg');
|
|
|
|
svg.setAttribute('version', '1.1');
|
|
|
|
svg.setAttribute('width', width + 'px');
|
|
|
|
svg.setAttribute('height', height + 'px');
|
|
|
|
svg.setAttribute('preserveAspectRatio', 'none');
|
|
|
|
svg.setAttribute('viewBox', '0 0 ' + width + ' ' + height);
|
|
|
|
|
|
|
|
return svg;
|
|
|
|
}
|
|
|
|
|
|
|
|
createElement(type) {
|
|
|
|
assert(typeof type === 'string', 'Invalid SVG element type');
|
|
|
|
|
|
|
|
return document.createElementNS(SVG_NS, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 19:36:26 +09:00
|
|
|
/**
|
|
|
|
* PDF page viewport created based on scale, rotation and offset.
|
|
|
|
* @class
|
|
|
|
* @alias PageViewport
|
|
|
|
*/
|
|
|
|
var PageViewport = (function PageViewportClosure() {
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @private
|
|
|
|
* @param viewBox {Array} xMin, yMin, xMax and yMax coordinates.
|
|
|
|
* @param scale {number} scale of the viewport.
|
|
|
|
* @param rotation {number} rotations of the viewport in degrees.
|
|
|
|
* @param offsetX {number} offset X
|
|
|
|
* @param offsetY {number} offset Y
|
|
|
|
* @param dontFlip {boolean} if true, axis Y will not be flipped.
|
|
|
|
*/
|
|
|
|
function PageViewport(viewBox, scale, rotation, offsetX, offsetY, dontFlip) {
|
|
|
|
this.viewBox = viewBox;
|
|
|
|
this.scale = scale;
|
|
|
|
this.rotation = rotation;
|
|
|
|
this.offsetX = offsetX;
|
|
|
|
this.offsetY = offsetY;
|
|
|
|
|
|
|
|
// creating transform to convert pdf coordinate system to the normal
|
|
|
|
// canvas like coordinates taking in account scale and rotation
|
|
|
|
var centerX = (viewBox[2] + viewBox[0]) / 2;
|
|
|
|
var centerY = (viewBox[3] + viewBox[1]) / 2;
|
|
|
|
var rotateA, rotateB, rotateC, rotateD;
|
|
|
|
rotation = rotation % 360;
|
|
|
|
rotation = rotation < 0 ? rotation + 360 : rotation;
|
|
|
|
switch (rotation) {
|
|
|
|
case 180:
|
|
|
|
rotateA = -1; rotateB = 0; rotateC = 0; rotateD = 1;
|
|
|
|
break;
|
|
|
|
case 90:
|
|
|
|
rotateA = 0; rotateB = 1; rotateC = 1; rotateD = 0;
|
|
|
|
break;
|
|
|
|
case 270:
|
|
|
|
rotateA = 0; rotateB = -1; rotateC = -1; rotateD = 0;
|
|
|
|
break;
|
|
|
|
// case 0:
|
|
|
|
default:
|
|
|
|
rotateA = 1; rotateB = 0; rotateC = 0; rotateD = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dontFlip) {
|
|
|
|
rotateC = -rotateC; rotateD = -rotateD;
|
|
|
|
}
|
|
|
|
|
|
|
|
var offsetCanvasX, offsetCanvasY;
|
|
|
|
var width, height;
|
|
|
|
if (rotateA === 0) {
|
|
|
|
offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
|
|
|
|
offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
|
|
|
|
width = Math.abs(viewBox[3] - viewBox[1]) * scale;
|
|
|
|
height = Math.abs(viewBox[2] - viewBox[0]) * scale;
|
|
|
|
} else {
|
|
|
|
offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
|
|
|
|
offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
|
|
|
|
width = Math.abs(viewBox[2] - viewBox[0]) * scale;
|
|
|
|
height = Math.abs(viewBox[3] - viewBox[1]) * scale;
|
|
|
|
}
|
|
|
|
// creating transform for the following operations:
|
|
|
|
// translate(-centerX, -centerY), rotate and flip vertically,
|
|
|
|
// scale, and translate(offsetCanvasX, offsetCanvasY)
|
|
|
|
this.transform = [
|
|
|
|
rotateA * scale,
|
|
|
|
rotateB * scale,
|
|
|
|
rotateC * scale,
|
|
|
|
rotateD * scale,
|
|
|
|
offsetCanvasX - rotateA * scale * centerX - rotateC * scale * centerY,
|
|
|
|
offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY
|
|
|
|
];
|
|
|
|
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
}
|
|
|
|
PageViewport.prototype = /** @lends PageViewport.prototype */ {
|
|
|
|
/**
|
|
|
|
* Clones viewport with additional properties.
|
|
|
|
* @param args {Object} (optional) If specified, may contain the 'scale' or
|
|
|
|
* 'rotation' properties to override the corresponding properties in
|
|
|
|
* the cloned viewport.
|
|
|
|
* @returns {PageViewport} Cloned viewport.
|
|
|
|
*/
|
|
|
|
clone: function PageViewPort_clone(args) {
|
|
|
|
args = args || {};
|
|
|
|
var scale = 'scale' in args ? args.scale : this.scale;
|
|
|
|
var rotation = 'rotation' in args ? args.rotation : this.rotation;
|
|
|
|
return new PageViewport(this.viewBox.slice(), scale, rotation,
|
|
|
|
this.offsetX, this.offsetY, args.dontFlip);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Converts PDF point to the viewport coordinates. For examples, useful for
|
|
|
|
* converting PDF location into canvas pixel coordinates.
|
|
|
|
* @param x {number} X coordinate.
|
|
|
|
* @param y {number} Y coordinate.
|
|
|
|
* @returns {Object} Object that contains 'x' and 'y' properties of the
|
|
|
|
* point in the viewport coordinate space.
|
|
|
|
* @see {@link convertToPdfPoint}
|
|
|
|
* @see {@link convertToViewportRectangle}
|
|
|
|
*/
|
|
|
|
convertToViewportPoint: function PageViewport_convertToViewportPoint(x, y) {
|
|
|
|
return Util.applyTransform([x, y], this.transform);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Converts PDF rectangle to the viewport coordinates.
|
|
|
|
* @param rect {Array} xMin, yMin, xMax and yMax coordinates.
|
|
|
|
* @returns {Array} Contains corresponding coordinates of the rectangle
|
|
|
|
* in the viewport coordinate space.
|
|
|
|
* @see {@link convertToViewportPoint}
|
|
|
|
*/
|
|
|
|
convertToViewportRectangle:
|
|
|
|
function PageViewport_convertToViewportRectangle(rect) {
|
|
|
|
var tl = Util.applyTransform([rect[0], rect[1]], this.transform);
|
|
|
|
var br = Util.applyTransform([rect[2], rect[3]], this.transform);
|
|
|
|
return [tl[0], tl[1], br[0], br[1]];
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Converts viewport coordinates to the PDF location. For examples, useful
|
|
|
|
* for converting canvas pixel location into PDF one.
|
|
|
|
* @param x {number} X coordinate.
|
|
|
|
* @param y {number} Y coordinate.
|
|
|
|
* @returns {Object} Object that contains 'x' and 'y' properties of the
|
|
|
|
* point in the PDF coordinate space.
|
|
|
|
* @see {@link convertToViewportPoint}
|
|
|
|
*/
|
|
|
|
convertToPdfPoint: function PageViewport_convertToPdfPoint(x, y) {
|
|
|
|
return Util.applyInverseTransform([x, y], this.transform);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return PageViewport;
|
|
|
|
})();
|
|
|
|
|
2017-03-13 21:32:23 +09:00
|
|
|
var RenderingCancelledException = (function RenderingCancelledException() {
|
|
|
|
function RenderingCancelledException(msg, type) {
|
|
|
|
this.message = msg;
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderingCancelledException.prototype = new Error();
|
|
|
|
RenderingCancelledException.prototype.name = 'RenderingCancelledException';
|
|
|
|
RenderingCancelledException.constructor = RenderingCancelledException;
|
|
|
|
|
|
|
|
return RenderingCancelledException;
|
|
|
|
})();
|
|
|
|
|
2018-02-13 22:03:52 +09:00
|
|
|
const LinkTarget = {
|
2016-03-03 09:48:21 +09:00
|
|
|
NONE: 0, // Default value.
|
|
|
|
SELF: 1,
|
|
|
|
BLANK: 2,
|
|
|
|
PARENT: 3,
|
|
|
|
TOP: 4,
|
|
|
|
};
|
|
|
|
|
2018-02-13 22:03:52 +09:00
|
|
|
const LinkTargetStringMap = [
|
2016-03-03 09:48:21 +09:00
|
|
|
'',
|
|
|
|
'_self',
|
|
|
|
'_blank',
|
|
|
|
'_parent',
|
|
|
|
'_top'
|
|
|
|
];
|
|
|
|
|
2016-03-29 04:49:22 +09:00
|
|
|
/**
|
|
|
|
* @typedef ExternalLinkParameters
|
|
|
|
* @typedef {Object} ExternalLinkParameters
|
2016-03-03 21:26:51 +09:00
|
|
|
* @property {string} url - An absolute URL.
|
2018-02-13 22:03:52 +09:00
|
|
|
* @property {LinkTarget} target - (optional) The link target.
|
|
|
|
* The default value is `LinkTarget.NONE`.
|
|
|
|
* @property {string} rel - (optional) The link relationship.
|
|
|
|
* The default value is `DEFAULT_LINK_REL`.
|
2016-03-29 04:49:22 +09:00
|
|
|
*/
|
2016-03-03 09:48:21 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds various attributes (href, title, target, rel) to hyperlinks.
|
|
|
|
* @param {HTMLLinkElement} link - The link element.
|
2016-03-03 21:26:51 +09:00
|
|
|
* @param {ExternalLinkParameters} params
|
2016-03-03 09:48:21 +09:00
|
|
|
*/
|
2018-02-13 22:03:52 +09:00
|
|
|
function addLinkAttributes(link, { url, target, rel, } = {}) {
|
2016-03-03 09:48:21 +09:00
|
|
|
link.href = link.title = (url ? removeNullCharacters(url) : '');
|
|
|
|
|
|
|
|
if (url) {
|
2018-02-13 22:03:52 +09:00
|
|
|
const LinkTargetValues = Object.values(LinkTarget);
|
|
|
|
let targetIndex =
|
|
|
|
LinkTargetValues.includes(target) ? target : LinkTarget.NONE;
|
|
|
|
link.target = LinkTargetStringMap[targetIndex];
|
2016-03-03 21:26:51 +09:00
|
|
|
|
2018-02-13 22:03:52 +09:00
|
|
|
link.rel = (typeof rel === 'string' ? rel : DEFAULT_LINK_REL);
|
2016-03-03 09:48:21 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the file name from a given URL.
|
|
|
|
function getFilenameFromUrl(url) {
|
|
|
|
var anchor = url.indexOf('#');
|
|
|
|
var query = url.indexOf('?');
|
|
|
|
var end = Math.min(
|
|
|
|
anchor > 0 ? anchor : url.length,
|
|
|
|
query > 0 ? query : url.length);
|
|
|
|
return url.substring(url.lastIndexOf('/', end) + 1, end);
|
|
|
|
}
|
2016-03-29 04:49:22 +09:00
|
|
|
|
2017-12-06 21:59:03 +09:00
|
|
|
class StatTimer {
|
|
|
|
constructor(enable = true) {
|
[api-major] Only create a `StatTimer` for pages when `enableStats == true` (issue 5215)
Unless the debugging tools (i.e. `PDFBug`) are enabled, or the `browsertest` is running, the `PDFPageProxy.stats` aren't actually used for anything.
Rather than initializing unnecessary `StatTimer` instances, we can simply re-use *one* dummy class (with static methods) for every page. Note that by using a dummy `StatTimer` in this way, rather than letting `PDFPageProxy.stats` be undefined, we don't need to guard *every* single stats collection callsite.
Since it wouldn't make much sense to attempt to use `PDFPageProxy.stats` when stat collection is disabled, it was instead changed to a "private" property (i.e. `PDFPageProxy._stats`) and a getter was added for accessing `PDFPageProxy.stats`. This getter will now return `null` when stat collection is disabled, making that case easy to handle.
For benchmarking purposes, the test-suite used to re-create the `StatTimer` after loading/rendering each page. However, modifying properties on various API code from the outside in this way seems very error-prone, and is an anti-pattern that we really should avoid at all cost. Hence the `PDFPageProxy.cleanup` method was modified to accept an optional parameter, which will take care of resetting `this.stats` when necessary, and `test/driver.js` was updated accordingly.
Finally, a tiny bit more validation was added on the viewer side, to ensure that all the code we're attempting to access is defined when handling `PDFPageProxy` stats.
2017-12-07 00:30:04 +09:00
|
|
|
this.enabled = !!enable;
|
2017-12-06 21:51:04 +09:00
|
|
|
this.started = Object.create(null);
|
|
|
|
this.times = [];
|
|
|
|
}
|
2017-12-06 21:59:03 +09:00
|
|
|
|
|
|
|
time(name) {
|
|
|
|
if (!this.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (name in this.started) {
|
|
|
|
warn('Timer is already running for ' + name);
|
|
|
|
}
|
|
|
|
this.started[name] = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
timeEnd(name) {
|
|
|
|
if (!this.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(name in this.started)) {
|
|
|
|
warn('Timer has not been started for ' + name);
|
|
|
|
}
|
|
|
|
this.times.push({
|
|
|
|
'name': name,
|
|
|
|
'start': this.started[name],
|
|
|
|
'end': Date.now(),
|
|
|
|
});
|
|
|
|
// Remove timer from started so it can be called again.
|
|
|
|
delete this.started[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
let times = this.times;
|
|
|
|
// Find the longest name for padding purposes.
|
|
|
|
let out = '', longest = 0;
|
|
|
|
for (let i = 0, ii = times.length; i < ii; ++i) {
|
|
|
|
let name = times[i]['name'];
|
|
|
|
if (name.length > longest) {
|
|
|
|
longest = name.length;
|
2017-12-06 21:51:04 +09:00
|
|
|
}
|
2017-12-06 21:59:03 +09:00
|
|
|
}
|
|
|
|
for (let i = 0, ii = times.length; i < ii; ++i) {
|
|
|
|
let span = times[i];
|
|
|
|
let duration = span.end - span.start;
|
|
|
|
out += `${span['name'].padEnd(longest)} ${duration}ms\n`;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 21:51:04 +09:00
|
|
|
|
[api-major] Only create a `StatTimer` for pages when `enableStats == true` (issue 5215)
Unless the debugging tools (i.e. `PDFBug`) are enabled, or the `browsertest` is running, the `PDFPageProxy.stats` aren't actually used for anything.
Rather than initializing unnecessary `StatTimer` instances, we can simply re-use *one* dummy class (with static methods) for every page. Note that by using a dummy `StatTimer` in this way, rather than letting `PDFPageProxy.stats` be undefined, we don't need to guard *every* single stats collection callsite.
Since it wouldn't make much sense to attempt to use `PDFPageProxy.stats` when stat collection is disabled, it was instead changed to a "private" property (i.e. `PDFPageProxy._stats`) and a getter was added for accessing `PDFPageProxy.stats`. This getter will now return `null` when stat collection is disabled, making that case easy to handle.
For benchmarking purposes, the test-suite used to re-create the `StatTimer` after loading/rendering each page. However, modifying properties on various API code from the outside in this way seems very error-prone, and is an anti-pattern that we really should avoid at all cost. Hence the `PDFPageProxy.cleanup` method was modified to accept an optional parameter, which will take care of resetting `this.stats` when necessary, and `test/driver.js` was updated accordingly.
Finally, a tiny bit more validation was added on the viewer side, to ensure that all the code we're attempting to access is defined when handling `PDFPageProxy` stats.
2017-12-07 00:30:04 +09:00
|
|
|
/**
|
|
|
|
* Helps avoid having to initialize {StatTimer} instances, e.g. one for every
|
|
|
|
* page, in cases where the collected stats are not actually being used.
|
|
|
|
* This (dummy) class can thus, since all its methods are `static`, be directly
|
|
|
|
* shared between multiple call-sites without the need to be initialized first.
|
|
|
|
*
|
|
|
|
* NOTE: This must implement the same interface as {StatTimer}.
|
|
|
|
*/
|
|
|
|
class DummyStatTimer {
|
|
|
|
constructor() {
|
2017-12-13 22:51:45 +09:00
|
|
|
unreachable('Cannot initialize DummyStatTimer.');
|
[api-major] Only create a `StatTimer` for pages when `enableStats == true` (issue 5215)
Unless the debugging tools (i.e. `PDFBug`) are enabled, or the `browsertest` is running, the `PDFPageProxy.stats` aren't actually used for anything.
Rather than initializing unnecessary `StatTimer` instances, we can simply re-use *one* dummy class (with static methods) for every page. Note that by using a dummy `StatTimer` in this way, rather than letting `PDFPageProxy.stats` be undefined, we don't need to guard *every* single stats collection callsite.
Since it wouldn't make much sense to attempt to use `PDFPageProxy.stats` when stat collection is disabled, it was instead changed to a "private" property (i.e. `PDFPageProxy._stats`) and a getter was added for accessing `PDFPageProxy.stats`. This getter will now return `null` when stat collection is disabled, making that case easy to handle.
For benchmarking purposes, the test-suite used to re-create the `StatTimer` after loading/rendering each page. However, modifying properties on various API code from the outside in this way seems very error-prone, and is an anti-pattern that we really should avoid at all cost. Hence the `PDFPageProxy.cleanup` method was modified to accept an optional parameter, which will take care of resetting `this.stats` when necessary, and `test/driver.js` was updated accordingly.
Finally, a tiny bit more validation was added on the viewer side, to ensure that all the code we're attempting to access is defined when handling `PDFPageProxy` stats.
2017-12-07 00:30:04 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
static time(name) {}
|
|
|
|
|
|
|
|
static timeEnd(name) {}
|
|
|
|
|
|
|
|
static toString() {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-02 21:25:33 +09:00
|
|
|
export {
|
2018-06-04 19:36:26 +09:00
|
|
|
PageViewport,
|
2017-04-02 21:25:33 +09:00
|
|
|
RenderingCancelledException,
|
|
|
|
addLinkAttributes,
|
|
|
|
getFilenameFromUrl,
|
|
|
|
LinkTarget,
|
|
|
|
DEFAULT_LINK_REL,
|
|
|
|
DOMCanvasFactory,
|
|
|
|
DOMCMapReaderFactory,
|
2017-07-24 07:09:18 +09:00
|
|
|
DOMSVGFactory,
|
2017-12-06 21:51:04 +09:00
|
|
|
StatTimer,
|
[api-major] Only create a `StatTimer` for pages when `enableStats == true` (issue 5215)
Unless the debugging tools (i.e. `PDFBug`) are enabled, or the `browsertest` is running, the `PDFPageProxy.stats` aren't actually used for anything.
Rather than initializing unnecessary `StatTimer` instances, we can simply re-use *one* dummy class (with static methods) for every page. Note that by using a dummy `StatTimer` in this way, rather than letting `PDFPageProxy.stats` be undefined, we don't need to guard *every* single stats collection callsite.
Since it wouldn't make much sense to attempt to use `PDFPageProxy.stats` when stat collection is disabled, it was instead changed to a "private" property (i.e. `PDFPageProxy._stats`) and a getter was added for accessing `PDFPageProxy.stats`. This getter will now return `null` when stat collection is disabled, making that case easy to handle.
For benchmarking purposes, the test-suite used to re-create the `StatTimer` after loading/rendering each page. However, modifying properties on various API code from the outside in this way seems very error-prone, and is an anti-pattern that we really should avoid at all cost. Hence the `PDFPageProxy.cleanup` method was modified to accept an optional parameter, which will take care of resetting `this.stats` when necessary, and `test/driver.js` was updated accordingly.
Finally, a tiny bit more validation was added on the viewer side, to ensure that all the code we're attempting to access is defined when handling `PDFPageProxy` stats.
2017-12-07 00:30:04 +09:00
|
|
|
DummyStatTimer,
|
2017-04-02 21:25:33 +09:00
|
|
|
};
|