Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / cppcanvas / source / mtfrenderer / bitmapaction.cxx
blob2777abb7e24c040ba7fdc695144089a461b5421e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/RepaintResult.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/vector/b2dsize.hxx>
30 #include <basegfx/point/b2dpoint.hxx>
31 #include <basegfx/range/b2drange.hxx>
32 #include <basegfx/tools/canvastools.hxx>
33 #include "cachedprimitivebase.hxx"
34 #include "bitmapaction.hxx"
35 #include "outdevstate.hxx"
36 #include "mtftools.hxx"
37 #include <basegfx/matrix/b2dhommatrixtools.hxx>
40 using namespace ::com::sun::star;
42 namespace cppcanvas
44 namespace internal
46 namespace
49 class BitmapAction : public CachedPrimitiveBase
51 public:
52 BitmapAction( const ::BitmapEx&,
53 const ::basegfx::B2DPoint& rDstPoint,
54 const CanvasSharedPtr&,
55 const OutDevState& );
56 BitmapAction( const ::BitmapEx&,
57 const ::basegfx::B2DPoint& rDstPoint,
58 const ::basegfx::B2DVector& rDstSize,
59 const CanvasSharedPtr&,
60 const OutDevState& );
62 virtual bool renderSubset( const ::basegfx::B2DHomMatrix& rTransformation,
63 const Subset& rSubset ) const override;
65 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const override;
66 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation,
67 const Subset& rSubset ) const override;
69 virtual sal_Int32 getActionCount() const override;
71 private:
72 using Action::render;
73 virtual bool renderPrimitive( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
74 const ::basegfx::B2DHomMatrix& rTransformation ) const override;
76 uno::Reference< rendering::XBitmap > mxBitmap;
77 CanvasSharedPtr mpCanvas;
78 rendering::RenderState maState;
82 BitmapAction::BitmapAction( const ::BitmapEx& rBmpEx,
83 const ::basegfx::B2DPoint& rDstPoint,
84 const CanvasSharedPtr& rCanvas,
85 const OutDevState& rState ) :
86 CachedPrimitiveBase( rCanvas, true ),
87 mxBitmap( vcl::unotools::xBitmapFromBitmapEx( rCanvas->getUNOCanvas()->getDevice(),
88 rBmpEx ) ),
89 mpCanvas( rCanvas ),
90 maState()
92 tools::initRenderState(maState,rState);
94 // Setup transformation such that the next render call is
95 // moved rPoint away.
96 const basegfx::B2DHomMatrix aLocalTransformation(basegfx::tools::createTranslateB2DHomMatrix(rDstPoint));
97 ::canvas::tools::appendToRenderState( maState,
98 aLocalTransformation );
100 // correct clip (which is relative to original transform)
101 tools::modifyClip( maState,
102 rState,
103 rCanvas,
104 rDstPoint,
105 nullptr,
106 nullptr );
109 BitmapAction::BitmapAction( const ::BitmapEx& rBmpEx,
110 const ::basegfx::B2DPoint& rDstPoint,
111 const ::basegfx::B2DVector& rDstSize,
112 const CanvasSharedPtr& rCanvas,
113 const OutDevState& rState ) :
114 CachedPrimitiveBase( rCanvas, true ),
115 mxBitmap( vcl::unotools::xBitmapFromBitmapEx( rCanvas->getUNOCanvas()->getDevice(),
116 rBmpEx ) ),
117 mpCanvas( rCanvas ),
118 maState()
120 tools::initRenderState(maState,rState);
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 const ::Size aBmpSize( rBmpEx.GetSizePixel() );
127 const ::basegfx::B2DVector aScale( rDstSize.getX() / aBmpSize.Width(),
128 rDstSize.getY() / aBmpSize.Height() );
129 const basegfx::B2DHomMatrix aLocalTransformation(basegfx::tools::createScaleTranslateB2DHomMatrix(
130 aScale, rDstPoint));
131 ::canvas::tools::appendToRenderState( maState, aLocalTransformation );
133 // correct clip (which is relative to original transform)
134 tools::modifyClip( maState,
135 rState,
136 rCanvas,
137 rDstPoint,
138 &aScale,
139 nullptr );
142 bool BitmapAction::renderPrimitive( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
143 const ::basegfx::B2DHomMatrix& rTransformation ) const
145 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::BitmapAction::renderPrimitive()" );
146 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::BitmapAction: 0x" << std::hex << this );
148 rendering::RenderState aLocalState( maState );
149 ::canvas::tools::prependToRenderState(aLocalState, rTransformation);
151 rCachedPrimitive = mpCanvas->getUNOCanvas()->drawBitmap( mxBitmap,
152 mpCanvas->getViewState(),
153 aLocalState );
155 return true;
158 bool BitmapAction::renderSubset( const ::basegfx::B2DHomMatrix& rTransformation,
159 const Subset& rSubset ) const
161 // bitmap only contains a single action, fail if subset
162 // requests different range
163 if( rSubset.mnSubsetBegin != 0 ||
164 rSubset.mnSubsetEnd != 1 )
165 return false;
167 return CachedPrimitiveBase::render( rTransformation );
170 ::basegfx::B2DRange BitmapAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const
172 rendering::RenderState aLocalState( maState );
173 ::canvas::tools::prependToRenderState(aLocalState, rTransformation);
175 const geometry::IntegerSize2D aSize( mxBitmap->getSize() );
177 return tools::calcDevicePixelBounds( ::basegfx::B2DRange( 0,0,
178 aSize.Width,
179 aSize.Height ),
180 mpCanvas->getViewState(),
181 aLocalState );
184 ::basegfx::B2DRange BitmapAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation,
185 const Subset& rSubset ) const
187 // bitmap only contains a single action, empty bounds
188 // if subset requests different range
189 if( rSubset.mnSubsetBegin != 0 ||
190 rSubset.mnSubsetEnd != 1 )
191 return ::basegfx::B2DRange();
193 return getBounds( rTransformation );
196 sal_Int32 BitmapAction::getActionCount() const
198 return 1;
202 ActionSharedPtr BitmapActionFactory::createBitmapAction( const ::BitmapEx& rBmpEx,
203 const ::basegfx::B2DPoint& rDstPoint,
204 const CanvasSharedPtr& rCanvas,
205 const OutDevState& rState )
207 return ActionSharedPtr( new BitmapAction(rBmpEx,
208 rDstPoint,
209 rCanvas,
210 rState ) );
213 ActionSharedPtr BitmapActionFactory::createBitmapAction( const ::BitmapEx& rBmpEx,
214 const ::basegfx::B2DPoint& rDstPoint,
215 const ::basegfx::B2DVector& rDstSize,
216 const CanvasSharedPtr& rCanvas,
217 const OutDevState& rState )
219 return ActionSharedPtr( new BitmapAction(rBmpEx,
220 rDstPoint,
221 rDstSize,
222 rCanvas,
223 rState ) );
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */