/* esm.sh - roughjs@4.6.4/bin/generator */
import{hachureLines as U}from"../../../hachure-fill@^0.5.2?target=es2022";function S(r,e){var t;let s=e.hachureAngle+90,n=e.hachureGap;n<0&&(n=e.strokeWidth*4),n=Math.max(n,.1);let a=1;return e.roughness>=1&&(((t=e.randomizer)===null||t===void 0?void 0:t.next())||Math.random())>.7&&(a=n),U(r,n,s,a||1)}var _=class{constructor(e){this.helper=e}fillPolygons(e,t){return this._fillPolygons(e,t)}_fillPolygons(e,t){let s=S(e,t);return{type:"fillSketch",ops:this.renderLines(s,t)}}renderLines(e,t){let s=[];for(let n of e)s.push(...this.helper.doubleLineOps(n[0][0],n[0][1],n[1][0],n[1][1],t));return s}};function L(r){let e=r[0],t=r[1];return Math.sqrt(Math.pow(e[0]-t[0],2)+Math.pow(e[1]-t[1],2))}var w=class extends _{fillPolygons(e,t){let s=t.hachureGap;s<0&&(s=t.strokeWidth*4),s=Math.max(s,.1);let n=Object.assign({},t,{hachureGap:s}),a=S(e,n),i=Math.PI/180*t.hachureAngle,o=[],c=s*.5*Math.cos(i),h=s*.5*Math.sin(i);for(let[u,p]of a)L([u,p])&&o.push([[u[0]-c,u[1]+h],[...p]],[[u[0]+c,u[1]-h],[...p]]);return{type:"fillSketch",ops:this.renderLines(o,t)}}};var W=class extends _{fillPolygons(e,t){let s=this._fillPolygons(e,t),n=Object.assign({},t,{hachureAngle:t.hachureAngle+90}),a=this._fillPolygons(e,n);return s.ops=s.ops.concat(a.ops),s}};var F=class{constructor(e){this.helper=e}fillPolygons(e,t){t=Object.assign({},t,{hachureAngle:0});let s=S(e,t);return this.dotsOnLines(s,t)}dotsOnLines(e,t){let s=[],n=t.hachureGap;n<0&&(n=t.strokeWidth*4),n=Math.max(n,.1);let a=t.fillWeight;a<0&&(a=t.strokeWidth/2);let i=n/4;for(let o of e){let c=L(o),h=c/n,l=Math.ceil(h)-1,u=c-l*n,p=(o[0][0]+o[1][0])/2-n/4,f=Math.min(o[0][1],o[1][1]);for(let g=0;g<l;g++){let M=f+u+g*n,b=p-i+Math.random()*2*i,m=M-i+Math.random()*2*i,O=this.helper.ellipse(b,m,a,a,t);s.push(...O.ops)}}return{type:"fillSketch",ops:s}}};var T=class{constructor(e){this.helper=e}fillPolygons(e,t){let s=S(e,t);return{type:"fillSketch",ops:this.dashedLine(s,t)}}dashedLine(e,t){let s=t.dashOffset<0?t.hachureGap<0?t.strokeWidth*4:t.hachureGap:t.dashOffset,n=t.dashGap<0?t.hachureGap<0?t.strokeWidth*4:t.hachureGap:t.dashGap,a=[];return e.forEach(i=>{let o=L(i),c=Math.floor(o/(s+n)),h=(o+n-c*(s+n))/2,l=i[0],u=i[1];l[0]>u[0]&&(l=i[1],u=i[0]);let p=Math.atan((u[1]-l[1])/(u[0]-l[0]));for(let f=0;f<c;f++){let g=f*(s+n),M=g+s,b=[l[0]+g*Math.cos(p)+h*Math.cos(p),l[1]+g*Math.sin(p)+h*Math.sin(p)],m=[l[0]+M*Math.cos(p)+h*Math.cos(p),l[1]+M*Math.sin(p)+h*Math.sin(p)];a.push(...this.helper.doubleLineOps(b[0],b[1],m[0],m[1],t))}}),a}};var G=class{constructor(e){this.helper=e}fillPolygons(e,t){let s=t.hachureGap<0?t.strokeWidth*4:t.hachureGap,n=t.zigzagOffset<0?s:t.zigzagOffset;t=Object.assign({},t,{hachureGap:s+n});let a=S(e,t);return{type:"fillSketch",ops:this.zigzagLines(a,n,t)}}zigzagLines(e,t,s){let n=[];return e.forEach(a=>{let i=L(a),o=Math.round(i/(2*t)),c=a[0],h=a[1];c[0]>h[0]&&(c=a[1],h=a[0]);let l=Math.atan((h[1]-c[1])/(h[0]-c[0]));for(let u=0;u<o;u++){let p=u*2*t,f=(u+1)*2*t,g=Math.sqrt(2*Math.pow(t,2)),M=[c[0]+p*Math.cos(l),c[1]+p*Math.sin(l)],b=[c[0]+f*Math.cos(l),c[1]+f*Math.sin(l)],m=[M[0]+g*Math.cos(l+Math.PI/4),M[1]+g*Math.sin(l+Math.PI/4)];n.push(...this.helper.doubleLineOps(M[0],M[1],m[0],m[1],s),...this.helper.doubleLineOps(m[0],m[1],b[0],b[1],s))}}),n}};var k={};function x(r,e){let t=r.fillStyle||"hachure";if(!k[t])switch(t){case"zigzag":k[t]||(k[t]=new w(e));break;case"cross-hatch":k[t]||(k[t]=new W(e));break;case"dots":k[t]||(k[t]=new F(e));break;case"dashed":k[t]||(k[t]=new T(e));break;case"zigzag-line":k[t]||(k[t]=new G(e));break;case"hachure":default:t="hachure",k[t]||(k[t]=new _(e));break}return k[t]}function B(){return Math.floor(Math.random()*2**31)}var R=class{constructor(e){this.seed=e}next(){return this.seed?(2**31-1&(this.seed=Math.imul(48271,this.seed)))/2**31:Math.random()}};import{parsePath as tt,normalize as et,absolutize as st}from"../../../path-data-parser@^0.1.0?target=es2022";var nt={randOffset:ot,randOffsetWithRange:it,ellipse:at,doubleLineOps:ht};function C(r,e,t,s,n){return{type:"path",ops:v(r,e,t,s,n)}}function I(r,e,t){let s=(r||[]).length;if(s>2){let n=[];for(let a=0;a<s-1;a++)n.push(...v(r[a][0],r[a][1],r[a+1][0],r[a+1][1],t));return e&&n.push(...v(r[s-1][0],r[s-1][1],r[0][0],r[0][1],t)),{type:"path",ops:n}}else if(s===2)return C(r[0][0],r[0][1],r[1][0],r[1][1],t);return{type:"path",ops:[]}}function rt(r,e){return I(r,!0,e)}function D(r,e,t,s,n){let a=[[r,e],[r+t,e],[r+t,e+s],[r,e+s]];return rt(a,n)}function H(r,e){let t=Y(r,1*(1+e.roughness*.2),e);if(!e.disableMultiStroke){let s=Y(r,1.5*(1+e.roughness*.22),lt(e));t=t.concat(s)}return{type:"path",ops:t}}function at(r,e,t,s,n){let a=q(t,s,n);return j(r,e,n,a).opset}function q(r,e,t){let s=Math.sqrt(Math.PI*2*Math.sqrt((Math.pow(r/2,2)+Math.pow(e/2,2))/2)),n=Math.ceil(Math.max(t.curveStepCount,t.curveStepCount/Math.sqrt(200)*s)),a=Math.PI*2/n,i=Math.abs(r/2),o=Math.abs(e/2),c=1-t.curveFitting;return i+=d(i*c,t),o+=d(o*c,t),{increment:a,rx:i,ry:o}}function j(r,e,t,s){let[n,a]=Z(s.increment,r,e,s.rx,s.ry,1,s.increment*y(.1,y(.4,1,t),t),t),i=A(n,null,t);if(!t.disableMultiStroke&&t.roughness!==0){let[o]=Z(s.increment,r,e,s.rx,s.ry,1.5,0,t),c=A(o,null,t);i=i.concat(c)}return{estimatedPoints:a,opset:{type:"path",ops:i}}}function E(r,e,t,s,n,a,i,o,c){let h=r,l=e,u=Math.abs(t/2),p=Math.abs(s/2);u+=d(u*.01,c),p+=d(p*.01,c);let f=n,g=a;for(;f<0;)f+=Math.PI*2,g+=Math.PI*2;g-f>Math.PI*2&&(f=0,g=Math.PI*2);let M=Math.PI*2/c.curveStepCount,b=Math.min(M/2,(g-f)/2),m=X(b,h,l,u,p,f,g,1,c);if(!c.disableMultiStroke){let O=X(b,h,l,u,p,f,g,1.5,c);m.push(...O)}return i&&(o?m.push(...v(h,l,h+u*Math.cos(f),l+p*Math.sin(f),c),...v(h,l,h+u*Math.cos(g),l+p*Math.sin(g),c)):m.push({op:"lineTo",data:[h,l]},{op:"lineTo",data:[h+u*Math.cos(f),l+p*Math.sin(f)]})),{type:"path",ops:m}}function V(r,e){let t=et(st(tt(r))),s=[],n=[0,0],a=[0,0];for(let{key:i,data:o}of t)switch(i){case"M":{a=[o[0],o[1]],n=[o[0],o[1]];break}case"L":s.push(...v(a[0],a[1],o[0],o[1],e)),a=[o[0],o[1]];break;case"C":{let[c,h,l,u,p,f]=o;s.push(...ct(c,h,l,u,p,f,a,e)),a=[p,f];break}case"Z":s.push(...v(a[0],a[1],n[0],n[1],e)),a=[n[0],n[1]];break}return{type:"path",ops:s}}function $(r,e){let t=[];for(let s of r)if(s.length){let n=e.maxRandomnessOffset||0,a=s.length;if(a>2){t.push({op:"move",data:[s[0][0]+d(n,e),s[0][1]+d(n,e)]});for(let i=1;i<a;i++)t.push({op:"lineTo",data:[s[i][0]+d(n,e),s[i][1]+d(n,e)]})}}return{type:"fillPath",ops:t}}function z(r,e){return x(e,nt).fillPolygons(r,e)}function J(r,e,t,s,n,a,i){let o=r,c=e,h=Math.abs(t/2),l=Math.abs(s/2);h+=d(h*.01,i),l+=d(l*.01,i);let u=n,p=a;for(;u<0;)u+=Math.PI*2,p+=Math.PI*2;p-u>Math.PI*2&&(u=0,p=Math.PI*2);let f=(p-u)/i.curveStepCount,g=[];for(let M=u;M<=p;M=M+f)g.push([o+h*Math.cos(M),c+l*Math.sin(M)]);return g.push([o+h*Math.cos(p),c+l*Math.sin(p)]),g.push([o,c]),z([g],i)}function ot(r,e){return d(r,e)}function it(r,e,t){return y(r,e,t)}function ht(r,e,t,s,n){return v(r,e,t,s,n,!0)}function lt(r){let e=Object.assign({},r);return e.randomizer=void 0,r.seed&&(e.seed=r.seed+1),e}function K(r){return r.randomizer||(r.randomizer=new R(r.seed||0)),r.randomizer.next()}function y(r,e,t,s=1){return t.roughness*s*(K(t)*(e-r)+r)}function d(r,e,t=1){return y(-r,r,e,t)}function v(r,e,t,s,n,a=!1){let i=a?n.disableMultiStrokeFill:n.disableMultiStroke,o=N(r,e,t,s,n,!0,!1);if(i)return o;let c=N(r,e,t,s,n,!0,!0);return o.concat(c)}function N(r,e,t,s,n,a,i){let o=Math.pow(r-t,2)+Math.pow(e-s,2),c=Math.sqrt(o),h=1;c<200?h=1:c>500?h=.4:h=-.0016668*c+1.233334;let l=n.maxRandomnessOffset||0;l*l*100>o&&(l=c/10);let u=l/2,p=.2+K(n)*.2,f=n.bowing*n.maxRandomnessOffset*(s-e)/200,g=n.bowing*n.maxRandomnessOffset*(r-t)/200;f=d(f,n,h),g=d(g,n,h);let M=[],b=()=>d(u,n,h),m=()=>d(l,n,h),O=n.preserveVertices;return a&&(i?M.push({op:"move",data:[r+(O?0:b()),e+(O?0:b())]}):M.push({op:"move",data:[r+(O?0:d(l,n,h)),e+(O?0:d(l,n,h))]})),i?M.push({op:"bcurveTo",data:[f+r+(t-r)*p+b(),g+e+(s-e)*p+b(),f+r+2*(t-r)*p+b(),g+e+2*(s-e)*p+b(),t+(O?0:b()),s+(O?0:b())]}):M.push({op:"bcurveTo",data:[f+r+(t-r)*p+m(),g+e+(s-e)*p+m(),f+r+2*(t-r)*p+m(),g+e+2*(s-e)*p+m(),t+(O?0:m()),s+(O?0:m())]}),M}function Y(r,e,t){let s=[];s.push([r[0][0]+d(e,t),r[0][1]+d(e,t)]),s.push([r[0][0]+d(e,t),r[0][1]+d(e,t)]);for(let n=1;n<r.length;n++)s.push([r[n][0]+d(e,t),r[n][1]+d(e,t)]),n===r.length-1&&s.push([r[n][0]+d(e,t),r[n][1]+d(e,t)]);return A(s,null,t)}function A(r,e,t){let s=r.length,n=[];if(s>3){let a=[],i=1-t.curveTightness;n.push({op:"move",data:[r[1][0],r[1][1]]});for(let o=1;o+2<s;o++){let c=r[o];a[0]=[c[0],c[1]],a[1]=[c[0]+(i*r[o+1][0]-i*r[o-1][0])/6,c[1]+(i*r[o+1][1]-i*r[o-1][1])/6],a[2]=[r[o+1][0]+(i*r[o][0]-i*r[o+2][0])/6,r[o+1][1]+(i*r[o][1]-i*r[o+2][1])/6],a[3]=[r[o+1][0],r[o+1][1]],n.push({op:"bcurveTo",data:[a[1][0],a[1][1],a[2][0],a[2][1],a[3][0],a[3][1]]})}if(e&&e.length===2){let o=t.maxRandomnessOffset;n.push({op:"lineTo",data:[e[0]+d(o,t),e[1]+d(o,t)]})}}else s===3?(n.push({op:"move",data:[r[1][0],r[1][1]]}),n.push({op:"bcurveTo",data:[r[1][0],r[1][1],r[2][0],r[2][1],r[2][0],r[2][1]]})):s===2&&n.push(...v(r[0][0],r[0][1],r[1][0],r[1][1],t));return n}function Z(r,e,t,s,n,a,i,o){let c=o.roughness===0,h=[],l=[];if(c){r=r/4,l.push([e+s*Math.cos(-r),t+n*Math.sin(-r)]);for(let u=0;u<=Math.PI*2;u=u+r){let p=[e+s*Math.cos(u),t+n*Math.sin(u)];h.push(p),l.push(p)}l.push([e+s*Math.cos(0),t+n*Math.sin(0)]),l.push([e+s*Math.cos(r),t+n*Math.sin(r)])}else{let u=d(.5,o)-Math.PI/2;l.push([d(a,o)+e+.9*s*Math.cos(u-r),d(a,o)+t+.9*n*Math.sin(u-r)]);let p=Math.PI*2+u-.01;for(let f=u;f<p;f=f+r){let g=[d(a,o)+e+s*Math.cos(f),d(a,o)+t+n*Math.sin(f)];h.push(g),l.push(g)}l.push([d(a,o)+e+s*Math.cos(u+Math.PI*2+i*.5),d(a,o)+t+n*Math.sin(u+Math.PI*2+i*.5)]),l.push([d(a,o)+e+.98*s*Math.cos(u+i),d(a,o)+t+.98*n*Math.sin(u+i)]),l.push([d(a,o)+e+.9*s*Math.cos(u+i*.5),d(a,o)+t+.9*n*Math.sin(u+i*.5)])}return[l,h]}function X(r,e,t,s,n,a,i,o,c){let h=a+d(.1,c),l=[];l.push([d(o,c)+e+.9*s*Math.cos(h-r),d(o,c)+t+.9*n*Math.sin(h-r)]);for(let u=h;u<=i;u=u+r)l.push([d(o,c)+e+s*Math.cos(u),d(o,c)+t+n*Math.sin(u)]);return l.push([e+s*Math.cos(i),t+n*Math.sin(i)]),l.push([e+s*Math.cos(i),t+n*Math.sin(i)]),A(l,null,c)}function ct(r,e,t,s,n,a,i,o){let c=[],h=[o.maxRandomnessOffset||1,(o.maxRandomnessOffset||1)+.3],l=[0,0],u=o.disableMultiStroke?1:2,p=o.preserveVertices;for(let f=0;f<u;f++)f===0?c.push({op:"move",data:[i[0],i[1]]}):c.push({op:"move",data:[i[0]+(p?0:d(h[0],o)),i[1]+(p?0:d(h[0],o))]}),l=p?[n,a]:[n+d(h[f],o),a+d(h[f],o)],c.push({op:"bcurveTo",data:[r+d(h[f],o),e+d(h[f],o),t+d(h[f],o),s+d(h[f],o),l[0],l[1]]});return c}import{curveToBezier as ut}from"../../../points-on-curve@^0.2.0/lib/curve-to-bezier?target=es2022";import{pointsOnBezierCurves as pt}from"../../../points-on-curve@^0.2.0?target=es2022";import{pointsOnPath as ft}from"../../../points-on-path@^0.2.1?target=es2022";var P="none",Q=class{constructor(e){this.defaultOptions={maxRandomnessOffset:2,roughness:1,bowing:1,stroke:"#000",strokeWidth:1,curveTightness:0,curveFitting:.95,curveStepCount:9,fillStyle:"hachure",fillWeight:-1,hachureAngle:-41,hachureGap:-1,dashOffset:-1,dashGap:-1,zigzagOffset:-1,seed:0,disableMultiStroke:!1,disableMultiStrokeFill:!1,preserveVertices:!1,fillShapeRoughnessGain:.8},this.config=e||{},this.config.options&&(this.defaultOptions=this._o(this.config.options))}static newSeed(){return B()}_o(e){return e?Object.assign({},this.defaultOptions,e):this.defaultOptions}_d(e,t,s){return{shape:e,sets:t||[],options:s||this.defaultOptions}}line(e,t,s,n,a){let i=this._o(a);return this._d("line",[C(e,t,s,n,i)],i)}rectangle(e,t,s,n,a){let i=this._o(a),o=[],c=D(e,t,s,n,i);if(i.fill){let h=[[e,t],[e+s,t],[e+s,t+n],[e,t+n]];i.fillStyle==="solid"?o.push($([h],i)):o.push(z([h],i))}return i.stroke!==P&&o.push(c),this._d("rectangle",o,i)}ellipse(e,t,s,n,a){let i=this._o(a),o=[],c=q(s,n,i),h=j(e,t,i,c);if(i.fill)if(i.fillStyle==="solid"){let l=j(e,t,i,c).opset;l.type="fillPath",o.push(l)}else o.push(z([h.estimatedPoints],i));return i.stroke!==P&&o.push(h.opset),this._d("ellipse",o,i)}circle(e,t,s,n){let a=this.ellipse(e,t,s,s,n);return a.shape="circle",a}linearPath(e,t){let s=this._o(t);return this._d("linearPath",[I(e,!1,s)],s)}arc(e,t,s,n,a,i,o=!1,c){let h=this._o(c),l=[],u=E(e,t,s,n,a,i,o,!0,h);if(o&&h.fill)if(h.fillStyle==="solid"){let p=Object.assign({},h);p.disableMultiStroke=!0;let f=E(e,t,s,n,a,i,!0,!1,p);f.type="fillPath",l.push(f)}else l.push(J(e,t,s,n,a,i,h));return h.stroke!==P&&l.push(u),this._d("arc",l,h)}curve(e,t){let s=this._o(t),n=[],a=H(e,s);if(s.fill&&s.fill!==P&&e.length>=3)if(s.fillStyle==="solid"){let i=H(e,Object.assign(Object.assign({},s),{disableMultiStroke:!0,roughness:s.roughness?s.roughness+s.fillShapeRoughnessGain:0}));n.push({type:"fillPath",ops:this._mergedShape(i.ops)})}else{let i=ut(e),o=pt(i,10,(1+s.roughness)/2);n.push(z([o],s))}return s.stroke!==P&&n.push(a),this._d("curve",n,s)}polygon(e,t){let s=this._o(t),n=[],a=I(e,!0,s);return s.fill&&(s.fillStyle==="solid"?n.push($([e],s)):n.push(z([e],s))),s.stroke!==P&&n.push(a),this._d("polygon",n,s)}path(e,t){let s=this._o(t),n=[];if(!e)return this._d("path",n,s);e=(e||"").replace(/\n/g," ").replace(/(-\s)/g,"-").replace("/(ss)/g"," ");let a=s.fill&&s.fill!=="transparent"&&s.fill!==P,i=s.stroke!==P,o=!!(s.simplification&&s.simplification<1),c=o?4-4*(s.simplification||1):(1+s.roughness)/2,h=ft(e,1,c),l=V(e,s);if(a)if(s.fillStyle==="solid")if(h.length===1){let u=V(e,Object.assign(Object.assign({},s),{disableMultiStroke:!0,roughness:s.roughness?s.roughness+s.fillShapeRoughnessGain:0}));n.push({type:"fillPath",ops:this._mergedShape(u.ops)})}else n.push($(h,s));else n.push(z(h,s));return i&&(o?h.forEach(u=>{n.push(I(u,!1,s))}):n.push(l)),this._d("path",n,s)}opsToPath(e,t){let s="";for(let n of e.ops){let a=typeof t=="number"&&t>=0?n.data.map(i=>+i.toFixed(t)):n.data;switch(n.op){case"move":s+=`M${a[0]} ${a[1]} `;break;case"bcurveTo":s+=`C${a[0]} ${a[1]}, ${a[2]} ${a[3]}, ${a[4]} ${a[5]} `;break;case"lineTo":s+=`L${a[0]} ${a[1]} `;break}}return s.trim()}toPaths(e){let t=e.sets||[],s=e.options||this.defaultOptions,n=[];for(let a of t){let i=null;switch(a.type){case"path":i={d:this.opsToPath(a),stroke:s.stroke,strokeWidth:s.strokeWidth,fill:P};break;case"fillPath":i={d:this.opsToPath(a),stroke:P,strokeWidth:0,fill:s.fill||P};break;case"fillSketch":i=this.fillSketch(a,s);break}i&&n.push(i)}return n}fillSketch(e,t){let s=t.fillWeight;return s<0&&(s=t.strokeWidth/2),{d:this.opsToPath(e),stroke:t.fill||P,strokeWidth:s,fill:P}}_mergedShape(e){return e.filter((t,s)=>s===0?!0:t.op!=="move")}};export{Q as RoughGenerator};
//# sourceMappingURL=generator.mjs.map