Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

105 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
// Generated by sdk/scripts/build-worker.mjs from src/image/recipeWorker.ts: do not edit.
/** The recipe worker's script: what the workers that draw texture recipes are started from. */
export const RECIPE_WORKER_SOURCE = "(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let t=(e,t)=>[e[0]+t[0],e[1]+t[1],e[2]+t[2]],n=(e,t)=>[e[0]-t[0],e[1]-t[1],e[2]-t[2]],r=(e,t)=>[e[0]*t,e[1]*t,e[2]*t],i=(e,t)=>e[0]*t[0]+e[1]*t[1]+e[2]*t[2],a=(e,t)=>[e[1]*t[2]-e[2]*t[1],e[2]*t[0]-e[0]*t[2],e[0]*t[1]-e[1]*t[0]],o=e=>Math.hypot(e[0],e[1],e[2]),s=e=>{let t=o(e);return t>0?[e[0]/t,e[1]/t,e[2]/t]:[0,0,0]},c=Math.PI/180;function l(e){let t=[],n=new Set(e.joints.filter(e=>e.mirror).map(e=>e.name));for(let r of e.joints){if(!r.mirror){t.push({name:r.name,at:r.at,parentName:r.parent});continue}let e={x:0,y:1,z:2}[r.mirror],i=[...r.at];i[e]=-i[e];let a=r.at[e]>=0,o=e=>r.parent===void 0?void 0:n.has(r.parent)?`${r.parent}.${e}`:r.parent;t.push({name:`${r.name}.L`,at:a?r.at:i,parentName:o(`L`)}),t.push({name:`${r.name}.R`,at:a?i:r.at,parentName:o(`R`)})}if(t.length===0)throw Error(`a rig needs at least one joint`);if(t.length>256)throw Error(`this rig has ${t.length} joints; a mesh can be posed by at most 256`);let r=new Map;t.forEach((e,t)=>{if(r.has(e.name))throw Error(`there are two joints called \"${e.name}\"`);r.set(e.name,t)});let i=t.map(e=>{if(e.parentName===void 0)return-1;let n=r.get(e.parentName);if(n===void 0)throw Error(`joint \"${e.name}\" has parent \"${e.parentName}\", which is not a joint; there are ${t.map(e=>e.name).join(`, `)}`);return n});if(i.filter(e=>e<0).length!==1)throw Error(`a rig has one root; these have no parent: ${t.filter((e,t)=>i[t]<0).map(e=>e.name).join(`, `)}`);let a=[],o=new Set;for(;a.length<t.length;){let e=a.length;if(t.forEach((e,t)=>{!o.has(t)&&(i[t]<0||o.has(i[t]))&&(a.push(t),o.add(t))}),a.length===e)throw Error(`the joints' parents go round in a loop`)}let s=new Map(a.map((e,t)=>[e,t])),c=a.map(e=>({name:t[e].name,at:t[e].at,parent:i[e]<0?-1:s.get(i[e]),children:[]}));return c.forEach((e,t)=>{e.parent>=0&&c[e.parent].children.push(t)}),c}let u=(e,t)=>[e[3]*t[0]+e[0]*t[3]+e[1]*t[2]-e[2]*t[1],e[3]*t[1]-e[0]*t[2]+e[1]*t[3]+e[2]*t[0],e[3]*t[2]+e[0]*t[1]-e[1]*t[0]+e[2]*t[3],e[3]*t[3]-e[0]*t[0]-e[1]*t[1]-e[2]*t[2]];function d(e,t,n){let r=(e,t)=>{let n=t*c/2,r=[0,0,0,Math.cos(n)];return r[e]=Math.sin(n),r};return u(r(2,n),u(r(1,t),r(0,e)))}function f(e,n){let i=[e[0],e[1],e[2]],o=r(a(i,n),2);return t(t(n,r(o,e[3])),a(i,o))}function p(e,t,n,r,i,a=!1){let o=e.map(e=>[e[0]+i*n,e[1],e[2],e[3]]),s=e=>(e%n+n)%n,c=(r?o.map(e=>[s(e[0]),e[1],e[2],e[3]]):o).slice().sort((e,t)=>e[0]-t[0]),l=c.length;if(l===1||!r&&t<=c[0][0])return[c[0][1],c[0][2],c[0][3]];if(!r&&t>=c[l-1][0])return[c[l-1][1],c[l-1][2],c[l-1][3]];let u=e=>{if(r){let t=Math.floor(e/l),r=c[(e%l+l)%l];return[r[0]+t*n,r[1],r[2],r[3]]}return c[Math.max(0,Math.min(l-1,e))]},d=r?-1:0,f=r?s(t):t;for(;u(d+1)[0]<=f;)d++;let[p,m,h,g]=[u(d-1),u(d),u(d+1),u(d+2)],_=h[0]-m[0],v=_>0?(f-m[0])/_:0;if(a)return[1,2,3].map(e=>m[e]+(h[e]-m[e])*v);let[y,b]=[v*v,v*v*v];return[1,2,3].map(e=>{let[t,n]=[m[e],h[e]],r=(n-p[e])/Math.max(h[0]-p[0],1e-6)*_,i=(g[e]-t)/Math.max(g[0]-m[0],1e-6)*_;return(2*b-3*y+1)*t+(b-2*y+v)*r+(-2*b+3*y)*n+(b-y)*i})}let m=e=>[-e[0],-e[1],-e[2],e[3]];function h(e,t){let n=s(e),r=s(t),o=i(n,r);if(o>1-1e-9)return[0,0,0,1];if(o<-1+1e-9){let e=s(a(n,Math.abs(n[0])<.9?[1,0,0]:[0,1,0]));return[e[0],e[1],e[2],0]}let c=a(n,r),l=1+o,u=Math.hypot(c[0],c[1],c[2],l);return[c[0]/u,c[1]/u,c[2]/u,l/u]}function g(e,n){let r=[];return e.forEach((e,i)=>{let a=n[i];if(e.parent<0)r[i]={q:a.q,origin:a.offset};else{let n=r[e.parent];r[i]={q:u(n.q,a.q),origin:t(n.origin,f(n.q,a.offset))}}}),r}function _(e,r,i,a){let o=r?.loop!==!1,s=(e,t)=>p(e,i,r.duration,o,t.phase??0,t.linear===!0),c=e.map(i=>{let a=r?.tracks?.find(e=>e.joint===i.name),o=a?.rotate&&!a.reach?s(a.rotate,a):[0,0,0],c=a?.move?s(a.move,a):[0,0,0],l=i.parent>=0?n(i.at,e[i.parent].at):i.at;return{q:d(o[0],o[1],o[2]),offset:t(l,c)}}),l=g(e,c);for(let t of r?.tracks??[]){if(!t.reach)continue;let n=e.findIndex(e=>e.name===t.joint),r=t.rotate?s(t.rotate,t):[0,0,0];l=v(e,c,l,n,s(t.reach,t),t.pole,d(r[0],r[1],r[2]))}return a&&(l=b(e,c,l,a)),{local:c,world:l}}function v(e,a,c,l,d,p,_){let v=e[l].parent,y=e[v].parent,b=e[y].parent,x=c[y].origin,S=c[v].origin,C=c[l].origin,w=o(n(e[v].at,e[y].at)),T=o(n(e[l].at,e[v].at)),E=n(d,x),D=Math.abs(w-T)+1e-4,O=w+T-1e-4,k=Math.max(D,Math.min(O,o(E))),A=s(E),ee=n(S,t(x,r(A,i(n(S,x),A)))),te=p??(o(ee)>w*.01?ee:[0,0,1]),j=n(te,r(A,i(te,A)));o(j)<1e-6&&(j=n([1,0,0],r(A,A[0])));let M=(w*w-T*T+k*k)/(2*k),N=Math.sqrt(Math.max(0,w*w-M*M)),ne=t(t(x,r(A,M)),r(s(j),N)),re=t(x,r(A,k)),ie=h(n(S,x),n(ne,x)),P=u(ie,c[y].q),F=u(h(f(ie,n(C,S)),n(re,ne)),u(ie,c[v].q)),I=b>=0?c[b].q:[0,0,0,1],ae=_??u(m(c[v].q),c[l].q);return a[y]={...a[y],q:u(m(I),P)},a[v]={...a[v],q:u(m(P),F)},a[l]={...a[l],q:_?u(m(F),ae):a[l].q},g(e,a)}let y=Math.PI/4;function b(e,i,a,o){let c=a,l=t=>t===void 0?-1:e.findIndex(e=>e.name===t),d=r=>{if(r===void 0)return null;let i=l(r);if(i>=0)return c[i].origin;let a=o.goals.find(e=>e.name===r);if(!a)return null;let s=l(a.parent);return s<0?a.at:t(c[s].origin,f(c[s].q,n(a.at,e[s].at)))};if(o.ground){let n=o.ground.height??0,[a,p]=o.ground.slope??[0,0],_=s([-a,1,-p]),b=o.ik.filter(e=>e.kind===`foot-plant`).map(e=>{let t=l(e.tip),r=c[t].origin;return{k:e,foot:t,at:r,q:c[t].q,lift:n+a*r[0]+p*r[2]}}),x=new Map;for(let e of b)e.k.pelvis&&x.set(e.k.pelvis,Math.min(x.get(e.k.pelvis)??0,e.lift*(e.k.weight??1)));for(let[n,r]of x){if(r>=0)continue;let a=l(n),o=e[a].parent,s=o>=0?f(m(c[o].q),[0,r,0]):[0,r,0];i[a]={...i[a],offset:t(i[a].offset,s)}}x.size&&(c=g(e,i));let S=Math.acos(Math.min(1,_[1])),C=S>y?s(t(r([0,1,0],Math.sin(S-y)),r(_,Math.sin(y)))):_;for(let t of b){let n=Math.min(1,Math.max(0,t.k.weight??1)),r=[t.at[0],t.at[1]+t.lift*n,t.at[2]],a=t.k.align===!1?void 0:u(h([0,1,0],C),t.q);c=v(e,i,c,t.foot,r,d(t.k.pole)??void 0,a)}}for(let t of o.ik){if(t.kind===`foot-plant`)continue;let r=l(t.tip),a=d(t.target);if(!(r<0||!a))if(t.kind===`two-bone`){let o=d(t.pole);c=v(e,i,c,r,a,o?n(o,c[e[e[r].parent].parent].origin):void 0)}else{let o=t.axis===`x`?[1,0,0]:t.axis===`y`?[0,1,0]:[0,0,1],s=f(c[r].q,o),l=u(h(s,n(a,c[r].origin)),c[r].q),d=e[r].parent;i[r]={...i[r],q:u(m(d>=0?c[d].q:[0,0,0,1]),l)},c=g(e,i)}}return c}function x(e,t,n,r){let{world:i}=_(e,t,n,r);return Object.fromEntries(e.map((e,t)=>[e.name,i[t].origin]))}function S(e,t,n,r){let{world:i}=_(e,t,n,r);return Object.fromEntries(e.map((e,t)=>{let{origin:n,q:r}=i[t];return[e.name,{at:n,x:f(r,[1,0,0]),y:f(r,[0,1,0]),z:f(r,[0,0,1])}]}))}let C=1.6,w=62*Math.PI/180;function T(e={}){let t=(e.height??1.6)/C,n=(e,n,r)=>[e*t,n*t,r*t],r=[Math.sin(w),-Math.cos(w),0],i=.158*(e.shoulders??1),a=e=>n(i+r[0]*e,1.268+r[1]*e,-.018),o=.595,s=[{name:`index`,z:.027,length:.078,radius:.0085},{name:`middle`,z:.009,length:.086,radius:.0088},{name:`ring`,z:-.009,length:.08,radius:.0082},{name:`little`,z:-.026,length:.062,radius:.0072}].map(e=>{let n=a(o);return{name:e.name,base:[n[0],n[1],n[2]+e.z*t],length:e.length*t,radius:e.radius*t}}),c=a(.505),l=[c[0]+r[0]*.022*t,c[1]+r[1]*.022*t-.004*t,c[2]+.028*t],u=[l[0]+r[0]*.038*t,l[1]+r[1]*.038*t-.006*t,l[2]+.024*t],d=[u[0]+r[0]*.036*t,u[1]+r[1]*.036*t-.006*t,u[2]+.01*t];return{k:t,along:r,shoulder:a(0),elbow:a(.27),wrist:c,knuckle:a(o),hip:n(.08,.81,-.004),knee:n(.068,.445,.006),ankle:n(.067,.078,-.016),ball:n(.072,.022,.09),toe:n(.072,.018,.165),pelvis:n(0,.86,-.004),waist:n(0,1,0),chest:n(0,1.16,-.008),neck:n(0,1.32,-.022),head:n(0,1.405,-.01),crown:n(0,C,-.012),fingers:s,thumb:{base:l,mid:u,tip:d}}}function E(e={}){let t=T(e),n=(e,t,n)=>[e[0]+(t[0]-e[0])*n,e[1]+(t[1]-e[1])*n,e[2]+(t[2]-e[2])*n],r=[.03*t.k,1.29*t.k,-.008*t.k],i=[{name:`hips`,at:t.pelvis},{name:`spine`,at:t.waist,parent:`hips`},{name:`chest`,at:t.chest,parent:`spine`},{name:`neck`,at:t.neck,parent:`chest`},{name:`head`,at:t.head,parent:`neck`},{name:`crown`,at:t.crown,parent:`head`},{name:`clavicle`,at:r,parent:`chest`,mirror:`x`},{name:`upperArm`,at:t.shoulder,parent:`clavicle`,mirror:`x`},{name:`forearm`,at:t.elbow,parent:`upperArm`,mirror:`x`},{name:`hand`,at:t.wrist,parent:`forearm`,mirror:`x`},{name:`thumb1`,at:t.thumb.base,parent:`hand`,mirror:`x`},{name:`thumb2`,at:t.thumb.mid,parent:`thumb1`,mirror:`x`},{name:`thumb3`,at:t.thumb.tip,parent:`thumb2`,mirror:`x`},{name:`thigh`,at:t.hip,parent:`hips`,mirror:`x`},{name:`shin`,at:t.knee,parent:`thigh`,mirror:`x`},{name:`foot`,at:t.ankle,parent:`shin`,mirror:`x`},{name:`toes`,at:t.ball,parent:`foot`,mirror:`x`},{name:`toeTip`,at:t.toe,parent:`toes`,mirror:`x`}];for(let e of t.fingers){let r=`hand`;for(let a=0;a<4;a++){let o=`${e.name}${a+1}`,s=[0,.45,.75,1][a];i.push({name:o,at:n(e.base,[e.base[0]+t.along[0]*e.length,e.base[1]+t.along[1]*e.length,e.base[2]],s),parent:r,mirror:`x`}),r=o}}return i}function D(e){if(e.humanoid===void 0)throw Error(\"`humanoid` says whose bones: true, or the build it was given\");let t=l({joints:E(e.humanoid===!0?{}:e.humanoid)}),n=new Set(t.map(e=>e.name)),r=e=>{if(!n.has(e))throw Error(`no joint \"${e}\"; there are ${[...n].join(`, `)}`)},i=new Map;for(let[t,n]of Object.entries(e.rotate??{})){if(r(t),n.length!==3)throw Error(`rotate of \"${t}\" must be [x, y, z] degrees`);i.set(t,{rotate:[[0,n[0],n[1],n[2]]]})}for(let[t,n]of Object.entries(e.move??{})){if(r(t),n.length!==3)throw Error(`move of \"${t}\" must be [x, y, z]`);i.set(t,{...i.get(t),move:[[0,n[0],n[1],n[2]]]})}for(let[t,n]of Object.entries(e.reach??{})){if(r(t),n.at?.length!==3)throw Error(`reach of \"${t}\" needs at: [x, y, z]`);i.set(t,{...i.get(t),reach:[[0,n.at[0],n.at[1],n.at[2]]],...n.pole?{pole:n.pole}:{}})}return{joints:t,animation:{name:`pose`,duration:1,loop:!1,tracks:[...i].map(([e,t])=>({joint:e,...t}))}}}function O(e){let{joints:t,animation:n}=D(e);return x(t,n,0)}function k(e){let{joints:t,animation:n}=D(e);return S(t,n,0)}let A=[`torso`,`arm`,`hand`,`leg`,`foot`,`neck`,`head`],ee=[`crown`,`head`,`neck`,`chest`,`spine`,`hips`,`upperArm.L`,`forearm.L`,`hand.L`,`middle4.L`,`upperArm.R`,`forearm.R`,`hand.R`,`middle4.R`,`thigh.L`,`shin.L`,`foot.L`,`toeTip.L`,`thigh.R`,`shin.R`,`foot.R`,`toeTip.R`],te=[[`hips`,`spine`],[`spine`,`chest`],[`chest`,`neck`],[`neck`,`head`],[`head`,`crown`],[`chest`,`upperArm.L`],[`upperArm.L`,`forearm.L`],[`forearm.L`,`hand.L`],[`hand.L`,`middle4.L`],[`chest`,`upperArm.R`],[`upperArm.R`,`forearm.R`],[`forearm.R`,`hand.R`],[`hand.R`,`middle4.R`],[`hips`,`thigh.L`],[`thigh.L`,`shin.L`],[`shin.L`,`foot.L`],[`foot.L`,`toeTip.L`],[`hips`,`thigh.R`],[`thigh.R`,`shin.R`],[`shin.R`,`foot.R`],[`foot.R`,`toeTip.R`]],j=(e,t,n)=>[e[0]+t[0]*n,e[1]+t[1]*n,e[2]+t[2]*n],M=(e,t,n)=>[e[0]+(t[0]-e[0])*n,e[1]+(t[1]-e[1])*n,e[2]+(t[2]-e[2])*n],N=e=>{let t=Math.hypot(e[0],e[1],e[2])||1;return[e[0]/t,e[1]/t,e[2]/t]},ne=(e,t)=>[e[1]*t[2]-e[2]*t[1],e[2]*t[0]-e[0]*t[2],e[0]*t[1]-e[1]*t[0]];function re(e,t){let n=t=>e[t],[r,i,a,o,s]=[n(`hips`),n(`spine`),n(`chest`),n(`neck`),n(`head`)],[c,l,u,d,f,p]=[t.hips??1,t.waist??1,t.chest??1,t.shoulders??1,t.limbs??1,t.head??1],m=[{tag:`torso`,rings:[{at:j(r.at,r.y,-.1),across:r.x,deep:r.z,a:.09*c,d:.06},{at:j(r.at,r.y,-.02),across:r.x,deep:r.z,a:.135*c,d:.085},{at:M(r.at,i.at,.6),across:i.x,deep:i.z,a:.112*l,d:.07},{at:i.at,across:i.x,deep:i.z,a:.1*l,d:.064},{at:a.at,across:a.x,deep:a.z,a:.128*u,d:.09},{at:M(a.at,o.at,.62),across:a.x,deep:a.z,a:.15*d,d:.078},{at:M(a.at,o.at,.92),across:a.x,deep:a.z,a:.075,d:.06}]}],h=(e,t,n)=>{let r=N(ne(t,Math.abs(t[1])>.9?[1,0,0]:[0,1,0]));return{at:e,across:r,deep:N(ne(t,r)),a:n*f,d:n*f}},g=(e,t,r)=>{let i=t.map(e=>n(e).at),a=[];for(let e=0;e<i.length;e++){let t=N(e+1<i.length?[i[e+1][0]-i[e][0],i[e+1][1]-i[e][1],i[e+1][2]-i[e][2]]:[i[e][0]-i[e-1][0],i[e][1]-i[e-1][1],i[e][2]-i[e-1][2]]);e===0&&a.push(h(j(i[0],t,-r[0]*.8),t,r[0]*.55)),a.push(h(i[e],t,r[e])),e===i.length-1&&a.push(h(j(i[e],t,r[e]*.8),t,r[e]*.55))}m.push({tag:e,rings:a})};for(let e of[`L`,`R`]){g(`arm`,[`upperArm.${e}`,`forearm.${e}`,`hand.${e}`],[.038,.029,.021]),g(`leg`,[`thigh.${e}`,`shin.${e}`,`foot.${e}`],[.072,.046,.03]);let[t,r]=[n(`hand.${e}`),n(`middle4.${e}`)];m.push({tag:`hand`,rings:[{at:t.at,across:t.z,deep:t.y,a:.026,d:.016},{at:M(t.at,r.at,.45),across:t.z,deep:t.y,a:.04,d:.017},{at:r.at,across:t.z,deep:t.y,a:.022,d:.01}]});let[i,a]=[n(`foot.${e}`),n(`toeTip.${e}`)],o=N([a.at[0]-i.at[0],a.at[1]-i.at[1],a.at[2]-i.at[2]]),s=N(ne(o,[1,0,0]));m.push({tag:`foot`,rings:[{at:j(i.at,o,-.05),across:[1,0,0],deep:s,a:.022,d:.026},{at:i.at,across:[1,0,0],deep:s,a:.034,d:.04},{at:M(i.at,a.at,.7),across:[1,0,0],deep:s,a:.04,d:.022},{at:a.at,across:[1,0,0],deep:s,a:.022,d:.01}]})}g(`neck`,[`neck`,`head`],[.042,.038]);let _=M(s.at,n(`crown`).at,.46),v=[];for(let e=0;e<=8;e++){let t=e/8*2-1,n=Math.sqrt(Math.max(1e-4,1-t*t));v.push({at:j(j(_,s.y,t*.108*p),s.z,.006),across:s.x,deep:s.z,a:.074*p*n,d:.09*p*n})}return m.push({tag:`head`,rings:v}),m}let ie=e=>{let t=Math.hypot(e[0],e[1],e[2])||1;return[e[0]/t,e[1]/t,e[2]/t]};function P(e,t,n,r,i){let[a,o]=r(e.at),s=e.height/1.6*i,c=(e.yaw??0)*Math.PI/180,l=(e.pitch??0)*Math.PI/180,[u,d,f,p]=[Math.cos(c),Math.sin(c),Math.cos(l),Math.sin(l)],m=e=>{let[t,n,r]=[e[0]*u+e[2]*d,e[1],-e[0]*d+e[2]*u];return[t,n*f-r*p,n*p+r*f]},h=e=>{let[t,n]=[e[1]*f+e[2]*p,-e[1]*p+e[2]*f];return[e[0]*u-n*d,t,e[0]*d+n*u]},g=e=>{let t=m(e);return[a+t[0]*s,o-t[1]*s]},_=e.build??{},v=e.pose??{},y={humanoid:_,...v.rotate?{rotate:v.rotate}:{},...v.move?{move:v.move}:{}},b=Object.entries(v.reach??{}),x=y;if(b.length){let e=O(y);x={...y,reach:Object.fromEntries(b.map(([t,n])=>{if(!e[t])throw Error(`no joint \"${t}\"; there are ${Object.keys(e).join(`, `)}`);return[t,{at:n.at.length===3?n.at:h([(n.at[0]-a)/s,(o-n.at[1])/s,m(e[t])[2]]),...n.pole?{pole:n.pole}:{}}]}))}}let S=k(x),C={};for(let e of ee){let t=g(S[e].at);C[e]=[Math.round(t[0]),Math.round(t[1])]}let[w,T]=[t*2,n*2],E=new Float32Array(w*T).fill(-1/0),D=new Int8Array(w*T).fill(-1),P=new Float32Array(w*T*3);for(let e of re(S,_)){let t=A.indexOf(e.tag),n=[];for(let t of e.rings)for(let e=0;e<14;e++){let r=e/14*Math.PI*2;n.push(j(j(t.at,t.across,Math.cos(r)*t.a),t.deep,Math.sin(r)*t.d))}let r=[];for(let t=0;t+1<e.rings.length;t++)for(let e=0;e<14;e++){let[n,i]=[t*14+e,t*14+(e+1)%14];r.push([n,i,i+14,n+14])}let i=e.rings.reduce((t,n)=>j(t,n.at,1/e.rings.length),[0,0,0]),c=n.map(()=>[0,0,0]);for(let[e,t,a,o]of r){let[r,s,l]=[n[e],n[t],n[a]],u=ne([s[0]-r[0],s[1]-r[1],s[2]-r[2]],[l[0]-r[0],l[1]-r[1],l[2]-r[2]]),d=[r[0]-i[0],r[1]-i[1],r[2]-i[2]];u[0]*d[0]+u[1]*d[1]+u[2]*d[2]<0&&(u=[-u[0],-u[1],-u[2]]);for(let n of[e,t,a,o])c[n][0]+=u[0],c[n][1]+=u[1],c[n][2]+=u[2]}let l=n.map(e=>{let t=m(e);return[(a+t[0]*s)*2,(o-t[1]*s)*2,t[2]]}),u=c.map(e=>m(N(e))),d=(e,n,r)=>{let[i,a,o]=[l[e],l[n],l[r]],s=(a[0]-i[0])*(o[1]-i[1])-(o[0]-i[0])*(a[1]-i[1]);if(s===0)return;let[c,d]=[Math.max(0,Math.floor(Math.min(i[0],a[0],o[0]))),Math.min(w-1,Math.ceil(Math.max(i[0],a[0],o[0])))],[f,p]=[Math.max(0,Math.floor(Math.min(i[1],a[1],o[1]))),Math.min(T-1,Math.ceil(Math.max(i[1],a[1],o[1])))];for(let l=f;l<=p;l++)for(let f=c;f<=d;f++){let[c,d]=[f+.5,l+.5],p=((c-i[0])*(o[1]-i[1])-(o[0]-i[0])*(d-i[1]))/s,m=((a[0]-i[0])*(d-i[1])-(c-i[0])*(a[1]-i[1]))/s,h=1-p-m;if(h<0||p<0||m<0)continue;let g=h*i[2]+p*a[2]+m*o[2],_=l*w+f;if(!(g<=E[_])){E[_]=g,D[_]=t;for(let t=0;t<3;t++)P[_*3+t]=h*u[e][t]+p*u[n][t]+m*u[r][t]}}};for(let[e,t,n,i]of r)d(e,t,n),d(e,n,i);for(let t of[0,e.rings.length-1])for(let e=1;e+1<14;e++)d(t*14,t*14+e,t*14+e+1)}let F=new Float32Array(t*n),I=new Float32Array(t*n*3),ae=new Float32Array(t*n),L=Object.fromEntries(A.map(e=>[e,new Float32Array(t*n)])),oe=1/4,R=.02;for(let e=0;e<T;e++)for(let n=0;n<w;n++){let r=e*w+n,i=(e>>1)*t+(n>>1),a=D[r],o=n+1<w?E[r+1]:-1/0,s=e+1<T?E[r+w]:-1/0;if(a>=0||o>-1/0||s>-1/0){let e=E[r];(Math.abs(e-o)>R||Math.abs(e-s)>R)&&(ae[i]=Math.min(1,ae[i]+oe*2))}if(!(a<0)){F[i]+=oe,L[A[a]][i]+=oe;for(let e=0;e<3;e++)I[i*3+e]+=P[r*3+e]*oe}}for(let e=0;e<F.length;e++){let t=Math.hypot(I[e*3],I[e*3+1],I[e*3+2]);if(t>0)for(let n=0;n<3;n++)I[e*3+n]/=t}let se=S.head,ce=S.crown,le=M(se.at,ce.at,.46),z=_.head??1,ue=e=>Array.from({length:13},(t,n)=>g(e(n/12*Math.PI-Math.PI/2)));return{mask:F,parts:L,normals:I,contour:ae,joints:C,lines:[...te.map(([e,t])=>[g(S[e].at),g(S[t].at)]),ue(e=>j(j(le,se.y,Math.sin(e)*.108*z),se.z,Math.cos(e)*.09*z+.006)),ue(e=>j(j(j(le,se.y,-.018*z),se.x,Math.sin(e)*.074*z),se.z,Math.cos(e)*.088*z+.006))],light:ie(e.light??[-.5,.6,.6])}}function F(e){let t=/^#([0-9a-fA-F]+)$/.exec(e)?.[1];if(!t||![3,4,6,8].includes(t.length))throw Error(`color ${JSON.stringify(e)} must be #rgb, #rgba, #rrggbb or #rrggbbaa`);let n=t.length<=4?[...t].map(e=>e+e).join(``):t,r=e=>parseInt(n.slice(e,e+2),16)/255;return[r(0),r(2),r(4),n.length===8?r(6):1]}let I=!1,ae=e=>{I=e},L=()=>I;function oe(e,t,n,r){let i=Math.max(1,Math.round(r*1.2)),a=e;for(let e=0;e<3;e++){let e=new Float32Array(t*n);for(let r=0;r<n;r++){let n=0;for(let e=-i;e<=i;e++)n+=a[r*t+(e%t+t)%t];for(let o=0;o<t;o++)e[r*t+o]=n/(2*i+1),n+=a[r*t+(o+i+1)%t]-a[r*t+((o-i)%t+t)%t]}let r=new Float32Array(t*n);for(let a=0;a<t;a++){let o=0;for(let r=-i;r<=i;r++)o+=e[(r%n+n)%n*t+a];for(let s=0;s<n;s++)r[s*t+a]=o/(2*i+1),o+=e[(s+i+1)%n*t+a]-e[((s-i)%n+n)%n*t+a]}a=r}return a}function R(e,t,n,r){if(r<=0)return e;if(I)return oe(e,t,n,r);let i=Math.max(1,Math.round(r*1.2)),a=e;for(let e=0;e<3;e++){let e=new Float32Array(t*n);for(let r=0;r<n;r++){let n=0;for(let e=-i;e<=i;e++)n+=a[r*t+Math.max(0,Math.min(t-1,e))];for(let o=0;o<t;o++)e[r*t+o]=n/(2*i+1),n+=a[r*t+Math.min(t-1,o+i+1)]-a[r*t+Math.max(0,o-i)]}let r=new Float32Array(t*n);for(let a=0;a<t;a++){let o=0;for(let r=-i;r<=i;r++)o+=e[Math.max(0,Math.min(n-1,r))*t+a];for(let s=0;s<n;s++)r[s*t+a]=o/(2*i+1),o+=e[Math.min(n-1,s+i+1)*t+a]-e[Math.max(0,s-i)*t+a]}a=r}return a}let se=[[1,0],[.9239,.3827],[.7071,.7071],[.3827,.9239],[0,1],[-.3827,.9239],[-.7071,.7071],[-.9239,.3827],[-1,0],[-.9239,-.3827],[-.7071,-.7071],[-.3827,-.9239],[0,-1],[.3827,-.9239],[.7071,-.7071],[.9239,-.3827]];function ce(e,t,n,r,i){let a=Math.imul(e,374761393)+Math.imul(t,668265263)+Math.imul(n,1442968193);a=Math.imul(a^a>>>13,1274126177),a=Math.imul(a^a>>>16,2246822519);let o=se[((a^a>>>16)>>>0)%se.length];return o[0]*r+o[1]*i}let le=e=>e*e*e*(e*(e*6-15)+10);function z(e,t,n,r=0,i=0){let[a,o]=[Math.floor(e),Math.floor(t)],[s,c]=[e-a,t-o],[l,u]=[le(s),le(c)],d=e=>r>0?(e%r+r)%r:e,f=e=>i>0?(e%i+i)%i:e,p=ce(d(a),f(o),n,s,c)*(1-l)+ce(d(a+1),f(o),n,s-1,c)*l,m=ce(d(a),f(o+1),n,s,c-1)*(1-l)+ce(d(a+1),f(o+1),n,s-1,c-1)*l;return p*(1-u)+m*u}let ue=[1.15,1.3,1.4,1.45,1.45,1.45,1.45,1.45];function B(e,t,n,r,i=0,a=0){let[o,s,c,l]=[0,0,1,1];for(let u=0;u<n;u++)o+=z(e*l+.37,t*l+.61,r+u*101,i*l,a*l)*c,s+=c,c*=.5,l*=2;return Math.max(0,Math.min(1,.5+o/s*ue[n-1]))}let V=e=>Math.max(0,Math.min(1,e)),de=e=>e*e*(3-2*e),H=(e,t,n)=>e*.2126+t*.7152+n*.0722;function fe(e,t,n){let[r,i]=[Math.max(e,t,n),Math.min(e,t,n)],a=(r+i)/2;if(r===i)return[0,0,a];let o=r-i,s=a>.5?o/(2-r-i):o/(r+i);return[(r===e?(t-n)/o+(t<n?6:0):r===t?(n-e)/o+2:(e-t)/o+4)/6,s,a]}function pe(e,t,n){if(t===0)return[n,n,n];let r=n<.5?n*(1+t):n+t-n*t,i=2*n-r,a=e=>{let t=e<0?e+1:e>1?e-1:e;return t<1/6?i+(r-i)*6*t:t<1/2?r:t<2/3?i+(r-i)*(2/3-t)*6:i};return[a(e+1/3),a(e),a(e-1/3)]}function me(e){let t=[...e].sort((e,t)=>e[0]-t[0]),n=t.map(e=>e[0]),r=t.map(e=>e[1]),i=n.length,a=n.slice(0,-1).map((e,t)=>(r[t+1]-r[t])/Math.max(1e-6,n[t+1]-e)),o=n.map((e,t)=>t===0?a[0]??0:t===i-1?a[i-2]??0:a[t-1]*a[t]<=0?0:(a[t-1]+a[t])/2),s=new Float32Array(1024);for(let e=0;e<s.length;e++){let t=e/(s.length-1);if(t<=n[0]){s[e]=r[0];continue}if(t>=n[i-1]){s[e]=r[i-1];continue}let a=0;for(;t>n[a+1];)a++;let c=n[a+1]-n[a],l=(t-n[a])/c,[u,d]=[l*l,l*l*l];s[e]=V((2*d-3*u+1)*r[a]+(d-2*u+l)*c*o[a]+(3*u-2*d)*r[a+1]+(d-u)*c*o[a+1])}return s}let he=(e,t)=>e[Math.round(V(t)*(e.length-1))];function ge(e){let t=[...e].sort((e,t)=>e[0]-t[0]).map(([e,t])=>[e,F(t)]),n=new Float32Array(768);for(let e=0;e<256;e++){let r=e/255,i=0;for(;i+2<t.length&&r>t[i+1][0];)i++;let[a,o]=[t[i],t[Math.min(t.length-1,i+1)]],s=V((r-a[0])/Math.max(1e-6,o[0]-a[0]));for(let t=0;t<3;t++)n[e*3+t]=a[1][t]+(o[1][t]-a[1][t])*s}return n}function _e(e,t,n,r){let i=new Float32Array(e.length),a=r.levels,[o,s]=a?.input??[0,1],[c,l]=a?.output??[0,1],u=a?.gamma??1,d=r.curves?Array.isArray(r.curves)?{all:me(r.curves)}:Object.fromEntries(Object.entries(r.curves).map(([e,t])=>[e,me(t)])):null,f=r.exposure===void 0?1:2**r.exposure,[p,m]=[r.brightness??0,r.contrast??0],h=m>=0?1/Math.max(.01,1-m*.98):1+m,[g,_]=[r.temperature??0,r.tint??0],[v,y,b]=[(r.hue??0)/360,r.saturation??0,r.lightness??0],x=r.vibrance??0,S=r.gradientMap?ge(r.gradientMap):null,C=r.posterize===void 0?0:Math.max(2,Math.round(r.posterize)),w=r.balance;for(let T=0;T<t*n;T++){let t=T*4,n=e[t+3];if(n<=0)continue;let[E,D,O]=[e[t]/n,e[t+1]/n,e[t+2]/n];if(f!==1&&(E*=f,D*=f,O*=f),a){let e=e=>c+(l-c)*V((e-o)/Math.max(1e-6,s-o))**(1/u);E=e(E),D=e(D),O=e(O)}if(d&&(d.all&&(E=he(d.all,E),D=he(d.all,D),O=he(d.all,O)),d.r&&(E=he(d.r,E)),d.g&&(D=he(d.g,D)),d.b&&(O=he(d.b,O))),p||m){let e=e=>(e+p-.5)*h+.5;E=e(E),D=e(D),O=e(O)}if((g||_)&&(E+=g*.2+_*.1,D-=_*.2,O+=-g*.2+_*.1),w){let e=V(H(E,D,O)),[t,n,r]=[de(V(1-e*2.5)),1-Math.abs(e-.5)*2,de(V((e-.6)*2.5))];for(let[e,i]of[[t,w.shadows],[n,w.midtones],[r,w.highlights]])i&&(E+=i[0]*e*.5,D+=i[1]*e*.5,O+=i[2]*e*.5)}if(v||y||b){let[e,t,n]=fe(V(E),V(D),V(O)),r=y>=0?t+(1-t)*y*(t>0):t*(1+y),i=b>=0?n+(1-n)*b:n*(1+b);[E,D,O]=pe((e+v+1)%1,V(r),V(i))}if(x){let[e,t]=[Math.max(E,D,O),Math.min(E,D,O)],n=x*(1-V(e-t)),r=H(E,D,O);E=r+(E-r)*(1+n),D=r+(D-r)*(1+n),O=r+(O-r)*(1+n)}if(r.blackWhite){let[e,t,n]=Array.isArray(r.blackWhite)?r.blackWhite:[.2126,.7152,.0722];E=D=O=E*e+D*t+O*n}if(S){let e=Math.round(V(H(E,D,O))*255)*3;[E,D,O]=[S[e],S[e+1],S[e+2]]}if(r.invert&&(E=1-E,D=1-D,O=1-O),C){let e=e=>Math.round(V(e)*(C-1))/(C-1);E=e(E),D=e(D),O=e(O)}r.threshold!==void 0&&(E=D=O=+(H(E,D,O)>=r.threshold)),i[t]=V(E)*n,i[t+1]=V(D)*n,i[t+2]=V(O)*n,i[t+3]=n}return i}let ve=(e,t,n,r,i,a,o)=>{let[s,c]=[Math.max(0,Math.min(t-1.001,r-.5)),Math.max(0,Math.min(n-1.001,i-.5))],[l,u]=[Math.floor(s),Math.floor(c)],[d,f]=[s-l,c-u],[p,m,h,g]=[(u*t+l)*4,(u*t+l+1)*4,((u+1)*t+l)*4,((u+1)*t+l+1)*4];for(let t=0;t<4;t++)a[o+t]=(e[p+t]*(1-d)+e[m+t]*d)*(1-f)+(e[h+t]*(1-d)+e[g+t]*d)*f},ye=(e,t,n,r)=>{let i=new Float32Array(e.length);for(let a=0;a<4;a++){let o=new Float32Array(t*n);for(let t=0;t<o.length;t++)o[t]=e[t*4+a];let s=R(o,t,n,r);for(let e=0;e<o.length;e++)i[e*4+a]=s[e]}return i};function be(e,t,n,r){let i=new Float32Array(e.length);for(let a=0;a<n;a++)for(let o=0;o<t;o++){let[s,c]=r(o+.5,a+.5);ve(e,t,n,s,c,i,(a*t+o)*4)}return i}function xe(e,t,n,r,i){let a=new Float32Array(e.length),o=new Float32Array(4);for(let s=0;s<n;s++)for(let c=0;c<t;c++){let l=(s*t+c)*4;for(let u=0;u<r;u++){let[d,f]=i(c+.5,s+.5,r===1?0:u/(r-1));ve(e,t,n,d,f,o,0);for(let e=0;e<4;e++)a[l+e]+=o[e]}for(let e=0;e<4;e++)a[l+e]/=r}return a}function Se(e,t,n,r){let i=new Float32Array(e),a=new Float32Array(3);for(let e=0;e<n;e++)for(let n=0;n<t;n++){a.fill(0),r(n+.5,e+.5,a);let o=(e*t+n)*4,s=i[o+3];for(let e=0;e<3;e++)i[o+e]=i[o+e]+(1-i[o+e])*V(a[e]),s=Math.max(s,i[o+e]);i[o+3]=s}return i}let U=(e,t,n)=>e[t]??n,Ce=(e,t,n)=>e[t]??n;function we(e,t,n,r,i,a){let o=[r/(2*a),i/(2*a)],s=(e=`at`)=>{let n=Ce(t,e,o);return[n[0]*a,n[1]*a]};switch(e){case`gaussian`:return ye(n,r,i,U(t,`radius`,4)*a);case`motion`:{let e=U(t,`angle`,0)*Math.PI/180,o=U(t,`distance`,16)*a,[s,c]=[Math.cos(e)*o,Math.sin(e)*o];return xe(n,r,i,Math.max(2,Math.min(64,Math.ceil(o))),(e,t,n)=>[e+s*(n-.5),t+c*(n-.5)])}case`spin`:{let[e,a]=s(),o=U(t,`angle`,10)*Math.PI/180;return xe(n,r,i,24,(t,n,r)=>{let i=o*(r-.5),[s,c]=[t-e,n-a];return[e+s*Math.cos(i)-c*Math.sin(i),a+s*Math.sin(i)+c*Math.cos(i)]})}case`zoom`:{let[e,a]=s(),o=U(t,`amount`,.1);return xe(n,r,i,24,(t,n,r)=>{let i=1+o*(r-.5);return[e+(t-e)*i,a+(n-a)*i]})}case`lens`:{let e=U(t,`radius`,6)*a,o=U(t,`highlights`,1),s=Math.max(2,Math.min(6,Math.ceil(e/2))),c=[[0,0]];for(let t=1;t<=s;t++){let n=t*7;for(let r=0;r<n;r++){let i=r/n*Math.PI*2+t;c.push([e*t*Math.cos(i)/s,e*t*Math.sin(i)/s])}}let l=new Float32Array(n.length),u=new Float32Array(4);for(let e=0;e<i;e++)for(let t=0;t<r;t++){let a=(e*r+t)*4,s=0;for(let[d,f]of c){ve(n,r,i,t+.5+d,e+.5+f,u,0);let c=H(u[0],u[1],u[2]),p=1+o*2*c*c*c*c;for(let e=0;e<4;e++)l[a+e]+=u[e]*p;s+=p}for(let e=0;e<4;e++)l[a+e]/=s}return l}case`sharpen`:{let e=ye(n,r,i,U(t,`radius`,1.5)*a),[o,s]=[U(t,`amount`,.8),U(t,`threshold`,0)],c=new Float32Array(n);for(let t=0;t<r*i;t++){let r=t*4,i=n[r+3];for(let t=0;t<3;t++){let a=n[r+t]-e[r+t];Math.abs(a)>=s&&(c[r+t]=Math.max(0,Math.min(i,n[r+t]+a*o)))}}return c}case`highPass`:{let e=ye(n,r,i,U(t,`radius`,6)*a),o=new Float32Array(n.length);for(let t=0;t<r*i;t++){let r=t*4,i=n[r+3];o[r+3]=i;for(let t=0;t<3;t++)o[r+t]=V(.5+n[r+t]-e[r+t])*i}return o}case`noise`:{let[e,o,s,c]=[U(t,`amount`,.08),U(t,`scale`,1)*a,U(t,`seed`,1),t.mono!==!1],l=new Float32Array(n);for(let t=0;t<i;t++)for(let i=0;i<r;i++){let a=(t*r+i)*4,u=n[a+3];for(let r=0;r<3;r++){let d=B(i/o,t/o,1,c?s:s+r*13)-.5;l[a+r]=Math.max(0,Math.min(u,n[a+r]+d*e*2*u))}}return l}case`median`:{let e=Math.max(1,Math.min(6,Math.round(U(t,`radius`,1)*a))),o=new Float32Array(n.length),s=[];for(let t=0;t<i;t++)for(let a=0;a<r;a++){s.length=0;for(let o=-e;o<=e;o++)for(let c=-e;c<=e;c++){let e=(Math.max(0,Math.min(i-1,t+o))*r+Math.max(0,Math.min(r-1,a+c)))*4;s.push([H(n[e],n[e+1],n[e+2]),e])}s.sort((e,t)=>e[0]-t[0]);let c=s[s.length>>1][1],l=(t*r+a)*4;for(let e=0;e<4;e++)o[l+e]=n[c+e]}return o}case`emboss`:{let e=U(t,`angle`,135)*Math.PI/180,o=U(t,`height`,2)*a,s=U(t,`amount`,2),[c,l]=[Math.round(Math.cos(e)*o),Math.round(Math.sin(e)*o)],u=new Float32Array(n.length);for(let e=0;e<i;e++)for(let t=0;t<r;t++){let a=(e*r+t)*4,o=(Math.max(0,Math.min(i-1,e+l))*r+Math.max(0,Math.min(r-1,t+c)))*4,d=H(n[a],n[a+1],n[a+2])-H(n[o],n[o+1],n[o+2]),f=V(.5+d*s),p=n[a+3];u[a]=u[a+1]=u[a+2]=f*p,u[a+3]=p}return u}case`ripple`:{let[e,o,s]=[U(t,`amount`,4)*a,U(t,`size`,24)*a,U(t,`seed`,1)];return be(n,r,i,(t,n)=>[t+Math.sin(n/o*Math.PI*2+s)*e,n+Math.sin(t/o*Math.PI*2+s*1.7)*e])}case`displace`:{let[e,o,s]=[U(t,`amount`,8)*a,U(t,`size`,40)*a,U(t,`seed`,1)];return be(n,r,i,(t,n)=>[t+(B(t/o,n/o,3,s)-.5)*e*2,n+(B(t/o,n/o,3,s+7)-.5)*e*2])}case`twirl`:{let[e,o]=s(),[c,l]=[U(t,`radius`,Math.min(r,i)/(2*a))*a,U(t,`angle`,45)*Math.PI/180];return be(n,r,i,(t,n)=>{let[r,i]=[t-e,n-o],a=Math.hypot(r,i);if(a>=c)return[t,n];let s=l*(1-a/c)**2;return[e+r*Math.cos(s)-i*Math.sin(s),o+r*Math.sin(s)+i*Math.cos(s)]})}case`pixelate`:{let e=Math.max(2,Math.round(U(t,`cell`,8)*a)),o=new Float32Array(n.length);for(let t=0;t<i;t+=e)for(let a=0;a<r;a+=e){let s=[0,0,0,0],c=0;for(let o=t;o<Math.min(i,t+e);o++)for(let t=a;t<Math.min(r,a+e);t++){let e=(o*r+t)*4;for(let t=0;t<4;t++)s[t]+=n[e+t];c++}for(let n=t;n<Math.min(i,t+e);n++)for(let t=a;t<Math.min(r,a+e);t++){let e=(n*r+t)*4;for(let t=0;t<4;t++)o[e+t]=s[t]/c}}return o}case`chromatic`:{let[e,o]=s(),c=U(t,`amount`,2)*a/Math.max(r,i),l=new Float32Array(n),u=new Float32Array(4);for(let t=0;t<i;t++)for(let a=0;a<r;a++){let s=(t*r+a)*4,[d,f]=[a+.5-e,t+.5-o];ve(n,r,i,e+d*(1+c),o+f*(1+c),u,0),l[s]=u[0],ve(n,r,i,e+d*(1-c),o+f*(1-c),u,0),l[s+2]=u[2]}return l}case`vignette`:{let[e,a]=s(),[o,c,l]=[U(t,`amount`,.6),U(t,`radius`,.55),U(t,`outer`,1.05)],u=F(t.color??`#000000`),d=Math.hypot(r,i)/2,f=new Float32Array(n);for(let t=0;t<i;t++)for(let i=0;i<r;i++){let s=(t*r+i)*4,p=o*de(V((Math.hypot(i+.5-e,t+.5-a)/d-c)/Math.max(.01,l-c)));for(let e=0;e<3;e++)f[s+e]=n[s+e]+(u[e]*n[s+3]-n[s+e])*p}return f}case`fog`:{let e=F(t.color??`#c8d0e0`),[o,s]=[Ce(t,`from`,[0,0])[0]*a,Ce(t,`from`,[0,0])[1]*a],[c,l]=[Ce(t,`to`,[0,i/a])[0]*a,Ce(t,`to`,[0,i/a])[1]*a],[u,d]=[c-o,l-s],f=u*u+d*d||1,[p,m,h,g]=[U(t,`density`,.6),U(t,`patchiness`,.5),U(t,`size`,120)*a,U(t,`seed`,1)],_=new Float32Array(n);for(let t=0;t<i;t++)for(let i=0;i<r;i++){let a=(t*r+i)*4,c=V(((i+.5-o)*u+(t+.5-s)*d)/f),l=m>0?1-m+m*2*B(i/(h*2),t/h,4,g):1,v=V(de(c)*p*l)*e[3];for(let t=0;t<3;t++)_[a+t]=n[a+t]*(1-v)+e[t]*v;_[a+3]=n[a+3]*(1-v)+v}return _}case`rays`:{let[e,a]=s(),[o,c,l,u]=[U(t,`threshold`,.6),U(t,`length`,.6),U(t,`strength`,.8),U(t,`decay`,.96)],d=new Float32Array(n.length);for(let e=0;e<r*i;e++){let t=e*4,r=V((Math.max(n[t],n[t+1],n[t+2])-o)/Math.max(.01,1-o));for(let e=0;e<3;e++)d[t+e]=n[t+e]*r;d[t+3]=1}let f=new Float32Array(4);return Se(n,r,i,(t,n,o)=>{let s=1;for(let p=0;p<48;p++){let m=1-p/48*c;ve(d,r,i,e+(t-e)*m,a+(n-a)*m,f,0);for(let e=0;e<3;e++)o[e]+=f[e]*s*l/48;s*=u}})}case`flare`:{let[e,o]=s(),c=F(t.color??`#ffe0b0`),[l,u]=[U(t,`size`,60)*a,U(t,`strength`,1)],[d,f]=[r/2,i/2],p=[[-.4,.22,.14],[-.8,.12,.2],[-1.3,.34,.09],[-1.7,.17,.13]];return Se(n,r,i,(t,n,r)=>{let i=Math.hypot(t-e,n-o),a=.9*Math.exp(-((i/l)**2))+.25*Math.exp(-i/(l*2.5));a+=.5*Math.exp(-Math.abs(n-o)/(l*.05))*Math.exp(-Math.abs(t-e)/(l*3.5));for(let[r,i,s]of p){let[c,u]=[e+(d-e)*(1-r),o+(f-o)*(1-r)],p=Math.hypot(t-c,n-u)/(l*i*2.2);p<1&&(a+=s*(1-p*p*p*p))}for(let e=0;e<3;e++)r[e]+=c[e]*a*u})}case`texture`:{let[e,o,s]=[U(t,`scale`,3)*a,U(t,`amount`,.3),U(t,`seed`,1)],c=U(t,`angle`,135)*Math.PI/180,[l,u]=[Math.cos(c),Math.sin(c)],d=new Float32Array(n);for(let t=0;t<i;t++)for(let i=0;i<r;i++){let a=(t*r+i)*4,c=1+(B(i/e,t/e,3,s)-B((i+l)/e,(t+u)/e,3,s))*o*3,f=n[a+3];for(let e=0;e<3;e++)d[a+e]=Math.max(0,Math.min(f,n[a+e]*c))}return d}default:throw Error(`no filter \"${String(e)}\"`)}}function Te(e){let t=e.match(/[a-zA-Z]|-?(?:\\d+\\.?\\d*|\\.\\d+)(?:[eE][-+]?\\d+)?/g)??[],n=[],r=null,[i,a,o,s,c,l]=[0,0,0,0,0,0],u=``,d=``,f=0,p=()=>{let n=t[f++];if(n===void 0||/[a-zA-Z]/.test(n))throw Error(`path ${JSON.stringify(e.slice(0,60))}: command ${u} is short of numbers`);return Number(n)},m=()=>{r||(r=[[i,a]],n.push({points:r,closed:!1}))},h=e=>{m();for(let t=1;t<=20;t++)r.push(e(t/20))};for(;f<t.length;){if(/[a-zA-Z]/.test(t[f]))u=t[f++];else if(!u)throw Error(`path ${JSON.stringify(e.slice(0,60))} must start with a command`);let g=u===u.toLowerCase(),[_,v]=g?[i,a]:[0,0];switch(u.toUpperCase()){case`M`:i=_+p(),a=v+p(),o=i,s=a,r=[[i,a]],n.push({points:r,closed:!1}),u=g?`l`:`L`;break;case`L`:i=_+p(),a=v+p(),m(),r.push([i,a]);break;case`H`:i=_+p(),m(),r.push([i,a]);break;case`V`:a=v+p(),m(),r.push([i,a]);break;case`C`:case`S`:{let[e,t]=u.toUpperCase()===`S`?d===`C`||d===`S`?[2*i-c,2*a-l]:[i,a]:[_+p(),v+p()],[n,r,o,s]=[_+p(),v+p(),_+p(),v+p()],[f,m]=[i,a];h(i=>{let a=1-i;return[a*a*a*f+3*a*a*i*e+3*a*i*i*n+i*i*i*o,a*a*a*m+3*a*a*i*t+3*a*i*i*r+i*i*i*s]}),[i,a,c,l]=[o,s,n,r];break}case`Q`:case`T`:{let[e,t]=u.toUpperCase()===`T`?d===`Q`||d===`T`?[2*i-c,2*a-l]:[i,a]:[_+p(),v+p()],[n,r]=[_+p(),v+p()],[o,s]=[i,a];h(i=>{let a=1-i;return[a*a*o+2*a*i*e+i*i*n,a*a*s+2*a*i*t+i*i*r]}),[i,a,c,l]=[n,r,e,t];break}case`Z`:n.length&&(n[n.length-1].closed=!0),i=o,a=s,r=null,u=``;break;default:throw Error(`path command ${u} is not one this draws; there are M L H V C S Q T Z`)}d=u.toUpperCase()===`L`&&d===`M`?`L`:u.toUpperCase()}return n}let Ee=[`normal`,`multiply`,`screen`,`overlay`,`add`,`softLight`,`hardLight`,`vividLight`,`linearLight`,`pinLight`,`colorDodge`,`colorBurn`,`linearBurn`,`darken`,`lighten`,`difference`,`exclusion`,`subtract`,`divide`,`hue`,`saturation`,`color`,`luminosity`],De={exposure:[-8,8],brightness:[-1,1],contrast:[-1,1],temperature:[-1,1],tint:[-1,1],hue:[-360,360],saturation:[-1,1],lightness:[-1,1],vibrance:[-1,1],posterize:[2,256],threshold:[0,1]};function Oe(e){if(typeof e!=`object`||!e)return`must be an object of what to change`;let t=e,n=[...Object.keys(De),`levels`,`curves`,`balance`,`blackWhite`,`gradientMap`,`invert`];for(let e of Object.keys(t))if(!n.includes(e))return`has unknown \"${e}\"; there are ${n.join(`, `)}`;if(Object.keys(t).length===0)return`changes nothing`;for(let[e,[n,r]]of Object.entries(De))if(t[e]!==void 0&&!(W(t[e])&&t[e]>=n&&t[e]<=r))return`${e} must be ${n}..${r}`;let r=e=>G(e)&&e.every(e=>e>=0&&e<=1),i=e=>Array.isArray(e)&&e.length>=2&&e.every(r);if(t.levels!==void 0){let e=t.levels;if(typeof e!=`object`||!e)return`levels must be { input?, gamma?, output? }`;for(let t of Object.keys(e))if(![`input`,`gamma`,`output`].includes(t))return`levels has unknown \"${t}\"; there are input, gamma, output`;for(let t of[`input`,`output`])if(e[t]!==void 0&&!r(e[t]))return`levels ${t} must be [low, high], each 0..1`;if(e.gamma!==void 0&&!(W(e.gamma)&&e.gamma>0))return`levels gamma must be positive`}if(t.curves!==void 0)if(Array.isArray(t.curves)){if(!i(t.curves))return`curves must be two or more [in, out], each 0..1`}else{let e=t.curves;if(typeof e!=`object`||!e)return`curves must be [[in, out]...] or { r, g, b }`;for(let[t,n]of Object.entries(e)){if(![`r`,`g`,`b`].includes(t))return`curves has unknown \"${t}\"; there are r, g, b`;if(!i(n))return`curves ${t} must be two or more [in, out], each 0..1`}}if(t.balance!==void 0)for(let[e,n]of Object.entries(t.balance)){if(![`shadows`,`midtones`,`highlights`].includes(e))return`balance has unknown \"${e}\"; there are shadows, midtones, highlights`;if(!(Array.isArray(n)&&n.length===3&&n.every(W)))return`balance ${e} must be [r, g, b]`}return t.blackWhite!==void 0&&typeof t.blackWhite!=`boolean`&&!(Array.isArray(t.blackWhite)&&t.blackWhite.length===3&&t.blackWhite.every(W))?`blackWhite must be true or [r, g, b]`:t.invert!==void 0&&typeof t.invert!=`boolean`?`invert must be true or false`:t.gradientMap!==void 0&&!(Array.isArray(t.gradientMap)&&t.gradientMap.length>=2&&t.gradientMap.every(e=>Array.isArray(e)&&e.length===2&&W(e[0])&&K(e[1])))?`gradientMap must be two or more [0..1, colour]`:null}let ke={gaussian:[`radius`],motion:[`angle`,`distance`],spin:[`at`,`angle`],zoom:[`at`,`amount`],lens:[`radius`,`highlights`],sharpen:[`amount`,`radius`,`threshold`],highPass:[`radius`],noise:[`amount`,`scale`,`mono`,`seed`],median:[`radius`],emboss:[`angle`,`height`,`amount`],ripple:[`amount`,`size`,`seed`],displace:[`amount`,`size`,`seed`],twirl:[`at`,`radius`,`angle`],pixelate:[`cell`],chromatic:[`at`,`amount`],vignette:[`at`,`amount`,`radius`,`outer`,`color`],fog:[`color`,`density`,`from`,`to`,`patchiness`,`size`,`seed`],rays:[`at`,`threshold`,`length`,`strength`,`decay`],flare:[`at`,`size`,`color`,`strength`],texture:[`scale`,`amount`,`angle`,`seed`]};function Ae(e,t){if(typeof e!=`string`||!Object.prototype.hasOwnProperty.call(ke,e))return`filter ${JSON.stringify(e)} must be one of ${Object.keys(ke).join(`, `)}`;if(t===void 0)return null;if(typeof t!=`object`||!t)return`params must be an object`;let n=ke[e];for(let[r,i]of Object.entries(t)){if(!n.includes(r))return`filter ${e} has unknown \"${r}\"; it takes ${n.join(`, `)}`;if(r===`color`){if(!K(i))return`filter ${e}: color must be #rgb..#rrggbbaa`}else if(r===`at`||r===`from`||r===`to`){if(!G(i))return`filter ${e}: ${r} must be [x, y]`}else if(r===`mono`){if(typeof i!=`boolean`)return`filter ${e}: mono must be true or false`}else if(!W(i))return`filter ${e}: ${r} must be a number`}return null}let je={ink:{hardness:1,taper:[.12,.3]},pen:{hardness:1,roundness:.25,angle:-45},pencil:{hardness:.6,grain:.35,grainScale:1.2,opacity:.75},charcoal:{hardness:.35,grain:.5,grainScale:2.2,dry:.12,opacity:.95},chalk:{hardness:.7,grain:.55,grainScale:6,opacity:.95},flat:{hardness:.85,roundness:.3,dry:.3},oil:{hardness:.65,roundness:.55,dry:.22,wet:.55,opacity:.95},gouache:{hardness:.8,roundness:.5,wet:.2,dry:.08,opacity:.95},watercolor:{hardness:.2,wet:.45,edge:.5,opacity:.45,grain:.05,grainScale:5},marker:{hardness:.9,roundness:.45,angle:-30,opacity:.72},airbrush:{hardness:0,flow:.05},spray:{hardness:.7,dab:.07,scatter:1,spacing:.5,opacity:.9},dry:{hardness:.75,roundness:.35,dry:.65,opacity:.95}},Me=[`brush`,`roundness`,`angle`,`grain`,`grainScale`,`dry`,`wet`,`edge`,`scatter`,`dab`,`spacing`,`flow`,`fade`],W=e=>typeof e==`number`&&Number.isFinite(e),G=e=>Array.isArray(e)&&e.length===2&&e.every(W),K=e=>typeof e==`string`&&/^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/.test(e);function Ne(e){if(typeof e==`string`)return K(e)?null:`${JSON.stringify(e)} must be #rgb, #rgba, #rrggbb or #rrggbbaa`;let t=e;if(typeof t!=`object`||!t)return`must be a colour or a gradient`;if(t.type!==`linear`&&t.type!==`radial`&&t.type!==`noise`&&t.type!==`form`&&t.type!==`image`&&t.type!==`grid`)return`a gradient has type linear, radial, noise, form, image or grid`;if(t.type===`grid`){if(!Array.isArray(t.colors)||t.colors.length===0||!t.colors.every(K))return`a grid needs colors: one or more`;for(let e of[`ground`,`off`])if(t[e]!==void 0&&!K(t[e]))return`a grid's ${e} must be a colour`;let e=t.quad;if(e!==void 0&&!(Array.isArray(e)&&e.length===4&&e.every(G)))return`a grid's quad must be four [x, y], in order round from the top left`;if(t.cell===void 0&&t.cells===void 0)return`a grid needs cell: [w, h] in pixels, or cells: [columns, rows]`;for(let e of[`cell`,`cells`])if(t[e]!==void 0&&!(G(t[e])&&t[e].every(e=>e>0)))return`a grid's ${e} must be two positive numbers`;return t.cell!==void 0&&e!==void 0?`a grid on a quad is counted in cells: [columns, rows]`:t.mark!==void 0&&!(G(t.mark)&&t.mark.every(e=>e>0&&e<=1))?`a grid's mark must be [w, h], each above 0 and up to 1`:t.lit!==void 0&&!(W(t.lit)&&t.lit>=0&&t.lit<=1)?`a grid's lit must be 0..1`:t.seed!==void 0&&!Number.isInteger(t.seed)?`a grid's seed must be a whole number`:null}if(t.type===`image`)return typeof t.image==`string`&&t.image?null:`an image fill needs image, the id of an image layer drawn before it`;if(t.type===`form`){if(typeof t.figure!=`string`||!t.figure)return`a form needs figure, the id of a figure drawn before it`;if(t.light!==void 0&&!(Array.isArray(t.light)&&t.light.length===3&&t.light.every(W)))return`a form's light must be [x, y, z], x right, y up, z toward the viewer`}if(t.type===`noise`){if(t.scale!==void 0&&!(W(t.scale)&&t.scale>0))return`noise scale must be a positive number of pixels`;if(t.octaves!==void 0&&!(Number.isInteger(t.octaves)&&t.octaves>=1&&t.octaves<=8))return`noise octaves must be a whole number from 1 to 8`;if(t.seed!==void 0&&!Number.isInteger(t.seed))return`noise seed must be a whole number`}if(t.type===`linear`&&!(G(t.from)&&G(t.to)))return`a linear gradient needs from and to, each [x, y]`;if(t.type===`linear`&&t.from[0]===t.to[0]&&t.from[1]===t.to[1])return`a linear gradient needs from and to apart: at one point it draws a single flat colour`;if(t.type===`radial`&&!(G(t.at)&&W(t.radius)&&t.radius>0))return`a radial gradient needs at: [x, y] and a positive radius`;if(!Array.isArray(t.stops)||t.stops.length<2)return`a gradient needs two stops or more, each [0..1, colour]`;for(let e of t.stops)if(!Array.isArray(e)||e.length!==2||!W(e[0])||!K(e[1]))return`a stop ${JSON.stringify(e)} must be [0..1, colour]`;return null}let Pe=`id.shape.at.size.radius.radii.inner.count.tooth.turn.points.d.rows.palette.scale.layers.fill.stroke.opacity.blend.transform.mirror.effects.clip.erase.visible.blur.strokes.from.guide.height.yaw.pitch.build.pose.light.src.except.text.fontSize.font.weight.align.vertical.tracking.vanish.depth.faces.roof.y.edge.reach.ripple.smear.quad.threshold.strength.thin.grain.grainScale.dry.face.within.feather.clipFeather.clipGrow.clipInvert.blendIf.adjust.filter.params`.split(`.`),Fe=e=>e.guide??(e.shape===`figure`||e.shape===`image`||e.shape===`perspective`),Ie=[`rect`,`circle`,`ellipse`,`ring`,`polygon`,`line`,`star`,`gear`,`path`,`pixels`,`paint`,`figure`,`image`,`text`,`block`,`perspective`,`reflect`,`bloom`,`adjust`,`filter`,`group`],Le=[`points`,`color`,`to`,`size`,`hardness`,`opacity`,`taper`,`bristles`,`strand`,`spread`,`vary`,`wobble`,`seed`,`smudge`,`erase`,`blend`,...Me];function Re(e){if(e.brush!==void 0&&!(typeof e.brush==`string`&&Object.prototype.hasOwnProperty.call(je,e.brush)))return`brush ${JSON.stringify(e.brush)} must be one of ${Object.keys(je).join(`, `)}`;for(let t of[`roundness`,`grain`,`dry`,`wet`,`edge`,`dab`,`flow`])if(e[t]!==void 0&&!(W(e[t])&&e[t]>=0&&e[t]<=1))return`${t} must be 0..1`;if(e.roundness===0||e.dab===0)return`${e.roundness===0?`roundness`:`dab`} must be above 0`;for(let t of[`grainScale`,`spacing`])if(e[t]!==void 0&&!(W(e[t])&&e[t]>0))return`${t} must be positive`;return e.scatter!==void 0&&!(W(e.scatter)&&e.scatter>=0)?`scatter must be 0 or more`:e.angle!==void 0&&!W(e.angle)?`angle must be degrees`:e.fade!==void 0&&!(Array.isArray(e.fade)&&e.fade.length===2&&e.fade.every(e=>W(e)&&e>=0&&e<=1))?`fade must be [start, end], each 0..1`:null}let ze=[`type`,`guides`,`colors`,`count`,`tip`,`size`,`clumps`,`gather`,`flyaway`,`vary`,`wobble`,`opacity`,`taper`,`seed`,`hardness`,...Me],Be=[`type`,`line`,`count`,`strokes`,`scale`,`turn`,`both`,`jitter`,`seed`],Ve=[`type`,`region`,`guides`,`angle`,`tone`,`smudge`,`size`,`length`,`coverage`,`spread`,`vary`,`opacity`,`hardness`,`seed`,`taper`,...Me],He=e=>Array.isArray(e)&&e.length>=2&&e.every(e=>Array.isArray(e)&&(e.length===2||e.length===3)&&e.every(W)),Ue=3e4;function We(e){let t=t=>{for(let n of t)if(e[n]!==void 0&&!(W(e[n])&&e[n]>=0&&e[n]<=1))return`${n} must be 0..1`;return null},n=e.type===`sweep`?ze:e.type===`along`?Be:Ve;for(let t of Object.keys(e))if(!n.includes(t))return`has unknown field \"${t}\"; a ${String(e.type)} takes ${n.join(`, `)}`;if(e.seed!==void 0&&!Number.isInteger(e.seed))return`seed must be a whole number`;if(e.type!==`along`){let t=Re(e);if(t)return t}if(e.type===`sweep`){if(!Array.isArray(e.guides)||e.guides.length<2||!e.guides.every(He))return`needs guides: two or more lines, each two or more [x, y]`;if(!Array.isArray(e.colors)||e.colors.length===0||!e.colors.every(K))return`needs colors: one or more, across the guides`;if(e.tip!==void 0&&!K(e.tip))return`tip must be a colour`;if(!(Number.isInteger(e.count)&&e.count>=1&&e.count<=3e4))return`count must be a whole number from 1 to ${Ue}`;if(e.clumps!==void 0&&!(Number.isInteger(e.clumps)&&e.clumps>=1))return`clumps must be a whole number of 1 or more`;for(let t of[`size`,`wobble`])if(e[t]!==void 0&&!(W(e[t])&&e[t]>=0))return`${t} must be 0 or more`;return e.taper!==void 0&&!(Array.isArray(e.taper)&&e.taper.length===2&&e.taper.every(e=>W(e)&&e>=0&&e<=1))?`taper must be [start, end], each 0..1`:t([`gather`,`flyaway`,`vary`,`opacity`])}if(e.type===`along`){if(!He(e.line))return`needs a line: two or more [x, y]`;if(!(Number.isInteger(e.count)&&e.count>=1&&e.count<=2e3))return`count must be a whole number from 1 to 2000`;if(!Array.isArray(e.strokes)||e.strokes.length===0)return`needs strokes, drawn about [0, 0]`;for(let[t,n]of e.strokes.entries()){if(n?.type!==void 0)return`strokes[${t}] must be a plain stroke`;let e=Ge(n);if(e)return`strokes[${t}] ${e}`}let t=e.scale;if(t!==void 0&&!(W(t)&&t>0)&&!(G(t)&&t.every(e=>e>0)))return`scale must be a positive number, or [start, end]`;for(let t of[`turn`,`jitter`])if(e[t]!==void 0&&!W(e[t]))return`${t} must be a number`;return null}if(e.region!==void 0){if(typeof e.region!=`string`)return`region must be an SVG path or \"@id\"`;if(!e.region.startsWith(`@`))try{Te(e.region)}catch(e){return`region: ${e.message}`}}if(e.guides!==void 0&&!(Array.isArray(e.guides)&&e.guides.length>0&&e.guides.every(He)))return`guides must be one or more lines, each two or more [x, y]`;if(e.angle!==void 0&&!W(e.angle))return`angle must be degrees`;if(e.tone===void 0&&!e.smudge)return`needs a tone, unless it smudges`;if(e.tone!==void 0){let t=Ne(e.tone);if(t)return`tone ${t}`}for(let t of[`size`,`length`,`coverage`])if(e[t]!==void 0&&!(W(e[t])&&e[t]>0))return`${t} must be positive`;return e.size===void 0?`needs a positive size`:e.spread!==void 0&&!(W(e.spread)&&e.spread>=0)?`spread must be 0 or more degrees`:t([`vary`,`opacity`,`hardness`,`smudge`])}function Ge(e){if(typeof e!=`object`||!e)return`must be an object`;if(e.type===void 0){let t=Re(e);if(t)return t}if(e.type===`sweep`||e.type===`field`||e.type===`along`)return We(e);if(e.type!==void 0)return`has type \"${String(e.type)}\"; there are sweep, field and along, or none for one stroke`;for(let t of Object.keys(e))if(!Le.includes(t))return`has unknown field \"${t}\"; a stroke takes ${Le.join(`, `)}`;if(!Array.isArray(e.points)||e.points.length===0||!e.points.every(e=>Array.isArray(e)&&(e.length===2||e.length===3)&&e.every(W)))return`needs points, each [x, y] or [x, y, pressure]`;if(!(W(e.size)&&e.size>0))return`needs a positive size`;if(!e.erase&&!e.smudge&&e.color===void 0)return`needs a color, unless it erases or smudges`;for(let t of[`color`,`to`])if(e[t]!==void 0&&!K(e[t]))return`${t} must be #rgb, #rgba, #rrggbb or #rrggbbaa`;for(let t of[`hardness`,`opacity`,`vary`,`smudge`])if(e[t]!==void 0&&!(W(e[t])&&e[t]>=0&&e[t]<=1))return`${t} must be 0..1`;for(let t of[`strand`,`spread`,`wobble`])if(e[t]!==void 0&&!(W(e[t])&&e[t]>=0))return`${t} must be 0 or more`;return e.bristles!==void 0&&!(Number.isInteger(e.bristles)&&e.bristles>=1&&e.bristles<=200)?`bristles must be a whole number from 1 to 200`:e.seed!==void 0&&!Number.isInteger(e.seed)?`seed must be a whole number`:e.taper!==void 0&&!(Array.isArray(e.taper)&&e.taper.length===2&&e.taper.every(e=>W(e)&&e>=0&&e<=1))?`taper must be [start, end], each 0..1`:e.blend!==void 0&&!Ee.includes(e.blend)?`blend must be one of ${Ee.join(`, `)}`:null}function Ke(e,t,n){let r=e=>{throw Error(`${t}: ${e}`)};(typeof e!=`object`||!e)&&r(`must be an object`);for(let t of Object.keys(e))Pe.includes(t)||r(`unknown field \"${t}\"; a layer takes ${Pe.join(`, `)}`);Ie.includes(e.shape)||r(`shape ${JSON.stringify(e.shape)} must be one of ${Ie.join(`, `)}`),e.id!==void 0&&((typeof e.id!=`string`||!e.id)&&r(`id must be a string`),n.has(e.id)&&r(`the id \"${String(e.id)}\" is already used`),n.add(e.id));let i=(t,n=()=>!0)=>{(!W(e[t])||!n(e[t]))&&r(`${t} must be a number${n===o?` above 0`:``}`)},a=t=>{G(e[t])||r(`${t} must be [a, b]`)},o=e=>e>0;switch(e.shape){case`rect`:e.face===void 0&&(a(`at`),a(`size`)),e.radius!==void 0&&!W(e.radius)&&!(Array.isArray(e.radius)&&e.radius.length===4&&e.radius.every(W))&&r(`radius must be a number or four corners`);break;case`circle`:a(`at`),i(`radius`,o);break;case`ellipse`:a(`at`),a(`radii`);break;case`ring`:a(`at`),i(`radius`,o),i(`inner`,o),e.inner>=e.radius&&r(`inner must be less than radius`);break;case`polygon`:case`line`:(!Array.isArray(e.points)||e.points.length<(e.shape===`line`?2:3)||!e.points.every(G))&&r(`points must be ${e.shape===`line`?`two`:`three`} or more [x, y]`),e.shape===`line`&&!e.stroke&&r(`a line needs a stroke: { color, width }`);break;case`star`:case`gear`:a(`at`),i(`radius`,o),i(`inner`,o),(!Number.isInteger(e.count)||e.count<3)&&r(`count must be 3 or more`);break;case`path`:(typeof e.d!=`string`||!e.d.trim())&&r(`d must be an SVG path: M L H V C S Q T Z`);try{Te(e.d).every(e=>e.points.length===0)&&r(`d draws nothing`)}catch(e){r(e.message)}break;case`figure`:{G(e.at)||r(`at must be [x, y]: the place on the floor under her`),W(e.height)&&e.height>0||r(`height must be pixels from floor to crown`);for(let t of[`yaw`,`pitch`])e[t]!==void 0&&!W(e[t])&&r(`${t} must be degrees`);e.light!==void 0&&!(Array.isArray(e.light)&&e.light.length===3&&e.light.every(W))&&r(`light must be [x, y, z], x right, y up, z toward the viewer`);let t=e.build;for(let[e,n]of Object.entries(t??{}))[`head`,`shoulders`,`chest`,`waist`,`hips`,`limbs`].includes(e)||r(`build has unknown \"${e}\"; it takes head, shoulders, chest, waist, hips, limbs`),W(n)&&n>0||r(`build ${e} must be positive`);let n=e.pose;for(let e of Object.keys(n??{}))[`rotate`,`move`,`reach`].includes(e)||r(`pose has unknown \"${e}\"; it takes rotate, move, reach`);for(let[e,t]of Object.entries(n??{}))for(let[n,i]of Object.entries(t)){let t=e===`reach`?i.at:i,a=e===`reach`?[2,3]:[3];Array.isArray(t)&&a.includes(t.length)&&t.every(W)||r(`pose ${e} \"${n}\" must be ${e===`reach`?`{ at: [x, y] or [x, y, z] }`:`[x, y, z]`}`)}break}case`text`:(typeof e.text!=`string`||!e.text)&&r(`text must say something`),e.within!==void 0&&!(Array.isArray(e.within)&&e.within.length===4&&e.within.every(e=>W(e)&&e>=0&&e<=1))&&r(`within must be [left, top, right, bottom], each 0..1`),e.face===void 0?e.quad===void 0?(a(`at`),W(e.fontSize)&&e.fontSize>0||r(`fontSize must be positive, in pixels`)):Array.isArray(e.quad)&&e.quad.length===4&&e.quad.every(G)||r(`quad must be four [x, y], in order round from the top left`):typeof e.face!=`string`&&r(`face must name a face of a block, as \"id.a\"`),e.font!==void 0&&typeof e.font!=`string`&&r(`font must be a family name or a font file in the project`),e.align!==void 0&&![`left`,`center`,`right`].includes(e.align)&&r(`align must be left, center or right`),e.tracking!==void 0&&!W(e.tracking)&&r(`tracking must be pixels`),e.thin!==void 0&&!(W(e.thin)&&e.thin>=0)&&r(`thin must be 0 or more pixels`);for(let t of[`grain`,`dry`])e[t]!==void 0&&!(W(e[t])&&e[t]>=0&&e[t]<=1)&&r(`${t} must be 0..1`);e.grainScale!==void 0&&!(W(e.grainScale)&&e.grainScale>0)&&r(`grainScale must be positive`),e.fill===void 0&&e.stroke===void 0&&r(`it needs a fill or a stroke, or it draws nothing`);break;case`adjust`:{let t=Oe(e.adjust);t&&r(`adjust ${t}`);break}case`filter`:{let t=Ae(e.filter,e.params);t&&r(t);break}case`bloom`:for(let t of[`threshold`,`strength`])e[t]!==void 0&&!(W(e[t])&&e[t]>=0)&&r(`${t} must be 0 or more`);e.threshold!==void 0&&e.threshold>1&&r(`threshold must be 0..1`),e.radius!==void 0&&!(W(e.radius)&&e.radius>0)&&r(`radius must be positive, in pixels`);break;case`perspective`:(!Array.isArray(e.vanish)||e.vanish.length===0||!e.vanish.every(G))&&r(`vanish must be one or more [x, y]`);break;case`block`:a(`at`),W(e.height)&&e.height>0||r(`height must be pixels, from the corner at its foot up to its top`),(!Array.isArray(e.vanish)||e.vanish.length<1||e.vanish.length>2||!e.vanish.every(G))&&r(`vanish must be one or two [x, y], where its faces recede to`),(!Array.isArray(e.depth)||e.depth.length!==e.vanish.length||!e.depth.every(e=>W(e)&&e>0&&e<=1))&&r(`depth must give each vanishing point how far the face runs toward it, each above 0 and up to 1`),(!Array.isArray(e.faces)||e.faces.length!==e.vanish.length)&&r(`faces must give one fill for each vanishing point`);for(let t of e.faces){let e=Ne(t);e&&r(`face ${e}`)}if(e.roof!==void 0){let t=Ne(e.roof);t&&r(`roof ${t}`)}break;case`reflect`:!W(e.y)&&!(Array.isArray(e.edge)&&e.edge.length>=2&&e.edge.every(G))&&r(`y must be the line it mirrors about, or edge two or more [x, y] along where things meet the ground`);for(let t of[`reach`,`ripple`,`smear`])e[t]!==void 0&&!(W(e[t])&&e[t]>=0)&&r(`${t} must be 0 or more pixels`);break;case`image`:(typeof e.src!=`string`||!e.src)&&r(`src must be the path of a picture in the project`),e.at!==void 0&&!G(e.at)&&r(`at must be [x, y]`),e.size!==void 0&&!G(e.size)&&r(`size must be [w, h]`);break;case`paint`:Array.isArray(e.strokes)||r(`strokes must be an array, which may start empty`),e.face!==void 0&&typeof e.face!=`string`&&r(`face must name a face of a block, as \"id.a\"`),e.from!==void 0&&e.from!==`below`&&r(`from must be \"below\"`),e.from!==void 0&&(e.fill!==void 0||e.effects!==void 0||e.blend!==void 0)&&r(`from \"below\" works on the picture itself, and takes no fill, effects or blend`),e.strokes.forEach((e,t)=>{let n=Ge(e);n&&r(`stroke ${t} ${n}`)});break;case`pixels`:{(!Array.isArray(e.rows)||e.rows.length===0||!e.rows.every(e=>typeof e==`string`))&&r(`rows must be strings, one character a pixel`);let t=e.palette;(typeof t!=`object`||!t)&&r(`palette must be { character: colour }`);for(let[e,n]of Object.entries(t))[...e].length!==1&&r(`palette key ${JSON.stringify(e)} must be one character`),K(n)||r(`palette \"${e}\" ${JSON.stringify(n)} must be a colour`);let n=new Set(Object.keys(t)),s=e.rows[0].length;e.rows.forEach((e,t)=>{[...e].length!==s&&r(`row ${t} is ${[...e].length} wide and row 0 is ${s}: a slip here shifts everything after it`);for(let i of e)i!==`.`&&i!==` `&&!n.has(i)&&r(`row ${t} has \"${i}\", which is not in the palette; \".\" and \" \" are nothing`)}),e.scale!==void 0&&i(`scale`,o),e.at!==void 0&&a(`at`);break}case`group`:Array.isArray(e.layers)||r(`a group needs layers`),e.layers.forEach((e,r)=>Ke(e,`${t}.layers[${r}]`,n))}if(e.fill!==void 0){let t=Ne(e.fill);t&&r(`fill ${t}`)}if(e.stroke!==void 0){let t=e.stroke;(typeof t!=`object`||!t||!W(t.width)||t.width<=0)&&r(`stroke must be { color, width, align? }`);let n=Ne(t.color);n&&r(`stroke color ${n}`),t.align!==void 0&&![`center`,`inside`,`outside`].includes(t.align)&&r(`stroke align must be center, inside or outside`),t.join!==void 0&&![`round`,`miter`,`bevel`].includes(t.join)&&r(`stroke join must be round, miter or bevel`),t.cap!==void 0&&![`round`,`butt`,`square`].includes(t.cap)&&r(`stroke cap must be round, butt or square`)}![`group`,`pixels`,`paint`,`figure`,`image`,`text`,`block`,`perspective`,`reflect`,`bloom`,`adjust`,`filter`,`line`].includes(e.shape)&&e.fill===void 0&&e.stroke===void 0&&!e.erase&&r(`it needs a fill or a stroke, or it draws nothing`),e.from!==void 0&&e.shape!==`paint`&&r(`from is for a paint layer`),e.guide!==void 0&&typeof e.guide!=`boolean`&&r(`guide must be true or false`),e.blur!==void 0&&!(W(e.blur)&&e.blur>=0)&&r(`blur must be 0 or more, in pixels`),e.blur&&e.erase&&r(`blur does not go with erase; put what cuts in a group with what it cuts, and blur the group`),e.opacity!==void 0&&!(W(e.opacity)&&e.opacity>=0&&e.opacity<=1)&&r(`opacity must be 0..1`),e.blend!==void 0&&!Ee.includes(e.blend)&&r(`blend must be one of ${Ee.join(`, `)}`);for(let t of[`feather`,`clipFeather`])e[t]!==void 0&&!(W(e[t])&&e[t]>=0)&&r(`${t} must be 0 or more pixels`);if(e.clipGrow!==void 0&&!W(e.clipGrow)&&r(`clipGrow must be pixels, more to grow what it clips to and less to shrink it`),e.clipInvert!==void 0&&typeof e.clipInvert!=`boolean`&&r(`clipInvert must be true or false`),e.blendIf!==void 0&&!(Array.isArray(e.blendIf)&&(e.blendIf.length===2||e.blendIf.length===3)&&e.blendIf.every(e=>W(e)&&e>=0&&e<=1))&&r(`blendIf must be [low, high] or [low, high, softness], each 0..1`),e.mirror!==void 0&&e.mirror!==`x`&&e.mirror!==`y`&&r(`mirror must be x or y`),e.clip!==void 0&&typeof e.clip!=`string`&&r(`clip must be a layer id`),e.transform!==void 0){let t=e.transform;for(let e of Object.keys(t))[`translate`,`rotate`,`scale`,`origin`].includes(e)||r(`transform has an unknown field \"${e}\"`);t.translate!==void 0&&!G(t.translate)&&r(`transform.translate must be [x, y]`),t.origin!==void 0&&!G(t.origin)&&r(`transform.origin must be [x, y]`),t.rotate!==void 0&&!W(t.rotate)&&r(`transform.rotate must be degrees`),t.scale!==void 0&&!W(t.scale)&&!G(t.scale)&&r(`transform.scale must be a number or [x, y]`)}if(e.effects!==void 0){Array.isArray(e.effects)||r(`effects must be an array`);for(let t of e.effects){if([`shadow`,`glow`,`innerShadow`,`innerGlow`,`bevel`,`overlay`].includes(t?.type)||r(`an effect has type ${JSON.stringify(t?.type)}; there are shadow, glow, innerShadow, innerGlow, bevel, overlay`),t.type===`overlay`){let e=Ne(t.fill);e&&r(`effect overlay: fill ${e}`)}t.blend!==void 0&&!Ee.includes(t.blend)&&r(`effect ${String(t.type)}: blend must be one of ${Ee.join(`, `)}`),t.shade!==void 0&&!K(t.shade)&&r(`effect ${String(t.type)}: shade must be #rgb..#rrggbbaa`);for(let e of Object.keys(t))[`type`,`color`,`x`,`y`,`blur`,`opacity`,`size`,`angle`,`depth`,`shade`,`fill`,`blend`].includes(e)||r(`effect ${String(t.type)} has an unknown field \"${e}\"`);t.color!==void 0&&!K(t.color)&&r(`effect ${String(t.type)}: color must be #rgb..#rrggbbaa`);for(let e of[`x`,`y`,`blur`,`opacity`,`size`,`angle`,`depth`])t[e]!==void 0&&!W(t[e])&&r(`effect ${String(t.type)}: ${e} must be a number`)}}}function qe(e){if(typeof e!=`object`||!e||Array.isArray(e))throw Error(`an image source must be { size, layers }`);let t=e;for(let e of Object.keys(t))if(![`version`,`size`,`background`,`tile`,`material`,`layers`].includes(e))throw Error(`unknown field \"${e}\"; an image has version, size, background, tile, material, layers`);if(t.tile!==void 0&&typeof t.tile!=`boolean`)throw Error(`tile must be true or false`);if(t.version!==void 0&&t.version!==1)throw Error(`version ${String(t.version)} is not one this editor reads (1)`);let[n,r]=typeof t.size==`number`?[t.size,t.size]:G(t.size)?t.size:[NaN,NaN];if(!Number.isInteger(n)||!Number.isInteger(r)||n<1||r<1||n>2048||r>2048)throw Error(`size must be a whole number or [width, height], 1..2048`);if(t.background!==void 0&&!K(t.background))throw Error(`background must be a colour; leave it out for a transparent picture`);if(!Array.isArray(t.layers))throw Error(`layers must be an array`);let i=new Set;if(t.layers.forEach((e,t)=>Ke(e,`layer ${t}${e?.id?` \"${String(e.id)}\"`:``}`,i)),t.material!==void 0){let e=t.material;if(typeof e!=`object`||!e||Array.isArray(e))throw Error(`material must be { relief?, height?, roughness?, metal? }`);for(let t of Object.keys(e))if(![`relief`,`height`,`roughness`,`metal`].includes(t))throw Error(`material has unknown \"${t}\"; it takes relief, height, roughness, metal`);if(e.relief!==void 0&&!(W(e.relief)&&e.relief>=0&&e.relief<=64))throw Error(`material.relief must be 0..64, in pixels`);let n=(e,t)=>{if(!Array.isArray(t)||t.length===0)throw Error(`material.${e} must be layers, one or more`);t.forEach((t,n)=>Ke(t,`material.${e} layer ${n}${t?.id?` \"${String(t.id)}\"`:``}`,i))};e.height!==void 0&&n(`height`,e.height);for(let t of[`roughness`,`metal`]){let r=e[t];if(r!==void 0)if(W(r)){if(r<0||r>1)throw Error(`material.${t} must be 0..1, or layers`)}else n(t,r)}}return{version:1,...t}}let Je=e=>{let t=Math.hypot(e[0],e[1],e[2])||1;return[e[0]/t,e[1]/t,e[2]/t]},Ye=[1,0,0,1,0,0],Xe=0,Ze=0,Qe=(e,t)=>[e[0]*t[0]+e[2]*t[1],e[1]*t[0]+e[3]*t[1],e[0]*t[2]+e[2]*t[3],e[1]*t[2]+e[3]*t[3],e[0]*t[4]+e[2]*t[5]+e[4],e[1]*t[4]+e[3]*t[5]+e[5]],q=(e,[t,n])=>[e[0]*t+e[2]*n+e[4],e[1]*t+e[3]*n+e[5]];function $e(e){let t=e[0]*e[3]-e[1]*e[2]||1e-12;return[e[3]/t,-e[1]/t,-e[2]/t,e[0]/t,(e[2]*e[5]-e[3]*e[4])/t,(e[1]*e[4]-e[0]*e[5])/t]}function et(e,t){if(!e)return Ye;let[n,r]=e.origin??t,i=(e.rotate??0)*Math.PI/180,[a,o]=typeof e.scale==`number`?[e.scale,e.scale]:e.scale??[1,1],[s,c]=e.translate??[0,0],l=[1,0,0,1,s+n,c+r];return l=Qe(l,[Math.cos(i),Math.sin(i),-Math.sin(i),Math.cos(i),0,0]),l=Qe(l,[a,0,0,o,0,0]),Qe(l,[1,0,0,1,-n,-r])}function tt(e,t,n,r){let i=Math.max(48,Math.ceil(Math.max(n,r)*2));return Array.from({length:i},(a,o)=>[e+n*Math.cos(o/i*Math.PI*2),t+r*Math.sin(o/i*Math.PI*2)])}function nt(e,t,n,r,i){let a=typeof i==`number`?[i,i,i,i]:i,o=Math.min(n,r)/2,[s,c,l,u]=[0,1,2,3].map(e=>Math.max(0,Math.min(o,a[e]??0))),d=[],f=(e,t,n,r)=>{if(n<=0){d.push([e,t]);return}let i=Math.max(4,Math.ceil(n));for(let a=0;a<=i;a++){let o=r+a/i*(Math.PI/2);d.push([e+n*Math.cos(o),t+n*Math.sin(o)])}};return f(e+s,t+s,s,Math.PI),f(e+n-c,t+c,c,-Math.PI/2),f(e+n-l,t+r-l,l,0),f(e+u,t+r-u,u,Math.PI/2),d}function rt(e){let t=e=>[{points:e,closed:!0}];switch(e.shape){case`rect`:{let[n,r]=e.at,[i,a]=e.size;return{runs:t(nt(n,r,i,a,e.radius??0)),middle:[n+i/2,r+a/2]}}case`circle`:{let[n,r]=e.at;return{runs:t(tt(n,r,e.radius,e.radius)),middle:[n,r]}}case`ellipse`:{let[n,r]=e.at,[i,a]=e.radii;return{runs:t(tt(n,r,i,a)),middle:[n,r]}}case`ring`:{let[n,r]=e.at,i=tt(n,r,e.inner,e.inner).reverse();return{runs:[...t(tt(n,r,e.radius,e.radius)),...t(i)],middle:[n,r]}}case`polygon`:case`line`:{let t=e.points,n=t.reduce((e,n)=>[e[0]+n[0]/t.length,e[1]+n[1]/t.length],[0,0]);return{runs:[{points:t,closed:e.shape===`polygon`}],middle:n}}case`star`:case`gear`:{let[n,r]=e.at,i=e.count,a=e.radius,o=e.inner,s=[],c=(e.turn??-90)*Math.PI/180;if(e.shape===`star`)for(let e=0;e<i*2;e++){let t=e%2==0?a:o,l=c+e/(i*2)*Math.PI*2;s.push([n+t*Math.cos(l),r+t*Math.sin(l)])}else{let t=Math.PI*2/i,l=(e.tooth??.3)*t,u=l*1.5;for(let e=0;e<i;e++){let i=c+e*t;for(let[e,t]of[[o,-u],[a,-l],[a,l],[o,u]])s.push([n+e*Math.cos(i+t),r+e*Math.sin(i+t)])}}return{runs:t(s),middle:[n,r]}}case`path`:{let t=Te(e.d),n=t.flatMap(e=>e.points);if(n.length===0)throw Error(`path draws nothing`);return{runs:t,middle:n.reduce((e,t)=>[e[0]+t[0]/n.length,e[1]+t[1]/n.length],[0,0])}}default:return{runs:[],middle:[0,0]}}}let it=[[0,0],[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[1,-1],[-1,1],[1,1]];function at(e,t,n,r,i,a){let[o,s,c,l]=[1/0,1/0,-1/0,-1/0];for(let t of e)t[0]<o&&(o=t[0]),t[0]>c&&(c=t[0]),t[1]<s&&(s=t[1]),t[1]>l&&(l=t[1]);return c+t+r>0&&o+t-r<i&&l+n+r>0&&s+n-r<a}function ot(e,t,n,r,i){if(!L())return i(0,0);let a=null;for(let[o,s]of it){let[c,l]=[o*n,s*r];if((o!==0||s!==0)&&!at(e(),c,l,t,n,r))continue;let u=i(c,l);if(!a){a=u;continue}for(let e=0;e<a.length;e++)u[e]>a[e]&&(a[e]=u[e])}return a??new Float32Array(n*r)}function st(e,t,n){return ot(()=>e.flat(),1,t,n,(r,i)=>ct(r===0&&i===0?e:e.map(e=>e.map(([e,t])=>[e+r,t+i])),t,n))}function ct(e,t,n){let r=new Float32Array(t*n),i=[],a=1/0,o=-1/0;for(let t of e)for(let e=0;e<t.length;e++){let[n,r]=[t[e],t[(e+1)%t.length]];if(n[1]===r[1])continue;let s=n[1]<r[1];i.push(s?{x0:n[0],y0:n[1],x1:r[0],y1:r[1],dir:1}:{x0:r[0],y0:r[1],x1:n[0],y1:n[1],dir:-1}),a=Math.min(a,n[1],r[1]),o=Math.max(o,n[1],r[1])}if(i.length===0)return r;let s=1/8,c=Math.max(0,Math.floor(a)),l=Math.min(n-1,Math.ceil(o)),u=[];for(let e=c;e<=l;e++)for(let n=0;n<8;n++){let a=e+(n+.5)*s;u.length=0;for(let e of i)a<e.y0||a>=e.y1||u.push({x:e.x0+(a-e.y0)/(e.y1-e.y0)*(e.x1-e.x0),dir:e.dir});if(u.length<2)continue;u.sort((e,t)=>e.x-t.x);let o=0;for(let n=0;n+1<u.length;n++){if(o+=u[n].dir,o===0)continue;let[i,a]=[Math.max(0,u[n].x),Math.min(t,u[n+1].x)];if(a<=i)continue;let[c,l]=[Math.floor(i),Math.min(t-1,Math.floor(a))],d=e*t;if(c===l){r[d+c]+=(a-i)*s;continue}r[d+c]+=(c+1-i)*s;for(let e=c+1;e<l;e++)r[d+e]+=s;r[d+l]+=(a-l)*s}}for(let e=0;e<r.length;e++)r[e]>1&&(r[e]=1);return r}function lt(e,t,n,r,i=`round`,a=`round`){return ot(()=>e.flatMap(e=>e.points),t*(i===`miter`?10:1)+2,n,r,(o,s)=>ut(o===0&&s===0?e:e.map(e=>({closed:e.closed,points:e.points.map(([e,t])=>[e+o,t+s])})),t,n,r,i,a))}function ut(e,t,n,r,i=`round`,a=`round`){let o=new Float32Array(n*r),s=t/2,c=(e,t)=>{let[i,a]=[Math.max(0,Math.floor(Math.min(e[0],t[0])-s-1)),Math.min(n-1,Math.ceil(Math.max(e[0],t[0])+s+1))],[c,l]=[Math.max(0,Math.floor(Math.min(e[1],t[1])-s-1)),Math.min(r-1,Math.ceil(Math.max(e[1],t[1])+s+1))],[u,d]=[t[0]-e[0],t[1]-e[1]],f=u*u+d*d;for(let t=c;t<=l;t++)for(let r=i;r<=a;r++){let[i,a]=[r+.5-e[0],t+.5-e[1]],c=f>0?Math.max(0,Math.min(1,(i*u+a*d)/f)):0,l=Math.max(0,Math.min(1,s-Math.hypot(i-u*c,a-d*c)+.5));l>o[t*n+r]&&(o[t*n+r]=l)}},l=e=>{let t=0;for(let n=0;n<e.length;n++){let[r,i]=[e[n],e[(n+1)%e.length]];t+=r[0]*i[1]-i[0]*r[1]}if(t===0)return;let i=e.map((n,r)=>{let i=e[(r+1)%e.length],a=Math.hypot(i[0]-n[0],i[1]-n[1])||1;return{x:n[0],y:n[1],nx:-(i[1]-n[1])/a*Math.sign(t),ny:(i[0]-n[0])/a*Math.sign(t)}}),[a,s]=[e.map(e=>e[0]),e.map(e=>e[1])],[c,l]=[Math.max(0,Math.floor(Math.min(...a)-1)),Math.min(n-1,Math.ceil(Math.max(...a)+1))],[u,d]=[Math.max(0,Math.floor(Math.min(...s)-1)),Math.min(r-1,Math.ceil(Math.max(...s)+1))];for(let e=u;e<=d;e++)for(let t=c;t<=l;t++){let r=1/0;for(let n of i)r=Math.min(r,(t+.5-n.x)*n.nx+(e+.5-n.y)*n.ny);if(r<=-.75)continue;let a=1;if(r<.75){let n=0;for(let r=0;r<6;r++)for(let a=0;a<6;a++){let[o,s]=[t+(a+.5)/6,e+(r+.5)/6];i.every(e=>(o-e.x)*e.nx+(s-e.y)*e.ny>=0)&&n++}a=n/36}a>o[e*n+t]&&(o[e*n+t]=a)}};for(let t of e){if(i===`round`&&a===`round`){let e=t.closed?t.points.length:t.points.length-1;for(let n=0;n<e;n++)c(t.points[n],t.points[(n+1)%t.points.length]);continue}let e=t.points.filter((e,t,n)=>t===0||Math.hypot(e[0]-n[t-1][0],e[1]-n[t-1][1])>1e-6);if(t.closed&&e.length>1&&Math.hypot(e[0][0]-e[e.length-1][0],e[0][1]-e[e.length-1][1])<=1e-6&&e.pop(),e.length<2){e[0]&&a!==`butt`&&c(e[0],e[0]);continue}let n=t.closed?e.length:e.length-1,r=t=>{let[n,r]=[e[t],e[(t+1)%e.length]],i=Math.hypot(r[0]-n[0],r[1]-n[1]);return[(r[0]-n[0])/i,(r[1]-n[1])/i]};for(let i=0;i<n;i++){let[o,c]=[e[i],e[(i+1)%e.length]],u=r(i),[d,f]=[-u[1]*s,u[0]*s],p=e=>e?a===`square`?s:0:.75,[m,h]=[p(!t.closed&&i===0),p(!t.closed&&i===n-1)],[g,_,v,y]=[o[0]-u[0]*m,o[1]-u[1]*m,c[0]+u[0]*h,c[1]+u[1]*h];l([[g+d,_+f],[v+d,y+f],[v-d,y-f],[g-d,_-f]])}!t.closed&&a===`round`&&(c(e[0],e[0]),c(e[e.length-1],e[e.length-1]));for(let n=+!t.closed;n<(t.closed?e.length:e.length-1);n++){let t=e[n],[a,o]=[r((n-1+e.length)%e.length),r(n)],u=a[0]*o[1]-a[1]*o[0];if(i===`round`){c(t,t);continue}if(Math.abs(u)<1e-6)continue;let d=u>0?-1:1,[f,p]=[[-a[1]*d,a[0]*d],[-o[1]*d,o[0]*d]],[m,h]=[[t[0]+f[0]*s,t[1]+f[1]*s],[t[0]+p[0]*s,t[1]+p[1]*s]],[g,_]=[f[0]+p[0],f[1]+p[1]],v=Math.hypot(g,_),y=v>0?2/v:1/0;l(i===`miter`&&y<=10?[t,m,[t[0]+g/v*s*y,t[1]+_/v*s*y],h]:[t,m,h])}}return o}function dt(e,t,n,r,i){let[a,o]=[Math.round(r),Math.round(i)];if(a===0&&o===0)return e;let s=new Float32Array(t*n);if(L()){for(let r=0;r<n;r++){let i=((r-o)%n+n)%n;for(let n=0;n<t;n++)s[r*t+n]=e[i*t+((n-a)%t+t)%t]}return s}for(let r=0;r<n;r++){let i=r-o;if(!(i<0||i>=n))for(let n=0;n<t;n++){let o=n-a;o>=0&&o<t&&(s[r*t+n]=e[i*t+o])}}return s}function ft(e){let[[t,n],[r,i],[a,o],[s,c]]=e,[l,u,d,f,p,m]=[r-a,s-a,t-r+a-s,i-o,c-o,n-i+o-c],h=l*p-u*f;if(Math.abs(h)<1e-9)return null;let g=(d*p-u*m)/h,_=(l*m-d*f)/h,[v,y,b,x,S,C]=[r-t+g*r,s-t+_*s,t,i-n+g*i,c-n+_*c,n],[w,T,E,D,O,k,A,ee,te]=[S-C*_,b*_-y,y*C-b*S,C*g-x,v-b*g,b*x-v*C,x*_-S*g,y*g-v*_,v*S-y*x];return(e,t)=>{let n=A*e+ee*t+te;return[(w*e+T*t+E)/n,(D*e+O*t+k)/n]}}function pt(e){let[[t,n],[r,i],[a,o],[s,c]]=e,[l,u,d,f,p,m]=[r-a,s-a,t-r+a-s,i-o,c-o,n-i+o-c],h=l*p-u*f||1e-9,g=(d*p-u*m)/h,_=(l*m-d*f)/h,[v,y,b,x,S,C]=[r-t+g*r,s-t+_*s,t,i-n+g*i,c-n+_*c,n];return(e,t)=>{let n=g*e+_*t+1;return[(v*e+y*t+b)/n,(x*e+S*t+C)/n]}}function mt(e,t){let[n,r]=e.face.replace(/^@/,``).split(`.`),i=r?t.blocks.get(n)?.[r]:void 0;if(!i)return t.notes.push(`layer ${e.id??e.shape}: face \"${e.face}\" names no face of a block drawn before it, so it drew nothing`),null;if(!e.within)return i;let a=pt(i),[o,s,c,l]=e.within;return[a(o,s),a(c,s),a(c,l),a(o,l)]}function ht(e,t){let n=e.colors.map(F),r=e.ground?F(e.ground):[0,0,0,0],i=e.off?F(e.off):r,[a,o]=e.mark??[.6,.6],[s,c]=[e.lit??1,e.seed??1],l=$e(t),u=e.quad?ft(e.quad):null,[d,f]=e.cells??[1,1],[p,m]=e.cell??[1,1],h=(e,t)=>{let h,g;if(u){let n=u(e,t);if(n[0]<0||n[1]<0||n[0]>1||n[1]>1)return r;[h,g]=[n[0]*d,n[1]*f]}else{let n=q(l,[e,t]);[h,g]=[n[0]/p,n[1]/m]}let[_,v]=[Math.floor(h),Math.floor(g)];return Math.abs(h-_-.5)>a/2||Math.abs(g-v-.5)>o/2?r:$(c*41+_,v)>=s?i:n[Math.floor($(c*43+v,_)*n.length)%n.length]};return(e,t)=>{let n=[0,0,0,0];for(let r=0;r<3;r++)for(let i=0;i<3;i++){let a=h(e-.5+(i+.5)/3,t-.5+(r+.5)/3);n[0]+=a[0]*a[3],n[1]+=a[1]*a[3],n[2]+=a[2]*a[3],n[3]+=a[3]}let r=n[3];return r>0?[n[0]/r,n[1]/r,n[2]/r,r/9]:[0,0,0,0]}}function gt(e,t,n,r=0){if(typeof e==`string`){let t=F(e);return()=>t}let i=[...e.type===`image`||e.type===`grid`?[]:e.stops].map(([e,t])=>[e,F(t)]).sort((e,t)=>e[0]-t[0]),a=$e(t),o=e=>{if(e<=i[0][0])return i[0][1];for(let t=1;t<i.length;t++)if(e<=i[t][0]){let[n,r]=i[t-1],[a,o]=i[t],s=(e-n)/Math.max(1e-6,a-n);return[r[0]+(o[0]-r[0])*s,r[1]+(o[1]-r[1])*s,r[2]+(o[2]-r[2])*s,r[3]+(o[3]-r[3])*s]}return i[i.length-1][1]};if(e.type===`image`){let t=n?.images.get(e.image);return t?(e,n)=>{let i=(Math.floor(n)*r+Math.floor(e))*4;return[t[i]??0,t[i+1]??0,t[i+2]??0,t[i+3]??0]}:(n?.notes.push(`a fill names image \"${e.image}\", which was not drawn before it`),()=>[0,0,0,0])}if(e.type===`grid`)return ht(e,t);if(e.type===`form`){let t=n?.figures.get(e.figure);if(!t)return n?.notes.push(`a form names figure \"${e.figure}\", which was not drawn before it`),()=>o(0);let i=e.light?Je(e.light):t.light;return(e,n)=>{let a=Math.floor(n)*r+Math.floor(e),[s,c,l]=[t.normals[a*3]??0,t.normals[a*3+1]??0,t.normals[a*3+2]??0];return o(.5+.5*(s*i[0]+c*i[1]+l*i[2]))}}if(e.type===`noise`){let[t,n,r]=[e.scale??32,e.octaves??4,e.seed??0];if(L()&&Xe>0){let[e,i]=[Math.max(1,Math.round(Xe/t)),Math.max(1,Math.round(Ze/t))];return(t,a)=>o(B((t-.5)/Xe*e,(a-.5)/Ze*i,n,r,e,i))}return(e,i)=>{let s=q(a,[e-.5,i-.5]);return o(B(s[0]/t,s[1]/t,n,r))}}if(e.type===`linear`){let[t,n]=[e.from,e.to],[r,i]=[n[0]-t[0],n[1]-t[1]],s=r*r+i*i||1;return(e,n)=>{let c=q(a,[e,n]);return o(((c[0]-t[0])*r+(c[1]-t[1])*i)/s)}}let[s,c]=e.at,l=e.radius;return(e,t)=>{let n=q(a,[e,t]);return o(Math.hypot(n[0]-s,n[1]-c)/l)}}var _t=class{constructor(e,t){this.w=e,this.h=t,this.px=new Float32Array(e*t*4)}};let vt=(e,t)=>{if(t<=.5)return e-(1-2*t)*e*(1-e);let n=e<=.25?((16*e-12)*e+4)*e:Math.sqrt(e);return e+(2*t-1)*(n-e)},yt=(e,t)=>e<=0?0:t>=1?1:Math.min(1,e/(1-t)),bt=(e,t)=>e>=1?1:t<=0?0:1-Math.min(1,(1-e)/t),xt=(e,t)=>t<.5?2*e*t:1-2*(1-e)*(1-t),St=(e,t,n)=>{switch(e){case`multiply`:return t*n;case`screen`:return t+n-t*n;case`overlay`:return xt(n,t);case`add`:return Math.min(1,t+n);case`softLight`:return vt(t,n);case`hardLight`:return xt(t,n);case`vividLight`:return n<.5?bt(t,2*n):yt(t,2*n-1);case`linearLight`:return Math.max(0,Math.min(1,t+2*n-1));case`pinLight`:return n<.5?Math.min(t,2*n):Math.max(t,2*n-1);case`colorDodge`:return yt(t,n);case`colorBurn`:return bt(t,n);case`linearBurn`:return Math.max(0,t+n-1);case`darken`:return Math.min(t,n);case`lighten`:return Math.max(t,n);case`difference`:return Math.abs(t-n);case`exclusion`:return t+n-2*t*n;case`subtract`:return Math.max(0,t-n);case`divide`:return n<=0?1:Math.min(1,t/n);default:return n}};function Ct(e,t){let n=t-H(e[0],e[1],e[2]),r=[e[0]+n,e[1]+n,e[2]+n],i=H(r[0],r[1],r[2]),[a,o]=[Math.min(...r),Math.max(...r)];if(a<0)for(let e=0;e<3;e++)r[e]=i+(r[e]-i)*i/(i-a);if(o>1)for(let e=0;e<3;e++)r[e]=i+(r[e]-i)*(1-i)/(o-i);return r}let wt=e=>Math.max(...e)-Math.min(...e);function Tt(e,t){let[n,r]=[Math.min(...e),Math.max(...e)];return r===n?[0,0,0]:[(e[0]-n)*t/(r-n),(e[1]-n)*t/(r-n),(e[2]-n)*t/(r-n)]}function Et(e,t,n){switch(e){case`hue`:return Ct(Tt(n,wt(t)),H(t[0],t[1],t[2]));case`saturation`:return Ct(Tt(t,wt(n)),H(t[0],t[1],t[2]));case`color`:return Ct(n,H(t[0],t[1],t[2]));default:return Ct(t,H(n[0],n[1],n[2]))}}let Dt=new Set([`hue`,`saturation`,`color`,`luminosity`]);function J(e,t,n,r,i,a,o){if(a<=0)return;let s=e.px,c=t*4,l=s[c+3];if(o===`normal`||l<=0)s[c]=s[c]*(1-a)+n*a,s[c+1]=s[c+1]*(1-a)+r*a,s[c+2]=s[c+2]*(1-a)+i*a;else{let[e,t,u]=[s[c]/l,s[c+1]/l,s[c+2]/l];if(Dt.has(o)){let d=Et(o,[e,t,u],[n,r,i]),[f,p,m]=[(1-l)*n+l*d[0],(1-l)*r+l*d[1],(1-l)*i+l*d[2]];s[c]=s[c]*(1-a)+f*a,s[c+1]=s[c+1]*(1-a)+p*a,s[c+2]=s[c+2]*(1-a)+m*a,s[c+3]=l+a-l*a;return}let d=(e,t)=>(1-l)*t+l*St(o,e,t);s[c]=s[c]*(1-a)+d(e,n)*a,s[c+1]=s[c+1]*(1-a)+d(t,r)*a,s[c+2]=s[c+2]*(1-a)+d(u,i)*a}s[c+3]=l+a-l*a}function Ot(e,t,n){let{w:r,h:i}=t,a=R(n,r,i,Math.max(.5,(e.size??4)/2)),o=(e.angle??225)*Math.PI/180,[s,c]=[Math.cos(o),Math.sin(o)],[l,u,d,f]=F(e.color??`#ffffff`),[p,m,h,g]=F(e.shade??`#000000`),_=(e.depth??1)*(e.size??4)*2,v=e.opacity??.8;for(let e=1;e<i-1;e++)for(let i=1;i<r-1;i++){let o=e*r+i;if(n[o]<=0)continue;let y=-((a[o+1]-a[o-1])*s+(a[o+r]-a[o-r])*c)*_;y>0?J(t,o,l,u,d,Math.min(1,y)*v*f*n[o],`screen`):y<0&&J(t,o,p,m,h,Math.min(1,-y)*v*g*n[o],`multiply`)}}function Y(e,t,n,r){let{w:i,h:a}=n;for(let o of e??[]){let e=o.type===`shadow`||o.type===`glow`;if(e!==t)continue;if(o.type===`bevel`){Ot(o,n,r);continue}if(o.type===`overlay`){let e=gt(o.fill,Ye);for(let t=0;t<a;t++)for(let a=0;a<i;a++){let s=t*i+a;if(r[s]<=0)continue;let c=e(a+.5,t+.5);J(n,s,c[0],c[1],c[2],c[3]*r[s]*(o.opacity??1),o.blend??`normal`)}continue}let[s,c,l,u]=F(o.color??(o.type===`glow`||o.type===`innerGlow`?`#ffffff`:`#000000`)),d=(o.opacity??1)*u,f=o.blur??(o.type===`glow`?8:4);if(e){let e=dt(r,i,a,o.x??0,o.y??0);o.type===`glow`&&(e=e.map(e=>Math.min(1,e*1.6)));let t=R(e,i,a,f);for(let e=0;e<t.length;e++)J(n,e,s,c,l,Math.min(1,t[e]*d),o.type===`glow`?`screen`:`normal`)}else{let e=R(dt(r.map(e=>1-e),i,a,o.x??0,o.y??0),i,a,f),t=o.type===`innerGlow`?`screen`:`normal`;for(let i=0;i<e.length;i++)J(n,i,s,c,l,Math.min(1,e[i]*r[i]*d),t)}}}function kt(e,t,n){let[r,i,a,o]=[e,t,-1,-1];for(let s=0;s<t;s++)for(let t=0;t<e;t++)n(s*e+t)&&(t<r&&(r=t),t>a&&(a=t),s<i&&(i=s),o=s);return a<0?null:[r,i,a-r+1,o-i+1]}function X(e,t,n,r,i){if(!t)return;let a=0;for(let e=0;e<n.length;e++)n[e]>a&&(a=n[e]);if(a<=.004)return;let o=kt(r,i,e=>n[e]>=Math.min(.5,a/2));o&&e.bounds.set(t,o)}function Z(e,t,n,r){let{w:i,h:a}=e;if(t.visible===!1){if(Fe(t)){let{visible:e,...o}=t;Z(new _t(i,a),{...o,guide:!1},n,r)}return}if(!r.showGuides&&Fe(t)){Z(new _t(i,a),{...t,guide:!1},n,r);return}if(t.mirror){let{mirror:o,...s}=t,c=(o===`x`?{x:-1,y:1}:{x:1,y:-1}).x<0?[-1,0,0,1,i/Q(n),0]:[1,0,0,-1,0,a/Q(n)];Z(e,s,n,r),Z(e,{...s,...t.id?{id:`${t.id}.mirrored`}:{}},Qe(n,c),r);return}let o=t.opacity??1,s=t.blend??`normal`,c=e=>r.masks.get(e.replace(/^@/,``)),l=t.clip?c(t.clip):void 0;if(l&&=(e=>{let r=e;return t.clipGrow&&(r=Qt(r,t.clipGrow*Q(n),i,a)),t.clipFeather&&(r=R(r,i,a,t.clipFeather*Q(n))),t.clipInvert?r.map(e=>1-e):r})(l),t.clip&&!l){r.notes.push(`layer ${t.id??t.shape}: clip \"${t.clip}\" names no layer drawn before it, so it drew nothing`);return}if(t.except){let e=c(t.except);e?l=e.map((e,t)=>(1-e)*(l?l[t]:1)):r.notes.push(`layer ${t.id??t.shape}: except \"${t.except}\" names no layer drawn before it`)}if(t.blendIf){let[n,r,o=.1]=t.blendIf,s=new Float32Array(i*a);for(let t=0;t<s.length;t++){let i=e.px[t*4+3],a=i>0?H(e.px[t*4]/i,e.px[t*4+1]/i,e.px[t*4+2]/i):0,c=Math.min(o>0?(a-n)/o+1:+(a>=n),o>0?(r-a)/o+1:+(a<=r));s[t]=Math.max(0,Math.min(1,c))*(l?l[t]:1)}l=s}if(t.feather){let{feather:c,opacity:u,blend:d,clip:f,except:p,blendIf:m,clipFeather:h,clipGrow:g,clipInvert:_,...v}=t,y=new _t(i,a);Z(y,v,n,r);let b=new Float32Array(i*a);for(let e=0;e<b.length;e++)b[e]=y.px[e*4+3];let x=R(b,i,a,c*Q(n)),S=new Float32Array(i*a);for(let t=0;t<b.length;t++){let n=b[t];if(n<=0)continue;let r=n*At(Math.max(0,Math.min(1,(x[t]/n-.5)*2.5)));S[t]=r*(l?l[t]:1),J(e,t,y.px[t*4]/n,y.px[t*4+1]/n,y.px[t*4+2]/n,S[t]*o,s)}t.id&&r.masks.set(t.id,S);return}if(t.shape===`adjust`||t.shape===`filter`){let r=Q(n),c=t.shape===`adjust`?_e(e.px,i,a,t.adjust):we(t.filter,t.params??{},e.px,i,a,r);for(let t=0;t<i*a;t++){let n=o*(l?l[t]:1);if(n<=0)continue;let r=t*4;if(s!==`normal`){let i=c[r+3];i>0&&J(e,t,c[r]/i,c[r+1]/i,c[r+2]/i,i*n,s);continue}for(let t=0;t<4;t++)e.px[r+t]=e.px[r+t]+(c[r+t]-e.px[r+t])*n}return}if(t.blur){let{blur:c,opacity:u,blend:d,clip:f,except:p,blendIf:m,clipFeather:h,clipGrow:g,clipInvert:_,...v}=t,y=new _t(i,a);Z(y,v,n,r);let[b,x,S,C]=[0,1,2,3].map(e=>{let t=new Float32Array(i*a);for(let n=0;n<t.length;n++)t[n]=y.px[n*4+e];return R(t,i,a,c*Q(n))});t.id&&r.masks.set(t.id,l?C.map((e,t)=>e*l[t]):C);for(let t=0;t<C.length;t++){let n=C[t];n<=0||J(e,t,b[t]/n,x[t]/n,S[t]/n,n*o*(l?l[t]:1),s)}return}if(t.shape===`group`){let c=new _t(i,a),u=Qe(n,et(t.transform,[i/2,a/2]));for(let e of t.layers??[])Z(c,e,u,r);let d=new Float32Array(i*a);for(let e=0;e<d.length;e++)d[e]=c.px[e*4+3];t.id&&r.masks.set(t.id,d),X(r,t.id,d,i,a),Y(t.effects,!0,e,d);for(let t=0;t<d.length;t++){let n=d[t]*o*(l?l[t]:1);if(n<=0)continue;let r=c.px[t*4+3]||1;J(e,t,c.px[t*4]/r,c.px[t*4+1]/r,c.px[t*4+2]/r,n,s)}Y(t.effects,!1,e,d);return}if(t.shape===`pixels`){an(e,t,n,r,o,s,l);return}if(t.shape===`paint`){Kt(e,t,n,r,o,s,l);return}if(t.shape===`figure`){qt(e,t,n,r,o,s,l);return}if(t.shape===`image`){rn(e,t,n,r,o,s,l);return}if(t.shape===`text`){$t(e,t,n,r,o,s,l);return}if(t.shape===`block`){en(e,t,n,r);return}if(t.shape===`perspective`){tn(e,t,n,o);return}if(t.shape===`reflect`){nn(e,t,n,r,o,l);return}if(t.shape===`bloom`){Yt(e,t,n,o,l);return}if(t.face&&t.shape===`rect`){let i=mt(t,r);if(!i)return;let{face:a,within:o,at:s,size:c,radius:l,...u}=t,d=u.fill&&typeof u.fill!=`string`&&u.fill.type===`grid`&&!u.fill.quad?{...u.fill,quad:i}:u.fill;Z(e,{...u,...d?{fill:d}:{},shape:`polygon`,points:i},n,r);return}let{runs:u,middle:d}=rt(t),f=Qe(n,et(t.transform,d)),p=u.map(e=>({closed:e.closed,points:e.points.map(e=>q(f,e))})),m=Math.sqrt(Math.abs(f[0]*f[3]-f[1]*f[2])),h=t.fill!==void 0&&t.shape!==`line`,g=t.shape===`line`?new Float32Array(i*a):st(p.map(e=>e.points),i,a),_=t.stroke?.align??`center`,v=t.stroke?lt(p,t.stroke.width*m*(_===`outside`?2:1),i,a,t.stroke.join,t.stroke.cap):null;if(v&&_===`inside`)for(let e=0;e<v.length;e++)v[e]*=g[e];if(v&&_===`outside`)for(let e=0;e<v.length;e++)g[e]>=.999&&(v[e]=0);let y=new Float32Array(i*a),b=0;for(let e=0;e<y.length;e++){let t=Math.max(h?g[e]:0,v?v[e]:0)*(l?l[e]:1);y[e]=t,b+=t}if(X(r,t.id,y,i,a),t.id){let e=_===`outside`?null:v;r.masks.set(t.id,t.shape===`line`?y:g.map((t,n)=>t*(e?1-e[n]:1)*(l?l[n]:1)))}if(b===0&&r.notes.push(`layer ${t.id??t.shape} draws nothing inside the picture${l?`, or nothing inside its clip`:``}`),t.erase){for(let t=0;t<y.length;t++){let n=1-y[t]*o;for(let r=0;r<4;r++)e.px[t*4+r]*=n}return}Y(t.effects,!0,e,y);let x=()=>{let n=gt(t.stroke.color,f,r,i);for(let t=0;t<a;t++)for(let r=0;r<i;r++){let a=t*i+r,c=v[a]*(l?l[a]:1);if(c<=0)continue;let u=n(r+.5,t+.5);J(e,a,u[0],u[1],u[2],c*u[3]*o,s)}};if(v&&_===`outside`&&x(),h){let n=gt(t.fill,f,r,i);for(let t=0;t<a;t++)for(let r=0;r<i;r++){let a=t*i+r,c=g[a]*(l?l[a]:1);if(c<=0)continue;let u=n(r+.5,t+.5);J(e,a,u[0],u[1],u[2],c*u[3]*o,s)}}Y(t.effects,!1,e,y),v&&_!==`outside`&&x()}let Q=e=>Math.sqrt(Math.abs(e[0]*e[3]-e[1]*e[2]))||1,$=(e,t)=>{let n=Math.imul(e,374761393)+Math.imul(t,668265263);return n=Math.imul(n^n>>>13,1274126177),n=Math.imul(n^n>>>16,2246822519),((n^n>>>16)>>>0)/4294967295},At=e=>e*e*(3-2*e),jt=null;function Mt(e,t,n){let r=`${e}x${t}:${n}`;if(jt?.key===r)return jt.values;let i=new Float32Array(e*t);for(let r=0;r<t;r++)for(let t=0;t<e;t++)i[r*e+t]=B((t+.5)/n,(r+.5)/n,3,77);return jt={key:r,values:i},i}function Nt(e,t){let n=e.map(e=>{let n=q(t,[e[0],e[1]]);return[n[0],n[1],e[2]??1]}),r=[{x:n[0][0],y:n[0][1],pressure:n[0][2],along:0}];for(let e=0;e+1<n.length;e++){let[t,i,a,o]=[n[Math.max(0,e-1)],n[e],n[e+1],n[Math.min(n.length-1,e+2)]],s=Math.max(1,Math.ceil(Math.hypot(a[0]-i[0],a[1]-i[1])));for(let e=1;e<=s;e++){let n=e/s,c=n*n,l=c*n,u=e=>.5*(2*i[e]+(a[e]-t[e])*n+(2*t[e]-5*i[e]+4*a[e]-o[e])*c+(3*i[e]-t[e]-3*a[e]+o[e])*l),[d,f]=[u(0),u(1)],p=r[r.length-1];r.push({x:d,y:f,pressure:i[2]+(a[2]-i[2])*n,along:p.along+Math.hypot(d-p.x,f-p.y)})}}return r}function Pt(e,t,n,r,i,a){let{w:o,h:s}=e,c=n[n.length-1].along,[l,u]=[F(t.color??`#000000`),F(t.to??t.color??`#000000`)],[d,f]=t.taper??[0,0],[p,m]=t.fade??[0,0],h=t.roundness??1,g=t.angle===void 0?null:[Math.cos(t.angle*Math.PI/180),Math.sin(t.angle*Math.PI/180)],[_,v,y,b,x,S]=[t.grain??0,(t.grainScale??2)*i,t.dry??0,t.wet??0,t.edge??0,t.flow??0],[C,w,T]=[t.scatter??0,t.dab??1,t.spacing],E=null,D=0,O=t.hardness??.5,k=t.size*i,A=t.smudge??0,ee=(t.wobble??0)*i,te=(t,n)=>{let[r,i]=[Math.max(0,Math.min(o-1,Math.floor(t))),Math.max(0,Math.min(s-1,Math.floor(n)))],a=(i*o+r)*4;return[e.px[a],e.px[a+1],e.px[a+2],e.px[a+3]]},j=null,M=0,N=c*r.from,ne=c*r.to;for(;;){for(;M+1<n.length-1&&n[M+1].along<N;)M++;let[i,x]=[n[M],n[Math.min(n.length-1,M+1)]],re=x.along-i.along,ie=re>0?Math.max(0,Math.min(1,(N-i.along)/re)):0,P=c>0?N/c:0,F=1;c>0&&d>0&&P<d&&(F*=At(P/d)),c>0&&f>0&&P>1-f&&(F*=At((1-P)/f));let I=k*(i.pressure+(x.pressure-i.pressure)*ie)*F,ae=r.radius===null?I/2:Math.min(r.radius,Math.max(I,r.radius*.4)),L=ae*w,[oe,R]=re>0?[-(x.y-i.y)/re,(x.x-i.x)/re]:[0,0],se=ee>0?z(N/(k*3+8),r.seed*.37+.5,r.seed)*2*ee:0,ce=r.offset*I+se;D++;let[le,ue]=C>0?[($(r.seed*3+1,D)-.5)*2,($(r.seed*3+2,D)-.5)*2]:[0,0],[B,V]=[i.x+(x.x-i.x)*ie+oe*ce+le*C*ae,i.y+(x.y-i.y)*ie+R*ce+ue*C*ae],de=1;if(c>0&&p>0&&P<p&&(de*=P/p),c>0&&m>0&&P>1-m&&(de*=(1-P)/m),L>=.12){let n=Math.max(1,L*(1-O)),i=Math.min(1,L*2),d=e=>{let t=Math.max(0,Math.min(1,(L+.5-e)/n));return n>1?At(t):t},[f,p]=g??[oe,R],m=(e,t)=>h>=1?Math.hypot(e,t):Math.hypot(e*f+t*p,(e*p-t*f)/h),x=(e,t)=>{if(_<=0&&y<=0)return 1;let n=1;if(_>0&&(n*=Math.max(0,Math.min(1,(Mt(o,s,v)[Math.floor(t)*o+Math.floor(e)]-(.12+_*.62))/.34+.5))),y>0){let i=(e-B)*oe+(t-V)*R+ce,a=z(i/Math.max(.8,k*.045),N/(k*3+8),r.seed+5),o=z(i/Math.max(1.5,k*.17),N/(k*5+12),r.seed+9),s=.5+.8*a+1.1*o;n*=Math.max(0,Math.min(1,(s-(.12+y*.68))/.22+.5))}return n},[C,w]=[Math.max(0,Math.floor(B-L-1)),Math.min(o-1,Math.ceil(B+L+1))],[T,D]=[Math.max(0,Math.floor(V-L-1)),Math.min(s-1,Math.ceil(V+L+1))];if(A>0){j??=te(B,V);for(let n=T;n<=D;n++)for(let r=C;r<=w;r++){let a=d(m(r+.5-B,n+.5-V))*i*A*(t.opacity??1)*x(r+.5,n+.5);if(a<=0)continue;let s=(n*o+r)*4;for(let t=0;t<4;t++)e.px[s+t]=e.px[s+t]+(j[t]-e.px[s+t])*a*.5}let n=te(B,V);for(let e=0;e<4;e++)j[e]=j[e]+(n[e]-j[e])*(1-A)*.5}else{let e=c>0?N/c:0,n=t=>Math.max(0,Math.min(1,(l[t]+(u[t]-l[t])*e)*r.shade)),[s,f,p]=[n(0),n(1),n(2)];if(b>0){let e=te(B,V),t=e[3]>.02?[e[0]/e[3],e[1]/e[3],e[2]/e[3]]:null,n=t?E?b*.09:b*.45:0,r=E??[s,f,p],i=[s,f,p];E=[0,1,2].map(e=>{let a=r[e]+(i[e]-r[e])*.012;return a+((t?t[e]:a)-a)*n}),[s,f,p]=E}let h=(l[3]+(u[3]-l[3])*e)*(t.opacity??1)*r.strength*de;for(let e=T;e<=D;e++)for(let t=C;t<=w;t++){let n=d(m(t+.5-B,e+.5-V))*i*h*x(t+.5,e+.5),r=e*o+t,c=a.mask[r],l=S>0?c+n*S*(1-c):n;l<=c||(c===0&&a.touched.push(r),a.mask[r]=l,a.rgb[r*3]=s,a.rgb[r*3+1]=f,a.rgb[r*3+2]=p)}}}if(N>=ne||c===0)break;N=Math.min(ne,N+(T===void 0?Math.max(.4,L*.1*h):Math.max(.3,L*2*T)))}let re=t.blend??`normal`;for(let n of a.touched){let r=a.mask[n],i=x>0?Math.min(1,r*(1-x*.45*At(Math.min(1,r/(t.opacity??1)))+x*1.1*Math.max(0,1-Math.abs(r/(t.opacity??1)-.4)*3))):r;if(t.erase)for(let t=0;t<4;t++)e.px[n*4+t]*=1-i;else J(e,n,a.rgb[n*3],a.rgb[n*3+1],a.rgb[n*3+2],i,re);a.mask[n]=0}a.touched.length=0}function Ft(e,t,n,r,i,a){if(!L()){It(e,t,n,r,i,a);return}let o=t.points.map(e=>q(n,[e[0],e[1]])),s=(t.size??24)*r*2+4;for(let[c,l]of it){let[u,d]=[c*e.w,l*e.h];(c!==0||l!==0)&&!at(o,u,d,s,e.w,e.h)||It(e,t,Qe([1,0,0,1,u,d],n),r,i,a)}}function It(e,t,n,r,i,a){let o=t.brush?{...je[t.brush],...t}:t,s=Nt(o.points,n),c=o.seed??i+1,l=o.bristles??1;if(l===1){Pt(e,o,s,{offset:0,radius:null,from:0,to:1,shade:1,strength:1,seed:c},r,a);return}let u=o.spread??1,d=o.vary??.15,f=(o.strand??Math.max(.8,o.size/l*2.2))*r/2;for(let t=0;t<l;t++){let n=e=>$(c*131+t,e);Pt(e,o,s,{offset:((t+n(1))/l-.5)*u,radius:f,from:n(2)*.06,to:1-n(3)*.1,shade:1+(n(4)-.5)*2*d,strength:1-n(5)*d*1.5,seed:c*31+t},r,a)}}let Lt=e=>Math.max(0,Math.min(255,Math.round(e*255))).toString(16).padStart(2,`0`),Rt=(e,t=1)=>`#${Lt(e[0]*t)}${Lt(e[1]*t)}${Lt(e[2]*t)}${Lt(e[3])}`;function zt(e,t){let n=Nt(e,Ye),r=n[n.length-1].along,i=0;return Array.from({length:t},(e,a)=>{let o=r*a/(t-1);for(;i+1<n.length-1&&n[i+1].along<o;)i++;let[s,c]=[n[i],n[i+1]??n[i]],l=c.along>s.along?Math.min(1,(o-s.along)/(c.along-s.along)):0;return{x:s.x+(c.x-s.x)*l,y:s.y+(c.y-s.y)*l,pressure:s.pressure+(c.pressure-s.pressure)*l}})}let Bt=e=>{let t={};for(let n of[`brush`,`roundness`,`angle`,`grain`,`grainScale`,`dry`,`wet`,`edge`,`scatter`,`dab`,`spacing`,`flow`,`fade`])e[n]!==void 0&&(t[n]=e[n]);return t};function Vt(e){let t=e.guides.map(e=>zt(e,14)),n=e.seed??1,r=e.colors.map(F),i=e.tip?F(e.tip):null,a=e.clumps??Math.max(2,Math.round(e.count/14)),[o,s,c,l]=[e.gather??.6,e.flyaway??.02,e.vary??.12,e.wobble??1.5],u=e=>{let t=Math.max(0,Math.min(.9999,e))*(r.length-1),[n,i]=[Math.floor(t),t-Math.floor(t)],[a,o]=[r[n],r[Math.min(r.length-1,n+1)]];return[a[0]+(o[0]-a[0])*i,a[1]+(o[1]-a[1])*i,a[2]+(o[2]-a[2])*i,a[3]+(o[3]-a[3])*i]},d=Array.from({length:a},(e,t)=>(t+.2+$(n*7+t,1)*.6)/a),f=Array.from({length:a},(e,t)=>1+($(n*7+t,2)-.5)*c*2.5),p=[];for(let r=0;r<e.count;r++){let m=e=>$(n*131+r,e),h=(r+m(1))/e.count,g=Math.min(a-1,Math.floor(h*a)),_=m(2)<s,[v,y]=[m(3)<.5?-1:1,.6+m(4)*1.4],b=1-m(5)*(_?.05:.16),x=[];for(let i=0;i<14;i++){let a=i/13*b,s=_?0:o*a*a,c=(h+(d[g]-h)*s)*(t.length-1),[u,f]=[Math.min(t.length-2,Math.floor(c)),c-Math.min(t.length-2,Math.floor(c))],p=e=>{let t=a*13,[n,r]=[Math.min(12,Math.floor(t)),t-Math.min(12,Math.floor(t))];return[e[n].x+(e[n+1].x-e[n].x)*r,e[n].y+(e[n+1].y-e[n].y)*r]},[m,S]=p(t[u]),[C,w]=p(t[u+1]),[T,E]=[m+(C-m)*f,S+(w-S)*f],[D,O]=[C-m,w-S],k=Math.hypot(D,O)||1,A=z(a*3.1,r*.73+.5,n)*l*2*a,ee=_?v*y*a*a*(k*.5+(e.size??1.2)*14):0;x.push([Math.round((T+D/k*(A+ee))*100)/100,Math.round((E+O/k*(A+ee))*100)/100])}let S=f[g]*(1+(m(6)-.5)*c),C=u(h+(m(7)-.5)*.08),w=(e.size??1.2)*(.7+m(8)*.6);p.push({...Bt(e),points:x,size:w,...e.brush&&e.hardness===void 0?{}:{hardness:e.hardness??.7},color:Rt(C,S),...i?{to:Rt([C[0]+(i[0]-C[0])*.8,C[1]+(i[1]-C[1])*.8,C[2]+(i[2]-C[2])*.8,C[3]],S)}:{},opacity:(e.opacity??(e.brush?je[e.brush].opacity??1:1))*(_?.55:.75+m(9)*.25),taper:e.taper??[.04,.45]})}return p}function Ht(e,t,n,r,i,a){let o=e.seed??1,s=e.region?.startsWith(`@`)?e.region.slice(1):null,c=s?i.masks.get(s):void 0;if(s&&!c)return i.notes.push(`${a}: a field's region \"@${s}\" names no layer drawn before it, so it drew nothing`),[];let l=c?kt(t,n,e=>c[e]>=.5):null;if(c&&!l)return[];let u=e.region&&!s?Te(e.region).map(e=>e.points):null,d=(e,i)=>{if(c){let a=q(r,[e,i]),[o,s]=[Math.floor(a[0]),Math.floor(a[1])];return o>=0&&s>=0&&o<t&&s<n&&c[s*t+o]>=.5}if(!u)return!0;let a=0;for(let t of u)for(let n=0;n<t.length;n++){let[r,o]=[t[n],t[(n+1)%t.length]];if(r[1]<=i==o[1]<=i)continue;let s=(o[0]-r[0])*(i-r[1])-(e-r[0])*(o[1]-r[1]);(o[1]>r[1]?s>0:s<0)&&(a+=o[1]>r[1]?1:-1)}return a!==0},f=$e(r),p=l?[[l[0],l[1]],[l[0]+l[2],l[1]],[l[0],l[1]+l[3]],[l[0]+l[2],l[1]+l[3]]].map(e=>q(f,e)):u?u.flat():[[0,0],[t,n]],[m,h]=[Math.min(...p.map(e=>e[0])),Math.max(...p.map(e=>e[0]))],[g,_]=[Math.min(...p.map(e=>e[1])),Math.max(...p.map(e=>e[1]))],v=(e.guides??[]).flatMap(e=>{let t=zt(e,24);return t.slice(0,-1).map((e,n)=>{let r=t[n+1],i=Math.hypot(r.x-e.x,r.y-e.y)||1;return{x:e.x,y:e.y,dx:(r.x-e.x)/i,dy:(r.y-e.y)/i}})}),y=(t,n)=>{if(v.length===0){let t=(e.angle??0)*Math.PI/180;return[Math.cos(t),Math.sin(t)]}let[r,i]=[0,0];for(let e of v){let a=1/(1+((t-e.x)**2+(n-e.y)**2)**1.5);r+=e.dx*a,i+=e.dy*a}let a=Math.hypot(r,i)||1;return[r/a,i/a]},b=e.tone!==void 0&&typeof e.tone!=`string`&&(e.tone.type===`form`||e.tone.type===`image`)?gt(e.tone,Ye,i,t):null,x=b?(e,t)=>{let n=q(r,[e,t]);return b(n[0],n[1])}:e.tone===void 0?null:gt(e.tone,Ye),S=e.length??e.size*3,C=Math.min(Ue,Math.ceil((e.coverage??4)*Math.max(1,(h-m)*(_-g))/(e.size*S))),w=(e.spread??12)*Math.PI/180,T=e.vary??.05,E=[],D=Math.max(1,Math.round(Math.sqrt(C*(h-m||1)/(_-g||1)))),O=Math.max(1,Math.ceil(C/D)),k=Array.from({length:D*O},(e,t)=>t).sort((e,t)=>$(o*53+e,9)-$(o*53+t,9));for(let t of k){let n=e=>$(o*197+t,e),[r,i]=[m+(t%D+n(1))/D*(h-m),g+(Math.floor(t/D)+n(2))/O*(_-g)];if(!d(r,i))continue;let a=(n(3)-.5)*2*w,s=[],[c,l]=[-S/2,S/5],[u,f]=[r,i],[p,v]=y(r,i);u+=p*c,f+=v*c;for(let e=0;e<=5;e++){s.push([Math.round(u*100)/100,Math.round(f*100)/100]);let[e,t]=y(u,f),[n,r]=[e*Math.cos(a)-t*Math.sin(a),e*Math.sin(a)+t*Math.cos(a)];u+=n*l,f+=r*l}E.push({...Bt(e),points:s,size:e.size*(.75+n(4)*.5),...e.brush?{}:{hardness:e.hardness??.25,opacity:e.opacity??.6},...e.brush&&e.hardness!==void 0?{hardness:e.hardness}:{},...e.brush&&e.opacity!==void 0?{opacity:e.opacity}:{},...e.smudge?{smudge:e.smudge}:{color:Rt(x(r,i),1+(n(5)-.5)*2*T)},taper:e.taper??[.12,.3]})}return E}function Ut(e){let t=e.seed??1,n=zt(e.line,Math.max(2,e.count)),[r,i]=typeof e.scale==`number`?[e.scale,e.scale]:e.scale??[1,1],a=e.jitter??0,o=[];for(let s=0;s<e.count;s++){let c=e.count===1?0:s/(e.count-1),l=n[s],[u,d]=s+1<n.length?[l,n[s+1]]:[n[s-1],l],f=e=>($(t*89+s,e)-.5)*2*a,p=r+(i-r)*c,m=Math.atan2(d.y-u.y,d.x-u.x);for(let t of e.both?[1,-1]:[1]){let n=m+((e.turn??0)*t+f(1)*.1)*Math.PI/180,[r,i]=[Math.cos(n),Math.sin(n)],[a,s]=[l.x+f(2),l.y+f(3)];for(let n of e.strokes)o.push({...n,size:n.size*p,points:n.points.map(e=>{let[n,o]=[e[0]*p,e[1]*p*t];return[a+n*r-o*i,s+n*i+o*r,...e[2]===void 0?[]:[e[2]]]})})}}return o}function Wt(e,t,n){let r=e.size??1.2,i=e.guides.map(e=>zt(e,3)[1]),a=0;for(let e=0;e+1<i.length;e++)a+=Math.hypot(i[e+1].x-i[e].x,i[e+1].y-i[e].y);(a<r*4||e.count*r>a*8*Math.max(1,e.guides.length))&&t.notes.push(`${n}: a sweep lays ${e.count} hairs of size ${r} across ${Math.round(a)} pixels, which fills it as a flat band; use fewer, thinner hairs or set the guides further apart`)}let Gt=(e,t,n,r,i,a)=>`type`in e?e.type===`sweep`?(Wt(e,i,a),Vt(e)):e.type===`along`?Ut(e):Ht(e,t,n,r,i,a):[e];function Kt(e,t,n,r,i,a,o){let{w:s,h:c}=e,l=Qe(n,et(t.transform,[s/2,c/2])),u=new _t(s,c);if(t.from===`below`&&u.px.set(e.px),t.fill!==void 0){let e=gt(t.fill,l,r,s);for(let t=0;t<c;t++)for(let n=0;n<s;n++){let r=e(n+.5,t+.5);J(u,t*s+n,r[0],r[1],r[2],r[3],`normal`)}}let d={mask:new Float32Array(s*c),rgb:new Float32Array(s*c*3),touched:[]},f=0,p=t.face?mt(t,r):null;if(t.face&&!p)return;let m=p?pt(p):null,h=e=>e.map(e=>[...m(e[0],e[1]),...e.slice(2)]);(t.strokes??[]).map(e=>m?`type`in e?e.type===`sweep`?{...e,guides:e.guides.map(h)}:e.type===`along`?{...e,line:h(e.line)}:e:{...e,points:h(e.points)}:e).forEach(e=>{for(let n of Gt(e,s,c,l,r,`layer ${t.id??`paint`}`))Ft(u,n,l,Q(l),f++,d)}),t.id&&r.painted.set(t.id,{entries:(t.strokes??[]).length,strokes:f});let g=new Float32Array(s*c);for(let e=0;e<g.length;e++)g[e]=u.px[e*4+3];let _=o?g.map((e,t)=>e*o[t]):g;if(t.id&&r.masks.set(t.id,_),X(r,t.id,_,s,c),t.from===`below`){for(let t=0;t<g.length*4;t++){let n=i*(o?o[Math.floor(t/4)]:1);e.px[t]=e.px[t]+(u.px[t]-e.px[t])*n}return}Y(t.effects,!0,e,g);for(let t=0;t<g.length;t++){let n=g[t];n<=0||J(e,t,u.px[t*4]/n,u.px[t*4+1]/n,u.px[t*4+2]/n,n*i*(o?o[t]:1),a)}Y(t.effects,!1,e,g)}function qt(e,t,n,r,i,a,o){let{w:s,h:c}=e,l=P(t,s,c,e=>q(n,e),Q(n)),u=Math.max(2,t.height*.03),d=R(l.mask,s,c,u),f=new Float32Array(l.normals);for(let e=0;e<3;e++){let t=new Float32Array(s*c);for(let n=0;n<t.length;n++)t[n]=l.normals[n*3+e]*l.mask[n];let n=R(t,s,c,u);for(let r=0;r<t.length;r++)l.mask[r]<.5&&d[r]>.01&&(f[r*3+e]=n[r]/d[r])}if(t.id){r.figures.set(t.id,{...l,normals:f});let e=e=>o?e.map((e,t)=>e*o[t]):e;r.masks.set(t.id,e(l.mask)),X(r,t.id,e(l.mask),s,c);for(let[n,i]of Object.entries(l.parts))r.masks.set(`${t.id}.${n}`,e(i)),X(r,`${t.id}.${n}`,e(i),s,c)}let[p,m,h]=l.light;for(let t=0;t<l.mask.length;t++){let n=l.mask[t];if(n<=0)continue;let r=.25+.7*Math.max(0,.5+.5*(l.normals[t*3]*p+l.normals[t*3+1]*m+l.normals[t*3+2]*h)),s=1-l.contour[t]*.85;J(e,t,r*s*.92,r*s*.94,r*s,n*i*(o?o[t]:1),a)}let g=lt(l.lines.map(e=>({closed:!1,points:e})),1.5,s,c);for(let t=0;t<g.length;t++)J(e,t,.95,.35,.3,g[t]*.8*i,`normal`);for(let[t,n]of Object.values(l.joints))for(let r=-2;r<=2;r++)for(let a=-2;a<=2;a++)t+a<0||n+r<0||t+a>=s||n+r>=c||a*a+r*r>5||J(e,(n+r)*s+t+a,.95,.2,.25,.85*i,`normal`);t.id&&r.figureJoints.set(t.id,l.joints)}function Jt(e,t,n,r,i){let a=[...e.text].length,o=96/(e.fontSize??96),s=Math.ceil(96*1.4*(a+1)+Math.abs(e.tracking??0)*o*a),[c,l]=e.vertical?[192,s]:[s,192],u=i({...e,fontSize:96,align:`left`,at:e.vertical?[192/2,96*.2]:[96*.2,96*1.3],...e.tracking?{tracking:e.tracking*o}:{},...e.stroke?{stroke:{...e.stroke,width:e.stroke.width*o}}:{}},Ye,c,l),d=kt(c,l,e=>u.fill[e]>=.5||(u.line?u.line[e]>=.5:!1)),f={fill:new Float32Array(n*r),line:u.line?new Float32Array(n*r):null},p=e.quad.map(e=>q(t,e)),m=ft(p);if(!d||!m)return f;let[h,g]=[Math.max(0,Math.floor(Math.min(...p.map(e=>e[0])))),Math.min(n-1,Math.ceil(Math.max(...p.map(e=>e[0]))))],[_,v]=[Math.max(0,Math.floor(Math.min(...p.map(e=>e[1])))),Math.min(r-1,Math.ceil(Math.max(...p.map(e=>e[1]))))];for(let e=_;e<=v;e++)for(let t=h;t<=g;t++){let[r,i]=[0,0];for(let n=0;n<3;n++)for(let a=0;a<3;a++){let[o,s]=m(t+(a+.5)/3,e+(n+.5)/3);if(o<0||s<0||o>=1||s>=1)continue;let l=Math.floor(d[1]+s*d[3])*c+Math.floor(d[0]+o*d[2]);r+=u.fill[l],u.line&&(i+=u.line[l])}f.fill[e*n+t]=r/9,f.line&&(f.line[e*n+t]=i/9)}return f}function Yt(e,t,n,r,i){let{w:a,h:o}=e,s=t.threshold??.6,c=(t.strength??1)*r,l=(t.radius??10)*Q(n),u=[new Float32Array(a*o),new Float32Array(a*o),new Float32Array(a*o)],d=Math.max(.001,1-s);for(let t=0;t<a*o;t++){let[n,r,i]=[e.px[t*4],e.px[t*4+1],e.px[t*4+2]],a=Math.max(n,r,i);if(a<=s)continue;let o=At(Math.min(1,(a-s)/d));u[0][t]=n*o,u[1][t]=r*o,u[2][t]=i*o}for(let t=0;t<3;t++){let[n,r]=[R(u[t],a,o,l*.5),R(u[t],a,o,l*2.5)];for(let s=0;s<a*o;s++){let a=Math.min(1,(n[s]*.7+r[s]*.9)*c)*(i?i[s]:1);if(a<=0)continue;let o=s*4;e.px[o+t]=e.px[o+t]+(1-e.px[o+t])*a,a>e.px[o+3]&&(e.px[o+3]=a)}}}function Xt(e,t,n,r){let i=Math.round(t);if(i<=0)return e;let a=new Float32Array(n*r);for(let t=0;t<r;t++)for(let r=0;r<n;r++){let o=1;for(let a=-i;a<=i;a++){let i=r+a,s=i<0||i>=n?0:e[t*n+i];s<o&&(o=s)}a[t*n+r]=o}let o=new Float32Array(n*r);for(let e=0;e<n;e++)for(let t=0;t<r;t++){let s=1;for(let o=-i;o<=i;o++){let i=t+o,c=i<0||i>=r?0:a[i*n+e];c<s&&(s=c)}o[t*n+e]=s}return o}function Zt(e,t,n){let[r,i]=[t.grain??0,t.dry??0];if(r<=0&&i<=0)return e;let a=t.grainScale??2;return e.map((e,t)=>{if(e<=0)return 0;let[o,s]=[t%n+.5,Math.floor(t/n)+.5],c=e;return r>0&&(c*=Math.max(0,Math.min(1,(B(o/a,s/a,3,77)-(.12+r*.62))/.34+.5))),i>0&&(c*=Math.max(0,Math.min(1,(.5+1.3*z(o/40,s/1.8,9)-(.12+i*.68))/.22+.5))),c})}function Qt(e,t,n,r){let i=Math.round(Math.abs(t));if(i===0)return e;let a=t>0?Math.max:Math.min,o=new Float32Array(n*r);for(let s=0;s<r;s++)for(let r=0;r<n;r++){let c=e[s*n+r];for(let o=-i;o<=i;o++){let i=r+o;c=a(c,i<0||i>=n?t>0?0:c:e[s*n+i])}o[s*n+r]=c}let s=new Float32Array(n*r);for(let e=0;e<n;e++)for(let c=0;c<r;c++){let l=o[c*n+e];for(let s=-i;s<=i;s++){let i=c+s;l=a(l,i<0||i>=r?t>0?0:l:o[i*n+e])}s[c*n+e]=l}return s}function $t(e,t,n,r,i,a,o){let{w:s,h:c}=e;if(!r.text)throw Error(`layer ${t.id??`text`}: text needs the editor's fonts, and this render has none`);let l=Qe(n,et(t.transform,t.at??[0,0])),u=t.face?mt(t,r):t.quad;if(t.face&&!u)return;let d=u?Jt({...t,quad:u},l,s,c,r.text):r.text(t,l,s,c),f=d.line,p=Zt(Xt(d.fill,(t.thin??0)*Q(l),s,c),t,s),m=new Float32Array(s*c);for(let e=0;e<m.length;e++)m[e]=Math.max(t.fill===void 0?0:p[e],f?f[e]:0)*(o?o[e]:1);t.id&&r.masks.set(t.id,m),X(r,t.id,m,s,c),Y(t.effects,!0,e,m);let h=(t,n)=>{for(let r=0;r<c;r++)for(let c=0;c<s;c++){let l=r*s+c,u=t[l]*(o?o[l]:1);if(u<=0)continue;let d=n(c+.5,r+.5);J(e,l,d[0],d[1],d[2],u*d[3]*i,a)}};f&&t.stroke&&h(f,gt(t.stroke.color,l,r,s)),t.fill!==void 0&&h(p,gt(t.fill,l,r,s)),Y(t.effects,!1,e,m)}function en(e,t,n,r){let[i,a]=t.at,o=a-t.height,s=(e,t,n,r)=>[e+(n[0]-e)*r,t+(n[1]-t)*r],c={},l=[`a`,`b`],u={...t.opacity===void 0?{}:{opacity:t.opacity},...t.blend?{blend:t.blend}:{},...t.clip?{clip:t.clip}:{},...t.except?{except:t.except}:{},...t.stroke?{stroke:t.stroke}:{}},d=(t.vanish??[]).map((e,n)=>{let r=t.depth[n],[d,f]=s(i,o,e,r),[p,m]=s(i,a,e,r),h=e[0]<i?[[d,f],[i,o],[i,a],[p,m]]:[[i,o],[d,f],[p,m],[i,a]];c[l[n]]=h;let g=t.faces[n],_=typeof g!=`string`&&g.type===`grid`&&!g.quad?{...g,quad:h}:g;return{...u,...t.id?{id:`${t.id}.${l[n]}`}:{},shape:`polygon`,points:h,fill:_}}),f=t.vanish??[];if(t.roof!==void 0&&f[0][1]<o){let e=[i,o],[n,r]=[s(i,o,f[0],t.depth[0]),f[1]?s(i,o,f[1],t.depth[1]):null],a=r&&f[1]?[e,n,(()=>{let[e,t,i,a]=[n,f[1],r,f[0]],o=(e[0]-t[0])*(i[1]-a[1])-(e[1]-t[1])*(i[0]-a[0]);if(Math.abs(o)<1e-6)return e;let s=((e[0]-i[0])*(i[1]-a[1])-(e[1]-i[1])*(i[0]-a[0]))/o;return[e[0]+s*(t[0]-e[0]),e[1]+s*(t[1]-e[1])]})(),r]:null;a&&(c.top=a,d.unshift({...u,...t.id?{id:`${t.id}.top`}:{},shape:`polygon`,points:a,fill:t.roof}))}for(let t of d)Z(e,t,n,r);if(t.id){r.blocks.set(t.id,Object.fromEntries(Object.entries(c).map(([e,t])=>[e,t.map(e=>{let t=q(n,e);return[Math.round(t[0]),Math.round(t[1])]})])));let i=new Float32Array(e.w*e.h);for(let e of Object.keys(c)){let n=r.masks.get(`${t.id}.${e}`);if(n)for(let e=0;e<i.length;e++)i[e]=Math.max(i[e],n[e])}r.masks.set(t.id,i),X(r,t.id,i,e.w,e.h)}}function tn(e,t,n,r){let{w:i,h:a}=e,o=Math.hypot(i,a),s=(t.vanish??[]).flatMap(e=>Array.from({length:24},(t,r)=>{let i=r/24*Math.PI*2,a=q(n,[e[0],e[1]]);return{closed:!1,points:[a,[a[0]+Math.cos(i)*o,a[1]+Math.sin(i)*o]]}})),c=(t.vanish??[])[0];c&&s.push({closed:!1,points:[q(n,[-i,c[1]]),q(n,[i*2,c[1]])]});let l=lt(s,1,i,a);for(let t=0;t<l.length;t++)J(e,t,.3,.9,1,l[t]*.55*r,`normal`)}function nn(e,t,n,r,i,a){let{w:o,h:s}=e,c=Q(n),l=new Float32Array(o),u=t.edge?.map(e=>q(n,e)).sort((e,t)=>e[0]-t[0]);for(let e=0;e<o;e++){if(!u){l[e]=q(n,[0,t.y])[1];continue}let r=0;for(;r+2<u.length&&u[r+1][0]<e+.5;)r++;let[i,a]=[u[r],u[r+1]],o=a[0]===i[0]?0:Math.max(0,Math.min(1,(e+.5-i[0])/(a[0]-i[0])));l[e]=i[1]+(a[1]-i[1])*o}let d=(t.reach??s)*c,f=(t.ripple??3)*c,p=new Float32Array(o*s*4);for(let t=0;t<s;t++)for(let n=0;n<o;n++){let r=t+.5-l[n];if(r<0||r>=d)continue;let i=Math.floor(l[n]-r);if(i<0)continue;let a=f*(r/d+.2),s=z(n/90,t/3.5,11)*a*2.4+z(n/22,t/1.6,12)*a,c=Math.max(0,Math.min(o-1,Math.round(n+s))),[u,m]=[(t*o+n)*4,(i*o+c)*4];for(let t=0;t<4;t++)p[u+t]=e.px[m+t]}let m=(t.smear??2)*c,h=[0,1,2,3].map(e=>{let t=new Float32Array(o*s);for(let n=0;n<t.length;n++)t[n]=p[n*4+e];return m>0?R(t,o,s,m):t}),g=new Float32Array(o*s);for(let n=0;n<s;n++)for(let r=0;r<o;r++){let s=n+.5-l[r];if(s<0)continue;let c=Math.max(0,1-s/d),u=n*o+r,f=h[3][u];g[u]=f*c*(a?a[u]:1),!(f<=0||g[u]<=0)&&J(e,u,h[0][u]/f,h[1][u]/f,h[2][u]/f,g[u]*i,t.blend??`normal`)}t.id&&r.masks.set(t.id,g),X(r,t.id,g,o,s)}function rn(e,t,n,r,i,a,o){let{w:s,h:c}=e,l=r.pictures[t.src];if(!l)throw Error(`there is no picture at ${t.src}`);let[u,d]=q(n,t.at??[0,0]),f=Q(n),[p,m]=(t.size??[l.width,l.height]).map(e=>e*f),h=new Float32Array(s*c*4),g=new Float32Array(s*c);for(let e=Math.max(0,Math.floor(d));e<Math.min(c,Math.ceil(d+m));e++)for(let t=Math.max(0,Math.floor(u));t<Math.min(s,Math.ceil(u+p));t++){let[n,r]=[(t+.5-u)/p*l.width-.5,(e+.5-d)/m*l.height-.5];if(n<-.5||r<-.5||n>l.width-.5||r>l.height-.5)continue;let[i,a]=[Math.max(0,Math.floor(n)),Math.max(0,Math.floor(r))],[o,c]=[Math.min(l.width-1,i+1),Math.min(l.height-1,a+1)],[f,_]=[Math.max(0,n-i),Math.max(0,r-a)],v=e*s+t;for(let e=0;e<4;e++){let t=(t,n)=>l.rgba[(n*l.width+t)*4+e]/255;h[v*4+e]=(t(i,a)*(1-f)+t(o,a)*f)*(1-_)+(t(i,c)*(1-f)+t(o,c)*f)*_}g[v]=h[v*4+3]}if(t.id){r.images.set(t.id,h);let e=o?g.map((e,t)=>e*o[t]):g;r.masks.set(t.id,e),X(r,t.id,e,s,c)}for(let t=0;t<g.length;t++)g[t]>0&&J(e,t,h[t*4],h[t*4+1],h[t*4+2],g[t]*i*(o?o[t]:1),a)}function an(e,t,n,r,i,a,o){let{w:s,h:c}=e,l=t.rows,u=Math.max(...l.map(e=>e.length)),d=(t.scale??Math.max(1,Math.floor(Math.min(s/u,c/l.length))))*Q(n),[f,p]=t.at?q(n,t.at):[(s-u*d)/2,(c-l.length*d)/2],m=new Map(Object.entries(t.palette??{}).map(([e,t])=>[e,F(t)])),h=new Float32Array(s*c),g=new Float32Array(s*c*3);l.forEach((e,t)=>{[...e].forEach((e,n)=>{let r=m.get(e);if(r)for(let e=Math.max(0,Math.round(p+t*d));e<Math.min(c,Math.round(p+(t+1)*d));e++)for(let t=Math.max(0,Math.round(f+n*d));t<Math.min(s,Math.round(f+(n+1)*d));t++)h[e*s+t]=r[3]*(o?o[e*s+t]:1),g[(e*s+t)*3]=r[0],g[(e*s+t)*3+1]=r[1],g[(e*s+t)*3+2]=r[2]})}),t.id&&r.masks.set(t.id,h),X(r,t.id,h,s,c),Y(t.effects,!0,e,h);for(let t=0;t<h.length;t++)J(e,t,g[t*3],g[t*3+1],g[t*3+2],h[t]*i,a);Y(t.effects,!1,e,h)}let on=new Map,sn={drawn:0,reused:0};function cn(e){let[t,n]=[3735928559,1103547991];for(let r=0;r<e.length;r++){let i=e.charCodeAt(r);t=Math.imul(t^i,2654435761),n=Math.imul(n^i,1597334677)}return((t^t>>>16)>>>0).toString(36)+((n^n>>>13)>>>0).toString(36)+e.length.toString(36)}function ln(e){return e?Object.entries(e).map(([e,t])=>{let n=0;for(let e=0;e<t.rgba.length;e+=97)n=Math.imul(n,31)+t.rgba[e]|0;return`${e}:${t.width}x${t.height}:${n}`}).join(`|`):``}function un(e,t,n){on.set(e,{px:t.px.slice(),drawn:{masks:new Map(n.masks),notes:[...n.notes],bounds:new Map(n.bounds),painted:new Map(n.painted),figures:new Map(n.figures),figureJoints:new Map(n.figureJoints),images:new Map(n.images),blocks:new Map(n.blocks)}});let r=0;for(let e of on.values())r+=e.px.byteLength;for(let[e,t]of on){if(r<=536870912)break;on.delete(e),r-=t.px.byteLength}}function dn(e,t,n){t.px.set(e.px),n.masks=new Map(e.drawn.masks),n.notes=[...e.drawn.notes],n.bounds=new Map(e.drawn.bounds),n.painted=new Map(e.drawn.painted),n.figures=new Map(e.drawn.figures),n.figureJoints=new Map(e.drawn.figureJoints),n.images=new Map(e.drawn.images),n.blocks=new Map(e.drawn.blocks)}function fn(e,t={}){let[n,r]=typeof e.size==`number`?[e.size,e.size]:e.size,i=new _t(n,r),a={masks:new Map,notes:[],bounds:new Map,painted:new Map,figures:new Map,figureJoints:new Map,showGuides:t.guides===!0,pictures:t.pictures??{},images:new Map,text:t.text,blocks:new Map},o=cn(JSON.stringify([n,r,e.background??null,t.guides===!0,ln(t.pictures),e.tile===!0])),s=e.layers.map(e=>o=cn(o+JSON.stringify(e))),c=0;for(let e=s.length-1;e>=0;e--){let t=on.get(s[e]);if(t){dn(t,i,a),on.delete(s[e]),on.set(s[e],t),c=e+1,sn.reused+=c;break}}if(c===0&&e.background){let[t,a,o,s]=F(e.background);for(let e=0;e<n*r;e++)J(i,e,t,a,o,s,`normal`)}let l=Date.now();ae(e.tile===!0),[Xe,Ze]=[n,r];try{for(let t=c;t<e.layers.length;t++)Z(i,e.layers[t],Ye,a),sn.drawn++,(t===e.layers.length-1||Date.now()-l>=150)&&(un(s[t],i,a),l=Date.now())}finally{ae(!1)}let u=new Uint8Array(n*r*4);for(let e=0;e<n*r;e++){let t=i.px[e*4+3],n=t>1e-5?1/t:0;u[e*4]=Math.round(Math.min(1,i.px[e*4]*n)*255),u[e*4+1]=Math.round(Math.min(1,i.px[e*4+1]*n)*255),u[e*4+2]=Math.round(Math.min(1,i.px[e*4+2]*n)*255),u[e*4+3]=Math.round(Math.min(1,t)*255)}let d=0;for(let e=0;e<n*r;e++)u[e*4+3]>=128&&d++;return{width:n,height:r,rgba:u,notes:a.notes,bounds:kt(n,r,e=>u[e*4+3]>=128),covers:Math.round(d/(n*r)*1e3)/1e3,layerBounds:Object.fromEntries(a.bounds),painted:Object.fromEntries(a.painted),figures:Object.fromEntries(a.figureJoints),blocks:Object.fromEntries(a.blocks)}}let pn=(e,t)=>(e[t*4]*.2126+e[t*4+1]*.7152+e[t*4+2]*.0722)/255;function mn(e,t,n){let r=fn({size:e.size,background:`#000000`,...e.tile?{tile:!0}:{},layers:t},n),i=new Float32Array(r.width*r.height);for(let e=0;e<i.length;e++)i[e]=pn(r.rgba,e);return{values:i,picture:r}}function hn(e,t,n,r,i){let a=(r,a)=>{let[o,s]=i?[(r%t+t)%t,(a%n+n)%n]:[Math.max(0,Math.min(t-1,r)),Math.max(0,Math.min(n-1,a))];return e[s*t+o]},o=new Uint8Array(t*n*4);for(let e=0;e<n;e++)for(let n=0;n<t;n++){let i=(a(n+1,e-1)+2*a(n+1,e)+a(n+1,e+1)-(a(n-1,e-1)+2*a(n-1,e)+a(n-1,e+1)))/8,s=(a(n-1,e-1)+2*a(n,e-1)+a(n+1,e-1)-(a(n-1,e+1)+2*a(n,e+1)+a(n+1,e+1)))/8,[c,l]=[-i*r,-s*r],u=Math.hypot(c,l,1),d=(e*t+n)*4;o[d]=Math.round((c/u*.5+.5)*255),o[d+1]=Math.round((l/u*.5+.5)*255),o[d+2]=Math.round((1/u*.5+.5)*255),o[d+3]=255}return{width:t,height:n,rgba:o}}function gn(e,t,n={}){let r=e.material??{},[i,a]=[t.width,t.height],o=[],s,c;if(r.height){let t=mn(e,r.height,n);[s,c]=[t.values,t.picture],o.push(...t.picture.notes.map(e=>`height: ${e}`))}else{s=new Float32Array(i*a);let e=new Uint8Array(i*a*4);for(let n=0;n<i*a;n++){s[n]=pn(t.rgba,n);let r=Math.round(s[n]*255);e.set([r,r,r,255],n*4)}c={width:i,height:a,rgba:e}}let[l,u]=[1,0];for(let e of s)e<l&&(l=e),e>u&&(u=e);u-l<1/255&&o.push(`the relief is flat: its height is one value everywhere, so the normal map says nothing`);let d=(e,t)=>typeof e==`number`?e:t,f=(t,r)=>{if(!Array.isArray(t))return null;let i=mn(e,t,n);return o.push(...i.picture.notes.map(e=>`${r}: ${e}`)),i.values},[p,m]=[f(r.roughness,`roughness`),f(r.metal,`metal`)],h=null;if(p||m){let e=new Uint8Array(i*a*4),[t,n]=[Math.round(d(r.roughness,.8)*255),Math.round(d(r.metal,0)*255)];for(let r=0;r<i*a;r++)e[r*4]=255,e[r*4+1]=p?Math.round(p[r]*255):t,e[r*4+2]=m?Math.round(m[r]*255):n,e[r*4+3]=255;h={width:i,height:a,rgba:e}}return{normal:hn(s,i,a,r.relief??4,e.tile===!0),surface:h,roughness:d(r.roughness,.8),metal:d(r.metal,0),height:c,notes:o}}function _n(e,t){return(n,r,i,a)=>{let o=e(i,a).getContext(`2d`,{willReadFrequently:!0});if(!o)throw Error(`could not draw text here`);let s=n.fontSize,c=n.font?`\"${t(n.font)??n.font}\", sans-serif`:`sans-serif`;o.font=`${n.weight??`normal`} ${s}px ${c}`,o.textBaseline=`alphabetic`,o.setTransform(r[0],r[1],r[2],r[3],r[4],r[5]);let[l,u]=n.at,d=[...n.text],f=n.tracking??0,p=e=>{if(n.vertical){o.textAlign=`center`,d.forEach((t,n)=>e(t,l,u+s*(n+.86)+(s*.08+f)*n));return}o.textAlign=`left`;let t=d.map(e=>o.measureText(e).width+f),r=t.reduce((e,t)=>e+t,0)-f,i=l-(n.align===`center`?r/2:n.align===`right`?r:0);d.forEach((n,r)=>{e(n,i,u),i+=t[r]})},m=()=>{let e=o.getImageData(0,0,i,a).data,t=new Float32Array(i*a);for(let n=0;n<t.length;n++)t[n]=e[n*4+3]/255;return t};p((e,t,n)=>o.fillText(e,t,n));let h=m(),g=null;return n.stroke&&(o.setTransform(1,0,0,1,0,0),o.clearRect(0,0,i,a),o.setTransform(r[0],r[1],r[2],r[3],r[4],r[5]),o.lineWidth=n.stroke.width,o.lineJoin=`round`,p((e,t,n)=>o.strokeText(e,t,n)),g=m()),{fill:h,line:g}}}let vn=[`normal`,`surface`],yn=/\\.(ttf|otf|woff2?)$/i,bn=e=>yn.test(e);function xn(e){let t=JSON.parse(e);if(typeof t!=`object`||!t||!(`mapOf`in t))return{picture:qe(t)};let{mapOf:n,map:r}=t;if(typeof n!=`string`||!n.toLowerCase().endsWith(`.esimage`))throw Error(`mapOf must be the project path of an .esimage`);if(!vn.includes(r))throw Error(`map must be one of ${vn.join(`, `)}`);let i=Object.keys(t).filter(e=>e!==`mapOf`&&e!==`map`&&e!==`version`);if(i.length>0)throw Error(`a map of a picture says only mapOf and map, and this one also says ${i.join(`, `)}`);return{map:{mapOf:n,map:r}}}function*Sn(e){function*t(e){for(let n of e)yield n,yield*t(n.layers??[])}yield*t(e.layers);for(let n of[e.material?.height,e.material?.roughness,e.material?.metal])Array.isArray(n)&&(yield*t(n))}function Cn(e){let t=new Set,n=new Set,r=!1;for(let i of Sn(e))i.shape===`image`&&i.src&&t.add(i.src),i.shape===`text`&&(r=!0,i.font&&bn(i.font)&&n.add(i.font));return{pictures:[...t],fonts:[...n],text:r}}typeof TextEncoder<`u`&&new TextEncoder;let wn=typeof TextDecoder<`u`?new TextDecoder:null;function Tn(e){if(wn)return wn.decode(e);let t=[],n=[];for(let r=0;r<e.length;){let i=e[r],a,o;if(i<128?(a=i,o=1):(i&224)==192?(a=i&31,o=2):(i&240)==224?(a=i&15,o=3):(i&248)==240?(a=i&7,o=4):(a=65533,o=1),o>1)if(r+o>e.length)a=65533,o=1;else for(let t=1;t<o;t++){let n=e[r+t];if((n&192)!=128){a=65533,o=1;break}a=a<<6|n&63}if(r+=o,a>65535){let e=a-65536;n.push(55296|e>>10,56320|e&1023)}else n.push(a);n.length>=4096&&(t.push(String.fromCharCode(...n)),n=[])}return n.length&&t.push(String.fromCharCode(...n)),t.join(``)}let En=`this thread has no canvas to draw text on`,Dn=new Map,On=e=>Tn(new Uint8Array(e)),kn=e=>e.byteOffset===0&&e.byteLength===e.buffer.byteLength?e:e.slice();function An(e,t){let n=e.files[t];if(!n)throw Error(`there is no file at ${t}`);return n}async function jn(e,t,n,r){let i=Cn(e),a={};for(let e of i.pictures){let i=t.pictures[e];if(i){a[e]={width:i.width,height:i.height,rgba:i.pixels};continue}if(r>=8)throw Error(`recipes laid in recipes go 8 deep at most`);let o=xn(On(An(t,e)));if(!(`picture`in o))throw Error(`${e} is a map of a picture, and a layer lays in a picture`);a[e]=(await jn(o.picture,t,n,r+1)).picture}let o;if(i.text){if(!n.canvas)throw Error(En);for(let e of i.fonts){if(Dn.has(e))continue;let r=`esimage-font-${Dn.size}`;await n.font(r,An(t,e)),Dn.set(e,r)}o=_n(n.canvas,e=>Dn.get(e))}let s={pictures:a,...o?{text:o}:{}};return{picture:fn(e,s),options:s}}async function Mn(e,t){let n=xn(e.source);if(`picture`in n){let{picture:r}=await jn(n.picture,e,t,0);return{width:r.width,height:r.height,pixels:kn(r.rgba)}}let{mapOf:r,map:i}=n.map,a=xn(On(An(e,r)));if(!(`picture`in a))throw Error(`${r} is itself a map of a picture`);let{picture:o,options:s}=await jn(a.picture,e,t,1),c=gn(a.picture,o,s);return i===`normal`?{width:c.normal.width,height:c.normal.height,pixels:kn(c.normal.rgba)}:c.surface?{width:c.surface.width,height:c.surface.height,pixels:kn(c.surface.rgba)}:{width:1,height:1,pixels:new Uint8Array([255,Math.round(c.roughness*255),Math.round(c.metal*255),255])}}if(typeof self<`u`&&self.document===void 0){let e=self,t={canvas:typeof OffscreenCanvas<`u`?(e,t)=>new OffscreenCanvas(e,t):null,async font(t,n){if(typeof FontFace>`u`||!e.fonts)throw Error(En);let r=new FontFace(t,n);await r.load(),e.fonts.add(r)}};e.onmessage=n=>{let r=n.data;Mn(r,t).then(t=>e.postMessage({id:r.id,result:t},[t.pixels.buffer]),t=>e.postMessage({id:r.id,error:t instanceof Error?t.message:String(t)}))}}return e.NO_TEXT_CANVAS=En,e.drawRecipe=Mn,e})({});";