mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 04:33:43 +00:00
Reland HP-GL/2 shared byte writer (#8697)
* HPGL: delegate encoding to repaired shared byte writer [skip ci] * HPGL: apply hosted rustfmt layout [skip ci] * HPGL: unblock CAD validation with equivalent document condition [skip ci] * HPGL: express duplicate point filter as match guard [skip ci] * 778: simplify baseline CAD dependency predicates [skip ci] * 778: keep baseline lint fixes out of HPGL cutover [skip ci] --------- Co-authored-by: Codex <codex@openai.com>
This commit is contained in:
@@ -534,47 +534,13 @@ fn plain(by_pen: &[Vec<Vec<(i64, i64)>>]) -> Vec<u8> {
|
||||
|
||||
/// 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 mut out = b"IN;".to_vec();
|
||||
if !pens.is_empty() {
|
||||
// Pens 1 to n on a plotter, and on a raster device (whose pens are 0 to NP − 1) too.
|
||||
let np = (pens.len() + 1).next_power_of_two().max(2);
|
||||
out.extend(format!("NP{np};").bytes());
|
||||
for (i, [r, g, b]) in pens.iter().enumerate() {
|
||||
out.extend(format!("PC{},{r},{g},{b};", i + 1).bytes());
|
||||
}
|
||||
}
|
||||
let mut at: Option<(i64, i64)> = None;
|
||||
for (i, paths) in by_pen.iter().enumerate().filter(|(_, p)| !p.is_empty()) {
|
||||
out.extend(format!("PU;SP{};PE", i + 1).bytes());
|
||||
for path in paths {
|
||||
for (k, &p) in path.iter().enumerate() {
|
||||
if k == 0 {
|
||||
out.push(b'<');
|
||||
}
|
||||
// The first move of the plot is absolute, and so is one longer than the range a
|
||||
// device takes for each number; every other is relative.
|
||||
match at.map(|a| (p.0 - a.0, p.1 - a.1)).filter(|d| d.0.abs() <= MAX_COORD && d.1.abs() <= MAX_COORD) {
|
||||
None => {
|
||||
out.push(b'=');
|
||||
pe_number(&mut out, p.0);
|
||||
pe_number(&mut out, p.1);
|
||||
}
|
||||
Some((dx, dy)) => {
|
||||
pe_number(&mut out, dx);
|
||||
pe_number(&mut out, dy);
|
||||
}
|
||||
}
|
||||
at = Some(p);
|
||||
}
|
||||
}
|
||||
out.push(b';');
|
||||
}
|
||||
out.extend(b"PU;SP0;");
|
||||
out
|
||||
let pens: Vec<_> = pens.iter().enumerate().map(|(index, &rgb)| craft_plotter::hpgl::write::Pen { number: index + 1, rgb }).collect();
|
||||
craft_plotter::hpgl::write::write_hpgl2(&pens, by_pen)
|
||||
}
|
||||
|
||||
/// `v` as a base-64 PE number: the sign in the low bit, the low digits from 63 up and the last
|
||||
/// digit from 191 up.
|
||||
#[cfg(test)]
|
||||
fn pe_number(out: &mut Vec<u8>, v: i64) {
|
||||
let mut n = (v.unsigned_abs() << 1) | u64::from(v < 0);
|
||||
while n >= 64 {
|
||||
@@ -973,10 +939,8 @@ impl<'a> Walk<'a> {
|
||||
end(&mut cur, &mut out, false);
|
||||
cur.push(p);
|
||||
}
|
||||
PathEl::LineTo(p) => {
|
||||
if cur.last() != Some(&p) {
|
||||
cur.push(p);
|
||||
}
|
||||
PathEl::LineTo(p) if cur.last() != Some(&p) => {
|
||||
cur.push(p);
|
||||
}
|
||||
PathEl::ClosePath => {
|
||||
let s = cur.first().copied();
|
||||
|
||||
@@ -1179,6 +1179,25 @@ fn encoded_numbers_follow_the_reference_guide() {
|
||||
assert_eq!(out, [194, 191]);
|
||||
}
|
||||
|
||||
/// The app adapter must preserve raw bytes and position across the one-based pen table.
|
||||
#[test]
|
||||
fn hpgl2_shared_writer_preserves_raw_bytes_and_pen_indices() {
|
||||
let paths = vec![vec![vec![(32, -32)]], vec![], vec![vec![(33, -31)], vec![(34, -30)]]];
|
||||
let pens = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
|
||||
assert_eq!(encoded(&paths, &pens), b"IN;NP4;PC1,1,2,3;PC2,4,5,6;PC3,7,8,9;PU;SP1;PE<=?\xc0@\xc0;PU;SP3;PE<\xc1\xc1<\xc1\xc1;PU;SP0;");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hpgl2_shared_writer_preserves_empty_and_last_admitted_pen() {
|
||||
assert_eq!(encoded(&[], &[]), b"IN;PU;SP0;");
|
||||
let pens = vec![[0, 0, 0]; MAX_PENS];
|
||||
let mut paths = vec![vec![]; MAX_PENS];
|
||||
paths[MAX_PENS - 1] = vec![vec![(0, 0)]];
|
||||
let bytes = encoded(&paths, &pens);
|
||||
assert!(bytes.starts_with(b"IN;NP256;PC1,0,0,0;"));
|
||||
assert!(bytes.ends_with(b"PC255,0,0,0;PU;SP255;PE<=\xbf\xbf;PU;SP0;"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hpgl2_writes_the_palette_and_encoded_polylines() {
|
||||
let out = plot(&three(), &HpglOptions { version: Version::Hpgl2, ..opts(&three()) });
|
||||
|
||||
Reference in New Issue
Block a user