-
Notifications
You must be signed in to change notification settings - Fork 2
/
polyPlotter.js
288 lines (243 loc) · 8.33 KB
/
polyPlotter.js
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
/* ____ __ ____ __ __ __
/ __ \____ / /_ __ / __ \/ /___ / /_/ /____ _____
/ /_/ / __ \/ / / / // /_/ / / __ \/ __/ __/ _ \/ ___/
/ ____/ /_/ / / /_/ // ____/ / /_/ / /_/ /_/ __/ /
/_/ \____/_/\__, //_/ /_/\____/\__/\__/\___/_/
/____/
An ExtendScript module to help draw paths in InDesign.
Version 0.6
Bruno Herfst 2017
MIT license (MIT)
https://github.com/GitBruno/PolyPlotter
*/
#target indesign
var polyPlotter = function( options ) {
// ref to self
var P = this;
// curent position
var currX = currY = 0;
var pathsHolder = [];
// Used when drawing paths
var scale = 100; // Percentage
var offset = [0, 0]; // X,Y
var objectStyleName = "";
function transformNumberArr( numArr, scale, offset ) {
var arr = numArr.slice(0);
var len = arr.length;
if( len < 1 ) {
// Array is empty
return arr;
}
if( arr[0].constructor === Array ) {
for (var i = 0; i < len; i++) {
arr[i] = transformNumberArr(arr[i], scale, offset);
}
} else { // arr is point
// Transform
arr = [ arr[0]/100 * scale + offset[0] ,
arr[1]/100 * scale + offset[1] ]
}
return arr;
}
function transformPath( pathArray ) {
return transformNumberArr( pathArray, scale, offset );
}
function transformAll( pathsHolder ) {
var transformedPaths = pathsHolder.slice(0);
var len = transformedPaths.length;
for (var p = 0; p < len; p++) {
transformedPaths[p].path = transformPath( transformedPaths[p].path );
}
return transformedPaths;
}
function drawPath( page, paths ) {
var pl = paths.length;
var pathType;
var newShape = page.rectangles.add();
try {
newShape.appliedObjectStyle = page.parent.parent.objectStyles.itemByName( objectStyleName, true );
} catch ( err ) {
newShape.appliedObjectStyle = page.parent.parent.objectStyles[0];
}
for (var p = 0; p < pl; p++) {
if( paths[p].open ) {
pathType = PathType.OPEN_PATH;
} else {
pathType = PathType.CLOSED_PATH;
}
var s = newShape.paths.add();
s.entirePath = paths[p].path;
s.pathType = pathType;
}
newShape.paths[0].remove();
}
// Set preferences
// ---------------
P.drawOffset = function ( x, y ) {
offset = [ parseFloat( x ), parseFloat( y ) ];
}
P.drawScale = function ( scalePercent ) {
scale = parseFloat( scalePercent );
}
P.setStyle = function ( styleName ) {
objectStyleName = String( styleName );
}
// Plot functions
// -------------
function updateCurr( x, y ) {
currX = parseFloat( x );
currY = parseFloat( y );
}
// New path object
function plotPath( path, open ) {
var pp = this;
pp.path = path || [];
pp.open = (open==null || open); // Standard true
// Is the first point already drawn
pp.startPoint = false;
pp.add = function ( points ) {
pp.path.push( points );
pp.startPoint = true;
}
}
// New plotter path holder
var currPath = new plotPath();
// Copy given path
P.copyShape = function( shape, options ) {
var myOffset = [0,0]; // X, Y Offset
var myScale = 100; // Percent
var resetBounds = false;
if( currPath.path.length > 0) {
pathsHolder.push( currPath );
}
currPath = new plotPath();
if( options ) {
if(options.hasOwnProperty('scale')) {
myScale = parseFloat(options.scale);
}
if(options.hasOwnProperty('offset')) {
myOffset = [parseFloat(options.offset[0]),parseFloat(options.offset[1])];
}
if(options.hasOwnProperty('resetBounds')) {
resetBounds = Boolean( options.resetBounds );
}
if(!options.hasOwnProperty('ignoreStyle') ) {
if(!Boolean( options.ignoreStyle )) {
objectStyleName = shape.appliedObjectStyle.name;
}
}
}
if(resetBounds) {
// Get shape offset
var sBounds = shape.geometricBounds;
myOffset[0] += -sBounds[1];
myOffset[1] += -sBounds[0];
}
var pathLen = shape.paths.length;
for(var p = 0; p < pathLen; p++){
currPath.path = transformNumberArr( shape.paths[p].entirePath, myScale, myOffset );
currPath.open = false;
if( shape.paths[p].pathType == PathType.OPEN_PATH ) {
currPath.open = true;
}
pathsHolder.push( currPath );
currPath = new plotPath();
}
}
// start a new path
P.newPath = function() {
if( currPath.path.length > 0) {
pathsHolder.push( currPath );
}
currPath = new plotPath();
}
// Move to new location
P.moveTo = function ( x, y ) {
currPath.startPoint = false;
updateCurr( x, y );
}
P.lineTo = function ( x, y ) {
if(!currPath.startPoint) {
currPath.add( [currX, currY] );
}
currPath.add( [parseFloat( x ), parseFloat( y )] );
updateCurr( x, y );
}
P.curveTo = function ( outx, outy, inx, iny, x, y) {
if(!currPath.startPoint) {
currPath.add( [currX, currY] );
}
var lastPoint = currPath.path[currPath.path.length-1];
if( lastPoint[0].constructor === Array ) {
// Curve point
lastPoint[2] = [parseFloat(outx), parseFloat(outy)];
} else {
// Normal point // Convert to curve point
currPath.path[currPath.path.length-1] = [ [lastPoint[0],lastPoint[1]],[lastPoint[0],lastPoint[1]],[parseFloat(outx), parseFloat(outy)] ]
}
currPath.add( [[parseFloat(inx), parseFloat(iny)],[parseFloat(x), parseFloat(y)],[parseFloat(x), parseFloat(y)]] );
}
// close a path
P.closePath = function() {
currPath.open = false;
P.newPath();
}
P.clear = function () {
// New Drawing
// Clears old data
// Reset
currPath = new plotPath();
pathsHolder = [];
}
// Adding points
// -------------
P.addRect = function ( x, y, w, h) {
// Adds a rectangle
pathsHolder.push( new plotPath( [ [x,y],[x+w,y],[x+w,y+h],[x,y+h] ], false) );
}
P.addOval = function ( x, y, w, h) {
// Adds an oval
var hw = w * 0.5; // Half width
var hh = h * 0.5; // Half height
var magic = 0.5522848;
var mx = x + hw; // Mid X
var my = y + hh; // Mid Y
var qh = hh * magic;
var qw = hw * magic;
pathsHolder.push( new plotPath( [ [ [mx-qw , y ], [mx , y ], [mx+qw , y ] ],
[ [x+w , my-qh], [x+w , my ], [x+w , my+qh] ],
[ [mx+qw , y+h ], [mx , y+h], [mx-qw , y+h ] ],
[ [x , my+qh], [x , my ], [x , my-qh] ]], false) );
}
// Get functions
// -------------
P.getCurrentLocation = function() {
return [currX, currY];
}
P.getPaths = function( options ) {
return transformAll( pathsHolder );
}
P.drawToPage = function( page, options ) {
P.newPath();
if( options ) {
if(options.hasOwnProperty('scale')) {
scale = parseInt(options.scale);
}
if(options.hasOwnProperty('x')) {
offset[0] = parseFloat( options.x );
}
if(options.hasOwnProperty('y')) {
offset[1] = parseFloat( options.y );
}
if(options.hasOwnProperty('style')) {
objectStyleName = String( options.style );
}
}
if( pathsHolder.length > 0) {
drawPath( page, transformAll( pathsHolder ) );
} else {
alert("No paths to draw, did you close the path?")
}
}
}
// End polyPlotter.js