Debian Codegraph @

src/lib/WPSPosition.h

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
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) 2009, 2011 Alonso Laurent ([email protected])
11 * Copyright (C) 2006, 2007 Andrew Ziem
12 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
13 * Copyright (C) 2004 Marc Maurer ([email protected])
14 * Copyright (C) 2003-2005 William Lachance ([email protected])
15 *
16 * For minor contributions see the git repository.
17 *
18 * Alternatively, the contents of this file may be used under the terms
19 * of the GNU Lesser General Public License Version 2.1 or later
20 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
21 * applicable instead of those above.
22 *
23 * For further information visit http://libwps.sourceforge.net
24 */
25
26 #ifndef WPS_POSITION_H
27 #define WPS_POSITION_H
28
29 #include <ostream>
30
31 #include <librevenge/librevenge.h>
32
33 #include "libwps_internal.h"
34
35 /** Class to define the position of an object (textbox, picture, ..) in the document
36 *
37 * Stores the page, object position, object size, anchor, wrapping, ...
38 */
39 class WPSPosition
40 {
41 public:
42 //! a list of enum used to defined the anchor
43 enum AnchorTo { Cell, Char, CharBaseLine, Paragraph, ParagraphContent, Page, PageContent };
44 //! an enum used to define the wrapping
45 enum Wrapping { WNone, WDynamic, WRunThrough }; // Add something for background ?
46 //! an enum used to define the relative X position
47 enum XPos { XRight, XLeft, XCenter, XFull };
48 //! an enum used to define the relative Y position
49 enum YPos { YTop, YBottom, YCenter, YFull };
50
51 public:
52 //! constructor
53 explicit WPSPosition(Vec2f const &orig=Vec2f(), Vec2f const &sz=Vec2f(), librevenge::RVNGUnit unt=librevenge::RVNG_INCH)
54 : m_anchorTo(Char)
55 , m_anchorCellName()
56 , m_xPos(XLeft)
57 , m_yPos(YTop)
58 , m_wrapping(WNone)
59 , m_page(0)
60 , m_orig(orig)
61 , m_size(sz)
62 , m_naturalSize()
63 , m_unit(unt)
64 , m_order(0) {}
65 WPSPosition(WPSPosition const &)=default;
66 WPSPosition(WPSPosition &&)=default;
67 WPSPosition &operator=(WPSPosition const &)=default;
68 WPSPosition &operator=(WPSPosition &&)=default;
69 //! destructor
70 ~WPSPosition() {}
71 //! operator<<
72 friend std::ostream &operator<<(std::ostream &o, WPSPosition const &pos)
73 {
74 Vec2f dest(pos.m_orig+pos.m_size);
75 o << "Pos=" << pos.m_orig << "x" << dest;
76 switch (pos.m_unit)
77 {
78 case librevenge::RVNG_INCH:
79 o << "(inch)";
80 break;
81 case librevenge::RVNG_POINT:
82 o << "(pt)";
83 break;
84 case librevenge::RVNG_TWIP:
85 o << "(tw)";
86 break;
87 case librevenge::RVNG_PERCENT:
88 case librevenge::RVNG_GENERIC:
89 case librevenge::RVNG_UNIT_ERROR:
90 default:
91 break;
92 }
93 if (pos.page()>0) o << ", page=" << pos.page();
94 return o;
95 }
96 //! basic operator==
97 bool operator==(WPSPosition const &f) const
98 {
99 return cmp(f) == 0;
100 }
101 //! basic operator!=
102 bool operator!=(WPSPosition const &f) const
103 {
104 return cmp(f) != 0;
105 }
106 //! basic operator<
107 bool operator<(WPSPosition const &f) const
108 {
109 return cmp(f) < 0;
110 }
111
112 //! returns the frame page
113 int page() const
114 {
115 return m_page;
116 }
117 //! return the frame origin
118 Vec2f const &origin() const
119 {
120 return m_orig;
121 }
122 //! returns the frame size
123 Vec2f const &size() const
124 {
125 return m_size;
126 }
127 //! returns the natural size (if known)
128 Vec2f const &naturalSize() const
129 {
130 return m_naturalSize;
131 }
132 //! returns the unit
133 librevenge::RVNGUnit unit() const
134 {
135 return m_unit;
136 }
137 //! returns a float which can be used to convert between to unit
138 static float getScaleFactor(librevenge::RVNGUnit orig, librevenge::RVNGUnit dest)
139 {
140 float actSc = 1.0, newSc = 1.0;
141 switch (orig)
142 {
143 case librevenge::RVNG_TWIP:
144 break;
145 case librevenge::RVNG_POINT:
146 actSc=20;
147 break;
148 case librevenge::RVNG_INCH:
149 actSc = 1440;
150 break;
151 case librevenge::RVNG_PERCENT:
152 case librevenge::RVNG_GENERIC:
153 case librevenge::RVNG_UNIT_ERROR:
154 default:
155 WPS_DEBUG_MSG(("WPSPosition::getScaleFactor %d unit must not appear\n", int(orig)));
156 }
157 switch (dest)
158 {
159 case librevenge::RVNG_TWIP:
160 break;
161 case librevenge::RVNG_POINT:
162 newSc=20;
163 break;
164 case librevenge::RVNG_INCH:
165 newSc = 1440;
166 break;
167 case librevenge::RVNG_PERCENT:
168 case librevenge::RVNG_GENERIC:
169 case librevenge::RVNG_UNIT_ERROR:
170 default:
171 WPS_DEBUG_MSG(("WPSPosition::getScaleFactor %d unit must not appear\n", int(dest)));
172 }
173 return actSc/newSc;
174 }
175 //! returns a float which can be used to scale some data in object unit
176 float getInvUnitScale(librevenge::RVNGUnit unt) const
177 {
178 return getScaleFactor(unt, m_unit);
179 }
180
181 //! sets the page
182 void setPage(int pg) const
183 {
184 const_cast<WPSPosition *>(this)->m_page = pg;
185 }
186 //! sets the frame origin
187 void setOrigin(Vec2f const &orig)
188 {
189 m_orig = orig;
190 }
191 //! sets the frame size
192 void setSize(Vec2f const &sz)
193 {
194 m_size = sz;
195 }
196 //! sets the natural size (if known)
197 void setNaturalSize(Vec2f const &natSize)
198 {
199 m_naturalSize = natSize;
200 }
201 //! sets the dimension unit
202 void setUnit(librevenge::RVNGUnit unt)
203 {
204 m_unit = unt;
205 }
206 //! sets/resets the page and the origin
207 void setPagePos(int pg, Vec2f const &newOrig) const
208 {
209 const_cast<WPSPosition *>(this)->m_page = pg;
210 const_cast<WPSPosition *>(this)->m_orig = newOrig;
211 }
212
213 //! sets the relative position
214 void setRelativePosition(AnchorTo anchor, XPos X = XLeft, YPos Y=YTop)
215 {
216 m_anchorTo = anchor;
217 m_xPos = X;
218 m_yPos = Y;
219 }
220
221 //! sets the anchor to a cell position
222 void setAnchorToCell(librevenge::RVNGString const &cellName)
223 {
224 m_anchorTo = Cell;
225 m_xPos = XLeft;
226 m_yPos = YTop;
227 m_anchorCellName=cellName;
228 }
229 //! returns background/foward order
230 int order() const
231 {
232 return m_order;
233 }
234 //! set background/foward order
235 void setOrder(int ord) const
236 {
237 m_order = ord;
238 }
239
240 //! anchor position
241 AnchorTo m_anchorTo;
242 //! the anchor cell name
243 librevenge::RVNGString m_anchorCellName;
244 //! X relative position
245 XPos m_xPos;
246 //! Y relative position
247 YPos m_yPos;
248 //! Wrapping
249 Wrapping m_wrapping;
250
251 protected:
252 //! basic function to compare two positions
253 int cmp(WPSPosition const &f) const
254 {
255 int diff = int(m_anchorTo) - int(f.m_anchorTo);
256 if (diff) return diff < 0 ? -1 : 1;
257 if (m_anchorCellName<f.m_anchorCellName) return -1;
258 if (m_anchorCellName>f.m_anchorCellName) return 1;
259 diff = int(m_xPos) - int(f.m_xPos);
260 if (diff) return diff < 0 ? -1 : 1;
261 diff = int(m_yPos) - int(f.m_yPos);
262 if (diff) return diff < 0 ? -1 : 1;
263 diff = page() - f.page();
264 if (diff) return diff < 0 ? -1 : 1;
265 diff = int(m_unit) - int(f.m_unit);
266 if (diff) return diff < 0 ? -1 : 1;
267 diff = m_orig.cmpY(f.m_orig);
268 if (diff) return diff;
269 diff = m_size.cmpY(f.m_size);
270 if (diff) return diff;
271 diff = m_naturalSize.cmpY(f.m_naturalSize);
272 if (diff) return diff;
273
274 return 0;
275 }
276
277 //! the page
278 int m_page;
279 Vec2f m_orig /** the origin position in a page */, m_size /* the size of the data*/, m_naturalSize /** the natural size of the data (if known) */;
280 //! the unit used in \a orig and in \a m_size. Default: in inches
281 librevenge::RVNGUnit m_unit;
282 //! background/foward order
283 mutable int m_order;
284 };
285
286 #endif
287 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */