LibreOffice Module vcl (master)  1
line.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 <cassert>
21 #include <numeric>
22 
23 #include <vcl/gdimtf.hxx>
24 #include <vcl/lineinfo.hxx>
25 #include <vcl/metaact.hxx>
26 #include <vcl/outdev.hxx>
27 #include <vcl/virdev.hxx>
28 
29 #include <salgdi.hxx>
30 
35 
36 void OutputDevice::DrawLine( const Point& rStartPt, const Point& rEndPt,
37  const LineInfo& rLineInfo )
38 {
40 
41  if ( rLineInfo.IsDefault() )
42  {
43  DrawLine( rStartPt, rEndPt );
44  return;
45  }
46 
47  if ( mpMetaFile )
48  mpMetaFile->AddAction( new MetaLineAction( rStartPt, rEndPt, rLineInfo ) );
49 
50  if ( !IsDeviceOutputNecessary() || !mbLineColor || ( LineStyle::NONE == rLineInfo.GetStyle() ) || ImplIsRecordLayout() )
51  return;
52 
53  if( !mpGraphics && !AcquireGraphics() )
54  return;
55 
56  if ( mbInitClipRegion )
58 
59  if ( mbOutputClipped )
60  return;
61 
62  const Point aStartPt( ImplLogicToDevicePixel( rStartPt ) );
63  const Point aEndPt( ImplLogicToDevicePixel( rEndPt ) );
64  const LineInfo aInfo( ImplLogicToDevicePixel( rLineInfo ) );
65  const bool bDashUsed(LineStyle::Dash == aInfo.GetStyle());
66  const bool bLineWidthUsed(aInfo.GetWidth() > 1);
67 
68  if ( mbInitLineColor )
69  InitLineColor();
70 
71  if(bDashUsed || bLineWidthUsed)
72  {
73  basegfx::B2DPolygon aLinePolygon;
74  aLinePolygon.append(basegfx::B2DPoint(aStartPt.X(), aStartPt.Y()));
75  aLinePolygon.append(basegfx::B2DPoint(aEndPt.X(), aEndPt.Y()));
76 
77  drawLine( basegfx::B2DPolyPolygon(aLinePolygon), aInfo );
78  }
79  else
80  {
81  mpGraphics->DrawLine( aStartPt.X(), aStartPt.Y(), aEndPt.X(), aEndPt.Y(), *this );
82  }
83 
84  if( mpAlphaVDev )
85  mpAlphaVDev->DrawLine( rStartPt, rEndPt, rLineInfo );
86 }
87 
88 void OutputDevice::DrawLine( const Point& rStartPt, const Point& rEndPt )
89 {
91 
92  if ( mpMetaFile )
93  mpMetaFile->AddAction( new MetaLineAction( rStartPt, rEndPt ) );
94 
96  return;
97 
98  if ( !mpGraphics && !AcquireGraphics() )
99  return;
100 
101  if ( mbInitClipRegion )
102  InitClipRegion();
103 
104  if ( mbOutputClipped )
105  return;
106 
107  if ( mbInitLineColor )
108  InitLineColor();
109 
110  bool bDrawn = false;
111 
112  // #i101598# support AA and snap for lines, too
115  && IsLineColor())
116  {
117  // at least transform with double precision to device coordinates; this will
118  // avoid pixel snap of single, appended lines
120  basegfx::B2DPolygon aB2DPolyLine;
121 
122  aB2DPolyLine.append(basegfx::B2DPoint(rStartPt.X(), rStartPt.Y()));
123  aB2DPolyLine.append(basegfx::B2DPoint(rEndPt.X(), rEndPt.Y()));
124  aB2DPolyLine.transform( aTransform );
125 
126  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
127 
128  bDrawn = mpGraphics->DrawPolyLine(
130  aB2DPolyLine,
131  0.0,
132  0.0, // tdf#124848 hairline
133  nullptr, // MM01
135  css::drawing::LineCap_BUTT,
136  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
137  bPixelSnapHairline,
138  this);
139  }
140  if(!bDrawn)
141  {
142  const Point aStartPt(ImplLogicToDevicePixel(rStartPt));
143  const Point aEndPt(ImplLogicToDevicePixel(rEndPt));
144  mpGraphics->DrawLine( aStartPt.X(), aStartPt.Y(), aEndPt.X(), aEndPt.Y(), *this );
145  }
146 
147  if( mpAlphaVDev )
148  mpAlphaVDev->DrawLine( rStartPt, rEndPt );
149 }
150 
151 void OutputDevice::drawLine( basegfx::B2DPolyPolygon aLinePolyPolygon, const LineInfo& rInfo )
152 {
155  && IsLineColor());
156  basegfx::B2DPolyPolygon aFillPolyPolygon;
157  const bool bDashUsed(LineStyle::Dash == rInfo.GetStyle());
158  const bool bLineWidthUsed(rInfo.GetWidth() > 1);
159 
160  if(bDashUsed && aLinePolyPolygon.count())
161  {
162  ::std::vector< double > fDotDashArray;
163  const double fDashLen(rInfo.GetDashLen());
164  const double fDotLen(rInfo.GetDotLen());
165  const double fDistance(rInfo.GetDistance());
166 
167  for(sal_uInt16 a(0); a < rInfo.GetDashCount(); a++)
168  {
169  fDotDashArray.push_back(fDashLen);
170  fDotDashArray.push_back(fDistance);
171  }
172 
173  for(sal_uInt16 b(0); b < rInfo.GetDotCount(); b++)
174  {
175  fDotDashArray.push_back(fDotLen);
176  fDotDashArray.push_back(fDistance);
177  }
178 
179  const double fAccumulated(::std::accumulate(fDotDashArray.begin(), fDotDashArray.end(), 0.0));
180 
181  if(fAccumulated > 0.0)
182  {
183  basegfx::B2DPolyPolygon aResult;
184 
185  for(auto const& rPolygon : aLinePolyPolygon)
186  {
187  basegfx::B2DPolyPolygon aLineTarget;
189  rPolygon,
190  fDotDashArray,
191  &aLineTarget);
192  aResult.append(aLineTarget);
193  }
194 
195  aLinePolyPolygon = aResult;
196  }
197  }
198 
199  if(bLineWidthUsed && aLinePolyPolygon.count())
200  {
201  const double fHalfLineWidth((rInfo.GetWidth() * 0.5) + 0.5);
202 
203  if(aLinePolyPolygon.areControlPointsUsed())
204  {
205  // #i110768# When area geometry has to be created, do not
206  // use the fallback bezier decomposition inside createAreaGeometry,
207  // but one that is at least as good as ImplSubdivideBezier was.
208  // There, Polygon::AdaptiveSubdivide was used with default parameter
209  // 1.0 as quality index.
210  aLinePolyPolygon = basegfx::utils::adaptiveSubdivideByDistance(aLinePolyPolygon, 1.0);
211  }
212 
213  for(auto const& rPolygon : aLinePolyPolygon)
214  {
215  aFillPolyPolygon.append(basegfx::utils::createAreaGeometry(
216  rPolygon,
217  fHalfLineWidth,
218  rInfo.GetLineJoin(),
219  rInfo.GetLineCap()));
220  }
221 
222  aLinePolyPolygon.clear();
223  }
224 
225  GDIMetaFile* pOldMetaFile = mpMetaFile;
226  mpMetaFile = nullptr;
227 
228  if(aLinePolyPolygon.count())
229  {
230  for(auto const& rB2DPolygon : aLinePolyPolygon)
231  {
232  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
233  bool bDone(false);
234 
235  if(bTryB2d)
236  {
237  bDone = mpGraphics->DrawPolyLine(
239  rB2DPolygon,
240  0.0,
241  0.0, // tdf#124848 hairline
242  nullptr, // MM01
244  css::drawing::LineCap_BUTT,
245  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
246  bPixelSnapHairline,
247  this);
248  }
249 
250  if(!bDone)
251  {
252  tools::Polygon aPolygon(rB2DPolygon);
254  aPolygon.GetSize(),
255  aPolygon.GetPointAry(),
256  *this);
257  }
258  }
259  }
260 
261  if(aFillPolyPolygon.count())
262  {
263  const Color aOldLineColor( maLineColor );
264  const Color aOldFillColor( maFillColor );
265 
266  SetLineColor();
267  InitLineColor();
268  SetFillColor( aOldLineColor );
269  InitFillColor();
270 
271  bool bDone(false);
272 
273  if(bTryB2d)
274  {
275  bDone = mpGraphics->DrawPolyPolygon(
277  aFillPolyPolygon,
278  0.0,
279  this);
280  }
281 
282  if(!bDone)
283  {
284  for(auto const& rB2DPolygon : aFillPolyPolygon)
285  {
286  tools::Polygon aPolygon(rB2DPolygon);
287 
288  // need to subdivide, mpGraphics->DrawPolygon ignores curves
289  aPolygon.AdaptiveSubdivide(aPolygon);
290  mpGraphics->DrawPolygon(aPolygon.GetSize(), aPolygon.GetConstPointAry(), *this);
291  }
292  }
293 
294  SetFillColor( aOldFillColor );
295  SetLineColor( aOldLineColor );
296  }
297 
298  mpMetaFile = pOldMetaFile;
299 }
300 
301 
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void append(const basegfx::B2DPoint &rPoint, sal_uInt32 nCount)
SAL_DLLPRIVATE bool ImplIsRecordLayout() const
Definition: outdev.cxx:638
void DrawLine(tools::Long nX1, tools::Long nY1, tools::Long nX2, tools::Long nY2, const OutputDevice &rOutDev)
virtual bool supportsOperation(OutDevSupportType) const =0
Color maLineColor
Definition: outdev.hxx:359
bool mbOutputClipped
Definition: outdev.hxx:378
void DrawPolyPolygon(sal_uInt32 nPoly, const sal_uInt32 *pPoints, const Point **pPtAry, const OutputDevice &rOutDev)
SAL_DLLPRIVATE bool is_double_buffered_window() const
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)
SAL_DLLPRIVATE void InitLineColor()
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
AntialiasingFlags mnAntialiasing
Definition: outdev.hxx:370
Color maFillColor
Definition: outdev.hxx:360
void DrawLine(const Point &rStartPt, const Point &rEndPt)
Definition: line.cxx:88
SalGraphics * mpGraphics
Graphics context to draw on.
Definition: outdev.hxx:314
SAL_DLLPRIVATE basegfx::B2DHomMatrix ImplGetDeviceTransformation() const
Get device transformation.
Definition: map.cxx:819
void applyLineDashing(const B2DPolygon &rCandidate, const std::vector< double > &rDotDashArray, B2DPolyPolygon *pLineTarget, B2DPolyPolygon *pGapTarget, double fDotDashLength)
void SetLineColor()
SAL_DLLPRIVATE void InitFillColor()
void DrawPolygon(sal_uInt32 nPoints, const Point *pPtAry, const OutputDevice &rOutDev)
uno_Any a
Point * GetPointAry()
bool IsLineColor() const
Definition: outdev.hxx:630
const Point * GetConstPointAry() const
virtual bool AcquireGraphics() const =0
Acquire a graphics device that the output device uses to draw on.
bool mbInitLineColor
Definition: outdev.hxx:381
void SetFillColor()
void DrawPolyLine(sal_uInt32 nPoints, Point const *pPtAry, const OutputDevice &rOutDev)
bool mbLineColor
Definition: outdev.hxx:379
sal_uInt16 GetSize() const
constexpr double deg2rad(double v)
SAL_DLLPRIVATE void drawLine(basegfx::B2DPolyPolygon aLinePolyPolygon, const LineInfo &rInfo)
Helper for line geometry paint with support for graphic expansion (pattern and fat_to_area) ...
Definition: line.cxx:151
void transform(const basegfx::B2DHomMatrix &rMatrix)
void append(const B2DPolygon &rPolygon, sal_uInt32 nCount=1)
VclPtr< VirtualDevice > mpAlphaVDev
Definition: outdev.hxx:329
sal_uInt32 count() const
void AddAction(const rtl::Reference< MetaAction > &pAction)
Definition: gdimtf.cxx:566
bool mbInitClipRegion
Definition: outdev.hxx:385
B2DPolygon adaptiveSubdivideByDistance(const B2DPolygon &rCandidate, double fDistanceBound)
virtual void InitClipRegion()
RasterOp GetRasterOp() const
Definition: outdev.hxx:615
void AdaptiveSubdivide(tools::Polygon &rResult, const double d=1.0) const
SAL_DLLPRIVATE tools::Rectangle ImplLogicToDevicePixel(const tools::Rectangle &rLogicRect) const
Convert a logical rectangle to a rectangle in physical device pixel units.
Definition: map.cxx:406
bool areControlPointsUsed() const
bool IsDeviceOutputNecessary() const
Definition: outdev.hxx:600
GDIMetaFile * mpMetaFile
Definition: outdev.hxx:317