circuitscript 0.0.22 → 0.0.25

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 (180) hide show
  1. package/dist/cjs/BaseVisitor.js +487 -0
  2. package/dist/cjs/SemanticTokenVisitor.js +218 -0
  3. package/dist/cjs/SymbolValidatorVisitor.js +233 -0
  4. package/dist/cjs/antlr/CircuitScriptLexer.js +302 -0
  5. package/dist/cjs/antlr/CircuitScriptParser.js +5128 -0
  6. package/dist/cjs/antlr/CircuitScriptVisitor.js +7 -0
  7. package/dist/cjs/draw_symbols.js +819 -0
  8. package/dist/cjs/execute.js +778 -0
  9. package/{src/export.ts → dist/cjs/export.js} +34 -56
  10. package/dist/cjs/fonts.js +4 -0
  11. package/dist/cjs/geometry.js +450 -0
  12. package/dist/cjs/globals.js +60 -0
  13. package/dist/cjs/helpers.js +269 -0
  14. package/dist/cjs/index.js +31 -0
  15. package/{src/layout.ts → dist/cjs/layout.js} +421 -1002
  16. package/dist/cjs/lexer.js +111 -0
  17. package/dist/cjs/logger.js +17 -0
  18. package/dist/cjs/main.js +82 -0
  19. package/dist/cjs/objects/ClassComponent.js +145 -0
  20. package/dist/cjs/objects/ExecutionScope.js +135 -0
  21. package/dist/cjs/objects/Frame.js +22 -0
  22. package/{src/objects/Net.ts → dist/cjs/objects/Net.js} +9 -24
  23. package/dist/cjs/objects/ParamDefinition.js +42 -0
  24. package/dist/cjs/objects/PinDefinition.js +31 -0
  25. package/dist/cjs/objects/PinTypes.js +11 -0
  26. package/dist/cjs/objects/Wire.js +9 -0
  27. package/dist/cjs/objects/types.js +15 -0
  28. package/dist/cjs/parser.js +70 -0
  29. package/dist/cjs/regenerate-tests.js +23 -0
  30. package/dist/cjs/render.js +155 -0
  31. package/{src/server.ts → dist/cjs/server.js} +15 -21
  32. package/dist/cjs/sizing.js +105 -0
  33. package/{src/utils.ts → dist/cjs/utils.js} +25 -35
  34. package/dist/cjs/validate.js +81 -0
  35. package/dist/cjs/visitor.js +844 -0
  36. package/dist/esm/BaseVisitor.mjs +488 -0
  37. package/dist/esm/SemanticTokenVisitor.mjs +215 -0
  38. package/dist/esm/SymbolValidatorVisitor.mjs +222 -0
  39. package/dist/esm/antlr/CircuitScriptLexer.mjs +276 -0
  40. package/dist/esm/antlr/CircuitScriptParser.mjs +5038 -0
  41. package/{build/src/antlr/CircuitScriptVisitor.js → dist/esm/antlr/CircuitScriptVisitor.mjs} +8 -3
  42. package/{build/src/draw_symbols.js → dist/esm/draw_symbols.mjs} +78 -33
  43. package/{build/src/execute.js → dist/esm/execute.mjs} +59 -60
  44. package/{build/src/export.js → dist/esm/export.mjs} +2 -2
  45. package/{build/src/geometry.js → dist/esm/geometry.mjs} +31 -15
  46. package/dist/esm/helpers.mjs +252 -0
  47. package/dist/esm/index.mjs +15 -0
  48. package/{build/src/layout.js → dist/esm/layout.mjs} +19 -11
  49. package/{build/src/lexer.js → dist/esm/lexer.mjs} +10 -10
  50. package/{build/src/main.js → dist/esm/main.mjs} +9 -14
  51. package/{build/src/objects/ClassComponent.js → dist/esm/objects/ClassComponent.mjs} +6 -3
  52. package/{build/src/objects/ExecutionScope.js → dist/esm/objects/ExecutionScope.mjs} +1 -0
  53. package/{build/src/objects/PinDefinition.js → dist/esm/objects/PinDefinition.mjs} +1 -1
  54. package/dist/esm/objects/types.mjs +12 -0
  55. package/dist/esm/parser.mjs +64 -0
  56. package/{build/src/regenerate-tests.js → dist/esm/regenerate-tests.mjs} +1 -1
  57. package/{build/src/render.js → dist/esm/render.mjs} +7 -24
  58. package/{build/src/sizing.js → dist/esm/sizing.mjs} +22 -8
  59. package/{src/main.ts → dist/esm/validate.mjs} +31 -62
  60. package/dist/esm/visitor.mjs +838 -0
  61. package/dist/types/BaseVisitor.d.ts +69 -0
  62. package/dist/types/SemanticTokenVisitor.d.ts +36 -0
  63. package/dist/types/SymbolValidatorVisitor.d.ts +61 -0
  64. package/{build/src → dist/types}/antlr/CircuitScriptLexer.d.ts +28 -27
  65. package/dist/types/antlr/CircuitScriptParser.d.ts +719 -0
  66. package/{build/src → dist/types}/antlr/CircuitScriptVisitor.d.ts +69 -59
  67. package/{build/src → dist/types}/draw_symbols.d.ts +11 -2
  68. package/{build/src → dist/types}/execute.d.ts +6 -9
  69. package/{build/src → dist/types}/geometry.d.ts +5 -1
  70. package/dist/types/helpers.d.ts +40 -0
  71. package/dist/types/index.d.ts +15 -0
  72. package/{build/src → dist/types}/layout.d.ts +10 -10
  73. package/{build/src → dist/types}/lexer.d.ts +2 -2
  74. package/{build/src → dist/types}/objects/ClassComponent.d.ts +2 -2
  75. package/{build/src → dist/types}/objects/ExecutionScope.d.ts +4 -1
  76. package/{build/src → dist/types}/objects/PinDefinition.d.ts +1 -1
  77. package/{build/src → dist/types}/objects/types.d.ts +5 -0
  78. package/dist/types/parser.d.ts +25 -0
  79. package/{build/src → dist/types}/render.d.ts +1 -1
  80. package/{build/src → dist/types}/sizing.d.ts +3 -1
  81. package/dist/types/validate.d.ts +2 -0
  82. package/dist/types/visitor.d.ts +80 -0
  83. package/libs/lib.cst +0 -2
  84. package/package.json +38 -15
  85. package/.editorconfig +0 -15
  86. package/.eslintignore +0 -1
  87. package/.eslintrc.json +0 -27
  88. package/.gitlab-ci.yml +0 -81
  89. package/.prettierignore +0 -8
  90. package/.prettierrc +0 -16
  91. package/__tests__/expectedResults.ts +0 -657
  92. package/__tests__/helpers.ts +0 -82
  93. package/__tests__/parseScripts.ts +0 -593
  94. package/__tests__/renderData/script1.cst +0 -58
  95. package/__tests__/renderData/script1.cst.svg +0 -1
  96. package/__tests__/renderData/script2.cst +0 -16
  97. package/__tests__/renderData/script2.cst.svg +0 -1
  98. package/__tests__/renderData/script3.cst +0 -30
  99. package/__tests__/renderData/script3.cst.svg +0 -1
  100. package/__tests__/renderData/script4.cst +0 -54
  101. package/__tests__/renderData/script4.cst.svg +0 -1
  102. package/__tests__/renderData/script5.cst +0 -23
  103. package/__tests__/renderData/script5.cst.svg +0 -1
  104. package/__tests__/renderData/script6.cst +0 -28
  105. package/__tests__/renderData/script6.cst.svg +0 -1
  106. package/__tests__/renderData/script7.cst +0 -26
  107. package/__tests__/renderData/script7.cst.svg +0 -1
  108. package/__tests__/renderData/script8.cst +0 -37
  109. package/__tests__/renderData/script8.cst.svg +0 -1
  110. package/__tests__/testCLI.ts +0 -68
  111. package/__tests__/testMathOps.ts +0 -36
  112. package/__tests__/testMergeWires.ts +0 -141
  113. package/__tests__/testParse.ts +0 -263
  114. package/__tests__/testRender.ts +0 -38
  115. package/build/src/antlr/CircuitScriptLexer.js +0 -287
  116. package/build/src/antlr/CircuitScriptParser.d.ts +0 -674
  117. package/build/src/antlr/CircuitScriptParser.js +0 -4841
  118. package/build/src/helpers.d.ts +0 -1
  119. package/build/src/helpers.js +0 -73
  120. package/build/src/objects/types.js +0 -6
  121. package/build/src/parser.js +0 -69
  122. package/build/src/visitor.d.ts +0 -133
  123. package/build/src/visitor.js +0 -1154
  124. package/documentation.md +0 -238
  125. package/examples/example_arduino_uno.cst +0 -1146
  126. package/examples/example_garden_pump.cst +0 -567
  127. package/examples/lib.cst +0 -185
  128. package/jest.config.js +0 -23
  129. package/refresh.html +0 -42
  130. package/server.cjs +0 -50
  131. package/src/antlr/CircuitScript.g4 +0 -209
  132. package/src/antlr/CircuitScriptLexer.ts +0 -317
  133. package/src/antlr/CircuitScriptParser.ts +0 -4979
  134. package/src/antlr/CircuitScriptVisitor.ts +0 -420
  135. package/src/draw_symbols.ts +0 -1085
  136. package/src/execute.ts +0 -1227
  137. package/src/fonts.ts +0 -1
  138. package/src/geometry.ts +0 -638
  139. package/src/globals.ts +0 -67
  140. package/src/helpers.ts +0 -114
  141. package/src/lexer.ts +0 -151
  142. package/src/logger.ts +0 -17
  143. package/src/objects/ClassComponent.ts +0 -223
  144. package/src/objects/ExecutionScope.ts +0 -201
  145. package/src/objects/Frame.ts +0 -20
  146. package/src/objects/ParamDefinition.ts +0 -49
  147. package/src/objects/PinDefinition.ts +0 -49
  148. package/src/objects/PinTypes.ts +0 -7
  149. package/src/objects/Wire.ts +0 -19
  150. package/src/objects/types.ts +0 -66
  151. package/src/parser.ts +0 -106
  152. package/src/regenerate-tests.ts +0 -25
  153. package/src/render.ts +0 -260
  154. package/src/sizing.ts +0 -96
  155. package/src/visitor.ts +0 -1691
  156. package/tsconfig.json +0 -27
  157. package/tsconfig.release.json +0 -8
  158. /package/{build/src/fonts.js → dist/esm/fonts.mjs} +0 -0
  159. /package/{build/src/globals.js → dist/esm/globals.mjs} +0 -0
  160. /package/{build/src/logger.js → dist/esm/logger.mjs} +0 -0
  161. /package/{build/src/objects/Frame.js → dist/esm/objects/Frame.mjs} +0 -0
  162. /package/{build/src/objects/Net.js → dist/esm/objects/Net.mjs} +0 -0
  163. /package/{build/src/objects/ParamDefinition.js → dist/esm/objects/ParamDefinition.mjs} +0 -0
  164. /package/{build/src/objects/PinTypes.js → dist/esm/objects/PinTypes.mjs} +0 -0
  165. /package/{build/src/objects/Wire.js → dist/esm/objects/Wire.mjs} +0 -0
  166. /package/{build/src/server.js → dist/esm/server.mjs} +0 -0
  167. /package/{build/src/utils.js → dist/esm/utils.mjs} +0 -0
  168. /package/{build/src → dist/types}/export.d.ts +0 -0
  169. /package/{build/src → dist/types}/fonts.d.ts +0 -0
  170. /package/{build/src → dist/types}/globals.d.ts +0 -0
  171. /package/{build/src → dist/types}/logger.d.ts +0 -0
  172. /package/{build/src → dist/types}/main.d.ts +0 -0
  173. /package/{build/src → dist/types}/objects/Frame.d.ts +0 -0
  174. /package/{build/src → dist/types}/objects/Net.d.ts +0 -0
  175. /package/{build/src → dist/types}/objects/ParamDefinition.d.ts +0 -0
  176. /package/{build/src → dist/types}/objects/PinTypes.d.ts +0 -0
  177. /package/{build/src → dist/types}/objects/Wire.d.ts +0 -0
  178. /package/{build/src → dist/types}/regenerate-tests.d.ts +0 -0
  179. /package/{build/src → dist/types}/server.d.ts +0 -0
  180. /package/{build/src → dist/types}/utils.d.ts +0 -0
@@ -0,0 +1,819 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SymbolDrawingCommands = exports.SymbolDrawing = exports.SymbolCustom = exports.PlaceHolderCommands = exports.SymbolPlaceholder = exports.SymbolText = exports.SymbolPointHidden = exports.SymbolFactory = exports.SymbolGraphic = void 0;
4
+ const globals_js_1 = require("./globals.js");
5
+ const geometry_js_1 = require("./geometry.js");
6
+ const defaultSymbolLineWidth = 2;
7
+ class SymbolGraphic {
8
+ constructor() {
9
+ this.drawPortsName = true;
10
+ this.displayBounds = true;
11
+ this._angle = 0;
12
+ this._flipX = 0;
13
+ this._flipY = 0;
14
+ this.labelTexts = new Map();
15
+ }
16
+ get angle() {
17
+ return (this._angle % 360);
18
+ }
19
+ set angle(value) {
20
+ this._angle = value;
21
+ }
22
+ get flipX() {
23
+ return this._flipX;
24
+ }
25
+ set flipX(value) {
26
+ this._flipX = value;
27
+ }
28
+ get flipY() {
29
+ return this._flipY;
30
+ }
31
+ set flipY(value) {
32
+ this._flipY = value;
33
+ }
34
+ refreshDrawing(calculateSize = true) {
35
+ this.generateDrawing();
36
+ calculateSize && this.calculateSize();
37
+ }
38
+ calculateSize() {
39
+ const { width, height } = this.drawing.getBoundingBox();
40
+ this.width = width;
41
+ this.height = height;
42
+ }
43
+ size() {
44
+ return {
45
+ width: this.width,
46
+ height: this.height
47
+ };
48
+ }
49
+ draw(group, extra) {
50
+ const innerGroup = group.group();
51
+ this.drawBody(innerGroup);
52
+ this.drawPins(innerGroup);
53
+ this.drawLabels(innerGroup);
54
+ this.drawPlaceRemove(innerGroup, extra);
55
+ }
56
+ drawPlaceRemove(group, extra) {
57
+ if (extra && extra.place === false) {
58
+ const { start, end } = this.drawing.getBoundingBox(true);
59
+ const path = geometry_js_1.Geometry.roundPathValues([
60
+ "M", start[0], start[1], "L", end[0], end[1],
61
+ "M", end[0], start[1], "L", start[0], end[1]
62
+ ]);
63
+ group.path(path)
64
+ .stroke({
65
+ width: defaultSymbolLineWidth,
66
+ color: 'red'
67
+ });
68
+ }
69
+ }
70
+ pinPosition(id) {
71
+ const pin = this.drawing.getPinPosition(id);
72
+ const [x, y] = pin.start;
73
+ const useX = Math.round(x * 10000) / 10000;
74
+ const useY = Math.round(y * 10000 / 10000);
75
+ return {
76
+ x: useX,
77
+ y: useY,
78
+ angle: pin.angle,
79
+ };
80
+ }
81
+ drawBounds(group) {
82
+ const bbox = this.drawing.getBoundingBox();
83
+ group.circle(3)
84
+ .translate(-3 / 2, -3 / 2)
85
+ .fill('red')
86
+ .stroke('none');
87
+ group.rect(bbox.width, bbox.height)
88
+ .translate(bbox.start[0], bbox.start[1])
89
+ .fill('none')
90
+ .stroke({
91
+ width: 1,
92
+ color: '#ccc',
93
+ });
94
+ }
95
+ drawBody(group) {
96
+ const paths = this.drawing.getPaths();
97
+ paths.forEach(pathInfo => {
98
+ const { path, lineColor, fillColor, lineWidth } = pathInfo;
99
+ group.path(path)
100
+ .stroke({
101
+ width: lineWidth,
102
+ color: lineColor,
103
+ })
104
+ .fill(fillColor);
105
+ });
106
+ }
107
+ drawPins(group) {
108
+ group.path(this.drawing.getPinsPath())
109
+ .stroke({
110
+ width: defaultSymbolLineWidth,
111
+ color: '#333',
112
+ });
113
+ }
114
+ drawLabels(group) {
115
+ const labels = this.drawing.getLabels();
116
+ labels.forEach(label => {
117
+ const tmpLabel = label;
118
+ const { fontSize = 10, anchor = geometry_js_1.HorizontalAlign.Left, vanchor = geometry_js_1.VerticalAlign.Bottom, fontWeight = 'regular', angle: labelAngle = 0, } = tmpLabel.style ?? {};
119
+ let anchorStyle = 'start';
120
+ let dominantBaseline = 'auto';
121
+ let useAnchor = anchor;
122
+ let useDominantBaseline = vanchor;
123
+ const isRotation180 = Math.abs(this.angle) === 180;
124
+ if (isRotation180) {
125
+ useAnchor = this.flipTextAnchor(anchor);
126
+ useDominantBaseline = this.flipDominantBaseline(vanchor);
127
+ }
128
+ switch (useAnchor) {
129
+ case geometry_js_1.HorizontalAlign.Left:
130
+ anchorStyle = (this.flipX === 0) ? 'start' : 'end';
131
+ break;
132
+ case geometry_js_1.HorizontalAlign.Middle:
133
+ anchorStyle = 'middle';
134
+ break;
135
+ case geometry_js_1.HorizontalAlign.Right:
136
+ anchorStyle = (this.flipX === 0) ? 'end' : 'start';
137
+ break;
138
+ }
139
+ switch (useDominantBaseline) {
140
+ case geometry_js_1.VerticalAlign.Top:
141
+ dominantBaseline = (this.flipY === 0) ? 'hanging' : 'text-top';
142
+ break;
143
+ case geometry_js_1.VerticalAlign.Middle:
144
+ dominantBaseline = 'middle';
145
+ break;
146
+ case geometry_js_1.VerticalAlign.Bottom:
147
+ dominantBaseline = (this.flipY === 0) ? 'text-top' : 'hanging';
148
+ break;
149
+ }
150
+ const position = tmpLabel.getLabelPosition();
151
+ if (this.flipX !== 0) {
152
+ position[0] *= -1;
153
+ }
154
+ if (this.flipY !== 0) {
155
+ position[1] *= -1;
156
+ }
157
+ const useFont = globals_js_1.defaultFont;
158
+ const textContainer = group.group();
159
+ const text = textContainer.text(tmpLabel.text)
160
+ .fill('#333')
161
+ .font({
162
+ family: useFont,
163
+ size: fontSize,
164
+ anchor: anchorStyle,
165
+ 'dominant-baseline': dominantBaseline,
166
+ weight: fontWeight,
167
+ });
168
+ let translateX, translateY;
169
+ let useRotateAngle = 0;
170
+ if (isRotation180) {
171
+ translateX = -position[0];
172
+ translateY = -position[1];
173
+ useRotateAngle = 0;
174
+ }
175
+ else {
176
+ translateX = position[0];
177
+ translateY = position[1];
178
+ useRotateAngle = this.angle;
179
+ }
180
+ translateX = this.roundValues(translateX);
181
+ translateY = this.roundValues(translateY);
182
+ text.rotate(labelAngle);
183
+ textContainer.translate(translateX, translateY)
184
+ .rotate(useRotateAngle, -translateX, -translateY);
185
+ const { a, b, c, d, e, f } = textContainer.matrix();
186
+ const newMatrix = {
187
+ a: this.roundValues(a),
188
+ b: this.roundValues(b),
189
+ c: this.roundValues(c),
190
+ d: this.roundValues(d),
191
+ e: this.roundValues(e),
192
+ f: this.roundValues(f),
193
+ };
194
+ textContainer.transform(newMatrix);
195
+ });
196
+ }
197
+ roundValues(value) {
198
+ return +value.toFixed(7);
199
+ }
200
+ flipTextAnchor(value) {
201
+ if (value === geometry_js_1.HorizontalAlign.Left) {
202
+ return geometry_js_1.HorizontalAlign.Right;
203
+ }
204
+ else if (value === geometry_js_1.HorizontalAlign.Right) {
205
+ return geometry_js_1.HorizontalAlign.Left;
206
+ }
207
+ else {
208
+ return geometry_js_1.HorizontalAlign.Middle;
209
+ }
210
+ }
211
+ flipDominantBaseline(value) {
212
+ if (value === geometry_js_1.VerticalAlign.Top) {
213
+ return geometry_js_1.VerticalAlign.Bottom;
214
+ }
215
+ else if (value === geometry_js_1.VerticalAlign.Bottom) {
216
+ return geometry_js_1.VerticalAlign.Top;
217
+ }
218
+ else {
219
+ return geometry_js_1.VerticalAlign.Middle;
220
+ }
221
+ }
222
+ setLabelValue(labelId, labelValue) {
223
+ this.labelTexts.set(labelId, labelValue);
224
+ }
225
+ getLabelValue(labelId) {
226
+ if (this.labelTexts.has(labelId)) {
227
+ return this.labelTexts.get(labelId);
228
+ }
229
+ return undefined;
230
+ }
231
+ }
232
+ exports.SymbolGraphic = SymbolGraphic;
233
+ function SymbolFactory(name) {
234
+ switch (name) {
235
+ case 'point':
236
+ return new SymbolPointHidden();
237
+ }
238
+ return null;
239
+ }
240
+ exports.SymbolFactory = SymbolFactory;
241
+ class SymbolPointHidden extends SymbolGraphic {
242
+ generateDrawing() {
243
+ const drawing = new SymbolDrawing();
244
+ drawing.addPin(1, 0, 0, 0, 0);
245
+ this.drawing = drawing;
246
+ }
247
+ }
248
+ exports.SymbolPointHidden = SymbolPointHidden;
249
+ class SymbolText extends SymbolGraphic {
250
+ constructor(text) {
251
+ super();
252
+ this.fontSize = 10;
253
+ this.fontWeight = 'regular';
254
+ this.text = text;
255
+ }
256
+ generateDrawing() {
257
+ const drawing = new SymbolDrawing();
258
+ drawing.addLabel(0, 0, this.text, {
259
+ fontSize: this.fontSize,
260
+ anchor: geometry_js_1.HorizontalAlign.Middle,
261
+ fontWeight: this.fontWeight,
262
+ });
263
+ this.drawing = drawing;
264
+ }
265
+ }
266
+ exports.SymbolText = SymbolText;
267
+ class SymbolPlaceholder extends SymbolGraphic {
268
+ generateDrawing() {
269
+ const drawing = this.drawing;
270
+ drawing.log("=== start generate drawing ===");
271
+ drawing.clear();
272
+ drawing.angle = this._angle;
273
+ drawing.flipX = this._flipX;
274
+ drawing.flipY = this._flipY;
275
+ const commands = drawing.getCommands();
276
+ drawing.log('id: ', drawing.id, 'angle: ', this._angle, "commands:", commands.length);
277
+ commands.forEach(([commandName, positionParams, keywordParams]) => {
278
+ switch (commandName) {
279
+ case PlaceHolderCommands.rect:
280
+ drawing.log('add rect', ...positionParams);
281
+ drawing.addRect(...positionParams);
282
+ break;
283
+ case PlaceHolderCommands.hline:
284
+ drawing.log('add hline', ...positionParams);
285
+ drawing.addHLine(...positionParams);
286
+ break;
287
+ case PlaceHolderCommands.vline:
288
+ drawing.log('add vline', ...positionParams);
289
+ drawing.addVLine(...positionParams);
290
+ break;
291
+ case PlaceHolderCommands.line:
292
+ drawing.log('add line', ...positionParams);
293
+ drawing.addLine(...positionParams);
294
+ break;
295
+ case PlaceHolderCommands.path:
296
+ drawing.addPath(...positionParams);
297
+ break;
298
+ case PlaceHolderCommands.lineWidth:
299
+ drawing.addSetLineWidth(...positionParams);
300
+ break;
301
+ case PlaceHolderCommands.fill:
302
+ drawing.addSetFillColor(...positionParams);
303
+ break;
304
+ case PlaceHolderCommands.lineColor:
305
+ drawing.addSetLineColor(...positionParams);
306
+ break;
307
+ case PlaceHolderCommands.arc:
308
+ drawing.addArc(...positionParams);
309
+ break;
310
+ case PlaceHolderCommands.circle:
311
+ drawing.addArc(...positionParams, 0, 360);
312
+ break;
313
+ case PlaceHolderCommands.triangle:
314
+ drawing.addTriangle(...positionParams);
315
+ break;
316
+ case PlaceHolderCommands.pin:
317
+ case PlaceHolderCommands.hpin:
318
+ case PlaceHolderCommands.vpin:
319
+ {
320
+ this.drawPinParams(drawing, commandName, keywordParams, positionParams);
321
+ break;
322
+ }
323
+ case PlaceHolderCommands.label: {
324
+ const keywords = ['fontSize', 'anchor', 'vanchor', 'angle'];
325
+ const style = {};
326
+ keywords.forEach(item => {
327
+ if (keywordParams.has(item)) {
328
+ style[item] = keywordParams.get(item);
329
+ }
330
+ });
331
+ positionParams = [...positionParams];
332
+ positionParams.push(style);
333
+ const labelId = positionParams[0];
334
+ const tmpPositionParams = [...positionParams];
335
+ const tmpLabelValue = this.getLabelValue(labelId);
336
+ if (tmpLabelValue !== undefined) {
337
+ tmpPositionParams[3] = tmpLabelValue;
338
+ }
339
+ drawing.log('add label', JSON.stringify(tmpPositionParams));
340
+ drawing.addLabelId(...tmpPositionParams);
341
+ break;
342
+ }
343
+ }
344
+ });
345
+ drawing.log("=== end generate drawing ===");
346
+ }
347
+ drawPinParams(drawing, commandName, keywordParams, positionParams) {
348
+ drawing.log('add pin', ...positionParams);
349
+ const keywordDisplayPinId = 'display_pin_id';
350
+ let displayPinId = true;
351
+ if (keywordParams.has(keywordDisplayPinId)
352
+ && keywordParams.get(keywordDisplayPinId) === 0) {
353
+ displayPinId = false;
354
+ }
355
+ let pinNameParam = null;
356
+ if (typeof positionParams[1] === 'string') {
357
+ pinNameParam = positionParams[1];
358
+ positionParams = [positionParams[0], ...positionParams.slice(2)];
359
+ }
360
+ const startX = positionParams[1];
361
+ const startY = positionParams[2];
362
+ if (commandName === PlaceHolderCommands.vpin) {
363
+ const magnitude = positionParams[3];
364
+ positionParams = [
365
+ positionParams[0],
366
+ startX,
367
+ startY,
368
+ startX,
369
+ startY + magnitude
370
+ ];
371
+ }
372
+ else if (commandName === PlaceHolderCommands.hpin) {
373
+ const magnitude = positionParams[3];
374
+ positionParams = [
375
+ positionParams[0],
376
+ startX,
377
+ startY,
378
+ startX + magnitude,
379
+ startY
380
+ ];
381
+ }
382
+ drawing.addPin(...positionParams);
383
+ const lastAddedPin = this.drawing.pins[this.drawing.pins.length - 1];
384
+ const [pinId, , angle] = lastAddedPin;
385
+ const [, , , endX, endY] = positionParams;
386
+ let pinNameAlignment = geometry_js_1.HorizontalAlign.Left;
387
+ let pinNameOffsetX = 4;
388
+ let pinIdOffsetX = 0;
389
+ let pinIdAlignment = geometry_js_1.HorizontalAlign.Left;
390
+ let pinIdVAlignment = geometry_js_1.VerticalAlign.Bottom;
391
+ let pinIdOffsetY = -2;
392
+ switch (angle) {
393
+ case 0:
394
+ pinNameAlignment = geometry_js_1.HorizontalAlign.Left;
395
+ pinNameOffsetX = 4;
396
+ pinIdAlignment = geometry_js_1.HorizontalAlign.Right;
397
+ pinIdOffsetX = -2;
398
+ break;
399
+ case 90:
400
+ case 180:
401
+ pinNameAlignment = geometry_js_1.HorizontalAlign.Right;
402
+ pinNameOffsetX = -4;
403
+ pinIdAlignment = geometry_js_1.HorizontalAlign.Left;
404
+ pinIdOffsetX = 2;
405
+ break;
406
+ case 270:
407
+ pinNameAlignment = geometry_js_1.HorizontalAlign.Left;
408
+ pinNameOffsetX = 4;
409
+ pinIdAlignment = geometry_js_1.HorizontalAlign.Left;
410
+ pinIdOffsetX = 2;
411
+ pinIdOffsetY = 2;
412
+ pinIdVAlignment = geometry_js_1.VerticalAlign.Top;
413
+ break;
414
+ }
415
+ if (angle === 0 || angle === 90 || angle === 180 || angle === 270) {
416
+ const usePinName = pinNameParam ?? "";
417
+ usePinName !== "" && drawing.addLabel(endX + pinNameOffsetX, endY, usePinName, {
418
+ fontSize: 10,
419
+ anchor: pinNameAlignment,
420
+ vanchor: geometry_js_1.VerticalAlign.Middle,
421
+ });
422
+ displayPinId && drawing.addLabel(endX + pinIdOffsetX, endY + pinIdOffsetY, pinId.toString(), {
423
+ fontSize: 8,
424
+ anchor: pinIdAlignment,
425
+ vanchor: pinIdVAlignment,
426
+ });
427
+ }
428
+ }
429
+ constructor(drawing) {
430
+ super();
431
+ this.drawing = drawing;
432
+ }
433
+ }
434
+ exports.SymbolPlaceholder = SymbolPlaceholder;
435
+ var PlaceHolderCommands;
436
+ (function (PlaceHolderCommands) {
437
+ PlaceHolderCommands["arc"] = "arc";
438
+ PlaceHolderCommands["circle"] = "circle";
439
+ PlaceHolderCommands["rect"] = "rect";
440
+ PlaceHolderCommands["triangle"] = "triangle";
441
+ PlaceHolderCommands["pin"] = "pin";
442
+ PlaceHolderCommands["hpin"] = "hpin";
443
+ PlaceHolderCommands["vpin"] = "vpin";
444
+ PlaceHolderCommands["hline"] = "hline";
445
+ PlaceHolderCommands["vline"] = "vline";
446
+ PlaceHolderCommands["line"] = "line";
447
+ PlaceHolderCommands["label"] = "label";
448
+ PlaceHolderCommands["path"] = "path";
449
+ PlaceHolderCommands["lineWidth"] = "lineWidth";
450
+ PlaceHolderCommands["fill"] = "fill";
451
+ PlaceHolderCommands["lineColor"] = "lineColor";
452
+ })(PlaceHolderCommands || (exports.PlaceHolderCommands = PlaceHolderCommands = {}));
453
+ class SymbolCustom extends SymbolGraphic {
454
+ constructor(pinDefinition) {
455
+ super();
456
+ this.pinDefinition = [];
457
+ this.bodyWidth = 100;
458
+ this.pinLength = 20;
459
+ this.width = 100;
460
+ this.height = 100;
461
+ this.pinSpacing = 20;
462
+ this.pinTextPadding = 5;
463
+ this.pins = [];
464
+ this.pinDefinition = pinDefinition;
465
+ }
466
+ generateDrawing() {
467
+ const leftPins = this.pinDefinition.filter(item => {
468
+ return item.side === globals_js_1.SymbolPinSide.Left;
469
+ });
470
+ const rightPins = this.pinDefinition.filter(item => {
471
+ return item.side === globals_js_1.SymbolPinSide.Right;
472
+ });
473
+ const maxLeftPins = Math.max(...leftPins.map(item => item.position)) + 1;
474
+ const maxRightPins = Math.max(...rightPins.map(item => item.position)) + 1;
475
+ const drawing = new SymbolDrawing();
476
+ drawing.angle = this._angle;
477
+ drawing.flipX = this._flipX;
478
+ drawing.flipY = this._flipY;
479
+ const bodyWidth = this.bodyWidth;
480
+ const bodyHeight = (1 + Math.max(maxLeftPins, maxRightPins)) * this.pinSpacing;
481
+ drawing.addRect(0, 0, bodyWidth, bodyHeight);
482
+ const leftPinStart = -bodyWidth / 2;
483
+ const rightPinStart = bodyWidth / 2;
484
+ const pinStartY = -bodyHeight / 2;
485
+ leftPins.forEach(pin => {
486
+ const position = pin.position;
487
+ const pinY = pinStartY + (position + 1) * this.pinSpacing;
488
+ drawing.addPin(pin.pinId, leftPinStart - this.pinLength, pinY, leftPinStart, pinY);
489
+ drawing.addLabel(leftPinStart + 4, pinY, pin.text, {
490
+ fontSize: 10,
491
+ anchor: geometry_js_1.HorizontalAlign.Left,
492
+ vanchor: geometry_js_1.VerticalAlign.Middle,
493
+ });
494
+ drawing.addLabel(leftPinStart - 2, pinY - 2, pin.pinId.toString(), {
495
+ fontSize: 8,
496
+ anchor: geometry_js_1.HorizontalAlign.Right,
497
+ vanchor: geometry_js_1.VerticalAlign.Bottom,
498
+ });
499
+ });
500
+ rightPins.forEach(pin => {
501
+ const position = pin.position;
502
+ const pinY = pinStartY + (position + 1) * this.pinSpacing;
503
+ drawing.addPin(pin.pinId, rightPinStart + this.pinLength, pinY, rightPinStart, pinY);
504
+ drawing.addLabel(rightPinStart - 4, pinY, pin.text, {
505
+ fontSize: 10,
506
+ anchor: geometry_js_1.HorizontalAlign.Right,
507
+ vanchor: geometry_js_1.VerticalAlign.Middle,
508
+ });
509
+ drawing.addLabel(rightPinStart + 2, pinY - 2, pin.pinId.toString(), {
510
+ fontSize: 8,
511
+ anchor: geometry_js_1.HorizontalAlign.Left,
512
+ vanchor: geometry_js_1.VerticalAlign.Bottom,
513
+ });
514
+ });
515
+ const instanceName = this.getLabelValue("refdes");
516
+ instanceName && drawing.addLabel(-bodyWidth / 2, -bodyHeight / 2 - 4, instanceName, {
517
+ fontSize: 10,
518
+ anchor: geometry_js_1.HorizontalAlign.Left,
519
+ });
520
+ const acceptedMPNKeys = ['MPN', 'mpn', 'manufacturer_pn'];
521
+ acceptedMPNKeys.some(key => {
522
+ const labelValue = this.getLabelValue(key);
523
+ if (labelValue !== undefined) {
524
+ drawing.addLabel(-bodyWidth / 2, bodyHeight / 2 + 4, labelValue, {
525
+ fontSize: 10,
526
+ anchor: geometry_js_1.HorizontalAlign.Left,
527
+ vanchor: geometry_js_1.VerticalAlign.Top,
528
+ });
529
+ }
530
+ });
531
+ this.drawing = drawing;
532
+ this._cacheLeftPins = leftPins;
533
+ this._cacheRightPins = rightPins;
534
+ }
535
+ calculateSize() {
536
+ this.width = this.bodyWidth + 2 * this.pinLength;
537
+ this.height = (1 + Math.max(this._cacheLeftPins.length, this._cacheRightPins.length)) * this.pinSpacing;
538
+ }
539
+ }
540
+ exports.SymbolCustom = SymbolCustom;
541
+ class SymbolDrawing {
542
+ constructor() {
543
+ this.items = [];
544
+ this.pins = [];
545
+ this.angle = 0;
546
+ this.flipX = 0;
547
+ this.flipY = 0;
548
+ this.mainOrigin = [0, 0];
549
+ this.logger = null;
550
+ }
551
+ clear() {
552
+ this.items = [];
553
+ this.pins = [];
554
+ }
555
+ log(...params) {
556
+ this.logger && this.logger.add(params.join(' '));
557
+ }
558
+ addLine(startX, startY, endX, endY) {
559
+ this.items.push(geometry_js_1.Geometry.segment([startX, startY], [endX, endY]));
560
+ return this;
561
+ }
562
+ addPin(pinId, startX, startY, endX, endY) {
563
+ let angle = 0;
564
+ if (startX === endX) {
565
+ if (startY > endY) {
566
+ angle = 270;
567
+ }
568
+ else if (startY < endY) {
569
+ angle = 90;
570
+ }
571
+ }
572
+ else {
573
+ if (startX < endX) {
574
+ angle = 0;
575
+ }
576
+ else if (startX > endX) {
577
+ angle = 180;
578
+ }
579
+ }
580
+ this.pins.push([
581
+ pinId,
582
+ geometry_js_1.Geometry.segment([startX, startY], [endX, endY]),
583
+ angle
584
+ ]);
585
+ return this;
586
+ }
587
+ addVLine(startX, startY, value) {
588
+ this.items.push(geometry_js_1.Geometry.segment([startX, startY], [startX, startY + value]));
589
+ return this;
590
+ }
591
+ addHLine(startX, startY, value) {
592
+ this.items.push(geometry_js_1.Geometry.segment([startX, startY], [startX + value, startY]));
593
+ return this;
594
+ }
595
+ addRect(centerX, centerY, width, height) {
596
+ const width2 = width / 2;
597
+ const height2 = height / 2;
598
+ this.items.push(geometry_js_1.Geometry.polygon([
599
+ [centerX - width2, centerY - height2],
600
+ [centerX + width2, centerY - height2],
601
+ [centerX + width2, centerY + height2],
602
+ [centerX - width2, centerY + height2],
603
+ [centerX - width2, centerY - height2]
604
+ ]));
605
+ return this;
606
+ }
607
+ addTriangle(startX, startY, endX, endY, width) {
608
+ const line = geometry_js_1.Geometry.line(startX, startY, endX, endY);
609
+ const normLine = line.norm;
610
+ const dx1 = normLine.x * width / 2;
611
+ const dy1 = normLine.y * width / 2;
612
+ const dx2 = normLine.x * -width / 2;
613
+ const dy2 = normLine.y * -width / 2;
614
+ this.items.push(geometry_js_1.Geometry.polygon([
615
+ [dx1 + startX, dy1 + startY],
616
+ [dx2 + startX, dy2 + startY],
617
+ [endX, endY],
618
+ [dx1 + startX, dy1 + startY],
619
+ ]));
620
+ return this;
621
+ }
622
+ addRect2(x, y, x2, y2) {
623
+ this.items.push(geometry_js_1.Geometry.polygon([
624
+ [x, y],
625
+ [x2, y],
626
+ [x2, y2],
627
+ [x, y2],
628
+ [x, y]
629
+ ]));
630
+ return this;
631
+ }
632
+ addLabel(x, y, textValue, style) {
633
+ this.items.push(geometry_js_1.Geometry.label(null, x, y, textValue, style));
634
+ return this;
635
+ }
636
+ addLabelId(id, x, y, textValue, style) {
637
+ this.items.push(geometry_js_1.Geometry.label(id, x, y, textValue, style));
638
+ return this;
639
+ }
640
+ addPath(...pathParts) {
641
+ const parts = pathParts.reduce((accum, tmp) => {
642
+ if (typeof tmp === "string") {
643
+ accum = accum.concat(tmp.split(" "));
644
+ }
645
+ else if (typeof tmp === "number") {
646
+ accum.push(tmp);
647
+ }
648
+ return accum;
649
+ }, []);
650
+ const geomObjects = [];
651
+ let currentObj = null;
652
+ for (let i = 0; i < parts.length; i++) {
653
+ const command = parts[i];
654
+ if (command === 'M') {
655
+ if (currentObj !== null) {
656
+ geomObjects.push(currentObj);
657
+ }
658
+ const x = Number(parts[i + 1]);
659
+ const y = Number(parts[i + 2]);
660
+ currentObj = [[x, y]];
661
+ i += 2;
662
+ }
663
+ else if (command === 'L') {
664
+ const x = Number(parts[i + 1]);
665
+ const y = Number(parts[i + 2]);
666
+ currentObj.push([x, y]);
667
+ i += 2;
668
+ }
669
+ else if (command === 'Z') {
670
+ const firstPoint = currentObj[0];
671
+ currentObj.push(firstPoint);
672
+ }
673
+ }
674
+ if (currentObj !== null) {
675
+ geomObjects.push(currentObj);
676
+ currentObj = null;
677
+ }
678
+ geomObjects.forEach(coords => {
679
+ const [first] = coords;
680
+ const last = coords[coords.length - 1];
681
+ if (first[0] === last[0] && first[1] === last[1]) {
682
+ this.items.push(geometry_js_1.Geometry.polygon(coords));
683
+ }
684
+ else {
685
+ this.items.push(geometry_js_1.Geometry.multiline(coords));
686
+ }
687
+ });
688
+ return this;
689
+ }
690
+ addSetLineWidth(value) {
691
+ this.items.push(new geometry_js_1.GeometryProp('lineWidth', value));
692
+ return this;
693
+ }
694
+ addSetLineColor(value) {
695
+ this.items.push(new geometry_js_1.GeometryProp('lineColor', value));
696
+ return this;
697
+ }
698
+ addSetFillColor(value) {
699
+ this.items.push(new geometry_js_1.GeometryProp('fillColor', value));
700
+ return this;
701
+ }
702
+ addArc(x, y, radius, startAngle, endAngle) {
703
+ startAngle = startAngle * Math.PI / 180;
704
+ endAngle = endAngle * Math.PI / 180;
705
+ this.items.push(geometry_js_1.Geometry.arc([x, y], radius, startAngle, endAngle, true));
706
+ return this;
707
+ }
708
+ getPaths() {
709
+ let currentFill = "#fff";
710
+ let currentLineWidth = 1;
711
+ let currentLineColor = '#333';
712
+ const pathItems = [];
713
+ this.items.forEach(item => {
714
+ if (!(item instanceof geometry_js_1.Label)) {
715
+ if (item instanceof geometry_js_1.GeometryProp) {
716
+ if (item.name === 'lineWidth') {
717
+ currentLineWidth = item.value;
718
+ }
719
+ else if (item.name === 'lineColor') {
720
+ currentLineColor = item.value;
721
+ }
722
+ else if (item.name === 'fillColor') {
723
+ currentFill = item.value;
724
+ }
725
+ }
726
+ else {
727
+ let tmpResult = geometry_js_1.Geometry.groupFlip([item], this.flipX, this.flipY);
728
+ tmpResult = geometry_js_1.Geometry.groupRotate(tmpResult, this.angle, this.mainOrigin);
729
+ const { path, isClosedPolygon } = this.featuresToPath(tmpResult);
730
+ pathItems.push({
731
+ path: path,
732
+ lineWidth: currentLineWidth,
733
+ lineColor: currentLineColor,
734
+ fillColor: isClosedPolygon ? currentFill : 'none',
735
+ });
736
+ }
737
+ }
738
+ });
739
+ return pathItems;
740
+ }
741
+ getPinsPath() {
742
+ let features = this.pins.map(item => item[1]);
743
+ features = geometry_js_1.Geometry.groupFlip(features, this.flipX, this.flipY);
744
+ features = geometry_js_1.Geometry.groupRotate(features, this.angle, this.mainOrigin);
745
+ const { path } = this.featuresToPath(features);
746
+ return path;
747
+ }
748
+ getLabels() {
749
+ return this.items.filter(item => item instanceof geometry_js_1.Label);
750
+ }
751
+ featuresToPath(items) {
752
+ return geometry_js_1.Geometry.featuresToPath(items);
753
+ }
754
+ getBoundingBox(excludeLabels = false) {
755
+ const pinFeatures = this.pins.map(pin => {
756
+ return pin[1];
757
+ });
758
+ const drawingFeatures = this.items.reduce((accum, item) => {
759
+ if (!excludeLabels || (excludeLabels && !(item instanceof geometry_js_1.Label))) {
760
+ if (!(item instanceof geometry_js_1.GeometryProp)) {
761
+ accum.push(item);
762
+ }
763
+ }
764
+ return accum;
765
+ }, []);
766
+ let features = [...drawingFeatures, ...pinFeatures];
767
+ features = geometry_js_1.Geometry.groupFlip(features, this.flipX, this.flipY);
768
+ features = geometry_js_1.Geometry.groupRotate(features, this.angle, this.mainOrigin);
769
+ return geometry_js_1.Geometry.groupBounds(features);
770
+ }
771
+ getPinPosition(pinId) {
772
+ const pin = this.pins.find(item => {
773
+ return item[0] === pinId;
774
+ });
775
+ if (pin) {
776
+ const [, feature, angle] = pin;
777
+ let tmpFeature = geometry_js_1.Geometry.flip(feature, this.flipX, this.flipY);
778
+ tmpFeature = geometry_js_1.Geometry.rotateDegs(tmpFeature, this.angle, this.mainOrigin);
779
+ const coords = geometry_js_1.Geometry.getCoords(tmpFeature);
780
+ return {
781
+ start: coords[0],
782
+ end: coords[1],
783
+ angle,
784
+ };
785
+ }
786
+ return null;
787
+ }
788
+ }
789
+ exports.SymbolDrawing = SymbolDrawing;
790
+ class SymbolDrawingCommands extends SymbolDrawing {
791
+ constructor(commands) {
792
+ super();
793
+ this.id = "";
794
+ this.commands = commands;
795
+ this.id = Math.random().toString().slice(2);
796
+ }
797
+ getCommands() {
798
+ return this.commands;
799
+ }
800
+ clone() {
801
+ const tmpCommands = this.commands.map(item => {
802
+ if (item[0] === PlaceHolderCommands.label) {
803
+ const commandName = item[0];
804
+ const positionParams = item[1];
805
+ const keywordParams = item[2];
806
+ const newMap = new Map();
807
+ for (const [key, value] of keywordParams) {
808
+ newMap.set(key, value);
809
+ }
810
+ return [commandName, positionParams, newMap];
811
+ }
812
+ else {
813
+ return [...item];
814
+ }
815
+ });
816
+ return new SymbolDrawingCommands(tmpCommands);
817
+ }
818
+ }
819
+ exports.SymbolDrawingCommands = SymbolDrawingCommands;