-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
82 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import type { | ||
AnyCircuitElement, | ||
SchematicComponent, | ||
SchematicPort, | ||
} from "circuit-json" | ||
import type { SvgObject } from "lib/svg-object" | ||
import { colorMap } from "lib/utils/colors" | ||
import { getSchFontSize } from "lib/utils/get-sch-font-size" | ||
import { getUnitVectorFromOutsideToEdge } from "lib/utils/get-unit-vector-from-outside-to-edge" | ||
import { applyToPoint, type Matrix } from "transformation-matrix" | ||
|
||
const LABEL_DIST_FROM_EDGE_MM = 0.1 | ||
|
||
export const createSvgObjectsForSchPortPinLabel = (params: { | ||
schPort: SchematicPort | ||
schComponent: SchematicComponent | ||
transform: Matrix | ||
circuitJson: AnyCircuitElement[] | ||
}): SvgObject[] => { | ||
const svgObjects: SvgObject[] = [] | ||
const { schPort, schComponent, transform, circuitJson } = params | ||
|
||
const realPinNumberPos = { | ||
x: schPort.center.x, | ||
y: schPort.center.y, | ||
} | ||
|
||
if (!schPort.side_of_component) return [] | ||
const vecToEdge = getUnitVectorFromOutsideToEdge(schPort.side_of_component) | ||
|
||
const realPinEdgeDistance = schPort.distance_from_component_edge ?? 0.4 | ||
|
||
// Move the pin number halfway to the edge of the box component so it sits | ||
// between the edge and the port, exactly in the middle | ||
realPinNumberPos.x += | ||
vecToEdge.x * (realPinEdgeDistance + LABEL_DIST_FROM_EDGE_MM) | ||
realPinNumberPos.y += | ||
vecToEdge.y * (realPinEdgeDistance + LABEL_DIST_FROM_EDGE_MM) | ||
|
||
// Transform the pin position from local to global coordinates | ||
const screenPinNumberTextPos = applyToPoint(transform, realPinNumberPos) | ||
|
||
const label = schComponent.port_labels?.[`pin${schPort.pin_number}`] | ||
|
||
if (!label) return [] | ||
|
||
svgObjects.push({ | ||
name: "text", | ||
type: "element", | ||
attributes: { | ||
class: "pin-number", | ||
x: screenPinNumberTextPos.x.toString(), | ||
y: screenPinNumberTextPos.y.toString(), | ||
style: "font-family: sans-serif;", | ||
fill: colorMap.schematic.pin_number, | ||
"text-anchor": schPort.side_of_component === "left" ? "start" : "end", | ||
"dominant-baseline": "middle", | ||
"font-size": `${getSchFontSize(transform, "pin_number")}px`, | ||
transform: | ||
schPort.side_of_component === "top" || | ||
schPort.side_of_component === "bottom" | ||
? `rotate(-90 ${screenPinNumberTextPos.x} ${screenPinNumberTextPos.y})` | ||
: "", | ||
}, | ||
children: [ | ||
{ | ||
type: "text", | ||
value: label || "", | ||
name: "", | ||
attributes: {}, | ||
children: [], | ||
}, | ||
], | ||
value: "", | ||
}) | ||
|
||
return svgObjects | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.