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 : #include "swrect.hxx"
21 : #include "swregion.hxx"
22 : #include "swtypes.hxx"
23 :
24 46190 : SwRegionRects::SwRegionRects( const SwRect &rStartRect, sal_uInt16 nInit ) :
25 : SwRects(),
26 46190 : aOrigin( rStartRect )
27 : {
28 46190 : reserve(nInit);
29 46190 : push_back( aOrigin );
30 46190 : }
31 :
32 : // If <rDel> is true then this Rect will be overwritten by <rRect> at
33 : // position <nPos>. Otherwise <rRect> is attached at the end.
34 47901 : inline void SwRegionRects::InsertRect( const SwRect &rRect,
35 : const sal_uInt16 nPos, bool &rDel )
36 : {
37 47901 : if( rDel )
38 : {
39 27978 : (*this)[nPos] = rRect;
40 27978 : rDel = false;
41 : }
42 : else
43 : {
44 19923 : push_back( rRect );
45 : }
46 47901 : }
47 :
48 0 : void SwRegionRects::operator+=( const SwRect &rRect )
49 : {
50 0 : bool f = false;
51 0 : InsertRect( rRect, 0, f );
52 0 : }
53 :
54 : /** Delete all overlaps of the Rects in array with the given <rRect>
55 :
56 : To do so, all existing rectangles have to be either split or deleted.
57 :
58 : @param rRect rectangle with the area that should be deleted
59 : */
60 51900 : void SwRegionRects::operator-=( const SwRect &rRect )
61 : {
62 51900 : sal_uInt16 nMax = size();
63 197620 : for ( sal_uInt16 i = 0; i < nMax; ++i )
64 : {
65 145720 : if ( rRect.IsOver( (*this)[i] ) )
66 : {
67 36429 : SwRect aTmp( (*this)[i] );
68 36429 : SwRect aInter( aTmp );
69 36429 : aInter._Intersection( rRect );
70 :
71 : // The first Rect that should be inserted takes position of i.
72 : // This avoids one Delete() call.
73 36429 : bool bDel = true;
74 :
75 : // now split; only those rectangles should be left over that are in
76 : // the "old" but not in the "new" area; hence, not in intersection.
77 : long nTmp;
78 36429 : if ( 0 < (nTmp = aInter.Top() - aTmp.Top()) )
79 : {
80 12356 : const long nOldVal = aTmp.Height();
81 12356 : aTmp.Height(nTmp);
82 12356 : InsertRect( aTmp, i, bDel );
83 12356 : aTmp.Height( nOldVal );
84 : }
85 :
86 36429 : aTmp.Top( aInter.Top() + aInter.Height() );
87 36429 : if ( aTmp.Height() > 0 )
88 11837 : InsertRect( aTmp, i, bDel );
89 :
90 36429 : aTmp.Top( aInter.Top() );
91 36429 : aTmp.Bottom( aInter.Bottom() );
92 36429 : if ( 0 < (nTmp = aInter.Left() - aTmp.Left()) )
93 : {
94 12942 : const long nOldVal = aTmp.Width();
95 12942 : aTmp.Width( nTmp );
96 12942 : InsertRect( aTmp, i, bDel );
97 12942 : aTmp.Width( nOldVal );
98 : }
99 :
100 36429 : aTmp.Left( aInter.Left() + aInter.Width() ); //+1?
101 36429 : if ( aTmp.Width() > 0 )
102 10766 : InsertRect( aTmp, i, bDel );
103 :
104 36429 : if( bDel )
105 : {
106 8451 : erase( begin() + i );
107 8451 : --i; // so that we don't forget any
108 8451 : --nMax; // so that we don't check too much
109 : }
110 : }
111 : }
112 51900 : }
113 :
114 : /** invert current rectangle
115 :
116 : Change the shape, such that holes with be areas and areas are holes now.
117 :
118 : Note: If no rects were removed, then the shape is identical to the original
119 : shape. As a result, it will be a NULL-SRectangle after inverting.
120 : */
121 4827 : void SwRegionRects::Invert()
122 : {
123 : // not very elegant and fast, but efficient:
124 : // Create a new region and remove all areas that are left over. Afterwards
125 : // copy all values.
126 :
127 : // To avoid unnecessary memory requirements, create a "useful" initial size:
128 : // Number of rectangles in this area * 2 + 2 for the special case of a
129 : // single hole (so four Rects in the inverse case).
130 4827 : SwRegionRects aInvRegion( aOrigin, size()*2+2 );
131 21361 : for( const_iterator it = begin(); it != end(); ++it )
132 16534 : aInvRegion -= *it;
133 :
134 : // overwrite all existing
135 4827 : swap( aInvRegion );
136 4827 : }
137 :
138 29348 : inline SwTwips CalcArea( const SwRect &rRect )
139 : {
140 29348 : return rRect.Width() * rRect.Height();
141 : }
142 :
143 : // combine all adjacent rectangles
144 3819 : void SwRegionRects::Compress( bool bFuzzy )
145 : {
146 14349 : for (size_type i = 0; i < size(); )
147 : {
148 6711 : bool bRestart(false);
149 12565 : for ( size_type j = i+1; j < size(); ++j )
150 : {
151 : // If one rectangle contains a second completely than the latter
152 : // does not need to be stored and can be deleted
153 7603 : if ( (*this)[i].IsInside( (*this)[j] ) )
154 : {
155 266 : erase( begin() + j );
156 266 : --j;
157 : }
158 7337 : else if ( (*this)[j].IsInside( (*this)[i] ) )
159 : {
160 0 : (*this)[i] = (*this)[j];
161 0 : erase( begin() + j );
162 0 : bRestart = true;
163 0 : break;
164 : }
165 : else
166 : {
167 : // If two rectangles have the same area of their union minus the
168 : // intersection then one of them can be deleted.
169 : // For combining as much as possible (and for having less single
170 : // paints), the area of the union can be a little bit larger:
171 : // ( 9622 * 141.5 = 1361513 ~= a quarter (1/4) centimeter wider
172 : // than the width of a A4 page
173 7337 : const long nFuzzy = bFuzzy ? 1361513 : 0;
174 7337 : SwRect aUnion( (*this)[i] );
175 7337 : aUnion.Union( (*this)[j] );
176 7337 : SwRect aInter( (*this)[i] );
177 7337 : aInter.Intersection( (*this)[j] );
178 22011 : if ( (::CalcArea( (*this)[i] ) +
179 14674 : ::CalcArea( (*this)[j] ) + nFuzzy) >=
180 7337 : (::CalcArea( aUnion ) - CalcArea( aInter )) )
181 : {
182 1749 : (*this)[i] = aUnion;
183 1749 : erase( begin() + j );
184 1749 : bRestart = true;
185 1749 : break;
186 : }
187 : }
188 : }
189 6711 : i = (bRestart) ? 0 : i+1;
190 : }
191 3819 : }
192 :
193 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|