1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "swrect.hxx"
#include "swregion.hxx"
#include "swtypes.hxx"
SwRegionRects::SwRegionRects( const SwRect &rStartRect, sal_uInt16 nInit ) :
SwRects(),
aOrigin( rStartRect )
{
reserve(nInit);
push_back( aOrigin );
}
// If <rDel> is sal_True then this Rect will be overwritten by <rRect> at
// position <nPos>. Otherwise <rRect> is attached at the end.
inline void SwRegionRects::InsertRect( const SwRect &rRect,
const sal_uInt16 nPos, bool &rDel )
{
if( rDel )
{
(*this)[nPos] = rRect;
rDel = false;
}
else
{
push_back( rRect );
}
}
/** Delete all overlaps of the Rects in array with the given <rRect>
To do so, all existing rectangles have to be either split or deleted.
@param rRect rectangle with the area that should be deleted
*/
void SwRegionRects::operator-=( const SwRect &rRect )
{
sal_uInt16 nMax = size();
for ( sal_uInt16 i = 0; i < nMax; ++i )
{
if ( rRect.IsOver( (*this)[i] ) )
{
SwRect aTmp( (*this)[i] );
SwRect aInter( aTmp );
aInter._Intersection( rRect );
// The first Rect that should be inserted takes position of i.
// This avoids one Delete() call.
bool bDel = true;
// now split; only those rectangles should be left over that are in
// the "old" but not in the "new" area; hence, not in intersection.
long nTmp;
if ( 0 < (nTmp = aInter.Top() - aTmp.Top()) )
{
const long nOldVal = aTmp.Height();
aTmp.Height(nTmp);
InsertRect( aTmp, i, bDel );
aTmp.Height( nOldVal );
}
aTmp.Top( aInter.Top() + aInter.Height() );
if ( aTmp.Height() > 0 )
InsertRect( aTmp, i, bDel );
aTmp.Top( aInter.Top() );
aTmp.Bottom( aInter.Bottom() );
if ( 0 < (nTmp = aInter.Left() - aTmp.Left()) )
{
const long nOldVal = aTmp.Width();
aTmp.Width( nTmp );
InsertRect( aTmp, i, bDel );
aTmp.Width( nOldVal );
}
aTmp.Left( aInter.Left() + aInter.Width() ); //+1?
if ( aTmp.Width() > 0 )
InsertRect( aTmp, i, bDel );
if( bDel )
{
erase( begin() + i );
--i; // so that we don't forget any
--nMax; // so that we don't check too much
}
}
}
}
/** invert current rectangle
Change the shape, such that holes with be areas and areas are holes now.
Note: If no rects were removed, then the shape is identical to the original
shape. As a result, it will be a NULL-SRectangle after inverting.
*/
void SwRegionRects::Invert()
{
// not very elegant and fast, but efficient:
// Create a new region and remove all areas that are left over. Afterwards
// copy all values.
// To avoid unnecessary memory requirements, create a "useful" initial size:
// Number of rectangles in this area * 2 + 2 for the special case of a
// single hole (so four Rects in the inverse case).
SwRegionRects aInvRegion( aOrigin, size()*2+2 );
for( const_iterator it = begin(); it != end(); ++it )
aInvRegion -= *it;
// overwrite all existing
swap( aInvRegion );
}
inline SwTwips CalcArea( const SwRect &rRect )
{
return rRect.Width() * rRect.Height();
}
// combine all adjacent rectangles
void SwRegionRects::Compress( bool bFuzzy )
{
for ( size_type i = 0; i < size(); ++i )
{
for ( size_type j = i+1; j < size(); ++j )
{
// If one rectangle contains a second completely than the latter
// does not need to be stored and can be deleted
if ( (*this)[i].IsInside( (*this)[j] ) )
{
erase( begin() + j );
--j;
}
else if ( (*this)[j].IsInside( (*this)[i] ) )
{
(*this)[i] = (*this)[j];
erase( begin() + j );
i = -1;
break;
}
else
{
// If two rectangles have the same area of their union minus the
// intersection then one of them can be deleted.
// For combining as much as possible (and for having less single
// paints), the area of the union can be a little bit larger:
// ( 9622 * 141.5 = 1361513 ~= a quarter (1/4) centimeter wider
// than the width of a A4 page
const long nFuzzy = bFuzzy ? 1361513 : 0;
SwRect aUnion( (*this)[i] );
aUnion.Union( (*this)[j] );
SwRect aInter( (*this)[i] );
aInter.Intersection( (*this)[j] );
if ( (::CalcArea( (*this)[i] ) +
::CalcArea( (*this)[j] ) + nFuzzy) >=
(::CalcArea( aUnion ) - CalcArea( aInter )) )
{
(*this)[i] = aUnion;
erase( begin() + j );
i = -1;
break;
}
}
}
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|