mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-03 17:16:14 -06:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d58afe6112 | ||
|
|
8024a21722 |
@@ -153,6 +153,17 @@ pub struct HfSection {
|
||||
pub size: Option<f32>,
|
||||
}
|
||||
|
||||
/// Most bytes one section's expanded text may hold. A code like `&A` adds the whole sheet name
|
||||
/// (or file path) each time it appears, and a JSON workbook's names have no length limit.
|
||||
const MAX_SECTION_BYTES: usize = 64 * 1024;
|
||||
|
||||
/// Appends `s` unless the section would pass [`MAX_SECTION_BYTES`]; what doesn't fit is left out.
|
||||
fn push_field(text: &mut String, s: &str) {
|
||||
if text.len().saturating_add(s.len()) <= MAX_SECTION_BYTES {
|
||||
text.push_str(s);
|
||||
}
|
||||
}
|
||||
|
||||
/// Expands a header/footer string into its left, centre and right sections.
|
||||
pub fn expand_header_footer(spec: &str, cx: &HfContext) -> [HfSection; 3] {
|
||||
let mut out: [HfSection; 3] = Default::default();
|
||||
@@ -187,14 +198,14 @@ pub fn expand_header_footer(spec: &str, cx: &HfContext) -> [HfSection; 3] {
|
||||
i += 1 + digits.len();
|
||||
}
|
||||
}
|
||||
sec.text.push_str(&n.to_string());
|
||||
push_field(&mut sec.text, &n.to_string());
|
||||
}
|
||||
'N' | 'n' => sec.text.push_str(&cx.pages.to_string()),
|
||||
'D' | 'd' => sec.text.push_str(&cx.date),
|
||||
'T' | 't' => sec.text.push_str(&cx.time),
|
||||
'A' | 'a' => sec.text.push_str(&cx.sheet),
|
||||
'F' | 'f' => sec.text.push_str(&cx.file),
|
||||
'Z' | 'z' => sec.text.push_str(&cx.path),
|
||||
'N' | 'n' => push_field(&mut sec.text, &cx.pages.to_string()),
|
||||
'D' | 'd' => push_field(&mut sec.text, &cx.date),
|
||||
'T' | 't' => push_field(&mut sec.text, &cx.time),
|
||||
'A' | 'a' => push_field(&mut sec.text, &cx.sheet),
|
||||
'F' | 'f' => push_field(&mut sec.text, &cx.file),
|
||||
'Z' | 'z' => push_field(&mut sec.text, &cx.path),
|
||||
'&' => sec.text.push('&'),
|
||||
'B' | 'b' => sec.bold = !sec.bold,
|
||||
'I' | 'i' => sec.italic = !sec.italic,
|
||||
|
||||
@@ -414,6 +414,12 @@ fn pdf_contains_text_and_header_codes_expand() {
|
||||
assert_eq!(r.text, "P4/9 9:00 AM");
|
||||
assert_eq!(expand_header_footer("plain", &cx)[1].text, "plain");
|
||||
assert_eq!(expand_header_footer("&", &cx)[1].text, "");
|
||||
// A megabyte sheet name repeated by 5,000 `&A` codes stops at the section's 64 KiB.
|
||||
let long = HfContext { sheet: "S".repeat(1024 * 1024), ..cx };
|
||||
let [_, c, _] = expand_header_footer(&"&A".repeat(5_000), &long);
|
||||
assert!(c.text.is_empty(), "the name alone is over the limit");
|
||||
let short = HfContext { sheet: "S".repeat(1000), ..Default::default() };
|
||||
assert_eq!(expand_header_footer(&"&A".repeat(5_000), &short)[1].text.len(), 65_000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user