2 var out$ = typeof exports !=
'undefined' && exports ||
this;
4 var doctype =
'<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
6 function isExternal(url) {
7 return url && url.lastIndexOf(
'http',0) == 0 && url.lastIndexOf(window.location.host) == -1;
10 function inlineImages(el, callback) {
11 var images = el.querySelectorAll(
'image');
12 var left = images.length;
16 for (var i = 0; i < images.length; i++) {
18 var href = image.getAttributeNS(
"http://www.w3.org/1999/xlink",
"href");
20 if (isExternal(href.value)) {
21 console.warn(
"Cannot render embedded images linking to external hosts: "+href.value);
25 var canvas = document.createElement(
'canvas');
26 var ctx = canvas.getContext(
'2d');
27 var img =
new Image();
28 href = href || image.getAttribute(
'href');
30 img.onload =
function() {
31 canvas.width = img.width;
32 canvas.height = img.height;
33 ctx.drawImage(img, 0, 0);
34 image.setAttributeNS(
"http://www.w3.org/1999/xlink",
"href", canvas.toDataURL(
'image/png'));
40 img.onerror =
function() {
41 console.log(
"Could not load "+href);
51 function styles(el, selectorRemap) {
53 var sheets = document.styleSheets;
54 for (var i = 0; i < sheets.length; i++) {
56 var rules = sheets[i].cssRules;
58 console.warn(
"Stylesheet could not be loaded: "+sheets[i].href);
63 for (var j = 0; j < rules.length; j++) {
65 if (typeof(rule.style) !=
"undefined") {
68 match = el.querySelector(rule.selectorText);
70 console.warn(
'Invalid CSS selector "' + rule.selectorText +
'"', err);
73 var selector = selectorRemap ? selectorRemap(rule.selectorText) : rule.selectorText;
74 css += selector +
" { " + rule.style.cssText +
" }\n";
75 }
else if(rule.cssText.match(/^@font-face/)) {
76 css += rule.cssText +
'\n';
85 function getDimension(el, clone, dim) {
86 var v = (el.viewBox.baseVal && el.viewBox.baseVal[dim]) ||
87 (clone.getAttribute(dim) !== null && !clone.getAttribute(dim).match(/%$/) && parseInt(clone.getAttribute(dim))) ||
88 el.getBoundingClientRect()[dim] ||
89 parseInt(clone.style[dim]) ||
90 parseInt(window.getComputedStyle(el).getPropertyValue(dim));
91 return (typeof v ===
'undefined' || v === null || isNaN(parseFloat(v))) ? 0 : v;
94 function reEncode(data) {
95 data = encodeURIComponent(data);
96 data = data.replace(/%([0-9A-F]{2})/g,
function(match, p1) {
97 var c = String.fromCharCode(
'0x'+p1);
98 return c ===
'%' ?
'%25' : c;
100 return decodeURIComponent(data);
103 out$.svgAsDataUri =
function(el, options, cb) {
104 options = options || {};
105 options.scale = options.scale || 1;
106 var xmlns =
"http://www.w3.org/2000/xmlns/";
108 inlineImages(el,
function() {
109 var outer = document.createElement(
"div");
110 var clone = el.cloneNode(
true);
112 if(el.tagName ==
'svg') {
113 width = options.width || getDimension(el, clone,
'width');
114 height = options.height || getDimension(el, clone,
'height');
115 }
else if(el.getBBox) {
116 var box = el.getBBox();
117 width = box.x + box.width;
118 height = box.y + box.height;
119 clone.setAttribute(
'transform', clone.getAttribute(
'transform').replace(/translate\(.*?\)/,
''));
121 var svg = document.createElementNS(
'http://www.w3.org/2000/svg',
'svg')
122 svg.appendChild(clone)
125 console.error(
'Attempted to render non-SVG element', el);
129 clone.setAttribute(
"version",
"1.1");
130 clone.setAttributeNS(xmlns,
"xmlns",
"http://www.w3.org/2000/svg");
131 clone.setAttributeNS(xmlns,
"xmlns:xlink",
"http://www.w3.org/1999/xlink");
132 clone.setAttribute(
"width", width * options.scale);
133 clone.setAttribute(
"height", height * options.scale);
134 clone.setAttribute(
"viewBox", [
141 outer.appendChild(clone);
143 var css = styles(el, options.selectorRemap);
144 var s = document.createElement(
'style');
145 s.setAttribute(
'type',
'text/css');
146 s.innerHTML =
"<![CDATA[\n" + css +
"\n]]>";
147 var defs = document.createElement(
'defs');
149 clone.insertBefore(defs, clone.firstChild);
151 var svg = doctype + outer.innerHTML;
152 var uri =
'data:image/svg+xml;base64,' + window.btoa(reEncode(svg));
159 out$.svgAsPngUri =
function(el, options, cb) {
160 out$.svgAsDataUri(el, options,
function(uri) {
161 var image =
new Image();
162 image.onload =
function() {
163 var canvas = document.createElement(
'canvas');
164 canvas.width = image.width;
165 canvas.height = image.height;
166 var context = canvas.getContext(
'2d');
167 if(options && options.backgroundColor){
168 context.fillStyle = options.backgroundColor;
169 context.fillRect(0, 0, canvas.width, canvas.height);
171 context.drawImage(image, 0, 0);
172 var a = document.createElement(
'a');
173 cb(canvas.toDataURL(
'image/png'));
179 out$.saveSvgAsPng =
function(el, name, options) {
180 options = options || {};
181 out$.svgAsPngUri(el, options,
function(uri) {
182 var a = document.createElement(
'a');
185 document.body.appendChild(a);
186 a.addEventListener(
"click",
function(e) {
187 a.parentNode.removeChild(a);