Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 40 additions & 3 deletions OdfFile/Reader/Converter/docx_conversion_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,21 @@ int twips(double points)
return (int)(points * 20 + (points < 0 ? -0.5 : 0.5));
}

// True, and the request cleared, if a page break (2) is pending for a body paragraph. A master page switch asks for
// it. Written as w:pageBreakBefore it adds no empty page when a section break has already started a new page; a page
// break run at the start of the paragraph would. Inside a table, a pending break comes from the table itself and goes
// to the first paragraph of its first cell: Word and LibreOffice start the table on a new page only for
// w:pageBreakBefore there.
bool take_page_break_before(docx_conversion_context & Context, bool in_drawing)
{
if (Context.get_page_break_before() != 2 || in_drawing || Context.process_headers_footers_
|| Context.get_process_note() != docx_conversion_context::noNote)
return false;

Context.set_page_break_before(0);
return true;
}

// Text width in points of a page layout (page width less the page margins), 0 if unknown.
// Section margins are left out: a section of its own gets them neither in its w:pgMar.
double text_width(odf_reader::page_layout_container & layouts, const std::wstring & page_layout_name)
Expand Down Expand Up @@ -1700,7 +1715,10 @@ void docx_conversion_context::serialize_paragraph_style(std::wostream & strm, co
// start_paragraph();
//}

if (!paragraph_style.str().empty() || !ParentId.empty())
// an automatic paragraph style without paragraph properties has not written the page break yet
const bool page_break_before = !in_styles && in_automatic_style_ && take_page_break_before(*this, in_drawing);

if (!paragraph_style.str().empty() || !ParentId.empty() || page_break_before)
{
CP_XML_NODE(L"w:pPr")
{
Expand All @@ -1716,6 +1734,13 @@ void docx_conversion_context::serialize_paragraph_style(std::wostream & strm, co
CP_XML_ATTR(L"w:val", ParentId);
}
}
if (page_break_before)
{
CP_XML_NODE(L"w:pageBreakBefore")
{
CP_XML_ATTR(L"w:val", L"true");
}
}
CP_XML_STREAM() << paragraph_style.str();
serialize_list_properties(CP_XML_STREAM());

Expand Down Expand Up @@ -2278,6 +2303,9 @@ int docx_conversion_context::process_paragraph_attr(odf_reader::text::paragraph_
}
output_stream() << L"<w:pStyle w:val=\"" << id << L"\" />";

if (take_page_break_before(*this, in_drawing))
output_stream() << L"<w:pageBreakBefore w:val=\"true\"/>";

if (!get_text_tracked_context().dumpPPr_.empty())
{
output_stream() << get_text_tracked_context().dumpPPr_;
Expand Down Expand Up @@ -2327,6 +2355,8 @@ int docx_conversion_context::process_paragraph_attr(odf_reader::text::paragraph_
// distance between a header/footer and the page body is all it gets.
header_footer_edge_spacing edge_spacing(*this, Attr, NULL);
const std::wstring edge_attr = header_footer_context_.edge_attr();
// or the page break of a table that starts a new page, for the first paragraph of its first cell
const bool page_break_before = take_page_break_before(*this, in_drawing);

if (!get_section_context().dump_.empty()
&& !get_table_context().in_table()
Expand All @@ -2339,12 +2369,19 @@ int docx_conversion_context::process_paragraph_attr(odf_reader::text::paragraph_
output_stream() << get_section_context().dump_;
get_section_context().dump_.clear();
//TODO find out if headers are real without styles and properties
if (page_break_before)
output_stream() << L"<w:pageBreakBefore w:val=\"true\"/>";
output_stream() << L"</w:pPr>";
return 3;
}
if (!edge_attr.empty())
if (!edge_attr.empty() || page_break_before)
{
output_stream() << L"<w:pPr><w:spacing " << edge_attr << L"/></w:pPr>";
output_stream() << L"<w:pPr>";
if (page_break_before)
output_stream() << L"<w:pageBreakBefore w:val=\"true\"/>";
if (!edge_attr.empty())
output_stream() << L"<w:spacing " << edge_attr << L"/>";
output_stream() << L"</w:pPr>";
return 3;
}

Expand Down
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 @@ -1028,6 +1028,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_;

// the endnote page of the document (office_text::docx_convert): the paragraph that ends the
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 @@ -1003,7 +1003,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 @@ -35,6 +35,7 @@

#include "serialize_elements.h"
#include "odfcontext.h"
#include "text_elements.h"
#include "style_paragraph_properties.h"
#include "styles.h"
#include "text_elements.h"
Expand Down Expand Up @@ -395,6 +396,7 @@ void office_text::docx_convert(oox::docx_conversion_context & Context)
}
}
}
text::break_before_master_page(Context, content_[i]);
content_[i]->docx_convert(Context);
}
// An empty paragraph of minimal height (the endnote page section and the save in DocxConverter
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 @@ -562,6 +562,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 @@ -921,6 +925,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 @@ -979,6 +984,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 @@ -1037,6 +1044,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 @@ -1047,6 +1086,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 @@ -1068,6 +1108,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 @@ -577,6 +577,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 @@ -92,6 +92,7 @@ add_core_gtest(
xlsx2ods_page_margins.cpp
chart_vary_colors.cpp
odt2docx_first_body_element.cpp
odt2docx_master_page_break.cpp
odt2docx_page_background_tile.cpp
odt2docx_meta_template.cpp
xlsx2ods_row_padding.cpp
Expand Down
Binary file not shown.
Binary file not shown.
Loading
Loading