1*25ea7f45SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*25ea7f45SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*25ea7f45SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*25ea7f45SAndrew Rist  * distributed with this work for additional information
6*25ea7f45SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*25ea7f45SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*25ea7f45SAndrew Rist  * "License"); you may not use this file except in compliance
9*25ea7f45SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*25ea7f45SAndrew Rist  *
11*25ea7f45SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*25ea7f45SAndrew Rist  *
13*25ea7f45SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*25ea7f45SAndrew Rist  * software distributed under the License is distributed on an
15*25ea7f45SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*25ea7f45SAndrew Rist  * KIND, either express or implied.  See the License for the
17*25ea7f45SAndrew Rist  * specific language governing permissions and limitations
18*25ea7f45SAndrew Rist  * under the License.
19*25ea7f45SAndrew Rist  *
20*25ea7f45SAndrew Rist  *************************************************************/
21*25ea7f45SAndrew Rist 
22*25ea7f45SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_canvas.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <canvas/debug.hxx>
28cdf0e10cSrcweir #include <tools/diagnose_ex.h>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <rtl/logfile.hxx>
31cdf0e10cSrcweir #include <rtl/math.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <com/sun/star/rendering/TexturingMode.hpp>
34cdf0e10cSrcweir #include <com/sun/star/rendering/CompositeOperation.hpp>
35cdf0e10cSrcweir #include <com/sun/star/rendering/RepaintResult.hpp>
36cdf0e10cSrcweir #include <com/sun/star/rendering/PathCapType.hpp>
37cdf0e10cSrcweir #include <com/sun/star/rendering/PathJoinType.hpp>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
40cdf0e10cSrcweir #include <basegfx/point/b2dpoint.hxx>
41cdf0e10cSrcweir #include <basegfx/tools/canvastools.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <comphelper/sequence.hxx>
44cdf0e10cSrcweir #include <canvas/canvastools.hxx>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include "dx_spritecanvas.hxx"
47cdf0e10cSrcweir #include "dx_impltools.hxx"
48cdf0e10cSrcweir #include "dx_canvasfont.hxx"
49cdf0e10cSrcweir #include "dx_textlayout.hxx"
50cdf0e10cSrcweir #include "dx_bitmapcanvashelper.hxx"
51cdf0e10cSrcweir 
52cdf0e10cSrcweir #include <algorithm>
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
55cdf0e10cSrcweir using namespace ::com::sun::star;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir namespace dxcanvas
58cdf0e10cSrcweir {
BitmapCanvasHelper()59cdf0e10cSrcweir     BitmapCanvasHelper::BitmapCanvasHelper() :
60cdf0e10cSrcweir         mpTarget()
61cdf0e10cSrcweir     {}
62cdf0e10cSrcweir 
disposing()63cdf0e10cSrcweir     void BitmapCanvasHelper::disposing()
64cdf0e10cSrcweir     {
65cdf0e10cSrcweir         mpTarget.reset();
66cdf0e10cSrcweir         CanvasHelper::disposing();
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir 
setTarget(const IBitmapSharedPtr & rTarget)69cdf0e10cSrcweir     void BitmapCanvasHelper::setTarget( const IBitmapSharedPtr& rTarget )
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         ENSURE_OR_THROW( rTarget,
72cdf0e10cSrcweir                           "BitmapCanvasHelper::setTarget(): Invalid target" );
73cdf0e10cSrcweir         ENSURE_OR_THROW( !mpTarget.get(),
74cdf0e10cSrcweir                           "BitmapCanvasHelper::setTarget(): target set, old target would be overwritten" );
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         mpTarget = rTarget;
77cdf0e10cSrcweir         CanvasHelper::setTarget(rTarget);
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
setTarget(const IBitmapSharedPtr & rTarget,const::basegfx::B2ISize & rOutputOffset)80cdf0e10cSrcweir     void BitmapCanvasHelper::setTarget( const IBitmapSharedPtr&   rTarget,
81cdf0e10cSrcweir                                         const ::basegfx::B2ISize& rOutputOffset )
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir         ENSURE_OR_THROW( rTarget,
84cdf0e10cSrcweir                          "BitmapCanvasHelper::setTarget(): invalid target" );
85cdf0e10cSrcweir         ENSURE_OR_THROW( !mpTarget.get(),
86cdf0e10cSrcweir                          "BitmapCanvasHelper::setTarget(): target set, old target would be overwritten" );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         mpTarget = rTarget;
89cdf0e10cSrcweir         CanvasHelper::setTarget(rTarget,rOutputOffset);
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
clear()92cdf0e10cSrcweir     void BitmapCanvasHelper::clear()
93cdf0e10cSrcweir     {
94cdf0e10cSrcweir         if( needOutput() )
95cdf0e10cSrcweir         {
96cdf0e10cSrcweir             GraphicsSharedPtr pGraphics( mpTarget->getGraphics() );
97cdf0e10cSrcweir 
98cdf0e10cSrcweir             Gdiplus::Color aClearColor = hasAlpha() ?
99cdf0e10cSrcweir                 Gdiplus::Color( 0,255,255,255 ) : Gdiplus::Color((Gdiplus::ARGB)Gdiplus::Color::White);
100cdf0e10cSrcweir 
101cdf0e10cSrcweir             ENSURE_OR_THROW(
102cdf0e10cSrcweir                 Gdiplus::Ok == pGraphics->SetCompositingMode(
103cdf0e10cSrcweir                     Gdiplus::CompositingModeSourceCopy ), // force set, don't blend
104cdf0e10cSrcweir                 "BitmapCanvasHelper::clear(): GDI+ SetCompositingMode call failed" );
105cdf0e10cSrcweir             ENSURE_OR_THROW(
106cdf0e10cSrcweir                 Gdiplus::Ok == pGraphics->Clear( aClearColor ),
107cdf0e10cSrcweir                 "BitmapCanvasHelper::clear(): GDI+ Clear call failed" );
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir 
drawTextLayout(const rendering::XCanvas *,const uno::Reference<rendering::XTextLayout> & xLayoutetText,const rendering::ViewState & viewState,const rendering::RenderState & renderState)111cdf0e10cSrcweir     uno::Reference< rendering::XCachedPrimitive > BitmapCanvasHelper::drawTextLayout( const rendering::XCanvas* 					  /*pCanvas*/,
112cdf0e10cSrcweir                                                                                       const uno::Reference< rendering::XTextLayout >& xLayoutetText,
113cdf0e10cSrcweir                                                                                       const rendering::ViewState& 					  viewState,
114cdf0e10cSrcweir                                                                                       const rendering::RenderState& 				  renderState )
115cdf0e10cSrcweir     {
116cdf0e10cSrcweir         ENSURE_OR_THROW( xLayoutetText.is(),
117cdf0e10cSrcweir                           "BitmapCanvasHelper::drawTextLayout: layout is NULL");
118cdf0e10cSrcweir 
119cdf0e10cSrcweir         if( needOutput() )
120cdf0e10cSrcweir         {
121cdf0e10cSrcweir 			TextLayout* pTextLayout =
122cdf0e10cSrcweir                 dynamic_cast< TextLayout* >( xLayoutetText.get() );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir             ENSURE_OR_THROW( pTextLayout,
125cdf0e10cSrcweir                                 "BitmapCanvasHelper::drawTextLayout(): TextLayout not compatible with this canvas" );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 			pTextLayout->draw( mpTarget->getGraphics(),
128cdf0e10cSrcweir                                viewState,
129cdf0e10cSrcweir                                renderState,
130cdf0e10cSrcweir                                maOutputOffset,
131cdf0e10cSrcweir                                mpDevice,
132cdf0e10cSrcweir                                mpTarget->hasAlpha() );
133cdf0e10cSrcweir         }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         return uno::Reference< rendering::XCachedPrimitive >(NULL);
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
copyRect(const rendering::XCanvas *,const uno::Reference<rendering::XBitmapCanvas> &,const geometry::RealRectangle2D &,const rendering::ViewState &,const rendering::RenderState &,const geometry::RealRectangle2D &,const rendering::ViewState &,const rendering::RenderState &)138cdf0e10cSrcweir     void BitmapCanvasHelper::copyRect( const rendering::XCanvas* 							/*pCanvas*/,
139cdf0e10cSrcweir                                        const uno::Reference< rendering::XBitmapCanvas >&	/*sourceCanvas*/,
140cdf0e10cSrcweir                                        const geometry::RealRectangle2D& 					/*sourceRect*/,
141cdf0e10cSrcweir                                        const rendering::ViewState& 							/*sourceViewState*/,
142cdf0e10cSrcweir                                        const rendering::RenderState& 						/*sourceRenderState*/,
143cdf0e10cSrcweir                                        const geometry::RealRectangle2D& 					/*destRect*/,
144cdf0e10cSrcweir                                        const rendering::ViewState& 							/*destViewState*/,
145cdf0e10cSrcweir                                        const rendering::RenderState& 						/*destRenderState*/ )
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         // TODO(F2): copyRect NYI
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir 
getSize()150cdf0e10cSrcweir     geometry::IntegerSize2D BitmapCanvasHelper::getSize()
151cdf0e10cSrcweir     {
152cdf0e10cSrcweir         if( !mpTarget )
153cdf0e10cSrcweir             return geometry::IntegerSize2D(1, 1);
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         return basegfx::unotools::integerSize2DFromB2ISize(mpTarget->getSize());
156cdf0e10cSrcweir     }
157cdf0e10cSrcweir 
getScaledBitmap(const geometry::RealSize2D &,sal_Bool)158cdf0e10cSrcweir     uno::Reference< rendering::XBitmap > BitmapCanvasHelper::getScaledBitmap( const geometry::RealSize2D& /*newSize*/,
159cdf0e10cSrcweir                                                                               sal_Bool 					  /*beFast*/ )
160cdf0e10cSrcweir     {
161cdf0e10cSrcweir         // TODO(F1):
162cdf0e10cSrcweir         return uno::Reference< rendering::XBitmap >();
163cdf0e10cSrcweir     }
164cdf0e10cSrcweir 
getData(rendering::IntegerBitmapLayout & bitmapLayout,const geometry::IntegerRectangle2D & rect)165cdf0e10cSrcweir     uno::Sequence< sal_Int8 > BitmapCanvasHelper::getData( rendering::IntegerBitmapLayout&     bitmapLayout,
166cdf0e10cSrcweir                                                            const geometry::IntegerRectangle2D& rect )
167cdf0e10cSrcweir     {
168cdf0e10cSrcweir         RTL_LOGFILE_CONTEXT( aLog, "::dxcanvas::BitmapCanvasHelper::getData()" );
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         ENSURE_OR_THROW( mpTarget,
171cdf0e10cSrcweir                           "::dxcanvas::BitmapCanvasHelper::getData(): disposed" );
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 		if( !mpTarget )
174cdf0e10cSrcweir 	        return uno::Sequence< sal_Int8 >();
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 		bitmapLayout = getMemoryLayout();
177cdf0e10cSrcweir 		return mpTarget->getData(bitmapLayout,rect);
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
setData(const uno::Sequence<sal_Int8> & data,const rendering::IntegerBitmapLayout & bitmapLayout,const geometry::IntegerRectangle2D & rect)180cdf0e10cSrcweir     void BitmapCanvasHelper::setData( const uno::Sequence< sal_Int8 >& 		data,
181cdf0e10cSrcweir                                       const rendering::IntegerBitmapLayout& bitmapLayout,
182cdf0e10cSrcweir                                       const geometry::IntegerRectangle2D& 	rect )
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         RTL_LOGFILE_CONTEXT( aLog, "::dxcanvas::BitmapCanvasHelper::setData()" );
185cdf0e10cSrcweir 
186cdf0e10cSrcweir         ENSURE_OR_THROW( mpTarget,
187cdf0e10cSrcweir                           "::dxcanvas::BitmapCanvasHelper::setData(): disposed" );
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 		if( !mpTarget )
190cdf0e10cSrcweir 			return;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 		mpTarget->setData(data,bitmapLayout,rect);
193cdf0e10cSrcweir     }
194cdf0e10cSrcweir 
setPixel(const uno::Sequence<sal_Int8> & color,const rendering::IntegerBitmapLayout & bitmapLayout,const geometry::IntegerPoint2D & pos)195cdf0e10cSrcweir     void BitmapCanvasHelper::setPixel( const uno::Sequence< sal_Int8 >&       color,
196cdf0e10cSrcweir                                        const rendering::IntegerBitmapLayout&  bitmapLayout,
197cdf0e10cSrcweir                                        const geometry::IntegerPoint2D&        pos )
198cdf0e10cSrcweir     {
199cdf0e10cSrcweir         RTL_LOGFILE_CONTEXT( aLog, "::dxcanvas::BitmapCanvasHelper::setPixel()" );
200cdf0e10cSrcweir 
201cdf0e10cSrcweir         ENSURE_OR_THROW( mpTarget,
202cdf0e10cSrcweir                           "::dxcanvas::BitmapCanvasHelper::setPixel(): disposed" );
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 		if( !mpTarget )
205cdf0e10cSrcweir 			return;
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 		mpTarget->setPixel(color,bitmapLayout,pos);
208cdf0e10cSrcweir     }
209cdf0e10cSrcweir 
getPixel(rendering::IntegerBitmapLayout & bitmapLayout,const geometry::IntegerPoint2D & pos)210cdf0e10cSrcweir     uno::Sequence< sal_Int8 > BitmapCanvasHelper::getPixel( rendering::IntegerBitmapLayout&   bitmapLayout,
211cdf0e10cSrcweir                                                             const geometry::IntegerPoint2D&   pos )
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir         RTL_LOGFILE_CONTEXT( aLog, "::dxcanvas::BitmapCanvasHelper::getPixel()" );
214cdf0e10cSrcweir 
215cdf0e10cSrcweir         ENSURE_OR_THROW( mpTarget,
216cdf0e10cSrcweir                           "::dxcanvas::BitmapCanvasHelper::getPixel(): disposed" );
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 		if( !mpTarget )
219cdf0e10cSrcweir 			return uno::Sequence< sal_Int8 >();
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 		bitmapLayout = getMemoryLayout();
222cdf0e10cSrcweir 		return mpTarget->getPixel(bitmapLayout,pos);
223cdf0e10cSrcweir     }
224cdf0e10cSrcweir 
getPalette()225cdf0e10cSrcweir     uno::Reference< rendering::XBitmapPalette > BitmapCanvasHelper::getPalette()
226cdf0e10cSrcweir     {
227cdf0e10cSrcweir         // TODO(F1): Palette bitmaps NYI
228cdf0e10cSrcweir         return uno::Reference< rendering::XBitmapPalette >();
229cdf0e10cSrcweir     }
230cdf0e10cSrcweir 
getMemoryLayout()231cdf0e10cSrcweir     rendering::IntegerBitmapLayout BitmapCanvasHelper::getMemoryLayout()
232cdf0e10cSrcweir     {
233cdf0e10cSrcweir         if( !mpTarget )
234cdf0e10cSrcweir             return rendering::IntegerBitmapLayout(); // we're disposed
235cdf0e10cSrcweir 
236cdf0e10cSrcweir         return ::canvas::tools::getStdMemoryLayout(getSize());
237cdf0e10cSrcweir     }
hasAlpha() const238cdf0e10cSrcweir     bool BitmapCanvasHelper::hasAlpha() const
239cdf0e10cSrcweir     {
240cdf0e10cSrcweir 		return mpTarget ? mpTarget->hasAlpha() : false;
241cdf0e10cSrcweir     }
242cdf0e10cSrcweir }
243