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,
102  css::drawing::LineCap_BUTT,
103  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
104  bPixelSnapHairline,
105  this);
106  if (!bSuccess)
107  break;
108  }
109  }
110 
111  if(bSuccess)
112  {
113  if( mpAlphaVDev )
114  mpAlphaVDev->DrawPolyPolygon( rPolyPoly );
115  return;
116  }
117  }
118 
119  if ( nPoly == 1 )
120  {
121  // #100127# Map to DrawPolygon
122  const tools::Polygon& aPoly = rPolyPoly.GetObject( 0 );
123  if( aPoly.GetSize() >= 2 )
124  {
125  GDIMetaFile* pOldMF = mpMetaFile;
126  mpMetaFile = nullptr;
127 
128  DrawPolygon( aPoly );
129 
130  mpMetaFile = pOldMF;
131  }
132  }
133  else
134  {
135  // #100127# moved real tools::PolyPolygon draw to separate method,
136  // have to call recursively, avoiding duplicate
137  // ImplLogicToDevicePixel calls
138  ImplDrawPolyPolygon( nPoly, ImplLogicToDevicePixel( rPolyPoly ) );
139  }
140  if( mpAlphaVDev )
141  mpAlphaVDev->DrawPolyPolygon( rPolyPoly );
142 }
143 
145 {
146  assert(!is_double_buffered_window());
147 
148  // AW: Do NOT paint empty polygons
149  if(rB2DPolygon.count())
150  {
151  basegfx::B2DPolyPolygon aPP( rB2DPolygon );
152  DrawPolyPolygon( aPP );
153  }
154 }
155 
157 {
158  assert(!is_double_buffered_window());
159 
160  if( mpMetaFile )
161  mpMetaFile->AddAction( new MetaPolygonAction( rPoly ) );
162 
163  sal_uInt16 nPoints = rPoly.GetSize();
164 
165  if ( !IsDeviceOutputNecessary() || (!mbLineColor && !mbFillColor) || (nPoints < 2) || ImplIsRecordLayout() )
166  return;
167 
168  // we need a graphics
169  if ( !mpGraphics && !AcquireGraphics() )
170  return;
171 
172  if ( mbInitClipRegion )
173  InitClipRegion();
174 
175  if ( mbOutputClipped )
176  return;
177 
178  if ( mbInitLineColor )
179  InitLineColor();
180 
181  if ( mbInitFillColor )
182  InitFillColor();
183 
184  // use b2dpolygon drawing if possible
188  (IsLineColor() || IsFillColor()))
189  {
191  basegfx::B2DPolygon aB2DPolygon(rPoly.getB2DPolygon());
192  bool bSuccess(true);
193 
194  // ensure closed - maybe assert, hinders buffering
195  if(!aB2DPolygon.isClosed())
196  {
197  aB2DPolygon.setClosed(true);
198  }
199 
200  if(IsFillColor())
201  {
202  bSuccess = mpGraphics->DrawPolyPolygon(
203  aTransform,
204  basegfx::B2DPolyPolygon(aB2DPolygon),
205  0.0,
206  this);
207  }
208 
209  if(bSuccess && IsLineColor())
210  {
211  const basegfx::B2DVector aB2DLineWidth( 1.0, 1.0 );
212  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
213 
214  bSuccess = mpGraphics->DrawPolyLine(
215  aTransform,
216  aB2DPolygon,
217  0.0,
218  aB2DLineWidth,
220  css::drawing::LineCap_BUTT,
221  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
222  bPixelSnapHairline,
223  this);
224  }
225 
226  if(bSuccess)
227  {
228  if( mpAlphaVDev )
229  mpAlphaVDev->DrawPolygon( rPoly );
230  return;
231  }
232  }
233 
234  tools::Polygon aPoly = ImplLogicToDevicePixel( rPoly );
235  const SalPoint* pPtAry = reinterpret_cast<const SalPoint*>(aPoly.GetConstPointAry());
236 
237  // #100127# Forward beziers to sal, if any
238  if( aPoly.HasFlags() )
239  {
240  const PolyFlags* pFlgAry = aPoly.GetConstFlagAry();
241  if( !mpGraphics->DrawPolygonBezier( nPoints, pPtAry, pFlgAry, this ) )
242  {
243  aPoly = tools::Polygon::SubdivideBezier(aPoly);
244  pPtAry = reinterpret_cast<const SalPoint*>(aPoly.GetConstPointAry());
245  mpGraphics->DrawPolygon( aPoly.GetSize(), pPtAry, this );
246  }
247  }
248  else
249  {
250  mpGraphics->DrawPolygon( nPoints, pPtAry, this );
251  }
252  if( mpAlphaVDev )
253  mpAlphaVDev->DrawPolygon( rPoly );
254 }
255 
256 // Caution: This method is nearly the same as
257 // OutputDevice::DrawTransparent( const basegfx::B2DPolyPolygon& rB2DPolyPoly, double fTransparency),
258 // so when changes are made here do not forget to make changes there, too
259 
261 {
262  assert(!is_double_buffered_window());
263 
264  if( mpMetaFile )
266 
267  // call helper
269 }
270 
272 {
273  // Do not paint empty PolyPolygons
274  if(!rB2DPolyPoly.count() || !IsDeviceOutputNecessary())
275  return;
276 
277  // we need a graphics
278  if( !mpGraphics && !AcquireGraphics() )
279  return;
280 
281  if( mbInitClipRegion )
282  InitClipRegion();
283 
284  if( mbOutputClipped )
285  return;
286 
287  if( mbInitLineColor )
288  InitLineColor();
289 
290  if( mbInitFillColor )
291  InitFillColor();
292 
296  (IsLineColor() || IsFillColor()))
297  {
299  basegfx::B2DPolyPolygon aB2DPolyPolygon(rB2DPolyPoly);
300  bool bSuccess(true);
301 
302  // ensure closed - maybe assert, hinders buffering
303  if(!aB2DPolyPolygon.isClosed())
304  {
305  aB2DPolyPolygon.setClosed(true);
306  }
307 
308  if(IsFillColor())
309  {
310  bSuccess = mpGraphics->DrawPolyPolygon(
311  aTransform,
312  aB2DPolyPolygon,
313  0.0,
314  this);
315  }
316 
317  if(bSuccess && IsLineColor())
318  {
319  const basegfx::B2DVector aB2DLineWidth( 1.0, 1.0 );
320  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
321 
322  for(auto const& rPolygon : aB2DPolyPolygon)
323  {
324  bSuccess = mpGraphics->DrawPolyLine(
325  aTransform,
326  rPolygon,
327  0.0,
328  aB2DLineWidth,
330  css::drawing::LineCap_BUTT,
331  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
332  bPixelSnapHairline,
333  this);
334  if (!bSuccess)
335  break;
336  }
337  }
338 
339  if(bSuccess)
340  {
341  return;
342  }
343  }
344 
345  // fallback to old polygon drawing if needed
346  const tools::PolyPolygon aToolsPolyPolygon( rB2DPolyPoly );
347  const tools::PolyPolygon aPixelPolyPolygon = ImplLogicToDevicePixel( aToolsPolyPolygon );
348  ImplDrawPolyPolygon( aPixelPolyPolygon.Count(), aPixelPolyPolygon );
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];
360  PolyFlags* aStackAry3[OUTDEV_POLYPOLY_STACKBUF];
361  sal_uInt32* pPointAry;
362  PCONSTSALPOINT* 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 PCONSTSALPOINT[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] = reinterpret_cast<PCONSTSALPOINT>(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(), reinterpret_cast<const SalPoint*>(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 SalPoint* pPtAry = reinterpret_cast<const SalPoint*>(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 SalPoint* pPtAry = reinterpret_cast<const SalPoint*>(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<PCONSTSALPOINT[]> pPointAryAry(new PCONSTSALPOINT[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] = reinterpret_cast<PCONSTSALPOINT>(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:657
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:156
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()
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:931
const PolyFlags * GetConstFlagAry() const
SAL_DLLPRIVATE void InitFillColor()
bool isClosed() const
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.
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:271
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:460
void AddAction(const rtl::Reference< MetaAction > &pAction)
Definition: gdimtf.cxx:540
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:604
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:624
bool IsDeviceOutputNecessary() const
Definition: outdev.hxx:589
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:442