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-10-15 20:27:10 +09:00
|
|
|
assert, CMapCompressionType, removeNullCharacters, stringToBytes, warn
|
2017-04-02 21:25:33 +09:00
|
|
|
} from '../shared/util';
|
2017-08-23 07:06:11 +09:00
|
|
|
import globalScope from '../shared/global_scope';
|
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) {
|
|
|
|
return Promise.reject(new Error('CMap baseUrl must be specified, ' +
|
|
|
|
'see "PDFJS.cMapUrl" (and also "PDFJS.cMapPacked").'));
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 06:37:51 +09:00
|
|
|
class SimpleDOMNode {
|
|
|
|
constructor(nodeName, nodeValue) {
|
|
|
|
this.nodeName = nodeName;
|
|
|
|
this.nodeValue = nodeValue;
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'parentNode', { value: null, writable: true, });
|
|
|
|
}
|
|
|
|
|
|
|
|
get firstChild() {
|
|
|
|
return this.childNodes[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
get nextSibling() {
|
|
|
|
let index = this.parentNode.childNodes.indexOf(this);
|
|
|
|
return this.parentNode.childNodes[index + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
get textContent() {
|
|
|
|
if (!this.childNodes) {
|
|
|
|
return this.nodeValue || '';
|
|
|
|
}
|
|
|
|
return this.childNodes.map(function(child) {
|
|
|
|
return child.textContent;
|
|
|
|
}).join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
hasChildNodes() {
|
|
|
|
return this.childNodes && this.childNodes.length > 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SimpleXMLParser {
|
|
|
|
parseFromString(data) {
|
|
|
|
let nodes = [];
|
|
|
|
|
|
|
|
// Remove all comments and processing instructions.
|
|
|
|
data = data.replace(/<\?[\s\S]*?\?>|<!--[\s\S]*?-->/g, '').trim();
|
|
|
|
data = data.replace(/<!DOCTYPE[^>\[]+(\[[^\]]+)?[^>]+>/g, '').trim();
|
|
|
|
|
|
|
|
// Extract all text nodes and replace them with a numeric index in
|
|
|
|
// the nodes.
|
|
|
|
data = data.replace(/>([^<][\s\S]*?)</g, (all, text) => {
|
|
|
|
let length = nodes.length;
|
|
|
|
let node = new SimpleDOMNode('#text', this._decodeXML(text));
|
|
|
|
nodes.push(node);
|
|
|
|
if (node.textContent.trim().length === 0) {
|
|
|
|
return '><'; // Ignore whitespace.
|
|
|
|
}
|
|
|
|
return '>' + length + ',<';
|
|
|
|
});
|
|
|
|
|
|
|
|
// Extract all CDATA nodes.
|
|
|
|
data = data.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g,
|
|
|
|
function(all, text) {
|
|
|
|
let length = nodes.length;
|
|
|
|
let node = new SimpleDOMNode('#text', text);
|
|
|
|
nodes.push(node);
|
|
|
|
return length + ',';
|
|
|
|
});
|
|
|
|
|
|
|
|
// Until nodes without '<' and '>' content are present, replace them
|
|
|
|
// with a numeric index in the nodes.
|
|
|
|
let regex =
|
|
|
|
/<([\w\:]+)((?:[\s\w:=]|'[^']*'|"[^"]*")*)(?:\/>|>([\d,]*)<\/[^>]+>)/g;
|
|
|
|
let lastLength;
|
|
|
|
do {
|
|
|
|
lastLength = nodes.length;
|
|
|
|
data = data.replace(regex, function(all, name, attrs, data) {
|
|
|
|
let length = nodes.length;
|
|
|
|
let node = new SimpleDOMNode(name);
|
|
|
|
let children = [];
|
|
|
|
if (data) {
|
|
|
|
data = data.split(',');
|
|
|
|
data.pop();
|
|
|
|
data.forEach(function(child) {
|
|
|
|
let childNode = nodes[+child];
|
|
|
|
childNode.parentNode = node;
|
|
|
|
children.push(childNode);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
node.childNodes = children;
|
|
|
|
nodes.push(node);
|
|
|
|
return length + ',';
|
|
|
|
});
|
|
|
|
} while (lastLength < nodes.length);
|
|
|
|
|
|
|
|
// We should only have one root index left, which will be last in the nodes.
|
|
|
|
return {
|
|
|
|
documentElement: nodes.pop(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_decodeXML(text) {
|
|
|
|
if (text.indexOf('&') < 0) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return text.replace(/&(#(x[0-9a-f]+|\d+)|\w+);/gi,
|
|
|
|
function(all, entityName, number) {
|
|
|
|
if (number) {
|
|
|
|
if (number[0] === 'x') {
|
|
|
|
number = parseInt(number.substring(1), 16);
|
|
|
|
} else {
|
|
|
|
number = +number;
|
|
|
|
}
|
|
|
|
return String.fromCharCode(number);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (entityName) {
|
|
|
|
case 'amp':
|
|
|
|
return '&';
|
|
|
|
case 'lt':
|
|
|
|
return '<';
|
|
|
|
case 'gt':
|
|
|
|
return '>';
|
|
|
|
case 'quot':
|
|
|
|
return '\"';
|
|
|
|
case 'apos':
|
|
|
|
return '\'';
|
|
|
|
}
|
|
|
|
return '&' + entityName + ';';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-10 10:24:15 +09:00
|
|
|
/**
|
|
|
|
* Optimised CSS custom property getter/setter.
|
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
var CustomStyle = (function CustomStyleClosure() {
|
|
|
|
|
|
|
|
// As noted on: http://www.zachstronaut.com/posts/2009/02/17/
|
|
|
|
// animate-css-transforms-firefox-webkit.html
|
|
|
|
// in some versions of IE9 it is critical that ms appear in this list
|
|
|
|
// before Moz
|
|
|
|
var prefixes = ['ms', 'Moz', 'Webkit', 'O'];
|
2016-01-28 02:04:13 +09:00
|
|
|
var _cache = Object.create(null);
|
2015-11-10 10:24:15 +09:00
|
|
|
|
|
|
|
function CustomStyle() {}
|
|
|
|
|
|
|
|
CustomStyle.getProp = function get(propName, element) {
|
|
|
|
// check cache only when no element is given
|
|
|
|
if (arguments.length === 1 && typeof _cache[propName] === 'string') {
|
|
|
|
return _cache[propName];
|
|
|
|
}
|
|
|
|
|
|
|
|
element = element || document.documentElement;
|
|
|
|
var style = element.style, prefixed, uPropName;
|
|
|
|
|
|
|
|
// test standard property first
|
|
|
|
if (typeof style[propName] === 'string') {
|
|
|
|
return (_cache[propName] = propName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// capitalize
|
|
|
|
uPropName = propName.charAt(0).toUpperCase() + propName.slice(1);
|
|
|
|
|
|
|
|
// test vendor specific properties
|
|
|
|
for (var i = 0, l = prefixes.length; i < l; i++) {
|
|
|
|
prefixed = prefixes[i] + uPropName;
|
|
|
|
if (typeof style[prefixed] === 'string') {
|
|
|
|
return (_cache[propName] = prefixed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 00:26:32 +09:00
|
|
|
// If all fails then set to undefined.
|
2015-11-10 10:24:15 +09:00
|
|
|
return (_cache[propName] = 'undefined');
|
|
|
|
};
|
|
|
|
|
|
|
|
CustomStyle.setProp = function set(propName, element, str) {
|
|
|
|
var prop = this.getProp(propName);
|
|
|
|
if (prop !== 'undefined') {
|
|
|
|
element.style[prop] = str;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return CustomStyle;
|
|
|
|
})();
|
|
|
|
|
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;
|
|
|
|
})();
|
|
|
|
|
2016-03-03 09:48:21 +09:00
|
|
|
var LinkTarget = {
|
|
|
|
NONE: 0, // Default value.
|
|
|
|
SELF: 1,
|
|
|
|
BLANK: 2,
|
|
|
|
PARENT: 3,
|
|
|
|
TOP: 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
var LinkTargetStringMap = [
|
|
|
|
'',
|
|
|
|
'_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.
|
|
|
|
* @property {LinkTarget} target - The link target.
|
|
|
|
* @property {string} rel - The link relationship.
|
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
|
|
|
*/
|
|
|
|
function addLinkAttributes(link, params) {
|
|
|
|
var url = params && params.url;
|
|
|
|
link.href = link.title = (url ? removeNullCharacters(url) : '');
|
|
|
|
|
|
|
|
if (url) {
|
2016-03-29 04:49:22 +09:00
|
|
|
var target = params.target;
|
|
|
|
if (typeof target === 'undefined') {
|
|
|
|
target = getDefaultSetting('externalLinkTarget');
|
2016-03-03 09:48:21 +09:00
|
|
|
}
|
2016-03-29 04:49:22 +09:00
|
|
|
link.target = LinkTargetStringMap[target];
|
2016-03-03 21:26:51 +09:00
|
|
|
|
2016-03-29 04:49:22 +09:00
|
|
|
var rel = params.rel;
|
|
|
|
if (typeof rel === 'undefined') {
|
|
|
|
rel = getDefaultSetting('externalLinkRel');
|
|
|
|
}
|
|
|
|
link.rel = 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
|
|
|
|
|
|
|
function getDefaultSetting(id) {
|
|
|
|
// The list of the settings and their default is maintained for backward
|
|
|
|
// compatibility and shall not be extended or modified. See also global.js.
|
2017-04-02 21:25:33 +09:00
|
|
|
var globalSettings = globalScope.PDFJS;
|
2016-03-29 04:49:22 +09:00
|
|
|
switch (id) {
|
|
|
|
case 'pdfBug':
|
|
|
|
return globalSettings ? globalSettings.pdfBug : false;
|
|
|
|
case 'disableAutoFetch':
|
|
|
|
return globalSettings ? globalSettings.disableAutoFetch : false;
|
|
|
|
case 'disableStream':
|
|
|
|
return globalSettings ? globalSettings.disableStream : false;
|
|
|
|
case 'disableRange':
|
|
|
|
return globalSettings ? globalSettings.disableRange : false;
|
|
|
|
case 'disableFontFace':
|
|
|
|
return globalSettings ? globalSettings.disableFontFace : false;
|
|
|
|
case 'disableCreateObjectURL':
|
|
|
|
return globalSettings ? globalSettings.disableCreateObjectURL : false;
|
|
|
|
case 'disableWebGL':
|
|
|
|
return globalSettings ? globalSettings.disableWebGL : true;
|
|
|
|
case 'cMapUrl':
|
|
|
|
return globalSettings ? globalSettings.cMapUrl : null;
|
|
|
|
case 'cMapPacked':
|
|
|
|
return globalSettings ? globalSettings.cMapPacked : false;
|
|
|
|
case 'postMessageTransfers':
|
|
|
|
return globalSettings ? globalSettings.postMessageTransfers : true;
|
2017-02-25 04:33:18 +09:00
|
|
|
case 'workerPort':
|
|
|
|
return globalSettings ? globalSettings.workerPort : null;
|
2016-03-29 04:49:22 +09:00
|
|
|
case 'workerSrc':
|
|
|
|
return globalSettings ? globalSettings.workerSrc : null;
|
|
|
|
case 'disableWorker':
|
|
|
|
return globalSettings ? globalSettings.disableWorker : false;
|
|
|
|
case 'maxImageSize':
|
|
|
|
return globalSettings ? globalSettings.maxImageSize : -1;
|
|
|
|
case 'imageResourcesPath':
|
|
|
|
return globalSettings ? globalSettings.imageResourcesPath : '';
|
|
|
|
case 'isEvalSupported':
|
|
|
|
return globalSettings ? globalSettings.isEvalSupported : true;
|
|
|
|
case 'externalLinkTarget':
|
|
|
|
if (!globalSettings) {
|
|
|
|
return LinkTarget.NONE;
|
|
|
|
}
|
|
|
|
switch (globalSettings.externalLinkTarget) {
|
|
|
|
case LinkTarget.NONE:
|
|
|
|
case LinkTarget.SELF:
|
|
|
|
case LinkTarget.BLANK:
|
|
|
|
case LinkTarget.PARENT:
|
|
|
|
case LinkTarget.TOP:
|
|
|
|
return globalSettings.externalLinkTarget;
|
|
|
|
}
|
|
|
|
warn('PDFJS.externalLinkTarget is invalid: ' +
|
|
|
|
globalSettings.externalLinkTarget);
|
|
|
|
// Reset the external link target, to suppress further warnings.
|
|
|
|
globalSettings.externalLinkTarget = LinkTarget.NONE;
|
|
|
|
return LinkTarget.NONE;
|
|
|
|
case 'externalLinkRel':
|
2017-01-24 01:34:27 +09:00
|
|
|
return globalSettings ? globalSettings.externalLinkRel : DEFAULT_LINK_REL;
|
2016-03-29 04:49:22 +09:00
|
|
|
case 'enableStats':
|
|
|
|
return !!(globalSettings && globalSettings.enableStats);
|
|
|
|
default:
|
|
|
|
throw new Error('Unknown default setting: ' + id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isExternalLinkTargetSet() {
|
|
|
|
var externalLinkTarget = getDefaultSetting('externalLinkTarget');
|
|
|
|
switch (externalLinkTarget) {
|
|
|
|
case LinkTarget.NONE:
|
|
|
|
return false;
|
|
|
|
case LinkTarget.SELF:
|
|
|
|
case LinkTarget.BLANK:
|
|
|
|
case LinkTarget.PARENT:
|
|
|
|
case LinkTarget.TOP:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-03-03 09:48:21 +09:00
|
|
|
|
2017-12-06 21:59:03 +09:00
|
|
|
class StatTimer {
|
|
|
|
constructor(enable = true) {
|
2017-12-06 21:51:04 +09:00
|
|
|
this.started = Object.create(null);
|
|
|
|
this.times = [];
|
2017-12-06 21:59:03 +09:00
|
|
|
this.enabled = !!enable;
|
2017-12-06 21:51:04 +09:00
|
|
|
}
|
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
|
|
|
|
2017-04-02 21:25:33 +09:00
|
|
|
export {
|
|
|
|
CustomStyle,
|
|
|
|
RenderingCancelledException,
|
|
|
|
addLinkAttributes,
|
|
|
|
isExternalLinkTargetSet,
|
|
|
|
getFilenameFromUrl,
|
|
|
|
LinkTarget,
|
|
|
|
getDefaultSetting,
|
|
|
|
DEFAULT_LINK_REL,
|
|
|
|
DOMCanvasFactory,
|
|
|
|
DOMCMapReaderFactory,
|
2017-07-24 07:09:18 +09:00
|
|
|
DOMSVGFactory,
|
2017-09-14 06:37:51 +09:00
|
|
|
SimpleXMLParser,
|
2017-12-06 21:51:04 +09:00
|
|
|
StatTimer,
|
2017-04-02 21:25:33 +09:00
|
|
|
};
|