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 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;
70 ~WPSPosition() {}
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
97 bool operator==(WPSPosition const &f) const
98 {
99 return cmp(f) == 0;
100 }
101
102 bool operator!=(WPSPosition const &f) const
103 {
104 return cmp(f) != 0;
105 }
106
107 bool operator<(WPSPosition const &f) const
108 {
109 return cmp(f) < 0;
110 }
111
113 int page() const
114 {
115 return m_page;
116 }
117
118 Vec2f const &origin() const
119 {
120 return m_orig;
121 }
122
123 Vec2f const &size() const
124 {
125 return m_size;
126 }
127
128 Vec2f const &naturalSize() const
129 {
130 return m_naturalSize;
131 }
132
133 librevenge::RVNGUnit unit() const
134 {
135 return m_unit;
136 }
137
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
176 float getInvUnitScale(librevenge::RVNGUnit unt) const
177 {
178 return getScaleFactor(unt, m_unit);
179 }
180
182 void setPage(int pg) const
183 {
184 const_cast<WPSPosition *>(this)->m_page = pg;
185 }
186
187 void setOrigin(Vec2f const &orig)
188 {
189 m_orig = orig;
190 }
191
192 void setSize(Vec2f const &sz)
193 {
194 m_size = sz;
195 }
196
197 void setNaturalSize(Vec2f const &natSize)
198 {
199 m_naturalSize = natSize;
200 }
201
202 void setUnit(librevenge::RVNGUnit unt)
203 {
204 m_unit = unt;
205 }
206
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
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
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
230 int order() const
231 {
232 return m_order;
233 }
234
235 void setOrder(int ord) const
236 {
237 m_order = ord;
238 }
239
241 AnchorTo m_anchorTo;
243 librevenge::RVNGString m_anchorCellName;
245 XPos m_xPos;
247 YPos m_yPos;
249 Wrapping m_wrapping;
250
251 protected:
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
278 int m_page;
279 Vec2f m_orig , m_size /* the size of the data*/, m_naturalSize ;
281 librevenge::RVNGUnit m_unit;
283 mutable int m_order;
284};
285
286#endif
287/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */
WPSPosition::operator=
WPSPosition & operator=(WPSPosition &&)=default
WPSPosition::YPos
YPos
an enum used to define the relative Y position
Definition WPSPosition.h:49
WPSPosition::YCenter
@ YCenter
Definition WPSPosition.h:49
WPSPosition::YTop
@ YTop
Definition WPSPosition.h:49
WPSPosition::YBottom
@ YBottom
Definition WPSPosition.h:49
WPSPosition::YFull
@ YFull
Definition WPSPosition.h:49
WPSPosition::WPSPosition
WPSPosition(WPSPosition &&)=default
WPSPosition::m_wrapping
Wrapping m_wrapping
Wrapping.
Definition WPSPosition.h:249
WPSPosition::WPSPosition
WPSPosition(WPSPosition const &)=default
WPSPosition::m_order
int m_order
background/foward order
Definition WPSPosition.h:283
WPSPosition::setAnchorToCell
void setAnchorToCell(librevenge::RVNGString const &cellName)
sets the anchor to a cell position
Definition WPSPosition.h:222
WPSPosition::operator=
WPSPosition & operator=(WPSPosition const &)=default
WPSPosition::WPSPosition
WPSPosition(Vec2f const &orig=Vec2f(), Vec2f const &sz=Vec2f(), librevenge::RVNGUnit unt=librevenge::RVNG_INCH)
constructor
Definition WPSPosition.h:53
WPSPosition::~WPSPosition
~WPSPosition()
destructor
Definition WPSPosition.h:70
WPSPosition::naturalSize
Vec2f const & naturalSize() const
returns the natural size (if known)
Definition WPSPosition.h:128
WPSPosition::setPagePos
void setPagePos(int pg, Vec2f const &newOrig) const
sets/resets the page and the origin
Definition WPSPosition.h:207
WPSPosition::order
int order() const
returns background/foward order
Definition WPSPosition.h:230
WPSPosition::unit
librevenge::RVNGUnit unit() const
returns the unit
Definition WPSPosition.h:133
WPSPosition::m_xPos
XPos m_xPos
X relative position.
Definition WPSPosition.h:245
WPSPosition::m_unit
librevenge::RVNGUnit m_unit
the unit used in orig and in m_size. Default: in inches
Definition WPSPosition.h:281
WPSPosition::AnchorTo
AnchorTo
a list of enum used to defined the anchor
Definition WPSPosition.h:43
WPSPosition::Page
@ Page
Definition WPSPosition.h:43
WPSPosition::Cell
@ Cell
Definition WPSPosition.h:43
WPSPosition::PageContent
@ PageContent
Definition WPSPosition.h:43
WPSPosition::Paragraph
@ Paragraph
Definition WPSPosition.h:43
WPSPosition::Char
@ Char
Definition WPSPosition.h:43
WPSPosition::CharBaseLine
@ CharBaseLine
Definition WPSPosition.h:43
WPSPosition::ParagraphContent
@ ParagraphContent
Definition WPSPosition.h:43
WPSPosition::setNaturalSize
void setNaturalSize(Vec2f const &natSize)
sets the natural size (if known)
Definition WPSPosition.h:197
WPSPosition::operator==
bool operator==(WPSPosition const &f) const
basic operator==
Definition WPSPosition.h:97
WPSPosition::setRelativePosition
void setRelativePosition(AnchorTo anchor, XPos X=XLeft, YPos Y=YTop)
sets the relative position
Definition WPSPosition.h:214
WPSPosition::m_naturalSize
Vec2f m_naturalSize
the natural size of the data (if known)
Definition WPSPosition.h:279
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:138
WPSPosition::setOrigin
void setOrigin(Vec2f const &orig)
sets the frame origin
Definition WPSPosition.h:187
WPSPosition::page
int page() const
returns the frame page
Definition WPSPosition.h:113
WPSPosition::setOrder
void setOrder(int ord) const
set background/foward order
Definition WPSPosition.h:235
WPSPosition::operator<<
friend std::ostream & operator<<(std::ostream &o, WPSPosition const &pos)
operator<<
Definition WPSPosition.h:72
WPSPosition::m_page
int m_page
the page
Definition WPSPosition.h:278
WPSPosition::cmp
int cmp(WPSPosition const &f) const
basic function to compare two positions
Definition WPSPosition.h:253
WPSPosition::operator<
bool operator<(WPSPosition const &f) const
basic operator<
Definition WPSPosition.h:107
WPSPosition::setPage
void setPage(int pg) const
sets the page
Definition WPSPosition.h:182
WPSPosition::setSize
void setSize(Vec2f const &sz)
sets the frame size
Definition WPSPosition.h:192
WPSPosition::Wrapping
Wrapping
an enum used to define the wrapping
Definition WPSPosition.h:45
WPSPosition::WNone
@ WNone
Definition WPSPosition.h:45
WPSPosition::WRunThrough
@ WRunThrough
Definition WPSPosition.h:45
WPSPosition::WDynamic
@ WDynamic
Definition WPSPosition.h:45
WPSPosition::setUnit
void setUnit(librevenge::RVNGUnit unt)
sets the dimension unit
Definition WPSPosition.h:202
WPSPosition::operator!=
bool operator!=(WPSPosition const &f) const
basic operator!=
Definition WPSPosition.h:102
WPSPosition::m_size
Vec2f m_size
Definition WPSPosition.h:279
WPSPosition::size
Vec2f const & size() const
returns the frame size
Definition WPSPosition.h:123
WPSPosition::XPos
XPos
an enum used to define the relative X position
Definition WPSPosition.h:47
WPSPosition::XRight
@ XRight
Definition WPSPosition.h:47
WPSPosition::XLeft
@ XLeft
Definition WPSPosition.h:47
WPSPosition::XFull
@ XFull
Definition WPSPosition.h:47
WPSPosition::XCenter
@ XCenter
Definition WPSPosition.h:47
WPSPosition::m_anchorCellName
librevenge::RVNGString m_anchorCellName
the anchor cell name
Definition WPSPosition.h:243
WPSPosition::m_anchorTo
AnchorTo m_anchorTo
anchor position
Definition WPSPosition.h:241
WPSPosition::m_yPos
YPos m_yPos
Y relative position.
Definition WPSPosition.h:247
WPSPosition::origin
Vec2f const & origin() const
return the frame origin
Definition WPSPosition.h:118
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:176
WPSPosition::m_orig
Vec2f m_orig
the origin position in a page
Definition WPSPosition.h:279
libwps_internal.h
Vec2f
Vec2< float > Vec2f
Vec2 of float.
Definition libwps_internal.h:704
WPS_DEBUG_MSG
#define WPS_DEBUG_MSG(M)
Definition libwps_internal.h:134