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