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 
49  if ( mbInitClipRegion )
51 
52  if ( mbOutputClipped )
53  return;
54 
55  if ( mbInitLineColor )
56  InitLineColor();
57 
58  // use b2dpolygon drawing if possible
61  rPoly.getB2DPolygon()))
62  {
63  return;
64  }
65 
66  const basegfx::B2DPolygon aB2DPolyLine(rPoly.getB2DPolygon());
68  const basegfx::B2DVector aB2DLineWidth( 1.0, 1.0 );
69  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
70 
72  aTransform,
73  aB2DPolyLine,
74  0.0,
75  aB2DLineWidth,
77  css::drawing::LineCap_BUTT,
78  basegfx::deg2rad(15.0) /*default fMiterMinimumAngle, not used*/,
79  bPixelSnapHairline,
80  this))
81  {
82  return;
83  }
84 
85  tools::Polygon aPoly = ImplLogicToDevicePixel( rPoly );
86  SalPoint* pPtAry = reinterpret_cast<SalPoint*>(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 = reinterpret_cast<SalPoint*>(aPoly.GetPointAry());
96  mpGraphics->DrawPolyLine( aPoly.GetSize(), pPtAry, this );
97  }
98  }
99  else
100  {
101  mpGraphics->DrawPolyLine( nPoints, pPtAry, this );
102  }
103 
104  if( mpAlphaVDev )
105  mpAlphaVDev->DrawPolyLine( rPoly );
106 }
107 
108 void OutputDevice::DrawPolyLine( const tools::Polygon& rPoly, const LineInfo& rLineInfo )
109 {
110  assert(!is_double_buffered_window());
111 
112  if ( rLineInfo.IsDefault() )
113  {
114  DrawPolyLine( rPoly );
115  return;
116  }
117 
118  // #i101491#
119  // Try direct Fallback to B2D-Version of DrawPolyLine
121  LineStyle::Solid == rLineInfo.GetStyle())
122  {
123  DrawPolyLine(
124  rPoly.getB2DPolygon(),
125  static_cast< double >(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( static_cast<long>(fLineWidth+0.5) );
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 
164  if( mbInitClipRegion )
165  InitClipRegion();
166 
167  if( mbOutputClipped )
168  return;
169 
170  if( mbInitLineColor )
171  InitLineColor();
172 
173  // use b2dpolygon drawing if possible
176  rB2DPolygon,
177  fLineWidth,
178  0.0,
179  eLineJoin,
180  eLineCap,
181  fMiterMinimumAngle))
182  {
183  return;
184  }
185 
186  // #i101491#
187  // no output yet; fallback to geometry decomposition and use filled polygon paint
188  // when line is fat and not too complex. ImplDrawPolyPolygonWithB2DPolyPolygon
189  // will do internal needed AA checks etc.
190  if(fLineWidth >= 2.5 &&
191  rB2DPolygon.count() &&
192  rB2DPolygon.count() <= 1000)
193  {
194  const double fHalfLineWidth((fLineWidth * 0.5) + 0.5);
195  const basegfx::B2DPolyPolygon aAreaPolyPolygon(
197  fHalfLineWidth,
198  eLineJoin,
199  eLineCap,
200  fMiterMinimumAngle));
201  const Color aOldLineColor(maLineColor);
202  const Color aOldFillColor(maFillColor);
203 
204  SetLineColor();
205  InitLineColor();
206  SetFillColor(aOldLineColor);
207  InitFillColor();
208 
209  // draw using a loop; else the topology will paint a PolyPolygon
210  for(auto const& rPolygon : aAreaPolyPolygon)
211  {
213  basegfx::B2DPolyPolygon(rPolygon));
214  }
215 
216  SetLineColor(aOldLineColor);
217  InitLineColor();
218  SetFillColor(aOldFillColor);
219  InitFillColor();
220 
221  const bool bTryAA((mnAntialiasing & AntialiasingFlags::EnableB2dDraw) &&
224  IsLineColor());
225 
226  // when AA it is necessary to also paint the filled polygon's outline
227  // to avoid optical gaps
228  for(auto const& rPolygon : aAreaPolyPolygon)
229  {
232  rPolygon,
233  0.0,
234  0.0,
236  css::drawing::LineCap_BUTT,
237  basegfx::deg2rad(15.0) /*default, not used*/,
238  bTryAA);
239  }
240  }
241  else
242  {
243  // fallback to old polygon drawing if needed
244  const tools::Polygon aToolsPolygon( rB2DPolygon );
245  LineInfo aLineInfo;
246  if( fLineWidth != 0.0 )
247  aLineInfo.SetWidth( static_cast<long>(fLineWidth+0.5) );
248 
249  drawPolyLine( aToolsPolygon, aLineInfo );
250  }
251 }
252 
253 void OutputDevice::drawPolyLine(const tools::Polygon& rPoly, const LineInfo& rLineInfo)
254 {
255  sal_uInt16 nPoints(rPoly.GetSize());
256 
257  if ( !IsDeviceOutputNecessary() || !mbLineColor || ( nPoints < 2 ) || ( LineStyle::NONE == rLineInfo.GetStyle() ) || ImplIsRecordLayout() )
258  return;
259 
260  tools::Polygon aPoly = ImplLogicToDevicePixel( rPoly );
261 
262  // we need a graphics
263  if ( !mpGraphics && !AcquireGraphics() )
264  return;
265 
266  if ( mbInitClipRegion )
267  InitClipRegion();
268 
269  if ( mbOutputClipped )
270  return;
271 
272  if ( mbInitLineColor )
273  InitLineColor();
274 
275  const LineInfo aInfo( ImplLogicToDevicePixel( rLineInfo ) );
276  const bool bDashUsed(LineStyle::Dash == aInfo.GetStyle());
277  const bool bLineWidthUsed(aInfo.GetWidth() > 1);
278 
279  if(bDashUsed || bLineWidthUsed)
280  {
281  drawLine ( basegfx::B2DPolyPolygon(aPoly.getB2DPolygon()), aInfo );
282  }
283  else
284  {
285  // #100127# the subdivision HAS to be done here since only a pointer
286  // to an array of points is given to the DrawPolyLine method, there is
287  // NO way to find out there that it's a curve.
288  if( aPoly.HasFlags() )
289  {
290  aPoly = tools::Polygon::SubdivideBezier( aPoly );
291  nPoints = aPoly.GetSize();
292  }
293 
294  mpGraphics->DrawPolyLine(nPoints, reinterpret_cast<SalPoint*>(aPoly.GetPointAry()), this);
295  }
296 
297  if( mpAlphaVDev )
298  mpAlphaVDev->DrawPolyLine( rPoly, rLineInfo );
299 }
300 
302  const basegfx::B2DHomMatrix& rObjectTransform,
303  const basegfx::B2DPolygon& rB2DPolygon,
304  double fLineWidth,
305  double fTransparency,
306  basegfx::B2DLineJoin eLineJoin,
307  css::drawing::LineCap eLineCap,
308  double fMiterMinimumAngle,
309  bool bBypassAACheck)
310 {
311  assert(!is_double_buffered_window());
312 
313  // AW: Do NOT paint empty PolyPolygons
314  if(!rB2DPolygon.count())
315  return true;
316 
317  // we need a graphics
318  if( !mpGraphics && !AcquireGraphics() )
319  return false;
320 
321  if( mbInitClipRegion )
322  InitClipRegion();
323 
324  if( mbOutputClipped )
325  return true;
326 
327  if( mbInitLineColor )
328  InitLineColor();
329 
330  const bool bTryAA( bBypassAACheck ||
334  IsLineColor()));
335 
336  if(bTryAA)
337  {
338  // combine rObjectTransform with WorldToDevice
339  const basegfx::B2DHomMatrix aTransform(ImplGetDeviceTransformation() * rObjectTransform);
340  const bool bLineWidthZero(basegfx::fTools::equalZero(fLineWidth));
341  const basegfx::B2DVector aB2DLineWidth(bLineWidthZero ? 1.0 : fLineWidth, bLineWidthZero ? 1.0 : fLineWidth);
342  const bool bPixelSnapHairline((mnAntialiasing & AntialiasingFlags::PixelSnapHairline) && rB2DPolygon.count() < 1000);
343 
344  // draw the polyline
345  bool bDrawSuccess = mpGraphics->DrawPolyLine(
346  aTransform,
347  rB2DPolygon,
348  fTransparency,
349  aB2DLineWidth,
350  eLineJoin,
351  eLineCap,
352  fMiterMinimumAngle,
353  bPixelSnapHairline,
354  this);
355 
356  if( bDrawSuccess )
357  {
358  // worked, add metafile action (if recorded) and return true
359  if( mpMetaFile )
360  {
361  LineInfo aLineInfo;
362  if( fLineWidth != 0.0 )
363  aLineInfo.SetWidth( static_cast<long>(fLineWidth+0.5) );
364  // Transport known information, might be needed
365  aLineInfo.SetLineJoin(eLineJoin);
366  aLineInfo.SetLineCap(eLineCap);
367  // MiterMinimumAngle does not exist yet in LineInfo
368  const tools::Polygon aToolsPolygon( rB2DPolygon );
369  mpMetaFile->AddAction( new MetaPolyLineAction( aToolsPolygon, aLineInfo ) );
370  }
371  return true;
372  }
373  }
374  return false;
375 }
376 
377 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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
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()
AntialiasingFlags mnAntialiasing
Definition: outdev.hxx:373
Color maFillColor
Definition: outdev.hxx:363
void DrawPolyLine(sal_uInt32 nPoints, SalPoint const *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 SetLineColor()
const PolyFlags * GetConstFlagAry() const
SAL_DLLPRIVATE void InitFillColor()
Point * GetPointAry()
bool DrawPolyLineDirect(const basegfx::B2DHomMatrix &rObjectTransform, const basegfx::B2DPolygon &rB2DPolygon, double fLineWidth=0.0, double fTransparency=0.0, basegfx::B2DLineJoin eLineJoin=basegfx::B2DLineJoin::NONE, css::drawing::LineCap eLineCap=css::drawing::LineCap_BUTT, double fMiterMinimumAngle=basegfx::deg2rad(15.0), bool bBypassAACheck=false)
Definition: polyline.cxx:301
bool IsLineColor() const
Definition: outdev.hxx:619
virtual bool AcquireGraphics() const =0
Acquire a graphics device that the output device uses to draw on.
bool mbInitLineColor
Definition: outdev.hxx:384
bool HasFlags() const
static bool equalZero(const double &rfVal)
void SetFillColor()
bool mbLineColor
Definition: outdev.hxx:382
sal_uInt16 GetSize() const
constexpr double deg2rad(double v)
::basegfx::B2DPolygon getB2DPolygon() 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
SAL_DLLPRIVATE void ImplDrawPolyPolygonWithB2DPolyPolygon(const basegfx::B2DPolyPolygon &rB2DPolyPoly)
Definition: polygon.cxx:271
VclPtr< VirtualDevice > mpAlphaVDev
Definition: outdev.hxx:331
void AddAction(const rtl::Reference< MetaAction > &pAction)
Definition: gdimtf.cxx:539
bool mbInitClipRegion
Definition: outdev.hxx:388
virtual void InitClipRegion()
RasterOp GetRasterOp() const
Definition: outdev.hxx:604
SAL_DLLPRIVATE void drawPolyLine(const tools::Polygon &rPoly, const LineInfo &rLineInfo)
Definition: polyline.cxx:253
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
bool DrawPolyLineBezier(sal_uInt32 nPoints, const SalPoint *pPtAry, const PolyFlags *pFlgAry, const OutputDevice *pOutDev)
bool IsDeviceOutputNecessary() const
Definition: outdev.hxx:589
GDIMetaFile * mpMetaFile
Definition: outdev.hxx:319
PolyFlags
sal_uInt32 count() const
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo