forked from CartoDB/node-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.svg.test.js
344 lines (308 loc) · 17.3 KB
/
image.svg.test.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* 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(done) {
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.from('asdfasdf'));
}, /SVG error: unable to parse "asdfasdf"/);
assert.throws(function() {
mapnik.Image.fromSVGSync('./test/data/SVG_DOES_NOT_EXIST.svg');
}, /SVG error: 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.from('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.from('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.from('asdf'), { scale: 'foo' }, function(err, res) {});
}, /'scale' must be a number/);
assert.throws(function() {
mapnik.Image.fromSVGBytes(undefined, { scale: 1 }, function(err, res) {});
}, /must provide a buffer argument/);
assert.throws(function() {
mapnik.Image.fromSVGBytes(null, { scale: 1 }, function(err, res) {});
}, /must provide a buffer argument/);
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.from('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/);
mapnik.Image.fromSVGBytes(new Buffer.from('a'), { scale: 1 }, function(err, res) {
assert.ok(err);
assert.ok(err.message.match(/SVG error: unable to parse "a"/));
var svgdata = "<svg width='1000000000000' height='1000000000000'><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.from(svgdata);
mapnik.Image.fromSVGBytes(buffer, { scale: 1 }, function(err, img) {
assert.ok(err);
assert.ok(err.message.match(/image created from svg must be 2048 pixels or fewer on each side/));
var svgdata2 = "<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 buffer2 = new Buffer.from(svgdata2);
mapnik.Image.fromSVGBytes(buffer2, { scale: 1000000 }, function(err, img) {
assert.ok(err);
assert.ok(err.message.match(/image created from svg must be 2048 pixels or fewer on each side/));
done();
});
});
});
});
it('blocks allocating a very large image', function(done) {
// 65535 is the max width/height in mapnik
var svgdata = "<svg width='65535' height='65535'><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.from(svgdata);
mapnik.Image.fromSVGBytes(buffer, function(err, img) {
assert.ok(err);
assert.ok(err.message.match(/image created from svg must be 2048 pixels or fewer on each side/));
done();
});
});
it('customized the max image size to block', function(done) {
// 65535 is the max width/height in mapnik
var svgdata = "<svg width='65535' height='65535'><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.from(svgdata);
mapnik.Image.fromSVGBytes(buffer, {max_size:2049}, function(err, img) {
assert.ok(err);
assert.ok(err.message.match(/image created from svg must be 2049 pixels or fewer on each side/));
done();
});
});
it('max image size blocks dimension*scale', function(done) {
// 65535 is the max width/height in mapnik
var svgdata = "<svg width='5000' height='5000'><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.from(svgdata);
mapnik.Image.fromSVGBytes(buffer, {scale: 2, max_size:10000-1}, function(err, img) {
assert.ok(err);
assert.ok(err.message.match(/image created from svg must be 9999 pixels or fewer on each side/));
done();
});
});
if (process.platform === 'win32') {
// skip on windows since appveyor does not have enough memory
it.skip('allocates very large image', function() {});
} else {
it('allocates very large image', function(done) {
// 65535 is the max width/height in mapnik
var svgdata = "<svg width='5000' height='5000'><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.from(svgdata);
mapnik.Image.fromSVGBytes(buffer, {scale: 2, max_size:10000}, function(err, img) {
if (err) throw err;
assert.equal(img.width(),10000);
assert.equal(img.height(),10000);
done();
});
});
}
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.from(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.from('asdfasdf'), function(err, svg) {
assert.ok(err);
assert.ok(err.message.match(/SVG error: 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 error: unable to open "\.\/test\/data\/SVG_DOES_NOT_EXIST\.svg"/));
assert.equal(svg, undefined);
done();
});
});
it('should not error with async in non-strict mode on svg with validation and parse errors', function(done) {
mapnik.Image.fromSVG('./test/data/vector_tile/errors.svg', {strict:false}, function(err, svg) {
assert.ok(!err);
assert.ok(svg);
done();
});
});
it('should not error with sync in non-strict mode on svg with validation and parse errors', function(done) {
try {
var img = mapnik.Image.fromSVGSync('./test/data/vector_tile/errors.svg', {strict:false});
assert.ok(img);
} catch (err) {
console.log(err);
assert.ok(!err);
}
done();
});
it('should error with async in strict mode on svg with validation and parse errors', function(done) {
mapnik.Image.fromSVG('./test/data/vector_tile/errors.svg', {strict:true}, function(err, svg) {
assert.ok(err);
assert.ok(err.message.match(/SVG validation error:/));
assert.equal(svg, undefined);
done();
});
});
it('should not error on svg with unhandled elements in non-strict mode', function() {
var svgdata = "<svg width='100' height='100'><g id='a'><text></text><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'/></g></svg>";
var buffer = new Buffer.from(svgdata);
var img = mapnik.Image.fromSVGBytesSync(buffer,{strict:false});
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);
});
function svg_error(svgdata) {
var buffer = new Buffer.from(svgdata);
var error = false;
try {
var img = mapnik.Image.fromSVGBytesSync(buffer,{strict:true});
} catch (err) {
error = err;
}
return error;
}
// test the mapnik core known unsupported elements: https://github.com/mapnik/mapnik/blob/634928fcbe780e8a5a355ddb3cd075ce2450adb4/src/svg/svg_parser.cpp#L106-L114
it('should error on svg with unhandled elements in strict mode', function() {
var error = svg_error("<svg width='100' height='100'><g id='a'><unsupported></unsupported><text></text><symbol></symbol><image></image><marker></marker><view></view><switch></switch><a></a><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'/></g></svg>");
assert.ok(error);
assert.ok(error.message.match(/<text> element is not supported/));
error = svg_error("<svg width='100' height='100'><g id='a'><unsupported></unsupported><symbol></symbol><image></image><marker></marker><view></view><switch></switch><a></a><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'/></g></svg>");
assert.ok(error);
assert.ok(error.message.match(/<symbol> element is not supported/));
error = svg_error("<svg width='100' height='100'><g id='a'><unsupported></unsupported><switch></switch><a></a><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'/></g></svg>");
assert.ok(error);
assert.ok(error.message.match(/<switch> element is not supported/));
error = svg_error("<svg width='100' height='100'><g id='a'><unsupported></unsupported><marker></marker><view></view><switch></switch><a></a><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'/></g></svg>");
assert.ok(error);
assert.ok(error.message.match(/<marker> element is not supported/));
error = svg_error("<svg width='100' height='100'><g id='a'><unsupported></unsupported><view></view><switch></switch><a></a><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'/></g></svg>");
assert.ok(error);
assert.ok(error.message.match(/<view> element is not supported/));
error = svg_error("<svg width='100' height='100'><g id='a'><unsupported></unsupported><a></a><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'/></g></svg>");
assert.ok(error);
assert.ok(error.message.match(/<a> element is not supported/));
error = svg_error("<svg width='100' height='100'><g id='a'><unsupported></unsupported><image></image><marker></marker><view></view><switch></switch><a></a><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'/></g></svg>");
assert.ok(error);
assert.ok(error.message.match(/<image> element is not supported/));
});
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, 17571);
});
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, 17571);
});
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, 17571);
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.from(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.from(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.from(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.from(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);
});
});