@mylasertools/kerfscript 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +69 -0
- package/LANGUAGE.md +189 -81
- package/README.md +2 -2
- package/dist/assembly.d.cts +2 -1
- package/dist/assembly.d.ts +2 -1
- package/dist/ast.d.cts +6 -0
- package/dist/ast.d.ts +6 -0
- package/dist/bores.d.cts +5 -0
- package/dist/bores.d.ts +5 -0
- package/dist/cli.js +1900 -274
- package/dist/customizer.d.cts +1 -0
- package/dist/customizer.d.ts +1 -0
- package/dist/evaluator.d.cts +9 -0
- package/dist/evaluator.d.ts +9 -0
- package/dist/functions.d.cts +2 -0
- package/dist/functions.d.ts +2 -0
- package/dist/geometry.d.cts +6 -0
- package/dist/geometry.d.ts +6 -0
- package/dist/halving.d.cts +8 -0
- package/dist/halving.d.ts +8 -0
- package/dist/highlight.cjs +2 -1
- package/dist/highlight.js +2 -1
- package/dist/index.cjs +131 -52
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +131 -52
- package/dist/language.cjs +111 -32
- package/dist/language.js +111 -32
- package/dist/libraries.cjs +93 -14
- package/dist/libraries.js +93 -14
- package/dist/plot.d.cts +16 -0
- package/dist/plot.d.ts +16 -0
- package/dist/signatures.cjs +160 -1
- package/dist/signatures.js +160 -1
- package/docs/cli.md +1 -0
- package/docs/compatibility.md +0 -8
- package/docs/customizer.md +18 -0
- package/docs/library.md +42 -1
- package/docs/parts-and-sheets.md +55 -0
- package/docs/versions.md +2 -2
- package/package.json +1 -1
package/dist/libraries.cjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/*! @mylasertools/kerfscript | (c) MyLaserTools | AGPL-3.0-only or commercial licence, see COMMERCIAL-LICENSE.md. Includes Clipper2 (BSL-1.0), opentype.js, tiny-inflate and string.prototype.codepointat (MIT), see THIRD-PARTY-NOTICES.md. */
|
|
2
|
-
"use strict";var B=Object.defineProperty;var z1=Object.getOwnPropertyDescriptor;var f1=Object.getOwnPropertyNames;var u1=Object.prototype.hasOwnProperty;var p1=(a,c)=>{for(var l in c)B(a,l,{get:c[l],enumerable:!0})},g1=(a,c,l,e)=>{if(c&&typeof c=="object"||typeof c=="function")for(let n of f1(c))!u1.call(a,n)&&n!==l&&B(a,n,{get:()=>c[n],enumerable:!(e=z1(c,n))||e.enumerable});return a};var b1=a=>g1(B({},"__esModule",{value:!0}),a);var
|
|
3
|
-
`&&(t++,
|
|
2
|
+
"use strict";var B=Object.defineProperty;var z1=Object.getOwnPropertyDescriptor;var f1=Object.getOwnPropertyNames;var u1=Object.prototype.hasOwnProperty;var p1=(a,c)=>{for(var l in c)B(a,l,{get:c[l],enumerable:!0})},g1=(a,c,l,e)=>{if(c&&typeof c=="object"||typeof c=="function")for(let n of f1(c))!u1.call(a,n)&&n!==l&&B(a,n,{get:()=>c[n],enumerable:!(e=z1(c,n))||e.enumerable});return a};var b1=a=>g1(B({},"__esModule",{value:!0}),a);var E1={};p1(E1,{FOLDER:()=>y,LOCK:()=>C,LibraryError:()=>R,MANIFEST:()=>u,add:()=>$1,check:()=>T1,findProject:()=>X,install:()=>d1,installedPaths:()=>C1,integrity:()=>T,libraryFile:()=>o1,pack:()=>R1,readLock:()=>Y,readManifest:()=>H,remove:()=>P1,resolver:()=>k1});module.exports=b1(E1);var r1=require("node:crypto"),z=require("node:fs"),m=require("node:path"),Z=require("node:zlib");var w1=(a,c=!0)=>`line ${a.line}${c?`, column ${a.col}`:""}${a.file?` in <${a.file}>`:""}`;var k=class extends Error{constructor(l,e){super(e?`${l} (${w1(e)})`:l);this.echoes=[];this.warnings=[];this.trace=[];this.traceMore=0;this.name="KerfError",this.reason=l,e&&(this.line=e.line,this.col=e.col,this.file=e.file)}addFrame(l,e,n){let t=this.trace.find(s=>s.name===e&&s.line===n.line&&s.col===n.col&&s.file===n.file);t?t.count++:this.trace.length>=50?this.traceMore++:this.trace.push({kind:l,name:e,line:n.line,col:n.col,...n.file&&{file:n.file},count:1})}},L1=["<=",">=","==","!=","&&","||","+","-","*","/","%","^","<",">","!","?",":","=",".",",",";","(",")","[","]","{","}","#"];function n1(a,c){let l=[],e=[],n=0,t=1,s=0,h=()=>({line:t,col:n-s+1,offset:n,...c&&{file:c}}),i=o=>{for(let d=0;d<o;d++)a[n]===`
|
|
3
|
+
`&&(t++,s=n+1),n++};for(;n<a.length;){let o=a[n];if(o===" "||o===" "||o==="\r"||o===`
|
|
4
4
|
`){i(1);continue}if(o==="/"&&a[n+1]==="/"){let r=a.indexOf(`
|
|
5
5
|
`,n),v=r<0?a.length:r,g=/^[ \t]*$/.test(a.slice(a.lastIndexOf(`
|
|
6
|
-
`,n-1)+1,n));e.push({text:a.slice(n+2,v).trim(),line:t,block:!1,own:g,start:n,end:v}),n=v;continue}if(o==="/"&&a[n+1]==="*"){let r=
|
|
7
|
-
`;else if(v==="t")r+=" ";else if(v==="u"){let g=a.slice(n+2,n+6);if(!/^[0-9a-fA-F]{4}$/.test(g))throw new
|
|
8
|
-
`)throw new
|
|
6
|
+
`,n-1)+1,n));e.push({text:a.slice(n+2,v).trim(),line:t,block:!1,own:g,start:n,end:v}),n=v;continue}if(o==="/"&&a[n+1]==="*"){let r=h(),v=a.indexOf("*/",n+2);if(v<0)throw new k("Unclosed block comment",r);e.push({text:a.slice(n+2,v).trim(),line:r.line,block:!0,own:!0,start:n,end:v+2}),i(v+2-n);continue}let d=h();if(/[0-9]/.test(o)||o==="."&&/[0-9]/.test(a[n+1]??"")){let r=/^(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?(?:(?:mm|cm|m|in|ft)(?![A-Za-z0-9_]))?/.exec(a.slice(n));l.push({type:"num",value:r[0],pos:d,end:n+r[0].length}),i(r[0].length);continue}if(/[A-Za-z_$]/.test(o)){let r=/^[A-Za-z_$][A-Za-z0-9_]*/.exec(a.slice(n));l.push({type:"id",value:r[0],pos:d,end:n+r[0].length}),i(r[0].length);continue}if(o==='"'){let r="";for(i(1);n<a.length&&a[n]!=='"';){if(a[n]==="\\"){let v=a[n+1];if(v==="n")r+=`
|
|
7
|
+
`;else if(v==="t")r+=" ";else if(v==="u"){let g=a.slice(n+2,n+6);if(!/^[0-9a-fA-F]{4}$/.test(g))throw new k("Bad \\u escape",h());r+=String.fromCharCode(parseInt(g,16)),i(4)}else r+=v;i(2);continue}if(a[n]===`
|
|
8
|
+
`)throw new k("Unclosed string",d);r+=a[n],i(1)}if(n>=a.length)throw new k("Unclosed string",d);i(1),l.push({type:"str",value:r,pos:d,end:n});continue}if(o==="<"&&/^<[A-Za-z0-9_./-]+>/.test(a.slice(n))){let r=l[l.length-1];if(r&&r.type==="id"&&(r.value==="use"||r.value==="include")){let v=/^<([A-Za-z0-9_./-]+)>/.exec(a.slice(n));l.push({type:"str",value:v[1],pos:d,end:n+v[0].length}),i(v[0].length);continue}}let f=L1.find(r=>a.startsWith(r,n));if(!f)throw new k(`Unexpected character "${o}"`,d);l.push({type:"punct",value:f,pos:d,end:n+f.length}),i(f.length)}return l.push({type:"eof",value:"",pos:h(),end:n}),{tokens:l,comments:e}}var K=new Set(["module","function","if","else","for","let","each","use","include","true","false","undef"]);function e1(a,c){let{tokens:l,comments:e}=n1(a,c),n=new D(l),t=[];for(;!n.at("eof");)t.push(...n.statement());return{program:{body:t},comments:e}}var D=class a{constructor(c){this.tokens=c;this.i=0}peek(c=0){return this.tokens[Math.min(this.i+c,this.tokens.length-1)]}at(c,l,e=0){let n=this.peek(e);return n.type===c&&(l===void 0||n.value===l)}isP(c,l=0){return this.at("punct",c,l)}isK(c,l=0){return this.at("id",c,l)}next(){return this.tokens[this.i++]}fail(c,l=this.peek()){let e=l.type==="eof"?"end of file":`"${l.value}"`;throw new k(`${c}, found ${e}`,l.pos)}expectP(c){return this.isP(c)||this.fail(`Expected "${c}"`),this.next()}separator(c){this.isP(c)||(this.isP(",")||this.fail(`Expected "," or "${c}"`),this.next())}ident(){let c=this.peek();return(c.type!=="id"||K.has(c.value))&&this.fail("Expected a name"),this.next()}last(){return this.tokens[this.i-1].end}statement(){let c=this.stmt();for(let l of c)l.end=this.last();return c}stmt(){let c=this.peek();if(this.isP(";"))return this.next(),[];if(this.isP("{")){this.next();let s=[];for(;!this.isP("}");)this.at("eof")&&this.fail('Expected "}"'),s.push(...this.statement());return this.next(),[{kind:"block",body:s,pos:c.pos}]}if(this.isK("module")){this.next();let s=this.ident(),h=this.params();return[{kind:"module",name:s.value,params:h,body:this.body(),pos:c.pos,namePos:s.pos}]}if(this.isK("function")&&this.at("id",void 0,1)){this.next();let s=this.ident(),h=this.params();this.expectP("=");let i=this.expr();return this.expectP(";"),[{kind:"function",name:s.value,params:h,body:i,pos:c.pos,namePos:s.pos}]}if(this.isK("use")||this.isK("include")){this.next();let s=this.peek();return s.type!=="str"&&this.fail("Expected a library path like <kerf/box>"),this.next(),this.isP(";")&&this.next(),[{kind:"use",path:s.value,include:c.value==="include",pos:c.pos}]}if(this.isK("for"))return this.next(),[{kind:"for",assigns:this.assigns(),body:this.body(),pos:c.pos}];if(this.isK("intersection_for")&&this.isP("(",1))return this.next(),[{kind:"for",assigns:this.assigns(),body:this.body(),pos:c.pos,intersect:!0}];if(this.isK("if")){this.next(),this.expectP("(");let s=this.expr();this.expectP(")");let h=this.body(),i=[];return this.isK("else")&&(this.next(),i=this.body()),[{kind:"if",cond:s,then:h,else:i,pos:c.pos}]}if(this.isK("let")&&this.isP("(",1))return this.next(),[{kind:"let",assigns:this.assigns(),body:this.body(),pos:c.pos}];if(c.type==="id"&&this.isP("=",1)){let s=this.ident().value;this.next();let h=this.peek().pos.offset,i=this.expr(),o=this.expectP(";").pos.offset;return[{kind:"assign",name:s,value:i,span:[h,o],pos:c.pos}]}let l=null;(this.isP("*")||this.isP("!")||this.isP("#")||this.isP("%"))&&(l=this.next().value);let e=this.peek();(e.type!=="id"||K.has(e.value))&&this.fail("Expected a statement"),this.next();let n=this.args(),t=this.body();return[{kind:"call",name:e.value,args:n,children:t,modifier:l,pos:e.pos}]}body(){if(this.isP(";"))return this.next(),[];if(this.isP("{")){this.next();let c=[];for(;!this.isP("}");)this.at("eof")&&this.fail('Expected "}"'),c.push(...this.statement());return this.next(),c}return this.statement()}params(){this.expectP("(");let c=[];for(;!this.isP(")");){let l=this.ident(),e=null;this.isP("=")&&(this.next(),e=this.expr()),c.push({name:l.value,default:e,pos:l.pos}),this.separator(")")}return this.next(),c}args(){this.expectP("(");let c=[];for(;!this.isP(")");){if(this.at("id")&&this.isP("=",1)){let l=this.next();this.next(),c.push({name:l.value,value:this.expr(),pos:l.pos})}else c.push({name:null,value:this.expr()});this.separator(")")}return this.next(),c}assigns(){this.expectP("(");let c=[];for(;!this.isP(")");){let l=this.ident();this.expectP("="),c.push({name:l.value,value:this.expr(),pos:l.pos}),this.separator(")")}return this.next(),c}expr(){let c=this.peek();if(this.isK("function")&&this.isP("(",1)){this.next();let e=this.params(),n=this.expr();return{kind:"lambda",params:e,body:n,pos:c.pos,end:this.last()}}if(this.isK("let")&&this.isP("(",1)){this.next();let e=this.assigns(),n=this.expr();return{kind:"let",assigns:e,body:n,pos:c.pos,end:this.last()}}if(this.at("id")&&(c.value==="assert"||c.value==="echo")&&this.isP("(",1)){let e=this.i;this.next();let n={kind:"call",callee:{kind:"var",name:c.value,pos:c.pos},args:this.args(),pos:c.pos};if(this.startsValue())return{kind:"chain",check:n,body:this.expr(),pos:c.pos};this.i=e}let l=this.binary(0);if(this.isP("?")){this.next();let e=this.expr();this.expectP(":");let n=this.expr();return{kind:"ternary",cond:l,then:e,else:n,pos:c.pos}}return l}static{this.LEVELS=[["||"],["&&"],["==","!="],["<","<=",">",">="],["+","-"],["*","/","%"]]}binary(c){if(c>=a.LEVELS.length)return this.unary();let l=this.binary(c+1);for(;this.at("punct")&&a.LEVELS[c].includes(this.peek().value);){let e=this.next(),n=this.binary(c+1);l={kind:"binary",op:e.value,left:l,right:n,pos:e.pos}}return l}startsValue(){let c=this.peek();return c.type==="num"||c.type==="str"||c.type==="id"?!0:this.isP("[")||this.isP("(")||this.isP("{")||this.isP("!")||this.isP("-")}unary(){if((this.isK("let")||this.isK("function"))&&this.isP("(",1))return this.expr();if(this.isP("-")||this.isP("!")||this.isP("+")){let c=this.next();return{kind:"unary",op:c.value,arg:this.unary(),pos:c.pos}}return this.power()}power(){let c=this.postfix();if(this.isP("^")){let l=this.next();return{kind:"binary",op:"^",left:c,right:this.unary(),pos:l.pos}}return c}postfix(){let c=this.primary();for(;;){let l=this.peek();if(this.isP("("))c={kind:"call",callee:c,args:this.args(),pos:c.pos};else if(this.isP("[")){this.next();let e=this.expr();this.expectP("]"),c={kind:"index",target:c,index:e,pos:l.pos}}else if(this.isP("."))this.next(),c={kind:"member",target:c,name:this.ident().value,pos:l.pos};else return c}}primary(){let c=this.peek();if(c.type==="num"){this.next();let l=/(mm|cm|m|in|ft)$/.exec(c.value);return l?{kind:"num",value:Number(c.value.slice(0,-l[1].length)),unit:l[1],pos:c.pos}:{kind:"num",value:Number(c.value),pos:c.pos}}if(c.type==="str")return this.next(),{kind:"str",value:c.value,pos:c.pos};if(c.type==="id")return c.value==="true"||c.value==="false"?(this.next(),{kind:"bool",value:c.value==="true",pos:c.pos}):c.value==="undef"?(this.next(),{kind:"undef",pos:c.pos}):(K.has(c.value)&&this.fail("Expected a value"),this.next(),{kind:"var",name:c.value,pos:c.pos});if(this.isP("(")){this.next();let l=this.expr();return this.expectP(")"),l}if(this.isP("["))return this.list();if(this.isP("{"))return this.record();this.fail("Expected a value")}record(){let c=this.expectP("{"),l=[];for(;!this.isP("}");){let e=this.ident();if(l.some(n=>n.name===e.value))throw new k(`Field ${e.value} is given twice`,e.pos);if(this.expectP("="),l.push({name:e.value,value:this.expr(),pos:e.pos}),!this.isP(","))break;this.next()}return this.expectP("}"),{kind:"record",fields:l,pos:c.pos}}list(){let c=this.expectP("[");if(this.isP("]"))return this.next(),{kind:"list",items:[],pos:c.pos};if(!this.isK("for")&&!this.isK("if")&&!this.isK("each")&&!(this.isK("let")&&this.isP("(",1))&&!(this.isP("(")&&(this.isK("for",1)||this.isK("if",1)))){let l=this.expr();if(this.isP(":")){this.next();let n=null,t=this.expr();return this.isP(":")&&(this.next(),n=t,t=this.expr()),this.expectP("]"),{kind:"range",start:l,step:n,end:t,pos:c.pos}}let e=[{kind:"item",expr:l}];return this.listRest(e,c.pos)}return this.listRest([],c.pos)}listRest(c,l){if(c.length){if(this.isP("]"))return this.next(),{kind:"list",items:c,pos:l};this.separator("]")}for(;!this.isP("]");)c.push(this.listItem()),this.separator("]");return this.next(),{kind:"list",items:c,pos:l}}listItem(){let c=this.peek();if(this.isK("for")&&this.isP("(",1)){this.next();let l=this.assigns(),e=this.listItem();return{kind:"for",assigns:l,body:e,pos:c.pos,end:this.last()}}if(this.isK("if")&&this.isP("(",1)){this.next(),this.expectP("(");let l=this.expr();this.expectP(")");let e=this.listItem(),n=null;return this.isK("else")&&(this.next(),n=this.listItem()),{kind:"if",cond:l,then:e,else:n}}if(this.isK("let")&&this.isP("(",1)){this.next();let l=this.assigns(),e=this.listItem();return{kind:"let",assigns:l,body:e,pos:c.pos,end:this.last()}}if(this.isK("each"))return this.next(),{kind:"each",expr:this.expr()};if(this.isP("(")&&(this.isK("for",1)||this.isK("if",1))){this.next();let l=this.listItem();return this.expectP(")"),l}return{kind:"item",expr:this.expr()}}};var t1={"kerf/box":`// kerf/box: finger-jointed boxes.
|
|
9
9
|
// Copyright (c) 2026 MyLaserTools. AGPL-3.0-only or MyLaserTools commercial licence.
|
|
10
10
|
|
|
11
11
|
// Box from its outer size [width, depth, height] in the current material.
|
|
@@ -275,6 +275,70 @@ module vents(size, slot = 3, pitch = 6) {
|
|
|
275
275
|
translate([0, ends]) circle(d = slot, $fn = 24);
|
|
276
276
|
}
|
|
277
277
|
}
|
|
278
|
+
`,"kerf/fit":`// kerf/fit: drawings scaled to a size and placed by a point of their box.
|
|
279
|
+
// Copyright (c) 2026 MyLaserTools. AGPL-3.0-only or MyLaserTools commercial licence.
|
|
280
|
+
//
|
|
281
|
+
// For any drawing whose size you don't know beforehand: an imported SVG
|
|
282
|
+
// path, a silhouette, a symbol, a line of text. Each measures its children
|
|
283
|
+
// and moves or scales them, keeping their layers, so cut, score and
|
|
284
|
+
// engrave stay together. They are for drawings, not for parts: scaling a
|
|
285
|
+
// part would scale its joints and leave its thickness.
|
|
286
|
+
|
|
287
|
+
// The fractions of a box an anchor names, [0, 0] the bottom left and
|
|
288
|
+
// [1, 1] the top right: "center", "left", "right", "top", "bottom",
|
|
289
|
+
// "top-left", "top-right", "bottom-left", "bottom-right", or [fx, fy]
|
|
290
|
+
function anchor_point(anchor) =
|
|
291
|
+
is_list(anchor) ? anchor
|
|
292
|
+
: let (
|
|
293
|
+
a = lower(anchor),
|
|
294
|
+
fx = contains(a, "left") ? 0 : contains(a, "right") ? 1 : 0.5,
|
|
295
|
+
fy = contains(a, "bottom") ? 0 : contains(a, "top") ? 1 : 0.5
|
|
296
|
+
)
|
|
297
|
+
assert(
|
|
298
|
+
len([for (w = split(a, "-")) if (w != "center" && w != "left" && w != "right" && w != "top" && w != "bottom") w]) == 0,
|
|
299
|
+
str("Unknown anchor \\"", anchor, "\\": use center, left, right, top, bottom or a corner such as top-left")
|
|
300
|
+
)
|
|
301
|
+
[fx, fy];
|
|
302
|
+
|
|
303
|
+
// The children moved so the point of their box that \`anchor\` names is at
|
|
304
|
+
// the origin: align("bottom-left") puts their box's corner there, and
|
|
305
|
+
// align() centres them.
|
|
306
|
+
module align(anchor = "center") {
|
|
307
|
+
f = anchor_point(anchor);
|
|
308
|
+
measure() {
|
|
309
|
+
children();
|
|
310
|
+
if (!is_undef($bounds))
|
|
311
|
+
translate([-($bounds[0] + f[0] * ($bounds[2] - $bounds[0])), -($bounds[1] + f[1] * ($bounds[3] - $bounds[1]))])
|
|
312
|
+
children();
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// The children scaled to \`size\` = [w, h] (or one number for a square):
|
|
317
|
+
// mode = "contain" keeps their proportions and fits them inside, "stretch"
|
|
318
|
+
// fills the size exactly. A drawing with no width or no height (a line)
|
|
319
|
+
// is scaled by the other side. They are then placed by \`anchor\` as
|
|
320
|
+
// align() does, the box's bottom left at the origin by default.
|
|
321
|
+
module fit(size, mode = "contain", anchor = "bottom-left") {
|
|
322
|
+
s = is_list(size) ? size : [size, size];
|
|
323
|
+
assert(s[0] > 0 && s[1] > 0, "fit(): size must be more than 0");
|
|
324
|
+
assert(mode == "contain" || mode == "stretch", "fit(): mode is \\"contain\\" or \\"stretch\\"");
|
|
325
|
+
f = anchor_point(anchor);
|
|
326
|
+
measure() {
|
|
327
|
+
children();
|
|
328
|
+
if (!is_undef($bounds)) {
|
|
329
|
+
w = $bounds[2] - $bounds[0];
|
|
330
|
+
h = $bounds[3] - $bounds[1];
|
|
331
|
+
kx = w > 0 ? s[0] / w : undef;
|
|
332
|
+
ky = h > 0 ? s[1] / h : undef;
|
|
333
|
+
k = mode == "stretch"
|
|
334
|
+
? [is_undef(kx) ? 1 : kx, is_undef(ky) ? 1 : ky]
|
|
335
|
+
: let (one = is_undef(kx) ? ky : is_undef(ky) ? kx : min(kx, ky)) [one, one];
|
|
336
|
+
if (!is_undef(k[0]))
|
|
337
|
+
translate([-f[0] * w * k[0], -f[1] * h * k[1]])
|
|
338
|
+
scale(k) translate([-$bounds[0], -$bounds[1]]) children();
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
278
342
|
`,"kerf/gears":`// kerf/gears: gears, racks, lantern gears and cams for moving models.
|
|
279
343
|
// Copyright (c) 2026 MyLaserTools. AGPL-3.0-only or MyLaserTools commercial licence.
|
|
280
344
|
//
|
|
@@ -1568,14 +1632,29 @@ function size_names(table) = join([for (e = table) e[0]], ", ");
|
|
|
1568
1632
|
// symbol_names() lists them: "heart", "star", "paw", "tree" and the like.
|
|
1569
1633
|
// box = "grid" sizes and places it by its icon set's grid (24 x 24 for
|
|
1570
1634
|
// Material Design Icons) instead, so icons of one set line up as drawn.
|
|
1571
|
-
|
|
1635
|
+
// solid = true cuts it as one silhouette, gaps narrower than 2 * close
|
|
1636
|
+
// closed and its holes filled, with the drawing's detail engraved on it:
|
|
1637
|
+
// a pictogram drawn as separate strokes (a moon and stars, yarn) cut out
|
|
1638
|
+
// in one piece. bridge = w also joins pieces too far apart to close, by
|
|
1639
|
+
// strips w wide.
|
|
1640
|
+
module symbol(name, size = 10, center = true, fill = "nonzero", box = "drawn", solid = false, close = undef, bridge = undef) {
|
|
1572
1641
|
assert(box == "drawn" || box == "grid", "box is \\"drawn\\" or \\"grid\\"");
|
|
1573
1642
|
s = symbol_entry(name);
|
|
1574
1643
|
vb = box == "grid" ? s[5] : s[2];
|
|
1575
1644
|
k = size / max(vb[2], vb[3]);
|
|
1576
|
-
translate(center ? [0, 0] : [vb[2] * k / 2, vb[3] * k / 2])
|
|
1645
|
+
module drawn() translate(center ? [0, 0] : [vb[2] * k / 2, vb[3] * k / 2])
|
|
1577
1646
|
scale([k, -k]) translate([-vb[0] - vb[2] / 2, -vb[1] - vb[3] / 2])
|
|
1578
1647
|
path(s[4], fill = fill, close = true);
|
|
1648
|
+
r = is_undef(close) ? size / 30 : close;
|
|
1649
|
+
module silhouette() if (is_undef(bridge)) fill_holes() close(r) drawn();
|
|
1650
|
+
else join_islands(bridge) fill_holes() close(r) drawn();
|
|
1651
|
+
if (solid) {
|
|
1652
|
+
silhouette();
|
|
1653
|
+
engrave() difference() {
|
|
1654
|
+
silhouette();
|
|
1655
|
+
drawn();
|
|
1656
|
+
}
|
|
1657
|
+
} else drawn();
|
|
1579
1658
|
}
|
|
1580
1659
|
|
|
1581
1660
|
// [width, height] of what a symbol draws when it is \`size\` across its
|
|
@@ -2556,10 +2635,10 @@ module tab_rect(size, edges = true, count = undef, width = undef) {
|
|
|
2556
2635
|
if (e[2]) translate([w, h]) rotate(180) tabs(w, count, width);
|
|
2557
2636
|
if (e[3]) translate([0, h]) rotate(270) tabs(h, count, width);
|
|
2558
2637
|
}
|
|
2559
|
-
`};var P="0.3";var u="kerf.json",
|
|
2560
|
-
`));c.update(`${l}\0${e.length}\0`),c.update(e)}return"sha256-"+c.digest("base64")}function h1(a){return a.kind==="folder"?"folder:"+(process.platform==="win32"?a.path.toLowerCase():a.path):a.kind==="github"?`github:${a.owner}/${a.repo}`.toLowerCase()+`@${a.ref}`:a.url}function
|
|
2561
|
-
`};async function
|
|
2562
|
-
`)&&
|
|
2563
|
-
`)&&(0,z.writeFileSync)((0,m.join)(a,
|
|
2564
|
-
`);try{return await
|
|
2565
|
-
`));let i=[...t.keys()].sort().map(
|
|
2638
|
+
`};var P="0.3";var u="kerf.json",C="kerf.lock",y="kerf_libraries",_=20<<20,I=2e3,W=/^[a-z0-9][a-z0-9_-]{0,63}$/,y1=/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/,A1=/^(readme|licen[cs]e|copying|notice|changelog)(\.[a-z]+)?$/i,R=class extends Error{},U=a=>{try{return(0,z.statSync)(a).isFile()}catch{return!1}},V=a=>{try{return(0,z.statSync)(a).isDirectory()}catch{return!1}},S=(a,c)=>a&&Object.prototype.hasOwnProperty.call(a,c)?a[c]:void 0,M=a=>{throw new R(a)};function s1(a,c){return(typeof a!="string"||!W.test(a))&&M(`${c} "${a}" is not a library name: use lowercase letters, digits, - and _`),a==="kerf"&&M(`${c}: "kerf" is the standard library's name`),a}function Q(a,c){(!a||typeof a!="object"||Array.isArray(a))&&M(`${c} must hold a JSON object`);let l=a;for(let n of["name","version","description","license","language"])l[n]!==void 0&&typeof l[n]!="string"&&M(`${c}: "${n}" must be a string`);l.name!==void 0&&s1(l.name,`${c}: name`),l.version!==void 0&&!y1.test(l.version)&&M(`${c}: "version" must look like 1.2.0, not "${l.version}"`),l.language!==void 0&&!/^\d+\.\d+$/.test(l.language)&&M(`${c}: "language" must look like ${P}, not "${l.language}"`),l.sheet!==void 0&&!(Array.isArray(l.sheet)&&l.sheet.length===2&&l.sheet.every(n=>typeof n=="number"&&Number.isFinite(n)&&n>0))&&M(`${c}: "sheet" must be [width, height] in mm, like [600, 400]`);let e=l.libraries??{};(!e||typeof e!="object"||Array.isArray(e))&&M(`${c}: "libraries" must be an object of name: source`);for(let[n,t]of Object.entries(e))s1(n,`${c}: library`),typeof t!="string"&&M(`${c}: the source of ${n} must be a string`);return{...l,libraries:{...e}}}function J(a,c){let l;try{l=(0,z.readFileSync)(a,"utf8")}catch{return M(`${c} was not found`)}try{return JSON.parse(l)}catch(e){return M(`${c} is not valid JSON: ${e.message}`)}}function H(a,c=u){return Q(J((0,m.join)(a,u),c),c)}function Y(a){if(!(0,z.existsSync)((0,m.join)(a,C)))return;let c=J((0,m.join)(a,C),C);(c?.lockVersion!==1||!c.libraries||typeof c.libraries!="object")&&M(`${C} is not a kerf.lock this kerf can read. Delete it and run kerf install.`);for(let[l,e]of Object.entries(c.libraries))(!e||["source","resolved","integrity"].some(n=>typeof e[n]!="string"))&&M(`${C}: the entry for ${l} is damaged. Remove it and run kerf install.`);return c}function X(a){for(let c=(0,m.resolve)(a);;){if(U((0,m.join)(c,u)))return c;let l=(0,m.dirname)(c);if(l===c)return;c=l}}function c1(a){try{return H(a).name}catch{return}}function o1(a,c,l=U){let e=(s,h)=>[(0,m.join)(s,h),(0,m.join)(s,h+".kerf")].find(i=>l(i));if((0,m.isAbsolute)(c))return[c,c+".kerf"].find(s=>l(s));let n=c.indexOf("/"),t=n>0?X(a):void 0;if(t){let s=c.slice(0,n);if(s===c1(t))return e(t,c.slice(n+1));if(V((0,m.join)(t,y,s)))return e((0,m.join)(t,y),c)}return e(a,c)}function k1(a){let c=(0,m.dirname)((0,m.resolve)(a));return l=>{let e=o1(c,l);return e===void 0?void 0:(0,z.readFileSync)(e,"utf8")}}function C1(a,c=200){let l=X(a);if(!l)return[];let e=[],n=(h,i)=>{for(let o of[...v1(h,3)].sort()){if(e.length>=c)return;o.endsWith(".kerf")&&e.push(i+o.slice(0,-5))}},t=c1(l);t&&n(l,t+"/");let s=(0,m.join)(l,y);if(V(s))for(let h of(0,z.readdirSync)(s).sort())W.test(h)&&V((0,m.join)(s,h))&&n((0,m.join)(s,h),h+"/");return e}function v1(a,c=1/0){let l=[],e=(n,t,s)=>{let h;try{h=(0,z.readdirSync)(n,{withFileTypes:!0})}catch{return}for(let i of h){if(i.name.startsWith(".")||i.name==="node_modules"||i.name===y)continue;let o=t?`${t}/${i.name}`:i.name;i.isDirectory()?s<c&&e((0,m.join)(n,i.name),o,s+1):i.isFile()&&(l.push(o),l.length>I&&M(`${a} has more than ${I} files`))}};return e(a,"",0),l}function O(a,c=()=>!0){let l=new Map;for(let e of v1(a))c(e)&&l.set(e,(0,z.readFileSync)((0,m.join)(a,...e.split("/"))));return l}var l1=a=>a===u||a.endsWith(".kerf")||!a.includes("/")&&A1.test(a);function T(a){let c=(0,r1.createHash)("sha256");for(let l of[...a.keys()].sort()){let e=Buffer.from(a.get(l).toString("utf8").replace(/\r\n/g,`
|
|
2639
|
+
`));c.update(`${l}\0${e.length}\0`),c.update(e)}return"sha256-"+c.digest("base64")}function h1(a){return a.kind==="folder"?"folder:"+(process.platform==="win32"?a.path.toLowerCase():a.path):a.kind==="github"?`github:${a.owner}/${a.repo}`.toLowerCase()+`@${a.ref}`:a.url}function E(a,c){let l=/^github:([\w.-]+)\/([\w.-]+?)(?:\.git)?(?:@(\S+))?$/.exec(a);if(l)return{kind:"github",owner:l[1],repo:l[2],ref:l[3]??"HEAD"};if(/^https?:\/\//i.test(a)){let n;try{n=new URL(a)}catch{return M(`${a} is not a link`)}return n.protocol==="http:"&&!["localhost","127.0.0.1","[::1]"].includes(n.hostname)&&M(`Use https:// to download ${a}`),{kind:"url",url:a}}let e=a.startsWith("file:")?a.slice(5):a;return e.startsWith("./")||e.startsWith("../")||(0,m.isAbsolute)(e)?{kind:"folder",path:(0,m.resolve)(c,e)}:M(`"${a}" is not a library source. Use github:owner/repo@tag, an https:// link to a .tar.gz, or a folder like ./my-library`)}async function H1(a,c){let l;try{l=await c(a)}catch(h){let i=h;return M(`Could not download ${a}: ${i.cause?.message??i.message}`)}l.ok||M(`Could not download ${a}: HTTP ${l.status}`+(l.status===404?". Check the name and the tag, and that it is public.":""));let e=()=>M(`${a} is larger than ${_>>20} MB`);if(Number(l.headers?.get("content-length"))>_&&e(),!l.body){let h=Buffer.from(await l.arrayBuffer());return h.length>_?e():h}let n=l.body.getReader(),t=[],s=0;for(;;){let{done:h,value:i}=await n.read();if(h||!i)break;s+=i.length,s>_&&(await n.cancel().catch(()=>{}),e()),t.push(i)}return Buffer.concat(t)}function V1(a,c){let l;try{l=(0,Z.gunzipSync)(a,{maxOutputLength:5*_})}catch{return M(`${c} is not a .tar.gz file`)}let e=new Map,n,t={},s;for(let h=0;h+512<=l.length;){let i=l.subarray(h,h+512);if(i.every(b=>b===0))break;let o=(b,A)=>{let L=i.subarray(b,b+A),q=L.indexOf(0);return L.subarray(0,q<0?A:q).toString("utf8")},d=parseInt(o(124,12).trim()||"0",8);d>=0||M(`${c} is damaged`);let f=o(156,1)||"0",r=l.subarray(h+512,h+512+d);if(h+=512+Math.ceil(d/512)*512,f==="x"||f==="g"){let b=q1(r);f==="g"?n=b.comment??n:t=b;continue}if(f==="L"){s=r.toString("utf8").replace(/\0+$/,"");continue}let v=o(345,155),g=t.path??s??(v?v+"/":"")+o(0,100);t={},s=void 0,(f==="0"||f==="7")&&e.set(g,Buffer.from(r)),e.size>I&&M(`${c} has more than ${I} files`)}return{files:e,commit:n}}function q1(a){let c={};for(let l=0;l<a.length;){let e=a.indexOf(32,l),n=parseInt(a.subarray(l,e).toString(),10);if(!(n>0)||e<0)break;let t=a.subarray(e+1,l+n-1).toString("utf8"),s=t.indexOf("=");s>0&&(c[t.slice(0,s)]=t.slice(s+1)),l+=n}return c}function i1(a,c){let l=new Map;for(let[t,s]of a){let h=t.replace(/^\.\//,""),i=h.split("/");((0,m.isAbsolute)(h)||h.includes("\\")||h.includes(":")||i.some(o=>o===".."||o===""))&&M(`${c} holds a file outside its folder: ${t}`),l.set(h,s)}let e="";if(!l.has(u)){let t=new Set([...l.keys()].map(s=>s.split("/")[0]));t.size===1&&(e=[...t][0]+"/")}let n=new Map;for(let[t,s]of l){if(!t.startsWith(e))continue;let h=t.slice(e.length);h.split("/").some(i=>i.startsWith(".")||i===y||i==="node_modules")||l1(h)&&n.set(h,s)}return n.has(u)||M(`${c} has no kerf.json at its top. Is it a KerfScript library?`),n}var x1=(a,c)=>{let[l,e]=a.split(".").map(Number),[n,t]=c.split(".").map(Number);return l>n||l===n&&e>t};async function m1(a,c,l,e,n){let t=E(a,c),s,h;if(t.kind==="folder")U((0,m.join)(t.path,u))||M(`${a}: no kerf.json in ${t.path}. Is it a KerfScript library?`),s=i1(O(t.path,l1),a),h=(0,m.relative)(l,t.path).split(m.sep).join("/")||".",!h.startsWith(".")&&!(0,m.isAbsolute)(h)&&(h="./"+h);else{let o=t.kind==="github"?`https://github.com/${t.owner}/${t.repo}/archive/${t.ref}.tar.gz`:t.url,d=n.get(o)??await H1(o,e);n.set(o,d);let f=V1(d,a);s=i1(f.files,a),h=t.kind==="github"&&/^[0-9a-f]{40}$/.test(f.commit??"")?`https://github.com/${t.owner}/${t.repo}/archive/${f.commit}.tar.gz`:o}let i=Q(JSON.parse(s.get(u).toString("utf8")),`the kerf.json of ${a}`);return i.name||M(`${a}: its kerf.json has no "name", so it is not a library`),i.language&&x1(i.language,P)&&M(`${i.name} needs KerfScript language ${i.language} or later, and this kerf has ${P}. Update kerf.`),{manifest:i,files:s,resolved:h,integrity:T(s)}}function _1(a,c){(0,z.rmSync)(a,{recursive:!0,force:!0});for(let[l,e]of c){let n=(0,m.join)(a,...l.split("/"));(0,z.mkdirSync)((0,m.dirname)(n),{recursive:!0}),(0,z.writeFileSync)(n,e)}}var S1=a=>{let c=[...a].sort(([l],[e])=>l<e?-1:1);return JSON.stringify({lockVersion:1,libraries:Object.fromEntries(c)},null,2)+`
|
|
2640
|
+
`};async function d1(a,c={}){let l=c.fetch??globalThis.fetch,e=c.log??(()=>{}),n=c.cache??new Map,t=H(a),s=Y(a),h=new Map,i=Object.entries(t.libraries).map(([r,v])=>({name:r,source:v,base:a,from:u})),o=(0,m.join)(a,y);for(;i.length;){let{name:r,source:v,base:g,from:b}=i.shift(),A=E(v,g),L=A.kind==="folder",q=L?`${v} (${A.path})`:v,j=h.get(r);if(j){j.at!==h1(A)&&!S(t.libraries,r)&&M(`${b} uses ${r} from ${q}, and ${j.from} uses it from ${j.shown}. Add ${r} to kerf.json with the source to use.`);continue}let G=S(s?.libraries,r),w=G?.source===v&&(!L||(0,m.resolve)(a,G.resolved)===A.path)?G:void 0;c.frozen&&!w&&M(`kerf.lock does not pin ${r} from ${v}. Run kerf install without --frozen.`);let $=(0,m.join)(o,r),x;if(w&&!L&&V($)&&T(O($))===w.integrity)x=w;else{let p=await m1(w&&!L?w.resolved:v,L?g:a,a,l,n);p.manifest.name!==r&&M(`${v} is the library "${p.manifest.name}", so ${b} must call it ${p.manifest.name}, not ${r}`),w&&p.integrity!==w.integrity&&(!L||c.frozen)&&M(L?`${r} in ${v} changed since kerf.lock pinned it. Run kerf install without --frozen.`:`${r} from ${w.resolved} no longer matches kerf.lock. If you trust the change, remove ${r} from kerf.lock and run kerf install again.`),_1($,p.files),x={source:v,...p.manifest.version?{version:p.manifest.version}:{},resolved:p.resolved,integrity:p.integrity},(!w||w.integrity!==p.integrity||!L)&&e(`${w?"Restored":"Installed"} ${r}${x.version?" "+x.version:""} from ${v}`)}h.set(r,{entry:x,at:h1(A),shown:q,from:b});let a1=A.kind==="folder"?A.path:void 0;for(let[p,F]of Object.entries(H($,`${y}/${r}/${u}`).libraries))E(F,a).kind==="folder"&&!a1&&M(`${r} uses ${p} from the folder ${F}: a published library needs github: or https: sources`),i.push({name:p,source:F,base:a1??a,from:r})}let d=S1(new Map([...h].map(([r,v])=>[r,v.entry]))),f=(0,z.existsSync)((0,m.join)(a,C))?(0,z.readFileSync)((0,m.join)(a,C),"utf8"):"";if(c.frozen&&d!==f.replace(/\r\n/g,`
|
|
2641
|
+
`)&&M("kerf.lock is out of date. Run kerf install without --frozen."),V(o))for(let r of(0,z.readdirSync)(o,{withFileTypes:!0}))r.isDirectory()&&W.test(r.name)&&!h.has(r.name)&&((0,z.rmSync)((0,m.join)(o,r.name),{recursive:!0,force:!0}),e(`Removed ${r.name}`));return d!==f.replace(/\r\n/g,`
|
|
2642
|
+
`)&&(0,z.writeFileSync)((0,m.join)(a,C),d),JSON.parse(d)}function T1(a){let c=H(a),l=Y(a),e=Object.keys(c.libraries);if(!l)return e.length?["kerf.lock is missing. Run kerf install."]:[];let n=[],t=new Set,s=[...e];for(;s.length;){let h=s.shift();if(t.has(h))continue;t.add(h);let i=S(l.libraries,h);if(!i){n.push(`${h} is not in kerf.lock. Run kerf install.`);continue}e.includes(h)&&i.source!==c.libraries[h]&&n.push(`kerf.json takes ${h} from ${c.libraries[h]}, and kerf.lock from ${i.source}. Run kerf install.`);let o=(0,m.join)(a,y,h);if(!V(o)){n.push(`${h} is not in ${y}. Run kerf install.`);continue}T(O(o))!==i.integrity&&n.push(`The files in ${y}/${h} differ from kerf.lock. Run kerf install.`);try{s.push(...Object.keys(H(o).libraries))}catch(d){n.push(d.message)}}for(let h of Object.keys(l.libraries))t.has(h)||n.push(`kerf.lock pins ${h}, which nothing uses any more.`);return n}function j1(a,c){let l=(0,m.join)(a,u),e=(0,z.existsSync)(l)?J(l,u):{};Q(e,u);let n={...e.libraries??{}};return c(n),e.libraries=n,{file:l,before:(0,z.existsSync)(l)?(0,z.readFileSync)(l,"utf8"):void 0,raw:e}}async function M1(a,c,l){let{file:e,before:n,raw:t}=j1(a,c);(0,z.writeFileSync)(e,JSON.stringify(t,null,2)+`
|
|
2643
|
+
`);try{return await d1(a,l)}catch(s){throw n===void 0?(0,z.rmSync)(e,{force:!0}):(0,z.writeFileSync)(e,n),s}}async function $1(a,c,l={}){let e=new Map,t=(await m1(c,a,a,l.fetch??globalThis.fetch,e)).manifest.name;t===c1(a)&&M(`This project is the library ${t}; it cannot use itself`);let s=await M1(a,h=>void(h[t]=c),{...l,cache:e});return{name:t,lock:s}}async function P1(a,c,l={}){return M1(a,e=>{S(e,c)===void 0&&M(`kerf.json has no library named ${c}`),delete e[c]},l)}function N(a,c=[]){if(Array.isArray(a))for(let l of a)N(l,c);else if(a&&typeof a=="object"){let l=a;if(l.kind==="use"&&typeof l.path=="string")c.push({path:l.path,line:l.pos.line});else for(let e of Object.values(l))N(e,c)}return c}function I1(a){let c=[];for(let[l,e]of a){let n=Buffer.alloc(512),t=l,s="";if(Buffer.byteLength(t)>100){let i=l.lastIndexOf("/",155);(i<0||Buffer.byteLength(l.slice(i+1))>100)&&M(`${l}: the path is too long to pack`),s=l.slice(0,i),t=l.slice(i+1)}n.write(t,0,100),n.write("0000644\0",100),n.write("0000000\0",108),n.write("0000000\0",116),n.write(e.length.toString(8).padStart(11,"0")+"\0",124),n.write("00000000000\0",136),n.fill(32,148,156),n.write("0",156),n.write("ustar\0",257),n.write("00",263),n.write(s,345,155);let h=0;for(let i of n)h+=i;n.write(h.toString(8).padStart(6,"0")+"\0 ",148),c.push(n,e,Buffer.alloc((512-e.length%512)%512))}return c.push(Buffer.alloc(1024)),(0,Z.gzipSync)(Buffer.concat(c),{level:9})}function R1(a,c){let l=H(a),e=l.name??M('kerf.json needs a "name" to pack a library'),n=l.version??M('kerf.json needs a "version", like 1.0.0'),t=O(a,l1),s=[];for(let[d,f]of Object.entries(l.libraries))E(f,a).kind==="folder"&&s.push(`kerf.json: ${d} comes from a folder; a published library needs github: or https: sources`);let h=d=>t.has(d)||t.has(d+".kerf");for(let[d,f]of t){if(!d.endsWith(".kerf"))continue;let r;try{r=e1(f.toString("utf8"),`${e}/${d}`).program}catch(v){s.push(`${d}: ${v.message}`);continue}for(let v of N(r)){let g=v.path.split("/")[0];Object.prototype.hasOwnProperty.call(t1,v.path)||g===e&&h(v.path.slice(e.length+1))||v.path.includes("/")&&S(l.libraries,g)!==void 0||s.push(`${d}, line ${v.line}: <${v.path}> will not be found once the library is installed`+(h(v.path)?`. Write <${e}/${v.path}> for the library's own file.`:"."))}}[...t.keys()].some(d=>d.endsWith(".kerf"))||s.push("There are no .kerf files to pack"),s.length&&M(s.join(`
|
|
2644
|
+
`));let i=[...t.keys()].sort().map(d=>[`${e}/${d}`,t.get(d)]),o=c??(0,m.join)(a,`${e}-${n}.tar.gz`);return(0,z.writeFileSync)(o,I1(i)),{file:o,name:e,version:n,files:t.size,integrity:T(t)}}0&&(module.exports={FOLDER,LOCK,LibraryError,MANIFEST,add,check,findProject,install,installedPaths,integrity,libraryFile,pack,readLock,readManifest,remove,resolver});
|
package/dist/libraries.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/*! @mylasertools/kerfscript | (c) MyLaserTools | AGPL-3.0-only or commercial licence, see COMMERCIAL-LICENSE.md. Includes Clipper2 (BSL-1.0), opentype.js, tiny-inflate and string.prototype.codepointat (MIT), see THIRD-PARTY-NOTICES.md. */
|
|
2
|
-
import{createHash as b1}from"node:crypto";import{existsSync as I,mkdirSync as w1,readdirSync as Q,readFileSync as T,rmSync as J,statSync as m1,writeFileSync as _}from"node:fs";import{dirname as Y,isAbsolute as
|
|
3
|
-
`&&(t++,
|
|
2
|
+
import{createHash as b1}from"node:crypto";import{existsSync as I,mkdirSync as w1,readdirSync as Q,readFileSync as T,rmSync as J,statSync as m1,writeFileSync as _}from"node:fs";import{dirname as Y,isAbsolute as Z,join as M,relative as L1,resolve as O,sep as y1}from"node:path";import{gunzipSync as A1,gzipSync as k1}from"node:zlib";var p1=(a,c=!0)=>`line ${a.line}${c?`, column ${a.col}`:""}${a.file?` in <${a.file}>`:""}`;var y=class extends Error{constructor(l,e){super(e?`${l} (${p1(e)})`:l);this.echoes=[];this.warnings=[];this.trace=[];this.traceMore=0;this.name="KerfError",this.reason=l,e&&(this.line=e.line,this.col=e.col,this.file=e.file)}addFrame(l,e,n){let t=this.trace.find(s=>s.name===e&&s.line===n.line&&s.col===n.col&&s.file===n.file);t?t.count++:this.trace.length>=50?this.traceMore++:this.trace.push({kind:l,name:e,line:n.line,col:n.col,...n.file&&{file:n.file},count:1})}},g1=["<=",">=","==","!=","&&","||","+","-","*","/","%","^","<",">","!","?",":","=",".",",",";","(",")","[","]","{","}","#"];function s1(a,c){let l=[],e=[],n=0,t=1,s=0,h=()=>({line:t,col:n-s+1,offset:n,...c&&{file:c}}),i=o=>{for(let m=0;m<o;m++)a[n]===`
|
|
3
|
+
`&&(t++,s=n+1),n++};for(;n<a.length;){let o=a[n];if(o===" "||o===" "||o==="\r"||o===`
|
|
4
4
|
`){i(1);continue}if(o==="/"&&a[n+1]==="/"){let r=a.indexOf(`
|
|
5
5
|
`,n),v=r<0?a.length:r,p=/^[ \t]*$/.test(a.slice(a.lastIndexOf(`
|
|
6
|
-
`,n-1)+1,n));e.push({text:a.slice(n+2,v).trim(),line:t,block:!1,own:p,start:n,end:v}),n=v;continue}if(o==="/"&&a[n+1]==="*"){let r=
|
|
7
|
-
`;else if(v==="t")r+=" ";else if(v==="u"){let p=a.slice(n+2,n+6);if(!/^[0-9a-fA-F]{4}$/.test(p))throw new
|
|
8
|
-
`)throw new
|
|
6
|
+
`,n-1)+1,n));e.push({text:a.slice(n+2,v).trim(),line:t,block:!1,own:p,start:n,end:v}),n=v;continue}if(o==="/"&&a[n+1]==="*"){let r=h(),v=a.indexOf("*/",n+2);if(v<0)throw new y("Unclosed block comment",r);e.push({text:a.slice(n+2,v).trim(),line:r.line,block:!0,own:!0,start:n,end:v+2}),i(v+2-n);continue}let m=h();if(/[0-9]/.test(o)||o==="."&&/[0-9]/.test(a[n+1]??"")){let r=/^(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?(?:(?:mm|cm|m|in|ft)(?![A-Za-z0-9_]))?/.exec(a.slice(n));l.push({type:"num",value:r[0],pos:m,end:n+r[0].length}),i(r[0].length);continue}if(/[A-Za-z_$]/.test(o)){let r=/^[A-Za-z_$][A-Za-z0-9_]*/.exec(a.slice(n));l.push({type:"id",value:r[0],pos:m,end:n+r[0].length}),i(r[0].length);continue}if(o==='"'){let r="";for(i(1);n<a.length&&a[n]!=='"';){if(a[n]==="\\"){let v=a[n+1];if(v==="n")r+=`
|
|
7
|
+
`;else if(v==="t")r+=" ";else if(v==="u"){let p=a.slice(n+2,n+6);if(!/^[0-9a-fA-F]{4}$/.test(p))throw new y("Bad \\u escape",h());r+=String.fromCharCode(parseInt(p,16)),i(4)}else r+=v;i(2);continue}if(a[n]===`
|
|
8
|
+
`)throw new y("Unclosed string",m);r+=a[n],i(1)}if(n>=a.length)throw new y("Unclosed string",m);i(1),l.push({type:"str",value:r,pos:m,end:n});continue}if(o==="<"&&/^<[A-Za-z0-9_./-]+>/.test(a.slice(n))){let r=l[l.length-1];if(r&&r.type==="id"&&(r.value==="use"||r.value==="include")){let v=/^<([A-Za-z0-9_./-]+)>/.exec(a.slice(n));l.push({type:"str",value:v[1],pos:m,end:n+v[0].length}),i(v[0].length);continue}}let z=g1.find(r=>a.startsWith(r,n));if(!z)throw new y(`Unexpected character "${o}"`,m);l.push({type:"punct",value:z,pos:m,end:n+z.length}),i(z.length)}return l.push({type:"eof",value:"",pos:h(),end:n}),{tokens:l,comments:e}}var D=new Set(["module","function","if","else","for","let","each","use","include","true","false","undef"]);function h1(a,c){let{tokens:l,comments:e}=s1(a,c),n=new N(l),t=[];for(;!n.at("eof");)t.push(...n.statement());return{program:{body:t},comments:e}}var N=class a{constructor(c){this.tokens=c;this.i=0}peek(c=0){return this.tokens[Math.min(this.i+c,this.tokens.length-1)]}at(c,l,e=0){let n=this.peek(e);return n.type===c&&(l===void 0||n.value===l)}isP(c,l=0){return this.at("punct",c,l)}isK(c,l=0){return this.at("id",c,l)}next(){return this.tokens[this.i++]}fail(c,l=this.peek()){let e=l.type==="eof"?"end of file":`"${l.value}"`;throw new y(`${c}, found ${e}`,l.pos)}expectP(c){return this.isP(c)||this.fail(`Expected "${c}"`),this.next()}separator(c){this.isP(c)||(this.isP(",")||this.fail(`Expected "," or "${c}"`),this.next())}ident(){let c=this.peek();return(c.type!=="id"||D.has(c.value))&&this.fail("Expected a name"),this.next()}last(){return this.tokens[this.i-1].end}statement(){let c=this.stmt();for(let l of c)l.end=this.last();return c}stmt(){let c=this.peek();if(this.isP(";"))return this.next(),[];if(this.isP("{")){this.next();let s=[];for(;!this.isP("}");)this.at("eof")&&this.fail('Expected "}"'),s.push(...this.statement());return this.next(),[{kind:"block",body:s,pos:c.pos}]}if(this.isK("module")){this.next();let s=this.ident(),h=this.params();return[{kind:"module",name:s.value,params:h,body:this.body(),pos:c.pos,namePos:s.pos}]}if(this.isK("function")&&this.at("id",void 0,1)){this.next();let s=this.ident(),h=this.params();this.expectP("=");let i=this.expr();return this.expectP(";"),[{kind:"function",name:s.value,params:h,body:i,pos:c.pos,namePos:s.pos}]}if(this.isK("use")||this.isK("include")){this.next();let s=this.peek();return s.type!=="str"&&this.fail("Expected a library path like <kerf/box>"),this.next(),this.isP(";")&&this.next(),[{kind:"use",path:s.value,include:c.value==="include",pos:c.pos}]}if(this.isK("for"))return this.next(),[{kind:"for",assigns:this.assigns(),body:this.body(),pos:c.pos}];if(this.isK("intersection_for")&&this.isP("(",1))return this.next(),[{kind:"for",assigns:this.assigns(),body:this.body(),pos:c.pos,intersect:!0}];if(this.isK("if")){this.next(),this.expectP("(");let s=this.expr();this.expectP(")");let h=this.body(),i=[];return this.isK("else")&&(this.next(),i=this.body()),[{kind:"if",cond:s,then:h,else:i,pos:c.pos}]}if(this.isK("let")&&this.isP("(",1))return this.next(),[{kind:"let",assigns:this.assigns(),body:this.body(),pos:c.pos}];if(c.type==="id"&&this.isP("=",1)){let s=this.ident().value;this.next();let h=this.peek().pos.offset,i=this.expr(),o=this.expectP(";").pos.offset;return[{kind:"assign",name:s,value:i,span:[h,o],pos:c.pos}]}let l=null;(this.isP("*")||this.isP("!")||this.isP("#")||this.isP("%"))&&(l=this.next().value);let e=this.peek();(e.type!=="id"||D.has(e.value))&&this.fail("Expected a statement"),this.next();let n=this.args(),t=this.body();return[{kind:"call",name:e.value,args:n,children:t,modifier:l,pos:e.pos}]}body(){if(this.isP(";"))return this.next(),[];if(this.isP("{")){this.next();let c=[];for(;!this.isP("}");)this.at("eof")&&this.fail('Expected "}"'),c.push(...this.statement());return this.next(),c}return this.statement()}params(){this.expectP("(");let c=[];for(;!this.isP(")");){let l=this.ident(),e=null;this.isP("=")&&(this.next(),e=this.expr()),c.push({name:l.value,default:e,pos:l.pos}),this.separator(")")}return this.next(),c}args(){this.expectP("(");let c=[];for(;!this.isP(")");){if(this.at("id")&&this.isP("=",1)){let l=this.next();this.next(),c.push({name:l.value,value:this.expr(),pos:l.pos})}else c.push({name:null,value:this.expr()});this.separator(")")}return this.next(),c}assigns(){this.expectP("(");let c=[];for(;!this.isP(")");){let l=this.ident();this.expectP("="),c.push({name:l.value,value:this.expr(),pos:l.pos}),this.separator(")")}return this.next(),c}expr(){let c=this.peek();if(this.isK("function")&&this.isP("(",1)){this.next();let e=this.params(),n=this.expr();return{kind:"lambda",params:e,body:n,pos:c.pos,end:this.last()}}if(this.isK("let")&&this.isP("(",1)){this.next();let e=this.assigns(),n=this.expr();return{kind:"let",assigns:e,body:n,pos:c.pos,end:this.last()}}if(this.at("id")&&(c.value==="assert"||c.value==="echo")&&this.isP("(",1)){let e=this.i;this.next();let n={kind:"call",callee:{kind:"var",name:c.value,pos:c.pos},args:this.args(),pos:c.pos};if(this.startsValue())return{kind:"chain",check:n,body:this.expr(),pos:c.pos};this.i=e}let l=this.binary(0);if(this.isP("?")){this.next();let e=this.expr();this.expectP(":");let n=this.expr();return{kind:"ternary",cond:l,then:e,else:n,pos:c.pos}}return l}static{this.LEVELS=[["||"],["&&"],["==","!="],["<","<=",">",">="],["+","-"],["*","/","%"]]}binary(c){if(c>=a.LEVELS.length)return this.unary();let l=this.binary(c+1);for(;this.at("punct")&&a.LEVELS[c].includes(this.peek().value);){let e=this.next(),n=this.binary(c+1);l={kind:"binary",op:e.value,left:l,right:n,pos:e.pos}}return l}startsValue(){let c=this.peek();return c.type==="num"||c.type==="str"||c.type==="id"?!0:this.isP("[")||this.isP("(")||this.isP("{")||this.isP("!")||this.isP("-")}unary(){if((this.isK("let")||this.isK("function"))&&this.isP("(",1))return this.expr();if(this.isP("-")||this.isP("!")||this.isP("+")){let c=this.next();return{kind:"unary",op:c.value,arg:this.unary(),pos:c.pos}}return this.power()}power(){let c=this.postfix();if(this.isP("^")){let l=this.next();return{kind:"binary",op:"^",left:c,right:this.unary(),pos:l.pos}}return c}postfix(){let c=this.primary();for(;;){let l=this.peek();if(this.isP("("))c={kind:"call",callee:c,args:this.args(),pos:c.pos};else if(this.isP("[")){this.next();let e=this.expr();this.expectP("]"),c={kind:"index",target:c,index:e,pos:l.pos}}else if(this.isP("."))this.next(),c={kind:"member",target:c,name:this.ident().value,pos:l.pos};else return c}}primary(){let c=this.peek();if(c.type==="num"){this.next();let l=/(mm|cm|m|in|ft)$/.exec(c.value);return l?{kind:"num",value:Number(c.value.slice(0,-l[1].length)),unit:l[1],pos:c.pos}:{kind:"num",value:Number(c.value),pos:c.pos}}if(c.type==="str")return this.next(),{kind:"str",value:c.value,pos:c.pos};if(c.type==="id")return c.value==="true"||c.value==="false"?(this.next(),{kind:"bool",value:c.value==="true",pos:c.pos}):c.value==="undef"?(this.next(),{kind:"undef",pos:c.pos}):(D.has(c.value)&&this.fail("Expected a value"),this.next(),{kind:"var",name:c.value,pos:c.pos});if(this.isP("(")){this.next();let l=this.expr();return this.expectP(")"),l}if(this.isP("["))return this.list();if(this.isP("{"))return this.record();this.fail("Expected a value")}record(){let c=this.expectP("{"),l=[];for(;!this.isP("}");){let e=this.ident();if(l.some(n=>n.name===e.value))throw new y(`Field ${e.value} is given twice`,e.pos);if(this.expectP("="),l.push({name:e.value,value:this.expr(),pos:e.pos}),!this.isP(","))break;this.next()}return this.expectP("}"),{kind:"record",fields:l,pos:c.pos}}list(){let c=this.expectP("[");if(this.isP("]"))return this.next(),{kind:"list",items:[],pos:c.pos};if(!this.isK("for")&&!this.isK("if")&&!this.isK("each")&&!(this.isK("let")&&this.isP("(",1))&&!(this.isP("(")&&(this.isK("for",1)||this.isK("if",1)))){let l=this.expr();if(this.isP(":")){this.next();let n=null,t=this.expr();return this.isP(":")&&(this.next(),n=t,t=this.expr()),this.expectP("]"),{kind:"range",start:l,step:n,end:t,pos:c.pos}}let e=[{kind:"item",expr:l}];return this.listRest(e,c.pos)}return this.listRest([],c.pos)}listRest(c,l){if(c.length){if(this.isP("]"))return this.next(),{kind:"list",items:c,pos:l};this.separator("]")}for(;!this.isP("]");)c.push(this.listItem()),this.separator("]");return this.next(),{kind:"list",items:c,pos:l}}listItem(){let c=this.peek();if(this.isK("for")&&this.isP("(",1)){this.next();let l=this.assigns(),e=this.listItem();return{kind:"for",assigns:l,body:e,pos:c.pos,end:this.last()}}if(this.isK("if")&&this.isP("(",1)){this.next(),this.expectP("(");let l=this.expr();this.expectP(")");let e=this.listItem(),n=null;return this.isK("else")&&(this.next(),n=this.listItem()),{kind:"if",cond:l,then:e,else:n}}if(this.isK("let")&&this.isP("(",1)){this.next();let l=this.assigns(),e=this.listItem();return{kind:"let",assigns:l,body:e,pos:c.pos,end:this.last()}}if(this.isK("each"))return this.next(),{kind:"each",expr:this.expr()};if(this.isP("(")&&(this.isK("for",1)||this.isK("if",1))){this.next();let l=this.listItem();return this.expectP(")"),l}return{kind:"item",expr:this.expr()}}};var i1={"kerf/box":`// kerf/box: finger-jointed boxes.
|
|
9
9
|
// Copyright (c) 2026 MyLaserTools. AGPL-3.0-only or MyLaserTools commercial licence.
|
|
10
10
|
|
|
11
11
|
// Box from its outer size [width, depth, height] in the current material.
|
|
@@ -275,6 +275,70 @@ module vents(size, slot = 3, pitch = 6) {
|
|
|
275
275
|
translate([0, ends]) circle(d = slot, $fn = 24);
|
|
276
276
|
}
|
|
277
277
|
}
|
|
278
|
+
`,"kerf/fit":`// kerf/fit: drawings scaled to a size and placed by a point of their box.
|
|
279
|
+
// Copyright (c) 2026 MyLaserTools. AGPL-3.0-only or MyLaserTools commercial licence.
|
|
280
|
+
//
|
|
281
|
+
// For any drawing whose size you don't know beforehand: an imported SVG
|
|
282
|
+
// path, a silhouette, a symbol, a line of text. Each measures its children
|
|
283
|
+
// and moves or scales them, keeping their layers, so cut, score and
|
|
284
|
+
// engrave stay together. They are for drawings, not for parts: scaling a
|
|
285
|
+
// part would scale its joints and leave its thickness.
|
|
286
|
+
|
|
287
|
+
// The fractions of a box an anchor names, [0, 0] the bottom left and
|
|
288
|
+
// [1, 1] the top right: "center", "left", "right", "top", "bottom",
|
|
289
|
+
// "top-left", "top-right", "bottom-left", "bottom-right", or [fx, fy]
|
|
290
|
+
function anchor_point(anchor) =
|
|
291
|
+
is_list(anchor) ? anchor
|
|
292
|
+
: let (
|
|
293
|
+
a = lower(anchor),
|
|
294
|
+
fx = contains(a, "left") ? 0 : contains(a, "right") ? 1 : 0.5,
|
|
295
|
+
fy = contains(a, "bottom") ? 0 : contains(a, "top") ? 1 : 0.5
|
|
296
|
+
)
|
|
297
|
+
assert(
|
|
298
|
+
len([for (w = split(a, "-")) if (w != "center" && w != "left" && w != "right" && w != "top" && w != "bottom") w]) == 0,
|
|
299
|
+
str("Unknown anchor \\"", anchor, "\\": use center, left, right, top, bottom or a corner such as top-left")
|
|
300
|
+
)
|
|
301
|
+
[fx, fy];
|
|
302
|
+
|
|
303
|
+
// The children moved so the point of their box that \`anchor\` names is at
|
|
304
|
+
// the origin: align("bottom-left") puts their box's corner there, and
|
|
305
|
+
// align() centres them.
|
|
306
|
+
module align(anchor = "center") {
|
|
307
|
+
f = anchor_point(anchor);
|
|
308
|
+
measure() {
|
|
309
|
+
children();
|
|
310
|
+
if (!is_undef($bounds))
|
|
311
|
+
translate([-($bounds[0] + f[0] * ($bounds[2] - $bounds[0])), -($bounds[1] + f[1] * ($bounds[3] - $bounds[1]))])
|
|
312
|
+
children();
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// The children scaled to \`size\` = [w, h] (or one number for a square):
|
|
317
|
+
// mode = "contain" keeps their proportions and fits them inside, "stretch"
|
|
318
|
+
// fills the size exactly. A drawing with no width or no height (a line)
|
|
319
|
+
// is scaled by the other side. They are then placed by \`anchor\` as
|
|
320
|
+
// align() does, the box's bottom left at the origin by default.
|
|
321
|
+
module fit(size, mode = "contain", anchor = "bottom-left") {
|
|
322
|
+
s = is_list(size) ? size : [size, size];
|
|
323
|
+
assert(s[0] > 0 && s[1] > 0, "fit(): size must be more than 0");
|
|
324
|
+
assert(mode == "contain" || mode == "stretch", "fit(): mode is \\"contain\\" or \\"stretch\\"");
|
|
325
|
+
f = anchor_point(anchor);
|
|
326
|
+
measure() {
|
|
327
|
+
children();
|
|
328
|
+
if (!is_undef($bounds)) {
|
|
329
|
+
w = $bounds[2] - $bounds[0];
|
|
330
|
+
h = $bounds[3] - $bounds[1];
|
|
331
|
+
kx = w > 0 ? s[0] / w : undef;
|
|
332
|
+
ky = h > 0 ? s[1] / h : undef;
|
|
333
|
+
k = mode == "stretch"
|
|
334
|
+
? [is_undef(kx) ? 1 : kx, is_undef(ky) ? 1 : ky]
|
|
335
|
+
: let (one = is_undef(kx) ? ky : is_undef(ky) ? kx : min(kx, ky)) [one, one];
|
|
336
|
+
if (!is_undef(k[0]))
|
|
337
|
+
translate([-f[0] * w * k[0], -f[1] * h * k[1]])
|
|
338
|
+
scale(k) translate([-$bounds[0], -$bounds[1]]) children();
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
278
342
|
`,"kerf/gears":`// kerf/gears: gears, racks, lantern gears and cams for moving models.
|
|
279
343
|
// Copyright (c) 2026 MyLaserTools. AGPL-3.0-only or MyLaserTools commercial licence.
|
|
280
344
|
//
|
|
@@ -1568,14 +1632,29 @@ function size_names(table) = join([for (e = table) e[0]], ", ");
|
|
|
1568
1632
|
// symbol_names() lists them: "heart", "star", "paw", "tree" and the like.
|
|
1569
1633
|
// box = "grid" sizes and places it by its icon set's grid (24 x 24 for
|
|
1570
1634
|
// Material Design Icons) instead, so icons of one set line up as drawn.
|
|
1571
|
-
|
|
1635
|
+
// solid = true cuts it as one silhouette, gaps narrower than 2 * close
|
|
1636
|
+
// closed and its holes filled, with the drawing's detail engraved on it:
|
|
1637
|
+
// a pictogram drawn as separate strokes (a moon and stars, yarn) cut out
|
|
1638
|
+
// in one piece. bridge = w also joins pieces too far apart to close, by
|
|
1639
|
+
// strips w wide.
|
|
1640
|
+
module symbol(name, size = 10, center = true, fill = "nonzero", box = "drawn", solid = false, close = undef, bridge = undef) {
|
|
1572
1641
|
assert(box == "drawn" || box == "grid", "box is \\"drawn\\" or \\"grid\\"");
|
|
1573
1642
|
s = symbol_entry(name);
|
|
1574
1643
|
vb = box == "grid" ? s[5] : s[2];
|
|
1575
1644
|
k = size / max(vb[2], vb[3]);
|
|
1576
|
-
translate(center ? [0, 0] : [vb[2] * k / 2, vb[3] * k / 2])
|
|
1645
|
+
module drawn() translate(center ? [0, 0] : [vb[2] * k / 2, vb[3] * k / 2])
|
|
1577
1646
|
scale([k, -k]) translate([-vb[0] - vb[2] / 2, -vb[1] - vb[3] / 2])
|
|
1578
1647
|
path(s[4], fill = fill, close = true);
|
|
1648
|
+
r = is_undef(close) ? size / 30 : close;
|
|
1649
|
+
module silhouette() if (is_undef(bridge)) fill_holes() close(r) drawn();
|
|
1650
|
+
else join_islands(bridge) fill_holes() close(r) drawn();
|
|
1651
|
+
if (solid) {
|
|
1652
|
+
silhouette();
|
|
1653
|
+
engrave() difference() {
|
|
1654
|
+
silhouette();
|
|
1655
|
+
drawn();
|
|
1656
|
+
}
|
|
1657
|
+
} else drawn();
|
|
1579
1658
|
}
|
|
1580
1659
|
|
|
1581
1660
|
// [width, height] of what a symbol draws when it is \`size\` across its
|
|
@@ -2556,10 +2635,10 @@ module tab_rect(size, edges = true, count = undef, width = undef) {
|
|
|
2556
2635
|
if (e[2]) translate([w, h]) rotate(180) tabs(w, count, width);
|
|
2557
2636
|
if (e[3]) translate([0, h]) rotate(270) tabs(h, count, width);
|
|
2558
2637
|
}
|
|
2559
|
-
`};var P="0.3";var u="kerf.json",
|
|
2560
|
-
`));c.update(`${l}\0${e.length}\0`),c.update(e)}return"sha256-"+c.digest("base64")}function o1(a){return a.kind==="folder"?"folder:"+(process.platform==="win32"?a.path.toLowerCase():a.path):a.kind==="github"?`github:${a.owner}/${a.repo}`.toLowerCase()+`@${a.ref}`:a.url}function
|
|
2561
|
-
`};async function $1(a,c={}){let l=c.fetch??globalThis.fetch,e=c.log??(()=>{}),n=c.cache??new Map,t=
|
|
2562
|
-
`)&&
|
|
2563
|
-
`)&&_(
|
|
2564
|
-
`);try{return await $1(a,l)}catch(
|
|
2565
|
-
`));let i=[...t.keys()].sort().map(m=>[`${e}/${m}`,t.get(m)]),o=c??
|
|
2638
|
+
`};var P="0.3";var u="kerf.json",k="kerf.lock",A="kerf_libraries",x=20<<20,R=2e3,X=/^[a-z0-9][a-z0-9_-]{0,63}$/,C1=/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/,H1=/^(readme|licen[cs]e|copying|notice|changelog)(\.[a-z]+)?$/i,W=class extends Error{},c1=a=>{try{return m1(a).isFile()}catch{return!1}},C=a=>{try{return m1(a).isDirectory()}catch{return!1}},S=(a,c)=>a&&Object.prototype.hasOwnProperty.call(a,c)?a[c]:void 0,d=a=>{throw new W(a)};function r1(a,c){return(typeof a!="string"||!X.test(a))&&d(`${c} "${a}" is not a library name: use lowercase letters, digits, - and _`),a==="kerf"&&d(`${c}: "kerf" is the standard library's name`),a}function l1(a,c){(!a||typeof a!="object"||Array.isArray(a))&&d(`${c} must hold a JSON object`);let l=a;for(let n of["name","version","description","license","language"])l[n]!==void 0&&typeof l[n]!="string"&&d(`${c}: "${n}" must be a string`);l.name!==void 0&&r1(l.name,`${c}: name`),l.version!==void 0&&!C1.test(l.version)&&d(`${c}: "version" must look like 1.2.0, not "${l.version}"`),l.language!==void 0&&!/^\d+\.\d+$/.test(l.language)&&d(`${c}: "language" must look like ${P}, not "${l.language}"`),l.sheet!==void 0&&!(Array.isArray(l.sheet)&&l.sheet.length===2&&l.sheet.every(n=>typeof n=="number"&&Number.isFinite(n)&&n>0))&&d(`${c}: "sheet" must be [width, height] in mm, like [600, 400]`);let e=l.libraries??{};(!e||typeof e!="object"||Array.isArray(e))&&d(`${c}: "libraries" must be an object of name: source`);for(let[n,t]of Object.entries(e))r1(n,`${c}: library`),typeof t!="string"&&d(`${c}: the source of ${n} must be a string`);return{...l,libraries:{...e}}}function a1(a,c){let l;try{l=T(a,"utf8")}catch{return d(`${c} was not found`)}try{return JSON.parse(l)}catch(e){return d(`${c} is not valid JSON: ${e.message}`)}}function H(a,c=u){return l1(a1(M(a,u),c),c)}function d1(a){if(!I(M(a,k)))return;let c=a1(M(a,k),k);(c?.lockVersion!==1||!c.libraries||typeof c.libraries!="object")&&d(`${k} is not a kerf.lock this kerf can read. Delete it and run kerf install.`);for(let[l,e]of Object.entries(c.libraries))(!e||["source","resolved","integrity"].some(n=>typeof e[n]!="string"))&&d(`${k}: the entry for ${l} is damaged. Remove it and run kerf install.`);return c}function M1(a){for(let c=O(a);;){if(c1(M(c,u)))return c;let l=Y(c);if(l===c)return;c=l}}function n1(a){try{return H(a).name}catch{return}}function V1(a,c,l=c1){let e=(s,h)=>[M(s,h),M(s,h+".kerf")].find(i=>l(i));if(Z(c))return[c,c+".kerf"].find(s=>l(s));let n=c.indexOf("/"),t=n>0?M1(a):void 0;if(t){let s=c.slice(0,n);if(s===n1(t))return e(t,c.slice(n+1));if(C(M(t,A,s)))return e(M(t,A),c)}return e(a,c)}function Q1(a){let c=Y(O(a));return l=>{let e=V1(c,l);return e===void 0?void 0:T(e,"utf8")}}function J1(a,c=200){let l=M1(a);if(!l)return[];let e=[],n=(h,i)=>{for(let o of[...z1(h,3)].sort()){if(e.length>=c)return;o.endsWith(".kerf")&&e.push(i+o.slice(0,-5))}},t=n1(l);t&&n(l,t+"/");let s=M(l,A);if(C(s))for(let h of Q(s).sort())X.test(h)&&C(M(s,h))&&n(M(s,h),h+"/");return e}function z1(a,c=1/0){let l=[],e=(n,t,s)=>{let h;try{h=Q(n,{withFileTypes:!0})}catch{return}for(let i of h){if(i.name.startsWith(".")||i.name==="node_modules"||i.name===A)continue;let o=t?`${t}/${i.name}`:i.name;i.isDirectory()?s<c&&e(M(n,i.name),o,s+1):i.isFile()&&(l.push(o),l.length>R&&d(`${a} has more than ${R} files`))}};return e(a,"",0),l}function G(a,c=()=>!0){let l=new Map;for(let e of z1(a))c(e)&&l.set(e,T(M(a,...e.split("/"))));return l}var e1=a=>a===u||a.endsWith(".kerf")||!a.includes("/")&&H1.test(a);function F(a){let c=b1("sha256");for(let l of[...a.keys()].sort()){let e=Buffer.from(a.get(l).toString("utf8").replace(/\r\n/g,`
|
|
2639
|
+
`));c.update(`${l}\0${e.length}\0`),c.update(e)}return"sha256-"+c.digest("base64")}function o1(a){return a.kind==="folder"?"folder:"+(process.platform==="win32"?a.path.toLowerCase():a.path):a.kind==="github"?`github:${a.owner}/${a.repo}`.toLowerCase()+`@${a.ref}`:a.url}function E(a,c){let l=/^github:([\w.-]+)\/([\w.-]+?)(?:\.git)?(?:@(\S+))?$/.exec(a);if(l)return{kind:"github",owner:l[1],repo:l[2],ref:l[3]??"HEAD"};if(/^https?:\/\//i.test(a)){let n;try{n=new URL(a)}catch{return d(`${a} is not a link`)}return n.protocol==="http:"&&!["localhost","127.0.0.1","[::1]"].includes(n.hostname)&&d(`Use https:// to download ${a}`),{kind:"url",url:a}}let e=a.startsWith("file:")?a.slice(5):a;return e.startsWith("./")||e.startsWith("../")||Z(e)?{kind:"folder",path:O(c,e)}:d(`"${a}" is not a library source. Use github:owner/repo@tag, an https:// link to a .tar.gz, or a folder like ./my-library`)}async function q1(a,c){let l;try{l=await c(a)}catch(h){let i=h;return d(`Could not download ${a}: ${i.cause?.message??i.message}`)}l.ok||d(`Could not download ${a}: HTTP ${l.status}`+(l.status===404?". Check the name and the tag, and that it is public.":""));let e=()=>d(`${a} is larger than ${x>>20} MB`);if(Number(l.headers?.get("content-length"))>x&&e(),!l.body){let h=Buffer.from(await l.arrayBuffer());return h.length>x?e():h}let n=l.body.getReader(),t=[],s=0;for(;;){let{done:h,value:i}=await n.read();if(h||!i)break;s+=i.length,s>x&&(await n.cancel().catch(()=>{}),e()),t.push(i)}return Buffer.concat(t)}function x1(a,c){let l;try{l=A1(a,{maxOutputLength:5*x})}catch{return d(`${c} is not a .tar.gz file`)}let e=new Map,n,t={},s;for(let h=0;h+512<=l.length;){let i=l.subarray(h,h+512);if(i.every(g=>g===0))break;let o=(g,L)=>{let w=i.subarray(g,g+L),V=w.indexOf(0);return w.subarray(0,V<0?L:V).toString("utf8")},m=parseInt(o(124,12).trim()||"0",8);m>=0||d(`${c} is damaged`);let z=o(156,1)||"0",r=l.subarray(h+512,h+512+m);if(h+=512+Math.ceil(m/512)*512,z==="x"||z==="g"){let g=_1(r);z==="g"?n=g.comment??n:t=g;continue}if(z==="L"){s=r.toString("utf8").replace(/\0+$/,"");continue}let v=o(345,155),p=t.path??s??(v?v+"/":"")+o(0,100);t={},s=void 0,(z==="0"||z==="7")&&e.set(p,Buffer.from(r)),e.size>R&&d(`${c} has more than ${R} files`)}return{files:e,commit:n}}function _1(a){let c={};for(let l=0;l<a.length;){let e=a.indexOf(32,l),n=parseInt(a.subarray(l,e).toString(),10);if(!(n>0)||e<0)break;let t=a.subarray(e+1,l+n-1).toString("utf8"),s=t.indexOf("=");s>0&&(c[t.slice(0,s)]=t.slice(s+1)),l+=n}return c}function v1(a,c){let l=new Map;for(let[t,s]of a){let h=t.replace(/^\.\//,""),i=h.split("/");(Z(h)||h.includes("\\")||h.includes(":")||i.some(o=>o===".."||o===""))&&d(`${c} holds a file outside its folder: ${t}`),l.set(h,s)}let e="";if(!l.has(u)){let t=new Set([...l.keys()].map(s=>s.split("/")[0]));t.size===1&&(e=[...t][0]+"/")}let n=new Map;for(let[t,s]of l){if(!t.startsWith(e))continue;let h=t.slice(e.length);h.split("/").some(i=>i.startsWith(".")||i===A||i==="node_modules")||e1(h)&&n.set(h,s)}return n.has(u)||d(`${c} has no kerf.json at its top. Is it a KerfScript library?`),n}var S1=(a,c)=>{let[l,e]=a.split(".").map(Number),[n,t]=c.split(".").map(Number);return l>n||l===n&&e>t};async function f1(a,c,l,e,n){let t=E(a,c),s,h;if(t.kind==="folder")c1(M(t.path,u))||d(`${a}: no kerf.json in ${t.path}. Is it a KerfScript library?`),s=v1(G(t.path,e1),a),h=L1(l,t.path).split(y1).join("/")||".",!h.startsWith(".")&&!Z(h)&&(h="./"+h);else{let o=t.kind==="github"?`https://github.com/${t.owner}/${t.repo}/archive/${t.ref}.tar.gz`:t.url,m=n.get(o)??await q1(o,e);n.set(o,m);let z=x1(m,a);s=v1(z.files,a),h=t.kind==="github"&&/^[0-9a-f]{40}$/.test(z.commit??"")?`https://github.com/${t.owner}/${t.repo}/archive/${z.commit}.tar.gz`:o}let i=l1(JSON.parse(s.get(u).toString("utf8")),`the kerf.json of ${a}`);return i.name||d(`${a}: its kerf.json has no "name", so it is not a library`),i.language&&S1(i.language,P)&&d(`${i.name} needs KerfScript language ${i.language} or later, and this kerf has ${P}. Update kerf.`),{manifest:i,files:s,resolved:h,integrity:F(s)}}function T1(a,c){J(a,{recursive:!0,force:!0});for(let[l,e]of c){let n=M(a,...l.split("/"));w1(Y(n),{recursive:!0}),_(n,e)}}var j1=a=>{let c=[...a].sort(([l],[e])=>l<e?-1:1);return JSON.stringify({lockVersion:1,libraries:Object.fromEntries(c)},null,2)+`
|
|
2640
|
+
`};async function $1(a,c={}){let l=c.fetch??globalThis.fetch,e=c.log??(()=>{}),n=c.cache??new Map,t=H(a),s=d1(a),h=new Map,i=Object.entries(t.libraries).map(([r,v])=>({name:r,source:v,base:a,from:u})),o=M(a,A);for(;i.length;){let{name:r,source:v,base:p,from:g}=i.shift(),L=E(v,p),w=L.kind==="folder",V=w?`${v} (${L.path})`:v,j=h.get(r);if(j){j.at!==o1(L)&&!S(t.libraries,r)&&d(`${g} uses ${r} from ${V}, and ${j.from} uses it from ${j.shown}. Add ${r} to kerf.json with the source to use.`);continue}let B=S(s?.libraries,r),b=B?.source===v&&(!w||O(a,B.resolved)===L.path)?B:void 0;c.frozen&&!b&&d(`kerf.lock does not pin ${r} from ${v}. Run kerf install without --frozen.`);let $=M(o,r),q;if(b&&!w&&C($)&&F(G($))===b.integrity)q=b;else{let f=await f1(b&&!w?b.resolved:v,w?p:a,a,l,n);f.manifest.name!==r&&d(`${v} is the library "${f.manifest.name}", so ${g} must call it ${f.manifest.name}, not ${r}`),b&&f.integrity!==b.integrity&&(!w||c.frozen)&&d(w?`${r} in ${v} changed since kerf.lock pinned it. Run kerf install without --frozen.`:`${r} from ${b.resolved} no longer matches kerf.lock. If you trust the change, remove ${r} from kerf.lock and run kerf install again.`),T1($,f.files),q={source:v,...f.manifest.version?{version:f.manifest.version}:{},resolved:f.resolved,integrity:f.integrity},(!b||b.integrity!==f.integrity||!w)&&e(`${b?"Restored":"Installed"} ${r}${q.version?" "+q.version:""} from ${v}`)}h.set(r,{entry:q,at:o1(L),shown:V,from:g});let t1=L.kind==="folder"?L.path:void 0;for(let[f,K]of Object.entries(H($,`${A}/${r}/${u}`).libraries))E(K,a).kind==="folder"&&!t1&&d(`${r} uses ${f} from the folder ${K}: a published library needs github: or https: sources`),i.push({name:f,source:K,base:t1??a,from:r})}let m=j1(new Map([...h].map(([r,v])=>[r,v.entry]))),z=I(M(a,k))?T(M(a,k),"utf8"):"";if(c.frozen&&m!==z.replace(/\r\n/g,`
|
|
2641
|
+
`)&&d("kerf.lock is out of date. Run kerf install without --frozen."),C(o))for(let r of Q(o,{withFileTypes:!0}))r.isDirectory()&&X.test(r.name)&&!h.has(r.name)&&(J(M(o,r.name),{recursive:!0,force:!0}),e(`Removed ${r.name}`));return m!==z.replace(/\r\n/g,`
|
|
2642
|
+
`)&&_(M(a,k),m),JSON.parse(m)}function Y1(a){let c=H(a),l=d1(a),e=Object.keys(c.libraries);if(!l)return e.length?["kerf.lock is missing. Run kerf install."]:[];let n=[],t=new Set,s=[...e];for(;s.length;){let h=s.shift();if(t.has(h))continue;t.add(h);let i=S(l.libraries,h);if(!i){n.push(`${h} is not in kerf.lock. Run kerf install.`);continue}e.includes(h)&&i.source!==c.libraries[h]&&n.push(`kerf.json takes ${h} from ${c.libraries[h]}, and kerf.lock from ${i.source}. Run kerf install.`);let o=M(a,A,h);if(!C(o)){n.push(`${h} is not in ${A}. Run kerf install.`);continue}F(G(o))!==i.integrity&&n.push(`The files in ${A}/${h} differ from kerf.lock. Run kerf install.`);try{s.push(...Object.keys(H(o).libraries))}catch(m){n.push(m.message)}}for(let h of Object.keys(l.libraries))t.has(h)||n.push(`kerf.lock pins ${h}, which nothing uses any more.`);return n}function P1(a,c){let l=M(a,u),e=I(l)?a1(l,u):{};l1(e,u);let n={...e.libraries??{}};return c(n),e.libraries=n,{file:l,before:I(l)?T(l,"utf8"):void 0,raw:e}}async function u1(a,c,l){let{file:e,before:n,raw:t}=P1(a,c);_(e,JSON.stringify(t,null,2)+`
|
|
2643
|
+
`);try{return await $1(a,l)}catch(s){throw n===void 0?J(e,{force:!0}):_(e,n),s}}async function X1(a,c,l={}){let e=new Map,t=(await f1(c,a,a,l.fetch??globalThis.fetch,e)).manifest.name;t===n1(a)&&d(`This project is the library ${t}; it cannot use itself`);let s=await u1(a,h=>void(h[t]=c),{...l,cache:e});return{name:t,lock:s}}async function c0(a,c,l={}){return u1(a,e=>{S(e,c)===void 0&&d(`kerf.json has no library named ${c}`),delete e[c]},l)}function U(a,c=[]){if(Array.isArray(a))for(let l of a)U(l,c);else if(a&&typeof a=="object"){let l=a;if(l.kind==="use"&&typeof l.path=="string")c.push({path:l.path,line:l.pos.line});else for(let e of Object.values(l))U(e,c)}return c}function I1(a){let c=[];for(let[l,e]of a){let n=Buffer.alloc(512),t=l,s="";if(Buffer.byteLength(t)>100){let i=l.lastIndexOf("/",155);(i<0||Buffer.byteLength(l.slice(i+1))>100)&&d(`${l}: the path is too long to pack`),s=l.slice(0,i),t=l.slice(i+1)}n.write(t,0,100),n.write("0000644\0",100),n.write("0000000\0",108),n.write("0000000\0",116),n.write(e.length.toString(8).padStart(11,"0")+"\0",124),n.write("00000000000\0",136),n.fill(32,148,156),n.write("0",156),n.write("ustar\0",257),n.write("00",263),n.write(s,345,155);let h=0;for(let i of n)h+=i;n.write(h.toString(8).padStart(6,"0")+"\0 ",148),c.push(n,e,Buffer.alloc((512-e.length%512)%512))}return c.push(Buffer.alloc(1024)),k1(Buffer.concat(c),{level:9})}function l0(a,c){let l=H(a),e=l.name??d('kerf.json needs a "name" to pack a library'),n=l.version??d('kerf.json needs a "version", like 1.0.0'),t=G(a,e1),s=[];for(let[m,z]of Object.entries(l.libraries))E(z,a).kind==="folder"&&s.push(`kerf.json: ${m} comes from a folder; a published library needs github: or https: sources`);let h=m=>t.has(m)||t.has(m+".kerf");for(let[m,z]of t){if(!m.endsWith(".kerf"))continue;let r;try{r=h1(z.toString("utf8"),`${e}/${m}`).program}catch(v){s.push(`${m}: ${v.message}`);continue}for(let v of U(r)){let p=v.path.split("/")[0];Object.prototype.hasOwnProperty.call(i1,v.path)||p===e&&h(v.path.slice(e.length+1))||v.path.includes("/")&&S(l.libraries,p)!==void 0||s.push(`${m}, line ${v.line}: <${v.path}> will not be found once the library is installed`+(h(v.path)?`. Write <${e}/${v.path}> for the library's own file.`:"."))}}[...t.keys()].some(m=>m.endsWith(".kerf"))||s.push("There are no .kerf files to pack"),s.length&&d(s.join(`
|
|
2644
|
+
`));let i=[...t.keys()].sort().map(m=>[`${e}/${m}`,t.get(m)]),o=c??M(a,`${e}-${n}.tar.gz`);return _(o,I1(i)),{file:o,name:e,version:n,files:t.size,integrity:F(t)}}export{A as FOLDER,k as LOCK,W as LibraryError,u as MANIFEST,X1 as add,Y1 as check,M1 as findProject,$1 as install,J1 as installedPaths,F as integrity,V1 as libraryFile,l0 as pack,d1 as readLock,H as readManifest,c0 as remove,Q1 as resolver};
|
package/dist/plot.d.cts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Poly, Pt } from './geometry.cjs';
|
|
2
|
+
export interface FlowOptions {
|
|
3
|
+
spacing: number;
|
|
4
|
+
step: number;
|
|
5
|
+
minLength: number;
|
|
6
|
+
seed: number;
|
|
7
|
+
box: [number, number, number, number];
|
|
8
|
+
inside: (p: Pt) => boolean;
|
|
9
|
+
dir: (p: Pt) => Pt | null;
|
|
10
|
+
tick: () => void;
|
|
11
|
+
maxPoints: number;
|
|
12
|
+
}
|
|
13
|
+
export declare function streamlines(o: FlowOptions): Poly[];
|
|
14
|
+
export declare function contourLines(v: Float64Array, nx: number, ny: number, x0: number, y0: number, cell: number, level: number): Poly[];
|
|
15
|
+
export declare function joinLines(lines: Poly[], tol?: number): Poly[];
|
|
16
|
+
export declare function insideTest(regions: Poly[]): (p: Pt) => boolean;
|
package/dist/plot.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Poly, Pt } from './geometry.js';
|
|
2
|
+
export interface FlowOptions {
|
|
3
|
+
spacing: number;
|
|
4
|
+
step: number;
|
|
5
|
+
minLength: number;
|
|
6
|
+
seed: number;
|
|
7
|
+
box: [number, number, number, number];
|
|
8
|
+
inside: (p: Pt) => boolean;
|
|
9
|
+
dir: (p: Pt) => Pt | null;
|
|
10
|
+
tick: () => void;
|
|
11
|
+
maxPoints: number;
|
|
12
|
+
}
|
|
13
|
+
export declare function streamlines(o: FlowOptions): Poly[];
|
|
14
|
+
export declare function contourLines(v: Float64Array, nx: number, ny: number, x0: number, y0: number, cell: number, level: number): Poly[];
|
|
15
|
+
export declare function joinLines(lines: Poly[], tol?: number): Poly[];
|
|
16
|
+
export declare function insideTest(regions: Poly[]): (p: Pt) => boolean;
|