LibreOffice Module vcl (master)  1
polygon.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 
25 #include <memory>
26 #include <tools/poly.hxx>
27 #include <vcl/gdimtf.hxx>
28 #include <vcl/metaact.hxx>
29 #include <vcl/outdev.hxx>
30 #include <vcl/virdev.hxx>
31 
32 #include <salgdi.hxx>
33 
34 #define OUTDEV_POLYPOLY_STACKBUF 32
35 
37 {
39 
40  if( mpMetaFile )
41  mpMetaFile->AddAction( new MetaPolyPolygonAction( rPolyPoly ) );
42 
43  sal_uInt16 nPoly = rPolyPoly.Count();
44 
45  if ( !IsDeviceOutputNecessary() || (!mbLineColor && !mbFillColor) || !nPoly || ImplIsRecordLayout() )
46  return;
47 
48  // we need a graphics
49  if ( !mpGraphics && !AcquireGraphics() )
50  return;
51 
52  if ( mbInitClipRegion )
54 
55  if ( mbOutputClipped )
56  return;
57 
58  if ( mbInitLineColor )
59  InitLineColor();
60 
61  if ( mbInitFillColor )
62  InitFillColor();
63 
64  // use b2dpolygon drawing if possible
67  (IsLineColor() || IsFillColor()))
68  {
70  basegfx::B2DPolyPolygon aB2DPolyPolygon(rPolyPoly.getB2DPolyPolygon());
71  bool bSuccess(true);
72 
73  // ensure closed - may be asserted, will prevent buffering
74  if(!aB2DPolyPolygon.isClosed())
75  {
76  aB2DPolyPolygon.setClosed(true);
77  }
78 
79  if(IsFillColor())
80  {
81  bSuccess = mpGraphics->DrawPolyPolygon(
82  aTransform,
83  aB2DPolyPolygon,
84  0.0,
85  this);
86  }
87 
88  if(bSuccess && IsLineColor())
89  {
90  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
91 
92  for(auto const& rPolygon : aB2DPolyPolygon)
93  {
94  bSuccess = mpGraphics->DrawPolyLine(
95  aTransform,
96  rPolygon,
97  0.0,
98  0.0, // tdf#124848 hairline
99  nullptr, // MM01
101  css::drawing::LineCap_BUTT,
102  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
103  bPixelSnapHairline,
104  this);
105  if (!bSuccess)
106  break;
107  }
108  }
109 
110  if(bSuccess)
111  {
112  if( mpAlphaVDev )
113  mpAlphaVDev->DrawPolyPolygon( rPolyPoly );
114  return;
115  }
116  }
117 
118  if ( nPoly == 1 )
119  {
120  // #100127# Map to DrawPolygon
121  const tools::Polygon& aPoly = rPolyPoly.GetObject( 0 );
122  if( aPoly.GetSize() >= 2 )
123  {
124  GDIMetaFile* pOldMF = mpMetaFile;
125  mpMetaFile = nullptr;
126 
127  DrawPolygon( aPoly );
128 
129  mpMetaFile = pOldMF;
130  }
131  }
132  else
133  {
134  // #100127# moved real tools::PolyPolygon draw to separate method,
135  // have to call recursively, avoiding duplicate
136  // ImplLogicToDevicePixel calls
137  ImplDrawPolyPolygon( nPoly, ImplLogicToDevicePixel( rPolyPoly ) );
138  }
139  if( mpAlphaVDev )
140  mpAlphaVDev->DrawPolyPolygon( rPolyPoly );
141 }
142 
144 {
146 
147  // AW: Do NOT paint empty polygons
148  if(rB2DPolygon.count())
149  {
150  basegfx::B2DPolyPolygon aPP( rB2DPolygon );
151  DrawPolyPolygon( aPP );
152  }
153 }
154 
156 {
158 
159  if( mpMetaFile )
160  mpMetaFile->AddAction( new MetaPolygonAction( rPoly ) );
161 
162  sal_uInt16 nPoints = rPoly.GetSize();
163 
164  if ( !IsDeviceOutputNecessary() || (!mbLineColor && !mbFillColor) || (nPoints < 2) || ImplIsRecordLayout() )
165  return;
166 
167  // we need a graphics
168  if ( !mpGraphics && !AcquireGraphics() )
169  return;
170 
171  if ( mbInitClipRegion )
172  InitClipRegion();
173 
174  if ( mbOutputClipped )
175  return;
176 
177  if ( mbInitLineColor )
178  InitLineColor();
179 
180  if ( mbInitFillColor )
181  InitFillColor();
182 
183  // use b2dpolygon drawing if possible
186  (IsLineColor() || IsFillColor()))
187  {
189  basegfx::B2DPolygon aB2DPolygon(rPoly.getB2DPolygon());
190  bool bSuccess(true);
191 
192  // ensure closed - maybe assert, hinders buffering
193  if(!aB2DPolygon.isClosed())
194  {
195  aB2DPolygon.setClosed(true);
196  }
197 
198  if(IsFillColor())
199  {
200  bSuccess = mpGraphics->DrawPolyPolygon(
201  aTransform,
202  basegfx::B2DPolyPolygon(aB2DPolygon),
203  0.0,
204  this);
205  }
206 
207  if(bSuccess && IsLineColor())
208  {
209  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
210 
211  bSuccess = mpGraphics->DrawPolyLine(
212  aTransform,
213  aB2DPolygon,
214  0.0,
215  0.0, // tdf#124848 hairline
216  nullptr, // MM01
218  css::drawing::LineCap_BUTT,
219  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
220  bPixelSnapHairline,
221  this);
222  }
223 
224  if(bSuccess)
225  {
226  if( mpAlphaVDev )
227  mpAlphaVDev->DrawPolygon( rPoly );
228  return;
229  }
230  }
231 
232  tools::Polygon aPoly = ImplLogicToDevicePixel( rPoly );
233  const Point* pPtAry = aPoly.GetConstPointAry();
234 
235  // #100127# Forward beziers to sal, if any
236  if( aPoly.HasFlags() )
237  {
238  const PolyFlags* pFlgAry = aPoly.GetConstFlagAry();
239  if( !mpGraphics->DrawPolygonBezier( nPoints, pPtAry, pFlgAry, this ) )
240  {
241  aPoly = tools::Polygon::SubdivideBezier(aPoly);
242  pPtAry = aPoly.GetConstPointAry();
243  mpGraphics->DrawPolygon( aPoly.GetSize(), pPtAry, this );
244  }
245  }
246  else
247  {
248  mpGraphics->DrawPolygon( nPoints, pPtAry, this );
249  }
250  if( mpAlphaVDev )
251  mpAlphaVDev->DrawPolygon( rPoly );
252 }
253 
254 // Caution: This method is nearly the same as
255 // OutputDevice::DrawTransparent( const basegfx::B2DPolyPolygon& rB2DPolyPoly, double fTransparency),
256 // so when changes are made here do not forget to make changes there, too
257 
259 {
261 
262  if( mpMetaFile )
263  mpMetaFile->AddAction( new MetaPolyPolygonAction( tools::PolyPolygon( rB2DPolyPoly ) ) );
264 
265  // call helper
267 }
268 
270 {
271  // Do not paint empty PolyPolygons
272  if(!rB2DPolyPoly.count() || !IsDeviceOutputNecessary())
273  return;
274 
275  // we need a graphics
276  if( !mpGraphics && !AcquireGraphics() )
277  return;
278 
279  if( mbInitClipRegion )
280  InitClipRegion();
281 
282  if( mbOutputClipped )
283  return;
284 
285  if( mbInitLineColor )
286  InitLineColor();
287 
288  if( mbInitFillColor )
289  InitFillColor();
290 
291  bool bSuccess(false);
292 
295  (IsLineColor() || IsFillColor()))
296  {
298  basegfx::B2DPolyPolygon aB2DPolyPolygon(rB2DPolyPoly);
299  bSuccess = true;
300 
301  // ensure closed - maybe assert, hinders buffering
302  if(!aB2DPolyPolygon.isClosed())
303  {
304  aB2DPolyPolygon.setClosed(true);
305  }
306 
307  if(IsFillColor())
308  {
309  bSuccess = mpGraphics->DrawPolyPolygon(
310  aTransform,
311  aB2DPolyPolygon,
312  0.0,
313  this);
314  }
315 
316  if(bSuccess && IsLineColor())
317  {
318  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
319 
320  for(auto const& rPolygon : aB2DPolyPolygon)
321  {
322  bSuccess = mpGraphics->DrawPolyLine(
323  aTransform,
324  rPolygon,
325  0.0,
326  0.0, // tdf#124848 hairline
327  nullptr, // MM01
329  css::drawing::LineCap_BUTT,
330  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
331  bPixelSnapHairline,
332  this);
333  if (!bSuccess)
334  break;
335  }
336  }
337  }
338 
339  if (!bSuccess)
340  {
341  // fallback to old polygon drawing if needed
342  const tools::PolyPolygon aToolsPolyPolygon(rB2DPolyPoly);
343  const tools::PolyPolygon aPixelPolyPolygon = ImplLogicToDevicePixel(aToolsPolyPolygon);
344  ImplDrawPolyPolygon(aPixelPolyPolygon.Count(), aPixelPolyPolygon);
345  }
346 
347  if (mpAlphaVDev)
348  mpAlphaVDev->ImplDrawPolyPolygonWithB2DPolyPolygon(rB2DPolyPoly);
349 }
350 
351 // #100127# Extracted from OutputDevice::DrawPolyPolygon()
352 void OutputDevice::ImplDrawPolyPolygon( sal_uInt16 nPoly, const tools::PolyPolygon& rPolyPoly )
353 {
354  // AW: This crashes on empty PolyPolygons, avoid that
355  if(!nPoly)
356  return;
357 
358  sal_uInt32 aStackAry1[OUTDEV_POLYPOLY_STACKBUF];
359  const Point* aStackAry2[OUTDEV_POLYPOLY_STACKBUF];
360  PolyFlags* aStackAry3[OUTDEV_POLYPOLY_STACKBUF];
361  sal_uInt32* pPointAry;
362  const Point** pPointAryAry;
363  const PolyFlags** pFlagAryAry;
364  sal_uInt16 i = 0;
365  sal_uInt16 j = 0;
366  sal_uInt16 last = 0;
367  bool bHaveBezier = false;
368  if ( nPoly > OUTDEV_POLYPOLY_STACKBUF )
369  {
370  pPointAry = new sal_uInt32[nPoly];
371  pPointAryAry = new const Point*[nPoly];
372  pFlagAryAry = new const PolyFlags*[nPoly];
373  }
374  else
375  {
376  pPointAry = aStackAry1;
377  pPointAryAry = aStackAry2;
378  pFlagAryAry = const_cast<const PolyFlags**>(aStackAry3);
379  }
380 
381  do
382  {
383  const tools::Polygon& rPoly = rPolyPoly.GetObject( i );
384  sal_uInt16 nSize = rPoly.GetSize();
385  if ( nSize )
386  {
387  pPointAry[j] = nSize;
388  pPointAryAry[j] = rPoly.GetConstPointAry();
389  pFlagAryAry[j] = rPoly.GetConstFlagAry();
390  last = i;
391 
392  if( pFlagAryAry[j] )
393  bHaveBezier = true;
394 
395  ++j;
396  }
397  ++i;
398  }
399  while ( i < nPoly );
400 
401  if ( j == 1 )
402  {
403  // #100127# Forward beziers to sal, if any
404  if( bHaveBezier )
405  {
406  if( !mpGraphics->DrawPolygonBezier( *pPointAry, *pPointAryAry, *pFlagAryAry, this ) )
407  {
408  tools::Polygon aPoly = tools::Polygon::SubdivideBezier( rPolyPoly.GetObject( last ) );
409  mpGraphics->DrawPolygon( aPoly.GetSize(), aPoly.GetConstPointAry(), this );
410  }
411  }
412  else
413  {
414  mpGraphics->DrawPolygon( *pPointAry, *pPointAryAry, this );
415  }
416  }
417  else
418  {
419  // #100127# Forward beziers to sal, if any
420  if( bHaveBezier )
421  {
422  if( !mpGraphics->DrawPolyPolygonBezier( j, pPointAry, pPointAryAry, pFlagAryAry, this ) )
423  {
425  ImplDrawPolyPolygon( aPolyPoly.Count(), aPolyPoly );
426  }
427  }
428  else
429  {
430  mpGraphics->DrawPolyPolygon( j, pPointAry, pPointAryAry, this );
431  }
432  }
433 
434  if ( pPointAry != aStackAry1 )
435  {
436  delete[] pPointAry;
437  delete[] pPointAryAry;
438  delete[] pFlagAryAry;
439  }
440 }
441 
442 void OutputDevice::ImplDrawPolygon( const tools::Polygon& rPoly, const tools::PolyPolygon* pClipPolyPoly )
443 {
444  if( pClipPolyPoly )
445  {
446  ImplDrawPolyPolygon( rPoly, pClipPolyPoly );
447  }
448  else
449  {
450  sal_uInt16 nPoints = rPoly.GetSize();
451 
452  if ( nPoints < 2 )
453  return;
454 
455  const Point* pPtAry = rPoly.GetConstPointAry();
456  mpGraphics->DrawPolygon( nPoints, pPtAry, this );
457  }
458 }
459 
460 void OutputDevice::ImplDrawPolyPolygon( const tools::PolyPolygon& rPolyPoly, const tools::PolyPolygon* pClipPolyPoly )
461 {
462  tools::PolyPolygon* pPolyPoly;
463 
464  if( pClipPolyPoly )
465  {
466  pPolyPoly = new tools::PolyPolygon;
467  rPolyPoly.GetIntersection( *pClipPolyPoly, *pPolyPoly );
468  }
469  else
470  {
471  pPolyPoly = const_cast<tools::PolyPolygon*>(&rPolyPoly);
472  }
473  if( pPolyPoly->Count() == 1 )
474  {
475  const tools::Polygon& rPoly = pPolyPoly->GetObject( 0 );
476  sal_uInt16 nSize = rPoly.GetSize();
477 
478  if( nSize >= 2 )
479  {
480  const Point* pPtAry = rPoly.GetConstPointAry();
481  mpGraphics->DrawPolygon( nSize, pPtAry, this );
482  }
483  }
484  else if( pPolyPoly->Count() )
485  {
486  sal_uInt16 nCount = pPolyPoly->Count();
487  std::unique_ptr<sal_uInt32[]> pPointAry(new sal_uInt32[nCount]);
488  std::unique_ptr<const Point*[]> pPointAryAry(new const Point*[nCount]);
489  sal_uInt16 i = 0;
490  do
491  {
492  const tools::Polygon& rPoly = pPolyPoly->GetObject( i );
493  sal_uInt16 nSize = rPoly.GetSize();
494  if ( nSize )
495  {
496  pPointAry[i] = nSize;
497  pPointAryAry[i] = rPoly.GetConstPointAry();
498  i++;
499  }
500  else
501  nCount--;
502  }
503  while( i < nCount );
504 
505  if( nCount == 1 )
506  mpGraphics->DrawPolygon( pPointAry[0], pPointAryAry[0], this );
507  else
508  mpGraphics->DrawPolyPolygon( nCount, pPointAry.get(), pPointAryAry.get(), this );
509  }
510 
511  if( pClipPolyPoly )
512  delete pPolyPoly;
513 }
514 
515 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 Count() const
const tools::Polygon & GetObject(sal_uInt16 nPos) const
SAL_DLLPRIVATE bool ImplIsRecordLayout() const
Definition: outdev.cxx:636
void setClosed(bool bNew)
virtual bool supportsOperation(OutDevSupportType) const =0
void DrawPolygon(const tools::Polygon &rPoly)
Render the given polygon.
Definition: polygon.cxx:155
bool mbOutputClipped
Definition: outdev.hxx:378
static Polygon SubdivideBezier(const Polygon &rPoly)
SAL_DLLPRIVATE bool is_double_buffered_window() const
void GetIntersection(const tools::PolyPolygon &rPolyPoly, tools::PolyPolygon &rResult) const
static tools::PolyPolygon SubdivideBezier(const tools::PolyPolygon &rPolyPoly)
SAL_DLLPRIVATE void InitLineColor()
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
int nCount
AntialiasingFlags mnAntialiasing
Definition: outdev.hxx:370
SalGraphics * mpGraphics
Graphics context to draw on.
Definition: outdev.hxx:314
SAL_DLLPRIVATE basegfx::B2DHomMatrix ImplGetDeviceTransformation() const
Get device transformation.
Definition: map.cxx:819
const PolyFlags * GetConstFlagAry() const
SAL_DLLPRIVATE void InitFillColor()
bool isClosed() const
int i
const char last[]
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.
void DrawPolyLine(sal_uInt32 nPoints, Point const *pPtAry, const OutputDevice *pOutDev)
bool mbInitLineColor
Definition: outdev.hxx:381
bool HasFlags() const
bool mbLineColor
Definition: outdev.hxx:379
sal_uInt16 GetSize() const
constexpr double deg2rad(double v)
::basegfx::B2DPolygon getB2DPolygon() const
bool DrawPolyPolygonBezier(sal_uInt32 nPoly, const sal_uInt32 *pPoints, const Point *const *pPtAry, const PolyFlags *const *pFlgAry, const OutputDevice *pOutDev)
void DrawPolyPolygon(sal_uInt32 nPoly, const sal_uInt32 *pPoints, const Point **pPtAry, const OutputDevice *pOutDev)
SAL_DLLPRIVATE void ImplDrawPolyPolygonWithB2DPolyPolygon(const basegfx::B2DPolyPolygon &rB2DPolyPoly)
Definition: polygon.cxx:269
void DrawPolygon(sal_uInt32 nPoints, const Point *pPtAry, const OutputDevice *pOutDev)
bool mbFillColor
Definition: outdev.hxx:380
VclPtr< VirtualDevice > mpAlphaVDev
Definition: outdev.hxx:329
sal_uInt32 count() const
::basegfx::B2DPolyPolygon getB2DPolyPolygon() const
SAL_DLLPRIVATE void ImplDrawPolyPolygon(const tools::PolyPolygon &rPolyPoly, const tools::PolyPolygon *pClipPolyPoly)
Definition: polygon.cxx:460
void AddAction(const rtl::Reference< MetaAction > &pAction)
Definition: gdimtf.cxx:566
bool mbInitClipRegion
Definition: outdev.hxx:385
void DrawPolyPolygon(const tools::PolyPolygon &rPolyPoly)
Render the given poly-polygon.
Definition: polygon.cxx:36
virtual void InitClipRegion()
#define OUTDEV_POLYPOLY_STACKBUF
Definition: polygon.cxx:34
RasterOp GetRasterOp() const
Definition: outdev.hxx:615
bool mbInitFillColor
Definition: outdev.hxx:382
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 IsFillColor() const
Definition: outdev.hxx:635
bool IsDeviceOutputNecessary() const
Definition: outdev.hxx:600
GDIMetaFile * mpMetaFile
Definition: outdev.hxx:317
PolyFlags
bool DrawPolygonBezier(sal_uInt32 nPoints, const Point *pPtAry, const PolyFlags *pFlgAry, const OutputDevice *pOutDev)
sal_uInt32 count() const
SAL_DLLPRIVATE void ImplDrawPolygon(const tools::Polygon &rPoly, const tools::PolyPolygon *pClipPolyPoly=nullptr)
Definition: polygon.cxx:442