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  * Copyright (C) 2009, 2011 Alonso Laurent (alonso@loria.fr)
00004  * Copyright (C) 2006, 2007 Andrew Ziem
00005  * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
00006  * Copyright (C) 2004 Marc Maurer (uwog@uwog.net)
00007  * Copyright (C) 2003-2005 William Lachance (william.lachance@sympatico.ca)
00008  *
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Library General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Library General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Library General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
00023  *
00024  * For further information visit http://libwps.sourceforge.net
00025  */
00026 
00027 /* "This product is not manufactured, approved, or supported by
00028  * Corel Corporation or Corel Corporation Limited."
00029  */
00030 
00031 #ifndef WPS_POSITION_H
00032 #define WPS_POSITION_H
00033 
00034 #include <ostream>
00035 
00036 #include <libwpd/WPXProperty.h>
00037 
00038 #include "libwps_internal.h"
00039 
00044 class WPSPosition
00045 {
00046 public:
00048         enum AnchorTo { Char, CharBaseLine, Paragraph, Page };
00050         enum Wrapping { WNone, WDynamic, WRunThrough }; // Add something for background ?
00052         enum XPos { XRight, XLeft, XCenter, XFull };
00054         enum YPos { YTop, YBottom, YCenter, YFull };
00055 
00056 public:
00058         WPSPosition(Vec2f const &orig=Vec2f(), Vec2f const &sz=Vec2f(), WPXUnit unt=WPX_INCH):
00059                 m_anchorTo(Char), m_xPos(XLeft), m_yPos(YTop), m_wrapping(WNone),
00060                 m_page(0), m_orig(orig), m_size(sz), m_naturalSize(), m_unit(unt), m_order(0) {}
00061 
00062         virtual ~WPSPosition() {}
00064         friend  std::ostream &operator<<(std::ostream &o, WPSPosition const &pos)
00065         {
00066                 Vec2f dest(pos.m_orig+pos.m_size);
00067                 o << "Pos=" << pos.m_orig << "x" << dest;
00068                 switch(pos.m_unit)
00069                 {
00070                 case WPX_INCH:
00071                         o << "(inch)";
00072                         break;
00073                 case WPX_POINT:
00074                         o << "(pt)";
00075                         break;
00076                 case WPX_TWIP:
00077                         o << "(tw)";
00078                         break;
00079                 default:
00080                         break;
00081                 }
00082                 if (pos.page()>0) o << ", page=" << pos.page();
00083                 return o;
00084         }
00086         bool operator==(WPSPosition const &f) const
00087         {
00088                 return cmp(f) == 0;
00089         }
00091         bool operator!=(WPSPosition const &f) const
00092         {
00093                 return cmp(f) != 0;
00094         }
00096         bool operator<(WPSPosition const &f) const
00097         {
00098                 return cmp(f) < 0;
00099         }
00100 
00102         int page() const
00103         {
00104                 return m_page;
00105         }
00107         Vec2f const &origin() const
00108         {
00109                 return m_orig;
00110         }
00112         Vec2f const &size() const
00113         {
00114                 return m_size;
00115         }
00117         Vec2f const &naturalSize() const
00118         {
00119                 return m_naturalSize;
00120         }
00122         WPXUnit unit() const
00123         {
00124                 return m_unit;
00125         }
00127         static float getScaleFactor(WPXUnit orig, WPXUnit dest)
00128         {
00129                 float actSc = 1.0, newSc = 1.0;
00130                 switch(orig)
00131                 {
00132                 case WPX_TWIP:
00133                         break;
00134                 case WPX_POINT:
00135                         actSc=20;
00136                         break;
00137                 case WPX_INCH:
00138                         actSc = 1440;
00139                         break;
00140                 default:
00141                         WPS_DEBUG_MSG(("WPSPosition::getScaleFactor %d unit must not appear\n", int(orig)));
00142                 }
00143                 switch(dest)
00144                 {
00145                 case WPX_TWIP:
00146                         break;
00147                 case WPX_POINT:
00148                         newSc=20;
00149                         break;
00150                 case WPX_INCH:
00151                         newSc = 1440;
00152                         break;
00153                 default:
00154                         WPS_DEBUG_MSG(("WPSPosition::getScaleFactor %d unit must not appear\n", int(dest)));
00155                 }
00156                 return actSc/newSc;
00157         }
00159         float getInvUnitScale(WPXUnit unt) const
00160         {
00161                 return getScaleFactor(unt, m_unit);
00162         }
00163 
00165         void setPage(int pg) const
00166         {
00167                 const_cast<WPSPosition *>(this)->m_page = pg;
00168         }
00170         void setOrigin(Vec2f const &orig)
00171         {
00172                 m_orig = orig;
00173         }
00175         void setSize(Vec2f const &sz)
00176         {
00177                 m_size = sz;
00178         }
00180         void setNaturalSize(Vec2f const &natSize)
00181         {
00182                 m_naturalSize = natSize;
00183         }
00185         void setUnit(WPXUnit unt)
00186         {
00187                 m_unit = unt;
00188         }
00190         void setPagePos(int pg, Vec2f const &newOrig) const
00191         {
00192                 const_cast<WPSPosition *>(this)->m_page = pg;
00193                 const_cast<WPSPosition *>(this)->m_orig = newOrig;
00194         }
00195 
00197         void setRelativePosition(AnchorTo anchor, XPos X = XLeft, YPos Y=YTop)
00198         {
00199                 m_anchorTo = anchor;
00200                 m_xPos = X;
00201                 m_yPos = Y;
00202         }
00203 
00205         int order() const
00206         {
00207                 return m_order;
00208         }
00210         void setOrder(int ord) const
00211         {
00212                 m_order = ord;
00213         }
00214 
00216         AnchorTo m_anchorTo;
00218         XPos m_xPos;
00220         YPos m_yPos;
00222         Wrapping m_wrapping;
00223 
00224 protected:
00226         int cmp(WPSPosition const &f) const
00227         {
00228                 int diff = int(m_anchorTo) - int(f.m_anchorTo);
00229                 if (diff) return diff < 0 ? -1 : 1;
00230                 diff = int(m_xPos) - int(f.m_xPos);
00231                 if (diff) return diff < 0 ? -1 : 1;
00232                 diff = int(m_yPos) - int(f.m_yPos);
00233                 if (diff) return diff < 0 ? -1 : 1;
00234                 diff = page() - f.page();
00235                 if (diff) return diff < 0 ? -1 : 1;
00236                 diff = int(m_unit) - int(f.m_unit);
00237                 if (diff) return diff < 0 ? -1 : 1;
00238                 diff = m_orig.cmpY(f.m_orig);
00239                 if (diff) return diff;
00240                 diff = m_size.cmpY(f.m_size);
00241                 if (diff) return diff;
00242                 diff = m_naturalSize.cmpY(f.m_naturalSize);
00243                 if (diff) return diff;
00244 
00245                 return 0;
00246         }
00247 
00249         int m_page;
00250         Vec2f m_orig , m_size /* the size of the data*/, m_naturalSize ;
00252         WPXUnit m_unit;
00254         mutable int m_order;
00255 };
00256 
00257 #endif
00258 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */