-
Notifications
You must be signed in to change notification settings - Fork 23
/
svg-export.min.js
1 lines (1 loc) · 9.39 KB
/
svg-export.min.js
1
(function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):(t="undefined"!=typeof globalThis?globalThis:t||self,e(t.svgExport=t.svgExport||{}))})(this,function(t){"use strict";function e(t){void 0!==typeof console&&"function"==typeof console.warn&&console.warn(t)}function n(t){var n=document.createElement("div");if(n.className="tempdiv-svg-exportJS","string"==typeof t&&(n.insertAdjacentHTML("beforeend",t.trim()),t=n.firstChild),!t.nodeType||1!==t.nodeType)return e("Error svg-export: The input svg was not recognized"),null;var i=t.cloneNode(!0);return i.style.display=null,n.appendChild(i),n.style.visibility="hidden",n.style.display="table",n.style.position="absolute",document.body.appendChild(n),i}function i(t){t&&t.pdfOptions&&(Object.keys(y.pdfOptions).forEach(function(e){if(t.pdfOptions.hasOwnProperty(e)&&typeof t.pdfOptions[e]==typeof y.pdfOptions[e]){if(""===t.pdfOptions[e])return;y.pdfOptions[e]=t.pdfOptions[e]}}),y.pdfOptions.pageLayout.margin||(y.pdfOptions.pageLayout.margin=50),y.pdfOptions.pageLayout.margins||(y.pdfOptions.pageLayout.margins={})),y.pdfOptions.pageLayout.margins.top=y.pdfOptions.pageLayout.margins.top||y.pdfOptions.pageLayout.margin,y.pdfOptions.pageLayout.margins.bottom=y.pdfOptions.pageLayout.margins.bottom||y.pdfOptions.pageLayout.margin,y.pdfOptions.pageLayout.margins.left=y.pdfOptions.pageLayout.margins.left||y.pdfOptions.pageLayout.margin,y.pdfOptions.pageLayout.margins.right=y.pdfOptions.pageLayout.margins.top||y.pdfOptions.pageLayout.margin,delete y.pdfOptions.pageLayout.margin,t&&y.pdfOptions.pageLayout.size||(y.pdfOptions.pageLayout.size=[Math.max(300,y.width)+y.pdfOptions.pageLayout.margins.left+y.pdfOptions.pageLayout.margins.right,Math.max(300,y.height)+y.pdfOptions.pageLayout.margins.top+y.pdfOptions.pageLayout.margins.bottom+(y.pdfOptions.addTitleToPage?2*y.pdfOptions.pdfTitleFontSize+10:0)+(""!==y.pdfOptions.chartCaption?4*y.pdfOptions.pdfCaptionFontSize+10:0)])}function o(t,e){y={originalWidth:100,originalHeight:100,originalMinXViewBox:0,originalMinYViewBox:0,width:100,height:100,scale:1,useCSS:!0,transparentBackgroundReplace:"white",allowCrossOriginImages:!1,elementsToExclude:[],pdfOptions:{customFonts:[],pageLayout:{margin:50,margins:{}},addTitleToPage:!0,chartCaption:"",pdfTextFontFamily:"Helvetica",pdfTitleFontSize:20,pdfCaptionFontSize:14},onDone:null},y.originalHeight=-1!==t.style.getPropertyValue("height").indexOf("%")||t.getAttribute("height")&&-1!==t.getAttribute("height").indexOf("%")?t.getBBox().height*y.scale:t.getBoundingClientRect().height*y.scale,y.originalWidth=-1!==t.style.getPropertyValue("width").indexOf("%")||t.getAttribute("width")&&-1!==t.getAttribute("width").indexOf("%")?t.getBBox().width*y.scale:t.getBoundingClientRect().width*y.scale,y.originalMinXViewBox=t.getAttribute("viewBox")?t.getAttribute("viewBox").split(/\s/)[0]:0,y.originalMinYViewBox=t.getAttribute("viewBox")?t.getAttribute("viewBox").split(/\s/)[1]:0,e&&e.scale&&"number"==typeof e.scale&&(y.scale=e.scale),e&&e.height?"number"==typeof e.height&&(y.height=e.height*y.scale):y.height=y.originalHeight*y.scale,e&&e.width?"number"==typeof e.width&&(y.width=e.width*y.scale):y.width=y.originalWidth*y.scale,e&&!1===e.useCSS&&(y.useCSS=!1),e&&e.transparentBackgroundReplace&&(y.transparentBackgroundReplace=e.transparentBackgroundReplace),e&&e.allowCrossOriginImages&&(y.allowCrossOriginImages=e.allowCrossOriginImages),e&&e.excludeByCSSSelector&&"string"==typeof e.excludeByCSSSelector&&(y.elementsToExclude=t.querySelectorAll(e.excludeByCSSSelector)),e&&e.onDone&&"function"==typeof e.onDone&&(y.onDone=e.onDone),i(e)}function a(t,n){if("function"==typeof getComputedStyle){for(var i=0;i<y.elementsToExclude.length;i++)if(y.elementsToExclude[i]===n)return;var o=window.getComputedStyle(t);if(o.length>0)for(const t of o)-1===["width","height","inline-size","block-size"].indexOf(t)&&n.style.setProperty(t,o.getPropertyValue(t));t.childNodes.forEach(function(t,e){1===t.nodeType&&a(t,n.childNodes[parseInt(e,10)])})}else e("Warning svg-export: this browser is not able to get computed styles")}function r(t,e,n){void 0===n&&(n=!0),y.useCSS&&"object"==typeof e&&(a(e,t),t.style.display=null),y.elementsToExclude.forEach(function(t){t.remove()}),t.style.width=null,t.style.height=null,t.setAttribute("width",y.width),t.setAttribute("height",y.height),t.setAttribute("preserveAspectRatio","none"),t.setAttribute("viewBox",y.originalMinXViewBox+" "+y.originalMinYViewBox+" "+y.originalWidth+" "+y.originalHeight);for(var i=document.getElementsByClassName("tempdiv-svg-exportJS");i.length>0;)i[0].parentNode.removeChild(i[0]);if(n){var o=new XMLSerializer,r=o.serializeToString(t).replace(/currentColor/g,"black");return r.match(/^<svg[^>]+xmlns="http\:\/\/www\.w3\.org\/2000\/svg"/)||(r=r.replace(/^<svg/,'<svg xmlns="http://www.w3.org/2000/svg"')),r.match(/^<svg[^>]+"http\:\/\/www\.w3\.org\/1999\/xlink"/)||(r=r.replace(/^<svg/,'<svg xmlns:xlink="http://www.w3.org/1999/xlink"')),r}return t}function s(t){return new Promise(function(e,n){var i=new Image;i.onload=function(){var n=document.createElement("canvas");n.width=this.naturalWidth||this.getAttribute("width")||this.style.getPropertyValue("width")||300,n.height=this.naturalHeight||this.getAttribute("height")||this.style.getPropertyValue("height")||300,n.getContext("2d").drawImage(this,0,0);var i=n.toDataURL("image/png");t.setAttribute("href",i),e()},y.allowCrossOriginImages&&(i.crossOrigin="anonymous"),i.src=t.getAttribute("href")||t.getAttributeNS("http://www.w3.org/1999/xlink","href")})}function p(t){var e=[];return t.forEach(function(t){var n=new Promise(function(e,n){var i=new XMLHttpRequest;i.onreadystatechange=function(){4===i.readyState&&200===i.status&&e(i.response)},i.open("GET",t,!0),i.responseType="arraybuffer",i.send(null)});e.push(n)}),e}function l(t,e,n){if(e=e.replace(/[/\\?%*:|"<>]/g,"_"),navigator.msSaveBlob){for(var i=decodeURIComponent(t.split(",")[1]),o=[],a=t.split(",")[0].split(":")[1].split(";")[0],r=0;r<i.length;r++)o.push(i.charCodeAt(r));var s=null;s=null!=n?n.msToBlob():new Blob([new Uint8Array(o)],{type:a}),navigator.msSaveBlob(s,e)}else{var p=document.createElement("a");p.download=e,p.href=t,document.body.appendChild(p),p.click(),document.body.removeChild(p)}y.onDone&&y.onDone()}function g(t,e,i){var a=n(t);if(a){null==e&&(e="chart"),o(a,i);var p=a.getElementsByTagName("image"),g=[];if(p)for(var d of p)(d.getAttribute("href")&&-1===d.getAttribute("href").indexOf("data:")||d.getAttribute("xlink:href")&&-1===d.getAttribute("xlink:href").indexOf("data:"))&&g.push(s(d));Promise.all(g).then(function(){var n=r(a,t);n='<?xml version="1.0" standalone="no"?>\r\n'+n;var i="data:image/svg+xml;charset=utf-8,"+encodeURIComponent(n);l(i,e+".svg")})}}function d(t,i,a,s){if("object"==typeof canvg){s=s.toLowerCase().replace("jpg","jpeg"),"png"!==s&&"jpeg"!==s&&(s="png");var p=n(t);if(p){null==i&&(i="chart");var g=document.createElement("canvas");a&&(a.width||a.height)||(a||(a={}),a.scale=10),o(p,a);var d=r(p,t);"jpeg"===s&&(d=d.replace(">",'><rect x="0" y="0" width="'+y.width+'" height="'+y.height+'" fill="'+y.transparentBackgroundReplace+'"/>'));var f=g.getContext("2d"),u=canvg.Canvg.fromString(f,d,{anonymousCrossOrigin:y.allowCrossOriginImages});u.start(),u.ready().then(function(){var t=g.toDataURL("image/"+s);l(t,i+"."+s,g)})}}else e("Error svg-export: PNG/JPEG export requires Canvg.js")}function f(t,e,n){d(t,e,n,"png")}function u(t,e,n){d(t,e,n,"jpeg")}function h(t,e,n){y.pdfOptions.addTitleToPage&&t.font(y.pdfOptions.pdfTextFontFamily).fontSize(y.pdfOptions.pdfTitleFontSize).text(e,{width:y.pdfOptions.pageLayout.size[0]-y.pdfOptions.pageLayout.margins.left-y.pdfOptions.pageLayout.margins.right}),SVGtoPDF(t,n,y.pdfOptions.pageLayout.margins.left,t.y+10,{width:y.width,height:y.height,preserveAspectRatio:"none",useCSS:y.useCSS}),""!==y.pdfOptions.chartCaption&&t.font(y.pdfOptions.pdfTextFontFamily).fontSize(y.pdfOptions.pdfCaptionFontSize).text(y.pdfOptions.chartCaption,y.pdfOptions.pageLayout.margins.left,y.pdfOptions.pageLayout.size[1]-y.pdfOptions.pageLayout.margins.bottom-4*y.pdfOptions.pdfCaptionFontSize,{width:y.pdfOptions.pageLayout.size[0]-y.pdfOptions.pageLayout.margins.left-y.pdfOptions.pageLayout.margins.right})}function c(t,i,a){if("function"==typeof PDFDocument&&"function"==typeof SVGtoPDF&&"function"==typeof blobStream){var g=n(t);if(g){null==i&&(i="chart"),o(g,a);var d=r(g,t,!1),f=new PDFDocument(y.pdfOptions.pageLayout),u=f.pipe(blobStream()),c=g.getElementsByTagName("image"),m=[];if(c)for(var w of c)(w.getAttribute("href")&&-1===w.getAttribute("href").indexOf("data:")||w.getAttribute("xlink:href")&&-1===w.getAttribute("xlink:href").indexOf("data:"))&&m.push(s(w));Promise.all(m).then(function(){if(y.pdfOptions.customFonts.length>0){var t=p(y.pdfOptions.customFonts.map(function(t){return t.url}));Promise.all(t).then(function(t){t.forEach(function(t,e){var n=y.pdfOptions.customFonts[parseInt(e,10)],i=d.querySelectorAll('[style*="'+n.fontName+'"]');i.forEach(function(t){t.style.fontFamily=n.fontName}),-1===n.url.indexOf(".ttc")&&-1===n.url.indexOf(".dfont")||!n.styleName?f.registerFont(n.fontName,t):f.registerFont(n.fontName,t,n.styleName)}),h(f,i,d),f.end()})}else h(f,i,d),f.end()}),u.on("finish",function(){var t=u.toBlobURL("application/pdf");l(t,i+".pdf")})}}else e("Error svg-export: PDF export requires PDFKit.js, blob-stream and SVG-to-PDFKit")}var m="1.2.0",y={};t.version=m,t.downloadSvg=g,t.downloadPng=f,t.downloadJpeg=u,t.downloadPdf=c,Object.defineProperty(t,"__esModule",{value:!0})});