merge the formfield patch from ooo-build
[ooovba.git] / basegfx / source / range / b2xrange.cxx
blob4a5d18d30cfcd92b1cb447eeb21bde53adb51dd6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: b2xrange.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basegfx.hxx"
34 #include <basegfx/range/b2drange.hxx>
35 #include <basegfx/range/b2irange.hxx>
36 #include <basegfx/range/b2ibox.hxx>
39 namespace basegfx
41 namespace
43 /** Generic implementation of the difference set computation
45 @tpl RangeType
46 Type to operate on. Must provide ValueType and TraitsType
47 nested types.
49 template< class RangeType > void doComputeSetDifference(
50 ::std::vector< RangeType >& o_rRanges,
51 const RangeType& a,
52 const RangeType& b )
54 o_rRanges.clear();
56 // special-casing the empty rect case (this will fail most
57 // of the times below, because of the DBL_MIN/MAX special
58 // values denoting emptyness in the rectangle.
59 if( a.isEmpty() )
61 o_rRanges.push_back( b );
62 return;
64 if( b.isEmpty() )
66 o_rRanges.push_back( a );
67 return;
70 const typename RangeType::ValueType ax(a.getMinX());
71 const typename RangeType::ValueType ay(a.getMinY());
72 const typename RangeType::TraitsType::DifferenceType aw(a.getWidth());
73 const typename RangeType::TraitsType::DifferenceType ah(a.getHeight());
74 const typename RangeType::ValueType bx(b.getMinX());
75 const typename RangeType::ValueType by(b.getMinY());
76 const typename RangeType::TraitsType::DifferenceType bw(b.getWidth());
77 const typename RangeType::TraitsType::DifferenceType bh(b.getHeight());
79 const typename RangeType::TraitsType::DifferenceType h0( (by > ay) ? by - ay : 0 );
80 const typename RangeType::TraitsType::DifferenceType h3( (by + bh < ay + ah) ? ay + ah - by - bh : 0 );
81 const typename RangeType::TraitsType::DifferenceType w1( (bx > ax) ? bx - ax : 0 );
82 const typename RangeType::TraitsType::DifferenceType w2( (ax + aw > bx + bw) ? ax + aw - bx - bw : 0 );
83 const typename RangeType::TraitsType::DifferenceType h12( (h0 + h3 < ah) ? ah - h0 - h3 : 0 );
85 // TODO(E2): Use numeric_cast instead of static_cast here,
86 // need range checks!
87 if (h0 > 0)
88 o_rRanges.push_back(
89 RangeType(ax,ay,
90 static_cast<typename RangeType::ValueType>(ax+aw),
91 static_cast<typename RangeType::ValueType>(ay+h0)) );
93 if (w1 > 0 && h12 > 0)
94 o_rRanges.push_back(
95 RangeType(ax,
96 static_cast<typename RangeType::ValueType>(ay+h0),
97 static_cast<typename RangeType::ValueType>(ax+w1),
98 static_cast<typename RangeType::ValueType>(ay+h0+h12)) );
100 if (w2 > 0 && h12 > 0)
101 o_rRanges.push_back(
102 RangeType(static_cast<typename RangeType::ValueType>(bx+bw),
103 static_cast<typename RangeType::ValueType>(ay+h0),
104 static_cast<typename RangeType::ValueType>(bx+bw+w2),
105 static_cast<typename RangeType::ValueType>(ay+h0+h12)) );
107 if (h3 > 0)
108 o_rRanges.push_back(
109 RangeType(ax,
110 static_cast<typename RangeType::ValueType>(ay+h0+h12),
111 static_cast<typename RangeType::ValueType>(ax+aw),
112 static_cast<typename RangeType::ValueType>(ay+h0+h12+h3)) );
116 ::std::vector< B2IRange >& computeSetDifference( ::std::vector< B2IRange >& o_rResult,
117 const B2IRange& rFirst,
118 const B2IRange& rSecond )
120 doComputeSetDifference( o_rResult, rFirst, rSecond );
122 return o_rResult;
125 ::std::vector< B2DRange >& computeSetDifference( ::std::vector< B2DRange >& o_rResult,
126 const B2DRange& rFirst,
127 const B2DRange& rSecond )
129 doComputeSetDifference( o_rResult, rFirst, rSecond );
131 return o_rResult;
134 ::std::vector< B2IBox >& computeSetDifference( ::std::vector< B2IBox >& o_rResult,
135 const B2IBox& rFirst,
136 const B2IBox& rSecond )
138 doComputeSetDifference( o_rResult, rFirst, rSecond );
140 return o_rResult;
143 } // end of namespace basegfx
145 // eof