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  const basegfx::B2DVector aB2DLineWidth( 1.0, 1.0 );
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 
130  aB2DPolyLine,
131  0.0,
132  aB2DLineWidth,
134  css::drawing::LineCap_BUTT,
135  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
136  bPixelSnapHairline,
137  this))
138  {
139  return;
140  }
141  }
142 
143  const Point aStartPt(ImplLogicToDevicePixel(rStartPt));
144  const Point aEndPt(ImplLogicToDevicePixel(rEndPt));
145 
146  mpGraphics->DrawLine( aStartPt.X(), aStartPt.Y(), aEndPt.X(), aEndPt.Y(), this );
147 
148  if( mpAlphaVDev )
149  mpAlphaVDev->DrawLine( rStartPt, rEndPt );
150 }
151 
152 void OutputDevice::drawLine( basegfx::B2DPolyPolygon aLinePolyPolygon, const LineInfo& rInfo )
153 {
157  && IsLineColor());
158  basegfx::B2DPolyPolygon aFillPolyPolygon;
159  const bool bDashUsed(LineStyle::Dash == rInfo.GetStyle());
160  const bool bLineWidthUsed(rInfo.GetWidth() > 1);
161 
162  if(bDashUsed && aLinePolyPolygon.count())
163  {
164  ::std::vector< double > fDotDashArray;
165  const double fDashLen(rInfo.GetDashLen());
166  const double fDotLen(rInfo.GetDotLen());
167  const double fDistance(rInfo.GetDistance());
168 
169  for(sal_uInt16 a(0); a < rInfo.GetDashCount(); a++)
170  {
171  fDotDashArray.push_back(fDashLen);
172  fDotDashArray.push_back(fDistance);
173  }
174 
175  for(sal_uInt16 b(0); b < rInfo.GetDotCount(); b++)
176  {
177  fDotDashArray.push_back(fDotLen);
178  fDotDashArray.push_back(fDistance);
179  }
180 
181  const double fAccumulated(::std::accumulate(fDotDashArray.begin(), fDotDashArray.end(), 0.0));
182 
183  if(fAccumulated > 0.0)
184  {
185  basegfx::B2DPolyPolygon aResult;
186 
187  for(auto const& rPolygon : aLinePolyPolygon)
188  {
189  basegfx::B2DPolyPolygon aLineTarget;
191  rPolygon,
192  fDotDashArray,
193  &aLineTarget);
194  aResult.append(aLineTarget);
195  }
196 
197  aLinePolyPolygon = aResult;
198  }
199  }
200 
201  if(bLineWidthUsed && aLinePolyPolygon.count())
202  {
203  const double fHalfLineWidth((rInfo.GetWidth() * 0.5) + 0.5);
204 
205  if(aLinePolyPolygon.areControlPointsUsed())
206  {
207  // #i110768# When area geometry has to be created, do not
208  // use the fallback bezier decomposition inside createAreaGeometry,
209  // but one that is at least as good as ImplSubdivideBezier was.
210  // There, Polygon::AdaptiveSubdivide was used with default parameter
211  // 1.0 as quality index.
212  aLinePolyPolygon = basegfx::utils::adaptiveSubdivideByDistance(aLinePolyPolygon, 1.0);
213  }
214 
215  for(auto const& rPolygon : aLinePolyPolygon)
216  {
217  aFillPolyPolygon.append(basegfx::utils::createAreaGeometry(
218  rPolygon,
219  fHalfLineWidth,
220  rInfo.GetLineJoin(),
221  rInfo.GetLineCap()));
222  }
223 
224  aLinePolyPolygon.clear();
225  }
226 
227  GDIMetaFile* pOldMetaFile = mpMetaFile;
228  mpMetaFile = nullptr;
229 
230  if(aLinePolyPolygon.count())
231  {
232  for(auto const& rB2DPolygon : aLinePolyPolygon)
233  {
234  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
235  bool bDone(false);
236 
237  if(bTryAA)
238  {
239  bDone = mpGraphics->DrawPolyLine(
241  rB2DPolygon,
242  0.0,
243  basegfx::B2DVector(1.0,1.0),
245  css::drawing::LineCap_BUTT,
246  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
247  bPixelSnapHairline,
248  this);
249  }
250 
251  if(!bDone)
252  {
253  tools::Polygon aPolygon(rB2DPolygon);
255  aPolygon.GetSize(),
256  reinterpret_cast<SalPoint*>(aPolygon.GetPointAry()),
257  this);
258  }
259  }
260  }
261 
262  if(aFillPolyPolygon.count())
263  {
264  const Color aOldLineColor( maLineColor );
265  const Color aOldFillColor( maFillColor );
266 
267  SetLineColor();
268  InitLineColor();
269  SetFillColor( aOldLineColor );
270  InitFillColor();
271 
272  bool bDone(false);
273 
274  if(bTryAA)
275  {
276  bDone = mpGraphics->DrawPolyPolygon(
278  aFillPolyPolygon,
279  0.0,
280  this);
281  }
282 
283  if(!bDone)
284  {
285  for(auto const& rB2DPolygon : aFillPolyPolygon)
286  {
287  tools::Polygon aPolygon(rB2DPolygon);
288 
289  // need to subdivide, mpGraphics->DrawPolygon ignores curves
290  aPolygon.AdaptiveSubdivide(aPolygon);
291  mpGraphics->DrawPolygon(aPolygon.GetSize(), reinterpret_cast<const SalPoint*>(aPolygon.GetConstPointAry()), this);
292  }
293  }
294 
295  SetFillColor( aOldFillColor );
296  SetLineColor( aOldLineColor );
297  }
298 
299  mpMetaFile = pOldMetaFile;
300 }
301 
302 
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void append(const basegfx::B2DPoint &rPoint, sal_uInt32 nCount)
SAL_DLLPRIVATE bool ImplIsRecordLayout() const
Definition: outdev.cxx:657
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
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:931
void applyLineDashing(const B2DPolygon &rCandidate, const std::vector< double > &rDotDashArray, B2DPolyPolygon *pLineTarget, B2DPolyPolygon *pGapTarget, double fDotDashLength)
void SetLineColor()
SAL_DLLPRIVATE void InitFillColor()
Point * GetPointAry()
bool IsLineColor() const
Definition: outdev.hxx:619
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)
long X() const
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:152
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:540
bool mbInitClipRegion
Definition: outdev.hxx:388
B2DPolygon adaptiveSubdivideByDistance(const B2DPolygon &rCandidate, double fDistanceBound)
virtual void InitClipRegion()
RasterOp GetRasterOp() const
Definition: outdev.hxx:604
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:589
long Y() const
GDIMetaFile * mpMetaFile
Definition: outdev.hxx:319