schematic-symbols 0.0.100 → 0.0.102

Sign up to get free protection for your applications and to get access to all the features.
package/drawing/arc.ts CHANGED
@@ -12,7 +12,7 @@ export function approximateArc(
12
12
  ): Point[] {
13
13
  // This is a simplified approximation. For a more accurate representation,
14
14
  // you might want to use a dedicated SVG path library.
15
- const steps = 30
15
+ const steps = 40
16
16
  const points: Point[] = []
17
17
  for (let i = 1; i < steps; i++) {
18
18
  const t = i / steps
@@ -0,0 +1,28 @@
1
+ import type { Point } from "./types"
2
+
3
+ export function approximateBezier(
4
+ p0: Point,
5
+ p1: Point,
6
+ p2: Point,
7
+ p3: Point,
8
+ ): Point[] {
9
+ const points: Point[] = []
10
+ const steps = 30
11
+ for (let t = 0; t <= 1; t += 1 / steps) {
12
+ const x =
13
+ Math.pow(1 - t, 3) * p0.x +
14
+ 3 * Math.pow(1 - t, 2) * t * p1.x +
15
+ 3 * (1 - t) * Math.pow(t, 2) * p2.x +
16
+ Math.pow(t, 3) * p3.x
17
+
18
+ const y =
19
+ Math.pow(1 - t, 3) * p0.y +
20
+ 3 * Math.pow(1 - t, 2) * t * p1.y +
21
+ 3 * (1 - t) * Math.pow(t, 2) * p2.y +
22
+ Math.pow(t, 3) * p3.y
23
+
24
+ points.push({ x, y })
25
+ }
26
+
27
+ return points
28
+ }
@@ -0,0 +1,91 @@
1
+ import { NinePointAnchor, SchSymbol } from "drawing/types"
2
+ import { rotateRightFacingSymbol } from "drawing/rotateSymbol"
3
+
4
+ interface ModifySymbolBuilder {
5
+ changeTextAnchor(
6
+ text: "{REF}" | "{VAL}" | string,
7
+ newAnchor: NinePointAnchor,
8
+ ): ModifySymbolBuilder
9
+ rotateRightFacingSymbol(
10
+ newOrientation: "up" | "down" | "left" | "right",
11
+ ): ModifySymbolBuilder
12
+ labelPort(currentLabel: string, newLabels: string[]): ModifySymbolBuilder
13
+ build(): SchSymbol
14
+ }
15
+
16
+ class SymbolModifier implements ModifySymbolBuilder {
17
+ private symbol: SchSymbol
18
+
19
+ constructor(symbol: SchSymbol) {
20
+ this.symbol = JSON.parse(JSON.stringify(symbol))
21
+ }
22
+
23
+ changeTextAnchor(
24
+ text: "{REF}" | "{VAL}" | string,
25
+ newAnchor: NinePointAnchor,
26
+ ): ModifySymbolBuilder {
27
+ this.symbol = {
28
+ ...this.symbol,
29
+ primitives: this.symbol.primitives.map((primitive) => {
30
+ if (primitive.type === "text" && primitive.text === text) {
31
+ return {
32
+ ...primitive,
33
+ anchor: newAnchor,
34
+ }
35
+ }
36
+ return primitive
37
+ }),
38
+ }
39
+ return this
40
+ }
41
+
42
+ labelPort(currentLabel: string, newLabels: string[]): ModifySymbolBuilder {
43
+ this.symbol = {
44
+ ...this.symbol,
45
+ ports: this.symbol.ports.map((port) => {
46
+ return port.labels.includes(currentLabel)
47
+ ? { ...port, labels: newLabels }
48
+ : port
49
+ }),
50
+ }
51
+ return this
52
+ }
53
+
54
+ rotateRightFacingSymbol(
55
+ newOrientation: "up" | "down" | "left" | "right",
56
+ ): ModifySymbolBuilder {
57
+ this.symbol = rotateRightFacingSymbol(this.symbol, {
58
+ newOrientation,
59
+ })
60
+ return this
61
+ }
62
+
63
+ build(): SchSymbol {
64
+ return this.symbol
65
+ }
66
+ }
67
+
68
+ export const modifySymbol = (symbol: any): ModifySymbolBuilder => {
69
+ return new SymbolModifier({
70
+ ...symbol,
71
+ primitives: symbol.primitives ?? [
72
+ ...Object.values(symbol.paths ?? {}),
73
+ ...Object.values(symbol.texts ?? {}),
74
+ ...Object.values(symbol.circles ?? {}),
75
+ ...Object.values(symbol.rects ?? {}),
76
+ ],
77
+ ports:
78
+ symbol.ports ??
79
+ Object.entries(symbol.refblocks).flatMap(([key, refblock]) => {
80
+ return [{ ...(refblock as object), labels: [key] }]
81
+ }),
82
+ center: symbol.center ?? {
83
+ x: symbol.bounds.centerX,
84
+ y: symbol.bounds.centerY,
85
+ },
86
+ size: symbol.size ?? {
87
+ width: symbol.bounds.width,
88
+ height: symbol.bounds.height,
89
+ },
90
+ })
91
+ }
@@ -1,5 +1,6 @@
1
1
  import type { Point } from "./types"
2
2
  import { approximateArc } from "./arc"
3
+ import { approximateBezier } from "./cubicBezierCurveArc"
3
4
 
4
5
  export function svgPathToPoints(pathString: string): Point[] {
5
6
  const points: Point[] = []
@@ -35,12 +36,21 @@ export function svgPathToPoints(pathString: string): Point[] {
35
36
  case "V":
36
37
  addPoint(currentX, args[0])
37
38
  break
38
- case "C":
39
- // For C command, we add both control points and the end point
40
- points.push({ x: args[0], y: args[1] }) // First control point
41
- points.push({ x: args[2], y: args[3] }) // Second control point
42
- addPoint(args[4], args[5]) // End point
39
+ case "C": {
40
+ const [x1, y1, x2, y2, x, y] = args
41
+
42
+ // Approximate the cubic Bézier curve
43
+ const bezierPoints = approximateBezier(
44
+ { x: currentX, y: currentY }, // Start point
45
+ { x: x1, y: y1 }, // Control point 1
46
+ { x: x2, y: y2 }, // Control point 2
47
+ { x, y }, // End point
48
+ )
49
+
50
+ points.push(...bezierPoints)
51
+ addPoint(x, y) // Update the current position
43
52
  break
53
+ }
44
54
  case "Q":
45
55
  // For Q command, we add both the control point and the end point
46
56
  points.push({ x: args[0], y: args[1] }) // Control point
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "schematic-symbols",
3
3
  "main": "./dist/index.js",
4
- "version": "0.0.100",
4
+ "version": "0.0.102",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist",
@@ -0,0 +1,10 @@
1
+ import { modifySymbol } from "../drawing/modify-symbol/modify-symbol"
2
+ import svgJson from "./capacitor_polarized_right"
3
+
4
+ export default modifySymbol(svgJson)
5
+ .changeTextAnchor("{VAL}", "middle_top")
6
+ .rotateRightFacingSymbol("down")
7
+ .labelPort("left1", ["1"])
8
+ .labelPort("right1", ["2"])
9
+ .changeTextAnchor("{REF}", "middle_left")
10
+ .build()
@@ -0,0 +1,10 @@
1
+ import { modifySymbol } from "../drawing/modify-symbol/modify-symbol"
2
+ import svgJson from "./capacitor_polarized_right"
3
+
4
+ export default modifySymbol(svgJson)
5
+ .changeTextAnchor("{VAL}", "middle_top")
6
+ .rotateRightFacingSymbol("left")
7
+ .labelPort("left1", ["1"])
8
+ .labelPort("right1", ["2"])
9
+ .changeTextAnchor("{REF}", "middle_top")
10
+ .build()
@@ -0,0 +1,10 @@
1
+ import { modifySymbol } from "../drawing/modify-symbol/modify-symbol"
2
+ import svgJson from "assets/generated/capacitor_polarized.json"
3
+
4
+ export default modifySymbol(svgJson)
5
+ .changeTextAnchor("{VAL}", "middle_top")
6
+ .rotateRightFacingSymbol("right")
7
+ .labelPort("left1", ["1"])
8
+ .labelPort("right1", ["2"])
9
+ .changeTextAnchor("{REF}", "middle_bottom")
10
+ .build()
@@ -0,0 +1,10 @@
1
+ import { modifySymbol } from "../drawing/modify-symbol/modify-symbol"
2
+ import svgJson from "./capacitor_polarized_right"
3
+
4
+ export default modifySymbol(svgJson)
5
+ .changeTextAnchor("{VAL}", "middle_top")
6
+ .rotateRightFacingSymbol("up")
7
+ .labelPort("left1", ["1"])
8
+ .labelPort("right1", ["2"])
9
+ .changeTextAnchor("{REF}", "middle_right")
10
+ .build()
@@ -1,6 +1,6 @@
1
1
  import { defineSymbol } from "drawing/defineSymbol"
2
2
  import svgJson from "assets/generated/diode.json"
3
- import { modifySymbol } from "scripts/lib/modify-symbol/modify-symbol"
3
+ import { modifySymbol } from "drawing/modify-symbol/modify-symbol"
4
4
 
5
5
  export default modifySymbol(svgJson)
6
6
  .labelPort("left1", ["1", "pos"])
@@ -2,6 +2,6 @@ import { defineSymbol } from "drawing/defineSymbol"
2
2
  import svgJson from "assets/generated/ground.json"
3
3
  import { Primitive } from "drawing/types"
4
4
  import groundHorz from "./ground_horz"
5
- import { modifySymbol } from "scripts/lib/modify-symbol/modify-symbol"
5
+ import { modifySymbol } from "drawing/modify-symbol/modify-symbol"
6
6
 
7
7
  export default modifySymbol(groundHorz).rotateRightFacingSymbol("down").build()
@@ -1,6 +1,6 @@
1
1
  import { defineSymbol } from "drawing/defineSymbol"
2
2
  import svgJson from "assets/generated/schottky_diode.json"
3
- import { modifySymbol } from "scripts/lib/modify-symbol/modify-symbol"
3
+ import { modifySymbol } from "drawing/modify-symbol/modify-symbol"
4
4
 
5
5
  export default modifySymbol(svgJson)
6
6
  .labelPort("left1", ["1", "pos"])