Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
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 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_WW8_CP_AND_FC_HXX
21 : #define INCLUDED_WW8_CP_AND_FC_HXX
22 :
23 : #include <sal/types.h>
24 : #include <doctok/WW8Document.hxx>
25 : #include <set>
26 : #include <boost/unordered_map.hpp>
27 : #include <iostream>
28 :
29 : namespace writerfilter {
30 : namespace doctok
31 : {
32 : using namespace ::std;
33 :
34 : template <class T>
35 0 : bool operator <= (const T & rA, const T & rB)
36 : {
37 0 : return ! ( rB < rA );
38 : }
39 :
40 : /**
41 : A character position.
42 :
43 : This is a wrapper to make the type for WW8 character position (CP)
44 : distinct from WW8 file character positions (FC).
45 : */
46 : struct Cp
47 : {
48 : sal_uInt32 nCp; // the WW8 character position
49 :
50 0 : Cp() : nCp(0) {}
51 :
52 0 : Cp(sal_uInt32 nCp_) : nCp(nCp_) {}
53 :
54 : /**
55 : Returns the WW8 character position.
56 :
57 : @return the WW8 character position
58 : */
59 0 : sal_uInt32 get() const { return nCp; }
60 :
61 : /**
62 : Sets the WW8 character position.
63 :
64 : @param nCp_ the WW8 character position to set
65 : */
66 : void set(sal_uInt32 nCp_) { nCp = nCp_; }
67 :
68 : /**
69 : Calculate CP moved backward.
70 :
71 : None of the involved CPs is changed.
72 :
73 : @param n amount of movement
74 :
75 : @return CP moved @n steps backward
76 : */
77 0 : sal_uInt32 operator - (const Cp & rCp) const
78 0 : { return nCp - rCp.nCp; }
79 :
80 : /**
81 : Calculate CP moved forward.
82 :
83 : None of the involved CPs is changed.
84 :
85 : @param n amount of movement
86 :
87 : @return CP moved @n steps forward
88 : */
89 0 : Cp operator + (sal_uInt32 n) const
90 0 : { return Cp(nCp + n); }
91 :
92 : /**
93 : Advance CP forward.
94 :
95 : @attention The CP is altered.
96 :
97 : @param n amount of movement
98 :
99 : @return CP moved @n steps forward
100 : */
101 0 : Cp & operator += (sal_uInt32 n)
102 : {
103 0 : nCp += n;
104 :
105 0 : return *this;
106 : }
107 :
108 : /**
109 : Return string representation of CP.
110 : */
111 : string toString() const;
112 :
113 : friend bool operator < (const Cp & rA, const Cp & rB);
114 : friend bool operator == (const Cp & rA, const Cp & rB);
115 : friend ostream & operator << (ostream & o, const Cp & rCp);
116 : };
117 :
118 : /**
119 : A file character position.
120 :
121 : This is a wrapper to make the type for WW8 character position (CP)
122 : distinct from WW8 file character positions (FC).
123 :
124 : \see{Cp}
125 : */
126 : struct Fc
127 : {
128 : sal_uInt32 mnFc; // the WW8 character position
129 : bool mbComplex;
130 :
131 0 : Fc() : mnFc(0), mbComplex(false) {}
132 :
133 0 : Fc(sal_uInt32 nFc, bool bComplex = true)
134 0 : : mnFc(nFc), mbComplex(bComplex)
135 0 : {}
136 :
137 0 : sal_uInt32 complexFactor() const { return mbComplex ? 1 : 2; }
138 :
139 : /**
140 : Returns the WW8 character position.
141 :
142 : @return the WW8 character position
143 : */
144 0 : sal_uInt32 get() const { return mnFc; }
145 :
146 : /**
147 : Sets the WW8 file character position.
148 :
149 : @param nFc the WW8 file character position to set
150 : */
151 : void set(sal_uInt32 nFc) { mnFc = nFc; }
152 :
153 : /**
154 : Set if the FC is complex.
155 :
156 : @param bComplex true if FC is set to be complex
157 : */
158 0 : void setComplex(bool bComplex) { mbComplex = bComplex; }
159 :
160 : /**
161 : Return if FC is complex.
162 :
163 : @retval true FC is complex
164 : @retval false else
165 : */
166 0 : bool isComplex() const { return mbComplex; }
167 :
168 : /**
169 : Distance of FCs.
170 :
171 : None of the involved FCs is changed.
172 :
173 : @param rFc FC to calculate distance to
174 :
175 : @return Distance from @a rFc to this FC
176 : */
177 0 : sal_uInt32 operator - (const Fc & rFc) const
178 0 : { return (mnFc - rFc.mnFc) / complexFactor(); }
179 :
180 : /**
181 : Calculate FC moved backward.
182 :
183 : None of the involved FCs is changed.
184 :
185 : @param n amount of movement
186 :
187 : @return FC moved @n steps backward
188 : */
189 : Fc operator - (sal_uInt32 n) const
190 : { return Fc(mnFc - n * complexFactor(), mbComplex); }
191 :
192 : /**
193 : Calculate FC moved forward.
194 :
195 : None of the involved FCs is changed.
196 :
197 : @param n amount of movement
198 :
199 : @return FC moved @n steps Forward
200 : */
201 0 : Fc operator + (sal_uInt32 n) const
202 0 : { return Fc(mnFc + n * complexFactor(), mbComplex); }
203 :
204 : /**
205 : Return string representation of FC.
206 : */
207 : string toString() const;
208 :
209 : friend bool operator < (const Fc & rA, const Fc & rB);
210 : friend bool operator == (const Fc & rA, const Fc & rB);
211 : friend ostream & operator << (ostream & o, const Fc & rFc);
212 : };
213 :
214 : /**
215 : A character position and a corresponding file character position
216 : paired.
217 : */
218 : class CpAndFc
219 : {
220 : private:
221 : /**
222 : character position
223 : */
224 : Cp mCp;
225 :
226 : /**
227 : file character position
228 : */
229 : Fc mFc;
230 :
231 : /**
232 : property type
233 : */
234 : PropertyType mType;
235 :
236 : public:
237 0 : CpAndFc() {}
238 : CpAndFc(const Cp & rCp, const Fc & rFc, PropertyType eType_);
239 :
240 : /**
241 : Return character position.
242 : */
243 0 : const Cp & getCp() const { return mCp; }
244 :
245 : /**
246 : Return file character position.
247 : */
248 0 : const Fc & getFc() const { return mFc; }
249 :
250 : /**
251 : Return property type.
252 : */
253 0 : PropertyType getType() const { return mType; }
254 :
255 : /**
256 : Return if FC is complex.
257 :
258 : @retval true FC is complex
259 : @retval false else
260 : */
261 0 : bool isComplex() const { return mFc.isComplex(); }
262 :
263 : /**
264 : Return the distance to other CpAndFc.
265 :
266 : @param rCpAndFc the other CpAndFc
267 :
268 : @return the distance from the CP in @a rCpAndFc to the CP in
269 : CpAndFc.
270 : */
271 0 : sal_uInt32 operator-(const CpAndFc & rCpAndFc) const
272 0 : { return mCp - rCpAndFc.mCp; }
273 :
274 : /**
275 : Return string representation of the CpAndFc.
276 : */
277 : string toString() const;
278 :
279 : friend bool operator < (const CpAndFc & rA, const CpAndFc & rB);
280 : friend bool operator == (const CpAndFc & rA, const CpAndFc & rB);
281 : friend ostream & operator << (ostream & o, const CpAndFc & rCpAndFc);
282 : };
283 :
284 : struct CpAndFcLess
285 : {
286 0 : CpAndFcLess()
287 : {
288 0 : }
289 :
290 0 : bool operator()(const CpAndFc & rA, const CpAndFc & rB) const
291 : {
292 0 : return rA < rB;
293 : }
294 :
295 : bool operator()(const CpAndFc & rA, const Cp & rB) const
296 : {
297 : return rA.getCp() < rB;
298 : }
299 :
300 : bool operator()(const Cp & rA, const CpAndFc & rB) const
301 : {
302 : return rA < rB.getCp();
303 : }
304 : };
305 :
306 :
307 : typedef set<CpAndFc, CpAndFcLess> CpAndFcs;
308 :
309 : ostream & operator << (ostream & o, const CpAndFcs & rCpAndFcs);
310 :
311 : struct CpHash
312 : {
313 0 : size_t operator()(const Cp & rCp) const
314 : {
315 0 : return rCp.get();
316 : }
317 : };
318 :
319 : struct FcHash
320 : {
321 0 : size_t operator()(const Fc & rFc) const
322 : {
323 0 : return rFc.get();
324 : }
325 : };
326 :
327 : struct CpEq
328 : {
329 0 : bool operator() (const Cp & rA, const Cp &rB) const
330 : {
331 0 : return rA == rB;
332 : }
333 : };
334 :
335 : struct CpAndFcHash
336 : {
337 0 : size_t operator()(const CpAndFc & rCpAndFc) const
338 : {
339 : CpHash aHash;
340 :
341 0 : return aHash(rCpAndFc.getCp());
342 : }
343 : };
344 :
345 : typedef boost::unordered_map<Cp, Fc, CpHash, CpEq> Cp2FcHashMap_t;
346 :
347 : } // namespace doctok
348 : } // namespace writerfilter
349 :
350 : #endif // INCLUDED_WW8_CP_AND_FC_HXX
351 :
352 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|