Skip to content

Commit

Permalink
added support for silkscreen text
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiboSoftwareDev committed Nov 6, 2024
1 parent c657c0a commit 6e92b1d
Show file tree
Hide file tree
Showing 9 changed files with 802 additions and 7 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"circuit-json": "*"
},
"dependencies": {
"@tscircuit/alphabet": "^0.0.2",
"fast-json-stable-stringify": "^2.1.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
PcbHole,
PcbSolderPaste,
PcbSilkscreenPath,
PcbSilkscreenText,
} from "circuit-json"
import stableStringify from "fast-json-stable-stringify"
import type { AnyGerberCommand } from "../any_gerber_command"
Expand Down Expand Up @@ -116,6 +117,18 @@ export const getApertureConfigFromPcbSilkscreenPath = (
throw new Error(`Provide stroke_width for: ${elm as any}`)
}

export const getApertureConfigFromPcbSilkscreenText = (
elm: PcbSilkscreenText,
): ApertureTemplateConfig => {
if ("font_size" in elm) {
return {
standard_template_code: "C",
diameter: elm.font_size / 4, // font size and diamater have different units of measurement
}
}
throw new Error(`Provide font_size for: ${elm as any}`)
}

export const getApertureConfigFromPcbSolderPaste = (
elm: PcbSolderPaste,
): ApertureTemplateConfig => {
Expand Down Expand Up @@ -242,6 +255,8 @@ function getAllApertureTemplateConfigsForLayer(
addConfigIfNew(getApertureConfigFromPcbVia(elm))
} else if (elm.type === "pcb_silkscreen_path")
addConfigIfNew(getApertureConfigFromPcbSilkscreenPath(elm))
else if (elm.type === "pcb_silkscreen_text")
addConfigIfNew(getApertureConfigFromPcbSilkscreenText(elm))
}

return configs
Expand Down
68 changes: 68 additions & 0 deletions src/gerber/convert-soup-to-gerber-commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import {
getApertureConfigFromCirclePcbHole,
getApertureConfigFromPcbPlatedHole,
getApertureConfigFromPcbSilkscreenPath,
getApertureConfigFromPcbSilkscreenText,
getApertureConfigFromPcbSmtpad,
getApertureConfigFromPcbSolderPaste,
getApertureConfigFromPcbVia,
} from "./defineAperturesForLayer"
import { findApertureNumber } from "./findApertureNumber"
import { getCommandHeaders } from "./getCommandHeaders"
import { getGerberLayerName } from "./getGerberLayerName"
import { lineAlphabet } from "@tscircuit/alphabet"

/**
* Converts tscircuit soup to arrays of Gerber commands for each layer
Expand Down Expand Up @@ -146,6 +148,72 @@ export const convertSoupToGerberCommands = (
})
}

glayer.push(...gerber.build())
}
} else if (element.type === "pcb_silkscreen_text") {
if (element.layer === layer) {
const glayer = glayers[getGerberLayerName(layer, "silkscreen")]
const apertureConfig = getApertureConfigFromPcbSilkscreenText(element)
const gerber = gerberBuilder().add("select_aperture", {
aperture_number: findApertureNumber(glayer, apertureConfig),
})

let initialX = element.anchor_position.x
let initialY = element.anchor_position.y
const fontSize = element.font_size
const letterSpacing = fontSize * 0.4
const spaceWidth = fontSize * 0.5

const textWidth =
element.text.split("").reduce((width, char) => {
if (char === " ") {
return width + spaceWidth + letterSpacing
}
return width + fontSize + letterSpacing
}, 0) - letterSpacing

const textHeight = fontSize
switch (element.anchor_alignment || "center") {
case "top_right":
// No adjustment needed
break
case "top_left":
initialX -= textWidth
break
case "bottom_right":
initialY -= textHeight
break
case "bottom_left":
initialX -= textWidth
initialY -= textHeight
break
case "center":
initialX -= textWidth / 2
initialY -= textHeight / 2
break
}

let anchoredX = initialX
const anchoredY = initialY
for (const char of element.text.toUpperCase()) {
if (char === " ") {
anchoredX += spaceWidth + letterSpacing
continue
}

const letterPaths = lineAlphabet[char] || []
for (const path of letterPaths) {
const x1 = anchoredX + path.x1 * fontSize
const y1 = anchoredY + path.y1 * fontSize
const x2 = anchoredX + path.x2 * fontSize
const y2 = anchoredY + path.y2 * fontSize

gerber.add("move_operation", { x: x1, y: mfy(y1) })
gerber.add("plot_operation", { x: x2, y: mfy(y2) })
}

anchoredX += fontSize + letterSpacing // Move to next character position
}
glayer.push(...gerber.build())
}
} else if (element.type === "pcb_smtpad") {
Expand Down
Loading

0 comments on commit 6e92b1d

Please sign in to comment.