Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_VCL_REGION_HXX
21 : #define INCLUDED_VCL_REGION_HXX
22 :
23 : #include <tools/gen.hxx>
24 : #include <vcl/dllapi.h>
25 :
26 : #include <basegfx/polygon/b2dpolypolygon.hxx>
27 : #include <boost/shared_ptr.hpp>
28 :
29 : class ImplRegionBand;
30 : class RegionBand;
31 : class Polygon;
32 : namespace tools { class PolyPolygon; }
33 : namespace vcl { class Window; }
34 : class OutputDevice;
35 : class Bitmap;
36 :
37 : typedef boost::shared_ptr< RegionBand > RegionBandPtr;
38 : typedef boost::shared_ptr< tools::PolyPolygon > PolyPolygonPtr;
39 : typedef boost::shared_ptr< basegfx::B2DPolyPolygon > B2DPolyPolygonPtr;
40 : typedef std::vector< Rectangle > RectangleVector;
41 :
42 : namespace vcl {
43 :
44 : class VCL_DLLPUBLIC Region
45 : {
46 : private:
47 : friend class ::OutputDevice;
48 : friend class ::vcl::Window;
49 : friend class ::Bitmap;
50 :
51 : // possible contents
52 : B2DPolyPolygonPtr mpB2DPolyPolygon;
53 : PolyPolygonPtr mpPolyPolygon;
54 : RegionBandPtr mpRegionBand;
55 :
56 : /// bitfield
57 : bool mbIsNull : 1;
58 :
59 : // helpers
60 : SAL_DLLPRIVATE void ImplCreatePolyPolyRegion( const tools::PolyPolygon& rPolyPoly );
61 : SAL_DLLPRIVATE void ImplCreatePolyPolyRegion( const basegfx::B2DPolyPolygon& rPolyPoly );
62 :
63 : SAL_DLLPRIVATE tools::PolyPolygon ImplCreatePolyPolygonFromRegionBand() const;
64 : SAL_DLLPRIVATE basegfx::B2DPolyPolygon ImplCreateB2DPolyPolygonFromRegionBand() const;
65 :
66 : public:
67 :
68 : explicit Region(bool bIsNull = false); // default creates empty region, with true a null region is created
69 : explicit Region(const Rectangle& rRect);
70 : explicit Region(const Polygon& rPolygon);
71 : explicit Region(const tools::PolyPolygon& rPolyPoly);
72 : explicit Region(const basegfx::B2DPolyPolygon&);
73 : Region(const vcl::Region& rRegion);
74 : ~Region();
75 :
76 : // direct access to contents
77 9644827 : const basegfx::B2DPolyPolygon* getB2DPolyPolygon() const { return mpB2DPolyPolygon.get(); }
78 9643787 : const tools::PolyPolygon* getPolyPolygon() const { return mpPolyPolygon.get(); }
79 18163758 : const RegionBand* getRegionBand() const { return mpRegionBand.get(); }
80 :
81 : // access with converters, the asked data will be created from the most
82 : // valuable data, buffered and returned
83 : const tools::PolyPolygon GetAsPolyPolygon() const;
84 : const basegfx::B2DPolyPolygon GetAsB2DPolyPolygon() const;
85 : const RegionBand* GetAsRegionBand() const;
86 :
87 : // manipulators
88 : void Move( long nHorzMove, long nVertMove );
89 : void Scale( double fScaleX, double fScaleY );
90 : bool Union( const Rectangle& rRegion );
91 : bool Intersect( const Rectangle& rRegion );
92 : bool Exclude( const Rectangle& rRegion );
93 : bool XOr( const Rectangle& rRegion );
94 : bool Union( const vcl::Region& rRegion );
95 : bool Intersect( const vcl::Region& rRegion );
96 : bool Exclude( const vcl::Region& rRegion );
97 : bool XOr( const vcl::Region& rRegion );
98 :
99 : bool IsEmpty() const;
100 9371585 : bool IsNull() const { return mbIsNull;}
101 :
102 : void SetEmpty();
103 : void SetNull();
104 :
105 : Rectangle GetBoundRect() const;
106 5123945 : bool HasPolyPolygonOrB2DPolyPolygon() const { return (getB2DPolyPolygon() || getPolyPolygon()); }
107 : void GetRegionRectangles(RectangleVector& rTarget) const;
108 :
109 : bool IsInside( const Point& rPoint ) const;
110 : bool IsInside( const Rectangle& rRect ) const;
111 : bool IsOver( const Rectangle& rRect ) const;
112 :
113 : vcl::Region& operator=( const vcl::Region& rRegion );
114 : vcl::Region& operator=( const Rectangle& rRect );
115 :
116 : bool operator==( const vcl::Region& rRegion ) const;
117 36608 : bool operator!=( const vcl::Region& rRegion ) const { return !(Region::operator==( rRegion )); }
118 :
119 : friend VCL_DLLPUBLIC SvStream& ReadRegion( SvStream& rIStm, vcl::Region& rRegion );
120 : friend VCL_DLLPUBLIC SvStream& WriteRegion( SvStream& rOStm, const vcl::Region& rRegion );
121 :
122 : /* workaround: faster conversion for PolyPolygons
123 : * if half of the Polygons contained in rPolyPoly are actually
124 : * rectangles, then the returned vcl::Region will be constructed by
125 : * XOr'ing the contained Polygons together; in the case of
126 : * only Rectangles this can be up to eight times faster than
127 : * Region( const tools::PolyPolygon& ).
128 : * Caution: this is only useful if the vcl::Region is known to be
129 : * changed to rectangles; e.g. if being set as clip region
130 : */
131 : static vcl::Region GetRegionFromPolyPolygon( const tools::PolyPolygon& rPolyPoly );
132 : };
133 :
134 : } /* namespace vcl */
135 :
136 : template< typename charT, typename traits >
137 : inline std::basic_ostream<charT, traits> & operator <<(
138 : std::basic_ostream<charT, traits> & stream, const vcl::Region& rRegion)
139 : {
140 : if (rRegion.IsEmpty())
141 : return stream << "EMPTY";
142 : if (rRegion.getB2DPolyPolygon())
143 : return stream << "B2DPolyPolygon("
144 : << *rRegion.getB2DPolyPolygon()
145 : << ")";
146 : if (rRegion.getPolyPolygon())
147 : return stream << "PolyPolygon("
148 : << *rRegion.getPolyPolygon()
149 : << ")";
150 : if (rRegion.getRegionBand())
151 : { // inlined because RegionBand is private to vcl
152 : stream << "RegionBand(";
153 : RectangleVector rects;
154 : rRegion.GetRegionRectangles(rects);
155 : if (rects.empty())
156 : stream << "EMPTY";
157 : for (size_t i = 0; i < rects.size(); ++i)
158 : stream << "[" << i << "] " << rects[i];
159 : stream << ")";
160 : }
161 : return stream;
162 : }
163 :
164 : #endif // INCLUDED_VCL_REGION_HXX
165 :
166 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|