forked from graysitory/Novelty
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Speech.jsx
402 lines (357 loc) · 11.4 KB
/
Speech.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/***********************************************************************/
/* */
/* Speeech :: Turns a shape into a speech balloon */
/* */
/* [Ver: 1.02] [Author: Marc Autret] [Modif: 07/27/15] */
/* [Lang: EN] [Req: InDesign CS4/CS5/CS6/CC] [Creat: 01/27/12] */
/* */
/* Installation: */
/* */
/* 1) Place the current file into Scripts/Scripts Panel/ */
/* */
/* 2) Start InDesign, open or create a document */
/* */
/* 3) Create a shape (oval, rectangle, polygon, textframe). */
/* */
/* 4a) In the Control panel, click one of the reference points */
/* to make it the 'control point', then select the shape. */
/* */
/* OR */
/* */
/* 4b) Select the Line tool, position the mouse where you want */
/* the speech balloon to point to, then draw a line from */
/* that position to the shape. Once the guideline is drawn, */
/* select *both* the line and the shape. */
/* */
/* 5) Exec the script from your scripts panel: */
/* Window > Automation > Scripts [CS4] */
/* Window > Utilities > Scripts [CS5/CS6/CC] */
/* + double-click on the script file name */
/* */
/* Bugs & Feedback : marc{at}indiscripts{dot}com */
/* www.indiscripts.com */
/* */
/***********************************************************************/
//======================================
// BORING CONSTANTS
//======================================
const MU_POINTS = +MeasurementUnits.POINTS,
BB_GEO = +BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS,
RO_SPREAD = +RulerOrigin.SPREAD_ORIGIN, // [ADD150727]
CS_INNER = +CoordinateSpaces.INNER_COORDINATES,
CS_PASTEBOARD = +CoordinateSpaces.PASTEBOARD_COORDINATES,
AP = AnchorPoint;
//======================================
// YOUR SETTINGS
//======================================
const PEAK_MIN_DIST = 24, // Minimum distance from the peak point to the edge (in pt)
ATTACH_WIDTH = 6, // Distance between the attach point and each 'twin' point (in pt)
DEPTH = 8, // Depth of the attach point within the frame (in pt)
DIR_WEIGHT = .5, // Magnitude of the direction vectors, given as a barycentric factor ]0,1]
CURVE = 2; // Curve factor (>=0)
//======================================
// UTILITIES
//======================================
var hitTest = function(/*PageItem*/shape, /*Number[][]*/ePath)
//--------------------------------------
{
var t;
try {
(t=shape.parent.rectangles.add()).paths[0].entirePath = ePath;
t.intersectPath(shape).remove();
t = shape = null;
}
catch(_)
{
t && t.remove();
}
return !shape;
};
//======================================
// MAIN ROUTINE
//======================================
var createPeaker = function(/*PageItem*/shape, /*GraphicLine=false*/gl)
//--------------------------------------
{
var mSQR = Math.sqrt,
mABS = Math.abs,
// ---
oInner = shape.resolve([[0,0],BB_GEO,CS_INNER],CS_INNER)[0],
dist2 = function(/*Number[2]*/xy_, /*Number[2]=[0,0]*/_xy)
{
_xy || (_xy=[0,0]);
var dx = xy_[0]-_xy[0],
dy = xy_[1]-_xy[1];
return dx*dx + dy*dy;
},
pathPointToPasteboard = function(/*PathPoint*/pp)
{
return pp.parent.parent.resolve([pp.anchor,[0,0],BB_GEO], CS_PASTEBOARD, true)[0];
},
anchorToInner = function(/*AnchorPoint|Number[2]*/xy)
{
var r = shape.resolve([xy,BB_GEO,CS_INNER],CS_INNER)[0];
return [ r[0]-oInner[0], r[1]-oInner[1]];
},
pasteboardToInner = function(/*Number[2]*/xy)
{
var r = shape.resolve(xy,CS_INNER)[0];
return [ r[0]-oInner[0], r[1]-oInner[1]];
},
oPathPoint = shape.resolve([[0,0],[0,0],BB_GEO],CS_PASTEBOARD,true)[0],
innerToPathPoint = function(/*Number[2]*/xy)
{
var r = shape.resolve([[xy[0]+oInner[0], xy[1]+oInner[1]],CS_INNER], CS_PASTEBOARD)[0];
return [ r[0]-oPathPoint[0], r[1]-oPathPoint[1] ];
},
// ---
u = anchorToInner([1,0])[0],
v = anchorToInner([0,1])[1],
inFrame = function(/*Number[2]*/xy)
{
return xy[0] >= 0 && xy[1] >= 0 && xy[0] <= u && xy[1] <= v;
},
// ---
EDGES = [
{index:0, value:0, alt:v}, // left
{index:1, value:0, alt:u}, // top
{index:0, value:u, alt:v}, // right
{index:1, value:v, alt:u} // bottom
],
p, a, paStep,
twinPts, dirPts,
seg, ePath, dup, edge,
// ---
i, j, k, t, d;
// Resolve the line points (p, a) and find the nearest edge
// ---
if( gl )
{
t = gl.paths[0].pathPoints;
seg = [
pasteboardToInner(pathPointToPasteboard(t[0])),
pasteboardToInner(pathPointToPasteboard(t[-1]))
];
t = [ inFrame(seg[0]), inFrame(seg[1]) ];
if( t[0] != t[1] )
{
d = 0;
i = +t[0];
}
else
{
d = t[0] ? 1 : -1;
t = anchorToInner([.5,.5]);
i = +(dist2(seg[0],t) < dist2(seg[1],t));
}
p = seg[i].concat(); // peak point
a = seg[1-i].concat(); // attach point
if( p[0]==(t=a[0]) && (t <= 0 || t >= u) ) return null;
if( p[1]==(t=a[1]) && (t <= 0 || t >= v) ) return null;
// Find the nearest edge and reset a
// ---
edge = null;
for( i=0 ; i < 4 ; ++i )
{
t = EDGES[i];
j = t.index;
if( d )
{
// d==1 => Edge <= p < a
// d==-1 => p < a < Edge
k = t.value;
if( -1==d )
{
if( k && a[j] <= k ) continue;
if( (!k) && a[j] >= 0 ) continue;
}
k = (d>0?a:p)[j] - k;
if( !k ) continue;
k = d * (a[j] - p[j]) / k;
}
else
{
// p < Edge <= a
k = p[j] - a[j];
if( !k ) continue;
k = (p[j] - t.value)/k;
}
if( k <= 0 || k > 1 ) continue;
d && (k=1/k);
k *= (a[1-j]-p[1-j]);
k = ( d <= 0 ) ? (p[1-j] + k) : (a[1-j]-k);
if( k < 0 || k > t.alt ) continue;
a[j] = t.value;
a[1-j] = k;
edge = t;
break;
}
if( null===edge ) return null;
}
else
{
t = app.layoutWindows.length && app.activeWindow;
if( t && (t instanceof LayoutWindow) )
{
switch( t.transformReferencePoint )
{
case AP.BOTTOM_CENTER_ANCHOR : t = [ [.5,1], [0,1], 3]; break;
case AP.BOTTOM_LEFT_ANCHOR : t = [ [.25,1], [-1,1], 3]; break;
case AP.LEFT_CENTER_ANCHOR : t = [ [0,.6], [-1,1], 0]; break;
case AP.RIGHT_CENTER_ANCHOR : t = [ [1,.6], [1,1], 2]; break;
case AP.TOP_CENTER_ANCHOR : t = [ [.5,0], [0,-1], 1]; break;
case AP.TOP_LEFT_ANCHOR : t = [ [.25,0], [-1,-1], 1]; break;
case AP.TOP_RIGHT_ANCHOR : t = [ [.75,0], [1,-1], 1]; break;
default : t = false;
}
}
t || (t = [ [.75,1], [1,1], 3]); // BOTTOM_RIGHT
a = anchorToInner(t[0]);
p = [a[0]+PEAK_MIN_DIST*(t[1][0]), a[1]+PEAK_MIN_DIST*(t[1][1])];
edge = EDGES[t[2]];
d = 0;
}
// Set a unit vector having the p->a direction
// ---
d = ((d==1)?-1:1)*mSQR(dist2(a,p));
paStep = [(a[0]-p[0])/d, (a[1]-p[1])/d];
i = edge.index;
j = 1-i;
k = edge.value;
t = k ? 1 : -1;
// Make sure p is outside the frame with respect to PEAK_MIN_DIST
// ---
if( PEAK_MIN_DIST > t*(p[i]-k) )
{
p[i] = k + PEAK_MIN_DIST*t;
p[j] = a[j] - paStep[j]*((a[i]-p[i])/paStep[i]);
}
// Make sure a intersects the shape
// ---
dup = shape.duplicate(undefined,[0,0]);
seg = [
[ 3*paStep[1]-6*paStep[0], -3*paStep[0]-6*paStep[1] ],
[ -3*paStep[1]-6*paStep[0], 3*paStep[0]-6*paStep[1] ]
];
do {
a = [a[0]+paStep[0],a[1]+paStep[1]];
ePath = [
innerToPathPoint(a),
innerToPathPoint([a[0]+seg[0][0], a[1]+seg[0][1]]),
innerToPathPoint([a[0]+seg[1][0], a[1]+seg[1][1]])
];
if( hitTest(dup,ePath) ){ dup=null; break; }
} while( inFrame(a) );
if( dup !== null )
{
dup.remove();
return null;
}
// Make sure a is inside the shape with respect to DEPTH
// ---
a[i] -= t*DEPTH;
// LEFT i=0 j=1 t=-1
// TOP i=1 j=0 t=-1
// RIGHT i=0 j=1 t=1
// BOTTOM i=1 j=0 t=1
// Set the twin points
// ---
k = i ? t : -t;
twinPts = [a.concat(), a.concat()];
twinPts[0][j] -= k*ATTACH_WIDTH;
twinPts[1][j] += k*ATTACH_WIDTH;
// Set the dir points with respect to DIR_WEIGHT and CURVE
// ---
d = a[j] - p[j];
dirPts = [twinPts[0].concat(), twinPts[1].concat()];
if( d && CURVE )
{
k = (p[i]-a[i])/(1+DIR_WEIGHT);
dirPts[0][i] += k;
dirPts[1][i] += k;
k = ((d>0)-(d<0))*CURVE*CURVE;
dirPts[0][j] += k;
dirPts[1][j] += k;
}
// Draw the peaker
// ---
ePath = [
innerToPathPoint(twinPts[0]),
[
innerToPathPoint(dirPts[0]),
innerToPathPoint(p),
innerToPathPoint(dirPts[1])
],
innerToPathPoint(twinPts[1])
];
(t=shape.parent.rectangles.add()).paths[0].entirePath = ePath;
t.addPath(shape);
gl && gl.remove();
return true;
};
//======================================
// INTERFACE
//======================================
var speeech = function(/*Polygon*/shape, /*?GraphicLine*/gLine)
//--------------------------------------
{
// Backup and set script prefs.
// ---
var vwPrefs = app.activeDocument.viewPreferences,
units = [+vwPrefs.horizontalMeasurementUnits, +vwPrefs.verticalMeasurementUnits],
ro = +vwPrefs.rulerOrigin; // [ADD150727]
vwPrefs.properties = {
horizontalMeasurementUnits: MU_POINTS,
verticalMeasurementUnits: MU_POINTS,
// ---
// As pointed out by Bruno Herfst we need to force 'ruler spread'
// option in order to support multipage process. [ADD150727]
// ---
rulerOrigin: RO_SPREAD,
};
app.scriptPreferences.enableRedraw = false;
// Main process.
// ---
try {
if( null === createPeaker(shape, gLine||false) )
alert( "Unable to attach the objects." );
}
catch(e)
{
alert( "InDesign returned the following error:\r\r" + e );
}
// Restore original prefs
// ---
app.scriptPreferences.enableRedraw = true;
vwPrefs.properties = {
horizontalMeasurementUnits: units[0],
verticalMeasurementUnits: units[1],
rulerOrigin: ro, // [ADD150727]
};
};
//======================================
// ROOT
//======================================
var root = function()
//--------------------------------------
{
if( !app.documents.length )
{
alert( "No document open." );
}
var sel = app.selection,
i;
switch( sel.length )
{
case 1 :
if( !sel[0].hasOwnProperty('paths') ) break;
return speeech(sel[0]);
case 2 :
if( !(sel[i=0] instanceof GraphicLine) && !(sel[i=1] instanceof GraphicLine) ) break;
if( !sel[1-i].hasOwnProperty('paths') ) break;
return speeech(sel[1-i], sel[i]);
default :
}
alert( "Please select one single shape, or both a shape and a graphic line." );
};
app.doScript(root, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Speeech!");