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