152 lines
5.9 KiB
TypeScript
152 lines
5.9 KiB
TypeScript
|
|
// A circuit as a sheet to print or keep: the drawing as plain SVG with its
|
||
|
|
// styles written in, framed, with a title block (project, circuit, date,
|
||
|
|
// sheet number) like a wiring diagram. For paper, every colour keeps its hue
|
||
|
|
// and turns from dark to light, so the sheet prints black on white.
|
||
|
|
|
||
|
|
const PROPS = [
|
||
|
|
'fill',
|
||
|
|
'fill-opacity',
|
||
|
|
'stroke',
|
||
|
|
'stroke-width',
|
||
|
|
'stroke-opacity',
|
||
|
|
'stroke-dasharray',
|
||
|
|
'stroke-linecap',
|
||
|
|
'stroke-linejoin',
|
||
|
|
'opacity',
|
||
|
|
'font-family',
|
||
|
|
'font-size',
|
||
|
|
'font-weight',
|
||
|
|
'font-style',
|
||
|
|
'text-anchor',
|
||
|
|
'dominant-baseline',
|
||
|
|
'letter-spacing',
|
||
|
|
'visibility',
|
||
|
|
]
|
||
|
|
|
||
|
|
/** Handles, hit areas and selection marks: on screen only. */
|
||
|
|
const SCREEN_ONLY =
|
||
|
|
'.pin-hit, .wire-hit, .sel-outline, .wire-halo, .wire-grip, .corner-handle, .fits, .targets, .drop-ring, .temp-wire, .pin-snap, .queue-badge, .row-hit, .cable-hit, title'
|
||
|
|
|
||
|
|
export interface SheetInfo {
|
||
|
|
project: string
|
||
|
|
circuit: string
|
||
|
|
date: Date
|
||
|
|
/** Area of the drawing to show, in the drawing's own units. */
|
||
|
|
box: { x: number; y: number; w: number; h: number }
|
||
|
|
/** White paper instead of the dark screen colours. */
|
||
|
|
paper?: boolean
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Same hue and saturation, lightness turned around: dark becomes light. */
|
||
|
|
export function paperColor(value: string): string {
|
||
|
|
const m = /^rgba?\(([^)]+)\)$/.exec(value.trim())
|
||
|
|
if (!m) return value
|
||
|
|
const [r, g, b, a] = m[1]!.split(/[\s,/]+/).filter(Boolean).map(Number) as [number, number, number, number?]
|
||
|
|
const [h, s, l] = toHsl(r / 255, g / 255, b / 255)
|
||
|
|
const [nr, ng, nb] = fromHsl(h, s, 1 - l)
|
||
|
|
const rgb = [nr, ng, nb].map(v => Math.round(v * 255)).join(', ')
|
||
|
|
return a === undefined || Number.isNaN(a) ? `rgb(${rgb})` : `rgba(${rgb}, ${a})`
|
||
|
|
}
|
||
|
|
|
||
|
|
function toHsl(r: number, g: number, b: number): [number, number, number] {
|
||
|
|
const max = Math.max(r, g, b)
|
||
|
|
const min = Math.min(r, g, b)
|
||
|
|
const l = (max + min) / 2
|
||
|
|
if (max === min) return [0, 0, l]
|
||
|
|
const d = max - min
|
||
|
|
const s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
|
||
|
|
const h = max === r ? (g - b) / d + (g < b ? 6 : 0) : max === g ? (b - r) / d + 2 : (r - g) / d + 4
|
||
|
|
return [h / 6, s, l]
|
||
|
|
}
|
||
|
|
|
||
|
|
function fromHsl(h: number, s: number, l: number): [number, number, number] {
|
||
|
|
if (!s) return [l, l, l]
|
||
|
|
const q = l < 0.5 ? l * (1 + s) : l + s - l * s
|
||
|
|
const p = 2 * l - q
|
||
|
|
const hue = (t: number) => {
|
||
|
|
const x = t < 0 ? t + 1 : t > 1 ? t - 1 : t
|
||
|
|
if (x < 1 / 6) return p + (q - p) * 6 * x
|
||
|
|
if (x < 1 / 2) return q
|
||
|
|
if (x < 2 / 3) return p + (q - p) * (2 / 3 - x) * 6
|
||
|
|
return p
|
||
|
|
}
|
||
|
|
return [hue(h + 1 / 3), hue(h), hue(h - 1 / 3)]
|
||
|
|
}
|
||
|
|
|
||
|
|
const esc = (s: string) => s.replace(/[&<>"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c]!)
|
||
|
|
|
||
|
|
/** The drawing inside `content` (the canvas's transformed group) as a standalone SVG sheet. */
|
||
|
|
export function sheetSvg(content: SVGGElement, info: SheetInfo): string {
|
||
|
|
const clone = content.cloneNode(true) as SVGGElement
|
||
|
|
const from = [content, ...content.querySelectorAll('*')]
|
||
|
|
const to = [clone, ...clone.querySelectorAll('*')]
|
||
|
|
from.forEach((el, i) => {
|
||
|
|
if (el.matches(SCREEN_ONLY)) to[i]!.setAttribute('data-screen-only', '')
|
||
|
|
const cs = getComputedStyle(el)
|
||
|
|
let style = ''
|
||
|
|
for (const p of PROPS) {
|
||
|
|
let v = cs.getPropertyValue(p)
|
||
|
|
if (!v) continue
|
||
|
|
if (info.paper && (p === 'fill' || p === 'stroke')) v = paperColor(v)
|
||
|
|
style += `${p}:${v};`
|
||
|
|
}
|
||
|
|
to[i]!.setAttribute('style', style)
|
||
|
|
to[i]!.removeAttribute('class')
|
||
|
|
})
|
||
|
|
// Removed only now: the two trees had to stay in step while copying styles.
|
||
|
|
for (const el of [...clone.querySelectorAll('[data-screen-only]')]) el.remove()
|
||
|
|
clone.removeAttribute('transform')
|
||
|
|
|
||
|
|
const { x, y, w, h } = info.box
|
||
|
|
const titleH = 34
|
||
|
|
const W = Math.max(w, 560)
|
||
|
|
const H = h + titleH
|
||
|
|
const bg = info.paper ? '#ffffff' : '#0e1116'
|
||
|
|
const ink = info.paper ? '#1b2330' : '#dbe2ea'
|
||
|
|
const line = info.paper ? '#9aa4b2' : '#3a4554'
|
||
|
|
const muted = info.paper ? '#5b6676' : '#8592a3'
|
||
|
|
const when = new Intl.DateTimeFormat(undefined, { dateStyle: 'medium', timeStyle: 'short' }).format(info.date)
|
||
|
|
const ty = y + h
|
||
|
|
const font = 'Inter, ui-sans-serif, system-ui, sans-serif'
|
||
|
|
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${x} ${y} ${W} ${H}" width="${W}" height="${H}">
|
||
|
|
<rect x="${x}" y="${y}" width="${W}" height="${H}" fill="${bg}"/>
|
||
|
|
${clone.outerHTML}
|
||
|
|
<rect x="${x + 0.5}" y="${y + 0.5}" width="${W - 1}" height="${H - 1}" fill="none" stroke="${line}"/>
|
||
|
|
<line x1="${x}" y1="${ty}" x2="${x + W}" y2="${ty}" stroke="${line}"/>
|
||
|
|
<g font-family="${font}" font-size="11">
|
||
|
|
<text x="${x + 12}" y="${ty + 21}" fill="${ink}" font-weight="700">${esc(info.project)} / ${esc(info.circuit)}</text>
|
||
|
|
<text x="${x + W / 2}" y="${ty + 21}" fill="${muted}" text-anchor="middle">${esc(when)}</text>
|
||
|
|
<text x="${x + W - 12}" y="${ty + 21}" fill="${muted}" text-anchor="end">Sheet 1 / 1 · logicate.blax.at</text>
|
||
|
|
</g>
|
||
|
|
</svg>`
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Opens the print dialog for a sheet (where it can be saved as PDF), landscape. */
|
||
|
|
export function printSheet(svg: string, title: string) {
|
||
|
|
const frame = document.createElement('iframe')
|
||
|
|
frame.style.cssText = 'position:fixed;right:0;bottom:0;width:0;height:0;border:0'
|
||
|
|
document.body.appendChild(frame)
|
||
|
|
const doc = frame.contentDocument!
|
||
|
|
doc.open()
|
||
|
|
doc.write(`<!doctype html><html><head><title>${esc(title)}</title><style>
|
||
|
|
@page { size: landscape; margin: 10mm; }
|
||
|
|
html, body { margin: 0; background: #fff; }
|
||
|
|
svg { display: block; width: 100%; height: auto; max-height: 100vh; }
|
||
|
|
</style></head><body>${svg}</body></html>`)
|
||
|
|
doc.close()
|
||
|
|
setTimeout(() => {
|
||
|
|
frame.contentWindow?.focus()
|
||
|
|
frame.contentWindow?.print()
|
||
|
|
setTimeout(() => frame.remove(), 2000)
|
||
|
|
}, 150)
|
||
|
|
}
|
||
|
|
|
||
|
|
export function downloadSheet(svg: string, name: string) {
|
||
|
|
const url = URL.createObjectURL(new Blob([svg], { type: 'image/svg+xml' }))
|
||
|
|
const a = document.createElement('a')
|
||
|
|
a.href = url
|
||
|
|
a.download = `${name.replace(/[^\w\- ]+/g, '').trim() || 'circuit'}.svg`
|
||
|
|
a.click()
|
||
|
|
setTimeout(() => URL.revokeObjectURL(url), 1000)
|
||
|
|
}
|