import { layout } from './stores/layout.svelte.js';
import { isEpcType, isInductionType, isSpurType, isCurvedType, getSymbolGroup, EPC_CONFIG, INDUCTION_CONFIG, getCurveGeometry } from './symbols.js';
import { deserializeSymbol } from './serialization.js';
import type { PlacedSymbol } from './types.js';
function downloadBlob(blob: Blob, filename: string) {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}
/** Serialize child elements of an SVG, stripping xmlns added by XMLSerializer */
function serializeChildren(parent: Element): string {
return Array.from(parent.children)
.map(el => new XMLSerializer().serializeToString(el)
.replace(/ xmlns="http:\/\/www\.w3\.org\/2000\/svg"/g, ''))
.join('\n ');
}
export async function exportSVG() {
const lines: string[] = [
'',
`');
downloadBlob(new Blob([lines.join('\n')], { type: 'image/svg+xml' }), 'test_view.svg');
}
/** Emit EPC symbol — polyline + icon + right box, wrapped in with id/label */
async function emitEpc(lines: string[], sym: PlacedSymbol, label: string, outerTransform: string) {
const waypoints = sym.epcWaypoints || EPC_CONFIG.defaultWaypoints;
const ox = sym.x;
const oy = sym.y;
const parts: string[] = [];
const tAttr = outerTransform ? ` transform="${outerTransform}"` : '';
// Polyline
if (waypoints.length >= 2) {
const points = waypoints.map(wp => `${ox + wp.x},${oy + wp.y}`).join(' ');
parts.push(` `);
}
if (waypoints.length >= 2) {
// Left icon
const lb = EPC_CONFIG.leftBox;
const p0x = ox + waypoints[0].x, p0y = oy + waypoints[0].y;
const p1x = ox + waypoints[1].x, p1y = oy + waypoints[1].y;
const lAngle = Math.atan2(p1y - p0y, p1x - p0x) * 180 / Math.PI;
try {
const svgText = await (await fetch(EPC_CONFIG.iconFile)).text();
const doc = new DOMParser().parseFromString(svgText, 'image/svg+xml');
const svgEl = doc.documentElement;
const vb = svgEl.getAttribute('viewBox');
const [vbX, vbY, vbW, vbH] = vb ? vb.split(/[\s,]+/).map(Number) : [0, 0, lb.w, lb.h];
const sx = lb.w / vbW;
const sy = lb.h / vbH;
const iconTransform = `translate(${p0x},${p0y}) rotate(${lAngle.toFixed(2)}) translate(${-lb.w},${-lb.h / 2}) scale(${sx.toFixed(6)},${sy.toFixed(6)}) translate(${-vbX},${-vbY})`;
const children = Array.from(svgEl.children);
if (children.length === 1 && children[0].tagName === 'g') {
const g = children[0];
const gT = g.getAttribute('transform') || '';
const fullT = gT ? `${iconTransform} ${gT}` : iconTransform;
parts.push(` `);
parts.push(` ${serializeChildren(g)}`);
parts.push(` `);
} else {
parts.push(` `);
parts.push(` ${serializeChildren(svgEl)}`);
parts.push(` `);
}
} catch {
parts.push(` `);
}
// Right box
const last = waypoints[waypoints.length - 1];
const prev = waypoints[waypoints.length - 2];
const plx = ox + last.x, ply = oy + last.y;
const ppx = ox + prev.x, ppy = oy + prev.y;
const rAngle = Math.atan2(ply - ppy, plx - ppx) * 180 / Math.PI;
const rb = EPC_CONFIG.rightBox;
parts.push(` `);
}
lines.push(` `);
lines.push(parts.join('\n'));
lines.push(' ');
}
export function loadLayoutJSON(file: File): Promise {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (ev) => {
try {
const data = JSON.parse(ev.target!.result as string);
layout.pushUndo();
if (data.gridSize) layout.gridSize = data.gridSize;
if (data.minSpacing) layout.minSpacing = data.minSpacing;
layout.symbols = [];
layout.nextId = 1;
for (const s of data.symbols) {
layout.symbols.push(deserializeSymbol(s, layout.nextId++));
}
layout.markDirty();
layout.saveMcmState();
resolve();
} catch (err) {
reject(err);
}
};
reader.readAsText(file);
});
}