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) {}
65 WPSPosition(WPSPosition const &)=default;
66 WPSPosition &operator=(WPSPosition const &)=default;
68 ~WPSPosition() {}
70 friend std::ostream &operator<<(std::ostream &o, WPSPosition const &pos)
71 {
72 Vec2f dest(pos.m_orig+pos.m_size);
73 o << "Pos=" << pos.m_orig << "x" << dest;
74 switch (pos.m_unit)
75 {
76 case librevenge::RVNG_INCH:
77 o << "(inch)";
78 break;
79 case librevenge::RVNG_POINT:
80 o << "(pt)";
81 break;
82 case librevenge::RVNG_TWIP:
83 o << "(tw)";
84 break;
85 case librevenge::RVNG_PERCENT:
86 case librevenge::RVNG_GENERIC:
87 case librevenge::RVNG_UNIT_ERROR:
88 default:
89 break;
90 }
91 if (pos.page()>0) o << ", page=" << pos.page();
92 return o;
93 }
95 bool operator==(WPSPosition const &f) const
96 {
97 return cmp(f) == 0;
98 }
100 bool operator!=(WPSPosition const &f) const
101 {
102 return cmp(f) != 0;
103 }
105 bool operator<(WPSPosition const &f) const
106 {
107 return cmp(f) < 0;
108 }
109
111 int page() const
112 {
113 return m_page;
114 }
116 Vec2f const &origin() const
117 {
118 return m_orig;
119 }
121 Vec2f const &size() const
122 {
123 return m_size;
124 }
126 Vec2f const &naturalSize() const
127 {
128 return m_naturalSize;
129 }
131 librevenge::RVNGUnit unit() const
132 {
133 return m_unit;
134 }
136 static float getScaleFactor(librevenge::RVNGUnit orig, librevenge::RVNGUnit dest)
137 {
138 float actSc = 1.0, newSc = 1.0;
139 switch (orig)
140 {
141 case librevenge::RVNG_TWIP:
142 break;
143 case librevenge::RVNG_POINT:
144 actSc=20;
145 break;
146 case librevenge::RVNG_INCH:
147 actSc = 1440;
148 break;
149 case librevenge::RVNG_PERCENT:
150 case librevenge::RVNG_GENERIC:
151 case librevenge::RVNG_UNIT_ERROR:
152 default:
153 WPS_DEBUG_MSG(("WPSPosition::getScaleFactor %d unit must not appear\n", int(orig)));
154 }
155 switch (dest)
156 {
157 case librevenge::RVNG_TWIP:
158 break;
159 case librevenge::RVNG_POINT:
160 newSc=20;
161 break;
162 case librevenge::RVNG_INCH:
163 newSc = 1440;
164 break;
165 case librevenge::RVNG_PERCENT:
166 case librevenge::RVNG_GENERIC:
167 case librevenge::RVNG_UNIT_ERROR:
168 default:
169 WPS_DEBUG_MSG(("WPSPosition::getScaleFactor %d unit must not appear\n", int(dest)));
170 }
171 return actSc/newSc;
172 }
174 float getInvUnitScale(librevenge::RVNGUnit unt) const
175 {
176 return getScaleFactor(unt, m_unit);
177 }
178
180 void setPage(int pg) const
181 {
182 const_cast<WPSPosition *>(this)->m_page = pg;
183 }
185 void setOrigin(Vec2f const &orig)
186 {
187 m_orig = orig;
188 }
190 void setSize(Vec2f const &sz)
191 {
192 m_size = sz;
193 }
195 void setNaturalSize(Vec2f const &natSize)
196 {
197 m_naturalSize = natSize;
198 }
200 void setUnit(librevenge::RVNGUnit unt)
201 {
202 m_unit = unt;
203 }
205 void setPagePos(int pg, Vec2f const &newOrig) const
206 {
207 const_cast<WPSPosition *>(this)->m_page = pg;
208 const_cast<WPSPosition *>(this)->m_orig = newOrig;
209 }
210
212 void setRelativePosition(AnchorTo anchor, XPos X = XLeft, YPos Y=YTop)
213 {
214 m_anchorTo = anchor;
215 m_xPos = X;
216 m_yPos = Y;
217 }
218
220 void setAnchorToCell(librevenge::RVNGString const &cellName)
221 {
222 m_anchorTo = Cell;
223 m_xPos = XLeft;
224 m_yPos = YTop;
225 m_anchorCellName=cellName;
226 }
228 int order() const
229 {
230 return m_order;
231 }
233 void setOrder(int ord) const
234 {
235 m_order = ord;
236 }
237
239 AnchorTo m_anchorTo;
241 librevenge::RVNGString m_anchorCellName;
243 XPos m_xPos;
245 YPos m_yPos;
247 Wrapping m_wrapping;
248
249 protected:
251 int cmp(WPSPosition const &f) const
252 {
253 int diff = int(m_anchorTo) - int(f.m_anchorTo);
254 if (diff) return diff < 0 ? -1 : 1;
255 if (m_anchorCellName<f.m_anchorCellName) return -1;
256 if (m_anchorCellName>f.m_anchorCellName) return 1;
257 diff = int(m_xPos) - int(f.m_xPos);
258 if (diff) return diff < 0 ? -1 : 1;
259 diff = int(m_yPos) - int(f.m_yPos);
260 if (diff) return diff < 0 ? -1 : 1;
261 diff = page() - f.page();
262 if (diff) return diff < 0 ? -1 : 1;
263 diff = int(m_unit) - int(f.m_unit);
264 if (diff) return diff < 0 ? -1 : 1;
265 diff = m_orig.cmpY(f.m_orig);
266 if (diff) return diff;
267 diff = m_size.cmpY(f.m_size);
268 if (diff) return diff;
269 diff = m_naturalSize.cmpY(f.m_naturalSize);
270 if (diff) return diff;
271
272 return 0;
273 }
274
276 int m_page;
277 Vec2f m_orig , m_size /* the size of the data*/, m_naturalSize ;
279 librevenge::RVNGUnit m_unit;
281 mutable int m_order;
282 };
283
284 #endif
285 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */
WPSPosition::m_naturalSize
Vec2f m_naturalSize
the natural size of the data (if known)
Definition: WPSPosition.h:277
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:241
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:95
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:212
WPSPosition::setSize
void setSize(Vec2f const &sz)
sets the frame size
Definition: WPSPosition.h:190
WPSPosition::m_order
int m_order
background/foward order
Definition: WPSPosition.h:281
WPSPosition::operator=
WPSPosition & operator=(WPSPosition const &)=default
WPSPosition::WNone
Definition: WPSPosition.h:45
WPSPosition::operator<<
friend std::ostream & operator<<(std::ostream &o, WPSPosition const &pos)
operator<<
Definition: WPSPosition.h:70
WPSPosition::setAnchorToCell
void setAnchorToCell(librevenge::RVNGString const &cellName)
sets the anchor to a cell position
Definition: WPSPosition.h:220
WPSPosition::setPage
void setPage(int pg) const
sets the page
Definition: WPSPosition.h:180
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:279
WPSPosition::page
int page() const
returns the frame page
Definition: WPSPosition.h:111
WPSPosition::setNaturalSize
void setNaturalSize(Vec2f const &natSize)
sets the natural size (if known)
Definition: WPSPosition.h:195
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:239
WPSPosition::WRunThrough
Definition: WPSPosition.h:45
WPSPosition::m_wrapping
Wrapping m_wrapping
Wrapping.
Definition: WPSPosition.h:247
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:205
WPSPosition::YCenter
Definition: WPSPosition.h:49
WPSPosition::naturalSize
Vec2f const & naturalSize() const
returns the natural size (if known)
Definition: WPSPosition.h:126
WPSPosition::m_orig
Vec2f m_orig
the origin position in a page
Definition: WPSPosition.h:277
WPSPosition::m_size
Vec2f m_size
Definition: WPSPosition.h:277
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:174
WPSPosition::origin
Vec2f const & origin() const
return the frame origin
Definition: WPSPosition.h:116
WPSPosition::operator<
bool operator<(WPSPosition const &f) const
basic operator<
Definition: WPSPosition.h:105
WPSPosition::order
int order() const
returns background/foward order
Definition: WPSPosition.h:228
WPSPosition::m_yPos
YPos m_yPos
Y relative position.
Definition: WPSPosition.h:245
WPSPosition::Page
Definition: WPSPosition.h:43
WPSPosition::setUnit
void setUnit(librevenge::RVNGUnit unt)
sets the dimension unit
Definition: WPSPosition.h:200
WPSPosition::setOrder
void setOrder(int ord) const
set background/foward order
Definition: WPSPosition.h:233
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:100
WPSPosition::size
Vec2f const & size() const
returns the frame size
Definition: WPSPosition.h:121
WPSPosition::XPos
XPos
an enum used to define the relative X position
Definition: WPSPosition.h:47
WPSPosition::~WPSPosition
~WPSPosition()
destructor
Definition: WPSPosition.h:68
WPSPosition::setOrigin
void setOrigin(Vec2f const &orig)
sets the frame origin
Definition: WPSPosition.h:185
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:131
WPSPosition::m_xPos
XPos m_xPos
X relative position.
Definition: WPSPosition.h:243
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:136
WPSPosition::cmp
int cmp(WPSPosition const &f) const
basic function to compare two positions
Definition: WPSPosition.h:251
WPSPosition::m_page
int m_page
the page
Definition: WPSPosition.h:276
WPSPosition::Paragraph
Definition: WPSPosition.h:43
WPSPosition::AnchorTo
AnchorTo
a list of enum used to defined the anchor
Definition: WPSPosition.h:43