diff --git a/dist/docs/assets/js/paper.js b/dist/docs/assets/js/paper.js index a28fb954..57aeaf0b 100644 --- a/dist/docs/assets/js/paper.js +++ b/dist/docs/assets/js/paper.js @@ -9,7 +9,7 @@ * * All rights reserved. * - * Date: Sun Mar 19 14:50:59 2017 +0100 + * Date: Sun Mar 19 16:03:29 2017 +0100 * *** * @@ -32,8 +32,7 @@ var paper = function(self, undefined) { -self = self || require('./node/window.js'); - +self = self || require('./node/self.js'); var window = self.window, document = self.document; diff --git a/dist/node/canvas.js b/dist/node/canvas.js index df4e8880..420e5c81 100644 --- a/dist/node/canvas.js +++ b/dist/node/canvas.js @@ -15,7 +15,7 @@ // - Various Node Canvas methods, routed through from HTMLCanvasElement: // toBuffer, pngStream, createPNGStream, jpgStream, createJPGStream -module.exports = function(window) { +module.exports = function(self) { var Canvas; try { Canvas = require('canvas'); @@ -25,14 +25,14 @@ module.exports = function(window) { // - On the browser, this corresponds to a worker context. // - On Node.js, it basically means the canvas is missing or not working // which can be treated the same way. - delete window.window; + delete self.window; console.info( - 'Unable to load Canvas module. Running in a headless context.'); + 'Canvas module not found, running in a headless context.'); return; } var idlUtils = require('jsdom/lib/jsdom/living/generated/utils'), - HTMLCanvasElement = window.HTMLCanvasElement; + HTMLCanvasElement = self.HTMLCanvasElement; // Add fake HTMLCanvasElement#type property: Object.defineProperty(HTMLCanvasElement.prototype, 'type', { diff --git a/dist/node/extend.js b/dist/node/extend.js index 831f93a6..1b342e7f 100644 --- a/dist/node/extend.js +++ b/dist/node/extend.js @@ -57,8 +57,8 @@ module.exports = function(paper) { paper.PaperScope.inject({ createCanvas: function(width, height, type) { // Do not use CanvasProvider.getCanvas(), since we may be changing - // the underlying node-canvas and don't want to release it after - // back into the pool. + // the underlying node-canvas when requesting PDF support, and don't + // want to release it after back into the pool. var canvas = paper.document.createElement('canvas'); canvas.width = width; canvas.height = height; diff --git a/dist/node/self.js b/dist/node/self.js new file mode 100644 index 00000000..0fa37c2c --- /dev/null +++ b/dist/node/self.js @@ -0,0 +1,73 @@ +/* + * Paper.js - The Swiss Army Knife of Vector Graphics Scripting. + * http://paperjs.org/ + * + * Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey + * http://scratchdisk.com/ & http://jonathanpuckey.com/ + * + * Distributed under the MIT license. See LICENSE file for details. + * + * All rights reserved. + */ + +// Node.js emulation layer of browser environment, based on jsdom with node- +// canvas integration. + +var self; + +try { + var jsdom = require('jsdom'); + + // Create our document and window objects through jsdom. + /* global document:true, window:true */ + var document = jsdom.jsdom('
', { + // Use the current working directory as the document's origin, so + // requests to local files work correctly with CORS. + url: 'file://' + process.cwd() + '/', + features: { + FetchExternalResources: ['img', 'script'] + } + }); + self = document.defaultView; + + require('./canvas.js')(self); + + // Define XMLSerializer shim, to emulate browser behavior. + // Effort to bring XMLSerializer to jsdom: + // https://github.com/tmpvar/jsdom/issues/1368 + /*jshint -W082 */ + function XMLSerializer() { + } + + XMLSerializer.prototype.serializeToString = function(node) { + if (!node) + return ''; + // Fix a jsdom issue where all SVG tagNames are lowercased: + // https://github.com/tmpvar/jsdom/issues/620 + var text = node.outerHTML, + tagNames = ['linearGradient', 'radialGradient', 'clipPath', + 'textPath']; + for (var i = 0, l = tagNames.length; i < l; i++) { + var tagName = tagNames[i]; + text = text.replace( + new RegExp('(<|)' + tagName.toLowerCase() + '\\b', 'g'), + function(match, start) { + return start + tagName; + }); + } + return text; + }; + + self.XMLSerializer = XMLSerializer; +} catch(e) { + console.info( + 'JSDom module not found, running in a headless context without DOM.'); + self = { + navigator: { + userAgent: 'Node.js (' + process.platform + '; U; rv:' + + process.version + ')' + } + }; +} + +module.exports = self; diff --git a/dist/node/window.js b/dist/node/window.js deleted file mode 100644 index 8dcd4e20..00000000 --- a/dist/node/window.js +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Paper.js - The Swiss Army Knife of Vector Graphics Scripting. - * http://paperjs.org/ - * - * Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey - * http://scratchdisk.com/ & http://jonathanpuckey.com/ - * - * Distributed under the MIT license. See LICENSE file for details. - * - * All rights reserved. - */ - -// Node.js emulation layer of browser environment, based on jsdom with node- -// canvas integration. - -var jsdom = require('jsdom'); - -// Create our document and window objects through jsdom. -/* global document:true, window:true */ -var document = jsdom.jsdom('', { - // Use the current working directory as the document's origin, so - // requests to local files work correctly with CORS. - url: 'file://' + process.cwd() + '/', - features: { - FetchExternalResources: ['img', 'script'] - } - }), - window = document.defaultView; - -require('./canvas.js')(window); - -// Define XMLSerializer and DOMParser shims, to emulate browser behavior. -// Effort to bring this to jsdom: https://github.com/tmpvar/jsdom/issues/1368 -function XMLSerializer() { -} - -XMLSerializer.prototype.serializeToString = function(node) { - if (!node) - return ''; - var text = node.outerHTML; - // Fix a jsdom issue where all SVG tagNames are lowercased: - // https://github.com/tmpvar/jsdom/issues/620 - var tagNames = ['linearGradient', 'radialGradient', 'clipPath', 'textPath']; - for (var i = 0, l = tagNames.length; i < l; i++) { - var tagName = tagNames[i]; - text = text.replace( - new RegExp('(<|)' + tagName.toLowerCase() + '\\b', 'g'), - function(match, start) { - return start + tagName; - }); - } - return text; -}; - -window.XMLSerializer = XMLSerializer; - -module.exports = window; diff --git a/dist/paper-core.js b/dist/paper-core.js index c1a90eed..ff737ecf 100644 --- a/dist/paper-core.js +++ b/dist/paper-core.js @@ -9,7 +9,7 @@ * * All rights reserved. * - * Date: Sun Mar 19 14:50:59 2017 +0100 + * Date: Sun Mar 19 16:03:29 2017 +0100 * *** * @@ -32,8 +32,7 @@ var paper = function(self, undefined) { -self = self || require('./node/window.js'); - +self = self || require('./node/self.js'); var window = self.window, document = self.document; diff --git a/dist/paper-core.min.js b/dist/paper-core.min.js index 6d3e63b4..9032ef7e 100644 --- a/dist/paper-core.min.js +++ b/dist/paper-core.min.js @@ -9,7 +9,7 @@ * * All rights reserved. * - * Date: Sun Mar 19 14:50:59 2017 +0100 + * Date: Sun Mar 19 16:03:29 2017 +0100 * *** * @@ -29,7 +29,7 @@ * created by Marijn Haverbeke and released under an MIT license. * */ -var paper=function(t,e){t=t||require("./node/window.js");var i=t.window,n=t.document,r=new function(){function t(t,e,r,s,a){function u(n,u){u=u||(u=o(e,n))&&(u.get?u:u.value),"string"==typeof u&&"#"===u[0]&&(u=t[u.substring(1)]||u);var c,f="function"==typeof u,d=u,_=a||f&&!u.base?u&&u.get?n in t:t[n]:null;a&&_||(f&&_&&(u.base=_),f&&s!==!1&&(c=n.match(/^([gs]et|is)(([A-Z])(.*))$/))&&(l[c[3].toLowerCase()+c[4]]=c[2]),d&&!f&&d.get&&"function"==typeof d.get&&i.isPlainObject(d)||(d={value:d,writable:!0}),(o(t,n)||{configurable:!0}).configurable&&(d.configurable=!0,d.enumerable=r),h(t,n,d))}var l={};if(e){for(var c in e)e.hasOwnProperty(c)&&!n.test(c)&&u(c);for(var c in l){var f=l[c],d=t["set"+f],_=t["get"+f]||d&&t["is"+f];!_||s!==!0&&0!==_.length||u(c,{get:_,set:d})}}return t}function i(){for(var t=0,e=arguments.length;t