xref: /aoo41x/main/basegfx/test/clipstate.cxx (revision 1f882ec4)
109dbbe93SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
309dbbe93SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
409dbbe93SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
509dbbe93SAndrew Rist  * distributed with this work for additional information
609dbbe93SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
709dbbe93SAndrew Rist  * to you under the Apache License, Version 2.0 (the
809dbbe93SAndrew Rist  * "License"); you may not use this file except in compliance
909dbbe93SAndrew Rist  * with the License.  You may obtain a copy of the License at
1009dbbe93SAndrew Rist  *
1109dbbe93SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1209dbbe93SAndrew Rist  *
1309dbbe93SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1409dbbe93SAndrew Rist  * software distributed under the License is distributed on an
1509dbbe93SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1609dbbe93SAndrew Rist  * KIND, either express or implied.  See the License for the
1709dbbe93SAndrew Rist  * specific language governing permissions and limitations
1809dbbe93SAndrew Rist  * under the License.
1909dbbe93SAndrew Rist  *
2009dbbe93SAndrew Rist  *************************************************************/
2109dbbe93SAndrew Rist 
2209dbbe93SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
26cdf0e10cSrcweir #include "precompiled_basegfx.hxx"
27cdf0e10cSrcweir // autogenerated file with codegen.pl
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "preextstl.h"
30cdf0e10cSrcweir #include "cppunit/TestAssert.h"
31cdf0e10cSrcweir #include "cppunit/TestFixture.h"
32cdf0e10cSrcweir #include "cppunit/extensions/HelperMacros.h"
33cdf0e10cSrcweir #include "postextstl.h"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <basegfx/tools/b2dclipstate.hxx>
36cdf0e10cSrcweir #include <basegfx/range/b2dpolyrange.hxx>
37cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
38cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygontools.hxx>
39cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygontools.hxx>
40cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygon.hxx>
41cdf0e10cSrcweir #include <basegfx/numeric/ftools.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <boost/bind.hpp>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir using namespace ::basegfx;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir namespace basegfx2d
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 
51cdf0e10cSrcweir class clipstate : public CppUnit::TestFixture
52cdf0e10cSrcweir {
53cdf0e10cSrcweir private:
54cdf0e10cSrcweir     tools::B2DClipState aUnion1;
55cdf0e10cSrcweir     tools::B2DClipState aUnion2;
56cdf0e10cSrcweir     tools::B2DClipState aIntersect;
57cdf0e10cSrcweir     tools::B2DClipState aXor;
58cdf0e10cSrcweir     tools::B2DClipState aSubtract;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir public:
setUp()61cdf0e10cSrcweir     void setUp()
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         B2DRange aCenter(100, 100, -100, -100);
64cdf0e10cSrcweir         B2DRange aNorth(-10, -110, 10, -90);
65cdf0e10cSrcweir         B2DRange aWest(-110, -10, -90, 10);
66cdf0e10cSrcweir         B2DRange aSouth(-10, 110, 10, 90);
67cdf0e10cSrcweir         B2DRange aEast(110, -10, 90, 10);
68cdf0e10cSrcweir 
69cdf0e10cSrcweir         aUnion1.unionRange(aCenter);
70cdf0e10cSrcweir         aUnion1.unionRange(aNorth);
71cdf0e10cSrcweir         aUnion1.unionRange(aWest);
72cdf0e10cSrcweir         aUnion1.unionRange(aSouth);
73cdf0e10cSrcweir         aUnion1.unionRange(aEast);
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         aUnion2.makeNull();
76cdf0e10cSrcweir         aUnion2.unionRange(aCenter);
77cdf0e10cSrcweir         aUnion2.unionRange(aNorth);
78cdf0e10cSrcweir         aUnion2.unionRange(aWest);
79cdf0e10cSrcweir         aUnion2.unionRange(aSouth);
80cdf0e10cSrcweir         aUnion2.unionRange(aEast);
81cdf0e10cSrcweir 
82cdf0e10cSrcweir         aIntersect.intersectRange(aCenter);
83cdf0e10cSrcweir         aIntersect.intersectRange(aNorth);
84cdf0e10cSrcweir         aIntersect.intersectRange(aWest);
85cdf0e10cSrcweir         aIntersect.intersectRange(aSouth);
86cdf0e10cSrcweir         aIntersect.intersectRange(aEast);
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         aXor.makeNull();
89cdf0e10cSrcweir         aXor.xorRange(aCenter);
90cdf0e10cSrcweir         aXor.xorRange(aNorth);
91cdf0e10cSrcweir         aXor.xorRange(aWest);
92cdf0e10cSrcweir         aXor.xorRange(aSouth);
93cdf0e10cSrcweir         aXor.xorRange(aEast);
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         aSubtract.intersectRange(aCenter);
96cdf0e10cSrcweir         aSubtract.subtractRange(aNorth);
97cdf0e10cSrcweir         aSubtract.subtractRange(aWest);
98cdf0e10cSrcweir         aSubtract.subtractRange(aSouth);
99cdf0e10cSrcweir         aSubtract.subtractRange(aEast);
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 
tearDown()102cdf0e10cSrcweir     void tearDown()
103cdf0e10cSrcweir     {}
104cdf0e10cSrcweir 
verifyPoly(const char * sName,const char * sSvg,const tools::B2DClipState & toTest)105cdf0e10cSrcweir     void verifyPoly(const char* sName, const char* sSvg, const tools::B2DClipState& toTest)
106cdf0e10cSrcweir     {
107cdf0e10cSrcweir #if defined(VERBOSE)
108cdf0e10cSrcweir         fprintf(stderr, "%s - svg:d=\"%s\"\n",
109cdf0e10cSrcweir                 sName, rtl::OUStringToOString(
110*1f882ec4SArmin Le Grand                     basegfx::tools::exportToSvgD(toTest.getClipPoly(), true, true, false),
111cdf0e10cSrcweir                     RTL_TEXTENCODING_UTF8).getStr() );
112cdf0e10cSrcweir #endif
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         B2DPolyPolygon aTmp1;
115cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(sName,
116cdf0e10cSrcweir                                tools::importFromSvgD(
117*1f882ec4SArmin Le Grand                                    aTmp1, rtl::OUString::createFromAscii(sSvg), false, 0));
118cdf0e10cSrcweir 
119cdf0e10cSrcweir         const rtl::OUString aSvg=
120*1f882ec4SArmin Le Grand             tools::exportToSvgD(toTest.getClipPoly(), true, true, false);
121cdf0e10cSrcweir         B2DPolyPolygon aTmp2;
122cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(sName,
123cdf0e10cSrcweir                                tools::importFromSvgD(
124*1f882ec4SArmin Le Grand                                    aTmp2, aSvg, false, 0));
125cdf0e10cSrcweir 
126cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
127cdf0e10cSrcweir             sName,
128cdf0e10cSrcweir             aTmp2 == aTmp1);
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir 
verifySimpleRange()131cdf0e10cSrcweir     void verifySimpleRange()
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         const char* unionSvg="m100 10v90h-90v10h-20v-10h-90v-90h-10v-20h10v-90h90v-10h20v10h90v90h10v20z";
134cdf0e10cSrcweir         const char* intersectSvg="m-100 10v-20h10v20zm80 90v-10h20v10zm-20-190v-10h20v10zm80 100v-20h10v20z";
135cdf0e10cSrcweir         const char* xorSvg="m-100 10h10v-20h-10zm90 110h20v-10h-20zm0-180h20v-10h-20zm100 110h10v-20h-10zm10 20v90h-90v10h-20v-10h-90v-90h-10v-20h10v-90h90v-10h20v10h90v90h10v20z";
136cdf0e10cSrcweir         const char* subtractSvg="m-90 10v-20h-10v-90h90v10h20v-10h90v90h-10v20h10v90h-90v-10h-20v10h-90v-90z";
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("cleared clip stays empty under union operation",
139cdf0e10cSrcweir                                aUnion1.isCleared());
140cdf0e10cSrcweir         verifyPoly("union", unionSvg, aUnion2);
141cdf0e10cSrcweir         verifyPoly("intersect", intersectSvg, aIntersect);
142cdf0e10cSrcweir         verifyPoly("xor", xorSvg, aXor);
143cdf0e10cSrcweir         verifyPoly("subtract", subtractSvg, aSubtract);
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir 
verifyMixedClips()146cdf0e10cSrcweir     void verifyMixedClips()
147cdf0e10cSrcweir     {
148cdf0e10cSrcweir         tools::B2DClipState aMixedClip;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir         const char* unionSvg="m100 10v90h-90v10h-20v-10h-90v-90h-10v-20h10v-90h90v-10h20v10h90v90h10v20z";
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         B2DPolyPolygon aTmp1;
153cdf0e10cSrcweir         tools::importFromSvgD(
154*1f882ec4SArmin Le Grand             aTmp1, rtl::OUString::createFromAscii(unionSvg), false, 0);
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         aMixedClip.intersectPolyPolygon(aTmp1);
157cdf0e10cSrcweir         aMixedClip.subtractRange(B2DRange(-20,-150,20,0));
158cdf0e10cSrcweir         aMixedClip.subtractRange(B2DRange(-150,-20,0,20));
159cdf0e10cSrcweir         aMixedClip.xorRange(B2DRange(-150,-150,150,150));
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         const char* mixedClipSvg="m0 0v20h-100v80h90v10h20v-10h90v-90h10v-20h-10v-90h-80v100zm-40-20v-80h-80v80zm-50 170v-300h300v300z";
162cdf0e10cSrcweir         verifyPoly("mixed clip", mixedClipSvg, aMixedClip);
163cdf0e10cSrcweir     }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(clipstate);
166cdf0e10cSrcweir     CPPUNIT_TEST(verifySimpleRange);
167cdf0e10cSrcweir     CPPUNIT_TEST(verifyMixedClips);
168cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
169cdf0e10cSrcweir };
170cdf0e10cSrcweir 
171cdf0e10cSrcweir // -----------------------------------------------------------------------------
172cdf0e10cSrcweir CPPUNIT_TEST_SUITE_REGISTRATION(basegfx2d::clipstate);
173cdf0e10cSrcweir } // namespace basegfx2d
174