-
Notifications
You must be signed in to change notification settings - Fork 114
/
FitSelectionToArtboards-Lite.jsx
296 lines (257 loc) · 8.54 KB
/
FitSelectionToArtboards-Lite.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
/*
FitSelectionToArtboards-Lite.jsx for Adobe Illustrator
Description: Proportional resizing one selected object to fit in parent artboard
Date: July, 2022
Modification date: April, 2024
Author: Sergey Osokin, email: hi@sergosokin.ru
Full version: https://github.com/creold/illustrator-scripts/blob/master/md/Item.md#fitselectiontoartboards
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
0.1.2 Fixed objects alignment with modified artboard rulers
0.1.1 Fixed text object fitting
0.1 Initial version
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via Buymeacoffee: https://www.buymeacoffee.com/aiscripts
- via Donatty https://donatty.com/sergosokin
- via DonatePay https://new.donatepay.ru/en/@osokin
- via YooMoney https://yoomoney.ru/to/410011149615582
NOTICE:
Tested with Adobe Illustrator CC 2019-2025 (Mac/Win).
This script is provided "as is" without warranty of any kind.
Free to use, not for sale
Released under the MIT license
http://opensource.org/licenses/mit-license.php
Check my other scripts: https://github.com/creold
*/
//@target illustrator
preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
function main() {
var CFG = {
visBnds: true,
isScaleStroke: true,
isContains: false,
tag: 'artboard',
aiVers: parseInt(app.version),
};
if (CFG.aiVers < 16) {
alert('Error\nSorry, script only works in Illustrator CS6 and later', 'Script error');
return;
}
if (!documents.length) {
alert('Error\nOpen a document and try again', 'Script error');
return;
}
if (!selection.length || selection.typename == 'TextRange') {
alert('Error\nPlease, select one item', 'Script error');
return;
}
var doc = app.activeDocument,
abIdx = doc.artboards.getActiveArtboardIndex(),
abBnds = doc.artboards[abIdx].artboardRect,
item = selection[0],
coord = app.coordinateSystem,
ruler = doc.artboards[abIdx].rulerOrigin;
// If the active artboard contains the selected object
if (!CFG.isContains || isContains(item, CFG.tag)) {
app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
doc.artboards[abIdx].rulerOrigin = [0, 0];
fitToArtboard(item, abBnds, CFG.visBnds, CFG.isScaleStroke);
centerToArtboard(item, abBnds);
app.coordinateSystem = coord;
doc.artboards[abIdx].rulerOrigin = ruler;
}
selection = [item];
}
// Check if item is in the active artboard area
function isContains(item, tag) {
var isContains = false;
addTag(item, tag);
selection = null;
redraw();
activeDocument.selectObjectsOnActiveArtboard();
for (var i = 0, len = selection.length; i < len; i++) {
var item = selection[i];
if (isTagExists(item, tag)) {
isContains = true;
break;
}
}
removeTag(item, tag);
return isContains;
}
// Add custom tag to item
function addTag(item, key) {
var tag = item.tags.add();
tag.name = key;
}
// Check if item tag exists
function isTagExists(item, key) {
try {
var tag = item.tags.getByName(key);
return true;
} catch (e) {
return false;
}
}
// Remove item tag
function removeTag(item, key) {
try {
var tag = item.tags.getByName(key);
tag.remove();
} catch (e) {}
}
// Fit the item to the size of the artboard
function fitToArtboard(item, abBnds, isVisBnds, isStroke) {
var type = isVisBnds ? 'visibleBounds' : 'geometricBounds';
var bnds = [];
if (isType(item, 'group|text')) {
var dup = item.duplicate();
app.executeMenuCommand('deselectall');
selection = dup;
outlineText(dup.pageItems ? dup.pageItems : [dup]);
dup = selection[0];
bnds = getVisibleBounds(dup, type);
app.executeMenuCommand('deselectall');
dup.remove();
} else {
bnds = getVisibleBounds(item, type);
}
var itemWidth = Math.abs(bnds[2] - bnds[0]),
itemHeight = Math.abs(bnds[1] - bnds[3]),
abWidth = Math.abs(abBnds[2] - abBnds[0]),
abHeight = Math.abs(abBnds[1] - abBnds[3]);
var ratioW = 100 * abWidth / itemWidth,
ratioH = 100 * abHeight / itemHeight,
ratio = Math.min(ratioW, ratioH);
// X, Y, Positions, FillPatterns, FillGradients, StrokePattern, LineWidths
item.resize(ratio, ratio, true, true, true, true, (isVisBnds || isStroke) ? ratio : 100);
}
// Create outlines
function outlineText(coll) {
for (var i = coll.length - 1; i >= 0; i--) {
var item = coll[i];
if (isType(item, 'text')) {
item.createOutline();
} else if (isType(item, 'group')) {
outlineText(item.pageItems);
}
}
}
// Get the actual "visible" bounds
// https://github.com/joshbduncan/adobe-scripts/blob/main/DrawVisibleBounds.jsx
function getVisibleBounds(obj, type) {
if (arguments.length == 1 || type == undefined) type = 'geometricBounds';
var doc = app.activeDocument;
var bnds, clippedItem, tmpItem, tmpLayer;
var curItem;
if (obj.typename === 'GroupItem') {
if (obj.clipped) {
// Check all sub objects to find the clipping path
for (var i = 0; i < obj.pageItems.length; i++) {
curItem = obj.pageItems[i];
if (curItem.clipping) {
clippedItem = curItem;
break;
} else if (curItem.typename === 'CompoundPathItem') {
if (!curItem.pathItems.length) {
// Catch compound path items with no pathItems
// via William Dowling @ github.com/wdjsdev
tmpLayer = doc.layers.add();
tmpItem = curItem.duplicate(tmpLayer);
app.executeMenuCommand('deselectall');
tmpItem.selected = true;
app.executeMenuCommand('noCompoundPath');
tmpLayer.hasSelectedArtwork = true;
app.executeMenuCommand('group');
clippedItem = selection[0];
break;
} else if (curItem.pathItems[0].clipping) {
clippedItem = curItem;
break;
}
}
}
if (!clippedItem) clippedItem = obj.pageItems[0];
bnds = clippedItem[type];
if (tmpLayer) {
tmpLayer.remove();
tmpLayer = undefined;
}
} else {
// If the object is not clipped
var subObjBnds;
var allBoundPoints = [[], [], [], []];
// Get the bounds of every object in the group
for (var i = 0; i < obj.pageItems.length; i++) {
curItem = obj.pageItems[i];
subObjBnds = getVisibleBounds(curItem, type);
allBoundPoints[0].push(subObjBnds[0]);
allBoundPoints[1].push(subObjBnds[1]);
allBoundPoints[2].push(subObjBnds[2]);
allBoundPoints[3].push(subObjBnds[3]);
}
// Determine the groups bounds from it sub object bound points
bnds = [
Math.min.apply(Math, allBoundPoints[0]),
Math.max.apply(Math, allBoundPoints[1]),
Math.max.apply(Math, allBoundPoints[2]),
Math.min.apply(Math, allBoundPoints[3]),
];
}
} else {
bnds = obj[type];
}
return bnds;
}
// Place the item in the center of the artboard
function centerToArtboard(item, abBnds) {
var bnds = item.geometricBounds,
itemSize = {
left: bnds[0],
top: bnds[1],
inLeft: bnds[0],
inTop: bnds[1],
inRight: bnds[2],
inBottom: bnds[3],
h: 0,
w: 0
};
if (isType(item, 'group') && item.clipped) {
bnds = getVisibleBounds(item, 'geometricBounds');
itemSize.inLeft = bnds[0];
itemSize.inTop = bnds[1];
itemSize.inRight = bnds[2];
itemSize.inBottom = bnds[3];
} else if (isType(item, 'group|text')) {
var dup = item.duplicate();
app.executeMenuCommand('deselectall');
selection = dup;
outlineText(dup.pageItems ? dup.pageItems : [dup]);
dup = selection[0];
bnds = getVisibleBounds(dup, 'geometricBounds');
app.executeMenuCommand('deselectall');
itemSize.inLeft = bnds[0];
itemSize.inTop = bnds[1];
itemSize.inRight = bnds[2];
itemSize.inBottom = bnds[3];
dup.remove();
}
abWidth = Math.abs(abBnds[2] - abBnds[0]);
abHeight = Math.abs(abBnds[1] - abBnds[3]);
itemSize.h = Math.abs(itemSize.inTop - itemSize.inBottom);
itemSize.w = Math.abs(itemSize.inRight - itemSize.inLeft);
var left = itemSize.left - itemSize.inLeft,
top = itemSize.top - itemSize.inTop,
centerX = left + (abWidth - itemSize.w) / 2,
centerY = top + (itemSize.h - abHeight) / 2;
item.position = [centerX, centerY];
}
// Check the item typename by short name
function isType(item, type) {
var regexp = new RegExp(type, 'i');
return regexp.test(item.typename);
}
try {
main();
} catch (e) {}