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 <librevenge/librevenge.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, WForeground, WRunThrough };
00055   enum XPos { XRight, XLeft, XCenter, XFull };
00057   enum YPos { YTop, YBottom, YCenter, YFull };
00058 
00059 public:
00061   MWAWPosition(MWAWVec2f const &orig=MWAWVec2f(), MWAWVec2f const &sz=MWAWVec2f(), librevenge::RVNGUnit theUnit=librevenge::RVNG_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   {
00069     MWAWVec2f dest(pos.m_orig+pos.m_size);
00070     o << "Pos=(" << pos.m_orig << ")x(" << dest << ")";
00071     switch (pos.m_unit) {
00072     case librevenge::RVNG_INCH:
00073       o << "(inch)";
00074       break;
00075     case librevenge::RVNG_POINT:
00076       o << "(pt)";
00077       break;
00078     case librevenge::RVNG_TWIP:
00079       o << "(tw)";
00080       break;
00081     case librevenge::RVNG_PERCENT:
00082     case librevenge::RVNG_GENERIC:
00083     case librevenge::RVNG_UNIT_ERROR:
00084     default:
00085       break;
00086     }
00087     if (pos.page()>0) o << ", page=" << pos.page();
00088     return o;
00089   }
00091   bool operator==(MWAWPosition const &f) const
00092   {
00093     return cmp(f) == 0;
00094   }
00096   bool operator!=(MWAWPosition const &f) const
00097   {
00098     return cmp(f) != 0;
00099   }
00101   bool operator<(MWAWPosition const &f) const
00102   {
00103     return cmp(f) < 0;
00104   }
00105 
00107   int page() const
00108   {
00109     return m_page;
00110   }
00112   MWAWVec2f const &origin() const
00113   {
00114     return m_orig;
00115   }
00117   MWAWVec2f const &size() const
00118   {
00119     return m_size;
00120   }
00122   MWAWVec2f const &naturalSize() const
00123   {
00124     return m_naturalSize;
00125   }
00127   MWAWVec2f const &leftTopClipping() const
00128   {
00129     return m_LTClip;
00130   }
00132   MWAWVec2f const &rightBottomClipping() const
00133   {
00134     return m_RBClip;
00135   }
00137   librevenge::RVNGUnit unit() const
00138   {
00139     return m_unit;
00140   }
00141   static float getScaleFactor(librevenge::RVNGUnit orig, librevenge::RVNGUnit dest)
00142   {
00143     float actSc = 1.0, newSc = 1.0;
00144     switch (orig) {
00145     case librevenge::RVNG_TWIP:
00146       break;
00147     case librevenge::RVNG_POINT:
00148       actSc=20;
00149       break;
00150     case librevenge::RVNG_INCH:
00151       actSc = 1440;
00152       break;
00153     case librevenge::RVNG_PERCENT:
00154     case librevenge::RVNG_GENERIC:
00155     case librevenge::RVNG_UNIT_ERROR:
00156     default:
00157       MWAW_DEBUG_MSG(("MWAWPosition::getScaleFactor %d unit must not appear\n", int(orig)));
00158     }
00159     switch (dest) {
00160     case librevenge::RVNG_TWIP:
00161       break;
00162     case librevenge::RVNG_POINT:
00163       newSc=20;
00164       break;
00165     case librevenge::RVNG_INCH:
00166       newSc = 1440;
00167       break;
00168     case librevenge::RVNG_PERCENT:
00169     case librevenge::RVNG_GENERIC:
00170     case librevenge::RVNG_UNIT_ERROR:
00171     default:
00172       MWAW_DEBUG_MSG(("MWAWPosition::getScaleFactor %d unit must not appear\n", int(dest)));
00173     }
00174     return actSc/newSc;
00175   }
00177   float getInvUnitScale(librevenge::RVNGUnit fromUnit) const
00178   {
00179     return getScaleFactor(fromUnit, m_unit);
00180   }
00181 
00183   void setPage(int pg) const
00184   {
00185     const_cast<MWAWPosition *>(this)->m_page = pg;
00186   }
00188   void setOrigin(MWAWVec2f const &orig)
00189   {
00190     m_orig = orig;
00191   }
00193   void setSize(MWAWVec2f const &sz)
00194   {
00195     m_size = sz;
00196   }
00198   void setNaturalSize(MWAWVec2f const &naturalSz)
00199   {
00200     m_naturalSize = naturalSz;
00201   }
00203   void setUnit(librevenge::RVNGUnit newUnit)
00204   {
00205     m_unit = newUnit;
00206   }
00208   void setPagePos(int pg, MWAWVec2f const &newOrig) const
00209   {
00210     const_cast<MWAWPosition *>(this)->m_page = pg;
00211     const_cast<MWAWPosition *>(this)->m_orig = newOrig;
00212   }
00213 
00215   void setRelativePosition(AnchorTo anchor, XPos X = XLeft, YPos Y=YTop)
00216   {
00217     m_anchorTo = anchor;
00218     m_xPos = X;
00219     m_yPos = Y;
00220   }
00221 
00223   void setClippingPosition(MWAWVec2f lTop, MWAWVec2f rBottom)
00224   {
00225     m_LTClip = lTop;
00226     m_RBClip = rBottom;
00227   }
00228 
00230   int order() const
00231   {
00232     return m_order;
00233   }
00235   void setOrder(int ord) const
00236   {
00237     m_order = ord;
00238   }
00239 
00241   AnchorTo m_anchorTo;
00243   XPos m_xPos;
00245   YPos m_yPos;
00247   Wrapping m_wrapping;
00248 
00249 protected:
00251   int cmp(MWAWPosition const &f) const
00252   {
00253     int diff = int(m_anchorTo) - int(f.m_anchorTo);
00254     if (diff) return diff < 0 ? -1 : 1;
00255     diff = int(m_xPos) - int(f.m_xPos);
00256     if (diff) return diff < 0 ? -1 : 1;
00257     diff = int(m_yPos) - int(f.m_yPos);
00258     if (diff) return diff < 0 ? -1 : 1;
00259     diff = page() - f.page();
00260     if (diff) return diff < 0 ? -1 : 1;
00261     diff = int(m_unit) - int(f.m_unit);
00262     if (diff) return diff < 0 ? -1 : 1;
00263     diff = m_orig.cmpY(f.m_orig);
00264     if (diff) return diff;
00265     diff = m_size.cmpY(f.m_size);
00266     if (diff) return diff;
00267     diff = m_naturalSize.cmpY(f.m_naturalSize);
00268     if (diff) return diff;
00269     diff = m_LTClip.cmpY(f.m_LTClip);
00270     if (diff) return diff;
00271     diff = m_RBClip.cmpY(f.m_RBClip);
00272     if (diff) return diff;
00273 
00274     return 0;
00275   }
00276 
00278   int m_page;
00279   MWAWVec2f m_orig , m_size /* the size of the data*/, m_naturalSize ;
00280   MWAWVec2f m_LTClip , m_RBClip /* the right bottom clip position */;
00282   librevenge::RVNGUnit m_unit;
00284   mutable int m_order;
00285 };
00286 
00287 #endif
00288 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: