-
Notifications
You must be signed in to change notification settings - Fork 37
/
specs.js
121 lines (111 loc) · 4.48 KB
/
specs.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var svg2img = require('../index'),
expect = require('expect.js'),
btoa = require('btoa'),
fs = require('fs'),
Image = require('canvas').Image,
Image64 = require('node-base64-image'),
util = require('util');
describe('Convert SVG', function () {
it('convert a svg file to png',function(done) {
svg2img(__dirname+'/ph.svg', function(error, data) {
expect(error).not.to.be.ok();
expect(Buffer.isBuffer(data)).to.be.ok();
expect(data.length).to.be.above(0);
done();
})
});
it('convert tiger file to png',function(done) {
svg2img(__dirname+'/tiger.svg', function(error, data) {
expect(error).not.to.be.ok();
expect(Buffer.isBuffer(data)).to.be.ok();
expect(data.length).to.be.above(0);
done();
})
});
it('convert a svg file and scale it',function(done) {
svg2img(__dirname+'/ph.svg', {'width':400, 'height':400}, function(error, data) {
expect(error).not.to.be.ok();
expect(Buffer.isBuffer(data)).to.be.ok();
expect(data.length).to.be.above(0);
done();
})
});
it('convert a svg file and scale it, keep preserveAspectRatio',function(done) {
svg2img(__dirname+'/ph.svg', {'width':400, 'height':400, preserveAspectRatio:true}, function(error, data) {
expect(error).not.to.be.ok();
expect(Buffer.isBuffer(data)).to.be.ok();
expect(data.length).to.be.above(0);
done();
})
});
it('convert a remote svg file to png',function(done) {
this.timeout(5000);
svg2img('https://upload.wikimedia.org/wikipedia/commons/a/a0/Svg_example1.svg', function(error, data) {
expect(error).not.to.be.ok();
expect(Buffer.isBuffer(data)).to.be.ok();
expect(data.length).to.be.above(0);
done();
})
});
it('convert a svg string to png',function(done) {
var svg = fs.readFileSync(__dirname+'/ph.svg');
svg2img(svg, null ,function(error, data) {
expect(error).not.to.be.ok();
expect(Buffer.isBuffer(data)).to.be.ok();
expect(data.length).to.be.above(0);
done();
})
});
it('convert a svg string to jpg',function(done) {
var svg = fs.readFileSync(__dirname+'/ph.svg');
svg2img(svg, {format:'jpeg'} ,function(error, data) {
expect(error).not.to.be.ok();
expect(Buffer.isBuffer(data)).to.be.ok();
expect(data.length).to.be.above(0);
done();
})
});
it('convert a svg base64 to png',function(done) {
var svg = fs.readFileSync(__dirname+'/ph.svg').toString('utf-8');
svg = 'data:image/svg+xml;base64,'+ btoa(svg);
svg2img(svg, null ,function(error, data) {
expect(error).not.to.be.ok();
expect(Buffer.isBuffer(data)).to.be.ok();
expect(data.length).to.be.above(0);
done();
})
});
it('convert a svg with an image', function (done) {
this.timeout(5000);
var imageUrl = 'https://zh.wikipedia.org/static/images/project-logos/zhwiki-hans.png';
Image64.encode(imageUrl, {}, function (err, base64) {
var svgString = util.format('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="540" height="258" ' +
'viewBox="0 0 540 258"><image width="540" height="258" x="0" y="0" href="%s"></image></svg>', 'data:image/png;base64,' + base64.toString('base64'));
svg2img(svgString, function(error, data) {
expect(error).not.to.be.ok();
expect(Buffer.isBuffer(data)).to.be.ok();
expect(data.length).to.be.above(0);
done();
});
});
});
it('scale a svg with width and height in style', function (done) {
svg2img(__dirname+'/fy.svg', {width : 20, height: 30}, function (err, data) {
var img = new Image();
img.onload = function () {
expect(img.width).to.be.eql(20);
expect(img.height).to.be.eql(30);
done();
}
img.src = data;
});
});
it('#20',function(done) {
svg2img(__dirname+'/20.svg', {}, function(error, data) {
expect(error).not.to.be.ok();
expect(Buffer.isBuffer(data)).to.be.ok();
expect(data.length).to.be.above(0);
done();
})
});
});