LibreOffice Module vcl (master)  1
polyline.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 
22 #include <sal/types.h>
23 
26 #include <vcl/gdimtf.hxx>
27 #include <vcl/metaact.hxx>
28 #include <vcl/outdev.hxx>
29 #include <vcl/virdev.hxx>
30 
31 #include <salgdi.hxx>
32 
34 {
35  assert(!is_double_buffered_window());
36 
37  if( mpMetaFile )
38  mpMetaFile->AddAction( new MetaPolyLineAction( rPoly ) );
39 
40  sal_uInt16 nPoints = rPoly.GetSize();
41 
42  if ( !IsDeviceOutputNecessary() || !mbLineColor || (nPoints < 2) || ImplIsRecordLayout() )
43  return;
44 
45  // we need a graphics
46  if ( !mpGraphics && !AcquireGraphics() )
47  return;
48  assert(mpGraphics);
49 
50  if ( mbInitClipRegion )
52 
53  if ( mbOutputClipped )
54  return;
55 
56  if ( mbInitLineColor )
57  InitLineColor();
58 
59  // use b2dpolygon drawing if possible
62  rPoly.getB2DPolygon()))
63  {
64  return;
65  }
66 
67  const basegfx::B2DPolygon aB2DPolyLine(rPoly.getB2DPolygon());
69  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
70 
71  bool bDrawn = mpGraphics->DrawPolyLine(
72  aTransform,
73  aB2DPolyLine,
74  0.0,
75  0.0, // tdf#124848 hairline
76  nullptr, // MM01
78  css::drawing::LineCap_BUTT,
79  basegfx::deg2rad(15.0) /*default fMiterMinimumAngle, not used*/,
80  bPixelSnapHairline,
81  *this);
82 
83  if(!bDrawn)
84  {
85  tools::Polygon aPoly = ImplLogicToDevicePixel( rPoly );
86  Point* pPtAry = aPoly.GetPointAry();
87 
88  // #100127# Forward beziers to sal, if any
89  if( aPoly.HasFlags() )
90  {
91  const PolyFlags* pFlgAry = aPoly.GetConstFlagAry();
92  if( !mpGraphics->DrawPolyLineBezier( nPoints, pPtAry, pFlgAry, *this ) )
93  {
94  aPoly = tools::Polygon::SubdivideBezier(aPoly);
95  pPtAry = aPoly.GetPointAry();
96  mpGraphics->DrawPolyLine( aPoly.GetSize(), pPtAry, *this );
97  }
98  }
99  else
100  {
101  mpGraphics->DrawPolyLine( nPoints, pPtAry, *this );
102  }
103  }
104 
105  if( mpAlphaVDev )
106  mpAlphaVDev->DrawPolyLine( rPoly );
107 }
108 
109 void OutputDevice::DrawPolyLine( const tools::Polygon& rPoly, const LineInfo& rLineInfo )
110 {
111  assert(!is_double_buffered_window());
112 
113  if ( rLineInfo.IsDefault() )
114  {
115  DrawPolyLine( rPoly );
116  return;
117  }
118 
119  // #i101491#
120  // Try direct Fallback to B2D-Version of DrawPolyLine
121  if(LineStyle::Solid == rLineInfo.GetStyle())
122  {
123  DrawPolyLine(
124  rPoly.getB2DPolygon(),
125  rLineInfo.GetWidth(),
126  rLineInfo.GetLineJoin(),
127  rLineInfo.GetLineCap(),
128  basegfx::deg2rad(15.0) /* default fMiterMinimumAngle, value not available in LineInfo */);
129  return;
130  }
131 
132  if ( mpMetaFile )
133  mpMetaFile->AddAction( new MetaPolyLineAction( rPoly, rLineInfo ) );
134 
135  drawPolyLine(rPoly, rLineInfo);
136 }
137 
139  double fLineWidth,
140  basegfx::B2DLineJoin eLineJoin,
141  css::drawing::LineCap eLineCap,
142  double fMiterMinimumAngle)
143 {
144  assert(!is_double_buffered_window());
145 
146  if( mpMetaFile )
147  {
148  LineInfo aLineInfo;
149  if( fLineWidth != 0.0 )
150  aLineInfo.SetWidth( fLineWidth );
151 
152  const tools::Polygon aToolsPolygon( rB2DPolygon );
153  mpMetaFile->AddAction( new MetaPolyLineAction( aToolsPolygon, aLineInfo ) );
154  }
155 
156  // Do not paint empty PolyPolygons
157  if(!rB2DPolygon.count() || !IsDeviceOutputNecessary())
158  return;
159 
160  // we need a graphics
161  if( !mpGraphics && !AcquireGraphics() )
162  return;
163  assert(mpGraphics);
164 
165  if( mbInitClipRegion )
166  InitClipRegion();
167 
168  if( mbOutputClipped )
169  return;
170 
171  if( mbInitLineColor )
172  InitLineColor();
173 
174  // use b2dpolygon drawing if possible
177  rB2DPolygon,
178  fLineWidth,
179  0.0,
180  nullptr, // MM01
181  eLineJoin,
182  eLineCap,
183  fMiterMinimumAngle))
184  {
185  return;
186  }
187 
188  // #i101491#
189  // no output yet; fallback to geometry decomposition and use filled polygon paint
190  // when line is fat and not too complex. ImplDrawPolyPolygonWithB2DPolyPolygon
191  // will do internal needed AA checks etc.
192  if(fLineWidth >= 2.5 &&
193  rB2DPolygon.count() &&
194  rB2DPolygon.count() <= 1000)
195  {
196  const double fHalfLineWidth((fLineWidth * 0.5) + 0.5);
197  const basegfx::B2DPolyPolygon aAreaPolyPolygon(
199  fHalfLineWidth,
200  eLineJoin,
201  eLineCap,
202  fMiterMinimumAngle));
203  const Color aOldLineColor(maLineColor);
204  const Color aOldFillColor(maFillColor);
205 
206  SetLineColor();
207  InitLineColor();
208  SetFillColor(aOldLineColor);
209  InitFillColor();
210 
211  // draw using a loop; else the topology will paint a PolyPolygon
212  for(auto const& rPolygon : aAreaPolyPolygon)
213  {
215  basegfx::B2DPolyPolygon(rPolygon));
216  }
217 
218  SetLineColor(aOldLineColor);
219  InitLineColor();
220  SetFillColor(aOldFillColor);
221  InitFillColor();
222 
223  // when AA it is necessary to also paint the filled polygon's outline
224  // to avoid optical gaps
225  for(auto const& rPolygon : aAreaPolyPolygon)
226  {
229  rPolygon);
230  }
231  }
232  else
233  {
234  // fallback to old polygon drawing if needed
235  const tools::Polygon aToolsPolygon( rB2DPolygon );
236  LineInfo aLineInfo;
237  if( fLineWidth != 0.0 )
238  aLineInfo.SetWidth( fLineWidth );
239 
240  drawPolyLine( aToolsPolygon, aLineInfo );
241  }
242 }
243 
244 void OutputDevice::drawPolyLine(const tools::Polygon& rPoly, const LineInfo& rLineInfo)
245 {
246  sal_uInt16 nPoints(rPoly.GetSize());
247 
248  if ( !IsDeviceOutputNecessary() || !mbLineColor || ( nPoints < 2 ) || ( LineStyle::NONE == rLineInfo.GetStyle() ) || ImplIsRecordLayout() )
249  return;
250 
251  // we need a graphics
252  if ( !mpGraphics && !AcquireGraphics() )
253  return;
254  assert(mpGraphics);
255 
256  if ( mbInitClipRegion )
257  InitClipRegion();
258 
259  if ( mbOutputClipped )
260  return;
261 
262  if ( mbInitLineColor )
263  InitLineColor();
264 
265  const LineInfo aInfo( ImplLogicToDevicePixel( rLineInfo ) );
266  const bool bDashUsed(LineStyle::Dash == aInfo.GetStyle());
267  const bool bLineWidthUsed(aInfo.GetWidth() > 1);
268 
269  if (bDashUsed || bLineWidthUsed)
270  {
272  drawLine(basegfx::B2DPolyPolygon(aPoly), aInfo);
273  }
274  else
275  {
276  tools::Polygon aPoly = ImplLogicToDevicePixel(rPoly);
277 
278  // #100127# the subdivision HAS to be done here since only a pointer
279  // to an array of points is given to the DrawPolyLine method, there is
280  // NO way to find out there that it's a curve.
281  if( aPoly.HasFlags() )
282  {
283  aPoly = tools::Polygon::SubdivideBezier( aPoly );
284  nPoints = aPoly.GetSize();
285  }
286 
287  mpGraphics->DrawPolyLine(nPoints, aPoly.GetPointAry(), *this);
288  }
289 
290  if( mpAlphaVDev )
291  mpAlphaVDev->DrawPolyLine( rPoly, rLineInfo );
292 }
293 
295  const basegfx::B2DHomMatrix& rObjectTransform,
296  const basegfx::B2DPolygon& rB2DPolygon,
297  double fLineWidth,
298  double fTransparency,
299  const std::vector< double >* pStroke, // MM01
300  basegfx::B2DLineJoin eLineJoin,
301  css::drawing::LineCap eLineCap,
302  double fMiterMinimumAngle)
303 {
304  if(DrawPolyLineDirectInternal(rObjectTransform, rB2DPolygon, fLineWidth, fTransparency,
305  pStroke, eLineJoin, eLineCap, fMiterMinimumAngle))
306  {
307  // Worked, add metafile action (if recorded). This is done only here,
308  // because this function is public, other OutDev functions already add metafile
309  // actions, so they call the internal function directly.
310  if( mpMetaFile )
311  {
312  LineInfo aLineInfo;
313  if( fLineWidth != 0.0 )
314  aLineInfo.SetWidth( fLineWidth );
315  // Transport known information, might be needed
316  aLineInfo.SetLineJoin(eLineJoin);
317  aLineInfo.SetLineCap(eLineCap);
318  // MiterMinimumAngle does not exist yet in LineInfo
319  const tools::Polygon aToolsPolygon( rB2DPolygon );
320  mpMetaFile->AddAction( new MetaPolyLineAction( aToolsPolygon, aLineInfo ) );
321  }
322  return true;
323  }
324  return false;
325 }
326 
328  const basegfx::B2DHomMatrix& rObjectTransform,
329  const basegfx::B2DPolygon& rB2DPolygon,
330  double fLineWidth,
331  double fTransparency,
332  const std::vector< double >* pStroke, // MM01
333  basegfx::B2DLineJoin eLineJoin,
334  css::drawing::LineCap eLineCap,
335  double fMiterMinimumAngle)
336 {
337  assert(!is_double_buffered_window());
338 
339  // AW: Do NOT paint empty PolyPolygons
340  if(!rB2DPolygon.count())
341  return true;
342 
343  // we need a graphics
344  if( !mpGraphics && !AcquireGraphics() )
345  return false;
346  assert(mpGraphics);
347 
348  if( mbInitClipRegion )
349  InitClipRegion();
350 
351  if( mbOutputClipped )
352  return true;
353 
354  if( mbInitLineColor )
355  InitLineColor();
356 
359  IsLineColor());
360 
361  if(bTryB2d)
362  {
363  // combine rObjectTransform with WorldToDevice
364  const basegfx::B2DHomMatrix aTransform(ImplGetDeviceTransformation() * rObjectTransform);
365  const bool bPixelSnapHairline((mnAntialiasing & AntialiasingFlags::PixelSnapHairline) && rB2DPolygon.count() < 1000);
366 
367  const double fAdjustedTransparency = mpAlphaVDev ? 0 : fTransparency;
368  // draw the polyline
369  bool bDrawSuccess = mpGraphics->DrawPolyLine(
370  aTransform,
371  rB2DPolygon,
372  fAdjustedTransparency,
373  fLineWidth, // tdf#124848 use LineWidth direct, do not try to solve for zero-case (aka hairline)
374  pStroke, // MM01
375  eLineJoin,
376  eLineCap,
377  fMiterMinimumAngle,
378  bPixelSnapHairline,
379  *this);
380 
381  if( bDrawSuccess )
382  {
383  if (mpAlphaVDev)
384  mpAlphaVDev->DrawPolyLineDirect(rObjectTransform, rB2DPolygon, fLineWidth,
385  fTransparency, pStroke, eLineJoin, eLineCap,
386  fMiterMinimumAngle);
387 
388  return true;
389  }
390  }
391  return false;
392 }
393 
394 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPRIVATE bool ImplIsRecordLayout() const
Definition: outdev.cxx:702
virtual bool supportsOperation(OutDevSupportType) const =0
Color maLineColor
Definition: outdev.hxx:224
bool mbOutputClipped
Definition: outdev.hxx:243
static Polygon SubdivideBezier(const Polygon &rPoly)
void DrawPolyLine(const tools::Polygon &rPoly)
Render the given polygon as a line stroke.
Definition: polyline.cxx:33
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()
Definition: line.cxx:84
bool DrawPolyLineDirectInternal(const basegfx::B2DHomMatrix &rObjectTransform, const basegfx::B2DPolygon &rB2DPolygon, double fLineWidth=0.0, double fTransparency=0.0, const std::vector< double > *=nullptr, basegfx::B2DLineJoin eLineJoin=basegfx::B2DLineJoin::NONE, css::drawing::LineCap eLineCap=css::drawing::LineCap_BUTT, double fMiterMinimumAngle=basegfx::deg2rad(15.0))
Definition: polyline.cxx:327
AntialiasingFlags mnAntialiasing
Definition: outdev.hxx:235
Color maFillColor
Definition: outdev.hxx:225
SalGraphics * mpGraphics
Graphics context to draw on.
Definition: outdev.hxx:180
SAL_DLLPRIVATE basegfx::B2DHomMatrix ImplGetDeviceTransformation() const
Get device transformation.
Definition: map.cxx:869
bool DrawPolyLineDirect(const basegfx::B2DHomMatrix &rObjectTransform, const basegfx::B2DPolygon &rB2DPolygon, double fLineWidth=0.0, double fTransparency=0.0, const std::vector< double > *=nullptr, basegfx::B2DLineJoin eLineJoin=basegfx::B2DLineJoin::NONE, css::drawing::LineCap eLineCap=css::drawing::LineCap_BUTT, double fMiterMinimumAngle=basegfx::deg2rad(15.0))
Definition: polyline.cxx:294
void SetLineColor()
Definition: line.cxx:36
const PolyFlags * GetConstFlagAry() const
SAL_DLLPRIVATE void InitFillColor()
Definition: fill.cxx:76
Point * GetPointAry()
bool IsLineColor() const
Definition: outdev.hxx:506
virtual bool AcquireGraphics() const =0
Acquire a graphics device that the output device uses to draw on.
bool mbInitLineColor
Definition: outdev.hxx:246
bool HasFlags() const
void SetFillColor()
Definition: fill.cxx:29
void DrawPolyLine(sal_uInt32 nPoints, Point const *pPtAry, const OutputDevice &rOutDev)
bool mbLineColor
Definition: outdev.hxx:244
sal_uInt16 GetSize() const
constexpr double deg2rad(double v)
::basegfx::B2DPolygon getB2DPolygon() const
bool DrawPolyLineBezier(sal_uInt32 nPoints, const Point *pPtAry, const PolyFlags *pFlgAry, const OutputDevice &rOutDev)
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:224
SAL_DLLPRIVATE void ImplDrawPolyPolygonWithB2DPolyPolygon(const basegfx::B2DPolyPolygon &rB2DPolyPoly)
Definition: polygon.cxx:271
VclPtr< VirtualDevice > mpAlphaVDev
Definition: outdev.hxx:194
void AddAction(const rtl::Reference< MetaAction > &pAction)
Definition: gdimtf.cxx:562
bool mbInitClipRegion
Definition: outdev.hxx:250
virtual void InitClipRegion()
RasterOp GetRasterOp() const
Definition: outdev.hxx:491
SAL_DLLPRIVATE void drawPolyLine(const tools::Polygon &rPoly, const LineInfo &rLineInfo)
Definition: polyline.cxx:244
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:386
bool IsDeviceOutputNecessary() const
Definition: outdev.hxx:476
GDIMetaFile * mpMetaFile
Definition: outdev.hxx:183
PolyFlags
sal_uInt32 count() const
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo