import{systemInfo}from"../../common/utils";export default function generateBase64Url(t,{width:a,height:e,gapX:o,gapY:l,offsetLeft:r,offsetTop:n,rotate:s,alpha:i,watermarkContent:c,lineSpace:f,watermarkColor:h,layout:g},m){const d="hexagonal"===g,p=t.getContext("2d");if(!p)return console.warn("当前环境不支持Canvas, 无法绘制水印"),void m("");const u=systemInfo.pixelRatio||1;let v={width:o+a};const x=(o+a)*u,w=(l+e)*u,y=a*u,I=e*u,C=x,A=w,D=(o+a)*u,R=(l+e)*u;t.width=x,t.height=w,d&&(t.width=2*x,t.height=2*w,v={width:o+2*a+a/2}),p.translate(r*u,n*u),p.globalAlpha=i,p.fillStyle="transparent",p.fillRect(0,0,y,I);const S=(t,a,e,o)=>{t.translate(a,e),t.rotate(Math.PI/180*Number(o)),t.translate(-a,-e)},$=Array.isArray(c)?c:[Object.assign({},c)];let b=0,E=0,L=0;$.forEach(t=>{t.top=b,t.url?(b+=e,L+=d?2:1):t.text&&(b+=f)});const U=(o,l=0,r=0,n=0,i=0)=>{var c,f;if(o.url){const{url:c,isGrayscale:f=!1}=o,h=t.createImage();h.crossOrigin="anonymous",h.referrerPolicy="no-referrer",h.src=c,h.onload=()=>{var c,g;if(null===(c=p.save)||void 0===c||c.call(p),S(p,n,i,s),f){p.drawImage(h,l,r+o.top*u,a*u,e*u);const t=p.getImageData(0,0,p.canvas.width,p.canvas.height),n=t.data;for(let t=0;t<n.length;t+=4){const a=(n[t]+n[t+1]+n[t+2])/3;n[t]=a,n[t+1]=a,n[t+2]=a}p.putImageData(t,0,0)}else p.drawImage(h,l,r+o.top*u,a*u,e*u);null===(g=p.restore)||void 0===g||g.call(p),E+=1,E===L&&m(t.toDataURL(),v)}}else if(o.text){const{text:t,fontSize:a=16,fontFamily:e="normal",fontWeight:g="normal"}=o,m=(null==o?void 0:o.fontColor)||h;null===(c=p.save)||void 0===c||c.call(p),S(p,n,i,s),((t,a,e,o,l,r,n,s,i)=>{t.font=`normal normal ${r} ${n*u}px/${o}px ${s}`,t.fillStyle=i,t.textAlign="start",t.textBaseline="top",t.fillText(l,a,e)})(p,l,r+o.top*u,I,t,g,a,e,m),null===(f=p.restore)||void 0===f||f.call(p)}};$.forEach(t=>{U(t,0,0,0,0)}),d&&$.forEach(t=>{U(t,D,R,C,A)}),0===L&&m(t.toDataURL(),v)}