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/lineattribute.hxx>
28 #include <basegfx/color/bcolor.hxx>
29 
30 //////////////////////////////////////////////////////////////////////////////
31 
32 namespace drawinglayer
33 {
34 	namespace attribute
35 	{
36 		class ImpLineAttribute
37 		{
38 		public:
39 			// refcounter
40 			sal_uInt32								mnRefCount;
41 
42             // data definitions
43 			basegfx::BColor							maColor;				// color
44 			double									mfWidth;				// absolute line width
45 			basegfx::B2DLineJoin					meLineJoin;				// type of LineJoin
46             com::sun::star::drawing::LineCap        meLineCap;              // BUTT, ROUND, or SQUARE
47 
ImpLineAttribute(const basegfx::BColor & rColor,double fWidth,basegfx::B2DLineJoin aB2DLineJoin,com::sun::star::drawing::LineCap aLineCap)48 			ImpLineAttribute(
49                 const basegfx::BColor& rColor,
50 				double fWidth,
51 				basegfx::B2DLineJoin aB2DLineJoin,
52                 com::sun::star::drawing::LineCap aLineCap)
53 			:	mnRefCount(0),
54                 maColor(rColor),
55                 mfWidth(fWidth),
56                 meLineJoin(aB2DLineJoin),
57                 meLineCap(aLineCap)
58 			{
59 			}
60 
61 			// data read access
getColor() const62 			const basegfx::BColor& getColor() const { return maColor; }
getWidth() const63 			double getWidth() const { return mfWidth; }
getLineJoin() const64 			basegfx::B2DLineJoin getLineJoin() const { return meLineJoin; }
getLineCap() const65             com::sun::star::drawing::LineCap getLineCap() const { return meLineCap; }
66 
operator ==(const ImpLineAttribute & rCandidate) const67 			bool operator==(const ImpLineAttribute& rCandidate) const
68 			{
69 				return (getColor() == rCandidate.getColor()
70 					&& getWidth() == rCandidate.getWidth()
71 					&& getLineJoin() == rCandidate.getLineJoin()
72                     && getLineCap() == rCandidate.getLineCap());
73 			}
74 
get_global_default()75             static ImpLineAttribute* get_global_default()
76             {
77                 static ImpLineAttribute* pDefault = 0;
78 
79                 if(!pDefault)
80                 {
81                     pDefault = new ImpLineAttribute(
82                         basegfx::BColor(),
83                         0.0,
84                         basegfx::B2DLINEJOIN_ROUND,
85                         com::sun::star::drawing::LineCap_BUTT);
86 
87                     // never delete; start with RefCount 1, not 0
88     			    pDefault->mnRefCount++;
89                 }
90 
91                 return pDefault;
92             }
93 		};
94 
LineAttribute(const basegfx::BColor & rColor,double fWidth,basegfx::B2DLineJoin aB2DLineJoin,com::sun::star::drawing::LineCap aLineCap)95         LineAttribute::LineAttribute(
96             const basegfx::BColor& rColor,
97 			double fWidth,
98 			basegfx::B2DLineJoin aB2DLineJoin,
99             com::sun::star::drawing::LineCap aLineCap)
100 		:	mpLineAttribute(
101                 new ImpLineAttribute(
102                     rColor,
103                     fWidth,
104                     aB2DLineJoin,
105                     aLineCap))
106 		{
107 		}
108 
LineAttribute()109 		LineAttribute::LineAttribute()
110         :	mpLineAttribute(ImpLineAttribute::get_global_default())
111 		{
112 			mpLineAttribute->mnRefCount++;
113 		}
114 
LineAttribute(const LineAttribute & rCandidate)115         LineAttribute::LineAttribute(const LineAttribute& rCandidate)
116 		:	mpLineAttribute(rCandidate.mpLineAttribute)
117 		{
118 			mpLineAttribute->mnRefCount++;
119 		}
120 
~LineAttribute()121 		LineAttribute::~LineAttribute()
122 		{
123 			if(mpLineAttribute->mnRefCount)
124 			{
125 				mpLineAttribute->mnRefCount--;
126 			}
127 			else
128 			{
129 				delete mpLineAttribute;
130 			}
131 		}
132 
isDefault() const133         bool LineAttribute::isDefault() const
134         {
135             return mpLineAttribute == ImpLineAttribute::get_global_default();
136         }
137 
operator =(const LineAttribute & rCandidate)138         LineAttribute& LineAttribute::operator=(const LineAttribute& rCandidate)
139 		{
140 			if(rCandidate.mpLineAttribute != mpLineAttribute)
141 			{
142 				if(mpLineAttribute->mnRefCount)
143 				{
144 					mpLineAttribute->mnRefCount--;
145 				}
146 				else
147 				{
148 					delete mpLineAttribute;
149 				}
150 
151 				mpLineAttribute = rCandidate.mpLineAttribute;
152 				mpLineAttribute->mnRefCount++;
153 			}
154 
155 			return *this;
156 		}
157 
operator ==(const LineAttribute & rCandidate) const158 		bool LineAttribute::operator==(const LineAttribute& rCandidate) const
159 		{
160 			if(rCandidate.mpLineAttribute == mpLineAttribute)
161 			{
162 				return true;
163 			}
164 
165 			if(rCandidate.isDefault() != isDefault())
166 			{
167 				return false;
168 			}
169 
170 			return (*rCandidate.mpLineAttribute == *mpLineAttribute);
171 		}
172 
getColor() const173 		const basegfx::BColor& LineAttribute::getColor() const
174 		{
175 			return mpLineAttribute->getColor();
176 		}
177 
getWidth() const178 		double LineAttribute::getWidth() const
179         {
180             return mpLineAttribute->getWidth();
181         }
182 
getLineJoin() const183         basegfx::B2DLineJoin LineAttribute::getLineJoin() const
184         {
185             return mpLineAttribute->getLineJoin();
186         }
187 
getLineCap() const188         com::sun::star::drawing::LineCap LineAttribute::getLineCap() const
189         {
190             return mpLineAttribute->getLineCap();
191         }
192 
193     } // end of namespace attribute
194 } // end of namespace drawinglayer
195 
196 //////////////////////////////////////////////////////////////////////////////
197 // eof
198