/src/libmwaw/src/lib/MWAWPosition.hxx
Line | Count | Source |
1 | | /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */ |
2 | | |
3 | | /* libmwaw |
4 | | * Version: MPL 2.0 / LGPLv2+ |
5 | | * |
6 | | * The contents of this file are subject to the Mozilla Public License Version |
7 | | * 2.0 (the "License"); you may not use this file except in compliance with |
8 | | * the License or as specified alternatively below. You may obtain a copy of |
9 | | * the License at http://www.mozilla.org/MPL/ |
10 | | * |
11 | | * Software distributed under the License is distributed on an "AS IS" basis, |
12 | | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
13 | | * for the specific language governing rights and limitations under the |
14 | | * License. |
15 | | * |
16 | | * Major Contributor(s): |
17 | | * Copyright (C) 2002 William Lachance (wrlach@gmail.com) |
18 | | * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net) |
19 | | * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch) |
20 | | * Copyright (C) 2006, 2007 Andrew Ziem |
21 | | * Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr) |
22 | | * |
23 | | * |
24 | | * All Rights Reserved. |
25 | | * |
26 | | * For minor contributions see the git repository. |
27 | | * |
28 | | * Alternatively, the contents of this file may be used under the terms of |
29 | | * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"), |
30 | | * in which case the provisions of the LGPLv2+ are applicable |
31 | | * instead of those above. |
32 | | */ |
33 | | |
34 | | #ifndef MWAW_POSITION_H |
35 | | #define MWAW_POSITION_H |
36 | | |
37 | | #include <ostream> |
38 | | |
39 | | #include <librevenge/librevenge.h> |
40 | | |
41 | | #include "libmwaw_internal.hxx" |
42 | | |
43 | | /** Class to define the position of an object (textbox, picture, ..) in the document |
44 | | * |
45 | | * Stores the page, object position, object size, anchor, wrapping, ... |
46 | | */ |
47 | | class MWAWPosition |
48 | | { |
49 | | public: |
50 | | //! a list of enum used to defined the anchor |
51 | | enum AnchorTo { Char, CharBaseLine, Frame, Paragraph, Page, Cell, Unknown }; |
52 | | //! an enum used to define the wrapping: none, ... |
53 | | enum Wrapping { WNone, WBackground, WDynamic, WForeground, WParallel, WRunThrough }; |
54 | | //! an enum used to define the relative X position |
55 | | enum XPos { XRight, XLeft, XCenter, XFull }; |
56 | | //! an enum used to define the relative Y position |
57 | | enum YPos { YTop, YBottom, YCenter, YFull }; |
58 | | |
59 | | public: |
60 | | //! constructor |
61 | | explicit MWAWPosition(MWAWVec2f const &orig=MWAWVec2f(), MWAWVec2f const &sz=MWAWVec2f(), librevenge::RVNGUnit theUnit=librevenge::RVNG_INCH) |
62 | 205M | : m_anchorTo(Unknown) |
63 | 205M | , m_anchorCellName("") |
64 | 205M | , m_xPos(XLeft) |
65 | 205M | , m_yPos(YTop) |
66 | 205M | , m_wrapping(WNone) |
67 | 205M | , m_page(0) |
68 | 205M | , m_orig(orig) |
69 | 205M | , m_size(sz) |
70 | 205M | , m_naturalSize() |
71 | 205M | , m_LTClip() |
72 | 205M | , m_RBClip() |
73 | 205M | , m_unit(theUnit) |
74 | 205M | , m_order(0) |
75 | 205M | { |
76 | 205M | } |
77 | 216M | MWAWPosition(MWAWPosition const &)=default; |
78 | 3.55M | MWAWPosition &operator=(MWAWPosition const &)=default; |
79 | 85.6M | MWAWPosition &operator=(MWAWPosition &&)=default; |
80 | | //! destructor |
81 | | ~MWAWPosition(); |
82 | | //! operator<< |
83 | | friend std::ostream &operator<<(std::ostream &o, MWAWPosition const &pos) |
84 | 0 | { |
85 | 0 | MWAWVec2f dest(pos.m_orig+pos.m_size); |
86 | 0 | o << "Pos=(" << pos.m_orig << ")x(" << dest << ")"; |
87 | 0 | switch (pos.m_unit) { |
88 | 0 | case librevenge::RVNG_INCH: |
89 | 0 | o << "(inch)"; |
90 | 0 | break; |
91 | 0 | case librevenge::RVNG_POINT: |
92 | 0 | o << "(pt)"; |
93 | 0 | break; |
94 | 0 | case librevenge::RVNG_TWIP: |
95 | 0 | o << "(tw)"; |
96 | 0 | break; |
97 | 0 | case librevenge::RVNG_PERCENT: |
98 | 0 | case librevenge::RVNG_GENERIC: |
99 | 0 | case librevenge::RVNG_UNIT_ERROR: |
100 | | #if !defined(__clang__) |
101 | | default: |
102 | | #endif |
103 | 0 | break; |
104 | 0 | } |
105 | 0 | if (pos.page()>0) o << ", page=" << pos.page(); |
106 | 0 | return o; |
107 | 0 | } |
108 | | //! basic operator== |
109 | | bool operator==(MWAWPosition const &f) const |
110 | 0 | { |
111 | 0 | return cmp(f) == 0; |
112 | 0 | } |
113 | | //! basic operator!= |
114 | | bool operator!=(MWAWPosition const &f) const |
115 | 0 | { |
116 | 0 | return cmp(f) != 0; |
117 | 0 | } |
118 | | //! basic operator< |
119 | | bool operator<(MWAWPosition const &f) const |
120 | 0 | { |
121 | 0 | return cmp(f) < 0; |
122 | 0 | } |
123 | | |
124 | | //! returns the frame page |
125 | | int page() const |
126 | 13.2M | { |
127 | 13.2M | return m_page; |
128 | 13.2M | } |
129 | | //! return the frame origin |
130 | | MWAWVec2f const &origin() const |
131 | 72.2M | { |
132 | 72.2M | return m_orig; |
133 | 72.2M | } |
134 | | //! returns the frame size |
135 | | MWAWVec2f const &size() const |
136 | 99.4M | { |
137 | 99.4M | return m_size; |
138 | 99.4M | } |
139 | | //! returns the natural size (if known) |
140 | | MWAWVec2f const &naturalSize() const |
141 | 17.3M | { |
142 | 17.3M | return m_naturalSize; |
143 | 17.3M | } |
144 | | //! returns the left top clipping |
145 | | MWAWVec2f const &leftTopClipping() const |
146 | 17.0M | { |
147 | 17.0M | return m_LTClip; |
148 | 17.0M | } |
149 | | //! returns the right bottom clipping |
150 | | MWAWVec2f const &rightBottomClipping() const |
151 | 17.0M | { |
152 | 17.0M | return m_RBClip; |
153 | 17.0M | } |
154 | | //! returns the unit |
155 | | librevenge::RVNGUnit unit() const |
156 | 26.3M | { |
157 | 26.3M | return m_unit; |
158 | 26.3M | } |
159 | | static float getScaleFactor(librevenge::RVNGUnit orig, librevenge::RVNGUnit dest) |
160 | 71.8M | { |
161 | 71.8M | float actSc = 1.0, newSc = 1.0; |
162 | 71.8M | switch (orig) { |
163 | 0 | case librevenge::RVNG_TWIP: |
164 | 0 | break; |
165 | 46.1M | case librevenge::RVNG_POINT: |
166 | 46.1M | actSc=20; |
167 | 46.1M | break; |
168 | 25.6M | case librevenge::RVNG_INCH: |
169 | 25.6M | actSc = 1440; |
170 | 25.6M | break; |
171 | 0 | case librevenge::RVNG_PERCENT: |
172 | 0 | case librevenge::RVNG_GENERIC: |
173 | 0 | case librevenge::RVNG_UNIT_ERROR: |
174 | | #if !defined(__clang__) |
175 | | default: |
176 | | #endif |
177 | 0 | MWAW_DEBUG_MSG(("MWAWPosition::getScaleFactor %d unit must not appear\n", int(orig))); |
178 | 71.8M | } |
179 | 71.8M | switch (dest) { |
180 | 6.96M | case librevenge::RVNG_TWIP: |
181 | 6.96M | break; |
182 | 60.4M | case librevenge::RVNG_POINT: |
183 | 60.4M | newSc=20; |
184 | 60.4M | break; |
185 | 4.38M | case librevenge::RVNG_INCH: |
186 | 4.38M | newSc = 1440; |
187 | 4.38M | break; |
188 | 0 | case librevenge::RVNG_PERCENT: |
189 | 0 | case librevenge::RVNG_GENERIC: |
190 | 0 | case librevenge::RVNG_UNIT_ERROR: |
191 | | #if !defined(__clang__) |
192 | | default: |
193 | | #endif |
194 | 0 | MWAW_DEBUG_MSG(("MWAWPosition::getScaleFactor %d unit must not appear\n", int(dest))); |
195 | 71.8M | } |
196 | 71.8M | return actSc/newSc; |
197 | 71.8M | } |
198 | | //! returns a float which can be used to scale some data in object unit |
199 | | float getInvUnitScale(librevenge::RVNGUnit fromUnit) const |
200 | 51.3M | { |
201 | 51.3M | return getScaleFactor(fromUnit, m_unit); |
202 | 51.3M | } |
203 | | |
204 | | //! sets the page |
205 | | void setPage(int pg) const |
206 | 52.9M | { |
207 | 52.9M | const_cast<MWAWPosition *>(this)->m_page = pg; |
208 | 52.9M | } |
209 | | //! sets the frame origin |
210 | | void setOrigin(MWAWVec2f const &orig) |
211 | 63.7M | { |
212 | 63.7M | m_orig = orig; |
213 | 63.7M | } |
214 | | //! sets the frame size |
215 | | void setSize(MWAWVec2f const &sz) |
216 | 59.4M | { |
217 | 59.4M | m_size = sz; |
218 | 59.4M | } |
219 | | //! sets the natural size (if known) |
220 | | void setNaturalSize(MWAWVec2f const &naturalSz) |
221 | 426k | { |
222 | 426k | m_naturalSize = naturalSz; |
223 | 426k | } |
224 | | //! sets the dimension unit |
225 | | void setUnit(librevenge::RVNGUnit newUnit) |
226 | 51.7M | { |
227 | 51.7M | m_unit = newUnit; |
228 | 51.7M | } |
229 | | //! sets/resets the page and the origin |
230 | | void setPagePos(int pg, MWAWVec2f const &newOrig) const |
231 | 19.7M | { |
232 | 19.7M | const_cast<MWAWPosition *>(this)->m_page = pg; |
233 | 19.7M | const_cast<MWAWPosition *>(this)->m_orig = newOrig; |
234 | 19.7M | } |
235 | | |
236 | | //! sets the relative position |
237 | | void setRelativePosition(AnchorTo anchor, XPos X = XLeft, YPos Y=YTop) |
238 | 64.3M | { |
239 | 64.3M | m_anchorTo = anchor; |
240 | 64.3M | m_xPos = X; |
241 | 64.3M | m_yPos = Y; |
242 | 64.3M | } |
243 | | //! sets the anchor to a cell position |
244 | | void setAnchorToCell(librevenge::RVNGString const &cellName) |
245 | 2.22k | { |
246 | 2.22k | m_anchorTo = Cell; |
247 | 2.22k | m_xPos = XLeft; |
248 | 2.22k | m_yPos = YTop; |
249 | 2.22k | m_anchorCellName=cellName; |
250 | 2.22k | } |
251 | | //! sets the clipping position |
252 | | void setClippingPosition(MWAWVec2f lTop, MWAWVec2f rBottom) |
253 | 3.40k | { |
254 | 3.40k | m_LTClip = lTop; |
255 | 3.40k | m_RBClip = rBottom; |
256 | 3.40k | } |
257 | | |
258 | | //! returns background/foward order |
259 | | int order() const |
260 | 33.1M | { |
261 | 33.1M | return m_order; |
262 | 33.1M | } |
263 | | //! set background/foward order |
264 | | void setOrder(int ord) const |
265 | 72.4M | { |
266 | 72.4M | m_order = ord; |
267 | 72.4M | } |
268 | | |
269 | | //! anchor position |
270 | | AnchorTo m_anchorTo; |
271 | | //! the anchor cell name |
272 | | librevenge::RVNGString m_anchorCellName; |
273 | | //! X relative position |
274 | | XPos m_xPos; |
275 | | //! Y relative position |
276 | | YPos m_yPos; |
277 | | //! Wrapping |
278 | | Wrapping m_wrapping; |
279 | | |
280 | | protected: |
281 | | //! basic function to compare two positions |
282 | | int cmp(MWAWPosition const &f) const |
283 | 0 | { |
284 | 0 | int diff = int(m_anchorTo) - int(f.m_anchorTo); |
285 | 0 | if (diff) return diff < 0 ? -1 : 1; |
286 | 0 | if (m_anchorCellName<f.m_anchorCellName) return -1; |
287 | 0 | if (m_anchorCellName>f.m_anchorCellName) return 1; |
288 | 0 | diff = int(m_xPos) - int(f.m_xPos); |
289 | 0 | if (diff) return diff < 0 ? -1 : 1; |
290 | 0 | diff = int(m_yPos) - int(f.m_yPos); |
291 | 0 | if (diff) return diff < 0 ? -1 : 1; |
292 | 0 | diff = page() - f.page(); |
293 | 0 | if (diff) return diff < 0 ? -1 : 1; |
294 | 0 | diff = int(m_unit) - int(f.m_unit); |
295 | 0 | if (diff) return diff < 0 ? -1 : 1; |
296 | 0 | diff = m_orig.cmpY(f.m_orig); |
297 | 0 | if (diff) return diff; |
298 | 0 | diff = m_size.cmpY(f.m_size); |
299 | 0 | if (diff) return diff; |
300 | 0 | diff = m_naturalSize.cmpY(f.m_naturalSize); |
301 | 0 | if (diff) return diff; |
302 | 0 | diff = m_LTClip.cmpY(f.m_LTClip); |
303 | 0 | if (diff) return diff; |
304 | 0 | diff = m_RBClip.cmpY(f.m_RBClip); |
305 | 0 | if (diff) return diff; |
306 | | |
307 | 0 | return 0; |
308 | 0 | } |
309 | | |
310 | | //! the page |
311 | | int m_page; |
312 | | MWAWVec2f 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) */; |
313 | | MWAWVec2f m_LTClip /** the left top clip position */, m_RBClip /* the right bottom clip position */; |
314 | | //! the unit used in \a orig, in \a m_size and in \a m_LTClip , .... Default: in inches |
315 | | librevenge::RVNGUnit m_unit; |
316 | | //! background/foward order |
317 | | mutable int m_order; |
318 | | }; |
319 | | |
320 | | #endif |
321 | | // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |