schematic-symbols 0.0.121 → 0.0.122

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. package/package.json +2 -5
  2. package/drawing/arc.ts +0 -31
  3. package/drawing/arrow.ts +0 -41
  4. package/drawing/box.ts +0 -5
  5. package/drawing/circle.ts +0 -7
  6. package/drawing/cubicBezierCurveArc.ts +0 -28
  7. package/drawing/defineSymbol.ts +0 -16
  8. package/drawing/getSvg.ts +0 -199
  9. package/drawing/index.ts +0 -10
  10. package/drawing/mapColor.ts +0 -10
  11. package/drawing/modify-symbol/modify-symbol.ts +0 -97
  12. package/drawing/ninePointAnchorToSvgAnchor.ts +0 -11
  13. package/drawing/path.ts +0 -5
  14. package/drawing/pathToSvgD.ts +0 -22
  15. package/drawing/resizeSymbol.ts +0 -67
  16. package/drawing/rotateSymbol.ts +0 -324
  17. package/drawing/svgPathToPoints.ts +0 -109
  18. package/drawing/text.ts +0 -8
  19. package/drawing/typeguards.ts +0 -155
  20. package/drawing/types.ts +0 -93
  21. package/drawing/utils/getBoundsOfPrimitives.ts +0 -51
  22. package/drawing/utils/makeYUpPositive.ts +0 -46
  23. package/index.ts +0 -24
  24. package/symbols/SPDT_switch_horz.ts +0 -22
  25. package/symbols/SPDT_switch_vert.ts +0 -20
  26. package/symbols/SPST_switch_horz.ts +0 -12
  27. package/symbols/SPST_switch_vert.ts +0 -20
  28. package/symbols/ac_voltmeter_horz.ts +0 -31
  29. package/symbols/ac_voltmeter_vert.ts +0 -8
  30. package/symbols/avalanche_diode_horz.ts +0 -19
  31. package/symbols/avalanche_diode_vert.ts +0 -10
  32. package/symbols/battery_horz.ts +0 -20
  33. package/symbols/battery_vert.ts +0 -6
  34. package/symbols/boxresistor_down.ts +0 -33
  35. package/symbols/boxresistor_left.ts +0 -33
  36. package/symbols/boxresistor_right.ts +0 -33
  37. package/symbols/boxresistor_up.ts +0 -33
  38. package/symbols/capacitor_down.ts +0 -34
  39. package/symbols/capacitor_left.ts +0 -11
  40. package/symbols/capacitor_polarized_down.ts +0 -34
  41. package/symbols/capacitor_polarized_left.ts +0 -10
  42. package/symbols/capacitor_polarized_right.ts +0 -10
  43. package/symbols/capacitor_polarized_up.ts +0 -34
  44. package/symbols/capacitor_right.ts +0 -10
  45. package/symbols/capacitor_up.ts +0 -34
  46. package/symbols/constant_current_diode_horz.ts +0 -9
  47. package/symbols/constant_current_diode_vert.ts +0 -11
  48. package/symbols/crystal_4pin_horz.ts +0 -11
  49. package/symbols/crystal_4pin_vert.ts +0 -10
  50. package/symbols/crystal_horz.ts +0 -9
  51. package/symbols/crystal_vert.ts +0 -10
  52. package/symbols/darlington_pair_transistor_horz.ts +0 -10
  53. package/symbols/darlington_pair_transistor_vert.ts +0 -12
  54. package/symbols/dc_ammeter_horz.ts +0 -32
  55. package/symbols/dc_ammeter_vert.ts +0 -51
  56. package/symbols/dc_voltmeter_horz.ts +0 -31
  57. package/symbols/dc_voltmeter_vert.ts +0 -19
  58. package/symbols/diac_horz.ts +0 -9
  59. package/symbols/diac_vert.ts +0 -16
  60. package/symbols/diode_down.ts +0 -4
  61. package/symbols/diode_left.ts +0 -4
  62. package/symbols/diode_right.ts +0 -9
  63. package/symbols/diode_up.ts +0 -4
  64. package/symbols/dpdt_normally_closed_switch_down.ts +0 -22
  65. package/symbols/dpdt_normally_closed_switch_left.ts +0 -23
  66. package/symbols/dpdt_normally_closed_switch_right.ts +0 -13
  67. package/symbols/dpdt_normally_closed_switch_up.ts +0 -22
  68. package/symbols/dpdt_switch_down.ts +0 -22
  69. package/symbols/dpdt_switch_left.ts +0 -23
  70. package/symbols/dpdt_switch_right.ts +0 -13
  71. package/symbols/dpdt_switch_up.ts +0 -22
  72. package/symbols/dpst_normally_closed_switch_down.ts +0 -22
  73. package/symbols/dpst_normally_closed_switch_left.ts +0 -23
  74. package/symbols/dpst_normally_closed_switch_right.ts +0 -11
  75. package/symbols/dpst_normally_closed_switch_up.ts +0 -22
  76. package/symbols/dpst_switch_down.ts +0 -21
  77. package/symbols/dpst_switch_left.ts +0 -23
  78. package/symbols/dpst_switch_right.ts +0 -11
  79. package/symbols/dpst_switch_up.ts +0 -22
  80. package/symbols/filled_diode_horz.ts +0 -9
  81. package/symbols/filled_diode_vert.ts +0 -4
  82. package/symbols/frequency_meter_horz.ts +0 -32
  83. package/symbols/frequency_meter_vert.ts +0 -19
  84. package/symbols/fuse_horz.ts +0 -24
  85. package/symbols/fuse_vert.ts +0 -4
  86. package/symbols/ground_horz.ts +0 -37
  87. package/symbols/ground_vert.ts +0 -4
  88. package/symbols/gunn_diode_horz.ts +0 -11
  89. package/symbols/gunn_diode_vert.ts +0 -11
  90. package/symbols/icled_down.ts +0 -4
  91. package/symbols/icled_left.ts +0 -4
  92. package/symbols/icled_right.ts +0 -11
  93. package/symbols/icled_up.ts +0 -27
  94. package/symbols/igbt_transistor_horz.ts +0 -10
  95. package/symbols/igbt_transistor_vert.ts +0 -21
  96. package/symbols/illuminated_push_button_normally_open_horz.ts +0 -9
  97. package/symbols/illuminated_push_button_normally_open_vert.ts +0 -20
  98. package/symbols/inductor_down.ts +0 -4
  99. package/symbols/inductor_left.ts +0 -4
  100. package/symbols/inductor_right.ts +0 -20
  101. package/symbols/inductor_up.ts +0 -4
  102. package/symbols/laser_diode_horz.ts +0 -10
  103. package/symbols/laser_diode_vert.ts +0 -13
  104. package/symbols/led_down.ts +0 -4
  105. package/symbols/led_left.ts +0 -4
  106. package/symbols/led_right.ts +0 -20
  107. package/symbols/led_up.ts +0 -27
  108. package/symbols/light_dependent_resistor_horz.ts +0 -20
  109. package/symbols/light_dependent_resistor_vert.ts +0 -20
  110. package/symbols/mosfet_depletion_normally_on_horz.ts +0 -19
  111. package/symbols/mosfet_depletion_normally_on_vert.ts +0 -21
  112. package/symbols/mushroom_head_normally_open_momentary_horz.ts +0 -20
  113. package/symbols/mushroom_head_normally_open_momentary_vert.ts +0 -23
  114. package/symbols/n_channel_d_mosfet_transistor_horz.ts +0 -20
  115. package/symbols/n_channel_d_mosfet_transistor_vert.ts +0 -20
  116. package/symbols/n_channel_e_mosfet_transistor_horz.ts +0 -21
  117. package/symbols/n_channel_e_mosfet_transistor_vert.ts +0 -20
  118. package/symbols/njfet_transistor_horz.ts +0 -20
  119. package/symbols/njfet_transistor_vert.ts +0 -20
  120. package/symbols/npn_bipolar_transistor_horz.ts +0 -11
  121. package/symbols/npn_bipolar_transistor_vert.ts +0 -21
  122. package/symbols/opamp_no_power_down.ts +0 -4
  123. package/symbols/opamp_no_power_left.ts +0 -4
  124. package/symbols/opamp_no_power_right.ts +0 -10
  125. package/symbols/opamp_no_power_up.ts +0 -4
  126. package/symbols/opamp_with_power_down.ts +0 -4
  127. package/symbols/opamp_with_power_left.ts +0 -4
  128. package/symbols/opamp_with_power_right.ts +0 -12
  129. package/symbols/opamp_with_power_up.ts +0 -4
  130. package/symbols/p_channel_d_mosfet_transistor_horz.ts +0 -20
  131. package/symbols/p_channel_d_mosfet_transistor_vert.ts +0 -20
  132. package/symbols/p_channel_e_mosfet_transistor_horz.ts +0 -20
  133. package/symbols/p_channel_e_mosfet_transistor_vert.ts +0 -20
  134. package/symbols/photodiode_horz.ts +0 -9
  135. package/symbols/photodiode_vert.ts +0 -13
  136. package/symbols/pjfet_transistor_horz.ts +0 -20
  137. package/symbols/pjfet_transistor_vert.ts +0 -20
  138. package/symbols/pnp_bipolar_transistor_horz.ts +0 -10
  139. package/symbols/pnp_bipolar_transistor_vert.ts +0 -20
  140. package/symbols/potentiometer2_horz.ts +0 -18
  141. package/symbols/potentiometer2_vert.ts +0 -14
  142. package/symbols/potentiometer3_down.ts +0 -4
  143. package/symbols/potentiometer3_left.ts +0 -4
  144. package/symbols/potentiometer3_right.ts +0 -10
  145. package/symbols/potentiometer3_up.ts +0 -4
  146. package/symbols/potentiometer_horz.ts +0 -18
  147. package/symbols/potentiometer_vert.ts +0 -6
  148. package/symbols/power_factor_meter_horz.ts +0 -42
  149. package/symbols/power_factor_meter_vert.ts +0 -19
  150. package/symbols/push_button_normally_closed_momentary_horz.ts +0 -20
  151. package/symbols/push_button_normally_closed_momentary_vert.ts +0 -4
  152. package/symbols/push_button_normally_open_momentary_horz.ts +0 -20
  153. package/symbols/push_button_normally_open_momentary_vert.ts +0 -4
  154. package/symbols/rectifier_diode_horz.ts +0 -21
  155. package/symbols/rectifier_diode_vert.ts +0 -17
  156. package/symbols/resonator_horz.ts +0 -34
  157. package/symbols/resonator_vert.ts +0 -4
  158. package/symbols/schottky_diode_horz.ts +0 -10
  159. package/symbols/schottky_diode_vert.ts +0 -18
  160. package/symbols/silicon_controlled_rectifier_horz.ts +0 -13
  161. package/symbols/silicon_controlled_rectifier_vert.ts +0 -15
  162. package/symbols/spdt_normally_closed_switch_down.ts +0 -22
  163. package/symbols/spdt_normally_closed_switch_left.ts +0 -22
  164. package/symbols/spdt_normally_closed_switch_right.ts +0 -10
  165. package/symbols/spdt_normally_closed_switch_up.ts +0 -22
  166. package/symbols/spst_normally_closed_switch_down.ts +0 -22
  167. package/symbols/spst_normally_closed_switch_left.ts +0 -22
  168. package/symbols/spst_normally_closed_switch_right.ts +0 -12
  169. package/symbols/spst_normally_closed_switch_up.ts +0 -22
  170. package/symbols/step_recovery_diode_horz.ts +0 -11
  171. package/symbols/step_recovery_diode_vert.ts +0 -4
  172. package/symbols/tachometer_horz.ts +0 -32
  173. package/symbols/tachometer_vert.ts +0 -19
  174. package/symbols/triac_horz.ts +0 -13
  175. package/symbols/triac_vert.ts +0 -16
  176. package/symbols/tunnel_diode_horz.ts +0 -19
  177. package/symbols/tunnel_diode_vert.ts +0 -18
  178. package/symbols/unijunction_transistor_horz.ts +0 -20
  179. package/symbols/unijunction_transistor_vert.ts +0 -19
  180. package/symbols/var_meter_horz.ts +0 -30
  181. package/symbols/var_meter_vert.ts +0 -4
  182. package/symbols/varactor_diode_horz.ts +0 -19
  183. package/symbols/varactor_diode_vert.ts +0 -18
  184. package/symbols/varistor_horz.ts +0 -24
  185. package/symbols/varistor_vert.ts +0 -4
  186. package/symbols/varmeter_horz.ts +0 -32
  187. package/symbols/varmeter_vert.ts +0 -19
  188. package/symbols/volt_meter_horz.ts +0 -10
  189. package/symbols/volt_meter_vert.ts +0 -4
  190. package/symbols/watt_hour_meter_horz.ts +0 -32
  191. package/symbols/watt_hour_meter_vert.ts +0 -19
  192. package/symbols/wattmeter_horz.ts +0 -32
  193. package/symbols/wattmeter_vert.ts +0 -19
  194. package/symbols/zener_diode_horz.ts +0 -19
  195. package/symbols/zener_diode_vert.ts +0 -17
package/package.json CHANGED
@@ -1,13 +1,10 @@
1
1
  {
2
2
  "name": "schematic-symbols",
3
3
  "main": "./dist/index.js",
4
- "version": "0.0.121",
4
+ "version": "0.0.122",
5
5
  "type": "module",
6
6
  "files": [
7
- "dist",
8
- "index.ts",
9
- "drawing",
10
- "symbols"
7
+ "dist"
11
8
  ],
12
9
  "scripts": {
13
10
  "start": "bun run build && bun --hot run dev-server.ts",
package/drawing/arc.ts DELETED
@@ -1,31 +0,0 @@
1
- import type { Point } from "./types"
2
-
3
- export function approximateArc(
4
- x1: number,
5
- y1: number,
6
- rx: number,
7
- ry: number,
8
- largeArcFlag: number,
9
- sweepFlag: number,
10
- x2: number,
11
- y2: number,
12
- ): Point[] {
13
- // This is a simplified approximation. For a more accurate representation,
14
- // you might want to use a dedicated SVG path library.
15
- const steps = 40
16
- const points: Point[] = []
17
- for (let i = 1; i < steps; i++) {
18
- const t = i / steps
19
- const x = (1 - t) * x1 + t * x2
20
- const y = (1 - t) * y1 + t * y2
21
- // Apply a simple curve to make it non-linear, with reduced height and no center drop
22
- const curveY =
23
- 0.03 *
24
- Math.abs(Math.sin(t * Math.PI)) *
25
- Math.min(rx, ry) *
26
- (largeArcFlag ? 1 : -1) *
27
- (sweepFlag ? -1 : 1)
28
- points.push({ x, y: y + curveY })
29
- }
30
- return points
31
- }
package/drawing/arrow.ts DELETED
@@ -1,41 +0,0 @@
1
- import { Point, Primitive } from "drawing"
2
- import { path } from "./path"
3
-
4
- export const arrow = (
5
- start: Point,
6
- end: Point,
7
- color: string = "black",
8
- headSize: number = 10,
9
- ): Primitive[] => {
10
- // Calculate the angle and length of the arrow
11
- const dx = end.x - start.x
12
- const dy = end.y - start.y
13
- const angle = Math.atan2(dy, dx)
14
- const length = Math.sqrt(dx * dx + dy * dy)
15
-
16
- // Calculate the points for the arrow head
17
- const headAngle = Math.PI / 6 // 30 degrees
18
- const headPoint1 = {
19
- x: end.x - headSize * Math.cos(angle - headAngle),
20
- y: end.y - headSize * Math.sin(angle - headAngle),
21
- }
22
- const headPoint2 = {
23
- x: end.x - headSize * Math.cos(angle + headAngle),
24
- y: end.y - headSize * Math.sin(angle + headAngle),
25
- }
26
-
27
- return [
28
- // Arrow body
29
- path({
30
- points: [start, end],
31
- color: color,
32
- fill: false,
33
- }),
34
- // Arrow head
35
- path({
36
- points: [headPoint1, end, headPoint2],
37
- color: color,
38
- fill: true,
39
- }),
40
- ]
41
- }
package/drawing/box.ts DELETED
@@ -1,5 +0,0 @@
1
- import type { BoxPrimitive } from "drawing"
2
-
3
- export function box(options: Omit<BoxPrimitive, "type">): BoxPrimitive {
4
- return { type: "box", ...options }
5
- }
package/drawing/circle.ts DELETED
@@ -1,7 +0,0 @@
1
- import type { CirclePrimitive } from "drawing"
2
-
3
- export function circle(
4
- options: Omit<CirclePrimitive, "type">,
5
- ): CirclePrimitive {
6
- return { type: "circle", ...options }
7
- }
@@ -1,28 +0,0 @@
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
- }
@@ -1,16 +0,0 @@
1
- import type { SchSymbol } from "./types"
2
- import { getBoundsOfPrimitives } from "./utils/getBoundsOfPrimitives"
3
-
4
- export function defineSymbol(
5
- symbol: Omit<SchSymbol, "size"> & Partial<Pick<SchSymbol, "size">>,
6
- ): SchSymbol {
7
- let size = symbol.size
8
- if (!size) {
9
- const bounds = getBoundsOfPrimitives(symbol.primitives)
10
- size = {
11
- width: bounds.maxX - bounds.minX,
12
- height: bounds.maxY - bounds.minY,
13
- }
14
- }
15
- return { ...symbol, size }
16
- }
package/drawing/getSvg.ts DELETED
@@ -1,199 +0,0 @@
1
- import { makeAnchorYUpPositive, makeYUpPositive } from "./utils/makeYUpPositive"
2
- import { mapColor } from "./mapColor"
3
- import { pathToSvgD } from "./pathToSvgD"
4
- import type { Point, Port, SchSymbol, TextPrimitive } from "./types"
5
-
6
- function createDiamondElement(
7
- center: Point,
8
- size = 0.05,
9
- yUpPositive: boolean = true,
10
- ): string {
11
- const { x, y } = center
12
- const halfSize = size / 2
13
- return `<path d="M ${x} ${makeYUpPositive(y, yUpPositive) - halfSize} L ${x + halfSize} ${makeYUpPositive(y, yUpPositive)} L ${x} ${
14
- makeYUpPositive(y, yUpPositive) + halfSize
15
- } L ${x - halfSize} ${makeYUpPositive(y, yUpPositive)} Z" fill="green" />`
16
- }
17
-
18
- function createTextElement(
19
- primitive: TextPrimitive,
20
- { yUpPositive }: { yUpPositive?: boolean },
21
- ): {
22
- text: string
23
- anchor: string
24
- } {
25
- const { x, y, text, fontSize = 0.1, anchor } = primitive
26
- let textAnchor: string
27
- const dx = 0
28
- let dy = 0
29
-
30
- const capHeight = fontSize * 0.75
31
-
32
- switch (anchor) {
33
- case "top_left":
34
- textAnchor = "start"
35
- dy = fontSize
36
- break
37
- case "top_right":
38
- textAnchor = "end"
39
- dy = fontSize
40
- break
41
- case "bottom_left":
42
- textAnchor = "start"
43
- break
44
- case "bottom_right":
45
- textAnchor = "end"
46
- break
47
- case "center":
48
- textAnchor = "middle"
49
- dy = fontSize / 2
50
- break
51
- case "middle_top":
52
- textAnchor = "middle"
53
- dy = capHeight
54
- break
55
- case "middle_bottom":
56
- textAnchor = "middle"
57
- break
58
- case "middle_left":
59
- textAnchor = "start"
60
- dy = capHeight / 2
61
- break
62
- case "middle_right":
63
- textAnchor = "end"
64
- dy = capHeight / 2
65
- break
66
- }
67
-
68
- return {
69
- text: `<text x="${x}" y="${makeYUpPositive(y, yUpPositive)}" dx="${dx}" dy="${dy}" text-anchor="${textAnchor}" style="font: ${
70
- fontSize ?? 0.1
71
- }px monospace; fill: ${mapColor("primary")}">${text}</text>`,
72
- anchor: `<rect x="${x - 0.025 / 2}" y="${
73
- makeYUpPositive(y, yUpPositive) - 0.025 / 2
74
- }" width="0.025" height="0.025" fill="blue" />`,
75
- }
76
- }
77
-
78
- function createPortElement(
79
- port: Port,
80
- { yUpPositive }: { yUpPositive?: boolean },
81
- ): string {
82
- const { x, y, labels } = port
83
- const rectSize = 0.05
84
- const labelFontSize = 0.08
85
- const label = labels[0] || ""
86
-
87
- return `
88
- <rect x="${x - rectSize / 2}" y="${
89
- makeYUpPositive(y, yUpPositive) - rectSize / 2
90
- }" width="${rectSize}" height="${rectSize}" fill="red" />
91
- <text x="${x - labelFontSize / 2}" y="${
92
- makeYUpPositive(y, yUpPositive) + rectSize + labelFontSize / 2
93
- }" text-anchor="middle" style="font: ${labelFontSize}px monospace; fill: #833;">${label}</text>
94
- `
95
- }
96
-
97
- export function getInnerSvg(
98
- symbol: SchSymbol,
99
- options: { width?: number; height?: number; debug?: boolean } = {},
100
- ): string {
101
- const { debug = false } = options
102
- const { primitives, size, ports } = symbol
103
- const svgElements = primitives.map((primitive) => {
104
- switch (primitive.type) {
105
- case "path":
106
- return `<path d="${pathToSvgD(primitive.points, {
107
- closed: primitive.closed,
108
- yUpPositive: true,
109
- })}" fill="${
110
- primitive.fill ? mapColor(primitive.color) : "none"
111
- }" stroke="${mapColor(
112
- primitive.color,
113
- )}" stroke-width="0.02" stroke-linecap="round" stroke-linejoin="round" />`
114
- case "text":
115
- const textElements = createTextElement(primitive, { yUpPositive: true })
116
- return textElements.text + (debug ? textElements.anchor : "")
117
- case "circle":
118
- return `<circle cx="${primitive.x}" cy="${makeYUpPositive(primitive.y, true)}" r="${
119
- primitive.radius
120
- }" fill="${primitive.fill ? mapColor(primitive.color) : "none"}" ${
121
- !primitive.fill
122
- ? `stroke="${mapColor(primitive.color)}" stroke-width="0.02"`
123
- : ""
124
- } />`
125
- case "box":
126
- return `<rect x="${primitive.x}" y="${makeYUpPositive(primitive.y)}" width="${
127
- primitive.width
128
- }" height="${primitive.height}" fill="${mapColor("primary")}" />`
129
- default:
130
- return ""
131
- }
132
- })
133
-
134
- const portElements = ports
135
- .map((p) => createPortElement(p, { yUpPositive: true }))
136
- .join("\n ")
137
-
138
- const centerDiamond = createDiamondElement(symbol.center)
139
-
140
- const debugElements = []
141
- if (debug) {
142
- const topLeft = {
143
- x: symbol.center.x - size.width / 2,
144
- y: symbol.center.y - size.height / 2,
145
- }
146
- debugElements.push(
147
- `<text x="${topLeft.x}" y="${topLeft.y}" style="font: 0.05px monospace; fill: #833;">${size.width.toFixed(2)} x ${size.height.toFixed(2)}</text>`,
148
- )
149
-
150
- // Show all available port labels
151
- ports.forEach((port, i) => {
152
- if (port.labels.length > 1) {
153
- const alternateLabels = port.labels.slice(1).join(", ")
154
- debugElements.push(
155
- `<text x="${topLeft.x}" y="${topLeft.y - (i + 1) * 0.05}" dy="-0.15" style="font: 0.05px monospace; fill: #833;">${port.labels[0]} [${alternateLabels}]</text>`,
156
- )
157
- }
158
- })
159
-
160
- debugElements.push(...debugElements)
161
- }
162
-
163
- return [
164
- svgElements.join("\n "),
165
- portElements,
166
- centerDiamond,
167
- ...debugElements,
168
- ].join("\n")
169
- }
170
-
171
- export function getSvg(
172
- symbol: SchSymbol,
173
- options: { width?: number; height?: number; debug?: boolean } = {},
174
- ): string {
175
- const { size } = symbol
176
- const innerSvg = getInnerSvg(symbol, options)
177
-
178
- // Use the center and the size to calculate the viewBox
179
- const bufferMultiple = 1.2
180
- const w = size.width * bufferMultiple
181
- const h = size.height * bufferMultiple
182
- const viewBox = {
183
- x: symbol.center.x - w / 2,
184
- y: makeYUpPositive(symbol.center.y, true) - h / 2,
185
- width: w,
186
- height: h,
187
- }
188
-
189
- if (options.width && !options.height) {
190
- options.height = options.width! * (viewBox.height / viewBox.width)
191
- } else if (!options.width && options.height) {
192
- options.width = options.height! * (viewBox.width / viewBox.height)
193
- } else if (!options.width && !options.height) {
194
- options.width = viewBox.width
195
- options.height = viewBox.height
196
- }
197
-
198
- return `<svg width="${options.width}" height="${options.height}" viewBox="${viewBox.x} ${viewBox.y} ${viewBox.width} ${viewBox.height}" xmlns="http://www.w3.org/2000/svg">${innerSvg}</svg>`
199
- }
package/drawing/index.ts DELETED
@@ -1,10 +0,0 @@
1
- export * from "./types"
2
- export * from "./box"
3
- export * from "./arrow"
4
- export * from "./circle"
5
- export * from "./getSvg"
6
- export * from "./mapColor"
7
- export * from "./resizeSymbol"
8
- export * from "./pathToSvgD"
9
- export * from "./text"
10
- export * from "./svgPathToPoints"
@@ -1,10 +0,0 @@
1
- export const mapColor = (color: string) => {
2
- switch (color) {
3
- case "primary":
4
- return "black"
5
- case "secondary":
6
- return "gray"
7
- default:
8
- return color
9
- }
10
- }
@@ -1,97 +0,0 @@
1
- import { NinePointAnchor, SchSymbol } from "drawing/types"
2
- import { rotateRightFacingSymbol } from "drawing/rotateSymbol"
3
- import { getBoundsOfPrimitives } from "drawing/utils/getBoundsOfPrimitives"
4
-
5
- interface ModifySymbolBuilder {
6
- changeTextAnchor(
7
- text: "{REF}" | "{VAL}" | string,
8
- newAnchor: NinePointAnchor,
9
- ): ModifySymbolBuilder
10
- rotateRightFacingSymbol(
11
- newOrientation: "up" | "down" | "left" | "right",
12
- ): ModifySymbolBuilder
13
- labelPort(currentLabel: string, newLabels: string[]): ModifySymbolBuilder
14
- build(): SchSymbol
15
- }
16
-
17
- class SymbolModifier implements ModifySymbolBuilder {
18
- private symbol: SchSymbol
19
-
20
- constructor(symbol: SchSymbol) {
21
- this.symbol = JSON.parse(JSON.stringify(symbol))
22
- this.symbol.size = this.computeSize()
23
- }
24
-
25
- changeTextAnchor(
26
- text: "{REF}" | "{VAL}" | string,
27
- newAnchor: NinePointAnchor,
28
- ): ModifySymbolBuilder {
29
- this.symbol = {
30
- ...this.symbol,
31
- primitives: this.symbol.primitives.map((primitive) => {
32
- if (primitive.type === "text" && primitive.text === text) {
33
- return {
34
- ...primitive,
35
- anchor: newAnchor,
36
- }
37
- }
38
- return primitive
39
- }),
40
- }
41
- return this
42
- }
43
-
44
- labelPort(currentLabel: string, newLabels: string[]): ModifySymbolBuilder {
45
- this.symbol = {
46
- ...this.symbol,
47
- ports: this.symbol.ports.map((port) => {
48
- return port.labels.includes(currentLabel)
49
- ? { ...port, labels: newLabels }
50
- : port
51
- }),
52
- }
53
- return this
54
- }
55
-
56
- rotateRightFacingSymbol(
57
- newOrientation: "up" | "down" | "left" | "right",
58
- ): ModifySymbolBuilder {
59
- this.symbol = rotateRightFacingSymbol(this.symbol, {
60
- newOrientation,
61
- })
62
- return this
63
- }
64
-
65
- computeSize(): { width: number; height: number } {
66
- const bounds = getBoundsOfPrimitives(this.symbol.primitives)
67
- return {
68
- width: bounds.maxX - bounds.minX,
69
- height: bounds.maxY - bounds.minY,
70
- }
71
- }
72
-
73
- build(): SchSymbol {
74
- return { ...this.symbol, size: this.computeSize() }
75
- }
76
- }
77
-
78
- export const modifySymbol = (symbol: any): ModifySymbolBuilder => {
79
- return new SymbolModifier({
80
- ...symbol,
81
- primitives: symbol.primitives ?? [
82
- ...Object.values(symbol.paths ?? {}),
83
- ...Object.values(symbol.texts ?? {}),
84
- ...Object.values(symbol.circles ?? {}),
85
- ...Object.values(symbol.rects ?? {}),
86
- ],
87
- ports:
88
- symbol.ports ??
89
- Object.entries(symbol.refblocks).flatMap(([key, refblock]) => {
90
- return [{ ...(refblock as object), labels: [key] }]
91
- }),
92
- center: symbol.center ?? {
93
- x: symbol.bounds.centerX,
94
- y: symbol.bounds.centerY,
95
- },
96
- })
97
- }
@@ -1,11 +0,0 @@
1
- export const ninePointAnchorToSvgAnchor = {
2
- top_left: "start",
3
- top_right: "end",
4
- bottom_left: "start",
5
- bottom_right: "end",
6
- center: "middle",
7
- middle_top: "middle",
8
- middle_bottom: "middle",
9
- middle_left: "start",
10
- middle_right: "end",
11
- }
package/drawing/path.ts DELETED
@@ -1,5 +0,0 @@
1
- import type { PathPrimitive } from "drawing"
2
-
3
- export function path(options: Omit<PathPrimitive, "type">): PathPrimitive {
4
- return { type: "path", ...options }
5
- }
@@ -1,22 +0,0 @@
1
- import type { Point } from "drawing"
2
- import { makeYUpPositive } from "./utils/makeYUpPositive"
3
-
4
- export function pathToSvgD(
5
- points: Point[],
6
- {
7
- closed = false,
8
- yUpPositive = true,
9
- }: {
10
- closed?: boolean
11
- yUpPositive?: boolean
12
- },
13
- ): string {
14
- const pathCommands = points
15
- .map(
16
- (point, index) =>
17
- `${index === 0 ? "M" : "L"}${point.x},${makeYUpPositive(point.y, yUpPositive)}`,
18
- )
19
- .join(" ")
20
-
21
- return closed ? `${pathCommands} Z` : pathCommands
22
- }
@@ -1,67 +0,0 @@
1
- import type { SchSymbol } from "./types"
2
-
3
- export function resizeSymbol(
4
- symbol: SchSymbol,
5
- newSize: { width?: number; height?: number },
6
- ): SchSymbol {
7
- const { width: oldWidth, height: oldHeight } = symbol.size
8
- let scaleX = 1,
9
- scaleY = 1
10
-
11
- if (newSize.width && newSize.height) {
12
- scaleX = newSize.width / oldWidth
13
- scaleY = newSize.height / oldHeight
14
- } else if (newSize.width) {
15
- scaleX = scaleY = newSize.width / oldWidth
16
- } else if (newSize.height) {
17
- scaleX = scaleY = newSize.height / oldHeight
18
- }
19
-
20
- const resizedPrimitives = symbol.primitives.map((primitive) => {
21
- switch (primitive.type) {
22
- case "path":
23
- return {
24
- ...primitive,
25
- points: primitive.points.map((p) => ({
26
- x: p.x * scaleX,
27
- y: p.y * scaleY,
28
- })),
29
- }
30
- case "text":
31
- case "circle":
32
- return {
33
- ...primitive,
34
- x: primitive.x * scaleX,
35
- y: primitive.y * scaleY,
36
- }
37
- case "box":
38
- return {
39
- ...primitive,
40
- x: primitive.x * scaleX,
41
- y: primitive.y * scaleY,
42
- width: primitive.width * scaleX,
43
- height: primitive.height * scaleY,
44
- }
45
- default:
46
- return primitive
47
- }
48
- })
49
-
50
- return {
51
- ...symbol,
52
- primitives: resizedPrimitives,
53
- center: {
54
- x: symbol.center.x * scaleX,
55
- y: symbol.center.y * scaleY,
56
- },
57
- ports: symbol.ports.map((port) => ({
58
- ...port,
59
- x: port.x * scaleX,
60
- y: port.y * scaleY,
61
- })),
62
- size: {
63
- width: oldWidth * scaleX,
64
- height: oldHeight * scaleY,
65
- },
66
- }
67
- }