1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libodfgen
3  * Version: MPL 2.0 / LGPLv2.1+
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * Major Contributor(s):
10  * Copyright (C) 2002-2004 William Lachance (wrlach@gmail.com)
11  *
12  * For minor contributions see the git repository.
13  *
14  * Alternatively, the contents of this file may be used under the terms
15  * of the GNU Lesser General Public License Version 2.1 or later
16  * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
17  * applicable instead of those above.
18  *
19  * For further information visit http://libwpd.sourceforge.net
20  */
21 
22 /* "This product is not manufactured, approved, or supported by
23  * Corel Corporation or Corel Corporation Limited."
24  */
25 #ifndef _PAGESPAN_HXX_
26 #define _PAGESPAN_HXX_
27 
28 #include <librevenge/librevenge.h>
29 #include <map>
30 #include <memory>
31 #include <set>
32 #include <vector>
33 
34 #include "FilterInternal.hxx"
35 
36 #include "Style.hxx"
37 
38 class DocumentElement;
39 class OdfDocumentHandler;
40 
41 //! the page drawing style
42 class PageDrawingStyle : public Style
43 {
44 public:
45 	PageDrawingStyle(librevenge::RVNGPropertyList const &propList, const librevenge::RVNGString &sName, Style::Zone zone);
46 	~PageDrawingStyle() override;
47 	void write(OdfDocumentHandler *pHandler) const override;
48 
49 private:
50 	librevenge::RVNGPropertyList mpPropList;
51 };
52 
53 //! the page layout style
54 class PageLayoutStyle : public Style
55 {
56 public:
57 	PageLayoutStyle(librevenge::RVNGPropertyList const &propList, const librevenge::RVNGString &sName, Style::Zone zone);
58 	~PageLayoutStyle() override;
59 	void write(OdfDocumentHandler *pHandler) const override;
60 	/** reset the page size (used by Od[pg]Generator to reset the size to union size)
61 		and the margins to 0
62 		\note size are given in inch
63 	 */
64 	void resetPageSizeAndMargins(double width, double height);
65 
66 private:
67 	librevenge::RVNGPropertyList mpPropList;
68 };
69 
70 class PageSpan
71 {
72 protected:
73 	enum ContentType : int
74 	{
75 		C_Header=0, C_HeaderFirst, C_HeaderLeft, C_HeaderLast,
76 		C_Footer, C_FooterFirst, C_FooterLeft, C_FooterLast,
77 		C_Master,
78 		C_NumContentTypes // keep this one last
79 	};
80 public:
81 	PageSpan(librevenge::RVNGString const &masterName, librevenge::RVNGString const &masterDisplay="", bool isMasterPage=false);
82 	virtual ~PageSpan();
83 	void writeMasterPages(OdfDocumentHandler *pHandler) const;
84 	//! returns true if this is a master page
isMasterPage() const85 	bool isMasterPage() const
86 	{
87 		return mbIsMasterPage;
88 	}
89 	//! returns the display name of the span's master page
getDisplayMasterName() const90 	librevenge::RVNGString getDisplayMasterName() const
91 	{
92 		return msMasterDisplay;
93 	}
94 	//! returns the name of the span's master page
getMasterName() const95 	librevenge::RVNGString getMasterName() const
96 	{
97 		return msMasterName;
98 	}
99 	//! set the layout name
setLayoutName(librevenge::RVNGString const & name)100 	void setLayoutName(librevenge::RVNGString const &name)
101 	{
102 		msLayoutName=name;
103 	}
104 	//! returns the name of the span's layout page
getLayoutName() const105 	librevenge::RVNGString getLayoutName() const
106 	{
107 		return msLayoutName;
108 	}
109 	//! set the display page drawing name
setDrawingName(librevenge::RVNGString const & name)110 	void setDrawingName(librevenge::RVNGString const &name)
111 	{
112 		msDrawingName=name;
113 	}
114 	//! returns the name of the span's page drawing
getDrawingName() const115 	librevenge::RVNGString getDrawingName() const
116 	{
117 		return msDrawingName;
118 	}
setHeaderContent(const std::shared_ptr<libodfgen::DocumentElementVector> & pContent)119 	void setHeaderContent(const std::shared_ptr<libodfgen::DocumentElementVector> &pContent)
120 	{
121 		storeContent(C_Header, pContent);
122 	}
setFooterContent(const std::shared_ptr<libodfgen::DocumentElementVector> & pContent)123 	void setFooterContent(const std::shared_ptr<libodfgen::DocumentElementVector> &pContent)
124 	{
125 		storeContent(C_Footer, pContent);
126 	}
setHeaderLeftContent(const std::shared_ptr<libodfgen::DocumentElementVector> & pContent)127 	void setHeaderLeftContent(const std::shared_ptr<libodfgen::DocumentElementVector> &pContent)
128 	{
129 		storeContent(C_HeaderLeft, pContent);
130 	}
setFooterLeftContent(const std::shared_ptr<libodfgen::DocumentElementVector> & pContent)131 	void setFooterLeftContent(const std::shared_ptr<libodfgen::DocumentElementVector> &pContent)
132 	{
133 		storeContent(C_FooterLeft, pContent);
134 	}
setHeaderFirstContent(const std::shared_ptr<libodfgen::DocumentElementVector> & pContent)135 	void setHeaderFirstContent(const std::shared_ptr<libodfgen::DocumentElementVector> &pContent)
136 	{
137 		storeContent(C_HeaderFirst, pContent);
138 	}
setFooterFirstContent(const std::shared_ptr<libodfgen::DocumentElementVector> & pContent)139 	void setFooterFirstContent(const std::shared_ptr<libodfgen::DocumentElementVector> &pContent)
140 	{
141 		storeContent(C_FooterFirst, pContent);
142 	}
setHeaderLastContent(const std::shared_ptr<libodfgen::DocumentElementVector> & pContent)143 	void setHeaderLastContent(const std::shared_ptr<libodfgen::DocumentElementVector> &pContent)
144 	{
145 		storeContent(C_HeaderLast, pContent);
146 	}
setFooterLastContent(const std::shared_ptr<libodfgen::DocumentElementVector> & pContent)147 	void setFooterLastContent(const std::shared_ptr<libodfgen::DocumentElementVector> &pContent)
148 	{
149 		storeContent(C_FooterLast, pContent);
150 	}
setMasterContent(const std::shared_ptr<libodfgen::DocumentElementVector> & pContent)151 	void setMasterContent(const std::shared_ptr<libodfgen::DocumentElementVector> &pContent)
152 	{
153 		storeContent(C_Master, pContent);
154 	}
155 
156 	/** small function which mainly replaced space by underscore */
157 	static librevenge::RVNGString protectString(librevenge::RVNGString const &orig);
158 protected:
159 	void storeContent(ContentType type, const std::shared_ptr<libodfgen::DocumentElementVector> &pContent);
160 	static void _writeContent(const char *contentTagName, const libodfgen::DocumentElementVector &content,
161 	                          OdfDocumentHandler *pHandler);
162 private:
163 	PageSpan(const PageSpan &);
164 	PageSpan &operator=(const PageSpan &);
165 	//! flag to know if this is a master page
166 	bool mbIsMasterPage;
167 	//! the page master name
168 	librevenge::RVNGString msMasterName;
169 	//! the page master display name
170 	librevenge::RVNGString msMasterDisplay;
171 	//! the layout display name
172 	librevenge::RVNGString msLayoutName;
173 	//! the page drawing display name
174 	librevenge::RVNGString msDrawingName;
175 	std::shared_ptr<libodfgen::DocumentElementVector> mpContent[C_NumContentTypes];
176 };
177 
178 //! class used to store the list of created page
179 class PageSpanManager
180 {
181 public:
182 	//! constructor
PageSpanManager()183 	PageSpanManager() : mpPageList(), mpNameToMasterMap(),
184 		mpLayoutList(), mpNameToLayoutMap(), mHashLayoutMap(),
185 		mpDrawingList(), mpNameToDrawingMap(), mHashDrawingMap()
186 	{
187 	}
188 	//! destructor
~PageSpanManager()189 	~PageSpanManager()
190 	{
191 		clean();
192 	}
193 	//! clean data
194 	void clean();
195 	//! create a new page and set it to current. Returns a pointer to this new page
196 	PageSpan *add(const librevenge::RVNGPropertyList &xPropList, bool masterPage=false);
197 	//! return the page span which correspond to a master name
198 	PageSpan *get(librevenge::RVNGString const &masterName);
199 	//! write the pages' layouts (style automatic) or the pages' drawing styles(content automatic)
200 	void writePageStyles(OdfDocumentHandler *pHandler, Style::Zone zone) const;
201 	void writeMasterPages(OdfDocumentHandler *pHandler) const;
202 
203 	/** reset all layout page sizes given in inches	and the margins to 0
204 
205 		\note this function is used by Od[pg]Generator to reset the
206 		size to union size, so it only reset size if there are several
207 		page layouts, ie. at least 2.
208 	 */
209 	void resetPageSizeAndMargins(double width, double height);
210 
211 protected:
212 	/* create a new drawing style if it does not exists and returns the final name for the drawing properties.
213 
214 	   \note If the property list does not contain any drawing style, returns the empty string*/
215 	librevenge::RVNGString findOrAddDrawing(const librevenge::RVNGPropertyList &xPropList, bool masterPage);
216 
217 	/* create a new layout style if it does not exists and returns the final name for the layout properties. */
218 	librevenge::RVNGString findOrAddLayout(const librevenge::RVNGPropertyList &xPropList);
219 
220 	//! the list of page
221 	std::vector<std::shared_ptr<PageSpan> > mpPageList;
222 	//! a map master page name to pagespan
223 	std::map<librevenge::RVNGString, std::shared_ptr<PageSpan> > mpNameToMasterMap;
224 
225 	//! the list of layout style
226 	std::vector<std::shared_ptr<PageLayoutStyle> > mpLayoutList;
227 	//! a map named layout to page layout style
228 	std::map<librevenge::RVNGString, std::shared_ptr<PageLayoutStyle> > mpNameToLayoutMap;
229 	// hash key -> layout style
230 	std::map<librevenge::RVNGString, librevenge::RVNGString> mHashLayoutMap;
231 	//! the list of drawing style
232 	std::vector<std::shared_ptr<PageDrawingStyle> > mpDrawingList;
233 	//! a map named drawing to page drawing style
234 	std::map<librevenge::RVNGString, std::shared_ptr<PageDrawingStyle> > mpNameToDrawingMap;
235 	// hash key -> drawing style
236 	std::map<librevenge::RVNGString, librevenge::RVNGString> mHashDrawingMap;
237 };
238 #endif
239 
240 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */
241