LibreOffice Module cppcanvas (master) 1
polypolyaction.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
21#include <com/sun/star/rendering/XCanvas.hpp>
22
23#include <sal/types.h>
24
31#include <sal/log.hxx>
32
34#include "polypolyaction.hxx"
35#include <outdevstate.hxx>
36#include <utility>
37#include "mtftools.hxx"
38
39
40using namespace ::com::sun::star;
41
42namespace cppcanvas::internal
43{
44 namespace
45 {
46 class PolyPolyAction : public CachedPrimitiveBase
47 {
48 public:
49 PolyPolyAction( const ::basegfx::B2DPolyPolygon&,
50 const CanvasSharedPtr&,
51 const OutDevState&,
52 bool bFill,
53 bool bStroke );
54 PolyPolyAction( const ::basegfx::B2DPolyPolygon&,
55 const CanvasSharedPtr&,
56 const OutDevState&,
57 bool bFill,
58 bool bStroke,
59 int nTransparency );
60
61 virtual bool renderSubset( const ::basegfx::B2DHomMatrix& rTransformation,
62 const Subset& rSubset ) const override;
63
64 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const override;
65 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation,
66 const Subset& rSubset ) const override;
67
68 virtual sal_Int32 getActionCount() const override;
69
70 private:
71 using Action::render;
72 virtual bool renderPrimitive( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
73 const ::basegfx::B2DHomMatrix& rTransformation ) const override;
74
75 const uno::Reference< rendering::XPolyPolygon2D > mxPolyPoly;
76 const ::basegfx::B2DRange maBounds;
78
79 // stroke color is now implicit: the maState.DeviceColor member
80 rendering::RenderState maState;
81
82 uno::Sequence< double > maFillColor;
83 };
84
85 PolyPolyAction::PolyPolyAction( const ::basegfx::B2DPolyPolygon& rPolyPoly,
86 const CanvasSharedPtr& rCanvas,
87 const OutDevState& rState,
88 bool bFill,
89 bool bStroke ) :
90 CachedPrimitiveBase( rCanvas, false ),
91 mxPolyPoly( ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon( rCanvas->getUNOCanvas()->getDevice(), rPolyPoly) ),
92 maBounds( ::basegfx::utils::getRange(rPolyPoly) ),
93 mpCanvas( rCanvas )
94 {
96
97 if( bFill )
98 maFillColor = rState.fillColor;
99
100 if( bStroke )
101 maState.DeviceColor = rState.lineColor;
102 }
103
104 PolyPolyAction::PolyPolyAction( const ::basegfx::B2DPolyPolygon& rPolyPoly,
105 const CanvasSharedPtr& rCanvas,
106 const OutDevState& rState,
107 bool bFill,
108 bool bStroke,
109 int nTransparency ) :
110 CachedPrimitiveBase( rCanvas, false ),
111 mxPolyPoly( ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon( rCanvas->getUNOCanvas()->getDevice(), rPolyPoly) ),
112 maBounds( ::basegfx::utils::getRange(rPolyPoly) ),
113 mpCanvas( rCanvas )
114 {
116
117 if( bFill )
118 {
119 maFillColor = rState.fillColor;
120
121 if( maFillColor.getLength() < 4 )
122 maFillColor.realloc( 4 );
123
124 // TODO(F1): Color management
125 // adapt fill color transparency
126 maFillColor.getArray()[3] = 1.0 - nTransparency / 100.0;
127 }
128
129 if( bStroke )
130 {
131 maState.DeviceColor = rState.lineColor;
132
133 if( maState.DeviceColor.getLength() < 4 )
134 maState.DeviceColor.realloc( 4 );
135
136 // TODO(F1): Color management
137 // adapt fill color transparency
138 maState.DeviceColor.getArray()[3] = 1.0 - nTransparency / 100.0;
139 }
140 }
141
142 bool PolyPolyAction::renderPrimitive( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
143 const ::basegfx::B2DHomMatrix& rTransformation ) const
144 {
145 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::PolyPolyAction::renderPrimitive()" );
146 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::PolyPolyAction: 0x" << std::hex << this );
147
148 rendering::RenderState aLocalState( maState );
149 ::canvas::tools::prependToRenderState(aLocalState, rTransformation);
150
151 if( maFillColor.hasElements() )
152 {
153 // TODO(E3): Use DBO's finalizer here,
154 // fillPolyPolygon() might throw
155 const uno::Sequence< double > aTmpColor( aLocalState.DeviceColor );
156 aLocalState.DeviceColor = maFillColor;
157
158 rCachedPrimitive = mpCanvas->getUNOCanvas()->fillPolyPolygon( mxPolyPoly,
159 mpCanvas->getViewState(),
160 aLocalState );
161
162 aLocalState.DeviceColor = aTmpColor;
163 }
164
165 if( aLocalState.DeviceColor.hasElements() )
166 {
167 rCachedPrimitive = mpCanvas->getUNOCanvas()->drawPolyPolygon( mxPolyPoly,
168 mpCanvas->getViewState(),
169 aLocalState );
170 }
171
172 return true;
173 }
174
175 bool PolyPolyAction::renderSubset( const ::basegfx::B2DHomMatrix& rTransformation,
176 const Subset& rSubset ) const
177 {
178 // TODO(F1): Split up poly-polygon into polygons, or even
179 // line segments, when subsets are requested.
180
181 // polygon only contains a single action, fail if subset
182 // requests different range
183 if( rSubset.mnSubsetBegin != 0 ||
184 rSubset.mnSubsetEnd != 1 )
185 return false;
186
187 return CachedPrimitiveBase::render( rTransformation );
188 }
189
190 ::basegfx::B2DRange PolyPolyAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const
191 {
192 rendering::RenderState aLocalState( maState );
193 ::canvas::tools::prependToRenderState(aLocalState, rTransformation);
194
196 maBounds,
197 mpCanvas->getViewState(),
198 aLocalState );
199 }
200
201 ::basegfx::B2DRange PolyPolyAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation,
202 const Subset& rSubset ) const
203 {
204 // TODO(F1): Split up poly-polygon into polygons, or even
205 // line segments, when subsets are requested.
206
207 // polygon only contains a single action, empty bounds
208 // if subset requests different range
209 if( rSubset.mnSubsetBegin != 0 ||
210 rSubset.mnSubsetEnd != 1 )
211 return ::basegfx::B2DRange();
212
213 return getBounds( rTransformation );
214 }
215
216 sal_Int32 PolyPolyAction::getActionCount() const
217 {
218 // TODO(F1): Split up poly-polygon into polygons, or even
219 // line segments, when subsets are requested.
220 return 1;
221 }
222
223
224 class TexturedPolyPolyAction : public CachedPrimitiveBase
225 {
226 public:
227 TexturedPolyPolyAction( const ::basegfx::B2DPolyPolygon& rPoly,
228 const CanvasSharedPtr& rCanvas,
229 const OutDevState& rState,
230 const rendering::Texture& rTexture );
231
232 virtual bool renderSubset( const ::basegfx::B2DHomMatrix& rTransformation,
233 const Subset& rSubset ) const override;
234
235 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const override;
236 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation,
237 const Subset& rSubset ) const override;
238
239 virtual sal_Int32 getActionCount() const override;
240
241 private:
242 using Action::render;
243 virtual bool renderPrimitive( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
244 const ::basegfx::B2DHomMatrix& rTransformation ) const override;
245
246 const uno::Reference< rendering::XPolyPolygon2D > mxPolyPoly;
247 const ::basegfx::B2DRectangle maBounds;
249
250 // stroke color is now implicit: the maState.DeviceColor member
251 rendering::RenderState maState;
252 const rendering::Texture maTexture;
253 };
254
255 TexturedPolyPolyAction::TexturedPolyPolyAction( const ::basegfx::B2DPolyPolygon& rPolyPoly,
256 const CanvasSharedPtr& rCanvas,
257 const OutDevState& rState,
258 const rendering::Texture& rTexture ) :
259 CachedPrimitiveBase( rCanvas, true ),
260 mxPolyPoly( ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon( rCanvas->getUNOCanvas()->getDevice(), rPolyPoly) ),
261 maBounds( ::basegfx::utils::getRange(rPolyPoly) ),
262 mpCanvas( rCanvas ),
263 maTexture( rTexture )
264 {
266 }
267
268 bool TexturedPolyPolyAction::renderPrimitive( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
269 const ::basegfx::B2DHomMatrix& rTransformation ) const
270 {
271 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::PolyPolyAction::renderPrimitive()" );
272 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::PolyPolyAction: 0x" << std::hex << this );
273
274 rendering::RenderState aLocalState( maState );
275 ::canvas::tools::prependToRenderState(aLocalState, rTransformation);
276
277 uno::Sequence< rendering::Texture > aSeq { maTexture };
278
279 rCachedPrimitive = mpCanvas->getUNOCanvas()->fillTexturedPolyPolygon( mxPolyPoly,
280 mpCanvas->getViewState(),
281 aLocalState,
282 aSeq );
283 return true;
284 }
285
286 bool TexturedPolyPolyAction::renderSubset( const ::basegfx::B2DHomMatrix& rTransformation,
287 const Subset& rSubset ) const
288 {
289 // TODO(F1): Split up poly-polygon into polygons, or even
290 // line segments, when subsets are requested.
291
292 // polygon only contains a single action, fail if subset
293 // requests different range
294 if( rSubset.mnSubsetBegin != 0 ||
295 rSubset.mnSubsetEnd != 1 )
296 return false;
297
298 return CachedPrimitiveBase::render( rTransformation );
299 }
300
301 ::basegfx::B2DRange TexturedPolyPolyAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const
302 {
303 rendering::RenderState aLocalState( maState );
304 ::canvas::tools::prependToRenderState(aLocalState, rTransformation);
305
307 maBounds,
308 mpCanvas->getViewState(),
309 aLocalState );
310 }
311
312 ::basegfx::B2DRange TexturedPolyPolyAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation,
313 const Subset& rSubset ) const
314 {
315 // TODO(F1): Split up poly-polygon into polygons, or even
316 // line segments, when subsets are requested.
317
318 // polygon only contains a single action, empty bounds
319 // if subset requests different range
320 if( rSubset.mnSubsetBegin != 0 ||
321 rSubset.mnSubsetEnd != 1 )
322 return ::basegfx::B2DRange();
323
324 return getBounds( rTransformation );
325 }
326
327 sal_Int32 TexturedPolyPolyAction::getActionCount() const
328 {
329 // TODO(F1): Split up poly-polygon into polygons, or even
330 // line segments, when subsets are requested.
331 return 1;
332 }
333
334
335 class StrokedPolyPolyAction : public CachedPrimitiveBase
336 {
337 public:
338 StrokedPolyPolyAction( const ::basegfx::B2DPolyPolygon& rPoly,
339 const CanvasSharedPtr& rCanvas,
340 const OutDevState& rState,
341 rendering::StrokeAttributes aStrokeAttributes );
342
343 virtual bool renderSubset( const ::basegfx::B2DHomMatrix& rTransformation,
344 const Subset& rSubset ) const override;
345
346 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const override;
347 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation,
348 const Subset& rSubset ) const override;
349
350 virtual sal_Int32 getActionCount() const override;
351
352 private:
353 using Action::render;
354 virtual bool renderPrimitive( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
355 const ::basegfx::B2DHomMatrix& rTransformation ) const override;
356
357 const uno::Reference< rendering::XPolyPolygon2D > mxPolyPoly;
358 const ::basegfx::B2DRectangle maBounds;
360 rendering::RenderState maState;
361 const rendering::StrokeAttributes maStrokeAttributes;
362 };
363
364 StrokedPolyPolyAction::StrokedPolyPolyAction( const ::basegfx::B2DPolyPolygon& rPolyPoly,
365 const CanvasSharedPtr& rCanvas,
366 const OutDevState& rState,
367 rendering::StrokeAttributes aStrokeAttributes ) :
368 CachedPrimitiveBase( rCanvas, false ),
369 mxPolyPoly( ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon( rCanvas->getUNOCanvas()->getDevice(), rPolyPoly) ),
370 maBounds( ::basegfx::utils::getRange(rPolyPoly) ),
371 mpCanvas( rCanvas ),
372 maStrokeAttributes(std::move( aStrokeAttributes ))
373 {
375 maState.DeviceColor = rState.lineColor;
376 }
377
378 bool StrokedPolyPolyAction::renderPrimitive( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
379 const ::basegfx::B2DHomMatrix& rTransformation ) const
380 {
381 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::PolyPolyAction::renderPrimitive()" );
382 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::PolyPolyAction: 0x" << std::hex << this );
383
384 rendering::RenderState aLocalState( maState );
385 ::canvas::tools::prependToRenderState(aLocalState, rTransformation);
386
387 rCachedPrimitive = mpCanvas->getUNOCanvas()->strokePolyPolygon( mxPolyPoly,
388 mpCanvas->getViewState(),
389 aLocalState,
391 return true;
392 }
393
394 bool StrokedPolyPolyAction::renderSubset( const ::basegfx::B2DHomMatrix& rTransformation,
395 const Subset& rSubset ) const
396 {
397 // TODO(F1): Split up poly-polygon into polygons, or even
398 // line segments, when subsets are requested.
399
400 // polygon only contains a single action, fail if subset
401 // requests different range
402 if( rSubset.mnSubsetBegin != 0 ||
403 rSubset.mnSubsetEnd != 1 )
404 return false;
405
406 return CachedPrimitiveBase::render( rTransformation );
407 }
408
409 ::basegfx::B2DRange StrokedPolyPolyAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const
410 {
411 rendering::RenderState aLocalState( maState );
412 ::canvas::tools::prependToRenderState(aLocalState, rTransformation);
413
415 maBounds,
416 mpCanvas->getViewState(),
417 aLocalState );
418 }
419
420 ::basegfx::B2DRange StrokedPolyPolyAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation,
421 const Subset& rSubset ) const
422 {
423 // TODO(F1): Split up poly-polygon into polygons, or even
424 // line segments, when subsets are requested.
425
426 // polygon only contains a single action, empty bounds
427 // if subset requests different range
428 if( rSubset.mnSubsetBegin != 0 ||
429 rSubset.mnSubsetEnd != 1 )
430 return ::basegfx::B2DRange();
431
432 return getBounds( rTransformation );
433 }
434
435 sal_Int32 StrokedPolyPolyAction::getActionCount() const
436 {
437 // TODO(F1): Split up poly-polygon into polygons, or even
438 // line segments, when subsets are requested.
439 return 1;
440 }
441 }
442
443 std::shared_ptr<Action> PolyPolyActionFactory::createPolyPolyAction( const ::basegfx::B2DPolyPolygon& rPoly,
444 const CanvasSharedPtr& rCanvas,
445 const OutDevState& rState )
446 {
447 OSL_ENSURE( rState.isLineColorSet || rState.isFillColorSet,
448 "PolyPolyActionFactory::createPolyPolyAction() with empty line and fill color" );
449 return std::make_shared<PolyPolyAction>( rPoly, rCanvas, rState,
450 rState.isFillColorSet,
451 rState.isLineColorSet );
452 }
453
454 std::shared_ptr<Action> PolyPolyActionFactory::createPolyPolyAction( const ::basegfx::B2DPolyPolygon& rPoly,
455 const CanvasSharedPtr& rCanvas,
456 const OutDevState& rState,
457 const rendering::Texture& rTexture )
458 {
459 return std::make_shared<TexturedPolyPolyAction>( rPoly, rCanvas, rState, rTexture );
460 }
461
462 std::shared_ptr<Action> PolyPolyActionFactory::createLinePolyPolyAction( const ::basegfx::B2DPolyPolygon& rPoly,
463 const CanvasSharedPtr& rCanvas,
464 const OutDevState& rState )
465 {
466 OSL_ENSURE( rState.isLineColorSet,
467 "PolyPolyActionFactory::createLinePolyPolyAction() called with empty line color" );
468
469 return std::make_shared<PolyPolyAction>( rPoly, rCanvas, rState,
470 false,
471 rState.isLineColorSet );
472 }
473
474 std::shared_ptr<Action> PolyPolyActionFactory::createPolyPolyAction( const ::basegfx::B2DPolyPolygon& rPoly,
475 const CanvasSharedPtr& rCanvas,
476 const OutDevState& rState,
477 const rendering::StrokeAttributes& rStrokeAttributes )
478 {
479 OSL_ENSURE( rState.isLineColorSet,
480 "PolyPolyActionFactory::createPolyPolyAction() for strokes called with empty line color" );
481 return std::make_shared<StrokedPolyPolyAction>( rPoly, rCanvas, rState, rStrokeAttributes );
482 }
483
484 std::shared_ptr<Action> PolyPolyActionFactory::createPolyPolyAction( const ::basegfx::B2DPolyPolygon& rPoly,
485 const CanvasSharedPtr& rCanvas,
486 const OutDevState& rState,
487 int nTransparency )
488 {
489 OSL_ENSURE( rState.isLineColorSet || rState.isFillColorSet,
490 "PolyPolyActionFactory::createPolyPolyAction() with empty line and fill color" );
491 return std::make_shared<PolyPolyAction>( rPoly, rCanvas, rState,
492 rState.isFillColorSet,
493 rState.isLineColorSet,
494 nTransparency );
495 }
496
497}
498
499/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool render(const ::basegfx::B2DHomMatrix &rTransformation) const =0
Render this action to the associated canvas.
Sequence< sal_Int8 > aSeq
#define SAL_INFO(area, stream)
uno::Reference< rendering::XPolyPolygon2D > xPolyPolygonFromB2DPolyPolygon(const uno::Reference< rendering::XGraphicDevice > &xGraphicDevice, const ::basegfx::B2DPolyPolygon &rPolyPoly)
B2DRange getRange(const B2DPolygon &rCandidate)
std::shared_ptr< Action > createLinePolyPolyAction(const ::basegfx::B2DPolyPolygon &, const CanvasSharedPtr &, const OutDevState &)
Create line polygon (always stroked, not filled)
std::shared_ptr< Action > createPolyPolyAction(const ::basegfx::B2DPolyPolygon &, const CanvasSharedPtr &, const OutDevState &)
Create polygon, fill/stroke according to state.
void initRenderState(rendering::RenderState &renderState, const ::cppcanvas::internal::OutDevState &outdevState)
Definition: mtftools.cxx:42
::basegfx::B2DRange calcDevicePixelBounds(const ::basegfx::B2DRange &rBounds, const rendering::ViewState &viewState, const rendering::RenderState &renderState)
Definition: mtftools.cxx:628
std::shared_ptr< Canvas > CanvasSharedPtr
const ::basegfx::B2DRange maBounds
const CanvasSharedPtr mpCanvas
rendering::RenderState maState
const uno::Reference< rendering::XPolyPolygon2D > mxPolyPoly
const rendering::Texture maTexture
uno::Sequence< double > maFillColor
const rendering::StrokeAttributes maStrokeAttributes