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 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_drawinglayer.hxx"
24 
25 #include <drawinglayer/attribute/sdrlineattribute.hxx>
26 #include <basegfx/color/bcolor.hxx>
27 
28 //////////////////////////////////////////////////////////////////////////////
29 
30 namespace drawinglayer
31 {
32 	namespace attribute
33 	{
34 		class ImpSdrLineAttribute
35 		{
36 		public:
37 			// refcounter
38 			sal_uInt32								mnRefCount;
39 
40 			// line definitions
41 			basegfx::B2DLineJoin				    meJoin;				// B2DLINEJOIN_* defines
42 			double									mfWidth;			// 1/100th mm, 0.0==hair
43 			double									mfTransparence;		// [0.0 .. 1.0], 0.0==no transp.
44 			basegfx::BColor							maColor;			// color of line
45             com::sun::star::drawing::LineCap        meCap;              // BUTT, ROUND, or SQUARE
46 			::std::vector< double >					maDotDashArray;		// array of double which defines the dot-dash pattern
47 			double									mfFullDotDashLen;	// sum of maDotDashArray (for convenience)
48 
ImpSdrLineAttribute(basegfx::B2DLineJoin eJoin,double fWidth,double fTransparence,const basegfx::BColor & rColor,com::sun::star::drawing::LineCap eCap,const::std::vector<double> & rDotDashArray,double fFullDotDashLen)49             ImpSdrLineAttribute(
50 				basegfx::B2DLineJoin eJoin,
51                 double fWidth,
52                 double fTransparence,
53                 const basegfx::BColor& rColor,
54                 com::sun::star::drawing::LineCap eCap,
55 				const ::std::vector< double >& rDotDashArray,
56                 double fFullDotDashLen)
57 			:	mnRefCount(0),
58 		    	meJoin(eJoin),
59 			    mfWidth(fWidth),
60 			    mfTransparence(fTransparence),
61 			    maColor(rColor),
62                 meCap(eCap),
63 			    maDotDashArray(rDotDashArray),
64 			    mfFullDotDashLen(fFullDotDashLen)
65 		    {
66 		    }
67 
ImpSdrLineAttribute(const basegfx::BColor & rColor)68             ImpSdrLineAttribute(const basegfx::BColor& rColor)
69 			:	mnRefCount(0),
70 		    	meJoin(basegfx::B2DLINEJOIN_NONE),
71 			    mfWidth(0.0),
72 			    mfTransparence(0.0),
73 			    maColor(rColor),
74                 meCap(com::sun::star::drawing::LineCap_BUTT),
75 			    maDotDashArray(),
76 			    mfFullDotDashLen(0.0)
77             {
78             }
79 
80 			// data read access
getJoin() const81 			basegfx::B2DLineJoin getJoin() const { return meJoin; }
getWidth() const82 			double getWidth() const { return mfWidth; }
getTransparence() const83 			double getTransparence() const { return mfTransparence; }
getColor() const84 			const basegfx::BColor& getColor() const { return maColor; }
getCap() const85             com::sun::star::drawing::LineCap getCap() const { return meCap; }
getDotDashArray() const86 			const ::std::vector< double >& getDotDashArray() const { return maDotDashArray; }
getFullDotDashLen() const87 			double getFullDotDashLen() const { return mfFullDotDashLen; }
88 
operator ==(const ImpSdrLineAttribute & rCandidate) const89             bool operator==(const ImpSdrLineAttribute& rCandidate) const
90 		    {
91 			    return (getJoin() == rCandidate.getJoin()
92 				    && getWidth() == rCandidate.getWidth()
93 				    && getTransparence() == rCandidate.getTransparence()
94 				    && getColor() == rCandidate.getColor()
95                     && getCap() == rCandidate.getCap()
96 				    && getDotDashArray() == rCandidate.getDotDashArray());
97 		    }
98 
get_global_default()99             static ImpSdrLineAttribute* get_global_default()
100             {
101                 static ImpSdrLineAttribute* pDefault = 0;
102 
103                 if(!pDefault)
104                 {
105                     pDefault = new ImpSdrLineAttribute(
106 			            basegfx::B2DLINEJOIN_ROUND,
107                         0.0,
108                         0.0,
109                         basegfx::BColor(),
110                         com::sun::star::drawing::LineCap_BUTT,
111 			            std::vector< double >(),
112                         0.0);
113 
114                     // never delete; start with RefCount 1, not 0
115     			    pDefault->mnRefCount++;
116                 }
117 
118                 return pDefault;
119             }
120 		};
121 
SdrLineAttribute(basegfx::B2DLineJoin eJoin,double fWidth,double fTransparence,const basegfx::BColor & rColor,com::sun::star::drawing::LineCap eCap,const::std::vector<double> & rDotDashArray,double fFullDotDashLen)122         SdrLineAttribute::SdrLineAttribute(
123 			basegfx::B2DLineJoin eJoin,
124             double fWidth,
125             double fTransparence,
126             const basegfx::BColor& rColor,
127             com::sun::star::drawing::LineCap eCap,
128 			const ::std::vector< double >& rDotDashArray,
129             double fFullDotDashLen)
130 		:	mpSdrLineAttribute(
131                 new ImpSdrLineAttribute(
132                     eJoin,
133                     fWidth,
134                     fTransparence,
135                     rColor,
136                     eCap,
137                     rDotDashArray,
138                     fFullDotDashLen))
139 		{
140 		}
141 
SdrLineAttribute(const basegfx::BColor & rColor)142         SdrLineAttribute::SdrLineAttribute(
143             const basegfx::BColor& rColor)
144 		:	mpSdrLineAttribute(
145                 new ImpSdrLineAttribute(
146                     rColor))
147         {
148         }
149 
SdrLineAttribute()150 		SdrLineAttribute::SdrLineAttribute()
151         :	mpSdrLineAttribute(ImpSdrLineAttribute::get_global_default())
152 		{
153 			mpSdrLineAttribute->mnRefCount++;
154 		}
155 
SdrLineAttribute(const SdrLineAttribute & rCandidate)156         SdrLineAttribute::SdrLineAttribute(const SdrLineAttribute& rCandidate)
157 		:	mpSdrLineAttribute(rCandidate.mpSdrLineAttribute)
158 		{
159 			mpSdrLineAttribute->mnRefCount++;
160 		}
161 
~SdrLineAttribute()162 		SdrLineAttribute::~SdrLineAttribute()
163 		{
164 			if(mpSdrLineAttribute->mnRefCount)
165 			{
166 				mpSdrLineAttribute->mnRefCount--;
167 			}
168 			else
169 			{
170 				delete mpSdrLineAttribute;
171 			}
172 		}
173 
isDefault() const174         bool SdrLineAttribute::isDefault() const
175         {
176             return mpSdrLineAttribute == ImpSdrLineAttribute::get_global_default();
177         }
178 
operator =(const SdrLineAttribute & rCandidate)179         SdrLineAttribute& SdrLineAttribute::operator=(const SdrLineAttribute& rCandidate)
180 		{
181 			if(rCandidate.mpSdrLineAttribute != mpSdrLineAttribute)
182 			{
183 				if(mpSdrLineAttribute->mnRefCount)
184 				{
185 					mpSdrLineAttribute->mnRefCount--;
186 				}
187 				else
188 				{
189 					delete mpSdrLineAttribute;
190 				}
191 
192 				mpSdrLineAttribute = rCandidate.mpSdrLineAttribute;
193 				mpSdrLineAttribute->mnRefCount++;
194 			}
195 
196 			return *this;
197 		}
198 
operator ==(const SdrLineAttribute & rCandidate) const199 		bool SdrLineAttribute::operator==(const SdrLineAttribute& rCandidate) const
200 		{
201 			if(rCandidate.mpSdrLineAttribute == mpSdrLineAttribute)
202 			{
203 				return true;
204 			}
205 
206 			if(rCandidate.isDefault() != isDefault())
207 			{
208 				return false;
209 			}
210 
211 			return (*rCandidate.mpSdrLineAttribute == *mpSdrLineAttribute);
212 		}
213 
getJoin() const214 		basegfx::B2DLineJoin SdrLineAttribute::getJoin() const
215         {
216             return mpSdrLineAttribute->getJoin();
217         }
218 
getWidth() const219 		double SdrLineAttribute::getWidth() const
220         {
221             return mpSdrLineAttribute->getWidth();
222         }
223 
getTransparence() const224 		double SdrLineAttribute::getTransparence() const
225         {
226             return mpSdrLineAttribute->getTransparence();
227         }
228 
getColor() const229 		const basegfx::BColor& SdrLineAttribute::getColor() const
230         {
231             return mpSdrLineAttribute->getColor();
232         }
233 
getDotDashArray() const234 		const ::std::vector< double >& SdrLineAttribute::getDotDashArray() const
235         {
236             return mpSdrLineAttribute->getDotDashArray();
237         }
238 
getFullDotDashLen() const239 		double SdrLineAttribute::getFullDotDashLen() const
240         {
241             return mpSdrLineAttribute->getFullDotDashLen();
242         }
243 
isDashed() const244 		bool SdrLineAttribute::isDashed() const
245         {
246             return (0L != getDotDashArray().size());
247         }
248 
getCap() const249         com::sun::star::drawing::LineCap SdrLineAttribute::getCap() const
250         {
251             return mpSdrLineAttribute->getCap();
252         }
253 
254     } // end of namespace attribute
255 } // end of namespace drawinglayer
256 
257 //////////////////////////////////////////////////////////////////////////////
258 // eof
259