1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
|
23 | return typeof obj;
|
24 | } : function (obj) {
|
25 | return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
26 | };
|
27 |
|
28 | var classCallCheck = function (instance, Constructor) {
|
29 | if (!(instance instanceof Constructor)) {
|
30 | throw new TypeError("Cannot call a class as a function");
|
31 | }
|
32 | };
|
33 |
|
34 | var createClass = function () {
|
35 | function defineProperties(target, props) {
|
36 | for (var i = 0; i < props.length; i++) {
|
37 | var descriptor = props[i];
|
38 | descriptor.enumerable = descriptor.enumerable || false;
|
39 | descriptor.configurable = true;
|
40 | if ("value" in descriptor) descriptor.writable = true;
|
41 | Object.defineProperty(target, descriptor.key, descriptor);
|
42 | }
|
43 | }
|
44 |
|
45 | return function (Constructor, protoProps, staticProps) {
|
46 | if (protoProps) defineProperties(Constructor.prototype, protoProps);
|
47 | if (staticProps) defineProperties(Constructor, staticProps);
|
48 | return Constructor;
|
49 | };
|
50 | }();
|
51 |
|
52 | var _extends = Object.assign || function (target) {
|
53 | for (var i = 1; i < arguments.length; i++) {
|
54 | var source = arguments[i];
|
55 |
|
56 | for (var key in source) {
|
57 | if (Object.prototype.hasOwnProperty.call(source, key)) {
|
58 | target[key] = source[key];
|
59 | }
|
60 | }
|
61 | }
|
62 |
|
63 | return target;
|
64 | };
|
65 |
|
66 | var WorkerWrapper = function () {
|
67 | function WorkerWrapper(worker) {
|
68 | var _this = this;
|
69 |
|
70 | classCallCheck(this, WorkerWrapper);
|
71 |
|
72 | this.worker = worker;
|
73 | this.listeners = [];
|
74 | this.nextId = 0;
|
75 |
|
76 | this.worker.addEventListener('message', function (event) {
|
77 | var id = event.data.id;
|
78 | var error = event.data.error;
|
79 | var result = event.data.result;
|
80 |
|
81 | _this.listeners[id](error, result);
|
82 | delete _this.listeners[id];
|
83 | });
|
84 | }
|
85 |
|
86 | createClass(WorkerWrapper, [{
|
87 | key: 'render',
|
88 | value: function render(src, options) {
|
89 | var _this2 = this;
|
90 |
|
91 | return new Promise(function (resolve, reject) {
|
92 | var id = _this2.nextId++;
|
93 |
|
94 | _this2.listeners[id] = function (error, result) {
|
95 | if (error) {
|
96 | reject(new Error(error.message, error.fileName, error.lineNumber));
|
97 | return;
|
98 | }
|
99 | resolve(result);
|
100 | };
|
101 |
|
102 | _this2.worker.postMessage({ id: id, src: src, options: options });
|
103 | });
|
104 | }
|
105 | }]);
|
106 | return WorkerWrapper;
|
107 | }();
|
108 |
|
109 | var ModuleWrapper = function ModuleWrapper(module, render) {
|
110 | classCallCheck(this, ModuleWrapper);
|
111 |
|
112 | var instance = module();
|
113 | this.render = function (src, options) {
|
114 | return new Promise(function (resolve, reject) {
|
115 | try {
|
116 | resolve(render(instance, src, options));
|
117 | } catch (error) {
|
118 | reject(error);
|
119 | }
|
120 | });
|
121 | };
|
122 | };
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function b64EncodeUnicode(str) {
|
128 | return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
|
129 | return String.fromCharCode('0x' + p1);
|
130 | }));
|
131 | }
|
132 |
|
133 | function defaultScale() {
|
134 | if ('devicePixelRatio' in window && window.devicePixelRatio > 1) {
|
135 | return window.devicePixelRatio;
|
136 | } else {
|
137 | return 1;
|
138 | }
|
139 | }
|
140 |
|
141 | function svgXmlToImageElement(svgXml) {
|
142 | var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
143 | _ref$scale = _ref.scale,
|
144 | scale = _ref$scale === undefined ? defaultScale() : _ref$scale,
|
145 | _ref$mimeType = _ref.mimeType,
|
146 | mimeType = _ref$mimeType === undefined ? "image/png" : _ref$mimeType,
|
147 | _ref$quality = _ref.quality,
|
148 | quality = _ref$quality === undefined ? 1 : _ref$quality;
|
149 |
|
150 | return new Promise(function (resolve, reject) {
|
151 | var svgImage = new Image();
|
152 |
|
153 | svgImage.onload = function () {
|
154 | var canvas = document.createElement('canvas');
|
155 | canvas.width = svgImage.width * scale;
|
156 | canvas.height = svgImage.height * scale;
|
157 |
|
158 | var context = canvas.getContext("2d");
|
159 | context.drawImage(svgImage, 0, 0, canvas.width, canvas.height);
|
160 |
|
161 | canvas.toBlob(function (blob) {
|
162 | var image = new Image();
|
163 | image.src = URL.createObjectURL(blob);
|
164 | image.width = svgImage.width;
|
165 | image.height = svgImage.height;
|
166 |
|
167 | resolve(image);
|
168 | }, mimeType, quality);
|
169 | };
|
170 |
|
171 | svgImage.onerror = function (e) {
|
172 | var error;
|
173 |
|
174 | if ('error' in e) {
|
175 | error = e.error;
|
176 | } else {
|
177 | error = new Error('Error loading SVG');
|
178 | }
|
179 |
|
180 | reject(error);
|
181 | };
|
182 |
|
183 | svgImage.src = 'data:image/svg+xml;base64,' + b64EncodeUnicode(svgXml);
|
184 | });
|
185 | }
|
186 |
|
187 | function svgXmlToImageElementFabric(svgXml) {
|
188 | var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
189 | _ref2$scale = _ref2.scale,
|
190 | scale = _ref2$scale === undefined ? defaultScale() : _ref2$scale,
|
191 | _ref2$mimeType = _ref2.mimeType,
|
192 | mimeType = _ref2$mimeType === undefined ? 'image/png' : _ref2$mimeType,
|
193 | _ref2$quality = _ref2.quality,
|
194 | quality = _ref2$quality === undefined ? 1 : _ref2$quality;
|
195 |
|
196 | var multiplier = scale;
|
197 |
|
198 | var format = void 0;
|
199 | if (mimeType == 'image/jpeg') {
|
200 | format = 'jpeg';
|
201 | } else if (mimeType == 'image/png') {
|
202 | format = 'png';
|
203 | }
|
204 |
|
205 | return new Promise(function (resolve, reject) {
|
206 | fabric.loadSVGFromString(svgXml, function (objects, options) {
|
207 |
|
208 | if (objects.length == 0) {
|
209 | reject(new Error('Error loading SVG with Fabric'));
|
210 | }
|
211 |
|
212 | var element = document.createElement("canvas");
|
213 | element.width = options.width;
|
214 | element.height = options.height;
|
215 |
|
216 | var canvas = new fabric.Canvas(element, { enableRetinaScaling: false });
|
217 | var obj = fabric.util.groupSVGElements(objects, options);
|
218 | canvas.add(obj).renderAll();
|
219 |
|
220 | var image = new Image();
|
221 | image.src = canvas.toDataURL({ format: format, multiplier: multiplier, quality: quality });
|
222 | image.width = options.width;
|
223 | image.height = options.height;
|
224 |
|
225 | resolve(image);
|
226 | });
|
227 | });
|
228 | }
|
229 |
|
230 | var Viz = function () {
|
231 | function Viz() {
|
232 | var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
233 | workerURL = _ref3.workerURL,
|
234 | worker = _ref3.worker,
|
235 | Module = _ref3.Module,
|
236 | render = _ref3.render;
|
237 |
|
238 | classCallCheck(this, Viz);
|
239 |
|
240 | if (typeof workerURL !== 'undefined') {
|
241 | this.wrapper = new WorkerWrapper(new Worker(workerURL));
|
242 | } else if (typeof worker !== 'undefined') {
|
243 | this.wrapper = new WorkerWrapper(worker);
|
244 | } else if (typeof Module !== 'undefined' && typeof render !== 'undefined') {
|
245 | this.wrapper = new ModuleWrapper(Module, render);
|
246 | } else if (typeof Viz.Module !== 'undefined' && typeof Viz.render !== 'undefined') {
|
247 | this.wrapper = new ModuleWrapper(Viz.Module, Viz.render);
|
248 | } else {
|
249 | throw new Error('Must specify workerURL or worker option, Module and render options, or include one of full.render.js or lite.render.js after viz.js.');
|
250 | }
|
251 | }
|
252 |
|
253 | createClass(Viz, [{
|
254 | key: 'renderString',
|
255 | value: function renderString(src) {
|
256 | var _ref4 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
257 | _ref4$format = _ref4.format,
|
258 | format = _ref4$format === undefined ? 'svg' : _ref4$format,
|
259 | _ref4$engine = _ref4.engine,
|
260 | engine = _ref4$engine === undefined ? 'dot' : _ref4$engine,
|
261 | _ref4$files = _ref4.files,
|
262 | files = _ref4$files === undefined ? [] : _ref4$files,
|
263 | _ref4$images = _ref4.images,
|
264 | images = _ref4$images === undefined ? [] : _ref4$images,
|
265 | _ref4$yInvert = _ref4.yInvert,
|
266 | yInvert = _ref4$yInvert === undefined ? false : _ref4$yInvert,
|
267 | _ref4$nop = _ref4.nop,
|
268 | nop = _ref4$nop === undefined ? 0 : _ref4$nop;
|
269 |
|
270 | for (var i = 0; i < images.length; i++) {
|
271 | files.push({
|
272 | path: images[i].path,
|
273 | data: '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n<svg width="' + images[i].width + '" height="' + images[i].height + '"></svg>'
|
274 | });
|
275 | }
|
276 |
|
277 | return this.wrapper.render(src, { format: format, engine: engine, files: files, images: images, yInvert: yInvert, nop: nop });
|
278 | }
|
279 | }, {
|
280 | key: 'renderSVGElement',
|
281 | value: function renderSVGElement(src) {
|
282 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
283 |
|
284 | return this.renderString(src, _extends({}, options, { format: 'svg' })).then(function (str) {
|
285 | var parser = new DOMParser();
|
286 | return parser.parseFromString(str, 'image/svg+xml').documentElement;
|
287 | });
|
288 | }
|
289 | }, {
|
290 | key: 'renderImageElement',
|
291 | value: function renderImageElement(src) {
|
292 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
293 | var scale = options.scale,
|
294 | mimeType = options.mimeType,
|
295 | quality = options.quality;
|
296 |
|
297 |
|
298 | return this.renderString(src, _extends({}, options, { format: 'svg' })).then(function (str) {
|
299 | if ((typeof fabric === 'undefined' ? 'undefined' : _typeof(fabric)) === "object" && fabric.loadSVGFromString) {
|
300 | return svgXmlToImageElementFabric(str, { scale: scale, mimeType: mimeType, quality: quality });
|
301 | } else {
|
302 | return svgXmlToImageElement(str, { scale: scale, mimeType: mimeType, quality: quality });
|
303 | }
|
304 | });
|
305 | }
|
306 | }, {
|
307 | key: 'renderJSONObject',
|
308 | value: function renderJSONObject(src) {
|
309 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
310 | var format = options.format;
|
311 |
|
312 |
|
313 | if (format !== 'json' || format !== 'json0') {
|
314 | format = 'json';
|
315 | }
|
316 |
|
317 | return this.renderString(src, _extends({}, options, { format: format })).then(function (str) {
|
318 | return JSON.parse(str);
|
319 | });
|
320 | }
|
321 | }]);
|
322 | return Viz;
|
323 | }();
|
324 |
|
325 | export default Viz;
|