Use the shared writer for classic HP-GL export

This commit is contained in:
Terra854
2026-11-24 21:45:54 +08:00
parent 19ebdcab54
commit c0e845f87d
2 changed files with 16 additions and 22 deletions
+1 -22
View File
@@ -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<usize>], 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<(i64, i64)>>]) -> Vec<u8> {
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<Vec<(i64, i64)>>], pens: &[[u8; 3]]) -> Vec<u8> {
let pens: Vec<_> = pens.iter().enumerate().map(|(index, &rgb)| craft_plotter::hpgl::write::Pen { number: index + 1, rgb }).collect();
+15
View File
@@ -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| {