-
Notifications
You must be signed in to change notification settings - Fork 114
/
CheckPixelPerfect.jsx
190 lines (168 loc) · 5.16 KB
/
CheckPixelPerfect.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
/*
CheckPixelPerfect.jsx for Adobe Illustrator
Description: Checks snapping of path points to pixel grid in 0.5 or 1.0 px increments
Date: February, 2023
Author: Sergey Osokin, email: hi@sergosokin.ru
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
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
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
// Main function
function main() {
var CFG = {
rgb: [255, 0, 0], // Marker RGB color
d: 6, // Marker diameter
opa: 50, // Marker group opacity 0-100
name: 'nonSnapping', // Marker group name
};
if (!isCorrectEnv('selection')) return;
var paths = getPaths(selection);
if (!paths.length) return;
var col = setRGBColor(CFG.rgb),
grp = paths[0].layer.groupItems.add();
grp.name = CFG.name;
grp.opacity = CFG.opa;
selection = null;
for (var i = 0, len = paths.length; i < len; i++) {
try {
checkPoints(paths[i], CFG.d, col, grp);
} catch (err) {}
}
if (!grp.pageItems.length) {
grp.remove();
alert('Everything is ok\nAll points are aligned to the pixel grid.', 'CheckPixelPerfect');
} else {
alert('Problem points are found\nTotal amount: ' + grp.pageItems.length, 'CheckPixelPerfect');
}
}
// Check the script environment
function isCorrectEnv() {
var args = ['app', 'document'];
args.push.apply(args, arguments);
for (var i = 0; i < args.length; i++) {
var arg = args[i].toString().toLowerCase();
switch (true) {
case /app/g.test(arg):
if (!/illustrator/i.test(app.name)) {
alert('Wrong application\nRun script from Adobe Illustrator', 'Script error');
return false;
}
break;
case /version/g.test(arg):
var rqdVers = parseFloat(arg.split(':')[1]);
if (parseFloat(app.version) < rqdVers) {
alert('Wrong app version\nSorry, script only works in Illustrator v.' + rqdVers + ' and later', 'Script error');
return false;
}
break;
case /document/g.test(arg):
if (!documents.length) {
alert('No documents\nOpen a document and try again', 'Script error');
return false;
}
break;
case /selection/g.test(arg):
if (!selection.length || selection.typename === 'TextRange') {
alert('Nothing selected\nPlease, select at least one object', 'Script error');
return false;
}
break;
}
}
return true;
}
// Get paths from selection
function getPaths(coll) {
var out = [];
for (var i = 0; i < coll.length; i++) {
var item = coll[i];
if (/group/i.test(item.typename) && item.pageItems.length) {
out = [].concat(out, getPaths(item.pageItems));
} else if (/compound/i.test(item.typename) && item.pathItems.length) {
out = [].concat(out, getPaths(item.pathItems));
} else if (/path/i.test(item.typename)) {
out.push(item);
}
}
return out;
}
// Generate solid RGB color
function setRGBColor(rgb) {
var c = new RGBColor();
c.red = rgb[0];
c.green = rgb[1];
c.blue = rgb[2];
return c;
}
// Check path points
function checkPoints(path, d, col, ctr) {
var pts = path.pathPoints;
var isEdit = isEditable(path);
for (var i = 0, len = pts.length; i < len; i++) {
try {
var p = pts[i];
if (!isGridSnap(p.anchor[0]) || !isGridSnap(p.anchor[1])) {
drawMarker(p, d, col, ctr);
if (isEdit) selectPoint(p);
}
} catch (err) {}
}
}
// Check snap to pixel grid
function isGridSnap(n) {
var cut = 1 * n.toFixed(2);
var dec = Math.abs(cut % 1);
return (cut == Math.round(n)) || (dec >= .5 && dec <= .51);
}
// Draw an ellipse at a point
function drawMarker(p, d, c, ctr) {
var dot = activeDocument.pathItems.ellipse(
p.anchor[1] + d * 0.5, // Top
p.anchor[0] - d * 0.5, // Left
d, // Width
d, // Height
false, // Reversed
true); // Inscribed
dot.stroked = false;
dot.fillColor = c;
dot.move(ctr, ElementPlacement.PLACEATBEGINNING);
}
// Check item for locking and visibility
function isEditable(item) {
var prnt = item.parent;
var state = true;
switch (prnt.typename) {
case 'GroupItem':
if (!prnt.editable) return false;
else state = isEditable(prnt);
break;
case 'Layer':
if (prnt.locked || !prnt.visible) return false;
else state = isEditable(prnt);
break;
}
return state;
}
// Select the point
function selectPoint(p) {
p.selected = PathPointSelection.ANCHORPOINT;
}
// Run script
try {
main();
} catch (err) {}