-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.js
97 lines (94 loc) · 2.02 KB
/
load.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
function Load(){
this.src = "";
this.idx = 0;
}
Load.prototype = {
getOutline : function(data){
this.src = data;
this.idx = 0;
var c_size = this._read();
$.log(c_size)
var outlines = [];
for(var i=0;i<c_size;i++){
outlines.push(this._getEachOutline());
}
return outlines;
},
_getEachOutline : function(){
this._min_x = this._min_y = 99999;
var out ={
width : this._read(),
height : this._read(),
polys : []
};
var p_size = this._read();
for(var i=0;i<p_size;i++){
out.polys.push(this._getEachPoly());
}
/*
* 对点阵座标进行调整以适应坐标系。+2 和 +4是为了给左右和上下各加2px的padding
*/
for(var i=0;i<p_size;i++){
var p = out.polys[i];
p.start.x -= this._min_x - 2;
p.start.y = out.height - p.start.y + this._min_y + 2;
for(var j=0;j<p.lines.length;j++){
var l = p.lines[j];
for(var u=0;u<l.points.length;u++){
l.points[u].x -= this._min_x - 2;
l.points[u].y = out.height - l.points[u].y + this._min_y + 2;
}
}
}
out.width+=4;
out.height+=4;
return out;
},
_getEachPoly : function(){
var poly = {
start : {
x : this._readX(),
y : this._readY()
},
lines : []
};
var line_size = this._read();
for(var i=0;i<line_size;i++){
poly.lines.push(this._getEachLine());
}
return poly;
},
_getEachLine : function(){
var t = this._read(), point_size = this._read();
var line = {
type : t===1 ? 'line' : 'bezier',
points : []
};
for(var i=0;i<point_size;i++){
line.points.push({
x : this._readX(),
y : this._readY()
})
}
return line;
},
_read : function(){
return this.src.charCodeAt(this.idx++);
},
_readX : function(){
var x = this._readF();
if(x<this._min_x)
this._min_x = x;
return x;
},
_readY : function(){
var y = this._readF();
if(y<this._min_y)
this._min_y = y;
return y;
},
_readF : function(){
var v = this.src.charCodeAt(this.idx++), f = this.src.charCodeAt(this.idx++);
return ((v & 0x8000)===0?v:(v-0x10000)) + f/0xffff;
}
}