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 
40 {
41 public:
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)
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;
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  }
97  bool operator==(WPSPosition const &f) const
98  {
99  return cmp(f) == 0;
100  }
102  bool operator!=(WPSPosition const &f) const
103  {
104  return cmp(f) != 0;
105  }
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  }
118  Vec2f const &origin() const
119  {
120  return m_orig;
121  }
123  Vec2f const &size() const
124  {
125  return m_size;
126  }
128  Vec2f const &naturalSize() const
129  {
130  return m_naturalSize;
131  }
133  librevenge::RVNGUnit unit() const
134  {
135  return m_unit;
136  }
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  }
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  }
187  void setOrigin(Vec2f const &orig)
188  {
189  m_orig = orig;
190  }
192  void setSize(Vec2f const &sz)
193  {
194  m_size = sz;
195  }
197  void setNaturalSize(Vec2f const &natSize)
198  {
199  m_naturalSize = natSize;
200  }
202  void setUnit(librevenge::RVNGUnit unt)
203  {
204  m_unit = unt;
205  }
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 
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  }
230  int order() const
231  {
232  return m_order;
233  }
235  void setOrder(int ord) const
236  {
237  m_order = ord;
238  }
239 
243  librevenge::RVNGString m_anchorCellName;
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: */
int cmpY(Vec2< T > const &p) const
a comparison function: which first compares y then x
Definition: libwps_internal.h:656
Class to define the position of an object (textbox, picture, ..) in the document.
Definition: WPSPosition.h:40
YPos
an enum used to define the relative Y position
Definition: WPSPosition.h:49
@ YCenter
Definition: WPSPosition.h:49
@ YTop
Definition: WPSPosition.h:49
@ YBottom
Definition: WPSPosition.h:49
@ YFull
Definition: WPSPosition.h:49
WPSPosition(WPSPosition &&)=default
Wrapping m_wrapping
Wrapping.
Definition: WPSPosition.h:249
Vec2f const & origin() const
return the frame origin
Definition: WPSPosition.h:118
WPSPosition(WPSPosition const &)=default
Vec2f const & naturalSize() const
returns the natural size (if known)
Definition: WPSPosition.h:128
friend std::ostream & operator<<(std::ostream &o, WPSPosition const &pos)
operator<<
Definition: WPSPosition.h:72
int m_order
background/foward order
Definition: WPSPosition.h:283
void setAnchorToCell(librevenge::RVNGString const &cellName)
sets the anchor to a cell position
Definition: WPSPosition.h:222
WPSPosition(Vec2f const &orig=Vec2f(), Vec2f const &sz=Vec2f(), librevenge::RVNGUnit unt=librevenge::RVNG_INCH)
constructor
Definition: WPSPosition.h:53
~WPSPosition()
destructor
Definition: WPSPosition.h:70
void setPagePos(int pg, Vec2f const &newOrig) const
sets/resets the page and the origin
Definition: WPSPosition.h:207
int order() const
returns background/foward order
Definition: WPSPosition.h:230
librevenge::RVNGUnit unit() const
returns the unit
Definition: WPSPosition.h:133
XPos m_xPos
X relative position.
Definition: WPSPosition.h:245
librevenge::RVNGUnit m_unit
the unit used in orig and in m_size. Default: in inches
Definition: WPSPosition.h:281
AnchorTo
a list of enum used to defined the anchor
Definition: WPSPosition.h:43
@ Page
Definition: WPSPosition.h:43
@ Cell
Definition: WPSPosition.h:43
@ PageContent
Definition: WPSPosition.h:43
@ Paragraph
Definition: WPSPosition.h:43
@ Char
Definition: WPSPosition.h:43
@ CharBaseLine
Definition: WPSPosition.h:43
@ ParagraphContent
Definition: WPSPosition.h:43
void setNaturalSize(Vec2f const &natSize)
sets the natural size (if known)
Definition: WPSPosition.h:197
bool operator==(WPSPosition const &f) const
basic operator==
Definition: WPSPosition.h:97
void setRelativePosition(AnchorTo anchor, XPos X=XLeft, YPos Y=YTop)
sets the relative position
Definition: WPSPosition.h:214
Vec2f m_naturalSize
the natural size of the data (if known)
Definition: WPSPosition.h:279
WPSPosition & operator=(WPSPosition const &)=default
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
void setOrigin(Vec2f const &orig)
sets the frame origin
Definition: WPSPosition.h:187
int page() const
returns the frame page
Definition: WPSPosition.h:113
void setOrder(int ord) const
set background/foward order
Definition: WPSPosition.h:235
int m_page
the page
Definition: WPSPosition.h:278
int cmp(WPSPosition const &f) const
basic function to compare two positions
Definition: WPSPosition.h:253
bool operator<(WPSPosition const &f) const
basic operator<
Definition: WPSPosition.h:107
void setPage(int pg) const
sets the page
Definition: WPSPosition.h:182
void setSize(Vec2f const &sz)
sets the frame size
Definition: WPSPosition.h:192
Wrapping
an enum used to define the wrapping
Definition: WPSPosition.h:45
@ WNone
Definition: WPSPosition.h:45
@ WRunThrough
Definition: WPSPosition.h:45
@ WDynamic
Definition: WPSPosition.h:45
Vec2f const & size() const
returns the frame size
Definition: WPSPosition.h:123
void setUnit(librevenge::RVNGUnit unt)
sets the dimension unit
Definition: WPSPosition.h:202
bool operator!=(WPSPosition const &f) const
basic operator!=
Definition: WPSPosition.h:102
Vec2f m_size
Definition: WPSPosition.h:279
XPos
an enum used to define the relative X position
Definition: WPSPosition.h:47
@ XRight
Definition: WPSPosition.h:47
@ XLeft
Definition: WPSPosition.h:47
@ XFull
Definition: WPSPosition.h:47
@ XCenter
Definition: WPSPosition.h:47
librevenge::RVNGString m_anchorCellName
the anchor cell name
Definition: WPSPosition.h:243
AnchorTo m_anchorTo
anchor position
Definition: WPSPosition.h:241
YPos m_yPos
Y relative position.
Definition: WPSPosition.h:247
WPSPosition & operator=(WPSPosition &&)=default
float getInvUnitScale(librevenge::RVNGUnit unt) const
returns a float which can be used to scale some data in object unit
Definition: WPSPosition.h:176
Vec2f m_orig
the origin position in a page
Definition: WPSPosition.h:279
Vec2< float > Vec2f
Vec2 of float.
Definition: libwps_internal.h:704
#define WPS_DEBUG_MSG(M)
Definition: libwps_internal.h:134

Generated on Thu Jan 20 2022 00:00:00 for libwps by doxygen 1.9.1