// SVG geometry for gate symbols, in pixels relative to the node origin. import { GRID as G, type Pt, type Shape } from './logic/geometry' import type { CrossingStyle, SymbolKind } from './logic/types' const NEGATED = new Set(['nand', 'nor', 'xnor', 'not']) const IEC_LABEL: Record = { 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 })) } /** A hop on one segment: from lo to hi along it (grid units), r pixels high. */ export interface HopSpan { lo: number hi: number r: number } /** * SVG path for a wire (points in grid units), with hops where it jumps over * other lines (`hops`: segment index to the stretches it jumps). Horizontal * segments jump upwards, vertical ones to the left. `scale` is pixels per * grid unit. */ export function wirePath(pts: Pt[], hops: Map | undefined, style: CrossingStyle, scale = G): string { let d = `M${pts[0]![0] * scale} ${pts[0]![1] * scale}` for (let i = 0; i < pts.length - 1; i++) { const [ax, ay] = pts[i]! const [bx, by] = pts[i + 1]! const spans = hops?.get(i) if (spans?.length && style !== 'plain' && (ay === by || ax === bx)) { const horizontal = ay === by const from = (horizontal ? ax : ay) * scale const to = (horizontal ? bx : by) * scale const dir = Math.sign(to - from) const c = (horizontal ? ay : ax) * scale // A point at `t` along the segment, `off` pixels out to the side the hop bulges. const at = (t: number, off = 0) => (horizontal ? `${t} ${c - off}` : `${c - off} ${t}`) const list = spans.map(h => ({ s: (dir > 0 ? h.lo : h.hi) * scale, e: (dir > 0 ? h.hi : h.lo) * scale, r: h.r })).sort((p, q) => dir * (p.s - q.s)) for (const { s, e, r } of list) { const half = Math.abs(e - s) / 2 d += `L${at(s)}` if (style === 'bow') { // Up for a horizontal segment, to the left for a vertical one. const sweep = horizontal ? (dir > 0 ? 1 : 0) : dir > 0 ? 0 : 1 d += `A${half} ${r} 0 0 ${sweep} ${at(e)}` } else if (style === 'triangle') { const lift = r * 1.3 if (half <= r * 1.2) d += `L${at((s + e) / 2, lift)}L${at(e)}` else d += `L${at(s + dir * r, lift)}L${at(e - dir * r, lift)}L${at(e)}` } else if (style === 'bridge') d += `L${at(s, r)}L${at(e, r)}L${at(e)}` else d += `M${at(e)}` } } d += `L${bx * scale} ${by * scale}` } return d }