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_canvas.hxx"
30 
31 #include <canvas/debug.hxx>
32 #include <tools/diagnose_ex.h>
33 #include <canvas/verbosetrace.hxx>
34 
35 #include <rtl/math.hxx>
36 
37 #include <vcl/outdev.hxx>
38 #include <vcl/bitmap.hxx>
39 #include <vcl/alpha.hxx>
40 #include <vcl/bitmapex.hxx>
41 #include <vcl/canvastools.hxx>
42 
43 #include <basegfx/matrix/b2dhommatrix.hxx>
44 #include <basegfx/point/b2dpoint.hxx>
45 #include <basegfx/tools/canvastools.hxx>
46 #include <basegfx/polygon/b2dpolygon.hxx>
47 #include <basegfx/polygon/b2dpolygontools.hxx>
48 #include <basegfx/polygon/b2dpolypolygontools.hxx>
49 #include <basegfx/numeric/ftools.hxx>
50 
51 #include <canvas/canvastools.hxx>
52 
53 #include "canvascustomsprite.hxx"
54 
55 
56 using namespace ::com::sun::star;
57 
58 
59 namespace vclcanvas
60 {
61 
62     CanvasCustomSprite::CanvasCustomSprite( const geometry::RealSize2D&               rSpriteSize,
63                                             rendering::XGraphicDevice&                rDevice,
64                                             const ::canvas::SpriteSurface::Reference& rOwningSpriteCanvas,
65                                             const OutDevProviderSharedPtr&            rOutDevProvider,
66                                             bool                                      bShowSpriteBounds )
67     {
68         ENSURE_OR_THROW( rOwningSpriteCanvas.get() &&
69                          rOutDevProvider,
70                          "CanvasCustomSprite::CanvasCustomSprite(): Invalid sprite canvas" );
71 
72         // setup back buffer
73         // -----------------
74 
75         const ::Size aSize(
76             static_cast<sal_Int32>( ::std::max( 1.0,
77                                                 ceil( rSpriteSize.Width ))),  // round up to nearest int,
78                 															  // enforce sprite to have at
79                 											 				  // least (1,1) pixel size
80             static_cast<sal_Int32>( ::std::max( 1.0,
81                                                 ceil( rSpriteSize.Height ))) );
82 
83         // create content backbuffer in screen depth
84         BackBufferSharedPtr pBackBuffer( new BackBuffer( rOutDevProvider->getOutDev() ) );
85         pBackBuffer->setSize( aSize );
86 
87         // create mask backbuffer, with one bit color depth
88         BackBufferSharedPtr pBackBufferMask( new BackBuffer( rOutDevProvider->getOutDev(),
89                                                              true ) );
90         pBackBufferMask->setSize( aSize );
91 
92         // TODO(F1): Implement alpha vdev (could prolly enable
93         // antialiasing again, then)
94 
95         // disable font antialiasing (causes ugly shadows otherwise)
96         pBackBuffer->getOutDev().SetAntialiasing( ANTIALIASING_DISABLE_TEXT );
97         pBackBufferMask->getOutDev().SetAntialiasing( ANTIALIASING_DISABLE_TEXT );
98 
99         // set mask vdev drawmode, such that everything is painted
100         // black. That leaves us with a binary image, white for
101         // background, black for painted content
102         pBackBufferMask->getOutDev().SetDrawMode( DRAWMODE_BLACKLINE | DRAWMODE_BLACKFILL | DRAWMODE_BLACKTEXT |
103                                                   DRAWMODE_BLACKGRADIENT | DRAWMODE_BLACKBITMAP );
104 
105 
106         // setup canvas helper
107         // -------------------
108 
109         // always render into back buffer, don't preserve state (it's
110         // our private VDev, after all), have notion of alpha
111         maCanvasHelper.init( rDevice,
112                              pBackBuffer,
113                              false,
114                              true );
115         maCanvasHelper.setBackgroundOutDev( pBackBufferMask );
116 
117 
118         // setup sprite helper
119         // -------------------
120 
121         maSpriteHelper.init( rSpriteSize,
122                              rOwningSpriteCanvas,
123                              pBackBuffer,
124                              pBackBufferMask,
125                              bShowSpriteBounds );
126 
127         // clear sprite to 100% transparent
128         maCanvasHelper.clear();
129     }
130 
131     void SAL_CALL CanvasCustomSprite::disposing()
132     {
133         tools::LocalGuard aGuard;
134 
135         // forward to parent
136         CanvasCustomSpriteBaseT::disposing();
137     }
138 
139 #define IMPLEMENTATION_NAME "VCLCanvas.CanvasCustomSprite"
140 #define SERVICE_NAME "com.sun.star.rendering.CanvasCustomSprite"
141 
142     ::rtl::OUString SAL_CALL CanvasCustomSprite::getImplementationName() throw( uno::RuntimeException )
143     {
144         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
145     }
146 
147     sal_Bool SAL_CALL CanvasCustomSprite::supportsService( const ::rtl::OUString& ServiceName ) throw( uno::RuntimeException )
148     {
149         return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
150     }
151 
152     uno::Sequence< ::rtl::OUString > SAL_CALL CanvasCustomSprite::getSupportedServiceNames()  throw( uno::RuntimeException )
153     {
154         uno::Sequence< ::rtl::OUString > aRet(1);
155         aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
156 
157         return aRet;
158     }
159 
160     // Sprite
161     void CanvasCustomSprite::redraw( OutputDevice& rOutDev,
162                                      bool          bBufferedUpdate ) const
163     {
164         tools::LocalGuard aGuard;
165 
166         redraw( rOutDev, maSpriteHelper.getPosPixel(), bBufferedUpdate );
167     }
168 
169     void CanvasCustomSprite::redraw( OutputDevice&              rOutDev,
170                                      const ::basegfx::B2DPoint& rOrigOutputPos,
171                                      bool                       bBufferedUpdate ) const
172     {
173         tools::LocalGuard aGuard;
174 
175         maSpriteHelper.redraw( rOutDev,
176                                rOrigOutputPos,
177                                mbSurfaceDirty,
178                                bBufferedUpdate );
179 
180         mbSurfaceDirty = false;
181     }
182 
183     bool CanvasCustomSprite::repaint( const GraphicObjectSharedPtr&	rGrf,
184                                       const rendering::ViewState&   viewState,
185                                       const rendering::RenderState& renderState,
186                                       const ::Point& 				rPt,
187                                       const ::Size& 				rSz,
188                                       const GraphicAttr&			rAttr ) const
189     {
190         tools::LocalGuard aGuard;
191 
192         mbSurfaceDirty = true;
193 
194         return maCanvasHelper.repaint( rGrf, viewState, renderState, rPt, rSz, rAttr );
195     }
196 
197 }
198