xref: /AOO41X/main/canvas/source/directx/dx_bitmapcanvashelper.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_canvas.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <canvas/debug.hxx>
32*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <rtl/logfile.hxx>
35*cdf0e10cSrcweir #include <rtl/math.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <com/sun/star/rendering/TexturingMode.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/rendering/CompositeOperation.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/rendering/RepaintResult.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/rendering/PathCapType.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/rendering/PathJoinType.hpp>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
44*cdf0e10cSrcweir #include <basegfx/point/b2dpoint.hxx>
45*cdf0e10cSrcweir #include <basegfx/tools/canvastools.hxx>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include <comphelper/sequence.hxx>
48*cdf0e10cSrcweir #include <canvas/canvastools.hxx>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include "dx_spritecanvas.hxx"
51*cdf0e10cSrcweir #include "dx_impltools.hxx"
52*cdf0e10cSrcweir #include "dx_canvasfont.hxx"
53*cdf0e10cSrcweir #include "dx_textlayout.hxx"
54*cdf0e10cSrcweir #include "dx_bitmapcanvashelper.hxx"
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir #include <algorithm>
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir using namespace ::com::sun::star;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir namespace dxcanvas
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir     BitmapCanvasHelper::BitmapCanvasHelper() :
64*cdf0e10cSrcweir         mpTarget()
65*cdf0e10cSrcweir     {}
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     void BitmapCanvasHelper::disposing()
68*cdf0e10cSrcweir     {
69*cdf0e10cSrcweir         mpTarget.reset();
70*cdf0e10cSrcweir         CanvasHelper::disposing();
71*cdf0e10cSrcweir     }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     void BitmapCanvasHelper::setTarget( const IBitmapSharedPtr& rTarget )
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir         ENSURE_OR_THROW( rTarget,
76*cdf0e10cSrcweir                           "BitmapCanvasHelper::setTarget(): Invalid target" );
77*cdf0e10cSrcweir         ENSURE_OR_THROW( !mpTarget.get(),
78*cdf0e10cSrcweir                           "BitmapCanvasHelper::setTarget(): target set, old target would be overwritten" );
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir         mpTarget = rTarget;
81*cdf0e10cSrcweir         CanvasHelper::setTarget(rTarget);
82*cdf0e10cSrcweir     }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     void BitmapCanvasHelper::setTarget( const IBitmapSharedPtr&   rTarget,
85*cdf0e10cSrcweir                                         const ::basegfx::B2ISize& rOutputOffset )
86*cdf0e10cSrcweir     {
87*cdf0e10cSrcweir         ENSURE_OR_THROW( rTarget,
88*cdf0e10cSrcweir                          "BitmapCanvasHelper::setTarget(): invalid target" );
89*cdf0e10cSrcweir         ENSURE_OR_THROW( !mpTarget.get(),
90*cdf0e10cSrcweir                          "BitmapCanvasHelper::setTarget(): target set, old target would be overwritten" );
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir         mpTarget = rTarget;
93*cdf0e10cSrcweir         CanvasHelper::setTarget(rTarget,rOutputOffset);
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     void BitmapCanvasHelper::clear()
97*cdf0e10cSrcweir     {
98*cdf0e10cSrcweir         if( needOutput() )
99*cdf0e10cSrcweir         {
100*cdf0e10cSrcweir             GraphicsSharedPtr pGraphics( mpTarget->getGraphics() );
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir             Gdiplus::Color aClearColor = hasAlpha() ?
103*cdf0e10cSrcweir                 Gdiplus::Color( 0,255,255,255 ) : Gdiplus::Color((Gdiplus::ARGB)Gdiplus::Color::White);
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir             ENSURE_OR_THROW(
106*cdf0e10cSrcweir                 Gdiplus::Ok == pGraphics->SetCompositingMode(
107*cdf0e10cSrcweir                     Gdiplus::CompositingModeSourceCopy ), // force set, don't blend
108*cdf0e10cSrcweir                 "BitmapCanvasHelper::clear(): GDI+ SetCompositingMode call failed" );
109*cdf0e10cSrcweir             ENSURE_OR_THROW(
110*cdf0e10cSrcweir                 Gdiplus::Ok == pGraphics->Clear( aClearColor ),
111*cdf0e10cSrcweir                 "BitmapCanvasHelper::clear(): GDI+ Clear call failed" );
112*cdf0e10cSrcweir         }
113*cdf0e10cSrcweir     }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     uno::Reference< rendering::XCachedPrimitive > BitmapCanvasHelper::drawTextLayout( const rendering::XCanvas* 					  /*pCanvas*/,
116*cdf0e10cSrcweir                                                                                       const uno::Reference< rendering::XTextLayout >& xLayoutetText,
117*cdf0e10cSrcweir                                                                                       const rendering::ViewState& 					  viewState,
118*cdf0e10cSrcweir                                                                                       const rendering::RenderState& 				  renderState )
119*cdf0e10cSrcweir     {
120*cdf0e10cSrcweir         ENSURE_OR_THROW( xLayoutetText.is(),
121*cdf0e10cSrcweir                           "BitmapCanvasHelper::drawTextLayout: layout is NULL");
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         if( needOutput() )
124*cdf0e10cSrcweir         {
125*cdf0e10cSrcweir 			TextLayout* pTextLayout =
126*cdf0e10cSrcweir                 dynamic_cast< TextLayout* >( xLayoutetText.get() );
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir             ENSURE_OR_THROW( pTextLayout,
129*cdf0e10cSrcweir                                 "BitmapCanvasHelper::drawTextLayout(): TextLayout not compatible with this canvas" );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 			pTextLayout->draw( mpTarget->getGraphics(),
132*cdf0e10cSrcweir                                viewState,
133*cdf0e10cSrcweir                                renderState,
134*cdf0e10cSrcweir                                maOutputOffset,
135*cdf0e10cSrcweir                                mpDevice,
136*cdf0e10cSrcweir                                mpTarget->hasAlpha() );
137*cdf0e10cSrcweir         }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir         return uno::Reference< rendering::XCachedPrimitive >(NULL);
140*cdf0e10cSrcweir     }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     void BitmapCanvasHelper::copyRect( const rendering::XCanvas* 							/*pCanvas*/,
143*cdf0e10cSrcweir                                        const uno::Reference< rendering::XBitmapCanvas >&	/*sourceCanvas*/,
144*cdf0e10cSrcweir                                        const geometry::RealRectangle2D& 					/*sourceRect*/,
145*cdf0e10cSrcweir                                        const rendering::ViewState& 							/*sourceViewState*/,
146*cdf0e10cSrcweir                                        const rendering::RenderState& 						/*sourceRenderState*/,
147*cdf0e10cSrcweir                                        const geometry::RealRectangle2D& 					/*destRect*/,
148*cdf0e10cSrcweir                                        const rendering::ViewState& 							/*destViewState*/,
149*cdf0e10cSrcweir                                        const rendering::RenderState& 						/*destRenderState*/ )
150*cdf0e10cSrcweir     {
151*cdf0e10cSrcweir         // TODO(F2): copyRect NYI
152*cdf0e10cSrcweir     }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     geometry::IntegerSize2D BitmapCanvasHelper::getSize()
155*cdf0e10cSrcweir     {
156*cdf0e10cSrcweir         if( !mpTarget )
157*cdf0e10cSrcweir             return geometry::IntegerSize2D(1, 1);
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir         return basegfx::unotools::integerSize2DFromB2ISize(mpTarget->getSize());
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     uno::Reference< rendering::XBitmap > BitmapCanvasHelper::getScaledBitmap( const geometry::RealSize2D& /*newSize*/,
163*cdf0e10cSrcweir                                                                               sal_Bool 					  /*beFast*/ )
164*cdf0e10cSrcweir     {
165*cdf0e10cSrcweir         // TODO(F1):
166*cdf0e10cSrcweir         return uno::Reference< rendering::XBitmap >();
167*cdf0e10cSrcweir     }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir     uno::Sequence< sal_Int8 > BitmapCanvasHelper::getData( rendering::IntegerBitmapLayout&     bitmapLayout,
170*cdf0e10cSrcweir                                                            const geometry::IntegerRectangle2D& rect )
171*cdf0e10cSrcweir     {
172*cdf0e10cSrcweir         RTL_LOGFILE_CONTEXT( aLog, "::dxcanvas::BitmapCanvasHelper::getData()" );
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         ENSURE_OR_THROW( mpTarget,
175*cdf0e10cSrcweir                           "::dxcanvas::BitmapCanvasHelper::getData(): disposed" );
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 		if( !mpTarget )
178*cdf0e10cSrcweir 	        return uno::Sequence< sal_Int8 >();
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 		bitmapLayout = getMemoryLayout();
181*cdf0e10cSrcweir 		return mpTarget->getData(bitmapLayout,rect);
182*cdf0e10cSrcweir     }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     void BitmapCanvasHelper::setData( const uno::Sequence< sal_Int8 >& 		data,
185*cdf0e10cSrcweir                                       const rendering::IntegerBitmapLayout& bitmapLayout,
186*cdf0e10cSrcweir                                       const geometry::IntegerRectangle2D& 	rect )
187*cdf0e10cSrcweir     {
188*cdf0e10cSrcweir         RTL_LOGFILE_CONTEXT( aLog, "::dxcanvas::BitmapCanvasHelper::setData()" );
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir         ENSURE_OR_THROW( mpTarget,
191*cdf0e10cSrcweir                           "::dxcanvas::BitmapCanvasHelper::setData(): disposed" );
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 		if( !mpTarget )
194*cdf0e10cSrcweir 			return;
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 		mpTarget->setData(data,bitmapLayout,rect);
197*cdf0e10cSrcweir     }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir     void BitmapCanvasHelper::setPixel( const uno::Sequence< sal_Int8 >&       color,
200*cdf0e10cSrcweir                                        const rendering::IntegerBitmapLayout&  bitmapLayout,
201*cdf0e10cSrcweir                                        const geometry::IntegerPoint2D&        pos )
202*cdf0e10cSrcweir     {
203*cdf0e10cSrcweir         RTL_LOGFILE_CONTEXT( aLog, "::dxcanvas::BitmapCanvasHelper::setPixel()" );
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir         ENSURE_OR_THROW( mpTarget,
206*cdf0e10cSrcweir                           "::dxcanvas::BitmapCanvasHelper::setPixel(): disposed" );
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 		if( !mpTarget )
209*cdf0e10cSrcweir 			return;
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 		mpTarget->setPixel(color,bitmapLayout,pos);
212*cdf0e10cSrcweir     }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir     uno::Sequence< sal_Int8 > BitmapCanvasHelper::getPixel( rendering::IntegerBitmapLayout&   bitmapLayout,
215*cdf0e10cSrcweir                                                             const geometry::IntegerPoint2D&   pos )
216*cdf0e10cSrcweir     {
217*cdf0e10cSrcweir         RTL_LOGFILE_CONTEXT( aLog, "::dxcanvas::BitmapCanvasHelper::getPixel()" );
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir         ENSURE_OR_THROW( mpTarget,
220*cdf0e10cSrcweir                           "::dxcanvas::BitmapCanvasHelper::getPixel(): disposed" );
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir 		if( !mpTarget )
223*cdf0e10cSrcweir 			return uno::Sequence< sal_Int8 >();
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 		bitmapLayout = getMemoryLayout();
226*cdf0e10cSrcweir 		return mpTarget->getPixel(bitmapLayout,pos);
227*cdf0e10cSrcweir     }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir     uno::Reference< rendering::XBitmapPalette > BitmapCanvasHelper::getPalette()
230*cdf0e10cSrcweir     {
231*cdf0e10cSrcweir         // TODO(F1): Palette bitmaps NYI
232*cdf0e10cSrcweir         return uno::Reference< rendering::XBitmapPalette >();
233*cdf0e10cSrcweir     }
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir     rendering::IntegerBitmapLayout BitmapCanvasHelper::getMemoryLayout()
236*cdf0e10cSrcweir     {
237*cdf0e10cSrcweir         if( !mpTarget )
238*cdf0e10cSrcweir             return rendering::IntegerBitmapLayout(); // we're disposed
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir         return ::canvas::tools::getStdMemoryLayout(getSize());
241*cdf0e10cSrcweir     }
242*cdf0e10cSrcweir     bool BitmapCanvasHelper::hasAlpha() const
243*cdf0e10cSrcweir     {
244*cdf0e10cSrcweir 		return mpTarget ? mpTarget->hasAlpha() : false;
245*cdf0e10cSrcweir     }
246*cdf0e10cSrcweir }
247