Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions OdfFile/Reader/Converter/docx_conversion_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,7 @@ class docx_conversion_context : boost::noncopyable
styles_map styles_map_;
std::unordered_map<std::wstring,bool> status_para{};
bool process_headers_footers_;
bool body_started_ = false; // a body paragraph has been written
bool delayed_converting_;
bool convert_delayed_enabled_;

Expand Down
2 changes: 1 addition & 1 deletion OdfFile/Reader/Format/odf_document_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ void odf_document::Impl::parse_styles(office_element *element)
continue;
}

if (styleInst->style_master_page_name_ && !styleInst->style_master_page_name_->empty())
if (styleInst->style_master_page_name_)
context_->styleContainer().add_master_page_name(styleInst->style_name_, *styleInst->style_master_page_name_);

context_->styleContainer().add_style(styleInst->style_name_,
Expand Down
27 changes: 26 additions & 1 deletion OdfFile/Reader/Format/odfcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,36 @@ namespace cpdoccore {
{
_CP_OPT(std::wstring) master_page;

// An empty name only stops the inheritance (see master_page_name_inherited), it is no master page.
map_wstring_wstring::const_iterator res = master_page_name_.find(StyleName);
if (res != master_page_name_.end())
if (res != master_page_name_.end() && !res->second.empty())
master_page = res->second;
return master_page;
}

_CP_OPT(std::wstring) styles_container::master_page_name_inherited(const std::wstring& StyleName,
odf_types::style_family::type Type) const
{
// LibreOffice keeps the master page as a paragraph attribute, so child styles inherit it.
// An empty value stops the inheritance. The depth limit guards against parent cycles.
std::wstring name = StyleName;
style_instance* inst = style_by_name(StyleName, Type, false);

for (int depth = 0; depth < 32; depth++)
{
map_wstring_wstring::const_iterator res = master_page_name_.find(name);
if (res != master_page_name_.end())
{
if (res->second.empty())
return boost::none;
return res->second;
}
if (!inst || !(inst = inst->parent()))
break;
name = inst->name();
}
return boost::none;
}

void styles_container::set_new_name_master_page(std::wstring ws_name_layout, std::wstring ws_new_name)
{
Expand Down
3 changes: 3 additions & 0 deletions OdfFile/Reader/Format/odfcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ class styles_container
style_instance * style_default_by_type (odf_types::style_family::type Type) const;

const _CP_OPT(std::wstring) master_page_name_by_name(const std::wstring & StyleName) const;
// style:master-page-name of the style or the nearest parent style that sets it; none when empty
_CP_OPT(std::wstring) master_page_name_inherited(const std::wstring & StyleName,
odf_types::style_family::type Type) const;

instances_array & instances() { return instances_; }

Expand Down
2 changes: 2 additions & 0 deletions OdfFile/Reader/Format/office_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "serialize_elements.h"
#include "odfcontext.h"
#include "text_elements.h"
#include "style_paragraph_properties.h"

namespace cpdoccore {
Expand Down Expand Up @@ -273,6 +274,7 @@ void office_text::docx_convert(oox::docx_conversion_context & Context)
}
}
}
text::break_before_master_page(Context, content_[i]);
content_[i]->docx_convert(Context);
}
Context.end_office_text();
Expand Down
3 changes: 2 additions & 1 deletion OdfFile/Reader/Format/style_table_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ void table_format_properties::docx_convert(oox::docx_conversion_context & Contex
_tblPr << L"<w:tblW w:type=\"pct\" w:w=\"5000\"/>";
}

if (common_break_attlist_.fo_break_before_)
// A master page switch has already asked for a new page (2); fo:break-before="auto" must not cancel it
if (common_break_attlist_.fo_break_before_ && Context.get_page_break_before() != 2)
{
Context.set_page_break_before(common_break_attlist_.fo_break_before_->get_type());
}
Expand Down
8 changes: 0 additions & 8 deletions OdfFile/Reader/Format/styles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,14 +637,6 @@ void style::add_attributes( const xml::attributes_wc_ptr & Attributes )
else
style_default_outline_level_= XmlUtils::GetInteger(*sTest);
}

if (style_master_page_name_)
{
if (style_master_page_name_->empty())
{
style_master_page_name_.reset();
}
}
}

void style::add_child_element( xml::sax * Reader, const std::wstring & Ns, const std::wstring & Name)
Expand Down
41 changes: 41 additions & 0 deletions OdfFile/Reader/Format/text_elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ void paragraph::docx_convert(oox::docx_conversion_context & Context, _CP_OPT(std
Context.end_paragraph_style();
Context.finish_paragraph();

if (!Context.process_headers_footers_ && !in_drawing &&
Context.get_process_note() == oox::docx_conversion_context::noNote)
Context.body_started_ = true;

if (next_masterPageName)
{
if (true == Context.set_master_page_name(*next_masterPageName))
Expand Down Expand Up @@ -802,6 +806,7 @@ void section::add_child_element( xml::sax * Reader, const std::wstring & Ns, con
void section::docx_convert(oox::docx_conversion_context & Context)
{
bool bAddSection = false;
const bool bHidden = section_attr_.display_ && section_attr_.display_->get_type() == odf_types::text_display::None;
if ( false == Context.get_drawing_state_content())
{
std::wstring current_page_properties = Context.get_page_properties();
Expand Down Expand Up @@ -860,6 +865,8 @@ void section::docx_convert(oox::docx_conversion_context & Context)
{
Context.set_implicit_end(true);
}
if (!bHidden && bAddSection)
break_before_master_page(Context, content_[i]);
content_[i]->docx_convert(Context);
}
if (bAddSection)
Expand Down Expand Up @@ -918,6 +925,38 @@ void index_body::add_child_element( xml::sax * Reader, const std::wstring & Ns,
CP_CREATE_ELEMENT(content_);
}

void break_before_master_page(oox::docx_conversion_context & Context, const office_element_ptr & elm)
{
// style:master-page-name always starts a new page, also when the page style stays the same.
// It is ignored inside tables.
if (!elm || !elm->element_style_name || !Context.body_started_ || Context.process_headers_footers_
|| Context.get_table_context().in_table())
return;

const bool is_table = elm->get_type() == typeTableTable;
const odf_types::style_family::type family = is_table ?
odf_types::style_family::Table : odf_types::style_family::Paragraph;

odf_reader::odf_read_context & odf_context = Context.root()->odf_context();
_CP_OPT(std::wstring) master_page =
odf_context.styleContainer().master_page_name_inherited(*elm->element_style_name, family);

// Like LibreOffice, ignore the name of a master page that does not exist
if (!master_page || !odf_context.pageLayoutContainer().master_page_by_name(*master_page))
return;

if (!is_table)
{
// A paragraph style that breaks the page already writes w:pageBreakBefore; a second break adds a page
const style_instance * style =
odf_context.styleContainer().style_by_name(*elm->element_style_name, family, false);
const paragraph_format_properties properties = calc_paragraph_properties_content(style);
if (properties.fo_break_before_ && properties.fo_break_before_->get_type() == odf_types::fo_break::Page)
return;
}
Context.set_page_break_before(2);
}

void index_body::docx_convert(oox::docx_conversion_context & Context)
{
if (index_title_)
Expand All @@ -928,6 +967,7 @@ void index_body::docx_convert(oox::docx_conversion_context & Context)
for (size_t i = 0; i < content_.size(); i++)
{
Context.start_index_element();
break_before_master_page(Context, content_[i]);
content_[i]->docx_convert(Context);
Context.end_index_element();
}
Expand All @@ -949,6 +989,7 @@ void index_title::docx_convert(oox::docx_conversion_context & Context)
{
for (size_t i = 0; i < content_.size(); i++)
{
break_before_master_page(Context, content_[i]);
content_[i]->docx_convert(Context);
}
}
Expand Down
3 changes: 3 additions & 0 deletions OdfFile/Reader/Format/text_elements.h
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,9 @@ class index_body : public office_element_impl<index_body>
virtual void add_child_element( xml::sax * Reader, const std::wstring & Ns, const std::wstring & Name);
};
CP_REGISTER_OFFICE_ELEMENT2(index_body);

// Sets a page break before a body element whose style names a master page (not before the first one).
void break_before_master_page(oox::docx_conversion_context & Context, const office_element_ptr & elm);
//---------------------------------------------------------------------------------------------------
class index_title : public office_element_impl<index_title>
{
Expand Down
1 change: 1 addition & 0 deletions OdfFile/Test/test_odf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ add_core_gtest(
pptx2odp_bullet_size.cpp
chart_vary_colors.cpp
odt2docx_first_body_element.cpp
odt2docx_master_page_break.cpp
odt2docx_meta_template.cpp
chart_subtitle.cpp
math_property_values.cpp
Expand Down
Binary file not shown.
145 changes: 145 additions & 0 deletions OdfFile/Test/test_odf/odt2docx_master_page_break.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// SPDX-FileCopyrightText: 2026 Euro-Office contributors
// SPDX-License-Identifier: AGPL-3.0-only

// ODT -> DOCX: a paragraph whose style names a master page starts a new page.

#include "gtest/gtest.h"

#include "common.h"

#include <string>

namespace
{
// The w:p element that contains the given text, or an empty string.
std::wstring paragraph_with_text(const std::wstring& xml, const std::wstring& text)
{
const size_t pos = xml.find(text);
if (pos == std::wstring::npos)
return L"";

size_t start = xml.rfind(L"<w:p>", pos);
const size_t start_attr = xml.rfind(L"<w:p ", pos);
if (start == std::wstring::npos || (start_attr != std::wstring::npos && start_attr > start))
start = start_attr;
if (start == std::wstring::npos)
return L"";

return xml.substr(start, pos - start);
}

bool has_page_break(const std::wstring& paragraph)
{
return paragraph.find(L"<w:pageBreakBefore w:val=\"true\"/>") != std::wstring::npos ||
paragraph.find(L"<w:br w:type=\"page\"/>") != std::wstring::npos;
}

// The w:style element with the given w:name, or an empty string.
std::wstring style_with_name(const std::wstring& xml, const std::wstring& name)
{
const size_t pos = xml.find(L"<w:name w:val=\"" + name + L"\"/>");
if (pos == std::wstring::npos)
return L"";

const size_t start = xml.rfind(L"<w:style ", pos);
const size_t end = xml.find(L"</w:style>", pos);
if (start == std::wstring::npos || end == std::wstring::npos)
return L"";

return xml.substr(start, end - start);
}
} // namespace

// style:master-page-name starts a new page even when the master page stays the same,
// also when inherited from a parent style or inside an index, but not on the first paragraph
// of the body, not inside a hidden section and not when an empty value overrides the parent.
TEST(OdtToDocxMasterPageBreak, MasterPageNameStartsNewPage)
{
const std::wstring dir = convert_odf_to_ooxml_dir(L"ExampleFiles/master_page_break.odt");
ASSERT_FALSE(dir.empty()) << "ODT -> DOCX conversion failed";

const std::wstring xml = read_part(dir, L"word/document.xml");
ASSERT_FALSE(xml.empty()) << "word/document.xml is missing";
EXPECT_TRUE(is_well_formed(xml));

const std::wstring first = paragraph_with_text(xml, L"First paragraph");
ASSERT_FALSE(first.empty());
EXPECT_FALSE(has_page_break(first));

const std::wstring plain = paragraph_with_text(xml, L"Plain paragraph");
ASSERT_FALSE(plain.empty());
EXPECT_FALSE(has_page_break(plain));

const std::wstring same = paragraph_with_text(xml, L"Same page style paragraph");
ASSERT_FALSE(same.empty());
EXPECT_TRUE(has_page_break(same));

const std::wstring hidden = paragraph_with_text(xml, L"Hidden paragraph");
if (!hidden.empty())
EXPECT_FALSE(has_page_break(hidden));

// the master page comes from the parent style
const std::wstring inherited = paragraph_with_text(xml, L"Inherited paragraph");
ASSERT_FALSE(inherited.empty());
EXPECT_TRUE(has_page_break(inherited));

// an empty style:master-page-name overrides the parent style: no new page
const std::wstring empty_master = paragraph_with_text(xml, L"Empty master paragraph");
ASSERT_FALSE(empty_master.empty());
EXPECT_FALSE(has_page_break(empty_master));

// a paragraph inside a table of contents
const std::wstring index = paragraph_with_text(xml, L"Index paragraph");
ASSERT_FALSE(index.empty());
EXPECT_TRUE(has_page_break(index));

const std::wstring last = paragraph_with_text(xml, L"Last paragraph");
ASSERT_FALSE(last.empty());
EXPECT_FALSE(has_page_break(last));
}

// The page break is written once, never for a master page that does not exist, not cancelled by
// fo:break-before="auto" on a table, and not inside a table.
TEST(OdtToDocxMasterPageBreak, MasterPageBreakOnceAndOnlyOnPages)
{
const std::wstring dir = convert_odf_to_ooxml_dir(L"ExampleFiles/master_page_break.odt");
ASSERT_FALSE(dir.empty()) << "ODT -> DOCX conversion failed";

const std::wstring xml = read_part(dir, L"word/document.xml");
ASSERT_FALSE(xml.empty()) << "word/document.xml is missing";
const std::wstring styles = read_part(dir, L"word/styles.xml");
ASSERT_FALSE(styles.empty()) << "word/styles.xml is missing";

// the paragraph style breaks the page itself: no second break in the paragraph
const std::wstring break_style = style_with_name(styles, L"ChapterBreak");
ASSERT_FALSE(break_style.empty());
EXPECT_NE(break_style.find(L"<w:pageBreakBefore w:val=\"true\"/>"), std::wstring::npos);

const std::wstring break_paragraph = paragraph_with_text(xml, L"Break style paragraph");
ASSERT_FALSE(break_paragraph.empty());
EXPECT_FALSE(has_page_break(break_paragraph));

// the same for an automatic style that inherits both from that style
const std::wstring break_child = paragraph_with_text(xml, L"Break child paragraph");
ASSERT_FALSE(break_child.empty());
EXPECT_FALSE(has_page_break(break_child));

// a master page that does not exist is ignored
const std::wstring missing = paragraph_with_text(xml, L"Missing master paragraph");
ASSERT_FALSE(missing.empty());
EXPECT_FALSE(has_page_break(missing));

// a table that names a master page starts a new page also with fo:break-before="auto"
const std::wstring table = paragraph_with_text(xml, L"Table with master");
ASSERT_FALSE(table.empty());
EXPECT_TRUE(has_page_break(table));

// a master page name inside a table cell does not break the page
const std::wstring cell = paragraph_with_text(xml, L"Cell section paragraph");
ASSERT_FALSE(cell.empty());
EXPECT_FALSE(has_page_break(cell));

const std::wstring last = paragraph_with_text(xml, L"Last paragraph");
ASSERT_FALSE(last.empty());
EXPECT_FALSE(has_page_break(last));
}
1 change: 1 addition & 0 deletions REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ SPDX-License-Identifier = "AGPL-3.0-only"
path = [
"OdfFile/Test/test_odf/ExampleFiles/numbered_paragraph_first.odt",
"OdfFile/Test/test_odf/ExampleFiles/page_sequence_first.odt",
"OdfFile/Test/test_odf/ExampleFiles/master_page_break.odt",
]
SPDX-FileCopyrightText = "2026 Euro-Office contributors"
SPDX-License-Identifier = "AGPL-3.0-only"
Expand Down
Loading