[K3d] Merge: Druck-SVG-Texte in Papier-mm (Layout/PDF), Raumstempel im Druck, Text-Lage aus dem Kern
Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
10 files changed
+449
-200
No files matched your search
+6
-27
@@ -15,7 +15,7 @@ import "svg2pdf.js";
|
||||
import type { Layout, LayoutAnnotation, MasterLayout, Project } from "../model/types";
|
||||
import { generatePlan } from "../plan/generatePlan";
|
||||
import { planToPrintSvg } from "./planToPrintSvg";
|
||||
import { wrapDocToLines, type SvgLine } from "../text/renderHtml";
|
||||
import { annotationTextLines } from "../text/renderHtml";
|
||||
import { docFromText } from "../text/richText";
|
||||
import {
|
||||
findMaster,
|
||||
@@ -122,33 +122,12 @@ function buildAnnotationTextsSvg(
|
||||
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;
|
||||
for (const line of annotationTextLines(ann, ann.doc ?? docFromText(ann.text))) {
|
||||
const lineTspan = document.createElementNS(SVG_NS, "tspan");
|
||||
lineTspan.setAttribute("x", String(x));
|
||||
lineTspan.setAttribute("y", String(y));
|
||||
lineTspan.setAttribute("text-anchor", anchor);
|
||||
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 {
|
||||
@@ -165,7 +144,7 @@ function buildAnnotationTextsSvg(
|
||||
}
|
||||
}
|
||||
el.appendChild(lineTspan);
|
||||
});
|
||||
}
|
||||
svg.appendChild(el);
|
||||
}
|
||||
return svg;
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
// Texte im Druck-SVG (Layout-Viewport, `planToPrintSvg`): Grösse in Papier-mm wie die Plan-Ansicht beim
|
||||
// Massstab des Viewports — Einzeltexte (einfach und Rich-Text), Masszahlen, Höhenkoten, Raumstempel.
|
||||
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { generatePlan } from "../plan/generatePlan";
|
||||
import { sampleProject } from "../model/sampleProject";
|
||||
import { flattenCategories, type Project } from "../model/types";
|
||||
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;
|
||||
querySelector(s: string): El | null;
|
||||
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;
|
||||
},
|
||||
querySelector(s) {
|
||||
for (const c of this.kids) {
|
||||
if (c.tag === s) return c;
|
||||
const r = c.querySelector(s);
|
||||
if (r) return r;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
get firstChild() {
|
||||
return this.kids[0] ?? null;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const textOf = (e: El): string => e.textContent + e.kids.map(textOf).join("");
|
||||
|
||||
/** `<text>`-Knoten des SVG mit Inhalt und ihren Run-Grössen (mm) und Zeilen-y. */
|
||||
function texts(svg: El): Array<{ text: string; sizes: number[]; ys: number[]; xs: number[] }> {
|
||||
return svg.kids
|
||||
.filter((k) => k.tag === "text")
|
||||
.map((t) => ({
|
||||
text: textOf(t),
|
||||
sizes: t.kids.flatMap((l) => l.kids.map((r) => Number(r.getAttribute("font-size")))),
|
||||
ys: t.kids.map((l) => Number(l.getAttribute("y"))),
|
||||
xs: t.kids.map((l) => Number(l.getAttribute("x"))),
|
||||
}));
|
||||
}
|
||||
|
||||
const N = 50;
|
||||
const K = 1000 / N; // Papier-mm je Meter
|
||||
|
||||
const project: Project = {
|
||||
...sampleProject,
|
||||
drawings2d: [
|
||||
...sampleProject.drawings2d,
|
||||
{ id: "t1", type: "drawing2d", levelId: "eg", categoryCode: "60", geom: { shape: "text", at: { x: 1, y: 2 }, text: "Einzeltext", height: 0.3, angle: 0 } },
|
||||
{
|
||||
id: "t2",
|
||||
type: "drawing2d",
|
||||
levelId: "eg",
|
||||
categoryCode: "60",
|
||||
geom: {
|
||||
shape: "text",
|
||||
at: { x: 1, y: 1 },
|
||||
text: "Titel\nzweite Zeile",
|
||||
height: 0.2,
|
||||
angle: 0,
|
||||
width: 4,
|
||||
doc: {
|
||||
version: 1,
|
||||
paragraphs: [
|
||||
{ runs: [{ text: "Titel", marks: { bold: true } }], align: "center" },
|
||||
{ runs: [{ text: "zweite ", marks: {} }, { text: "Zeile", marks: { italic: true } }] },
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{ id: "d1", type: "drawing2d", levelId: "eg", categoryCode: "60", geom: { shape: "dimension", pts: [{ x: 0, y: -2 }, { x: 5, y: -2 }], offset: 0.5 } },
|
||||
{ id: "k1", type: "drawing2d", levelId: "eg", categoryCode: "60", geom: { shape: "kote", at: { x: 6, y: 1 }, value: 0, kind: "okFertig", rotation: 0 } },
|
||||
] as Project["drawings2d"],
|
||||
};
|
||||
|
||||
describe("Druck-SVG: Textgrössen in Papier-mm", () => {
|
||||
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;
|
||||
});
|
||||
|
||||
const codes = new Set(flattenCategories(project.layers).map((c) => c.code));
|
||||
const print = () => {
|
||||
const plan = generatePlan(project, "eg", codes, undefined, "mittel", false, false, true, N);
|
||||
return texts(planToPrintSvg(plan, { scaleDenominator: N, pageWidthMm: 420, pageHeightMm: 297 }).svg as unknown as El);
|
||||
};
|
||||
|
||||
it("Einzeltext: Höhe 0.3 m bei 1:50 = 6 mm", () => {
|
||||
const t = print().find((x) => x.text === "Einzeltext")!;
|
||||
expect(t.sizes).toEqual([0.3 * K]);
|
||||
});
|
||||
|
||||
it("Rich-Text: Runs in Papier-mm, Zeilenvorschub 1.25 × Höhe, Mitte in der Spalte", () => {
|
||||
const t = print().find((x) => x.text.startsWith("Titel"))!;
|
||||
expect(t.sizes.every((s) => s === 0.2 * K)).toBe(true);
|
||||
expect(t.ys[1] - t.ys[0]).toBeCloseTo(0.2 * K * 1.25, 3);
|
||||
expect(t.xs[0] - t.xs[1]).toBeCloseTo((4 * K) / 2, 3);
|
||||
});
|
||||
|
||||
it("Masszahl in der Papier-Schrifthöhe (4 mm), Höhenkote lesbar gross", () => {
|
||||
const all = print();
|
||||
const dim = all.find((x) => x.text === "5.00")!;
|
||||
expect(dim.sizes[0]).toBeCloseTo(4, 3);
|
||||
const kote = all.find((x) => /0[.,]00/.test(x.text) && x !== dim);
|
||||
expect(kote).toBeDefined();
|
||||
expect(kote!.sizes[0]).toBeGreaterThan(1);
|
||||
});
|
||||
|
||||
it("Raumstempel im Viewport, Grösse in Papier-pt (10 pt)", () => {
|
||||
const stamp = print().find((x) => x.text.includes("Wohnen"))!;
|
||||
expect(stamp).toBeDefined();
|
||||
expect(stamp.sizes[0]).toBeCloseTo(10 * (25.4 / 72), 3);
|
||||
});
|
||||
});
|
||||
+37
-11
@@ -1,21 +1,47 @@
|
||||
// Tests für wrapDocToLines: Wortumbruch auf feste Breite unter Erhalt der
|
||||
// Run-Grenzen (gemischte Formatierung pro Zeile bleibt möglich).
|
||||
// Tests für layoutText: Wortumbruch auf feste Breite unter Erhalt der
|
||||
// Run-Grenzen (gemischte Formatierung pro Zeile bleibt möglich) und Lage der Zeilen.
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { docFromText, makeRun, type RichTextDoc } from "./richText";
|
||||
import { docToHtml, docToLines, runCssProps, wrapDocToLines } from "./renderHtml";
|
||||
import { annotationTextLines, docToHtml, layoutText, runCssProps } from "./renderHtml";
|
||||
|
||||
const OPTS = { x: 0, y: 0, lineHeight: 10, unitPerPt: 1, basePt: 10, color: "#000000" };
|
||||
|
||||
describe("wrapDocToLines", () => {
|
||||
it("ohne wrapWidthUnits identisch zu docToLines (eine Zeile je Absatz)", () => {
|
||||
const doc = docFromText("hallo welt");
|
||||
expect(wrapDocToLines(doc, OPTS)).toEqual(docToLines(doc, OPTS));
|
||||
describe("layoutText", () => {
|
||||
it("ohne wrapWidthUnits eine Zeile je Absatz", () => {
|
||||
const doc = docFromText("hallo welt\nzwei");
|
||||
expect(layoutText(doc, OPTS).map((l) => l.tspans.map((t) => t.text).join(""))).toEqual(["hallo welt", "zwei"]);
|
||||
expect(layoutText(doc, { ...OPTS, wrapWidthUnits: 0 })).toEqual(layoutText(doc, OPTS));
|
||||
});
|
||||
|
||||
it("Lage: erste Grundlinie am Anker, weitere um den Vorschub tiefer; Mitte/rechts in der Spalte", () => {
|
||||
const doc: RichTextDoc = {
|
||||
version: 1,
|
||||
paragraphs: [
|
||||
{ runs: [makeRun("a")] },
|
||||
{ runs: [makeRun("b")], align: "center", lineHeight: 2 },
|
||||
{ runs: [makeRun("c")], align: "right" },
|
||||
],
|
||||
};
|
||||
const at = (o: object) => layoutText(doc, { ...OPTS, x: 5, y: 7, ...o }).map((l) => [l.x, l.y, l.anchor]);
|
||||
expect(at({})).toEqual([[5, 7, "start"], [5, 27, "middle"], [5, 37, "end"]]);
|
||||
expect(at({ columnWidth: 40 })).toEqual([[5, 7, "start"], [25, 27, "middle"], [45, 37, "end"]]);
|
||||
expect(at({ columnWidth: NaN })).toEqual(at({}));
|
||||
});
|
||||
|
||||
it("Layout-Markup: Basis = Höhe in mm, Vorschub 1.25 × Höhe, Rahmen nur bei Breite > 0 umbrochen", () => {
|
||||
const doc = docFromText("eins zwei drei");
|
||||
const l = annotationTextLines({ xMm: 10, yMm: 20, heightMm: 4, widthMm: 15 }, doc);
|
||||
expect(l.map((x) => x.y)).toEqual([20, 25, 30]);
|
||||
expect(l[0].tspans[0]).toMatchObject({ fontSize: 4, fill: "#111111" });
|
||||
// Negative Breite: kein Umbruch, aber (wie früher) Spalte für die Lage.
|
||||
const r = annotationTextLines({ xMm: 10, yMm: 20, heightMm: 4, widthMm: -6 }, { version: 1, paragraphs: [{ runs: [makeRun("x")], align: "right" }] });
|
||||
expect([r.length, r[0].x]).toEqual([1, 4]);
|
||||
});
|
||||
|
||||
it("bricht lange Absätze wortweise auf mehrere Zeilen um", () => {
|
||||
const doc = docFromText("eins zwei drei vier fünf");
|
||||
const lines = wrapDocToLines(doc, { ...OPTS, wrapWidthUnits: 30 });
|
||||
const lines = layoutText(doc, { ...OPTS, wrapWidthUnits: 30 });
|
||||
expect(lines.length).toBeGreaterThan(1);
|
||||
const joined = lines.map((l) => l.tspans.map((t) => t.text).join("")).join(" ");
|
||||
expect(joined.replace(/\s+/g, " ").trim()).toBe("eins zwei drei vier fünf");
|
||||
@@ -34,7 +60,7 @@ describe("wrapDocToLines", () => {
|
||||
},
|
||||
],
|
||||
};
|
||||
const lines = wrapDocToLines(doc, { ...OPTS, wrapWidthUnits: 1000 });
|
||||
const lines = layoutText(doc, { ...OPTS, wrapWidthUnits: 1000 });
|
||||
expect(lines).toHaveLength(1);
|
||||
const boldTspan = lines[0].tspans.find((t) => t.text === "fett");
|
||||
expect(boldTspan?.fontWeight).toBe("700");
|
||||
@@ -44,14 +70,14 @@ describe("wrapDocToLines", () => {
|
||||
|
||||
it("leerer Absatz erzeugt eine leere Zeile (Vorschub bleibt erhalten)", () => {
|
||||
const doc: RichTextDoc = { version: 1, paragraphs: [{ runs: [] }, { runs: [makeRun("x")] }] };
|
||||
const lines = wrapDocToLines(doc, { ...OPTS, wrapWidthUnits: 100 });
|
||||
const lines = layoutText(doc, { ...OPTS, wrapWidthUnits: 100 });
|
||||
expect(lines).toHaveLength(2);
|
||||
expect(lines[0].tspans).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("sehr schmale Breite bricht auf ein Wort je Zeile um, ohne Wörter zu verlieren", () => {
|
||||
const doc = docFromText("aa bb cc");
|
||||
const lines = wrapDocToLines(doc, { ...OPTS, wrapWidthUnits: 1 });
|
||||
const lines = layoutText(doc, { ...OPTS, wrapWidthUnits: 1 });
|
||||
const words = lines.map((l) => l.tspans.map((t) => t.text).join("").trim());
|
||||
expect(words).toEqual(["aa", "bb", "cc"]);
|
||||
});
|
||||
|
||||
+59
-46
@@ -4,7 +4,7 @@
|
||||
// bilden dieselbe Mark→Stil-Zuordnung ab, damit Editor-Vorschau und gezeichneter
|
||||
// Text identisch aussehen. Reines Modul: keine React-/App-Abhängigkeit.
|
||||
//
|
||||
// Das Zeilen-Layout (`docToLines`/`wrapDocToLines`: Run→Tspan, Wortumbruch) rechnet der Rust-Kern
|
||||
// Das Zeilen-Layout (`layoutText`: Run→Tspan, Wortumbruch, Lage x/y/Anker je Zeile) rechnet der Rust-Kern
|
||||
// (`src-tauri/core/src/text_layout.rs`, Bindung `text_lines`); hier nur die Fassade.
|
||||
|
||||
import type { Marks, Paragraph, RichTextDoc, TextRun } from "./richText";
|
||||
@@ -145,18 +145,24 @@ export interface SvgTspan {
|
||||
baselineShift?: number;
|
||||
}
|
||||
|
||||
/** Eine gerenderte Zeile (= Absatz) mit ihren Runs. */
|
||||
/** Eine gesetzte Zeile (Absatz bzw. umbrochene Teilzeile) mit ihren Runs und ihrer Lage. */
|
||||
export interface SvgLine {
|
||||
tspans: SvgTspan[];
|
||||
align: "left" | "center" | "right";
|
||||
/** Zeilenhöhe dieses Absatzes (Vielfaches der Basisgrösse), s. `Paragraph.lineHeight`. */
|
||||
/** SVG-`text-anchor` zur Ausrichtung. */
|
||||
anchor: "start" | "middle" | "end";
|
||||
/** Vorschub zu dieser Zeile (Vielfaches der Basisgrösse bzw. `lineHeight`), s. `Paragraph.lineHeight`. */
|
||||
lineHeight: number;
|
||||
/** x der Zeile (Anker, mit Spalte Mitte/rechts darin). */
|
||||
x: number;
|
||||
/** Grundlinie der Zeile (Benutzereinheiten, nach unten wachsend). */
|
||||
y: number;
|
||||
}
|
||||
|
||||
export interface SvgTextOptions {
|
||||
/** Ankerpunkt X in Benutzereinheiten. */
|
||||
x: number;
|
||||
/** Baseline der ERSTEN Zeile, Y in Benutzereinheiten. */
|
||||
/** Baseline der ERSTEN Zeile, Y in Benutzereinheiten (weitere Zeilen tiefer). */
|
||||
y: number;
|
||||
/**
|
||||
* Zeilenvorschub (Baseline zu Baseline) in Benutzereinheiten für Zeilen OHNE
|
||||
@@ -174,9 +180,17 @@ export interface SvgTextOptions {
|
||||
basePt?: number;
|
||||
/** Standardfarbe "#rrggbb" für Runs ohne Farbe. */
|
||||
color?: string;
|
||||
/**
|
||||
* Wortumbruch auf diese Breite (gleiche Einheiten; ≤ 0/fehlt = keiner). Wortbreite geschätzt
|
||||
* (UTF-16-Länge × Schriftgrösse × 0.55, kein Font-Messen); Run-Grenzen bleiben.
|
||||
*/
|
||||
wrapWidthUnits?: number;
|
||||
/** Spaltenbreite für die Lage: Mitte/rechts ausgerichtete Zeilen liegen darin (0/NaN/fehlt = am Anker). */
|
||||
columnWidth?: number;
|
||||
}
|
||||
|
||||
const ALIGNS = ["left", "center", "right"] as const;
|
||||
const ANCHORS = ["start", "middle", "end"] as const;
|
||||
|
||||
/** UTF-16-Einheiten `f[at…at+n)` als Text. */
|
||||
function unitsText(f: Float64Array, at: number, n: number): string {
|
||||
@@ -186,13 +200,10 @@ function unitsText(f: Float64Array, at: number, n: number): string {
|
||||
}
|
||||
|
||||
/**
|
||||
* Flache Zeilen des Kerns (Format s. `src-tauri/core/src/web/text_layout.rs`) → `SvgLine[]`, mit
|
||||
* `withY` samt Grundlinie `y`. Die Tspan-Texte sind Ausschnitte der Eingabe (`text(Absatz, Run)`).
|
||||
* Feldreihenfolge der Tspans wie früher in TS.
|
||||
* Gesetzte Zeilen des Kerns (Format s. `src-tauri/core/src/web/text_layout.rs`) → `SvgLine[]`. Die
|
||||
* Tspan-Texte sind Ausschnitte der Eingabe (`text(Absatz, Run)`). Feldreihenfolge der Tspans wie früher in TS.
|
||||
*/
|
||||
function decodeLines(f: Float64Array, text: (para: number, run: number) => string, withY: true): PlacedLine[];
|
||||
function decodeLines(f: Float64Array, text: (para: number, run: number) => string, withY: false): SvgLine[];
|
||||
function decodeLines(f: Float64Array, text: (para: number, run: number) => string, withY: boolean): Array<SvgLine | PlacedLine> {
|
||||
function decodeLines(f: Float64Array, text: (para: number, run: number) => string): SvgLine[] {
|
||||
if (f.length < 2) return [];
|
||||
let k = 2;
|
||||
const styles: Array<Omit<SvgTspan, "text">> = [];
|
||||
@@ -213,34 +224,40 @@ function decodeLines(f: Float64Array, text: (para: number, run: number) => strin
|
||||
if (bits & 16) st.baselineShift = shift;
|
||||
styles.push(st);
|
||||
}
|
||||
const out: Array<SvgLine | PlacedLine> = new Array(f[0]);
|
||||
const out: SvgLine[] = new Array(f[0]);
|
||||
for (let i = 0; i < f[0]; i++) {
|
||||
const align = ALIGNS[f[k]] ?? "left";
|
||||
const a = f[k];
|
||||
const lineHeight = f[k + 1];
|
||||
const y = f[k + 2];
|
||||
const n = f[k + 3];
|
||||
k += 4;
|
||||
const x = f[k + 2];
|
||||
const y = f[k + 3];
|
||||
const n = f[k + 4];
|
||||
k += 5;
|
||||
const tspans: SvgTspan[] = new Array(n);
|
||||
for (let j = 0; j < n; j++, k += 5) {
|
||||
const src = text(f[k + 1], f[k + 2]);
|
||||
tspans[j] = { text: f[k + 3] === 0 && f[k + 4] === src.length ? src : src.slice(f[k + 3], f[k + 4]), ...styles[f[k]] };
|
||||
}
|
||||
out[i] = withY ? { tspans, align, lineHeight, y } : { tspans, align, lineHeight };
|
||||
out[i] = { tspans, align: ALIGNS[a] ?? "left", anchor: ANCHORS[a] ?? "start", lineHeight, x, y };
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Zuletzt gesetzte Zeilen je Dokument-Objekt und Optionen (Neuzeichnen beim Zoomen/Verschieben). */
|
||||
/** Zuletzt gesetzte Zeilen je Dokument-Objekt und Optionen (Neuzeichnen ohne Änderung). */
|
||||
const memo = new WeakMap<RichTextDoc, Map<string, SvgLine[]>>();
|
||||
|
||||
/** Zeilen aus dem Kern (`text_lines`); kaputte Eingabe → keine Zeilen. Ergebnis nicht verändern (geteilt). */
|
||||
function kernelLines(doc: RichTextDoc, opts: SvgTextOptions, wrap: number | undefined): SvgLine[] {
|
||||
const key = `${opts.lineHeight}|${opts.unitPerPt}|${opts.basePt}|${wrap}|${opts.color}`;
|
||||
/**
|
||||
* Setzt das Dokument (Kern `text_lines`: Runs → Tspans mit Grössen in Benutzereinheiten, eine Zeile
|
||||
* je Absatz bzw. umbrochene Teilzeile, Lage x/y/Anker je Zeile). Der Aufrufer legt nur noch die
|
||||
* Knoten an. Kaputte Eingabe → keine Zeilen. Ergebnis nicht verändern (geteilt).
|
||||
*/
|
||||
export function layoutText(doc: RichTextDoc, opts: SvgTextOptions): SvgLine[] {
|
||||
const { x, y, lineHeight, unitPerPt, basePt, color, wrapWidthUnits, columnWidth } = opts;
|
||||
const key = `${lineHeight}|${unitPerPt}|${basePt}|${wrapWidthUnits}|${color}|${x}|${y}|${columnWidth}`;
|
||||
let byOpts = memo.get(doc);
|
||||
const hit = byOpts?.get(key);
|
||||
if (hit) return hit;
|
||||
const o = Float64Array.of(opts.lineHeight, opts.unitPerPt, opts.basePt == null ? 0 : 1, opts.basePt ?? 0, wrap ?? NaN);
|
||||
const lines = decodeLines(core().text_lines(JSON.stringify(doc), o, opts.color ?? undefined), (p, r) => doc.paragraphs[p].runs[r].text, false);
|
||||
const o = Float64Array.of(lineHeight, unitPerPt, basePt == null ? 0 : 1, basePt ?? 0, wrapWidthUnits ?? NaN, x, y, columnWidth ?? NaN);
|
||||
const lines = decodeLines(core().text_lines(JSON.stringify(doc), o, color ?? undefined), (p, r) => doc.paragraphs[p].runs[r].text);
|
||||
if (!byOpts) memo.set(doc, (byOpts = new Map()));
|
||||
if (byOpts.size > 8) byOpts.clear();
|
||||
byOpts.set(key, lines);
|
||||
@@ -248,44 +265,40 @@ function kernelLines(doc: RichTextDoc, opts: SvgTextOptions, wrap: number | unde
|
||||
}
|
||||
|
||||
/**
|
||||
* Zerlegt das Dokument in Zeilen/Tspans mit bereits in Benutzereinheiten
|
||||
* umgerechneten Grössen (eine Zeile je Absatz). Der Aufrufer positioniert die Zeilen selbst
|
||||
* (Baseline der Zeile i = `y + Σ lineHeight[0..i-1]`).
|
||||
* Zeilen eines Text-Markups auf dem Layout-Blatt (Papier-mm; Layout-Blatt und Layout-PDF): Höhe
|
||||
* `heightMm` als Basisgrösse, Vorschub 1.25 × Höhe, erste Grundlinie bei `yMm`; mit Rahmenbreite
|
||||
* (`widthMm` > 0) wortweise umbrochen, Mitte/rechts im Rahmen (jede Breite ≠ 0 zählt dafür, wie früher).
|
||||
*/
|
||||
export function docToLines(doc: RichTextDoc, opts: SvgTextOptions): SvgLine[] {
|
||||
return kernelLines(doc, opts, undefined);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wie {@link docToLines}, bricht aber zusätzlich wortweise auf `wrapWidthUnits`
|
||||
* um (in denselben Benutzereinheiten wie `SvgTextOptions.x`) — für Textfelder/
|
||||
* -rahmen mit fester Breite (InDesign-artig). Wortbreite geschätzt (UTF-16-Länge ×
|
||||
* Schriftgrösse × 0.55, kein Font-Messen). Ohne `wrapWidthUnits` identisch zu `docToLines`.
|
||||
* Respektiert Run-Grenzen: ein Wort behält die Formatierung seines Runs.
|
||||
*/
|
||||
export function wrapDocToLines(
|
||||
export function annotationTextLines(
|
||||
ann: { xMm: number; yMm: number; heightMm: number; widthMm?: number; color?: string },
|
||||
doc: RichTextDoc,
|
||||
opts: SvgTextOptions & { wrapWidthUnits?: number },
|
||||
): SvgLine[] {
|
||||
return kernelLines(doc, opts, opts.wrapWidthUnits);
|
||||
return layoutText(doc, {
|
||||
x: ann.xMm,
|
||||
y: ann.yMm,
|
||||
lineHeight: ann.heightMm * 1.25,
|
||||
unitPerPt: 25.4 / 72,
|
||||
basePt: (ann.heightMm / 25.4) * 72,
|
||||
color: ann.color ?? "#111111",
|
||||
wrapWidthUnits: ann.widthMm && ann.widthMm > 0 ? ann.widthMm : undefined,
|
||||
columnWidth: ann.widthMm,
|
||||
});
|
||||
}
|
||||
|
||||
/** Eine gesetzte Zeile mit ihrer Grundlinie `y` (Bildschirm: nach unten wachsend). */
|
||||
export type PlacedLine = SvgLine & { y: number };
|
||||
|
||||
/**
|
||||
* Raumstempel-Block (Kern `stamp_block`): Rich-Text-Zeilen (Vorschub 1.3 × Basisgrösse bzw. die
|
||||
* Absatz-Zeilenhöhe) und darunter die Live-Zeilen (0.8 × Basisgrösse), vertikal um `y0` zentriert.
|
||||
* `stamp` ist das Plan-Primitiv `text`.
|
||||
* Absatz-Zeilenhöhe) und darunter die Live-Zeilen (0.8 × Basisgrösse), vertikal um `y0` zentriert,
|
||||
* alle bei `x0`. `stamp` ist das Plan-Primitiv `text`.
|
||||
*/
|
||||
export function stampBlockLines(
|
||||
stamp: { at: { x: number; y: number }; doc: RichTextDoc; extraLines: ReadonlyArray<{ text: string; align: string }>; basePt: number; color?: string },
|
||||
unitPerPt: number,
|
||||
x0: number,
|
||||
y0: number,
|
||||
): PlacedLine[] {
|
||||
): SvgLine[] {
|
||||
const n = stamp.doc.paragraphs.length;
|
||||
const text = (p: number, r: number): string => (p < n ? stamp.doc.paragraphs[p].runs[r].text : stamp.extraLines[p - n].text);
|
||||
return decodeLines(core().text_stamp_block(JSON.stringify(stamp), unitPerPt, y0), text, true);
|
||||
return decodeLines(core().text_stamp_block(JSON.stringify(stamp), unitPerPt, x0, y0), text);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+4
-21
@@ -59,7 +59,7 @@ import {
|
||||
} from "./layoutSheetMath";
|
||||
import { RichTextEditor } from "../editors/RichTextEditor";
|
||||
import { docFromText } from "../text/richText";
|
||||
import { wrapDocToLines, type SvgLine } from "../text/renderHtml";
|
||||
import { annotationTextLines } from "../text/renderHtml";
|
||||
import { contentFontBaselineFromTop, TEXT_LINE_HEIGHT_MULT } from "../text/fontMetrics";
|
||||
import { readImageFile, defaultImageSizeMm } from "../io/imageAsset";
|
||||
import { rasterizePdfPages } from "../io/pdfImport";
|
||||
@@ -1490,31 +1490,14 @@ function AnnotationSvg({
|
||||
}
|
||||
// Text: reiches Doc (mehrere Absätze/gemischte Formatierung) hat Vorrang vor
|
||||
// dem einfachen String — bricht bei gesetzter Rahmenbreite (`widthMm`)
|
||||
// wortweise um (Run-Grenzen bleiben erhalten, s. `wrapDocToLines`).
|
||||
// wortweise um (Run-Grenzen bleiben erhalten); Zeilen samt Lage aus dem Kern (`layoutText`).
|
||||
if (ann.doc) {
|
||||
const lineHeight = ann.heightMm * 1.25;
|
||||
const lines: SvgLine[] = wrapDocToLines(ann.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 lines = annotationTextLines(ann, ann.doc);
|
||||
return (
|
||||
<text opacity={opacity}>
|
||||
{lines.map((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;
|
||||
return (
|
||||
<tspan key={k} x={x} y={y} textAnchor={anchor}>
|
||||
<tspan key={k} x={line.x} y={line.y} textAnchor={line.anchor}>
|
||||
{line.tspans.length === 0
|
||||
? ""
|
||||
: line.tspans.map((ts, j) => (
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
import type { HatchRender, Primitive } from "../../../core";
|
||||
import { NO_HATCH } from "../../../core";
|
||||
import { stampBlockLines, wrapDocToLines, wrapPlainText, type SvgLine } from "../../../text/renderHtml";
|
||||
import { layoutText, stampBlockLines, wrapPlainText } from "../../../text/renderHtml";
|
||||
import type { Vec2 } from "../../../model/types";
|
||||
import { quantizePen } from "../../../export/sceneToPrintSvg";
|
||||
import { buildRandomHatchRuns, zigzagPoints, motifPoints, DASH_MM_TO_M } from "../../../core";
|
||||
@@ -709,23 +709,17 @@ export function renderPrimitive(
|
||||
const unitPerPt = (1 / 72) * 0.0254 * nRef * PX_PER_M;
|
||||
// Rich-Text-Zeilen (Stempel-Doc) + LIVE-Zusatzzeilen (Fläche/SIA) darunter, Block vertikal
|
||||
// um den Anker zentriert (Kern `stamp_block`, absatzeigene Zeilenhöhen inbegriffen).
|
||||
const lines = stampBlockLines(p, unitPerPt, c.y);
|
||||
const lines = stampBlockLines(p, unitPerPt, c.x, c.y);
|
||||
if (lines.length === 0) return null;
|
||||
return (
|
||||
<text style={{ pointerEvents: "none", userSelect: "none" }}>
|
||||
{lines.map((line, k) => {
|
||||
const anchor =
|
||||
line.align === "center"
|
||||
? "middle"
|
||||
: line.align === "right"
|
||||
? "end"
|
||||
: "start";
|
||||
return (
|
||||
<tspan
|
||||
key={k}
|
||||
x={c.x}
|
||||
x={line.x}
|
||||
y={line.y}
|
||||
textAnchor={anchor}
|
||||
textAnchor={line.anchor}
|
||||
>
|
||||
{line.tspans.map((ts, j) => (
|
||||
<tspan
|
||||
@@ -776,30 +770,20 @@ export function renderPrimitive(
|
||||
// Reiches Textfeld (InDesign-artig, mehrere Absätze/gemischte Formatierung):
|
||||
// hat Vorrang vor dem Alt-Pfad (einfacher String + einheitliche `marks`)
|
||||
// weiter unten. Bricht bei gesetzter Spaltenbreite wortweise um (Run-Grenzen
|
||||
// bleiben erhalten, s. `wrapDocToLines`).
|
||||
// bleiben erhalten); Zeilen samt Lage aus dem Kern (`layoutText`).
|
||||
if (p.doc) {
|
||||
const unitPerPt = (0.0254 / 72) * PX_PER_M;
|
||||
const basePt = (p.heightM / 0.0254) * 72;
|
||||
const lineHeight = fontSize * 1.25;
|
||||
const wrapWidthUnits = p.wrapWidth && p.wrapWidth > 0 ? p.wrapWidth * PX_PER_M : undefined;
|
||||
const docLines: SvgLine[] = wrapDocToLines(p.doc, {
|
||||
x: 0,
|
||||
y: 0,
|
||||
lineHeight,
|
||||
unitPerPt,
|
||||
basePt,
|
||||
const docLines = layoutText(p.doc, {
|
||||
x: c.x,
|
||||
y: c.y,
|
||||
lineHeight: fontSize * 1.25,
|
||||
unitPerPt: (0.0254 / 72) * PX_PER_M,
|
||||
basePt: (p.heightM / 0.0254) * 72,
|
||||
color: p.color,
|
||||
...(wrapWidthUnits ? { wrapWidthUnits } : {}),
|
||||
wrapWidthUnits,
|
||||
columnWidth: wrapWidthUnits,
|
||||
});
|
||||
if (docLines.length === 0) return null;
|
||||
const lineYs: number[] = [];
|
||||
{
|
||||
let y = c.y;
|
||||
docLines.forEach((line, k) => {
|
||||
if (k > 0) y += line.lineHeight;
|
||||
lineYs.push(y);
|
||||
});
|
||||
}
|
||||
return (
|
||||
<text
|
||||
opacity={p.greyed ? 0.45 : undefined}
|
||||
@@ -807,17 +791,8 @@ export function renderPrimitive(
|
||||
style={{ pointerEvents: "none", userSelect: "none" }}
|
||||
>
|
||||
{docLines.map((line, k) => {
|
||||
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;
|
||||
return (
|
||||
<tspan key={k} x={x} y={lineYs[k]} textAnchor={anchor}>
|
||||
<tspan key={k} x={line.x} y={line.y} textAnchor={line.anchor}>
|
||||
{line.tspans.length === 0
|
||||
? ""
|
||||
: line.tspans.map((ts, j) => (
|
||||
|
||||
Reference in new issue
Block a user