Coverage Report

Created: 2026-09-06 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwps/src/lib/WPSPosition.h
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
/* libwps
3
 * Version: MPL 2.0 / LGPLv2.1+
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * Major Contributor(s):
10
 * Copyright (C) 2009, 2011 Alonso Laurent (alonso@loria.fr)
11
 * Copyright (C) 2006, 2007 Andrew Ziem
12
 * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
13
 * Copyright (C) 2004 Marc Maurer (uwog@uwog.net)
14
 * Copyright (C) 2003-2005 William Lachance (william.lachance@sympatico.ca)
15
 *
16
 * For minor contributions see the git repository.
17
 *
18
 * Alternatively, the contents of this file may be used under the terms
19
 * of the GNU Lesser General Public License Version 2.1 or later
20
 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
21
 * applicable instead of those above.
22
 *
23
 * For further information visit http://libwps.sourceforge.net
24
 */
25
26
#ifndef WPS_POSITION_H
27
#define WPS_POSITION_H
28
29
#include <ostream>
30
31
#include <librevenge/librevenge.h>
32
33
#include "libwps_internal.h"
34
35
/** Class to define the position of an object (textbox, picture, ..) in the document
36
 *
37
 * Stores the page, object position, object size, anchor, wrapping, ...
38
 */
39
class WPSPosition
40
{
41
public:
42
  //! a list of enum used to defined the anchor
43
  enum AnchorTo { Cell, Char, CharBaseLine, Paragraph, ParagraphContent, Page, PageContent };
44
  //! an enum used to define the wrapping
45
  enum Wrapping { WNone, WDynamic, WRunThrough }; // Add something for background ?
46
  //! an enum used to define the relative X position
47
  enum XPos { XRight, XLeft, XCenter, XFull };
48
  //! an enum used to define the relative Y position
49
  enum YPos { YTop, YBottom, YCenter, YFull };
50
51
public:
52
  //! constructor
53
  explicit WPSPosition(Vec2f const &orig=Vec2f(), Vec2f const &sz=Vec2f(), librevenge::RVNGUnit unt=librevenge::RVNG_INCH)
54
779k
    : m_anchorTo(Char)
55
779k
    , m_anchorCellName()
56
779k
    , m_xPos(XLeft)
57
779k
    , m_yPos(YTop)
58
779k
    , m_wrapping(WNone)
59
779k
    , m_page(0)
60
779k
    , m_orig(orig)
61
779k
    , m_size(sz)
62
779k
    , m_naturalSize()
63
779k
    , m_unit(unt)
64
779k
    , m_order(0) {}
65
1.20M
  WPSPosition(WPSPosition const &)=default;
66
  WPSPosition(WPSPosition &&)=default;
67
  WPSPosition &operator=(WPSPosition const &)=default;
68
45.8k
  WPSPosition &operator=(WPSPosition &&)=default;
69
  //! destructor
70
1.98M
  ~WPSPosition() {}
71
  //! operator<<
72
  friend std::ostream &operator<<(std::ostream &o, WPSPosition const &pos)
73
0
  {
74
0
    Vec2f dest(pos.m_orig+pos.m_size);
75
0
    o << "Pos=" << pos.m_orig << "x" << dest;
76
0
    switch (pos.m_unit)
77
0
    {
78
0
    case librevenge::RVNG_INCH:
79
0
      o << "(inch)";
80
0
      break;
81
0
    case librevenge::RVNG_POINT:
82
0
      o << "(pt)";
83
0
      break;
84
0
    case librevenge::RVNG_TWIP:
85
0
      o << "(tw)";
86
0
      break;
87
0
    case librevenge::RVNG_PERCENT:
88
0
    case librevenge::RVNG_GENERIC:
89
0
    case librevenge::RVNG_UNIT_ERROR:
90
0
    default:
91
0
      break;
92
0
    }
93
0
    if (pos.page()>0) o << ", page=" << pos.page();
94
0
    return o;
95
0
  }
96
  //! basic operator==
97
  bool operator==(WPSPosition const &f) const
98
0
  {
99
0
    return cmp(f) == 0;
100
0
  }
101
  //! basic operator!=
102
  bool operator!=(WPSPosition const &f) const
103
0
  {
104
0
    return cmp(f) != 0;
105
0
  }
106
  //! basic operator<
107
  bool operator<(WPSPosition const &f) const
108
0
  {
109
0
    return cmp(f) < 0;
110
0
  }
111
112
  //! returns the frame page
113
  int page() const
114
1.18M
  {
115
1.18M
    return m_page;
116
1.18M
  }
117
  //! return the frame origin
118
  Vec2f const &origin() const
119
666k
  {
120
666k
    return m_orig;
121
666k
  }
122
  //! returns the frame size
123
  Vec2f const &size() const
124
1.23M
  {
125
1.23M
    return m_size;
126
1.23M
  }
127
  //! returns the natural size (if known)
128
  Vec2f const &naturalSize() const
129
280k
  {
130
280k
    return m_naturalSize;
131
280k
  }
132
  //! returns the unit
133
  librevenge::RVNGUnit unit() const
134
382k
  {
135
382k
    return m_unit;
136
382k
  }
137
  //! returns a float which can be used to convert between to unit
138
  static float getScaleFactor(librevenge::RVNGUnit orig, librevenge::RVNGUnit dest)
139
675k
  {
140
675k
    float actSc = 1.0, newSc = 1.0;
141
675k
    switch (orig)
142
675k
    {
143
16.7k
    case librevenge::RVNG_TWIP:
144
16.7k
      break;
145
381k
    case librevenge::RVNG_POINT:
146
381k
      actSc=20;
147
381k
      break;
148
278k
    case librevenge::RVNG_INCH:
149
278k
      actSc = 1440;
150
278k
      break;
151
0
    case librevenge::RVNG_PERCENT:
152
0
    case librevenge::RVNG_GENERIC:
153
0
    case librevenge::RVNG_UNIT_ERROR:
154
0
    default:
155
0
      WPS_DEBUG_MSG(("WPSPosition::getScaleFactor %d unit must not appear\n", int(orig)));
156
675k
    }
157
675k
    switch (dest)
158
675k
    {
159
0
    case librevenge::RVNG_TWIP:
160
0
      break;
161
579k
    case librevenge::RVNG_POINT:
162
579k
      newSc=20;
163
579k
      break;
164
96.3k
    case librevenge::RVNG_INCH:
165
96.3k
      newSc = 1440;
166
96.3k
      break;
167
0
    case librevenge::RVNG_PERCENT:
168
0
    case librevenge::RVNG_GENERIC:
169
0
    case librevenge::RVNG_UNIT_ERROR:
170
0
    default:
171
0
      WPS_DEBUG_MSG(("WPSPosition::getScaleFactor %d unit must not appear\n", int(dest)));
172
675k
    }
173
675k
    return actSc/newSc;
174
675k
  }
175
  //! returns a float which can be used to scale some data in object unit
176
  float getInvUnitScale(librevenge::RVNGUnit unt) const
177
556k
  {
178
556k
    return getScaleFactor(unt, m_unit);
179
556k
  }
180
181
  //! sets the page
182
  void setPage(int pg) const
183
619k
  {
184
619k
    const_cast<WPSPosition *>(this)->m_page = pg;
185
619k
  }
186
  //! sets the frame origin
187
  void setOrigin(Vec2f const &orig)
188
565k
  {
189
565k
    m_orig = orig;
190
565k
  }
191
  //! sets the frame size
192
  void setSize(Vec2f const &sz)
193
493k
  {
194
493k
    m_size = sz;
195
493k
  }
196
  //! sets the natural size (if known)
197
  void setNaturalSize(Vec2f const &natSize)
198
2.19k
  {
199
2.19k
    m_naturalSize = natSize;
200
2.19k
  }
201
  //! sets the dimension unit
202
  void setUnit(librevenge::RVNGUnit unt)
203
347
  {
204
347
    m_unit = unt;
205
347
  }
206
  //! sets/resets the page and the origin
207
  void setPagePos(int pg, Vec2f const &newOrig) const
208
0
  {
209
0
    const_cast<WPSPosition *>(this)->m_page = pg;
210
0
    const_cast<WPSPosition *>(this)->m_orig = newOrig;
211
0
  }
212
213
  //! sets the relative position
214
  void setRelativePosition(AnchorTo anchor, XPos X = XLeft, YPos Y=YTop)
215
599k
  {
216
599k
    m_anchorTo = anchor;
217
599k
    m_xPos = X;
218
599k
    m_yPos = Y;
219
599k
  }
220
221
  //! sets the anchor to a cell position
222
  void setAnchorToCell(librevenge::RVNGString const &cellName)
223
0
  {
224
0
    m_anchorTo = Cell;
225
0
    m_xPos = XLeft;
226
0
    m_yPos = YTop;
227
0
    m_anchorCellName=cellName;
228
0
  }
229
  //! returns background/foward order
230
  int order() const
231
0
  {
232
0
    return m_order;
233
0
  }
234
  //! set background/foward order
235
  void setOrder(int ord) const
236
0
  {
237
0
    m_order = ord;
238
0
  }
239
240
  //! anchor position
241
  AnchorTo m_anchorTo;
242
  //! the anchor cell name
243
  librevenge::RVNGString m_anchorCellName;
244
  //! X relative position
245
  XPos m_xPos;
246
  //! Y relative position
247
  YPos m_yPos;
248
  //! Wrapping
249
  Wrapping m_wrapping;
250
251
protected:
252
  //! basic function to compare two positions
253
  int cmp(WPSPosition const &f) const
254
0
  {
255
0
    int diff = int(m_anchorTo) - int(f.m_anchorTo);
256
0
    if (diff) return diff < 0 ? -1 : 1;
257
0
    if (m_anchorCellName<f.m_anchorCellName) return -1;
258
0
    if (m_anchorCellName>f.m_anchorCellName) return 1;
259
0
    diff = int(m_xPos) - int(f.m_xPos);
260
0
    if (diff) return diff < 0 ? -1 : 1;
261
0
    diff = int(m_yPos) - int(f.m_yPos);
262
0
    if (diff) return diff < 0 ? -1 : 1;
263
0
    diff = page() - f.page();
264
0
    if (diff) return diff < 0 ? -1 : 1;
265
0
    diff = int(m_unit) - int(f.m_unit);
266
0
    if (diff) return diff < 0 ? -1 : 1;
267
0
    diff = m_orig.cmpY(f.m_orig);
268
0
    if (diff) return diff;
269
0
    diff = m_size.cmpY(f.m_size);
270
0
    if (diff) return diff;
271
0
    diff = m_naturalSize.cmpY(f.m_naturalSize);
272
0
    if (diff) return diff;
273
0
274
0
    return 0;
275
0
  }
276
277
  //! the page
278
  int m_page;
279
  Vec2f m_orig /** the origin position in a page */, m_size /* the size of the data*/, m_naturalSize /** the natural size of the data (if known) */;
280
  //! the unit used in \a orig and in \a m_size. Default: in inches
281
  librevenge::RVNGUnit m_unit;
282
  //! background/foward order
283
  mutable int m_order;
284
};
285
286
#endif
287
/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */