-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathHelpers.jsx
More file actions
525 lines (476 loc) · 12.3 KB
/
Helpers.jsx
File metadata and controls
525 lines (476 loc) · 12.3 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
518
519
520
521
522
523
524
525
/**
* @author Astute Graphics <info@astutegraphics.com>
* @copyright 2018 Astute Graphics
* @version 1.0.0
* @url https://astutegraphics.com
* @url https://atomiclotus.net
*
* ABOUT:
*
* Helper functions for general use.
*
* CREDITS:
*
* This extension is based on the CEP Boilerplate extension by Scott Lewis
* at Atomic Lotus, LLC.
*
* NO WARRANTIES:
*
* You are free to use, modify, and distribute this script as you see fit.
* No credit is required but would be greatly appreciated.
*
* THIS SCRIPT IS OFFERED AS-IS WITHOUT ANY WARRANTY OR GUARANTEES OF ANY KIND.
* YOU USE THIS SCRIPT COMPLETELY AT YOUR OWN RISK AND UNDER NO CIRCUMSTANCES WILL
* THE DEVELOPER AND/OR DISTRIBUTOR OF THIS SCRIPT BE HELD LIABLE FOR DAMAGES OF
* ANY KIND INCLUDING LOSS OF DATA OR DAMAGE TO HARDWARE OR SOFTWARE. IF YOU DO
* NOT AGREE TO THESE TERMS, DO NOT USE THIS SCRIPT.
*/
/**
* Test if this class supports the Ai Object type. Before you can use
* this method, define an array in the global scope named `supportedTypes`.
* The underscore indicates the array is meant to be private.
* @param {string} theType
* @returns {boolean}
* @private
*/
function isSupported(theType) {
if (typeof(supportedTypes) == 'undefined') {
throw "You must create a global array named `supportedTypes` with the supported type names";
}
return supportedTypes.indexOf(theType.toLowerCase()) >= 0;
}
/**
* Check the type of an object.
* @param {*} theItem
* @param {string} theType
* @returns {boolean}
* @private
*/
function isType(theItem, theType) {
return strcmp(typeof(theItem), theType);
}
/**
* Check the typename of an AI Object.
* @param {*} theItem
* @param {string} theTypename
* @returns {boolean}
* @private
*/
function isTypename(theItem, theTypename) {
if (strcmp(theItem.typename, 'undefined')) return false;
return strcmp(theItem.typename, theTypename);
}
/**
* Get the typename of an object if it is set.
* @param {object} theItem
* @returns {string|null}
*/
function getTypename(theItem) {
if (isDefined(theItem.typename)) return theItem.typename;
return "undefined";
}
/**
* Is theItem an object?
* @param {*} theItem
* @returns {*}
* @private
*/
function isObject(theItem) {
return isType(theItem, 'object');
}
/**
* Is theItem a function?
* @param {*} theItem
* @returns {boolean}
* @private
*/
function isFunction(theItem) {
return isType(theItem, 'function');
}
/**
* Is theItem a string?
* @param {*} theItem
* @returns {boolean}
*/
function isString(theItem) {
return isType(theItem, 'string');
}
/**
* Is theItem a number?
* @param {*} theItem
* @returns {boolean}
*/
function isNumber(theItem) {
return ! isNaN(theItem);
}
/**
* Determine if a value is true-ish.
* USE ONLY with strings, ints, and booleans.
* @param what
* @returns {boolean}
*/
function isTrue(what) {
if (isTrue === true) return true;
if (isString(what)) {
var truish = [
'yes', 'oui', 'ja', 'da',
'si', 'yeah', 'yep', 'yup',
'fuck yes', 'fuck yeah', 'fuckin a',
'you know it', 'of course'
];
what = what.toLowerCase();
if (what === "true") return true;
if (truish.indexOf(what) != -1) return true;
}
if (! isNaN(what)) {
if (parseInt(what) > 0) return true;
}
return false;
}
/**
* Is theString an error (Starts with the word 'Error')?
* @param {string} theString
* @returns {boolean}
*/
function isErrorString(theString) {
return theString.substr(0, 5).toLowerCase() == 'error';
}
/**
* Is theItem a GroupItem?
* @param {*} theItem
* @returns {boolean}
* @private
*/
function isGroupItem(theItem) {
return isTypename(theItem, 'GroupItem');
}
/**
* Is theItem a PathItem?
* @param {*} theItem
* @returns {boolean}
* @private
*/
function isPathItem(theItem) {
return isTypename(theItem, 'PathItem');
}
/**
* Is theItem a CompoundPathItem?
* @param {GroupItem} theItem
* @returns {boolean}
* @private
*/
function isCompoundPathItem(theItem) {
return isTypename(theItem, 'CompoundPathItem');
}
/**
* Test if a value is defined.
* @param {string} property
* @returns {boolean}
* @private
*/
function isDefined(theItem) {
return typeof(theItem) != 'undefined';
}
/**
* If theItem is defined, return it, otherwise set it to theDefault value.
* @param {*} theItem
* @param {*) theDefault
* @returns {boolean|*}
*/
function isDefinedOr(theItem, theDefault) {
if (typeof(theItem) != 'undefined') {
return theItem;
}
return theDefault;
}
/**
* If theItem is not empty, return it, otherwise set it to theDefault value.
* @param {*} theItem
* @param {*) theDefault
* @returns {boolean|*}
*/
function isEmptyOr(theItem, theDefault) {
if (! isEmpty(theItem)) {
return theItem;
}
return theDefault;
}
/**
* Get the current timestamp.
* @returns {number}
* @private
*/
function now() {
return (new Date()).getTime();
}
/**
* Ensures a URL ends with a trailing slash.
* @param url
* @returns {*}
*/
function slash(url) {
if (url.charAt(url.length-1) != '/') {
url += '/';
}
return url;
};
/**
* Appends a string to a base string using a divider.
* @param {string} base
* @param {string} add
* @param {string} divider
* @returns {string}
*/
function concat(base, add, divider) {
if (base.charAt(base.length-1) != divider) {
base += divider;
}
return base + add;
}
/**
* Case-insensitive string comparison.
* @param {string} aText
* @param {string} bText
* @returns {boolean}
*/
function strcmp(aText, bText) {
return aText.toLowerCase() == bText.toLowerCase();
}
/**
* Trap function execution in a try/catch block.
* @param {function} func
* @returns {*}
*/
function trap(func, customError) {
try {
return func();
}
catch(e) {
return customError || e.message ;
}
}
/**
* Test of a variable is completely empty.
* @param {*} data
* @returns {boolean}
*/
function isEmpty(data) {
if (typeof(data) == 'number' || typeof(data) == 'boolean') {
return false;
}
if (typeof(data) == 'undefined' || data === null) {
return true;
}
if (typeof(data.length) != 'undefined') {
return data.length == 0;
}
var count = 0;
for (var i in data) {
if (data.hasOwnProperty(i)) count ++;
}
return count == 0;
}
/**
* Convert XML document to string.
* @param {XmlDocument} xmlData
* @returns {string}
*/
function xmlToString(xmlData) {
//IE
if (window.ActiveXObject){
return xmlData.xml;
}
// Everyone else.
return (new XMLSerializer()).serializeToString(xmlData);
}
/**
* Trim newline chars from a long string.
* @param {string} theText
* @returns {string}
*/
function trimNewLines(theText) {
return theText.replace(/\r?\n/g, "");
}
/**
* Remove empty group nodes from SVG.
* @param {XmlDocument} $svg
* @returns {XmlDocument}
*/
function removeEmptyNodes($svg) {
$("g", $svg).each(function(i) {
var $group = $(this);
if ($.trim($group.text()) == "") {
$group.remove();
}
});
return $svg;
}
/**
* Set the PathPoints in an AI PathItem from SVG path value.
* @param {PathItem} path
* @param {string} svg
*
* @author Malcolm McLean <malcolm@astutegraphics.co.uk>
*/
function setPathItemFromSVG(path, svg)
{
var i;
var pp;
var pointArray = svgToPathPointArray(svg);
while(path.pathPoints.length > 1)
{
path.pathPoints[0].remove();
}
path.pathPoints[0].anchor = pointArray[0].anchor;
path.pathPoints[0].leftDirection = pointArray[0].leftDirection;
path.pathPoints[0].rightDirection = pointArray[0].rightDirection;
for(i=1;i<pointArray.length;i++)
{
pp = path.pathPoints.add();
pp.anchor = pointArray[i].anchor;
pp.leftDirection = pointArray[i].leftDirection;
pp.rightDirection = pointArray[i].rightDirection;
pp.pointType = PointType.CORNER;
}
}
/**
* Copies path points from one path to another.
* @param targetPath
* @param sourcePath
*/
function copyPathPoints(targetPath, sourcePath) {
var i,
pp,
pointArray,
targetPPKey,
sourcePPKey;
targetPPKey = targetPath.selected ? "selectedPathPoints" : "pathPoints";
sourcePPKey = sourcePath.selected ? "selectedPathPoints" : "pathPoints";
while (targetPath[targetPPKey].length > 1) {
targetPath[targetPPKey][0].remove();
}
pointArray = sourcePath[sourcePPKey];
targetPath[targetPPKey][0].anchor = pointArray[0].anchor;
targetPath[targetPPKey][0].leftDirection = pointArray[0].leftDirection;
targetPath[targetPPKey][0].rightDirection = pointArray[0].rightDirection;
for(i=1; i < pointArray.length; i++) {
try {
pp = targetPath[targetPPKey].add();
pp.anchor = pointArray[i].anchor;
pp.leftDirection = pointArray[i].leftDirection;
pp.rightDirection = pointArray[i].rightDirection;
pp.pointType = PointType.CORNER;
}
catch(e) {
Utils.dump("[copyPathPoints()#targetPath[targetPPKey].add()] " + e.message);
}
}
}
/**
* Converts SVG Path value to cubic bezier points.
* @param {string} svg
* @returns {Array}
*
* @author Malcolm McLean <malcolm@astutegraphics.co.uk>
*/
function svgToPathPointArray(svg)
{
var result = [];
var splits = svg.split("C");
var i;
var point = {};
var start = splits[0].slice(1, splits[0].length);
var starts = start.split(",");
if(starts.length != 2)
{
return [];
}
point.anchor = [parseFloat(starts[0]), parseFloat(starts[1])];
result.push(point);
point = {};
for(i=1; i < splits.length;i++)
{
point = {};
segs = splits[i].split(",");
if(segs.length != 6)
{
return [];
}
result[i-1].rightDirection = [parseFloat(segs[0]), parseFloat(segs[1])];
point.leftDirection = [parseFloat(segs[2]), parseFloat(segs[3])];
point.anchor = [parseFloat(segs[4]), parseFloat(segs[5])];
result.push(point);
}
if(svg.indexOf("Z"))
{
result[0].leftDirection = point.leftDirection;
point = {};
if(result.length > 1)
{
result.pop();
}
}
for(i=0;i<result.length;i++)
{
result[i].anchor[0] *= 0.5;
result[i].anchor[1] *= 0.5;
result[i].leftDirection[0] *= 0.5;
result[i].leftDirection[1] *= 0.5;
result[i].rightDirection[0] *= 0.5;
result[i].rightDirection[1] *= 0.5;
}
return result;
}
/**
* Converts AI PathItem PathPoints to SVG path value.
* @param {PathItem} path
* @returns {*}
*
* @author Malcolm McLean <malcolm@astutegraphics.co.uk>
*/
function pathItemToSVG(path)
{
var i;
var answer = "";
var ppa;
var ppb;
if(path.pathPoints.length == 0)
return "";
answer = "M" + path.pathPoints[0].anchor[0].toFixed(2) + "," + path.pathPoints[0].anchor[1].toFixed(2);
for(i=0;i<path.pathPoints.length-1;i++)
{
ppa = path.pathPoints[i];
ppb = path.pathPoints[i+1];
answer += "C";
answer += ppa.rightDirection[0].toFixed(2);
answer += ",";
answer += ppa.rightDirection[1].toFixed(2);
answer += ",";
answer += ppb.leftDirection[0].toFixed(2);
answer += ",";
answer += ppb.leftDirection[1].toFixed(2);
answer += ",";
answer += ppb.anchor[0].toFixed(2);
answer += ",";
answer += ppb.anchor[1].toFixed(2);
}
if(path.closed)
{
ppa = path.pathPoints[path.pathPoints.length-1];
ppb = path.pathPoints[0];
answer += "C";
answer += ppa.rightDirection[0].toFixed(2);
answer += ",";
answer += ppa.rightDirection[1].toFixed(2);
answer += ",";
answer += ppb.leftDirection[0].toFixed(2);
answer += ",";
answer += ppb.leftDirection[1].toFixed(2);
answer += ",";
answer += ppb.anchor[0].toFixed(2);
answer += ",";
answer += ppb.anchor[1].toFixed(2);
answer += "Z";
}
return answer;
}