forked from paperjs/paper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase.js
More file actions
517 lines (490 loc) · 16.7 KB
/
Copy pathBase.js
File metadata and controls
517 lines (490 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
/**
* @name Base
* @class
* @private
*/
// Extend Base with utility functions used across the library. Also set
// this.Base on the injection scope, since straps.js ommits that.
Base.inject(/** @lends Base# */{
// Have generics versions of #clone() and #toString():
generics: true,
/**
* General purpose clone function that delegates cloning to the constructor
* that receives the object to be cloned as the first argument.
* Note: #clone() needs to be overridden in any class that requires other
* cloning behavior.
*/
clone: function() {
return new this.constructor(this);
},
/**
* Renders base objects to strings in object literal notation.
*/
toString: function() {
return this._id != null
? (this._class || 'Object') + (this._name
? " '" + this._name + "'"
: ' @' + this._id)
: '{ ' + Base.each(this, function(value, key) {
// Hide internal properties even if they are enumerable
if (!/^_/.test(key)) {
var type = typeof value;
this.push(key + ': ' + (type === 'number'
? Formatter.instance.number(value)
: type === 'string' ? "'" + value + "'" : value));
}
}, []).join(', ') + ' }';
},
/**
* Serializes this object to a JSON string.
*
* @param {Object} [options={ precision: 5 }]
*/
exportJSON: function(options) {
return Base.exportJSON(this, options);
},
// To support JSON.stringify:
toJSON: function() {
return Base.serialize(this);
},
/**
* #_set() is part of the mechanism for constructors which take one object
* literal describing all the properties to be set on the created instance.
*
* @param {Object} props an object describing the properties to set
* @param {Object} [exclude=undefined] a lookup table listing properties to
* exclude.
* @return {Boolean} {@true if the object is a plain object}
*/
_set: function(props, exclude) {
if (props && Base.isPlainObject(props)) {
// If props is a filtering object, we need to execute hasOwnProperty
// on the original object (it's parent / prototype). See _filtered
// inheritance trick in the argument reading code.
var orig = props._filtering || props;
for (var key in orig) {
if (key in this && orig.hasOwnProperty(key)
&& (!exclude || !exclude[key])) {
var value = props[key];
// Due to the _filtered inheritance trick, undefined is used
// to mask already consumed named arguments.
if (value !== undefined)
this[key] = value;
}
}
return true;
}
},
statics: /** @lends Base */{
// Keep track of all named classes for serialization and exporting.
exports: {},
extend: function extend() {
// Override Base.extend() to register named classes in Base.exports,
// for deserialization and injection into PaperScope.
var res = extend.base.apply(this, arguments),
name = res.prototype._class;
if (name && !Base.exports[name])
Base.exports[name] = res;
return res;
},
/**
* Checks if two values or objects are equals to each other, by using
* their equals() methods if available, and also comparing elements of
* arrays and properties of objects.
*/
equals: function(obj1, obj2) {
function checkKeys(o1, o2) {
for (var i in o1)
if (o1.hasOwnProperty(i) && !o2.hasOwnProperty(i))
return false;
return true;
}
if (obj1 === obj2)
return true;
// Call #equals() on both obj1 and obj2
if (obj1 && obj1.equals)
return obj1.equals(obj2);
if (obj2 && obj2.equals)
return obj2.equals(obj1);
// Compare arrays
if (Array.isArray(obj1) && Array.isArray(obj2)) {
if (obj1.length !== obj2.length)
return false;
for (var i = 0, l = obj1.length; i < l; i++) {
if (!Base.equals(obj1[i], obj2[i]))
return false;
}
return true;
}
// Compare objects
if (obj1 && typeof obj1 === 'object'
&& obj2 && typeof obj2 === 'object') {
if (!checkKeys(obj1, obj2) || !checkKeys(obj2, obj1))
return false;
for (var i in obj1) {
if (obj1.hasOwnProperty(i) && !Base.equals(obj1[i], obj2[i]))
return false;
}
return true;
}
return false;
},
/**
* When called on a subclass of Base, it reads arguments of the type of
* the subclass from the passed arguments list or array, at the given
* index, up to the specified length.
* When called directly on Base, it reads any value without conversion
* from the apssed arguments list or array.
* This is used in argument conversion, e.g. by all basic types (Point,
* Size, Rectangle) and also higher classes such as Color and Segment.
* @param {Array} list the list to read from, either an arguments object
* or a normal array.
* @param {Number} start the index at which to start reading in the list
* @param {Number} length the amount of elements that can be read
* @param {Object} options {@code options.readNull} controls whether null
* is returned or converted. {@code options.clone} controls
* whether passed objects should be cloned if they are already
* provided in the required type
*/
read: function(list, start, length, options) {
// See if it's called directly on Base, and if so, read value and
// return without object conversion.
if (this === Base) {
var value = this.peek(list, start);
list._index++;
list.__read = 1;
return value;
}
var proto = this.prototype,
readIndex = proto._readIndex,
index = start || readIndex && list._index || 0;
if (!length)
length = list.length - index;
var obj = list[index];
if (obj instanceof this
|| options && options.readNull && obj == null && length <= 1) {
if (readIndex)
list._index = index + 1;
return obj && options && options.clone ? obj.clone() : obj;
}
obj = Base.create(this.prototype);
if (readIndex)
obj.__read = true;
// If options were provided, pass them on to the constructed object
if (options)
obj.__options = options;
obj = obj.initialize.apply(obj, index > 0 || length < list.length
? Array.prototype.slice.call(list, index, index + length)
: list) || obj;
if (readIndex) {
list._index = index + obj.__read;
// Have arguments.__read point to the amount of args read in the
// last read() call
list.__read = obj.__read;
delete obj.__read;
if (options)
delete obj.__options;
}
return obj;
},
/**
* Allows peeking ahead in reading of values and objects from arguments
* list through Base.read().
* @param {Array} list the list to read from, either an arguments object
* or a normal array.
* @param {Number} start the index at which to start reading in the list
*/
peek: function(list, start) {
return list[list._index = start || list._index || 0];
},
/**
* Reads all readable arguments from the list, handling nested arrays
* seperately.
* @param {Array} list the list to read from, either an arguments object
* or a normal array.
* @param {Number} start the index at which to start reading in the list
* @param {Object} options {@code options.readNull} controls whether null
* is returned or converted. {@code options.clone} controls
* whether passed objects should be cloned if they are already
* provided in the required type
*/
readAll: function(list, start, options) {
var res = [], entry;
for (var i = start || 0, l = list.length; i < l; i++) {
res.push(Array.isArray(entry = list[i])
// lenghh = 0 for length = max
? this.read(entry, 0, 0, options)
: this.read(list, i, 1, options));
}
return res;
},
/**
* Allows using of Base.read() mechanism in combination with reading
* named arguments form a passed property object literal. Calling
* Base.readNamed() can read both from such named properties and normal
* unnamed arguments through Base.read(). In use for example for the
* various Path.Constructors.
* @param {Array} list the list to read from, either an arguments object
* or a normal array.
* @param {Number} start the index at which to start reading in the list
* @param {String} name the property name to read from.
*/
readNamed: function(list, name, start, length, options) {
var value = this.getNamed(list, name),
hasObject = value !== undefined;
if (hasObject) {
// Create a _filtered object that inherits from argument 0, and
// override all fields that were already read with undefined.
var filtered = list._filtered;
if (!filtered) {
filtered = list._filtered = Base.create(list[0]);
// Point _filtering to the original so Base#_set() can
// execute hasOwnProperty on it.
filtered._filtering = list[0];
}
// delete wouldn't work since the masked parent's value would
// shine through.
filtered[name] = undefined;
}
return this.read(hasObject ? [value] : list, start, length, options);
},
/**
* @return the named value if the list provides an arguments object,
* {@code null} if the named value is {@code null} or {@code undefined},
* and {@code undefined} if there is no arguments object.
* If no name is provided, it returns the whole arguments object.
*/
getNamed: function(list, name) {
var arg = list[0];
if (list._hasObject === undefined)
list._hasObject = list.length === 1 && Base.isPlainObject(arg);
if (list._hasObject)
// Return the whole arguments object if no name is provided.
return name ? arg[name] : list._filtered || arg;
},
/**
* Checks if the argument list has a named argument with the given name.
* If name is {@code null}, it returns {@code true} if there are any
* named arguments.
*/
hasNamed: function(list, name) {
return !!this.getNamed(list, name);
},
/**
* Returns true if obj is either a plain object or an array, as used by
* many argument reading methods.
*/
isPlainValue: function(obj) {
return this.isPlainObject(obj) || Array.isArray(obj);
},
/**
* Serializes the passed object into a format that can be passed to
* JSON.stringify() for JSON serialization.
*/
serialize: function(obj, options, compact, dictionary) {
options = options || {};
var root = !dictionary,
res;
if (root) {
options.formatter = new Formatter(options.precision);
// Create a simple dictionary object that handles all the
// storing and retrieving of dictionary definitions and
// references, e.g. for symbols and gradients. Items that want
// to support this need to define globally unique _id attribute.
/**
* @namespace
* @private
*/
dictionary = {
length: 0,
definitions: {},
references: {},
add: function(item, create) {
// See if we have reference entry with the given id
// already. If not, call create on the item to allow it
// to create the definition, then store the reference
// to it and return it.
var id = '#' + item._id,
ref = this.references[id];
if (!ref) {
this.length++;
var res = create.call(item),
name = item._class;
// Also automatically insert class for dictionary
// entries.
if (name && res[0] !== name)
res.unshift(name);
this.definitions[id] = res;
ref = this.references[id] = [id];
}
return ref;
}
};
}
if (obj && obj._serialize) {
res = obj._serialize(options, dictionary);
// If we don't serialize to compact form (meaning no type
// identifier), see if _serialize didn't already add the class,
// e.g. for classes that do not support compact form.
var name = obj._class;
if (name && !compact && !res._compact && res[0] !== name)
res.unshift(name);
} else if (Array.isArray(obj)) {
res = [];
for (var i = 0, l = obj.length; i < l; i++)
res[i] = Base.serialize(obj[i], options, compact,
dictionary);
// Mark array as compact, so obj._serialize handling above
// doesn't add the class name again.
if (compact)
res._compact = true;
} else if (Base.isPlainObject(obj)) {
res = {};
for (var i in obj)
if (obj.hasOwnProperty(i))
res[i] = Base.serialize(obj[i], options, compact,
dictionary);
} else if (typeof obj === 'number') {
res = options.formatter.number(obj, options.precision);
} else {
res = obj;
}
return root && dictionary.length > 0
? [['dictionary', dictionary.definitions], res]
: res;
},
/**
* Deserializes from parsed JSON data. A simple convention is followed:
* Array values with a string at the first position are links to
* deserializable types through Base.exports, and the values following
* in the array are the arguments to their initialize function.
* Any other value is passed on unmodified.
* The passed data is recoursively traversed and converted, leaves first
*/
deserialize: function(obj, data) {
var res = obj;
// A data side-car to deserialize that can hold any kind of 'global'
// data across a deserialization. It's currently just used to hold
// dictionary definitions.
data = data || {};
if (Array.isArray(obj)) {
// See if it's a serialized type. If so, the rest of the array
// are the arguments to #initialize(). Either way, we simply
// deserialize all elements of the array.
var type = obj[0],
// Handle stored dictionary specially, since we need to
// keep is a lookup table to retrieve referenced items from.
isDictionary = type === 'dictionary';
if (!isDictionary) {
// First see if this is perhaps a dictionary reference, and
// if so return its definition instead.
if (data.dictionary && obj.length == 1 && /^#/.test(type))
return data.dictionary[type];
type = Base.exports[type];
}
res = [];
// Skip first type entry for arguments
for (var i = type ? 1 : 0, l = obj.length; i < l; i++)
res.push(Base.deserialize(obj[i], data));
if (isDictionary) {
data.dictionary = res[0];
} else if (type) {
// Create serialized type and pass collected arguments to
// constructor().
var args = res;
res = Base.create(type.prototype);
type.apply(res, args);
}
} else if (Base.isPlainObject(obj)) {
res = {};
for (var key in obj)
res[key] = Base.deserialize(obj[key], data);
}
return res;
},
exportJSON: function(obj, options) {
return JSON.stringify(Base.serialize(obj, options));
},
importJSON: function(json) {
return Base.deserialize(
typeof json === 'string' ? JSON.parse(json) : json);
},
/**
* Utility function for adding and removing items from a list of which
* each entry keeps a reference to its index in the list in the private
* _index property. Used for PaperScope#projects and Item#children.
*/
splice: function(list, items, index, remove) {
var amount = items && items.length,
append = index === undefined;
index = append ? list.length : index;
if (index > list.length)
index = list.length;
// Update _index on the items to be added first.
for (var i = 0; i < amount; i++)
items[i]._index = index + i;
if (append) {
// Append them all at the end by using push
list.push.apply(list, items);
// Nothing removed, and nothing to adjust above
return [];
} else {
// Insert somewhere else and/or remove
var args = [index, remove];
if (items)
args.push.apply(args, items);
var removed = list.splice.apply(list, args);
// Delete the indices of the removed items
for (var i = 0, l = removed.length; i < l; i++)
delete removed[i]._index;
// Adjust the indices of the items above.
for (var i = index + amount, l = list.length; i < l; i++)
list[i]._index = i;
return removed;
}
},
/**
* Merge all passed hash objects into a newly creted Base object.
*/
merge: function() {
return Base.each(arguments, function(hash) {
Base.each(hash, function(value, key) {
this[key] = value;
}, this);
}, new Base(), true); // Pass true for asArray, as arguments is none
},
/**
* Capitalizes the passed string: hello world -> Hello World
*/
capitalize: function(str) {
return str.replace(/\b[a-z]/g, function(match) {
return match.toUpperCase();
});
},
/**
* Camelizes the passed hyphenated string: caps-lock -> capsLock
*/
camelize: function(str) {
return str.replace(/-(.)/g, function(all, chr) {
return chr.toUpperCase();
});
},
/**
* Converst camelized strings to hyphenated ones: CapsLock -> caps-lock
*/
hyphenate: function(str) {
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}
}
});