WPSPosition.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
00002 /* libwps
00003  * Version: MPL 2.0 / LGPLv2.1+
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this
00007  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Major Contributor(s):
00010  * Copyright (C) 2009, 2011 Alonso Laurent (alonso@loria.fr)
00011  * Copyright (C) 2006, 2007 Andrew Ziem
00012  * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
00013  * Copyright (C) 2004 Marc Maurer (uwog@uwog.net)
00014  * Copyright (C) 2003-2005 William Lachance (william.lachance@sympatico.ca)
00015  *
00016  * For minor contributions see the git repository.
00017  *
00018  * Alternatively, the contents of this file may be used under the terms
00019  * of the GNU Lesser General Public License Version 2.1 or later
00020  * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
00021  * applicable instead of those above.
00022  *
00023  * For further information visit http://libwps.sourceforge.net
00024  */
00025 
00026 #ifndef WPS_POSITION_H
00027 #define WPS_POSITION_H
00028 
00029 #include <ostream>
00030 
00031 #include <librevenge/librevenge.h>
00032 
00033 #include "libwps_internal.h"
00034 
00039 class WPSPosition
00040 {
00041 public:
00043         enum AnchorTo { Char, CharBaseLine, Paragraph, ParagraphContent, Page, PageContent };
00045         enum Wrapping { WNone, WDynamic, WRunThrough }; // Add something for background ?
00047         enum XPos { XRight, XLeft, XCenter, XFull };
00049         enum YPos { YTop, YBottom, YCenter, YFull };
00050 
00051 public:
00053         WPSPosition(Vec2f const &orig=Vec2f(), Vec2f const &sz=Vec2f(), librevenge::RVNGUnit unt=librevenge::RVNG_INCH):
00054                 m_anchorTo(Char), m_xPos(XLeft), m_yPos(YTop), m_wrapping(WNone),
00055                 m_page(0), m_orig(orig), m_size(sz), m_naturalSize(), m_unit(unt), m_order(0) {}
00056 
00057         virtual ~WPSPosition() {}
00059         friend  std::ostream &operator<<(std::ostream &o, WPSPosition const &pos)
00060         {
00061                 Vec2f dest(pos.m_orig+pos.m_size);
00062                 o << "Pos=" << pos.m_orig << "x" << dest;
00063                 switch (pos.m_unit)
00064                 {
00065                 case librevenge::RVNG_INCH:
00066                         o << "(inch)";
00067                         break;
00068                 case librevenge::RVNG_POINT:
00069                         o << "(pt)";
00070                         break;
00071                 case librevenge::RVNG_TWIP:
00072                         o << "(tw)";
00073                         break;
00074                 case librevenge::RVNG_PERCENT:
00075                 case librevenge::RVNG_GENERIC:
00076                 case librevenge::RVNG_UNIT_ERROR:
00077                 default:
00078                         break;
00079                 }
00080                 if (pos.page()>0) o << ", page=" << pos.page();
00081                 return o;
00082         }
00084         bool operator==(WPSPosition const &f) const
00085         {
00086                 return cmp(f) == 0;
00087         }
00089         bool operator!=(WPSPosition const &f) const
00090         {
00091                 return cmp(f) != 0;
00092         }
00094         bool operator<(WPSPosition const &f) const
00095         {
00096                 return cmp(f) < 0;
00097         }
00098 
00100         int page() const
00101         {
00102                 return m_page;
00103         }
00105         Vec2f const &origin() const
00106         {
00107                 return m_orig;
00108         }
00110         Vec2f const &size() const
00111         {
00112                 return m_size;
00113         }
00115         Vec2f const &naturalSize() const
00116         {
00117                 return m_naturalSize;
00118         }
00120         librevenge::RVNGUnit unit() const
00121         {
00122                 return m_unit;
00123         }
00125         static float getScaleFactor(librevenge::RVNGUnit orig, librevenge::RVNGUnit dest)
00126         {
00127                 float actSc = 1.0, newSc = 1.0;
00128                 switch (orig)
00129                 {
00130                 case librevenge::RVNG_TWIP:
00131                         break;
00132                 case librevenge::RVNG_POINT:
00133                         actSc=20;
00134                         break;
00135                 case librevenge::RVNG_INCH:
00136                         actSc = 1440;
00137                         break;
00138                 case librevenge::RVNG_PERCENT:
00139                 case librevenge::RVNG_GENERIC:
00140                 case librevenge::RVNG_UNIT_ERROR:
00141                 default:
00142                         WPS_DEBUG_MSG(("WPSPosition::getScaleFactor %d unit must not appear\n", int(orig)));
00143                 }
00144                 switch (dest)
00145                 {
00146                 case librevenge::RVNG_TWIP:
00147                         break;
00148                 case librevenge::RVNG_POINT:
00149                         newSc=20;
00150                         break;
00151                 case librevenge::RVNG_INCH:
00152                         newSc = 1440;
00153                         break;
00154                 case librevenge::RVNG_PERCENT:
00155                 case librevenge::RVNG_GENERIC:
00156                 case librevenge::RVNG_UNIT_ERROR:
00157                 default:
00158                         WPS_DEBUG_MSG(("WPSPosition::getScaleFactor %d unit must not appear\n", int(dest)));
00159                 }
00160                 return actSc/newSc;
00161         }
00163         float getInvUnitScale(librevenge::RVNGUnit unt) const
00164         {
00165                 return getScaleFactor(unt, m_unit);
00166         }
00167 
00169         void setPage(int pg) const
00170         {
00171                 const_cast<WPSPosition *>(this)->m_page = pg;
00172         }
00174         void setOrigin(Vec2f const &orig)
00175         {
00176                 m_orig = orig;
00177         }
00179         void setSize(Vec2f const &sz)
00180         {
00181                 m_size = sz;
00182         }
00184         void setNaturalSize(Vec2f const &natSize)
00185         {
00186                 m_naturalSize = natSize;
00187         }
00189         void setUnit(librevenge::RVNGUnit unt)
00190         {
00191                 m_unit = unt;
00192         }
00194         void setPagePos(int pg, Vec2f const &newOrig) const
00195         {
00196                 const_cast<WPSPosition *>(this)->m_page = pg;
00197                 const_cast<WPSPosition *>(this)->m_orig = newOrig;
00198         }
00199 
00201         void setRelativePosition(AnchorTo anchor, XPos X = XLeft, YPos Y=YTop)
00202         {
00203                 m_anchorTo = anchor;
00204                 m_xPos = X;
00205                 m_yPos = Y;
00206         }
00207 
00209         int order() const
00210         {
00211                 return m_order;
00212         }
00214         void setOrder(int ord) const
00215         {
00216                 m_order = ord;
00217         }
00218 
00220         AnchorTo m_anchorTo;
00222         XPos m_xPos;
00224         YPos m_yPos;
00226         Wrapping m_wrapping;
00227 
00228 protected:
00230         int cmp(WPSPosition const &f) const
00231         {
00232                 int diff = int(m_anchorTo) - int(f.m_anchorTo);
00233                 if (diff) return diff < 0 ? -1 : 1;
00234                 diff = int(m_xPos) - int(f.m_xPos);
00235                 if (diff) return diff < 0 ? -1 : 1;
00236                 diff = int(m_yPos) - int(f.m_yPos);
00237                 if (diff) return diff < 0 ? -1 : 1;
00238                 diff = page() - f.page();
00239                 if (diff) return diff < 0 ? -1 : 1;
00240                 diff = int(m_unit) - int(f.m_unit);
00241                 if (diff) return diff < 0 ? -1 : 1;
00242                 diff = m_orig.cmpY(f.m_orig);
00243                 if (diff) return diff;
00244                 diff = m_size.cmpY(f.m_size);
00245                 if (diff) return diff;
00246                 diff = m_naturalSize.cmpY(f.m_naturalSize);
00247                 if (diff) return diff;
00248 
00249                 return 0;
00250         }
00251 
00253         int m_page;
00254         Vec2f m_orig , m_size /* the size of the data*/, m_naturalSize ;
00256         librevenge::RVNGUnit m_unit;
00258         mutable int m_order;
00259 };
00260 
00261 #endif
00262 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */