[93] Merge: Zeichnen auf dem Layout-Blatt = Zeichnen im Grundriss (Blatt-Ebene mit normalen 2D-Zeichnungen, Format 3 migriert Anmerkungen)
Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
43 files changed
+1245
-2756
No files matched your search
+37
-114
@@ -12,17 +12,19 @@
|
||||
|
||||
import { jsPDF } from "jspdf";
|
||||
import "svg2pdf.js";
|
||||
import type { Layout, LayoutAnnotation, MasterLayout, Project } from "../model/types";
|
||||
import type { Layout, MasterLayout, Project } from "../model/types";
|
||||
import { generatePlan } from "../plan/generatePlan";
|
||||
import { planToPrintSvg } from "./planToPrintSvg";
|
||||
import { annotationTextLines } from "../text/renderHtml";
|
||||
import { docFromText } from "../text/richText";
|
||||
import { collectVisibleCodes } from "../model/types";
|
||||
import {
|
||||
findMaster,
|
||||
findSnapshot,
|
||||
folderPdfPages,
|
||||
resolveTitleBlock,
|
||||
resolveViewportScale,
|
||||
SHEET_SCALE_DENOMINATOR,
|
||||
sheetLevelOf,
|
||||
sheetMmToModel,
|
||||
visibleCodesFromSnapshot,
|
||||
type FolderPdfPage,
|
||||
type ResolvedTitleBlock,
|
||||
@@ -79,110 +81,36 @@ function clip(s: string, max: number): string {
|
||||
return s.length > max ? s.slice(0, max - 1) + "…" : s;
|
||||
}
|
||||
|
||||
/** "#rrggbb"/"#rgb" → [r,g,b] (0..255); ungültige Eingabe fällt auf Tinte zurück. */
|
||||
function hexToRgb(hex: string): [number, number, number] {
|
||||
const h = hex.replace("#", "").trim();
|
||||
const full = h.length === 3 ? h.split("").map((c) => c + c).join("") : h;
|
||||
const n = parseInt(full, 16);
|
||||
if (full.length !== 6 || !Number.isFinite(n)) return [17, 17, 17];
|
||||
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
||||
}
|
||||
|
||||
/** Bildformat aus dem MIME-Teil der Data-URL ableiten (jsPDF `addImage` braucht es explizit). */
|
||||
function imageFormatFromDataUrl(src: string): string {
|
||||
const ext = (/^data:image\/(\w+)/i.exec(src)?.[1] ?? "png").toLowerCase();
|
||||
if (ext === "jpg" || ext === "jpeg") return "JPEG";
|
||||
if (ext === "webp") return "WEBP";
|
||||
return "PNG";
|
||||
}
|
||||
|
||||
const SVG_NS = "http://www.w3.org/2000/svg";
|
||||
|
||||
/**
|
||||
* Baut EIN Mini-SVG (Blattgrösse) mit allen Text-Markups einer Seite — reiche
|
||||
* Docs (mehrere Absätze/gemischte Formatierung) + Wortumbruch bei gesetzter
|
||||
* Rahmenbreite, exakt wie `LayoutSheet.tsx`s `AnnotationSvg`. Ein gemeinsamer
|
||||
* `doc.svg()`-Aufruf statt einem je Annotation (einfacher, ein Layout-Pass).
|
||||
* `null`, wenn die Seite keine Text-Markups trägt.
|
||||
* Die 2D-Zeichnung des Blatts (Blatt-Ebene, `DrawingLevel.layoutId`) über Viewports und Master:
|
||||
* derselbe Plan wie am Bildschirm (`generatePlan`, Massstab 1:1, Export-Modus), über
|
||||
* `planToPrintSvg` fest aufs Papier gelegt (Ursprung oben links), ohne eigenes weisses Blatt.
|
||||
*/
|
||||
function buildAnnotationTextsSvg(
|
||||
annotations: LayoutAnnotation[],
|
||||
sheetW: number,
|
||||
sheetH: number,
|
||||
): SVGSVGElement | null {
|
||||
const texts = annotations.filter(
|
||||
(a): a is Extract<LayoutAnnotation, { kind: "text" }> => a.kind === "text",
|
||||
async function renderSheetDrawing(doc: jsPDF, project: Project, layout: Layout, widthMm: number, heightMm: number): Promise<void> {
|
||||
const level = sheetLevelOf(project, layout.id);
|
||||
if (!level || !project.drawings2d.some((d) => d.levelId === level.id)) return;
|
||||
const a = sheetMmToModel(0, heightMm);
|
||||
const b = sheetMmToModel(widthMm, 0);
|
||||
const plan = generatePlan(
|
||||
project,
|
||||
level.id,
|
||||
collectVisibleCodes(project.layers),
|
||||
undefined,
|
||||
"fein",
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
SHEET_SCALE_DENOMINATOR,
|
||||
true,
|
||||
);
|
||||
if (texts.length === 0) return null;
|
||||
|
||||
const svg = document.createElementNS(SVG_NS, "svg") as SVGSVGElement;
|
||||
svg.setAttribute("xmlns", SVG_NS);
|
||||
svg.setAttribute("width", String(sheetW));
|
||||
svg.setAttribute("height", String(sheetH));
|
||||
svg.setAttribute("viewBox", `0 0 ${sheetW} ${sheetH}`);
|
||||
|
||||
for (const ann of texts) {
|
||||
const el = document.createElementNS(SVG_NS, "text");
|
||||
for (const line of annotationTextLines(ann, ann.doc ?? docFromText(ann.text))) {
|
||||
const lineTspan = document.createElementNS(SVG_NS, "tspan");
|
||||
lineTspan.setAttribute("x", String(line.x));
|
||||
lineTspan.setAttribute("y", String(line.y));
|
||||
lineTspan.setAttribute("text-anchor", line.anchor);
|
||||
if (line.tspans.length === 0) {
|
||||
lineTspan.textContent = "";
|
||||
} else {
|
||||
for (const ts of line.tspans) {
|
||||
const run = document.createElementNS(SVG_NS, "tspan");
|
||||
run.setAttribute("font-size", String(ts.fontSize));
|
||||
run.setAttribute("fill", ts.fill);
|
||||
if (ts.fontWeight) run.setAttribute("font-weight", ts.fontWeight);
|
||||
if (ts.fontStyle) run.setAttribute("font-style", ts.fontStyle);
|
||||
run.setAttribute("font-family", ts.fontFamily ?? "Helvetica, Arial, sans-serif");
|
||||
if (ts.textDecoration) run.setAttribute("text-decoration", ts.textDecoration);
|
||||
run.textContent = ts.text;
|
||||
lineTspan.appendChild(run);
|
||||
}
|
||||
}
|
||||
el.appendChild(lineTspan);
|
||||
}
|
||||
svg.appendChild(el);
|
||||
}
|
||||
return svg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeichnet die 2D-Markups (Linie/Rechteck/Bild) EINER Seite direkt via jsPDF-
|
||||
* Vektorprimitive — dieselbe Schicht wie `LayoutSheet.tsx`s Annotations-SVG,
|
||||
* hier aber ÜBER Viewports UND Master gezeichnet (Aufrufreihenfolge in
|
||||
* `renderPage`), analog zur Zeichenreihenfolge im In-Viewport-Editor.
|
||||
*/
|
||||
function drawAnnotationShapes(doc: jsPDF, layout: Layout): void {
|
||||
for (const ann of layout.annotations ?? []) {
|
||||
if (ann.kind === "line") {
|
||||
const [r, g, b] = hexToRgb(ann.color ?? "#111111");
|
||||
doc.setDrawColor(r, g, b);
|
||||
doc.setLineWidth(ann.weightMm ?? 0.25);
|
||||
doc.line(ann.x1Mm, ann.y1Mm, ann.x2Mm, ann.y2Mm);
|
||||
} else if (ann.kind === "rect") {
|
||||
const [r, g, b] = hexToRgb(ann.color ?? "#111111");
|
||||
doc.setDrawColor(r, g, b);
|
||||
doc.setLineWidth(ann.weightMm ?? 0.25);
|
||||
doc.rect(ann.xMm, ann.yMm, ann.widthMm, ann.heightMm);
|
||||
} else if (ann.kind === "image" && ann.widthMm > 0 && ann.heightMm > 0 && ann.src) {
|
||||
try {
|
||||
doc.addImage(
|
||||
ann.src,
|
||||
imageFormatFromDataUrl(ann.src),
|
||||
ann.xMm,
|
||||
ann.yMm,
|
||||
ann.widthMm,
|
||||
ann.heightMm,
|
||||
);
|
||||
} catch {
|
||||
// Defekte/leere Data-URL überspringt nur dieses Bild, nicht die Seite.
|
||||
}
|
||||
}
|
||||
}
|
||||
const print = 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 doc.svg(print.svg, { x: 0, y: 0, width: widthMm, height: heightMm });
|
||||
}
|
||||
|
||||
/** Zeichnet die Viewports + Master-Elemente EINER Seite in das jsPDF-Dokument. */
|
||||
@@ -242,16 +170,11 @@ async function renderPage(
|
||||
drawTitleBlock(doc, widthMm, heightMm, tb, layout.paper, layout.orientation);
|
||||
}
|
||||
|
||||
// 2D-Markups (Linie/Rechteck/Bild/Text) — ÜBER Viewports + Master, wie im
|
||||
// In-Viewport-Editor (LayoutSheet.tsx). Fehlte bisher komplett im Export.
|
||||
drawAnnotationShapes(doc, layout);
|
||||
const textsSvg = buildAnnotationTextsSvg(layout.annotations ?? [], widthMm, heightMm);
|
||||
if (textsSvg) {
|
||||
try {
|
||||
await doc.svg(textsSvg, { x: 0, y: 0, width: widthMm, height: heightMm });
|
||||
} catch {
|
||||
// Rich-Text-Rendering eines Markups darf die Seite nicht killen.
|
||||
}
|
||||
// 2D-Zeichnung des Blatts — ÜBER Viewports + Master, wie am Bildschirm.
|
||||
try {
|
||||
await renderSheetDrawing(doc, project, layout, widthMm, heightMm);
|
||||
} catch {
|
||||
// Eine Blatt-Zeichnung, die nicht rendert, darf die Seite nicht killen.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
// Blatt-Zeichnung im Layout-PDF: die Blatt-Ebene eines Layouts (`DrawingLevel.layoutId`, Papier 1:1 in
|
||||
// Metern) liegt über `planToPrintSvg` mit festem Bereich genau auf dem Papier — Ursprung oben links,
|
||||
// Strichstärken unverändert (auch mit `scaleLineWeights`), ohne weisses Blatt dahinter.
|
||||
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { generatePlan } from "../plan/generatePlan";
|
||||
import { sampleProject } from "../model/sampleProject";
|
||||
import { collectVisibleCodes, type Project } from "../model/types";
|
||||
import { createSheetLevel, sheetMmToModel, SHEET_SCALE_DENOMINATOR } from "../model/layoutModel";
|
||||
import { planToPrintSvg } from "./planToPrintSvg";
|
||||
|
||||
interface El {
|
||||
tag: string;
|
||||
attrs: Map<string, string>;
|
||||
kids: El[];
|
||||
textContent: string;
|
||||
setAttribute(k: string, v: unknown): void;
|
||||
getAttribute(k: string): string | null;
|
||||
appendChild(c: El): El;
|
||||
insertBefore(n: El, r: El | null): El;
|
||||
readonly firstChild: El | null;
|
||||
}
|
||||
|
||||
function el(tag: string): El {
|
||||
return {
|
||||
tag,
|
||||
attrs: new Map(),
|
||||
kids: [],
|
||||
textContent: "",
|
||||
setAttribute(k, v) {
|
||||
this.attrs.set(k, String(v));
|
||||
},
|
||||
getAttribute(k) {
|
||||
return this.attrs.get(k) ?? null;
|
||||
},
|
||||
appendChild(c) {
|
||||
this.kids.push(c);
|
||||
return c;
|
||||
},
|
||||
insertBefore(n) {
|
||||
this.kids.unshift(n);
|
||||
return n;
|
||||
},
|
||||
get firstChild() {
|
||||
return this.kids[0] ?? null;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const all = (e: El): El[] => [e, ...e.kids.flatMap(all)];
|
||||
|
||||
const sheet = createSheetLevel("blatt", { id: "L1", name: "Blatt 1" });
|
||||
const a = sheetMmToModel(10, 20);
|
||||
const b = sheetMmToModel(110, 20);
|
||||
const project: Project = {
|
||||
...sampleProject,
|
||||
scaleLineWeights: true,
|
||||
drawingLevels: [...sampleProject.drawingLevels, sheet],
|
||||
drawings2d: [
|
||||
...sampleProject.drawings2d,
|
||||
{ id: "s1", type: "drawing2d", levelId: "blatt", categoryCode: "20", geom: { shape: "line", a, b }, weightMm: 0.5, color: "#ff0000" },
|
||||
] as Project["drawings2d"],
|
||||
};
|
||||
|
||||
describe("Druck-SVG der Blatt-Ebene", () => {
|
||||
let prev: unknown;
|
||||
beforeAll(() => {
|
||||
const g = globalThis as { document?: unknown };
|
||||
prev = g.document;
|
||||
g.document = { createElementNS: (_: string, tag: string) => el(tag) };
|
||||
});
|
||||
afterAll(() => {
|
||||
(globalThis as { document?: unknown }).document = prev;
|
||||
});
|
||||
|
||||
it("liegt mm-genau auf dem Papier, Stärke 1:1, ohne weisses Blatt", () => {
|
||||
const plan = generatePlan(project, "blatt", collectVisibleCodes(project.layers), undefined, "fein", false, false, false, SHEET_SCALE_DENOMINATOR, true);
|
||||
// Nur die Blatt-Zeichnung: keine Wände des Geschosses, keine projektglobalen Zutaten.
|
||||
expect(plan.primitives.every((p) => !("wallId" in p && p.wallId))).toBe(true);
|
||||
const lo = sheetMmToModel(0, 297);
|
||||
const hi = sheetMmToModel(210, 0);
|
||||
const print = planToPrintSvg(plan, {
|
||||
scaleDenominator: SHEET_SCALE_DENOMINATOR,
|
||||
pageWidthMm: 210,
|
||||
pageHeightMm: 297,
|
||||
bounds: { minX: lo.x, minY: lo.y, maxX: hi.x, maxY: hi.y },
|
||||
transparent: true,
|
||||
});
|
||||
const svg = print.svg as unknown as El;
|
||||
expect(svg.kids.some((k) => k.tag === "rect" && k.getAttribute("fill") === "#ffffff")).toBe(false);
|
||||
const red = all(svg).filter((e) => e.getAttribute("stroke") === "#ff0000");
|
||||
expect(red.length).toBeGreaterThan(0);
|
||||
const geo = red.map((e) => e.getAttribute("points") ?? `${e.getAttribute("x1")},${e.getAttribute("y1")} ${e.getAttribute("x2")},${e.getAttribute("y2")}`).join(" ");
|
||||
const nums = geo.split(/[\s,]+/).map(Number);
|
||||
expect(nums).toHaveLength(4);
|
||||
[10, 20, 110, 20].forEach((v, i) => expect(nums[i]).toBeCloseTo(v, 6));
|
||||
expect(Number(red[0].getAttribute("stroke-width"))).toBeCloseTo(0.5, 9);
|
||||
});
|
||||
});
|
||||
@@ -100,6 +100,13 @@ export interface PrintSvgOptions {
|
||||
pageHeightMm: number;
|
||||
/** Rand um den Plan-Inhalt in mm (weisser Blattrand). */
|
||||
marginMm?: number;
|
||||
/**
|
||||
* Fester Modell-Bereich, der das Blatt füllt (statt den Inhalt zu zentrieren) — die Blatt-Ebene
|
||||
* eines Layouts liegt so genau auf dem Papier (Ursprung oben links, s. `DrawingLevel.layoutId`).
|
||||
*/
|
||||
bounds?: { minX: number; minY: number; maxX: number; maxY: number };
|
||||
/** Ohne weisses Blatt dahinter (als Schicht über anderem Inhalt). */
|
||||
transparent?: boolean;
|
||||
}
|
||||
|
||||
/** Modell-Meter → Papier-Millimeter im Massstab 1:N. */
|
||||
@@ -120,7 +127,7 @@ export function planToPrintSvg(plan: Plan, opts: PrintSvgOptions): PrintSvg {
|
||||
// bewusst, ohne ihn aktuell für einen Versatz zu nutzen (zentrierte Lage).
|
||||
void (opts.marginMm ?? 10);
|
||||
|
||||
const b = plan.bounds;
|
||||
const b = opts.bounds ?? plan.bounds;
|
||||
// Inhalts-Maße in mm (massstäblich), unverschoben.
|
||||
const contentWmm = (b.maxX - b.minX) * k;
|
||||
const contentHmm = (b.maxY - b.minY) * k;
|
||||
@@ -151,7 +158,7 @@ export function planToPrintSvg(plan: Plan, opts: PrintSvgOptions): PrintSvg {
|
||||
bg.setAttribute("width", String(opts.pageWidthMm));
|
||||
bg.setAttribute("height", String(opts.pageHeightMm));
|
||||
bg.setAttribute("fill", "#ffffff");
|
||||
svg.appendChild(bg);
|
||||
if (!opts.transparent) svg.appendChild(bg);
|
||||
|
||||
// Jedes Primitiv in mm-Knoten übersetzen (Kern, Anweisungsliste s. `plan_print_svg.rs`).
|
||||
const o = Float64Array.of(n, opts.pageWidthMm, opts.pageHeightMm, b.minX, b.minY, b.maxX, b.maxY);
|
||||
|
||||
Reference in new issue
Block a user