Skip to content

Commit

Permalink
Implemented create svg schematic text
Browse files Browse the repository at this point in the history
  • Loading branch information
AnasSarkiz committed Nov 17, 2024
1 parent 6d7274a commit 8b462e3
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/sch/convert-circuit-json-to-schematic-svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { createSvgObjectsFromSchematicComponent } from "./svg-object-fns/create-
import { createSvgObjectsFromSchDebugObject } from "./svg-object-fns/create-svg-objects-from-sch-debug-object"
import { createSchematicTrace } from "./svg-object-fns/create-svg-objects-from-sch-trace"
import { createSvgObjectsForSchNetLabel } from "./svg-object-fns/create-svg-objects-for-sch-net-label"
import { createSvgSchText } from "./svg-object-fns/create-svg-objects-for-sch-text"

interface Options {
width?: number
Expand Down Expand Up @@ -103,6 +104,7 @@ export function convertCircuitJsonToSchematicSvg(
const schComponentSvgs: SvgObject[] = []
const schTraceSvgs: SvgObject[] = []
const schNetLabel: SvgObject[] = []
const schText: SvgObject[] = []

for (const elm of circuitJson) {
if (elm.type === "schematic_debug_object") {
Expand All @@ -120,8 +122,13 @@ export function convertCircuitJsonToSchematicSvg(
} else if (elm.type === "schematic_trace") {
schTraceSvgs.push(...createSchematicTrace(elm, transform))
} else if (elm.type === "schematic_net_label") {
console.log(elm, transform)
schNetLabel.push(...createSvgObjectsForSchNetLabel(elm, transform))
}
else if (elm.type === "schematic_text") {
console.log(elm, transform)
schText.push(createSvgSchText(elm, transform))
}
}

// Add elements in correct order
Expand All @@ -130,6 +137,7 @@ export function convertCircuitJsonToSchematicSvg(
...schComponentSvgs,
...schTraceSvgs,
...schNetLabel,
...schText
)

// Add labeled points if provided
Expand Down
2 changes: 2 additions & 0 deletions lib/sch/get-schematic-bounds-from-circuit-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export function getSchematicBoundsFromCircuitJson(
updateBounds(edge.from, { width: 0.1, height: 0.1 }, 0)
updateBounds(edge.to, { width: 0.1, height: 0.1 }, 0)
}
}else if (item.type ==="schematic_text"){
updateBounds(item.position, { width: 0.1, height: 0.1 }, 0)
}
}

Expand Down
66 changes: 66 additions & 0 deletions lib/sch/svg-object-fns/create-svg-objects-for-sch-text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type { SvgObject } from "lib/svg-object";
import { colorMap } from "lib/utils/colors";
import { getSchScreenFontSize } from "lib/utils/get-sch-font-size";
import { applyToPoint, type Matrix } from "transformation-matrix";

export const createSvgSchText = (
elm: {
anchor: "center" | "left" | "right" | "top" | "bottom";
type: "schematic_text";
schematic_component_id: string;
rotation: number;
schematic_text_id: string;
text: string;
position: { x: number; y: number };
},
transform: Matrix
): SvgObject => {


// Apply transformation
const center = applyToPoint(transform, elm.position);

console.log("Transformed Center Position:", center);
console.log("Element Position:", elm.position);

const textAnchorMap: Record<typeof elm.anchor, string> = {
center: "middle",
left: "start",
right: "end",
top: "middle",
bottom: "middle",
};

const dominantBaselineMap: Record<typeof elm.anchor, string> = {
center: "middle",
left: "middle",
right: "middle",
top: "hanging",
bottom: "ideographic",
};

return {
type: "element",
name: "text",
value: "",
attributes: {
x: center.x.toString(),
y: center.y.toString(),
fill : colorMap.schematic.sheet_label,
"text-anchor": textAnchorMap[elm.anchor],
"dominant-baseline": dominantBaselineMap[elm.anchor],
"font-family": "sans-serif",
"font-size": `${getSchScreenFontSize(transform, "reference_designator")}px`,
transform: `rotate(${elm.rotation}, ${center.x}, ${center.y})`,
},
children: [
{
type: "text",
value: elm.text,
name: elm.schematic_text_id,
attributes: {},
children: [],
},
],
};
};
17 changes: 17 additions & 0 deletions tests/sch/__snapshots__/schText.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions tests/sch/schText.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect, test } from "bun:test"
import type { SchematicText } from "circuit-json"
import { convertCircuitJsonToSchematicSvg } from "lib/index"

test("schematic text", () => {

const circuitJson: SchematicText[] = [
{
type: "schematic_text",
position: { x: 0, y: 1 },
anchor: "right",
schematic_component_id: "",
rotation: 0,
schematic_text_id: "",
text: "Anas"
},
{
type: "schematic_text",
position: { x: 1, y: 1 },
anchor: "left",
schematic_component_id: "",
rotation: 0,
schematic_text_id: "",
text: "Ahmed"
},
{
type: "schematic_text",
position: { x: 0, y: 0 },
anchor: "center",
schematic_component_id: "",
rotation: 180,
schematic_text_id: "",
text: "Abse"
},
]

expect(
// @ts-ignore
convertCircuitJsonToSchematicSvg(circuitJson),
).toMatchSvgSnapshot(import.meta.path)
})

0 comments on commit 8b462e3

Please sign in to comment.