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