-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.js
More file actions
208 lines (184 loc) · 4.57 KB
/
util.js
File metadata and controls
208 lines (184 loc) · 4.57 KB
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
/*!
* Thrust 2010 Copyright (C) 2010 by Sven Helmberger.
*
* This software is licensed under the AGPL license.
* project root for LICENSE AND README.
*
*/
(function(){
this.parseSubPaths=function(pathData, xOff, yOff)
{
var idx = 0;
var data = pathData.split(/[ ,]/);
var len = data.length;
var paths=[];
var points = [];
var lastCmd = "L";
while (idx < len)
{
var cmd = data[idx++];
if (cmd < "A")
{
cmd = lastCmd;
if (cmd == "M")
{
cmd = "L";
}
if (cmd == "m")
{
cmd = "l";
}
idx--;
}
else
{
lastCmd = cmd;
}
var absolute = false;
if (cmd <= "a")
{
absolute = true;
cmd = String.fromCharCode(cmd.charCodeAt(0) + 32);
}
switch(cmd)
{
case "m":
points = [];
x=0;
y=0;
case "l":
if (absolute)
{
x = +data[idx++];
y = +data[idx++];
}
else
{
x += +data[idx++];
y += +data[idx++];
}
points.push(new Vector2D(x,y));
break;
case "z":
paths.push(points);
points = [];
x=0;
y=0;
break;
default:
idx++;
console.error("Unknown cmd " + cmd);
}
}
if (points.length)
{
paths.push(points);
}
if (xOff !== undefined || yOff !== undefined)
{
x = xOff || 0;
y = yOff || 0;
//console.debug("path offset x: %d, y: %d", x,y);
for ( var i = 0, len = paths.length; i < len; i++)
{
var points = paths[i];
for ( var j = 0, len2 = points.length; j < len2; j++)
{
var pt = points[j];
pt.x += x;
pt.y += y;
}
}
}
return paths;
};
this.findClosestPointInCandidates = function(candidates, point)
{
//console.debug("candidates = %o, len = %s", candidates, candidates.length);
var minDistance = Infinity, bestCandidate, bestClosest, candidate;
for ( var i = 0, len = candidates.length; i < len; i++)
{
candidate = candidates[i];
if (candidate && candidate.type === "line")
{
// new Marker(candidate.pt0.x, candidate.pt0.y);
// new Marker(candidate.pt1.x, candidate.pt1.y);
var closest = closestPointOnLineSegment(point, candidate.pt0, candidate.pt1);
var distance = closest.substract(point).length();
if (distance < minDistance)
{
minDistance = distance;
bestCandidate = candidate;
bestClosest = closest;
}
}
}
return {closest: closest, obj: bestCandidate, distance: minDistance};
};
var _indexOf = Array.prototype.indexOf;
this.removeFromArray = function(array,obj)
{
var idx = -1;
if (_indexOf)
{
idx = _indexOf.call(array,obj);
}
else
{
for (var i = 0, len = array.length; i < len ; i++)
{
if (array[i] === obj)
{
idx = i;
break;
}
}
}
if (idx >= 0)
{
array.splice(idx,1);
}
};
this.chksum = function(s)
{
var chk = 0xBAB3CAFE;
for (var i=0, len = s.length; i < len; i++)
{
chk = (chk ^ (s.charCodeAt(i) * 69069)) & 0xffffffff;
}
return chk;
};
this.parseTransform = function($elem)
{
var s = $elem.attr("transform");
var m = /translate\((.*)\)/.exec(s);
if (m)
{
var n = m[1].split(",");
var v = new Vector2D(+n[0], +(n[1] || 0));
//console.debug("matched %o", v);
return v;
}
//console.debug("not matched %s", s);
return null;
};
this.svgJSON = function($elem)
{
var $desc = $elem.find("desc");
if ($desc.length)
{
var txt = $desc.text();
try
{
var data = JSON.parse(txt);
//console.debug("metadata is %o", data);
return data;
}
catch(e)
{
console.error("Could not parse svg metadata:", txt);
}
}
return {};
};
})();