God-Files aufgeteilt (ResourceManager/generatePlan/toWalls3d/App.tsx-Handler), Totcode entfernt (geometry-Crate, OCCT/HLR-Spike), PDF/Bild-Import ergänzt.
ResourceManager.tsx, generatePlan.ts und toWalls3d.ts sind nach Bauteil- Domäne in src/ui/resourceManager/, src/plan/generatePlan/ und src/plan/toWalls3d/ aufgeteilt (reine Verschiebung, keine Verhaltens- änderung). App.tsx verliert weitere Handler-Gruppen an eigene Hooks (useExportHandlers, useImportHandling, useKeyboardShortcuts) sowie Hit-Testing an src/viewport/planHitTest.ts. Unbenutzte Rust-geometry-Crate (durch WASM-Joins abgelöst) sowie der alte OCCT/HLR-Schnitt-Spike (hlr.ts/occt.ts, vite occt-Aliases) entfernt. Neu: PDF-Import als Rasterbild (src/io/pdfImport.ts) + gemeinsamer Bild-Asset-Helfer (src/io/imageAsset.ts) für Plan- und Layout-Bilder. tsc -b sauber, vitest 891/891 grün.
This commit is contained in:
1 parent
06c0143d32
commit
1507c2bf00
85 files changed
+12461
-11280
No files matched your search
+142
-1
@@ -12,10 +12,12 @@
|
||||
|
||||
import { jsPDF } from "jspdf";
|
||||
import "svg2pdf.js";
|
||||
import type { MasterLayout, Project } from "../model/types";
|
||||
import type { Layout, LayoutAnnotation, MasterLayout, Project } from "../model/types";
|
||||
import { generatePlan } from "../plan/generatePlan";
|
||||
import { planToPrintSvg } from "./planToPrintSvg";
|
||||
import { saveBinaryFile } from "../io/saveFile";
|
||||
import { wrapDocToLines, type SvgLine } from "../text/renderHtml";
|
||||
import { docFromText } from "../text/richText";
|
||||
import {
|
||||
findMaster,
|
||||
findSnapshot,
|
||||
@@ -77,6 +79,133 @@ 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.
|
||||
*/
|
||||
function buildAnnotationTextsSvg(
|
||||
annotations: LayoutAnnotation[],
|
||||
sheetW: number,
|
||||
sheetH: number,
|
||||
): SVGSVGElement | null {
|
||||
const texts = annotations.filter(
|
||||
(a): a is Extract<LayoutAnnotation, { kind: "text" }> => a.kind === "text",
|
||||
);
|
||||
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 doc = ann.doc ?? docFromText(ann.text);
|
||||
const lineHeight = ann.heightMm * 1.25;
|
||||
const lines: SvgLine[] = wrapDocToLines(doc, {
|
||||
x: 0,
|
||||
y: 0,
|
||||
lineHeight,
|
||||
unitPerPt: 25.4 / 72,
|
||||
basePt: (ann.heightMm / 25.4) * 72,
|
||||
color: ann.color ?? "#111111",
|
||||
...(ann.widthMm && ann.widthMm > 0 ? { wrapWidthUnits: ann.widthMm } : {}),
|
||||
});
|
||||
let y = ann.yMm;
|
||||
const el = document.createElementNS(SVG_NS, "text");
|
||||
lines.forEach((line, k) => {
|
||||
if (k > 0) y += line.lineHeight;
|
||||
const anchor = line.align === "center" ? "middle" : line.align === "right" ? "end" : "start";
|
||||
const x = !ann.widthMm
|
||||
? ann.xMm
|
||||
: line.align === "center"
|
||||
? ann.xMm + ann.widthMm / 2
|
||||
: line.align === "right"
|
||||
? ann.xMm + ann.widthMm
|
||||
: ann.xMm;
|
||||
const lineTspan = document.createElementNS(SVG_NS, "tspan");
|
||||
lineTspan.setAttribute("x", String(x));
|
||||
lineTspan.setAttribute("y", String(y));
|
||||
lineTspan.setAttribute("text-anchor", 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.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Zeichnet die Viewports + Master-Elemente EINER Seite in das jsPDF-Dokument. */
|
||||
async function renderPage(
|
||||
doc: jsPDF,
|
||||
@@ -128,6 +257,18 @@ async function renderPage(
|
||||
const tb = resolveTitleBlock(project, layout, master);
|
||||
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.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,6 +25,8 @@ import type { HatchRender, Plan, Primitive } from "../plan/generatePlan";
|
||||
import type { Vec2 } from "../model/types";
|
||||
import { buildRandomHatchRuns, zigzagPoints, motifPoints } from "../plan/glPlan/glPlanHatch";
|
||||
import { dashHasDot } from "../ui/lineSegments";
|
||||
import { wrapDocToLines, type SvgLine } from "../text/renderHtml";
|
||||
import { docFromText } from "../text/richText";
|
||||
|
||||
/** SVG-Namespace für die programmatisch erzeugten Knoten. */
|
||||
const SVG_NS = "http://www.w3.org/2000/svg";
|
||||
@@ -254,9 +256,113 @@ function appendPrimitive(
|
||||
svg.appendChild(node);
|
||||
break;
|
||||
}
|
||||
case "drawingText": {
|
||||
const node = drawingTextNode(p, map, mmPerM);
|
||||
if (node) {
|
||||
if (opacity) node.setAttribute("opacity", opacity);
|
||||
svg.appendChild(node);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "drawingImage": {
|
||||
const node = drawingImageNode(p, map, mmPerM);
|
||||
if (node) {
|
||||
if (p.opacity < 1) node.setAttribute("opacity", String(p.opacity * (p.greyed ? 0.45 : 1)));
|
||||
else if (opacity) node.setAttribute("opacity", opacity);
|
||||
svg.appendChild(node);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Modellverankerter Einzeltext (Text-Werkzeug/DXF-Import) als `<text>` mit
|
||||
* `<tspan>`-Zeilen — analog zur Bildschirm-Darstellung (PlanView.tsx
|
||||
* `case "drawingText"`), aber DOM-Knoten statt JSX. Bricht bei gesetzter
|
||||
* Spaltenbreite wortweise um (Run-Grenzen bleiben erhalten).
|
||||
*/
|
||||
function drawingTextNode(
|
||||
p: Extract<Primitive, { kind: "drawingText" }>,
|
||||
map: Mapper,
|
||||
mmPerM: number,
|
||||
): SVGTextElement | null {
|
||||
const doc = p.doc ?? docFromText(p.text, p.marks ?? {});
|
||||
const c = map(p.at);
|
||||
const fontSizeMm = p.heightM * mmPerM;
|
||||
const unitPerPt = (0.0254 / 72) * mmPerM;
|
||||
const basePt = unitPerPt > 0 ? p.heightM / unitPerPt : 0;
|
||||
const lineHeight = fontSizeMm * 1.25;
|
||||
const wrapWidthUnits = p.wrapWidth && p.wrapWidth > 0 ? p.wrapWidth * mmPerM : undefined;
|
||||
const lines: SvgLine[] = wrapDocToLines(doc, {
|
||||
x: 0,
|
||||
y: 0,
|
||||
lineHeight,
|
||||
unitPerPt,
|
||||
basePt,
|
||||
color: p.color,
|
||||
...(wrapWidthUnits ? { wrapWidthUnits } : {}),
|
||||
});
|
||||
if (lines.length === 0) return null;
|
||||
|
||||
const el = document.createElementNS(SVG_NS, "text");
|
||||
const deg = -(p.angle * 180) / Math.PI;
|
||||
if (deg) el.setAttribute("transform", `rotate(${round(deg)} ${round(c.x)} ${round(c.y)})`);
|
||||
let y = c.y;
|
||||
lines.forEach((line, k) => {
|
||||
if (k > 0) y += line.lineHeight;
|
||||
const anchor = line.align === "center" ? "middle" : line.align === "right" ? "end" : "start";
|
||||
const x = !wrapWidthUnits
|
||||
? c.x
|
||||
: line.align === "center"
|
||||
? c.x + wrapWidthUnits / 2
|
||||
: line.align === "right"
|
||||
? c.x + wrapWidthUnits
|
||||
: c.x;
|
||||
const lineTspan = document.createElementNS(SVG_NS, "tspan");
|
||||
lineTspan.setAttribute("x", String(round(x)));
|
||||
lineTspan.setAttribute("y", String(round(y)));
|
||||
lineTspan.setAttribute("text-anchor", 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(round(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);
|
||||
});
|
||||
return el;
|
||||
}
|
||||
|
||||
/** Eingebettetes Rasterbild (Data-URL) als achsparalleles `<image>`. */
|
||||
function drawingImageNode(
|
||||
p: Extract<Primitive, { kind: "drawingImage" }>,
|
||||
map: Mapper,
|
||||
mmPerM: number,
|
||||
): SVGImageElement | null {
|
||||
const w = (p.max.x - p.min.x) * mmPerM;
|
||||
const h = (p.max.y - p.min.y) * mmPerM;
|
||||
if (w <= 0 || h <= 0) return null;
|
||||
const topLeft = map({ x: p.min.x, y: p.max.y });
|
||||
const el = document.createElementNS(SVG_NS, "image");
|
||||
el.setAttribute("href", p.src);
|
||||
el.setAttribute("x", String(round(topLeft.x)));
|
||||
el.setAttribute("y", String(round(topLeft.y)));
|
||||
el.setAttribute("width", String(round(w)));
|
||||
el.setAttribute("height", String(round(h)));
|
||||
el.setAttribute("preserveAspectRatio", "none");
|
||||
return el;
|
||||
}
|
||||
|
||||
/**
|
||||
* Polygon mit Füllung/Umriss/Schraffur. Reihenfolge wie in der PlanView:
|
||||
* • pattern "none" → reine Füllung (oder „none") + Umriss.
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
import type {
|
||||
RArc,
|
||||
RFill,
|
||||
RImage,
|
||||
RLine,
|
||||
ROutline,
|
||||
RPoint,
|
||||
@@ -142,6 +143,11 @@ export function sceneToPrintSvg(scene: RScene, opts: PrintSceneOptions): PrintSc
|
||||
bg.setAttribute("fill", "#ffffff");
|
||||
svg.appendChild(bg);
|
||||
|
||||
// Eingebettete Bilder (Foto/Logo/PDF-Seite) ZWISCHEN Blatt und Vektorgeometrie
|
||||
// — dieselbe Schicht-Reihenfolge wie georeferenzierte Luftbilder im SVG-Pfad
|
||||
// (PlanView.tsx), damit darübergezeichnete Linien/Schraffuren sichtbar bleiben.
|
||||
for (const img of scene.images) appendImage(svg, img, map, k);
|
||||
|
||||
// ── Maler-Reihenfolge: Fuellungen/Umrisse/Polylinien/Linien gemeinsam nach
|
||||
// `z` sortieren (stabiler Sort => Einfuegereihenfolge bricht Gleichstaende
|
||||
// genau wie `seq` in `compile_scene`).
|
||||
@@ -319,6 +325,25 @@ function appendArc(svg: SVGSVGElement, a: RArc, map: Mapper, mmPerM: number): vo
|
||||
svg.appendChild(el);
|
||||
}
|
||||
|
||||
/** Eingebettetes Rasterbild (Data-URL) als achsparalleles `<image>`. */
|
||||
function appendImage(svg: SVGSVGElement, img: RImage, map: Mapper, mmPerM: number): void {
|
||||
const w = (img.max[0] - img.min[0]) * mmPerM;
|
||||
const h = (img.max[1] - img.min[1]) * mmPerM;
|
||||
if (w <= 0 || h <= 0) return;
|
||||
// Papier-Y zeigt nach unten -> oben-links = (min.x, max.y) im Welt-Raum.
|
||||
const [x] = map(img.min);
|
||||
const [, y] = map([img.min[0], img.max[1]]);
|
||||
const el = document.createElementNS(SVG_NS, "image");
|
||||
el.setAttribute("href", img.src);
|
||||
el.setAttribute("x", String(round(x)));
|
||||
el.setAttribute("y", String(round(y)));
|
||||
el.setAttribute("width", String(round(w)));
|
||||
el.setAttribute("height", String(round(h)));
|
||||
el.setAttribute("preserveAspectRatio", "none");
|
||||
if (img.opacity < 1) setOpacityAttr(el, "opacity", img.opacity);
|
||||
svg.appendChild(el);
|
||||
}
|
||||
|
||||
/** SVG-Schriftfamilie fuer Plan-Beschriftung (Vektor-Text im PDF). */
|
||||
const TEXT_FONT_FAMILY = "Helvetica, Arial, sans-serif";
|
||||
|
||||
|
||||
Reference in new issue
Block a user