-
Notifications
You must be signed in to change notification settings - Fork 3
/
Outliner.jsx
369 lines (349 loc) · 11.7 KB
/
Outliner.jsx
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
/*
TODO:
-- Additional font support?
https://github.com/Inventsable/Outliner
contact: [email protected]
Barebones script to convert all paths in current document to permanent Outlines, including handles and anchors.
This action can be undone with a single Edit > Undo command.
You can edit the below settings:
*/
var anchorWidth = 4; // number in pixels, width of stroke
var anchorSize = 20; // number in pixels, height/width of rectangle
var handleSize = 25; // number in pixels, size of ellipse/orb where handle is grabbed
var anchorColor = newRGB(50, 50, 200); // RGB value, defaults to blue
var anchorIsFilled = false; // Boolean, if true anchors are filled, otherwise have only stroke
//
var parentGroupLabel = "_nodes";
var anchorLabel = "_anchor";
var handleLabel = "_handle";
var stickLabel = "_stick";
//
var outlineWidth = 5; // number in pixels, width of stroke
var outlineColor = newRGB(35, 31, 32); // The RGB value of color (default rich black)
//
var useLayerLabelColor = true; // Boolean, if true override above anchorColor and use the Layer's label instead
var forceOpacity = true; // Boolean, if true force all paths to have full opacity
var overrideComplex = false; // Boolean, if true clone all objects and attempt to reconstruct them
// This only needs to be true if you have complex Appearances like multiple strokes per object
var mergeClippingMasks = true; // Boolean, if true will use Pathfinder > Intersect on all Clipping Masks and contents
// If merging is true, requires an additional Undo command per item merged to get back to original
var renameGenericPaths = true; // Boolean, if true will rename unnamed paths as their parent layer
var generateIds = false; // Boolean, if true with generate names with 3 character unique identifiers
var groupRelated = true; // Boolean, if true create child groups for each handle within a parent group for anchor and both handles
/*
Do not edit below unless you know what you're doing!
*/
convertAllToOutlines();
var doc = app.activeDocument;
function convertAllToOutlines() {
convertListToOutlines(scanCurrentPageItems());
sortLayerContents();
}
// Return a list of current pathItems in activeDoc or their clones when overriding complex appearance
function scanCurrentPageItems() {
var list = [];
if (!overrideComplex) {
if (mergeClippingMasks) mergeClippingPaths();
for (var i = app.activeDocument.pathItems.length - 1; i >= 0; i--)
list.push(app.activeDocument.pathItems[i]);
return list;
} else {
return cloneAllPathItems();
}
}
function convertListToOutlines(list) {
for (var i = list.length - 1; i >= 0; i--) {
var item = list[i];
item.name = renameGenericPaths
? rollName(
item.name || item.parent.name || item.layer.name,
item,
item.layer
)
: item.name || item.parent.name || item.layer.name;
if (item.stroked || item.filled) {
replaceAppearance(item);
var parentgroup = groupRelated
? app.activeDocument.groupItems.add()
: null;
if (groupRelated) {
parentgroup.name = item.name + parentGroupLabel;
parentgroup.move(item.layer, ElementPlacement.PLACEATBEGINNING);
}
if (item.pathPoints && item.pathPoints.length)
for (var p = 0; p < item.pathPoints.length; p++) {
var point = item.pathPoints[p];
var pointName = item.name + "[" + p + "]";
var group = groupRelated ? parentgroup.groupItems.add() : null;
if (groupRelated) group.name = pointName;
drawAnchor(point, item.layer, pointName, group);
drawHandle(point, "left", item.layer, pointName, group);
drawHandle(point, "right", item.layer, pointName, group);
item.opacity = forceOpacity ? 100.0 : item.opacity;
}
}
}
}
function drawAnchor(point, layer, name, group) {
var anchor = groupRelated
? group.pathItems.rectangle(
point.anchor[1] + anchorSize / 2,
point.anchor[0] - anchorSize / 2,
anchorSize,
anchorSize
)
: app.activeDocument.pathItems.rectangle(
point.anchor[1] + anchorSize / 2,
point.anchor[0] - anchorSize / 2,
anchorSize,
anchorSize
);
anchor.name = name + anchorLabel;
if (!groupRelated) anchor.move(layer, ElementPlacement.PLACEATBEGINNING);
setAnchorAppearance(anchor, false, layer);
return [anchor];
}
function drawHandle(point, direction, layer, name, group) {
if (
Number(point.anchor[0]) !== Number(point[direction + "Direction"][0]) ||
Number(point.anchor[1]) !== Number(point[direction + "Direction"][1])
) {
var stick = groupRelated
? group.pathItems.add()
: app.activeDocument.pathItems.add();
stick.setEntirePath([point.anchor, point[direction + "Direction"]]);
if (!groupRelated) stick.move(layer, ElementPlacement.PLACEATBEGINNING);
stick.name = name + "_" + direction.charAt(0).toUpperCase() + stickLabel;
setAnchorAppearance(stick, true, layer);
var handle = groupRelated
? group.pathItems.ellipse(
point[direction + "Direction"][1] + handleSize / 2,
point[direction + "Direction"][0] - handleSize / 2,
handleSize,
handleSize
)
: app.activeDocument.pathItems.ellipse(
point[direction + "Direction"][1] + handleSize / 2,
point[direction + "Direction"][0] - handleSize / 2,
handleSize,
handleSize
);
if (!groupRelated) handle.move(layer, ElementPlacement.PLACEATBEGINNING);
handle.stroked = false;
handle.filled = true;
handle.name = name + "_" + direction.charAt(0).toUpperCase() + handleLabel;
handle.fillColor = useLayerLabelColor ? layer.color : anchorColor;
return [stick, handle];
}
}
function setAnchorAppearance(item, isHandle, layer) {
var realColor = useLayerLabelColor ? layer.color : anchorColor;
if (!isHandle) {
item.filled = anchorIsFilled;
item.stroked = !anchorIsFilled;
if (!anchorIsFilled) {
item.strokeWidth = anchorWidth;
item.strokeColor = realColor;
} else {
item.fillColor = realColor;
}
} else {
item.filled = false;
item.stroked = true;
item.strokeWidth = anchorWidth;
item.strokeColor = realColor;
}
}
function replaceAppearance(item) {
item.filled = false;
item.stroked = true;
item.strokeWidth = outlineWidth;
item.strokeColor = outlineColor;
}
function newRGB(r, g, b) {
var color = new RGBColor();
color.red = r;
color.green = g;
color.blue = b;
return color;
}
// Rearrange results per layer so anchor Groups are directly above their target path
function sortLayerContents() {
for (var i = 0; i < app.activeDocument.layers.length; i++) {
var layer = app.activeDocument.layers[i];
for (var c = 0; c < layer.pathItems.length; c++)
layer.pathItems[c].zOrder(ZOrderMethod.BRINGTOFRONT);
var offset = layer.pathItems.length + 1;
for (var c = 0; c < layer.groupItems.length; c++) {
var group = layer.groupItems[c];
offset = Number(offset) - Number(1);
for (var z = 0; z < offset; z++) group.zOrder(ZOrderMethod.BRINGFORWARD);
}
}
}
// Generates a unique identifier for layer to use in children nodes
function rollName(name, item, layer) {
var siblingCount = 0;
var nameRX = new RegExp(name + "\\[\\d\\].*");
if (!generateIds)
for (var i = 0; i < layer.pathItems.length; i++)
if (
nameRX.test(layer.pathItems[i].name) &&
layer.pathItems[i] !== item &&
!/group/i.test(layer.pathItems[i].typename)
)
siblingCount++;
return generateIds
? name + "_" + shortId() + "_"
: name + "[" + siblingCount + "]";
}
// Reconstruct all PathItems with basic data to override any complex appearances
function cloneAllPathItems() {
var list = [];
var cloneProps = ["position", "left", "top", "name", "closed"];
var pathProps = ["anchor", "leftDirection", "rightDirection", "pointType"];
for (var i = app.activeDocument.pathItems.length - 1; i >= 0; i--) {
var item = app.activeDocument.pathItems[i];
var clone = {
pathPoints: [],
};
for (var v = 0; v < cloneProps.length; v++) {
var prop = cloneProps[v];
clone[prop] = item[prop];
}
for (var v = 0; v < item.pathPoints.length; v++)
clone.pathPoints.push(item.pathPoints[v]);
list.push(clone);
item.remove();
}
var dupes = [];
for (var i = 0; i < list.length; i++) {
var schema = list[i];
var item = app.activeDocument.pathItems.add();
for (var v = 0; v < cloneProps.length; v++) {
var prop = cloneProps[v];
item[prop] = schema[prop];
}
for (var v = 0; v < schema.pathPoints.length; v++) {
var point = schema.pathPoints[v];
var newpoint = item.pathPoints.add();
for (var c = 0; c < pathProps.length; c++) {
var prop = pathProps[c];
newpoint[prop] = point[prop];
}
}
dupes.push(item);
}
return dupes;
}
function mergeClippingPaths() {
app.selection = null;
app.executeMenuCommand("Clipping Masks menu item");
var masks = app.selection;
if (app.selection.length < 1) return null;
for (var i = 0; i < masks.length; i++) {
var mask = masks[i];
var parent = mask.parent;
var siblings = [];
for (var v = 0; v < parent.pathItems.length; v++) {
var child = parent.pathItems[v];
if (!child.clipping) {
// var tag = child.tags.add();
// tag.name = "marked";
siblings.push(child);
}
}
if (siblings.length > 1)
for (var v = 1; v < siblings.length; v++) {
app.selection = null;
var dupe = mask.duplicate();
var sibling = siblings[v];
var lastname = sibling.name;
dupe.selected = true;
sibling.selected = true;
intersectAction();
//
// TODO
// If path has name, doing intersect creates a new path and this reference is lost.
//
}
app.selection = null;
mask.selected = true;
siblings[0].selected = true;
var lastname = siblings[0].name;
intersectAction();
app.selection = null;
//
// Fix name transfer
//
parent.selected = true;
app.executeMenuCommand("ungroup");
app.selection = null;
}
}
// Thanks Qwertyfly
// https://community.adobe.com/t5/illustrator/js-cs6-executemenucommand/m-p/5904772#M19673
function intersectAction() {
if ((app.documents.length = 0)) {
return;
}
var ActionString = [
"/version 3",
"/name [ 10",
" 4578706f727454657374",
"]",
"/isOpen 1",
"/actionCount 1",
"/action-1 {",
" /name [ 9",
" 496e74657273656374",
" ]",
" /keyIndex 0",
" /colorIndex 0",
" /isOpen 1",
" /eventCount 1",
" /event-1 {",
" /useRulersIn1stQuadrant 0",
" /internalName (ai_plugin_pathfinder)",
" /localizedName [ 10",
" 5061746866696e646572",
" ]",
" /isOpen 0",
" /isOn 1",
" /hasDialog 0",
" /parameterCount 1",
" /parameter-1 {",
" /key 1851878757",
" /showInPalette -1",
" /type (enumerated)",
" /name [ 9",
" 496e74657273656374",
" ]",
" /value 1",
" }",
" }",
"}",
].join("\n");
createAction(ActionString);
var ActionString = null;
app.doScript("Intersect", "ExportTest", false);
app.unloadAction("ExportTest", "");
function createAction(str) {
var f = new File("~/ScriptAction.aia");
f.open("w");
f.write(str);
f.close();
app.loadAction(f);
f.remove();
}
}
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function shortId() {
var str = "";
var codex = "0123456789abcdefghijklmnopqrstuvwxyz";
for (var i = 0; i <= 2; i++)
str += codex.charAt(randomInt(0, codex.length - 1));
return str.toUpperCase();
}