You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

8 lines
8.8 KiB

/**
* Minified by jsDelivr using Terser v5.19.2.
* Original file: /npm/save-svg-as-png@1.4.17/lib/saveSvgAsPng.js
*
* Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
*/
"use strict";!function(){var e="undefined"!=typeof exports&&exports||"undefined"!=typeof define&&{}||this||window;"undefined"!=typeof define&&define("save-svg-as-png",[],(function(){return e})),e.default=e;var t="http://www.w3.org/2000/xmlns/",n="http://www.w3.org/2000/svg",r=/url\(["']?(.+?)["']?\)/,o={woff2:"font/woff2",woff:"font/woff",otf:"application/x-font-opentype",ttf:"application/x-font-ttf",eot:"application/vnd.ms-fontobject",sfnt:"application/font-sfnt",svg:"image/svg+xml"},i=function(e){return e instanceof HTMLElement||e instanceof SVGElement},a=function(e){if(!i(e))throw new Error("an HTMLElement or SVGElement is required; got "+e)},s=function(e){return new Promise((function(t,n){i(e)?t(e):n(new Error("an HTMLElement or SVGElement is required; got "+e))}))},u=function(e,t,n){var r=e.viewBox&&e.viewBox.baseVal&&e.viewBox.baseVal[n]||null!==t.getAttribute(n)&&!t.getAttribute(n).match(/%$/)&&parseInt(t.getAttribute(n))||e.getBoundingClientRect()[n]||parseInt(t.style[n])||parseInt(window.getComputedStyle(e).getPropertyValue(n));return null==r||isNaN(parseFloat(r))?0:r},l=function(e){for(var t=window.atob(e.split(",")[1]),n=e.split(",")[0].split(":")[1].split(";")[0],r=new ArrayBuffer(t.length),o=new Uint8Array(r),i=0;i<t.length;i++)o[i]=t.charCodeAt(i);return new Blob([r],{type:n})},c=function(e){return Promise.all(Array.from(e.querySelectorAll("image")).map((function(e){var t,n=e.getAttributeNS("http://www.w3.org/1999/xlink","href")||e.getAttribute("href");return n?((t=n)&&0===t.lastIndexOf("http",0)&&-1===t.lastIndexOf(window.location.host)&&(n+=(-1===n.indexOf("?")?"?":"&")+"t="+(new Date).valueOf()),new Promise((function(t,r){var o=document.createElement("canvas"),i=new Image;i.crossOrigin="anonymous",i.src=n,i.onerror=function(){return r(new Error("Could not load "+n))},i.onload=function(){o.width=i.width,o.height=i.height,o.getContext("2d").drawImage(i,0,0),e.setAttributeNS("http://www.w3.org/1999/xlink","href",o.toDataURL("image/png")),t(!0)}}))):Promise.resolve(null)})))},f={},d=function(e){return Promise.all(e.map((function(e){return new Promise((function(t,n){if(f[e.url])return t(f[e.url]);var o=new XMLHttpRequest;o.addEventListener("load",(function(){var n=function(e){for(var t="",n=new Uint8Array(e),r=0;r<n.byteLength;r++)t+=String.fromCharCode(n[r]);return window.btoa(t)}(o.response),i=e.text.replace(r,'url("data:'+e.format+";base64,"+n+'")')+"\n";f[e.url]=i,t(i)})),o.addEventListener("error",(function(n){console.warn("Failed to load font from: "+e.url,n),f[e.url]=null,t(null)})),o.addEventListener("abort",(function(n){console.warn("Aborted loading font from: "+e.url,n),t(null)})),o.open("GET",e.url),o.responseType="arraybuffer",o.send()}))}))).then((function(e){return e.filter((function(e){return e})).join("")}))},h=null,w=function(e,t){var n=t||{},i=n.selectorRemap,a=n.modifyStyle,s=n.modifyCss,u=n.fonts,l=n.excludeUnusedCss,c=s||function(e,t){return(i?i(e):e)+"{"+(a?a(t):t)+"}\n"},f=[],w=void 0===u,g=u||[];return(h||(h=Array.from(document.styleSheets).map((function(e){try{return{rules:e.cssRules,href:e.href}}catch(t){return console.warn("Stylesheet could not be loaded: "+e.href,t),{}}})))).forEach((function(t){var n=t.rules,i=t.href;n&&Array.from(n).forEach((function(t){if(void 0!==t.style)if(function(e,t){if(t)try{return e.querySelector(t)||e.parentNode&&e.parentNode.querySelector(t)}catch(e){console.warn('Invalid CSS selector "'+t+'"',e)}}(e,t.selectorText))f.push(c(t.selectorText,t.style.cssText));else if(w&&t.cssText.match(/^@font-face/)){var n=function(e,t){var n=e.cssText.match(r),i=n&&n[1]||"";if(i&&!i.match(/^data:/)&&"about:blank"!==i){var a,s,u=i.startsWith("../")?t+"/../"+i:i.startsWith("./")?t+"/."+i:i;return{text:e.cssText,format:(a=u,s=Object.keys(o).filter((function(e){return a.indexOf("."+e)>0})).map((function(e){return o[e]})),s?s[0]:(console.error("Unknown font format for "+a+". Fonts may not be working correctly."),"application/octet-stream")),url:u}}}(t,i);n&&g.push(n)}else l||f.push(t.cssText)}))})),d(g).then((function(e){return f.join("\n")+e}))},g=function(){if(!navigator.msSaveOrOpenBlob&&!("download"in document.createElement("a")))return{popup:window.open()}};e.prepareSvg=function(e,r,o){a(e);var i=r||{},s=i.left,l=void 0===s?0:s,f=i.top,d=void 0===f?0:f,h=i.width,g=i.height,p=i.scale,m=void 0===p?1:p,v=i.responsive,b=void 0!==v&&v,y=i.excludeCss,x=void 0!==y&&y;return c(e).then((function(){var i=e.cloneNode(!0);i.style.backgroundColor=(r||{}).backgroundColor||e.style.backgroundColor;var a=function(e,t,n,r){if("svg"===e.tagName)return{width:n||u(e,t,"width"),height:r||u(e,t,"height")};if(e.getBBox){var o=e.getBBox(),i=o.x,a=o.y;return{width:i+o.width,height:a+o.height}}}(e,i,h,g),s=a.width,c=a.height;if("svg"!==e.tagName){if(!e.getBBox)return void console.error("Attempted to render non-SVG element",e);null!=i.getAttribute("transform")&&i.setAttribute("transform",i.getAttribute("transform").replace(/translate\(.*?\)/,""));var f=document.createElementNS("http://www.w3.org/2000/svg","svg");f.appendChild(i),i=f}if(i.setAttribute("version","1.1"),i.setAttribute("viewBox",[l,d,s,c].join(" ")),i.getAttribute("xmlns")||i.setAttributeNS(t,"xmlns",n),i.getAttribute("xmlns:xlink")||i.setAttributeNS(t,"xmlns:xlink","http://www.w3.org/1999/xlink"),b?(i.removeAttribute("width"),i.removeAttribute("height"),i.setAttribute("preserveAspectRatio","xMinYMin meet")):(i.setAttribute("width",s*m),i.setAttribute("height",c*m)),Array.from(i.querySelectorAll("foreignObject > *")).forEach((function(e){e.setAttributeNS(t,"xmlns","svg"===e.tagName?n:"http://www.w3.org/1999/xhtml")})),!x)return w(e,r).then((function(e){var t=document.createElement("style");t.setAttribute("type","text/css"),t.innerHTML="<![CDATA[\n"+e+"\n]]>";var n=document.createElement("defs");n.appendChild(t),i.insertBefore(n,i.firstChild);var r=document.createElement("div");r.appendChild(i);var a=r.innerHTML.replace(/NS\d+:href/gi,'xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href');if("function"!=typeof o)return{src:a,width:s,height:c};o(a,s,c)}));var p=document.createElement("div");p.appendChild(i);var v=p.innerHTML;if("function"!=typeof o)return{src:v,width:s,height:c};o(v,s,c)}))},e.svgAsDataUri=function(t,n,r){return a(t),e.prepareSvg(t,n).then((function(e){var t=e.src,n=e.width,o=e.height,i="data:image/svg+xml;base64,"+window.btoa(decodeURIComponent(encodeURIComponent('<?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" [<!ENTITY nbsp "&#160;">]>'+t).replace(/%([0-9A-F]{2})/g,(function(e,t){var n=String.fromCharCode("0x"+t);return"%"===n?"%25":n}))));return"function"==typeof r&&r(i,n,o),i}))},e.svgAsPngUri=function(t,n,r){a(t);var o=n||{},i=o.encoderType,s=void 0===i?"image/png":i,u=o.encoderOptions,l=void 0===u?.8:u,c=o.canvg,f=function(e){var t=e.src,n=e.width,o=e.height,i=document.createElement("canvas"),a=i.getContext("2d"),u=window.devicePixelRatio||1;i.width=n*u,i.height=o*u,i.style.width=i.width+"px",i.style.height=i.height+"px",a.setTransform(u,0,0,u,0,0),c?c(i,t):a.drawImage(t,0,0);var f=void 0;try{f=i.toDataURL(s,l)}catch(e){if("undefined"!=typeof SecurityError&&e instanceof SecurityError||"SecurityError"===e.name)return void console.error("Rendered SVG images cannot be downloaded in this browser.");throw e}return"function"==typeof r&&r(f,i.width,i.height),Promise.resolve(f)};return c?e.prepareSvg(t,n).then(f):e.svgAsDataUri(t,n).then((function(e){return new Promise((function(t,n){var r=new Image;r.onload=function(){return t(f({src:r,width:r.width,height:r.height}))},r.onerror=function(){n("There was an error loading the data URI as an image on the following SVG\n"+window.atob(e.slice(26))+"Open the following link to see browser's diagnosis\n"+e)},r.src=e}))}))},e.download=function(e,t,n){if(navigator.msSaveOrOpenBlob)navigator.msSaveOrOpenBlob(l(t),e);else{var r=document.createElement("a");if("download"in r){r.download=e,r.style.display="none",document.body.appendChild(r);try{var o=l(t),i=URL.createObjectURL(o);r.href=i,r.onclick=function(){return requestAnimationFrame((function(){return URL.revokeObjectURL(i)}))}}catch(e){console.error(e),console.warn("Error while getting object URL. Falling back to string URL."),r.href=t}r.click(),document.body.removeChild(r)}else n&&n.popup&&(n.popup.document.title=e,n.popup.location.replace(t))}},e.saveSvg=function(t,n,r){var o=g();return s(t).then((function(t){return e.svgAsDataUri(t,r||{})})).then((function(t){return e.download(n,t,o)}))},e.saveSvgAsPng=function(t,n,r){var o=g();return s(t).then((function(t){return e.svgAsPngUri(t,r||{})})).then((function(t){return e.download(n,t,o)}))}}();
//# sourceMappingURL=/sm/6bb468d94b52a018d68dd20a722931fea8e568bdd629e5750ac1ee3eed334667.map