From 7fcdc80b61e7535e97e8a89b714b0111dacf25d4 Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Sun, 11 Oct 2026 18:01:55 +0000 Subject: [PATCH] feat: read IncludeSectionPrefix and name pages and sections by it Section chunk 0x4C02 stores IncludeSectionPrefix as a u16 after a u16 0. The section Name is its prefix; pages of a section that includes the prefix are named with it. The first section starts at its stored start even when it continues numbering, and the page descriptor writes a return in the marker as U+2029. --- docs/format/objects.md | 47 +++++++++++++++++++++++++++++++++++------- src/idml/designmap.rs | 4 ++-- src/idml/pages.rs | 23 +++++++++++++++++---- src/idml/spread.rs | 5 +++-- src/model/spread.rs | 10 ++++++--- 5 files changed, 71 insertions(+), 18 deletions(-) diff --git a/docs/format/objects.md b/docs/format/objects.md index 058953c..fc2db4e 100644 --- a/docs/format/objects.md +++ b/docs/format/objects.md @@ -2845,7 +2845,8 @@ Section chunk 0x4C02: | u32 | Page number start (`PageNumberStart`) | | u32 | Page number style: 0x4C15 = `Arabic`, 0x4C17 = `LowerRoman`, 0x4C12 = `Kanji`, 0x4C13 = `DoubleLeadingZeros`, 0x4C16 = `UpperRoman`, 0x4C19 = `LowerLetters`, 0x4C1A = `SingleLeadingZeros` (below) | | u32 | Continue numbering (1 = true, `ContinueNumbering`) | -| u32 | Not identified | +| u16 | 0 in every chunk | +| u16 | Include section prefix (1 = true, `IncludeSectionPrefix`) | | u8 flag, string | `AlternateLayout`: the string, with `$ID/` before it if the flag is 1 | In the 546 sections of the trustworthy pairs, 539 chunks have all these @@ -2854,6 +2855,18 @@ fields; the prefix, marker and alternate layout equal the IDML values in The 7 shorter chunks are in DOM 7 and 8 files and end after the second u32 above. +**Include section prefix.** Over the 1,345 sections of the 1,252 +trustworthy pairs after 2026-10, 1,336 chunks have the u16 pair after +continue numbering. The first u16 is 0 in all of them. The second is 1 +in all 11 sections with IDML `IncludeSectionPrefix="true"` (10 distinct +files) and 0 in all 1,325 with `false`. The other 9 chunks, one each in +9 DOM 7 files, end before the field; IDML gives them `false`. + +**Section name.** IDML `Name` equals `SectionPrefix` in all 1,345 +sections: 15 sections in 14 distinct files have a prefix, and 1,330 +have an empty prefix and an empty name. The prefix does not depend on +`IncludeSectionPrefix` (9 of the 15 include it, 6 do not). + **Alternate layouts.** A section with a layout name (not empty and not `$ID/`) starts an alternate layout, and so does the first section. `AlternateLayoutLength` of a section is the number of pages from its @@ -2865,11 +2878,19 @@ in IDML from DOM 8 on. Over the trustworthy pairs the converted **Page descriptor.** A document page has a `Descriptor` property, a list of the section prefix, the page number style, `ContinueNumbering`, -`false`, the page number and the marker; from DOM 20 on the page number -is in the list twice. Over the trustworthy pairs: 7,800 of 7,924 pages; -64 others are in sections with a style the converter does not know (IDML -gives it as an empty string), and the converter leaves their descriptor -out. The fourth item is `IncludeSectionPrefix`, false in every sample. +`IncludeSectionPrefix`, the page number and the marker; from DOM 20 on +the page number is in the list twice. Over the trustworthy pairs: 7,800 +of 7,924 pages; 64 others are in sections with a style the converter +does not know (IDML gives it as an empty string), and the converter +leaves their descriptor out. + +In the descriptor, a return (U+000D) in the marker is a paragraph +separator (U+2029); the section's `Marker` attribute keeps the return. +Over the 15,855 pages of the trustworthy pairs after 2026-10, 568 pages +(6 distinct files) are in sections whose marker ends with a return, and +their descriptors end with U+2029 in its place (568 of 568). No marker +has a return elsewhere. 1,080 pages have a marker without a return, +which the descriptor repeats as it is, and 14,207 have an empty marker. Evidence: @@ -2923,7 +2944,19 @@ The converter writes `PageNumberStyle` for the seven codes and leaves it out (with a warning) for other codes. The converter names document pages by their number in their section, -counting on from the previous section when numbering continues. In a +counting on from the previous section when numbering continues. The +section at the document's first page starts at its page number start +even when it continues numbering: the document continues the numbering +of a book. Over the 1,252 trustworthy pairs after 2026-10, the IDML +page number of the first page equals that field in 1,252 of 1,252, +including 18 documents (18 distinct files) whose first section +continues numbering from a start above 1. The 49 later sections that +continue numbering count on from the previous section (49 of 49), though +41 store another start. In a section with `IncludeSectionPrefix`, the +page name is the section prefix followed by the number: `2-1`, `2-2` +for prefix `2-`, and `1장_01` for a prefix `1장_` and style 0x4C1A. +After these rules, page names and descriptors equal the IDML values on +15,855 of 15,855 pages of those pairs. In a section with style 0x4C17 it writes the number in lower-case Roman (i, ii, iii), as in the PDF labels above, and in a section with style 0x4C12 in Chinese digits, as the folios show. No pair shows IDML page names in diff --git a/src/idml/designmap.rs b/src/idml/designmap.rs index 1ddda11..c5d5696 100644 --- a/src/idml/designmap.rs +++ b/src/idml/designmap.rs @@ -409,9 +409,9 @@ impl Writer<'_> { x.start("Section") .attr("Self", uref(Some(section.uid))) .attr("Length", length.to_string()) - .attr("Name", "") + .attr("Name", §ion.prefix) .attr("ContinueNumbering", section.continue_numbering.to_string()) - .attr("IncludeSectionPrefix", "false") + .attr("IncludeSectionPrefix", section.include_prefix.to_string()) .attr("Marker", §ion.marker) .attr("PageStart", uref(Some(pages[first]))); if doc.version.major >= 8 diff --git a/src/idml/pages.rs b/src/idml/pages.rs index 64d9cf5..fceacb2 100644 --- a/src/idml/pages.rs +++ b/src/idml/pages.rs @@ -160,7 +160,8 @@ pub(super) fn kanji_digits(n: u32) -> String { } /// The name of each document page: its number (see `page_numbers`) in -/// its section's style (objects.md, master spread names and sections). +/// its section's style, after the section prefix if the section includes +/// it (objects.md, master spread names and sections). pub(super) fn page_names(doc: &Document) -> Vec { let numbers = page_numbers(doc); let mut out: Vec = numbers.iter().map(u32::to_string).collect(); @@ -172,10 +173,13 @@ pub(super) fn page_names(doc: &Document) -> Vec { numbering::SINGLE_LEADING_ZEROS => single_leading_zeros, numbering::DOUBLE_LEADING_ZEROS => double_leading_zeros, numbering::LOWER_LETTERS => lower_letter, - _ => continue, + _ => |n| n.to_string(), }; for i in (first..first + length).filter(|&i| numbers[i] > 0) { out[i] = name(numbers[i]); + if s.include_prefix { + out[i].insert_str(0, &s.prefix); + } } } out @@ -193,7 +197,9 @@ pub(super) fn page_sections(doc: &Document) -> Vec<(Section, u32)> { out } -/// The number shown on each document page, from the sections. Pages +/// The number shown on each document page, from the sections. The +/// section at the first page starts at its start number even when it +/// continues numbering (from the previous document of a book). Pages /// not covered by a section are numbered by their position. pub(super) fn page_numbers(doc: &Document) -> Vec { let count = document_pages(doc).len(); @@ -202,7 +208,7 @@ pub(super) fn page_numbers(doc: &Document) -> Vec { for (s, first, length) in section_ranges(doc) { let mut number = match next { Some(n) if s.continue_numbering => n, - None if s.continue_numbering => first as u32 + 1, + None if s.continue_numbering && first > 0 => first as u32 + 1, _ => s.start, }; for n in out.iter_mut().skip(first).take(length) { @@ -236,6 +242,7 @@ mod tests { uid, page, continue_numbering, + include_prefix: false, start, style: numbering::ARABIC, prefix: String::new(), @@ -274,6 +281,14 @@ mod tests { let mut doc = doc; doc.sections[0].style = numbering::LOWER_ROMAN; assert_eq!(page_names(&doc), ["i", "ii", "1", "2", "3", "4"]); + // The first section starts at its start number even when it + // continues numbering; a section that includes its prefix puts + // it before each page name. + doc.sections[0].start = 5; + doc.sections[2].prefix = "B-".into(); + doc.sections[2].include_prefix = true; + assert_eq!(page_numbers(&doc), [5, 6, 1, 2, 3, 4]); + assert_eq!(page_names(&doc), ["v", "vi", "B-1", "B-2", "3", "4"]); } #[test] diff --git a/src/idml/spread.rs b/src/idml/spread.rs index b24c7d1..25eebae 100644 --- a/src/idml/spread.rs +++ b/src/idml/spread.rs @@ -1492,13 +1492,14 @@ impl Writer<'_> { item("enumeration", style.into()) }, item("boolean", s.continue_numbering.to_string()), - item("boolean", "false".into()), + item("boolean", s.include_prefix.to_string()), item("long", number.to_string()), ]; if self.doc.version.major >= 20 { items.push(item("long", number.to_string())); } - items.push(item("string", s.marker.clone())); + // A return in the marker is a paragraph separator here. + items.push(item("string", s.marker.replace('\r', "\u{2029}"))); props.push(Node { tag: "Descriptor".into(), attrs: vec![("type".into(), "list".into())], diff --git a/src/model/spread.rs b/src/model/spread.rs index 7d70023..00b5661 100644 --- a/src/model/spread.rs +++ b/src/model/spread.rs @@ -199,6 +199,8 @@ pub struct Section { /// starts at the first page of the document. pub page: Option, pub continue_numbering: bool, + /// `IncludeSectionPrefix`: page names start with the prefix. + pub include_prefix: bool, pub start: u32, /// Page number style code (see `numbering`). pub style: u32, @@ -300,6 +302,7 @@ impl<'a> Reader<'a> { uid, page: None, continue_numbering: true, + include_prefix: false, start: 1, style: numbering::ARABIC, prefix: String::new(), @@ -307,8 +310,8 @@ impl<'a> Reader<'a> { alternate_layout: None, }; // u8, string (prefix), u8, string (marker), u32 first page, u32 - // page number start, u32 numbering style, u32 continue, u32, then - // u8 and string (alternate layout). + // page number start, u32 numbering style, u32 continue, u16 0, u16 + // include prefix, then u8 and string (alternate layout). if let Some(d) = self.chunk(uid, chunk::SECTION_INFO)? { let mut c = self.cursor(&d); let parsed = (|| -> Result<(String, String, [u32; 4]), Error> { @@ -326,7 +329,8 @@ impl<'a> Reader<'a> { section.prefix = prefix; section.marker = marker; section.alternate_layout = (|| -> Result<(u8, String), Error> { - c.u32()?; + c.u16()?; + section.include_prefix = c.u16()? != 0; let flag = c.flag()?; Ok((flag, c.string()?)) })()