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 {
39  assert(!is_double_buffered_window());
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 {
90  assert(!is_double_buffered_window());
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  // #i101598# support AA and snap for lines, too
114  && IsLineColor())
115  {
116  // at least transform with double precision to device coordinates; this will
117  // avoid pixel snap of single, appended lines
119  basegfx::B2DPolygon aB2DPolyLine;
120 
121  aB2DPolyLine.append(basegfx::B2DPoint(rStartPt.X(), rStartPt.Y()));
122  aB2DPolyLine.append(basegfx::B2DPoint(rEndPt.X(), rEndPt.Y()));
123  aB2DPolyLine.transform( aTransform );
124 
125  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
126 
129  aB2DPolyLine,
130  0.0,
131  // 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  return;
141  }
142  }
143 
144  const Point aStartPt(ImplLogicToDevicePixel(rStartPt));
145  const Point aEndPt(ImplLogicToDevicePixel(rEndPt));
146 
147  mpGraphics->DrawLine( aStartPt.X(), aStartPt.Y(), aEndPt.X(), aEndPt.Y(), this );
148 
149  if( mpAlphaVDev )
150  mpAlphaVDev->DrawLine( rStartPt, rEndPt );
151 }
152 
153 void OutputDevice::drawLine( basegfx::B2DPolyPolygon aLinePolyPolygon, const LineInfo& rInfo )
154 {
158  && IsLineColor());
159  basegfx::B2DPolyPolygon aFillPolyPolygon;
160  const bool bDashUsed(LineStyle::Dash == rInfo.GetStyle());
161  const bool bLineWidthUsed(rInfo.GetWidth() > 1);
162 
163  if(bDashUsed && aLinePolyPolygon.count())
164  {
165  ::std::vector< double > fDotDashArray;
166  const double fDashLen(rInfo.GetDashLen());
167  const double fDotLen(rInfo.GetDotLen());
168  const double fDistance(rInfo.GetDistance());
169 
170  for(sal_uInt16 a(0); a < rInfo.GetDashCount(); a++)
171  {
172  fDotDashArray.push_back(fDashLen);
173  fDotDashArray.push_back(fDistance);
174  }
175 
176  for(sal_uInt16 b(0); b < rInfo.GetDotCount(); b++)
177  {
178  fDotDashArray.push_back(fDotLen);
179  fDotDashArray.push_back(fDistance);
180  }
181 
182  const double fAccumulated(::std::accumulate(fDotDashArray.begin(), fDotDashArray.end(), 0.0));
183 
184  if(fAccumulated > 0.0)
185  {
186  basegfx::B2DPolyPolygon aResult;
187 
188  for(auto const& rPolygon : aLinePolyPolygon)
189  {
190  basegfx::B2DPolyPolygon aLineTarget;
192  rPolygon,
193  fDotDashArray,
194  &aLineTarget);
195  aResult.append(aLineTarget);
196  }
197 
198  aLinePolyPolygon = aResult;
199  }
200  }
201 
202  if(bLineWidthUsed && aLinePolyPolygon.count())
203  {
204  const double fHalfLineWidth((rInfo.GetWidth() * 0.5) + 0.5);
205 
206  if(aLinePolyPolygon.areControlPointsUsed())
207  {
208  // #i110768# When area geometry has to be created, do not
209  // use the fallback bezier decomposition inside createAreaGeometry,
210  // but one that is at least as good as ImplSubdivideBezier was.
211  // There, Polygon::AdaptiveSubdivide was used with default parameter
212  // 1.0 as quality index.
213  aLinePolyPolygon = basegfx::utils::adaptiveSubdivideByDistance(aLinePolyPolygon, 1.0);
214  }
215 
216  for(auto const& rPolygon : aLinePolyPolygon)
217  {
218  aFillPolyPolygon.append(basegfx::utils::createAreaGeometry(
219  rPolygon,
220  fHalfLineWidth,
221  rInfo.GetLineJoin(),
222  rInfo.GetLineCap()));
223  }
224 
225  aLinePolyPolygon.clear();
226  }
227 
228  GDIMetaFile* pOldMetaFile = mpMetaFile;
229  mpMetaFile = nullptr;
230 
231  if(aLinePolyPolygon.count())
232  {
233  for(auto const& rB2DPolygon : aLinePolyPolygon)
234  {
235  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
236  bool bDone(false);
237 
238  if(bTryAA)
239  {
240  bDone = mpGraphics->DrawPolyLine(
242  rB2DPolygon,
243  0.0,
244  // tdf#124848 hairline
246  nullptr, // MM01
248  css::drawing::LineCap_BUTT,
249  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
250  bPixelSnapHairline,
251  this);
252  }
253 
254  if(!bDone)
255  {
256  tools::Polygon aPolygon(rB2DPolygon);
258  aPolygon.GetSize(),
259  reinterpret_cast<SalPoint*>(aPolygon.GetPointAry()),
260  this);
261  }
262  }
263  }
264 
265  if(aFillPolyPolygon.count())
266  {
267  const Color aOldLineColor( maLineColor );
268  const Color aOldFillColor( maFillColor );
269 
270  SetLineColor();
271  InitLineColor();
272  SetFillColor( aOldLineColor );
273  InitFillColor();
274 
275  bool bDone(false);
276 
277  if(bTryAA)
278  {
279  bDone = mpGraphics->DrawPolyPolygon(
281  aFillPolyPolygon,
282  0.0,
283  this);
284  }
285 
286  if(!bDone)
287  {
288  for(auto const& rB2DPolygon : aFillPolyPolygon)
289  {
290  tools::Polygon aPolygon(rB2DPolygon);
291 
292  // need to subdivide, mpGraphics->DrawPolygon ignores curves
293  aPolygon.AdaptiveSubdivide(aPolygon);
294  mpGraphics->DrawPolygon(aPolygon.GetSize(), reinterpret_cast<const SalPoint*>(aPolygon.GetConstPointAry()), this);
295  }
296  }
297 
298  SetFillColor( aOldFillColor );
299  SetLineColor( aOldLineColor );
300  }
301 
302  mpMetaFile = pOldMetaFile;
303 }
304 
305 
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void append(const basegfx::B2DPoint &rPoint, sal_uInt32 nCount)
SAL_DLLPRIVATE bool ImplIsRecordLayout() const
Definition: outdev.cxx:650
virtual bool supportsOperation(OutDevSupportType) const =0
Color maLineColor
Definition: outdev.hxx:362
bool mbOutputClipped
Definition: outdev.hxx:381
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()
AntialiasingFlags mnAntialiasing
Definition: outdev.hxx:373
static const B2DVector & getEmptyVector()
Color maFillColor
Definition: outdev.hxx:363
void DrawLine(const Point &rStartPt, const Point &rEndPt)
Definition: line.cxx:88
void DrawPolyLine(sal_uInt32 nPoints, SalPoint const *pPtAry, const OutputDevice *pOutDev)
void DrawPolyPolygon(sal_uInt32 nPoly, const sal_uInt32 *pPoints, PCONSTSALPOINT *pPtAry, const OutputDevice *pOutDev)
void DrawPolygon(sal_uInt32 nPoints, const SalPoint *pPtAry, const OutputDevice *pOutDev)
SalGraphics * mpGraphics
Graphics context to draw on.
Definition: outdev.hxx:316
SAL_DLLPRIVATE basegfx::B2DHomMatrix ImplGetDeviceTransformation() const
Get device transformation.
Definition: map.cxx:925
void applyLineDashing(const B2DPolygon &rCandidate, const std::vector< double > &rDotDashArray, B2DPolyPolygon *pLineTarget, B2DPolyPolygon *pGapTarget, double fDotDashLength)
void SetLineColor()
SAL_DLLPRIVATE void InitFillColor()
uno_Any a
Point * GetPointAry()
bool IsLineColor() const
Definition: outdev.hxx:625
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:384
void SetFillColor()
bool mbLineColor
Definition: outdev.hxx:382
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:153
void transform(const basegfx::B2DHomMatrix &rMatrix)
void append(const B2DPolygon &rPolygon, sal_uInt32 nCount=1)
VclPtr< VirtualDevice > mpAlphaVDev
Definition: outdev.hxx:331
sal_uInt32 count() const
void AddAction(const rtl::Reference< MetaAction > &pAction)
Definition: gdimtf.cxx:543
bool mbInitClipRegion
Definition: outdev.hxx:388
B2DPolygon adaptiveSubdivideByDistance(const B2DPolygon &rCandidate, double fDistanceBound)
virtual void InitClipRegion()
RasterOp GetRasterOp() const
Definition: outdev.hxx:610
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:504
void DrawLine(long nX1, long nY1, long nX2, long nY2, const OutputDevice *pOutDev)
bool areControlPointsUsed() const
bool IsDeviceOutputNecessary() const
Definition: outdev.hxx:595
GDIMetaFile * mpMetaFile
Definition: outdev.hxx:319