xref: /AOO41X/main/drawinglayer/source/attribute/lineattribute.cxx (revision b2937f997bda0a05141a2d862a64f7be893955b7)
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 
47             ImpLineAttribute(
48                 const basegfx::BColor& rColor,
49                 double fWidth,
50                 basegfx::B2DLineJoin aB2DLineJoin)
51             :   mnRefCount(0),
52                 maColor(rColor),
53                 mfWidth(fWidth),
54                 meLineJoin(aB2DLineJoin)
55             {
56             }
57 
58             // data read access
59             const basegfx::BColor& getColor() const { return maColor; }
60             double getWidth() const { return mfWidth; }
61             basegfx::B2DLineJoin getLineJoin() const { return meLineJoin; }
62 
63             bool operator==(const ImpLineAttribute& rCandidate) const
64             {
65                 return (getColor() == rCandidate.getColor()
66                     && getWidth() == rCandidate.getWidth()
67                     && getLineJoin() == rCandidate.getLineJoin());
68             }
69 
70             static ImpLineAttribute* get_global_default()
71             {
72                 static ImpLineAttribute* pDefault = 0;
73 
74                 if(!pDefault)
75                 {
76                     pDefault = new ImpLineAttribute(
77                         basegfx::BColor(),
78                         0.0,
79                         basegfx::B2DLINEJOIN_ROUND);
80 
81                     // never delete; start with RefCount 1, not 0
82                     pDefault->mnRefCount++;
83                 }
84 
85                 return pDefault;
86             }
87         };
88 
89         LineAttribute::LineAttribute(
90             const basegfx::BColor& rColor,
91             double fWidth,
92             basegfx::B2DLineJoin aB2DLineJoin)
93         :   mpLineAttribute(new ImpLineAttribute(
94                 rColor, fWidth, aB2DLineJoin))
95         {
96         }
97 
98         LineAttribute::LineAttribute()
99         :   mpLineAttribute(ImpLineAttribute::get_global_default())
100         {
101             mpLineAttribute->mnRefCount++;
102         }
103 
104         LineAttribute::LineAttribute(const LineAttribute& rCandidate)
105         :   mpLineAttribute(rCandidate.mpLineAttribute)
106         {
107             mpLineAttribute->mnRefCount++;
108         }
109 
110         LineAttribute::~LineAttribute()
111         {
112             if(mpLineAttribute->mnRefCount)
113             {
114                 mpLineAttribute->mnRefCount--;
115             }
116             else
117             {
118                 delete mpLineAttribute;
119             }
120         }
121 
122         bool LineAttribute::isDefault() const
123         {
124             return mpLineAttribute == ImpLineAttribute::get_global_default();
125         }
126 
127         LineAttribute& LineAttribute::operator=(const LineAttribute& rCandidate)
128         {
129             if(rCandidate.mpLineAttribute != mpLineAttribute)
130             {
131                 if(mpLineAttribute->mnRefCount)
132                 {
133                     mpLineAttribute->mnRefCount--;
134                 }
135                 else
136                 {
137                     delete mpLineAttribute;
138                 }
139 
140                 mpLineAttribute = rCandidate.mpLineAttribute;
141                 mpLineAttribute->mnRefCount++;
142             }
143 
144             return *this;
145         }
146 
147         bool LineAttribute::operator==(const LineAttribute& rCandidate) const
148         {
149             if(rCandidate.mpLineAttribute == mpLineAttribute)
150             {
151                 return true;
152             }
153 
154             if(rCandidate.isDefault() != isDefault())
155             {
156                 return false;
157             }
158 
159             return (*rCandidate.mpLineAttribute == *mpLineAttribute);
160         }
161 
162         const basegfx::BColor& LineAttribute::getColor() const
163         {
164             return mpLineAttribute->getColor();
165         }
166 
167         double LineAttribute::getWidth() const
168         {
169             return mpLineAttribute->getWidth();
170         }
171 
172         basegfx::B2DLineJoin LineAttribute::getLineJoin() const
173         {
174             return mpLineAttribute->getLineJoin();
175         }
176 
177     } // end of namespace attribute
178 } // end of namespace drawinglayer
179 
180 //////////////////////////////////////////////////////////////////////////////
181 // eof
182