104 lines
3.1 KiB
TypeScript
104 lines
3.1 KiB
TypeScript
|
|
// SVG geometry for gate symbols, in pixels relative to the node origin.
|
|||
|
|
|
|||
|
|
import { GRID as G, type Shape } from './logic/geometry'
|
|||
|
|
import type { SymbolKind } from './logic/types'
|
|||
|
|
|
|||
|
|
const NEGATED = new Set<SymbolKind>(['nand', 'nor', 'xnor', 'not'])
|
|||
|
|
|
|||
|
|
const IEC_LABEL: Record<SymbolKind, string> = {
|
|||
|
|
and: '&',
|
|||
|
|
nand: '&',
|
|||
|
|
or: '≥1',
|
|||
|
|
nor: '≥1',
|
|||
|
|
xor: '=1',
|
|||
|
|
xnor: '=1',
|
|||
|
|
not: '1',
|
|||
|
|
buf: '1',
|
|||
|
|
box: '',
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export interface GateDrawing {
|
|||
|
|
stubs: string
|
|||
|
|
body: string
|
|||
|
|
/** Extra stroke-only path (second curve of an ANSI XOR). */
|
|||
|
|
extra?: string
|
|||
|
|
bubble?: { cx: number; cy: number; r: number }
|
|||
|
|
label?: { x: number; y: number; text: string }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export function gateDrawing(shape: Shape, style: 'iec' | 'ansi'): GateDrawing {
|
|||
|
|
const symbol = shape.gate!
|
|||
|
|
const { body } = shape
|
|||
|
|
const W = body.w * G
|
|||
|
|
const out = shape.outputs[0]!
|
|||
|
|
const oy = out.dy * G
|
|||
|
|
const negated = NEGATED.has(symbol)
|
|||
|
|
const r = 0.22 * G
|
|||
|
|
const one = shape.inputs.length === 1
|
|||
|
|
const top = (one ? body.y + 0.25 : body.y) * G
|
|||
|
|
const bottom = (one ? body.y + body.h - 0.25 : body.y + body.h) * G
|
|||
|
|
const mid = (top + bottom) / 2
|
|||
|
|
const x0 = 0.5 * G
|
|||
|
|
const x1 = W - 0.5 * G - (negated ? 2 * r : 0)
|
|||
|
|
|
|||
|
|
const stubs = [
|
|||
|
|
...shape.inputs.map(p => `M0 ${p.dy * G}H${x0 + 0.5 * G}`),
|
|||
|
|
`M${x1 - 0.3 * G} ${oy}H${W}`,
|
|||
|
|
].join('')
|
|||
|
|
const bubble = negated ? { cx: x1 + r, cy: oy, r } : undefined
|
|||
|
|
|
|||
|
|
if (style === 'iec') {
|
|||
|
|
return {
|
|||
|
|
stubs,
|
|||
|
|
body: `M${x0} ${top}H${x1}V${bottom}H${x0}Z`,
|
|||
|
|
bubble,
|
|||
|
|
label: { x: (x0 + x1) / 2, y: top + 0.62 * G, text: IEC_LABEL[symbol] },
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const h = bottom - top
|
|||
|
|
switch (symbol) {
|
|||
|
|
case 'and':
|
|||
|
|
case 'nand': {
|
|||
|
|
const rad = h / 2
|
|||
|
|
const xm = Math.max(x0, x1 - rad)
|
|||
|
|
return { stubs, bubble, body: `M${x0} ${top}H${xm}A${rad} ${rad} 0 0 1 ${xm} ${bottom}H${x0}Z` }
|
|||
|
|
}
|
|||
|
|
case 'or':
|
|||
|
|
case 'nor':
|
|||
|
|
case 'xor':
|
|||
|
|
case 'xnor': {
|
|||
|
|
const xs = symbol.startsWith('x') ? x0 + 0.3 * G : x0
|
|||
|
|
const w = x1 - xs
|
|||
|
|
const path = `M${xs} ${top}Q${xs + 0.28 * w} ${mid} ${xs} ${bottom}Q${xs + 0.62 * w} ${bottom} ${x1} ${mid}Q${xs + 0.62 * w} ${top} ${xs} ${top}Z`
|
|||
|
|
const extra = xs !== x0 ? `M${x0} ${top}Q${x0 + 0.28 * w} ${mid} ${x0} ${bottom}` : undefined
|
|||
|
|
return { stubs, bubble, body: path, extra }
|
|||
|
|
}
|
|||
|
|
default:
|
|||
|
|
return { stubs, bubble, body: `M${x0} ${top}L${x1} ${mid}L${x0} ${bottom}Z` }
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 7-segment patterns for 0–F, segments a..g as bits 0..6.
|
|||
|
|
const SEGMENTS = [0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71]
|
|||
|
|
|
|||
|
|
export function sevenSegment(value: number) {
|
|||
|
|
const xa = 1.15 * G
|
|||
|
|
const xb = 2.35 * G
|
|||
|
|
const y0 = -0.05 * G
|
|||
|
|
const y1 = 1.5 * G
|
|||
|
|
const y2 = 3.05 * G
|
|||
|
|
const g = 3
|
|||
|
|
const segs: [number, number, number, number][] = [
|
|||
|
|
[xa + g, y0, xb - g, y0],
|
|||
|
|
[xb, y0 + g, xb, y1 - g],
|
|||
|
|
[xb, y1 + g, xb, y2 - g],
|
|||
|
|
[xa + g, y2, xb - g, y2],
|
|||
|
|
[xa, y1 + g, xa, y2 - g],
|
|||
|
|
[xa, y0 + g, xa, y1 - g],
|
|||
|
|
[xa + g, y1, xb - g, y1],
|
|||
|
|
]
|
|||
|
|
const bits = SEGMENTS[value & 15]!
|
|||
|
|
return segs.map((s, i) => ({ d: `M${s[0]} ${s[1]}L${s[2]} ${s[3]}`, on: (bits >> i) & 1 }))
|
|||
|
|
}
|