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 <tools/stream.hxx>
21 #include <tools/vcompat.hxx>
22 #include <vcl/lineinfo.hxx>
23 #include <basegfx/polygon/b2dpolypolygon.hxx>
24 #include <basegfx/polygon/b2dpolygontools.hxx>
25 #include <basegfx/polygon/b2dlinegeometry.hxx>
26 #include <numeric>
27 
28 
ImplLineInfo()29 ImplLineInfo::ImplLineInfo()
30     : mnWidth(0)
31     , mnDashLen(0)
32     , mnDotLen(0)
33     , mnDistance(0)
34     , meLineJoin(basegfx::B2DLineJoin::Round)
35     , meLineCap(css::drawing::LineCap_BUTT)
36     , meStyle(LineStyle::Solid)
37     , mnDashCount(0)
38     , mnDotCount(0)
39 {
40 }
41 
operator ==(const ImplLineInfo & rB) const42 inline bool ImplLineInfo::operator==( const ImplLineInfo& rB ) const
43 {
44     return(meStyle == rB.meStyle
45         && mnWidth == rB.mnWidth
46         && mnDashCount == rB.mnDashCount
47         && mnDashLen == rB.mnDashLen
48         && mnDotCount == rB.mnDotCount
49         && mnDotLen == rB.mnDotLen
50         && mnDistance == rB.mnDistance
51         && meLineJoin == rB.meLineJoin
52         && meLineCap == rB.meLineCap);
53 }
54 
55 
LineInfo(LineStyle eStyle,sal_Int32 nWidth)56 LineInfo::LineInfo( LineStyle eStyle, sal_Int32 nWidth ) : mpImplLineInfo()
57 {
58     mpImplLineInfo->meStyle = eStyle;
59     mpImplLineInfo->mnWidth = nWidth;
60 }
61 
62 LineInfo::LineInfo( const LineInfo& ) = default;
63 
64 LineInfo::LineInfo( LineInfo&& ) = default;
65 
66 LineInfo::~LineInfo() = default;
67 
68 LineInfo& LineInfo::operator=( const LineInfo& ) = default;
69 
70 LineInfo& LineInfo::operator=( LineInfo&& ) = default;
71 
operator ==(const LineInfo & rLineInfo) const72 bool LineInfo::operator==( const LineInfo& rLineInfo ) const
73 {
74     return mpImplLineInfo == rLineInfo.mpImplLineInfo;
75 }
76 
SetStyle(LineStyle eStyle)77 void LineInfo::SetStyle( LineStyle eStyle )
78 {
79     mpImplLineInfo->meStyle = eStyle;
80 }
81 
SetWidth(sal_Int32 nWidth)82 void LineInfo::SetWidth( sal_Int32 nWidth )
83 {
84     mpImplLineInfo->mnWidth = nWidth;
85 }
86 
SetDashCount(sal_uInt16 nDashCount)87 void LineInfo::SetDashCount( sal_uInt16 nDashCount )
88 {
89     mpImplLineInfo->mnDashCount = nDashCount;
90 }
91 
SetDashLen(sal_Int32 nDashLen)92 void LineInfo::SetDashLen( sal_Int32 nDashLen )
93 {
94     mpImplLineInfo->mnDashLen = nDashLen;
95 }
96 
SetDotCount(sal_uInt16 nDotCount)97 void LineInfo::SetDotCount( sal_uInt16 nDotCount )
98 {
99     mpImplLineInfo->mnDotCount = nDotCount;
100 }
101 
SetDotLen(sal_Int32 nDotLen)102 void LineInfo::SetDotLen( sal_Int32 nDotLen )
103 {
104     mpImplLineInfo->mnDotLen = nDotLen;
105 }
106 
SetDistance(sal_Int32 nDistance)107 void LineInfo::SetDistance( sal_Int32 nDistance )
108 {
109     mpImplLineInfo->mnDistance = nDistance;
110 }
111 
SetLineJoin(basegfx::B2DLineJoin eLineJoin)112 void LineInfo::SetLineJoin(basegfx::B2DLineJoin eLineJoin)
113 {
114 
115     if(eLineJoin != mpImplLineInfo->meLineJoin)
116     {
117         mpImplLineInfo->meLineJoin = eLineJoin;
118     }
119 }
120 
SetLineCap(css::drawing::LineCap eLineCap)121 void LineInfo::SetLineCap(css::drawing::LineCap eLineCap)
122 {
123     if(eLineCap != mpImplLineInfo->meLineCap)
124     {
125         mpImplLineInfo->meLineCap = eLineCap;
126     }
127 }
128 
IsDefault() const129 bool LineInfo::IsDefault() const
130 {
131     return( !mpImplLineInfo->mnWidth
132         && ( LineStyle::Solid == mpImplLineInfo->meStyle )
133         && ( css::drawing::LineCap_BUTT == mpImplLineInfo->meLineCap));
134 }
135 
ReadLineInfo(SvStream & rIStm,LineInfo & rLineInfo)136 SvStream& ReadLineInfo( SvStream& rIStm, LineInfo& rLineInfo )
137 {
138     VersionCompat   aCompat( rIStm, StreamMode::READ );
139     sal_uInt16          nTmp16(0);
140     sal_Int32       nTmp32(0);
141 
142     rIStm.ReadUInt16( nTmp16 ); rLineInfo.mpImplLineInfo->meStyle = static_cast<LineStyle>(nTmp16);
143     rIStm.ReadInt32( nTmp32 );
144     rLineInfo.mpImplLineInfo->mnWidth = nTmp32;
145 
146     if( aCompat.GetVersion() >= 2 )
147     {
148         // version 2
149         rIStm.ReadUInt16( rLineInfo.mpImplLineInfo->mnDashCount ).ReadInt32( nTmp32 );
150         rLineInfo.mpImplLineInfo->mnDashLen = nTmp32;
151         rIStm.ReadUInt16( rLineInfo.mpImplLineInfo->mnDotCount ).ReadInt32( nTmp32 );
152         rLineInfo.mpImplLineInfo->mnDotLen = nTmp32;
153         rIStm.ReadInt32( nTmp32 );
154         rLineInfo.mpImplLineInfo->mnDistance = nTmp32;
155     }
156 
157     if( aCompat.GetVersion() >= 3 )
158     {
159         // version 3
160         rIStm.ReadUInt16( nTmp16 ); rLineInfo.mpImplLineInfo->meLineJoin = static_cast<basegfx::B2DLineJoin>(nTmp16);
161     }
162 
163     if( aCompat.GetVersion() >= 4 )
164     {
165         // version 4
166         rIStm.ReadUInt16( nTmp16 ); rLineInfo.mpImplLineInfo->meLineCap = static_cast<css::drawing::LineCap>(nTmp16);
167     }
168 
169     return rIStm;
170 }
171 
WriteLineInfo(SvStream & rOStm,const LineInfo & rLineInfo)172 SvStream& WriteLineInfo( SvStream& rOStm, const LineInfo& rLineInfo )
173 {
174     VersionCompat aCompat( rOStm, StreamMode::WRITE, 4 );
175 
176     // version 1
177     rOStm.WriteUInt16( static_cast<sal_uInt16>(rLineInfo.mpImplLineInfo->meStyle) )
178          .WriteInt32( rLineInfo.mpImplLineInfo->mnWidth );
179 
180     // since version2
181     rOStm.WriteUInt16( rLineInfo.mpImplLineInfo->mnDashCount )
182          .WriteInt32( rLineInfo.mpImplLineInfo->mnDashLen );
183     rOStm.WriteUInt16( rLineInfo.mpImplLineInfo->mnDotCount )
184          .WriteInt32( rLineInfo.mpImplLineInfo->mnDotLen );
185     rOStm.WriteInt32( rLineInfo.mpImplLineInfo->mnDistance );
186 
187     // since version3
188     rOStm.WriteUInt16( static_cast<sal_uInt16>(rLineInfo.mpImplLineInfo->meLineJoin) );
189 
190     // since version4
191     rOStm.WriteUInt16( static_cast<sal_uInt16>(rLineInfo.mpImplLineInfo->meLineCap) );
192 
193     return rOStm;
194 }
195 
applyToB2DPolyPolygon(basegfx::B2DPolyPolygon & io_rLinePolyPolygon,basegfx::B2DPolyPolygon & o_rFillPolyPolygon) const196 void LineInfo::applyToB2DPolyPolygon(
197     basegfx::B2DPolyPolygon& io_rLinePolyPolygon,
198     basegfx::B2DPolyPolygon& o_rFillPolyPolygon) const
199 {
200     o_rFillPolyPolygon.clear();
201 
202     if(io_rLinePolyPolygon.count())
203     {
204         if(LineStyle::Dash == GetStyle())
205         {
206             ::std::vector< double > fDotDashArray;
207             const double fDashLen(GetDashLen());
208             const double fDotLen(GetDotLen());
209             const double fDistance(GetDistance());
210 
211             for(sal_uInt16 a(0); a < GetDashCount(); a++)
212             {
213                 fDotDashArray.push_back(fDashLen);
214                 fDotDashArray.push_back(fDistance);
215             }
216 
217             for(sal_uInt16 b(0); b < GetDotCount(); b++)
218             {
219                 fDotDashArray.push_back(fDotLen);
220                 fDotDashArray.push_back(fDistance);
221             }
222 
223             const double fAccumulated(::std::accumulate(fDotDashArray.begin(), fDotDashArray.end(), 0.0));
224 
225             if(fAccumulated > 0.0)
226             {
227                 basegfx::B2DPolyPolygon aResult;
228 
229                 for(auto const& rPolygon : io_rLinePolyPolygon)
230                 {
231                     basegfx::B2DPolyPolygon aLineTraget;
232                     basegfx::utils::applyLineDashing(
233                         rPolygon,
234                         fDotDashArray,
235                         &aLineTraget);
236                     aResult.append(aLineTraget);
237                 }
238 
239                 io_rLinePolyPolygon = aResult;
240             }
241         }
242 
243         if(GetWidth() > 1 && io_rLinePolyPolygon.count())
244         {
245             const double fHalfLineWidth((GetWidth() * 0.5) + 0.5);
246 
247             for(auto const& rPolygon : io_rLinePolyPolygon)
248             {
249                 o_rFillPolyPolygon.append(basegfx::utils::createAreaGeometry(
250                     rPolygon,
251                     fHalfLineWidth,
252                     GetLineJoin(),
253                     GetLineCap()));
254             }
255 
256             io_rLinePolyPolygon.clear();
257         }
258     }
259 }
260 
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
262