1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <com/sun/star/rendering/XBitmap.hpp>
22 #include <com/sun/star/rendering/XCanvas.hpp>
23 #include <com/sun/star/rendering/XCachedPrimitive.hpp>
24 #include <vcl/bitmapex.hxx>
25 #include <tools/gen.hxx>
26 #include <vcl/canvastools.hxx>
27 #include <canvas/canvastools.hxx>
28 #include <basegfx/matrix/b2dhommatrix.hxx>
29 #include <basegfx/point/b2dpoint.hxx>
30 #include <basegfx/range/b2drange.hxx>
31 #include <sal/log.hxx>
32 #include "cachedprimitivebase.hxx"
33 #include "bitmapaction.hxx"
34 #include <outdevstate.hxx>
35 #include "mtftools.hxx"
36 #include <basegfx/matrix/b2dhommatrixtools.hxx>
39 using namespace ::com::sun::star
;
41 namespace cppcanvas::internal
46 class BitmapAction
: public CachedPrimitiveBase
49 BitmapAction( const ::BitmapEx
&,
50 const ::basegfx::B2DPoint
& rDstPoint
,
51 const CanvasSharedPtr
&,
53 BitmapAction( const ::BitmapEx
&,
54 const ::basegfx::B2DPoint
& rDstPoint
,
55 const ::basegfx::B2DVector
& rDstSize
,
56 const CanvasSharedPtr
&,
59 virtual bool renderSubset( const ::basegfx::B2DHomMatrix
& rTransformation
,
60 const Subset
& rSubset
) const override
;
62 virtual ::basegfx::B2DRange
getBounds( const ::basegfx::B2DHomMatrix
& rTransformation
) const override
;
63 virtual ::basegfx::B2DRange
getBounds( const ::basegfx::B2DHomMatrix
& rTransformation
,
64 const Subset
& rSubset
) const override
;
66 virtual sal_Int32
getActionCount() const override
;
70 virtual bool renderPrimitive( uno::Reference
< rendering::XCachedPrimitive
>& rCachedPrimitive
,
71 const ::basegfx::B2DHomMatrix
& rTransformation
) const override
;
73 uno::Reference
< rendering::XBitmap
> mxBitmap
;
74 CanvasSharedPtr mpCanvas
;
75 rendering::RenderState maState
;
79 BitmapAction::BitmapAction( const ::BitmapEx
& rBmpEx
,
80 const ::basegfx::B2DPoint
& rDstPoint
,
81 const CanvasSharedPtr
& rCanvas
,
82 const OutDevState
& rState
) :
83 CachedPrimitiveBase( rCanvas
, true ),
84 mxBitmap( vcl::unotools::xBitmapFromBitmapEx( rBmpEx
) ),
87 tools::initRenderState(maState
,rState
);
89 // Setup transformation such that the next render call is
91 const basegfx::B2DHomMatrix
aLocalTransformation(basegfx::utils::createTranslateB2DHomMatrix(rDstPoint
));
92 ::canvas::tools::appendToRenderState( maState
,
93 aLocalTransformation
);
95 // correct clip (which is relative to original transform)
96 tools::modifyClip( maState
,
104 BitmapAction::BitmapAction( const ::BitmapEx
& rBmpEx
,
105 const ::basegfx::B2DPoint
& rDstPoint
,
106 const ::basegfx::B2DVector
& rDstSize
,
107 const CanvasSharedPtr
& rCanvas
,
108 const OutDevState
& rState
) :
109 CachedPrimitiveBase( rCanvas
, true ),
110 mxBitmap( vcl::unotools::xBitmapFromBitmapEx( rBmpEx
) ),
113 tools::initRenderState(maState
,rState
);
115 // Setup transformation such that the next render call is
116 // moved rPoint away, and scaled according to the ratio
117 // given by src and dst size.
118 const ::Size
aBmpSize( rBmpEx
.GetSizePixel() );
120 const ::basegfx::B2DVector
aScale( rDstSize
.getX() / aBmpSize
.Width(),
121 rDstSize
.getY() / aBmpSize
.Height() );
122 const basegfx::B2DHomMatrix
aLocalTransformation(basegfx::utils::createScaleTranslateB2DHomMatrix(
124 ::canvas::tools::appendToRenderState( maState
, aLocalTransformation
);
126 // correct clip (which is relative to original transform)
127 tools::modifyClip( maState
,
135 bool BitmapAction::renderPrimitive( uno::Reference
< rendering::XCachedPrimitive
>& rCachedPrimitive
,
136 const ::basegfx::B2DHomMatrix
& rTransformation
) const
138 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::BitmapAction::renderPrimitive()" );
139 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::BitmapAction: 0x" << std::hex
<< this );
141 rendering::RenderState
aLocalState( maState
);
142 ::canvas::tools::prependToRenderState(aLocalState
, rTransformation
);
144 rCachedPrimitive
= mpCanvas
->getUNOCanvas()->drawBitmap( mxBitmap
,
145 mpCanvas
->getViewState(),
151 bool BitmapAction::renderSubset( const ::basegfx::B2DHomMatrix
& rTransformation
,
152 const Subset
& rSubset
) const
154 // bitmap only contains a single action, fail if subset
155 // requests different range
156 if( rSubset
.mnSubsetBegin
!= 0 ||
157 rSubset
.mnSubsetEnd
!= 1 )
160 return CachedPrimitiveBase::render( rTransformation
);
163 ::basegfx::B2DRange
BitmapAction::getBounds( const ::basegfx::B2DHomMatrix
& rTransformation
) const
165 rendering::RenderState
aLocalState( maState
);
166 ::canvas::tools::prependToRenderState(aLocalState
, rTransformation
);
168 const geometry::IntegerSize2D
aSize( mxBitmap
->getSize() );
170 return tools::calcDevicePixelBounds( ::basegfx::B2DRange( 0,0,
173 mpCanvas
->getViewState(),
177 ::basegfx::B2DRange
BitmapAction::getBounds( const ::basegfx::B2DHomMatrix
& rTransformation
,
178 const Subset
& rSubset
) const
180 // bitmap only contains a single action, empty bounds
181 // if subset requests different range
182 if( rSubset
.mnSubsetBegin
!= 0 ||
183 rSubset
.mnSubsetEnd
!= 1 )
184 return ::basegfx::B2DRange();
186 return getBounds( rTransformation
);
189 sal_Int32
BitmapAction::getActionCount() const
195 std::shared_ptr
<Action
> BitmapActionFactory::createBitmapAction( const ::BitmapEx
& rBmpEx
,
196 const ::basegfx::B2DPoint
& rDstPoint
,
197 const CanvasSharedPtr
& rCanvas
,
198 const OutDevState
& rState
)
200 return std::make_shared
<BitmapAction
>(rBmpEx
, rDstPoint
, rCanvas
, rState
);
203 std::shared_ptr
<Action
> BitmapActionFactory::createBitmapAction( const ::BitmapEx
& rBmpEx
,
204 const ::basegfx::B2DPoint
& rDstPoint
,
205 const ::basegfx::B2DVector
& rDstSize
,
206 const CanvasSharedPtr
& rCanvas
,
207 const OutDevState
& rState
)
209 return std::make_shared
<BitmapAction
>(rBmpEx
,
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */