Debian Codegraph @

cppcanvas/source/mtfrenderer/transparencygroupaction.cxx

raw
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/config.h>
21
22 #include <utility>
23
24 #include <tools/gen.hxx>
25 #include <tools/debug.hxx>
26
27 #include <canvas/canvastools.hxx>
28
29 #include <com/sun/star/rendering/XBitmap.hpp>
30 #include <com/sun/star/rendering/XCanvas.hpp>
31
32 #include <vcl/metaact.hxx>
33 #include <vcl/svapp.hxx>
34 #include <vcl/virdev.hxx>
35 #include <vcl/gdimtf.hxx>
36
37 #include <basegfx/range/b2drange.hxx>
38 #include <basegfx/point/b2dpoint.hxx>
39 #include <basegfx/vector/b2dsize.hxx>
40 #include <basegfx/numeric/ftools.hxx>
41 #include <basegfx/matrix/b2dhommatrix.hxx>
42 #include <basegfx/tuple/b2dtuple.hxx>
43 #include <basegfx/utils/canvastools.hxx>
44 #include <basegfx/matrix/b2dhommatrixtools.hxx>
45 #include <sal/log.hxx>
46
47 #include "transparencygroupaction.hxx"
48 #include <outdevstate.hxx>
49 #include "mtftools.hxx"
50 #include <cppcanvas/vclfactory.hxx>
51
52 #if OSL_DEBUG_LEVEL > 2
53 #include <vcl/canvastools.hxx>
54 #endif
55
56 using namespace ::com::sun::star;
57
58 namespace cppcanvas::internal
59 {
60 // free support functions
61 // ======================
62 namespace
63 {
64 class TransparencyGroupAction : public Action
65 {
66 public:
67 /** Create new transparency group action.
68
69 @param rGroupMtf
70 Metafile that groups all actions to be rendered
71 transparent.
72
73 @param rAlphaGradient
74 VCL gradient, to be rendered into the action's alpha
75 channel.
76
77 @param rDstPoint
78 Left, top edge of destination, in current state
79 coordinate system
80
81 @param rDstSize
82 Size of the transparency group object, in current
83 state coordinate system.
84 */
85 TransparencyGroupAction( std::unique_ptr< GDIMetaFile >&& rGroupMtf,
86 std::optional< Gradient >&& rAlphaGradient,
87 const ::basegfx::B2DPoint& rDstPoint,
88 const ::basegfx::B2DVector& rDstSize,
89 const CanvasSharedPtr& rCanvas,
90 const OutDevState& rState );
91
92 TransparencyGroupAction(const TransparencyGroupAction&) = delete;
93 const TransparencyGroupAction& operator=(const TransparencyGroupAction&) = delete;
94
95 virtual bool render( const ::basegfx::B2DHomMatrix& rTransformation ) const override;
96 virtual bool renderSubset( const ::basegfx::B2DHomMatrix& rTransformation,
97 const Subset& rSubset ) const override;
98
99 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const override;
100 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation,
101 const Subset& rSubset ) const override;
102
103 virtual sal_Int32 getActionCount() const override;
104
105 private:
106 std::unique_ptr< GDIMetaFile > mpGroupMtf;
107 std::optional< Gradient > mpAlphaGradient;
108
109 const ::basegfx::B2DSize maDstSize;
110
111 mutable uno::Reference< rendering::XBitmap > mxBufferBitmap; // contains last rendered version
112 mutable ::basegfx::B2DHomMatrix maLastTransformation; // contains last active transformation
113 mutable Subset maLastSubset; // contains last effective subset
114
115 // transformation for
116 // mxBufferBitmap content
117 CanvasSharedPtr mpCanvas;
118 rendering::RenderState maState;
119 };
120
121
122 /** Setup transformation such that the next render call is
123 moved rPoint away, and scaled according to the ratio
124 given by src and dst size.
125 */
126 void implSetupTransform( rendering::RenderState& rRenderState,
127 const ::basegfx::B2DPoint& rDstPoint )
128 {
129 ::basegfx::B2DHomMatrix aLocalTransformation;
130
131 aLocalTransformation.translate( rDstPoint.getX(),
132 rDstPoint.getY() );
133 ::canvastools::appendToRenderState( rRenderState,
134 aLocalTransformation );
135 }
136
137 TransparencyGroupAction::TransparencyGroupAction( std::unique_ptr< GDIMetaFile >&& rGroupMtf,
138 std::optional< Gradient >&& rAlphaGradient,
139 const ::basegfx::B2DPoint& rDstPoint,
140 const ::basegfx::B2DVector& rDstSize,
141 const CanvasSharedPtr& rCanvas,
142 const OutDevState& rState ) :
143 mpGroupMtf( std::move(rGroupMtf) ),
144 mpAlphaGradient( std::move(rAlphaGradient) ),
145 maDstSize(rDstSize.getX(), rDstSize.getY()),
146 mpCanvas( rCanvas )
147 {
148 cppcanvastools::initRenderState(maState,rState);
149 implSetupTransform( maState, rDstPoint );
150
151 // correct clip (which is relative to original transform)
152 cppcanvastools::modifyClip( maState,
153 rState,
154 rCanvas,
155 rDstPoint,
156 nullptr,
157 nullptr );
158
159 maLastSubset.mnSubsetBegin = 0;
160 maLastSubset.mnSubsetEnd = -1;
161 }
162
163 // TODO(P3): The whole float transparency handling is a mess,
164 // this should be refactored. What's more, the old idea of
165 // having only internal 'metaactions', and not the original
166 // GDIMetaFile now looks a lot less attractive. Try to move
167 // into the direction of having a direct GDIMetaFile2XCanvas
168 // renderer, and maybe a separate metafile XCanvas
169 // implementation.
170 bool TransparencyGroupAction::renderSubset( const ::basegfx::B2DHomMatrix& rTransformation,
171 const Subset& rSubset ) const
172 {
173 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::TransparencyGroupAction::renderSubset()" );
174 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::TransparencyGroupAction: 0x" << std::hex << this );
175
176 // determine overall transformation matrix (render, view,
177 // and passed transformation)
178 ::basegfx::B2DHomMatrix aTransform = ::canvastools::getRenderStateTransform( maState );
179 aTransform = rTransformation * aTransform;
180
181 ::basegfx::B2DHomMatrix aTotalTransform = ::canvastools::getViewStateTransform( mpCanvas->getViewState() );
182 aTotalTransform = aTotalTransform * aTransform;
183
184 // since pure translational changes to the transformation
185 // does not matter, remove them before comparing
186 aTotalTransform.set( 0, 2, 0.0 );
187 aTotalTransform.set( 1, 2, 0.0 );
188
189 // determine total scaling factor of the
190 // transformation matrix - need to make the bitmap
191 // large enough
192 ::basegfx::B2DTuple aScale;
193 ::basegfx::B2DTuple aTranslate;
194 double nRotate;
195 double nShearX;
196 if( !aTotalTransform.decompose( aScale,
197 aTranslate,
198 nRotate,
199 nShearX ) )
200 {
201 SAL_WARN( "cppcanvas.emf", "TransparencyGroupAction::renderSubset(): non-decomposable transformation" );
202 return false;
203 }
204
205 ::Point aMtfOffsetPoint;
206
207 // if there's no buffer bitmap, or as soon as the
208 // total transformation changes, we've got to
209 // re-render the bitmap
210 if( !mxBufferBitmap.is() ||
211 aTotalTransform != maLastTransformation ||
212 rSubset.mnSubsetBegin != maLastSubset.mnSubsetBegin ||
213 rSubset.mnSubsetEnd != maLastSubset.mnSubsetEnd )
214 {
215 DBG_TESTSOLARMUTEX();
216
217 // tdf#150610 fix broken rendering of text meta actions
218 // Even when drawing to a VirtualDevice where antialiasing
219 // is disabled, text will still be drawn with some
220 // antialiased pixels on HiDPI displays. So, expand the
221 // size of the VirtualDevice slightly to capture any of
222 // the pixels drawn past the edges of the destination
223 // bounds.
224 bool bHasTextActions = false;
225 MetaAction* pCurrAct;
226 int nCurrActionIndex;
227 for( nCurrActionIndex=0, pCurrAct=mpGroupMtf->FirstAction();
228 pCurrAct && !bHasTextActions;
229 ++nCurrActionIndex, pCurrAct = mpGroupMtf->NextAction() )
230 {
231 switch( pCurrAct->GetType() )
232 {
233 case MetaActionType::TEXT:
234 case MetaActionType::TEXTARRAY:
235 case MetaActionType::STRETCHTEXT:
236 case MetaActionType::TEXTRECT:
237 if( ( rSubset.mnSubsetBegin == 0 && rSubset.mnSubsetEnd == -1 ) || ( rSubset.mnSubsetBegin <= nCurrActionIndex && rSubset.mnSubsetEnd > nCurrActionIndex ) )
238 bHasTextActions = true;
239 break;
240 default:
241 break;
242 }
243 }
244
245 // output size of metafile
246 ::Size aOutputSizePixel( ::basegfx::fround<::tools::Long>( aScale.getX() * maDstSize.getWidth() ),
247 ::basegfx::fround<::tools::Long>( aScale.getY() * maDstSize.getHeight() ) );
248
249 sal_Int32 nBitmapExtra;
250 if ( bHasTextActions )
251 {
252 nBitmapExtra = 10;
253 aMtfOffsetPoint = ::Point( nBitmapExtra / 2, nBitmapExtra / 2 );
254 }
255 else
256 {
257 // Related tdf#150610 assume antialiasing is enabled
258 // Although antialiasing is normally disabled in the
259 // VirtualDevice, lines in tdf#150610 will draw past
260 // the edge of the VirtualDevice when running a
261 // slideshow so always add an extra pixel on the
262 // right and bottom edges.
263 nBitmapExtra = 1;
264 }
265
266 // pixel size of cache bitmap: round up to nearest int
267 ::Point aBitmapPoint;
268 ::Size aBitmapSizePixel( static_cast<sal_Int32>( aScale.getX() * maDstSize.getWidth() ) + nBitmapExtra,
269 static_cast<sal_Int32>( aScale.getY() * maDstSize.getHeight() ) + nBitmapExtra );
270
271 // render our content into an appropriately sized
272 // VirtualDevice with alpha channel
273 ScopedVclPtrInstance<VirtualDevice> aVDev(
274 *::Application::GetDefaultDevice(), DeviceFormat::WITH_ALPHA );
275 aVDev->SetOutputSizePixel( aBitmapSizePixel, true, true );
276 aVDev->SetMapMode();
277
278 if( rSubset.mnSubsetBegin != 0 ||
279 rSubset.mnSubsetEnd != -1 )
280 {
281 // true subset - extract referenced
282 // metaactions from mpGroupMtf
283 GDIMetaFile aMtf;
284
285 // extract subset actions
286 for( nCurrActionIndex=0,
287 pCurrAct=mpGroupMtf->FirstAction();
288 pCurrAct;
289 ++nCurrActionIndex, pCurrAct = mpGroupMtf->NextAction() )
290 {
291 switch( pCurrAct->GetType() )
292 {
293 case MetaActionType::PUSH:
294 case MetaActionType::POP:
295 case MetaActionType::CLIPREGION:
296 case MetaActionType::ISECTRECTCLIPREGION:
297 case MetaActionType::ISECTREGIONCLIPREGION:
298 case MetaActionType::MOVECLIPREGION:
299 case MetaActionType::LINECOLOR:
300 case MetaActionType::FILLCOLOR:
301 case MetaActionType::TEXTCOLOR:
302 case MetaActionType::TEXTFILLCOLOR:
303 case MetaActionType::TEXTLINECOLOR:
304 case MetaActionType::TEXTALIGN:
305 case MetaActionType::FONT:
306 case MetaActionType::RASTEROP:
307 case MetaActionType::REFPOINT:
308 case MetaActionType::LAYOUTMODE:
309 // state-changing action - copy as-is
310 aMtf.AddAction( pCurrAct->Clone() );
311 break;
312
313 case MetaActionType::GRADIENT:
314 case MetaActionType::HATCH:
315 case MetaActionType::EPS:
316 case MetaActionType::COMMENT:
317 case MetaActionType::POINT:
318 case MetaActionType::PIXEL:
319 case MetaActionType::LINE:
320 case MetaActionType::RECT:
321 case MetaActionType::ROUNDRECT:
322 case MetaActionType::ELLIPSE:
323 case MetaActionType::ARC:
324 case MetaActionType::PIE:
325 case MetaActionType::CHORD:
326 case MetaActionType::POLYLINE:
327 case MetaActionType::POLYGON:
328 case MetaActionType::POLYPOLYGON:
329 case MetaActionType::BMP:
330 case MetaActionType::BMPSCALE:
331 case MetaActionType::BMPSCALEPART:
332 case MetaActionType::BMPEX:
333 case MetaActionType::BMPEXSCALE:
334 case MetaActionType::BMPEXSCALEPART:
335 case MetaActionType::MASK:
336 case MetaActionType::MASKSCALE:
337 case MetaActionType::MASKSCALEPART:
338 case MetaActionType::GRADIENTEX:
339 case MetaActionType::WALLPAPER:
340 case MetaActionType::Transparent:
341 case MetaActionType::FLOATTRANSPARENT:
342 case MetaActionType::TEXT:
343 case MetaActionType::TEXTARRAY:
344 case MetaActionType::TEXTLINE:
345 case MetaActionType::TEXTRECT:
346 case MetaActionType::STRETCHTEXT:
347 // output-generating action - only
348 // copy, if we're within the
349 // requested subset
350 if( rSubset.mnSubsetBegin <= nCurrActionIndex &&
351 rSubset.mnSubsetEnd > nCurrActionIndex )
352 {
353 aMtf.AddAction( pCurrAct->Clone() );
354 }
355 break;
356
357 default:
358 SAL_WARN( "cppcanvas.emf", "Unknown meta action type encountered" );
359 break;
360 }
361 }
362
363 aVDev->DrawTransparent( aMtf,
364 aBitmapPoint,
365 aBitmapSizePixel,
366 aMtfOffsetPoint,
367 aOutputSizePixel,
368 *mpAlphaGradient );
369 }
370 else
371 {
372 // no subsetting - render whole mtf
373 aVDev->DrawTransparent( *mpGroupMtf,
374 aBitmapPoint,
375 aBitmapSizePixel,
376 aMtfOffsetPoint,
377 aOutputSizePixel,
378 *mpAlphaGradient );
379 }
380
381
382 // update buffered bitmap and transformation
383 BitmapSharedPtr aBmp( VCLFactory::createBitmap(
384 mpCanvas,
385 aVDev->GetBitmap(
386 aBitmapPoint,
387 aBitmapSizePixel ) ) );
388 mxBufferBitmap = aBmp->getUNOBitmap();
389 maLastTransformation = aTotalTransform;
390 maLastSubset = rSubset;
391 }
392
393 // determine target transformation (we can't simply pass
394 // aTotalTransform as assembled above, since we must take
395 // the canvas' view state as is, it might contain clipping
396 // (which, in turn, is relative to the view
397 // transformation))
398
399 // given that aTotalTransform is the identity
400 // transformation, we could simply render our bitmap
401 // as-is. Now, since the mxBufferBitmap content already
402 // accounts for scale changes in the overall
403 // transformation, we must factor this out
404 // before. Generally, the transformation matrix should be
405 // structured like this:
406 // Translation*Rotation*Shear*Scale. Thus, to neutralize
407 // the contained scaling, we've got to right-multiply with
408 // the inverse.
409 ::basegfx::B2DHomMatrix aScaleCorrection;
410 aScaleCorrection.translate( -aMtfOffsetPoint.X(), -aMtfOffsetPoint.Y() );
411 aScaleCorrection.scale( 1/aScale.getX(), 1/aScale.getY() );
412 aTransform = aTransform * aScaleCorrection;
413
414 rendering::RenderState aLocalState( maState );
415 ::canvastools::setRenderStateTransform(aLocalState, aTransform);
416
417 if(aLocalState.Clip.is())
418 {
419 // tdf#95709
420 // Adjust renderstate clip to modified scale from above
421 ::basegfx::B2DPolyPolygon aClip = ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(aLocalState.Clip);
422 aClip.transform(basegfx::utils::createScaleB2DHomMatrix(aScale));
423 aLocalState.Clip = ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon(mpCanvas->getUNOCanvas()->getDevice(), aClip);
424 }
425
426 #if OSL_DEBUG_LEVEL > 2
427 aLocalState.Clip.clear();
428 aLocalState.DeviceColor =
429 vcl::unotools::colorToDoubleSequence(
430 ::Color( 0x80FF0000 ),
431 mpCanvas->getUNOCanvas()->getDevice()->getDeviceColorSpace() );
432
433 if( maState.Clip.is() )
434 mpCanvas->getUNOCanvas()->fillPolyPolygon( maState.Clip,
435 mpCanvas->getViewState(),
436 aLocalState );
437
438 aLocalState.DeviceColor = maState.DeviceColor;
439 #endif
440
441 // no further alpha changes necessary -> draw directly
442 mpCanvas->getUNOCanvas()->drawBitmap( mxBufferBitmap,
443 mpCanvas->getViewState(),
444 aLocalState );
445 return true;
446 }
447
448 // TODO(P3): The whole float transparency handling is a mess,
449 // this should be refactored. What's more, the old idea of
450 // having only internal 'metaactions', and not the original
451 // GDIMetaFile now looks a lot less attractive. Try to move
452 // into the direction of having a direct GDIMetaFile2XCanvas
453 // renderer, and maybe a separate metafile XCanvas
454 // implementation.
455 bool TransparencyGroupAction::render( const ::basegfx::B2DHomMatrix& rTransformation ) const
456 {
457 Subset aSubset;
458
459 aSubset.mnSubsetBegin = 0;
460 aSubset.mnSubsetEnd = -1;
461
462 return renderSubset( rTransformation, aSubset );
463 }
464
465 ::basegfx::B2DRange TransparencyGroupAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const
466 {
467 rendering::RenderState aLocalState( maState );
468 ::canvastools::prependToRenderState(aLocalState, rTransformation);
469
470 return cppcanvastools::calcDevicePixelBounds(
471 ::basegfx::B2DRange( 0,0,
472 maDstSize.getWidth(),
473 maDstSize.getHeight() ),
474 mpCanvas->getViewState(),
475 aLocalState );
476 }
477
478 ::basegfx::B2DRange TransparencyGroupAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation,
479 const Subset& rSubset ) const
480 {
481 // TODO(F3): Currently, the bounds for
482 // TransparencyGroupAction subsets equal those of the
483 // full set, although this action is able to render
484 // true subsets.
485
486 // polygon only contains a single action, empty bounds
487 // if subset requests different range
488 if( rSubset.mnSubsetBegin != 0 ||
489 rSubset.mnSubsetEnd != 1 )
490 return ::basegfx::B2DRange();
491
492 return getBounds( rTransformation );
493 }
494
495 sal_Int32 TransparencyGroupAction::getActionCount() const
496 {
497 return mpGroupMtf ? mpGroupMtf->GetActionSize() : 0;
498 }
499
500 }
501
502 std::shared_ptr<Action> TransparencyGroupActionFactory::createTransparencyGroupAction( std::unique_ptr< GDIMetaFile >&& rGroupMtf,
503 std::optional< Gradient >&& rAlphaGradient,
504 const ::basegfx::B2DPoint& rDstPoint,
505 const ::basegfx::B2DVector& rDstSize,
506 const CanvasSharedPtr& rCanvas,
507 const OutDevState& rState )
508 {
509 return std::make_shared<TransparencyGroupAction>(std::move(rGroupMtf),
510 std::move(rAlphaGradient),
511 rDstPoint,
512 rDstSize,
513 rCanvas,
514 rState );
515 }
516
517 }
518
519 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */