[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,8 +6,8 @@
|
||||
//! Polylinie, Bögen als `<path>`.
|
||||
//!
|
||||
//! In TS bleiben: der `<svg>`-Rahmen und das weisse Blatt und die Bild-Schraffur-`<pattern>`
|
||||
//! (laufende Ids im Dokument). Die Einzeltexte setzt der Kern (`crate::text_layout`, Zeilen als
|
||||
//! `<tspan>` je Zeile mit je einem `<tspan>` je Run/Wort).
|
||||
//! (laufende Ids im Dokument). Einzeltexte und Raumstempel setzt der Kern (`crate::text_layout`,
|
||||
//! Zeilen als `<tspan>` je Zeile mit je einem `<tspan>` je Run/Wort), Grössen in Papier-mm.
|
||||
//!
|
||||
//! Format: Datensätze getrennt durch `\x1e`, Felder durch `\x1f`:
|
||||
//! - `E`, Ebene (0 = ins `<svg>`, 1 = in die offene Gruppe), Tag, dann Name/Wert-Paare;
|
||||
@@ -24,10 +24,10 @@
|
||||
use crate::hatch::{self, Hatch, Motif, Pattern, V2};
|
||||
use crate::js_fmt::js_number;
|
||||
use crate::jsnum;
|
||||
use crate::plan_buf::{whole, Rec, View, F_COLOR, F_DASH_NULL, F_OPACITY, T_ARC, T_CIRCLE, T_DRAWING_ARC, T_DRAWING_TEXT, T_IMAGE, T_LINE, T_POLYGON};
|
||||
use crate::plan_buf::{whole, Rec, View, F_COLOR, F_DASH_NULL, F_OPACITY, T_ARC, T_CIRCLE, T_DRAWING_ARC, T_DRAWING_TEXT, T_IMAGE, T_LINE, T_POLYGON, T_TEXT};
|
||||
use crate::plan_parts::{greyed_of, Part};
|
||||
use crate::plan_text::SingleText;
|
||||
use crate::text_layout::{wrap_doc_to_lines, LayoutOpts, LineAlign, TEXT_LINE_HEIGHT_MULT};
|
||||
use crate::plan_text::{SingleText, StampText};
|
||||
use crate::text_layout::{place_lines, stamp_block, wrap_doc_to_lines, Line, LayoutOpts, LineAlign, PT_TO_MM, TEXT_LINE_HEIGHT_MULT};
|
||||
use std::f64::consts::PI;
|
||||
|
||||
/// Optionen: Massstab-Nenner, Blatt (mm) und Ausmass des Plans (`Plan.bounds`).
|
||||
@@ -414,6 +414,15 @@ fn entry(out: &mut Out, view: &View, i: usize, hatches: &[(i32, Hatch, i32)], m:
|
||||
}
|
||||
out.el(0, "path", &attrs);
|
||||
}
|
||||
T_TEXT => {
|
||||
let js = view.string(whole(f[0])?)?;
|
||||
let j: serde_json::Value = serde_json::from_str(&js).ok()?;
|
||||
let jg = j.get("greyed").and_then(|g| g.as_bool()).unwrap_or(false);
|
||||
let obj = j.get("opacity").and_then(|o| o.as_f64()).unwrap_or(1.0);
|
||||
if let Ok(t) = serde_json::from_str::<StampText>(&js) {
|
||||
stamp_node(out, &t, m, opacity_of(jg, obj));
|
||||
}
|
||||
}
|
||||
T_DRAWING_TEXT | T_IMAGE => {
|
||||
let js = view.string(whole(f[0])?)?;
|
||||
let j: serde_json::Value = serde_json::from_str(&js).ok()?;
|
||||
@@ -458,14 +467,18 @@ fn entry(out: &mut Out, view: &View, i: usize, hatches: &[(i32, Hatch, i32)], m:
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// Einzeltext (Text-Werkzeug/DXF-Import) als `<text>` mit `<tspan>`-Zeilen (früher
|
||||
/// `drawingTextNode` in `planToPrintSvg.ts`): Grösse in Papier-mm, bei Spaltenbreite wortweise
|
||||
/// umbrochen (Run-Grenzen bleiben), gedreht um den Anker.
|
||||
/// Einzeltext (Text-Werkzeug/DXF-Import, auch Masszahlen und Koten) als `<text>` mit
|
||||
/// `<tspan>`-Zeilen (früher `drawingTextNode` in `planToPrintSvg.ts`): Grösse in Papier-mm (Höhe ×
|
||||
/// mm je Meter, wie die Plan-Ansicht beim Massstab), bei Spaltenbreite wortweise umbrochen
|
||||
/// (Run-Grenzen bleiben), gedreht um den Anker. Run-eigene `sizePt` wie in der SVG-Plan-Ansicht als
|
||||
/// Modell-pt (1 pt = 0.0254/72 m), also auch × mm je Meter.
|
||||
fn text_node(out: &mut Out, t: &SingleText, m: Map, opacity: String) {
|
||||
let k = m.k;
|
||||
let c = m.at(t.at.x, t.at.y);
|
||||
let unit_per_pt = (0.0254 / 72.0) * k;
|
||||
let base_pt = if unit_per_pt > 0.0 { t.height_m / unit_per_pt } else { 0.0 };
|
||||
// Basisgrösse in Modell-pt (bis 30.10. `height_m / unit_per_pt`: Meter durch mm je pt — die
|
||||
// Runs kamen in Metern statt Papier-mm heraus, im Layout k-mal zu klein).
|
||||
let base_pt = if unit_per_pt > 0.0 { t.height_m * k / unit_per_pt } else { 0.0 };
|
||||
let wrap = t.wrap().map(|w| w * k);
|
||||
let o = LayoutOpts { line_height: t.height_m * k * TEXT_LINE_HEIGHT_MULT, unit_per_pt, base_pt, color: t.color.as_deref().unwrap_or("#000000") };
|
||||
let lines = wrap_doc_to_lines(&t.doc(), &o, wrap);
|
||||
@@ -481,36 +494,46 @@ fn text_node(out: &mut Out, t: &SingleText, m: Map, opacity: String) {
|
||||
attrs.push(("opacity", opacity));
|
||||
}
|
||||
out.tagged(&["X"], &attrs);
|
||||
// `!wrapWidthUnits`: 0/NaN zählen wie keine Spalte.
|
||||
let col = wrap.filter(|w| *w != 0.0 && !w.is_nan());
|
||||
let mut y = c.y;
|
||||
for (n, line) in lines.iter().enumerate() {
|
||||
if n > 0 {
|
||||
y += line.line_height;
|
||||
}
|
||||
let (anchor, x) = match (line.align, col) {
|
||||
(LineAlign::Center, Some(w)) => ("middle", c.x + w / 2.0),
|
||||
(LineAlign::Right, Some(w)) => ("end", c.x + w),
|
||||
(LineAlign::Center, None) => ("middle", c.x),
|
||||
(LineAlign::Right, None) => ("end", c.x),
|
||||
(LineAlign::Left, _) => ("start", c.x),
|
||||
};
|
||||
let at = place_lines(lines.iter().map(|l| (l.align, l.line_height)), c.x, c.y, wrap);
|
||||
for (line, (x, y)) in lines.iter().zip(at) {
|
||||
let content = if line.tspans.is_empty() { "\u{200b}" } else { "" };
|
||||
out.tagged(&["L", content], &[("x", r3(x)), ("y", r3(y)), ("text-anchor", anchor.to_string())]);
|
||||
for ts in &line.tspans {
|
||||
let mut a: Vec<(&str, String)> = vec![("font-size", r3(ts.font_size)), ("fill", ts.fill.clone())];
|
||||
if let Some(w) = ts.font_weight() {
|
||||
a.push(("font-weight", w.to_string()));
|
||||
}
|
||||
if let Some(st) = ts.font_style() {
|
||||
a.push(("font-style", st.to_string()));
|
||||
}
|
||||
a.push(("font-family", ts.font_family.clone().unwrap_or_else(|| "Helvetica, Arial, sans-serif".to_string())));
|
||||
if let Some(d) = ts.text_decoration {
|
||||
a.push(("text-decoration", d.to_string()));
|
||||
}
|
||||
out.tagged(&["R", &ts.text], &a);
|
||||
line_node(out, line, content, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
/// Zeilen-`<tspan>` mit je einem Run-`<tspan>`.
|
||||
fn line_node(out: &mut Out, line: &Line, content: &str, x: f64, y: f64) {
|
||||
out.tagged(&["L", content], &[("x", r3(x)), ("y", r3(y)), ("text-anchor", line.align.anchor().to_string())]);
|
||||
for ts in &line.tspans {
|
||||
let mut a: Vec<(&str, String)> = vec![("font-size", r3(ts.font_size)), ("fill", ts.fill.clone())];
|
||||
if let Some(w) = ts.font_weight() {
|
||||
a.push(("font-weight", w.to_string()));
|
||||
}
|
||||
if let Some(st) = ts.font_style() {
|
||||
a.push(("font-style", st.to_string()));
|
||||
}
|
||||
a.push(("font-family", ts.font_family.clone().unwrap_or_else(|| "Helvetica, Arial, sans-serif".to_string())));
|
||||
if let Some(d) = ts.text_decoration {
|
||||
a.push(("text-decoration", d.to_string()));
|
||||
}
|
||||
out.tagged(&["R", &ts.text], &a);
|
||||
}
|
||||
}
|
||||
|
||||
/// Raumstempel als `<text>` (fehlte im Druck-SVG bis 30.10.): wie die Plan-Ansicht im Druckmodus —
|
||||
/// Rich-Text-Zeilen und Live-Zeilen (Fläche, Nutzung) als Block um den Anker zentriert
|
||||
/// ([`stamp_block`]), Grösse in Papier-pt (unabhängig vom Massstab).
|
||||
fn stamp_node(out: &mut Out, t: &StampText, m: Map, opacity: String) {
|
||||
let c = m.at(t.at.x, t.at.y);
|
||||
let extra: Vec<(&str, LineAlign)> = t.extra_lines.iter().map(|l| (l.text.as_str(), LineAlign::of(Some(l.align)))).collect();
|
||||
let placed = stamp_block(&t.doc, &extra, t.base_pt, PT_TO_MM, t.color.as_deref().unwrap_or("#000000"), c.y);
|
||||
if placed.is_empty() {
|
||||
return;
|
||||
}
|
||||
let attrs: Vec<(&str, String)> = if opacity.is_empty() { Vec::new() } else { vec![("opacity", opacity)] };
|
||||
out.tagged(&["X"], &attrs);
|
||||
for p in &placed {
|
||||
line_node(out, &p.line, "", c.x, p.y);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -565,21 +588,57 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn einzeltext_als_zeilen_und_runs() {
|
||||
let js = "{\"kind\":\"drawingText\",\"at\":{\"x\":1,\"y\":2},\"text\":\"aa bb\",\"heightM\":0.5,\"angle\":0.5,\"color\":\"#123\",\"wrapWidth\":0.6,\"marks\":{\"bold\":true}}";
|
||||
let js = "{\"kind\":\"drawingText\",\"at\":{\"x\":1,\"y\":2},\"text\":\"aa bb\",\"heightM\":0.5,\"angle\":0.5,\"color\":\"#123\",\"wrapWidth\":2,\"marks\":{\"bold\":true}}";
|
||||
let t: SingleText = serde_json::from_str(js).unwrap();
|
||||
let m = Map { off_x: 0.0, off_y: 0.0, min_x: 0.0, max_y: 10.0, k: 10.0 };
|
||||
let mut out = Out { s: String::new(), first: true };
|
||||
text_node(&mut out, &t, m, "0.5".into());
|
||||
let recs: Vec<Vec<&str>> = out.s.split('\u{1e}').map(|r| r.split('\u{1f}').collect()).collect();
|
||||
// Drehung um den Anker (10, 80 mm), eine Zeile (Spalte 6 mm), je Wort/Leerraum ein Run.
|
||||
// Drehung um den Anker (10, 80 mm), eine Zeile (Spalte 20 mm), je Wort/Leerraum ein Run.
|
||||
assert_eq!(recs[0], ["X", "transform", "rotate(-28.648 10 80)", "opacity", "0.5"]);
|
||||
assert_eq!(recs[1], ["L", "", "x", "10", "y", "80", "text-anchor", "start"]);
|
||||
// Run-Grösse = heightM (wie die frühere TS-Fassung: `basePt · unitPerPt` mit `unitPerPt` in
|
||||
// pt→mm, also Meter statt Papier-mm — bekannte Abweichung, bewusst unverändert übernommen).
|
||||
assert_eq!(recs[2], ["R", "aa", "font-size", "0.5", "fill", "#123", "font-weight", "700", "font-family", "Helvetica, Arial, sans-serif"]);
|
||||
// Run-Grösse in Papier-mm: 0.5 m × 10 mm/m (wie die Plan-Ansicht beim Massstab).
|
||||
assert_eq!(recs[2], ["R", "aa", "font-size", "5", "fill", "#123", "font-weight", "700", "font-family", "Helvetica, Arial, sans-serif"]);
|
||||
assert_eq!((recs[3][1], recs[4][1], recs.len()), (" ", "bb", 5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn einzeltext_umbruch_und_zeilen_in_papier_mm() {
|
||||
// Spalte 2 m = 20 mm, Höhe 0.25 m = 2.5 mm: «aaaa bbbb» (je 4 × 2.5 × 0.55 = 5.5 mm) passt in
|
||||
// eine Zeile; Run mit eigener Grösse 72 pt = 1 Modell-Zoll = 0.254 mm bei 10 mm/m.
|
||||
let js = r##"{"at":{"x":0,"y":10},"text":"","heightM":0.25,"angle":0,"wrapWidth":2,"doc":{"version":1,"paragraphs":[{"runs":[{"text":"aaaa bbbb","marks":{}}]},{"runs":[{"text":"c","marks":{"sizePt":72}}],"lineHeight":2}]}}"##;
|
||||
let t: SingleText = serde_json::from_str(js).unwrap();
|
||||
let m = Map { off_x: 0.0, off_y: 0.0, min_x: 0.0, max_y: 10.0, k: 10.0 };
|
||||
let mut out = Out { s: String::new(), first: true };
|
||||
text_node(&mut out, &t, m, String::new());
|
||||
let recs: Vec<Vec<&str>> = out.s.split('\u{1e}').map(|r| r.split('\u{1f}').collect()).collect();
|
||||
let lines: Vec<&Vec<&str>> = recs.iter().filter(|r| r[0] == "L").collect();
|
||||
assert_eq!(lines.len(), 2);
|
||||
// Absatz-Zeilenhöhe 2 × Basisgrösse (2.5 mm).
|
||||
assert_eq!((lines[0][5], lines[1][5]), ("0", "5"));
|
||||
let sizes: Vec<&str> = recs.iter().filter(|r| r[0] == "R").map(|r| r[3]).collect();
|
||||
assert_eq!(sizes, ["2.5", "2.5", "2.5", "0.254"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raumstempel_als_block_in_papier_pt() {
|
||||
let js = r##"{"at":{"x":2,"y":3},"doc":{"version":1,"paragraphs":[{"runs":[{"text":"Wohnen","marks":{"bold":true}}],"align":"center"}]},"extraLines":[{"text":"17.01 m²","align":"center"}],"basePt":10,"color":"#333"}"##;
|
||||
let t: StampText = serde_json::from_str(js).unwrap();
|
||||
let m = Map { off_x: 0.0, off_y: 0.0, min_x: 0.0, max_y: 10.0, k: 20.0 };
|
||||
let mut out = Out { s: String::new(), first: true };
|
||||
stamp_node(&mut out, &t, m, "0.3".into());
|
||||
let recs: Vec<Vec<&str>> = out.s.split('\u{1e}').map(|r| r.split('\u{1f}').collect()).collect();
|
||||
assert_eq!(recs[0], ["X", "opacity", "0.3"]);
|
||||
// Anker (40, 140 mm); 10 pt = 3.528 mm, Live-Zeile 0.8 ×; Block um den Anker zentriert.
|
||||
assert_eq!(&recs[1][..4], ["L", "", "x", "40"]);
|
||||
assert_eq!(recs[1][7], "middle");
|
||||
assert_eq!(recs[2], ["R", "Wohnen", "font-size", "3.528", "fill", "#333", "font-weight", "700", "font-family", "Helvetica, Arial, sans-serif"]);
|
||||
assert_eq!((recs[4][1], recs[4][3]), ("17.01 m²", "2.822"));
|
||||
let y0: f64 = recs[1][5].parse().unwrap();
|
||||
let y1: f64 = recs[3][5].parse().unwrap();
|
||||
assert!(y0 < 140.0 && y1 > 140.0 && (y1 - y0 - 10.0 * PT_TO_MM * 1.3).abs() < 1e-3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn schraffur_parallel_geklippt() {
|
||||
let sq = [V2 { x: 0.0, y: 0.0 }, V2 { x: 10.0, y: 0.0 }, V2 { x: 10.0, y: 10.0 }, V2 { x: 0.0, y: 10.0 }];
|
||||
|
||||
@@ -44,6 +44,14 @@ impl LineAlign {
|
||||
_ => LineAlign::Left,
|
||||
}
|
||||
}
|
||||
/// SVG-`text-anchor` der Zeile.
|
||||
pub fn anchor(self) -> &'static str {
|
||||
match self {
|
||||
LineAlign::Left => "start",
|
||||
LineAlign::Center => "middle",
|
||||
LineAlign::Right => "end",
|
||||
}
|
||||
}
|
||||
/// 0 links · 1 Mitte · 2 rechts (render2d, DXF).
|
||||
pub fn code(self) -> u32 {
|
||||
match self {
|
||||
@@ -346,6 +354,30 @@ pub fn wrap_doc_to_lines(doc: &RichTextDoc, o: &LayoutOpts, wrap: Option<f64>) -
|
||||
lines_of(doc, layout_refs(doc, o, wrap), o)
|
||||
}
|
||||
|
||||
/// Lage der Zeilen eines Textblocks (früher je Verbraucher in TS: SVG-Rückfall der Plan-Ansicht,
|
||||
/// Layout-Blatt, Layout-PDF, Druck-SVG): `rows` = (Ausrichtung, Vorschub) je Zeile → (x, Grundlinie
|
||||
/// y) je Zeile. Die erste Grundlinie liegt bei `y`, jede weitere um ihren Vorschub tiefer (`y` wächst
|
||||
/// nach unten); x am Anker bzw. mit Spaltenbreite `col` Mitte/rechts in der Spalte (0/NaN = keine
|
||||
/// Spalte, wie `!width` in TS).
|
||||
pub fn place_lines(rows: impl IntoIterator<Item = (LineAlign, f64)>, x: f64, y: f64, col: Option<f64>) -> Vec<(f64, f64)> {
|
||||
let col = col.filter(|w| *w != 0.0 && !w.is_nan());
|
||||
let mut y = y;
|
||||
rows.into_iter()
|
||||
.enumerate()
|
||||
.map(|(n, (align, lh))| {
|
||||
if n > 0 {
|
||||
y += lh;
|
||||
}
|
||||
let x = match (align, col) {
|
||||
(LineAlign::Center, Some(w)) => x + w / 2.0,
|
||||
(LineAlign::Right, Some(w)) => x + w,
|
||||
_ => x,
|
||||
};
|
||||
(x, y)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Eine gesetzte Zeile mit ihrer Grundlinie (`y`).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Placed {
|
||||
@@ -539,6 +571,19 @@ mod tests {
|
||||
lines.iter().map(Line::content).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zeilen_lage_am_anker_und_in_der_spalte() {
|
||||
let rows = [(LineAlign::Left, 9.0), (LineAlign::Center, 2.5), (LineAlign::Right, 4.0)];
|
||||
// Erste Grundlinie am Anker, jede weitere um ihren Vorschub tiefer.
|
||||
assert_eq!(place_lines(rows, 1.0, 10.0, None), [(1.0, 10.0), (1.0, 12.5), (1.0, 16.5)]);
|
||||
// Spalte: Mitte/rechts in der Spalte; 0/NaN wie keine, negativ zählt (wie `!width` in TS).
|
||||
assert_eq!(place_lines(rows, 1.0, 10.0, Some(6.0)), [(1.0, 10.0), (4.0, 12.5), (7.0, 16.5)]);
|
||||
assert_eq!(place_lines(rows, 1.0, 10.0, Some(0.0)), place_lines(rows, 1.0, 10.0, None));
|
||||
assert_eq!(place_lines(rows, 1.0, 10.0, Some(f64::NAN)), place_lines(rows, 1.0, 10.0, None));
|
||||
assert_eq!(place_lines(rows, 1.0, 10.0, Some(-2.0))[2], (-1.0, 16.5));
|
||||
assert_eq!([LineAlign::Left, LineAlign::Center, LineAlign::Right].map(LineAlign::anchor), ["start", "middle", "end"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ohne_spalte_eine_zeile_je_absatz() {
|
||||
let d = doc_from_text("hallo welt\n\nzwei", &Marks::default());
|
||||
|
||||
@@ -6,12 +6,13 @@
|
||||
//! Zeilen kommen flach zurück (`encode_lines`): `[nZeilen, nStile, Stil*, Zeile*]`;
|
||||
//! Stil = `[Grösse, Bits, Versatz, nFarbe, nSchrift (−1 = keine), Farbe…, Schrift…]` (Bits: 1 fett,
|
||||
//! 2 kursiv, 4 unterstrichen, 8 durchgestrichen, 16 mit Versatz; Texte als UTF-16-Einheiten);
|
||||
//! Zeile = `[Ausrichtung 0/1/2, Vorschub, y (NaN = keins), nTspans, (Stil, Absatz, Run, von, bis)*]`
|
||||
//! Zeile = `[Ausrichtung 0/1/2, Vorschub, x, Grundlinie y, nTspans, (Stil, Absatz, Run, von, bis)*]`
|
||||
//! (Lage aus [`place_lines`] bzw. dem Raumstempel-Block — die Verbraucher rechnen keine Lage mehr)
|
||||
//! — der Text eines Tspans ist `runs[Run].text.slice(von, bis)` des Absatzes in der Eingabe (TS hat
|
||||
//! sie schon; keine Texte über die Grenze zurück).
|
||||
|
||||
use crate::model::RichTextDoc;
|
||||
use crate::text_layout::{layout_refs, tspan_of, stamp_block_refs, stamp_extra_tspan, text_frame_corners, wrap_plain, LayoutOpts, LineAlign, LineRef, Tok, Tspan};
|
||||
use crate::text_layout::{layout_refs, place_lines, tspan_of, stamp_block_refs, stamp_extra_tspan, text_frame_corners, wrap_plain, LayoutOpts, LineAlign, LineRef, Tok, Tspan};
|
||||
use wasm_bindgen::prelude::wasm_bindgen;
|
||||
|
||||
fn style_bits(t: &Tspan) -> f64 {
|
||||
@@ -25,14 +26,15 @@ fn style_bits(t: &Tspan) -> f64 {
|
||||
}
|
||||
|
||||
/// Verweis-Zeilen flach (Format s. Modulkopf); `style(tok)` = Stil (Tspan ohne Text) des Stücks,
|
||||
/// `ys` = Grundlinien je Zeile.
|
||||
fn encode_lines(lines: &[LineRef], ys: Option<&[f64]>, style: impl Fn(&Tok) -> Tspan) -> Vec<f64> {
|
||||
/// `at` = (x, Grundlinie) je Zeile.
|
||||
fn encode_lines(lines: &[LineRef], at: &[(f64, f64)], style: impl Fn(&Tok) -> Tspan) -> Vec<f64> {
|
||||
let mut styles: Vec<Tspan> = Vec::new();
|
||||
let mut keys: Vec<(usize, usize)> = Vec::new();
|
||||
let mut key_style: Vec<usize> = Vec::new();
|
||||
let mut body: Vec<f64> = Vec::new();
|
||||
for (k, l) in lines.iter().enumerate() {
|
||||
body.extend_from_slice(&[l.align.code() as f64, l.line_height, ys.and_then(|y| y.get(k).copied()).unwrap_or(f64::NAN), l.toks.len() as f64]);
|
||||
let (x, y) = at.get(k).copied().unwrap_or((f64::NAN, f64::NAN));
|
||||
body.extend_from_slice(&[l.align.code() as f64, l.line_height, x, y, l.toks.len() as f64]);
|
||||
for t in &l.toks {
|
||||
let si = match keys.iter().position(|q| *q == (t.para, t.run)) {
|
||||
Some(i) => key_style[i],
|
||||
@@ -73,16 +75,18 @@ fn encode_lines(lines: &[LineRef], ys: Option<&[f64]>, style: impl Fn(&Tok) -> T
|
||||
out
|
||||
}
|
||||
|
||||
/// Zeilen eines Dokuments (`SvgLine[]`, flach). `opts` = `[lineHeight, unitPerPt, basePt gesetzt
|
||||
/// 0/1, basePt, Spaltenbreite]` (Spalte ≤ 0/NaN = kein Umbruch); `color` fehlt → `#000000`.
|
||||
/// Gesetzte Zeilen eines Dokuments (`PlacedLine[]`, flach). `opts` = `[lineHeight, unitPerPt,
|
||||
/// basePt gesetzt 0/1, basePt, Umbruchbreite, x, y, Spaltenbreite]` (Umbruch ≤ 0/NaN = keiner;
|
||||
/// Spalte 0/NaN = keine, sonst Mitte/rechts darin, s. [`place_lines`]); `color` fehlt → `#000000`.
|
||||
/// Leer = kaputte Eingabe.
|
||||
#[wasm_bindgen]
|
||||
pub fn text_lines(doc_json: &str, opts: &[f64], color: Option<String>) -> Vec<f64> {
|
||||
let Ok([line_height, unit_per_pt, has_base, base_pt, wrap]) = <[f64; 5]>::try_from(opts) else { return Vec::new() };
|
||||
let Ok([line_height, unit_per_pt, has_base, base_pt, wrap, x, y, col]) = <[f64; 8]>::try_from(opts) else { return Vec::new() };
|
||||
let Ok(doc) = serde_json::from_str::<RichTextDoc>(doc_json) else { return Vec::new() };
|
||||
let o = LayoutOpts { line_height, unit_per_pt, base_pt: if has_base != 0.0 { base_pt } else { 12.0 }, color: color.as_deref().unwrap_or("#000000") };
|
||||
let refs = layout_refs(&doc, &o, Some(wrap));
|
||||
encode_lines(&refs, None, |t| tspan_of(String::new(), &doc.paragraphs[t.para].runs[t.run].marks, o.base_pt, o.unit_per_pt, o.color))
|
||||
let at = place_lines(refs.iter().map(|l| (l.align, l.line_height)), x, y, Some(col));
|
||||
encode_lines(&refs, &at, |t| tspan_of(String::new(), &doc.paragraphs[t.para].runs[t.run].marks, o.base_pt, o.unit_per_pt, o.color))
|
||||
}
|
||||
|
||||
/// Rahmen eines Einzeltexts: `opts` = `[atX, atY, heightM, angle, Spaltenbreite (NaN = keine),
|
||||
@@ -96,18 +100,18 @@ pub fn text_frame(doc_json: &str, opts: &[f64]) -> Vec<f64> {
|
||||
}
|
||||
|
||||
/// Raumstempel-Block der Plan-Ansicht (`stamp_block_refs`): `prim_json` = das Primitiv `text`;
|
||||
/// Zeilen flach mit Grundlinie `y` (um `y0` zentriert, `y` nach unten; Live-Zeile `i` als Absatz
|
||||
/// Absatzzahl + `i`). Leer = kaputte Eingabe.
|
||||
/// Zeilen flach mit x = `x0` und Grundlinie `y` (um `y0` zentriert, `y` nach unten; Live-Zeile `i`
|
||||
/// als Absatz Absatzzahl + `i`). Leer = kaputte Eingabe.
|
||||
#[wasm_bindgen]
|
||||
pub fn text_stamp_block(prim_json: &str, unit_per_pt: f64, y0: f64) -> Vec<f64> {
|
||||
pub fn text_stamp_block(prim_json: &str, unit_per_pt: f64, x0: f64, y0: f64) -> Vec<f64> {
|
||||
let Ok(p) = serde_json::from_str::<crate::plan_text::StampText>(prim_json) else { return Vec::new() };
|
||||
let extra: Vec<(&str, LineAlign)> = p.extra_lines.iter().map(|l| (l.text.as_str(), LineAlign::of(Some(l.align)))).collect();
|
||||
let color = p.color.as_deref().unwrap_or("#000000");
|
||||
let placed = stamp_block_refs(&p.doc, &extra, p.base_pt, unit_per_pt, color, y0);
|
||||
let ys: Vec<f64> = placed.iter().map(|q| q.1).collect();
|
||||
let at: Vec<(f64, f64)> = placed.iter().map(|q| (x0, q.1)).collect();
|
||||
let lines: Vec<LineRef> = placed.into_iter().map(|q| q.0).collect();
|
||||
let n = p.doc.paragraphs.len();
|
||||
encode_lines(&lines, Some(&ys), |t| {
|
||||
encode_lines(&lines, &at, |t| {
|
||||
if t.para >= n {
|
||||
stamp_extra_tspan(String::new(), p.base_pt, unit_per_pt, color)
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user