MWAWPosition.hxx
Go to the documentation of this file.
00001 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
00002 
00003 /* libmwaw
00004 * Version: MPL 2.0 / LGPLv2+
00005 *
00006 * The contents of this file are subject to the Mozilla Public License Version
00007 * 2.0 (the "License"); you may not use this file except in compliance with
00008 * the License or as specified alternatively below. You may obtain a copy of
00009 * the License at http://www.mozilla.org/MPL/
00010 *
00011 * Software distributed under the License is distributed on an "AS IS" basis,
00012 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013 * for the specific language governing rights and limitations under the
00014 * License.
00015 *
00016 * Major Contributor(s):
00017 * Copyright (C) 2002 William Lachance (wrlach@gmail.com)
00018 * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
00019 * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
00020 * Copyright (C) 2006, 2007 Andrew Ziem
00021 * Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
00022 *
00023 *
00024 * All Rights Reserved.
00025 *
00026 * For minor contributions see the git repository.
00027 *
00028 * Alternatively, the contents of this file may be used under the terms of
00029 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
00030 * in which case the provisions of the LGPLv2+ are applicable
00031 * instead of those above.
00032 */
00033 
00034 #ifndef MWAW_POSITION_H
00035 #define MWAW_POSITION_H
00036 
00037 #include <ostream>
00038 
00039 #include <libwpd/libwpd.h>
00040 
00041 #include "libmwaw_internal.hxx"
00042 
00047 class MWAWPosition
00048 {
00049 public:
00051   enum AnchorTo { Char, CharBaseLine, Frame, Paragraph, Page, Unknown };
00053   enum Wrapping { WNone, WBackground, WDynamic, WRunThrough };
00055   enum XPos { XRight, XLeft, XCenter, XFull };
00057   enum YPos { YTop, YBottom, YCenter, YFull };
00058 
00059 public:
00061   MWAWPosition(Vec2f const &orig=Vec2f(), Vec2f const &sz=Vec2f(), WPXUnit theUnit=WPX_INCH):
00062     m_anchorTo(Unknown), m_xPos(XLeft), m_yPos(YTop), m_wrapping(WNone),
00063     m_page(0), m_orig(orig), m_size(sz), m_naturalSize(), m_LTClip(), m_RBClip(), m_unit(theUnit), m_order(0) {}
00064 
00065   virtual ~MWAWPosition() {}
00067   friend  std::ostream &operator<<(std::ostream &o, MWAWPosition const &pos) {
00068     Vec2f dest(pos.m_orig+pos.m_size);
00069     o << "Pos=(" << pos.m_orig << ")x(" << dest << ")";
00070     switch(pos.m_unit) {
00071     case WPX_INCH:
00072       o << "(inch)";
00073       break;
00074     case WPX_POINT:
00075       o << "(pt)";
00076       break;
00077     case WPX_TWIP:
00078       o << "(tw)";
00079       break;
00080     case WPX_PERCENT:
00081     case WPX_GENERIC:
00082     default:
00083       break;
00084     }
00085     if (pos.page()>0) o << ", page=" << pos.page();
00086     return o;
00087   }
00089   bool operator==(MWAWPosition const &f) const {
00090     return cmp(f) == 0;
00091   }
00093   bool operator!=(MWAWPosition const &f) const {
00094     return cmp(f) != 0;
00095   }
00097   bool operator<(MWAWPosition const &f) const {
00098     return cmp(f) < 0;
00099   }
00100 
00102   int page() const {
00103     return m_page;
00104   }
00106   Vec2f const &origin() const {
00107     return m_orig;
00108   }
00110   Vec2f const &size() const {
00111     return m_size;
00112   }
00114   Vec2f const &naturalSize() const {
00115     return m_naturalSize;
00116   }
00118   Vec2f const &leftTopClipping() const {
00119     return m_LTClip;
00120   }
00122   Vec2f const &rightBottomClipping() const {
00123     return m_RBClip;
00124   }
00126   WPXUnit unit() const {
00127     return m_unit;
00128   }
00129   static float getScaleFactor(WPXUnit orig, WPXUnit dest) {
00130     float actSc = 1.0, newSc = 1.0;
00131     switch(orig) {
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     case WPX_PERCENT:
00141     case WPX_GENERIC:
00142     default:
00143       MWAW_DEBUG_MSG(("MWAWPosition::getScaleFactor %d unit must not appear\n", int(orig)));
00144     }
00145     switch(dest) {
00146     case WPX_TWIP:
00147       break;
00148     case WPX_POINT:
00149       newSc=20;
00150       break;
00151     case WPX_INCH:
00152       newSc = 1440;
00153       break;
00154     case WPX_PERCENT:
00155     case WPX_GENERIC:
00156     default:
00157       MWAW_DEBUG_MSG(("MWAWPosition::getScaleFactor %d unit must not appear\n", int(dest)));
00158     }
00159     return actSc/newSc;
00160   }
00162   float getInvUnitScale(WPXUnit fromUnit) const {
00163     return getScaleFactor(fromUnit, m_unit);
00164   }
00165 
00167   void setPage(int pg) const {
00168     const_cast<MWAWPosition *>(this)->m_page = pg;
00169   }
00171   void setOrigin(Vec2f const &orig) {
00172     m_orig = orig;
00173   }
00175   void setSize(Vec2f const &sz) {
00176     m_size = sz;
00177   }
00179   void setNaturalSize(Vec2f const &naturalSz) {
00180     m_naturalSize = naturalSz;
00181   }
00183   void setUnit(WPXUnit newUnit) {
00184     m_unit = newUnit;
00185   }
00187   void setPagePos(int pg, Vec2f const &newOrig) const {
00188     const_cast<MWAWPosition *>(this)->m_page = pg;
00189     const_cast<MWAWPosition *>(this)->m_orig = newOrig;
00190   }
00191 
00193   void setRelativePosition(AnchorTo anchor, XPos X = XLeft, YPos Y=YTop) {
00194     m_anchorTo = anchor;
00195     m_xPos = X;
00196     m_yPos = Y;
00197   }
00198 
00200   void setClippingPosition(Vec2f lTop, Vec2f rBottom) {
00201     m_LTClip = lTop;
00202     m_RBClip = rBottom;
00203   }
00204 
00206   int order() const {
00207     return m_order;
00208   }
00210   void setOrder(int ord) const {
00211     m_order = ord;
00212   }
00213 
00215   AnchorTo m_anchorTo;
00217   XPos m_xPos;
00219   YPos m_yPos;
00221   Wrapping m_wrapping;
00222 
00223 protected:
00225   int cmp(MWAWPosition const &f) const {
00226     int diff = int(m_anchorTo) - int(f.m_anchorTo);
00227     if (diff) return diff < 0 ? -1 : 1;
00228     diff = int(m_xPos) - int(f.m_xPos);
00229     if (diff) return diff < 0 ? -1 : 1;
00230     diff = int(m_yPos) - int(f.m_yPos);
00231     if (diff) return diff < 0 ? -1 : 1;
00232     diff = page() - f.page();
00233     if (diff) return diff < 0 ? -1 : 1;
00234     diff = int(m_unit) - int(f.m_unit);
00235     if (diff) return diff < 0 ? -1 : 1;
00236     diff = m_orig.cmpY(f.m_orig);
00237     if (diff) return diff;
00238     diff = m_size.cmpY(f.m_size);
00239     if (diff) return diff;
00240     diff = m_naturalSize.cmpY(f.m_naturalSize);
00241     if (diff) return diff;
00242     diff = m_LTClip.cmpY(f.m_LTClip);
00243     if (diff) return diff;
00244     diff = m_RBClip.cmpY(f.m_RBClip);
00245     if (diff) return diff;
00246 
00247     return 0;
00248   }
00249 
00251   int m_page;
00252   Vec2f m_orig , m_size /* the size of the data*/, m_naturalSize ;
00253   Vec2f m_LTClip , m_RBClip /* the right bottom clip position */;
00255   WPXUnit m_unit;
00257   mutable int m_order;
00258 };
00259 
00260 #endif
00261 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: