-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathaiRotater.jsx
162 lines (160 loc) · 5.07 KB
/
aiRotater.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
//Rotate an selected Object, to fit selected anchor points tangent.
//選択したオブジェクトを選択したアンカーポイントに中心を揃え、そのポイントにおける接線に合せて回転をかけてます。
$._aiRotater ={
offset : 0,
isLft : true,
vtcl : false,
rst : true,
apply : function (ofst, lft, rst, vtcl){
$._aiRotater.offset = ofst;
$._aiRotater.isLft = lft;
$._aiRotater.vtcl = vtcl;
$._aiRotater.rst = rst;
var tg = app.activeDocument.selection;
if (tg.length!=2) return;
if ($._aiRotater.isTGP(tg[0]))
$._aiRotater.exe(tg[0], $._aiRotater.restoreRotation(tg[1]));
else if ($._aiRotater.isTGP(tg[1]))
$._aiRotater.exe(tg[1], $._aiRotater.restoreRotation(tg[0]));
},
exe : function (a, b){
if ($._aiRotater.vtcl) $._aiRotater.rotater(b,[0,0],-Math.PI/2);
var angle,idx;
var tgp = $._aiRotater.getTGP(a);
if (!tgp) return;
if (tgp.parent.pathPoints.length<2) return;
var ctr = $._aiRotater.getCenter(b);
var diff = [tgp.anchor[0] - ctr[0], tgp.anchor[1] - ctr[1]];
if (!$._aiRotater.isCorner(a.pathPoints[$._aiRotater.getIndex(a)])){
angle = $._aiRotater.getAngle(
tgp.rightDirection[0] - tgp.leftDirection[0],
tgp.rightDirection[1] - tgp.leftDirection[1]);
if (angle>=Math.PI/2) angle -= Math.PI;
if (angle<=-Math.PI/2) angle += Math.PI;
}
else if ($._aiRotater .isLft&&!$._aiRotater.isSame(tgp.leftDirection,tgp.anchor)){
angle = $._aiRotater.getAngle(
tgp.anchor[0] - tgp.leftDirection[0],
tgp.anchor[1] - tgp.leftDirection[1]);
}
else if (!$._aiRotater .isLft&&!$._aiRotater.isSame(tgp.rightDirection,tgp.anchor)){
angle = $._aiRotater.getAngle(
tgp.rightDirection[0] - tgp.anchor[0],
tgp.rightDirection[1] - tgp.anchor[1]);
}
else {
idx = $._aiRotater.getIndex(tgp.parent);
if (idx!=0){
if (tgp.parent.PolarityValues==PolarityValues.POSITIVE
||idx==tgp.parent.pathPoints.length-1){
angle = $._aiRotater.getAngle(
tgp.anchor[0] - tgp.parent.pathPoints[idx-1].rightDirection[0],
tgp.anchor[1] - tgp.parent.pathPoints[idx-1].rightDirection[1]);
}
else {
angle = $._aiRotater.getAngle(
tgp.anchor[0] - tgp.parent.pathPoints[idx-1].leftDirection[0],
tgp.anchor[1] - tgp.parent.pathPoints[idx-1].leftDirection[1]);
if (angle>Math.PI/2) angle += Math.PI/2;
}
}
}
$._aiRotater.rotater(b, diff, angle);
},
isSame : function(a,b){
if(a[0]==b[0]&&a[1]==b[1]) return true;
return false;
},
getIndex : function(tg){
for (var i=0;i<tg.pathPoints.length;i++)
if (tg.pathPoints[i].selected==PathPointSelection.ANCHORPOINT)
return i;
return null;
},
rotater : function (tg, df, angle){
var tm = new Matrix();
tm.mValueA = Math.cos(angle);
tm.mValueB = Math.sin(angle);
tm.mValueC = -Math.sin(angle);
tm.mValueD = Math.cos(angle);
tm.mValueTX = df[0] + $._aiRotater.offset * Math.cos(angle+Math.PI/2);
tm.mValueTY = df[1] + $._aiRotater.offset * Math.sin(angle+Math.PI/2);
tg.transform(tm,true,true,true,true,1);
},
isTGP : function (obj){
if (obj.typename=="GroupItem"
||obj.typename=="TextFrame"
||obj.typename=="CompoundPathItem"
||obj.typename=="SymbolItem")
return false;
if (obj.typename=="PathItem") {
var n = 0;
for (var i=0;i<obj.pathPoints.length;i++){
if(obj.pathPoints[i].selected==PathPointSelection.ANCHORPOINT)
n++;
if (n>1) return false;
}
return true;
}
},
getTGP : function (obj){
var n = 0;
var a;
for (var i=0;i<obj.pathPoints.length;i++){
if(obj.pathPoints[i].selected==PathPointSelection.ANCHORPOINT){
a = obj.pathPoints[i];
n++;
}
if (n>1) return false;
}
return a;
},
getCenter : function (tg){
var tmp = tg.geometricBounds;
return [tmp[0]+(tmp[2]-tmp[0])/2, tmp[1]-(tmp[1]-tmp[3])/2];
},
isCorner : function(pt){
var p= new Array();
p[0] = pt.anchor;
p[1] = pt.leftDirection;
p[2] = pt.rightDirection;
if (p[0][0]==p[1][0]&&p[0][0]==p[2][0]){
if (p[1][1]==p[2][1]) return true;
if (p[0][1]<p[1][1]&&p[0][1]>p[2][1]
||p[0][1]>p[1][1]&&p[0][1]<p[2][1]) return true;
return false;
}
var delta = (p[0][1] - p[1][1]) / (p[0][0] - p[1][0]);
var ofst = p[0][1] - p[0][0] * delta;
var rslt = Math.floor(delta * p[2][0] + ofst) - Math.floor (p[2][1]);
if (rslt==0) return false;
else return true;
},
getAngle : function (x,y){
if (x==0&&y==0) return 0;
return Math.atan2(y, x);
},
restoreRotation : function (tgt){
if (!$._aiRotater.rst) return tgt;
if (tgt.typename=="PathItem"
||tgt.typename=="CompoundPathItem"
||tgt.typename=="SymbolItem") return tgt;
var tx;
if (tgt.typename=="TextFrame") tx = tgt;
else {
if (tgt.textFrames.length<1) return tgt;
tx = tgt.textFrames[0];
}
var angle;
if (tx.matrix.mValueA<0&&tx.matrix.mValueB<0)
angle = Math.acos(tx.matrix.mValueD);
else if (tx.matrix.mValueC<0)
angle = -Math.acos(tx.matrix.mValueA);
else
angle = -Math.asin(tx.matrix.mValueB);
var tm = app.getRotationMatrix(angle/Math.PI*180);
tgt.transform(tm,true,true,true,true,1);
return tgt;
}
}
$._aiRotater.apply(0,false,true,false);