1*25b11142SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*25b11142SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*25b11142SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*25b11142SAndrew Rist  * distributed with this work for additional information
6*25b11142SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*25b11142SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*25b11142SAndrew Rist  * "License"); you may not use this file except in compliance
9*25b11142SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*25b11142SAndrew Rist  *
11*25b11142SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*25b11142SAndrew Rist  *
13*25b11142SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*25b11142SAndrew Rist  * software distributed under the License is distributed on an
15*25b11142SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*25b11142SAndrew Rist  * KIND, either express or implied.  See the License for the
17*25b11142SAndrew Rist  * specific language governing permissions and limitations
18*25b11142SAndrew Rist  * under the License.
19*25b11142SAndrew Rist  *
20*25b11142SAndrew Rist  *************************************************************/
21*25b11142SAndrew Rist 
22*25b11142SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_cppcanvas.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <rtl/logfile.hxx>
28cdf0e10cSrcweir #include <com/sun/star/rendering/XBitmap.hpp>
29cdf0e10cSrcweir #include <com/sun/star/rendering/RepaintResult.hpp>
30cdf0e10cSrcweir #include <com/sun/star/rendering/XCachedPrimitive.hpp>
31cdf0e10cSrcweir #include <vcl/bitmapex.hxx>
32cdf0e10cSrcweir #include <tools/gen.hxx>
33cdf0e10cSrcweir #include <vcl/canvastools.hxx>
34cdf0e10cSrcweir #include <canvas/canvastools.hxx>
35cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
36cdf0e10cSrcweir #include <basegfx/vector/b2dsize.hxx>
37cdf0e10cSrcweir #include <basegfx/point/b2dpoint.hxx>
38cdf0e10cSrcweir #include <basegfx/range/b2drange.hxx>
39cdf0e10cSrcweir #include <basegfx/tools/canvastools.hxx>
40cdf0e10cSrcweir #include <boost/utility.hpp>
41cdf0e10cSrcweir #include "cachedprimitivebase.hxx"
42cdf0e10cSrcweir #include "bitmapaction.hxx"
43cdf0e10cSrcweir #include "outdevstate.hxx"
44cdf0e10cSrcweir #include "mtftools.hxx"
45cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrixtools.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir using namespace ::com::sun::star;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir namespace cppcanvas
51cdf0e10cSrcweir {
52cdf0e10cSrcweir     namespace internal
53cdf0e10cSrcweir     {
54cdf0e10cSrcweir         namespace
55cdf0e10cSrcweir         {
56cdf0e10cSrcweir 
57cdf0e10cSrcweir             class BitmapAction : public CachedPrimitiveBase
58cdf0e10cSrcweir             {
59cdf0e10cSrcweir             public:
60cdf0e10cSrcweir                 BitmapAction( const ::BitmapEx&,
61cdf0e10cSrcweir                               const ::basegfx::B2DPoint& rDstPoint,
62cdf0e10cSrcweir                               const CanvasSharedPtr&,
63cdf0e10cSrcweir                               const OutDevState& );
64cdf0e10cSrcweir                 BitmapAction( const ::BitmapEx&,
65cdf0e10cSrcweir                               const ::basegfx::B2DPoint&  rDstPoint,
66cdf0e10cSrcweir                               const ::basegfx::B2DVector& rDstSize,
67cdf0e10cSrcweir                               const CanvasSharedPtr&,
68cdf0e10cSrcweir                               const OutDevState& );
69cdf0e10cSrcweir 
70cdf0e10cSrcweir                 virtual bool render( const ::basegfx::B2DHomMatrix& rTransformation,
71cdf0e10cSrcweir                                      const Subset&					rSubset ) const;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir                 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const;
74cdf0e10cSrcweir                 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix&	rTransformation,
75cdf0e10cSrcweir                                                        const Subset&					rSubset ) const;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir                 virtual sal_Int32 getActionCount() const;
78cdf0e10cSrcweir 
79cdf0e10cSrcweir             private:
80cdf0e10cSrcweir                 using Action::render;
81cdf0e10cSrcweir                 virtual bool render( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
82cdf0e10cSrcweir                                      const ::basegfx::B2DHomMatrix&                 rTransformation ) const;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir                 uno::Reference< rendering::XBitmap > 					mxBitmap;
85cdf0e10cSrcweir                 CanvasSharedPtr											mpCanvas;
86cdf0e10cSrcweir                 rendering::RenderState									maState;
87cdf0e10cSrcweir             };
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 
BitmapAction(const::BitmapEx & rBmpEx,const::basegfx::B2DPoint & rDstPoint,const CanvasSharedPtr & rCanvas,const OutDevState & rState)90cdf0e10cSrcweir             BitmapAction::BitmapAction( const ::BitmapEx&		   rBmpEx,
91cdf0e10cSrcweir                                         const ::basegfx::B2DPoint& rDstPoint,
92cdf0e10cSrcweir                                         const CanvasSharedPtr&     rCanvas,
93cdf0e10cSrcweir                                         const OutDevState&         rState ) :
94cdf0e10cSrcweir                 CachedPrimitiveBase( rCanvas, true ),
95cdf0e10cSrcweir                 mxBitmap( ::vcl::unotools::xBitmapFromBitmapEx( rCanvas->getUNOCanvas()->getDevice(),
96cdf0e10cSrcweir                                                                 rBmpEx ) ),
97cdf0e10cSrcweir                 mpCanvas( rCanvas ),
98cdf0e10cSrcweir                 maState()
99cdf0e10cSrcweir             {
100cdf0e10cSrcweir                 tools::initRenderState(maState,rState);
101cdf0e10cSrcweir 
102cdf0e10cSrcweir                 // Setup transformation such that the next render call is
103cdf0e10cSrcweir                 // moved rPoint away.
104cdf0e10cSrcweir 				const basegfx::B2DHomMatrix	aLocalTransformation(basegfx::tools::createTranslateB2DHomMatrix(rDstPoint));
105cdf0e10cSrcweir                 ::canvas::tools::appendToRenderState( maState,
106cdf0e10cSrcweir                                                       aLocalTransformation );
107cdf0e10cSrcweir 
108cdf0e10cSrcweir                 // correct clip (which is relative to original transform)
109cdf0e10cSrcweir                 tools::modifyClip( maState,
110cdf0e10cSrcweir                                    rState,
111cdf0e10cSrcweir                                    rCanvas,
112cdf0e10cSrcweir                                    rDstPoint,
113cdf0e10cSrcweir                                    NULL,
114cdf0e10cSrcweir                                    NULL );
115cdf0e10cSrcweir             }
116cdf0e10cSrcweir 
BitmapAction(const::BitmapEx & rBmpEx,const::basegfx::B2DPoint & rDstPoint,const::basegfx::B2DVector & rDstSize,const CanvasSharedPtr & rCanvas,const OutDevState & rState)117cdf0e10cSrcweir             BitmapAction::BitmapAction( const ::BitmapEx&		    rBmpEx,
118cdf0e10cSrcweir                                         const ::basegfx::B2DPoint&  rDstPoint,
119cdf0e10cSrcweir                                         const ::basegfx::B2DVector& rDstSize,
120cdf0e10cSrcweir                                         const CanvasSharedPtr&      rCanvas,
121cdf0e10cSrcweir                                         const OutDevState&          rState 		) :
122cdf0e10cSrcweir                 CachedPrimitiveBase( rCanvas, true ),
123cdf0e10cSrcweir                 mxBitmap( ::vcl::unotools::xBitmapFromBitmapEx( rCanvas->getUNOCanvas()->getDevice(),
124cdf0e10cSrcweir                                                                 rBmpEx ) ),
125cdf0e10cSrcweir                 mpCanvas( rCanvas ),
126cdf0e10cSrcweir                 maState()
127cdf0e10cSrcweir             {
128cdf0e10cSrcweir                 tools::initRenderState(maState,rState);
129cdf0e10cSrcweir 
130cdf0e10cSrcweir                 // Setup transformation such that the next render call is
131cdf0e10cSrcweir                 // moved rPoint away, and scaled according to the ratio
132cdf0e10cSrcweir                 // given by src and dst size.
133cdf0e10cSrcweir                 const ::Size aBmpSize( rBmpEx.GetSizePixel() );
134cdf0e10cSrcweir 
135cdf0e10cSrcweir                 const ::basegfx::B2DVector aScale( rDstSize.getX() / aBmpSize.Width(),
136cdf0e10cSrcweir                                                    rDstSize.getY() / aBmpSize.Height() );
137cdf0e10cSrcweir 				const basegfx::B2DHomMatrix	aLocalTransformation(basegfx::tools::createScaleTranslateB2DHomMatrix(
138cdf0e10cSrcweir 					aScale, rDstPoint));
139cdf0e10cSrcweir                 ::canvas::tools::appendToRenderState( maState, aLocalTransformation );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir                 // correct clip (which is relative to original transform)
142cdf0e10cSrcweir                 tools::modifyClip( maState,
143cdf0e10cSrcweir                                    rState,
144cdf0e10cSrcweir                                    rCanvas,
145cdf0e10cSrcweir                                    rDstPoint,
146cdf0e10cSrcweir                                    &aScale,
147cdf0e10cSrcweir                                    NULL );
148cdf0e10cSrcweir             }
149cdf0e10cSrcweir 
render(uno::Reference<rendering::XCachedPrimitive> & rCachedPrimitive,const::basegfx::B2DHomMatrix & rTransformation) const150cdf0e10cSrcweir             bool BitmapAction::render( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
151cdf0e10cSrcweir                                        const ::basegfx::B2DHomMatrix&                 rTransformation ) const
152cdf0e10cSrcweir             {
153cdf0e10cSrcweir                 RTL_LOGFILE_CONTEXT( aLog, "::cppcanvas::internal::BitmapAction::render()" );
154cdf0e10cSrcweir                 RTL_LOGFILE_CONTEXT_TRACE1( aLog, "::cppcanvas::internal::BitmapAction: 0x%X", this );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir                 rendering::RenderState aLocalState( maState );
157cdf0e10cSrcweir                 ::canvas::tools::prependToRenderState(aLocalState, rTransformation);
158cdf0e10cSrcweir 
159cdf0e10cSrcweir                 rCachedPrimitive = mpCanvas->getUNOCanvas()->drawBitmap( mxBitmap,
160cdf0e10cSrcweir                                                                          mpCanvas->getViewState(),
161cdf0e10cSrcweir                                                                          aLocalState );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir                 return true;
164cdf0e10cSrcweir             }
165cdf0e10cSrcweir 
render(const::basegfx::B2DHomMatrix & rTransformation,const Subset & rSubset) const166cdf0e10cSrcweir             bool BitmapAction::render( const ::basegfx::B2DHomMatrix&	rTransformation,
167cdf0e10cSrcweir                                        const Subset&					rSubset ) const
168cdf0e10cSrcweir             {
169cdf0e10cSrcweir                 // bitmap only contains a single action, fail if subset
170cdf0e10cSrcweir                 // requests different range
171cdf0e10cSrcweir                 if( rSubset.mnSubsetBegin != 0 ||
172cdf0e10cSrcweir                     rSubset.mnSubsetEnd != 1 )
173cdf0e10cSrcweir                     return false;
174cdf0e10cSrcweir 
175cdf0e10cSrcweir                 return CachedPrimitiveBase::render( rTransformation );
176cdf0e10cSrcweir             }
177cdf0e10cSrcweir 
getBounds(const::basegfx::B2DHomMatrix & rTransformation) const178cdf0e10cSrcweir             ::basegfx::B2DRange BitmapAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const
179cdf0e10cSrcweir             {
180cdf0e10cSrcweir                 rendering::RenderState aLocalState( maState );
181cdf0e10cSrcweir                 ::canvas::tools::prependToRenderState(aLocalState, rTransformation);
182cdf0e10cSrcweir 
183cdf0e10cSrcweir                 const geometry::IntegerSize2D aSize( mxBitmap->getSize() );
184cdf0e10cSrcweir 
185cdf0e10cSrcweir                 return tools::calcDevicePixelBounds( ::basegfx::B2DRange( 0,0,
186cdf0e10cSrcweir                                                                           aSize.Width,
187cdf0e10cSrcweir                                                                           aSize.Height ),
188cdf0e10cSrcweir                                                      mpCanvas->getViewState(),
189cdf0e10cSrcweir                                                      aLocalState );
190cdf0e10cSrcweir             }
191cdf0e10cSrcweir 
getBounds(const::basegfx::B2DHomMatrix & rTransformation,const Subset & rSubset) const192cdf0e10cSrcweir             ::basegfx::B2DRange BitmapAction::getBounds( const ::basegfx::B2DHomMatrix&	rTransformation,
193cdf0e10cSrcweir                                                          const Subset&					rSubset ) const
194cdf0e10cSrcweir             {
195cdf0e10cSrcweir                 // bitmap only contains a single action, empty bounds
196cdf0e10cSrcweir                 // if subset requests different range
197cdf0e10cSrcweir                 if( rSubset.mnSubsetBegin != 0 ||
198cdf0e10cSrcweir                     rSubset.mnSubsetEnd != 1 )
199cdf0e10cSrcweir                     return ::basegfx::B2DRange();
200cdf0e10cSrcweir 
201cdf0e10cSrcweir                 return getBounds( rTransformation );
202cdf0e10cSrcweir             }
203cdf0e10cSrcweir 
getActionCount() const204cdf0e10cSrcweir             sal_Int32 BitmapAction::getActionCount() const
205cdf0e10cSrcweir             {
206cdf0e10cSrcweir                 return 1;
207cdf0e10cSrcweir             }
208cdf0e10cSrcweir         }
209cdf0e10cSrcweir 
createBitmapAction(const::BitmapEx & rBmpEx,const::basegfx::B2DPoint & rDstPoint,const CanvasSharedPtr & rCanvas,const OutDevState & rState)210cdf0e10cSrcweir         ActionSharedPtr BitmapActionFactory::createBitmapAction( const ::BitmapEx&          rBmpEx,
211cdf0e10cSrcweir                                                                  const ::basegfx::B2DPoint& rDstPoint,
212cdf0e10cSrcweir                                                                  const CanvasSharedPtr&     rCanvas,
213cdf0e10cSrcweir                                                                  const OutDevState&         rState )
214cdf0e10cSrcweir         {
215cdf0e10cSrcweir             return ActionSharedPtr( new BitmapAction(rBmpEx,
216cdf0e10cSrcweir                                                      rDstPoint,
217cdf0e10cSrcweir                                                      rCanvas,
218cdf0e10cSrcweir                                                      rState ) );
219cdf0e10cSrcweir         }
220cdf0e10cSrcweir 
createBitmapAction(const::BitmapEx & rBmpEx,const::basegfx::B2DPoint & rDstPoint,const::basegfx::B2DVector & rDstSize,const CanvasSharedPtr & rCanvas,const OutDevState & rState)221cdf0e10cSrcweir         ActionSharedPtr BitmapActionFactory::createBitmapAction( const ::BitmapEx&           rBmpEx,
222cdf0e10cSrcweir                                                                  const ::basegfx::B2DPoint&  rDstPoint,
223cdf0e10cSrcweir                                                                  const ::basegfx::B2DVector& rDstSize,
224cdf0e10cSrcweir                                                                  const CanvasSharedPtr&      rCanvas,
225cdf0e10cSrcweir                                                                  const OutDevState&          rState )
226cdf0e10cSrcweir         {
227cdf0e10cSrcweir             return ActionSharedPtr( new BitmapAction(rBmpEx,
228cdf0e10cSrcweir                                                      rDstPoint,
229cdf0e10cSrcweir                                                      rDstSize,
230cdf0e10cSrcweir                                                      rCanvas,
231cdf0e10cSrcweir                                                      rState ) );
232cdf0e10cSrcweir         }
233cdf0e10cSrcweir     }
234cdf0e10cSrcweir }
235