forked from iconic/illustrator-svg-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVG Exporter.jsx
355 lines (283 loc) · 9 KB
/
SVG Exporter.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
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Waybury
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#target illustrator
var exportFolder,
sourceDoc,
itemsToExport,
exportDoc,
svgOptions;
try {
if ( app.documents.length > 0 ) {
svgOptions = new ExportOptionsSVG();
svgOptions.embedRasterImages = false;
svgOptions.cssProperties = SVGCSSPropertyLocation.PRESENTATIONATTRIBUTES;
svgOptions.fontSubsetting = SVGFontSubsetting.None;
svgOptions.documentEncoding = SVGDocumentEncoding.UTF8;
svgOptions.coordinatePrecision = 4;
itemsToExport = [];
sourceDoc = app.activeDocument;
exportFolder = Folder.selectDialog('Select Folder to Save Files');
exportDoc = documents.add(DocumentColorSpace.RGB);
main();
exportDoc.close(SaveOptions.DONOTSAVECHANGES);
}
else{
throw new Error('There are no documents open. Open a document and try again.');
}
}
catch(e) {
alert(e.message, "Script Alert", true);
}
function main() {
var item;
app.activeDocument = sourceDoc;
itemsToExport = getNamedItems(sourceDoc);
for ( var i = 0, len = itemsToExport.length; i < len; i++ ) {
item = itemsToExport[i];
if ( item.typename === 'Artboard' ) {
exportArtboard(item);
} else if ( item.typename === 'Layer' ) {
exportLayer(item);
} else {
exportItem(item);
}
// Empty export document
exportDoc.pageItems.removeAll();
}
}
function exportArtboard(artboard) {
var item,
name,
prettyName,
doc,
rect,
bbox;
app.activeDocument = sourceDoc;
rect = artboard.artboardRect;
bbox = sourceDoc.pathItems.rectangle(rect[1], rect[0], rect[2]-rect[0], rect[1]-rect[3]);
bbox.stroked = false;
bbox.name = '__ILSVGEX__BOUNDING_BOX';
name = artboard.name;
prettyName = name.slice(0, -4).replace(/[^\w\s]|_/g, " ").replace(/\s+/g, "-").toLowerCase();
app.activeDocument = exportDoc;
for ( var i = 0, len = sourceDoc.pageItems.length; i < len; i++ ) {
item = sourceDoc.pageItems[i];
if( hitTest(item, bbox) && !item.locked && !anyParentLocked(item) ) {
item.duplicate( exportDoc, ElementPlacement.PLACEATEND );
}
}
app.activeDocument = exportDoc;
exportDoc.pageItems.getByName('__ILSVGEX__BOUNDING_BOX').remove();
// Check if artboard is blank, clean up and exit
if(!exportDoc.pageItems.length) {
sourceDoc.pageItems.getByName('__ILSVGEX__BOUNDING_BOX').remove();
return;
}
for ( i = 0, len = exportDoc.pageItems.length; i < len; i++) {
item = exportDoc.pageItems[i];
/*
* For the moment, all pageItems are made visible and exported
* unless they are locked. This may not make sense, but it'll
* work for now.
*/
item.hidden = false;
}
exportDoc.layers[0].name = prettyName;
exportSVG( exportDoc, name, bbox.visibleBounds, svgOptions );
sourceDoc.pageItems.getByName('__ILSVGEX__BOUNDING_BOX').remove();
}
function exportLayer(layer) {
var item,
startX,
startY,
endX,
endY,
name,
prettyName,
itemName,
layerItems;
layerItems = [];
for ( var i = 0, len = layer.pageItems.length; i < len; i++ ) {
layerItems.push(layer.pageItems[i]);
}
recurseItems(layer.layers, layerItems);
if ( !layerItems.length ) {
return;
}
name = layer.name;
prettyName = name.slice(0, -4).replace(/[^\w\s]|_/g, " ").replace(/\s+/g, "-").toLowerCase();
for ( i = 0, len = layerItems.length; i < len; i++ ) {
app.activeDocument = sourceDoc;
item = layerItems[i];
item.duplicate( exportDoc, ElementPlacement.PLACEATEND );
}
app.activeDocument = exportDoc;
for ( i = 0, len = exportDoc.pageItems.length; i < len; i++) {
item = exportDoc.pageItems[i];
/*
* For the moment, all pageItems are made visible and exported
* unless they are locked. This may not make sense, but it'll
* work for now.
*/
item.hidden = false;
if(item.name) {
itemName = item.name;
if(itemName.split('.').pop() === 'svg') {
itemName = itemName.slice(0, -4);
}
itemName = itemName.replace(/[^\w\s]|_/g, " ").replace(/\s+/g, "-").toLowerCase()
item.name = prettyName + '-' + itemName;
}
/*
* We want the smallest startX, startY for obvious reasons.
* We also want the smallest endX and endY because Illustrator
* Extendscript treats this coordinate reversed to how the UI
* treats it (e.g., -142 in the UI is 142).
*
*/
startX = ( !startX || startX > item.visibleBounds[0] ) ? item.visibleBounds[0] : startX;
startY = ( !startY || startY < item.visibleBounds[1] ) ? item.visibleBounds[1] : startY;
endX = ( !endX || endX < item.visibleBounds[2] ) ? item.visibleBounds[2] : endX;
endY = ( !endY || endY > item.visibleBounds[3] ) ? item.visibleBounds[3] : endY;
}
exportDoc.layers[0].name = name.slice(0, -4);
exportSVG( exportDoc, name, [startX, startY, endX, endY], svgOptions );
}
function exportItem(item) {
var name,
newItem;
name = item.name;
newItem = item.duplicate( exportDoc, ElementPlacement.PLACEATEND );
newItem.hidden = false;
newItem.name = item.name.slice(0, -4);
app.activeDocument = exportDoc;
exportDoc.layers[0].name = ' ';
exportSVG( exportDoc, name, item.visibleBounds, svgOptions );
}
function exportSVG(doc, name, bounds, exportOptions) {
doc.artboards[0].artboardRect = bounds;
var file = new File( exportFolder.fsName + '/' + name );
doc.exportFile( file, ExportType.SVG, exportOptions );
}
function getNamedItems(doc) {
var item,
items,
doclayers,
artboards;
items = [];
// Check all artboards for name match
artboards = [];
for ( var i = 0, len = doc.artboards.length; i < len; i++ ) {
item = doc.artboards[i];
if ( item.name.split('.').pop() === 'svg' ) {
items.push(item);
}
}
// Check all layers for name match
doclayers = [];
recurseLayers( doc.layers, doclayers );
for ( i = 0, len = doclayers.length; i < len; i++ ) {
item = doclayers[i];
if ( item.name.split('.').pop() === 'svg' && !item.locked && !anyParentLocked(item) ) {
items.push(item);
}
}
// Check all pageItems for name match
for ( i = 0, len = doc.pageItems.length; i < len; i++ ) {
item = doc.pageItems[i];
if ( item.name.split('.').pop() === 'svg' && !item.locked && !anyParentLocked(item) ) {
items.push(item);
}
}
return items;
}
function recurseLayers(layers, layerArray) {
var layer;
for ( var i = 0, len = layers.length; i < len; i++ ) {
layer = layers[i];
if ( !layer.locked ) {
layerArray.push(layer);
}
if (layer.layers.length > 0) {
recurseLayers( layer.layers, layerArray );
}
}
}
function recurseItems(layers, items) {
var layer;
for ( var i = 0, len = layers.length; i < len; i++ ) {
layer = layers[i];
if ( layer.pageItems.length > 0 && !layer.locked ) {
for ( var j = 0, plen = layer.pageItems.length; j < plen; j++ ) {
if ( !layer.pageItems[j].locked ) {
items.push(layer.pageItems[j]);
}
}
}
if ( layer.layers.length > 0 ) {
recurseItems( layer.layers, items );
}
}
}
function anyParentLocked(item) {
while ( item.parent ) {
if ( item.parent.locked ) {
return true;
}
item = item.parent;
}
return false;
}
/* Code derived from John Wundes ( [email protected] ) www.wundes.com
* Copyright (c) 2005 wundes.com
* All rights reserved.
*
* This code is derived from software contributed to or originating on wundes.com
*/
function hitTest(a,b){
if(!hitTestX(a,b)){
return false;
}
if(!hitTestY(a,b)){
return false;
}
return true;
}
function hitTestX(a,b){
var p1 = a.visibleBounds[0];
var p2 = b.visibleBounds[0];
if( (p2<=p1 && p1<=p2+b.width) || (p1<=p2 && p2<=p1+a.width) ) {
return true;
}
return false;
}
function hitTestY(a,b){
var p3 = a.visibleBounds[1];
var p4 = b.visibleBounds[1];
if( (p3>=p4 && p4>=(p3-a.height)) || (p4>=p3 && p3>=(p4-b.height)) ) {
return true;
}
return false;
}