Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge tested main a930276fd746 into PR #305

sanyamsingh001745712fa7db2
diff --cc crates/doc/src/attrs.rs
index 9e8fe541f,b100e19af..6fc627b31
--- a/crates/doc/src/attrs.rs
+++ b/crates/doc/src/attrs.rs
@@@ -794,10 -960,13 +969,13 @@@ attr_set!
/// Named list (Define Lists) the numbers belong to ("" = the story's own list).
list_name: String = String::new(),
/// Start At: restart the numbering at this number.
- start_at: Option<u32> = None,
+ start_at: Option<u32> = None => "deserialize_explicit_value",
bullet_char: String = "\u{2022}".into(),
number_style: NumberStyle = NumberStyle::Arabic,
- /// Text after the number/bullet (`^t` = tab).
+ /// A numbered paragraph's label: `^#` is the number, other metacharacters expand as in
+ /// [`expand_list_text`].
+ number_expression: String = "^#.^t".into(),
+ /// Text after the bullet (metacharacters expand as in [`expand_list_text`]).
list_separator: String = "\t".into(),
balance_ragged: bool = false,
optical_margin: bool = false,
diff --cc crates/format/src/lib.rs
index c34fee1f4,86393f803..6e066778f
--- a/crates/format/src/lib.rs
+++ b/crates/format/src/lib.rs
@@@ -300,103 -647,78 +647,183 @@@ mod tests
assert!(matches!(load(&buf.into_inner()), Err(FormatError::TooNew(99))));
}
+ fn r7a_nullable_document(local_clear: bool, style_clear: bool) -> Document {
+ use designcraft_doc::{CharAttrs, ParaAttrs};
+ let mut d = Document::new(&NewDocument { pages: 1, ..Default::default() });
+ let base = d.styles.default_paragraph.clone();
+ let chars = CharAttrs {
+ leading_aki: Some(Some(4.0)),
+ trailing_aki: Some(Some(3.0)),
+ underline_weight: Some(Some(2.0)),
+ underline_offset: Some(Some(1.0)),
+ strikethrough_weight: Some(Some(2.0)),
+ strikethrough_offset: Some(Some(1.0)),
+ ..Default::default()
+ };
+ let para = ParaAttrs {
+ start_at: Some(Some(7)),
+ paragraph_kashida_width: Some(Some(0.5)),
+ kinsoku: Some(Some(Default::default())),
+ ..Default::default()
+ };
+ let style = std::sync::Arc::make_mut(&mut d.styles).paragraph.iter_mut().find(|s| s.name == base).unwrap();
+ style.chars = chars;
+ style.para = para;
+ let clear_chars = CharAttrs {
+ leading_aki: Some(None),
+ trailing_aki: Some(None),
+ underline_weight: Some(None),
+ underline_offset: Some(None),
+ strikethrough_weight: Some(None),
+ strikethrough_offset: Some(None),
+ ..Default::default()
+ };
+ let clear_para = ParaAttrs { start_at: Some(None), paragraph_kashida_width: Some(None), kinsoku: Some(None), ..Default::default() };
+ let selected = if style_clear {
+ std::sync::Arc::make_mut(&mut d.styles).paragraph.push(designcraft_doc::styles::ParagraphStyle {
+ name: "Defaults".into(),
+ based_on: Some(base.clone()),
+ next_style: None,
+ para: clear_para.clone(),
+ chars: clear_chars.clone(),
+ shortcut: String::new(),
+ });
+ "Defaults".to_string()
+ } else {
+ base
+ };
+ let format = ParaFormat {
+ style: selected,
+ para: if local_clear { clear_para } else { ParaAttrs::default() },
+ chars: if local_clear { clear_chars } else { CharAttrs::default() },
+ table: None,
+ };
++ // Pin legacy paragraph defaults so both formats isolate nullable overrides.
++ for style in &mut d.styles_mut().paragraph {
++ style.para.keep_all_lines = Some(true);
++ style.para.drop_cap_align_left = Some(false);
++ }
+ let layer = d.default_layer();
+ d.add_text_frame(SpreadRef::Doc(0), Rect::new(36.0, 36.0, 300.0, 400.0), layer, "clear", format).unwrap();
+ d
+ }
+
+ fn r7a_nullable_resolution(d: &Document) -> (designcraft_doc::ParaProps, designcraft_doc::CharProps) {
+ let s = d.stories.values().find(|s| s.text == "clear").unwrap();
+ d.styles.resolve_para(s.paras.first().unwrap())
+ }
+
+ fn r7a_assert_nullable_defaults_survive(d: &Document) {
+ let expected = r7a_nullable_resolution(d);
+ assert_eq!(expected.0.start_at, None);
+ assert_eq!(expected.0.paragraph_kashida_width, None);
+ assert_eq!(expected.0.kinsoku, None);
+ assert_eq!(expected.1.leading_aki, None);
+ for bytes in [save(d).unwrap(), serde_json::to_vec(d).unwrap()] {
+ let back = load(&bytes).unwrap();
+ assert_eq!(r7a_nullable_resolution(&back), expected, "saved explicit defaults must not become inherited values");
+ assert_eq!(back, *d, "native ZIP and legacy JSON preserve the complete document");
+ }
+ }
+
+ #[test]
+ fn r7a_native_local_default_text_overrides_survive_zip_and_legacy_json() {
+ r7a_assert_nullable_defaults_survive(&r7a_nullable_document(true, false));
+ }
+
+ #[test]
+ fn r7a_native_named_style_default_overrides_survive_zip_and_legacy_json() {
+ r7a_assert_nullable_defaults_survive(&r7a_nullable_document(false, true));
+ }
+
+ #[test]
+ fn r7a_native_nullable_missing_and_explicit_value_controls() {
+ let d = r7a_nullable_document(false, false);
+ assert_eq!(r7a_nullable_resolution(&d).0.start_at, Some(7));
+ assert_eq!(r7a_nullable_resolution(&d).1.leading_aki, Some(4.0));
+ for bytes in [save(&d).unwrap(), serde_json::to_vec(&d).unwrap()] {
+ let back = load(&bytes).unwrap();
+ assert_eq!(r7a_nullable_resolution(&back), r7a_nullable_resolution(&d));
+ assert_eq!(back, d);
+ }
+ let attrs: designcraft_doc::CharAttrs = serde_json::from_value(json!({"size":null})).unwrap();
+ assert!(attrs.size.is_none(), "nonnullable fields retain their existing null contract");
+ let props: designcraft_doc::CharProps = serde_json::from_value(json!({"leadingAki":null})).unwrap();
+ assert_eq!(props.leading_aki, None);
+ }
++
+ /// Write `len` bytes of `fill` to the current entry of `z`.
+ fn write_fill(z: &mut zip::ZipWriter<Cursor<Vec<u8>>>, len: usize, fill: u8) {
+ let chunk = vec![fill; 1 << 20];
+ let mut left = len;
+ while left > 0 {
+ let n = left.min(chunk.len());
+ z.write_all(&chunk[..n]).unwrap();
+ left -= n;
+ }
+ }
+
+ /// Make the archive claim that entry `name` expands to `size` bytes, in both its local header
+ /// and its central directory record (the archive has no ZIP64 fields or data descriptors).
+ fn claim_size(bytes: &mut [u8], name: &str, size: u32) {
+ let mut patched = 0;
+ for i in 0..bytes.len().saturating_sub(46) {
+ let (size_at, len_at, name_at) = match &bytes[i..i + 4] {
+ b"PK\x03\x04" => (22, 26, 30),
+ b"PK\x01\x02" => (24, 28, 46),
+ _ => continue,
+ };
+ let len = u16::from_le_bytes([bytes[i + len_at], bytes[i + len_at + 1]]) as usize;
+ if bytes.get(i + name_at..i + name_at + len) == Some(name.as_bytes()) {
+ bytes[i + size_at..i + size_at + 4].copy_from_slice(&size.to_le_bytes());
+ patched += 1;
+ }
+ }
+ assert_eq!(patched, 2, "{name} not found in both headers");
+ }
+
+ /// A small archive whose `document.json` expands past the 256 MiB limit while claiming to
+ /// hold one byte is refused once the limit is reached, with an error that says so, instead of
+ /// being read whole into memory.
+ #[test]
+ fn oversized_document_json_is_refused() {
+ let mut z = zip::ZipWriter::new(Cursor::new(Vec::new()));
+ z.start_file("document.json", zip::write::SimpleFileOptions::default()).unwrap();
+ write_fill(&mut z, (256 << 20) + 1, b' ');
+ let mut bytes = z.finish().unwrap().into_inner();
+ assert!(bytes.len() < 4 << 20, "{}", bytes.len());
+ claim_size(&mut bytes, "document.json", 1);
+ let Err(err) = load(&bytes) else { panic!("an oversized document.json loaded") };
+ assert!(err.to_string().contains("limit"), "{err}");
+ }
+
+ /// An embedded file that expands past the 1 GiB limit (and says so) is refused too.
+ #[test]
+ fn oversized_asset_is_refused() {
+ let mut d = Document::new(&NewDocument::default());
+ let aid = png_asset(&mut d, &[1]);
+ place(&mut d, aid, 0.0);
+ let mut z = zip::ZipWriter::new(Cursor::new(Vec::new()));
+ z.start_file("document.json", zip::write::SimpleFileOptions::default()).unwrap();
+ z.write_all(&serde_json::to_vec(&d).unwrap()).unwrap();
+ z.start_file(format!("assets/{}.png", aid.0), zip::write::SimpleFileOptions::default()).unwrap();
+ write_fill(&mut z, (1 << 30) + 1, 0);
+ let Err(err) = load(&z.finish().unwrap().into_inner()) else { panic!("an oversized asset loaded") };
+ assert!(err.to_string().contains("limit"), "{err}");
+ }
+
+ /// An archive with more entries than any document needs is refused before any is read.
+ #[test]
+ fn too_many_entries_are_refused() {
+ let d = Document::new(&NewDocument::default());
+ let mut z = zip::ZipWriter::new(Cursor::new(Vec::new()));
+ let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
+ z.start_file("document.json", stored).unwrap();
+ z.write_all(&serde_json::to_vec(&d).unwrap()).unwrap();
+ for i in 0..100_000 {
+ z.start_file(format!("junk/{i}"), stored).unwrap();
+ }
+ let Err(err) = load(&z.finish().unwrap().into_inner()) else { panic!("an archive with 100 001 entries loaded") };
+ assert!(err.to_string().contains("entries"), "{err}");
+ }
}