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  assert(mpGraphics);
52 
53  if ( mbInitClipRegion )
55 
56  if ( mbOutputClipped )
57  return;
58 
59  if ( mbInitLineColor )
60  InitLineColor();
61 
62  if ( mbInitFillColor )
63  InitFillColor();
64 
65  // 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 {
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  assert(mpGraphics);
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
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 Point* pPtAry = 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 = 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 )
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  assert(mpGraphics);
281 
282  if( mbInitClipRegion )
283  InitClipRegion();
284 
285  if( mbOutputClipped )
286  return;
287 
288  if( mbInitLineColor )
289  InitLineColor();
290 
291  if( mbInitFillColor )
292  InitFillColor();
293 
294  bool bSuccess(false);
295 
298  (IsLineColor() || IsFillColor()))
299  {
301  basegfx::B2DPolyPolygon aB2DPolyPolygon(rB2DPolyPoly);
302  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 bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
322 
323  for(auto const& rPolygon : aB2DPolyPolygon)
324  {
325  bSuccess = mpGraphics->DrawPolyLine(
326  aTransform,
327  rPolygon,
328  0.0,
329  0.0, // tdf#124848 hairline
330  nullptr, // MM01
332  css::drawing::LineCap_BUTT,
333  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
334  bPixelSnapHairline,
335  *this);
336  if (!bSuccess)
337  break;
338  }
339  }
340  }
341 
342  if (!bSuccess)
343  {
344  // fallback to old polygon drawing if needed
345  const tools::PolyPolygon aToolsPolyPolygon(rB2DPolyPoly);
346  const tools::PolyPolygon aPixelPolyPolygon = ImplLogicToDevicePixel(aToolsPolyPolygon);
347  ImplDrawPolyPolygon(aPixelPolyPolygon.Count(), aPixelPolyPolygon);
348  }
349 
350  if (mpAlphaVDev)
351  mpAlphaVDev->ImplDrawPolyPolygonWithB2DPolyPolygon(rB2DPolyPoly);
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];
362  const Point* aStackAry2[OUTDEV_POLYPOLY_STACKBUF];
363  PolyFlags* aStackAry3[OUTDEV_POLYPOLY_STACKBUF];
364  sal_uInt32* pPointAry;
365  const Point** 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 const Point*[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] = 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(), 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 Point* pPtAry = 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 Point* pPtAry = 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<const Point*[]> pPointAryAry(new const Point*[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] = 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:651
void setClosed(bool bNew)
virtual bool supportsOperation(OutDevSupportType) const =0
constexpr OUStringLiteral last
void DrawPolygon(const tools::Polygon &rPoly)
Render the given polygon.
Definition: polygon.cxx:156
bool mbOutputClipped
Definition: outdev.hxx:241
static Polygon SubdivideBezier(const Polygon &rPoly)
void DrawPolyPolygon(sal_uInt32 nPoly, const sal_uInt32 *pPoints, const Point **pPtAry, const OutputDevice &rOutDev)
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:233
SalGraphics * mpGraphics
Graphics context to draw on.
Definition: outdev.hxx:175
SAL_DLLPRIVATE basegfx::B2DHomMatrix ImplGetDeviceTransformation() const
Get device transformation.
Definition: map.cxx:826
const PolyFlags * GetConstFlagAry() const
SAL_DLLPRIVATE void InitFillColor()
void DrawPolygon(sal_uInt32 nPoints, const Point *pPtAry, const OutputDevice &rOutDev)
bool isClosed() const
int i
bool IsLineColor() const
Definition: outdev.hxx:505
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:244
bool HasFlags() const
void DrawPolyLine(sal_uInt32 nPoints, Point const *pPtAry, const OutputDevice &rOutDev)
bool mbLineColor
Definition: outdev.hxx:242
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:243
VclPtr< VirtualDevice > mpAlphaVDev
Definition: outdev.hxx:192
bool DrawPolygonBezier(sal_uInt32 nPoints, const Point *pPtAry, const PolyFlags *pFlgAry, const OutputDevice &rOutDev)
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:563
bool mbInitClipRegion
Definition: outdev.hxx:248
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:490
bool mbInitFillColor
Definition: outdev.hxx:245
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:376
bool DrawPolyPolygonBezier(sal_uInt32 nPoly, const sal_uInt32 *pPoints, const Point *const *pPtAry, const PolyFlags *const *pFlgAry, const OutputDevice &rOutDev)
bool IsFillColor() const
Definition: outdev.hxx:510
bool IsDeviceOutputNecessary() const
Definition: outdev.hxx:475
GDIMetaFile * mpMetaFile
Definition: outdev.hxx:178
PolyFlags
sal_uInt32 count() const
SAL_DLLPRIVATE void ImplDrawPolygon(const tools::Polygon &rPoly, const tools::PolyPolygon *pClipPolyPoly=nullptr)
Definition: polygon.cxx:445