-
Notifications
You must be signed in to change notification settings - Fork 218
/
rasterize.js
94 lines (82 loc) · 2.96 KB
/
rasterize.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
var rasterize = (function (util, browser, documentHelper, document2svg, svg2image, inlineresources) {
"use strict";
var module = {};
var generalDrawError = function (e) {
return {
message: "Error rendering page",
originalError: e
};
};
var drawSvgAsImg = function (svg) {
return svg2image.renderSvg(svg)
.then(function (image) {
return {
image: image,
svg: svg
};
}, function (e) {
throw generalDrawError(e);
});
};
var drawImageOnCanvas = function (image, canvas) {
try {
canvas.getContext("2d").drawImage(image, 0, 0);
} catch (e) {
// Firefox throws a 'NS_ERROR_NOT_AVAILABLE' if the SVG is faulty
throw generalDrawError(e);
}
};
var doDraw = function (doc, canvas, options) {
return document2svg.drawDocumentAsSvg(doc, options)
.then(drawSvgAsImg)
.then(function (result) {
if (canvas) {
drawImageOnCanvas(result.image, canvas);
}
return result;
});
};
var operateJavaScriptOnDocument = function (doc, options) {
return browser.executeJavascript(doc, options)
.then(function (result) {
var document = result.document;
documentHelper.persistInputValues(document);
return {
document: document,
errors: result.errors
};
});
};
module.rasterize = function (doc, canvas, options) {
var inlineOptions;
inlineOptions = util.clone(options);
inlineOptions.inlineScripts = options.executeJs === true;
return inlineresources.inlineReferences(doc, inlineOptions)
.then(function (errors) {
if (options.executeJs) {
return operateJavaScriptOnDocument(doc, options)
.then(function (result) {
return {
document: result.document,
errors: errors.concat(result.errors)
};
});
} else {
return {
document: doc,
errors: errors
};
}
}).then(function (result) {
return doDraw(result.document, canvas, options)
.then(function (drawResult) {
return {
image: drawResult.image,
svg: drawResult.svg,
errors: result.errors
};
});
});
};
return module;
}(util, browser, documentHelper, document2svg, svg2image, inlineresources));