[153] Strichmuster einheitlich in Papier-mm: Linientypen im Kern (line_pattern), Musterphase mit Strich an den Enden in Plan-SVG, Druck-SVG, PDF und Strich-Shader, Achsen massstabsunabhängig

This commit is contained in:
Claude committed 2026-11-07 14:19:35 +00:00
1 parent e2952538a3
commit 8ceb24f00b
44 files changed
+776 -127

No files matched your search

+59
View File
@@ -0,0 +1,59 @@
// [153] PDF-Druck-SVG: Strichmuster in Papier-mm, in jedem Massstab gleich lang, Musterphase vom Kern
// (`plan::line_pattern::phase`): beide Enden einer gestrichelten Linie auf einem Strich.
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { sceneToPrintSvg } from "./sceneToPrintSvg";
import type { RScene } from "../plan/toRenderScene";
/** Kleinst-DOM (die Testumgebung ist `node`): Elemente mit Attributen und Kindern. */
class FakeEl {
attrs = new Map<string, string>();
children: FakeEl[] = [];
textContent = "";
constructor(public tag: string) {}
setAttribute(k: string, v: string): void {
this.attrs.set(k, v);
}
appendChild(c: FakeEl): FakeEl {
this.children.push(c);
return c;
}
}
const g = globalThis as { document?: unknown };
let saved: unknown;
beforeEach(() => {
saved = g.document;
g.document = { createElementNS: (_ns: string, tag: string) => new FakeEl(tag) };
});
afterEach(() => {
g.document = saved;
});
/** Eine gestrichelte Linie, die bei 1:`n` auf dem Papier 30 mm lang ist. */
function scene(n: number): RScene {
const len = (30 * n) / 1000;
return {
fills: [],
outlines: [],
polylines: [],
arcs: [],
lines: [{ a: [0, 0], b: [len, 0], color: [0, 0, 0, 1], widthMm: 0.18, dash: [2, 1], z: 0 }],
texts: [],
images: [],
bounds: { minX: 0, minY: -1, maxX: len, maxY: 1 },
} as RScene;
}
describe("Druck-SVG: Strichmuster", () => {
it("gleiche Musterlänge und Phase in 1:50, 1:100, 1:200", () => {
const seen = [50, 100, 200].map((n) => {
const svg = sceneToPrintSvg(scene(n), { scaleDenominator: n, pageWidthMm: 100, pageHeightMm: 100 }).svg as unknown as FakeEl;
const line = svg.children.find((c) => c.tag === "line")!;
return [line.attrs.get("stroke-dasharray"), line.attrs.get("stroke-dashoffset")];
});
expect(seen.every((s) => s[0] === "2 1")).toBe(true);
// 30 mm mit [2, 1]: genau 10 Zyklen → Phase so, dass das Ende auf einem Strich liegt (nicht 0).
expect(seen[0][1]).toBeDefined();
expect(seen.every((s) => s[1] === seen[0][1])).toBe(true);
});
});
+19
View File
@@ -22,6 +22,7 @@
//
// Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md).
import { core } from "../engine/coreWasm";
import { lineEndsSvgAttrs } from "../model/lineEnds";
import type {
RArc,
@@ -278,10 +279,27 @@ function appendStroke(
el.setAttribute("stroke-dasharray", dash.map((d) => round(d)).join(" "));
// Punkte (Strich 0) brauchen runde Enden, sonst unsichtbar ([96]).
if (dash.some((d) => d === 0)) el.setAttribute("stroke-linecap", "round");
setDashOffset(el, pts.map((p) => map(p)), closed, dash);
}
svg.appendChild(el);
}
/**
* [153] Musterphase eines gestrichelten Zugs (Papier-mm-Punkte): Kern `plan::line_pattern::phase`,
* damit beide Enden möglichst auf einem Strich liegen — gleich wie Plan-/Druck-SVG und Bildschirm.
*/
function setDashOffset(el: Element, ptsMm: [number, number][], closed: boolean, dash: number[]): void {
let len = 0;
const n = ptsMm.length;
for (let i = 0; i + 1 < n + (closed ? 1 : 0); i++) {
const [ax, ay] = ptsMm[i];
const [bx, by] = ptsMm[(i + 1) % n];
len += Math.hypot(bx - ax, by - ay);
}
const ph = core().line_dash_phase(len, Float64Array.from(dash));
if (ph > 1e-9) el.setAttribute("stroke-dashoffset", String(round(ph)));
}
/** Freies Liniensegment (Tuerblatt, Referenzlinie, Symbol). */
function appendLine(svg: SVGSVGElement, l: RLine, map: Mapper, _mmPerM: number): void {
if (l.widthMm <= 0) return;
@@ -298,6 +316,7 @@ function appendLine(svg: SVGSVGElement, l: RLine, map: Mapper, _mmPerM: number):
if (l.dash && l.dash.length) {
el.setAttribute("stroke-dasharray", l.dash.map((d) => round(d)).join(" "));
if (l.dash.some((d) => d === 0)) el.setAttribute("stroke-linecap", "round");
setDashOffset(el, [[ax, ay], [bx, by]], false, l.dash);
}
svg.appendChild(el);
}