diff --git a/crates/engine/src/cmd/exchange.rs b/crates/engine/src/cmd/exchange.rs index f7a2fbc65..0171a9acc 100644 --- a/crates/engine/src/cmd/exchange.rs +++ b/crates/engine/src/cmd/exchange.rs @@ -57,7 +57,7 @@ pub fn specs() -> Vec { CommandSpec::new("pdfbook", "Sheet Books to PDF...", run_pdfbook) .menu(&["File", "Sheet Books to PDF..."]) .alias(&["printpdf", "pdfb"]) - .params("{path?: \"DIR/NAME^NAME2.pdf\" (default: the drawing's file; a `^` splits the name into one book name per row of frames; else returns base64 `data` per book), names?: [book names], frameHeight?: mm (default 297), unit?: mm per drawing unit (default 1)} → {books: [{name, pages, bytes, path? | data}], skipped}: closed polylines of the frame height in model space are sheet frames; each row (same lower edge, bottom to top) is one PDF, its frames left to right one page each on the paper (ISO A-series, either orientation) they match") + .params("{path?: \"DIR/NAME^NAME2.pdf\" (default: the drawing's file; a `^` splits the name into one book name per row of frames; else returns base64 `data` per book), names?: [book names], frameHeight?: mm (default 297), unit?: mm per drawing unit (default 1)} → {books: [{name, pages, bytes, path? | data}], skipped}: closed polylines of the frame height in model space are sheet frames; each row (same lower edge, bottom to top) is one PDF, its frames left to right one page each on the paper (any standard size — ISO, JIS, ANSI — either orientation) they match") .noundo(), CommandSpec::new("attach", "Attach", |s, p| match str_param(p, "path").map(extension) { Some(e) if super::image::is_image_ext(&e) => super::image::run_imageattach(s, p), @@ -573,7 +573,8 @@ struct Frame { landscape: bool, } -/// The ISO/ANSI paper a frame of `w` x `h` mm fits (either orientation), and whether it is landscape. +/// The standard paper (ISO, JIS or ANSI) a frame of `w` x `h` mm fits (either orientation), and whether +/// it is landscape. fn paper_for(w: f64, h: f64) -> Option<(&'static str, bool)> { cadcraft_render::PAPER_SIZES.iter().find_map(|p| { let (pw, ph) = (p.width_mm, p.height_mm); @@ -878,6 +879,17 @@ mod tests { let r = s.execute("preview", &json!({ "layout": "layout1", "paper": "A4", "landscape": true, "width": 600 })).unwrap(); assert_eq!((r["layout"].as_str(), r["width"].as_u64(), r["height"].as_u64()), (Some("Layout1"), Some(600), Some(424))); assert_eq!(r["paper"], "ISO A4 (210.00 x 297.00 MM)"); + // The bare B5 is the ISO size it always was; the JIS sheet needs its explicit name, and the + // Japanese postcards resolve by their names and their Japanese spelling. + let r = s.execute("preview", &json!({ "layout": "layout1", "paper": "B5", "landscape": false, "width": 400 })).unwrap(); + assert_eq!(r["paper"], "ISO B5 (176.00 x 250.00 MM)"); + assert_eq!((r["width"].as_u64(), r["height"].as_u64()), (Some(400), Some(568))); + let r = s.execute("preview", &json!({ "layout": "layout1", "paper": "B5 (JIS)", "landscape": false, "width": 400 })).unwrap(); + assert_eq!(r["paper"], "JIS B5 (182.00 x 257.00 MM)"); + assert_eq!((r["width"].as_u64(), r["height"].as_u64()), (Some(400), Some(565))); + let r = s.execute("preview", &json!({ "layout": "layout1", "paper": "はがき", "landscape": false, "width": 200 })).unwrap(); + assert_eq!(r["paper"], "Japanese Postcard (100.00 x 148.00 MM)"); + assert_eq!(r["height"], 296); assert!(s.execute("preview", &json!({ "layout": "Nope" })).is_err()); assert!(s.execute("preview", &json!({ "paper": "Nope" })).is_err()); for p in [json!({ "scale": -1, "fit": false }), json!({ "scale": 1e-300, "fit": false }), json!({ "width": -5 }), json!({ "width": 1e30 })] { diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 41dc7571b..164950ffd 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -82,7 +82,7 @@ pub fn specs() -> Vec { CommandSpec::new("pagesetup", "Page Setup Manager...", run_pagesetup) .menu(&["File", "Page Setup Manager..."]) .params( - "{layout?: current, device?: plotter name (PLOTTERMANAGER) | \"None\", paper?: the device's paper | \"A4\"|\"A3\"|\"Letter\"|\"ANSI B\"|…, width?, height? (mm), landscape?, paperUnits?: \"mm\"|\"inches\" (one paper-space unit on the layout), margins?: [l,b,r,t] mm, lineweights?, plotArea?, scale?, scaleToFit?, center?, plotStyleTable?: \"monochrome.ctb\"|\"grayscale.ctb\"|\"default.ctb\"|\"None\"|…, displayPlotStyles?: bool, plotTransparency?: bool}", + "{layout?: current, device?: plotter name (PLOTTERMANAGER) | \"None\", paper?: the device's paper | \"A4\"|\"A3\"|\"Letter\"|\"ANSI B\"|\"B4 (JIS)\"|\"Japanese Postcard\"|…, width?, height? (mm), landscape?, paperUnits?: \"mm\"|\"inches\" (one paper-space unit on the layout), margins?: [l,b,r,t] mm, lineweights?, plotArea?, scale?, scaleToFit?, center?, plotStyleTable?: \"monochrome.ctb\"|\"grayscale.ctb\"|\"default.ctb\"|\"None\"|…, displayPlotStyles?: bool, plotTransparency?: bool}", ), CommandSpec::new("plot", "Print...", |s, p| run_plot(s, &super::plotters::plot_options(s, p, "plot")?, "plot")) .menu(&["File", "Print..."]) diff --git a/crates/io/src/pdf.rs b/crates/io/src/pdf.rs index fcaab1b0a..7fed3ce9a 100644 --- a/crates/io/src/pdf.rs +++ b/crates/io/src/pdf.rs @@ -61,7 +61,8 @@ const MAX_TEXT: usize = 1000; pub struct PdfOptions { /// Plot style table name, replacing the page setup's (`"None"` plots without one). pub plot_style_table: Option, - /// Paper name from [`cadcraft_render::PAPER_SIZES`] (`"A4"`, `"Letter"`, `"ANSI B"`…). + /// Paper name from [`cadcraft_render::PAPER_SIZES`] (`"A4"`, `"Letter"`, `"ANSI B"`, `"B4 (JIS)"`, + /// `"Japanese Postcard"`…). pub paper: Option, /// Custom paper size in millimetres (portrait width, height). pub paper_mm: Option<(f64, f64)>, diff --git a/crates/io/src/tests.rs b/crates/io/src/tests.rs index 1201662a6..dee4bb810 100644 --- a/crates/io/src/tests.rs +++ b/crates/io/src/tests.rs @@ -1024,6 +1024,47 @@ fn pdf_model_fitted_to_sheet() { assert!(write(&d, "x.pdf").unwrap().starts_with(b"%PDF")); } +/// Paper sizes reach the PDF page: the bare B4/B5/B6 are the ISO sizes, the JIS ones are reachable +/// by their explicit names, and the Japanese postcards/envelopes/四六判 plot at theirs. +#[test] +fn pdf_plot_jis_and_japanese_paper_sizes() { + let d = sample(); + for (name, w, h) in [ + ("B4", 708.66, 1000.63), + ("B4 (ISO)", 708.66, 1000.63), + ("B5", 498.90, 708.66), + ("B5-iso", 498.90, 708.66), + ("B6", 354.33, 498.90), + ("B6 (ISO)", 354.33, 498.90), + ("B4 (JIS)", 728.50, 1031.81), + ("JIS B4", 728.50, 1031.81), + ("B5 (JIS)", 515.91, 728.50), + ("b5-jis", 515.91, 728.50), + ("B6 (JIS)", 362.83, 515.91), + ("Japanese Postcard", 283.46, 419.53), + ("はがき", 283.46, 419.53), + ("Japanese Double Postcard", 419.53, 566.93), + ("往復はがき", 419.53, 566.93), + ("Japanese Envelope #3", 340.16, 666.14), + ("長形3号", 340.16, 666.14), + ("Japanese Envelope Kaku #2", 680.31, 941.10), + ("角形2号", 680.31, 941.10), + ("四六判", 360.00, 532.91), + ] { + let bytes = plot(&d, &Space::Model, &serde_json::json!({"paper": name, "landscape": false})).unwrap(); + check_pdf(&bytes); + let (got_w, got_h) = media_box(&bytes); + assert!((got_w - w).abs() < 0.01 && (got_h - h).abs() < 0.01, "{name}: {got_w} x {got_h}, wanted {w} x {h}"); + } + // The ambiguous bare postcard aliases are gone. + assert!(plot(&d, &Space::Model, &serde_json::json!({"paper": "Postcard"})).is_err()); + assert!(plot(&d, &Space::Model, &serde_json::json!({"paper": "Double Postcard"})).is_err()); + // Existing ISO values and the unknown-name error are unchanged. + let (w, h) = media_box(&plot(&d, &Space::Model, &serde_json::json!({"paper": "A3", "landscape": false})).unwrap()); + assert!((w - 841.89).abs() < 0.01 && (h - 1190.55).abs() < 0.01, "A3: {w} x {h}"); + assert!(plot(&d, &Space::Model, &serde_json::json!({"paper": "Z9"})).is_err()); +} + #[test] fn pdf_layout_one_to_one_with_viewport() { let mut d = sample(); diff --git a/crates/render/src/paper.rs b/crates/render/src/paper.rs index be0cd67c7..dbbd0471d 100644 --- a/crates/render/src/paper.rs +++ b/crates/render/src/paper.rs @@ -31,7 +31,11 @@ const fn inch(name: &'static str, aliases: &'static [&'static str], w: f64, h: f PaperSize { name, aliases, width_mm: w * 25.4, height_mm: h * 25.4, imperial: true } } -/// Common ISO (A0–A6, B0–B5) and ANSI/ARCH sheet sizes (ISO 216, ANSI/ASME Y14.1). +/// Common ISO (A0–A6, B0–B6), JIS (B4–B6), Japanese stationery and ANSI/ARCH sheet sizes +/// (ISO 216, JIS P 0138, ANSI/ASME Y14.1). +/// +/// The bare names `B4`/`B5`/`B6` are the ISO sizes (250×353, 176×250 and 125×176 mm); the JIS +/// sheets (257×364, 182×257 and 128×182 mm) need explicit names (`JIS B4`, `B4 (JIS)`…). pub const PAPER_SIZES: &[PaperSize] = &[ iso("ISO A0 (841.00 x 1189.00 MM)", &["A0", "ISO A0"], 841.0, 1189.0), iso("ISO A1 (594.00 x 841.00 MM)", &["A1", "ISO A1"], 594.0, 841.0), @@ -44,8 +48,22 @@ pub const PAPER_SIZES: &[PaperSize] = &[ iso("ISO B1 (707.00 x 1000.00 MM)", &["B1", "ISO B1"], 707.0, 1000.0), iso("ISO B2 (500.00 x 707.00 MM)", &["B2", "ISO B2"], 500.0, 707.0), iso("ISO B3 (353.00 x 500.00 MM)", &["B3", "ISO B3"], 353.0, 500.0), - iso("ISO B4 (250.00 x 353.00 MM)", &["B4", "ISO B4"], 250.0, 353.0), - iso("ISO B5 (176.00 x 250.00 MM)", &["B5", "ISO B5"], 176.0, 250.0), + iso("ISO B4 (250.00 x 353.00 MM)", &["B4", "ISO B4", "B4 (ISO)"], 250.0, 353.0), + iso("ISO B5 (176.00 x 250.00 MM)", &["B5", "ISO B5", "B5 (ISO)"], 176.0, 250.0), + iso("ISO B6 (125.00 x 176.00 MM)", &["B6", "ISO B6", "B6 (ISO)"], 125.0, 176.0), + // JIS B4–B6 (JIS P 0138): the sheets Japanese drafting and Excel call B4/B5/B6, a few + // millimetres larger than the ISO ones. Explicit names only, so the bare names stay ISO. + iso("JIS B4 (257.00 x 364.00 MM)", &["B4 (JIS)", "JIS B4"], 257.0, 364.0), + iso("JIS B5 (182.00 x 257.00 MM)", &["B5 (JIS)", "JIS B5"], 182.0, 257.0), + iso("JIS B6 (128.00 x 182.00 MM)", &["B6 (JIS)", "JIS B6"], 128.0, 182.0), + // Japanese stationery: postcards, the long (chou) and square (kaku) envelopes and 四六判, the + // 127×188 mm book/paper trade size. No bare `Postcard`: postcard sizes differ by country, so + // the name (and はがき) say which one is meant. + iso("Japanese Postcard (100.00 x 148.00 MM)", &["Japanese Postcard", "はがき"], 100.0, 148.0), + iso("Japanese Double Postcard (148.00 x 200.00 MM)", &["Japanese Double Postcard", "往復はがき"], 148.0, 200.0), + iso("Japanese Envelope #3 (120.00 x 235.00 MM)", &["Japanese Envelope #3", "Envelope #3", "長形3号"], 120.0, 235.0), + iso("Japanese Envelope Kaku #2 (240.00 x 332.00 MM)", &["Japanese Envelope Kaku #2", "Kaku #2", "角形2号"], 240.0, 332.0), + iso("Japanese Shirokuban (127.00 x 188.00 MM)", &["Shirokuban", "Yotsuro", "四六判"], 127.0, 188.0), inch("ANSI A (8.50 x 11.00 Inches)", &["ANSI A", "Letter", "A-size"], 8.5, 11.0), inch("Legal (8.50 x 14.00 Inches)", &["Legal"], 8.5, 14.0), inch("ANSI B (11.00 x 17.00 Inches)", &["ANSI B", "Tabloid", "Ledger", "B-size"], 11.0, 17.0), @@ -68,9 +86,22 @@ impl PaperSize { } } -/// Look up a paper size by canonical name or alias (case- and space-insensitive). +/// Look up a paper size by canonical name or alias (case-insensitive; spaces, underscores, hyphens +/// and parentheses are ignored, so `B5 (JIS)`, `b5-jis` and `b5jis` are one name). pub fn paper_size(name: &str) -> Option<&'static PaperSize> { - let norm = |s: &str| s.chars().filter(|c| !c.is_whitespace() && *c != '_' && *c != '-').collect::().to_ascii_lowercase(); + // Full-width forms are what a Japanese keyboard or IME produces (`B5 (JIS)`), so fold them + // to ASCII before the rest of the normalisation: U+FF01..U+FF5E are ASCII + 0xFEE0. + let norm = |s: &str| { + s.chars() + .map(|c| match c { + '\u{FF01}'..='\u{FF5E}' => char::from_u32(c as u32 - 0xFEE0).unwrap_or(c), + '\u{3000}' => ' ', + _ => c, + }) + .filter(|c| !c.is_whitespace() && !matches!(c, '_' | '-' | '(' | ')')) + .collect::() + .to_ascii_lowercase() + }; let n = norm(name); if n.is_empty() { return None; @@ -200,6 +231,73 @@ pub fn sheet(d: &Drawing, layout: &str) -> Option { mod tests { use super::*; + #[test] + fn jis_and_japanese_paper_sizes() { + // The bare names B4/B5/B6 are the ISO sizes, as they were before the JIS sheets arrived. + for (name, w, h) in [ + ("B4", 250.0, 353.0), + ("b4", 250.0, 353.0), + ("ISO B4", 250.0, 353.0), + ("B4 (ISO)", 250.0, 353.0), + ("B5", 176.0, 250.0), + ("b5-iso", 176.0, 250.0), + ("B6", 125.0, 176.0), + ("B6 (ISO)", 125.0, 176.0), + ] { + let p = paper_size(name).unwrap_or_else(|| panic!("{name}")); + assert_eq!((p.width_mm, p.height_mm), (w, h), "{name}"); + assert!(!p.imperial, "{name} is a metric sheet"); + } + assert_eq!(paper_size("B4").map(|p| p.name), Some("ISO B4 (250.00 x 353.00 MM)")); + assert_eq!(paper_size("B6").map(|p| p.name), Some("ISO B6 (125.00 x 176.00 MM)")); + // The JIS sizes (JIS P 0138) are reachable by their explicit names only. + for (name, w, h) in [ + ("B4 (JIS)", 257.0, 364.0), + ("b4-jis", 257.0, 364.0), + ("JIS B4", 257.0, 364.0), + ("B5 (JIS)", 182.0, 257.0), + ("b5 (jis)", 182.0, 257.0), + ("JIS B5", 182.0, 257.0), + ("B6 (JIS)", 128.0, 182.0), + ("b6jis", 128.0, 182.0), + ("JIS B6", 128.0, 182.0), + ] { + let p = paper_size(name).unwrap_or_else(|| panic!("{name}")); + assert_eq!((p.width_mm, p.height_mm), (w, h), "{name}"); + assert!(!p.imperial, "{name} is a metric sheet"); + } + assert_eq!(paper_size("JIS B5").map(|p| p.name), Some("JIS B5 (182.00 x 257.00 MM)")); + // A page setup stores the canonical name; the ISO and JIS names both keep resolving, so a + // drawing keeps the sheet it stored. + for name in ["ISO B4 (250.00 x 353.00 MM)", "JIS B4 (257.00 x 364.00 MM)"] { + assert_eq!(paper_size(name).map(|p| p.name), Some(name), "{name}"); + } + // Japanese postcards, envelopes and 四六判. + for (name, w, h) in [ + ("Japanese Postcard", 100.0, 148.0), + ("はがき", 100.0, 148.0), + ("Japanese Double Postcard", 148.0, 200.0), + ("往復はがき", 148.0, 200.0), + ("Japanese Envelope #3", 120.0, 235.0), + ("長形3号", 120.0, 235.0), + ("Japanese Envelope Kaku #2", 240.0, 332.0), + ("kaku #2", 240.0, 332.0), + ("角形2号", 240.0, 332.0), + ("Shirokuban", 127.0, 188.0), + ("四六判", 127.0, 188.0), + ] { + let p = paper_size(name).unwrap_or_else(|| panic!("{name}")); + assert_eq!((p.width_mm, p.height_mm), (w, h), "{name}"); + } + // The bare postcard aliases are ambiguous and gone. + assert!(paper_size("Postcard").is_none()); + assert!(paper_size("Double Postcard").is_none()); + // A6 was already an ISO sheet. + assert_eq!(paper_size("A6").map(|p| (p.width_mm, p.height_mm)), Some((105.0, 148.0))); + // Unknown names still fail. + assert!(paper_size("Z9").is_none()); + } + #[test] fn metric_length_unit_reads_the_length_unit() { // Metric units, whatever MEASUREMENT says. @@ -346,3 +444,22 @@ pub fn saved_plot(page: &PageSetup) -> SavedPlot { let scale = Some(page.scale).filter(|s| !page.scale_to_fit && s.is_finite() && *s > 0.0 && (*s - 1.0).abs() > 1e-9); SavedPlot { window, fit, scale, center: page.center } } + +#[cfg(test)] +mod wide_name_tests { + use super::*; + + /// A name typed on a Japanese keyboard arrives in full-width forms (`B5 (JIS)`); it must + /// resolve to the same sheet as `B5 (JIS)`. + #[test] + fn full_width_names_resolve_like_their_ascii_forms() { + for (wide, ascii) in [("B5 (JIS)", "B5 (JIS)"), ("A4", "A4"), ("B4", "B4")] { + let w = paper_size(wide).unwrap_or_else(|| panic!("{wide}")); + let a = paper_size(ascii).unwrap_or_else(|| panic!("{ascii}")); + assert_eq!(w.name, a.name, "{wide} vs {ascii}"); + } + // The Japanese spellings still work, and the ideographic space is a separator. + assert_eq!(paper_size("はがき").map(|p| p.name), paper_size("Japanese Postcard").map(|p| p.name)); + assert_eq!(paper_size(" B5 ").map(|p| p.name), Some(paper_size("B5").unwrap().name)); + } +} diff --git a/docs/plotters.md b/docs/plotters.md index 6879c729f..ad0cf4f96 100644 --- a/docs/plotters.md +++ b/docs/plotters.md @@ -16,12 +16,23 @@ The built-in devices come first: They offer the standard sizes: -- ISO 216 A0–A6 and B0–B5 +- ISO 216 A0–A6 and B0–B6 +- JIS B4–B6 (JIS P 0138) by explicit name: `B4 (JIS)`/`JIS B4`, `B5 (JIS)`/`JIS B5` and + `B6 (JIS)`/`JIS B6` (257×364, 182×257 and 128×182 mm). The bare `B4`/`B5`/`B6` stay the ISO + sizes (250×353, 176×250 and 125×176 mm) +- Japanese stationery: Japanese Postcard (はがき, 100×148 mm), Japanese Double Postcard (往復はがき, + 148×200 mm), Japanese Envelope #3 (長形3号, 120×235 mm), Japanese Envelope Kaku #2 (角形2号, + 240×332 mm) and 四六判 (127×188 mm) - ANSI A–E and Legal - ARCH A–E and E1 -ISO sheets have 5 mm margins. Inch sheets have quarter-inch (6.35 mm) margins. A page setup -with the device `None` plots to PDF on the page setup's paper, as before. +Metric sheets (ISO and JIS) have 5 mm margins, inch sheets have quarter-inch (6.35 mm) margins. +A page setup with the device `None` plots to PDF on the page setup's paper, as before. Paper names +resolve case-insensitively and ignore spaces, hyphens and parentheses (`B5 (JIS)`, `b5-jis`, +`はがき`). + +`B4`/`B5` keep meaning the ISO sizes, so a drawing or caller that used them doesn't change; a plot +that means JIS B4/B5 says `B4 (JIS)`/`B5 (JIS)`. A drawing keeps the sheet size it stored. User devices are files in our own format, kept in the `plotters` folder of the CADCraft settings directory: