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.
- package/dist/cjs/BaseVisitor.js +487 -0
- package/dist/cjs/SemanticTokenVisitor.js +218 -0
- package/dist/cjs/SymbolValidatorVisitor.js +233 -0
- package/dist/cjs/antlr/CircuitScriptLexer.js +302 -0
- package/dist/cjs/antlr/CircuitScriptParser.js +5128 -0
- package/dist/cjs/antlr/CircuitScriptVisitor.js +7 -0
- package/dist/cjs/draw_symbols.js +819 -0
- package/dist/cjs/execute.js +778 -0
- package/{src/export.ts → dist/cjs/export.js} +34 -56
- package/dist/cjs/fonts.js +4 -0
- package/dist/cjs/geometry.js +450 -0
- package/dist/cjs/globals.js +60 -0
- package/dist/cjs/helpers.js +269 -0
- package/dist/cjs/index.js +31 -0
- package/{src/layout.ts → dist/cjs/layout.js} +421 -1002
- package/dist/cjs/lexer.js +111 -0
- package/dist/cjs/logger.js +17 -0
- package/dist/cjs/main.js +82 -0
- package/dist/cjs/objects/ClassComponent.js +145 -0
- package/dist/cjs/objects/ExecutionScope.js +135 -0
- package/dist/cjs/objects/Frame.js +22 -0
- package/{src/objects/Net.ts → dist/cjs/objects/Net.js} +9 -24
- package/dist/cjs/objects/ParamDefinition.js +42 -0
- package/dist/cjs/objects/PinDefinition.js +31 -0
- package/dist/cjs/objects/PinTypes.js +11 -0
- package/dist/cjs/objects/Wire.js +9 -0
- package/dist/cjs/objects/types.js +15 -0
- package/dist/cjs/parser.js +70 -0
- package/dist/cjs/regenerate-tests.js +23 -0
- package/dist/cjs/render.js +155 -0
- package/{src/server.ts → dist/cjs/server.js} +15 -21
- package/dist/cjs/sizing.js +105 -0
- package/{src/utils.ts → dist/cjs/utils.js} +25 -35
- package/dist/cjs/validate.js +81 -0
- package/dist/cjs/visitor.js +844 -0
- package/dist/esm/BaseVisitor.mjs +488 -0
- package/dist/esm/SemanticTokenVisitor.mjs +215 -0
- package/dist/esm/SymbolValidatorVisitor.mjs +222 -0
- package/dist/esm/antlr/CircuitScriptLexer.mjs +276 -0
- package/dist/esm/antlr/CircuitScriptParser.mjs +5038 -0
- package/{build/src/antlr/CircuitScriptVisitor.js → dist/esm/antlr/CircuitScriptVisitor.mjs} +8 -3
- package/{build/src/draw_symbols.js → dist/esm/draw_symbols.mjs} +78 -33
- package/{build/src/execute.js → dist/esm/execute.mjs} +59 -60
- package/{build/src/export.js → dist/esm/export.mjs} +2 -2
- package/{build/src/geometry.js → dist/esm/geometry.mjs} +31 -15
- package/dist/esm/helpers.mjs +252 -0
- package/dist/esm/index.mjs +15 -0
- package/{build/src/layout.js → dist/esm/layout.mjs} +19 -11
- package/{build/src/lexer.js → dist/esm/lexer.mjs} +10 -10
- package/{build/src/main.js → dist/esm/main.mjs} +9 -14
- package/{build/src/objects/ClassComponent.js → dist/esm/objects/ClassComponent.mjs} +6 -3
- package/{build/src/objects/ExecutionScope.js → dist/esm/objects/ExecutionScope.mjs} +1 -0
- package/{build/src/objects/PinDefinition.js → dist/esm/objects/PinDefinition.mjs} +1 -1
- package/dist/esm/objects/types.mjs +12 -0
- package/dist/esm/parser.mjs +64 -0
- package/{build/src/regenerate-tests.js → dist/esm/regenerate-tests.mjs} +1 -1
- package/{build/src/render.js → dist/esm/render.mjs} +7 -24
- package/{build/src/sizing.js → dist/esm/sizing.mjs} +22 -8
- package/{src/main.ts → dist/esm/validate.mjs} +31 -62
- package/dist/esm/visitor.mjs +838 -0
- package/dist/types/BaseVisitor.d.ts +69 -0
- package/dist/types/SemanticTokenVisitor.d.ts +36 -0
- package/dist/types/SymbolValidatorVisitor.d.ts +61 -0
- package/{build/src → dist/types}/antlr/CircuitScriptLexer.d.ts +28 -27
- package/dist/types/antlr/CircuitScriptParser.d.ts +719 -0
- package/{build/src → dist/types}/antlr/CircuitScriptVisitor.d.ts +69 -59
- package/{build/src → dist/types}/draw_symbols.d.ts +11 -2
- package/{build/src → dist/types}/execute.d.ts +6 -9
- package/{build/src → dist/types}/geometry.d.ts +5 -1
- package/dist/types/helpers.d.ts +40 -0
- package/dist/types/index.d.ts +15 -0
- package/{build/src → dist/types}/layout.d.ts +10 -10
- package/{build/src → dist/types}/lexer.d.ts +2 -2
- package/{build/src → dist/types}/objects/ClassComponent.d.ts +2 -2
- package/{build/src → dist/types}/objects/ExecutionScope.d.ts +4 -1
- package/{build/src → dist/types}/objects/PinDefinition.d.ts +1 -1
- package/{build/src → dist/types}/objects/types.d.ts +5 -0
- package/dist/types/parser.d.ts +25 -0
- package/{build/src → dist/types}/render.d.ts +1 -1
- package/{build/src → dist/types}/sizing.d.ts +3 -1
- package/dist/types/validate.d.ts +2 -0
- package/dist/types/visitor.d.ts +80 -0
- package/libs/lib.cst +0 -2
- package/package.json +38 -15
- package/.editorconfig +0 -15
- package/.eslintignore +0 -1
- package/.eslintrc.json +0 -27
- package/.gitlab-ci.yml +0 -81
- package/.prettierignore +0 -8
- package/.prettierrc +0 -16
- package/__tests__/expectedResults.ts +0 -657
- package/__tests__/helpers.ts +0 -82
- package/__tests__/parseScripts.ts +0 -593
- package/__tests__/renderData/script1.cst +0 -58
- package/__tests__/renderData/script1.cst.svg +0 -1
- package/__tests__/renderData/script2.cst +0 -16
- package/__tests__/renderData/script2.cst.svg +0 -1
- package/__tests__/renderData/script3.cst +0 -30
- package/__tests__/renderData/script3.cst.svg +0 -1
- package/__tests__/renderData/script4.cst +0 -54
- package/__tests__/renderData/script4.cst.svg +0 -1
- package/__tests__/renderData/script5.cst +0 -23
- package/__tests__/renderData/script5.cst.svg +0 -1
- package/__tests__/renderData/script6.cst +0 -28
- package/__tests__/renderData/script6.cst.svg +0 -1
- package/__tests__/renderData/script7.cst +0 -26
- package/__tests__/renderData/script7.cst.svg +0 -1
- package/__tests__/renderData/script8.cst +0 -37
- package/__tests__/renderData/script8.cst.svg +0 -1
- package/__tests__/testCLI.ts +0 -68
- package/__tests__/testMathOps.ts +0 -36
- package/__tests__/testMergeWires.ts +0 -141
- package/__tests__/testParse.ts +0 -263
- package/__tests__/testRender.ts +0 -38
- package/build/src/antlr/CircuitScriptLexer.js +0 -287
- package/build/src/antlr/CircuitScriptParser.d.ts +0 -674
- package/build/src/antlr/CircuitScriptParser.js +0 -4841
- package/build/src/helpers.d.ts +0 -1
- package/build/src/helpers.js +0 -73
- package/build/src/objects/types.js +0 -6
- package/build/src/parser.js +0 -69
- package/build/src/visitor.d.ts +0 -133
- package/build/src/visitor.js +0 -1154
- package/documentation.md +0 -238
- package/examples/example_arduino_uno.cst +0 -1146
- package/examples/example_garden_pump.cst +0 -567
- package/examples/lib.cst +0 -185
- package/jest.config.js +0 -23
- package/refresh.html +0 -42
- package/server.cjs +0 -50
- package/src/antlr/CircuitScript.g4 +0 -209
- package/src/antlr/CircuitScriptLexer.ts +0 -317
- package/src/antlr/CircuitScriptParser.ts +0 -4979
- package/src/antlr/CircuitScriptVisitor.ts +0 -420
- package/src/draw_symbols.ts +0 -1085
- package/src/execute.ts +0 -1227
- package/src/fonts.ts +0 -1
- package/src/geometry.ts +0 -638
- package/src/globals.ts +0 -67
- package/src/helpers.ts +0 -114
- package/src/lexer.ts +0 -151
- package/src/logger.ts +0 -17
- package/src/objects/ClassComponent.ts +0 -223
- package/src/objects/ExecutionScope.ts +0 -201
- package/src/objects/Frame.ts +0 -20
- package/src/objects/ParamDefinition.ts +0 -49
- package/src/objects/PinDefinition.ts +0 -49
- package/src/objects/PinTypes.ts +0 -7
- package/src/objects/Wire.ts +0 -19
- package/src/objects/types.ts +0 -66
- package/src/parser.ts +0 -106
- package/src/regenerate-tests.ts +0 -25
- package/src/render.ts +0 -260
- package/src/sizing.ts +0 -96
- package/src/visitor.ts +0 -1691
- package/tsconfig.json +0 -27
- package/tsconfig.release.json +0 -8
- /package/{build/src/fonts.js → dist/esm/fonts.mjs} +0 -0
- /package/{build/src/globals.js → dist/esm/globals.mjs} +0 -0
- /package/{build/src/logger.js → dist/esm/logger.mjs} +0 -0
- /package/{build/src/objects/Frame.js → dist/esm/objects/Frame.mjs} +0 -0
- /package/{build/src/objects/Net.js → dist/esm/objects/Net.mjs} +0 -0
- /package/{build/src/objects/ParamDefinition.js → dist/esm/objects/ParamDefinition.mjs} +0 -0
- /package/{build/src/objects/PinTypes.js → dist/esm/objects/PinTypes.mjs} +0 -0
- /package/{build/src/objects/Wire.js → dist/esm/objects/Wire.mjs} +0 -0
- /package/{build/src/server.js → dist/esm/server.mjs} +0 -0
- /package/{build/src/utils.js → dist/esm/utils.mjs} +0 -0
- /package/{build/src → dist/types}/export.d.ts +0 -0
- /package/{build/src → dist/types}/fonts.d.ts +0 -0
- /package/{build/src → dist/types}/globals.d.ts +0 -0
- /package/{build/src → dist/types}/logger.d.ts +0 -0
- /package/{build/src → dist/types}/main.d.ts +0 -0
- /package/{build/src → dist/types}/objects/Frame.d.ts +0 -0
- /package/{build/src → dist/types}/objects/Net.d.ts +0 -0
- /package/{build/src → dist/types}/objects/ParamDefinition.d.ts +0 -0
- /package/{build/src → dist/types}/objects/PinTypes.d.ts +0 -0
- /package/{build/src → dist/types}/objects/Wire.d.ts +0 -0
- /package/{build/src → dist/types}/regenerate-tests.d.ts +0 -0
- /package/{build/src → dist/types}/server.d.ts +0 -0
- /package/{build/src → dist/types}/utils.d.ts +0 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CircuitscriptParserErrorListener = exports.TempErrorStrategy = exports.parseFileWithVisitor = void 0;
|
|
4
|
+
const CircuitScriptParser_js_1 = require("./antlr/CircuitScriptParser.js");
|
|
5
|
+
const lexer_js_1 = require("./lexer.js");
|
|
6
|
+
const utils_js_1 = require("./utils.js");
|
|
7
|
+
const antlr4ng_1 = require("antlr4ng");
|
|
8
|
+
function parseFileWithVisitor(visitor, data) {
|
|
9
|
+
const chars = antlr4ng_1.CharStream.fromString(data);
|
|
10
|
+
const lexer = new lexer_js_1.MainLexer(chars);
|
|
11
|
+
const lexerTimer = new utils_js_1.SimpleStopwatch();
|
|
12
|
+
const tokens = new antlr4ng_1.CommonTokenStream(lexer);
|
|
13
|
+
tokens.fill();
|
|
14
|
+
const lexerTimeTaken = lexerTimer.lap();
|
|
15
|
+
const parserTimer = new utils_js_1.SimpleStopwatch();
|
|
16
|
+
const parser = new CircuitScriptParser_js_1.CircuitScriptParser(tokens);
|
|
17
|
+
const tree = parser.script();
|
|
18
|
+
let hasError = false;
|
|
19
|
+
try {
|
|
20
|
+
visitor.visit(tree);
|
|
21
|
+
}
|
|
22
|
+
catch (err) {
|
|
23
|
+
console.log(err);
|
|
24
|
+
hasError = true;
|
|
25
|
+
}
|
|
26
|
+
const parserTimeTaken = parserTimer.lap();
|
|
27
|
+
return {
|
|
28
|
+
tree, parser,
|
|
29
|
+
hasParseError: false,
|
|
30
|
+
hasError,
|
|
31
|
+
parserTimeTaken,
|
|
32
|
+
lexerTimeTaken,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
exports.parseFileWithVisitor = parseFileWithVisitor;
|
|
36
|
+
class TempErrorStrategy extends antlr4ng_1.DefaultErrorStrategy {
|
|
37
|
+
recover(recognizer, e) {
|
|
38
|
+
throw new Error('Method not implemented.');
|
|
39
|
+
}
|
|
40
|
+
reportError(recognizer, e) {
|
|
41
|
+
throw new Error('Method not implemented.');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.TempErrorStrategy = TempErrorStrategy;
|
|
45
|
+
class CircuitscriptParserErrorListener {
|
|
46
|
+
constructor(onErrorHandler = null) {
|
|
47
|
+
this.syntaxErrorCounter = 0;
|
|
48
|
+
this.onErrorHandler = null;
|
|
49
|
+
this.onErrorHandler = onErrorHandler;
|
|
50
|
+
}
|
|
51
|
+
syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e) {
|
|
52
|
+
if (this.onErrorHandler) {
|
|
53
|
+
this.onErrorHandler(line, charPositionInLine, msg, e);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
console.log("Syntax error at line", line, ':', charPositionInLine, ' - ', msg);
|
|
57
|
+
}
|
|
58
|
+
this.syntaxErrorCounter++;
|
|
59
|
+
}
|
|
60
|
+
reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs) {
|
|
61
|
+
}
|
|
62
|
+
reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs) {
|
|
63
|
+
}
|
|
64
|
+
reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs) {
|
|
65
|
+
}
|
|
66
|
+
hasSyntaxErrors() {
|
|
67
|
+
return (this.syntaxErrorCounter > 0);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.CircuitscriptParserErrorListener = CircuitscriptParserErrorListener;
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
const fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const helpers_js_1 = require("./helpers.js");
|
|
8
|
+
const mainDir = './__tests__/renderData/';
|
|
9
|
+
const cstFiles = [];
|
|
10
|
+
const files = fs_1.default.readdirSync(mainDir);
|
|
11
|
+
files.forEach(file => {
|
|
12
|
+
if (file.endsWith('.cst')) {
|
|
13
|
+
cstFiles.push(file);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
const useCurrentDir = './examples/';
|
|
17
|
+
cstFiles.forEach(file => {
|
|
18
|
+
const inputPath = mainDir + file;
|
|
19
|
+
const scriptData = fs_1.default.readFileSync(inputPath, { encoding: 'utf-8' });
|
|
20
|
+
const outputPath = inputPath + '.svg';
|
|
21
|
+
(0, helpers_js_1.renderScript)(scriptData, outputPath, { currentDirectory: useCurrentDir });
|
|
22
|
+
console.log('generated ', outputPath);
|
|
23
|
+
});
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateSVG2 = void 0;
|
|
4
|
+
const svg_js_1 = require("@svgdotjs/svg.js");
|
|
5
|
+
const layout_js_1 = require("./layout.js");
|
|
6
|
+
const sizing_js_1 = require("./sizing.js");
|
|
7
|
+
const globals_js_1 = require("./globals.js");
|
|
8
|
+
const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
|
|
9
|
+
const utils_js_1 = require("./utils.js");
|
|
10
|
+
function generateSVG2(graph) {
|
|
11
|
+
const window = (0, sizing_js_1.getCreateSVGWindow)()();
|
|
12
|
+
const document = window.document;
|
|
13
|
+
(0, svg_js_1.registerWindow)(window, document);
|
|
14
|
+
const canvas = (0, svg_js_1.SVG)(document.documentElement);
|
|
15
|
+
(0, sizing_js_1.applyFontsToSVG)(canvas);
|
|
16
|
+
generateSVGChild(canvas, graph.components, graph.wires, graph.junctions, graph.mergedWires, graph.frameObjects, graph.textObjects);
|
|
17
|
+
const { x, y, width, height } = canvas.bbox();
|
|
18
|
+
const margin = 5;
|
|
19
|
+
const widthAndMargin = width + margin * 2;
|
|
20
|
+
const heightAndMargin = height + margin * 2;
|
|
21
|
+
const scale = 1;
|
|
22
|
+
canvas.size(widthAndMargin * scale, heightAndMargin * scale);
|
|
23
|
+
canvas.viewbox(x - margin, y - margin, widthAndMargin, heightAndMargin);
|
|
24
|
+
return canvas.svg();
|
|
25
|
+
}
|
|
26
|
+
exports.generateSVG2 = generateSVG2;
|
|
27
|
+
function generateSVGChild(canvas, components, wires, junctions, mergedWires, frameObjects, textObjects) {
|
|
28
|
+
const displayWireId = false;
|
|
29
|
+
const bounds = (0, layout_js_1.getBounds)(components, wires, junctions, frameObjects);
|
|
30
|
+
drawGrid(canvas.group().translate(0, 0), {
|
|
31
|
+
x: bounds.xmin,
|
|
32
|
+
y: bounds.ymin,
|
|
33
|
+
x2: bounds.xmax,
|
|
34
|
+
y2: bounds.ymax
|
|
35
|
+
});
|
|
36
|
+
components.forEach(item => {
|
|
37
|
+
const { x, y, width, height } = item;
|
|
38
|
+
const symbolGroup = canvas.group();
|
|
39
|
+
symbolGroup.translate(x, y);
|
|
40
|
+
const { symbol = null } = item;
|
|
41
|
+
if (symbol !== null && symbol) {
|
|
42
|
+
const extra = {};
|
|
43
|
+
if (item.component.parameters.has('__is_net')) {
|
|
44
|
+
extra.net_name = item.component.parameters.get(globals_js_1.ParamKeys.net_name);
|
|
45
|
+
}
|
|
46
|
+
else if (item.component.parameters.has('value')) {
|
|
47
|
+
let tmpValue = item.component.parameters.get('value');
|
|
48
|
+
if (tmpValue instanceof ParamDefinition_js_1.NumericValue) {
|
|
49
|
+
tmpValue = tmpValue.value;
|
|
50
|
+
}
|
|
51
|
+
extra.value = tmpValue;
|
|
52
|
+
}
|
|
53
|
+
extra.instance_name = item.component.instanceName;
|
|
54
|
+
if (item.component.parameters.has('place')) {
|
|
55
|
+
extra.place = item.component.parameters.get('place');
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
extra.place = true;
|
|
59
|
+
}
|
|
60
|
+
symbol.draw(symbolGroup, extra);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
symbolGroup.rect(width, height)
|
|
64
|
+
.fill(globals_js_1.bodyColor)
|
|
65
|
+
.stroke({ width: 1, color: '#333' });
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
if (displayWireId) {
|
|
69
|
+
const wiresGroup = canvas.group().translate(0, 0);
|
|
70
|
+
wires.forEach(wire => {
|
|
71
|
+
wiresGroup.text(wire.id.toString())
|
|
72
|
+
.font({
|
|
73
|
+
family: 'Inter',
|
|
74
|
+
size: 10,
|
|
75
|
+
})
|
|
76
|
+
.translate(wire.x + 5, wire.y + 5);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
const mergedWireGroup = canvas.group();
|
|
80
|
+
mergedWires.forEach(tmpItem => {
|
|
81
|
+
const { segments, intersectPoints } = tmpItem;
|
|
82
|
+
segments.forEach(segment => {
|
|
83
|
+
const pt1 = segment[0];
|
|
84
|
+
const pt2 = segment[1];
|
|
85
|
+
mergedWireGroup.line([pt1, pt2])
|
|
86
|
+
.stroke({ width: 1, color: globals_js_1.wireColor, linecap: 'square' })
|
|
87
|
+
.fill('none');
|
|
88
|
+
});
|
|
89
|
+
intersectPoints.forEach(point => {
|
|
90
|
+
const [x, y, count] = point;
|
|
91
|
+
mergedWireGroup.circle(globals_js_1.junctionSize)
|
|
92
|
+
.translate(x - globals_js_1.junctionSize / 2, y - globals_js_1.junctionSize / 2)
|
|
93
|
+
.fill(globals_js_1.junctionColor)
|
|
94
|
+
.stroke('none');
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
const frameGroup = canvas.group();
|
|
98
|
+
const showElementFrames = false;
|
|
99
|
+
frameObjects.forEach(item => {
|
|
100
|
+
const { bounds, borderWidth } = item;
|
|
101
|
+
if (borderWidth > 0) {
|
|
102
|
+
const { width, height } = (0, utils_js_1.getBoundsSize)(bounds);
|
|
103
|
+
let strokeColor = '#111';
|
|
104
|
+
if (item.type === layout_js_1.RenderFrameType.Container) {
|
|
105
|
+
strokeColor = '#111';
|
|
106
|
+
}
|
|
107
|
+
else if (item.type === layout_js_1.RenderFrameType.Elements) {
|
|
108
|
+
strokeColor = '#aaa';
|
|
109
|
+
if (!showElementFrames) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const tmpRect = frameGroup.rect(width, height)
|
|
114
|
+
.fill('none')
|
|
115
|
+
.stroke({ width: borderWidth, color: strokeColor });
|
|
116
|
+
tmpRect.translate(item.x, item.y);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
textObjects.forEach(item => {
|
|
120
|
+
const { x, y, symbol } = item;
|
|
121
|
+
const innerGroup = canvas.group();
|
|
122
|
+
innerGroup.translate(x, y);
|
|
123
|
+
symbol.draw(innerGroup);
|
|
124
|
+
});
|
|
125
|
+
const drawOrigin = false;
|
|
126
|
+
drawOrigin && canvas.group().translate(0, 0)
|
|
127
|
+
.circle(5)
|
|
128
|
+
.translate(-5 / 2, -5 / 2)
|
|
129
|
+
.stroke('none').fill('red');
|
|
130
|
+
}
|
|
131
|
+
function drawGrid(group, canvasSize) {
|
|
132
|
+
const gridSize = 20;
|
|
133
|
+
const { x, y, x2, y2 } = canvasSize;
|
|
134
|
+
const gridStartX = (Math.floor(x / gridSize) - 1) * gridSize;
|
|
135
|
+
const gridStartY = (Math.floor(y / gridSize) - 1) * gridSize;
|
|
136
|
+
const gridEndX = (Math.ceil(x2 / gridSize) + 1) * gridSize;
|
|
137
|
+
const gridEndY = (Math.ceil(y2 / gridSize) + 1) * gridSize;
|
|
138
|
+
const numCols = Math.ceil((gridEndX - gridStartX) / gridSize);
|
|
139
|
+
const lines = [];
|
|
140
|
+
for (let i = 0; i <= numCols; i++) {
|
|
141
|
+
const startX = gridStartX + i * gridSize;
|
|
142
|
+
const startY = gridStartY - 0.5;
|
|
143
|
+
const endY = gridEndY;
|
|
144
|
+
lines.push(`M ${startX} ${startY} L ${startX} ${endY}`);
|
|
145
|
+
}
|
|
146
|
+
group.path(lines.join(" "))
|
|
147
|
+
.fill('none')
|
|
148
|
+
.attr({
|
|
149
|
+
'stroke-dasharray': '1,' + (gridSize - 1),
|
|
150
|
+
})
|
|
151
|
+
.stroke({
|
|
152
|
+
width: 1,
|
|
153
|
+
color: '#aaa'
|
|
154
|
+
});
|
|
155
|
+
}
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
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
|
+
const express_1 = __importDefault(require("express"));
|
|
7
|
+
const ws_1 = __importDefault(require("ws"));
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const app = (0, express_1.default)();
|
|
10
|
+
const wsServer = new ws_1.default.Server({ noServer: true });
|
|
8
11
|
wsServer.on('connection', socket => {
|
|
9
|
-
|
|
10
12
|
const fileName = 'output.svg';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
fs.readFile(fileName, 'utf8', (err, data) => {
|
|
13
|
+
function emitFile() {
|
|
14
|
+
fs_1.default.readFile(fileName, 'utf8', (err, data) => {
|
|
14
15
|
if (err) {
|
|
15
16
|
throw err;
|
|
16
17
|
}
|
|
@@ -18,33 +19,26 @@ wsServer.on('connection', socket => {
|
|
|
18
19
|
socket.send(data);
|
|
19
20
|
});
|
|
20
21
|
}
|
|
21
|
-
|
|
22
22
|
let emitTimeout = null;
|
|
23
|
-
|
|
23
|
+
fs_1.default.watch(fileName, (event, targetFile) => {
|
|
24
24
|
if (event === 'change') {
|
|
25
25
|
console.log((new Date()).toISOString(), 'file changed!');
|
|
26
|
-
|
|
27
|
-
// emit the change
|
|
28
26
|
clearInterval(emitTimeout);
|
|
29
27
|
emitTimeout = setTimeout(() => {
|
|
30
28
|
emitFile();
|
|
31
29
|
}, 200);
|
|
32
30
|
}
|
|
33
31
|
});
|
|
34
|
-
|
|
35
32
|
socket.on('message', message => {
|
|
36
33
|
console.log(message.toString());
|
|
37
34
|
emitFile();
|
|
38
35
|
});
|
|
39
36
|
});
|
|
40
|
-
|
|
41
|
-
app.use(express.static('./'))
|
|
42
|
-
|
|
37
|
+
app.use(express_1.default.static('./'));
|
|
43
38
|
const server = app.listen(3000);
|
|
44
39
|
server.on('upgrade', (request, socket, head) => {
|
|
45
40
|
wsServer.handleUpgrade(request, socket, head, socket => {
|
|
46
41
|
wsServer.emit('connection', socket, request);
|
|
47
42
|
});
|
|
48
43
|
});
|
|
49
|
-
|
|
50
|
-
console.log('server started at port 3000');
|
|
44
|
+
console.log('server started at port 3000');
|
|
@@ -0,0 +1,105 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.measureTextSize2 = exports.measureTextSize = exports.applyFontsToSVG = exports.getCreateSVGWindow = exports.prepareSVGEnvironment = void 0;
|
|
27
|
+
const svg_js_1 = require("@svgdotjs/svg.js");
|
|
28
|
+
const geometry_js_1 = require("./geometry.js");
|
|
29
|
+
const globals_js_1 = require("./globals.js");
|
|
30
|
+
const helpers_js_1 = require("./helpers.js");
|
|
31
|
+
let MainCanvas = null;
|
|
32
|
+
const supportedFonts = {};
|
|
33
|
+
let globalCreateSVGWindow;
|
|
34
|
+
async function prepareSVGEnvironment(fontsPath) {
|
|
35
|
+
const moduleType = (0, helpers_js_1.detectJSModuleType)();
|
|
36
|
+
if (moduleType === helpers_js_1.JSModuleType.CommonJs) {
|
|
37
|
+
const { config, createSVGWindow } = await Promise.resolve().then(() => __importStar(require('svgdom')));
|
|
38
|
+
globalCreateSVGWindow = createSVGWindow;
|
|
39
|
+
if (fontsPath !== null) {
|
|
40
|
+
await config.setFontDir(fontsPath)
|
|
41
|
+
.setFontFamilyMappings(supportedFonts)
|
|
42
|
+
.preloadFonts();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.prepareSVGEnvironment = prepareSVGEnvironment;
|
|
47
|
+
function getCreateSVGWindow() {
|
|
48
|
+
if (globalCreateSVGWindow === undefined) {
|
|
49
|
+
throw "SVG environment is not set up yet";
|
|
50
|
+
}
|
|
51
|
+
return globalCreateSVGWindow;
|
|
52
|
+
}
|
|
53
|
+
exports.getCreateSVGWindow = getCreateSVGWindow;
|
|
54
|
+
function applyFontsToSVG(canvas) {
|
|
55
|
+
}
|
|
56
|
+
exports.applyFontsToSVG = applyFontsToSVG;
|
|
57
|
+
async function measureTextSize(text, fontFamily, fontSize) {
|
|
58
|
+
return measureTextSize2(text, fontFamily, fontSize);
|
|
59
|
+
}
|
|
60
|
+
exports.measureTextSize = measureTextSize;
|
|
61
|
+
const measureTextSizeCache = {};
|
|
62
|
+
const measureTextSizeCacheHits = {};
|
|
63
|
+
function measureTextSize2(text, fontFamily, fontSize, fontWeight = 'regular', anchor = geometry_js_1.HorizontalAlign.Left, vanchor = geometry_js_1.VerticalAlign.Bottom) {
|
|
64
|
+
if (MainCanvas === null) {
|
|
65
|
+
const window = getCreateSVGWindow()();
|
|
66
|
+
const { document } = window;
|
|
67
|
+
(0, svg_js_1.registerWindow)(window, document);
|
|
68
|
+
MainCanvas = (0, svg_js_1.SVG)(document.documentElement);
|
|
69
|
+
}
|
|
70
|
+
const key = `${text}-${fontFamily}-${fontSize}-${fontWeight}-${anchor}-${vanchor}`;
|
|
71
|
+
if (measureTextSizeCache[key] === undefined) {
|
|
72
|
+
let dominantBaseline = 'hanging';
|
|
73
|
+
switch (vanchor) {
|
|
74
|
+
case geometry_js_1.VerticalAlign.Top:
|
|
75
|
+
dominantBaseline = 'hanging';
|
|
76
|
+
break;
|
|
77
|
+
case geometry_js_1.VerticalAlign.Middle:
|
|
78
|
+
dominantBaseline = 'middle';
|
|
79
|
+
break;
|
|
80
|
+
case geometry_js_1.VerticalAlign.Bottom:
|
|
81
|
+
dominantBaseline = 'text-top';
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
fontFamily = globals_js_1.defaultFont;
|
|
85
|
+
const tmpTextElement = MainCanvas.text(text).font({
|
|
86
|
+
family: fontFamily,
|
|
87
|
+
size: fontSize,
|
|
88
|
+
anchor: anchor,
|
|
89
|
+
'dominant-baseline': dominantBaseline,
|
|
90
|
+
weight: fontWeight,
|
|
91
|
+
}).fill('#333');
|
|
92
|
+
const textbox = tmpTextElement.bbox();
|
|
93
|
+
const { width, height } = textbox;
|
|
94
|
+
tmpTextElement.remove();
|
|
95
|
+
measureTextSizeCache[key] = {
|
|
96
|
+
width: Math.round(width * 100) / 100,
|
|
97
|
+
height: Math.round(height * 100) / 100,
|
|
98
|
+
box: textbox,
|
|
99
|
+
};
|
|
100
|
+
measureTextSizeCacheHits[key] = 0;
|
|
101
|
+
}
|
|
102
|
+
measureTextSizeCacheHits[key]++;
|
|
103
|
+
return measureTextSizeCache[key];
|
|
104
|
+
}
|
|
105
|
+
exports.measureTextSize2 = measureTextSize2;
|
|
@@ -1,64 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getBoundsSize = exports.toNearestGrid = exports.resizeToNearestGrid = exports.printBounds = exports.resizeBounds = exports.SimpleStopwatch = void 0;
|
|
4
|
+
class SimpleStopwatch {
|
|
5
5
|
constructor() {
|
|
6
6
|
this.startTime = new Date();
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
lap(): number {
|
|
8
|
+
lap() {
|
|
10
9
|
return (new Date()).getTime() - this.startTime.getTime();
|
|
11
10
|
}
|
|
12
11
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
xmin: number, ymin: number,
|
|
16
|
-
xmax: number, ymax: number,
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function resizeBounds(bounds: BoundBox, value: number): BoundBox {
|
|
12
|
+
exports.SimpleStopwatch = SimpleStopwatch;
|
|
13
|
+
function resizeBounds(bounds, value) {
|
|
20
14
|
return {
|
|
21
15
|
xmin: bounds.xmin - value,
|
|
22
16
|
xmax: bounds.xmax + value,
|
|
23
|
-
|
|
24
17
|
ymin: bounds.ymin - value,
|
|
25
18
|
ymax: bounds.ymax + value
|
|
26
|
-
}
|
|
19
|
+
};
|
|
27
20
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (bounds !== null){
|
|
21
|
+
exports.resizeBounds = resizeBounds;
|
|
22
|
+
function printBounds(bounds) {
|
|
23
|
+
if (bounds !== null) {
|
|
31
24
|
return `[${bounds.xmin}, ${bounds.ymin} to ${bounds.xmax}, ${bounds.ymax}]`;
|
|
32
|
-
}
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
33
27
|
return 'null';
|
|
34
28
|
}
|
|
35
29
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
// to the grid, otherwise the expanded grid might "just nice" fit.
|
|
41
|
-
const addXMin = (bounds.xmin % gridSize) === 0 ? -1: 0;
|
|
42
|
-
const addYMin = (bounds.ymin % gridSize) === 0 ? -1: 0;
|
|
30
|
+
exports.printBounds = printBounds;
|
|
31
|
+
function resizeToNearestGrid(bounds, gridSize = 20) {
|
|
32
|
+
const addXMin = (bounds.xmin % gridSize) === 0 ? -1 : 0;
|
|
33
|
+
const addYMin = (bounds.ymin % gridSize) === 0 ? -1 : 0;
|
|
43
34
|
const addXMax = (bounds.xmax % gridSize) === 0 ? 1 : 0;
|
|
44
35
|
const addYMax = (bounds.ymax % gridSize) === 0 ? 1 : 0;
|
|
45
|
-
|
|
46
36
|
return {
|
|
47
37
|
xmin: Math.floor((bounds.xmin + addXMin) / gridSize) * gridSize,
|
|
48
38
|
ymin: Math.floor((bounds.ymin + addYMin) / gridSize) * gridSize,
|
|
49
|
-
|
|
50
39
|
xmax: Math.ceil((bounds.xmax + addXMax) / gridSize) * gridSize,
|
|
51
40
|
ymax: Math.ceil((bounds.ymax + addYMax) / gridSize) * gridSize,
|
|
52
|
-
}
|
|
41
|
+
};
|
|
53
42
|
}
|
|
54
|
-
|
|
55
|
-
|
|
43
|
+
exports.resizeToNearestGrid = resizeToNearestGrid;
|
|
44
|
+
function toNearestGrid(value, gridSize) {
|
|
56
45
|
return Math.floor(value / gridSize) * gridSize;
|
|
57
46
|
}
|
|
58
|
-
|
|
59
|
-
|
|
47
|
+
exports.toNearestGrid = toNearestGrid;
|
|
48
|
+
function getBoundsSize(bounds) {
|
|
60
49
|
return {
|
|
61
50
|
width: bounds.xmax - bounds.xmin,
|
|
62
51
|
height: bounds.ymax - bounds.ymin,
|
|
63
|
-
}
|
|
64
|
-
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
exports.getBoundsSize = getBoundsSize;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.validate = void 0;
|
|
8
|
+
const commander_1 = require("commander");
|
|
9
|
+
const figlet_1 = __importDefault(require("figlet"));
|
|
10
|
+
const path_1 = __importDefault(require("path"));
|
|
11
|
+
const fs_1 = require("fs");
|
|
12
|
+
const sizing_js_1 = require("./sizing.js");
|
|
13
|
+
const helpers_js_1 = require("./helpers.js");
|
|
14
|
+
async function validate() {
|
|
15
|
+
const { filePath } = (0, helpers_js_1.getCurrentPath)();
|
|
16
|
+
const toolSrcPath = filePath;
|
|
17
|
+
const toolDirectory = path_1.default.dirname(toolSrcPath) + '/../../';
|
|
18
|
+
const fontsPath = toolDirectory + '/fonts';
|
|
19
|
+
const defaultLibsPath = toolDirectory + '/libs';
|
|
20
|
+
const packageJson = JSON.parse((0, fs_1.readFileSync)(toolDirectory + 'package.json').toString());
|
|
21
|
+
;
|
|
22
|
+
const { version } = packageJson;
|
|
23
|
+
commander_1.program
|
|
24
|
+
.description('generate graphical output from circuitscript files')
|
|
25
|
+
.version(version)
|
|
26
|
+
.option('-i, --input text <input text>', 'Input text directly')
|
|
27
|
+
.option('-f, --input-file <path>', 'Input file')
|
|
28
|
+
.option('-o, --output <path>', 'Output path')
|
|
29
|
+
.option('-c, --current-directory <path>', 'Set current directory')
|
|
30
|
+
.option('-k, --kicad-netlist <filename>', 'Create KiCad netlist')
|
|
31
|
+
.option('-w, --watch', 'Watch for file changes')
|
|
32
|
+
.option('-n, --dump-nets', 'Dump out net information')
|
|
33
|
+
.option('-d, --dump-data', 'Dump data during parsing')
|
|
34
|
+
.option('-s, --stats', 'Show stats during generation');
|
|
35
|
+
commander_1.program.addHelpText('before', figlet_1.default.textSync('circuitscript', {
|
|
36
|
+
font: 'Small Slant'
|
|
37
|
+
}));
|
|
38
|
+
if (process.argv.length < 3) {
|
|
39
|
+
commander_1.program.help();
|
|
40
|
+
}
|
|
41
|
+
commander_1.program.parse();
|
|
42
|
+
const options = commander_1.program.opts();
|
|
43
|
+
const watchFileChanges = options.watch;
|
|
44
|
+
const outputPath = options.output ?? null;
|
|
45
|
+
const dumpNets = options.dumpNets;
|
|
46
|
+
const dumpData = options.dumpData;
|
|
47
|
+
const kicadNetlist = options.kicadNetlist;
|
|
48
|
+
let currentDirectory = options.currentDirectory ?? null;
|
|
49
|
+
if (watchFileChanges) {
|
|
50
|
+
console.log('watching for file changes...');
|
|
51
|
+
}
|
|
52
|
+
await (0, sizing_js_1.prepareSVGEnvironment)(fontsPath);
|
|
53
|
+
let inputFilePath = null;
|
|
54
|
+
let scriptData;
|
|
55
|
+
if (options.input) {
|
|
56
|
+
scriptData = options.input;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
inputFilePath = options.inputFile;
|
|
60
|
+
const tmpScriptData = (0, helpers_js_1.getScriptText)(inputFilePath);
|
|
61
|
+
if (tmpScriptData === null) {
|
|
62
|
+
throw "File does not exists";
|
|
63
|
+
}
|
|
64
|
+
scriptData = tmpScriptData;
|
|
65
|
+
if (currentDirectory === null) {
|
|
66
|
+
currentDirectory = path_1.default.dirname(inputFilePath);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const scriptOptions = {
|
|
70
|
+
currentDirectory,
|
|
71
|
+
defaultLibsPath,
|
|
72
|
+
dumpNets,
|
|
73
|
+
dumpData,
|
|
74
|
+
kicadNetlistPath: kicadNetlist,
|
|
75
|
+
showStats: options.stats,
|
|
76
|
+
};
|
|
77
|
+
const visitor = (0, helpers_js_1.validateScript)(scriptData, scriptOptions);
|
|
78
|
+
const semanticTokensVisitor = (0, helpers_js_1.getSemanticTokens)(scriptData, scriptOptions);
|
|
79
|
+
}
|
|
80
|
+
exports.validate = validate;
|
|
81
|
+
validate();
|