[89] Layout-Blatt: Zeichnen wie im Grundriss (Blatt-Ebene statt eigener Annotationen)
Jedes Layout hat eine Blatt-Ebene (DrawingLevel.layoutId, Papier 1:1 in Metern); ein offenes Blatt macht sie zur aktiven Ebene in derselben PlanView: gleiche Befehle, Fang, Griffe, Vorschau/HUD, Auswahl, Undo und dasselbe Rendering. Das Blatt liefert nur den Hintergrund (PlanView.backdrop) und das Platzieren der Viewports. Format 3 migriert Layout.annotations zu drawings2d. Layout-PDF legt die Blatt-Ebene über planToPrintSvg aufs Papier. Alter Annotations-Pfad gelöscht. e2e layoutSheet.spec.ts.
This commit is contained in:
43 files changed
+1245
-2756
No files matched your search
@@ -432,7 +432,7 @@ export function ImportDialog({
|
||||
value={existingLevelId}
|
||||
onChange={(v) => setExistingLevelId(v)}
|
||||
title={t("import.target.existing")}
|
||||
options={project.drawingLevels.map((z) => ({
|
||||
options={project.drawingLevels.filter((z) => !z.layoutId).map((z) => ({
|
||||
value: z.id,
|
||||
label: `${z.name} · ${t(kindBadgeKey(z.kind))}`,
|
||||
}))}
|
||||
|
||||
+326
-1260
File diff suppressed because it is too large.
Load diff
@@ -1,27 +1,13 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { LayoutAnnotation, LayoutViewport } from "../model/types";
|
||||
import type { LayoutViewport } from "../model/types";
|
||||
import {
|
||||
annotationBoundsMm,
|
||||
annotationHandleAt,
|
||||
applyHandleDrag,
|
||||
applyLineHandleDrag,
|
||||
clampRectToSheet,
|
||||
distanceToSegment,
|
||||
fitTransform,
|
||||
handleAt,
|
||||
lineHandleAt,
|
||||
mmToScreen,
|
||||
normalizeRect,
|
||||
panBy,
|
||||
pickAnnotation,
|
||||
pickAnnotationHandle,
|
||||
pickViewport,
|
||||
screenToMm,
|
||||
translateAnnotation,
|
||||
vpRect,
|
||||
zoomAtCursor,
|
||||
type RectMm,
|
||||
type SheetTransform,
|
||||
} from "./layoutSheetMath";
|
||||
|
||||
const vp = (
|
||||
@@ -32,64 +18,6 @@ const vp = (
|
||||
heightMm: number,
|
||||
): LayoutViewport => ({ id, snapshotId: "s", xMm, yMm, widthMm, heightMm });
|
||||
|
||||
describe("mm ↔ screen", () => {
|
||||
const t: SheetTransform = { scale: 2, tx: 100, ty: 50 };
|
||||
it("mmToScreen applies scale + translate", () => {
|
||||
expect(mmToScreen(t, { xMm: 10, yMm: 20 })).toEqual({ x: 120, y: 90 });
|
||||
});
|
||||
it("screenToMm is the inverse", () => {
|
||||
const p = screenToMm(t, 120, 90);
|
||||
expect(p.xMm).toBeCloseTo(10);
|
||||
expect(p.yMm).toBeCloseTo(20);
|
||||
});
|
||||
it("roundtrips arbitrary points", () => {
|
||||
const s = mmToScreen(t, { xMm: 37.5, yMm: -12.25 });
|
||||
const m = screenToMm(t, s.x, s.y);
|
||||
expect(m.xMm).toBeCloseTo(37.5);
|
||||
expect(m.yMm).toBeCloseTo(-12.25);
|
||||
});
|
||||
});
|
||||
|
||||
describe("fitTransform", () => {
|
||||
it("centres an A4 portrait sheet in a wide canvas", () => {
|
||||
const t = fitTransform(210, 297, 1000, 600, 0.92);
|
||||
// Höhe ist der Engpass: scale = 600*0.92/297.
|
||||
expect(t.scale).toBeCloseTo((600 * 0.92) / 297);
|
||||
// Zentriert: linker Rand = (1000 - 210*scale)/2.
|
||||
expect(t.tx).toBeCloseTo((1000 - 210 * t.scale) / 2);
|
||||
expect(t.ty).toBeCloseTo((600 - 297 * t.scale) / 2);
|
||||
});
|
||||
it("falls back to scale 1 on degenerate canvas", () => {
|
||||
const t = fitTransform(210, 297, 0, 0);
|
||||
expect(t.scale).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("zoomAtCursor", () => {
|
||||
it("keeps the mm point under the cursor fixed", () => {
|
||||
const t: SheetTransform = { scale: 2, tx: 100, ty: 50 };
|
||||
const cursor = { x: 300, y: 220 };
|
||||
const before = screenToMm(t, cursor.x, cursor.y);
|
||||
const zt = zoomAtCursor(t, 1.25, cursor.x, cursor.y);
|
||||
expect(zt.scale).toBeCloseTo(2.5);
|
||||
const after = screenToMm(zt, cursor.x, cursor.y);
|
||||
expect(after.xMm).toBeCloseTo(before.xMm);
|
||||
expect(after.yMm).toBeCloseTo(before.yMm);
|
||||
});
|
||||
it("clamps scale to bounds", () => {
|
||||
const t: SheetTransform = { scale: 30, tx: 0, ty: 0 };
|
||||
const zt = zoomAtCursor(t, 4, 0, 0);
|
||||
expect(zt.scale).toBeLessThanOrEqual(40);
|
||||
});
|
||||
});
|
||||
|
||||
describe("panBy", () => {
|
||||
it("shifts translation, keeps scale", () => {
|
||||
const t = panBy({ scale: 3, tx: 10, ty: 20 }, 5, -7);
|
||||
expect(t).toEqual({ scale: 3, tx: 15, ty: 13 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleAt", () => {
|
||||
const r: RectMm = { xMm: 20, yMm: 20, widthMm: 100, heightMm: 60 };
|
||||
it("detects corners with priority over edges", () => {
|
||||
@@ -203,189 +131,3 @@ describe("vpRect", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ── Annotationen (Linie/Rechteck/Text) ──────────────────────────────────────
|
||||
|
||||
const line = (id: string, x1: number, y1: number, x2: number, y2: number): LayoutAnnotation => ({
|
||||
id,
|
||||
kind: "line",
|
||||
x1Mm: x1,
|
||||
y1Mm: y1,
|
||||
x2Mm: x2,
|
||||
y2Mm: y2,
|
||||
});
|
||||
const rect = (id: string, x: number, y: number, w: number, h: number): LayoutAnnotation => ({
|
||||
id,
|
||||
kind: "rect",
|
||||
xMm: x,
|
||||
yMm: y,
|
||||
widthMm: w,
|
||||
heightMm: h,
|
||||
});
|
||||
const text = (id: string, x: number, y: number, t: string, h = 3.5): LayoutAnnotation => ({
|
||||
id,
|
||||
kind: "text",
|
||||
xMm: x,
|
||||
yMm: y,
|
||||
text: t,
|
||||
heightMm: h,
|
||||
});
|
||||
const image = (id: string, x: number, y: number, w: number, h: number): LayoutAnnotation => ({
|
||||
id,
|
||||
kind: "image",
|
||||
xMm: x,
|
||||
yMm: y,
|
||||
widthMm: w,
|
||||
heightMm: h,
|
||||
src: "data:image/png;base64,",
|
||||
});
|
||||
|
||||
describe("distanceToSegment", () => {
|
||||
it("returns perpendicular distance for a point beside the segment", () => {
|
||||
expect(distanceToSegment(5, 3, 0, 0, 10, 0)).toBeCloseTo(3);
|
||||
});
|
||||
it("clamps to the nearest endpoint beyond the segment", () => {
|
||||
expect(distanceToSegment(-4, 0, 0, 0, 10, 0)).toBeCloseTo(4);
|
||||
expect(distanceToSegment(14, 0, 0, 0, 10, 0)).toBeCloseTo(4);
|
||||
});
|
||||
it("degenerates to point distance for a zero-length segment", () => {
|
||||
expect(distanceToSegment(3, 4, 0, 0, 0, 0)).toBeCloseTo(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe("annotationBoundsMm", () => {
|
||||
it("rect: its own rectangle", () => {
|
||||
expect(annotationBoundsMm(rect("r", 10, 20, 30, 40))).toEqual({
|
||||
xMm: 10,
|
||||
yMm: 20,
|
||||
widthMm: 30,
|
||||
heightMm: 40,
|
||||
});
|
||||
});
|
||||
it("line: normalized bounding rect of both endpoints", () => {
|
||||
expect(annotationBoundsMm(line("l", 30, 30, 10, 10))).toEqual({
|
||||
xMm: 10,
|
||||
yMm: 10,
|
||||
widthMm: 20,
|
||||
heightMm: 20,
|
||||
});
|
||||
});
|
||||
it("text: box sits above the baseline, width grows with character count", () => {
|
||||
const b = annotationBoundsMm(text("t", 10, 20, "AB", 4));
|
||||
expect(b.xMm).toBe(10);
|
||||
expect(b.yMm).toBe(16); // 20 - heightMm
|
||||
expect(b.heightMm).toBeCloseTo(5); // heightMm * 1.25
|
||||
expect(b.widthMm).toBeGreaterThan(0);
|
||||
});
|
||||
it("image: its own rectangle, like rect", () => {
|
||||
expect(annotationBoundsMm(image("i", 5, 5, 40, 30))).toEqual({
|
||||
xMm: 5,
|
||||
yMm: 5,
|
||||
widthMm: 40,
|
||||
heightMm: 30,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("pickAnnotation", () => {
|
||||
it("hits a line within tolerance of the segment", () => {
|
||||
expect(pickAnnotation([line("l", 0, 0, 10, 0)], 5, 1, 2)).toBe("l");
|
||||
expect(pickAnnotation([line("l", 0, 0, 10, 0)], 5, 5, 2)).toBe(null);
|
||||
});
|
||||
it("hits a rect on its border and its interior", () => {
|
||||
const r = rect("r", 20, 20, 10, 10);
|
||||
expect(pickAnnotation([r], 20, 20, 1)).toBe("r"); // corner
|
||||
expect(pickAnnotation([r], 25, 25, 1)).toBe("r"); // interior
|
||||
expect(pickAnnotation([r], 100, 100, 1)).toBe(null);
|
||||
});
|
||||
it("hits a text annotation near its baseline box", () => {
|
||||
const tx = text("t", 10, 20, "Hallo", 4);
|
||||
expect(pickAnnotation([tx], 12, 18, 1)).toBe("t");
|
||||
expect(pickAnnotation([tx], 200, 200, 1)).toBe(null);
|
||||
});
|
||||
it("returns the topmost (last) annotation in overlap, like pickViewport", () => {
|
||||
const overlapping = [rect("a", 0, 0, 50, 50), rect("b", 10, 10, 50, 50)];
|
||||
expect(pickAnnotation(overlapping, 30, 30, 1)).toBe("b");
|
||||
});
|
||||
it("hits an image on its border and its interior, like rect", () => {
|
||||
const img = image("i", 20, 20, 10, 10);
|
||||
expect(pickAnnotation([img], 20, 20, 1)).toBe("i"); // corner
|
||||
expect(pickAnnotation([img], 25, 25, 1)).toBe("i"); // interior
|
||||
expect(pickAnnotation([img], 100, 100, 1)).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("lineHandleAt", () => {
|
||||
it("hits the first endpoint within tolerance", () => {
|
||||
expect(lineHandleAt(0, 0, 10, 0, 0.5, 0.5, 2)).toBe("p1");
|
||||
});
|
||||
it("hits the second endpoint within tolerance", () => {
|
||||
expect(lineHandleAt(0, 0, 10, 0, 9.5, 0.5, 2)).toBe("p2");
|
||||
});
|
||||
it("hits the body between the endpoints", () => {
|
||||
expect(lineHandleAt(0, 0, 10, 0, 5, 1, 2)).toBe("body");
|
||||
});
|
||||
it("misses far away", () => {
|
||||
expect(lineHandleAt(0, 0, 10, 0, 5, 20, 2)).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("annotationHandleAt / pickAnnotationHandle", () => {
|
||||
it("line: endpoint takes priority over body", () => {
|
||||
const l = line("l", 0, 0, 10, 0);
|
||||
expect(annotationHandleAt(l, 0, 0.5, 2)).toEqual({ kind: "line", handle: "p1" });
|
||||
expect(annotationHandleAt(l, 5, 0.5, 2)).toEqual({ kind: "line", handle: "body" });
|
||||
});
|
||||
it("rect: corner/edge/body like a viewport", () => {
|
||||
const r = rect("r", 20, 20, 10, 10);
|
||||
expect(annotationHandleAt(r, 20, 20, 1)).toEqual({ kind: "rect", handle: "nw" });
|
||||
expect(annotationHandleAt(r, 25, 25, 1)).toEqual({ kind: "rect", handle: "body" });
|
||||
});
|
||||
it("image: same handle set as rect", () => {
|
||||
const img = image("i", 20, 20, 10, 10);
|
||||
expect(annotationHandleAt(img, 30, 30, 1)).toEqual({ kind: "image", handle: "se" });
|
||||
});
|
||||
it("text: no handle system, plain bbox hit still reported as pickAnnotationHandle body", () => {
|
||||
const tx = text("t", 10, 20, "Hallo", 4);
|
||||
expect(annotationHandleAt(tx, 12, 18, 1)).toBe(null);
|
||||
expect(pickAnnotationHandle([tx], 12, 18, 1)).toEqual({
|
||||
id: "t",
|
||||
hit: { kind: "line", handle: "body" },
|
||||
});
|
||||
});
|
||||
it("pickAnnotationHandle returns null outside every annotation", () => {
|
||||
expect(pickAnnotationHandle([line("l", 0, 0, 10, 0)], 500, 500, 1)).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("applyLineHandleDrag", () => {
|
||||
const l = line("l", 0, 0, 10, 0) as Extract<LayoutAnnotation, { kind: "line" }>;
|
||||
it("p1 moves only the first endpoint", () => {
|
||||
expect(applyLineHandleDrag(l, "p1", 2, 3)).toEqual({ x1Mm: 2, y1Mm: 3, x2Mm: 10, y2Mm: 0 });
|
||||
});
|
||||
it("p2 moves only the second endpoint", () => {
|
||||
expect(applyLineHandleDrag(l, "p2", 2, 3)).toEqual({ x1Mm: 0, y1Mm: 0, x2Mm: 12, y2Mm: 3 });
|
||||
});
|
||||
it("body moves both endpoints", () => {
|
||||
expect(applyLineHandleDrag(l, "body", 2, 3)).toEqual({ x1Mm: 2, y1Mm: 3, x2Mm: 12, y2Mm: 3 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("translateAnnotation", () => {
|
||||
it("moves both endpoints of a line", () => {
|
||||
const moved = translateAnnotation(line("l", 0, 0, 10, 10), 5, -5);
|
||||
expect(moved).toMatchObject({ x1Mm: 5, y1Mm: -5, x2Mm: 15, y2Mm: 5 });
|
||||
});
|
||||
it("moves the origin of a rect", () => {
|
||||
const moved = translateAnnotation(rect("r", 10, 10, 20, 20), 3, 4);
|
||||
expect(moved).toMatchObject({ xMm: 13, yMm: 14, widthMm: 20, heightMm: 20 });
|
||||
});
|
||||
it("moves the anchor point of a text annotation", () => {
|
||||
const moved = translateAnnotation(text("t", 10, 10, "Hi"), 1, 2);
|
||||
expect(moved).toMatchObject({ xMm: 11, yMm: 12, text: "Hi" });
|
||||
});
|
||||
it("moves the origin of an image, like rect", () => {
|
||||
const moved = translateAnnotation(image("i", 10, 10, 20, 20), 3, 4);
|
||||
expect(moved).toMatchObject({ xMm: 13, yMm: 14, widthMm: 20, heightMm: 20 });
|
||||
});
|
||||
});
|
||||
+4
-295
@@ -1,25 +1,14 @@
|
||||
// Reine, testbare Geometrie für die In-Viewport-Layout-Ansicht (Phase 2b):
|
||||
// Bildschirm↔Blatt-mm-Transform (Pan/Zoom-to-Cursor/Einpassen), Viewport-
|
||||
// Reine, testbare Geometrie für die Viewports auf dem Layout-Blatt: Viewport-
|
||||
// Trefferlogik (Body + 8 Resize-Griffe), Resize-Mathe mit Mindestgrösse,
|
||||
// Rechteck-Normalisierung (Aufziehen) und Clamp an die Blattkanten.
|
||||
//
|
||||
// KEINE React-/DOM-Abhängigkeit — nur Zahlen. Das UI (LayoutSheet.tsx) baut
|
||||
// darauf auf. Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md).
|
||||
|
||||
import type { LayoutAnnotation, LayoutViewport } from "../model/types";
|
||||
import type { LayoutViewport } from "../model/types";
|
||||
|
||||
/**
|
||||
* Affine Blatt-Transform: Bildschirm-Pixel = mm · scale + translate.
|
||||
* `scale` ist px pro mm (immer > 0), `tx/ty` die Blatt-Ursprungslage (mm 0,0)
|
||||
* in Canvas-Pixeln.
|
||||
*/
|
||||
export interface SheetTransform {
|
||||
/** Pixel pro Millimeter (> 0). */
|
||||
scale: number;
|
||||
/** Bildschirm-X des Blatt-Ursprungs (mm x=0) in Canvas-Pixeln. */
|
||||
tx: number;
|
||||
/** Bildschirm-Y des Blatt-Ursprungs (mm y=0) in Canvas-Pixeln. */
|
||||
ty: number;
|
||||
function clamp(v: number, lo: number, hi: number): number {
|
||||
return v < lo ? lo : v > hi ? hi : v;
|
||||
}
|
||||
|
||||
/** Ein Punkt in Blatt-Millimetern. */
|
||||
@@ -48,68 +37,6 @@ export type Handle =
|
||||
| "w"
|
||||
| "body";
|
||||
|
||||
export const MIN_SCALE = 0.05;
|
||||
export const MAX_SCALE = 40;
|
||||
|
||||
function clamp(v: number, lo: number, hi: number): number {
|
||||
return v < lo ? lo : v > hi ? hi : v;
|
||||
}
|
||||
|
||||
// ── Transform ────────────────────────────────────────────────────────────────
|
||||
|
||||
/** Blatt-mm → Canvas-Pixel. */
|
||||
export function mmToScreen(t: SheetTransform, p: Mm): { x: number; y: number } {
|
||||
return { x: p.xMm * t.scale + t.tx, y: p.yMm * t.scale + t.ty };
|
||||
}
|
||||
|
||||
/** Canvas-Pixel → Blatt-mm. */
|
||||
export function screenToMm(t: SheetTransform, sx: number, sy: number): Mm {
|
||||
return { xMm: (sx - t.tx) / t.scale, yMm: (sy - t.ty) / t.scale };
|
||||
}
|
||||
|
||||
/**
|
||||
* Einpass-Transform: Blatt (mm) zentriert in die Canvas-Fläche (px), mit
|
||||
* `margin` (Anteil, z. B. 0.92 = 8 % Rand). Degenerierte Eingaben → scale=1.
|
||||
*/
|
||||
export function fitTransform(
|
||||
sheetWMm: number,
|
||||
sheetHMm: number,
|
||||
canvasWPx: number,
|
||||
canvasHPx: number,
|
||||
margin = 0.92,
|
||||
): SheetTransform {
|
||||
const sx = (canvasWPx * margin) / sheetWMm;
|
||||
const sy = (canvasHPx * margin) / sheetHMm;
|
||||
let scale = Math.min(sx, sy);
|
||||
if (!Number.isFinite(scale) || scale <= 0) scale = 1;
|
||||
scale = clamp(scale, MIN_SCALE, MAX_SCALE);
|
||||
const tx = (canvasWPx - sheetWMm * scale) / 2;
|
||||
const ty = (canvasHPx - sheetHMm * scale) / 2;
|
||||
return { scale, tx, ty };
|
||||
}
|
||||
|
||||
/**
|
||||
* Zoom um den Cursor: der mm-Punkt unter (sx,sy) bleibt fix. `factor` > 1 zoomt
|
||||
* rein, < 1 raus; der Massstab wird auf [MIN_SCALE, MAX_SCALE] geklemmt.
|
||||
*/
|
||||
export function zoomAtCursor(
|
||||
t: SheetTransform,
|
||||
factor: number,
|
||||
sx: number,
|
||||
sy: number,
|
||||
): SheetTransform {
|
||||
const newScale = clamp(t.scale * factor, MIN_SCALE, MAX_SCALE);
|
||||
// mm-Punkt unter dem Cursor VOR dem Zoom.
|
||||
const m = screenToMm(t, sx, sy);
|
||||
// Neue Translation, damit derselbe mm-Punkt wieder unter (sx,sy) liegt.
|
||||
return { scale: newScale, tx: sx - m.xMm * newScale, ty: sy - m.yMm * newScale };
|
||||
}
|
||||
|
||||
/** Verschiebt die Transform um (dxPx,dyPx) Canvas-Pixel (Pan). */
|
||||
export function panBy(t: SheetTransform, dxPx: number, dyPx: number): SheetTransform {
|
||||
return { scale: t.scale, tx: t.tx + dxPx, ty: t.ty + dyPx };
|
||||
}
|
||||
|
||||
// ── Viewport-Rechtecke / Treffer ─────────────────────────────────────────────
|
||||
|
||||
/** Rechteck-Sicht eines Viewports (nur die Blattgeometrie). */
|
||||
@@ -320,221 +247,3 @@ export function handleAnchors(r: RectMm): { handle: Handle; xMm: number; yMm: nu
|
||||
{ handle: "w", xMm: x, yMm: cy },
|
||||
];
|
||||
}
|
||||
|
||||
// ── Annotationen (Linie/Rechteck/Text/Bild) ─────────────────────────────────
|
||||
// Rechteck/Bild teilen sich die Viewport-Griff-Mathe (dieselbe RectMm-Form,
|
||||
// 8 Ecken/Kanten-Griffe). Linie bekommt zwei Endpunkt-Griffe (wie eine Linie
|
||||
// im Grundriss). Text bleibt vorerst griff-los (Rahmen kommt beim Aufziehen).
|
||||
|
||||
/** Kürzester Abstand (mm) eines Punkts zu einem Liniensegment. */
|
||||
export function distanceToSegment(
|
||||
px: number,
|
||||
py: number,
|
||||
x1: number,
|
||||
y1: number,
|
||||
x2: number,
|
||||
y2: number,
|
||||
): number {
|
||||
const dx = x2 - x1;
|
||||
const dy = y2 - y1;
|
||||
const lenSq = dx * dx + dy * dy;
|
||||
if (lenSq === 0) return Math.hypot(px - x1, py - y1);
|
||||
const t = clamp(((px - x1) * dx + (py - y1) * dy) / lenSq, 0, 1);
|
||||
const cx = x1 + t * dx;
|
||||
const cy = y1 + t * dy;
|
||||
return Math.hypot(px - cx, py - cy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Umhüllendes Rechteck (mm) einer Annotation — für Text nur eine GROBE Box
|
||||
* (kein echtes Font-Measuring im SVG; Breite aus Zeichenzahl geschätzt), reicht
|
||||
* aber für Trefferlogik + Auswahl-Rahmen. `yMm` einer Text-Annotation ist die
|
||||
* Baseline, die Box liegt daher oberhalb davon.
|
||||
*/
|
||||
export function annotationBoundsMm(ann: LayoutAnnotation): RectMm {
|
||||
if (ann.kind === "rect" || ann.kind === "image") {
|
||||
return { xMm: ann.xMm, yMm: ann.yMm, widthMm: ann.widthMm, heightMm: ann.heightMm };
|
||||
}
|
||||
if (ann.kind === "line") {
|
||||
return normalizeRect(ann.x1Mm, ann.y1Mm, ann.x2Mm, ann.y2Mm);
|
||||
}
|
||||
// Text: grobe Box aus Zeichenzahl geschätzt (kein echtes Font-Measuring im
|
||||
// SVG) — bei gesetzter Rahmenbreite (`widthMm`, Rich-Text-Frame) gilt diese
|
||||
// statt der Schätzung; die Höhe bleibt eine grobe Zeilenschätzung.
|
||||
const estWidthMm = Math.max(ann.heightMm, ann.text.length * ann.heightMm * 0.6);
|
||||
const lines = ann.doc ? Math.max(1, ann.doc.paragraphs.length) : 1;
|
||||
return {
|
||||
xMm: ann.xMm,
|
||||
yMm: ann.yMm - ann.heightMm,
|
||||
widthMm: ann.widthMm ?? estWidthMm,
|
||||
heightMm: ann.heightMm * 1.25 * lines,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Trefferlogik (mm) für EINE Annotation: Linie → Abstand zum Segment ≤ tolMm;
|
||||
* Rechteck → Rahmen ODER Innenfläche (wiederverwendet {@link handleAt}, jeder
|
||||
* Nicht-null-Rückgabewert zählt als Treffer); Text → Bounding-Box + Toleranz.
|
||||
*/
|
||||
export function annotationHit(
|
||||
ann: LayoutAnnotation,
|
||||
xMm: number,
|
||||
yMm: number,
|
||||
tolMm: number,
|
||||
): boolean {
|
||||
if (ann.kind === "line") {
|
||||
return distanceToSegment(xMm, yMm, ann.x1Mm, ann.y1Mm, ann.x2Mm, ann.y2Mm) <= tolMm;
|
||||
}
|
||||
if (ann.kind === "rect" || ann.kind === "image") {
|
||||
const r: RectMm = { xMm: ann.xMm, yMm: ann.yMm, widthMm: ann.widthMm, heightMm: ann.heightMm };
|
||||
return handleAt(r, xMm, yMm, tolMm) !== null;
|
||||
}
|
||||
const b = annotationBoundsMm(ann);
|
||||
return (
|
||||
xMm >= b.xMm - tolMm &&
|
||||
xMm <= b.xMm + b.widthMm + tolMm &&
|
||||
yMm >= b.yMm - tolMm &&
|
||||
yMm <= b.yMm + b.heightMm + tolMm
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Topmost-Treffer über eine Annotations-Liste (letzte = oberste, wie
|
||||
* {@link pickViewport}). Liefert die Id des ERSTEN Treffers von oben, sonst null.
|
||||
*/
|
||||
export function pickAnnotation(
|
||||
annotations: LayoutAnnotation[],
|
||||
xMm: number,
|
||||
yMm: number,
|
||||
tolMm: number,
|
||||
): string | null {
|
||||
for (let i = annotations.length - 1; i >= 0; i--) {
|
||||
const a = annotations[i];
|
||||
if (annotationHit(a, xMm, yMm, tolMm)) return a.id;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ── Resize-Griffe für Annotationen (wie im Grundriss: Endpunkte/Ecken) ──────
|
||||
|
||||
/** Griff einer Linien-Annotation: die beiden Endpunkte oder der Körper. */
|
||||
export type LineHandle = "p1" | "p2" | "body";
|
||||
|
||||
/** Griff-Ergebnis für EINE Annotation — je Art ein anderer Griff-Typ. */
|
||||
export type AnnotHandleHit =
|
||||
| { kind: "rect" | "image"; handle: Handle }
|
||||
| { kind: "line"; handle: LineHandle };
|
||||
|
||||
/**
|
||||
* Griff (oder Körper) einer Linien-Annotation an einem mm-Punkt: die
|
||||
* Endpunkte haben Vorrang vor dem Segment (wie ein Linien-Griff im Grundriss).
|
||||
*/
|
||||
export function lineHandleAt(
|
||||
x1: number,
|
||||
y1: number,
|
||||
x2: number,
|
||||
y2: number,
|
||||
xMm: number,
|
||||
yMm: number,
|
||||
tolMm: number,
|
||||
): LineHandle | null {
|
||||
if (Math.hypot(xMm - x1, yMm - y1) <= tolMm) return "p1";
|
||||
if (Math.hypot(xMm - x2, yMm - y2) <= tolMm) return "p2";
|
||||
if (distanceToSegment(xMm, yMm, x1, y1, x2, y2) <= tolMm) return "body";
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Griff (oder Körper) EINER Annotation an einem mm-Punkt — Linie liefert einen
|
||||
* Endpunkt-Griff, Rechteck/Bild die 8 Viewport-artigen Ecken-/Kanten-Griffe
|
||||
* (wiederverwendet {@link handleAt}). Text hat (noch) keine Griffe → `null`.
|
||||
*/
|
||||
export function annotationHandleAt(
|
||||
ann: LayoutAnnotation,
|
||||
xMm: number,
|
||||
yMm: number,
|
||||
tolMm: number,
|
||||
): AnnotHandleHit | null {
|
||||
if (ann.kind === "line") {
|
||||
const h = lineHandleAt(ann.x1Mm, ann.y1Mm, ann.x2Mm, ann.y2Mm, xMm, yMm, tolMm);
|
||||
return h ? { kind: "line", handle: h } : null;
|
||||
}
|
||||
if (ann.kind === "rect" || ann.kind === "image") {
|
||||
const r: RectMm = { xMm: ann.xMm, yMm: ann.yMm, widthMm: ann.widthMm, heightMm: ann.heightMm };
|
||||
const h = handleAt(r, xMm, yMm, tolMm);
|
||||
return h ? { kind: ann.kind, handle: h } : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Topmost Griff-Treffer über eine Annotations-Liste (wie {@link pickAnnotation},
|
||||
* aber liefert zusätzlich den konkreten Griff für Resize statt nur Verschieben).
|
||||
*/
|
||||
export function pickAnnotationHandle(
|
||||
annotations: LayoutAnnotation[],
|
||||
xMm: number,
|
||||
yMm: number,
|
||||
tolMm: number,
|
||||
): { id: string; hit: AnnotHandleHit } | null {
|
||||
for (let i = annotations.length - 1; i >= 0; i--) {
|
||||
const a = annotations[i];
|
||||
const hit = annotationHandleAt(a, xMm, yMm, tolMm);
|
||||
if (hit) return { id: a.id, hit };
|
||||
if (annotationHit(a, xMm, yMm, tolMm)) {
|
||||
// Text (kein Griff-System) oder ein Nicht-Griff-Treffer im Inneren.
|
||||
return { id: a.id, hit: { kind: "line", handle: "body" } };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Endpunkte einer Linien-Annotation als mm-Punkte (für die Griff-Darstellung). */
|
||||
export function lineHandleAnchors(ann: Extract<LayoutAnnotation, { kind: "line" }>): Mm[] {
|
||||
return [
|
||||
{ xMm: ann.x1Mm, yMm: ann.y1Mm },
|
||||
{ xMm: ann.x2Mm, yMm: ann.y2Mm },
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Wendet einen Linien-Griff-Drag an: `p1`/`p2` verschiebt genau diesen
|
||||
* Endpunkt, `body` das ganze Segment (Delta).
|
||||
*/
|
||||
export function applyLineHandleDrag(
|
||||
start: Extract<LayoutAnnotation, { kind: "line" }>,
|
||||
handle: LineHandle,
|
||||
dxMm: number,
|
||||
dyMm: number,
|
||||
): { x1Mm: number; y1Mm: number; x2Mm: number; y2Mm: number } {
|
||||
if (handle === "p1") {
|
||||
return { x1Mm: start.x1Mm + dxMm, y1Mm: start.y1Mm + dyMm, x2Mm: start.x2Mm, y2Mm: start.y2Mm };
|
||||
}
|
||||
if (handle === "p2") {
|
||||
return { x1Mm: start.x1Mm, y1Mm: start.y1Mm, x2Mm: start.x2Mm + dxMm, y2Mm: start.y2Mm + dyMm };
|
||||
}
|
||||
return {
|
||||
x1Mm: start.x1Mm + dxMm,
|
||||
y1Mm: start.y1Mm + dyMm,
|
||||
x2Mm: start.x2Mm + dxMm,
|
||||
y2Mm: start.y2Mm + dyMm,
|
||||
};
|
||||
}
|
||||
|
||||
/** Verschiebt eine Annotation um ein mm-Delta — alle ihre Punkte wandern mit. */
|
||||
export function translateAnnotation(
|
||||
ann: LayoutAnnotation,
|
||||
dxMm: number,
|
||||
dyMm: number,
|
||||
): LayoutAnnotation {
|
||||
if (ann.kind === "line") {
|
||||
return {
|
||||
...ann,
|
||||
x1Mm: ann.x1Mm + dxMm,
|
||||
y1Mm: ann.y1Mm + dyMm,
|
||||
x2Mm: ann.x2Mm + dxMm,
|
||||
y2Mm: ann.y2Mm + dyMm,
|
||||
};
|
||||
}
|
||||
return { ...ann, xMm: ann.xMm + dxMm, yMm: ann.yMm + dyMm };
|
||||
}
|
||||
Reference in new issue
Block a user