From c0e845f87d22638c961cda65b3ebffc25e24e2a3 Mon Sep 17 00:00:00 2001 From: Terra854 Date: Tue, 24 Nov 2026 21:45:54 +0800 Subject: [PATCH] Use the shared writer for classic HP-GL export --- crates/cad/src/hpgl/write.rs | 23 +---------------------- crates/cad/src/hpgl/write/tests.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/crates/cad/src/hpgl/write.rs b/crates/cad/src/hpgl/write.rs index 4e08fb3f5e..d951e92681 100644 --- a/crates/cad/src/hpgl/write.rs +++ b/crates/cad/src/hpgl/write.rs @@ -304,7 +304,7 @@ pub fn export_regions(doc: &Document, opts: &HpglOptions, regions: &[Rect]) -> R warn(&mut warnings, PAST_CLASSIC_RANGE); } let bytes = match opts.version { - Version::Hpgl => plain(&by_pen), + Version::Hpgl => craft_plotter::hpgl::write::write_hpgl(&by_pen), Version::Hpgl2 => encoded(&by_pen, &pens), }; out.push(HpglOutput { bytes, pens: pens.clone(), warnings, setting_notes }); @@ -511,27 +511,6 @@ fn inner_first(left: Vec<(usize, Vec<(i64, i64)>)>, outer: &[Vec], waitin paths.extend(held.into_iter().flatten()); } -/// HP-GL: `IN;`, each pen's paths as `PU` and `PD`, `PU;SP0;`. -fn plain(by_pen: &[Vec>]) -> Vec { - let mut s = String::from("IN;"); - for (i, paths) in by_pen.iter().enumerate().filter(|(_, p)| !p.is_empty()) { - s.push_str(&format!("PU;SP{};", i + 1)); - for path in paths { - let Some(((x, y), rest)) = path.split_first() else { continue }; - s.push_str(&format!("PU{x},{y};PD")); - for (k, (x, y)) in rest.iter().enumerate() { - if k > 0 { - s.push(','); - } - s.push_str(&format!("{x},{y}")); - } - s.push(';'); - } - } - s.push_str("PU;SP0;"); - s.into_bytes() -} - /// HP-GL/2: `IN;`, the palette, each pen's paths as one `PE`, `PU;SP0;`. fn encoded(by_pen: &[Vec>], pens: &[[u8; 3]]) -> Vec { let pens: Vec<_> = pens.iter().enumerate().map(|(index, &rgb)| craft_plotter::hpgl::write::Pen { number: index + 1, rgb }).collect(); diff --git a/crates/cad/src/hpgl/write/tests.rs b/crates/cad/src/hpgl/write/tests.rs index f04b331696..b371c832db 100644 --- a/crates/cad/src/hpgl/write/tests.rs +++ b/crates/cad/src/hpgl/write/tests.rs @@ -112,6 +112,21 @@ fn each_new_color_is_the_next_pen_and_pens_draw_in_turn() { assert!(!text(&table).contains("SP2"), "{}", text(&table)); } +#[test] +fn classic_hpgl_preserves_explicit_pen_gaps_and_path_order() { + let d = doc_with(|d| { + vec![ + stroked(d, line((130.0, 70.0), (140.0, 80.0)), Color::rgb8(0, 0, 255)), + stroked(d, line((10.0, 20.0), (20.0, 40.0)), Color::rgb8(255, 0, 0)), + stroked(d, line((80.0, 60.0), (90.0, 70.0)), Color::rgb8(0, 0, 255)), + ] + }); + let out = + plot(&d, &HpglOptions { version: Version::Hpgl, origin: Origin::Center, pens: vec![[255, 0, 0], [0, 255, 0], [0, 0, 255]], ..opts(&d) }); + // The empty green pen must leave the two blue paths on pen three. + assert_eq!(out.bytes, b"IN;PU;SP1;PU-1270,423;PD-1129,141;PU;SP3;PU423,-282;PD564,-423;PU-282,-141;PD-141,-282;PU;SP0;"); +} + #[test] fn a_filled_path_is_its_closed_outline_and_a_stroke_its_line() { let d = doc_with(|d| {