1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_WW8_CP_AND_FC_HXX
25 #define INCLUDED_WW8_CP_AND_FC_HXX
26 
27 #include <sal/types.h>
28 #ifndef INCLUDED_DOCUMENT_HXX
29 #include <doctok/WW8Document.hxx>
30 #endif
31 #include <set>
32 #include <hash_map>
33 #include <iostream>
34 
35 namespace writerfilter {
36 namespace doctok
37 {
38 using namespace ::std;
39 
40 template <class T>
operator <=(const T & rA,const T & rB)41 bool operator <= (const T & rA, const T & rB)
42 {
43     return ! ( rB < rA );
44 }
45 
46 /**
47    A character position.
48 
49    This is a wrapper to make the type for WW8 character position (CP)
50    distinct from WW8 file character positions (FC).
51 */
52 struct Cp
53 {
54     sal_uInt32 nCp; // the WW8 character position
55 
Cpwriterfilter::doctok::Cp56     Cp() : nCp(0) {}
57 
Cpwriterfilter::doctok::Cp58     Cp(sal_uInt32 nCp_) : nCp(nCp_) {}
59 
60     /**
61        Returns the WW8 character position.
62 
63        @return the WW8 character position
64     */
getwriterfilter::doctok::Cp65     sal_uInt32 get() const { return nCp; }
66 
67     /**
68        Sets the WW8 character position.
69 
70        @param nCp_    the WW8 character position to set
71     */
setwriterfilter::doctok::Cp72     void set(sal_uInt32 nCp_) { nCp = nCp_; }
73 
74     /**
75        Calculate CP moved backward.
76 
77        None of the involved CPs is changed.
78 
79        @param n     amount of movement
80 
81        @return CP moved @n steps backward
82      */
operator -writerfilter::doctok::Cp83     sal_uInt32 operator - (const Cp & rCp) const
84     { return nCp - rCp.nCp; }
85 
86     /**
87        Calculate CP moved forward.
88 
89        None of the involved CPs is changed.
90 
91        @param n     amount of movement
92 
93        @return CP moved @n steps forward
94      */
operator +writerfilter::doctok::Cp95     Cp operator + (sal_uInt32 n) const
96     { return Cp(nCp + n); }
97 
98     /**
99        Advance CP forward.
100 
101        @attention The CP is altered.
102 
103        @param n      amount of movement
104 
105        @return  CP moved @n steps forward
106      */
operator +=writerfilter::doctok::Cp107     Cp & operator += (sal_uInt32 n)
108     {
109         nCp += n;
110 
111         return *this;
112     }
113 
114     /**
115        Return string representation of CP.
116      */
117     string toString() const;
118 
119     friend bool operator < (const Cp & rA, const Cp & rB);
120     friend bool operator == (const Cp & rA, const Cp & rB);
121     friend ostream & operator << (ostream & o, const Cp & rCp);
122 };
123 
124 /**
125    A file character position.
126 
127    This is a wrapper to make the type for WW8 character position (CP)
128    distinct from WW8 file character positions (FC).
129 
130    \see{Cp}
131 */
132 struct Fc
133 {
134     sal_uInt32 mnFc; // the WW8 character position
135     bool mbComplex;
136 
Fcwriterfilter::doctok::Fc137     Fc() : mnFc(0), mbComplex(false) {}
138 
Fcwriterfilter::doctok::Fc139     Fc(sal_uInt32 nFc, bool bComplex = true)
140     : mnFc(nFc), mbComplex(bComplex)
141     {}
142 
complexFactorwriterfilter::doctok::Fc143     sal_uInt32 complexFactor() const { return mbComplex ? 1 : 2; }
144 
145     /**
146        Returns the WW8 character position.
147 
148        @return the WW8 character position
149     */
getwriterfilter::doctok::Fc150     sal_uInt32 get() const { return mnFc; }
151 
152     /**
153        Sets the WW8 file character position.
154 
155        @param nFc    the WW8 file character position to set
156     */
setwriterfilter::doctok::Fc157     void set(sal_uInt32 nFc) { mnFc = nFc; }
158 
159     /**
160        Set if the FC is complex.
161 
162        @param bComplex      true if FC is set to be complex
163      */
setComplexwriterfilter::doctok::Fc164     void setComplex(bool bComplex) { mbComplex = bComplex; }
165 
166     /**
167        Return if FC is complex.
168 
169        @retval true   FC is complex
170        @retval false  else
171      */
isComplexwriterfilter::doctok::Fc172     bool isComplex() const { return mbComplex; }
173 
174     /**
175        Distance of FCs.
176 
177        None of the involved FCs is changed.
178 
179        @param  rFc      FC to calculate distance to
180 
181        @return Distance from @a rFc to this FC
182      */
operator -writerfilter::doctok::Fc183     sal_uInt32 operator - (const Fc & rFc) const
184     { return (mnFc - rFc.mnFc) / complexFactor(); }
185 
186     /**
187        Calculate FC moved backward.
188 
189        None of the involved FCs is changed.
190 
191        @param n     amount of movement
192 
193        @return FC moved @n steps backward
194      */
operator -writerfilter::doctok::Fc195     Fc operator - (sal_uInt32 n) const
196     { return Fc(mnFc - n * complexFactor(), mbComplex); }
197 
198     /**
199        Calculate FC moved forward.
200 
201        None of the involved FCs is changed.
202 
203        @param n     amount of movement
204 
205        @return FC moved @n steps Forward
206      */
operator +writerfilter::doctok::Fc207     Fc operator + (sal_uInt32 n) const
208     { return Fc(mnFc + n * complexFactor(), mbComplex); }
209 
210     /**
211        Return string representation of FC.
212      */
213     string toString() const;
214 
215     friend bool operator < (const Fc & rA, const Fc & rB);
216     friend bool operator == (const Fc & rA, const Fc & rB);
217     friend ostream & operator << (ostream & o, const Fc & rFc);
218 };
219 
220 /**
221    A character position and a corresponding file character position
222    paired.
223  */
224 class CpAndFc
225 {
226 private:
227     /**
228        character position
229     */
230     Cp mCp;
231 
232     /**
233        file character position
234     */
235     Fc mFc;
236 
237     /**
238        property type
239     */
240     PropertyType mType;
241 
242 public:
CpAndFc()243     CpAndFc() {}
244     CpAndFc(const Cp & rCp, const Fc & rFc, PropertyType eType_);
245 
246     /**
247        Return character position.
248     */
getCp() const249     const Cp & getCp() const { return mCp; }
250 
251     /**
252        Return file character position.
253     */
getFc() const254     const Fc & getFc() const { return mFc; }
255 
256     /**
257        Return property type.
258     */
getType() const259     PropertyType getType() const { return mType; }
260 
261     /**
262        Return if FC is complex.
263 
264        @retval true    FC is complex
265        @retval false   else
266      */
isComplex() const267     bool isComplex() const { return mFc.isComplex(); }
268 
269     /**
270        Return the distance to other CpAndFc.
271 
272        @param  rCpAndFc    the other CpAndFc
273 
274        @return the distance from the CP in @a rCpAndFc to the CP in
275        CpAndFc.
276      */
operator -(const CpAndFc & rCpAndFc) const277     sal_uInt32 operator-(const CpAndFc & rCpAndFc) const
278     { return mCp - rCpAndFc.mCp; }
279 
280     /**
281        Return string representation of the CpAndFc.
282     */
283     string toString() const;
284 
285     friend bool operator < (const CpAndFc & rA, const CpAndFc & rB);
286     friend bool operator == (const CpAndFc & rA, const CpAndFc & rB);
287     friend ostream & operator << (ostream & o, const CpAndFc & rCpAndFc);
288 };
289 
290 struct CpAndFcLess
291 {
CpAndFcLesswriterfilter::doctok::CpAndFcLess292     CpAndFcLess()
293     {
294     }
295 
operator ()writerfilter::doctok::CpAndFcLess296     bool operator()(const CpAndFc & rA, const CpAndFc & rB) const
297     {
298         return rA < rB;
299     }
300 
operator ()writerfilter::doctok::CpAndFcLess301     bool operator()(const CpAndFc & rA, const Cp & rB) const
302     {
303         return rA.getCp() < rB;
304     }
305 
operator ()writerfilter::doctok::CpAndFcLess306     bool operator()(const Cp & rA, const CpAndFc & rB) const
307     {
308         return rA < rB.getCp();
309     }
310 };
311 
312 
313 typedef set<CpAndFc, CpAndFcLess> CpAndFcs;
314 
315 ostream & operator << (ostream & o, const CpAndFcs & rCpAndFcs);
316 
317 struct CpHash
318 {
operator ()writerfilter::doctok::CpHash319     size_t operator()(const Cp & rCp) const
320     {
321         return rCp.get();
322     }
323 };
324 
325 struct FcHash
326 {
operator ()writerfilter::doctok::FcHash327     size_t operator()(const Fc & rFc) const
328     {
329         return rFc.get();
330     }
331 };
332 
333 struct CpEq
334 {
operator ()writerfilter::doctok::CpEq335     bool operator() (const Cp & rA, const Cp &rB) const
336     {
337         return rA == rB;
338     }
339 };
340 
341 struct CpAndFcHash
342 {
operator ()writerfilter::doctok::CpAndFcHash343     size_t operator()(const CpAndFc & rCpAndFc) const
344     {
345         CpHash aHash;
346 
347         return aHash(rCpAndFc.getCp());
348     }
349 };
350 
351 typedef hash_map<Cp, Fc, CpHash, CpEq> Cp2FcHashMap_t;
352 
353 } // namespace doctok
354 } // namespace writerfilter
355 
356 #endif // INCLUDED_WW8_CP_AND_FC_HXX
357