WPSPosition.h
Go to the documentation of this file.
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* libwps
* Version: MPL 2.0 / LGPLv2.1+
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Major Contributor(s):
* Copyright (C) 2009, 2011 Alonso Laurent (alonso@loria.fr)
* Copyright (C) 2006, 2007 Andrew Ziem
* Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
* Copyright (C) 2004 Marc Maurer (uwog@uwog.net)
* Copyright (C) 2003-2005 William Lachance (william.lachance@sympatico.ca)
*
* For minor contributions see the git repository.
*
* Alternatively, the contents of this file may be used under the terms
* of the GNU Lesser General Public License Version 2.1 or later
* (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
* applicable instead of those above.
*
* For further information visit http://libwps.sourceforge.net
*/
#ifndef WPS_POSITION_H
#define WPS_POSITION_H
#include <ostream>
#include <libwpd/libwpd.h>
#include "libwps_internal.h"
class WPSPosition
{
public:
enum AnchorTo { Char, CharBaseLine, Paragraph, Page };
enum Wrapping { WNone, WDynamic, WRunThrough }; // Add something for background ?
enum XPos { XRight, XLeft, XCenter, XFull };
enum YPos { YTop, YBottom, YCenter, YFull };
public:
WPSPosition(Vec2f const &orig=Vec2f(), Vec2f const &sz=Vec2f(), WPXUnit unt=WPX_INCH):
m_anchorTo(Char), m_xPos(XLeft), m_yPos(YTop), m_wrapping(WNone),
m_page(0), m_orig(orig), m_size(sz), m_naturalSize(), m_unit(unt), m_order(0) {}
virtual ~WPSPosition() {}
friend std::ostream &operator<<(std::ostream &o, WPSPosition const &pos)
{
Vec2f dest(pos.m_orig+pos.m_size);
o << "Pos=" << pos.m_orig << "x" << dest;
switch(pos.m_unit)
{
case WPX_INCH:
o << "(inch)";
break;
case WPX_POINT:
o << "(pt)";
break;
case WPX_TWIP:
o << "(tw)";
break;
case WPX_PERCENT:
case WPX_GENERIC:
default:
break;
}
if (pos.page()>0) o << ", page=" << pos.page();
return o;
}
bool operator==(WPSPosition const &f) const
{
return cmp(f) == 0;
}
bool operator!=(WPSPosition const &f) const
{
return cmp(f) != 0;
}
bool operator<(WPSPosition const &f) const
{
return cmp(f) < 0;
}
int page() const
{
return m_page;
}
Vec2f const &origin() const
{
return m_orig;
}
Vec2f const &size() const
{
return m_size;
}
Vec2f const &naturalSize() const
{
return m_naturalSize;
}
WPXUnit unit() const
{
return m_unit;
}
static float getScaleFactor(WPXUnit orig, WPXUnit dest)
{
float actSc = 1.0, newSc = 1.0;
switch(orig)
{
case WPX_TWIP:
break;
case WPX_POINT:
actSc=20;
break;
case WPX_INCH:
actSc = 1440;
break;
case WPX_PERCENT:
case WPX_GENERIC:
default:
WPS_DEBUG_MSG(("WPSPosition::getScaleFactor %d unit must not appear\n", int(orig)));
}
switch(dest)
{
case WPX_TWIP:
break;
case WPX_POINT:
newSc=20;
break;
case WPX_INCH:
newSc = 1440;
break;
case WPX_PERCENT:
case WPX_GENERIC:
default:
WPS_DEBUG_MSG(("WPSPosition::getScaleFactor %d unit must not appear\n", int(dest)));
}
return actSc/newSc;
}
float getInvUnitScale(WPXUnit unt) const
{
return getScaleFactor(unt, m_unit);
}
void setPage(int pg) const
{
const_cast< WPSPosition *>(this)->m_page = pg;
}
void setOrigin(Vec2f const &orig)
{
m_orig = orig;
}
void setSize(Vec2f const &sz)
{
m_size = sz;
}
void setNaturalSize(Vec2f const &natSize)
{
m_naturalSize = natSize;
}
void setUnit(WPXUnit unt)
{
m_unit = unt;
}
void setPagePos(int pg, Vec2f const &newOrig) const
{
const_cast< WPSPosition *>(this)->m_page = pg;
const_cast< WPSPosition *>(this)->m_orig = newOrig;
}
void setRelativePosition(AnchorTo anchor, XPos X = XLeft, YPos Y=YTop)
{
m_anchorTo = anchor;
m_xPos = X;
m_yPos = Y;
}
int order() const
{
return m_order;
}
void setOrder(int ord) const
{
m_order = ord;
}
AnchorTo m_anchorTo;
XPos m_xPos;
YPos m_yPos;
Wrapping m_wrapping;
protected:
int cmp(WPSPosition const &f) const
{
int diff = int(m_anchorTo) - int(f.m_anchorTo);
if (diff) return diff < 0 ? -1 : 1;
diff = int(m_xPos) - int(f.m_xPos);
if (diff) return diff < 0 ? -1 : 1;
diff = int(m_yPos) - int(f.m_yPos);
if (diff) return diff < 0 ? -1 : 1;
diff = page() - f.page();
if (diff) return diff < 0 ? -1 : 1;
diff = int(m_unit) - int(f.m_unit);
if (diff) return diff < 0 ? -1 : 1;
diff = m_orig.cmpY(f.m_orig);
if (diff) return diff;
diff = m_size.cmpY(f.m_size);
if (diff) return diff;
diff = m_naturalSize.cmpY(f.m_naturalSize);
if (diff) return diff;
return 0;
}
int m_page;
Vec2f m_orig , m_size /* the size of the data*/, m_naturalSize ;
WPXUnit m_unit;
mutable int m_order;
};
#endif
/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */