xref: /aoo42x/main/basegfx/test/clipstate.cxx (revision 09dbbe93)
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:
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 
102     void tearDown()
103     {}
104 
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()),
111                     RTL_TEXTENCODING_UTF8).getStr() );
112 #endif
113 
114         B2DPolyPolygon aTmp1;
115         CPPUNIT_ASSERT_MESSAGE(sName,
116                                tools::importFromSvgD(
117                                    aTmp1,
118                                    rtl::OUString::createFromAscii(sSvg)));
119 
120         const rtl::OUString aSvg=
121             tools::exportToSvgD(toTest.getClipPoly());
122         B2DPolyPolygon aTmp2;
123         CPPUNIT_ASSERT_MESSAGE(sName,
124                                tools::importFromSvgD(
125                                    aTmp2,
126                                    aSvg));
127 
128         CPPUNIT_ASSERT_MESSAGE(
129             sName,
130             aTmp2 == aTmp1);
131     }
132 
133     void verifySimpleRange()
134     {
135         const char* unionSvg="m100 10v90h-90v10h-20v-10h-90v-90h-10v-20h10v-90h90v-10h20v10h90v90h10v20z";
136         const char* intersectSvg="m-100 10v-20h10v20zm80 90v-10h20v10zm-20-190v-10h20v10zm80 100v-20h10v20z";
137         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";
138         const char* subtractSvg="m-90 10v-20h-10v-90h90v10h20v-10h90v90h-10v20h10v90h-90v-10h-20v10h-90v-90z";
139 
140         CPPUNIT_ASSERT_MESSAGE("cleared clip stays empty under union operation",
141                                aUnion1.isCleared());
142         verifyPoly("union", unionSvg, aUnion2);
143         verifyPoly("intersect", intersectSvg, aIntersect);
144         verifyPoly("xor", xorSvg, aXor);
145         verifyPoly("subtract", subtractSvg, aSubtract);
146     }
147 
148     void verifyMixedClips()
149     {
150         tools::B2DClipState aMixedClip;
151 
152         const char* unionSvg="m100 10v90h-90v10h-20v-10h-90v-90h-10v-20h10v-90h90v-10h20v10h90v90h10v20z";
153 
154         B2DPolyPolygon aTmp1;
155         tools::importFromSvgD(
156             aTmp1,
157             rtl::OUString::createFromAscii(unionSvg));
158 
159         aMixedClip.intersectPolyPolygon(aTmp1);
160         aMixedClip.subtractRange(B2DRange(-20,-150,20,0));
161         aMixedClip.subtractRange(B2DRange(-150,-20,0,20));
162         aMixedClip.xorRange(B2DRange(-150,-150,150,150));
163 
164         const char* mixedClipSvg="m0 0v20h-100v80h90v10h20v-10h90v-90h10v-20h-10v-90h-80v100zm-40-20v-80h-80v80zm-50 170v-300h300v300z";
165         verifyPoly("mixed clip", mixedClipSvg, aMixedClip);
166     }
167 
168     CPPUNIT_TEST_SUITE(clipstate);
169     CPPUNIT_TEST(verifySimpleRange);
170     CPPUNIT_TEST(verifyMixedClips);
171     CPPUNIT_TEST_SUITE_END();
172 };
173 
174 // -----------------------------------------------------------------------------
175 CPPUNIT_TEST_SUITE_REGISTRATION(basegfx2d::clipstate);
176 } // namespace basegfx2d
177