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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_drawinglayer.hxx"
26 
27 #include <drawinglayer/attribute/linestartendattribute.hxx>
28 #include <basegfx/polygon/b2dpolygon.hxx>
29 #include <basegfx/polygon/b2dpolypolygon.hxx>
30 
31 //////////////////////////////////////////////////////////////////////////////
32 
33 namespace drawinglayer
34 {
35 	namespace attribute
36 	{
37 		class ImpLineStartEndAttribute
38 		{
39 		public:
40 			// refcounter
41 			sal_uInt32								mnRefCount;
42 
43             // data definitions
44 			double									mfWidth;				// absolute line StartEndGeometry base width
45 			basegfx::B2DPolyPolygon					maPolyPolygon;			// the StartEndGeometry PolyPolygon
46 
47 			// bitfield
48 			unsigned								mbCentered : 1;			// use centered to ineStart/End point?
49 
ImpLineStartEndAttribute(double fWidth,const basegfx::B2DPolyPolygon & rPolyPolygon,bool bCentered)50 			ImpLineStartEndAttribute(
51 				double fWidth,
52 				const basegfx::B2DPolyPolygon& rPolyPolygon,
53 				bool bCentered)
54 			:	mnRefCount(0),
55 				mfWidth(fWidth),
56 				maPolyPolygon(rPolyPolygon),
57 				mbCentered(bCentered)
58 			{
59 			}
60 
61 			// data read access
getWidth() const62 			double getWidth() const { return mfWidth; }
getB2DPolyPolygon() const63 			const basegfx::B2DPolyPolygon& getB2DPolyPolygon() const { return maPolyPolygon; }
isCentered() const64 			bool isCentered() const { return mbCentered; }
65 
operator ==(const ImpLineStartEndAttribute & rCandidate) const66             bool operator==(const ImpLineStartEndAttribute& rCandidate) const
67 			{
68 				return (basegfx::fTools::equal(getWidth(), rCandidate.getWidth())
69 					&& getB2DPolyPolygon() == rCandidate.getB2DPolyPolygon()
70 					&& isCentered() == rCandidate.isCentered());
71 			}
72 
get_global_default()73             static ImpLineStartEndAttribute* get_global_default()
74             {
75                 static ImpLineStartEndAttribute* pDefault = 0;
76 
77                 if(!pDefault)
78                 {
79                     pDefault = new ImpLineStartEndAttribute(
80                         0.0,
81                         basegfx::B2DPolyPolygon(),
82                         false);
83 
84                     // never delete; start with RefCount 1, not 0
85     			    pDefault->mnRefCount++;
86                 }
87 
88                 return pDefault;
89             }
90         };
91 
LineStartEndAttribute(double fWidth,const basegfx::B2DPolyPolygon & rPolyPolygon,bool bCentered)92         LineStartEndAttribute::LineStartEndAttribute(
93 			double fWidth,
94 			const basegfx::B2DPolyPolygon& rPolyPolygon,
95 			bool bCentered)
96 		:	mpLineStartEndAttribute(new ImpLineStartEndAttribute(
97                 fWidth, rPolyPolygon, bCentered))
98 		{
99 		}
100 
LineStartEndAttribute()101 		LineStartEndAttribute::LineStartEndAttribute()
102         :	mpLineStartEndAttribute(ImpLineStartEndAttribute::get_global_default())
103 		{
104 			mpLineStartEndAttribute->mnRefCount++;
105 		}
106 
LineStartEndAttribute(const LineStartEndAttribute & rCandidate)107         LineStartEndAttribute::LineStartEndAttribute(const LineStartEndAttribute& rCandidate)
108 		:	mpLineStartEndAttribute(rCandidate.mpLineStartEndAttribute)
109 		{
110 			mpLineStartEndAttribute->mnRefCount++;
111 		}
112 
~LineStartEndAttribute()113 		LineStartEndAttribute::~LineStartEndAttribute()
114 		{
115 			if(mpLineStartEndAttribute->mnRefCount)
116 			{
117 				mpLineStartEndAttribute->mnRefCount--;
118 			}
119 			else
120 			{
121 				delete mpLineStartEndAttribute;
122 			}
123 		}
124 
isDefault() const125         bool LineStartEndAttribute::isDefault() const
126         {
127             return mpLineStartEndAttribute == ImpLineStartEndAttribute::get_global_default();
128         }
129 
operator =(const LineStartEndAttribute & rCandidate)130         LineStartEndAttribute& LineStartEndAttribute::operator=(const LineStartEndAttribute& rCandidate)
131 		{
132 			if(rCandidate.mpLineStartEndAttribute != mpLineStartEndAttribute)
133 			{
134 				if(mpLineStartEndAttribute->mnRefCount)
135 				{
136 					mpLineStartEndAttribute->mnRefCount--;
137 				}
138 				else
139 				{
140 					delete mpLineStartEndAttribute;
141 				}
142 
143 				mpLineStartEndAttribute = rCandidate.mpLineStartEndAttribute;
144 				mpLineStartEndAttribute->mnRefCount++;
145 			}
146 
147 			return *this;
148 		}
149 
operator ==(const LineStartEndAttribute & rCandidate) const150 		bool LineStartEndAttribute::operator==(const LineStartEndAttribute& rCandidate) const
151 		{
152 			if(rCandidate.mpLineStartEndAttribute == mpLineStartEndAttribute)
153 			{
154 				return true;
155 			}
156 
157 			if(rCandidate.isDefault() != isDefault())
158 			{
159 				return false;
160 			}
161 
162 			return (*rCandidate.mpLineStartEndAttribute == *mpLineStartEndAttribute);
163 		}
164 
getWidth() const165 		double LineStartEndAttribute::getWidth() const
166         {
167             return mpLineStartEndAttribute->getWidth();
168         }
169 
getB2DPolyPolygon() const170 		const basegfx::B2DPolyPolygon& LineStartEndAttribute::getB2DPolyPolygon() const
171         {
172             return mpLineStartEndAttribute->getB2DPolyPolygon();
173         }
174 
isCentered() const175 		bool LineStartEndAttribute::isCentered() const
176         {
177             return mpLineStartEndAttribute->isCentered();
178         }
179 
isActive() const180         bool LineStartEndAttribute::isActive() const
181         {
182             return (0.0 != getWidth()
183                 && 0 != getB2DPolyPolygon().count()
184                 && 0 != getB2DPolyPolygon().getB2DPolygon(0).count());
185         }
186 	} // end of namespace attribute
187 } // end of namespace drawinglayer
188 
189 //////////////////////////////////////////////////////////////////////////////
190 // eof
191