LibreOffice Module vcl (master)  1
lineinfo.cxx
Go to the documentation of this file.
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>
26 #include <numeric>
27 
28 
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 
42 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 
56 LineInfo::LineInfo( LineStyle eStyle, double nWidth )
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 
72 bool LineInfo::operator==( const LineInfo& rLineInfo ) const
73 {
74  return mpImplLineInfo == rLineInfo.mpImplLineInfo;
75 }
76 
77 void LineInfo::SetStyle( LineStyle eStyle )
78 {
79  mpImplLineInfo->meStyle = eStyle;
80 }
81 
82 void LineInfo::SetWidth( double nWidth )
83 {
84  mpImplLineInfo->mnWidth = nWidth;
85 }
86 
87 void LineInfo::SetDashCount( sal_uInt16 nDashCount )
88 {
89  mpImplLineInfo->mnDashCount = nDashCount;
90 }
91 
92 void LineInfo::SetDashLen( double nDashLen )
93 {
94  mpImplLineInfo->mnDashLen = nDashLen;
95 }
96 
97 void LineInfo::SetDotCount( sal_uInt16 nDotCount )
98 {
99  mpImplLineInfo->mnDotCount = nDotCount;
100 }
101 
102 void LineInfo::SetDotLen( double nDotLen )
103 {
104  mpImplLineInfo->mnDotLen = nDotLen;
105 }
106 
107 void LineInfo::SetDistance( double nDistance )
108 {
109  mpImplLineInfo->mnDistance = nDistance;
110 }
111 
112 void LineInfo::SetLineJoin(basegfx::B2DLineJoin eLineJoin)
113 {
114  mpImplLineInfo->meLineJoin = eLineJoin;
115 }
116 
117 void LineInfo::SetLineCap(css::drawing::LineCap eLineCap)
118 {
119  mpImplLineInfo->meLineCap = eLineCap;
120 }
121 
122 bool LineInfo::IsDefault() const
123 {
124  return( !mpImplLineInfo->mnWidth
125  && ( LineStyle::Solid == mpImplLineInfo->meStyle )
126  && ( css::drawing::LineCap_BUTT == mpImplLineInfo->meLineCap));
127 }
128 
129 SvStream& ReadLineInfo( SvStream& rIStm, LineInfo& rLineInfo )
130 {
131  VersionCompatRead aCompat( rIStm );
132  sal_uInt16 nTmp16(0);
133  sal_Int32 nTmp32(0);
134 
135  rIStm.ReadUInt16( nTmp16 );
136  rLineInfo.mpImplLineInfo->meStyle = static_cast<LineStyle>(nTmp16);
137  rIStm.ReadInt32( nTmp32 );
138  rLineInfo.mpImplLineInfo->mnWidth = nTmp32;
139 
140  if( aCompat.GetVersion() >= 2 )
141  {
142  // version 2
143  rIStm.ReadUInt16( rLineInfo.mpImplLineInfo->mnDashCount ).ReadInt32( nTmp32 );
144  rLineInfo.mpImplLineInfo->mnDashLen = nTmp32;
145  rIStm.ReadUInt16( rLineInfo.mpImplLineInfo->mnDotCount ).ReadInt32( nTmp32 );
146  rLineInfo.mpImplLineInfo->mnDotLen = nTmp32;
147  rIStm.ReadInt32( nTmp32 );
148  rLineInfo.mpImplLineInfo->mnDistance = nTmp32;
149  }
150 
151  if( aCompat.GetVersion() >= 3 )
152  {
153  // version 3
154  rIStm.ReadUInt16( nTmp16 );
155  rLineInfo.mpImplLineInfo->meLineJoin = static_cast<basegfx::B2DLineJoin>(nTmp16);
156  }
157 
158  if( aCompat.GetVersion() >= 4 )
159  {
160  // version 4
161  rIStm.ReadUInt16( nTmp16 );
162  rLineInfo.mpImplLineInfo->meLineCap = static_cast<css::drawing::LineCap>(nTmp16);
163  }
164 
165  if( aCompat.GetVersion() >= 5 )
166  {
167  // version 5
168  rIStm.ReadDouble( rLineInfo.mpImplLineInfo->mnWidth );
169  rIStm.ReadDouble( rLineInfo.mpImplLineInfo->mnDashLen );
170  rIStm.ReadDouble( rLineInfo.mpImplLineInfo->mnDotLen );
171  rIStm.ReadDouble( rLineInfo.mpImplLineInfo->mnDistance );
172  }
173 
174  return rIStm;
175 }
176 
177 SvStream& WriteLineInfo( SvStream& rOStm, const LineInfo& rLineInfo )
178 {
179  VersionCompatWrite aCompat( rOStm, 5 );
180 
181  // version 1
182  rOStm.WriteUInt16( static_cast<sal_uInt16>(rLineInfo.mpImplLineInfo->meStyle) )
183  .WriteInt32( basegfx::fround( rLineInfo.mpImplLineInfo->mnWidth ));
184 
185  // since version2
186  rOStm.WriteUInt16( rLineInfo.mpImplLineInfo->mnDashCount )
187  .WriteInt32( basegfx::fround( rLineInfo.mpImplLineInfo->mnDashLen ));
188  rOStm.WriteUInt16( rLineInfo.mpImplLineInfo->mnDotCount )
189  .WriteInt32( basegfx::fround( rLineInfo.mpImplLineInfo->mnDotLen ));
190  rOStm.WriteInt32( basegfx::fround( rLineInfo.mpImplLineInfo->mnDistance ));
191 
192  // since version3
193  rOStm.WriteUInt16( static_cast<sal_uInt16>(rLineInfo.mpImplLineInfo->meLineJoin) );
194 
195  // since version4
196  rOStm.WriteUInt16( static_cast<sal_uInt16>(rLineInfo.mpImplLineInfo->meLineCap) );
197 
198  // since version5
199  rOStm.WriteDouble( rLineInfo.mpImplLineInfo->mnWidth );
200  rOStm.WriteDouble( rLineInfo.mpImplLineInfo->mnDashLen );
201  rOStm.WriteDouble( rLineInfo.mpImplLineInfo->mnDotLen );
202  rOStm.WriteDouble( rLineInfo.mpImplLineInfo->mnDistance );
203 
204  return rOStm;
205 }
206 
207 void LineInfo::applyToB2DPolyPolygon(
208  basegfx::B2DPolyPolygon& io_rLinePolyPolygon,
209  basegfx::B2DPolyPolygon& o_rFillPolyPolygon) const
210 {
211  o_rFillPolyPolygon.clear();
212 
213  if(!io_rLinePolyPolygon.count())
214  return;
215 
216  if(LineStyle::Dash == GetStyle())
217  {
218  ::std::vector< double > fDotDashArray;
219  const double fDashLen(GetDashLen());
220  const double fDotLen(GetDotLen());
221  const double fDistance(GetDistance());
222 
223  for(sal_uInt16 a(0); a < GetDashCount(); a++)
224  {
225  fDotDashArray.push_back(fDashLen);
226  fDotDashArray.push_back(fDistance);
227  }
228 
229  for(sal_uInt16 b(0); b < GetDotCount(); b++)
230  {
231  fDotDashArray.push_back(fDotLen);
232  fDotDashArray.push_back(fDistance);
233  }
234 
235  const double fAccumulated(::std::accumulate(fDotDashArray.begin(), fDotDashArray.end(), 0.0));
236 
237  if(fAccumulated > 0.0)
238  {
239  basegfx::B2DPolyPolygon aResult;
240 
241  for(auto const& rPolygon : std::as_const(io_rLinePolyPolygon))
242  {
243  basegfx::B2DPolyPolygon aLineTarget;
245  rPolygon,
246  fDotDashArray,
247  &aLineTarget);
248  aResult.append(aLineTarget);
249  }
250 
251  io_rLinePolyPolygon = aResult;
252  }
253  }
254 
255  if(!(GetWidth() > 1 && io_rLinePolyPolygon.count()))
256  return;
257 
258  const double fHalfLineWidth((GetWidth() * 0.5) + 0.5);
259 
260  for(auto const& rPolygon : std::as_const(io_rLinePolyPolygon))
261  {
262  o_rFillPolyPolygon.append(basegfx::utils::createAreaGeometry(
263  rPolygon,
264  fHalfLineWidth,
265  GetLineJoin(),
266  GetLineCap()));
267  }
268 
269  io_rLinePolyPolygon.clear();
270 }
271 
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvStream & WriteUInt16(sal_uInt16 nUInt16)
SvStream & WriteInt32(sal_Int32 nInt32)
SvStream & ReadUInt16(sal_uInt16 &rUInt16)
B2DPolyPolygon createAreaGeometry(const B2DPolygon &rCandidate, double fHalfLineWidth, B2DLineJoin eJoin, css::drawing::LineCap eCap, double fMaxAllowedAngle=basegfx::deg2rad(12.5), double fMaxPartOfEdge=0.4, double fMiterMinimumAngle=basegfx::deg2rad(15.0), basegfx::triangulator::B2DTriangleVector *pTriangles=nullptr)
LineStyle
Definition: vclenum.hxx:184
exports com.sun.star. drawing
B2IRange fround(const B2DRange &rRange)
double mnDotLen
Definition: lineinfo.hxx:37
void applyLineDashing(const B2DPolygon &rCandidate, const std::vector< double > &rDotDashArray, B2DPolyPolygon *pLineTarget, B2DPolyPolygon *pGapTarget, double fDotDashLength)
SvStream & ReadLineInfo(SvStream &rIStm, LineInfo &rLineInfo)
Definition: lineinfo.cxx:129
uno_Any a
double mnDashLen
Definition: lineinfo.hxx:36
B2DLineJoin
sal_uInt16 mnDotCount
Definition: lineinfo.hxx:45
SvStream & ReadDouble(double &rDouble)
SvStream & ReadInt32(sal_Int32 &rInt32)
bool operator==(const ImplLineInfo &) const
Definition: lineinfo.cxx:42
void append(const B2DPolygon &rPolygon, sal_uInt32 nCount=1)
sal_uInt32 count() const
sal_uInt16 GetVersion() const
LineStyle meStyle
Definition: lineinfo.hxx:42
basegfx::B2DLineJoin meLineJoin
Definition: lineinfo.hxx:40
double mnWidth
Definition: lineinfo.hxx:35
double mnWidth
css::drawing::LineCap meLineCap
Definition: lineinfo.hxx:41
double mnDistance
Definition: lineinfo.hxx:38
SvStream & WriteDouble(const double &rDouble)
std::string Round
SvStream & WriteLineInfo(SvStream &rOStm, const LineInfo &rLineInfo)
Definition: lineinfo.cxx:177
sal_uInt16 mnDashCount
Definition: lineinfo.hxx:44