mirror of
https://github.com/storytold/gridcraft.git
synced 2026-12-01 18:26:05 -06:00
Merge branch 'main' into fix/2641-cascading-autofilter-values
This commit is contained in:
@@ -57,7 +57,7 @@ const MAX_CHART_PRIMS: usize = 4_000_000;
|
||||
use layout::bands;
|
||||
pub(crate) use layout::split_bands;
|
||||
pub use layout::{PrintOptions, SheetLayout, layout_sheet, layouts, paper_size, print_range};
|
||||
use layout::{known_paper, seg_size, segments};
|
||||
use layout::{seg_size, segments};
|
||||
use objects::{PicCache, draw_objects};
|
||||
#[cfg(test)]
|
||||
use text::{center_across_width, wrap_lines};
|
||||
@@ -96,7 +96,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Page Setup…",
|
||||
["Page Layout", "Page Setup"],
|
||||
None,
|
||||
"{sheet?, orientation?: portrait|landscape, paper?: Letter|Legal|A4|A3|A5|A6|Tabloid|Executive|Ledger|B4|B5|B6|B4 (ISO)|B5 (ISO)|B6 (ISO)|Japanese Postcard|Japanese Double Postcard|Japanese Envelope #3|Japanese Envelope #4|Japanese Envelope Kaku #2|Japanese Envelope Kaku #3 (B4/B5/B6 are the JIS sizes), margins?: {left,right,top,bottom,header,footer} (inches; also accepted as top-level keys), scale?: 10..400, fitWidth?, fitHeight? (pages; 0 = automatic), fitToPage?: bool, centerH?, centerV?, gridlines?, headings?, header?: \"&L&A&RPage &P of &N\", footer?, printArea?: \"A1:F50\" (\"\" clears), titleRows?: \"1:2\", titleCols?: \"A:A\", rowBreaks?: [46, 91], colBreaks?: [\"K\"], pageOrder?: downThenOver|overThenDown} → the sheet's print settings",
|
||||
"{sheet?, orientation?: portrait|landscape, paper?: Letter|Legal|A4|A3|A5|A6|Tabloid|Executive|Ledger|B4|B5|B6|B4 (ISO)|B5 (ISO)|B6 (ISO)|Japanese Postcard|Japanese Double Postcard|Japanese Envelope #3|Japanese Envelope #4|Japanese Envelope Kaku #2|Japanese Envelope Kaku #3|Statement|Envelope #10|Envelope DL (B4/B5/B6 are the JIS sizes), margins?: {left,right,top,bottom,header,footer} (inches; also accepted as top-level keys), scale?: 10..400, fitWidth?, fitHeight? (pages; 0 = automatic), fitToPage?: bool, centerH?, centerV?, gridlines?, headings?, header?: \"&L&A&RPage &P of &N\", footer?, printArea?: \"A1:F50\" (\"\" clears), titleRows?: \"1:2\", titleCols?: \"A:A\", rowBreaks?: [46, 91], colBreaks?: [\"K\"], pageOrder?: downThenOver|overThenDown} → the sheet's print settings",
|
||||
has_doc,
|
||||
page_setup
|
||||
),
|
||||
@@ -309,8 +309,9 @@ fn page_setup(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
let paper = match str_param(p, "paper") {
|
||||
None => None,
|
||||
Some(name) => {
|
||||
known_paper(name).ok_or_else(|| bad(CMD, format!("unknown paper size `{name}`")))?;
|
||||
Some(name.trim().to_string())
|
||||
// Stored under its table name, which every file format knows.
|
||||
let paper = gridcraft_model::paper::canonical(name).ok_or_else(|| bad(CMD, format!("unknown paper size `{name}`")))?;
|
||||
Some(paper.to_string())
|
||||
}
|
||||
};
|
||||
let margin_src = p.get("margins").filter(|m| m.is_object()).unwrap_or(p);
|
||||
|
||||
@@ -9,43 +9,10 @@ pub(super) const DENSE_BLOCK: u64 = 60_000;
|
||||
|
||||
// ---------------------------------------------------------------- paper
|
||||
|
||||
/// Paper size in points (portrait) for a paper name; unknown names fall back to Letter.
|
||||
/// Paper size in points, as named (see `gridcraft_model::paper`); unknown names, and the empty
|
||||
/// name of a file that stated no paper, fall back to Letter.
|
||||
pub fn paper_size(name: &str) -> (f32, f32) {
|
||||
known_paper(name).unwrap_or((612.0, 792.0))
|
||||
}
|
||||
|
||||
/// Paper sizes in points, portrait. `B4`/`B5`/`B6` are the **JIS** sizes Japanese Excel and its
|
||||
/// e-mail attachments use (257×364, 182×257 and 128×182 mm); the ISO B series stays reachable as
|
||||
/// `B4 (ISO)`/`B5 (ISO)`/`B6 (ISO)`. The Japanese postcards and envelopes are the JIS ones too.
|
||||
pub(super) fn known_paper(name: &str) -> Option<(f32, f32)> {
|
||||
// Spaces, hyphens, underscores and parentheses don't matter: `B5 (JIS)`, `b5-jis` and `b5`
|
||||
// are one name.
|
||||
let n = name.trim().to_ascii_lowercase().replace([' ', '-', '_', '(', ')'], "");
|
||||
Some(match n.as_str() {
|
||||
"letter" | "usletter" => (612.0, 792.0),
|
||||
"legal" | "uslegal" => (612.0, 1008.0),
|
||||
"a4" => (595.28, 841.89),
|
||||
"a3" => (841.89, 1190.55),
|
||||
"a5" => (419.53, 595.28),
|
||||
"a6" => (297.64, 419.53),
|
||||
"tabloid" | "11x17" => (792.0, 1224.0),
|
||||
"ledger" => (1224.0, 792.0),
|
||||
"executive" => (522.0, 756.0),
|
||||
"b4" | "b4jis" => (728.50, 1031.81),
|
||||
"b5" | "b5jis" => (515.91, 728.50),
|
||||
"b6" | "b6jis" => (362.83, 515.91),
|
||||
"b4iso" => (708.66, 1000.63),
|
||||
"b5iso" => (498.90, 708.66),
|
||||
"b6iso" => (354.33, 498.90),
|
||||
"japanesepostcard" | "postcard" => (283.46, 419.53),
|
||||
"japanesedoublepostcard" | "doublepostcard" => (419.53, 566.93),
|
||||
"japaneseenvelope#3" | "chou#3" => (340.16, 666.14),
|
||||
// 長形4号 is 90×205 mm; 90×225 mm is 長形40号, a different envelope, so it stays out.
|
||||
"japaneseenvelope#4" | "chou#4" => (255.12, 581.10),
|
||||
"japaneseenvelopekaku#2" | "kaku#2" => (680.31, 941.10),
|
||||
"japaneseenvelopekaku#3" | "kaku#3" => (612.28, 785.20),
|
||||
_ => return None,
|
||||
})
|
||||
gridcraft_model::paper::size_pt(name).unwrap_or((612.0, 792.0))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- layout
|
||||
|
||||
@@ -974,6 +974,48 @@ fn page_setup_paper_sizes_reach_the_pdf_page() {
|
||||
assert!(s.execute("pageLayout.pageSetup", json!({"paper": "Napkin"})).is_err());
|
||||
}
|
||||
|
||||
/// An empty paper name means no paper is stated; it prints on Letter.
|
||||
#[test]
|
||||
fn an_unstated_paper_prints_on_letter() {
|
||||
let mut wb = gridcraft_model::Workbook::new();
|
||||
std::sync::Arc::make_mut(&mut wb.sheets[0]).set_value(CellRef::new(0, 0), gridcraft_core::Value::Text("x".into()));
|
||||
std::sync::Arc::make_mut(&mut wb.sheets[0]).print.paper = String::new();
|
||||
let layouts = super::layout::layouts(&wb, &super::layout::PrintOptions::default());
|
||||
let l = &layouts[0];
|
||||
assert_eq!((l.page_w, l.page_h), (612.0, 792.0));
|
||||
assert_eq!(paper_size(""), (612.0, 792.0));
|
||||
}
|
||||
|
||||
/// Both commands that set the paper store a known one under its table name, so every file
|
||||
/// format finds it.
|
||||
#[test]
|
||||
fn paper_size_commands_store_the_table_name() {
|
||||
let mut s = s();
|
||||
s.execute("pageLayout.size", json!({"paper": "b5-jis"})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().wb.sheets[0].print.paper, "B5");
|
||||
s.execute("pageLayout.pageSetup", json!({"paper": "11x17"})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().wb.sheets[0].print.paper, "Tabloid");
|
||||
// Statement and the envelopes XLSX names are accepted by both commands (Page Setup refused
|
||||
// them before), stored under their table name and printed on their own size.
|
||||
s.execute("range.setValues", json!({"range": "A1", "values": [["x"]]})).unwrap();
|
||||
let close = |v: &Json, want: f64| (v.as_f64().unwrap() - want).abs() < 0.01;
|
||||
for (cmd, given, stored, w, h) in [
|
||||
("pageLayout.pageSetup", "statement", "Statement", 396.0, 612.0),
|
||||
("pageLayout.pageSetup", "envelope #10", "Envelope #10", 297.0, 684.0),
|
||||
("pageLayout.pageSetup", "Envelope-DL", "Envelope DL", 311.81, 623.62),
|
||||
("pageLayout.size", "Statement", "Statement", 396.0, 612.0),
|
||||
("pageLayout.size", "envelope dl", "Envelope DL", 311.81, 623.62),
|
||||
] {
|
||||
s.execute(cmd, json!({"paper": given})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().wb.sheets[0].print.paper, stored, "{cmd} {given}");
|
||||
let pv = s.execute("file.printPreview", json!({})).unwrap();
|
||||
let page = &pv["pageList"][0];
|
||||
assert!(close(&page["width"], w) && close(&page["height"], h), "{cmd} {given}: {page}");
|
||||
}
|
||||
// A new workbook still prints on Letter.
|
||||
assert_eq!(gridcraft_model::Workbook::new().sheets[0].print.paper, "Letter");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_settings_never_panic_and_cap_pages() {
|
||||
let mut s = s();
|
||||
@@ -1344,3 +1386,39 @@ fn chart_budget_counts_path_points_and_drops_charts_that_do_not_fit() {
|
||||
assert!(pics.chart((0, 1), || vec![line(1000)]).is_none());
|
||||
assert!(pics.charts.is_empty() && pics.rejected_charts.contains(&(0, 1)));
|
||||
}
|
||||
|
||||
/// XLSX id 34 is ISO B5 (176 × 250 mm), not the Letter fallback of the old name.
|
||||
#[test]
|
||||
fn imported_xlsx_b5_envelope_uses_iso_b5_print_dimensions() {
|
||||
use std::io::{Cursor, Read, Write};
|
||||
let mut wb = gridcraft_model::Workbook::new();
|
||||
wb.sheet_mut(0).unwrap().set_value(CellRef::new(0, 0), gridcraft_core::Value::Text("x".into()));
|
||||
wb.sheet_mut(0).unwrap().print.paper = "Legal".into();
|
||||
let bytes = gridcraft_xlsx::write_xlsx(&wb).unwrap();
|
||||
let mut input = zip::ZipArchive::new(Cursor::new(&bytes)).unwrap();
|
||||
let mut output = zip::ZipWriter::new(Cursor::new(Vec::new()));
|
||||
for i in 0..input.len() {
|
||||
let mut part = input.by_index(i).unwrap();
|
||||
let mut data = Vec::new();
|
||||
part.read_to_end(&mut data).unwrap();
|
||||
let name = part.name().unwrap().to_string();
|
||||
if name == "xl/worksheets/sheet1.xml" {
|
||||
let xml = String::from_utf8(data).unwrap();
|
||||
assert!(xml.contains(r#"paperSize="5""#));
|
||||
// Set the input id independently of the shared paper-name mapping under test.
|
||||
data = xml.replace(r#"paperSize="5""#, r#"paperSize="34""#).into_bytes();
|
||||
}
|
||||
output.start_file(name, zip::write::SimpleFileOptions::default()).unwrap();
|
||||
output.write_all(&data).unwrap();
|
||||
}
|
||||
let bytes = output.finish().unwrap().into_inner();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.open", json!({"name": "iso-b5.xlsx", "base64": crate::io::base64_encode(&bytes)})).unwrap();
|
||||
let preview = s.execute("file.printPreview", json!({})).unwrap();
|
||||
let page = &preview["pageList"][0];
|
||||
assert!((page["width"].as_f64().unwrap() - 498.90).abs() < 0.01, "{page}");
|
||||
assert!((page["height"].as_f64().unwrap() - 708.66).abs() < 0.01, "{page}");
|
||||
let (pdf, pages) = pdf_of(&mut s, json!({}));
|
||||
assert_eq!(pages, 1);
|
||||
assert!(contains(&pdf, "/MediaBox [0 0 498.9 708.66]"));
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Size",
|
||||
["Page Layout", "Page Setup"],
|
||||
None,
|
||||
"{paper: Letter|Legal|A4|A3|A5|A6|Tabloid|Executive|Ledger|B4|B5|B6|B4 (ISO)|B5 (ISO)|B6 (ISO)|Japanese Postcard|Japanese Double Postcard|Japanese Envelope #3|Japanese Envelope #4|Japanese Envelope Kaku #2|Japanese Envelope Kaku #3 (B4/B5/B6 are the JIS sizes)}",
|
||||
"{paper: Letter|Legal|A4|A3|A5|A6|Tabloid|Executive|Ledger|B4|B5|B6|B4 (ISO)|B5 (ISO)|B6 (ISO)|Japanese Postcard|Japanese Double Postcard|Japanese Envelope #3|Japanese Envelope #4|Japanese Envelope Kaku #2|Japanese Envelope Kaku #3|Statement|Envelope #10|Envelope DL (B4/B5/B6 are the JIS sizes)}",
|
||||
has_doc,
|
||||
paper
|
||||
),
|
||||
@@ -324,7 +324,9 @@ fn orientation(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
}
|
||||
|
||||
fn paper(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
let paper = str_param(p, "paper").unwrap_or("Letter").to_string();
|
||||
// A known paper is stored under its table name; any other name is kept (it prints on Letter).
|
||||
let name = str_param(p, "paper").unwrap_or("Letter");
|
||||
let paper = gridcraft_model::paper::canonical(name).unwrap_or(name).to_string();
|
||||
edit(s, |cx| {
|
||||
let si = cx.sheet_index();
|
||||
cx.sheet_mut(si)?.print.paper = paper.clone();
|
||||
|
||||
Reference in New Issue
Block a user