-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsvg.js
executable file
·105 lines (100 loc) · 2.76 KB
/
svg.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
"use strict";
const Svg = function (info, pathlist, bitmap) {
this.bitmap = bitmap;
this.pathlist = pathlist;
this.size = info.svgSize;
this.opt_type = info.opt_type;
};
Svg.prototype = {
bezier: function (i, curve) {
var size = this.size;
var b =
"C " +
(curve.c[i * 3 + 0].x * size).toFixed(3) +
" " +
(curve.c[i * 3 + 0].y * size).toFixed(3) +
",";
b +=
(curve.c[i * 3 + 1].x * size).toFixed(3) +
" " +
(curve.c[i * 3 + 1].y * size).toFixed(3) +
",";
b +=
(curve.c[i * 3 + 2].x * size).toFixed(3) +
" " +
(curve.c[i * 3 + 2].y * size).toFixed(3) +
" ";
return b;
},
segment: function (i, curve) {
var size = this.size;
var s =
"L " +
(curve.c[i * 3 + 1].x * size).toFixed(3) +
" " +
(curve.c[i * 3 + 1].y * size).toFixed(3) +
" ";
s +=
(curve.c[i * 3 + 2].x * size).toFixed(3) +
" " +
(curve.c[i * 3 + 2].y * size).toFixed(3) +
" ";
return s;
},
path: function (curve) {
var size = this.size;
var n = curve.n,
i;
var p =
"M" +
(curve.c[(n - 1) * 3 + 2].x * size).toFixed(3) +
" " +
(curve.c[(n - 1) * 3 + 2].y * size).toFixed(3) +
" ";
for (i = 0; i < n; i++) {
if (curve.tag[i] === "CURVE") {
p += this.bezier(i, curve);
} else if (curve.tag[i] === "CORNER") {
p += this.segment(i, curve);
}
}
//p +=
return p;
},
get: function () {
var size = this.size,
bitmap = this.bitmap,
pathlist = this.pathlist;
var w = bitmap.w * size,
h = bitmap.h * size,
len = pathlist.length,
c,
i,
strokec,
fillc,
fillrule;
var svg =
'<svg version="1.1" width="' +
w +
'" height="' +
h +
'" xmlns="http://www.w3.org/2000/svg">';
svg += '<path d="';
for (i = 0; i < len; i++) {
c = pathlist[i].curve;
svg += this.path(c);
}
if (this.opt_type === "curve") {
strokec = "black";
fillc = "none";
fillrule = "";
} else {
strokec = "none";
fillc = "black";
fillrule = ' fill-rule="evenodd"';
}
svg += '" stroke="' + strokec + '" fill="' + fillc + '"' + fillrule + "/></svg>";
return svg;
},
};
module.exports = Svg;