@mylasertools/kerfscript 0.9.0 → 0.10.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 +88 -0
- package/LANGUAGE.md +13 -5
- package/README.md +2 -1
- package/dist/assembly.d.cts +2 -0
- package/dist/assembly.d.ts +2 -0
- package/dist/bores.d.cts +10 -0
- package/dist/bores.d.ts +10 -0
- package/dist/cli.js +7899 -6630
- package/dist/customizer.d.cts +3 -0
- package/dist/customizer.d.ts +3 -0
- package/dist/effects.d.cts +31 -0
- package/dist/effects.d.ts +31 -0
- package/dist/evaluator.d.cts +1 -0
- package/dist/evaluator.d.ts +1 -0
- package/dist/export/mesh.d.cts +53 -0
- package/dist/export/mesh.d.ts +53 -0
- package/dist/export/model.d.cts +12 -0
- package/dist/export/model.d.ts +12 -0
- package/dist/export/triangulate.d.cts +5 -0
- package/dist/export/triangulate.d.ts +5 -0
- package/dist/export/zip.d.cts +5 -0
- package/dist/export/zip.d.ts +5 -0
- package/dist/geometry.d.cts +1 -0
- package/dist/geometry.d.ts +1 -0
- package/dist/highlight.cjs +2 -2
- package/dist/highlight.js +2 -2
- package/dist/import/dbg.d.cts +1 -0
- package/dist/import/dbg.d.ts +1 -0
- package/dist/index.cjs +435 -48
- package/dist/index.d.cts +6 -2
- package/dist/index.d.ts +6 -2
- package/dist/index.js +435 -48
- package/dist/language.cjs +410 -30
- package/dist/language.js +410 -30
- package/dist/libraries.cjs +393 -14
- package/dist/libraries.js +393 -14
- package/dist/render.d.cts +3 -0
- package/dist/render.d.ts +3 -0
- package/dist/signatures.cjs +23 -5
- package/dist/signatures.js +23 -5
- package/dist/text.d.cts +6 -0
- package/dist/text.d.ts +6 -0
- package/docs/3d-files.md +72 -0
- package/docs/cli.md +27 -22
- package/docs/customizer.md +56 -0
- package/docs/javascript.md +46 -0
- package/docs/library.md +120 -0
- package/docs/openscad.md +15 -13
- package/docs/parts-and-sheets.md +2 -1
- 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
|
|
3
|
-
`&&(t++,s=n+1),n++};for(;n<a.length;){let
|
|
4
|
-
`){i(1);continue}if(
|
|
5
|
-
`,n),
|
|
6
|
-
`,n-1)+1,n));e.push({text:a.slice(n+2,
|
|
7
|
-
`;else if(
|
|
8
|
-
`)throw new k("Unclosed string",d);
|
|
2
|
+
"use strict";var B=Object.defineProperty;var M1=Object.getOwnPropertyDescriptor;var z1=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 z1(c))!u1.call(a,n)&&n!==l&&B(a,n,{get:()=>c[n],enumerable:!(e=M1(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:()=>j1,check:()=>T1,findProject:()=>X,install:()=>d1,installedPaths:()=>C1,integrity:()=>T,libraryFile:()=>r1,pack:()=>R1,readLock:()=>J,readManifest:()=>H,remove:()=>P1,resolver:()=>k1});module.exports=b1(E1);var o1=require("node:crypto"),M=require("node:fs"),v=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=r=>{for(let d=0;d<r;d++)a[n]===`
|
|
3
|
+
`&&(t++,s=n+1),n++};for(;n<a.length;){let r=a[n];if(r===" "||r===" "||r==="\r"||r===`
|
|
4
|
+
`){i(1);continue}if(r==="/"&&a[n+1]==="/"){let o=a.indexOf(`
|
|
5
|
+
`,n),m=o<0?a.length:o,g=/^[ \t]*$/.test(a.slice(a.lastIndexOf(`
|
|
6
|
+
`,n-1)+1,n));e.push({text:a.slice(n+2,m).trim(),line:t,block:!1,own:g,start:n,end:m}),n=m;continue}if(r==="/"&&a[n+1]==="*"){let o=h(),m=a.indexOf("*/",n+2);if(m<0)throw new k("Unclosed block comment",o);e.push({text:a.slice(n+2,m).trim(),line:o.line,block:!0,own:!0,start:n,end:m+2}),i(m+2-n);continue}let d=h();if(/[0-9]/.test(r)||r==="."&&/[0-9]/.test(a[n+1]??"")){let o=/^(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?(?:(?:mm|cm|m|in|ft)(?![A-Za-z0-9_]))?/.exec(a.slice(n));l.push({type:"num",value:o[0],pos:d,end:n+o[0].length}),i(o[0].length);continue}if(/[A-Za-z_$]/.test(r)){let o=/^[A-Za-z_$][A-Za-z0-9_]*/.exec(a.slice(n));l.push({type:"id",value:o[0],pos:d,end:n+o[0].length}),i(o[0].length);continue}if(r==='"'){let o="";for(i(1);n<a.length&&a[n]!=='"';){if(a[n]==="\\"){let m=a[n+1];if(m==="n")o+=`
|
|
7
|
+
`;else if(m==="t")o+=" ";else if(m==="u"){let g=a.slice(n+2,n+6);if(!/^[0-9a-fA-F]{4}$/.test(g))throw new k("Bad \\u escape",h());o+=String.fromCharCode(parseInt(g,16)),i(4)}else o+=m;i(2);continue}if(a[n]===`
|
|
8
|
+
`)throw new k("Unclosed string",d);o+=a[n],i(1)}if(n>=a.length)throw new k("Unclosed string",d);i(1),l.push({type:"str",value:o,pos:d,end:n});continue}if(r==="<"&&/^<[A-Za-z0-9_./\\: -]+>/.test(a.slice(n))){let o=l[l.length-1];if(o&&o.type==="id"&&(o.value==="use"||o.value==="include")){let m=/^<([A-Za-z0-9_./\\: -]+)>/.exec(a.slice(n));l.push({type:"str",value:m[1],pos:d,end:n+m[0].length}),i(m[0].length);continue}}let z=L1.find(o=>a.startsWith(o,n));if(!z)throw new k(`Unexpected character "${r}"`,d);l.push({type:"punct",value:z,pos:d,end:n+z.length}),i(z.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(),r=this.expectP(";").pos.offset;return[{kind:"assign",name:s,value:i,span:[h,r],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.
|
|
@@ -1542,6 +1542,330 @@ module ring_stack(diameters, hole, name = "ring", count = undef, feet = true, cl
|
|
|
1542
1542
|
translate([-(t + clearance) / 2, -t - 1]) rect([t + clearance, h / 2 + t + 1]);
|
|
1543
1543
|
}
|
|
1544
1544
|
}
|
|
1545
|
+
`,"kerf/shapes":`// kerf/shapes: basic shapes by name, sized to a box.
|
|
1546
|
+
// Copyright (c) 2026 MyLaserTools. AGPL-3.0-only or MyLaserTools commercial licence.
|
|
1547
|
+
//
|
|
1548
|
+
// Hearts, teardrops, clouds, shields, banners, arrows and the other shapes
|
|
1549
|
+
// that signs, tags, ornaments and boxes are made of. Each takes its size as
|
|
1550
|
+
// [w, h] (or one number for both) and is centred on the origin, as circle()
|
|
1551
|
+
// and ellipse() are; arrows and chevrons point along x, the others stand
|
|
1552
|
+
// upright. A shape with one outline also has a _points() function giving
|
|
1553
|
+
// that outline, counter-clockwise, for offset_path(), flex_strip() and
|
|
1554
|
+
// anything else that works on points. Curves follow $fn when it is set,
|
|
1555
|
+
// and otherwise take about a point per millimetre.
|
|
1556
|
+
|
|
1557
|
+
use <kerf/fit>
|
|
1558
|
+
|
|
1559
|
+
// [w, h] from a number or a pair, checked
|
|
1560
|
+
function shape_size(size, what) =
|
|
1561
|
+
let (s = is_list(size) ? size : [size, size])
|
|
1562
|
+
assert(
|
|
1563
|
+
len(s) == 2 && is_num(s[0]) && is_num(s[1]) && s[0] > 0 && s[1] > 0,
|
|
1564
|
+
str(what, "(): size must be more than 0, as a number or [w, h]")
|
|
1565
|
+
)
|
|
1566
|
+
s;
|
|
1567
|
+
|
|
1568
|
+
// How many points a curve about \`length\` mm long is drawn with
|
|
1569
|
+
function shape_steps(length) = $fn > 0 ? max(3, $fn) : clamp(ceil(length), 32, 200);
|
|
1570
|
+
|
|
1571
|
+
// The points scaled to fill the box [w, h], centred on the origin
|
|
1572
|
+
function shape_fill(points, s) =
|
|
1573
|
+
let (b = bounds(points), c = [(b[0] + b[2]) / 2, (b[1] + b[3]) / 2], kx = s[0] / (b[2] - b[0]), ky = s[1] / (b[3] - b[1]))
|
|
1574
|
+
[for (p = points) [(p[0] - c[0]) * kx, (p[1] - c[1]) * ky]];
|
|
1575
|
+
|
|
1576
|
+
// The points moved so their box is centred on the origin
|
|
1577
|
+
function shape_centre(points) =
|
|
1578
|
+
let (b = bounds(points), c = [(b[0] + b[2]) / 2, (b[1] + b[3]) / 2])
|
|
1579
|
+
[for (p = points) p - c];
|
|
1580
|
+
|
|
1581
|
+
// Points along an arc round \`c\` of radius r, from angle a0 to a1, the first
|
|
1582
|
+
// and last included
|
|
1583
|
+
function shape_arc(c, r, a0, a1, n) = [for (i = [0:n]) let (a = a0 + (a1 - a0) * i / n) c + r * [cos(a), sin(a)]];
|
|
1584
|
+
|
|
1585
|
+
// A heart: two round lobes meeting in a dip, and a point at the bottom
|
|
1586
|
+
module heart(size = 20) polygon(heart_points(size));
|
|
1587
|
+
|
|
1588
|
+
// The outline of heart(), as points
|
|
1589
|
+
function heart_points(size = 20) =
|
|
1590
|
+
let (s = shape_size(size, "heart"), n = shape_steps(1.7 * (s[0] + s[1])))
|
|
1591
|
+
shape_fill([for (i = [0:n - 1]) let (a = -360 * i / n) [16 * pow(sin(a), 3), 13 * cos(a) - 5 * cos(2 * a) - 2 * cos(3 * a) - cos(4 * a)]], s);
|
|
1592
|
+
|
|
1593
|
+
// A drop: round at the bottom, coming to a point at the top. It must be
|
|
1594
|
+
// taller than it is wide.
|
|
1595
|
+
module teardrop(size = [14, 20]) polygon(teardrop_points(size));
|
|
1596
|
+
|
|
1597
|
+
// The outline of teardrop(), as points
|
|
1598
|
+
function teardrop_points(size = [14, 20]) =
|
|
1599
|
+
let (
|
|
1600
|
+
s = shape_size(size, "teardrop"),
|
|
1601
|
+
r = s[0] / 2,
|
|
1602
|
+
d = s[1] - r
|
|
1603
|
+
)
|
|
1604
|
+
assert(s[1] > s[0], "teardrop(): make it taller than it is wide")
|
|
1605
|
+
let (
|
|
1606
|
+
t = acos(r / d),
|
|
1607
|
+
n = shape_steps(PI * s[0])
|
|
1608
|
+
)
|
|
1609
|
+
[for (p = concat([[0, s[1]]], shape_arc([0, r], r, 90 + t, 450 - t, n))) p - [0, s[1] / 2]];
|
|
1610
|
+
|
|
1611
|
+
// An egg standing on its broad end; \`taper\` is how much narrower the top
|
|
1612
|
+
// is, 0 for an ellipse
|
|
1613
|
+
module egg(size = [16, 20], taper = 0.2) polygon(egg_points(size, taper));
|
|
1614
|
+
|
|
1615
|
+
// The outline of egg(), as points
|
|
1616
|
+
function egg_points(size = [16, 20], taper = 0.2) =
|
|
1617
|
+
let (s = shape_size(size, "egg"), n = shape_steps(1.6 * (s[0] + s[1])))
|
|
1618
|
+
assert(taper >= 0 && taper < 1, "egg(): taper is from 0 up to 1")
|
|
1619
|
+
shape_fill([for (i = [0:n - 1]) let (a = 360 * i / n) [cos(a) * (1 - taper * sin(a)), sin(a)]], s);
|
|
1620
|
+
|
|
1621
|
+
// A lens (a leaf, a petal, an eye): two arcs meeting in a point at each
|
|
1622
|
+
// end of its longer side, the top and bottom of a tall one, the left and
|
|
1623
|
+
// right of a wide one
|
|
1624
|
+
module lens(size = [10, 20]) polygon(lens_points(size));
|
|
1625
|
+
|
|
1626
|
+
// The outline of lens(), as points
|
|
1627
|
+
function lens_points(size = [10, 20]) =
|
|
1628
|
+
let (s = shape_size(size, "lens"))
|
|
1629
|
+
// A wide lens is a tall one turned a quarter turn, so its arcs stay less
|
|
1630
|
+
// than half circles and its ends pointed
|
|
1631
|
+
s[0] > s[1] ? [for (p = shape_tall_lens([s[1], s[0]])) [p[1], -p[0]]] : shape_tall_lens(s);
|
|
1632
|
+
|
|
1633
|
+
// A lens with its points at the top and the bottom, no wider than tall
|
|
1634
|
+
function shape_tall_lens(s) =
|
|
1635
|
+
let (
|
|
1636
|
+
g = s[0] / 2,
|
|
1637
|
+
R = g / 2 + s[1] * s[1] / (8 * g),
|
|
1638
|
+
t = asin(min(1, s[1] / 2 / R)),
|
|
1639
|
+
// even, so a point falls on the widest place
|
|
1640
|
+
n = 2 * max(2, ceil(shape_steps(PI * (s[0] + s[1]) / 2) / 4))
|
|
1641
|
+
)
|
|
1642
|
+
concat(shape_arc([g - R, 0], R, -t, t, n), [for (p = shape_arc([R - g, 0], R, 180 - t, 180 + t, n)) if (abs(p[1]) < s[1] / 2 - 1e-9) p]);
|
|
1643
|
+
|
|
1644
|
+
// A crescent moon \`d\` across, open to the right, \`thickness\` at its widest
|
|
1645
|
+
module crescent(d = 20, thickness) polygon(crescent_points(d, thickness));
|
|
1646
|
+
|
|
1647
|
+
// The outline of crescent(), as points
|
|
1648
|
+
function crescent_points(d = 20, thickness) =
|
|
1649
|
+
let (
|
|
1650
|
+
R = d / 2,
|
|
1651
|
+
t = is_undef(thickness) ? d / 4 : thickness
|
|
1652
|
+
)
|
|
1653
|
+
assert(is_num(d) && d > 0, "crescent(): d must be more than 0")
|
|
1654
|
+
assert(t > 0 && t < d, "crescent(): thickness must be more than 0 and less than d")
|
|
1655
|
+
let (
|
|
1656
|
+
Y = sqrt(R * R - t * t / 4),
|
|
1657
|
+
a = atan2(Y, t / 2),
|
|
1658
|
+
b = atan2(Y, -t / 2),
|
|
1659
|
+
n = shape_steps(PI * d)
|
|
1660
|
+
)
|
|
1661
|
+
shape_centre(concat(
|
|
1662
|
+
shape_arc([0, 0], R, a, 360 - a, n),
|
|
1663
|
+
[for (i = [1:n - 1]) let (q = 360 - b - (360 - 2 * b) * i / n) [t, 0] + R * [cos(q), sin(q)]]
|
|
1664
|
+
));
|
|
1665
|
+
|
|
1666
|
+
// A rectangle with round ends (a stadium, a pill)
|
|
1667
|
+
module capsule(size = [30, 10]) polygon(capsule_points(size));
|
|
1668
|
+
|
|
1669
|
+
// The outline of capsule(), as points
|
|
1670
|
+
function capsule_points(size = [30, 10]) =
|
|
1671
|
+
let (
|
|
1672
|
+
s = shape_size(size, "capsule"),
|
|
1673
|
+
r = min(s) / 2,
|
|
1674
|
+
c = s[0] >= s[1] ? [s[0] / 2 - r, 0] : [0, s[1] / 2 - r],
|
|
1675
|
+
a = s[0] >= s[1] ? -90 : 0,
|
|
1676
|
+
n = ceil(shape_steps(PI * r * 2) / 2)
|
|
1677
|
+
)
|
|
1678
|
+
concat(shape_arc(c, r, a, a + 180, n), shape_arc(-c, r, a + 180, a + 360, n));
|
|
1679
|
+
|
|
1680
|
+
// A diamond (a rhombus) touching the middle of each side of its box
|
|
1681
|
+
module diamond(size = [14, 20]) polygon(diamond_points(size));
|
|
1682
|
+
|
|
1683
|
+
// The outline of diamond(), as points
|
|
1684
|
+
function diamond_points(size = [14, 20]) =
|
|
1685
|
+
let (s = shape_size(size, "diamond") / 2) [[0, -s[1]], [s[0], 0], [0, s[1]], [-s[0], 0]];
|
|
1686
|
+
|
|
1687
|
+
// A regular polygon with \`n\` corners \`r\` from its centre, as
|
|
1688
|
+
// regular_polygon(n, r), each rounded with radius \`round\`
|
|
1689
|
+
module rounded_polygon(n = 6, r = 10, round = 2) polygon(rounded_polygon_points(n, r, round));
|
|
1690
|
+
|
|
1691
|
+
// The outline of rounded_polygon(), as points
|
|
1692
|
+
function rounded_polygon_points(n = 6, r = 10, round = 2) =
|
|
1693
|
+
assert(is_num(n) && n >= 3, "rounded_polygon(): n must be 3 or more")
|
|
1694
|
+
assert(r > 0, "rounded_polygon(): r must be more than 0")
|
|
1695
|
+
assert(round >= 0 && round < r * cos(180 / n), "rounded_polygon(): round must be less than the distance to a side")
|
|
1696
|
+
let (
|
|
1697
|
+
k = floor(n),
|
|
1698
|
+
c = r - round / cos(180 / k),
|
|
1699
|
+
m = round > 0 ? max(2, ceil(shape_steps(2 * PI * round) / k)) : 0
|
|
1700
|
+
)
|
|
1701
|
+
[for (i = [0:k - 1]) let (a = 360 * i / k)
|
|
1702
|
+
each (round > 0 ? shape_arc(c * [cos(a), sin(a)], round, a - 180 / k, a + 180 / k, m) : [r * [cos(a), sin(a)]])];
|
|
1703
|
+
|
|
1704
|
+
// A shield: a flat top, straight sides, curving in to a point at the
|
|
1705
|
+
// bottom. \`point\` is how much of the height the curved part takes.
|
|
1706
|
+
module shield(size = [16, 20], point = 0.55) polygon(shield_points(size, point));
|
|
1707
|
+
|
|
1708
|
+
// The outline of shield(), as points
|
|
1709
|
+
function shield_points(size = [16, 20], point = 0.55) =
|
|
1710
|
+
let (s = shape_size(size, "shield"), w = s[0] / 2, top = s[1] / 2)
|
|
1711
|
+
assert(point > 0 && point <= 1, "shield(): point is more than 0, up to 1")
|
|
1712
|
+
let (
|
|
1713
|
+
yk = top - s[1] * (1 - point),
|
|
1714
|
+
n = shape_steps(s[1] * point),
|
|
1715
|
+
side = function(u) [w * (1 - u * u), yk - (yk + top) * u]
|
|
1716
|
+
)
|
|
1717
|
+
concat(
|
|
1718
|
+
[[-w, top]],
|
|
1719
|
+
[for (i = [0:n]) let (p = side(i / n)) [-p[0], p[1]]],
|
|
1720
|
+
[for (i = [n - 1:-1:0]) side(i / n)],
|
|
1721
|
+
[[w, top]]
|
|
1722
|
+
);
|
|
1723
|
+
|
|
1724
|
+
// An arrow pointing along x: \`size\` is its length and the head's width,
|
|
1725
|
+
// \`shaft\` the shaft's width and \`head\` the head's length
|
|
1726
|
+
module arrow(size = [30, 14], shaft, head) polygon(arrow_points(size, shaft, head));
|
|
1727
|
+
|
|
1728
|
+
// The outline of arrow(), as points
|
|
1729
|
+
function arrow_points(size = [30, 14], shaft, head) =
|
|
1730
|
+
let (
|
|
1731
|
+
s = shape_size(size, "arrow"),
|
|
1732
|
+
L = s[0] / 2,
|
|
1733
|
+
W = s[1] / 2,
|
|
1734
|
+
sw = is_undef(shaft) ? s[1] / 3 : shaft,
|
|
1735
|
+
hl = is_undef(head) ? min(s[0] / 2, s[1]) : head
|
|
1736
|
+
)
|
|
1737
|
+
assert(sw > 0 && sw <= s[1], "arrow(): shaft must be more than 0 and no wider than the head")
|
|
1738
|
+
assert(hl > 0 && hl <= s[0], "arrow(): head must be more than 0 and no longer than the arrow")
|
|
1739
|
+
[[-L, -sw / 2], [L - hl, -sw / 2], [L - hl, -W], [L, 0], [L - hl, W], [L - hl, sw / 2], [-L, sw / 2]];
|
|
1740
|
+
|
|
1741
|
+
// A chevron (>) pointing along x, \`thickness\` across each arm along x
|
|
1742
|
+
module chevron(size = [12, 20], thickness) polygon(chevron_points(size, thickness));
|
|
1743
|
+
|
|
1744
|
+
// The outline of chevron(), as points
|
|
1745
|
+
function chevron_points(size = [12, 20], thickness) =
|
|
1746
|
+
let (
|
|
1747
|
+
s = shape_size(size, "chevron"),
|
|
1748
|
+
w = s[0] / 2,
|
|
1749
|
+
h = s[1] / 2,
|
|
1750
|
+
t = is_undef(thickness) ? s[0] / 2 : thickness
|
|
1751
|
+
)
|
|
1752
|
+
assert(t > 0 && t < s[0], "chevron(): thickness must be more than 0 and less than the width")
|
|
1753
|
+
[[-w, -h], [-w + t, -h], [w, 0], [-w + t, h], [-w, h], [w - t, 0]];
|
|
1754
|
+
|
|
1755
|
+
// A banner with a V cut \`notch\` deep into each end
|
|
1756
|
+
module banner(size = [60, 16], notch) polygon(banner_points(size, notch));
|
|
1757
|
+
|
|
1758
|
+
// The outline of banner(), as points
|
|
1759
|
+
function banner_points(size = [60, 16], notch) =
|
|
1760
|
+
let (
|
|
1761
|
+
s = shape_size(size, "banner"),
|
|
1762
|
+
w = s[0] / 2,
|
|
1763
|
+
h = s[1] / 2,
|
|
1764
|
+
v = is_undef(notch) ? s[1] / 3 : notch
|
|
1765
|
+
)
|
|
1766
|
+
assert(v >= 0 && v < s[0] / 2, "banner(): notch must be less than half the width")
|
|
1767
|
+
[[-w, -h], [w, -h], [w - v, 0], [w, h], [-w, h], [-w + v, 0]];
|
|
1768
|
+
|
|
1769
|
+
// A house: walls and a pitched roof \`roof\` tall
|
|
1770
|
+
module house(size = [20, 24], roof) polygon(house_points(size, roof));
|
|
1771
|
+
|
|
1772
|
+
// The outline of house(), as points
|
|
1773
|
+
function house_points(size = [20, 24], roof) =
|
|
1774
|
+
let (
|
|
1775
|
+
s = shape_size(size, "house"),
|
|
1776
|
+
w = s[0] / 2,
|
|
1777
|
+
h = s[1] / 2,
|
|
1778
|
+
rf = is_undef(roof) ? s[0] / 2 : roof
|
|
1779
|
+
)
|
|
1780
|
+
assert(rf > 0 && rf < s[1], "house(): roof must be more than 0 and less than the height")
|
|
1781
|
+
[[-w, -h], [w, -h], [w, h - rf], [0, h], [-w, h - rf]];
|
|
1782
|
+
|
|
1783
|
+
// A gift or luggage tag: its left end's corners cut off by \`cut\`, with a
|
|
1784
|
+
// hole \`hole\` across near that end (0 for none)
|
|
1785
|
+
module tag(size = [50, 25], cut, hole) {
|
|
1786
|
+
s = shape_size(size, "tag");
|
|
1787
|
+
hd = is_undef(hole) ? s[1] / 4 : hole;
|
|
1788
|
+
difference() {
|
|
1789
|
+
polygon(tag_points(size, cut));
|
|
1790
|
+
if (hd > 0) translate([-s[0] / 2 + s[1] / 3, 0]) circle(d = hd);
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
// The outline of tag(), without its hole, as points
|
|
1795
|
+
function tag_points(size = [50, 25], cut) =
|
|
1796
|
+
let (
|
|
1797
|
+
s = shape_size(size, "tag"),
|
|
1798
|
+
w = s[0] / 2,
|
|
1799
|
+
h = s[1] / 2,
|
|
1800
|
+
c = is_undef(cut) ? s[1] / 4 : cut
|
|
1801
|
+
)
|
|
1802
|
+
assert(c >= 0 && c < min(s[0], s[1] / 2), "tag(): cut must be less than half the height")
|
|
1803
|
+
[[-w + c, -h], [w, -h], [w, h], [-w + c, h], [-w, h - c], [-w, -h + c]];
|
|
1804
|
+
|
|
1805
|
+
// A ring \`d\` across, its band \`width\` wide
|
|
1806
|
+
module ring(d = 20, width = 3) {
|
|
1807
|
+
assert(is_num(d) && d > 0, "ring(): d must be more than 0");
|
|
1808
|
+
assert(width > 0 && width < d / 2, "ring(): width must be more than 0 and less than half of d");
|
|
1809
|
+
difference() {
|
|
1810
|
+
circle(d = d);
|
|
1811
|
+
circle(d = d - 2 * width);
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
// A circle \`d\` across with \`n\` round scallops round its edge
|
|
1816
|
+
module scalloped(d = 30, n = 16) {
|
|
1817
|
+
assert(is_num(d) && d > 0, "scalloped(): d must be more than 0");
|
|
1818
|
+
assert(n >= 3, "scalloped(): n must be 3 or more");
|
|
1819
|
+
k = sin(180 / n);
|
|
1820
|
+
rb = d / 2 / (1 + k);
|
|
1821
|
+
union() {
|
|
1822
|
+
circle(r = rb);
|
|
1823
|
+
for (i = [0:n - 1]) rotate(360 * i / n) translate([rb, 0]) circle(r = rb * k);
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
// A flower \`d\` across with \`petals\` petals round a round middle \`center\`
|
|
1828
|
+
// of d across
|
|
1829
|
+
module flower(d = 30, petals = 6, center = 0.35) {
|
|
1830
|
+
assert(is_num(d) && d > 0, "flower(): d must be more than 0");
|
|
1831
|
+
assert(petals >= 3, "flower(): petals must be 3 or more");
|
|
1832
|
+
assert(center > 0 && center < 1, "flower(): center is a part of d, more than 0 and less than 1");
|
|
1833
|
+
pl = d / 2 * (1 - center / 3);
|
|
1834
|
+
rc = d / 2 - pl / 2;
|
|
1835
|
+
pw = min(pl, 2.2 * rc * sin(180 / petals));
|
|
1836
|
+
union() {
|
|
1837
|
+
circle(d = d * center);
|
|
1838
|
+
for (i = [0:petals - 1]) rotate(90 + 360 * i / petals) translate([rc, 0]) ellipse(d = [pl, pw]);
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
// A cloud: a flat bottom with \`puffs\` round puffs along the top
|
|
1843
|
+
module cloud(size = [40, 24], puffs = 3) {
|
|
1844
|
+
s = shape_size(size, "cloud");
|
|
1845
|
+
assert(puffs >= 1, "cloud(): puffs must be 1 or more");
|
|
1846
|
+
fit(s, mode = "stretch", anchor = "center") union() {
|
|
1847
|
+
translate([0.08, 0]) rect([0.84, 0.3], r = 0.15);
|
|
1848
|
+
for (i = [0:puffs - 1]) let (f = (i + 0.5) / puffs, rise = sin(180 * f), r = (0.3 + 0.2 * rise) / max(1, puffs / 2.2))
|
|
1849
|
+
translate([0.12 + 0.76 * f, 0.18 + 0.32 * rise]) circle(r = r, $fn = shape_steps(150));
|
|
1850
|
+
translate([0.12, 0.17]) circle(r = 0.12, $fn = shape_steps(80));
|
|
1851
|
+
translate([0.88, 0.17]) circle(r = 0.12, $fn = shape_steps(80));
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
// A speech bubble: a rounded box with a tail \`tail\` long at its bottom
|
|
1856
|
+
// left (mirror() it for the right). The tail is part of \`size\`.
|
|
1857
|
+
module speech_bubble(size = [40, 30], r, tail) {
|
|
1858
|
+
s = shape_size(size, "speech_bubble");
|
|
1859
|
+
th = is_undef(tail) ? s[1] / 4 : tail;
|
|
1860
|
+
assert(th > 0 && th < s[1] / 2, "speech_bubble(): tail must be more than 0 and less than half the height");
|
|
1861
|
+
rr = is_undef(r) ? min(s[0], s[1] - th) / 5 : r;
|
|
1862
|
+
bh = s[1] - th;
|
|
1863
|
+
yb = -s[1] / 2 + th;
|
|
1864
|
+
union() {
|
|
1865
|
+
translate([-s[0] / 2, yb]) rect([s[0], bh], r = rr);
|
|
1866
|
+
polygon([[-s[0] * 0.3, yb + min(bh / 2, rr + 1)], [-s[0] * 0.3 + s[0] / 5, yb + min(bh / 2, rr + 1)], [-s[0] * 0.38, -s[1] / 2]]);
|
|
1867
|
+
}
|
|
1868
|
+
}
|
|
1545
1869
|
`,"kerf/sizes":`// kerf/sizes: paper, card and photo sizes, in millimetres, for holders,
|
|
1546
1870
|
// folders, frames and boxes made to fit them.
|
|
1547
1871
|
// Copyright (c) 2026 MyLaserTools. AGPL-3.0-only or MyLaserTools commercial licence.
|
|
@@ -1620,6 +1944,61 @@ function lookup_alias(name, aliases) = let (k = lower(name), hit = [for (e = ali
|
|
|
1620
1944
|
len(hit) ? hit[0] : name;
|
|
1621
1945
|
// The names in a table, for messages
|
|
1622
1946
|
function size_names(table) = join([for (e = table) e[0]], ", ");
|
|
1947
|
+
`,"kerf/stand":`// kerf/stand: a sign, a photo or a calendar standing in a base.
|
|
1948
|
+
// Copyright (c) 2026 MyLaserTools. AGPL-3.0-only or MyLaserTools commercial licence.
|
|
1949
|
+
//
|
|
1950
|
+
// The panel has tabs along its bottom edge that go through slots in a
|
|
1951
|
+
// base, upright or leaning back. stand() makes both parts and puts them
|
|
1952
|
+
// together; stand_tabs() and stand_slots() are the two halves of the joint
|
|
1953
|
+
// for a base of your own, and match whenever they are given the same
|
|
1954
|
+
// width, count, tab and lean. The base is $thickness thick, as the panel.
|
|
1955
|
+
|
|
1956
|
+
use <kerf/tabs>
|
|
1957
|
+
|
|
1958
|
+
// The panel \`width\` wide drawn by the children, its bottom edge on y = 0
|
|
1959
|
+
// from x = 0, standing in a base \`depth\` deep and \`margin\` wider than it
|
|
1960
|
+
// on each side, leaning back by \`lean\` degrees. Its back face meets the
|
|
1961
|
+
// base \`back\` from the front, half way by default. The parts are \`name\`
|
|
1962
|
+
// and "\`name\` stand".
|
|
1963
|
+
module stand(width, depth = 40, count = undef, tab = undef, lean = 0, margin = 10, back = undef, r = 3, clearance = 0.1, name = "Sign") {
|
|
1964
|
+
t = $thickness;
|
|
1965
|
+
assert(width > 0, "stand(): width must be more than 0");
|
|
1966
|
+
assert(lean >= 0 && lean < 45, "stand(): lean is from 0 up to 45 degrees");
|
|
1967
|
+
ys = is_undef(back) ? depth / 2 : back;
|
|
1968
|
+
assert(ys > 0 && ys < depth, "stand(): back must be inside the base's depth");
|
|
1969
|
+
part(name, at = [0, ys, t], rotate = [90 - lean, 0, 0]) {
|
|
1970
|
+
children();
|
|
1971
|
+
stand_tabs(width, count, tab, lean);
|
|
1972
|
+
}
|
|
1973
|
+
part(str(name, " stand"), at = [0, 0, 0]) difference() {
|
|
1974
|
+
translate([-margin, 0]) rect([width + 2 * margin, depth], r = r);
|
|
1975
|
+
translate([0, ys]) stand_slots(width, count, tab, lean, clearance);
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
|
|
1979
|
+
// Tabs for the bottom edge of a panel \`width\` wide, x from 0 to \`width\`,
|
|
1980
|
+
// hanging below y = 0 far enough to go through a base $thickness thick
|
|
1981
|
+
// when the panel leans back by \`lean\` degrees. Each is \`tab\` wide, half
|
|
1982
|
+
// the space between tabs by default; \`count\` is one for every 50 mm.
|
|
1983
|
+
module stand_tabs(width, count = undef, tab = undef, lean = 0) {
|
|
1984
|
+
at = tab_positions(width, count);
|
|
1985
|
+
w = is_undef(tab) ? width / (2 * len(at)) : tab;
|
|
1986
|
+
d = $thickness / cos(lean);
|
|
1987
|
+
for (x = at) translate([x - w / 2, -d]) rect([w, d + 0.01]);
|
|
1988
|
+
}
|
|
1989
|
+
|
|
1990
|
+
// The slots in a base for stand_tabs() with the same width, count, tab
|
|
1991
|
+
// and lean, x from 0 to \`width\`, for a panel whose back face meets the top
|
|
1992
|
+
// of the base on y = 0, standing towards -y and leaning back towards +y.
|
|
1993
|
+
// \`clearance\` is added to each slot's width and depth.
|
|
1994
|
+
module stand_slots(width, count = undef, tab = undef, lean = 0, clearance = 0.1) {
|
|
1995
|
+
at = tab_positions(width, count);
|
|
1996
|
+
w = is_undef(tab) ? width / (2 * len(at)) : tab;
|
|
1997
|
+
t = $thickness;
|
|
1998
|
+
// from the back face at the top to the tab's front corner at its end
|
|
1999
|
+
across = t * cos(lean) + t * tan(lean);
|
|
2000
|
+
for (x = at) translate([x, -across / 2]) rect([w + clearance, across + clearance], center = true);
|
|
2001
|
+
}
|
|
1623
2002
|
`,"kerf/symbols":`// kerf/symbols: 867 pictograms by name, for engraving and cutting.
|
|
1624
2003
|
// The library code: Copyright (c) 2026 MyLaserTools. AGPL-3.0-only or
|
|
1625
2004
|
// MyLaserTools commercial licence. The symbols come from open icon sets,
|
|
@@ -2635,10 +3014,10 @@ module tab_rect(size, edges = true, count = undef, width = undef) {
|
|
|
2635
3014
|
if (e[2]) translate([w, h]) rotate(180) tabs(w, count, width);
|
|
2636
3015
|
if (e[3]) translate([0, h]) rotate(270) tabs(h, count, width);
|
|
2637
3016
|
}
|
|
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,
|
|
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
|
|
2640
|
-
`};async function d1(a,c={}){let l=c.fetch??globalThis.fetch,e=c.log??(()=>{}),n=c.cache??new Map,t=H(a),s=
|
|
2641
|
-
`)&&
|
|
2642
|
-
`)&&(0,
|
|
2643
|
-
`);try{return await d1(a,l)}catch(s){throw n===void 0?(0,
|
|
2644
|
-
`));let i=[...t.keys()].sort().map(d=>[`${e}/${d}`,t.get(d)]),
|
|
3017
|
+
`};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,M.statSync)(a).isFile()}catch{return!1}},V=a=>{try{return(0,M.statSync)(a).isDirectory()}catch{return!1}},S=(a,c)=>a&&Object.prototype.hasOwnProperty.call(a,c)?a[c]:void 0,f=a=>{throw new R(a)};function s1(a,c){return(typeof a!="string"||!W.test(a))&&f(`${c} "${a}" is not a library name: use lowercase letters, digits, - and _`),a==="kerf"&&f(`${c}: "kerf" is the standard library's name`),a}function Y(a,c){(!a||typeof a!="object"||Array.isArray(a))&&f(`${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"&&f(`${c}: "${n}" must be a string`);l.name!==void 0&&s1(l.name,`${c}: name`),l.version!==void 0&&!y1.test(l.version)&&f(`${c}: "version" must look like 1.2.0, not "${l.version}"`),l.language!==void 0&&!/^\d+\.\d+$/.test(l.language)&&f(`${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))&&f(`${c}: "sheet" must be [width, height] in mm, like [600, 400]`);let e=l.libraries??{};(!e||typeof e!="object"||Array.isArray(e))&&f(`${c}: "libraries" must be an object of name: source`);for(let[n,t]of Object.entries(e))s1(n,`${c}: library`),typeof t!="string"&&f(`${c}: the source of ${n} must be a string`);return{...l,libraries:{...e}}}function Q(a,c){let l;try{l=(0,M.readFileSync)(a,"utf8")}catch{return f(`${c} was not found`)}try{return JSON.parse(l)}catch(e){return f(`${c} is not valid JSON: ${e.message}`)}}function H(a,c=u){return Y(Q((0,v.join)(a,u),c),c)}function J(a){if(!(0,M.existsSync)((0,v.join)(a,C)))return;let c=Q((0,v.join)(a,C),C);(c?.lockVersion!==1||!c.libraries||typeof c.libraries!="object")&&f(`${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"))&&f(`${C}: the entry for ${l} is damaged. Remove it and run kerf install.`);return c}function X(a){for(let c=(0,v.resolve)(a);;){if(U((0,v.join)(c,u)))return c;let l=(0,v.dirname)(c);if(l===c)return;c=l}}function c1(a){try{return H(a).name}catch{return}}function r1(a,c,l=U){let e=(s,h)=>[(0,v.join)(s,h),(0,v.join)(s,h+".kerf")].find(i=>l(i));if((0,v.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,v.join)(t,y,s)))return e((0,v.join)(t,y),c)}return e(a,c)}function k1(a){let c=(0,v.dirname)((0,v.resolve)(a));return l=>{let e=r1(c,l);return e===void 0?void 0:(0,M.readFileSync)(e,"utf8")}}function C1(a,c=200){let l=X(a);if(!l)return[];let e=[],n=(h,i)=>{for(let r of[...m1(h,3)].sort()){if(e.length>=c)return;r.endsWith(".kerf")&&e.push(i+r.slice(0,-5))}},t=c1(l);t&&n(l,t+"/");let s=(0,v.join)(l,y);if(V(s))for(let h of(0,M.readdirSync)(s).sort())W.test(h)&&V((0,v.join)(s,h))&&n((0,v.join)(s,h),h+"/");return e}function m1(a,c=1/0){let l=[],e=(n,t,s)=>{let h;try{h=(0,M.readdirSync)(n,{withFileTypes:!0})}catch{return}for(let i of h){if(i.name.startsWith(".")||i.name==="node_modules"||i.name===y)continue;let r=t?`${t}/${i.name}`:i.name;i.isDirectory()?s<c&&e((0,v.join)(n,i.name),r,s+1):i.isFile()&&(l.push(r),l.length>I&&f(`${a} has more than ${I} files`))}};return e(a,"",0),l}function O(a,c=()=>!0){let l=new Map;for(let e of m1(a))c(e)&&l.set(e,(0,M.readFileSync)((0,v.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,o1.createHash)("sha256");for(let l of[...a.keys()].sort()){let e=Buffer.from(a.get(l).toString("utf8").replace(/\r\n/g,`
|
|
3018
|
+
`));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 f(`${a} is not a link`)}return n.protocol==="http:"&&!["localhost","127.0.0.1","[::1]"].includes(n.hostname)&&f(`Use https:// to download ${a}`),{kind:"url",url:a}}let e=a.startsWith("file:")?a.slice(5):a;return e.startsWith("./")||e.startsWith("../")||(0,v.isAbsolute)(e)?{kind:"folder",path:(0,v.resolve)(c,e)}:f(`"${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 f(`Could not download ${a}: ${i.cause?.message??i.message}`)}l.ok||f(`Could not download ${a}: HTTP ${l.status}`+(l.status===404?". Check the name and the tag, and that it is public.":""));let e=()=>f(`${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 f(`${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 r=(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(r(124,12).trim()||"0",8);d>=0||f(`${c} is damaged`);let z=r(156,1)||"0",o=l.subarray(h+512,h+512+d);if(h+=512+Math.ceil(d/512)*512,z==="x"||z==="g"){let b=q1(o);z==="g"?n=b.comment??n:t=b;continue}if(z==="L"){s=o.toString("utf8").replace(/\0+$/,"");continue}let m=r(345,155),g=t.path??s??(m?m+"/":"")+r(0,100);t={},s=void 0,(z==="0"||z==="7")&&e.set(g,Buffer.from(o)),e.size>I&&f(`${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,v.isAbsolute)(h)||h.includes("\\")||h.includes(":")||i.some(r=>r===".."||r===""))&&f(`${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)||f(`${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 v1(a,c,l,e,n){let t=E(a,c),s,h;if(t.kind==="folder")U((0,v.join)(t.path,u))||f(`${a}: no kerf.json in ${t.path}. Is it a KerfScript library?`),s=i1(O(t.path,l1),a),h=(0,v.relative)(l,t.path).split(v.sep).join("/")||".",!h.startsWith(".")&&!(0,v.isAbsolute)(h)&&(h="./"+h);else{let r=t.kind==="github"?`https://github.com/${t.owner}/${t.repo}/archive/${t.ref}.tar.gz`:t.url,d=n.get(r)??await H1(r,e);n.set(r,d);let z=V1(d,a);s=i1(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`:r}let i=Y(JSON.parse(s.get(u).toString("utf8")),`the kerf.json of ${a}`);return i.name||f(`${a}: its kerf.json has no "name", so it is not a library`),i.language&&x1(i.language,P)&&f(`${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,M.rmSync)(a,{recursive:!0,force:!0});for(let[l,e]of c){let n=(0,v.join)(a,...l.split("/"));(0,M.mkdirSync)((0,v.dirname)(n),{recursive:!0}),(0,M.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)+`
|
|
3019
|
+
`};async function d1(a,c={}){let l=c.fetch??globalThis.fetch,e=c.log??(()=>{}),n=c.cache??new Map,t=H(a),s=J(a),h=new Map,i=Object.entries(t.libraries).map(([o,m])=>({name:o,source:m,base:a,from:u})),r=(0,v.join)(a,y);for(;i.length;){let{name:o,source:m,base:g,from:b}=i.shift(),A=E(m,g),L=A.kind==="folder",q=L?`${m} (${A.path})`:m,$=h.get(o);if($){$.at!==h1(A)&&!S(t.libraries,o)&&f(`${b} uses ${o} from ${q}, and ${$.from} uses it from ${$.shown}. Add ${o} to kerf.json with the source to use.`);continue}let G=S(s?.libraries,o),w=G?.source===m&&(!L||(0,v.resolve)(a,G.resolved)===A.path)?G:void 0;c.frozen&&!w&&f(`kerf.lock does not pin ${o} from ${m}. Run kerf install without --frozen.`);let j=(0,v.join)(r,o),x;if(w&&!L&&V(j)&&T(O(j))===w.integrity)x=w;else{let p=await v1(w&&!L?w.resolved:m,L?g:a,a,l,n);p.manifest.name!==o&&f(`${m} is the library "${p.manifest.name}", so ${b} must call it ${p.manifest.name}, not ${o}`),w&&p.integrity!==w.integrity&&(!L||c.frozen)&&f(L?`${o} in ${m} changed since kerf.lock pinned it. Run kerf install without --frozen.`:`${o} from ${w.resolved} no longer matches kerf.lock. If you trust the change, remove ${o} from kerf.lock and run kerf install again.`),_1(j,p.files),x={source:m,...p.manifest.version?{version:p.manifest.version}:{},resolved:p.resolved,integrity:p.integrity},(!w||w.integrity!==p.integrity||!L)&&e(`${w?"Restored":"Installed"} ${o}${x.version?" "+x.version:""} from ${m}`)}h.set(o,{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(j,`${y}/${o}/${u}`).libraries))E(F,a).kind==="folder"&&!a1&&f(`${o} uses ${p} from the folder ${F}: a published library needs github: or https: sources`),i.push({name:p,source:F,base:a1??a,from:o})}let d=S1(new Map([...h].map(([o,m])=>[o,m.entry]))),z=(0,M.existsSync)((0,v.join)(a,C))?(0,M.readFileSync)((0,v.join)(a,C),"utf8"):"";if(c.frozen&&d!==z.replace(/\r\n/g,`
|
|
3020
|
+
`)&&f("kerf.lock is out of date. Run kerf install without --frozen."),V(r))for(let o of(0,M.readdirSync)(r,{withFileTypes:!0}))o.isDirectory()&&W.test(o.name)&&!h.has(o.name)&&((0,M.rmSync)((0,v.join)(r,o.name),{recursive:!0,force:!0}),e(`Removed ${o.name}`));return d!==z.replace(/\r\n/g,`
|
|
3021
|
+
`)&&(0,M.writeFileSync)((0,v.join)(a,C),d),JSON.parse(d)}function T1(a){let c=H(a),l=J(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 r=(0,v.join)(a,y,h);if(!V(r)){n.push(`${h} is not in ${y}. Run kerf install.`);continue}T(O(r))!==i.integrity&&n.push(`The files in ${y}/${h} differ from kerf.lock. Run kerf install.`);try{s.push(...Object.keys(H(r).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 $1(a,c){let l=(0,v.join)(a,u),e=(0,M.existsSync)(l)?Q(l,u):{};Y(e,u);let n={...e.libraries??{}};return c(n),e.libraries=n,{file:l,before:(0,M.existsSync)(l)?(0,M.readFileSync)(l,"utf8"):void 0,raw:e}}async function f1(a,c,l){let{file:e,before:n,raw:t}=$1(a,c);(0,M.writeFileSync)(e,JSON.stringify(t,null,2)+`
|
|
3022
|
+
`);try{return await d1(a,l)}catch(s){throw n===void 0?(0,M.rmSync)(e,{force:!0}):(0,M.writeFileSync)(e,n),s}}async function j1(a,c,l={}){let e=new Map,t=(await v1(c,a,a,l.fetch??globalThis.fetch,e)).manifest.name;t===c1(a)&&f(`This project is the library ${t}; it cannot use itself`);let s=await f1(a,h=>void(h[t]=c),{...l,cache:e});return{name:t,lock:s}}async function P1(a,c,l={}){return f1(a,e=>{S(e,c)===void 0&&f(`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)&&f(`${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??f('kerf.json needs a "name" to pack a library'),n=l.version??f('kerf.json needs a "version", like 1.0.0'),t=O(a,l1),s=[];for(let[d,z]of Object.entries(l.libraries))E(z,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,z]of t){if(!d.endsWith(".kerf"))continue;let o;try{o=e1(z.toString("utf8"),`${e}/${d}`).program}catch(m){s.push(`${d}: ${m.message}`);continue}for(let m of N(o)){let g=m.path.split("/")[0];Object.prototype.hasOwnProperty.call(t1,m.path)||g===e&&h(m.path.slice(e.length+1))||m.path.includes("/")&&S(l.libraries,g)!==void 0||s.push(`${d}, line ${m.line}: <${m.path}> will not be found once the library is installed`+(h(m.path)?`. Write <${e}/${m.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&&f(s.join(`
|
|
3023
|
+
`));let i=[...t.keys()].sort().map(d=>[`${e}/${d}`,t.get(d)]),r=c??(0,v.join)(a,`${e}-${n}.tar.gz`);return(0,M.writeFileSync)(r,I1(i)),{file:r,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});
|