formacad 0.0.0-stage → 0.2.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.
Files changed (97) hide show
  1. package/LICENSE +200 -0
  2. package/README.md +98 -2
  3. package/build/cli/src/forma.d.ts +2 -0
  4. package/build/cli/src/forma.js +546 -0
  5. package/build/cli/src/forma.js.map +1 -0
  6. package/build/cli/src/mcp.d.ts +1 -0
  7. package/build/cli/src/mcp.js +495 -0
  8. package/build/cli/src/mcp.js.map +1 -0
  9. package/build/cli/src/preview.d.ts +31 -0
  10. package/build/cli/src/preview.js +427 -0
  11. package/build/cli/src/preview.js.map +1 -0
  12. package/build/core/fonts/Roboto-Bold.ttf +0 -0
  13. package/build/core/fonts/Roboto-Regular.ttf +0 -0
  14. package/build/core/src/bevel.d.ts +41 -0
  15. package/build/core/src/bevel.js +548 -0
  16. package/build/core/src/bevel.js.map +1 -0
  17. package/build/core/src/difflib.d.ts +3 -0
  18. package/build/core/src/difflib.js +94 -0
  19. package/build/core/src/difflib.js.map +1 -0
  20. package/build/core/src/errors.d.ts +9 -0
  21. package/build/core/src/errors.js +14 -0
  22. package/build/core/src/errors.js.map +1 -0
  23. package/build/core/src/export2d.d.ts +10 -0
  24. package/build/core/src/export2d.js +127 -0
  25. package/build/core/src/export2d.js.map +1 -0
  26. package/build/core/src/geometry.d.ts +93 -0
  27. package/build/core/src/geometry.js +946 -0
  28. package/build/core/src/geometry.js.map +1 -0
  29. package/build/core/src/gltf.d.ts +6 -0
  30. package/build/core/src/gltf.js +154 -0
  31. package/build/core/src/gltf.js.map +1 -0
  32. package/build/core/src/host.d.ts +28 -0
  33. package/build/core/src/host.js +113 -0
  34. package/build/core/src/host.js.map +1 -0
  35. package/build/core/src/importers.d.ts +12 -0
  36. package/build/core/src/importers.js +1025 -0
  37. package/build/core/src/importers.js.map +1 -0
  38. package/build/core/src/index.d.ts +92 -0
  39. package/build/core/src/index.js +227 -0
  40. package/build/core/src/index.js.map +1 -0
  41. package/build/core/src/inflate.d.ts +15 -0
  42. package/build/core/src/inflate.js +230 -0
  43. package/build/core/src/inflate.js.map +1 -0
  44. package/build/core/src/interp.d.ts +178 -0
  45. package/build/core/src/interp.js +3012 -0
  46. package/build/core/src/interp.js.map +1 -0
  47. package/build/core/src/lexer.d.ts +14 -0
  48. package/build/core/src/lexer.js +217 -0
  49. package/build/core/src/lexer.js.map +1 -0
  50. package/build/core/src/manifold.d.ts +26 -0
  51. package/build/core/src/manifold.js +687 -0
  52. package/build/core/src/manifold.js.map +1 -0
  53. package/build/core/src/modeling.d.ts +18 -0
  54. package/build/core/src/modeling.js +483 -0
  55. package/build/core/src/modeling.js.map +1 -0
  56. package/build/core/src/parser.d.ts +46 -0
  57. package/build/core/src/parser.js +478 -0
  58. package/build/core/src/parser.js.map +1 -0
  59. package/build/core/src/scad/colors.d.ts +1 -0
  60. package/build/core/src/scad/colors.js +37 -0
  61. package/build/core/src/scad/colors.js.map +1 -0
  62. package/build/core/src/scad/convert.d.ts +33 -0
  63. package/build/core/src/scad/convert.js +2842 -0
  64. package/build/core/src/scad/convert.js.map +1 -0
  65. package/build/core/src/scad/parser.d.ts +239 -0
  66. package/build/core/src/scad/parser.js +578 -0
  67. package/build/core/src/scad/parser.js.map +1 -0
  68. package/build/core/src/scad/sources.d.ts +39 -0
  69. package/build/core/src/scad/sources.js +171 -0
  70. package/build/core/src/scad/sources.js.map +1 -0
  71. package/build/core/src/std.d.ts +3 -0
  72. package/build/core/src/std.js +47 -0
  73. package/build/core/src/std.js.map +1 -0
  74. package/build/core/src/surface.d.ts +21 -0
  75. package/build/core/src/surface.js +253 -0
  76. package/build/core/src/surface.js.map +1 -0
  77. package/build/core/src/threemf.d.ts +4 -0
  78. package/build/core/src/threemf.js +127 -0
  79. package/build/core/src/threemf.js.map +1 -0
  80. package/build/core/src/ttf.d.ts +39 -0
  81. package/build/core/src/ttf.js +473 -0
  82. package/build/core/src/ttf.js.map +1 -0
  83. package/build/core/src/values.d.ts +146 -0
  84. package/build/core/src/values.js +365 -0
  85. package/build/core/src/values.js.map +1 -0
  86. package/build/core/src/xml.d.ts +13 -0
  87. package/build/core/src/xml.js +152 -0
  88. package/build/core/src/xml.js.map +1 -0
  89. package/build/core/std/electronics.forma +120 -0
  90. package/build/core/std/enclosures.forma +76 -0
  91. package/build/core/std/fasteners.forma +170 -0
  92. package/build/core/std/gridfinity.forma +182 -0
  93. package/build/core/std/joints.forma +120 -0
  94. package/build/core/std/mechanics.forma +276 -0
  95. package/build/core/std/patterns.forma +138 -0
  96. package/build/core/std/threads.forma +152 -0
  97. package/package.json +59 -4
@@ -0,0 +1,127 @@
1
+ // 3MF exporter (ZIP archive, stored, with XML model and per-triangle colours).
2
+ // Zero runtime dependencies — works identically in Node.js (CLI) and browsers.
3
+ let CRC_TABLE = null;
4
+ function crc32(data) {
5
+ if (!CRC_TABLE) {
6
+ CRC_TABLE = new Uint32Array(256);
7
+ for (let n = 0; n < 256; n++) {
8
+ let c = n;
9
+ for (let k = 0; k < 8; k++)
10
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
11
+ CRC_TABLE[n] = c >>> 0;
12
+ }
13
+ }
14
+ let c = 0xffffffff;
15
+ for (let i = 0; i < data.length; i++)
16
+ c = CRC_TABLE[(c ^ data[i]) & 0xff] ^ (c >>> 8);
17
+ return (c ^ 0xffffffff) >>> 0;
18
+ }
19
+ export function zipStore(files) {
20
+ const enc = new TextEncoder();
21
+ const chunks = [];
22
+ const central = [];
23
+ let offset = 0;
24
+ for (const [name, data] of files) {
25
+ const nameBytes = enc.encode(name);
26
+ const crc = crc32(data);
27
+ const local = new Uint8Array(30 + nameBytes.length);
28
+ const lv = new DataView(local.buffer);
29
+ lv.setUint32(0, 0x04034b50, true);
30
+ lv.setUint16(4, 20, true);
31
+ lv.setUint16(6, 0x800, true);
32
+ lv.setUint32(14, crc, true);
33
+ lv.setUint32(18, data.length, true);
34
+ lv.setUint32(22, data.length, true);
35
+ lv.setUint16(26, nameBytes.length, true);
36
+ local.set(nameBytes, 30);
37
+ const cen = new Uint8Array(46 + nameBytes.length);
38
+ const cv = new DataView(cen.buffer);
39
+ cv.setUint32(0, 0x02014b50, true);
40
+ cv.setUint16(4, 20, true);
41
+ cv.setUint16(6, 20, true);
42
+ cv.setUint16(8, 0x800, true);
43
+ cv.setUint32(16, crc, true);
44
+ cv.setUint32(20, data.length, true);
45
+ cv.setUint32(24, data.length, true);
46
+ cv.setUint16(28, nameBytes.length, true);
47
+ cv.setUint32(42, offset, true);
48
+ cen.set(nameBytes, 46);
49
+ chunks.push(local, data);
50
+ central.push(cen);
51
+ offset += local.length + data.length;
52
+ }
53
+ const cenSize = central.reduce((s, c) => s + c.length, 0);
54
+ const end = new Uint8Array(22);
55
+ const ev = new DataView(end.buffer);
56
+ ev.setUint32(0, 0x06054b50, true);
57
+ ev.setUint16(8, files.length, true);
58
+ ev.setUint16(10, files.length, true);
59
+ ev.setUint32(12, cenSize, true);
60
+ ev.setUint32(16, offset, true);
61
+ const all = [...chunks, ...central, end];
62
+ const out = new Uint8Array(all.reduce((s, c) => s + c.length, 0));
63
+ let o = 0;
64
+ for (const c of all) {
65
+ out.set(c, o);
66
+ o += c.length;
67
+ }
68
+ return out;
69
+ }
70
+ /** Binary 3MF archive (with per-triangle colours for multi-colour slicers). */
71
+ export function threeMfBytes(polys, name = 'Forma model', defaultColor = '#b8c4d6') {
72
+ const index = new Map();
73
+ const verts = [];
74
+ const tris = [];
75
+ const colors = [];
76
+ const colorIndex = new Map();
77
+ const fmt = (x) => (Math.round(x * 1e5) / 1e5).toString();
78
+ const vertId = (x, y, z) => {
79
+ const sx = fmt(x), sy = fmt(y), sz = fmt(z);
80
+ const key = sx + ' ' + sy + ' ' + sz;
81
+ let id = index.get(key);
82
+ if (id === undefined) {
83
+ id = verts.length;
84
+ index.set(key, id);
85
+ verts.push(`<vertex x="${sx}" y="${sy}" z="${sz}"/>`);
86
+ }
87
+ return id;
88
+ };
89
+ for (const p of polys) {
90
+ const v = p.v;
91
+ if (v.length < 3)
92
+ continue;
93
+ const raw = (p.c || defaultColor).toUpperCase();
94
+ const col = raw.length === 7 ? raw + 'FF' : raw;
95
+ let ci = colorIndex.get(col);
96
+ if (ci === undefined) {
97
+ ci = colors.length;
98
+ colorIndex.set(col, ci);
99
+ colors.push(col);
100
+ }
101
+ const i0 = vertId(v[0][0], v[0][1], v[0][2]);
102
+ for (let i = 1; i < v.length - 1; i++) {
103
+ const i1 = vertId(v[i][0], v[i][1], v[i][2]);
104
+ const i2 = vertId(v[i + 1][0], v[i + 1][1], v[i + 1][2]);
105
+ if (i0 === i1 || i1 === i2 || i0 === i2)
106
+ continue;
107
+ tris.push(`<triangle v1="${i0}" v2="${i1}" v3="${i2}" pid="1" p1="${ci}"/>`);
108
+ }
109
+ }
110
+ const esc = (s) => s.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;');
111
+ const model = `<?xml version="1.0" encoding="UTF-8"?>
112
+ <model unit="millimeter" xml:lang="en-US" xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02">
113
+ <metadata name="Application">Forma</metadata>
114
+ <resources>
115
+ <basematerials id="1">${colors.map((c, i) => `<base name="Color ${i + 1}" displaycolor="${c}"/>`).join('')}</basematerials>
116
+ <object id="2" type="model" name="${esc(name)}" pid="1" pindex="0"><mesh><vertices>${verts.join('')}</vertices><triangles>${tris.join('')}</triangles></mesh></object>
117
+ </resources>
118
+ <build><item objectid="2"/></build>
119
+ </model>`;
120
+ const enc = new TextEncoder();
121
+ return zipStore([
122
+ ['[Content_Types].xml', enc.encode('<?xml version="1.0" encoding="UTF-8"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="model" ContentType="application/vnd.ms-package.3dmanufacturing-3dmodel+xml"/></Types>')],
123
+ ['_rels/.rels', enc.encode('<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Target="/3D/3dmodel.model" Id="rel0" Type="http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel"/></Relationships>')],
124
+ ['3D/3dmodel.model', enc.encode(model)],
125
+ ]);
126
+ }
127
+ //# sourceMappingURL=threemf.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"threemf.js","sourceRoot":"","sources":["../../../packages/core/src/threemf.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,+EAA+E;AAI/E,IAAI,SAAS,GAAuB,IAAI,CAAC;AAEzC,SAAS,KAAK,CAAC,IAAgB;IAC7B,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,SAAS,GAAG,IAAI,WAAW,CAAC,GAAG,CAAC,CAAC;QACjC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;YAC7B,IAAI,CAAC,GAAG,CAAC,CAAC;YACV,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;gBAAE,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;YACzE,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACzB,CAAC;IACH,CAAC;IACD,IAAI,CAAC,GAAG,UAAU,CAAC;IACnB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE;QAAE,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IACtF,OAAO,CAAC,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC;AAChC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AE,OAAO,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAClG,MAAM,KAAK,GAAG;;;;wBAIQ,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,qBAAqB,CAAC,GAAG,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC;oCACtE,GAAG,CAAC,IAAI,CAAC,wCAAwC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,yBAAyB,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;;;SAGhI,CAAC;IACR,MAAM,GAAG,GAAG,IAAI,WAAW,EAAE,CAAC;IAC9B,OAAO,QAAQ,CAAC;QACd,CAAC,qBAAqB,EAAE,GAAG,CAAC,MAAM,CAAC,+TAA+T,CAAC,CAAC;QACpW,CAAC,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,oQAAoQ,CAAC,CAAC;QACjS,CAAC,kBAAkB,EAAE,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;KACxC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,39 @@
1
+ import { Vec2 } from './geometry.js';
2
+ export declare class TtfFont {
3
+ private dv;
4
+ private tables;
5
+ readonly unitsPerEm: number;
6
+ readonly ascent: number;
7
+ readonly descent: number;
8
+ readonly lineGap: number;
9
+ private indexToLocFormat;
10
+ private numGlyphs;
11
+ private numHMetrics;
12
+ private locaOffset;
13
+ private glyfOffset;
14
+ private hmtxOffset;
15
+ private cmapLookup;
16
+ private kerning;
17
+ private glyphCache;
18
+ constructor(data: Uint8Array);
19
+ private parseCmap;
20
+ private parseKern;
21
+ glyphId(codePoint: number): number;
22
+ advanceWidth(gid: number): number;
23
+ kern(leftGid: number, rightGid: number): number;
24
+ private glyphBounds;
25
+ /** Extract flattened contours for glyph `gid` in font units (CCW outer / CW hole). */
26
+ glyphContours(gid: number, curveSteps: number, depth?: number): Vec2[][];
27
+ }
28
+ export interface TextLayoutOptions {
29
+ size: number;
30
+ spacing: number;
31
+ lineHeight: number;
32
+ align: 'left' | 'center' | 'right';
33
+ valign: 'baseline' | 'bottom' | 'center' | 'top';
34
+ curveSteps: number;
35
+ }
36
+ /** Lay out single- or multi-line Unicode text into 2D contours in millimetres. */
37
+ export declare function layoutText(font: TtfFont, text: string, opts: TextLayoutOptions): Vec2[][];
38
+ export declare function getBundledFont(weight: 'regular' | 'bold'): TtfFont | null;
39
+ export declare function parseTtfBytes(bytes: Uint8Array): TtfFont;
@@ -0,0 +1,473 @@
1
+ // Zero-dependency TrueType (.ttf) glyph outline parser & multilingual text layout.
2
+ // Supports simple & composite glyphs (Latin Extended, Cyrillic/Ukrainian, Greek, etc.),
3
+ // cmap format 4 & 12, format 0 kerning, and bundled Roboto (Regular & Bold).
4
+ export class TtfFont {
5
+ dv;
6
+ tables = new Map();
7
+ unitsPerEm;
8
+ ascent;
9
+ descent;
10
+ lineGap;
11
+ indexToLocFormat;
12
+ numGlyphs;
13
+ numHMetrics;
14
+ locaOffset;
15
+ glyfOffset;
16
+ hmtxOffset;
17
+ cmapLookup;
18
+ kerning = new Map();
19
+ glyphCache = new Map();
20
+ constructor(data) {
21
+ this.dv = new DataView(data.buffer, data.byteOffset, data.byteLength);
22
+ if (data.byteLength < 12)
23
+ throw new Error('file is too small to be a TrueType font');
24
+ const numTables = this.dv.getUint16(4, false);
25
+ for (let i = 0; i < numTables; i++) {
26
+ const p = 12 + i * 16;
27
+ if (p + 16 > data.byteLength)
28
+ break;
29
+ const tag = String.fromCharCode(data[p], data[p + 1], data[p + 2], data[p + 3]);
30
+ this.tables.set(tag, {
31
+ offset: this.dv.getUint32(p + 8, false),
32
+ length: this.dv.getUint32(p + 12, false),
33
+ });
34
+ }
35
+ const req = (t) => {
36
+ const e = this.tables.get(t);
37
+ if (!e)
38
+ throw new Error(`TrueType font is missing required '${t}' table (only .ttf glyph outlines are supported)`);
39
+ return e;
40
+ };
41
+ const head = req('head');
42
+ const hhea = req('hhea');
43
+ const maxp = req('maxp');
44
+ const hmtx = req('hmtx');
45
+ const loca = req('loca');
46
+ const glyf = req('glyf');
47
+ const cmap = req('cmap');
48
+ this.unitsPerEm = this.dv.getUint16(head.offset + 18, false);
49
+ this.indexToLocFormat = this.dv.getInt16(head.offset + 50, false);
50
+ this.ascent = this.dv.getInt16(hhea.offset + 4, false);
51
+ this.descent = this.dv.getInt16(hhea.offset + 6, false);
52
+ this.lineGap = this.dv.getInt16(hhea.offset + 8, false);
53
+ this.numHMetrics = this.dv.getUint16(hhea.offset + 34, false);
54
+ this.numGlyphs = this.dv.getUint16(maxp.offset + 4, false);
55
+ this.hmtxOffset = hmtx.offset;
56
+ this.locaOffset = loca.offset;
57
+ this.glyfOffset = glyf.offset;
58
+ this.cmapLookup = this.parseCmap(cmap.offset);
59
+ const kern = this.tables.get('kern');
60
+ if (kern)
61
+ this.parseKern(kern.offset);
62
+ }
63
+ parseCmap(base) {
64
+ const numSub = this.dv.getUint16(base + 2, false);
65
+ let fmt12 = -1;
66
+ let fmt4 = -1;
67
+ for (let i = 0; i < numSub; i++) {
68
+ const rec = base + 4 + i * 8;
69
+ const pid = this.dv.getUint16(rec, false);
70
+ const eid = this.dv.getUint16(rec + 2, false);
71
+ const sub = base + this.dv.getUint32(rec + 4, false);
72
+ const fmt = this.dv.getUint16(sub, false);
73
+ if (fmt === 12 && ((pid === 3 && eid === 10) || pid === 0))
74
+ fmt12 = sub;
75
+ else if (fmt === 4 && ((pid === 3 && eid === 1) || pid === 0) && fmt4 < 0)
76
+ fmt4 = sub;
77
+ }
78
+ if (fmt12 >= 0) {
79
+ const nGroups = this.dv.getUint32(fmt12 + 12, false);
80
+ const groups = [];
81
+ for (let i = 0; i < nGroups; i++) {
82
+ const g = fmt12 + 16 + i * 12;
83
+ groups.push([
84
+ this.dv.getUint32(g, false),
85
+ this.dv.getUint32(g + 4, false),
86
+ this.dv.getUint32(g + 8, false),
87
+ ]);
88
+ }
89
+ return (cp) => {
90
+ let lo = 0, hi = groups.length - 1;
91
+ while (lo <= hi) {
92
+ const mid = (lo + hi) >>> 1;
93
+ const [s, e, gid] = groups[mid];
94
+ if (cp < s)
95
+ hi = mid - 1;
96
+ else if (cp > e)
97
+ lo = mid + 1;
98
+ else
99
+ return gid + (cp - s);
100
+ }
101
+ return 0;
102
+ };
103
+ }
104
+ if (fmt4 >= 0) {
105
+ const segCount = this.dv.getUint16(fmt4 + 6, false) >>> 1;
106
+ const endCodes = fmt4 + 14;
107
+ const startCodes = endCodes + segCount * 2 + 2;
108
+ const idDeltas = startCodes + segCount * 2;
109
+ const idRangeOffsets = idDeltas + segCount * 2;
110
+ return (cp) => {
111
+ if (cp > 0xffff)
112
+ return 0;
113
+ let lo = 0, hi = segCount - 1;
114
+ while (lo <= hi) {
115
+ const mid = (lo + hi) >>> 1;
116
+ const end = this.dv.getUint16(endCodes + mid * 2, false);
117
+ const start = this.dv.getUint16(startCodes + mid * 2, false);
118
+ if (cp < start)
119
+ hi = mid - 1;
120
+ else if (cp > end)
121
+ lo = mid + 1;
122
+ else {
123
+ const roPos = idRangeOffsets + mid * 2;
124
+ const ro = this.dv.getUint16(roPos, false);
125
+ const delta = this.dv.getInt16(idDeltas + mid * 2, false);
126
+ if (ro === 0)
127
+ return (cp + delta) & 0xffff;
128
+ const gidAddr = roPos + ro + (cp - start) * 2;
129
+ const gid = this.dv.getUint16(gidAddr, false);
130
+ return gid === 0 ? 0 : (gid + delta) & 0xffff;
131
+ }
132
+ }
133
+ return 0;
134
+ };
135
+ }
136
+ throw new Error('TrueType font has no Unicode cmap table (format 4 or 12)');
137
+ }
138
+ parseKern(base) {
139
+ const nTables = this.dv.getUint16(base + 2, false);
140
+ let p = base + 4;
141
+ for (let t = 0; t < nTables; t++) {
142
+ if (p + 6 > this.dv.byteLength)
143
+ break;
144
+ const len = this.dv.getUint16(p + 2, false);
145
+ const cov = this.dv.getUint16(p + 4, false);
146
+ const format = cov >>> 8;
147
+ if (format === 0 && (cov & 1) === 1) {
148
+ const nPairs = this.dv.getUint16(p + 6, false);
149
+ let q = p + 14;
150
+ for (let i = 0; i < nPairs; i++) {
151
+ const left = this.dv.getUint16(q, false);
152
+ const right = this.dv.getUint16(q + 2, false);
153
+ const val = this.dv.getInt16(q + 4, false);
154
+ this.kerning.set((left << 16) | right, val);
155
+ q += 6;
156
+ }
157
+ }
158
+ p += len;
159
+ }
160
+ }
161
+ glyphId(codePoint) {
162
+ return this.cmapLookup(codePoint);
163
+ }
164
+ advanceWidth(gid) {
165
+ const idx = Math.min(gid, this.numHMetrics - 1);
166
+ return this.dv.getUint16(this.hmtxOffset + idx * 4, false);
167
+ }
168
+ kern(leftGid, rightGid) {
169
+ return this.kerning.get((leftGid << 16) | rightGid) ?? 0;
170
+ }
171
+ glyphBounds(gid) {
172
+ if (gid < 0 || gid >= this.numGlyphs)
173
+ return [0, 0];
174
+ if (this.indexToLocFormat === 0) {
175
+ const a = this.dv.getUint16(this.locaOffset + gid * 2, false) * 2;
176
+ const b = this.dv.getUint16(this.locaOffset + (gid + 1) * 2, false) * 2;
177
+ return [a, b - a];
178
+ }
179
+ const a = this.dv.getUint32(this.locaOffset + gid * 4, false);
180
+ const b = this.dv.getUint32(this.locaOffset + (gid + 1) * 4, false);
181
+ return [a, b - a];
182
+ }
183
+ /** Extract flattened contours for glyph `gid` in font units (CCW outer / CW hole). */
184
+ glyphContours(gid, curveSteps, depth = 0) {
185
+ if (depth > 8)
186
+ return [];
187
+ const cacheKey = (gid << 8) | Math.min(curveSteps, 255);
188
+ const cached = this.glyphCache.get(cacheKey);
189
+ if (cached)
190
+ return cached;
191
+ const [relOff, len] = this.glyphBounds(gid);
192
+ if (len <= 0)
193
+ return [];
194
+ const base = this.glyfOffset + relOff;
195
+ const nContours = this.dv.getInt16(base, false);
196
+ const out = [];
197
+ if (nContours > 0) {
198
+ const endPts = [];
199
+ for (let i = 0; i < nContours; i++)
200
+ endPts.push(this.dv.getUint16(base + 10 + i * 2, false));
201
+ const numPts = endPts[nContours - 1] + 1;
202
+ let p = base + 10 + nContours * 2;
203
+ const instrLen = this.dv.getUint16(p, false);
204
+ p += 2 + instrLen;
205
+ const flags = new Uint8Array(numPts);
206
+ for (let i = 0; i < numPts; i++) {
207
+ const f = this.dv.getUint8(p++);
208
+ flags[i] = f;
209
+ if (f & 0x08) {
210
+ const repeat = this.dv.getUint8(p++);
211
+ for (let r = 0; r < repeat && i + 1 < numPts; r++)
212
+ flags[++i] = f;
213
+ }
214
+ }
215
+ const xs = new Float64Array(numPts);
216
+ let x = 0;
217
+ for (let i = 0; i < numPts; i++) {
218
+ const f = flags[i];
219
+ if (f & 0x02) {
220
+ const dx = this.dv.getUint8(p++);
221
+ x += (f & 0x10) ? dx : -dx;
222
+ }
223
+ else if (!(f & 0x10)) {
224
+ x += this.dv.getInt16(p, false);
225
+ p += 2;
226
+ }
227
+ xs[i] = x;
228
+ }
229
+ const ys = new Float64Array(numPts);
230
+ let y = 0;
231
+ for (let i = 0; i < numPts; i++) {
232
+ const f = flags[i];
233
+ if (f & 0x04) {
234
+ const dy = this.dv.getUint8(p++);
235
+ y += (f & 0x20) ? dy : -dy;
236
+ }
237
+ else if (!(f & 0x20)) {
238
+ y += this.dv.getInt16(p, false);
239
+ p += 2;
240
+ }
241
+ ys[i] = y;
242
+ }
243
+ let start = 0;
244
+ for (let c = 0; c < nContours; c++) {
245
+ const end = endPts[c];
246
+ const ring = flattenContour(xs, ys, flags, start, end, curveSteps);
247
+ if (ring.length >= 3) {
248
+ // TrueType outer contours are CW, holes are CCW; reverse to CCW outer / CW hole for Clipper2.
249
+ ring.reverse();
250
+ out.push(ring);
251
+ }
252
+ start = end + 1;
253
+ }
254
+ }
255
+ else if (nContours < 0) {
256
+ // Composite glyph (e.g., accented Latin & Cyrillic characters like ї, é, ü, ą)
257
+ let p = base + 10;
258
+ let flags = 0x0020;
259
+ while (flags & 0x0020) {
260
+ flags = this.dv.getUint16(p, false);
261
+ const subGid = this.dv.getUint16(p + 2, false);
262
+ p += 4;
263
+ let dx = 0, dy = 0;
264
+ if (flags & 0x0001) {
265
+ // ARG_1_AND_2_ARE_WORDS
266
+ if (flags & 0x0002) {
267
+ dx = this.dv.getInt16(p, false);
268
+ dy = this.dv.getInt16(p + 2, false);
269
+ }
270
+ p += 4;
271
+ }
272
+ else {
273
+ if (flags & 0x0002) {
274
+ dx = this.dv.getInt8(p);
275
+ dy = this.dv.getInt8(p + 1);
276
+ }
277
+ p += 2;
278
+ }
279
+ let a = 1, b = 0, c = 0, d = 1;
280
+ if (flags & 0x0008) {
281
+ a = d = this.dv.getInt16(p, false) / 16384;
282
+ p += 2;
283
+ }
284
+ else if (flags & 0x0040) {
285
+ a = this.dv.getInt16(p, false) / 16384;
286
+ d = this.dv.getInt16(p + 2, false) / 16384;
287
+ p += 4;
288
+ }
289
+ else if (flags & 0x0080) {
290
+ a = this.dv.getInt16(p, false) / 16384;
291
+ b = this.dv.getInt16(p + 2, false) / 16384;
292
+ c = this.dv.getInt16(p + 4, false) / 16384;
293
+ d = this.dv.getInt16(p + 6, false) / 16384;
294
+ p += 8;
295
+ }
296
+ const sub = this.glyphContours(subGid, curveSteps, depth + 1);
297
+ for (const ring of sub) {
298
+ out.push(ring.map(([px, py]) => [a * px + c * py + dx, b * px + d * py + dy]));
299
+ }
300
+ }
301
+ }
302
+ this.glyphCache.set(cacheKey, out);
303
+ return out;
304
+ }
305
+ }
306
+ function flattenContour(xs, ys, flags, start, end, steps) {
307
+ const count = end - start + 1;
308
+ if (count < 2)
309
+ return [];
310
+ const pts = [];
311
+ for (let i = start; i <= end; i++)
312
+ pts.push({ x: xs[i], y: ys[i], on: Boolean(flags[i] & 1) });
313
+ // Build explicit sequence starting at an on-curve point
314
+ const firstOn = pts.findIndex((p) => p.on);
315
+ let seq;
316
+ if (firstOn >= 0) {
317
+ seq = pts.slice(firstOn).concat(pts.slice(0, firstOn));
318
+ }
319
+ else {
320
+ const mid = { x: (pts[count - 1].x + pts[0].x) / 2, y: (pts[count - 1].y + pts[0].y) / 2, on: true };
321
+ seq = [mid, ...pts];
322
+ }
323
+ const out = [];
324
+ const push = (x, y) => {
325
+ const prev = out[out.length - 1];
326
+ if (!prev || Math.abs(prev[0] - x) > 1e-6 || Math.abs(prev[1] - y) > 1e-6)
327
+ out.push([x, y]);
328
+ };
329
+ let cur = seq[0];
330
+ push(cur.x, cur.y);
331
+ let i = 1;
332
+ while (i < seq.length) {
333
+ const next = seq[i];
334
+ if (next.on) {
335
+ push(next.x, next.y);
336
+ cur = next;
337
+ i++;
338
+ }
339
+ else {
340
+ const ctrl = next;
341
+ let target;
342
+ if (i + 1 < seq.length && seq[i + 1].on) {
343
+ target = seq[i + 1];
344
+ i += 2;
345
+ }
346
+ else {
347
+ const after = i + 1 < seq.length ? seq[i + 1] : seq[0];
348
+ target = { x: (ctrl.x + after.x) / 2, y: (ctrl.y + after.y) / 2, on: true };
349
+ i += 1;
350
+ }
351
+ for (let s = 1; s <= steps; s++) {
352
+ const t = s / steps;
353
+ const u = 1 - t;
354
+ push(u * u * cur.x + 2 * u * t * ctrl.x + t * t * target.x, u * u * cur.y + 2 * u * t * ctrl.y + t * t * target.y);
355
+ }
356
+ cur = target;
357
+ }
358
+ }
359
+ // Close quadratic if last point wasn't back at seq[0]
360
+ const first = out[0], last = out[out.length - 1];
361
+ if (out.length > 1 && Math.abs(first[0] - last[0]) <= 1e-6 && Math.abs(first[1] - last[1]) <= 1e-6)
362
+ out.pop();
363
+ return out;
364
+ }
365
+ /** Lay out single- or multi-line Unicode text into 2D contours in millimetres. */
366
+ export function layoutText(font, text, opts) {
367
+ const scale = opts.size / font.unitsPerEm;
368
+ const lineStep = font.unitsPerEm * opts.lineHeight;
369
+ const lines = text.split('\n');
370
+ const laidOut = [];
371
+ let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
372
+ for (let li = 0; li < lines.length; li++) {
373
+ const line = lines[li];
374
+ const lineY = -li * lineStep;
375
+ const lineContours = [];
376
+ let cursorX = 0;
377
+ let prevGid = -1;
378
+ for (const ch of line) {
379
+ const cp = ch.codePointAt(0);
380
+ const gid = font.glyphId(cp);
381
+ if (prevGid >= 0)
382
+ cursorX += font.kern(prevGid, gid);
383
+ const glyph = font.glyphContours(gid, opts.curveSteps);
384
+ for (const ring of glyph) {
385
+ lineContours.push(ring.map(([px, py]) => [px + cursorX, py + lineY]));
386
+ }
387
+ cursorX += font.advanceWidth(gid) * opts.spacing;
388
+ prevGid = gid;
389
+ }
390
+ laidOut.push({ contours: lineContours, width: cursorX, y: lineY });
391
+ }
392
+ // Horizontal alignment per line
393
+ const all = [];
394
+ for (const line of laidOut) {
395
+ const dx = opts.align === 'center' ? -line.width / 2 : opts.align === 'right' ? -line.width : 0;
396
+ for (const ring of line.contours) {
397
+ const shifted = ring.map(([px, py]) => {
398
+ const x = (px + dx) * scale;
399
+ const y = py * scale;
400
+ if (x < minX)
401
+ minX = x;
402
+ if (x > maxX)
403
+ maxX = x;
404
+ if (y < minY)
405
+ minY = y;
406
+ if (y > maxY)
407
+ maxY = y;
408
+ return [x, y];
409
+ });
410
+ all.push(shifted);
411
+ }
412
+ }
413
+ if (!all.length)
414
+ return [];
415
+ // Vertical alignment
416
+ let dy = 0;
417
+ if (opts.valign === 'center')
418
+ dy = -(minY + maxY) / 2;
419
+ else if (opts.valign === 'bottom')
420
+ dy = -minY;
421
+ else if (opts.valign === 'top')
422
+ dy = -maxY;
423
+ if (dy !== 0) {
424
+ for (const ring of all) {
425
+ for (let i = 0; i < ring.length; i++)
426
+ ring[i] = [ring[i][0], ring[i][1] + dy];
427
+ }
428
+ }
429
+ return all;
430
+ }
431
+ // ---- Bundled Roboto font loader (Node.js + Browser/Web Worker) --------------------------------
432
+ const defaultFonts = new Map();
433
+ const customFontCache = new WeakMap();
434
+ async function loadBundledFont(weight) {
435
+ const filename = weight === 'bold' ? 'Roboto-Bold.ttf' : 'Roboto-Regular.ttf';
436
+ try {
437
+ const proc = globalThis.process;
438
+ if (proc?.versions?.node) {
439
+ const fs = proc.getBuiltinModule('fs');
440
+ const url = proc.getBuiltinModule('url');
441
+ for (const rel of ['../fonts/' + filename, '../../../packages/core/fonts/' + filename]) {
442
+ const p = url.fileURLToPath(new URL(rel, import.meta.url));
443
+ if (fs.existsSync(p))
444
+ return new TtfFont(new Uint8Array(fs.readFileSync(p)));
445
+ }
446
+ return null;
447
+ }
448
+ const resp = await fetch(new URL('../fonts/' + filename, import.meta.url).href);
449
+ if (!resp.ok)
450
+ return null;
451
+ return new TtfFont(new Uint8Array(await resp.arrayBuffer()));
452
+ }
453
+ catch {
454
+ return null;
455
+ }
456
+ }
457
+ const [regFont, boldFont] = await Promise.all([loadBundledFont('regular'), loadBundledFont('bold')]);
458
+ if (regFont)
459
+ defaultFonts.set('regular', regFont);
460
+ if (boldFont)
461
+ defaultFonts.set('bold', boldFont);
462
+ export function getBundledFont(weight) {
463
+ return defaultFonts.get(weight) ?? defaultFonts.get('regular') ?? null;
464
+ }
465
+ export function parseTtfBytes(bytes) {
466
+ let cached = customFontCache.get(bytes);
467
+ if (!cached) {
468
+ cached = new TtfFont(bytes);
469
+ customFontCache.set(bytes, cached);
470
+ }
471
+ return cached;
472
+ }
473
+ //# sourceMappingURL=ttf.js.map