UNPKG

@wayrisignals/kicad-webview

Version:

Interactive KiCad schematic, PCB and 3D viewer for the web, with Gerber, Excellon, linked views and offline HTML exports

4 lines (3 loc) • 28.2 kB
/*! KiCad WebView 1.2.1 | Copyright (c) 2026 wayri | SPDX-License-Identifier: MPL-2.0 | Source: https://github.com/wayri/kicad-webview/tree/v1.2.1 | Keep LICENSE and THIRD-PARTY-NOTICES.md; bundled dependencies retain their licenses. */ import{a as ye,b as xe}from"./chunk-FHIEKW7H.js";import{b as Zt}from"./chunk-FPOLRSV2.js";import{b as pe}from"./chunk-TZWR7E27.js";var be=pe((Jt,st)=>{(kt=>{"use strict";let It=kt.KiCadFileCompatibility||(typeof st=="object"&&st.exports?ye():null),zt=kt.KiCadDesignDiff||(typeof st=="object"&&st.exports?Zt():null),Qt=kt.KiCadDesignDiff||(typeof st=="object"&&st.exports?Zt():null),F={page:"#a0a0a0",symbol:"#840000",pin:"#840000",wire:"#008400",bus:"#000080",label:"#008400",text:"#000000"},Rt={A0:[1189,841],A1:[841,594],A2:[594,420],A3:[420,297],A4:[297,210],A5:[210,148],USLetter:[279.4,215.9],USLegal:[355.6,215.9],USLedger:[431.8,279.4]},Bt=kt.KiCadWorksheet||(typeof st=="object"&&st.exports?xe():null),te=[16,10,16,21,20,24,26,10,14,14,16,26,10,26,10,22,20,20,20,20,20,20,20,20,20,20,10,10,26,26,26,18,27,18,21,21,21,19,18,21,22,10,16,21,17,24,22,22,21,22,21,20,16,22,18,24,20,18,20,14,14,14,12,16,8,19,19,18,19,18,12,19,19,10,10,17,11,28,19,19,19,19,13,17,12,19,16,22,17,16,17,14,20,14,15];function Gt(i){let f=[],a="",m=0;for(let A of String(i).matchAll(/~\{([^{}]*)\}/g)){a+=String(i).slice(m,A.index);let z=a.length;a+=A[1],f.push({start:z,end:a.length}),m=A.index+A[0].length}return a+=String(i).slice(m),{text:a,bars:f}}function _t(i,f){return Array.from(Gt(i).text).reduce((a,m)=>a+(te[m.charCodeAt(0)-32]??21)*f/21,0)+f*.16}let ee=Number.isSafeInteger(zt?.MAX_RENDER_PRIMITIVES)&&zt.MAX_RENDER_PRIMITIVES>0?zt.MAX_RENDER_PRIMITIVES:15e4,c=i=>i&&typeof i=="object"&&!Array.isArray(i)?i.quoted:i,rt=i=>Array.isArray(i)?String(c(i[0])):"",q=(i,f)=>Array.isArray(i)?i.filter(a=>Array.isArray(a)&&rt(a)===f):[],b=(i,f)=>q(i,f)[0],P=(i,f)=>c(b(i,f)?.[1]),C=i=>typeof i=="number"&&Number.isFinite(i),$=(i,f)=>{let a=b(i,f),m=c(a?.[1]),A=c(a?.[2]);return C(m)&&C(A)?{x:m,y:A}:null},ft=i=>P(i,"uuid")??P(i,"tstamp")??null,At=i=>Math.round(i*1e6)/1e6;function tt(i,f){return Array.isArray(i)&&i.some(a=>{if(Array.isArray(a)&&rt(a)===f){let m=c(a[1]);return m===void 0||m===!0||m==="yes"||m==="true"||m===1}return!Array.isArray(a)&&!(a&&typeof a=="object")&&a===f})}function Pt(i={x:0,y:0},f=0,a="",m=!1){let A=f*Math.PI/180,z=Math.cos(A),v=Math.sin(A);return V=>{let H=V.x,Q=m?-V.y:V.y,ot=H*z+Q*v,w=-H*v+Q*z;return a==="x"&&(w=-w),a==="y"&&(ot=-ot),{x:At(i.x+ot),y:At(i.y+w)}}}function ct(i){let f=rt(i)==="effects"?i:b(i,"effects");return tt(i,"hide")||tt(f,"hide")||tt(b(f,"font"),"hide")}function ht(i,f=1.27){let a=rt(i)==="effects"?i:b(i,"effects"),m=b(a,"font"),A=b(m,"size"),z=c(A?.[1]),v=c(A?.[2]);return C(z)&&C(v)&&z>0&&v>0?At(v):f}function ut(i){let f=b(i,"effects"),a=b(f,"font"),m=b(a,"size"),A=c(m?.[1]),z=c(m?.[2]),v=b(f,"justify")?.slice(1).map(c)||[];return{bold:tt(a,"bold"),italic:tt(a,"italic"),...typeof P(f,"href")=="string"?{href:P(f,"href")}:{},...C(A)&&A>0?{fontWidth:A}:{},...C(z)&&z>0?{fontHeight:z}:{},...it(a)&&it(a)!=="rgba(0,0,0,0)"?{sourceColor:it(a),color:it(a)}:{},...P(a,"face")?{fontFace:P(a,"face")}:{},...P(a,"thickness")?{textThickness:P(a,"thickness")}:{},lineSpacing:1.5,align:v.includes("left")?"left":v.includes("right")?"right":"center",baseline:v.includes("top")?"top":v.includes("bottom")?"bottom":"middle"}}function Ct(i,f=.15){let a=b(i,"stroke"),m=P(a,"width")??P(i,"width");return C(m)&&m>0?m:f}function it(i){let f=b(i,"color")?.slice(1).map(c);if(!f||f.length!==4||f.some(v=>!C(v))||f.slice(0,3).some(v=>v<0||v>255))return;let[a,m,A,z]=f;return`rgba(${a},${m},${A},${Math.max(0,Math.min(1,z))})`}function Tt(i){let f=b(i,"stroke"),a=it(f),m=b(i,"fill"),A=it(m),z=P(f,"type");return{...a&&a!=="rgba(0,0,0,0)"?{sourceColor:a,color:a}:{},...A?{fillColor:A}:{},...["dash","dot","dash_dot","dash_dot_dot"].includes(z)?{lineStyle:z}:{}}}function $t(i){let f=P(b(i,"fill"),"type");return["outline","background","color","hatch","reverse_hatch","cross_hatch"].includes(f)?f:void 0}function ie(i){let f=b(i,"pts");return q(f,"xy").map(a=>$([a],"xy")).filter(Boolean)}let Lt=4*1024*1024,Dt=16e6,ne=8*1024*1024,se=16e6,re=Uint32Array.from({length:256},(i,f)=>{for(let a=0;a<8;a++)f=f&1?3988292384^f>>>1:f>>>1;return f>>>0});function oe(i,f){let a=b(i,"data")?.slice(1).map(c),m=0;if(!a?.length)throw Error("No inline bitmap data was saved in the schematic.");if(a.some(N=>typeof N!="string"||(m+=N.length)>Math.ceil(Lt/3)*4))throw Error("Embedded bitmap exceeds its 4 MiB limit or has invalid base64 data.");let A=a.join("");if(A.length%4||!/^[A-Za-z0-9+/]*={0,2}$/.test(A))throw Error("Embedded image is not base64 bitmap data.");let z=atob(A);if(z.length>Lt||btoa(z)!==A)throw Error("Embedded bitmap data is oversized or not canonical base64.");let v=Uint8Array.from(z,N=>N.charCodeAt(0)),V=new DataView(v.buffer);if(z.startsWith("BM"))return ae(V,A,f);if(v.length<45||[137,80,78,71,13,10,26,10].some((N,Y)=>v[Y]!==N))throw Error("Embedded image is not a PNG or BMP.");let H=N=>V.getUint32(N),Q=N=>String.fromCharCode(...v.subarray(N,N+4)),ot=[z.slice(0,8)],w=0,L=0,Et=null,Mt=!1,pt=!1,Ut=0;for(let N=8;N<v.length;){if(++Ut>4096||N+12>v.length)throw Error("Embedded PNG chunk structure is invalid.");let Y=H(N),K=Q(N+4),mt=N+12+Y;if(mt>v.length||!/^[A-Za-z]{4}$/.test(K))throw Error("Embedded PNG chunk is truncated or invalid.");let yt=4294967295;for(let wt=N+4;wt<mt-4;wt++)yt=re[(yt^v[wt])&255]^yt>>>8;if((yt^4294967295)>>>0!==H(mt-4))throw Error("Embedded PNG checksum is invalid.");if(K==="IHDR"){if(N!==8||Y!==13)throw Error("Embedded PNG has an invalid header.");w=H(N+8),L=H(N+12);let wt=v[N+16],Wt=v[N+17],bt={0:[1,2,4,8,16],2:[8,16],3:[1,2,4,8],4:[8,16],6:[8,16]};if(!w||!L||w*L>Dt||!bt[Wt]?.includes(wt)||v[N+18]!==0||v[N+19]!==0||v[N+20]>1)throw Error("Embedded PNG exceeds 16 megapixels or uses an invalid header.")}else{if(N===8)throw Error("Embedded PNG header is missing.");if(K==="pHYs"){if(Y!==9||Et!==null)throw Error("Embedded PNG physical resolution is invalid.");if(v[N+16]===1)Et=H(N+8);else if(v[N+16]!==0)throw Error("Embedded PNG resolution unit is invalid.")}else if(K==="IDAT")pt=!0;else if(K==="IEND"){if(Y||!pt)throw Error("Embedded PNG end marker is invalid.");Mt=!0}else{if(["acTL","fcTL","fdAT"].includes(K))throw Error("Animated PNGs are not supported in the bounded preview.");if(K[0]===K[0].toUpperCase()&&K!=="PLTE")throw Error("Embedded PNG has an unsupported critical chunk.")}}if(["iCCP","zTXt","iTXt"].includes(K)||ot.push(z.slice(N,mt)),Mt)break;N=mt}if(!Mt)throw Error("Embedded PNG end marker is missing.");return{dataUrl:"data:image/png;base64,"+btoa(ot.join("")),pixelWidth:w,pixelHeight:L,ppi:jt(Et,f),bytes:v.length}}function jt(i,f){let a=300;if(i!==null&&i/100>1){let m=Math.round(i*.0254),A=Math.floor(i/100)>1?Math.round(Math.floor(i/100)*2.54):m;a=f<=20230121?300:f<20260623?A:m}if(!Number.isFinite(a)||a<1||a>1e6)throw Error("Embedded PNG resolution exceeds the preview limit.");return a}function ae(i,f,a){let m=i.byteLength;if(m<54||i.getUint32(2,!0)!==m||i.getUint32(14,!0)!==40)throw Error("Embedded BMP has an unsupported or invalid header.");let A=i.getUint32(10,!0),z=i.getInt32(18,!0),v=Math.abs(i.getInt32(22,!0)),V=i.getUint16(28,!0);if(z<1||v<1||z*v>Dt||i.getUint16(26,!0)!==1||![24,32].includes(V)||i.getUint32(30,!0)!==0)throw Error("Embedded BMP must use uncompressed 24-bit or 32-bit pixels within the 16 megapixel limit.");let H=Math.ceil(z*V/32)*4*v;if(A<54||A+H!==m)throw Error("Embedded BMP pixel data is truncated or invalid.");return{dataUrl:"data:image/bmp;base64,"+f,pixelWidth:z,pixelHeight:v,ppi:jt(i.getInt32(38,!0),a),bytes:m}}function Xt(i,f=()=>{}){let a=b(i,"paper"),m=c(a?.[1])||"A4",A=Rt[m];if(m==="User"){let H=c(a?.[2]),Q=c(a?.[3]);C(H)&&C(Q)&&H>20&&Q>20&&H<2e3&&Q<2e3&&(A=[H,Q])}A||(f(`Unsupported paper size ${m}; showing A4 bounds.`),A=Rt.A4);let[z,v]=A;a?.some(H=>c(H)==="portrait")&&z>v&&([z,v]=[v,z]);let V=b(i,"title_block");return{width:z,height:v,format:m,titleBlock:{title:P(V,"title")||"",date:P(V,"date")||"",rev:P(V,"rev")||"",company:P(V,"company")||"",comments:Array.from({length:9},(H,Q)=>c(q(V,"comment").find(ot=>c(ot[1])===Q+1)?.[2])||"")}}}function le(i,f="",a={}){if(!Array.isArray(i)||rt(i)!=="kicad_sch")throw new Error("Schematic renderer requires a parsed kicad_sch root.");let m=[],A=[],z=new Set,v=0,V=0,H=0,Q=0,ot=It?.inspect(i);A.push(...ot?.warnings||[]);let w=t=>{z.has(t)||(z.add(t),A.push(t))};function L(t){if(m.length>=ee)throw new Error("Schematic has too many drawing primitives.");m.push({...t,path:f})}let Et=It?.textResolver(i,{...a,path:f},w),Mt=new Map;for(let t of[...q(i,"symbol"),...q(i,"sheet")])ft(t)&&Mt.set(ft(t),Et?.fields(t));let pt=(t,e={})=>Et?.resolve(t,Mt.get(e.ownerUuid||e.uuid)||{})??String(t??""),Ut=(t,e={})=>It?.textLinks?.(pt(t,e),e.href===void 0?void 0:pt(e.href,e))||[],N=Xt(i,w);function Y(t,e,r,o,n={}){t&&e&&L({type:"line",points:[t,e],color:r,width:o,...n})}function K(t,e,r,o,n,s={}){if(t&&e!==void 0&&e!==null&&String(e)){let x=Gt(pt(e,s)),l=Ut(e,s);if(L({type:"text",x:t.x,y:t.y,text:x.text,...x.bars.length||String(e)!==x.text?{rawText:String(e)}:{},size:r,angle:C(o)?o:0,color:n,...s,...l.length?{links:l}:{}}),x.bars.length){let y=s.fontWidth||r,d=s.fontHeight||r,M=x.text.split(` `),k=(o||0)*Math.PI/180,R=(E,S)=>({x:At(t.x+E*Math.cos(k)-S*Math.sin(k)),y:At(t.y+E*Math.sin(k)+S*Math.cos(k))}),B=0;for(let E=0;E<M.length;E++){let S=M[E],u=_t(S,y),g=s.align==="left"?0:s.align==="right"?-u:-u/2,_=(E-(M.length-1)/2)*d*(s.lineSpacing||1.5)+(s.baseline==="top"?d/2:s.baseline==="bottom"?-d/2:0)-d*1.0181;for(let I of x.bars){let U=Math.max(I.start-B,0),W=Math.min(I.end-B,S.length);if(W<=U)continue;let p=g+_t(S.slice(0,U),y)+y*.02,h=g+_t(S.slice(0,W),y)-y*.18;Y(R(p,_),R(h,_),n,s.textThickness||.1524,{...s,kind:"text-overbar",...["label","global_label","hierarchical_label","sheet-pin"].includes(s.kind)?{electricalKind:s.kind}:{}})}B+=S.length+1}}}}function mt(t,e,r,o,n={},s){!t||!e||L({type:"polygon",points:[t,{x:e.x,y:t.y},e,{x:t.x,y:e.y}],color:r,width:o,...s?{fill:s}:{},...n})}if(Bt)for(let t of a.worksheet?a.worksheet(N,f,a,pt,w):Bt.build(N,f,a))L(t);else w("Default worksheet module is unavailable.");!a.worksheet&&N.titleBlock.comments.slice(4).some(Boolean)&&w("The KiCad default drawing sheet displays only comments 1\u20134.");function yt(t,e,r,o=F.symbol){let n=rt(t),s=Ct(t,.15),x=$t(t);if(r={...r,...Tt(t)},n==="bezier"){let l=q(b(t,"pts"),"xy").map(y=>({x:c(y[1]),y:c(y[2])}));try{if(l.some(d=>!C(d.x)||!C(d.y)))throw Error("Invalid Bezier control.");let y=zt.cubicBezierPoints(l.map(e));if(v+y.length>15e4)throw Error("Bezier point budget reached.");v+=y.length,L({type:"polyline",points:y,color:o,width:s,...x?{fill:x}:{},...r})}catch{w("Invalid or over-budget bezier was skipped.")}}else if(n==="polyline"||n==="wire"||n==="bus"){let l=ie(t).map(e);l.length>1?L({type:"polyline",points:l,color:o,width:s,...x?{fill:x}:{},...r}):w(`${n} without two points was skipped.`)}else if(n==="rectangle"){let l=$(t,"start"),y=$(t,"end");if(!l||!y){w("Rectangle without start/end was skipped.");return}let d=[l,{x:y.x,y:l.y},y,{x:l.x,y:y.y}].map(e);L({type:"polygon",points:d,color:o,width:s,...x?{fill:x}:{},...r})}else if(n==="circle"){let l=$(t,"center"),y=P(t,"radius");if(!l||!C(y)||y<=0){w("Circle without center/radius was skipped.");return}let d=e(l);L({type:"circle",x:d.x,y:d.y,r:y,color:o,width:s,...x?{fill:x}:{},...r})}else if(n==="arc"){let l=$(t,"start"),y=$(t,"mid"),d=$(t,"end");if(!l||!y||!d){w("Legacy or incomplete arc without start/mid/end was skipped.");return}L({type:"arc",start:e(l),mid:e(y),end:e(d),color:o,width:s,...x?{fill:x}:{},...r})}else if(n==="text_box"||n==="table_cell"){if(ct(t))return;let l=$(t,"at")||$(t,"start"),y=$(t,"size"),M=$(t,"end")||(l&&y?{x:l.x+y.x,y:l.y+y.y}:null);if(!l||!M||l.x===M.x||l.y===M.y){w("Text box without valid position/size was skipped.");return}let k={...r};for(let J of["color","sourceColor","lineStyle","fillColor"])delete k[J];let R=Math.min(l.x,M.x),B=Math.max(l.x,M.x),E=Math.min(l.y,M.y),S=Math.max(l.y,M.y),u=ht(t),g=ut(t),_=P(b(t,"stroke"),"width"),I=!(C(_)&&_<0);L({type:"polygon",points:[{x:R,y:E},{x:B,y:E},{x:B,y:S},{x:R,y:S}].map(e),color:o,width:s,stroke:I,...x?{fill:x}:{},...r,...g.href!==void 0?{links:Ut(c(t[1]),{...r,href:g.href})}:{}});let U=b(t,"margins"),W=u*.75+(I?s/2:0),p=U?.slice(1).map(c);if(p&&(p.length!==4||p.some(J=>!C(J)||J<0))){w("Text box with invalid saved margins has no rendered text.");return}let[h,D,Z,at]=p||[W,W,W,W],G={left:R+h,top:E+D,right:B-Z,bottom:S-at};if(G.right<=G.left||G.bottom<=G.top){w("Text box margins leave no text area.");return}let T=((c(b(t,"at")?.[3])||0)%360+360)%360;if(T!==0&&T!==90){w("Text box with unsupported non-horizontal/non-vertical text angle has no rendered text.");return}let j=T===90,X=j?G.bottom-G.top:G.right-G.left,O=j?G.right-G.left:G.bottom-G.top,nt=pt(c(t[1]),r),lt=[];if(nt.length>65536){w("Text box exceeds its 65536-character preview limit; text was skipped.");return}for(let J of nt.split(/\r\n|\r|\n/)){let et="",xt=0;for(let Nt of J.match(/\S+\s*|\s+/g)||[]){let ue=_t(Nt,g.fontWidth||u);et&&xt+ue>X&&(lt.push(et.trimEnd()),et="",xt=0);for(let Ot of Nt){let Yt=_t(Ot,g.fontWidth||u)-(g.fontWidth||u)*.16;et&&xt+Yt>X&&(lt.push(et.trimEnd()),et="",xt=0),et+=Ot,xt+=Yt}}lt.push(et.trimEnd())}let vt=u*g.lineSpacing,dt=u+Math.max(0,lt.length-1)*vt,St=g.baseline==="top"?0:g.baseline==="bottom"?O-dt:(O-dt)/2,gt=g.align==="left"?0:g.align==="right"?X:X/2,qt=(J,et)=>j?{x:G.left+et,y:G.bottom-J}:{x:G.left+J,y:G.top+et};for(let J=0;J<lt.length;J++){let et=qt(gt,St+u/2+J*vt),xt=e(et),Nt=e(qt(gt+1,St+u/2+J*vt));K(xt,lt[J],u,Math.atan2(Nt.y-xt.y,Nt.x-xt.x)*180/Math.PI,o,{...k,...g,baseline:"middle"})}w("Text-box wrapping uses preview font metrics; exact KiCad font layout may differ.")}else if(n==="text"){if(ct(t))return;let l=$(t,"at");if(!l){w("Text without position was skipped.");return}let y=!!c(b(t,"at")?.[3]),d=e(l),M=e({x:l.x+1,y:l.y}),k=e({x:l.x,y:l.y+1}),R=y?k:M,B=R.x-d.x,E=R.y-d.y,S=Math.abs(E)>Math.abs(B),u=ut(t);(S?E>0:B<0)&&u.align!=="center"&&(u.align=u.align==="left"?"right":"left");let g=M.x-d.x,_=M.y-d.y,I=k.x-d.x,U=k.y-d.y,W=g*U-_*I>0,p=g>.5&&U>.5;W&&y!==p&&u.baseline!=="middle"&&(u.baseline=u.baseline==="top"?"bottom":"top"),K(d,c(t[1]),ht(t),S?-90:0,o,{...r,...u})}else w(`Unsupported graphic ${n} was skipped.`)}function wt(t,e){let r=P(t,"column_count"),o=b(t,"column_widths")?.slice(1).map(c),n=b(t,"row_heights")?.slice(1).map(c),s=q(b(t,"cells"),"table_cell");if(!Number.isInteger(r)||r<1||r>100||!o||o.length!==r||!n?.length||n.length>100||r*n.length>1e3||s.length!==r*n.length||[...o,...n].some(E=>!C(E)||E<=0)){w("Schematic table has invalid dimensions or exceeds the 1000-cell preview limit; it was skipped.");return}let x=[],l=new Set,y=0;for(let E=0;E<s.length;E++){let S=s[E],u=$(S,"at"),g=$(S,"size"),_=b(S,"span")?.slice(1).map(c)||[1,1],I=c(b(S,"at")?.[3])||0,U=Math.floor(E/r),W=E%r;if(I!==0){w("Rotated schematic tables are not rendered; open KiCad for their native layout.");return}if(_.length!==2||_.some(p=>!Number.isInteger(p)||p<0)||U+_[1]>n.length||W+_[0]>r||!u||!g||!C(u.x+g.x)||!C(u.y+g.y)||(y+=String(c(S[1])??"").length)>262144){w("Schematic table has invalid cell geometry/spans or exceeds its text budget; it was skipped.");return}if(_[0]===0||_[1]===0){if(!l.has(E)){w("Schematic table contains an unbound merged cell; it was skipped.");return}x.push(null);continue}if(g.x<=0||g.y<=0){w("Schematic table has invalid cell bounds; it was skipped.");return}for(let p=U;p<U+_[1];p++)for(let h=W;h<W+_[0];h++){let D=p*r+h;if(l.has(D)){w("Schematic table has overlapping spans; it was skipped.");return}l.add(D)}x.push({at:u,end:{x:u.x+g.x,y:u.y+g.y},span:_,row:U,col:W,cell:S})}let d=x.filter(Boolean),M=b(t,"border"),k=b(t,"separators"),R=E=>E;for(let E of d)yt([...E.cell.filter(S=>rt(S)!=="stroke"),["stroke",["width",-1]]],R,{...e,uuid:ft(E.cell)||e.uuid,ownerUuid:e.uuid,kind:"table-cell"},F.text);let B=(E,S,u)=>{(P(b(u,"stroke"),"width")??0)<0||Y(E,S,F.text,Ct(u),{...e,kind:"table-border",...Tt(u)})};for(let{at:E,end:S,span:u,row:g,col:_}of d){let I=g===0&&tt(M,"header")?M:tt(k,"cols")?k:null,U=g===0&&tt(M,"header")?M:tt(k,"rows")?k:null;I&&_+u[0]<r&&B({x:S.x,y:E.y},S,I),U&&g+u[1]<n.length&&B(S,{x:E.x,y:S.y},U)}if(tt(M,"external")){let E=Math.min(...d.map(I=>I.at.x)),S=Math.min(...d.map(I=>I.at.y)),u=Math.max(...d.map(I=>I.end.x)),g=Math.max(...d.map(I=>I.end.y)),_=[{x:E,y:S},{x:u,y:S},{x:u,y:g},{x:E,y:g}];for(let I=0;I<4;I++)B(_[I],_[(I+1)%4],M)}}let Wt=b(i,"lib_symbols"),bt=new Map;for(let t of q(Wt,"symbol")){let e=c(t[1]);typeof e=="string"&&bt.set(e,t)}function Ht(t,e,r,o=new Set){if(o.has(t)||o.size>8)return w(`Symbol inheritance cycle at ${t}.`),[];let n=bt.get(t);if(!n)return w(`Embedded library symbol ${t} is unavailable.`),[];o.add(t);let s=[],x=P(n,"extends");if(x){let l=t.includes(":")?t.slice(0,t.indexOf(":")+1):"",y=bt.has(x)?x:`${l}${x}`;s.push(...Ht(y,e,r,o))}for(let l of q(n,"symbol")){let y=c(l[1]),d=typeof y=="string"&&/_(\d+)_(\d+)$/.exec(y);if(!d){w(`Unrecognized library unit ${y} in ${t} was skipped.`);continue}let M=Number(d[1]),k=Number(d[2]);(M===0||M===e)&&(k===0||k===r)&&s.push(l)}return n.some(l=>Array.isArray(l)&&["pin","polyline","rectangle","circle","arc","bezier","text","text_box"].includes(rt(l)))&&s.push(n),s}function Ft(t,e=new Set){if(e.has(t)||e.size>8)return 1;e.add(t);let r=bt.get(t);if(!r)return 1;let o=1,n=P(r,"extends");if(n){let s=t.includes(":")?t.slice(0,t.indexOf(":")+1):"";o=Ft(bt.has(n)?n:s+n,e)}for(let s of q(r,"symbol")){let x=/_(\d+)_(\d+)$/.exec(c(s[1])||"");x&&(o=Math.max(o,Number(x[1])))}return o}function ce(t,e,r,o){let n=$(t,"at"),s=P(t,"length")??0;if(!n||!C(s)||s<0){w("Pin without valid position/length was skipped.");return}let x=c(b(t,"at")?.[3])??0;if(!C(x)){w("Pin with invalid angle was skipped.");return}let l=x*Math.PI/180,y=Math.cos(l),d=Math.sin(l),M={x:n.x+s*y,y:n.y+s*d},k=b(t,"name"),R=b(t,"number"),B=c(t[2]),E={...r,ownerUuid:r.uuid,kind:"pin",pinNumber:String(c(R?.[1])??""),pinName:String(c(k?.[1])??""),pinType:c(t[1]),pinStyle:B,connectPoint:e(n)};if(tt(t,"hide")||ct(t)){Y(e(n),e(n),F.pin,0,{...E,hidden:!0,stroke:!1,pick:!1});return}let u=.635,g=1.27,_=(j,X)=>({x:M.x+j*y-X*d,y:M.y+j*d+X*y}),I={...E,kind:"pin-marker"},U=j=>L({type:"polyline",points:j.map(e),color:F.pin,width:.1524,...I}),W=M;if(["inverted","inverted_clock"].includes(B)){let j=e(_(-u,0));L({type:"circle",x:j.x,y:j.y,r:u,color:F.pin,width:.1524,...I}),W=_(-g,0)}["clock","inverted_clock","clock_low"].includes(B)&&U([_(0,-u),_(g,0),_(0,u)]),["input_low","clock_low"].includes(B)&&U([_(-g,0),_(-g,g),_(0,0)]),B==="output_low"&&U([_(0,g),_(-g,0)]),B==="edge_clock_high"&&(U([_(0,-u),_(-g,0),_(0,u)]),W=_(-g,0)),B==="non_logic"&&(U([_(u,u),_(-u,-u)]),U([_(u,-u),_(-u,u)])),Y(e(n),e(W),F.pin,.1524,E),["line","inverted","clock","inverted_clock","input_low","clock_low","output_low","edge_clock_high","non_logic"].includes(B)||w("Pin graphic style "+B+" is simplified to a line.");let p=e(n),h=e(M),D=Math.abs(h.x-p.x)>=Math.abs(h.y-p.y),Z=D?0:-90,at=D?h.x>=p.x?"left":"right":h.y<=p.y?"left":"right",G=tt(b(o,"pin_names"),"hide"),T=tt(b(o,"pin_numbers"),"hide");if(!G&&k&&!ct(k)&&c(k[1])&&c(k[1])!=="~"){let j=P(b(o,"pin_names"),"offset")??.508,X=ht(k),O,nt;C(j)&&j>0?(O=e(_(j,0)),nt=at):(O={x:(p.x+h.x)/2-(D?0:X/2+.3556),y:(p.y+h.y)/2-(D?X/2+.3556:0)},nt="center"),K(O,c(k[1]),X,Z,"#008484",{...E,kind:"pin-name",...ut(k),align:nt})}if(!T&&R&&!ct(R)&&c(R[1])&&c(R[1])!=="~"){let j=ht(R),X=j/2+.3556,O=P(b(o,"pin_names"),"offset")??.508,nt=!G&&c(k?.[1])!=="~"&&O===0?-1:1,lt={x:(p.x+h.x)/2-(D?0:X*nt),y:(p.y+h.y)/2-(D?X*nt:0)};K(lt,c(R[1]),j,Z,"#a90000",{...E,kind:"pin-number",...ut(R),align:"center"})}}function de(t){let e=$(t,"at");if(!e){w("Placed symbol without position was skipped.");return}let r=P(t,"lib_name")??P(t,"lib_id")??c(t[1]);if(typeof r!="string"){w("Placed symbol without library ID was skipped.");return}let o=P(t,"unit")??1,n=P(t,"convert")??1,s=c(b(t,"at")?.[3])??0;[0,90,180,270].includes((s%360+360)%360)||w(`Symbol ${r} has non-orthogonal rotation ${s}.`);let x=c(b(t,"mirror")?.[1])||"";x&&!["x","y"].includes(x)&&w(`Symbol ${r} has unsupported mirror ${x}.`);let l=Pt(e,C(s)?s:0,x,!0),y=ft(t),d=q(t,"property").find(h=>c(h[1])==="Reference"),M=c(d?.[2])||"",k={uuid:y,reference:M,kind:"symbol"},R=bt.get(r);b(R,"power")&&(k.powerSymbol=!0);let B=new Map(q(t,"pin").map(h=>[String(c(h[1])),ft(h)])),E=Ht(r,C(o)?o:1,C(n)?n:1);E.length||w(`No drawable unit ${o} style ${n} for ${r}.`);let S=m.length,u=new Map;for(let h of E)for(let D of h){if(!Array.isArray(D))continue;let Z=rt(D);if(Z==="pin"){let at=m.length;ce(D,l,{...k,savedPinUuid:B.get(String(c(b(D,"number")?.[1])))},R||h);for(let G=at;G<m.length;G++){let T=m[G];if(!["pin-name","pin-number"].includes(T.kind))continue;let j=JSON.stringify([T.kind,T.x,T.y,T.angle,T.size,T.align,T.baseline,T.color,T.fontWidth,T.fontHeight,T.fontFace,T.bold,T.italic,T.textThickness,T.lineSpacing,T.href,T.connectPoint,T.kind==="pin-name"?T.text:null]),X=u.get(j);if(!X){u.set(j,T);continue}let O=X.pinNumbers||[X.pinNumber];O.includes(T.pinNumber)||O.push(T.pinNumber),X.pinNumbers=O,T.kind==="pin-number"&&(X.text=O.join(", ")),m.splice(G--,1)}}else["polyline","rectangle","circle","arc","bezier","text","text_box"].includes(Z)?yt(D,l,k):Z==="image"&&w(`Symbol graphic ${Z} is not rendered.`)}let g=1/0,_=1/0,I=-1/0,U=-1/0,W=h=>{!h||!C(h.x)||!C(h.y)||(g=Math.min(g,h.x),I=Math.max(I,h.x),_=Math.min(_,h.y),U=Math.max(U,h.y))};for(let h of m.slice(S))if(h.uuid===y&&!h.hidden)if(Array.isArray(h.points))for(let D of h.points)W(D);else h.type==="circle"?(W({x:h.x-h.r,y:h.y-h.r}),W({x:h.x+h.r,y:h.y+h.r})):h.type==="arc"&&(W(h.start),W(h.mid),W(h.end));C(g)||(g=e.x-1.5,I=e.x+1.5,_=e.y-1.5,U=e.y+1.5);let p=.4;L({type:"polygon",points:[{x:g-p,y:_-p},{x:I+p,y:_-p},{x:I+p,y:U+p},{x:g-p,y:U+p}],pick:!0,stroke:!1,fill:!1,...k});for(let h of q(t,"property")){if(ct(h))continue;let D=$(h,"at"),Z=c(h[2]);if(!D){w(`Visible property ${c(h[1])} has no position.`);continue}let at=c(b(h,"at")?.[3])||0,G=s+at,T=(G%180+180)%180,j=Pt({x:0,y:0},G,x),X=j({x:1,y:0}),O=j({x:0,y:1}),nt=T*Math.PI/180,lt=Math.cos(nt),vt=Math.sin(nt),dt=ut(h);if(X.x*lt-X.y*vt<0&&(dt.align={left:"right",right:"left"}[dt.align]||dt.align),O.x*vt+O.y*lt<0&&(dt.baseline={top:"bottom",bottom:"top"}[dt.baseline]||dt.baseline),c(h[1])==="Reference"&&Ft(r)>1&&Number.isInteger(o)&&o>0){let St="",gt=o;for(;gt>0;)gt--,St=String.fromCharCode(65+gt%26)+St,gt=Math.floor(gt/26);Z+=St}K(D,Z,ht(h),-T,F.symbol,{...k,kind:"property",property:c(h[1]),...dt})}}function Kt(t,e,r=rt(t)){if(ct(t))return;let o=$(t,"at");if(!o){w(`${r} without position was skipped.`);return}let n=pt(c(t[1]),e),s=ht(t),x=c(b(t,"at")?.[3])||0,l=r==="sheet-pin"?((x+180)%360+360)%360:x,y=P(t,"shape")||(r==="sheet-pin"?c(t[2]):"input"),d=ut(t),M=r==="global_label"?F.symbol:"#725600",k={...e,kind:r,connectPoint:o},R=Pt(o,l);["input","output","bidirectional","tri_state","passive"].includes(y)||w(`Unsupported ${r} shape ${y}; using passive outline.`);let E=r==="sheet-pin"&&{input:"output",output:"input"}[y]||y,S=["input","bidirectional","tri_state"].includes(E),u=["output","bidirectional","tri_state"].includes(E),g,_,I=-l;if(r==="hierarchical_label"||r==="sheet-pin"){let p=s/2;S&&u?g=[{x:0,y:0},{x:p,y:-p},{x:s,y:0},{x:p,y:p}]:S?g=[{x:0,y:0},{x:p,y:p},{x:s,y:p},{x:s,y:-p},{x:p,y:-p}]:u?g=[{x:s,y:0},{x:p,y:-p},{x:0,y:-p},{x:0,y:p},{x:p,y:p}]:g=[{x:0,y:-p},{x:s,y:-p},{x:s,y:p},{x:0,y:p}],_=R({x:(d.fontWidth||s)+.1905,y:0})}else{let p=s*.875,h=s,D=s*.375,Z=Math.max(s,_t(n||"",d.fontWidth||s)),at=Z+s*.8905+(S?p:0)+(u?p:0);g=[{x:0,y:0},{x:S?p:0,y:h},{x:at-(u?p:0),y:h},{x:at,y:0},{x:at-(u?p:0),y:-h},{x:S?p:0,y:-h}],_=R({x:D+(S?s*.75:0),y:0}),d.textWidth=Z}L({type:"polygon",points:g.map(R),color:M,width:.1524,...k});let U=(l%360+360)%360,W="left";(U===180||U===270)&&(I+=180,W="right"),K(_,n,s,I,M,{...k,...d,align:W,baseline:"middle"})}function fe(t){let e=$(t,"at"),r=$(t,"size"),o=ft(t);if(!e||!r||r.x<=0||r.y<=0){w("Sheet without valid position/size was skipped.");return}let n={uuid:o,kind:"sheet"};mt(e,{x:e.x+r.x,y:e.y+r.y},F.symbol,Ct(t),{...n,...Tt(t)},$t(t)||(it(b(t,"fill"))&&!it(b(t,"fill")).endsWith(",0)")?"color":void 0));for(let s of q(t,"property")){if(ct(s))continue;let x=$(s,"at");K(x,c(s[2]),ht(s),-(c(b(s,"at")?.[3])||0),F.symbol,{...n,kind:"sheet-property",property:c(s[1]),...ut(s)})}for(let s of q(t,"pin"))Kt(s,{...n,pinUuid:ft(s)},"sheet-pin")}function he(t,e){let r=$(t,"at"),o=c(b(t,"at")?.[3]),n=P(t,"length")??2.54,s=P(t,"shape")??"round";if(!r||![0,90,180,270].includes(o)||!C(n)||n<0||!["dot","round","diamond","rectangle"].includes(s)){w("Directive label with invalid position, orientation, length or shape was skipped.");return}let x=Pt(r,o%180===0?180:270),l="#484848",y=.1524,d=s==="dot"?.3556:s==="rectangle"?.4064:.508,M={...e,connectPoint:r};if(Y(r,x({x:0,y:n-d}),l,y,M),s==="round"||s==="dot"){let k=x({x:0,y:n});L({type:"circle",...k,r:d,color:l,width:y,...s==="dot"?{fill:"outline"}:{},...M})}else{let k=s==="diamond"?[{x:0,y:n-d},{x:-2*d,y:n},{x:0,y:n+d},{x:2*d,y:n}]:[{x:-2*d,y:n-d},{x:-2*d,y:n+d},{x:2*d,y:n+d},{x:2*d,y:n-d}];L({type:"polygon",points:k.map(x),color:l,width:y,...M})}for(let k of q(t,"property"))ct(k)||K($(k,"at"),c(k[2]),ht(k),-(c(b(k,"at")?.[3])||0),"#840084",{...M,...ut(k),property:c(k[1])})}let Vt=t=>t;for(let t of i){if(!Array.isArray(t))continue;let e=rt(t),r={uuid:ft(t),kind:e};if(e==="symbol")de(t);else if(e==="sheet")fe(t);else if(e==="table")wt(t,r);else if(e==="directive_label"||e==="netclass_flag")he(t,r);else if(e==="rule_area"){let o=Qt?.ruleAreaPoints(t);if(!o){w("Rule area without a valid polygon was skipped.");continue}let n=b(t,"polyline"),s=Tt(n),x=$t(n),l=P(b(n,"stroke"),"type");L({type:"polygon",points:o,color:"#ff0000",width:Ct(n,.1524),...l!=="solid"?{lineStyle:"dash"}:{},...x?{fill:x}:{},...r,...s,uuid:r.uuid??ft(n)})}else if(e==="wire"||e==="bus")yt(t,Vt,r,e==="bus"?F.bus:F.wire);else if(["polyline","rectangle","circle","arc","bezier","text_box"].includes(e))yt(t,Vt,r,F.text);else if(e==="junction"){let o=$(t,"at"),n=P(t,"diameter");o&&L({type:"circle",x:o.x,y:o.y,r:C(n)&&n>0?n/2:.5,color:F.wire,width:.15,fill:"outline",...r,...it(t)&&it(t)!=="rgba(0,0,0,0)"?{color:it(t),sourceColor:it(t)}:{}})}else if(e==="no_connect"){let o=$(t,"at");o&&(Y({x:o.x-.7,y:o.y-.7},{x:o.x+.7,y:o.y+.7},F.pin,.15,r),Y({x:o.x-.7,y:o.y+.7},{x:o.x+.7,y:o.y-.7},F.pin,.15,r))}else if(["label","global_label","hierarchical_label","text"].includes(e)){if(ct(t))continue;let o=$(t,"at");e==="global_label"||e==="hierarchical_label"?Kt(t,r):K(o,c(t[1]),ht(t),e==="label"?-(((c(b(t,"at")?.[3])||0)%180+180)%180):-(c(b(t,"at")?.[3])||0),e==="text"?F.text:F.label,{...r,...ut(t)})}else if(e==="bus_entry"){let o=$(t,"at"),n=$(t,"size");o&&n&&Y(o,{x:o.x+n.x,y:o.y+n.y},F.bus,.3,r)}else if(e==="image")try{let o=$(t,"at"),n=P(t,"scale")??1,s=P(t,"rotate")??0,x=P(t,"opacity")??1;if(!o||!C(n)||n<=0||!C(s)||!C(x)||x<0||x>1)throw Error("Embedded bitmap has invalid position, scale, rotation or opacity.");if(Q>=16)throw Error("Schematic exceeds its 16-image preview limit.");let l=oe(t,Number(P(i,"version"))||20230121),y=l.pixelWidth*l.pixelHeight;if(V+l.bytes>ne||H+y>se)throw Error("Schematic exceeds its total 8 MiB / 16 megapixel image preview budget.");let d=l.pixelWidth*25.4/l.ppi*n,M=l.pixelHeight*25.4/l.ppi*n;if(!C(d)||!C(M)||d>1e5||M>1e5)throw Error("Embedded bitmap physical size exceeds the preview limit.");let k=Pt(o,s);L({type:"image",dataUrl:l.dataUrl,pixelWidth:l.pixelWidth,pixelHeight:l.pixelHeight,points:[{x:-d/2,y:-M/2},{x:d/2,y:-M/2},{x:d/2,y:M/2},{x:-d/2,y:M/2}].map(k),opacity:x,...r}),V+=l.bytes,H+=y,Q++}catch(o){w("Schematic image was not rendered: "+o.message)}}return{primitives:m,page:N,warnings:A,compatibility:ot,embeddedFonts:It?.embeddedFonts?.(i)}}kt.KiCadSchematicRenderer={build:le,pageSettings:Xt},typeof st=="object"&&st.exports&&(st.exports=kt.KiCadSchematicRenderer)})(typeof globalThis=="object"?globalThis:Jt)});export default be();