* 'master' of https://github.com/paperjs/paper.js:
  Fix: #initialize() to correctly convert #getValue() array back to Curve object.
  Handle allowNull correctly again in SVGImport.
  Simplify SVGImport attribute parsing and correctly handle failing tests with invalid coordinates.
  Further refine Node.js integration.
  Improve the Node version of Base.isPlainObject()
This commit is contained in:
hkrish 2013-05-11 14:42:56 +02:00
commit 158001c154
7 changed files with 51 additions and 46 deletions

View file

@ -332,7 +332,7 @@ var Base = new function() { // Bootstrap scope
*/
isPlainObject: function(obj) {
var proto = obj !== null && typeof obj === 'object'
&& Object.getPrototypeOf(obj);
&& Object.getPrototypeOf(obj);
return proto && (proto === Object.prototype
|| proto === Base.prototype);
},

View file

@ -20,5 +20,5 @@ Base.each(this, function(val, key) {
// See paper.js for the non-dev version of this code. We cannot handle dev there
// due to the seperate loading of all source files, which are only availabe
// after the execution of paper.js
paper = new PaperScope();
var paper = new PaperScope();
/*#*/ } // options.version == 'dev'

View file

@ -87,28 +87,29 @@ var context = vm.createContext({
// Load Paper.js library files:
context.include('paper.js');
// Fix version now. Remove 2nd dot, so we can make a float out of it:
options.version = parseFloat(json.version.replace(/(.)(\d)$/, '$2'));
// Since the created context fo Paper.js compilation, and the context in which
// Node.js scripts are executed do not share the definition of Object and Array,
// we need to redefine Base.isPlainObject() here.
// Object(obj) === obj is a trick from underscore, but also returns true for all
// Base objects. So we are filtering these out with an instanceof check, but
// Include Base instances since we're using them as hashes.
// Since the context used for Paper.js compilation, and the context in which
// Node.js scripts are executed do not share the definition of Object, we need
// to redefine Base.isPlainObject() here.
// So instead of checking for Object.prototype, we're checking
// proto.constructor.name for 'Object'
// TODO: Benchmark the speed and consider this implementation instead of the
// current one.
// current one in straps.js too
var Base = context.Base;
Base.isPlainObject = function(obj) {
return Object(obj) === obj && !Array.isArray(obj) && (!(obj instanceof Base)
|| Object.getPrototypeOf(obj) === Base.prototype);
var proto = obj !== null && typeof obj === 'object'
&& Object.getPrototypeOf(obj);
return proto && (proto.constructor.name === 'Object'
|| proto === Base.prototype);
};
// Expose the Canvas, XMLSerializer to paper scopes:
Base.each({
Canvas: Canvas,
XMLSerializer: XMLSerializer,
DOMParser: DOMParser
DOMParser: DOMParser,
// Also fix version. Remove 2nd dot, so we can make a float out of it:
version: parseFloat(json.version.replace(/(.)(\d)$/, '$2'))
}, function(value, key) {
this[key] = value;
}, context.PaperScope.prototype);
@ -124,4 +125,4 @@ require.extensions['.pjs'] = function(module, uri) {
module.exports = scope;
};
module.exports = new context.PaperScope();
module.exports = context.paper;

View file

@ -127,7 +127,9 @@ var paper = new function() {
/*#*/ include('core/PaperScript.js');
/*#*/ include('core/initialize.js');
/*#*/ if (options.version != 'dev') {
/*#*/ if (options.server) {
return paper;
/*#*/ } else if (options.version != 'dev') {
// Finally inject the classes set on 'this' into the PaperScope class and create
// the first PaperScope and return it, all in one statement.
// The version for 'dev' of this happens in core/initialize.js, since it depends

View file

@ -79,7 +79,7 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{
// can create segments for those.
point1 = [arg0, arg1];
point2 = [arg6, arg7];
handle1 = [arg2 - arg0, arg7 - arg1];
handle1 = [arg2 - arg0, arg3 - arg1];
handle2 = [arg4 - arg6, arg5 - arg7];
}
this._segment1 = new Segment(point1, null, handle1);

View file

@ -489,8 +489,8 @@ new function() {
/**
* {@grouptitle SVG Conversion}
*
* Exports the item and all its child items as an SVG DOM, all contained
* in one top level SVG group node.
* Exports the item and all its children as an SVG DOM, all contained
* in one top level SVG node.
*
* @return {SVGSVGElement} the item converted to an SVG node
*/

View file

@ -19,35 +19,37 @@ new function() {
// objects, dealing with baseVal, and item lists.
// index is option, and if passed, causes a lookup in a list.
function getValue(node, key, allowNull, index) {
var namespace = SVGNamespaces[key];
var value = namespace
? node.getAttributeNS(namespace, key)
: node.getAttribute(key);
// Note: String values are unfortunately not stored in base.value, but
// in base directly, so we need to check both, also on item lists, using
// Base.pick(base.value, base)
if (index != null && value != null) {
var values = value.split(',');
value = values[index];
if (value == null)
value = values[values.length - 1];
}
if (/^[\d.+-]/.test(value))
value = parseFloat(value);
return value;
function getValue(node, name, isString, allowNull) {
var namespace = SVGNamespaces[name],
value = namespace
? node.getAttributeNS(namespace, name)
: node.getAttribute(name);
if (value == 'null')
value = null;
// Interpret value as number. Never return NaN, but 0 instead.
// If the value is a sequence of numbers, parseFloat will
// return the first occuring number, which is enough for now.
return value == null
? allowNull
? null
: isString
? ''
: 0
: isString
? value
: parseFloat(value);
}
function getPoint(node, x, y, allowNull, index) {
x = getValue(node, x, allowNull, index);
y = getValue(node, y, allowNull, index);
function getPoint(node, x, y, allowNull) {
x = getValue(node, x, false, allowNull);
y = getValue(node, y, false, allowNull);
return allowNull && x == null && y == null ? null
: Point.create(x || 0, y || 0);
}
function getSize(node, w, h, allowNull, index) {
w = getValue(node, w, allowNull, index);
h = getValue(node, h, allowNull, index);
function getSize(node, w, h, allowNull) {
w = getValue(node, w, false, allowNull);
h = getValue(node, h, false, allowNull);
return allowNull && w == null && h == null ? null
: Size.create(w || 0, h || 0);
}
@ -181,7 +183,7 @@ new function() {
// http://www.w3.org/TR/SVG/struct.html#ImageElement
image: function (node) {
var raster = new Raster(getValue(node, 'href'));
var raster = new Raster(getValue(node, 'href', true));
raster.attach('load', function() {
var size = getSize(node, 'width', 'height');
this.setSize(size);
@ -208,7 +210,7 @@ new function() {
// TODO: Support overflow and width, height, in combination with
// overflow: hidden. Paper.js currently does not suport PlacedSymbol
// clipping, but perhaps it should?
var id = (getValue(node, 'href') || '').substring(1),
var id = (getValue(node, 'href', true) || '').substring(1),
definition = definitions[id],
point = getPoint(node, 'x', 'y');
// Use place if we're dealing with a symbol:
@ -258,8 +260,8 @@ new function() {
// TODO: Support for these is missing in Paper.js right now
// rotate: character rotation
// lengthAdjust:
var text = new PointText(getPoint(node, 'x', 'y', false, 0)
.add(getPoint(node, 'dx', 'dy', false, 0)));
var text = new PointText(getPoint(node, 'x', 'y', false)
.add(getPoint(node, 'dx', 'dy', false)));
text.setContent(node.textContent.trim() || '');
return text;
}