Repository navigation
fix(OdfFile): start a new page at every master page switch in ODT -> DOCX - #336
Conversation
…DOCX A paragraph or table whose style sets style:master-page-name starts a new page (ODF 1.3 part 3, 19.501), also when the named master page is the one already in use or has the same page layout. The DOCX writer only started a new section when the master page name and its page layout both changed, so in all other cases the page break was lost and the text ran on on the same page. Write a page break before every body paragraph or table that names a master page, except before the first paragraph of the body, inside hidden sections and inside tables. As in LibreOffice, take the master page from the nearest parent style that sets it, stop at an empty value, check paragraphs inside tables of contents and other indexes, and ignore names of master pages that do not exist. The reader now keeps empty master page names so that they can stop the inheritance; master_page_name_by_name() still returns no master page for them. No second break is written when the paragraph style already breaks the page with fo:break-before="page", and fo:break-before="auto" on a table style no longer cancels the break of the table's master page. The existing section handling is unchanged. Tested with two new test_odf cases and a fixture that covers each of these cases; they fail without this change and pass with it. All ctest suites and the conversion test pass. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Schaefer <christoph.schaefer@nextcloud.com>
DmySyz
left a comment
There was a problem hiding this comment.
A note from Claude (if you think that it shouldn't block this PR - can be reconsidered):
Worth checking before merge
A real page-style change from a non-automatic paragraph style may give an empty page.
- When the master page name and its layout both change, the existing code still writes a section break (w:sectPr, next page). This PR also sets the break value 2 on the same paragraph.
- Automatic style: the break becomes w:pageBreakBefore on the first paragraph of the new section. Word and LibreOffice don't add a page for that, since the paragraph is already at the top of a page.
- Named style (or an automatic style without paragraph properties): the properties aren't written inline, so the value survives to paragraph::docx_convert and becomes a <w:br w:type="page"/> run at the start of the paragraph (text_elements.cpp:394-402).
- An explicit page break right after a next-page section break gives an empty page in Word.
- LibreOffice lets named styles carry a page style ("Breaks → With page style", e.g. on a chapter heading style), so this can happen.
- The tests don't combine a page-layout change with the new break; the PR says section handling is unchanged.
- Suggestion: add a fixture where a named style switches to a master page with a different layout, and check there is no <w:br w:type="page"/>. Or skip setting the break when a section break is already pending for that paragraph.
Minor
- List paragraphs: break_before_master_page runs on top-level body elements, section children and index entries, but not on paragraphs inside a text:list. For a list, the element's style is the list style, so nothing is found. A list paragraph naming a master page (rare) still gets no break.
- Lookup ignores the style family: master_page_name_ is keyed by style name only. The new lookup passes the family only for walking parents. That's older behaviour and harmless as long as a table style and a paragraph style don't share a name.
… -> DOCX When a master page switch also changes the page layout, the paragraph before it still ends a section. A paragraph with an automatic style that has paragraph properties got the new page break as w:pageBreakBefore, which adds no page when the paragraph already starts one. A paragraph with a named style, or with an automatic style without paragraph properties, got a page break run at its start instead. After a section break that starts a new page, for example one that changes the page size or orientation, that run gives an empty page. Write the break as w:pageBreakBefore for these paragraphs too, right after w:pStyle. Paragraphs inside tables, drawings and notes keep the old handling. The section handling is unchanged. Tested with a new test_odf case and fixture: a named style and an automatic style without paragraph properties switch to a landscape master page and back. It fails without this change and passes with it; an automatic style with paragraph properties guards the path that was already right. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Schaefer <christoph.schaefer@nextcloud.com>
|
Thanks, good catch. The main point holds, and I fixed it in 9a4194d. Empty page after a page layout change Confirmed. For a named style, or an automatic style without paragraph properties, One detail is different from what you describe: that section break is Instead, the break is now always written as
As you said, Test: Minor 1: list paragraphs Confirmed. Strictly, Minor 2: lookup ignores the style family Agreed, and it predates this PR. On main, Assisted-by: ClaudeCode:claude-opus-5-5 |
DmySyz
left a comment
There was a problem hiding this comment.
Thanks for the fix. There may or may not be one more issue:
The description says "for a table, the break is written as w:pageBreakBefore on the first paragraph of the table's first cell". By reading the code, that only happens when that paragraph has an automatic style with paragraph properties.
How the value travels:
- break_before_master_page sets the value 2 before the table starts.
- take_page_break_before returns false inside a table.
- A style-less cell paragraph (like text:p in the fixture's table T1) writes no w:pPr at all (process_paragraph_attr returns 0).
- A named one, such as LibreOffice's default "Table Contents", takes the w:pStyle path.
In both cases the value reaches text_elements.cpp:394, which writes <w:br w:type="page"/> inside the first cell.
Why the test passes anyway: has_page_break() accepts a page-break run as well as w:pageBreakBefore, so MasterPageBreakOnceAndOnlyOnPages passes either way.
Why the form matters (hand-made DOCX, a paragraph followed by a table):
| LibreOffice | Euro-Office (sdkjs) | |
|---|---|---|
| Page-break run | ignored, table stays on page 1 | ignored |
| w:pageBreakBefore | table starts on a new page | ignored |
Word also uses w:pageBreakBefore on the first cell's paragraph for "table starts on a new page". The editor ignores both forms; that's a separate sdkjs issue.
Suggested fix:
- Write w:pageBreakBefore for the first paragraph of the first cell whatever its style: let the pending table break through take_page_break_before there, and open a w:pPr for the style-less case.
- Make the table assertion check for w:pageBreakBefore and the absence of a run.
This path already existed for tables with fo:break-before="page". But this PR sends master-page tables through it, and its description says otherwise.
Note: the table finding comes from reading the code. Running the existing test with the stricter assertion would confirm or refute it.
…age-break Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Schaefer <christoph.schaefer@nextcloud.com>
…t cell A table that starts a new page (a master page switch or fo:break-before="page") leaves the break pending for the first paragraph of its first cell. Unless that paragraph had an automatic style with paragraph properties, the break was written as a page break run inside the cell. LibreOffice and Word ignore such a run, so the table stayed on the previous page. Let the pending break through take_page_break_before inside tables too, and write w:pPr with w:pageBreakBefore for a paragraph without a style. Header and footer paragraphs keep the old output. The fixture gets tables whose first cell paragraph has a named style and an automatic style without and with paragraph properties. The table checks now require w:pageBreakBefore and no page break run. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Schaefer <christoph.schaefer@nextcloud.com>
|
Thanks, you were right. I checked it with a build of the PR head: for a table after a master page switch, the first cell paragraph got <w:br w:type="page"/> when it had no style, a named style, or an automatic style without paragraph properties. Only an automatic style with paragraph properties got w:pageBreakBefore. Fixed in 38c2588, as you suggested:
This also fixes the same case for tables with fo:break-before="page", which used this path before. Tests: the table check in MasterPageBreakOnceAndOnlyOnPages now requires w:pageBreakBefore and no page break run. The new TableBreakIsPageBreakBeforeInFirstCell covers a first cell paragraph with a named style ("Table Contents"), an automatic style without paragraph properties and one with them. It also checks that the second paragraph in the cell gets no break and that document.xml has no page break run at all. Both fail without the fix. I also merged main into the branch (cbf22a2), because the fix touches the same lines as the header/footer spacing change on main. Both are kept. Locally Could you take another look? |
Resolve two conflicts: - office_text.cpp: main added the check for a page style change after a table (#369) before each body element is converted; this branch adds the master page break there. Keep both: the table check first, then the break, then the conversion. - test_odf CMakeLists.txt: keep the entries of this branch and the ones added on main. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Schaefer <christoph.schaefer@nextcloud.com>
|
@DmySyz the table point from your second review is fixed in 38c2588 (details in my comment from 18 Nov): the first paragraph of the table's first cell gets Assisted-by: ClaudeCode:claude-opus-5-5 |
Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Schaefer <christoph.schaefer@nextcloud.com>
In ODF, a paragraph or table whose style names a master page (style:master-page-name) starts a new page, also when that master page is the one already in use. The ODT -> DOCX conversion only wrote a section break when both the master page name and its page layout changed. If the page style stayed the same, or a different page style had the same layout, the page break was lost and the following text ran on on the same page, so the converted document had fewer pages than in LibreOffice.
The fix writes a page break before every paragraph or table in the body that names a master page. Like LibreOffice, it:
The section handling for real page style changes stays as it was.
Changes:
master_page_name_by_name()still returns no master page for an empty name, so existing callers behave as before.fo:break-before="page"gets no second break (that added an empty page). A table style withfo:break-before="auto"no longer cancels the break of the table's master page. Missing master pages and master page names inside table cells no longer break the page.For a table, the break is written as
w:pageBreakBeforeon the first paragraph of the table's first cell. This holds whatever the style of that paragraph. The same applies to a table with fo:break-before="page", which before got a page break run in its first cell (ignored by LibreOffice) unless the paragraph had an automatic style with paragraph properties.Tests: four test_odf cases with two small ODT fixtures.
OdtToDocxMasterPageBreak.TableBreakIsPageBreakBeforeInFirstCellchecksw:pageBreakBeforeand no page break run for a table whose first cell paragraph has a named style, an automatic style without and with paragraph properties.OdtToDocxMasterPageBreak.PageLayoutChangeGivesNoEmptyPagechecks that a master page with another page layout givesw:pageBreakBeforeand no page break run.OdtToDocxMasterPageBreak.MasterPageNameStartsNewPagechecks the break for the same master page on a later paragraph, a master page inherited from the parent style and a paragraph inside a table of contents, and checks that no break is written on the first paragraph, inside a hidden section or after an empty override.OdtToDocxMasterPageBreak.MasterPageBreakOnceAndOnlyOnPageschecks the single break for a style withfo:break-before="page"(also through an automatic style), a missing master page, a table withfo:break-before="auto"and a master page name inside a table cell. Each case fails without the part of the change that handles it, and both tests pass with the whole change. All ctest suites and the conversion test pass.Merge order: please merge #335 first. Today the editor saves text sections in ODT as master page switches, which that PR fixes; without it, this change would show those switches as page breaks after a save.
Assisted-by: ClaudeCode:claude-opus-5-5
🤖 Generated with Claude Code