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 <sal/types.h>
22#include <tools/poly.hxx>
23
24#include <vcl/metaact.hxx>
25#include <vcl/virdev.hxx>
26
27#include <salgdi.hxx>
28
29#include <cassert>
30#include <memory>
31
32#define OUTDEV_POLYPOLY_STACKBUF 32
33
35{
37
38 if( mpMetaFile )
39 mpMetaFile->AddAction( new MetaPolyPolygonAction( rPolyPoly ) );
40
41 sal_uInt16 nPoly = rPolyPoly.Count();
42
43 if ( !IsDeviceOutputNecessary() || (!mbLineColor && !mbFillColor) || !nPoly || ImplIsRecordLayout() )
44 return;
45
46 // we need a graphics
47 if ( !mpGraphics && !AcquireGraphics() )
48 return;
49 assert(mpGraphics);
50
51 if ( mbInitClipRegion )
53
54 if ( mbOutputClipped )
55 return;
56
57 if ( mbInitLineColor )
59
60 if ( mbInitFillColor )
62
63 // use b2dpolygon drawing if possible
66 (IsLineColor() || IsFillColor()))
67 {
69 basegfx::B2DPolyPolygon aB2DPolyPolygon(rPolyPoly.getB2DPolyPolygon());
70 bool bSuccess(true);
71
72 // ensure closed - may be asserted, will prevent buffering
73 if(!aB2DPolyPolygon.isClosed())
74 {
75 aB2DPolyPolygon.setClosed(true);
76 }
77
78 if(IsFillColor())
79 {
80 bSuccess = mpGraphics->DrawPolyPolygon(
81 aTransform,
82 aB2DPolyPolygon,
83 0.0,
84 *this);
85 }
86
87 if(bSuccess && IsLineColor())
88 {
89 const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
90
91 for(auto const& rPolygon : std::as_const(aB2DPolyPolygon))
92 {
93 bSuccess = mpGraphics->DrawPolyLine(
94 aTransform,
95 rPolygon,
96 0.0,
97 0.0, // tdf#124848 hairline
98 nullptr, // MM01
100 css::drawing::LineCap_BUTT,
101 basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
102 bPixelSnapHairline,
103 *this);
104 if (!bSuccess)
105 break;
106 }
107 }
108
109 if(bSuccess)
110 {
111 if( mpAlphaVDev )
112 mpAlphaVDev->DrawPolyPolygon( rPolyPoly );
113 return;
114 }
115 }
116
117 if ( nPoly == 1 )
118 {
119 // #100127# Map to DrawPolygon
120 const tools::Polygon& aPoly = rPolyPoly.GetObject( 0 );
121 if( aPoly.GetSize() >= 2 )
122 {
123 GDIMetaFile* pOldMF = mpMetaFile;
124 mpMetaFile = nullptr;
125
126 DrawPolygon( aPoly );
127
128 mpMetaFile = pOldMF;
129 }
130 }
131 else
132 {
133 // #100127# moved real tools::PolyPolygon draw to separate method,
134 // have to call recursively, avoiding duplicate
135 // ImplLogicToDevicePixel calls
136 ImplDrawPolyPolygon( nPoly, ImplLogicToDevicePixel( rPolyPoly ) );
137 }
138 if( mpAlphaVDev )
139 mpAlphaVDev->DrawPolyPolygon( rPolyPoly );
140}
141
143{
144 assert(!is_double_buffered_window());
145
146 // AW: Do NOT paint empty polygons
147 if(rB2DPolygon.count())
148 {
149 basegfx::B2DPolyPolygon aPP( rB2DPolygon );
150 DrawPolyPolygon( aPP );
151 }
152}
153
155{
156 assert(!is_double_buffered_window());
157
158 if( mpMetaFile )
159 mpMetaFile->AddAction( new MetaPolygonAction( rPoly ) );
160
161 sal_uInt16 nPoints = rPoly.GetSize();
162
163 if ( !IsDeviceOutputNecessary() || (!mbLineColor && !mbFillColor) || (nPoints < 2) || ImplIsRecordLayout() )
164 return;
165
166 // we need a graphics
167 if ( !mpGraphics && !AcquireGraphics() )
168 return;
169 assert(mpGraphics);
170
171 if ( mbInitClipRegion )
173
174 if ( mbOutputClipped )
175 return;
176
177 if ( mbInitLineColor )
179
180 if ( mbInitFillColor )
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{
260 assert(!is_double_buffered_window());
261
262 if( mpMetaFile )
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 assert(mpGraphics);
279
280 if( mbInitClipRegion )
282
283 if( mbOutputClipped )
284 return;
285
286 if( mbInitLineColor )
288
289 if( mbInitFillColor )
291
292 bool bSuccess(false);
293
296 (IsLineColor() || IsFillColor()))
297 {
299 basegfx::B2DPolyPolygon aB2DPolyPolygon(rB2DPolyPoly);
300 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 : std::as_const(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
340 if (!bSuccess)
341 {
342 // fallback to old polygon drawing if needed
343 const tools::PolyPolygon aToolsPolyPolygon(rB2DPolyPoly);
344 const tools::PolyPolygon aPixelPolyPolygon = ImplLogicToDevicePixel(aToolsPolyPolygon);
345 ImplDrawPolyPolygon(aPixelPolyPolygon.Count(), aPixelPolyPolygon);
346 }
347
348 if (mpAlphaVDev)
350}
351
352// #100127# Extracted from OutputDevice::DrawPolyPolygon()
353void OutputDevice::ImplDrawPolyPolygon( sal_uInt16 nPoly, const tools::PolyPolygon& rPolyPoly )
354{
355 // AW: This crashes on empty PolyPolygons, avoid that
356 if(!nPoly)
357 return;
358
359 sal_uInt32 aStackAry1[OUTDEV_POLYPOLY_STACKBUF];
360 const Point* aStackAry2[OUTDEV_POLYPOLY_STACKBUF];
362 sal_uInt32* pPointAry;
363 const Point** pPointAryAry;
364 const PolyFlags** pFlagAryAry;
365 sal_uInt16 i = 0;
366 sal_uInt16 j = 0;
367 sal_uInt16 last = 0;
368 bool bHaveBezier = false;
369 if ( nPoly > OUTDEV_POLYPOLY_STACKBUF )
370 {
371 pPointAry = new sal_uInt32[nPoly];
372 pPointAryAry = new const Point*[nPoly];
373 pFlagAryAry = new const PolyFlags*[nPoly];
374 }
375 else
376 {
377 pPointAry = aStackAry1;
378 pPointAryAry = aStackAry2;
379 pFlagAryAry = const_cast<const PolyFlags**>(aStackAry3);
380 }
381
382 do
383 {
384 const tools::Polygon& rPoly = rPolyPoly.GetObject( i );
385 sal_uInt16 nSize = rPoly.GetSize();
386 if ( nSize )
387 {
388 pPointAry[j] = nSize;
389 pPointAryAry[j] = rPoly.GetConstPointAry();
390 pFlagAryAry[j] = rPoly.GetConstFlagAry();
391 last = i;
392
393 if( pFlagAryAry[j] )
394 bHaveBezier = true;
395
396 ++j;
397 }
398 ++i;
399 }
400 while ( i < nPoly );
401
402 if ( j == 1 )
403 {
404 // #100127# Forward beziers to sal, if any
405 if( bHaveBezier )
406 {
407 if( !mpGraphics->DrawPolygonBezier( *pPointAry, *pPointAryAry, *pFlagAryAry, *this ) )
408 {
410 mpGraphics->DrawPolygon( aPoly.GetSize(), aPoly.GetConstPointAry(), *this );
411 }
412 }
413 else
414 {
415 mpGraphics->DrawPolygon( *pPointAry, *pPointAryAry, *this );
416 }
417 }
418 else
419 {
420 // #100127# Forward beziers to sal, if any
421 if( bHaveBezier )
422 {
423 if (!mpGraphics->DrawPolyPolygonBezier(j, pPointAry, pPointAryAry, pFlagAryAry, *this))
424 {
426 ImplDrawPolyPolygon( aPolyPoly.Count(), aPolyPoly );
427 }
428 }
429 else
430 {
431 mpGraphics->DrawPolyPolygon( j, pPointAry, pPointAryAry, *this );
432 }
433 }
434
435 if ( pPointAry != aStackAry1 )
436 {
437 delete[] pPointAry;
438 delete[] pPointAryAry;
439 delete[] pFlagAryAry;
440 }
441}
442
443void OutputDevice::ImplDrawPolygon( const tools::Polygon& rPoly, const tools::PolyPolygon* pClipPolyPoly )
444{
445 if( pClipPolyPoly )
446 {
447 ImplDrawPolyPolygon( tools::PolyPolygon(rPoly), pClipPolyPoly );
448 }
449 else
450 {
451 sal_uInt16 nPoints = rPoly.GetSize();
452
453 if ( nPoints < 2 )
454 return;
455
456 const Point* pPtAry = rPoly.GetConstPointAry();
457 mpGraphics->DrawPolygon( nPoints, pPtAry, *this );
458 }
459}
460
461void OutputDevice::ImplDrawPolyPolygon( const tools::PolyPolygon& rPolyPoly, const tools::PolyPolygon* pClipPolyPoly )
462{
463 tools::PolyPolygon* pPolyPoly;
464
465 if( pClipPolyPoly )
466 {
467 pPolyPoly = new tools::PolyPolygon;
468 rPolyPoly.GetIntersection( *pClipPolyPoly, *pPolyPoly );
469 }
470 else
471 {
472 pPolyPoly = const_cast<tools::PolyPolygon*>(&rPolyPoly);
473 }
474 if( pPolyPoly->Count() == 1 )
475 {
476 const tools::Polygon& rPoly = pPolyPoly->GetObject( 0 );
477 sal_uInt16 nSize = rPoly.GetSize();
478
479 if( nSize >= 2 )
480 {
481 const Point* pPtAry = rPoly.GetConstPointAry();
482 mpGraphics->DrawPolygon( nSize, pPtAry, *this );
483 }
484 }
485 else if( pPolyPoly->Count() )
486 {
487 sal_uInt16 nCount = pPolyPoly->Count();
488 std::unique_ptr<sal_uInt32[]> pPointAry(new sal_uInt32[nCount]);
489 std::unique_ptr<const Point*[]> pPointAryAry(new const Point*[nCount]);
490 sal_uInt16 i = 0;
491 do
492 {
493 const tools::Polygon& rPoly = pPolyPoly->GetObject( i );
494 sal_uInt16 nSize = rPoly.GetSize();
495 if ( nSize )
496 {
497 pPointAry[i] = nSize;
498 pPointAryAry[i] = rPoly.GetConstPointAry();
499 i++;
500 }
501 else
502 nCount--;
503 }
504 while( i < nCount );
505
506 if( nCount == 1 )
507 mpGraphics->DrawPolygon( pPointAry[0], pPointAryAry[0], *this );
508 else
509 mpGraphics->DrawPolyPolygon( nCount, pPointAry.get(), pPointAryAry.get(), *this );
510 }
511
512 if( pClipPolyPoly )
513 delete pPolyPoly;
514}
515
516/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void AddAction(const rtl::Reference< MetaAction > &pAction)
Definition: gdimtf.cxx:585
virtual void InitClipRegion()
SAL_DLLPRIVATE bool is_double_buffered_window() const
SAL_DLLPRIVATE void InitLineColor()
Definition: line.cxx:85
bool mbOutputClipped
Definition: outdev.hxx:245
RasterOp GetRasterOp() const
Definition: outdev.hxx:496
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:334
bool mbFillColor
Definition: outdev.hxx:247
SAL_DLLPRIVATE void ImplDrawPolygon(const tools::Polygon &rPoly, const tools::PolyPolygon *pClipPolyPoly=nullptr)
Definition: polygon.cxx:443
virtual bool AcquireGraphics() const =0
Acquire a graphics device that the output device uses to draw on.
SAL_DLLPRIVATE bool ImplIsRecordLayout() const
Definition: outdev.cxx:708
bool IsFillColor() const
Definition: outdev.hxx:516
GDIMetaFile * mpMetaFile
Definition: outdev.hxx:185
SAL_DLLPRIVATE void InitFillColor()
Definition: fill.cxx:76
bool mbInitLineColor
Definition: outdev.hxx:248
void DrawPolygon(const tools::Polygon &rPoly)
Render the given polygon.
Definition: polygon.cxx:154
SalGraphics * mpGraphics
Graphics context to draw on.
Definition: outdev.hxx:182
bool IsLineColor() const
Definition: outdev.hxx:511
SAL_DLLPRIVATE void ImplDrawPolyPolygonWithB2DPolyPolygon(const basegfx::B2DPolyPolygon &rB2DPolyPoly)
Definition: polygon.cxx:269
SAL_DLLPRIVATE void ImplDrawPolyPolygon(const tools::PolyPolygon &rPolyPoly, const tools::PolyPolygon *pClipPolyPoly)
Definition: polygon.cxx:461
bool mbInitClipRegion
Definition: outdev.hxx:252
AntialiasingFlags mnAntialiasing
Definition: outdev.hxx:237
bool IsDeviceOutputNecessary() const
Definition: outdev.hxx:481
VclPtr< VirtualDevice > mpAlphaVDev
Definition: outdev.hxx:196
void DrawPolyPolygon(const tools::PolyPolygon &rPolyPoly)
Render the given poly-polygon.
Definition: polygon.cxx:34
SAL_DLLPRIVATE basegfx::B2DHomMatrix ImplGetDeviceTransformation() const
Get device transformation.
Definition: map.cxx:870
bool mbLineColor
Definition: outdev.hxx:246
bool mbInitFillColor
Definition: outdev.hxx:249
virtual bool supportsOperation(OutDevSupportType) const =0
void DrawPolyLine(sal_uInt32 nPoints, Point const *pPtAry, const OutputDevice &rOutDev)
bool DrawPolyPolygonBezier(sal_uInt32 nPoly, const sal_uInt32 *pPoints, const Point *const *pPtAry, const PolyFlags *const *pFlgAry, const OutputDevice &rOutDev)
bool DrawPolygonBezier(sal_uInt32 nPoints, const Point *pPtAry, const PolyFlags *pFlgAry, const OutputDevice &rOutDev)
void DrawPolyPolygon(sal_uInt32 nPoly, const sal_uInt32 *pPoints, const Point **pPtAry, const OutputDevice &rOutDev)
void DrawPolygon(sal_uInt32 nPoints, const Point *pPtAry, const OutputDevice &rOutDev)
void setClosed(bool bNew)
sal_uInt32 count() const
bool isClosed() const
sal_uInt32 count() const
void setClosed(bool bNew)
sal_uInt16 Count() const
void GetIntersection(const tools::PolyPolygon &rPolyPoly, tools::PolyPolygon &rResult) const
static tools::PolyPolygon SubdivideBezier(const tools::PolyPolygon &rPolyPoly)
::basegfx::B2DPolyPolygon getB2DPolyPolygon() const
const tools::Polygon & GetObject(sal_uInt16 nPos) const
::basegfx::B2DPolygon getB2DPolygon() const
const Point * GetConstPointAry() const
bool HasFlags() const
sal_uInt16 GetSize() const
static Polygon SubdivideBezier(const Polygon &rPoly)
const PolyFlags * GetConstFlagAry() const
int nCount
constexpr double deg2rad(double v)
int i
constexpr OUStringLiteral last
PolyFlags
#define OUTDEV_POLYPOLY_STACKBUF
Definition: polygon.cxx:32