rough-native
Version:
Create graphics using HTML Canvas or SVG with a hand-drawn, sketchy, appearance. Features comprehensive React hooks, memory management, and React 18 concurrent rendering support.
2 lines (1 loc) • 48.2 kB
JavaScript
var rough=function(e,t){"use strict";function s(e,t,s){if(e&&e.length){const[r,n]=t,a=Math.PI/180*s,o=Math.cos(a),i=Math.sin(a);for(const t of e){const[e,s]=t;t[0]=(e-r)*o-(s-n)*i+r,t[1]=(e-r)*i+(s-n)*o+n}}}function r(e,t){return e[0]===t[0]&&e[1]===t[1]}function n(e,t,n,a=1){const o=n,i=Math.max(t,.1),h=e[0]&&e[0][0]&&"number"==typeof e[0][0]?[e]:e,c=[0,0];if(o)for(const e of h)s(e,c,o);const l=function(e,t,s){const n=[];for(const t of e){const e=[...t];r(e[0],e[e.length-1])||e.push([e[0][0],e[0][1]]),e.length>2&&n.push(e)}const a=[];t=Math.max(t,.1);const o=[];for(const e of n)for(let t=0;t<e.length-1;t++){const s=e[t],r=e[t+1];if(s[1]!==r[1]){const e=Math.min(s[1],r[1]);o.push({ymin:e,ymax:Math.max(s[1],r[1]),x:e===s[1]?s[0]:r[0],islope:(r[0]-s[0])/(r[1]-s[1])})}}if(o.sort(((e,t)=>e.ymin<t.ymin?-1:e.ymin>t.ymin?1:e.x<t.x?-1:e.x>t.x?1:e.ymax===t.ymax?0:(e.ymax-t.ymax)/Math.abs(e.ymax-t.ymax))),!o.length)return a;let i=[],h=o[0].ymin,c=0;for(;i.length||o.length;){if(o.length){let e=-1;for(let t=0;t<o.length&&!(o[t].ymin>h);t++)e=t;o.splice(0,e+1).forEach((e=>{i.push({s:h,edge:e})}))}if(i=i.filter((e=>!(e.edge.ymax<=h))),i.sort(((e,t)=>e.edge.x===t.edge.x?0:(e.edge.x-t.edge.x)/Math.abs(e.edge.x-t.edge.x))),(1!==s||c%t==0)&&i.length>1)for(let e=0;e<i.length;e+=2){const t=e+1;if(t>=i.length)break;const s=i[e].edge,r=i[t].edge;a.push([[Math.round(s.x),h],[Math.round(r.x),h]])}h+=s,i.forEach((e=>{e.edge.x=e.edge.x+s*e.edge.islope})),c++}return a}(h,i,a);if(o){for(const e of h)s(e,c,-o);!function(e,t,r){const n=[];e.forEach((e=>n.push(...e))),s(n,t,r)}(l,c,-o)}return l}const a={HIGH_MEMORY_PRESSURE_THRESHOLD:.8,MEDIUM_MEMORY_PRESSURE_THRESHOLD:.6,CACHE_SIZE_HIGH_PRESSURE:50,CACHE_SIZE_MEDIUM_PRESSURE:200,CACHE_SIZE_LOW_PRESSURE:500,CACHE_SIZE_DEFAULT:200,HIGH_PRESSURE_CLEANUP_PERCENT:.75,HIGH_PRESSURE_KEEP_PERCENT:.25,MEDIUM_PRESSURE_CLEANUP_PERCENT:.5,MEDIUM_PRESSURE_KEEP_PERCENT:.5,SHAPE_CACHE_MULTIPLIER:2,MAX_CONCURRENT_GENERATIONS_UNDER_PRESSURE:2,MEMORY_CHECK_INTERVAL_MS:5e3,BYTES_TO_KB:1024,KB_TO_MB:1024},o={MAX_ERROR_CACHE_SIZE:50,ERROR_RATE_LIMIT_PER_MINUTE:10,ERROR_CACHE_CLEANUP_INTERVAL_MS:6e4,SAMPLE_SIZE_FOR_ARRAY_PARAMS:3,MS_PER_MINUTE:6e4},i={MEMORY:a,ERROR:o,DRAWING:{DEFAULT_MAX_RANDOMNESS_OFFSET:2,DEFAULT_ROUGHNESS:1,DEFAULT_BOWING:1,DEFAULT_STROKE_WIDTH:1,DEFAULT_CURVE_TIGHTNESS:0,DEFAULT_CURVE_FITTING:.95,DEFAULT_CURVE_STEP_COUNT:9,DEFAULT_FILL_WEIGHT:-1,DEFAULT_HACHURE_ANGLE:-41,DEFAULT_HACHURE_GAP:-1,DEFAULT_DASH_OFFSET:-1,DEFAULT_DASH_GAP:-1,DEFAULT_ZIGZAG_OFFSET:-1,DEFAULT_SEED:0,DEFAULT_DISABLE_MULTI_STROKE:!1,DEFAULT_DISABLE_MULTI_STROKE_FILL:!1,DEFAULT_PRESERVE_VERTICES:!1,DEFAULT_FILL_SHAPE_ROUGHNESS_GAIN:.8,CURVE_ROUGHNESS_MULTIPLIER:.2,MULTI_STROKE_OFFSET_MULTIPLIER:1.5,MULTI_STROKE_ROUGHNESS_MULTIPLIER:.22,BEZIER_CURVE_POINTS:10,SIMPLIFICATION_FACTOR:4},RENDERING:{CURVE_STEP_COUNT_BASE:200,ELLIPSE_INCREMENT_OFFSET_FACTOR:.1,ELLIPSE_INCREMENT_OFFSET_RANGE:.4,ELLIPSE_MULTI_STROKE_FACTOR:1.5,ARC_RADIUS_OFFSET_FACTOR:.01,ARC_INCREMENT_DIVISOR:2,ARC_MULTI_STROKE_FACTOR:1.5,PATTERN_FILL_ARC_RADIUS_OFFSET:.01,LINE_LENGTH_THRESHOLD_SHORT:200,LINE_LENGTH_THRESHOLD_LONG:500,ROUGHNESS_GAIN_LONG_LINES:.4,ROUGHNESS_GAIN_COEFFICIENT:-.0016668,ROUGHNESS_GAIN_CONSTANT:1.233334,OFFSET_SQUARED_MULTIPLIER:100,OFFSET_DIVISOR_LONG_LINES:10,DIVERGE_POINT_BASE:.2,DIVERGE_POINT_RANGE:.2,BOWING_DISPLACEMENT_DIVISOR:200},FILL_PATTERN:{HACHURE_ANGLE_ADJUSTMENT:90,DEFAULT_HACHURE_GAP_MULTIPLIER:4,MIN_HACHURE_GAP:.1,HACHURE_SKIP_OFFSET_THRESHOLD:.7,CROSS_HATCH_ANGLE_INCREMENT:90,DEFAULT_ZIGZAG_GAP_MULTIPLIER:4,MIN_ZIGZAG_GAP:.1,DEGREES_TO_RADIANS_FACTOR:180,ZIGZAG_DISPLACEMENT_FACTOR:.5},RANDOM:{SEED_UPPER_BOUND:2**31,LCG_MASK:2**31-1,LCG_MULTIPLIER:48271,LCG_DIVISOR:2**31},REACT_HOOK:{ID_PREFIX:"rough-",MAX_RECURSION_DEPTH:10,KEY_LOOKUP_SET_THRESHOLD:10,DEFAULT_CURVE_TIGHTNESS:2},VALIDATION:{MIN_POINT_ARRAY_LENGTH:2,MIN_POLYGON_POINTS:3,MIN_PATH_STRING_LENGTH:0},getEnvironment:()=>{var e,t;const s="undefined"!=typeof process&&"production"===(null===(e=process.env)||void 0===e?void 0:e.NODE_ENV),r="undefined"!=typeof process&&"true"===(null===(t=process.env)||void 0===t?void 0:t.ROUGH_LOW_END_DEVICE);return{cacheSize:r?a.CACHE_SIZE_HIGH_PRESSURE:s?a.CACHE_SIZE_MEDIUM_PRESSURE:a.CACHE_SIZE_DEFAULT,highMemoryThreshold:r?.7:a.HIGH_MEMORY_PRESSURE_THRESHOLD,memoryCheckInterval:s?2*a.MEMORY_CHECK_INTERVAL_MS:a.MEMORY_CHECK_INTERVAL_MS,enableErrorLogging:!s,errorRateLimit:s?o.ERROR_RATE_LIMIT_PER_MINUTE/2:o.ERROR_RATE_LIMIT_PER_MINUTE}}};function h(e,t){var s;const r=t.hachureAngle+i.FILL_PATTERN.HACHURE_ANGLE_ADJUSTMENT;let a=t.hachureGap;a<0&&(a=t.strokeWidth*i.FILL_PATTERN.DEFAULT_HACHURE_GAP_MULTIPLIER),a=Math.round(Math.max(a,i.FILL_PATTERN.MIN_HACHURE_GAP));let o=1;return t.roughness>=1&&((null===(s=t.randomizer)||void 0===s?void 0:s.next())||Math.random())>i.FILL_PATTERN.HACHURE_SKIP_OFFSET_THRESHOLD&&(o=a),n(e,a,r,o||1)}class c{constructor(e){this.helper=e}fillPolygons(e,t){return this._fillPolygons(e,t)}_fillPolygons(e,t){const s=h(e,t);return{type:"fillSketch",ops:this.renderLines(s,t)}}renderLines(e,t){const s=[];for(const r of e)s.push(...this.helper.doubleLineOps(r[0][0],r[0][1],r[1][0],r[1][1],t));return s}}function l(e){const t=e[0],s=e[1];return Math.sqrt(Math.pow(t[0]-s[0],2)+Math.pow(t[1]-s[1],2))}class u extends c{fillPolygons(e,t){let s=t.hachureGap;s<0&&(s=4*t.strokeWidth),s=Math.max(s,.1);const r=h(e,Object.assign({},t,{hachureGap:s})),n=Math.PI/180*t.hachureAngle,a=[],o=.5*s*Math.cos(n),i=.5*s*Math.sin(n);for(const[e,t]of r)l([e,t])&&a.push([[e[0]-o,e[1]+i],[...t]],[[e[0]+o,e[1]-i],[...t]]);return{type:"fillSketch",ops:this.renderLines(a,t)}}}class E extends c{fillPolygons(e,t){const s=this._fillPolygons(e,t),r=Object.assign({},t,{hachureAngle:t.hachureAngle+i.FILL_PATTERN.CROSS_HATCH_ANGLE_INCREMENT}),n=this._fillPolygons(e,r);return s.ops=s.ops.concat(n.ops),s}}class p{constructor(e){this.helper=e}fillPolygons(e,t){const s=h(e,t=Object.assign({},t,{hachureAngle:0}));return this.dotsOnLines(s,t)}dotsOnLines(e,t){const s=[];let r=t.hachureGap;r<0&&(r=4*t.strokeWidth),r=Math.max(r,.1);let n=t.fillWeight;n<0&&(n=t.strokeWidth/2);const a=r/4;for(const o of e){const e=l(o),i=e/r,h=Math.ceil(i)-1,c=e-h*r,u=(o[0][0]+o[1][0])/2-r/4,E=Math.min(o[0][1],o[1][1]);for(let e=0;e<h;e++){const o=E+c+e*r,i=u-a+2*Math.random()*a,h=o-a+2*Math.random()*a,l=this.helper.ellipse(i,h,n,n,t);s.push(...l.ops)}}return{type:"fillSketch",ops:s}}}class f{constructor(e){this.helper=e}fillPolygons(e,t){const s=h(e,t);return{type:"fillSketch",ops:this.dashedLine(s,t)}}dashedLine(e,t){const s=t.dashOffset<0?t.hachureGap<0?4*t.strokeWidth:t.hachureGap:t.dashOffset,r=t.dashGap<0?t.hachureGap<0?4*t.strokeWidth:t.hachureGap:t.dashGap,n=[];return e.forEach((e=>{const a=l(e),o=Math.floor(a/(s+r)),i=(a+r-o*(s+r))/2;let h=e[0],c=e[1];h[0]>c[0]&&(h=e[1],c=e[0]);const u=Math.atan((c[1]-h[1])/(c[0]-h[0]));for(let e=0;e<o;e++){const a=e*(s+r),o=a+s,c=[h[0]+a*Math.cos(u)+i*Math.cos(u),h[1]+a*Math.sin(u)+i*Math.sin(u)],l=[h[0]+o*Math.cos(u)+i*Math.cos(u),h[1]+o*Math.sin(u)+i*Math.sin(u)];n.push(...this.helper.doubleLineOps(c[0],c[1],l[0],l[1],t))}})),n}}class d{constructor(e){this.helper=e}fillPolygons(e,t){const s=t.hachureGap<0?4*t.strokeWidth:t.hachureGap,r=t.zigzagOffset<0?s:t.zigzagOffset,n=h(e,t=Object.assign({},t,{hachureGap:s+r}));return{type:"fillSketch",ops:this.zigzagLines(n,r,t)}}zigzagLines(e,t,s){const r=[];return e.forEach((e=>{const n=l(e),a=Math.round(n/(2*t));let o=e[0],i=e[1];o[0]>i[0]&&(o=e[1],i=e[0]);const h=Math.atan((i[1]-o[1])/(i[0]-o[0]));for(let e=0;e<a;e++){const n=2*e*t,a=2*(e+1)*t,i=Math.sqrt(2*Math.pow(t,2)),c=[o[0]+n*Math.cos(h),o[1]+n*Math.sin(h)],l=[o[0]+a*Math.cos(h),o[1]+a*Math.sin(h)],u=[c[0]+i*Math.cos(h+Math.PI/4),c[1]+i*Math.sin(h+Math.PI/4)];r.push(...this.helper.doubleLineOps(c[0],c[1],u[0],u[1],s),...this.helper.doubleLineOps(u[0],u[1],l[0],l[1],s))}})),r}}const _={};class R{constructor(e){this.seed=e}next(){return this.seed?(i.RANDOM.LCG_MASK&(this.seed=Math.imul(i.RANDOM.LCG_MULTIPLIER,this.seed)))/i.RANDOM.LCG_DIVISOR:Math.random()}}const g=0,M=1,m=2,S={A:7,a:7,C:6,c:6,H:1,h:1,L:2,l:2,M:2,m:2,Q:4,q:4,S:4,s:4,T:2,t:2,V:1,v:1,Z:0,z:0};function I(e,t){return e.type===t}function T(e){const t=[],s=function(e){const t=new Array;for(;""!==e;)if(e.match(/^([ \t\r\n,]+)/))e=e.substr(RegExp.$1.length);else if(e.match(/^([aAcChHlLmMqQsStTvVzZ])/))t[t.length]={type:g,text:RegExp.$1},e=e.substr(RegExp.$1.length);else{if(!e.match(/^(([-+]?[0-9]+(\.[0-9]*)?|[-+]?\.[0-9]+)([eE][-+]?[0-9]+)?)/))return[];t[t.length]={type:M,text:`${parseFloat(RegExp.$1)}`},e=e.substr(RegExp.$1.length)}return t[t.length]={type:m,text:""},t}(e);let r="BOD",n=0,a=s[n];for(;!I(a,m);){let o=0;const i=[];if("BOD"===r){if("M"!==a.text&&"m"!==a.text)return T("M0,0"+e);n++,o=S[a.text],r=a.text}else I(a,M)?o=S[r]:(n++,o=S[a.text],r=a.text);if(!(n+o<s.length))throw new Error("Path data ended short");for(let e=n;e<n+o;e++){const t=s[e];if(!I(t,M))throw new Error("Param not a number: "+r+","+t.text);i[i.length]=+t.text}if("number"!=typeof S[r])throw new Error("Bad segment: "+r);{const e={key:r,data:i};t.push(e),n+=o,a=s[n],"M"===r&&(r="L"),"m"===r&&(r="l")}}return t}function y(e){let t=0,s=0,r=0,n=0;const a=[];for(const{key:o,data:i}of e)switch(o){case"M":a.push({key:"M",data:[...i]}),[t,s]=i,[r,n]=i;break;case"m":t+=i[0],s+=i[1],a.push({key:"M",data:[t,s]}),r=t,n=s;break;case"L":a.push({key:"L",data:[...i]}),[t,s]=i;break;case"l":t+=i[0],s+=i[1],a.push({key:"L",data:[t,s]});break;case"C":a.push({key:"C",data:[...i]}),t=i[4],s=i[5];break;case"c":{const e=i.map(((e,r)=>r%2?e+s:e+t));a.push({key:"C",data:e}),t=e[4],s=e[5];break}case"Q":a.push({key:"Q",data:[...i]}),t=i[2],s=i[3];break;case"q":{const e=i.map(((e,r)=>r%2?e+s:e+t));a.push({key:"Q",data:e}),t=e[2],s=e[3];break}case"A":a.push({key:"A",data:[...i]}),t=i[5],s=i[6];break;case"a":t+=i[5],s+=i[6],a.push({key:"A",data:[i[0],i[1],i[2],i[3],i[4],t,s]});break;case"H":a.push({key:"H",data:[...i]}),t=i[0];break;case"h":t+=i[0],a.push({key:"H",data:[t]});break;case"V":a.push({key:"V",data:[...i]}),s=i[0];break;case"v":s+=i[0],a.push({key:"V",data:[s]});break;case"S":a.push({key:"S",data:[...i]}),t=i[2],s=i[3];break;case"s":{const e=i.map(((e,r)=>r%2?e+s:e+t));a.push({key:"S",data:e}),t=e[2],s=e[3];break}case"T":a.push({key:"T",data:[...i]}),t=i[0],s=i[1];break;case"t":t+=i[0],s+=i[1],a.push({key:"T",data:[t,s]});break;case"Z":case"z":a.push({key:"Z",data:[]}),t=r,s=n}return a}function A(e){const t=[];let s="",r=0,n=0,a=0,o=0,i=0,h=0;for(const{key:c,data:l}of e){switch(c){case"M":t.push({key:"M",data:[...l]}),[r,n]=l,[a,o]=l;break;case"C":t.push({key:"C",data:[...l]}),r=l[4],n=l[5],i=l[2],h=l[3];break;case"L":t.push({key:"L",data:[...l]}),[r,n]=l;break;case"H":r=l[0],t.push({key:"L",data:[r,n]});break;case"V":n=l[0],t.push({key:"L",data:[r,n]});break;case"S":{let e=0,a=0;"C"===s||"S"===s?(e=r+(r-i),a=n+(n-h)):(e=r,a=n),t.push({key:"C",data:[e,a,...l]}),i=l[0],h=l[1],r=l[2],n=l[3];break}case"T":{const[e,a]=l;let o=0,c=0;"Q"===s||"T"===s?(o=r+(r-i),c=n+(n-h)):(o=r,c=n);const u=r+2*(o-r)/3,E=n+2*(c-n)/3,p=e+2*(o-e)/3,f=a+2*(c-a)/3;t.push({key:"C",data:[u,E,p,f,e,a]}),i=o,h=c,r=e,n=a;break}case"Q":{const[e,s,a,o]=l,c=r+2*(e-r)/3,u=n+2*(s-n)/3,E=a+2*(e-a)/3,p=o+2*(s-o)/3;t.push({key:"C",data:[c,u,E,p,a,o]}),i=e,h=s,r=a,n=o;break}case"A":{const e=Math.abs(l[0]),s=Math.abs(l[1]),a=l[2],o=l[3],i=l[4],h=l[5],c=l[6];if(0===e||0===s)t.push({key:"C",data:[r,n,h,c,h,c]}),r=h,n=c;else if(r!==h||n!==c){L(r,n,h,c,e,s,a,o,i).forEach((function(e){t.push({key:"C",data:e})})),r=h,n=c}break}case"Z":t.push({key:"Z",data:[]}),r=a,n=o}s=c}return t}function O(e,t,s){return[e*Math.cos(s)-t*Math.sin(s),e*Math.sin(s)+t*Math.cos(s)]}function L(e,t,s,r,n,a,o,i,h,c){const l=(u=o,Math.PI*u/180);var u;let E=[],p=0,f=0,d=0,_=0;if(c)[p,f,d,_]=c;else{[e,t]=O(e,t,-l),[s,r]=O(s,r,-l);const o=(e-s)/2,c=(t-r)/2;let u=o*o/(n*n)+c*c/(a*a);u>1&&(u=Math.sqrt(u),n*=u,a*=u);const E=n*n,R=a*a,g=E*R-E*c*c-R*o*o,M=E*c*c+R*o*o,m=(i===h?-1:1)*Math.sqrt(Math.abs(g/M));d=m*n*c/a+(e+s)/2,_=m*-a*o/n+(t+r)/2,p=Math.asin(parseFloat(((t-_)/a).toFixed(9))),f=Math.asin(parseFloat(((r-_)/a).toFixed(9))),e<d&&(p=Math.PI-p),s<d&&(f=Math.PI-f),p<0&&(p=2*Math.PI+p),f<0&&(f=2*Math.PI+f),h&&p>f&&(p-=2*Math.PI),!h&&f>p&&(f-=2*Math.PI)}let R=f-p;if(Math.abs(R)>120*Math.PI/180){const e=f,t=s,i=r;f=h&&f>p?p+120*Math.PI/180*1:p+120*Math.PI/180*-1,E=L(s=d+n*Math.cos(f),r=_+a*Math.sin(f),t,i,n,a,o,0,h,[f,e,d,_])}R=f-p;const g=Math.cos(p),M=Math.sin(p),m=Math.cos(f),S=Math.sin(f),I=Math.tan(R/4),T=4/3*n*I,y=4/3*a*I,A=[e,t],N=[e+T*M,t-y*g],C=[s+T*S,r-y*m],P=[s,r];if(N[0]=2*A[0]-N[0],N[1]=2*A[1]-N[1],c)return[N,C,P].concat(E);{E=[N,C,P].concat(E);const e=[];for(let t=0;t<E.length;t+=3){const s=O(E[t][0],E[t][1],l),r=O(E[t+1][0],E[t+1][1],l),n=O(E[t+2][0],E[t+2][1],l);e.push([s[0],s[1],r[0],r[1],n[0],n[1]])}return e}}const N={randOffset:function(e,t){return z(e,t)},randOffsetWithRange:function(e,t,s){return W(e,t,s)},ellipse:function(e,t,s,r,n){const a=G(s,r,n);return b(e,t,n,a).opset},doubleLineOps:function(e,t,s,r,n){return V(e,t,s,r,n,!0)}};function C(e,t,s,r,n){return{type:"path",ops:V(e,t,s,r,n)}}function P(e,t,s){const r=(e||[]).length;if(r>2){const n=[];for(let t=0;t<r-1;t++)n.push(...V(e[t][0],e[t][1],e[t+1][0],e[t+1][1],s));return t&&n.push(...V(e[r-1][0],e[r-1][1],e[0][0],e[0][1],s)),{type:"path",ops:n}}return 2===r?C(e[0][0],e[0][1],e[1][0],e[1][1],s):{type:"path",ops:[]}}function D(e,t,s,r,n){return function(e,t){return P(e,!0,t)}([[e,t],[e+s,t],[e+s,t+r],[e,t+r]],n)}function U(e,t){if(e.length){const s="number"==typeof e[0][0]?[e]:e,r=K(s[0],1*(1+t.roughness*i.DRAWING.CURVE_ROUGHNESS_MULTIPLIER),t),n=t.disableMultiStroke?[]:K(s[0],i.DRAWING.MULTI_STROKE_OFFSET_MULTIPLIER*(1+t.roughness*i.DRAWING.MULTI_STROKE_ROUGHNESS_MULTIPLIER),w(t));for(let e=1;e<s.length;e++){const a=s[e];if(a.length){const e=K(a,1*(1+t.roughness*i.DRAWING.CURVE_ROUGHNESS_MULTIPLIER),t),s=t.disableMultiStroke?[]:K(a,i.DRAWING.MULTI_STROKE_OFFSET_MULTIPLIER*(1+t.roughness*i.DRAWING.MULTI_STROKE_ROUGHNESS_MULTIPLIER),w(t));for(const t of e)"move"!==t.op&&r.push(t);for(const e of s)"move"!==e.op&&n.push(e)}}return{type:"path",ops:r.concat(n)}}return{type:"path",ops:[]}}function G(e,t,s){const r=Math.sqrt(2*Math.PI*Math.sqrt((Math.pow(e/2,2)+Math.pow(t/2,2))/2)),n=Math.ceil(Math.max(s.curveStepCount,s.curveStepCount/Math.sqrt(i.RENDERING.CURVE_STEP_COUNT_BASE)*r)),a=2*Math.PI/n;let o=Math.abs(e/2),h=Math.abs(t/2);const c=1-s.curveFitting;return o+=z(o*c,s),h+=z(h*c,s),{increment:a,rx:o,ry:h}}function b(e,t,s,r){const[n,a]=Y(r.increment,e,t,r.rx,r.ry,1,r.increment*W(i.RENDERING.ELLIPSE_INCREMENT_OFFSET_FACTOR,W(i.RENDERING.ELLIPSE_INCREMENT_OFFSET_RANGE,1,s),s),s);let o=j(n,null,s);if(!s.disableMultiStroke&&0!==s.roughness){const[n]=Y(r.increment,e,t,r.rx,r.ry,i.RENDERING.ELLIPSE_MULTI_STROKE_FACTOR,0,s),a=j(n,null,s);o=o.concat(a)}return{estimatedPoints:a,opset:{type:"path",ops:o}}}function k(e,t,s,r,n,a,o,h,c){const l=e,u=t;let E=Math.abs(s/2),p=Math.abs(r/2);E+=z(E*i.RENDERING.ARC_RADIUS_OFFSET_FACTOR,c),p+=z(p*i.RENDERING.ARC_RADIUS_OFFSET_FACTOR,c);let f=n,d=a;for(;f<0;)f+=2*Math.PI,d+=2*Math.PI;d-f>2*Math.PI&&(f=0,d=2*Math.PI);const _=2*Math.PI/c.curveStepCount,R=Math.min(_/i.RENDERING.ARC_INCREMENT_DIVISOR,(d-f)/i.RENDERING.ARC_INCREMENT_DIVISOR),g=Z(R,l,u,E,p,f,d,1,c);if(!c.disableMultiStroke){const e=Z(R,l,u,E,p,f,d,i.RENDERING.ARC_MULTI_STROKE_FACTOR,c);g.push(...e)}return o&&(h?g.push(...V(l,u,l+E*Math.cos(f),u+p*Math.sin(f),c),...V(l,u,l+E*Math.cos(d),u+p*Math.sin(d),c)):g.push({op:"lineTo",data:[l,u]},{op:"lineTo",data:[l+E*Math.cos(f),u+p*Math.sin(f)]})),{type:"path",ops:g}}function H(e,t){const s=A(y(T(e))),r=[];let n=[0,0],a=[0,0];for(const{key:e,data:o}of s)switch(e){case"M":a=[o[0],o[1]],n=[o[0],o[1]];break;case"L":r.push(...V(a[0],a[1],o[0],o[1],t)),a=[o[0],o[1]];break;case"C":{const[e,s,n,i,h,c]=o;r.push(...B(e,s,n,i,h,c,a,t)),a=[h,c];break}case"Z":r.push(...V(a[0],a[1],n[0],n[1],t)),a=[n[0],n[1]]}return{type:"path",ops:r}}function v(e,t){const s=[];for(const r of e)if(r.length){const e=t.maxRandomnessOffset||0,n=r.length;if(n>2){s.push({op:"move",data:[r[0][0]+z(e,t),r[0][1]+z(e,t)]});for(let a=1;a<n;a++)s.push({op:"lineTo",data:[r[a][0]+z(e,t),r[a][1]+z(e,t)]})}}return{type:"fillPath",ops:s}}function F(e,t){return function(e,t){let s=e.fillStyle||"hachure";if(!_[s])switch(s){case"zigzag":_[s]||(_[s]=new u(t));break;case"cross-hatch":_[s]||(_[s]=new E(t));break;case"dots":_[s]||(_[s]=new p(t));break;case"dashed":_[s]||(_[s]=new f(t));break;case"zigzag-line":_[s]||(_[s]=new d(t));break;default:s="hachure",_[s]||(_[s]=new c(t))}return _[s]}(t,N).fillPolygons(e,t)}function w(e){const t=Object.assign({},e);return t.randomizer=void 0,e.seed&&(t.seed=e.seed+1),t}function x(e){return e.randomizer||(e.randomizer=new R(e.seed||0)),e.randomizer.next()}function W(e,t,s,r=1){return s.roughness*r*(x(s)*(t-e)+e)}function z(e,t,s=1){return W(-e,e,t,s)}function V(e,t,s,r,n,a=!1){const o=a?n.disableMultiStrokeFill:n.disableMultiStroke,i=$(e,t,s,r,n,!0,!1);if(o)return i;const h=$(e,t,s,r,n,!0,!0);return i.concat(h)}function $(e,t,s,r,n,a,o){const h=Math.pow(e-s,2)+Math.pow(t-r,2),c=Math.sqrt(h);let l=1;l=c<i.RENDERING.LINE_LENGTH_THRESHOLD_SHORT?1:c>i.RENDERING.LINE_LENGTH_THRESHOLD_LONG?i.RENDERING.ROUGHNESS_GAIN_LONG_LINES:i.RENDERING.ROUGHNESS_GAIN_COEFFICIENT*c+i.RENDERING.ROUGHNESS_GAIN_CONSTANT;let u=n.maxRandomnessOffset||0;u*u*i.RENDERING.OFFSET_SQUARED_MULTIPLIER>h&&(u=c/i.RENDERING.OFFSET_DIVISOR_LONG_LINES);const E=u/2,p=i.RENDERING.DIVERGE_POINT_BASE+x(n)*i.RENDERING.DIVERGE_POINT_RANGE;let f=n.bowing*n.maxRandomnessOffset*(r-t)/i.RENDERING.BOWING_DISPLACEMENT_DIVISOR,d=n.bowing*n.maxRandomnessOffset*(e-s)/i.RENDERING.BOWING_DISPLACEMENT_DIVISOR;f=z(f,n,l),d=z(d,n,l);const _=[],R=()=>z(E,n,l),g=()=>z(u,n,l),M=n.preserveVertices;return a&&(o?_.push({op:"move",data:[e+(M?0:R()),t+(M?0:R())]}):_.push({op:"move",data:[e+(M?0:z(u,n,l)),t+(M?0:z(u,n,l))]})),o?_.push({op:"bcurveTo",data:[f+e+(s-e)*p+R(),d+t+(r-t)*p+R(),f+e+2*(s-e)*p+R(),d+t+2*(r-t)*p+R(),s+(M?0:R()),r+(M?0:R())]}):_.push({op:"bcurveTo",data:[f+e+(s-e)*p+g(),d+t+(r-t)*p+g(),f+e+2*(s-e)*p+g(),d+t+2*(r-t)*p+g(),s+(M?0:g()),r+(M?0:g())]}),_}function K(e,t,s){if(!e.length)return[];const r=[];r.push([e[0][0]+z(t,s),e[0][1]+z(t,s)]),r.push([e[0][0]+z(t,s),e[0][1]+z(t,s)]);for(let n=1;n<e.length;n++)r.push([e[n][0]+z(t,s),e[n][1]+z(t,s)]),n===e.length-1&&r.push([e[n][0]+z(t,s),e[n][1]+z(t,s)]);return j(r,null,s)}function j(e,t,s){const r=e.length,n=[];if(r>3){const a=[],o=1-s.curveTightness;n.push({op:"move",data:[e[1][0],e[1][1]]});for(let t=1;t+2<r;t++){const s=e[t];a[0]=[s[0],s[1]],a[1]=[s[0]+(o*e[t+1][0]-o*e[t-1][0])/6,s[1]+(o*e[t+1][1]-o*e[t-1][1])/6],a[2]=[e[t+1][0]+(o*e[t][0]-o*e[t+2][0])/6,e[t+1][1]+(o*e[t][1]-o*e[t+2][1])/6],a[3]=[e[t+1][0],e[t+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(t&&2===t.length){const e=s.maxRandomnessOffset;n.push({op:"lineTo",data:[t[0]+z(e,s),t[1]+z(e,s)]})}}else 3===r?(n.push({op:"move",data:[e[1][0],e[1][1]]}),n.push({op:"bcurveTo",data:[e[1][0],e[1][1],e[2][0],e[2][1],e[2][0],e[2][1]]})):2===r&&n.push(...$(e[0][0],e[0][1],e[1][0],e[1][1],s,!0,!0));return n}function Y(e,t,s,r,n,a,o,i){const h=[],c=[];if(0===i.roughness){e/=4,c.push([t+r*Math.cos(-e),s+n*Math.sin(-e)]);for(let a=0;a<=2*Math.PI;a+=e){const e=[t+r*Math.cos(a),s+n*Math.sin(a)];h.push(e),c.push(e)}c.push([t+r*Math.cos(0),s+n*Math.sin(0)]),c.push([t+r*Math.cos(e),s+n*Math.sin(e)])}else{const l=z(.5,i)-Math.PI/2;c.push([z(a,i)+t+.9*r*Math.cos(l-e),z(a,i)+s+.9*n*Math.sin(l-e)]);const u=2*Math.PI+l-.01;for(let o=l;o<u;o+=e){const e=[z(a,i)+t+r*Math.cos(o),z(a,i)+s+n*Math.sin(o)];h.push(e),c.push(e)}c.push([z(a,i)+t+r*Math.cos(l+2*Math.PI+.5*o),z(a,i)+s+n*Math.sin(l+2*Math.PI+.5*o)]),c.push([z(a,i)+t+.98*r*Math.cos(l+o),z(a,i)+s+.98*n*Math.sin(l+o)]),c.push([z(a,i)+t+.9*r*Math.cos(l+.5*o),z(a,i)+s+.9*n*Math.sin(l+.5*o)])}return[c,h]}function Z(e,t,s,r,n,a,o,i,h){const c=a+z(.1,h),l=[];l.push([z(i,h)+t+.9*r*Math.cos(c-e),z(i,h)+s+.9*n*Math.sin(c-e)]);for(let a=c;a<=o;a+=e)l.push([z(i,h)+t+r*Math.cos(a),z(i,h)+s+n*Math.sin(a)]);return l.push([t+r*Math.cos(o),s+n*Math.sin(o)]),l.push([t+r*Math.cos(o),s+n*Math.sin(o)]),j(l,null,h)}function B(e,t,s,r,n,a,o,i){const h=[],c=[i.maxRandomnessOffset||1,(i.maxRandomnessOffset||1)+.3];let l=[0,0];const u=i.disableMultiStroke?1:2,E=i.preserveVertices;for(let p=0;p<u;p++)0===p?h.push({op:"move",data:[o[0],o[1]]}):h.push({op:"move",data:[o[0]+(E?0:z(c[0],i)),o[1]+(E?0:z(c[0],i))]}),l=E?[n,a]:[n+z(c[p],i),a+z(c[p],i)],h.push({op:"bcurveTo",data:[e+z(c[p],i),t+z(c[p],i),s+z(c[p],i),r+z(c[p],i),l[0],l[1]]});return h}function q(e){return[...e]}function J(e,t=0){const s=e.length;if(s<3)throw new Error("A curve must have at least three points.");const r=[];if(3===s)r.push(q(e[0]),q(e[1]),q(e[2]),q(e[2]));else{const s=[];s.push(e[0],e[0]);for(let t=1;t<e.length;t++)s.push(e[t]),t===e.length-1&&s.push(e[t]);const n=[],a=1-t;r.push(q(s[0]));for(let e=1;e+2<s.length;e++){const t=s[e];n[0]=[t[0],t[1]],n[1]=[t[0]+(a*s[e+1][0]-a*s[e-1][0])/6,t[1]+(a*s[e+1][1]-a*s[e-1][1])/6],n[2]=[s[e+1][0]+(a*s[e][0]-a*s[e+2][0])/6,s[e+1][1]+(a*s[e][1]-a*s[e+2][1])/6],n[3]=[s[e+1][0],s[e+1][1]],r.push(n[1],n[2],n[3])}}return r}function Q(e,t){return Math.pow(e[0]-t[0],2)+Math.pow(e[1]-t[1],2)}function X(e,t,s){const r=Q(t,s);if(0===r)return Q(e,t);let n=((e[0]-t[0])*(s[0]-t[0])+(e[1]-t[1])*(s[1]-t[1]))/r;return n=Math.max(0,Math.min(1,n)),Q(e,ee(t,s,n))}function ee(e,t,s){return[e[0]+(t[0]-e[0])*s,e[1]+(t[1]-e[1])*s]}function te(e,t,s,r){const n=r||[];if(function(e,t){const s=e[t+0],r=e[t+1],n=e[t+2],a=e[t+3];let o=3*r[0]-2*s[0]-a[0];o*=o;let i=3*r[1]-2*s[1]-a[1];i*=i;let h=3*n[0]-2*a[0]-s[0];h*=h;let c=3*n[1]-2*a[1]-s[1];return c*=c,o<h&&(o=h),i<c&&(i=c),o+i}(e,t)<s){const s=e[t+0];if(n.length){(a=n[n.length-1],o=s,Math.sqrt(Q(a,o)))>1&&n.push(s)}else n.push(s);n.push(e[t+3])}else{const r=.5,a=e[t+0],o=e[t+1],i=e[t+2],h=e[t+3],c=ee(a,o,r),l=ee(o,i,r),u=ee(i,h,r),E=ee(c,l,r),p=ee(l,u,r),f=ee(E,p,r);te([a,c,E,f],0,s,n),te([f,p,u,h],0,s,n)}var a,o;return n}function se(e,t){return re(e,0,e.length,t)}function re(e,t,s,r,n){const a=n||[],o=e[t],i=e[s-1];let h=0,c=1;for(let r=t+1;r<s-1;++r){const t=X(e[r],o,i);t>h&&(h=t,c=r)}return Math.sqrt(h)>r?(re(e,t,c+1,r,a),re(e,c,s,r,a)):(a.length||a.push(o),a.push(i)),a}function ne(e,t=.15,s){const r=[],n=(e.length-1)/3;for(let s=0;s<n;s++){te(e,3*s,t,r)}return s&&s>0?re(r,0,r.length,s):r}const ae="none";class oe{constructor(e){this.defaultOptions={maxRandomnessOffset:i.DRAWING.DEFAULT_MAX_RANDOMNESS_OFFSET,roughness:i.DRAWING.DEFAULT_ROUGHNESS,bowing:i.DRAWING.DEFAULT_BOWING,stroke:"#000",strokeWidth:i.DRAWING.DEFAULT_STROKE_WIDTH,curveTightness:i.DRAWING.DEFAULT_CURVE_TIGHTNESS,curveFitting:i.DRAWING.DEFAULT_CURVE_FITTING,curveStepCount:i.DRAWING.DEFAULT_CURVE_STEP_COUNT,fillStyle:"hachure",fillWeight:i.DRAWING.DEFAULT_FILL_WEIGHT,hachureAngle:i.DRAWING.DEFAULT_HACHURE_ANGLE,hachureGap:i.DRAWING.DEFAULT_HACHURE_GAP,dashOffset:i.DRAWING.DEFAULT_DASH_OFFSET,dashGap:i.DRAWING.DEFAULT_DASH_GAP,zigzagOffset:i.DRAWING.DEFAULT_ZIGZAG_OFFSET,seed:i.DRAWING.DEFAULT_SEED,disableMultiStroke:i.DRAWING.DEFAULT_DISABLE_MULTI_STROKE,disableMultiStrokeFill:i.DRAWING.DEFAULT_DISABLE_MULTI_STROKE_FILL,preserveVertices:i.DRAWING.DEFAULT_PRESERVE_VERTICES,fillShapeRoughnessGain:i.DRAWING.DEFAULT_FILL_SHAPE_ROUGHNESS_GAIN},this.config=e||{},this.config.options&&(this.defaultOptions=this._o(this.config.options))}static newSeed(){return Math.floor(Math.random()*i.RANDOM.SEED_UPPER_BOUND)}_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,r,n){const a=this._o(n);return this._d("line",[C(e,t,s,r,a)],a)}rectangle(e,t,s,r,n){const a=this._o(n),o=[],i=D(e,t,s,r,a);if(a.fill){const n=[[e,t],[e+s,t],[e+s,t+r],[e,t+r]];"solid"===a.fillStyle?o.push(v([n],a)):o.push(F([n],a))}return a.stroke!==ae&&o.push(i),this._d("rectangle",o,a)}ellipse(e,t,s,r,n){const a=this._o(n),o=[],i=G(s,r,a),h=b(e,t,a,i);if(a.fill)if("solid"===a.fillStyle){const s=b(e,t,a,i).opset;s.type="fillPath",o.push(s)}else o.push(F([h.estimatedPoints],a));return a.stroke!==ae&&o.push(h.opset),this._d("ellipse",o,a)}circle(e,t,s,r){const n=this.ellipse(e,t,s,s,r);return n.shape="circle",n}linearPath(e,t){const s=this._o(t);return this._d("linearPath",[P(e,!1,s)],s)}arc(e,t,s,r,n,a,o=!1,h){const c=this._o(h),l=[],u=k(e,t,s,r,n,a,o,!0,c);if(o&&c.fill)if("solid"===c.fillStyle){const o=Object.assign({},c);o.disableMultiStroke=!0;const i=k(e,t,s,r,n,a,!0,!1,o);i.type="fillPath",l.push(i)}else l.push(function(e,t,s,r,n,a,o){const h=e,c=t;let l=Math.abs(s/2),u=Math.abs(r/2);l+=z(l*i.RENDERING.ARC_RADIUS_OFFSET_FACTOR,o),u+=z(u*i.RENDERING.ARC_RADIUS_OFFSET_FACTOR,o);let E=n,p=a;for(;E<0;)E+=2*Math.PI,p+=2*Math.PI;p-E>2*Math.PI&&(E=0,p=2*Math.PI);const f=(p-E)/o.curveStepCount,d=[];for(let e=E;e<=p;e+=f)d.push([h+l*Math.cos(e),c+u*Math.sin(e)]);return d.push([h+l*Math.cos(p),c+u*Math.sin(p)]),d.push([h,c]),F([d],o)}(e,t,s,r,n,a,c));return c.stroke!==ae&&l.push(u),this._d("arc",l,c)}curve(e,t){const s=this._o(t),r=[],n=U(e,s);if(s.fill&&s.fill!==ae)if("solid"===s.fillStyle){const t=U(e,Object.assign(Object.assign({},s),{disableMultiStroke:!0,roughness:s.roughness?s.roughness+s.fillShapeRoughnessGain:0}));r.push({type:"fillPath",ops:this._mergedShape(t.ops)})}else{const t=[],n=e;if(n.length){const e="number"==typeof n[0][0]?[n]:n;for(const r of e)r.length<3?t.push(...r):3===r.length?t.push(...ne(J([r[0],r[0],r[1],r[2]]),i.DRAWING.BEZIER_CURVE_POINTS,(1+s.roughness)/2)):t.push(...ne(J(r),i.DRAWING.BEZIER_CURVE_POINTS,(1+s.roughness)/2))}t.length&&r.push(F([t],s))}return s.stroke!==ae&&r.push(n),this._d("curve",r,s)}polygon(e,t){const s=this._o(t),r=[],n=P(e,!0,s);return s.fill&&("solid"===s.fillStyle?r.push(v([e],s)):r.push(F([e],s))),s.stroke!==ae&&r.push(n),this._d("polygon",r,s)}path(e,t){const s=this._o(t),r=[];if(!e)return this._d("path",r,s);e=(e||"").replace(/\n/g," ").replace(/(-\s)/g,"-").replace("/(ss)/g"," ");const n=s.fill&&"transparent"!==s.fill&&s.fill!==ae,a=s.stroke!==ae,o=!!(s.simplification&&s.simplification<1),i=function(e,t,s){const r=A(y(T(e))),n=[];let a=[],o=[0,0],i=[];const h=()=>{i.length>=4&&a.push(...ne(i,t)),i=[]},c=()=>{h(),a.length&&(n.push(a),a=[])};for(const{key:e,data:t}of r)switch(e){case"M":c(),o=[t[0],t[1]],a.push(o);break;case"L":h(),a.push([t[0],t[1]]);break;case"C":if(!i.length){const e=a.length?a[a.length-1]:o;i.push([e[0],e[1]])}i.push([t[0],t[1]]),i.push([t[2],t[3]]),i.push([t[4],t[5]]);break;case"Z":h(),a.push([o[0],o[1]])}if(c(),!s)return n;const l=[];for(const e of n){const t=se(e,s);t.length&&l.push(t)}return l}(e,1,o?4-4*(s.simplification||1):(1+s.roughness)/2),h=H(e,s);if(n)if("solid"===s.fillStyle)if(1===i.length){const t=H(e,Object.assign(Object.assign({},s),{disableMultiStroke:!0,roughness:s.roughness?s.roughness+s.fillShapeRoughnessGain:0}));r.push({type:"fillPath",ops:this._mergedShape(t.ops)})}else r.push(v(i,s));else r.push(F(i,s));return a&&(o?i.forEach((e=>{r.push(P(e,!1,s))})):r.push(h)),this._d("path",r,s)}opsToPath(e,t){let s="";for(const r of e.ops){const e="number"==typeof t&&t>=0?r.data.map((e=>+e.toFixed(t))):r.data;switch(r.op){case"move":s+=`M${e[0]} ${e[1]} `;break;case"bcurveTo":s+=`C${e[0]} ${e[1]}, ${e[2]} ${e[3]}, ${e[4]} ${e[5]} `;break;case"lineTo":s+=`L${e[0]} ${e[1]} `}}return s.trim()}toPaths(e){const t=e.sets||[],s=e.options||this.defaultOptions,r=[];for(const e of t){let t=null;switch(e.type){case"path":t={d:this.opsToPath(e),stroke:s.stroke,strokeWidth:s.strokeWidth,fill:ae};break;case"fillPath":t={d:this.opsToPath(e),stroke:ae,strokeWidth:0,fill:s.fill||ae};break;case"fillSketch":t=this.fillSketch(e,s)}t&&r.push(t)}return r}fillSketch(e,t){let s=t.fillWeight;return s<0&&(s=t.strokeWidth/2),{d:this.opsToPath(e),stroke:t.fill||ae,strokeWidth:s,fill:ae}}_mergedShape(e){return e.filter(((e,t)=>0===t||"move"!==e.op))}}class ie{constructor(e){this.gen=new oe(e),this.instanceId=Math.random().toString(36).substring(2,15),this.registerInstance()}registerInstance(){ie.instanceCount++,"undefined"==typeof FinalizationRegistry||ie.finalizationRegistry||(ie.finalizationRegistry=new FinalizationRegistry((e=>{this.handleInstanceFinalization(e)}))),ie.finalizationRegistry&&ie.finalizationRegistry.register(this,this.instanceId),1===ie.instanceCount&&this.startCleanupTimer()}handleInstanceFinalization(e){ie.instanceCount=Math.max(0,ie.instanceCount-1),0===ie.instanceCount&&ie.cleanupTimer&&(clearInterval(ie.cleanupTimer),ie.cleanupTimer=null,ie.errorCache.clear())}startCleanupTimer(){ie.cleanupTimer&&clearInterval(ie.cleanupTimer),ie.cleanupTimer=setInterval((()=>{this.cleanupErrorCache()}),ie.cacheCleanupInterval)}dispose(){ie.finalizationRegistry&&ie.finalizationRegistry.unregister(this),this.handleInstanceFinalization(this.instanceId)}static forceCleanup(){ie.cleanupTimer&&(clearInterval(ie.cleanupTimer),ie.cleanupTimer=null),ie.errorCache.clear(),ie.instanceCount=0}cleanupErrorCache(){const e=Date.now()-i.ERROR.MS_PER_MINUTE,t=Array.from(ie.errorCache.entries());for(const[s,r]of t)r.lastSeen<e&&ie.errorCache.delete(s);if(ie.errorCache.size>ie.maxErrorCacheSize){const e=Array.from(ie.errorCache.entries()).sort(((e,t)=>e[1].lastSeen-t[1].lastSeen));e.slice(0,e.length-ie.maxErrorCacheSize).forEach((([e])=>ie.errorCache.delete(e)))}}logError(e){if(!this.shouldLog())return;const t=`${e.method}:${e.error||"unknown"}`,s=Date.now(),r=ie.errorCache.get(t);if(r){r.count++,r.lastSeen=s;const e=s-r.context.timestamp;if(r.count/Math.max(1,e/i.ERROR.MS_PER_MINUTE)>ie.errorRateLimit)return}else ie.errorCache.set(t,{count:1,lastSeen:s,context:Object.assign(Object.assign({},e),{timestamp:s})});const n={method:e.method,error:e.error,timestamp:new Date(s).toISOString()};e.parameters&&(n.parameters=this.sanitizeParameters(e.parameters)),r&&r.count>1&&(n.occurrences=r.count),console.warn(`rough-native: ${e.method} failed`,n)}sanitizeParameters(e){if(Array.isArray(e))return{type:"array",length:e.length,sample:e.slice(0,i.ERROR.SAMPLE_SIZE_FOR_ARRAY_PARAMS)};if("object"==typeof e&&null!==e){const t={};for(const[s,r]of Object.entries(e))Array.isArray(r)?t[s]={type:"array",length:r.length}:t[s]="object"==typeof r&&null!==r?{type:"object",keys:Object.keys(r)}:r;return t}return e}shouldLog(){return"undefined"==typeof process||"production"!==process.env.NODE_ENV}draw(e){var t;try{if(!e)return this.logError({method:"draw",error:"drawable is null or undefined"}),this.createEmptyElement();const s=e.sets||[],r=e.options||this.getDefaultOptions(),n=[],a=null===(t=e.options)||void 0===t?void 0:t.fixedDecimalPlaceDigits;for(const t of s){if(!t){this.logError({method:"draw",error:"null drawing in drawable.sets"});continue}let s=null;try{switch(t.type){case"path":{const e=this.safeOpsToPath(t,a);if(!e){this.logError({method:"draw",error:"failed to generate path data",parameters:{drawingType:"path",drawing:t}});continue}s={props:Object.assign(Object.assign({d:e,stroke:r.stroke,strokeWidth:r.strokeWidth,fill:"none"},r.strokeLineDash&&{strokeDasharray:r.strokeLineDash.join(" ").trim()}),r.strokeLineDashOffset&&{strokeDashoffset:r.strokeLineDashOffset})};break}case"fillPath":{const n=this.safeOpsToPath(t,a);if(!n){this.logError({method:"draw",error:"failed to generate fill path data",parameters:{drawingType:"fillPath",drawing:t}});continue}s={props:Object.assign({d:n,stroke:"none",strokeWidth:0,fill:r.fill||""},"curve"===e.shape||"polygon"===e.shape?{fillRule:"evenodd"}:{})};break}case"fillSketch":s=this.safeFillSketch(t,r);break;default:this.logError({method:"draw",error:`unknown drawing type: ${t.type}`,parameters:{drawing:t}});continue}}catch(e){this.logError({method:"draw",error:`failed to process ${t.type} drawing: ${e}`,parameters:{drawingType:t.type,drawing:t}});continue}s&&this.validateElement(s)?n.push(s):s&&this.logError({method:"draw",error:"generated element failed validation",parameters:{element:s,drawingType:t.type}})}return{props:{},children:n}}catch(t){return this.logError({method:"draw",error:`draw method failed: ${t}`,parameters:{drawable:e}}),this.createEmptyElement()}}safeFillSketch(e,t){try{const s=this.safeOpsToPath(e,t.fixedDecimalPlaceDigits);if(!s)return null;let r=t.fillWeight;return r<0&&(r=t.strokeWidth/2),{props:Object.assign(Object.assign({d:s,stroke:t.fill||"",strokeWidth:r,fill:"none"},t.fillLineDash&&{strokeDasharray:t.fillLineDash.join(" ").trim()}),t.fillLineDashOffset&&{strokeDashoffset:t.fillLineDashOffset})}}catch(s){return this.logError({method:"safeFillSketch",error:`fillSketch generation failed: ${s}`,parameters:{drawing:e,options:t}}),null}}safeOpsToPath(e,t){try{return e&&e.ops&&Array.isArray(e.ops)?this.opsToPath(e,t):null}catch(s){return this.logError({method:"safeOpsToPath",error:`opsToPath conversion failed: ${s}`,parameters:{drawing:e,fixedDecimalPlaceDigits:t}}),null}}validateElement(e){return e&&"object"==typeof e&&e.props&&"object"==typeof e.props}createEmptyElement(){return{props:{},children:[]}}validateCoordinates(e){return e.every((e=>"number"==typeof e&&!isNaN(e)&&isFinite(e)))}validateDimensions(...e){return e.every((e=>"number"==typeof e&&!isNaN(e)&&isFinite(e)&&e>=0))}validatePoints(e){return Array.isArray(e)&&e.length>0&&e.every((e=>Array.isArray(e)&&e.length>=i.VALIDATION.MIN_POINT_ARRAY_LENGTH&&this.validateCoordinates([e[0],e[1]])))}validateAngles(e,t){return this.validateCoordinates([e,t])}validateCurvePoints(e){var t;return!(!Array.isArray(e)||0===e.length)&&("number"==typeof(null===(t=e[0])||void 0===t?void 0:t[0])?this.validatePoints(e):e.every((e=>this.validatePoints(e))))}get generator(){return this.gen}getDefaultOptions(){return this.gen.defaultOptions}opsToPath(e,t){return this.gen.opsToPath(e,t)}line(e,t,s,r,n){try{if(!this.validateCoordinates([e,t,s,r]))return this.logError({method:"line",error:"invalid coordinates",parameters:{x1:e,y1:t,x2:s,y2:r,options:n}}),this.createEmptyElement();const a=this.gen.line(e,t,s,r,n);return this.draw(a)}catch(a){return this.logError({method:"line",error:`generation failed: ${a}`,parameters:{x1:e,y1:t,x2:s,y2:r,options:n}}),this.createEmptyElement()}}rectangle(e,t,s,r,n){try{if(!this.validateCoordinates([e,t])||!this.validateDimensions(s,r))return this.logError({method:"rectangle",error:"invalid parameters",parameters:{x:e,y:t,width:s,height:r,options:n}}),this.createEmptyElement();const a=this.gen.rectangle(e,t,s,r,n);return this.draw(a)}catch(a){return this.logError({method:"rectangle",error:`generation failed: ${a}`,parameters:{x:e,y:t,width:s,height:r,options:n}}),this.createEmptyElement()}}ellipse(e,t,s,r,n){try{if(!this.validateCoordinates([e,t])||!this.validateDimensions(s,r))return this.logError({method:"ellipse",error:"invalid parameters",parameters:{x:e,y:t,width:s,height:r,options:n}}),this.createEmptyElement();const a=this.gen.ellipse(e,t,s,r,n);return this.draw(a)}catch(a){return this.logError({method:"ellipse",error:`generation failed: ${a}`,parameters:{x:e,y:t,width:s,height:r,options:n}}),this.createEmptyElement()}}circle(e,t,s,r){try{if(!this.validateCoordinates([e,t])||!this.validateDimensions(s))return this.logError({method:"circle",error:"invalid parameters",parameters:{x:e,y:t,diameter:s,options:r}}),this.createEmptyElement();const n=this.gen.circle(e,t,s,r);return this.draw(n)}catch(n){return this.logError({method:"circle",error:`generation failed: ${n}`,parameters:{x:e,y:t,diameter:s,options:r}}),this.createEmptyElement()}}linearPath(e,t){try{if(!this.validatePoints(e))return this.logError({method:"linearPath",error:"invalid points",parameters:{points:e,options:t}}),this.createEmptyElement();const s=this.gen.linearPath(e,t);return this.draw(s)}catch(s){return this.logError({method:"linearPath",error:`generation failed: ${s}`,parameters:{points:e,options:t}}),this.createEmptyElement()}}polygon(e,t){try{if(!this.validatePoints(e)||e.length<i.VALIDATION.MIN_POLYGON_POINTS)return this.logError({method:"polygon",error:"invalid points (need at least 3 points)",parameters:{points:e,options:t}}),this.createEmptyElement();const s=this.gen.polygon(e,t);return this.draw(s)}catch(s){return this.logError({method:"polygon",error:`generation failed: ${s}`,parameters:{points:e,options:t}}),this.createEmptyElement()}}arc(e,t,s,r,n,a,o=!1,i){try{if(!this.validateCoordinates([e,t])||!this.validateDimensions(s,r)||!this.validateAngles(n,a))return this.logError({method:"arc",error:"invalid parameters",parameters:{x:e,y:t,width:s,height:r,start:n,stop:a,closed:o,options:i}}),this.createEmptyElement();const h=this.gen.arc(e,t,s,r,n,a,o,i);return this.draw(h)}catch(h){return this.logError({method:"arc",error:`generation failed: ${h}`,parameters:{x:e,y:t,width:s,height:r,start:n,stop:a,closed:o,options:i}}),this.createEmptyElement()}}curve(e,t){try{if(!this.validateCurvePoints(e))return this.logError({method:"curve",error:"invalid points",parameters:{points:e,options:t}}),this.createEmptyElement();const s=this.gen.curve(e,t);return this.draw(s)}catch(s){return this.logError({method:"curve",error:`generation failed: ${s}`,parameters:{points:e,options:t}}),this.createEmptyElement()}}path(e,t){try{if(!e||"string"!=typeof e||e.trim().length===i.VALIDATION.MIN_PATH_STRING_LENGTH)return this.logError({method:"path",error:"invalid path data",parameters:{d:e,options:t}}),this.createEmptyElement();const s=this.gen.path(e,t);return this.draw(s)}catch(s){return this.logError({method:"path",error:`generation failed: ${s}`,parameters:{d:e,options:t}}),this.createEmptyElement()}}}ie.errorCache=new Map,ie.maxErrorCacheSize=i.ERROR.MAX_ERROR_CACHE_SIZE,ie.errorRateLimit=i.ERROR.ERROR_RATE_LIMIT_PER_MINUTE,ie.cacheCleanupInterval=i.ERROR.ERROR_CACHE_CLEANUP_INTERVAL_MS,ie.cleanupTimer=null,ie.instanceCount=0,ie.finalizationRegistry=null;const he=new class{constructor(){this.instances=new Map,this.listeners=new Set,this.nextId=0}createInstance(e){const t=`${i.REACT_HOOK.ID_PREFIX}${this.nextId++}`,s=new ie(e);return this.instances.set(t,s),this.notifyListeners(),t}getInstance(e){return this.instances.get(e)}updateInstance(e,t){const s=this.instances.get(e);s&&s.dispose();const r=new ie(t);this.instances.set(e,r),this.notifyListeners()}deleteInstance(e){const t=this.instances.get(e);t&&(t.dispose(),this.instances.delete(e),this.notifyListeners())}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}getSnapshot(){return new Map(this.instances)}notifyListeners(){this.listeners.forEach((e=>e()))}clear(){this.instances.forEach((e=>e.dispose())),this.instances.clear(),this.notifyListeners()}};const ce=new class{constructor(){this.cache=new Map,this.listeners=new Set,this.pendingGenerations=new Set,this.memoryMonitor=le.getInstance()}get maxCacheSize(){return this.memoryMonitor.getRecommendedCacheSize()*i.MEMORY.SHAPE_CACHE_MULTIPLIER}generateShape(e,t){if(this.cache.has(e))return this.cache.get(e);if(this.memoryMonitor.shouldAgressivelyCleanup()&&this.pendingGenerations.size>2)return null;if(this.pendingGenerations.has(e))return null;this.cache.size>=this.maxCacheSize&&this.performMemoryPressureCleanup(),this.pendingGenerations.add(e);try{const s=t();return this.cache.set(e,s),this.pendingGenerations.delete(e),this.notifyListeners(),s}catch(t){if(this.pendingGenerations.delete(e),!this.memoryMonitor.shouldAgressivelyCleanup()){const s={props:{},children:[],error:String(t)};this.cache.set(e,s)}return this.notifyListeners(),{props:{},children:[],error:String(t)}}}performMemoryPressureCleanup(){const e=this.memoryMonitor.getMemoryPressureLevel();if("high"===e){const e=Array.from(this.cache.entries()),t=Math.floor(e.length*i.MEMORY.HIGH_PRESSURE_KEEP_PERCENT),s=e.slice(-t);this.cache.clear(),s.forEach((([e,t])=>this.cache.set(e,t)))}else if("medium"===e){const e=Array.from(this.cache.entries()),t=Math.floor(e.length*i.MEMORY.MEDIUM_PRESSURE_KEEP_PERCENT),s=e.slice(-t);this.cache.clear(),s.forEach((([e,t])=>this.cache.set(e,t)))}}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}getSnapshot(){return new Map(this.cache)}notifyListeners(){this.listeners.forEach((e=>e()))}clearCache(){this.cache.clear(),this.pendingGenerations.clear(),this.notifyListeners()}};class le{constructor(){this.memoryPressureLevel="low",this.lastMemoryCheck=0,this.checkInterval=i.MEMORY.MEMORY_CHECK_INTERVAL_MS}static getInstance(){return le.instance||(le.instance=new le),le.instance}getMemoryPressureLevel(){const e=Date.now();return e-this.lastMemoryCheck>this.checkInterval&&(this.checkMemoryPressure(),this.lastMemoryCheck=e),this.memoryPressureLevel}checkMemoryPressure(){try{if("undefined"!=typeof global&&global.performance&&global.performance.memory){const e=global.performance.memory,t=e.usedJSHeapSize/e.jsHeapSizeLimit;return void(t>i.MEMORY.HIGH_MEMORY_PRESSURE_THRESHOLD?this.memoryPressureLevel="high":t>i.MEMORY.MEDIUM_MEMORY_PRESSURE_THRESHOLD?this.memoryPressureLevel="medium":this.memoryPressureLevel="low")}if("undefined"!=typeof performance&&performance.memory){const e=performance.memory,t=e.usedJSHeapSize/e.jsHeapSizeLimit;return void(t>i.MEMORY.HIGH_MEMORY_PRESSURE_THRESHOLD?this.memoryPressureLevel="high":t>i.MEMORY.MEDIUM_MEMORY_PRESSURE_THRESHOLD?this.memoryPressureLevel="medium":this.memoryPressureLevel="low")}if("undefined"!=typeof global){const e="undefined"!=typeof navigator?navigator.userAgent:"",t=/Android.*4\.|Android.*[0-3]\./i.test(e);return void(this.memoryPressureLevel=t?"high":"medium")}this.memoryPressureLevel="medium"}catch(e){this.memoryPressureLevel="medium"}}getRecommendedCacheSize(){switch(this.memoryPressureLevel){case"high":return i.MEMORY.CACHE_SIZE_HIGH_PRESSURE;case"medium":return i.MEMORY.CACHE_SIZE_MEDIUM_PRESSURE;case"low":return i.MEMORY.CACHE_SIZE_LOW_PRESSURE;default:return i.MEMORY.CACHE_SIZE_DEFAULT}}shouldAgressivelyCleanup(){return"high"===this.memoryPressureLevel}}const ue=new class{constructor(){this.cache=new WeakMap,this.cacheCount=0,this.cacheHits=0,this.cacheMisses=0,this.memoryMonitor=le.getInstance()}get maxCacheSize(){return this.memoryMonitor.getRecommendedCacheSize()}get(e,t){if("object"!=typeof e||"object"!=typeof t||null===e||null===t)return;const s=this.cache.get(e);if(s){const e=s.get(t);if(void 0!==e)return this.cacheHits++,e}this.cacheMisses++}set(e,t,s){if("object"!=typeof e||"object"!=typeof t||null===e||null===t)return;const r=this.maxCacheSize;if(this.cacheCount++,this.cacheCount>r&&(this.cache=new WeakMap,this.cacheCount=0,this.memoryMonitor.shouldAgressivelyCleanup()&&"undefined"!=typeof global&&global.gc))try{global.gc()}catch(e){}let n=this.cache.get(e);if(n||(n=new WeakMap,this.cache.set(e,n)),n.set(t,s),!this.memoryMonitor.shouldAgressivelyCleanup()){let r=this.cache.get(t);r||(r=new WeakMap,this.cache.set(t,r)),r.set(e,s)}}getStats(){return{hits:this.cacheHits,misses:this.cacheMisses,hitRate:this.cacheHits/(this.cacheHits+this.cacheMisses),memoryPressure:this.memoryMonitor.getMemoryPressureLevel(),maxCacheSize:this.maxCacheSize,currentCacheCount:this.cacheCount}}};function Ee(e,t,s=0){if(e===t)return!0;if(s>i.REACT_HOOK.MAX_RECURSION_DEPTH)return!1;const r=typeof e;if(r!==typeof t)return!1;if(null===e||null===t||void 0===e||void 0===t)return e===t;if("object"!==r)return e===t;const n=ue.get(e,t);if(void 0!==n)return n;let a;if(e instanceof Date&&t instanceof Date)a=e.getTime()===t.getTime();else if(Array.isArray(e))if(Array.isArray(t)&&e.length===t.length){a=!0;for(let r=0;r<e.length;r++)if(!Ee(e[r],t[r],s+1)){a=!1;break}}else a=!1;else if(Array.isArray(t))a=!1;else{const r=Object.keys(e),n=Object.keys(t);if(r.length!==n.length)a=!1;else{a=!0;const o=r.length>i.REACT_HOOK.KEY_LOOKUP_SET_THRESHOLD?new Set(n):n;for(const n of r){if(!(Array.isArray(o)?o.includes(n):o.has(n))||!Ee(e[n],t[n],s+1)){a=!1;break}}}}return ue.set(e,t,a),a}function pe(e,s){const r=t.useRef(e),n=t.useRef(e),a=t.useRef(s),o=t.useRef(!0);return t.useMemo((()=>{if(o.current)return o.current=!1,!0;if(!s&&!a.current)return!Ee(e,n.current);if(!s||!a.current||s.length!==a.current.length)return!0;for(let e=0;e<s.length;e++)if(!Ee(s[e],a.current[e]))return!0;return!1}),s||[])&&(n.current=r.current,r.current=e,a.current=s),t.useEffect((()=>()=>{r.current=null,n.current=null,a.current=void 0}),[]),r.current}const fe={getDeepEqualStats:()=>ue.getStats(),clearDeepEqualCache:()=>{ue.cache=new WeakMap},getRoughInstanceCount:()=>he.getSnapshot().size,getShapeCacheSize:()=>ce.getSnapshot().size,clearRoughInstances:()=>he.clear(),clearShapeCache:()=>ce.clearCache(),getMemoryPressure:()=>le.getInstance().getMemoryPressureLevel(),getRecommendedCacheSize:()=>le.getInstance().getRecommendedCacheSize(),forceMemoryCleanup:()=>{if(le.getInstance().shouldAgressivelyCleanup()&&(ce.clearCache(),ue.cache=new WeakMap,"undefined"!=typeof global&&global.gc))try{global.gc()}catch(e){}},clearAllCaches:()=>{ue.cache=new WeakMap,he.clear(),ce.clearCache()}};function de(e){const s=t.useRef(null),r=pe(e,[e]),n=t.useSyncExternalStore(he.subscribe.bind(he),he.getSnapshot.bind(he),he.getSnapshot.bind(he)),a=t.useMemo((()=>(s.current?he.updateInstance(s.current,r):s.current=he.createInstance(r),he.getInstance(s.current))),[r,n]);if(t.useEffect((()=>()=>{s.current&&(he.deleteInstance(s.current),s.current=null)}),[]),!a)throw new Error("Failed to create RoughReactNativeSVG instance");return a}var _e={reactNativeSvg:e=>new ie(e),generator:e=>new oe(e),newSeed:()=>oe.newSeed()};return e.debugUtils=fe,e.default=_e,e.useDeepMemoWithDebug=function(e,s,r){const n=pe(e,s);return t.useEffect((()=>{if("development"===process.env.NODE_ENV&&r)try{const e="undefined"!=typeof performance?performance.memory:void 0;e&&e.usedJSHeapSize&&console.debug(`useDeepMemo[${r}]: ${(e.usedJSHeapSize/i.MEMORY.BYTES_TO_KB/i.MEMORY.KB_TO_MB).toFixed(2)}MB`)}catch(e){}})),n},e.useRough=de,e.useRoughShape=function(e,s,r,n){const a=de(n),o=pe(s,[s]),i=pe(r,[r]),h=t.useMemo((()=>`${e}-${JSON.stringify(o)}-${JSON.stringify(i)}`),[e,o,i]),c=t.useSyncExternalStore(ce.subscribe.bind(ce),ce.getSnapshot.bind(ce),ce.getSnapshot.bind(ce));return t.useMemo((()=>ce.generateShape(h,(()=>{switch(e){case"line":{const[e,t,s,r]=o;return a.line(e,t,s,r,i)}case"rectangle":{const[e,t,s,r]=o;return a.rectangle(e,t,s,r,i)}case"ellipse":{const[e,t,s,r]=o;return a.ellipse(e,t,s,r,i)}case"circle":{const[e,t,s]=o;return a.circle(e,t,s,i)}case"linearPath":{const e=o;return a.linearPath(e,i)}case"polygon":{const e=o;return a.polygon(e,i)}case"arc":{const[e,t,s,r,n,h,c]=o;return a.arc(e,t,s,r,n,h,c||!1,i)}case"curve":{const e=o;return a.curve(e,i)}case"path":{const e=o;return a.path(e,i)}default:throw new Error(`Unknown shape type: ${e}`)}}))),[h,a,e,o,i,c])||{props:{},children:[]}},e.useRoughShapes=function(e,s){const r=de(s),n=pe(e,[e]),a=t.useMemo((()=>`batch-${JSON.stringify(n)}`),[n]),o=t.useSyncExternalStore(ce.subscribe.bind(ce),ce.getSnapshot.bind(ce),ce.getSnapshot.bind(ce));return t.useMemo((()=>ce.generateShape(a,(()=>n.map(((e,t)=>{try{const{type:t,params:s,options:n}=e;switch(t){case"line":{const[e,t,a,o]=s;return r.line(e,t,a,o,n)}case"rectangle":{const[e,t,a,o]=s;return r.rectangle(e,t,a,o,n)}case"ellipse":{const[e,t,a,o]=s;return r.ellipse(e,t,a,o,n)}case"circle":{const[e,t,a]=s;return r.circle(e,t,a,n)}case"linearPath":{const e=s;return r.linearPath(e,n)}case"polygon":{const e=s;return r.polygon(e,n)}case"arc":{const[e,t,a,o,i,h,c]=s;return r.arc(e,t,a,o,i,h,c||!1,n)}case"curve":{const e=s;return r.curve(e,n)}case"path":{const e=s;return r.path(e,n)}default:return{props:{},children:[]}}}catch(e){return{props:{},children:[],error:`Shape ${t} failed: ${String(e)}`}}}))))),[a,r,n,o])||[]},e.useStableRough=function(e){const s=t.useRef(void 0),r=t.useRef(null),n=t.useMemo((()=>!(!e&&!s.current)&&(!e||!s.current||!Ee(e,s.current))),[e]),a=t.useMemo((()=>(r.current&&!n||(r.current&&r.current.dispose(),r.current=new ie(e),s.current=e),r.current)),[e,n]);return t.useEffect((()=>()=>{r.current&&(r.current.dispose(),r.current=null)}),[]),a},Object.defineProperty(e,"__esModule",{value:!0}),e}({},react);