This repository was archived by the owner on May 7, 2025. It is now read-only.
forked from mapnik/node-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage.svg.test.js
More file actions
195 lines (178 loc) · 8.96 KB
/
Copy pathimage.svg.test.js
File metadata and controls
195 lines (178 loc) · 8.96 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
/* global describe it */
'use strict';
var mapnik = require('../');
var assert = require('assert');
var fs = require('fs');
describe('mapnik.Image SVG', function() {
it('should throw with invalid usage', function() {
assert.throws(function() { new mapnik.Image.fromSVGSync(); });
assert.throws(function() { new mapnik.Image.fromSVG(); });
assert.throws(function() { new mapnik.Image.fromSVGBytes(); });
assert.throws(function() {
new mapnik.Image.fromSVGBytesSync(1);
}, /must provide a buffer argument/);
assert.throws(function() {
new mapnik.Image.fromSVGBytesSync({});
}, /first argument is invalid, must be a Buffer/);
assert.throws(function() {
new mapnik.Image.fromSVGBytes(1, function(err, svg) {});
}, /must provide a buffer argument/);
assert.throws(function() {
new mapnik.Image.fromSVGBytes({}, function(err, svg) {});
}, /first argument is invalid, must be a Buffer/);
assert.throws(function() {
new mapnik.Image.fromSVGBytesSync(new Buffer('asdfasdf'));
}, /SVG parse error:\s+Unable to parse 'asdfasdf'/);
assert.throws(function() {
mapnik.Image.fromSVGSync('./test/data/SVG_DOES_NOT_EXIST.svg');
}, /SVG parse error:\s+Unable to open '.\/test\/data\/SVG_DOES_NOT_EXIST.svg'/);
assert.throws(function() {
mapnik.Image.fromSVGSync('./test/data/vector_tile/tile0.expected-svg.svg', 256);
}, /optional second arg must be an options object/);
assert.throws(function() {
mapnik.Image.fromSVG('./test/data/vector_tile/tile0.expected-svg.svg', 256);
}, /last argument must be a callback function/);
assert.throws(function() {
mapnik.Image.fromSVGBytes(new Buffer('asdfasdf'), 256);
}, /last argument must be a callback function/);
assert.throws(function() {
mapnik.Image.fromSVG('./test/data/vector_tile/tile0.expected-svg.svg', 256, function(err, res) {});
}, /optional second arg must be an options object/);
assert.throws(function() {
mapnik.Image.fromSVGBytes(new Buffer('asdfasdf'), 256, function(err, res) {});
}, /optional second arg must be an options object/);
assert.throws(function() {
mapnik.Image.fromSVGSync('./test/data/vector_tile/tile0.expected-svg.svg', { scale: 'foo' });
}, /'scale' must be a number/);
assert.throws(function() {
mapnik.Image.fromSVG('./test/data/vector_tile/tile0.expected-svg.svg', { scale: 'foo' }, function(err, res) {});
}, /'scale' must be a number/);
assert.throws(function() {
mapnik.Image.fromSVGBytes(new Buffer('asdf'), { scale: 'foo' }, function(err, res) {});
}, /'scale' must be a number/);
assert.throws(function() {
mapnik.Image.fromSVGSync('./test/data/vector_tile/tile0.expected-svg.svg', { scale: -1 });
}, /'scale' must be a positive non zero number/);
assert.throws(function() {
mapnik.Image.fromSVG('./test/data/vector_tile/tile0.expected-svg.svg', { scale: -1 }, function(err, res) {});
}, /'scale' must be a positive non zero number/);
assert.throws(function() {
mapnik.Image.fromSVGBytes(new Buffer('asdf'), { scale: -1 }, function(err, res) {});
}, /'scale' must be a positive non zero number/);
assert.throws(function() {
mapnik.Image.fromSVGSync('./test/data/vector_tile/tile0.corrupt-svg.svg');
}, /image created from svg must have a width and height greater then zero/);
});
it('should err with async file w/o width or height', function(done) {
mapnik.Image.fromSVG('./test/data/vector_tile/tile0.corrupt-svg.svg', function(err, svg) {
assert.ok(err);
assert.ok(err.message.match(/image created from svg must have a width and height greater then zero/));
assert.equal(svg, undefined);
done();
});
});
it('should err with async file w/o width or height as Bytes', function(done) {
var svgdata = "<svg width='0' height='0'><g id='a'><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'/></g></svg>";
var buffer = new Buffer(svgdata);
mapnik.Image.fromSVGBytes(buffer, function(err, img) {
assert.ok(err);
assert.ok(err.message.match(/image created from svg must have a width and height greater then zero/));
assert.equal(img, undefined);
done();
});
});
it('should err with async invalid buffer', function(done) {
mapnik.Image.fromSVGBytes(new Buffer('asdfasdf'), function(err, svg) {
assert.ok(err);
assert.ok(err.message.match(/SVG parse error:\s+Unable to parse 'asdfasdf'/));
assert.equal(svg, undefined);
done();
});
});
it('should err with async non-existent file', function(done) {
mapnik.Image.fromSVG('./test/data/SVG_DOES_NOT_EXIST.svg', function(err, svg) {
assert.ok(err);
assert.ok(err.message.match(/SVG parse error:\s+Unable to open '.\/test\/data\/SVG_DOES_NOT_EXIST.svg'/));
assert.equal(svg, undefined);
done();
});
});
it('should error with async file full of errors', function(done) {
mapnik.Image.fromSVG('./test/data/vector_tile/errors.svg', function(err, svg) {
assert.ok(err);
assert.ok(err.message.match(/SVG parse error:/));
assert.equal(svg, undefined);
done();
});
});
it('#fromSVGSync load from SVG file', function() {
var img = mapnik.Image.fromSVGSync('./test/data/vector_tile/tile0.expected-svg.svg');
assert.ok(img);
assert.ok(img instanceof mapnik.Image);
assert.equal(img.width(), 256);
assert.equal(img.height(), 256);
assert.equal(img.encodeSync('png32').length, 17256);
});
it('#fromSVGSync load from SVG file - 2', function() {
var img = mapnik.Image.fromSVG('./test/data/vector_tile/tile0.expected-svg.svg');
assert.ok(img);
assert.ok(img instanceof mapnik.Image);
assert.equal(img.width(), 256);
assert.equal(img.height(), 256);
assert.equal(img.encodeSync('png32').length, 17256);
});
it('#fromSVG load from SVG file', function(done) {
mapnik.Image.fromSVG('./test/data/vector_tile/tile0.expected-svg.svg', function(err, img) {
assert.ok(img); assert.ok(img instanceof mapnik.Image);
assert.equal(img.width(), 256);
assert.equal(img.height(), 256);
assert.equal(img.encodeSync('png32').length, 17256);
assert.equal(img.premultiplied(), false);
done();
});
});
it('#fromSVGBytesSync load from SVG buffer', function() {
var svgdata = "<svg width='100' height='100'><g id='a'><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'/></g></svg>";
var buffer = new Buffer(svgdata);
var img = mapnik.Image.fromSVGBytesSync(buffer);
assert.ok(img);
assert.ok(img instanceof mapnik.Image);
assert.equal(img.width(), 100);
assert.equal(img.height(), 100);
assert.equal(img.encodeSync("png").length, 1270);
});
it('#fromSVGBytesSync load from SVG buffer - 2', function() {
var svgdata = "<svg width='100' height='100'><g id='a'><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'/></g></svg>";
var buffer = new Buffer(svgdata);
var img = mapnik.Image.fromSVGBytes(buffer);
assert.ok(img);
assert.ok(img instanceof mapnik.Image);
assert.equal(img.width(), 100);
assert.equal(img.height(), 100);
assert.equal(img.encodeSync("png").length, 1270);
});
it('#fromSVGBytes load from SVG buffer', function(done) {
var svgdata = "<svg width='100' height='100'><g id='a'><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'/></g></svg>";
var buffer = new Buffer(svgdata);
mapnik.Image.fromSVGBytes(buffer, function(err, img) {
assert.ok(img);
assert.ok(img instanceof mapnik.Image);
assert.equal(img.width(), 100);
assert.equal(img.height(), 100);
assert.equal(img.encodeSync("png").length, 1270);
assert.equal(img.premultiplied(), false);
done();
});
});
it('svg scaling', function() {
var svgdata = "<svg width='100' height='100'><g id='a'><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'/></g></svg>";
var buffer = new Buffer(svgdata);
var img = mapnik.Image.fromSVGBytesSync(buffer, { scale: 0.5});
assert.ok(img);
assert.ok(img instanceof mapnik.Image);
assert.equal(img.width(), 50);
assert.equal(img.height(), 50);
assert.equal(img.encodeSync("png").length, 616);
assert.equal(img.premultiplied(), false);
});
});