UNPKG

@wayrisignals/kicad-webview

Version:

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

3 lines (2 loc) • 4.39 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{d as L,f as Y}from"./chunk-AXOGACE7.js";import"./chunk-TZWR7E27.js";var te=t=>t?.quoted??t,r=(t,o)=>t?.find(l=>Array.isArray(l)&&l[0]===o),f=(t,o=0)=>typeof t=="number"&&Number.isFinite(t)&&Math.abs(t)<=1e5?t:o,p=t=>Math.trunc(f(t)*1e3)/1e3,q=2*1024*1024;function oe(t,o){if(typeof t!="string"||t.length>q||o.sourceByteLength(t)>q)throw new Error("Worksheet must be text within 2 MiB.");let l=o.parse(t);if(!["kicad_wks","page_layout"].includes(l[0]))throw new Error("Expected a KiCad worksheet template.");return(s,b,m,y,u)=>ne(l,s,b,m,y,u)}function ne(t,o,l,s,b,m){let y=r(t,"setup"),u=e=>f(r(y,e+"_margin")?.[1],10),x=u("left"),M=u("top"),N=o.width-u("right"),_=o.height-u("bottom"),$=(e,c,A)=>{if(typeof e?.[1]!="number"||typeof e?.[2]!="number")return null;let k=e[3]||"rbcorner";return["ltcorner","lbcorner","rtcorner","rbcorner"].includes(k)?{x:k[0]==="l"?x+p(e[1])+c:N-p(e[1])-c,y:k[1]==="t"?M+p(e[2])+A:_-p(e[2])-A}:null},{title:H="",date:J="",rev:O="",company:V="",comments:G=[]}=o.titleBlock||{},W={T:H,D:J,R:O,Y:V,Z:o.format,F:l.split("/").at(-1),S:s.sheetNumber??1,N:s.sheetCount??1,P:s.sheetName??"/",K:s.kicadVersion||"KiCad","%":"%"},Q={PAPER:o.format,"#":W.S,"##":W.N},w=[],C=0,R=0,U=r(y,"textsize"),P=e=>e.x>=x&&e.x<=N&&e.y>=M&&e.y<=_;for(let e of t){if(!Array.isArray(e)||["setup","version","generator","generator_version"].includes(e[0]))continue;let c=e[0];if(!["line","rect","polygon","tbtext"].includes(c)){m('Worksheet object "'+String(c).slice(0,80)+'" is not rendered.');continue}let A=r(e,"option")?.[1],k=String(s.sheetNumber??1)==="1";if(A==="page1only"&&!k||A==="notonpage1"&&k)continue;let z=Math.max(1,Math.trunc(f(r(e,"repeat")?.[1],1)));if(z>1e3)throw new Error("Worksheet repeat count exceeds 1,000.");let F=p(r(e,"incrx")?.[1]),K=p(r(e,"incry")?.[1]),j=f(r(e,"rotate")?.[1]),B=j*Math.PI/180,D=Math.cos(B),I=Math.sin(B),E=r(e,"font"),T=r(E,"size")||U,v=r(e,"justify"),X=r(e,c==="line"||c==="rect"?"start":"pos"),ee=r(e,"end"),S={frame:!0,kind:"custom-worksheet",color:"#a0a0a0",width:f(r(e,"linewidth")?.[1],f(r(y,"linewidth")?.[1],.15))||.15};for(let a=0;a<z;a++){let d=$(X,F*a,K*a),g=$(ee,F*a,K*a);if(!(!d||a>0&&c!=="polygon"&&(!P(d)||g&&!P(g)))){if(c==="line"&&g&&w.push({...S,type:"line",points:[d,g]}),c==="rect"&&g&&w.push({...S,type:"polygon",points:[d,{x:g.x,y:d.y},g,{x:d.x,y:g.y}]}),c==="polygon")for(let n of e){if(!Array.isArray(n)||n[0]!=="pts")continue;if(C+=n.length-1,C>1e5)throw new Error("Worksheet exceeds 100,000 polygon vertices.");let h=n.slice(1).filter(i=>Array.isArray(i)&&i[0]==="xy").map(([,i,Z])=>({x:d.x+p(i)*D+p(Z)*I,y:d.y-p(i)*I+p(Z)*D}));h.length>=2&&(!a||h.every(P))&&w.push({...S,type:"polygon",points:h,fill:h.length>2})}if(c==="tbtext"){let n=String(te(e[1])??"");if(a&&!/[%$\n]/.test(n)){let h=a*f(r(e,"incrlabel")?.[1],1);n=/\d+$/.test(n)?n.replace(/\d+$/,i=>String(Number(i)+h)):n.replace(/[A-Za-z]$/,i=>String.fromCharCode(i.charCodeAt(0)+h))}if(t[0]==="page_layout"&&(n=n.replace(/%([C][0-8]|[TDRYZFSNPK%])/g,(h,i)=>i.length===2?G[Number(i[1])]??"":W[i]??"")),n=b(n.replace(/\$\{(PAPER|##?)\}/g,(h,i)=>String(Q[i]??h))),R+=n.length,R>25e4)throw new Error("Worksheet exceeds its display text budget.");n&&w.push({...S,type:"text",...d,text:n,angle:-j,fontWidth:f(T?.[1],1.5),fontHeight:f(T?.[2],1.5),align:v?.includes("right")?"right":v?.includes("center")?"center":"left",baseline:v?.includes("top")?"top":v?.includes("bottom")?"bottom":"middle",bold:E?.includes("bold"),italic:E?.includes("italic")})}if(w.length>1e4)throw new Error("Worksheet exceeds 10,000 drawing elements.")}}}return w}function ie(t,o,l){let s=t.find(b=>b.path.replace(/\.kicad_pro$/i,"")===o.replace(/\.kicad_(pcb|sch)$/i,""))||(t.length===1?t[0]:null);if(s){let b=s.path.split("/").slice(0,-1),m=/\.kicad_pcb$/i.test(o)?s.pcb:s.sch;if(typeof m=="string"&&m){let y=m.replace(/^\$\{KIPRJMOD\}[\\/]/,"").replaceAll("\\","/"),u=[...b,y].join("/");Y(y)&&l.files.some(x=>x.path===u&&L(x.path))?l.worksheet=u:l.notes.push("The saved project worksheet is not in the loaded files. Include the template or supply worksheetSource.")}}}export{ie as applySavedWorksheet,oe as parseWorksheet,q as worksheetLimit};