circuitscript 0.0.22 → 0.0.24

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 (164) hide show
  1. package/dist/cjs/antlr/CircuitScriptLexer.js +288 -0
  2. package/dist/cjs/antlr/CircuitScriptParser.js +4905 -0
  3. package/dist/cjs/antlr/CircuitScriptVisitor.js +6 -0
  4. package/{src/draw_symbols.ts → dist/cjs/draw_symbols.js} +303 -614
  5. package/dist/cjs/execute.js +780 -0
  6. package/{src/export.ts → dist/cjs/export.js} +34 -56
  7. package/dist/cjs/fonts.js +4 -0
  8. package/dist/cjs/geometry.js +430 -0
  9. package/dist/cjs/globals.js +60 -0
  10. package/dist/cjs/helpers.js +99 -0
  11. package/dist/cjs/index.js +29 -0
  12. package/{src/layout.ts → dist/cjs/layout.js} +413 -1002
  13. package/dist/cjs/lexer.js +114 -0
  14. package/dist/cjs/logger.js +17 -0
  15. package/dist/cjs/main.js +87 -0
  16. package/dist/cjs/objects/ClassComponent.js +142 -0
  17. package/dist/cjs/objects/ExecutionScope.js +134 -0
  18. package/dist/cjs/objects/Frame.js +22 -0
  19. package/{src/objects/Net.ts → dist/cjs/objects/Net.js} +9 -24
  20. package/dist/cjs/objects/ParamDefinition.js +42 -0
  21. package/dist/cjs/objects/PinDefinition.js +31 -0
  22. package/dist/cjs/objects/PinTypes.js +11 -0
  23. package/dist/cjs/objects/Wire.js +9 -0
  24. package/dist/cjs/objects/types.js +9 -0
  25. package/dist/cjs/parser.js +299 -0
  26. package/dist/cjs/regenerate-tests.js +23 -0
  27. package/dist/cjs/render.js +155 -0
  28. package/{src/server.ts → dist/cjs/server.js} +15 -21
  29. package/dist/cjs/sizing.js +105 -0
  30. package/{src/utils.ts → dist/cjs/utils.js} +25 -35
  31. package/{src/visitor.ts → dist/cjs/visitor.js} +392 -948
  32. package/{build/src/antlr/CircuitScriptLexer.js → dist/esm/antlr/CircuitScriptLexer.mjs} +90 -91
  33. package/{build/src/antlr/CircuitScriptParser.js → dist/esm/antlr/CircuitScriptParser.mjs} +138 -136
  34. package/{build/src/draw_symbols.js → dist/esm/draw_symbols.mjs} +11 -11
  35. package/{build/src/execute.js → dist/esm/execute.mjs} +9 -8
  36. package/{build/src/export.js → dist/esm/export.mjs} +2 -2
  37. package/{build/src/geometry.js → dist/esm/geometry.mjs} +3 -7
  38. package/{build/src/helpers.js → dist/esm/helpers.mjs} +27 -7
  39. package/dist/esm/index.mjs +13 -0
  40. package/{build/src/layout.js → dist/esm/layout.mjs} +11 -11
  41. package/{build/src/lexer.js → dist/esm/lexer.mjs} +2 -2
  42. package/{build/src/main.js → dist/esm/main.mjs} +5 -5
  43. package/{build/src/objects/ClassComponent.js → dist/esm/objects/ClassComponent.mjs} +3 -3
  44. package/{build/src/objects/PinDefinition.js → dist/esm/objects/PinDefinition.mjs} +1 -1
  45. package/dist/esm/parser.mjs +269 -0
  46. package/{build/src/regenerate-tests.js → dist/esm/regenerate-tests.mjs} +1 -1
  47. package/{build/src/render.js → dist/esm/render.mjs} +7 -24
  48. package/{build/src/sizing.js → dist/esm/sizing.mjs} +22 -8
  49. package/{build/src/visitor.js → dist/esm/visitor.mjs} +10 -29
  50. package/{build/src → dist/types}/antlr/CircuitScriptLexer.d.ts +23 -23
  51. package/{build/src → dist/types}/antlr/CircuitScriptParser.d.ts +24 -23
  52. package/{build/src → dist/types}/draw_symbols.d.ts +2 -2
  53. package/{build/src → dist/types}/execute.d.ts +2 -2
  54. package/{build/src → dist/types}/geometry.d.ts +1 -1
  55. package/dist/types/helpers.d.ts +9 -0
  56. package/dist/types/index.d.ts +13 -0
  57. package/{build/src → dist/types}/layout.d.ts +10 -10
  58. package/{build/src → dist/types}/objects/ClassComponent.d.ts +2 -2
  59. package/{build/src → dist/types}/objects/PinDefinition.d.ts +1 -1
  60. package/dist/types/parser.d.ts +38 -0
  61. package/{build/src → dist/types}/render.d.ts +1 -1
  62. package/{build/src → dist/types}/sizing.d.ts +3 -1
  63. package/{build/src → dist/types}/visitor.d.ts +5 -3
  64. package/package.json +30 -12
  65. package/.editorconfig +0 -15
  66. package/.eslintignore +0 -1
  67. package/.eslintrc.json +0 -27
  68. package/.gitlab-ci.yml +0 -81
  69. package/.prettierignore +0 -8
  70. package/.prettierrc +0 -16
  71. package/__tests__/expectedResults.ts +0 -657
  72. package/__tests__/helpers.ts +0 -82
  73. package/__tests__/parseScripts.ts +0 -593
  74. package/__tests__/renderData/script1.cst +0 -58
  75. package/__tests__/renderData/script1.cst.svg +0 -1
  76. package/__tests__/renderData/script2.cst +0 -16
  77. package/__tests__/renderData/script2.cst.svg +0 -1
  78. package/__tests__/renderData/script3.cst +0 -30
  79. package/__tests__/renderData/script3.cst.svg +0 -1
  80. package/__tests__/renderData/script4.cst +0 -54
  81. package/__tests__/renderData/script4.cst.svg +0 -1
  82. package/__tests__/renderData/script5.cst +0 -23
  83. package/__tests__/renderData/script5.cst.svg +0 -1
  84. package/__tests__/renderData/script6.cst +0 -28
  85. package/__tests__/renderData/script6.cst.svg +0 -1
  86. package/__tests__/renderData/script7.cst +0 -26
  87. package/__tests__/renderData/script7.cst.svg +0 -1
  88. package/__tests__/renderData/script8.cst +0 -37
  89. package/__tests__/renderData/script8.cst.svg +0 -1
  90. package/__tests__/testCLI.ts +0 -68
  91. package/__tests__/testMathOps.ts +0 -36
  92. package/__tests__/testMergeWires.ts +0 -141
  93. package/__tests__/testParse.ts +0 -263
  94. package/__tests__/testRender.ts +0 -38
  95. package/build/src/helpers.d.ts +0 -1
  96. package/build/src/parser.js +0 -69
  97. package/documentation.md +0 -238
  98. package/examples/example_arduino_uno.cst +0 -1146
  99. package/examples/example_garden_pump.cst +0 -567
  100. package/examples/lib.cst +0 -185
  101. package/fonts/Inter-Bold.ttf +0 -0
  102. package/fonts/Inter-Regular.ttf +0 -0
  103. package/fonts/OpenSans-Regular.ttf +0 -0
  104. package/fonts/Roboto-Regular.ttf +0 -0
  105. package/jest.config.js +0 -23
  106. package/libs/lib.cst +0 -185
  107. package/refresh.html +0 -42
  108. package/server.cjs +0 -50
  109. package/src/antlr/CircuitScript.g4 +0 -209
  110. package/src/antlr/CircuitScriptLexer.ts +0 -317
  111. package/src/antlr/CircuitScriptParser.ts +0 -4979
  112. package/src/antlr/CircuitScriptVisitor.ts +0 -420
  113. package/src/execute.ts +0 -1227
  114. package/src/fonts.ts +0 -1
  115. package/src/geometry.ts +0 -638
  116. package/src/globals.ts +0 -67
  117. package/src/helpers.ts +0 -114
  118. package/src/lexer.ts +0 -151
  119. package/src/logger.ts +0 -17
  120. package/src/main.ts +0 -105
  121. package/src/objects/ClassComponent.ts +0 -223
  122. package/src/objects/ExecutionScope.ts +0 -201
  123. package/src/objects/Frame.ts +0 -20
  124. package/src/objects/ParamDefinition.ts +0 -49
  125. package/src/objects/PinDefinition.ts +0 -49
  126. package/src/objects/PinTypes.ts +0 -7
  127. package/src/objects/Wire.ts +0 -19
  128. package/src/objects/types.ts +0 -66
  129. package/src/parser.ts +0 -106
  130. package/src/regenerate-tests.ts +0 -25
  131. package/src/render.ts +0 -260
  132. package/src/sizing.ts +0 -96
  133. package/tsconfig.json +0 -27
  134. package/tsconfig.release.json +0 -8
  135. /package/{build/src/antlr/CircuitScriptVisitor.js → dist/esm/antlr/CircuitScriptVisitor.mjs} +0 -0
  136. /package/{build/src/fonts.js → dist/esm/fonts.mjs} +0 -0
  137. /package/{build/src/globals.js → dist/esm/globals.mjs} +0 -0
  138. /package/{build/src/logger.js → dist/esm/logger.mjs} +0 -0
  139. /package/{build/src/objects/ExecutionScope.js → dist/esm/objects/ExecutionScope.mjs} +0 -0
  140. /package/{build/src/objects/Frame.js → dist/esm/objects/Frame.mjs} +0 -0
  141. /package/{build/src/objects/Net.js → dist/esm/objects/Net.mjs} +0 -0
  142. /package/{build/src/objects/ParamDefinition.js → dist/esm/objects/ParamDefinition.mjs} +0 -0
  143. /package/{build/src/objects/PinTypes.js → dist/esm/objects/PinTypes.mjs} +0 -0
  144. /package/{build/src/objects/Wire.js → dist/esm/objects/Wire.mjs} +0 -0
  145. /package/{build/src/objects/types.js → dist/esm/objects/types.mjs} +0 -0
  146. /package/{build/src/server.js → dist/esm/server.mjs} +0 -0
  147. /package/{build/src/utils.js → dist/esm/utils.mjs} +0 -0
  148. /package/{build/src → dist/types}/antlr/CircuitScriptVisitor.d.ts +0 -0
  149. /package/{build/src → dist/types}/export.d.ts +0 -0
  150. /package/{build/src → dist/types}/fonts.d.ts +0 -0
  151. /package/{build/src → dist/types}/globals.d.ts +0 -0
  152. /package/{build/src → dist/types}/lexer.d.ts +0 -0
  153. /package/{build/src → dist/types}/logger.d.ts +0 -0
  154. /package/{build/src → dist/types}/main.d.ts +0 -0
  155. /package/{build/src → dist/types}/objects/ExecutionScope.d.ts +0 -0
  156. /package/{build/src → dist/types}/objects/Frame.d.ts +0 -0
  157. /package/{build/src → dist/types}/objects/Net.d.ts +0 -0
  158. /package/{build/src → dist/types}/objects/ParamDefinition.d.ts +0 -0
  159. /package/{build/src → dist/types}/objects/PinTypes.d.ts +0 -0
  160. /package/{build/src → dist/types}/objects/Wire.d.ts +0 -0
  161. /package/{build/src → dist/types}/objects/types.d.ts +0 -0
  162. /package/{build/src → dist/types}/regenerate-tests.d.ts +0 -0
  163. /package/{build/src → dist/types}/server.d.ts +0 -0
  164. /package/{build/src → dist/types}/utils.d.ts +0 -0
@@ -0,0 +1,430 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.VerticalAlign = exports.HorizontalAlign = exports.Geometry = exports.GeometryProp = exports.Label = void 0;
7
+ const core_1 = __importDefault(require("@flatten-js/core"));
8
+ const sizing_js_1 = require("./sizing.js");
9
+ const globals_js_1 = require("./globals.js");
10
+ const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
11
+ class Label extends core_1.default.Polygon {
12
+ get box() {
13
+ return this.polygon.box;
14
+ }
15
+ constructor(id, text, anchorPoint, polygon, style, bounds) {
16
+ super(polygon.vertices);
17
+ this.anchorPoint = [0, 0];
18
+ this.boundingBox = { width: -1, height: -1 };
19
+ this.font = 'default';
20
+ this.id = id;
21
+ this.text = text;
22
+ this.anchorPoint = anchorPoint;
23
+ this.style = style;
24
+ this.boundingBox = polygon.box;
25
+ this.polygon = polygon;
26
+ this.textMeasurementBounds = bounds;
27
+ }
28
+ static fromPoint(id, x, y, text, style) {
29
+ let useText;
30
+ if (typeof text === 'number') {
31
+ useText = text.toString();
32
+ }
33
+ else if (typeof text === 'object'
34
+ && text instanceof ParamDefinition_js_1.NumericValue) {
35
+ useText = text.toDisplayString();
36
+ }
37
+ else if (typeof text === 'string') {
38
+ useText = text;
39
+ }
40
+ else {
41
+ throw 'Invalid string passed into label';
42
+ }
43
+ const { fontSize = 10, anchor = HorizontalAlign.Left, vanchor = VerticalAlign.Bottom, fontWeight = 'regular', } = style ?? {};
44
+ const { width, height, box } = (0, sizing_js_1.measureTextSize2)(useText, globals_js_1.defaultFont, fontSize, fontWeight, anchor, vanchor);
45
+ const polygonCoords = [
46
+ [box.x, box.y],
47
+ [box.x2, box.y],
48
+ [box.x2, box.y2],
49
+ [box.x, box.y2],
50
+ [box.x, box.y],
51
+ ];
52
+ const polygon = new core_1.default.Polygon(polygonCoords);
53
+ return new Label(id, useText, [x, y], polygon, style, box);
54
+ }
55
+ rotate(angle, origin) {
56
+ const polygonRotate = super.rotate(angle, origin);
57
+ return new Label(this.id, this.text, this.anchorPoint, polygonRotate, this.style, this.textMeasurementBounds);
58
+ }
59
+ getLabelPosition() {
60
+ return this.anchorPoint;
61
+ }
62
+ }
63
+ exports.Label = Label;
64
+ class GeometryProp {
65
+ constructor(name, value) {
66
+ this.name = name;
67
+ this.value = value;
68
+ }
69
+ }
70
+ exports.GeometryProp = GeometryProp;
71
+ class Geometry {
72
+ static point(x, y) {
73
+ return new core_1.default.Point(x, y);
74
+ }
75
+ static line(x1, y1, x2, y2) {
76
+ return new core_1.default.Line(Geometry.point(x1, y1), Geometry.point(x2, y2));
77
+ }
78
+ static label(id, x, y, text, style) {
79
+ return Label.fromPoint(id, x, y, text, style);
80
+ }
81
+ static segment(start, end) {
82
+ return new core_1.default.Segment(Geometry.point(start[0], start[1]), Geometry.point(end[0], end[1]));
83
+ }
84
+ static polygon(coords) {
85
+ return new core_1.default.Polygon(coords);
86
+ }
87
+ static multiline(coords) {
88
+ const segments = [];
89
+ for (let i = 0; i < coords.length - 1; i++) {
90
+ segments.push(new core_1.default.Segment(Geometry.point(coords[i][0], coords[i][1]), Geometry.point(coords[i + 1][0], coords[i + 1][1])));
91
+ }
92
+ return new core_1.default.Multiline(segments);
93
+ }
94
+ static arc(center, radius, startAngle, endAngle, sweepDirection) {
95
+ return new core_1.default.Arc(Geometry.point(center[0], center[1]), radius, startAngle, endAngle, sweepDirection);
96
+ }
97
+ static getCoords(item) {
98
+ const points = item.vertices.map(vertex => {
99
+ return [vertex.x, vertex.y];
100
+ });
101
+ return points;
102
+ }
103
+ static rotateDegs(feature, angleDegrees, center) {
104
+ const angleRads = angleDegrees * Math.PI / 180;
105
+ return feature.rotate(angleRads, Geometry.point(center[0], center[1]));
106
+ }
107
+ static groupRotate(features, angle, center) {
108
+ const angleRads = angle * Math.PI / 180;
109
+ const rotateAboutPoint = Geometry.point(center[0], center[1]);
110
+ return features.map(feature => {
111
+ return feature.rotate(angleRads, rotateAboutPoint);
112
+ });
113
+ }
114
+ static groupBounds(features) {
115
+ let minX = Number.POSITIVE_INFINITY;
116
+ let minY = Number.POSITIVE_INFINITY;
117
+ let maxX = Number.NEGATIVE_INFINITY;
118
+ let maxY = Number.NEGATIVE_INFINITY;
119
+ features.forEach(feature => {
120
+ const box = feature.box;
121
+ if (feature instanceof Label
122
+ && typeof feature.text === 'string'
123
+ && feature.text.trim().length === 0) {
124
+ return;
125
+ }
126
+ if (box.xmin === undefined) {
127
+ throw "Invalid box!";
128
+ }
129
+ minX = Math.min(minX, box.xmin);
130
+ minY = Math.min(minY, box.ymin);
131
+ maxX = Math.max(maxX, box.xmax);
132
+ maxY = Math.max(maxY, box.ymax);
133
+ });
134
+ return {
135
+ start: [minX, minY],
136
+ end: [maxX, maxY],
137
+ width: maxX - minX,
138
+ height: maxY - minY,
139
+ };
140
+ }
141
+ static getType(feature) {
142
+ if (feature instanceof Label) {
143
+ return 'label';
144
+ }
145
+ else if (feature instanceof core_1.default.Polygon) {
146
+ return 'polygon';
147
+ }
148
+ else if (feature instanceof core_1.default.Segment) {
149
+ return 'segment';
150
+ }
151
+ console.log('unknown type', feature);
152
+ }
153
+ static featuresToPath(items) {
154
+ const paths = [];
155
+ let isClosedPolygon = false;
156
+ items.forEach(item => {
157
+ if (item instanceof Label) {
158
+ return;
159
+ }
160
+ const path = [];
161
+ if (item instanceof core_1.default.Arc) {
162
+ const x = item.center.x;
163
+ const y = item.center.y;
164
+ const radius = item.r;
165
+ let useEndAngle = item.endAngle;
166
+ let extraEnd = '';
167
+ if (item.startAngle === 0 && item.endAngle === Geometry.FullCircleRadians) {
168
+ useEndAngle = 359.9999 * Math.PI / 180;
169
+ isClosedPolygon = true;
170
+ extraEnd = ' Z';
171
+ }
172
+ const startPoint = getArcPointRadians(x, y, radius, item.startAngle);
173
+ const endPoint = getArcPointRadians(x, y, radius, useEndAngle);
174
+ paths.push('M ' + startPoint[0] + ' ' + startPoint[1]
175
+ + 'A ' + radius + ' ' + radius + ' 0 1 1 '
176
+ + endPoint[0] + ' ' + endPoint[1] + extraEnd);
177
+ }
178
+ else {
179
+ const coords = Geometry.getCoords(item);
180
+ if (item instanceof core_1.default.Polygon) {
181
+ isClosedPolygon = true;
182
+ }
183
+ for (let i = 0; i < coords.length; i++) {
184
+ const [x, y] = coords[i];
185
+ const command = (i === 0) ? 'M' : 'L';
186
+ path.push(`${command} ${x} ${y}`);
187
+ }
188
+ if (isClosedPolygon) {
189
+ path.push('Z');
190
+ }
191
+ paths.push(path.join(' '));
192
+ }
193
+ });
194
+ return {
195
+ path: paths.join(" "),
196
+ isClosedPolygon,
197
+ };
198
+ }
199
+ static angle(dx, dy) {
200
+ const line = new core_1.default.Segment(new core_1.default.Point(0, 0), new core_1.default.Point(dx, dy));
201
+ return line.slope * 180 / Math.PI;
202
+ }
203
+ static getQuadrant(dx, dy) {
204
+ const angle = Geometry.angle(dx, dy);
205
+ return Number(Math.floor(angle / 90));
206
+ }
207
+ static mergeWires(wirePoints) {
208
+ const existingSegments = [];
209
+ wirePoints.forEach(points => {
210
+ const tmpPoints = points.map(pt => {
211
+ return new core_1.default.Point(pt.x, pt.y);
212
+ });
213
+ for (let i = 0; i < tmpPoints.length - 1; i++) {
214
+ const pt1 = tmpPoints[i];
215
+ const pt2 = tmpPoints[i + 1];
216
+ const newSegments = [
217
+ new core_1.default.Segment(pt1, pt2)
218
+ ];
219
+ for (let j = 0; j < existingSegments.length; j++) {
220
+ const currentSegment = existingSegments[j];
221
+ for (let k = 0; k < newSegments.length; k++) {
222
+ const newSegment = newSegments[k];
223
+ const segmentsAreSame = newSegment.equalTo(currentSegment) || newSegment.reverse().equalTo(currentSegment);
224
+ if (segmentsAreSame) {
225
+ newSegments.splice(k, 1);
226
+ j = Math.max(0, j - 1);
227
+ break;
228
+ }
229
+ const intersectPoints = currentSegment.intersect(newSegment);
230
+ if (intersectPoints.length > 0) {
231
+ const endToEndIntersect = intersectPoints.length === 1 &&
232
+ (currentSegment.end.equalTo(newSegment.start) || currentSegment.end.equalTo(newSegment.end) ||
233
+ currentSegment.start.equalTo(newSegment.start) || currentSegment.start.equalTo(newSegment.end));
234
+ if (endToEndIntersect) {
235
+ continue;
236
+ }
237
+ const splitSegments = [];
238
+ intersectPoints.forEach(intersectPoint => {
239
+ const splitResult1 = currentSegment.split(intersectPoint);
240
+ const splitResult2 = newSegment.split(intersectPoint);
241
+ [...splitResult1, ...splitResult2].forEach(segment => {
242
+ if (segment !== null) {
243
+ const matchingSegmentIndex = splitSegments.findIndex(item => {
244
+ return item.equalTo(segment);
245
+ });
246
+ if (matchingSegmentIndex === -1 &&
247
+ !segment.equalTo(currentSegment) && !segment.equalTo(newSegment)) {
248
+ splitSegments.push(segment);
249
+ }
250
+ }
251
+ });
252
+ });
253
+ const splitCurrentSegments = [];
254
+ const splitNewSegments = [];
255
+ splitSegments.forEach(segment => {
256
+ if (currentSegment.contains(segment.start) && currentSegment.contains(segment.end)) {
257
+ splitCurrentSegments.push(segment);
258
+ }
259
+ else {
260
+ splitNewSegments.push(segment);
261
+ }
262
+ });
263
+ replaceSegments(existingSegments, j, splitCurrentSegments);
264
+ replaceSegments(newSegments, k, splitNewSegments);
265
+ j = Math.max(0, j - 1);
266
+ }
267
+ }
268
+ }
269
+ newSegments.forEach(segment => {
270
+ existingSegments.push(segment);
271
+ });
272
+ }
273
+ });
274
+ const trackWirePoints = [];
275
+ existingSegments.forEach(segment => {
276
+ trackWirePoints.push([segment.start.x, segment.start.y]);
277
+ trackWirePoints.push([segment.end.x, segment.end.y]);
278
+ });
279
+ const accumPoints = trackWirePoints.reduce((accum, point) => {
280
+ const found = accum.find(item => {
281
+ return item[0] === point[0] && item[1] === point[1];
282
+ });
283
+ if (found) {
284
+ found[2]++;
285
+ }
286
+ else {
287
+ accum.push([point[0], point[1], 1]);
288
+ }
289
+ return accum;
290
+ }, []);
291
+ const intersectPoints = accumPoints.reduce((accum, entry) => {
292
+ if (entry[2] > 2) {
293
+ accum.push(entry);
294
+ }
295
+ return accum;
296
+ }, []);
297
+ const segments = existingSegments.map(segment => {
298
+ return [
299
+ [segment.start.x, segment.start.y],
300
+ [segment.end.x, segment.end.y]
301
+ ];
302
+ });
303
+ return {
304
+ intersectPoints,
305
+ segments,
306
+ };
307
+ }
308
+ }
309
+ exports.Geometry = Geometry;
310
+ Geometry.FullCircleRadians = 2 * Math.PI;
311
+ function replaceSegments(segments, index, replacedSegments) {
312
+ if (replacedSegments.length > 0) {
313
+ segments.splice(index, 1);
314
+ }
315
+ let counter = 0;
316
+ replacedSegments.forEach(item => {
317
+ if (item !== null) {
318
+ segments.splice(index + counter, 0, item);
319
+ counter++;
320
+ }
321
+ });
322
+ return counter;
323
+ }
324
+ function labelPolygonForAnchors(x, y, width, height, hAnchor, vAnchor) {
325
+ let coordVectors = [];
326
+ if (hAnchor === HorizontalAlign.Left) {
327
+ if (vAnchor === VerticalAlign.Bottom) {
328
+ coordVectors = [
329
+ [0, 0],
330
+ [0, -1],
331
+ [1, -1],
332
+ [1, 1],
333
+ ];
334
+ }
335
+ else if (vAnchor === VerticalAlign.Middle) {
336
+ coordVectors = [
337
+ [0, -0.5],
338
+ [0, 0.5],
339
+ [1, 0.5],
340
+ [1, -0.5],
341
+ ];
342
+ }
343
+ else if (vAnchor === VerticalAlign.Top) {
344
+ coordVectors = [
345
+ [0, 0],
346
+ [1, 0],
347
+ [1, 1],
348
+ [0, 1],
349
+ ];
350
+ }
351
+ }
352
+ else if (hAnchor === HorizontalAlign.Right) {
353
+ if (vAnchor === VerticalAlign.Bottom) {
354
+ coordVectors = [
355
+ [0, 0],
356
+ [-1, 0],
357
+ [-1, -1],
358
+ [0, -1],
359
+ ];
360
+ }
361
+ else if (vAnchor === VerticalAlign.Middle) {
362
+ coordVectors = [
363
+ [0, -0.5],
364
+ [0, 0.5],
365
+ [-1, 0.5],
366
+ [-1, -0.5],
367
+ ];
368
+ }
369
+ else if (vAnchor === VerticalAlign.Top) {
370
+ coordVectors = [
371
+ [0, 0],
372
+ [0, 1],
373
+ [-1, 1],
374
+ [-1, 0]
375
+ ];
376
+ }
377
+ }
378
+ else if (hAnchor === HorizontalAlign.Middle) {
379
+ if (vAnchor === VerticalAlign.Bottom) {
380
+ coordVectors = [
381
+ [-0.5, 0],
382
+ [-0.5, -1],
383
+ [0.5, -1],
384
+ [0.5, 0]
385
+ ];
386
+ }
387
+ else if (vAnchor === VerticalAlign.Middle) {
388
+ coordVectors = [
389
+ [-0.5, 0.5],
390
+ [-0.5, -0.5],
391
+ [0.5, -0.5],
392
+ [0.5, 0.5],
393
+ ];
394
+ }
395
+ else if (vAnchor === VerticalAlign.Top) {
396
+ coordVectors = [
397
+ [0.5, 0],
398
+ [0.5, 1],
399
+ [-0.5, 1],
400
+ [-0.5, 0]
401
+ ];
402
+ }
403
+ }
404
+ return coordVectors.map(([vx, vy]) => {
405
+ return [
406
+ x + vx * width,
407
+ y + vy * height
408
+ ];
409
+ });
410
+ }
411
+ var HorizontalAlign;
412
+ (function (HorizontalAlign) {
413
+ HorizontalAlign["Left"] = "left";
414
+ HorizontalAlign["Middle"] = "middle";
415
+ HorizontalAlign["Right"] = "right";
416
+ })(HorizontalAlign || (exports.HorizontalAlign = HorizontalAlign = {}));
417
+ var VerticalAlign;
418
+ (function (VerticalAlign) {
419
+ VerticalAlign["Top"] = "top";
420
+ VerticalAlign["Middle"] = "middle";
421
+ VerticalAlign["Bottom"] = "bottom";
422
+ })(VerticalAlign || (exports.VerticalAlign = VerticalAlign = {}));
423
+ function getArcPointRadians(centerX, centerY, radius, angleRads) {
424
+ const dx = Math.cos(angleRads);
425
+ const dy = Math.sin(angleRads);
426
+ return [
427
+ centerX + dx * radius,
428
+ centerY + dy * radius
429
+ ];
430
+ }
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BlockTypes = exports.ReferenceTypes = exports.ComponentTypes = exports.wireColor = exports.junctionColor = exports.junctionSize = exports.bodyColor = exports.defaultFontSize = exports.defaultFontBold = exports.defaultFont = exports.portHeight = exports.portWidth = exports.SymbolPinSide = exports.LayoutDirection = exports.ParamKeys = exports.NoNetText = exports.GlobalNames = void 0;
4
+ var GlobalNames;
5
+ (function (GlobalNames) {
6
+ GlobalNames["__root"] = "__root";
7
+ GlobalNames["gnd"] = "gnd";
8
+ GlobalNames["DefaultResistor"] = "res";
9
+ GlobalNames["DefaultCapacitor"] = "cap";
10
+ GlobalNames["DefaultInductor"] = "ind";
11
+ GlobalNames["symbol"] = "symbol";
12
+ })(GlobalNames || (exports.GlobalNames = GlobalNames = {}));
13
+ exports.NoNetText = 'NO_NET';
14
+ var ParamKeys;
15
+ (function (ParamKeys) {
16
+ ParamKeys["__is_net"] = "__is_net";
17
+ ParamKeys["__is_label"] = "__is_label";
18
+ ParamKeys["priority"] = "priority";
19
+ ParamKeys["net_name"] = "net_name";
20
+ })(ParamKeys || (exports.ParamKeys = ParamKeys = {}));
21
+ var LayoutDirection;
22
+ (function (LayoutDirection) {
23
+ LayoutDirection["RIGHT"] = "RIGHT";
24
+ LayoutDirection["LEFT"] = "LEFT";
25
+ })(LayoutDirection || (exports.LayoutDirection = LayoutDirection = {}));
26
+ var SymbolPinSide;
27
+ (function (SymbolPinSide) {
28
+ SymbolPinSide["Left"] = "left";
29
+ SymbolPinSide["Right"] = "right";
30
+ })(SymbolPinSide || (exports.SymbolPinSide = SymbolPinSide = {}));
31
+ exports.portWidth = 20;
32
+ exports.portHeight = 2;
33
+ exports.defaultFont = 'Open Sans-Regular, Arial';
34
+ exports.defaultFontBold = 'Open Sans-Bold, Arial-Bold, Arial';
35
+ exports.defaultFontSize = 10;
36
+ exports.bodyColor = '#FFFEAF';
37
+ exports.junctionSize = 5;
38
+ exports.junctionColor = 'rgb(0, 132, 0)';
39
+ exports.wireColor = 'rgb(0, 132, 0)';
40
+ var ComponentTypes;
41
+ (function (ComponentTypes) {
42
+ ComponentTypes["gnd"] = "gnd";
43
+ ComponentTypes["net"] = "net";
44
+ ComponentTypes["label"] = "label";
45
+ ComponentTypes["point"] = "point";
46
+ })(ComponentTypes || (exports.ComponentTypes = ComponentTypes = {}));
47
+ var ReferenceTypes;
48
+ (function (ReferenceTypes) {
49
+ ReferenceTypes["function"] = "function";
50
+ ReferenceTypes["value"] = "value";
51
+ ReferenceTypes["variable"] = "variable";
52
+ ReferenceTypes["instance"] = "instance";
53
+ })(ReferenceTypes || (exports.ReferenceTypes = ReferenceTypes = {}));
54
+ var BlockTypes;
55
+ (function (BlockTypes) {
56
+ BlockTypes[BlockTypes["Branch"] = 1] = "Branch";
57
+ BlockTypes[BlockTypes["Join"] = 2] = "Join";
58
+ BlockTypes[BlockTypes["Parallel"] = 3] = "Parallel";
59
+ BlockTypes[BlockTypes["Point"] = 4] = "Point";
60
+ })(BlockTypes || (exports.BlockTypes = BlockTypes = {}));
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCurrentPath = exports.detectJSModuleType = exports.renderScript = exports.JSModuleType = void 0;
4
+ const fs_1 = require("fs");
5
+ const export_js_1 = require("./export.js");
6
+ const layout_js_1 = require("./layout.js");
7
+ const ExecutionScope_js_1 = require("./objects/ExecutionScope.js");
8
+ const parser_js_1 = require("./parser.js");
9
+ const render_js_1 = require("./render.js");
10
+ const utils_js_1 = require("./utils.js");
11
+ const visitor_js_1 = require("./visitor.js");
12
+ const this_file_1 = require("this-file");
13
+ var JSModuleType;
14
+ (function (JSModuleType) {
15
+ JSModuleType["CommonJs"] = "cjs";
16
+ JSModuleType["ESM"] = "mjs";
17
+ })(JSModuleType || (exports.JSModuleType = JSModuleType = {}));
18
+ function renderScript(scriptData, outputPath, options) {
19
+ const { currentDirectory = null, defaultLibsPath, dumpNets = false, dumpData = false, kicadNetlistPath = null, showStats = false } = options;
20
+ const visitor = new visitor_js_1.MainVisitor(true);
21
+ visitor.onImportFile = visitor.createImportFileHandler(currentDirectory, defaultLibsPath);
22
+ visitor.print('reading file');
23
+ visitor.print('done reading file');
24
+ const { tree, parser, hasParseError, hasError, parserTimeTaken, lexerTimeTaken } = (0, parser_js_1.parseFileWithVisitor)(visitor, scriptData);
25
+ showStats && console.log('Lexing took:', lexerTimeTaken);
26
+ showStats && console.log('Parsing took:', parserTimeTaken);
27
+ dumpNets && console.log(visitor.dumpNets());
28
+ dumpData && (0, fs_1.writeFileSync)('dump/tree.lisp', tree.toStringTree(null, parser));
29
+ dumpData && (0, fs_1.writeFileSync)('dump/raw-parser.txt', visitor.logger.dump());
30
+ if (hasError || hasParseError) {
31
+ console.log('Error while parsing');
32
+ return null;
33
+ }
34
+ try {
35
+ visitor.annotateComponents();
36
+ }
37
+ catch (err) {
38
+ console.log('Error during annotation: ', err);
39
+ }
40
+ if (kicadNetlistPath) {
41
+ const kicadNetList = (0, export_js_1.generateKiCADNetList)(visitor.getNetList());
42
+ (0, fs_1.writeFileSync)(kicadNetlistPath, kicadNetList);
43
+ console.log('Generated KiCad netlist file');
44
+ }
45
+ const { sequence, nets } = visitor.getGraph();
46
+ const tmpSequence = sequence.map(item => {
47
+ const tmp = [...item];
48
+ const action = tmp[0];
49
+ if (action === ExecutionScope_js_1.SequenceAction.Wire) {
50
+ tmp[2] = tmp[2].map(item2 => {
51
+ return [item2.direction, item2.value].join(",");
52
+ }).join(" ");
53
+ }
54
+ else if (action === ExecutionScope_js_1.SequenceAction.Frame) {
55
+ tmp[1] = item[1].frameId;
56
+ }
57
+ else if (action !== ExecutionScope_js_1.SequenceAction.WireJump) {
58
+ tmp[1] = item[1].instanceName;
59
+ }
60
+ return tmp.join(" | ");
61
+ });
62
+ dumpData && (0, fs_1.writeFileSync)('dump/raw-sequence.txt', tmpSequence.join('\n'));
63
+ let svgOutput = null;
64
+ try {
65
+ const layoutEngine = new layout_js_1.LayoutEngine();
66
+ const layoutTimer = new utils_js_1.SimpleStopwatch();
67
+ const graph = layoutEngine.runLayout(sequence, nets);
68
+ layoutEngine.printWarnings();
69
+ showStats && console.log('Layout took:', layoutTimer.lap());
70
+ dumpData && (0, fs_1.writeFileSync)('dump/raw-layout.txt', layoutEngine.logger.dump());
71
+ const generateSvgTimer = new utils_js_1.SimpleStopwatch();
72
+ svgOutput = (0, render_js_1.generateSVG2)(graph);
73
+ showStats && console.log('Render took:', generateSvgTimer.lap());
74
+ if (outputPath) {
75
+ (0, fs_1.writeFileSync)(outputPath, svgOutput);
76
+ }
77
+ }
78
+ catch (err) {
79
+ console.log('Error during render: ', err);
80
+ }
81
+ return svgOutput;
82
+ }
83
+ exports.renderScript = renderScript;
84
+ function detectJSModuleType() {
85
+ if (typeof __filename === 'undefined' &&
86
+ typeof __dirname === 'undefined') {
87
+ return JSModuleType.ESM;
88
+ }
89
+ else {
90
+ return JSModuleType.CommonJs;
91
+ }
92
+ }
93
+ exports.detectJSModuleType = detectJSModuleType;
94
+ const context = (0, this_file_1.createContext)();
95
+ function getCurrentPath() {
96
+ const filename = context.filename;
97
+ return { filePath: filename };
98
+ }
99
+ exports.getCurrentPath = getCurrentPath;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./draw_symbols.js"), exports);
18
+ __exportStar(require("./execute.js"), exports);
19
+ __exportStar(require("./export.js"), exports);
20
+ __exportStar(require("./geometry.js"), exports);
21
+ __exportStar(require("./globals.js"), exports);
22
+ __exportStar(require("./helpers.js"), exports);
23
+ __exportStar(require("./layout.js"), exports);
24
+ __exportStar(require("./lexer.js"), exports);
25
+ __exportStar(require("./logger.js"), exports);
26
+ __exportStar(require("./parser.js"), exports);
27
+ __exportStar(require("./render.js"), exports);
28
+ __exportStar(require("./utils.js"), exports);
29
+ __exportStar(require("./visitor.js"), exports);