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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 #include <modeltoviewhelper.hxx>
27 
28 namespace ModelToViewHelper
29 {
30 
31 /** Converts a model position into a view position
32 */
ConvertToViewPosition(const ConversionMap * pMap,sal_uInt32 nModelPos)33 sal_uInt32 ConvertToViewPosition( const ConversionMap* pMap, sal_uInt32 nModelPos )
34 {
35     sal_uInt32 nRet = nModelPos;
36 
37     if ( !pMap )
38         return nRet;
39 
40     // Search for entry behind nPos:
41     ConversionMap::const_iterator aIter;
42     for ( aIter = pMap->begin(); aIter != pMap->end(); ++aIter )
43     {
44         if ( (*aIter).first >= nModelPos )
45         {
46             const sal_uInt32 nPosModel  = (*aIter).first;
47             const sal_uInt32 nPosExpand = (*aIter).second;
48 
49             const sal_uInt32 nDistToNextModel  = nPosModel - nModelPos;
50             nRet = nPosExpand - nDistToNextModel;
51             break;
52         }
53     }
54 
55     return nRet;
56 }
57 
58 
59 /** Converts a view position into a model position
60 */
ConvertToModelPosition(const ConversionMap * pMap,sal_uInt32 nViewPos)61 ModelPosition ConvertToModelPosition( const ConversionMap* pMap, sal_uInt32 nViewPos )
62 {
63     ModelPosition aRet;
64     aRet.mnPos = nViewPos;
65 
66     if ( !pMap )
67         return aRet;
68 
69     // Search for entry behind nPos:
70     ConversionMap::const_iterator aIter;
71     for ( aIter = pMap->begin(); aIter != pMap->end(); ++aIter )
72     {
73         if ( (*aIter).second > nViewPos )
74         {
75             const sal_uInt32 nPosModel  = (*aIter).first;
76             const sal_uInt32 nPosExpand = (*aIter).second;
77 
78             // If nViewPos is in front of first field, we are finished.
79             if ( aIter == pMap->begin() )
80                 break;
81 
82             --aIter;
83 
84             // nPrevPosModel is the field position
85             const sal_uInt32 nPrevPosModel  = (*aIter).first;
86             const sal_uInt32 nPrevPosExpand = (*aIter).second;
87 
88             const sal_uInt32 nLengthModel  = nPosModel - nPrevPosModel;
89             const sal_uInt32 nLengthExpand = nPosExpand - nPrevPosExpand;
90 
91             const sal_uInt32 nFieldLengthExpand = nLengthExpand - nLengthModel + 1;
92             const sal_uInt32 nFieldEndExpand = nPrevPosExpand + nFieldLengthExpand;
93 
94             // Check if nPos is outside of field:
95             if ( nFieldEndExpand <= nViewPos )
96             {
97                 // nPos is outside of field:
98                 const sal_uInt32 nDistToField = nViewPos - nFieldEndExpand + 1;
99                 aRet.mnPos  = nPrevPosModel + nDistToField;
100             }
101             else
102             {
103                 // nViewPos is inside a field:
104                 aRet.mnPos = nPrevPosModel;
105                 aRet.mnSubPos = nViewPos - nPrevPosExpand;
106                 aRet.mbIsField = true;
107             }
108 
109             break;
110         }
111     }
112 
113     return aRet;
114 }
115 
116 } // namespace ModelToViewStringConverter end
117