WPSPosition.h Source File documentation

WPSPosition.h
Go to the documentation of this file.
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 (alonso@loria.fr)
11 * Copyright (C) 2006, 2007 Andrew Ziem
12 * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
13 * Copyright (C) 2004 Marc Maurer (uwog@uwog.net)
14 * Copyright (C) 2003-2005 William Lachance (william.lachance@sympatico.ca)
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
39 class WPSPosition
40 {
41 public:
43 enum AnchorTo { Cell, Char, CharBaseLine, Paragraph, ParagraphContent, Page, PageContent };
45 enum Wrapping { WNone, WDynamic, WRunThrough }; // Add something for background ?
47 enum XPos { XRight, XLeft, XCenter, XFull };
49 enum YPos { YTop, YBottom, YCenter, YFull };
50
51 public:
53 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) {}
66 ~WPSPosition() {}
68 friend std::ostream &operator<<(std::ostream &o, WPSPosition const &pos)
69 {
70 Vec2f dest(pos.m_orig+pos.m_size);
71 o << "Pos=" << pos.m_orig << "x" << dest;
72 switch (pos.m_unit)
73 {
74 case librevenge::RVNG_INCH:
75 o << "(inch)";
76 break;
77 case librevenge::RVNG_POINT:
78 o << "(pt)";
79 break;
80 case librevenge::RVNG_TWIP:
81 o << "(tw)";
82 break;
83 case librevenge::RVNG_PERCENT:
84 case librevenge::RVNG_GENERIC:
85 case librevenge::RVNG_UNIT_ERROR:
86 default:
87 break;
88 }
89 if (pos.page()>0) o << ", page=" << pos.page();
90 return o;
91 }
93 bool operator==(WPSPosition const &f) const
94 {
95 return cmp(f) == 0;
96 }
98 bool operator!=(WPSPosition const &f) const
99 {
100 return cmp(f) != 0;
101 }
103 bool operator<(WPSPosition const &f) const
104 {
105 return cmp(f) < 0;
106 }
107
109 int page() const
110 {
111 return m_page;
112 }
114 Vec2f const &origin() const
115 {
116 return m_orig;
117 }
119 Vec2f const &size() const
120 {
121 return m_size;
122 }
124 Vec2f const &naturalSize() const
125 {
126 return m_naturalSize;
127 }
129 librevenge::RVNGUnit unit() const
130 {
131 return m_unit;
132 }
134 static float getScaleFactor(librevenge::RVNGUnit orig, librevenge::RVNGUnit dest)
135 {
136 float actSc = 1.0, newSc = 1.0;
137 switch (orig)
138 {
139 case librevenge::RVNG_TWIP:
140 break;
141 case librevenge::RVNG_POINT:
142 actSc=20;
143 break;
144 case librevenge::RVNG_INCH:
145 actSc = 1440;
146 break;
147 case librevenge::RVNG_PERCENT:
148 case librevenge::RVNG_GENERIC:
149 case librevenge::RVNG_UNIT_ERROR:
150 default:
151 WPS_DEBUG_MSG(("WPSPosition::getScaleFactor %d unit must not appear\n", int(orig)));
152 }
153 switch (dest)
154 {
155 case librevenge::RVNG_TWIP:
156 break;
157 case librevenge::RVNG_POINT:
158 newSc=20;
159 break;
160 case librevenge::RVNG_INCH:
161 newSc = 1440;
162 break;
163 case librevenge::RVNG_PERCENT:
164 case librevenge::RVNG_GENERIC:
165 case librevenge::RVNG_UNIT_ERROR:
166 default:
167 WPS_DEBUG_MSG(("WPSPosition::getScaleFactor %d unit must not appear\n", int(dest)));
168 }
169 return actSc/newSc;
170 }
172 float getInvUnitScale(librevenge::RVNGUnit unt) const
173 {
174 return getScaleFactor(unt, m_unit);
175 }
176
178 void setPage(int pg) const
179 {
180 const_cast<WPSPosition *>(this)->m_page = pg;
181 }
183 void setOrigin(Vec2f const &orig)
184 {
185 m_orig = orig;
186 }
188 void setSize(Vec2f const &sz)
189 {
190 m_size = sz;
191 }
193 void setNaturalSize(Vec2f const &natSize)
194 {
195 m_naturalSize = natSize;
196 }
198 void setUnit(librevenge::RVNGUnit unt)
199 {
200 m_unit = unt;
201 }
203 void setPagePos(int pg, Vec2f const &newOrig) const
204 {
205 const_cast<WPSPosition *>(this)->m_page = pg;
206 const_cast<WPSPosition *>(this)->m_orig = newOrig;
207 }
208
210 void setRelativePosition(AnchorTo anchor, XPos X = XLeft, YPos Y=YTop)
211 {
212 m_anchorTo = anchor;
213 m_xPos = X;
214 m_yPos = Y;
215 }
216
218 void setAnchorToCell(librevenge::RVNGString const &cellName)
219 {
220 m_anchorTo = Cell;
221 m_xPos = XLeft;
222 m_yPos = YTop;
223 m_anchorCellName=cellName;
224 }
226 int order() const
227 {
228 return m_order;
229 }
231 void setOrder(int ord) const
232 {
233 m_order = ord;
234 }
235
237 AnchorTo m_anchorTo;
239 librevenge::RVNGString m_anchorCellName;
241 XPos m_xPos;
243 YPos m_yPos;
245 Wrapping m_wrapping;
246
247 protected:
249 int cmp(WPSPosition const &f) const
250 {
251 int diff = int(m_anchorTo) - int(f.m_anchorTo);
252 if (diff) return diff < 0 ? -1 : 1;
253 if (m_anchorCellName<f.m_anchorCellName) return -1;
254 if (m_anchorCellName>f.m_anchorCellName) return 1;
255 diff = int(m_xPos) - int(f.m_xPos);
256 if (diff) return diff < 0 ? -1 : 1;
257 diff = int(m_yPos) - int(f.m_yPos);
258 if (diff) return diff < 0 ? -1 : 1;
259 diff = page() - f.page();
260 if (diff) return diff < 0 ? -1 : 1;
261 diff = int(m_unit) - int(f.m_unit);
262 if (diff) return diff < 0 ? -1 : 1;
263 diff = m_orig.cmpY(f.m_orig);
264 if (diff) return diff;
265 diff = m_size.cmpY(f.m_size);
266 if (diff) return diff;
267 diff = m_naturalSize.cmpY(f.m_naturalSize);
268 if (diff) return diff;
269
270 return 0;
271 }
272
274 int m_page;
275 Vec2f m_orig , m_size /* the size of the data*/, m_naturalSize ;
277 librevenge::RVNGUnit m_unit;
279 mutable int m_order;
280 };
281
282 #endif
283 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */
WPSPosition::m_naturalSize
Vec2f m_naturalSize
the natural size of the data (if known)
Definition: WPSPosition.h:275
Vec2::cmpY
int cmpY(Vec2< T > const &p) const
a comparison function: which first compares y then x
Definition: libwps_internal.h:656
WPSPosition::m_anchorCellName
librevenge::RVNGString m_anchorCellName
the anchor cell name
Definition: WPSPosition.h:239
WPSPosition::YPos
YPos
an enum used to define the relative Y position
Definition: WPSPosition.h:49
Vec2f
Vec2< float > Vec2f
Vec2 of float.
Definition: libwps_internal.h:704
WPSPosition::operator==
bool operator==(WPSPosition const &f) const
basic operator==
Definition: WPSPosition.h:93
WPSPosition::XRight
Definition: WPSPosition.h:47
WPSPosition::YBottom
Definition: WPSPosition.h:49
WPSPosition::Char
Definition: WPSPosition.h:43
WPSPosition::ParagraphContent
Definition: WPSPosition.h:43
WPSPosition::setRelativePosition
void setRelativePosition(AnchorTo anchor, XPos X=XLeft, YPos Y=YTop)
sets the relative position
Definition: WPSPosition.h:210
WPSPosition::setSize
void setSize(Vec2f const &sz)
sets the frame size
Definition: WPSPosition.h:188
WPSPosition::m_order
int m_order
background/foward order
Definition: WPSPosition.h:279
WPSPosition::WNone
Definition: WPSPosition.h:45
WPSPosition::operator<<
friend std::ostream & operator<<(std::ostream &o, WPSPosition const &pos)
operator<<
Definition: WPSPosition.h:68
WPSPosition::setAnchorToCell
void setAnchorToCell(librevenge::RVNGString const &cellName)
sets the anchor to a cell position
Definition: WPSPosition.h:218
WPSPosition::setPage
void setPage(int pg) const
sets the page
Definition: WPSPosition.h:178
WPSPosition::WDynamic
Definition: WPSPosition.h:45
WPSPosition::YTop
Definition: WPSPosition.h:49
WPSPosition::m_unit
librevenge::RVNGUnit m_unit
the unit used in orig and in m_size. Default: in inches
Definition: WPSPosition.h:277
WPSPosition::page
int page() const
returns the frame page
Definition: WPSPosition.h:109
WPSPosition::setNaturalSize
void setNaturalSize(Vec2f const &natSize)
sets the natural size (if known)
Definition: WPSPosition.h:193
WPSPosition::Wrapping
Wrapping
an enum used to define the wrapping
Definition: WPSPosition.h:45
WPSPosition::XLeft
Definition: WPSPosition.h:47
WPSPosition::m_anchorTo
AnchorTo m_anchorTo
anchor position
Definition: WPSPosition.h:237
WPSPosition::WRunThrough
Definition: WPSPosition.h:45
WPSPosition::m_wrapping
Wrapping m_wrapping
Wrapping.
Definition: WPSPosition.h:245
WPSPosition::XFull
Definition: WPSPosition.h:47
WPSPosition::PageContent
Definition: WPSPosition.h:43
libwps_internal.h
WPSPosition::setPagePos
void setPagePos(int pg, Vec2f const &newOrig) const
sets/resets the page and the origin
Definition: WPSPosition.h:203
WPSPosition::YCenter
Definition: WPSPosition.h:49
WPSPosition::naturalSize
Vec2f const & naturalSize() const
returns the natural size (if known)
Definition: WPSPosition.h:124
WPSPosition::m_orig
Vec2f m_orig
the origin position in a page
Definition: WPSPosition.h:275
WPSPosition::m_size
Vec2f m_size
Definition: WPSPosition.h:275
WPSPosition::getInvUnitScale
float getInvUnitScale(librevenge::RVNGUnit unt) const
returns a float which can be used to scale some data in object unit
Definition: WPSPosition.h:172
WPSPosition::origin
Vec2f const & origin() const
return the frame origin
Definition: WPSPosition.h:114
WPSPosition::operator<
bool operator<(WPSPosition const &f) const
basic operator<
Definition: WPSPosition.h:103
WPSPosition::order
int order() const
returns background/foward order
Definition: WPSPosition.h:226
WPSPosition::m_yPos
YPos m_yPos
Y relative position.
Definition: WPSPosition.h:243
WPSPosition::Page
Definition: WPSPosition.h:43
WPSPosition::setUnit
void setUnit(librevenge::RVNGUnit unt)
sets the dimension unit
Definition: WPSPosition.h:198
WPSPosition::setOrder
void setOrder(int ord) const
set background/foward order
Definition: WPSPosition.h:231
WPS_DEBUG_MSG
#define WPS_DEBUG_MSG(M)
Definition: libwps_internal.h:134
WPSPosition::operator!=
bool operator!=(WPSPosition const &f) const
basic operator!=
Definition: WPSPosition.h:98
WPSPosition::size
Vec2f const & size() const
returns the frame size
Definition: WPSPosition.h:119
WPSPosition::XPos
XPos
an enum used to define the relative X position
Definition: WPSPosition.h:47
WPSPosition::~WPSPosition
~WPSPosition()
destructor
Definition: WPSPosition.h:66
WPSPosition::setOrigin
void setOrigin(Vec2f const &orig)
sets the frame origin
Definition: WPSPosition.h:183
WPSPosition::WPSPosition
WPSPosition(Vec2f const &orig=Vec2f(), Vec2f const &sz=Vec2f(), librevenge::RVNGUnit unt=librevenge::RVNG_INCH)
constructor
Definition: WPSPosition.h:53
WPSPosition::CharBaseLine
Definition: WPSPosition.h:43
WPSPosition::YFull
Definition: WPSPosition.h:49
WPSPosition::unit
librevenge::RVNGUnit unit() const
returns the unit
Definition: WPSPosition.h:129
WPSPosition::m_xPos
XPos m_xPos
X relative position.
Definition: WPSPosition.h:241
WPSPosition::XCenter
Definition: WPSPosition.h:47
WPSPosition::Cell
Definition: WPSPosition.h:43
Vec2< float >
WPSPosition
Class to define the position of an object (textbox, picture, ..) in the document. ...
Definition: WPSPosition.h:39
WPSPosition::getScaleFactor
static float getScaleFactor(librevenge::RVNGUnit orig, librevenge::RVNGUnit dest)
returns a float which can be used to convert between to unit
Definition: WPSPosition.h:134
WPSPosition::cmp
int cmp(WPSPosition const &f) const
basic function to compare two positions
Definition: WPSPosition.h:249
WPSPosition::m_page
int m_page
the page
Definition: WPSPosition.h:274
WPSPosition::Paragraph
Definition: WPSPosition.h:43
WPSPosition::AnchorTo
AnchorTo
a list of enum used to defined the anchor
Definition: WPSPosition.h:43