1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cppcanvas.hxx"
30 
31 #include <rtl/logfile.hxx>
32 #include <com/sun/star/rendering/XBitmap.hpp>
33 #include <com/sun/star/rendering/RepaintResult.hpp>
34 #include <com/sun/star/rendering/XCachedPrimitive.hpp>
35 #include <vcl/bitmapex.hxx>
36 #include <tools/gen.hxx>
37 #include <vcl/canvastools.hxx>
38 #include <canvas/canvastools.hxx>
39 #include <basegfx/matrix/b2dhommatrix.hxx>
40 #include <basegfx/vector/b2dsize.hxx>
41 #include <basegfx/point/b2dpoint.hxx>
42 #include <basegfx/range/b2drange.hxx>
43 #include <basegfx/tools/canvastools.hxx>
44 #include <boost/utility.hpp>
45 #include "cachedprimitivebase.hxx"
46 #include "bitmapaction.hxx"
47 #include "outdevstate.hxx"
48 #include "mtftools.hxx"
49 #include <basegfx/matrix/b2dhommatrixtools.hxx>
50 
51 
52 using namespace ::com::sun::star;
53 
54 namespace cppcanvas
55 {
56     namespace internal
57     {
58         namespace
59         {
60 
61             class BitmapAction : public CachedPrimitiveBase
62             {
63             public:
64                 BitmapAction( const ::BitmapEx&,
65                               const ::basegfx::B2DPoint& rDstPoint,
66                               const CanvasSharedPtr&,
67                               const OutDevState& );
68                 BitmapAction( const ::BitmapEx&,
69                               const ::basegfx::B2DPoint&  rDstPoint,
70                               const ::basegfx::B2DVector& rDstSize,
71                               const CanvasSharedPtr&,
72                               const OutDevState& );
73 
74                 virtual bool render( const ::basegfx::B2DHomMatrix& rTransformation,
75                                      const Subset&					rSubset ) const;
76 
77                 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const;
78                 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix&	rTransformation,
79                                                        const Subset&					rSubset ) const;
80 
81                 virtual sal_Int32 getActionCount() const;
82 
83             private:
84                 using Action::render;
85                 virtual bool render( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
86                                      const ::basegfx::B2DHomMatrix&                 rTransformation ) const;
87 
88                 uno::Reference< rendering::XBitmap > 					mxBitmap;
89                 CanvasSharedPtr											mpCanvas;
90                 rendering::RenderState									maState;
91             };
92 
93 
94             BitmapAction::BitmapAction( const ::BitmapEx&		   rBmpEx,
95                                         const ::basegfx::B2DPoint& rDstPoint,
96                                         const CanvasSharedPtr&     rCanvas,
97                                         const OutDevState&         rState ) :
98                 CachedPrimitiveBase( rCanvas, true ),
99                 mxBitmap( ::vcl::unotools::xBitmapFromBitmapEx( rCanvas->getUNOCanvas()->getDevice(),
100                                                                 rBmpEx ) ),
101                 mpCanvas( rCanvas ),
102                 maState()
103             {
104                 tools::initRenderState(maState,rState);
105 
106                 // Setup transformation such that the next render call is
107                 // moved rPoint away.
108 				const basegfx::B2DHomMatrix	aLocalTransformation(basegfx::tools::createTranslateB2DHomMatrix(rDstPoint));
109                 ::canvas::tools::appendToRenderState( maState,
110                                                       aLocalTransformation );
111 
112                 // correct clip (which is relative to original transform)
113                 tools::modifyClip( maState,
114                                    rState,
115                                    rCanvas,
116                                    rDstPoint,
117                                    NULL,
118                                    NULL );
119             }
120 
121             BitmapAction::BitmapAction( const ::BitmapEx&		    rBmpEx,
122                                         const ::basegfx::B2DPoint&  rDstPoint,
123                                         const ::basegfx::B2DVector& rDstSize,
124                                         const CanvasSharedPtr&      rCanvas,
125                                         const OutDevState&          rState 		) :
126                 CachedPrimitiveBase( rCanvas, true ),
127                 mxBitmap( ::vcl::unotools::xBitmapFromBitmapEx( rCanvas->getUNOCanvas()->getDevice(),
128                                                                 rBmpEx ) ),
129                 mpCanvas( rCanvas ),
130                 maState()
131             {
132                 tools::initRenderState(maState,rState);
133 
134                 // Setup transformation such that the next render call is
135                 // moved rPoint away, and scaled according to the ratio
136                 // given by src and dst size.
137                 const ::Size aBmpSize( rBmpEx.GetSizePixel() );
138 
139                 const ::basegfx::B2DVector aScale( rDstSize.getX() / aBmpSize.Width(),
140                                                    rDstSize.getY() / aBmpSize.Height() );
141 				const basegfx::B2DHomMatrix	aLocalTransformation(basegfx::tools::createScaleTranslateB2DHomMatrix(
142 					aScale, rDstPoint));
143                 ::canvas::tools::appendToRenderState( maState, aLocalTransformation );
144 
145                 // correct clip (which is relative to original transform)
146                 tools::modifyClip( maState,
147                                    rState,
148                                    rCanvas,
149                                    rDstPoint,
150                                    &aScale,
151                                    NULL );
152             }
153 
154             bool BitmapAction::render( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
155                                        const ::basegfx::B2DHomMatrix&                 rTransformation ) const
156             {
157                 RTL_LOGFILE_CONTEXT( aLog, "::cppcanvas::internal::BitmapAction::render()" );
158                 RTL_LOGFILE_CONTEXT_TRACE1( aLog, "::cppcanvas::internal::BitmapAction: 0x%X", this );
159 
160                 rendering::RenderState aLocalState( maState );
161                 ::canvas::tools::prependToRenderState(aLocalState, rTransformation);
162 
163                 rCachedPrimitive = mpCanvas->getUNOCanvas()->drawBitmap( mxBitmap,
164                                                                          mpCanvas->getViewState(),
165                                                                          aLocalState );
166 
167                 return true;
168             }
169 
170             bool BitmapAction::render( const ::basegfx::B2DHomMatrix&	rTransformation,
171                                        const Subset&					rSubset ) const
172             {
173                 // bitmap only contains a single action, fail if subset
174                 // requests different range
175                 if( rSubset.mnSubsetBegin != 0 ||
176                     rSubset.mnSubsetEnd != 1 )
177                     return false;
178 
179                 return CachedPrimitiveBase::render( rTransformation );
180             }
181 
182             ::basegfx::B2DRange BitmapAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const
183             {
184                 rendering::RenderState aLocalState( maState );
185                 ::canvas::tools::prependToRenderState(aLocalState, rTransformation);
186 
187                 const geometry::IntegerSize2D aSize( mxBitmap->getSize() );
188 
189                 return tools::calcDevicePixelBounds( ::basegfx::B2DRange( 0,0,
190                                                                           aSize.Width,
191                                                                           aSize.Height ),
192                                                      mpCanvas->getViewState(),
193                                                      aLocalState );
194             }
195 
196             ::basegfx::B2DRange BitmapAction::getBounds( const ::basegfx::B2DHomMatrix&	rTransformation,
197                                                          const Subset&					rSubset ) const
198             {
199                 // bitmap only contains a single action, empty bounds
200                 // if subset requests different range
201                 if( rSubset.mnSubsetBegin != 0 ||
202                     rSubset.mnSubsetEnd != 1 )
203                     return ::basegfx::B2DRange();
204 
205                 return getBounds( rTransformation );
206             }
207 
208             sal_Int32 BitmapAction::getActionCount() const
209             {
210                 return 1;
211             }
212         }
213 
214         ActionSharedPtr BitmapActionFactory::createBitmapAction( const ::BitmapEx&          rBmpEx,
215                                                                  const ::basegfx::B2DPoint& rDstPoint,
216                                                                  const CanvasSharedPtr&     rCanvas,
217                                                                  const OutDevState&         rState )
218         {
219             return ActionSharedPtr( new BitmapAction(rBmpEx,
220                                                      rDstPoint,
221                                                      rCanvas,
222                                                      rState ) );
223         }
224 
225         ActionSharedPtr BitmapActionFactory::createBitmapAction( const ::BitmapEx&           rBmpEx,
226                                                                  const ::basegfx::B2DPoint&  rDstPoint,
227                                                                  const ::basegfx::B2DVector& rDstSize,
228                                                                  const CanvasSharedPtr&      rCanvas,
229                                                                  const OutDevState&          rState )
230         {
231             return ActionSharedPtr( new BitmapAction(rBmpEx,
232                                                      rDstPoint,
233                                                      rDstSize,
234                                                      rCanvas,
235                                                      rState ) );
236         }
237     }
238 }
239