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
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 bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
92 
93  for(auto const& rPolygon : aB2DPolyPolygon)
94  {
95  bSuccess = mpGraphics->DrawPolyLine(
96  aTransform,
97  rPolygon,
98  0.0,
99  0.0, // tdf#124848 hairline
100  nullptr, // MM01
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 {
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 {
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 bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
212 
213  bSuccess = mpGraphics->DrawPolyLine(
214  aTransform,
215  aB2DPolygon,
216  0.0,
217  0.0, // tdf#124848 hairline
218  nullptr, // MM01
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 {
263 
264  if( mpMetaFile )
265  mpMetaFile->AddAction( new MetaPolyPolygonAction( tools::PolyPolygon( rB2DPolyPoly ) ) );
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 bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
320 
321  for(auto const& rPolygon : aB2DPolyPolygon)
322  {
323  bSuccess = mpGraphics->DrawPolyLine(
324  aTransform,
325  rPolygon,
326  0.0,
327  0.0, // tdf#124848 hairline
328  nullptr, // MM01
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  if (mpAlphaVDev)
342  mpAlphaVDev->ImplDrawPolyPolygonWithB2DPolyPolygon(rB2DPolyPoly);
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:641
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:379
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:371
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:314
SAL_DLLPRIVATE basegfx::B2DHomMatrix ImplGetDeviceTransformation() const
Get device transformation.
Definition: map.cxx:933
const PolyFlags * GetConstFlagAry() const
SAL_DLLPRIVATE void InitFillColor()
bool isClosed() const
int i
const char last[]
bool IsLineColor() const
Definition: outdev.hxx:631
const Point * GetConstPointAry() const
virtual bool AcquireGraphics() const =0
Acquire a graphics device that the output device uses to draw on.
bool mbInitLineColor
Definition: outdev.hxx:382
bool HasFlags() const
bool mbLineColor
Definition: outdev.hxx:380
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:381
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:463
void AddAction(const rtl::Reference< MetaAction > &pAction)
Definition: gdimtf.cxx:566
bool mbInitClipRegion
Definition: outdev.hxx:386
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:616
bool mbInitFillColor
Definition: outdev.hxx:383
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:636
bool IsDeviceOutputNecessary() const
Definition: outdev.hxx:601
bool DrawPolygonBezier(sal_uInt32 nPoints, const SalPoint *pPtAry, const PolyFlags *pFlgAry, const OutputDevice *pOutDev)
GDIMetaFile * mpMetaFile
Definition: outdev.hxx:317
PolyFlags
sal_uInt32 count() const
SAL_DLLPRIVATE void ImplDrawPolygon(const tools::Polygon &rPoly, const tools::PolyPolygon *pClipPolyPoly=nullptr)
Definition: polygon.cxx:445