Branch data 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 : : #include <drawinglayer/attribute/strokeattribute.hxx>
21 : : #include <numeric>
22 : :
23 : : //////////////////////////////////////////////////////////////////////////////
24 : :
25 : : namespace drawinglayer
26 : : {
27 : : namespace attribute
28 : : {
29 : 25448 : class ImpStrokeAttribute
30 : : {
31 : : public:
32 : : // refcounter
33 : : sal_uInt32 mnRefCount;
34 : :
35 : : // data definitions
36 : : ::std::vector< double > maDotDashArray; // array of double which defines the dot-dash pattern
37 : : double mfFullDotDashLen; // sum of maDotDashArray (for convenience)
38 : :
39 : 25526 : ImpStrokeAttribute(
40 : : const ::std::vector< double >& rDotDashArray,
41 : : double fFullDotDashLen)
42 : : : mnRefCount(0),
43 : : maDotDashArray(rDotDashArray),
44 : 25526 : mfFullDotDashLen(fFullDotDashLen)
45 : : {
46 : 25526 : }
47 : :
48 : : // data read access
49 : 8209 : const ::std::vector< double >& getDotDashArray() const { return maDotDashArray; }
50 : 30523 : double getFullDotDashLen() const
51 : : {
52 [ + + ][ + + ]: 30523 : if(0.0 == mfFullDotDashLen && maDotDashArray.size())
[ + + ]
53 : : {
54 : : // calculate length on demand
55 : 566 : const double fAccumulated(::std::accumulate(maDotDashArray.begin(), maDotDashArray.end(), 0.0));
56 : 566 : const_cast< ImpStrokeAttribute* >(this)->mfFullDotDashLen = fAccumulated;
57 : : }
58 : :
59 : 30523 : return mfFullDotDashLen;
60 : : }
61 : :
62 : 108 : bool operator==(const ImpStrokeAttribute& rCandidate) const
63 : : {
64 : 108 : return (getDotDashArray() == rCandidate.getDotDashArray()
65 [ + - ][ + - ]: 108 : && getFullDotDashLen() == rCandidate.getFullDotDashLen());
66 : : }
67 : :
68 : 26728 : static ImpStrokeAttribute* get_global_default()
69 : : {
70 : : static ImpStrokeAttribute* pDefault = 0;
71 : :
72 [ + + ]: 26728 : if(!pDefault)
73 : : {
74 : : pDefault = new ImpStrokeAttribute(
75 : : std::vector< double >(),
76 [ + - ][ + - ]: 73 : 0.0);
77 : :
78 : : // never delete; start with RefCount 1, not 0
79 : 73 : pDefault->mnRefCount++;
80 : : }
81 : :
82 : 26728 : return pDefault;
83 : : }
84 : : };
85 : :
86 : 25453 : StrokeAttribute::StrokeAttribute(
87 : : const ::std::vector< double >& rDotDashArray,
88 : : double fFullDotDashLen)
89 : : : mpStrokeAttribute(new ImpStrokeAttribute(
90 [ + - ]: 25453 : rDotDashArray, fFullDotDashLen))
91 : : {
92 : 25453 : }
93 : :
94 : 5764 : StrokeAttribute::StrokeAttribute()
95 : 5764 : : mpStrokeAttribute(ImpStrokeAttribute::get_global_default())
96 : : {
97 : 5764 : mpStrokeAttribute->mnRefCount++;
98 : 5764 : }
99 : :
100 : 29046 : StrokeAttribute::StrokeAttribute(const StrokeAttribute& rCandidate)
101 : 29046 : : mpStrokeAttribute(rCandidate.mpStrokeAttribute)
102 : : {
103 : 29046 : mpStrokeAttribute->mnRefCount++;
104 : 29046 : }
105 : :
106 : 60190 : StrokeAttribute::~StrokeAttribute()
107 : : {
108 [ + + ]: 60190 : if(mpStrokeAttribute->mnRefCount)
109 : : {
110 : 34742 : mpStrokeAttribute->mnRefCount--;
111 : : }
112 : : else
113 : : {
114 [ + - ]: 25448 : delete mpStrokeAttribute;
115 : : }
116 : 60190 : }
117 : :
118 : 20964 : bool StrokeAttribute::isDefault() const
119 : : {
120 : 20964 : return mpStrokeAttribute == ImpStrokeAttribute::get_global_default();
121 : : }
122 : :
123 : 728 : StrokeAttribute& StrokeAttribute::operator=(const StrokeAttribute& rCandidate)
124 : : {
125 [ + - ]: 728 : if(rCandidate.mpStrokeAttribute != mpStrokeAttribute)
126 : : {
127 [ + - ]: 728 : if(mpStrokeAttribute->mnRefCount)
128 : : {
129 : 728 : mpStrokeAttribute->mnRefCount--;
130 : : }
131 : : else
132 : : {
133 [ # # ]: 0 : delete mpStrokeAttribute;
134 : : }
135 : :
136 : 728 : mpStrokeAttribute = rCandidate.mpStrokeAttribute;
137 : 728 : mpStrokeAttribute->mnRefCount++;
138 : : }
139 : :
140 : 728 : return *this;
141 : : }
142 : :
143 : 108 : bool StrokeAttribute::operator==(const StrokeAttribute& rCandidate) const
144 : : {
145 [ - + ]: 108 : if(rCandidate.mpStrokeAttribute == mpStrokeAttribute)
146 : : {
147 : 0 : return true;
148 : : }
149 : :
150 [ - + ]: 108 : if(rCandidate.isDefault() != isDefault())
151 : : {
152 : 0 : return false;
153 : : }
154 : :
155 : 108 : return (*rCandidate.mpStrokeAttribute == *mpStrokeAttribute);
156 : : }
157 : :
158 : 7993 : const ::std::vector< double >& StrokeAttribute::getDotDashArray() const
159 : : {
160 : 7993 : return mpStrokeAttribute->getDotDashArray();
161 : : }
162 : :
163 : 30307 : double StrokeAttribute::getFullDotDashLen() const
164 : : {
165 : 30307 : return mpStrokeAttribute->getFullDotDashLen();
166 : : }
167 : : } // end of namespace attribute
168 : : } // end of namespace drawinglayer
169 : :
170 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|