[323] CLI: Befehl sheet (Layout-Blatt/Präsentationsblatt/Plakat als SVG, headless); Präsentationsplan-Rahmen mit 8 mm Rand (Achskreise/Gesamtmass oben nicht mehr abgeschnitten); Review-Bilder

This commit is contained in:
Claude committed 2026-11-20 18:49:39 +00:00
1 parent 596a03a397
commit b5b7d52d0d
15 files changed
+379 -32

No files matched your search

+22
View File
@@ -0,0 +1,22 @@
// Minimales DOM für den Headless-Betrieb (dossier-cli, vitest in Node): `planToPrintSvg`/`sceneToPrintSvg`
// bauen ihr SVG über `document.createElementNS`. Im Browser gibt es das; in Node stellen wir für die Dauer des
// Aufrufs ein xmldom-Dokument bereit. Keine zweite Logik — dieselben Druck-Serializer, nur ein anderes DOM.
import { DOMImplementation, XMLSerializer } from "@xmldom/xmldom";
/** Führt `fn` mit einem `document` aus; ohne vorhandenes (Browser) wird ein xmldom-Dokument ein- und danach ausgehängt. */
export async function withHeadlessDom<T>(fn: () => Promise<T> | T): Promise<T> {
const g = globalThis as { document?: unknown };
if (g.document) return fn();
g.document = new DOMImplementation().createDocument(null, null, null);
try {
return await fn();
} finally {
delete g.document;
}
}
/** SVG-Element (Browser oder xmldom) als Text. */
export function serializeSvg(el: Element | SVGElement): string {
return new XMLSerializer().serializeToString(el as never);
}
+41 -23
View File
@@ -99,7 +99,7 @@ function drawTitleBlock(
/** Schrifthöhe in mm → pt (jsPDF misst Schriftgrössen in pt). */
const MM_TO_PT = 72 / 25.4;
function clip(s: string, max: number): string {
export function clip(s: string, max: number): string {
return s.length > max ? s.slice(0, max - 1) + "…" : s;
}
@@ -108,9 +108,10 @@ function clip(s: string, max: number): string {
* derselbe Plan wie am Bildschirm (`generatePlan`, Massstab 1:1, Export-Modus), über
* `planToPrintSvg` fest aufs Papier gelegt (Ursprung oben links), ohne eigenes weisses Blatt.
*/
async function renderSheetDrawing(doc: jsPDF, project: Project, layout: Layout, widthMm: number, heightMm: number): Promise<void> {
/** Druck-SVG der Blatt-Zeichnung (`null` ohne Zeichnungen) — gemeinsam für PDF und SVG-Blatt (`layoutSvg.ts`). */
export function sheetDrawingPrint(project: Project, layout: Layout, widthMm: number, heightMm: number): SVGSVGElement | null {
const level = sheetLevelOf(project, layout.id);
if (!level || !project.drawings2d.some((d) => d.levelId === level.id)) return;
if (!level || !project.drawings2d.some((d) => d.levelId === level.id)) return null;
const a = sheetMmToModel(0, heightMm);
const b = sheetMmToModel(widthMm, 0);
const plan = generatePlan(
@@ -125,15 +126,20 @@ async function renderSheetDrawing(doc: jsPDF, project: Project, layout: Layout,
SHEET_SCALE_DENOMINATOR,
true,
);
const print = planToPrintSvg(plan, {
return planToPrintSvg(plan, {
scaleDenominator: SHEET_SCALE_DENOMINATOR,
pageWidthMm: widthMm,
pageHeightMm: heightMm,
bounds: { minX: a.x, minY: a.y, maxX: b.x, maxY: b.y },
transparent: true,
});
await prepareSvgFonts(doc, print.svg);
await doc.svg(print.svg, { x: 0, y: 0, width: widthMm, height: heightMm });
}).svg;
}
async function renderSheetDrawing(doc: jsPDF, project: Project, layout: Layout, widthMm: number, heightMm: number): Promise<void> {
const svg = sheetDrawingPrint(project, layout, widthMm, heightMm);
if (!svg) return;
await prepareSvgFonts(doc, svg);
await doc.svg(svg, { x: 0, y: 0, width: widthMm, height: heightMm });
}
/** Optionen des Seiten-Aufbaus (Publikationssätze, [79]/[80]). */
@@ -209,6 +215,31 @@ function drawPosterHeaderPdf(doc: jsPDF, project: Project, layout: Layout, maste
}
}
/**
* Plan-Quelle EINES Viewports (Ausschnitt, Massstab, Vorlage, Varianten) — gemeinsam für PDF ({@link renderPage}) und
* SVG-Blatt (`layoutSvg.ts`), damit beide dieselben Pläne zeigen. `null`, wenn der Ausschnitt fehlt.
*/
export function viewportPlanSource(project: Project, vp: Layout["viewports"][number], scaleOverride?: number) {
const snap = findSnapshot(project, vp.snapshotId);
if (!snap) return null;
const codes = visibleCodesFromSnapshot(snap);
// Massstab zuerst: er geht in die Plan-Erzeugung ein (Bemassung in Papier-mm, Strichstärken
// nach Massstab) — wie ViewportPlan in LayoutSheet.tsx, sonst wiche das PDF von der Vorschau ab.
const scaleN = scaleOverride ?? resolveViewportScale(vp, snap);
// Geschoss/Zeichnung, Schnitt oder Ansicht ([115]) — wie ViewportPlanSvg, im Druck-Modus.
// [33] Override-Regeln des Ausschnitts bzw. seiner verknüpften Ansichtsvorlage (wie am Bildschirm).
const ruleIds = snapshotOverrideRuleIds(project, snap);
const template = snapshotTemplate(project, snap);
// [266] 3D-Ausschnitt: 3D-Dokument aus dem Kern (`viewportSnapshotPlan`).
// [272] Variantenkombination des Ausschnitts/Viewports.
const dopts = viewportDesignOptions(project, snap, vp);
const planOf = async (visible: Set<string>): Promise<Plan> =>
isDoc3dSnapshot(snap) ? viewportSnapshotPlan(project, snap, visible, scaleN, true, vp) : viewportLevelPlan(project, snap.activeLevelId, visible, snap.detail, scaleN, true, snapshotMono(project, snap), ruleIds, template, snap.modelViewOptionId, dopts);
// [208] Eigene Bildmitte (Plakat): Ausschnitt um die Mitte; was darüber hinausgeht, schneidet der Rahmen ab.
const centered = viewportCenterBounds(vp, scaleN);
return { snap, codes, scaleN, planOf, centered };
}
/** Zeichnet die Viewports + Master-Elemente EINER Seite in das jsPDF-Dokument. */
async function renderPage(
doc: jsPDF,
@@ -221,23 +252,10 @@ async function renderPage(
// Echte Viewport-Pläne (wie ViewportPlan in LayoutSheet.tsx).
for (const vp of layout.viewports) {
const snap = findSnapshot(project, vp.snapshotId);
if (!snap) continue;
const src = viewportPlanSource(project, vp, ctx.scaleOverride);
if (!src) continue;
try {
const codes = visibleCodesFromSnapshot(snap);
// Massstab zuerst: er geht in die Plan-Erzeugung ein (Bemassung in Papier-mm, Strichstärken
// nach Massstab) — wie ViewportPlan in LayoutSheet.tsx, sonst wiche das PDF von der Vorschau ab.
const scaleN = ctx.scaleOverride ?? resolveViewportScale(vp, snap);
// Geschoss/Zeichnung, Schnitt oder Ansicht ([115]) — wie ViewportPlanSvg, im Druck-Modus.
// [33] Override-Regeln des Ausschnitts bzw. seiner verknüpften Ansichtsvorlage (wie am Bildschirm).
const ruleIds = snapshotOverrideRuleIds(project, snap);
const template = snapshotTemplate(project, snap);
// [266] 3D-Ausschnitt: 3D-Dokument aus dem Kern (`viewportSnapshotPlan`).
// [272] Variantenkombination des Ausschnitts/Viewports.
const dopts = viewportDesignOptions(project, snap, vp);
const planOf = (visible: Set<string>) => (isDoc3dSnapshot(snap) ? viewportSnapshotPlan(project, snap, visible, scaleN, true, vp) : viewportLevelPlan(project, snap.activeLevelId, visible, snap.detail, scaleN, true, snapshotMono(project, snap), ruleIds, template, snap.modelViewOptionId, dopts));
// [208] Eigene Bildmitte (Plakat): Ausschnitt um die Mitte; was darüber hinausgeht, schneidet der Rahmen ab.
const centered = viewportCenterBounds(vp, scaleN);
const { codes, scaleN, planOf, centered } = src;
const draw = async (plan: Plan, transparent: boolean, bounds?: Plan["bounds"]) => {
const print = planToPrintSvg(plan, { scaleDenominator: scaleN, pageWidthMm: vp.widthMm, pageHeightMm: vp.heightMm, transparent, bounds: centered ?? bounds });
await prepareSvgFonts(doc, print.svg);
+163
View File
@@ -0,0 +1,163 @@
// Layout-Blatt als SVG (headless, `dossier-cli sheet`): dieselben Pläne wie der PDF-Export (`layoutPdf.ts`:
// `viewportPlanSource`, `sheetDrawingPrint`, Druck-Serializer `planToPrintSvg`) und dieselben Blatt-Elemente
// (Rahmen, Schriftfeld bzw. Plakat-Kopfzeile, Massstabsangaben, Legenden, Bilder, Tabellen) — nur als SVG-Text statt jsPDF.
// Vektor-PDF braucht jsPDF + svg2pdf und damit ein echtes DOM (Browser/Tauri): hier nicht.
import type { Layout, MasterLayout, Project } from "../model/types";
import { t } from "../i18n/translate";
import { effectiveSheetSizeMm, findMaster, resolveTitleBlock, type ResolvedTitleBlock } from "../model/layoutModel";
import { imageGeometry } from "../model/layoutImages";
import { posterHeaderItems, viewportScaleLabel, viewportVariantLabel } from "../model/posterTemplates";
import { legendPlan, legendPrintSvg } from "./legendSvg";
import { clip, sheetDrawingPrint, viewportPlanSource } from "./layoutPdf";
import { planToPrintSvg } from "./planToPrintSvg";
import { sheetTables, PDF_TABLE_FONT } from "./sheetTable";
import { serializeSvg, withHeadlessDom } from "./headlessDom";
import type { PresentationLabels } from "../plan/presentationSheet";
const INK = "#111111";
const FONT = "Inter, Helvetica, Arial, sans-serif";
const esc = (s: string): string => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
const num = (v: number): string => String(Math.round(v * 1000) / 1000);
/** Beschriftungen des Präsentationsblatts (de/en über `t`), gemeinsam für App und CLI. */
export function presentationSheetLabels(format: string): PresentationLabels {
return {
sheetName: t("presentation.sheetName"),
masterName: t("presentation.masterName", { format }),
north: t("presentation.north"),
northGroup: t("presentation.northGroup"),
scaleBarGroup: t("presentation.scaleBarGroup"),
situation: t("presentation.situation"),
tableAreas: t("presentation.table.areas"),
tableSiteProof: t("presentation.table.siteProof"),
tableUnits: t("presentation.table.units"),
imagePlaceholder: t("presentation.image.placeholder"),
imageHint: t("presentation.image.hint"),
axo: t("presentation.axo"),
};
}
function textEl(x: number, y: number, s: string, size: number, o: { bold?: boolean; italic?: boolean; anchor?: "start" | "middle" | "end"; fill?: string; family?: string } = {}): string {
const a = [`x="${num(x)}"`, `y="${num(y)}"`, `font-size="${num(size)}"`, `font-family="${o.family ?? FONT}"`, `fill="${o.fill ?? INK}"`];
if (o.bold) a.push('font-weight="bold"');
if (o.italic) a.push('font-style="italic"');
if (o.anchor && o.anchor !== "start") a.push(`text-anchor="${o.anchor}"`);
return `<text ${a.join(" ")}>${esc(s)}</text>`;
}
/** Schriftfeld unten rechts (Mass wie `drawTitleBlock` im PDF). */
function titleBlockSvg(w: number, h: number, tb: ResolvedTitleBlock, layout: Layout): string {
const bw = 70;
const bh = 30;
const x = w - bw - 6;
const y = h - bh - 6;
const fmt = `${layout.paper.toUpperCase()} ${layout.orientation === "landscape" ? "quer" : "hoch"}`;
const o: string[] = [
`<rect x="${x}" y="${y}" width="${bw}" height="${bh}" fill="#fff" stroke="${INK}" stroke-width="0.35"/>`,
`<line x1="${x}" y1="${y + 8}" x2="${x + bw}" y2="${y + 8}" stroke="${INK}" stroke-width="0.2"/>`,
`<line x1="${x}" y1="${y + bh - 7}" x2="${x + bw}" y2="${y + bh - 7}" stroke="${INK}" stroke-width="0.2"/>`,
textEl(x + 3, y + 5.6, clip(tb.projectName, 28), 3.2, { bold: true }),
textEl(x + bw - 3, y + 5.6, tb.planNumber, 3.2, { bold: true, anchor: "end" }),
textEl(x + 3, y + 13, clip(tb.sheetName, 36), 2.8),
];
if (tb.revision) o.push(textEl(x + bw - 3, y + 13, `${t("layouts.revision.short")} ${tb.revision}`, 2.5, { anchor: "end" }));
if (tb.author) o.push(textEl(x + 3, y + 18, clip(tb.author, 40), 2.5, { fill: "#444" }));
o.push(textEl(x + 3, y + bh - 2.4, tb.scale, 2.8), textEl(x + bw - 3, y + bh - 2.4, `${tb.date} · ${fmt}`, 2.5, { anchor: "end" }));
return o.join("");
}
function posterHeaderSvg(project: Project, layout: Layout, master: MasterLayout, w: number, h: number): string {
return posterHeaderItems(project, layout, master, { w, h })
.map((it) =>
it.kind === "rect"
? `<rect x="${num(it.x)}" y="${num(it.y)}" width="${num(it.w)}" height="${num(it.h)}" fill="none" stroke="${INK}" stroke-width="${num(it.stroke)}"/>`
: textEl(it.x, it.y, it.text, it.sizeMm, { bold: it.bold, anchor: it.align === "left" ? "start" : it.align === "center" ? "middle" : "end" }),
)
.join("");
}
/** Eingebettetes SVG-Element an Blatt-Position `x,y` (verschachteltes `<svg>` schneidet am eigenen Rahmen ab). */
function nested(svg: SVGSVGElement, x: number, y: number, w: number, h: number): string {
svg.setAttribute("x", num(x));
svg.setAttribute("y", num(y));
svg.setAttribute("width", num(w));
svg.setAttribute("height", num(h));
svg.setAttribute("overflow", "hidden");
return serializeSvg(svg);
}
export interface LayoutSvgOptions {
/** Massstab (1:N) für alle Viewports statt dem eigenen. */
scaleOverride?: number;
}
/**
* Das Blatt `layout` als SVG-Text (Papier-mm, Ursprung oben links): Viewports, Bilder, Massstabsangaben, Legenden,
* Rahmen + Schriftfeld/Kopfzeile, Tabellen, Blatt-Zeichnung — Stapelung wie im PDF. Ein Viewport, der nicht rendert,
* lässt nur sich selbst weg (Meldung in `warnings`).
*/
export async function buildLayoutSvg(project: Project, layout: Layout, opts: LayoutSvgOptions = {}): Promise<{ svg: string; widthMm: number; heightMm: number; warnings: string[] }> {
const master = findMaster(project, layout);
const { w, h } = effectiveSheetSizeMm(master ?? layout);
const warnings: string[] = [];
const parts: string[] = [];
await withHeadlessDom(async () => {
for (const vp of layout.viewports) {
const src = viewportPlanSource(project, vp, opts.scaleOverride);
if (!src) {
warnings.push(`Viewport ${vp.id}: Ausschnitt ${vp.snapshotId} fehlt`);
continue;
}
try {
const plan = await src.planOf(src.codes);
const print = planToPrintSvg(plan, { scaleDenominator: src.scaleN, pageWidthMm: vp.widthMm, pageHeightMm: vp.heightMm, transparent: false, bounds: src.centered ?? undefined });
parts.push(nested(print.svg, vp.xMm, vp.yMm, vp.widthMm, vp.heightMm));
} catch (e) {
warnings.push(`Viewport ${vp.id}: ${e instanceof Error ? e.message : String(e)}`);
}
}
for (const img of layout.images ?? []) {
if (!/^data:image\/(png|jpeg);base64,/.test(img.src) || img.widthMm <= 0 || img.heightMm <= 0) continue;
const g = imageGeometry(img);
const id = `clip-${esc(img.id)}`;
parts.push(`<clipPath id="${id}"><rect x="${num(g.clip.x)}" y="${num(g.clip.y)}" width="${num(g.clip.w)}" height="${num(g.clip.h)}"/></clipPath><image clip-path="url(#${id})" href="${img.src}" x="${num(g.draw.x)}" y="${num(g.draw.y)}" width="${num(g.draw.w)}" height="${num(g.draw.h)}" preserveAspectRatio="none"/>`);
}
if (master?.viewportScaleLabels || project.designOptionSets?.length) {
for (const vp of layout.viewports) {
const l = master?.viewportScaleLabels ? viewportScaleLabel(project, vp, { w, h }, opts.scaleOverride) : viewportVariantLabel(project, vp, { w, h });
if (l) parts.push(textEl(l.x, l.y, l.text, l.sizeMm));
}
}
for (const lg of layout.legends ?? []) {
try {
const lp = legendPlan(project, layout, lg);
if (lp) parts.push(nested(legendPrintSvg(lp), lp.xMm, lp.yMm, lp.widthMm, lp.heightMm));
} catch (e) {
warnings.push(`Legende ${lg.id}: ${e instanceof Error ? e.message : String(e)}`);
}
}
if (master) {
if (master.border ?? true) parts.push(`<rect x="5" y="5" width="${w - 10}" height="${h - 10}" fill="none" stroke="${INK}" stroke-width="0.35"/>`);
parts.push(master.posterHeader ? posterHeaderSvg(project, layout, master, w, h) : titleBlockSvg(w, h, resolveTitleBlock(project, layout, master), layout));
}
try {
for (const tb of sheetTables(project, layout.id)) {
for (const p of tb.prims) {
if (p.k === "line") parts.push(`<line x1="${num(p.x1)}" y1="${num(p.y1)}" x2="${num(p.x2)}" y2="${num(p.y2)}" stroke="${INK}" stroke-width="${num(p.w)}"/>`);
else parts.push(textEl(p.x, p.y, p.s, p.size, { bold: p.bold, italic: p.italic, family: `'${PDF_TABLE_FONT[p.font]}', ${FONT}`, anchor: p.align === "r" ? "end" : p.align === "c" ? "middle" : "start" }));
}
}
} catch (e) {
warnings.push(`Tabellen: ${e instanceof Error ? e.message : String(e)}`);
}
try {
const drawing = sheetDrawingPrint(project, layout, w, h);
if (drawing) parts.push(nested(drawing, 0, 0, w, h));
} catch (e) {
warnings.push(`Blatt-Zeichnung: ${e instanceof Error ? e.message : String(e)}`);
}
});
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${w}mm" height="${h}mm" viewBox="0 0 ${w} ${h}"><rect width="${w}" height="${h}" fill="#fff"/>${parts.join("")}</svg>\n`;
return { svg, widthMm: w, heightMm: h, warnings };
}