Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 27 additions & 1 deletion OdfFile/Reader/Converter/docx_conversion_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,19 @@ 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 outside tables. 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.
bool take_page_break_before(docx_conversion_context & Context, bool in_drawing)
{
if (Context.get_page_break_before() != 2 || in_drawing || Context.get_table_context().in_table()
|| 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 @@ -1769,7 +1782,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 @@ -1785,6 +1801,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 @@ -2306,6 +2329,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
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.
Binary file not shown.
Loading
Loading