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 #include <rtl/instance.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <com/sun/star/util/Endianness.hpp>
35cdf0e10cSrcweir #include <com/sun/star/rendering/TexturingMode.hpp>
36cdf0e10cSrcweir #include <com/sun/star/rendering/CompositeOperation.hpp>
37cdf0e10cSrcweir #include <com/sun/star/rendering/RepaintResult.hpp>
38cdf0e10cSrcweir #include <com/sun/star/rendering/PathCapType.hpp>
39cdf0e10cSrcweir #include <com/sun/star/rendering/PathJoinType.hpp>
40cdf0e10cSrcweir #include <com/sun/star/rendering/XIntegerBitmapColorSpace.hpp>
41cdf0e10cSrcweir #include <com/sun/star/rendering/IntegerBitmapLayout.hpp>
42cdf0e10cSrcweir #include <com/sun/star/rendering/ColorSpaceType.hpp>
43cdf0e10cSrcweir #include <com/sun/star/rendering/ColorComponentTag.hpp>
44cdf0e10cSrcweir #include <com/sun/star/rendering/RenderingIntent.hpp>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
47cdf0e10cSrcweir #include <basegfx/point/b2dpoint.hxx>
48cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
49cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygon.hxx>
50cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygontools.hxx>
51cdf0e10cSrcweir #include <basegfx/tools/canvastools.hxx>
52cdf0e10cSrcweir #include <basegfx/tools/keystoplerp.hxx>
53cdf0e10cSrcweir #include <basegfx/tools/lerp.hxx>
54cdf0e10cSrcweir 
55cdf0e10cSrcweir #include <comphelper/sequence.hxx>
56cdf0e10cSrcweir #include <cppuhelper/compbase1.hxx>
57cdf0e10cSrcweir 
58cdf0e10cSrcweir #include <canvas/canvastools.hxx>
59cdf0e10cSrcweir #include <canvas/parametricpolypolygon.hxx>
60cdf0e10cSrcweir 
61cdf0e10cSrcweir #include <vcl/canvastools.hxx>
62cdf0e10cSrcweir #include <vcl/bitmapex.hxx>
63cdf0e10cSrcweir #include <vcl/bmpacc.hxx>
64cdf0e10cSrcweir #include <vcl/virdev.hxx>
65cdf0e10cSrcweir 
66cdf0e10cSrcweir #include "cairo_spritecanvas.hxx"
67cdf0e10cSrcweir #include "cairo_cachedbitmap.hxx"
68cdf0e10cSrcweir #include "cairo_canvashelper.hxx"
69cdf0e10cSrcweir #include "cairo_canvasbitmap.hxx"
70cdf0e10cSrcweir 
71cdf0e10cSrcweir #include <boost/tuple/tuple.hpp>
72cdf0e10cSrcweir #include <algorithm>
73cdf0e10cSrcweir 
74cdf0e10cSrcweir using namespace ::cairo;
75cdf0e10cSrcweir using namespace ::com::sun::star;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir namespace cairocanvas
78cdf0e10cSrcweir {
CanvasHelper()79cdf0e10cSrcweir     CanvasHelper::CanvasHelper() :
80cdf0e10cSrcweir         mpSurfaceProvider(NULL),
81cdf0e10cSrcweir         mpDevice(NULL),
82cdf0e10cSrcweir 		mpVirtualDevice(),
83cdf0e10cSrcweir         mbHaveAlpha(),
84cdf0e10cSrcweir 		mpCairo(),
85cdf0e10cSrcweir 		mpSurface(),
86cdf0e10cSrcweir         maSize()
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir     }
89cdf0e10cSrcweir 
disposing()90cdf0e10cSrcweir     void CanvasHelper::disposing()
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir 		mpSurface.reset();
93cdf0e10cSrcweir 		mpCairo.reset();
94cdf0e10cSrcweir         mpVirtualDevice.reset();
95cdf0e10cSrcweir         mpDevice = NULL;
96cdf0e10cSrcweir         mpSurfaceProvider = NULL;
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir 
init(const::basegfx::B2ISize & rSizePixel,SurfaceProvider & rSurfaceProvider,rendering::XGraphicDevice * pDevice)99cdf0e10cSrcweir     void CanvasHelper::init( const ::basegfx::B2ISize&  rSizePixel,
100cdf0e10cSrcweir                              SurfaceProvider&           rSurfaceProvider,
101cdf0e10cSrcweir                              rendering::XGraphicDevice* pDevice )
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         maSize = rSizePixel;
104cdf0e10cSrcweir         mpSurfaceProvider = &rSurfaceProvider;
105cdf0e10cSrcweir         mpDevice = pDevice;
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir 
setSize(const::basegfx::B2ISize & rSize)108cdf0e10cSrcweir     void CanvasHelper::setSize( const ::basegfx::B2ISize& rSize )
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir         maSize = rSize;
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir 
setSurface(const SurfaceSharedPtr & pSurface,bool bHasAlpha)113cdf0e10cSrcweir     void CanvasHelper::setSurface( const SurfaceSharedPtr& pSurface, bool bHasAlpha )
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         mbHaveAlpha = bHasAlpha;
116cdf0e10cSrcweir         mpVirtualDevice.reset();
117cdf0e10cSrcweir 		mpSurface = pSurface;
118cdf0e10cSrcweir 		mpCairo = pSurface->getCairo();
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
setColor(Cairo * pCairo,const uno::Sequence<double> & rColor)121cdf0e10cSrcweir     static void setColor( Cairo* pCairo,
122cdf0e10cSrcweir                           const uno::Sequence<double>& rColor )
123cdf0e10cSrcweir     {
124cdf0e10cSrcweir         if( rColor.getLength() > 3 )
125cdf0e10cSrcweir         {
126cdf0e10cSrcweir             const double alpha = rColor[3];
127cdf0e10cSrcweir 
128cdf0e10cSrcweir             cairo_set_source_rgba( pCairo,
129cdf0e10cSrcweir                                    alpha*rColor[0],
130cdf0e10cSrcweir                                    alpha*rColor[1],
131cdf0e10cSrcweir                                    alpha*rColor[2],
132cdf0e10cSrcweir                                    alpha );
133cdf0e10cSrcweir         }
134cdf0e10cSrcweir         else if( rColor.getLength() == 3 )
135cdf0e10cSrcweir             cairo_set_source_rgb( pCairo,
136cdf0e10cSrcweir                                   rColor[0],
137cdf0e10cSrcweir                                   rColor[1],
138cdf0e10cSrcweir                                   rColor[2] );
139cdf0e10cSrcweir     }
140cdf0e10cSrcweir 
useStates(const rendering::ViewState & viewState,const rendering::RenderState & renderState,bool bSetColor)141cdf0e10cSrcweir     void CanvasHelper::useStates( const rendering::ViewState& viewState,
142cdf0e10cSrcweir                                   const rendering::RenderState& renderState,
143cdf0e10cSrcweir                                   bool bSetColor )
144cdf0e10cSrcweir     {
145cdf0e10cSrcweir         Matrix aViewMatrix;
146cdf0e10cSrcweir         Matrix aRenderMatrix;
147cdf0e10cSrcweir         Matrix aCombinedMatrix;
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         cairo_matrix_init( &aViewMatrix,
150cdf0e10cSrcweir                            viewState.AffineTransform.m00, viewState.AffineTransform.m10, viewState.AffineTransform.m01,
151cdf0e10cSrcweir                            viewState.AffineTransform.m11, viewState.AffineTransform.m02, viewState.AffineTransform.m12);
152cdf0e10cSrcweir         cairo_matrix_init( &aRenderMatrix,
153cdf0e10cSrcweir                            renderState.AffineTransform.m00, renderState.AffineTransform.m10, renderState.AffineTransform.m01,
154cdf0e10cSrcweir                            renderState.AffineTransform.m11, renderState.AffineTransform.m02, renderState.AffineTransform.m12);
155cdf0e10cSrcweir         cairo_matrix_multiply( &aCombinedMatrix, &aRenderMatrix, &aViewMatrix);
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         if( viewState.Clip.is() ) {
158cdf0e10cSrcweir             OSL_TRACE ("view clip");
159cdf0e10cSrcweir 
160cdf0e10cSrcweir             aViewMatrix.x0 = basegfx::fround( aViewMatrix.x0 );
161cdf0e10cSrcweir             aViewMatrix.y0 = basegfx::fround( aViewMatrix.y0 );
162cdf0e10cSrcweir             cairo_set_matrix( mpCairo.get(), &aViewMatrix );
163cdf0e10cSrcweir             doPolyPolygonPath( viewState.Clip, Clip );
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir         aCombinedMatrix.x0 = basegfx::fround( aCombinedMatrix.x0 );
167cdf0e10cSrcweir         aCombinedMatrix.y0 = basegfx::fround( aCombinedMatrix.y0 );
168cdf0e10cSrcweir         cairo_set_matrix( mpCairo.get(), &aCombinedMatrix );
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         if( renderState.Clip.is() ) {
171cdf0e10cSrcweir             OSL_TRACE ("render clip BEGIN");
172cdf0e10cSrcweir 
173cdf0e10cSrcweir             doPolyPolygonPath( renderState.Clip, Clip );
174cdf0e10cSrcweir             OSL_TRACE ("render clip END");
175cdf0e10cSrcweir         }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir         if( bSetColor )
178cdf0e10cSrcweir             setColor(mpCairo.get(),renderState.DeviceColor);
179cdf0e10cSrcweir 
180cdf0e10cSrcweir         cairo_operator_t compositingMode( CAIRO_OPERATOR_OVER );
181cdf0e10cSrcweir         switch( renderState.CompositeOperation )
182cdf0e10cSrcweir         {
183cdf0e10cSrcweir             case rendering::CompositeOperation::CLEAR:
184cdf0e10cSrcweir                 compositingMode = CAIRO_OPERATOR_CLEAR;
185cdf0e10cSrcweir                 break;
186cdf0e10cSrcweir             case rendering::CompositeOperation::SOURCE:
187cdf0e10cSrcweir                 compositingMode = CAIRO_OPERATOR_SOURCE;
188cdf0e10cSrcweir                 break;
189cdf0e10cSrcweir             case rendering::CompositeOperation::DESTINATION:
190cdf0e10cSrcweir                 compositingMode = CAIRO_OPERATOR_DEST;
191cdf0e10cSrcweir                 break;
192cdf0e10cSrcweir             case rendering::CompositeOperation::OVER:
193cdf0e10cSrcweir                 compositingMode = CAIRO_OPERATOR_OVER;
194cdf0e10cSrcweir                 break;
195cdf0e10cSrcweir             case rendering::CompositeOperation::UNDER:
196cdf0e10cSrcweir                 compositingMode = CAIRO_OPERATOR_DEST;
197cdf0e10cSrcweir                 break;
198cdf0e10cSrcweir             case rendering::CompositeOperation::INSIDE:
199cdf0e10cSrcweir                 compositingMode = CAIRO_OPERATOR_IN;
200cdf0e10cSrcweir                 break;
201cdf0e10cSrcweir             case rendering::CompositeOperation::INSIDE_REVERSE:
202cdf0e10cSrcweir                 compositingMode = CAIRO_OPERATOR_OUT;
203cdf0e10cSrcweir                 break;
204cdf0e10cSrcweir             case rendering::CompositeOperation::OUTSIDE:
205cdf0e10cSrcweir                 compositingMode = CAIRO_OPERATOR_DEST_OVER;
206cdf0e10cSrcweir                 break;
207cdf0e10cSrcweir             case rendering::CompositeOperation::OUTSIDE_REVERSE:
208cdf0e10cSrcweir                 compositingMode = CAIRO_OPERATOR_DEST_OUT;
209cdf0e10cSrcweir                 break;
210cdf0e10cSrcweir             case rendering::CompositeOperation::ATOP:
211cdf0e10cSrcweir                 compositingMode = CAIRO_OPERATOR_ATOP;
212cdf0e10cSrcweir                 break;
213cdf0e10cSrcweir             case rendering::CompositeOperation::ATOP_REVERSE:
214cdf0e10cSrcweir                 compositingMode = CAIRO_OPERATOR_DEST_ATOP;
215cdf0e10cSrcweir                 break;
216cdf0e10cSrcweir             case rendering::CompositeOperation::XOR:
217cdf0e10cSrcweir                 compositingMode = CAIRO_OPERATOR_XOR;
218cdf0e10cSrcweir                 break;
219cdf0e10cSrcweir             case rendering::CompositeOperation::ADD:
220cdf0e10cSrcweir                 compositingMode = CAIRO_OPERATOR_ADD;
221cdf0e10cSrcweir                 break;
222cdf0e10cSrcweir             case rendering::CompositeOperation::SATURATE:
223cdf0e10cSrcweir                 compositingMode = CAIRO_OPERATOR_SATURATE;
224cdf0e10cSrcweir                 break;
225cdf0e10cSrcweir         }
226cdf0e10cSrcweir         cairo_set_operator( mpCairo.get(), compositingMode );
227cdf0e10cSrcweir     }
228cdf0e10cSrcweir 
clear()229cdf0e10cSrcweir     void CanvasHelper::clear()
230cdf0e10cSrcweir     {
231cdf0e10cSrcweir         OSL_TRACE ("clear whole area: %d x %d", maSize.getX(), maSize.getY() );
232cdf0e10cSrcweir 
233cdf0e10cSrcweir         if( mpCairo )
234cdf0e10cSrcweir         {
235cdf0e10cSrcweir             cairo_save( mpCairo.get() );
236cdf0e10cSrcweir 
237cdf0e10cSrcweir             cairo_identity_matrix( mpCairo.get() );
238cdf0e10cSrcweir             // this does not really differ from all-zero, as cairo
239cdf0e10cSrcweir             // internally converts to premultiplied alpha. but anyway,
240cdf0e10cSrcweir             // this keeps it consistent with the other canvas impls
241cdf0e10cSrcweir             if( mbHaveAlpha )
242cdf0e10cSrcweir                 cairo_set_source_rgba( mpCairo.get(), 1.0, 1.0, 1.0, 0.0 );
243cdf0e10cSrcweir             else
244cdf0e10cSrcweir                 cairo_set_source_rgb( mpCairo.get(), 1.0, 1.0, 1.0 );
245cdf0e10cSrcweir             cairo_set_operator( mpCairo.get(), CAIRO_OPERATOR_SOURCE );
246cdf0e10cSrcweir 
247cdf0e10cSrcweir 			cairo_rectangle( mpCairo.get(), 0, 0, maSize.getX(), maSize.getY() );
248cdf0e10cSrcweir             cairo_fill( mpCairo.get() );
249cdf0e10cSrcweir 
250cdf0e10cSrcweir             cairo_restore( mpCairo.get() );
251cdf0e10cSrcweir         }
252cdf0e10cSrcweir     }
253cdf0e10cSrcweir 
drawPoint(const rendering::XCanvas *,const geometry::RealPoint2D &,const rendering::ViewState &,const rendering::RenderState &)254cdf0e10cSrcweir     void CanvasHelper::drawPoint( const rendering::XCanvas* 	,
255cdf0e10cSrcweir                                   const geometry::RealPoint2D& 	,
256cdf0e10cSrcweir                                   const rendering::ViewState& 	,
257cdf0e10cSrcweir                                   const rendering::RenderState&	 )
258cdf0e10cSrcweir     {
259cdf0e10cSrcweir     }
260cdf0e10cSrcweir 
drawLine(const rendering::XCanvas *,const geometry::RealPoint2D & aStartPoint,const geometry::RealPoint2D & aEndPoint,const rendering::ViewState & viewState,const rendering::RenderState & renderState)261cdf0e10cSrcweir     void CanvasHelper::drawLine( const rendering::XCanvas* 		/*pCanvas*/,
262cdf0e10cSrcweir                                  const geometry::RealPoint2D& 	aStartPoint,
263cdf0e10cSrcweir                                  const geometry::RealPoint2D& 	aEndPoint,
264cdf0e10cSrcweir                                  const rendering::ViewState& 	viewState,
265cdf0e10cSrcweir                                  const rendering::RenderState& 	renderState )
266cdf0e10cSrcweir     {
267cdf0e10cSrcweir 	if( mpCairo ) {
268cdf0e10cSrcweir 	    cairo_save( mpCairo.get() );
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 	    cairo_set_line_width( mpCairo.get(), 1 );
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 	    useStates( viewState, renderState, true );
273cdf0e10cSrcweir 
274cdf0e10cSrcweir 	    cairo_move_to( mpCairo.get(), aStartPoint.X + 0.5, aStartPoint.Y + 0.5 );
275cdf0e10cSrcweir 	    cairo_line_to( mpCairo.get(), aEndPoint.X + 0.5, aEndPoint.Y + 0.5 );
276cdf0e10cSrcweir 	    cairo_stroke( mpCairo.get() );
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 	    cairo_restore( mpCairo.get() );
279cdf0e10cSrcweir 	}
280cdf0e10cSrcweir     }
281cdf0e10cSrcweir 
drawBezier(const rendering::XCanvas *,const geometry::RealBezierSegment2D & aBezierSegment,const geometry::RealPoint2D & aEndPoint,const rendering::ViewState & viewState,const rendering::RenderState & renderState)282cdf0e10cSrcweir     void CanvasHelper::drawBezier( const rendering::XCanvas* 			,
283cdf0e10cSrcweir                                    const geometry::RealBezierSegment2D&	aBezierSegment,
284cdf0e10cSrcweir                                    const geometry::RealPoint2D& 		aEndPoint,
285cdf0e10cSrcweir                                    const rendering::ViewState& 			viewState,
286cdf0e10cSrcweir                                    const rendering::RenderState& 		renderState )
287cdf0e10cSrcweir     {
288cdf0e10cSrcweir 	if( mpCairo ) {
289cdf0e10cSrcweir 	    cairo_save( mpCairo.get() );
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 	    cairo_set_line_width( mpCairo.get(), 1 );
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 	    useStates( viewState, renderState, true );
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 	    cairo_move_to( mpCairo.get(), aBezierSegment.Px + 0.5, aBezierSegment.Py + 0.5 );
296cdf0e10cSrcweir         cairo_curve_to( mpCairo.get(),
297cdf0e10cSrcweir                         aBezierSegment.C1x + 0.5, aBezierSegment.C1y + 0.5,
298cdf0e10cSrcweir                         aBezierSegment.C2x + 0.5, aBezierSegment.C2y + 0.5,
299cdf0e10cSrcweir                         aEndPoint.X + 0.5, aEndPoint.Y + 0.5 );
300cdf0e10cSrcweir 	    cairo_stroke( mpCairo.get() );
301cdf0e10cSrcweir 
302cdf0e10cSrcweir 	    cairo_restore( mpCairo.get() );
303cdf0e10cSrcweir 	}
304cdf0e10cSrcweir     }
305cdf0e10cSrcweir 
306cdf0e10cSrcweir #define CANVASBITMAP_IMPLEMENTATION_NAME "CairoCanvas::CanvasBitmap"
307cdf0e10cSrcweir #define PARAMETRICPOLYPOLYGON_IMPLEMENTATION_NAME "Canvas::ParametricPolyPolygon"
308cdf0e10cSrcweir 
309cdf0e10cSrcweir 
310cdf0e10cSrcweir     /** surfaceFromXBitmap Create a surface from XBitmap
311cdf0e10cSrcweir      * @param xBitmap bitmap image that will be used for the surface
312cdf0e10cSrcweir      * @param bHasAlpha will be set to true if resulting surface has alpha
313cdf0e10cSrcweir      *
314cdf0e10cSrcweir      * This is a helper function for the other surfaceFromXBitmap().
315cdf0e10cSrcweir      * This function tries to create surface from xBitmap by checking if xBitmap is CanvasBitmap or SpriteCanvas.
316cdf0e10cSrcweir      *
317cdf0e10cSrcweir      * @return created surface or NULL
318cdf0e10cSrcweir      **/
surfaceFromXBitmap(const uno::Reference<rendering::XBitmap> & xBitmap)319cdf0e10cSrcweir     static SurfaceSharedPtr surfaceFromXBitmap( const uno::Reference< rendering::XBitmap >& xBitmap )
320cdf0e10cSrcweir     {
321cdf0e10cSrcweir 		CanvasBitmap* pBitmapImpl = dynamic_cast< CanvasBitmap* >( xBitmap.get() );
322cdf0e10cSrcweir 		if( pBitmapImpl )
323cdf0e10cSrcweir 			return pBitmapImpl->getSurface();
324cdf0e10cSrcweir 
325cdf0e10cSrcweir         SurfaceProvider* pSurfaceProvider = dynamic_cast<SurfaceProvider*>( xBitmap.get() );
326cdf0e10cSrcweir         if( pSurfaceProvider )
327cdf0e10cSrcweir             return pSurfaceProvider->getSurface();
328cdf0e10cSrcweir 
329cdf0e10cSrcweir 		return SurfaceSharedPtr();
330cdf0e10cSrcweir     }
331cdf0e10cSrcweir 
bitmapExFromXBitmap(const uno::Reference<rendering::XBitmap> & xBitmap)332cdf0e10cSrcweir     static ::BitmapEx bitmapExFromXBitmap( const uno::Reference< rendering::XBitmap >& xBitmap )
333cdf0e10cSrcweir     {
334cdf0e10cSrcweir         // TODO(F1): Add support for floating point bitmap formats
335cdf0e10cSrcweir         uno::Reference<rendering::XIntegerReadOnlyBitmap> xIntBmp(xBitmap,
336cdf0e10cSrcweir                                                                   uno::UNO_QUERY_THROW);
337cdf0e10cSrcweir         ::BitmapEx aBmpEx = ::vcl::unotools::bitmapExFromXBitmap(xIntBmp);
338cdf0e10cSrcweir         if( !!aBmpEx )
339cdf0e10cSrcweir             return aBmpEx;
340cdf0e10cSrcweir 
341cdf0e10cSrcweir         // TODO(F1): extract pixel from XBitmap interface
342cdf0e10cSrcweir         ENSURE_OR_THROW( false,
343cdf0e10cSrcweir                          "bitmapExFromXBitmap(): could not extract BitmapEx" );
344cdf0e10cSrcweir 
345cdf0e10cSrcweir         return ::BitmapEx();
346cdf0e10cSrcweir     }
347cdf0e10cSrcweir 
readAlpha(BitmapReadAccess * pAlphaReadAcc,long nY,const long nWidth,unsigned char * data,long nOff)348cdf0e10cSrcweir     static bool readAlpha( BitmapReadAccess* pAlphaReadAcc, long nY, const long nWidth, unsigned char* data, long nOff )
349cdf0e10cSrcweir     {
350cdf0e10cSrcweir 	bool bIsAlpha = false;
351cdf0e10cSrcweir 	long nX;
352cdf0e10cSrcweir 	int nAlpha;
353cdf0e10cSrcweir 	Scanline pReadScan;
354cdf0e10cSrcweir 
355cdf0e10cSrcweir 	nOff += 3;
356cdf0e10cSrcweir 
357cdf0e10cSrcweir 	switch( pAlphaReadAcc->GetScanlineFormat() ) {
358cdf0e10cSrcweir 	case BMP_FORMAT_8BIT_TC_MASK:
359cdf0e10cSrcweir 	    pReadScan = pAlphaReadAcc->GetScanline( nY );
360cdf0e10cSrcweir 	    for( nX = 0; nX < nWidth; nX++ ) {
361cdf0e10cSrcweir 		nAlpha = data[ nOff ] = 255 - ( *pReadScan++ );
362cdf0e10cSrcweir 		if( nAlpha != 255 )
363cdf0e10cSrcweir 		    bIsAlpha = true;
364cdf0e10cSrcweir 		nOff += 4;
365cdf0e10cSrcweir 	    }
366cdf0e10cSrcweir 	    break;
367cdf0e10cSrcweir 	case BMP_FORMAT_8BIT_PAL:
368cdf0e10cSrcweir 	    pReadScan = pAlphaReadAcc->GetScanline( nY );
369cdf0e10cSrcweir 	    for( nX = 0; nX < nWidth; nX++ ) {
370cdf0e10cSrcweir 		nAlpha = data[ nOff ] = 255 - ( pAlphaReadAcc->GetPaletteColor( *pReadScan++ ).GetBlue() );
371cdf0e10cSrcweir 		if( nAlpha != 255 )
372cdf0e10cSrcweir 		    bIsAlpha = true;
373cdf0e10cSrcweir 		nOff += 4;
374cdf0e10cSrcweir 	    }
375cdf0e10cSrcweir 	    break;
376cdf0e10cSrcweir 	default:
377cdf0e10cSrcweir 	    OSL_TRACE( "fallback to GetColor for alpha - slow, format: %d", pAlphaReadAcc->GetScanlineFormat() );
378cdf0e10cSrcweir 	    for( nX = 0; nX < nWidth; nX++ ) {
379cdf0e10cSrcweir 		nAlpha = data[ nOff ] = 255 - pAlphaReadAcc->GetColor( nY, nX ).GetBlue();
380cdf0e10cSrcweir 		if( nAlpha != 255 )
381cdf0e10cSrcweir 		    bIsAlpha = true;
382cdf0e10cSrcweir 		nOff += 4;
383cdf0e10cSrcweir 	    }
384cdf0e10cSrcweir 	}
385cdf0e10cSrcweir 
386cdf0e10cSrcweir 	return bIsAlpha;
387cdf0e10cSrcweir     }
388cdf0e10cSrcweir 
389cdf0e10cSrcweir 
390cdf0e10cSrcweir     /** surfaceFromXBitmap Create a surface from XBitmap
391cdf0e10cSrcweir      * @param xBitmap bitmap image that will be used for the surface
392cdf0e10cSrcweir      * @param rDevice reference to the device into which we want to draw
393cdf0e10cSrcweir      * @param data will be filled with alpha data, if xBitmap is alpha/transparent image
394cdf0e10cSrcweir      * @param bHasAlpha will be set to true if resulting surface has alpha
395cdf0e10cSrcweir      *
396cdf0e10cSrcweir      * This function tries various methods for creating a surface from xBitmap. It also uses
397cdf0e10cSrcweir      * the helper function surfaceFromXBitmap( xBitmap, bHasAlpha )
398cdf0e10cSrcweir      *
399cdf0e10cSrcweir      * @return created surface or NULL
400cdf0e10cSrcweir      **/
surfaceFromXBitmap(const uno::Reference<rendering::XBitmap> & xBitmap,const SurfaceProviderRef & rSurfaceProvider,unsigned char * & data,bool & bHasAlpha)401cdf0e10cSrcweir     static SurfaceSharedPtr surfaceFromXBitmap( const uno::Reference< rendering::XBitmap >& xBitmap, const SurfaceProviderRef& rSurfaceProvider, unsigned char*& data, bool& bHasAlpha )
402cdf0e10cSrcweir     {
403cdf0e10cSrcweir         bHasAlpha = xBitmap->hasAlpha();
404cdf0e10cSrcweir 		SurfaceSharedPtr pSurface = surfaceFromXBitmap( xBitmap );
405cdf0e10cSrcweir 		if( pSurface )
406cdf0e10cSrcweir 			data = NULL;
407cdf0e10cSrcweir 		else
408cdf0e10cSrcweir         {
409cdf0e10cSrcweir 			::BitmapEx aBmpEx = bitmapExFromXBitmap(xBitmap);
410cdf0e10cSrcweir 			::Bitmap aBitmap = aBmpEx.GetBitmap();
411cdf0e10cSrcweir 
412cdf0e10cSrcweir 			// there's no pixmap for alpha bitmap. we might still
413cdf0e10cSrcweir 			// use rgb pixmap and only access alpha pixels the
414cdf0e10cSrcweir 			// slow way. now we just speedup rgb bitmaps
415cdf0e10cSrcweir 			if( !aBmpEx.IsTransparent() && !aBmpEx.IsAlpha() ) {
416cdf0e10cSrcweir 				pSurface = rSurfaceProvider->createSurface( aBitmap );
417cdf0e10cSrcweir 				data = NULL;
418cdf0e10cSrcweir 				bHasAlpha = false;
419cdf0e10cSrcweir 			}
420cdf0e10cSrcweir 
421cdf0e10cSrcweir 			if( !pSurface ) {
422cdf0e10cSrcweir                 AlphaMask aAlpha = aBmpEx.GetAlpha();
423cdf0e10cSrcweir 
424cdf0e10cSrcweir 				::BitmapReadAccess*	pBitmapReadAcc = aBitmap.AcquireReadAccess();
425cdf0e10cSrcweir 				::BitmapReadAccess*	pAlphaReadAcc = NULL;
426cdf0e10cSrcweir 				const long		nWidth = pBitmapReadAcc->Width();
427cdf0e10cSrcweir 				const long		nHeight = pBitmapReadAcc->Height();
428cdf0e10cSrcweir 				long nX, nY;
429cdf0e10cSrcweir 				bool bIsAlpha = false;
430cdf0e10cSrcweir 
431cdf0e10cSrcweir 				if( aBmpEx.IsTransparent() || aBmpEx.IsAlpha() )
432cdf0e10cSrcweir 					pAlphaReadAcc = aAlpha.AcquireReadAccess();
433cdf0e10cSrcweir 
434cdf0e10cSrcweir 				data = (unsigned char*) malloc( nWidth*nHeight*4 );
435cdf0e10cSrcweir 
436cdf0e10cSrcweir 				long nOff = 0;
437cdf0e10cSrcweir 				::Color aColor;
438cdf0e10cSrcweir 				unsigned int nAlpha = 255;
439cdf0e10cSrcweir 
440cdf0e10cSrcweir 				for( nY = 0; nY < nHeight; nY++ ) {
441cdf0e10cSrcweir 					::Scanline pReadScan;
442cdf0e10cSrcweir 
443cdf0e10cSrcweir 					switch( pBitmapReadAcc->GetScanlineFormat() ) {
444cdf0e10cSrcweir 					case BMP_FORMAT_8BIT_PAL:
445cdf0e10cSrcweir 						pReadScan = pBitmapReadAcc->GetScanline( nY );
446cdf0e10cSrcweir 						if( pAlphaReadAcc )
447cdf0e10cSrcweir 							if( readAlpha( pAlphaReadAcc, nY, nWidth, data, nOff ) )
448cdf0e10cSrcweir 								bIsAlpha = true;
449cdf0e10cSrcweir 
450cdf0e10cSrcweir 						for( nX = 0; nX < nWidth; nX++ ) {
451cdf0e10cSrcweir #ifdef OSL_BIGENDIAN
452cdf0e10cSrcweir 							if( pAlphaReadAcc )
453cdf0e10cSrcweir 								nAlpha = data[ nOff++ ];
454cdf0e10cSrcweir 							else
455cdf0e10cSrcweir 								nAlpha = data[ nOff++ ] = 255;
456cdf0e10cSrcweir #else
457cdf0e10cSrcweir 							if( pAlphaReadAcc )
458cdf0e10cSrcweir 								nAlpha = data[ nOff + 3 ];
459cdf0e10cSrcweir 							else
460cdf0e10cSrcweir 								nAlpha = data[ nOff + 3 ] = 255;
461cdf0e10cSrcweir #endif
462cdf0e10cSrcweir 							aColor = pBitmapReadAcc->GetPaletteColor( *pReadScan++ );
463cdf0e10cSrcweir 
464cdf0e10cSrcweir #ifdef OSL_BIGENDIAN
465cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( aColor.GetRed() ) )/255 );
466cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( aColor.GetGreen() ) )/255 );
467cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( aColor.GetBlue() ) )/255 );
468cdf0e10cSrcweir #else
469cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( aColor.GetBlue() ) )/255 );
470cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( aColor.GetGreen() ) )/255 );
471cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( aColor.GetRed() ) )/255 );
472cdf0e10cSrcweir 							nOff++;
473cdf0e10cSrcweir #endif
474cdf0e10cSrcweir 						}
475cdf0e10cSrcweir 						break;
476cdf0e10cSrcweir 					case BMP_FORMAT_24BIT_TC_BGR:
477cdf0e10cSrcweir 						pReadScan = pBitmapReadAcc->GetScanline( nY );
478cdf0e10cSrcweir 						if( pAlphaReadAcc )
479cdf0e10cSrcweir 							if( readAlpha( pAlphaReadAcc, nY, nWidth, data, nOff ) )
480cdf0e10cSrcweir 								bIsAlpha = true;
481cdf0e10cSrcweir 
482cdf0e10cSrcweir 						for( nX = 0; nX < nWidth; nX++ ) {
483cdf0e10cSrcweir #ifdef OSL_BIGENDIAN
484cdf0e10cSrcweir 							if( pAlphaReadAcc )
485cdf0e10cSrcweir 								nAlpha = data[ nOff ];
486cdf0e10cSrcweir 							else
487cdf0e10cSrcweir 								nAlpha = data[ nOff ] = 255;
488cdf0e10cSrcweir 							data[ nOff + 3 ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
489cdf0e10cSrcweir 							data[ nOff + 2 ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
490cdf0e10cSrcweir 							data[ nOff + 1 ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
491cdf0e10cSrcweir 							nOff += 4;
492cdf0e10cSrcweir #else
493cdf0e10cSrcweir 							if( pAlphaReadAcc )
494cdf0e10cSrcweir 								nAlpha = data[ nOff + 3 ];
495cdf0e10cSrcweir 							else
496cdf0e10cSrcweir 								nAlpha = data[ nOff + 3 ] = 255;
497cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
498cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
499cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
500cdf0e10cSrcweir 							nOff++;
501cdf0e10cSrcweir #endif
502cdf0e10cSrcweir 						}
503cdf0e10cSrcweir 						break;
504cdf0e10cSrcweir 					case BMP_FORMAT_24BIT_TC_RGB:
505cdf0e10cSrcweir 						pReadScan = pBitmapReadAcc->GetScanline( nY );
506cdf0e10cSrcweir 						if( pAlphaReadAcc )
507cdf0e10cSrcweir 							if( readAlpha( pAlphaReadAcc, nY, nWidth, data, nOff ) )
508cdf0e10cSrcweir 								bIsAlpha = true;
509cdf0e10cSrcweir 
510cdf0e10cSrcweir 						for( nX = 0; nX < nWidth; nX++ ) {
511cdf0e10cSrcweir #ifdef OSL_BIGENDIAN
512cdf0e10cSrcweir 							if( pAlphaReadAcc )
513cdf0e10cSrcweir 								nAlpha = data[ nOff++ ];
514cdf0e10cSrcweir 							else
515cdf0e10cSrcweir 								nAlpha = data[ nOff++ ] = 255;
516cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
517cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
518cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
519cdf0e10cSrcweir #else
520cdf0e10cSrcweir 							if( pAlphaReadAcc )
521cdf0e10cSrcweir 								nAlpha = data[ nOff + 3 ];
522cdf0e10cSrcweir 							else
523cdf0e10cSrcweir 								nAlpha = data[ nOff + 3 ] = 255;
524cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( pReadScan[ 2 ] ) )/255 );
525cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( pReadScan[ 1 ] ) )/255 );
526cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( pReadScan[ 0 ] ) )/255 );
527cdf0e10cSrcweir 							pReadScan += 3;
528cdf0e10cSrcweir 							nOff++;
529cdf0e10cSrcweir #endif
530cdf0e10cSrcweir 						}
531cdf0e10cSrcweir 						break;
532cdf0e10cSrcweir 					case BMP_FORMAT_32BIT_TC_BGRA:
533cdf0e10cSrcweir 						pReadScan = pBitmapReadAcc->GetScanline( nY );
534cdf0e10cSrcweir 						if( pAlphaReadAcc )
535cdf0e10cSrcweir 							if( readAlpha( pAlphaReadAcc, nY, nWidth, data, nOff ) )
536cdf0e10cSrcweir 								bIsAlpha = true;
537cdf0e10cSrcweir 
538cdf0e10cSrcweir 						for( nX = 0; nX < nWidth; nX++ ) {
539cdf0e10cSrcweir #ifdef OSL_BIGENDIAN
540cdf0e10cSrcweir 							if( pAlphaReadAcc )
541cdf0e10cSrcweir 								nAlpha = data[ nOff++ ];
542cdf0e10cSrcweir 							else
543cdf0e10cSrcweir 								nAlpha = data[ nOff++ ] = pReadScan[ 3 ];
544cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( pReadScan[ 2 ] ) )/255 );
545cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( pReadScan[ 1 ] ) )/255 );
546cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( pReadScan[ 0 ] ) )/255 );
547cdf0e10cSrcweir 							pReadScan += 4;
548cdf0e10cSrcweir #else
549cdf0e10cSrcweir 							if( pAlphaReadAcc )
550cdf0e10cSrcweir 								nAlpha = data[ nOff + 3 ];
551cdf0e10cSrcweir 							else
552cdf0e10cSrcweir 								nAlpha = data[ nOff + 3 ] = pReadScan[ 3 ];
553cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
554cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
555cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
556cdf0e10cSrcweir 							pReadScan++;
557cdf0e10cSrcweir 							nOff++;
558cdf0e10cSrcweir #endif
559cdf0e10cSrcweir 						}
560cdf0e10cSrcweir 						break;
561cdf0e10cSrcweir 					case BMP_FORMAT_32BIT_TC_RGBA:
562cdf0e10cSrcweir 						pReadScan = pBitmapReadAcc->GetScanline( nY );
563cdf0e10cSrcweir 						if( pAlphaReadAcc )
564cdf0e10cSrcweir 							if( readAlpha( pAlphaReadAcc, nY, nWidth, data, nOff ) )
565cdf0e10cSrcweir 								bIsAlpha = true;
566cdf0e10cSrcweir 
567cdf0e10cSrcweir 						for( nX = 0; nX < nWidth; nX++ ) {
568cdf0e10cSrcweir #ifdef OSL_BIGENDIAN
569cdf0e10cSrcweir 							if( pAlphaReadAcc )
570cdf0e10cSrcweir 								nAlpha = data[ nOff ++ ];
571cdf0e10cSrcweir 							else
572cdf0e10cSrcweir 								nAlpha = data[ nOff ++ ] = 255;
573cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
574cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
575cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( *pReadScan++ ) )/255 );
576cdf0e10cSrcweir 							pReadScan++;
577cdf0e10cSrcweir #else
578cdf0e10cSrcweir 							if( pAlphaReadAcc )
579cdf0e10cSrcweir 								nAlpha = data[ nOff + 3 ];
580cdf0e10cSrcweir 							else
581cdf0e10cSrcweir 								nAlpha = data[ nOff + 3 ] = 255;
582cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( pReadScan[ 2 ] ) )/255 );
583cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( pReadScan[ 1 ] ) )/255 );
584cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*( pReadScan[ 0 ] ) )/255 );
585cdf0e10cSrcweir 							pReadScan += 4;
586cdf0e10cSrcweir 							nOff++;
587cdf0e10cSrcweir #endif
588cdf0e10cSrcweir 						}
589cdf0e10cSrcweir 						break;
590cdf0e10cSrcweir 					default:
591cdf0e10cSrcweir 						OSL_TRACE( "fallback to GetColor - slow, format: %d", pBitmapReadAcc->GetScanlineFormat() );
592cdf0e10cSrcweir 
593cdf0e10cSrcweir 						if( pAlphaReadAcc )
594cdf0e10cSrcweir 							if( readAlpha( pAlphaReadAcc, nY, nWidth, data, nOff ) )
595cdf0e10cSrcweir 								bIsAlpha = true;
596cdf0e10cSrcweir 
597cdf0e10cSrcweir 						for( nX = 0; nX < nWidth; nX++ ) {
598cdf0e10cSrcweir 							aColor = pBitmapReadAcc->GetColor( nY, nX );
599cdf0e10cSrcweir 
600cdf0e10cSrcweir 							// cairo need premultiplied color values
601cdf0e10cSrcweir 							// TODO(rodo) handle endianess
602cdf0e10cSrcweir #ifdef OSL_BIGENDIAN
603cdf0e10cSrcweir 							if( pAlphaReadAcc )
604cdf0e10cSrcweir 								nAlpha = data[ nOff++ ];
605cdf0e10cSrcweir 							else
606cdf0e10cSrcweir 								nAlpha = data[ nOff++ ] = 255;
607cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*aColor.GetRed() )/255 );
608cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*aColor.GetGreen() )/255 );
609cdf0e10cSrcweir                             data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*aColor.GetBlue() )/255 );
610cdf0e10cSrcweir #else
611cdf0e10cSrcweir 							if( pAlphaReadAcc )
612cdf0e10cSrcweir 								nAlpha = data[ nOff + 3 ];
613cdf0e10cSrcweir 							else
614cdf0e10cSrcweir 								nAlpha = data[ nOff + 3 ] = 255;
615cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*aColor.GetBlue() )/255 );
616cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*aColor.GetGreen() )/255 );
617cdf0e10cSrcweir 							data[ nOff++ ] = sal::static_int_cast<unsigned char>(( nAlpha*aColor.GetRed() )/255 );
618cdf0e10cSrcweir 							nOff ++;
619cdf0e10cSrcweir #endif
620cdf0e10cSrcweir 						}
621cdf0e10cSrcweir 					}
622cdf0e10cSrcweir 				}
623cdf0e10cSrcweir 
624cdf0e10cSrcweir 				aBitmap.ReleaseAccess( pBitmapReadAcc );
625cdf0e10cSrcweir 				if( pAlphaReadAcc )
626cdf0e10cSrcweir 					aAlpha.ReleaseAccess( pAlphaReadAcc );
627cdf0e10cSrcweir 
628cdf0e10cSrcweir 				SurfaceSharedPtr pImageSurface = createSurface(
629cdf0e10cSrcweir                     CairoSurfaceSharedPtr(
630cdf0e10cSrcweir                         cairo_image_surface_create_for_data(
631cdf0e10cSrcweir                             data,
632cdf0e10cSrcweir                             bIsAlpha ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
633cdf0e10cSrcweir                             nWidth, nHeight, nWidth*4 ),
634cdf0e10cSrcweir                         &cairo_surface_destroy) );
635cdf0e10cSrcweir 
636cdf0e10cSrcweir 				// 		pSurface = rSurfaceProvider->getSurface( ::basegfx::B2ISize( nWidth, nHeight ), bIsAlpha ? CAIRO_CONTENT_COLOR_ALPHA : CAIRO_CONTENT_COLOR );
637cdf0e10cSrcweir 				// 		Cairo* pTargetCairo = cairo_create( pSurface );
638cdf0e10cSrcweir 				// 		cairo_set_source_surface( pTargetCairo, pImageSurface, 0, 0 );
639cdf0e10cSrcweir 
640cdf0e10cSrcweir 				// 				    //if( !bIsAlpha )
641cdf0e10cSrcweir 				// 				    //cairo_set_operator( pTargetCairo, CAIRO_OPERATOR_SOURCE );
642cdf0e10cSrcweir 
643cdf0e10cSrcweir 				// 		cairo_paint( pTargetCairo );
644cdf0e10cSrcweir 				// 		cairo_destroy( pTargetCairo );
645cdf0e10cSrcweir 				// 		cairo_surface_destroy( pImageSurface );
646cdf0e10cSrcweir 				pSurface = pImageSurface;
647cdf0e10cSrcweir 
648cdf0e10cSrcweir 				bHasAlpha = bIsAlpha;
649cdf0e10cSrcweir 
650cdf0e10cSrcweir 				OSL_TRACE("image: %d x %d alpha: %d alphaRead %p", nWidth, nHeight, bIsAlpha, pAlphaReadAcc);
651cdf0e10cSrcweir 			}
652cdf0e10cSrcweir 		}
653cdf0e10cSrcweir 
654cdf0e10cSrcweir 		return pSurface;
655cdf0e10cSrcweir     }
656cdf0e10cSrcweir 
addColorStops(Pattern * pPattern,const uno::Sequence<uno::Sequence<double>> & rColors,const uno::Sequence<double> & rStops,bool bReverseStops=false)657cdf0e10cSrcweir     static void addColorStops( Pattern* pPattern, const uno::Sequence< uno::Sequence< double > >& rColors, const uno::Sequence< double >& rStops, bool bReverseStops = false )
658cdf0e10cSrcweir     {
659cdf0e10cSrcweir         float stop;
660cdf0e10cSrcweir         int i;
661cdf0e10cSrcweir 
662cdf0e10cSrcweir         OSL_ASSERT( rColors.getLength() == rStops.getLength() );
663cdf0e10cSrcweir 
664cdf0e10cSrcweir         for( i = 0; i < rColors.getLength(); i++ ) {
665cdf0e10cSrcweir             const uno::Sequence< double >& rColor( rColors[i] );
666cdf0e10cSrcweir             stop = bReverseStops ? 1 - rStops[i] : rStops[i];
667cdf0e10cSrcweir             if( rColor.getLength() == 3 )
668cdf0e10cSrcweir                 cairo_pattern_add_color_stop_rgb( pPattern, stop, rColor[0], rColor[1], rColor[2] );
669cdf0e10cSrcweir             else if( rColor.getLength() == 4 ) {
670cdf0e10cSrcweir                 double alpha = rColor[3];
671cdf0e10cSrcweir                 // cairo expects premultiplied alpha
672cdf0e10cSrcweir                 cairo_pattern_add_color_stop_rgba( pPattern, stop, rColor[0]*alpha, rColor[1]*alpha, rColor[2]*alpha, alpha );
673cdf0e10cSrcweir             }
674cdf0e10cSrcweir         }
675cdf0e10cSrcweir     }
676cdf0e10cSrcweir 
lerp(const uno::Sequence<double> & rLeft,const uno::Sequence<double> & rRight,double fAlpha)677cdf0e10cSrcweir     static uno::Sequence<double> lerp(const uno::Sequence<double>& rLeft, const uno::Sequence<double>& rRight, double fAlpha)
678cdf0e10cSrcweir     {
679cdf0e10cSrcweir         if( rLeft.getLength() == 3 )
680cdf0e10cSrcweir         {
681cdf0e10cSrcweir             uno::Sequence<double> aRes(3);
682cdf0e10cSrcweir             aRes[0] = basegfx::tools::lerp(rLeft[0],rRight[0],fAlpha);
683cdf0e10cSrcweir             aRes[1] = basegfx::tools::lerp(rLeft[1],rRight[1],fAlpha);
684cdf0e10cSrcweir             aRes[2] = basegfx::tools::lerp(rLeft[2],rRight[2],fAlpha);
685cdf0e10cSrcweir             return aRes;
686cdf0e10cSrcweir         }
687cdf0e10cSrcweir         else if( rLeft.getLength() == 4 )
688cdf0e10cSrcweir         {
689cdf0e10cSrcweir             uno::Sequence<double> aRes(4);
690cdf0e10cSrcweir             aRes[0] = basegfx::tools::lerp(rLeft[0],rRight[0],fAlpha);
691cdf0e10cSrcweir             aRes[1] = basegfx::tools::lerp(rLeft[1],rRight[1],fAlpha);
692cdf0e10cSrcweir             aRes[2] = basegfx::tools::lerp(rLeft[2],rRight[2],fAlpha);
693cdf0e10cSrcweir             aRes[3] = basegfx::tools::lerp(rLeft[3],rRight[3],fAlpha);
694cdf0e10cSrcweir             return aRes;
695cdf0e10cSrcweir         }
696cdf0e10cSrcweir 
697cdf0e10cSrcweir         return uno::Sequence<double>();
698cdf0e10cSrcweir     }
699cdf0e10cSrcweir 
patternFromParametricPolyPolygon(::canvas::ParametricPolyPolygon & rPolygon)700cdf0e10cSrcweir     static Pattern* patternFromParametricPolyPolygon( ::canvas::ParametricPolyPolygon& rPolygon )
701cdf0e10cSrcweir     {
702cdf0e10cSrcweir 	Pattern* pPattern = NULL;
703cdf0e10cSrcweir 	const ::canvas::ParametricPolyPolygon::Values aValues = rPolygon.getValues();
704cdf0e10cSrcweir 	double x0, x1, y0, y1, cx, cy, r0, r1;
705cdf0e10cSrcweir 
706cdf0e10cSrcweir // undef macros from vclenum.hxx which conflicts with GradientType enum values
707cdf0e10cSrcweir #undef GRADIENT_LINEAR
708cdf0e10cSrcweir #undef GRADIENT_ELLIPTICAL
709cdf0e10cSrcweir 
710cdf0e10cSrcweir 	switch( aValues.meType ) {
711cdf0e10cSrcweir 	case ::canvas::ParametricPolyPolygon::GRADIENT_LINEAR:
712cdf0e10cSrcweir 	    x0 = 0;
713cdf0e10cSrcweir 	    y0 = 0;
714cdf0e10cSrcweir 	    x1 = 1;
715cdf0e10cSrcweir 	    y1 = 0;
716cdf0e10cSrcweir 	    pPattern = cairo_pattern_create_linear( x0, y0, x1, y1 );
717cdf0e10cSrcweir 	    addColorStops( pPattern, aValues.maColors, aValues.maStops );
718cdf0e10cSrcweir 	    break;
719cdf0e10cSrcweir 
720cdf0e10cSrcweir 	case ::canvas::ParametricPolyPolygon::GRADIENT_ELLIPTICAL:
721cdf0e10cSrcweir 	    cx = 0;
722cdf0e10cSrcweir 	    cy = 0;
723cdf0e10cSrcweir 	    r0 = 0;
724cdf0e10cSrcweir 	    r1 = 1;
725cdf0e10cSrcweir 
726cdf0e10cSrcweir 	    pPattern = cairo_pattern_create_radial( cx, cy, r0, cy, cy, r1 );
727cdf0e10cSrcweir 	    addColorStops( pPattern, aValues.maColors, aValues.maStops, true );
728cdf0e10cSrcweir 	    break;
729cdf0e10cSrcweir     default:
730cdf0e10cSrcweir         break;
731cdf0e10cSrcweir 	}
732cdf0e10cSrcweir 
733cdf0e10cSrcweir 	return pPattern;
734cdf0e10cSrcweir     }
735cdf0e10cSrcweir 
doOperation(Operation aOperation,Cairo * pCairo,const uno::Sequence<rendering::Texture> * pTextures,const SurfaceProviderRef & pDevice,const basegfx::B2DRange & rBounds)736cdf0e10cSrcweir     static void doOperation( Operation aOperation,
737cdf0e10cSrcweir                              Cairo* pCairo,
738cdf0e10cSrcweir                              const uno::Sequence< rendering::Texture >* pTextures,
739cdf0e10cSrcweir                              const SurfaceProviderRef& pDevice,
740cdf0e10cSrcweir                              const basegfx::B2DRange& rBounds )
741cdf0e10cSrcweir     {
742cdf0e10cSrcweir 	switch( aOperation ) {
743cdf0e10cSrcweir 	case Fill:
744cdf0e10cSrcweir 		/* TODO: multitexturing */
745cdf0e10cSrcweir 	    if( pTextures ) {
746cdf0e10cSrcweir 		const ::com::sun::star::rendering::Texture& aTexture ( (*pTextures)[0] );
747cdf0e10cSrcweir 		if( aTexture.Bitmap.is() ) {
748cdf0e10cSrcweir 		    unsigned char* data = NULL;
749cdf0e10cSrcweir 		    bool bHasAlpha = false;
750cdf0e10cSrcweir 		    SurfaceSharedPtr pSurface = surfaceFromXBitmap( (*pTextures)[0].Bitmap, pDevice, data, bHasAlpha );
751cdf0e10cSrcweir 
752cdf0e10cSrcweir 		    if( pSurface ) {
753cdf0e10cSrcweir 			cairo_pattern_t* pPattern;
754cdf0e10cSrcweir 
755cdf0e10cSrcweir 			cairo_save( pCairo );
756cdf0e10cSrcweir 
757cdf0e10cSrcweir 			::com::sun::star::geometry::AffineMatrix2D aTransform( aTexture.AffineTransform );
758cdf0e10cSrcweir 			Matrix aScaleMatrix, aTextureMatrix, aScaledTextureMatrix;
759cdf0e10cSrcweir 
760cdf0e10cSrcweir 			cairo_matrix_init( &aTextureMatrix,
761cdf0e10cSrcweir 					   aTransform.m00, aTransform.m10, aTransform.m01,
762cdf0e10cSrcweir 					   aTransform.m11, aTransform.m02, aTransform.m12);
763cdf0e10cSrcweir 
764cdf0e10cSrcweir 			geometry::IntegerSize2D aSize = aTexture.Bitmap->getSize();
765cdf0e10cSrcweir 
766cdf0e10cSrcweir 			cairo_matrix_init_scale( &aScaleMatrix, 1.0/aSize.Width, 1.0/aSize.Height );
767cdf0e10cSrcweir 			cairo_matrix_multiply( &aScaledTextureMatrix, &aTextureMatrix, &aScaleMatrix );
768cdf0e10cSrcweir 			cairo_matrix_invert( &aScaledTextureMatrix );
769cdf0e10cSrcweir 
770cdf0e10cSrcweir 			// we don't care about repeat mode yet, so the workaround is disabled for now
771cdf0e10cSrcweir 			pPattern = cairo_pattern_create_for_surface( pSurface->getCairoSurface().get() );
772cdf0e10cSrcweir 
773cdf0e10cSrcweir  			if( aTexture.RepeatModeX == rendering::TexturingMode::REPEAT &&
774cdf0e10cSrcweir 			    aTexture.RepeatModeY == rendering::TexturingMode::REPEAT )
775cdf0e10cSrcweir             {
776cdf0e10cSrcweir 			    cairo_pattern_set_extend( pPattern, CAIRO_EXTEND_REPEAT );
777cdf0e10cSrcweir             }
778cdf0e10cSrcweir             else if ( aTexture.RepeatModeX == rendering::TexturingMode::NONE &&
779cdf0e10cSrcweir                       aTexture.RepeatModeY == rendering::TexturingMode::NONE )
780cdf0e10cSrcweir             {
781cdf0e10cSrcweir 			    cairo_pattern_set_extend( pPattern, CAIRO_EXTEND_NONE );
782cdf0e10cSrcweir             }
783cdf0e10cSrcweir             else if ( aTexture.RepeatModeX == rendering::TexturingMode::CLAMP &&
784cdf0e10cSrcweir                       aTexture.RepeatModeY == rendering::TexturingMode::CLAMP )
785cdf0e10cSrcweir             {
786cdf0e10cSrcweir 			    cairo_pattern_set_extend( pPattern, CAIRO_EXTEND_PAD );
787cdf0e10cSrcweir             }
788cdf0e10cSrcweir 
789cdf0e10cSrcweir 			aScaledTextureMatrix.x0 = basegfx::fround( aScaledTextureMatrix.x0 );
790cdf0e10cSrcweir 			aScaledTextureMatrix.y0 = basegfx::fround( aScaledTextureMatrix.y0 );
791cdf0e10cSrcweir 			cairo_pattern_set_matrix( pPattern, &aScaledTextureMatrix );
792cdf0e10cSrcweir 
793cdf0e10cSrcweir 			cairo_set_source( pCairo, pPattern );
794cdf0e10cSrcweir 			if( !bHasAlpha )
795cdf0e10cSrcweir 			    cairo_set_operator( pCairo, CAIRO_OPERATOR_SOURCE );
796cdf0e10cSrcweir 			cairo_fill( pCairo );
797cdf0e10cSrcweir 
798cdf0e10cSrcweir 			cairo_restore( pCairo );
799cdf0e10cSrcweir 
800cdf0e10cSrcweir 			cairo_pattern_destroy( pPattern );
801cdf0e10cSrcweir 		    }
802cdf0e10cSrcweir 
803cdf0e10cSrcweir 		    if( data )
804cdf0e10cSrcweir 			free( data );
805cdf0e10cSrcweir 		} else if( aTexture.Gradient.is() ) {
806cdf0e10cSrcweir 		    uno::Reference< lang::XServiceInfo > xRef( aTexture.Gradient, uno::UNO_QUERY );
807cdf0e10cSrcweir 
808cdf0e10cSrcweir 		    OSL_TRACE( "gradient fill" );
809cdf0e10cSrcweir 		    if( xRef.is() &&
810cdf0e10cSrcweir 			xRef->getImplementationName().equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( PARAMETRICPOLYPOLYGON_IMPLEMENTATION_NAME ) ) ) ) {
811cdf0e10cSrcweir 				// TODO(Q1): Maybe use dynamic_cast here
812cdf0e10cSrcweir 
813cdf0e10cSrcweir 				// TODO(E1): Return value
814cdf0e10cSrcweir 				// TODO(F1): FillRule
815cdf0e10cSrcweir 			OSL_TRACE( "known implementation" );
816cdf0e10cSrcweir 
817cdf0e10cSrcweir 			::canvas::ParametricPolyPolygon* pPolyImpl = static_cast< ::canvas::ParametricPolyPolygon* >( aTexture.Gradient.get() );
818cdf0e10cSrcweir 			::com::sun::star::geometry::AffineMatrix2D aTransform( aTexture.AffineTransform );
819cdf0e10cSrcweir 			Matrix aTextureMatrix;
820cdf0e10cSrcweir 
821cdf0e10cSrcweir 			cairo_matrix_init( &aTextureMatrix,
822cdf0e10cSrcweir 					   aTransform.m00, aTransform.m10, aTransform.m01,
823cdf0e10cSrcweir 					   aTransform.m11, aTransform.m02, aTransform.m12);
824cdf0e10cSrcweir             if( pPolyImpl->getValues().meType == canvas::ParametricPolyPolygon::GRADIENT_RECTANGULAR )
825cdf0e10cSrcweir             {
826cdf0e10cSrcweir                 // no general path gradient yet in cairo; emulate then
827cdf0e10cSrcweir                 cairo_save( pCairo );
828cdf0e10cSrcweir                 cairo_clip( pCairo );
829cdf0e10cSrcweir 
830cdf0e10cSrcweir                 // fill bound rect with start color
831cdf0e10cSrcweir                 cairo_rectangle( pCairo, rBounds.getMinX(), rBounds.getMinY(),
832cdf0e10cSrcweir                                  rBounds.getWidth(), rBounds.getHeight() );
833cdf0e10cSrcweir                 setColor(pCairo,pPolyImpl->getValues().maColors[0]);
834cdf0e10cSrcweir                 cairo_fill(pCairo);
835cdf0e10cSrcweir 
836cdf0e10cSrcweir                 cairo_transform( pCairo, &aTextureMatrix );
837cdf0e10cSrcweir 
838cdf0e10cSrcweir                 // longest line in gradient bound rect
839cdf0e10cSrcweir                 const unsigned int nGradientSize(
840cdf0e10cSrcweir                     static_cast<unsigned int>(
841cdf0e10cSrcweir                         ::basegfx::B2DVector(rBounds.getMinimum() - rBounds.getMaximum()).getLength() + 1.0 ) );
842cdf0e10cSrcweir 
843cdf0e10cSrcweir                 // typical number for pixel of the same color (strip size)
844cdf0e10cSrcweir                 const unsigned int nStripSize( nGradientSize < 50 ? 2 : 4 );
845cdf0e10cSrcweir 
846cdf0e10cSrcweir                 // use at least three steps, and at utmost the number of color
847cdf0e10cSrcweir                 // steps
848cdf0e10cSrcweir                 const unsigned int nStepCount(
849cdf0e10cSrcweir                     ::std::max(
850cdf0e10cSrcweir                         3U,
851cdf0e10cSrcweir                         ::std::min(
852cdf0e10cSrcweir                             nGradientSize / nStripSize,
853cdf0e10cSrcweir                             128U )) + 1 );
854cdf0e10cSrcweir 
855cdf0e10cSrcweir                 const uno::Sequence<double>* pColors=&pPolyImpl->getValues().maColors[0];
856cdf0e10cSrcweir                 basegfx::tools::KeyStopLerp aLerper(pPolyImpl->getValues().maStops);
857cdf0e10cSrcweir                 for( unsigned int i=1; i<nStepCount; ++i )
858cdf0e10cSrcweir                 {
859cdf0e10cSrcweir                     const double fT( i/double(nStepCount) );
860cdf0e10cSrcweir 
861cdf0e10cSrcweir                     std::ptrdiff_t nIndex;
862cdf0e10cSrcweir                     double fAlpha;
863cdf0e10cSrcweir                     boost::tuples::tie(nIndex,fAlpha)=aLerper.lerp(fT);
864cdf0e10cSrcweir 
865cdf0e10cSrcweir                     setColor(pCairo, lerp(pColors[nIndex], pColors[nIndex+1], fAlpha));
866cdf0e10cSrcweir                     cairo_rectangle( pCairo, -1+fT, -1+fT, 2-2*fT, 2-2*fT );
867cdf0e10cSrcweir                     cairo_fill(pCairo);
868cdf0e10cSrcweir                 }
869cdf0e10cSrcweir 
870cdf0e10cSrcweir                 cairo_restore( pCairo );
871cdf0e10cSrcweir             }
872cdf0e10cSrcweir             else
873cdf0e10cSrcweir             {
874cdf0e10cSrcweir                 Pattern* pPattern = patternFromParametricPolyPolygon( *pPolyImpl );
875cdf0e10cSrcweir 
876cdf0e10cSrcweir                 if( pPattern ) {
877cdf0e10cSrcweir                     OSL_TRACE( "filling with pattern" );
878cdf0e10cSrcweir 
879cdf0e10cSrcweir                     cairo_save( pCairo );
880cdf0e10cSrcweir 
881cdf0e10cSrcweir                     cairo_transform( pCairo, &aTextureMatrix );
882cdf0e10cSrcweir                     cairo_set_source( pCairo, pPattern );
883cdf0e10cSrcweir                     cairo_fill( pCairo );
884cdf0e10cSrcweir                     cairo_restore( pCairo );
885cdf0e10cSrcweir 
886cdf0e10cSrcweir                     cairo_pattern_destroy( pPattern );
887cdf0e10cSrcweir                 }
888cdf0e10cSrcweir 		    }
889cdf0e10cSrcweir             }
890cdf0e10cSrcweir 		}
891cdf0e10cSrcweir 	    } else
892cdf0e10cSrcweir 		cairo_fill( pCairo );
893cdf0e10cSrcweir 	    OSL_TRACE("fill");
894cdf0e10cSrcweir 	break;
895cdf0e10cSrcweir 	case Stroke:
896cdf0e10cSrcweir 	    cairo_stroke( pCairo );
897cdf0e10cSrcweir 	    OSL_TRACE("stroke");
898cdf0e10cSrcweir 	break;
899cdf0e10cSrcweir 	case Clip:
900cdf0e10cSrcweir 	    cairo_clip( pCairo );
901cdf0e10cSrcweir 	    OSL_TRACE("clip");
902cdf0e10cSrcweir 	break;
903cdf0e10cSrcweir 	}
904cdf0e10cSrcweir     }
905cdf0e10cSrcweir 
clipNULL(Cairo * pCairo)906cdf0e10cSrcweir     static void clipNULL( Cairo *pCairo )
907cdf0e10cSrcweir     {
908cdf0e10cSrcweir 	OSL_TRACE("clipNULL");
909cdf0e10cSrcweir 	Matrix aOrigMatrix, aIdentityMatrix;
910cdf0e10cSrcweir 
911cdf0e10cSrcweir 	/* we set identity matrix here to overcome bug in cairo 0.9.2
912cdf0e10cSrcweir 	   where XCreatePixmap is called with zero width and height.
913cdf0e10cSrcweir 
914cdf0e10cSrcweir 	   it also reaches faster path in cairo clipping code.
915cdf0e10cSrcweir 	*/
916cdf0e10cSrcweir 	cairo_matrix_init_identity( &aIdentityMatrix );
917cdf0e10cSrcweir 	cairo_get_matrix( pCairo, &aOrigMatrix );
918cdf0e10cSrcweir 	cairo_set_matrix( pCairo, &aIdentityMatrix );
919cdf0e10cSrcweir 
920cdf0e10cSrcweir 	cairo_reset_clip( pCairo );
921cdf0e10cSrcweir 	cairo_rectangle( pCairo, 0, 0, 1, 1 );
922cdf0e10cSrcweir 	cairo_clip( pCairo );
923cdf0e10cSrcweir 	cairo_rectangle( pCairo, 2, 0, 1, 1 );
924cdf0e10cSrcweir 	cairo_clip( pCairo );
925cdf0e10cSrcweir 
926cdf0e10cSrcweir 	/* restore the original matrix */
927cdf0e10cSrcweir 	cairo_set_matrix( pCairo, &aOrigMatrix );
928cdf0e10cSrcweir     }
929cdf0e10cSrcweir 
doPolyPolygonImplementation(::basegfx::B2DPolyPolygon aPolyPolygon,Operation aOperation,Cairo * pCairo,const uno::Sequence<rendering::Texture> * pTextures,const SurfaceProviderRef & pDevice,rendering::FillRule eFillrule)930cdf0e10cSrcweir     void doPolyPolygonImplementation( ::basegfx::B2DPolyPolygon aPolyPolygon,
931cdf0e10cSrcweir                                       Operation aOperation,
932cdf0e10cSrcweir                                       Cairo* pCairo,
933cdf0e10cSrcweir                                       const uno::Sequence< rendering::Texture >* pTextures,
934cdf0e10cSrcweir                                       const SurfaceProviderRef& pDevice,
935cdf0e10cSrcweir                                       rendering::FillRule eFillrule )
936cdf0e10cSrcweir     {
937cdf0e10cSrcweir 		if( pTextures )
938cdf0e10cSrcweir 			ENSURE_ARG_OR_THROW( pTextures->getLength(),
939cdf0e10cSrcweir 							 "CanvasHelper::fillTexturedPolyPolygon: empty texture sequence");
940cdf0e10cSrcweir 
941cdf0e10cSrcweir 	bool bOpToDo = false;
942cdf0e10cSrcweir 	Matrix aOrigMatrix, aIdentityMatrix;
943cdf0e10cSrcweir 	double nX, nY, nBX, nBY, nAX, nAY;
944cdf0e10cSrcweir 
945cdf0e10cSrcweir 	cairo_get_matrix( pCairo, &aOrigMatrix );
946cdf0e10cSrcweir 	cairo_matrix_init_identity( &aIdentityMatrix );
947cdf0e10cSrcweir 	cairo_set_matrix( pCairo, &aIdentityMatrix );
948cdf0e10cSrcweir 
949cdf0e10cSrcweir     cairo_set_fill_rule( pCairo,
950cdf0e10cSrcweir                          eFillrule == rendering::FillRule_EVEN_ODD ?
951cdf0e10cSrcweir                          CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING );
952cdf0e10cSrcweir 
953cdf0e10cSrcweir 	for( sal_uInt32 nPolygonIndex = 0; nPolygonIndex < aPolyPolygon.count(); nPolygonIndex++ ) {
954cdf0e10cSrcweir 	    ::basegfx::B2DPolygon aPolygon( aPolyPolygon.getB2DPolygon( nPolygonIndex ) );
955cdf0e10cSrcweir 	    const sal_uInt32 nPointCount( aPolygon.count() );
956cdf0e10cSrcweir         // to correctly render closed curves, need to output first
957cdf0e10cSrcweir         // point twice (so output one additional point)
958cdf0e10cSrcweir 	    const sal_uInt32 nExtendedPointCount( nPointCount +
959cdf0e10cSrcweir                                               aPolygon.isClosed()*aPolygon.areControlPointsUsed() );
960cdf0e10cSrcweir 
961cdf0e10cSrcweir 	    if( nPointCount > 1) {
962cdf0e10cSrcweir 		bool bIsBezier = aPolygon.areControlPointsUsed();
963cdf0e10cSrcweir         bool bIsRectangle = ::basegfx::tools::isRectangle( aPolygon );
964cdf0e10cSrcweir 		::basegfx::B2DPoint aA, aB, aP;
965cdf0e10cSrcweir 
966cdf0e10cSrcweir 		for( sal_uInt32 j=0; j < nExtendedPointCount; j++ ) {
967cdf0e10cSrcweir 		    aP = aPolygon.getB2DPoint( j % nPointCount );
968cdf0e10cSrcweir 
969cdf0e10cSrcweir 		    nX = aP.getX();
970cdf0e10cSrcweir 		    nY = aP.getY();
971cdf0e10cSrcweir 		    cairo_matrix_transform_point( &aOrigMatrix, &nX, &nY );
972cdf0e10cSrcweir 
973cdf0e10cSrcweir  		    if( ! bIsBezier && (bIsRectangle || aOperation == Clip) ) {
974cdf0e10cSrcweir                 nX = basegfx::fround( nX );
975cdf0e10cSrcweir                 nY = basegfx::fround( nY );
976cdf0e10cSrcweir 		    }
977cdf0e10cSrcweir 
978cdf0e10cSrcweir 		    if( aOperation == Stroke ) {
979cdf0e10cSrcweir                 nX += 0.5;
980cdf0e10cSrcweir                 nY += 0.5;
981cdf0e10cSrcweir 		    }
982cdf0e10cSrcweir 
983cdf0e10cSrcweir             if( j==0 )
984cdf0e10cSrcweir             {
985cdf0e10cSrcweir                 cairo_move_to( pCairo, nX, nY );
986cdf0e10cSrcweir                 OSL_TRACE( "move to %f,%f", nX, nY );
987cdf0e10cSrcweir             }
988cdf0e10cSrcweir             else {
989cdf0e10cSrcweir                 if( bIsBezier ) {
990cdf0e10cSrcweir                     aA = aPolygon.getNextControlPoint( (j-1) % nPointCount );
991cdf0e10cSrcweir                     aB = aPolygon.getPrevControlPoint( j % nPointCount );
992cdf0e10cSrcweir 
993cdf0e10cSrcweir                     nAX = aA.getX();
994cdf0e10cSrcweir                     nAY = aA.getY();
995cdf0e10cSrcweir                     nBX = aB.getX();
996cdf0e10cSrcweir                     nBY = aB.getY();
997cdf0e10cSrcweir 
998cdf0e10cSrcweir                     if( aOperation == Stroke ) {
999cdf0e10cSrcweir                         nAX += 0.5;
1000cdf0e10cSrcweir                         nAY += 0.5;
1001cdf0e10cSrcweir                         nBX += 0.5;
1002cdf0e10cSrcweir                         nBY += 0.5;
1003cdf0e10cSrcweir                     }
1004cdf0e10cSrcweir                     cairo_matrix_transform_point( &aOrigMatrix, &nAX, &nAY );
1005cdf0e10cSrcweir                     cairo_matrix_transform_point( &aOrigMatrix, &nBX, &nBY );
1006cdf0e10cSrcweir                     cairo_curve_to( pCairo, nAX, nAY, nBX, nBY, nX, nY );
1007cdf0e10cSrcweir                 } else {
1008cdf0e10cSrcweir                     cairo_line_to( pCairo, nX, nY );
1009cdf0e10cSrcweir                     OSL_TRACE( "line to %f,%f", nX, nY );
1010cdf0e10cSrcweir                 }
1011cdf0e10cSrcweir                 bOpToDo = true;
1012cdf0e10cSrcweir             }
1013cdf0e10cSrcweir 		}
1014cdf0e10cSrcweir 
1015cdf0e10cSrcweir 		if( aPolygon.isClosed() )
1016cdf0e10cSrcweir 		    cairo_close_path( pCairo );
1017cdf0e10cSrcweir 
1018cdf0e10cSrcweir 		if( aOperation == Fill && pTextures ) {
1019cdf0e10cSrcweir 		    cairo_set_matrix( pCairo, &aOrigMatrix );
1020cdf0e10cSrcweir 		    doOperation( aOperation, pCairo, pTextures, pDevice, aPolyPolygon.getB2DRange() );
1021cdf0e10cSrcweir 		    cairo_set_matrix( pCairo, &aIdentityMatrix );
1022cdf0e10cSrcweir 		}
1023cdf0e10cSrcweir 	    } else {
1024cdf0e10cSrcweir 		OSL_TRACE( "empty polygon for op: %d\n", aOperation );
1025cdf0e10cSrcweir 		if( aOperation == Clip ) {
1026cdf0e10cSrcweir 		    clipNULL( pCairo );
1027cdf0e10cSrcweir 
1028cdf0e10cSrcweir 		    return;
1029cdf0e10cSrcweir 		}
1030cdf0e10cSrcweir 	    }
1031cdf0e10cSrcweir 	}
1032cdf0e10cSrcweir 	if( bOpToDo && ( aOperation != Fill || !pTextures ) )
1033cdf0e10cSrcweir 	    doOperation( aOperation, pCairo, pTextures, pDevice, aPolyPolygon.getB2DRange() );
1034cdf0e10cSrcweir 
1035cdf0e10cSrcweir 	cairo_set_matrix( pCairo, &aOrigMatrix );
1036cdf0e10cSrcweir 
1037cdf0e10cSrcweir 	if( aPolyPolygon.count() == 0 && aOperation == Clip )
1038cdf0e10cSrcweir  	    clipNULL( pCairo );
1039cdf0e10cSrcweir     }
1040cdf0e10cSrcweir 
doPolyPolygonPath(const uno::Reference<rendering::XPolyPolygon2D> & xPolyPolygon,Operation aOperation,bool bNoLineJoin,const uno::Sequence<rendering::Texture> * pTextures,Cairo * pCairo) const1041cdf0e10cSrcweir     void CanvasHelper::doPolyPolygonPath( const uno::Reference< rendering::XPolyPolygon2D >& xPolyPolygon,
1042cdf0e10cSrcweir 					    Operation aOperation,
1043cdf0e10cSrcweir                         bool bNoLineJoin,
1044cdf0e10cSrcweir 					    const uno::Sequence< rendering::Texture >* pTextures,
1045cdf0e10cSrcweir 					    Cairo* pCairo ) const
1046cdf0e10cSrcweir     {
1047cdf0e10cSrcweir         const ::basegfx::B2DPolyPolygon& rPolyPoly(
1048cdf0e10cSrcweir             ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(xPolyPolygon) );
1049cdf0e10cSrcweir 
1050cdf0e10cSrcweir         if( !pCairo )
1051cdf0e10cSrcweir             pCairo = mpCairo.get();
1052cdf0e10cSrcweir 
1053cdf0e10cSrcweir         if(bNoLineJoin && Stroke == aOperation)
1054cdf0e10cSrcweir         {
1055cdf0e10cSrcweir             // emulate rendering::PathJoinType::NONE by painting single edges
1056cdf0e10cSrcweir             for(sal_uInt32 a(0); a < rPolyPoly.count(); a++)
1057cdf0e10cSrcweir             {
1058cdf0e10cSrcweir                 const basegfx::B2DPolygon aCandidate(rPolyPoly.getB2DPolygon(a));
1059cdf0e10cSrcweir                 const sal_uInt32 nPointCount(aCandidate.count());
1060cdf0e10cSrcweir 
1061cdf0e10cSrcweir                 if(nPointCount)
1062cdf0e10cSrcweir                 {
1063cdf0e10cSrcweir                     const sal_uInt32 nEdgeCount(aCandidate.isClosed() ? nPointCount + 1: nPointCount);
1064cdf0e10cSrcweir                     basegfx::B2DPolygon aEdge;
1065cdf0e10cSrcweir                     aEdge.append(aCandidate.getB2DPoint(0));
1066cdf0e10cSrcweir                     aEdge.append(basegfx::B2DPoint(0.0, 0.0));
1067cdf0e10cSrcweir 
1068cdf0e10cSrcweir                     for(sal_uInt32 b(0); b < nEdgeCount; b++)
1069cdf0e10cSrcweir 					{
1070cdf0e10cSrcweir 						const sal_uInt32 nNextIndex((b + 1) % nPointCount);
1071cdf0e10cSrcweir                         aEdge.setB2DPoint(1, aCandidate.getB2DPoint(nNextIndex));
1072cdf0e10cSrcweir                         aEdge.setNextControlPoint(0, aCandidate.getNextControlPoint(b));
1073cdf0e10cSrcweir                         aEdge.setPrevControlPoint(1, aCandidate.getPrevControlPoint(nNextIndex));
1074cdf0e10cSrcweir 
1075cdf0e10cSrcweir                         doPolyPolygonImplementation( basegfx::B2DPolyPolygon(aEdge),
1076cdf0e10cSrcweir                                                      aOperation,
1077cdf0e10cSrcweir                                                      pCairo, pTextures,
1078cdf0e10cSrcweir                                                      mpSurfaceProvider,
1079cdf0e10cSrcweir                                                      xPolyPolygon->getFillRule() );
1080cdf0e10cSrcweir 
1081cdf0e10cSrcweir                         // prepare next step
1082cdf0e10cSrcweir                         aEdge.setB2DPoint(0, aEdge.getB2DPoint(1));
1083cdf0e10cSrcweir 					}
1084cdf0e10cSrcweir                 }
1085cdf0e10cSrcweir             }
1086cdf0e10cSrcweir         }
1087cdf0e10cSrcweir         else
1088cdf0e10cSrcweir         {
1089cdf0e10cSrcweir             doPolyPolygonImplementation( rPolyPoly, aOperation,
1090cdf0e10cSrcweir                                          pCairo, pTextures,
1091cdf0e10cSrcweir                                          mpSurfaceProvider,
1092cdf0e10cSrcweir                                          xPolyPolygon->getFillRule() );
1093cdf0e10cSrcweir         }
1094cdf0e10cSrcweir     }
1095cdf0e10cSrcweir 
drawPolyPolygon(const rendering::XCanvas *,const uno::Reference<rendering::XPolyPolygon2D> & xPolyPolygon,const rendering::ViewState & viewState,const rendering::RenderState & renderState)1096cdf0e10cSrcweir     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::drawPolyPolygon( const rendering::XCanvas* 							,
1097cdf0e10cSrcweir                                                                                  const uno::Reference< rendering::XPolyPolygon2D >& xPolyPolygon,
1098cdf0e10cSrcweir                                                                                  const rendering::ViewState& 						viewState,
1099cdf0e10cSrcweir                                                                                  const rendering::RenderState& 						renderState )
1100cdf0e10cSrcweir     {
1101cdf0e10cSrcweir #ifdef CAIRO_CANVAS_PERF_TRACE
1102cdf0e10cSrcweir         struct timespec aTimer;
1103cdf0e10cSrcweir         mxDevice->startPerfTrace( &aTimer );
1104cdf0e10cSrcweir #endif
1105cdf0e10cSrcweir 
1106cdf0e10cSrcweir         if( mpCairo ) {
1107cdf0e10cSrcweir             cairo_save( mpCairo.get() );
1108cdf0e10cSrcweir 
1109cdf0e10cSrcweir             cairo_set_line_width( mpCairo.get(), 1 );
1110cdf0e10cSrcweir 
1111cdf0e10cSrcweir             useStates( viewState, renderState, true );
1112cdf0e10cSrcweir             doPolyPolygonPath( xPolyPolygon, Stroke );
1113cdf0e10cSrcweir 
1114cdf0e10cSrcweir             cairo_restore( mpCairo.get() );
1115cdf0e10cSrcweir         } else
1116cdf0e10cSrcweir             OSL_TRACE ("CanvasHelper called after it was disposed");
1117cdf0e10cSrcweir 
1118cdf0e10cSrcweir #ifdef CAIRO_CANVAS_PERF_TRACE
1119cdf0e10cSrcweir         mxDevice->stopPerfTrace( &aTimer, "drawPolyPolygon" );
1120cdf0e10cSrcweir #endif
1121cdf0e10cSrcweir 
1122cdf0e10cSrcweir         return uno::Reference< rendering::XCachedPrimitive >(NULL);
1123cdf0e10cSrcweir     }
1124cdf0e10cSrcweir 
strokePolyPolygon(const rendering::XCanvas *,const uno::Reference<rendering::XPolyPolygon2D> & xPolyPolygon,const rendering::ViewState & viewState,const rendering::RenderState & renderState,const rendering::StrokeAttributes & strokeAttributes)1125cdf0e10cSrcweir     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::strokePolyPolygon( const rendering::XCanvas* 							,
1126cdf0e10cSrcweir                                                                                    const uno::Reference< rendering::XPolyPolygon2D >& 	xPolyPolygon,
1127cdf0e10cSrcweir                                                                                    const rendering::ViewState& 							viewState,
1128cdf0e10cSrcweir                                                                                    const rendering::RenderState& 						renderState,
1129cdf0e10cSrcweir                                                                                    const rendering::StrokeAttributes& 					strokeAttributes )
1130cdf0e10cSrcweir     {
1131cdf0e10cSrcweir 	#ifdef CAIRO_CANVAS_PERF_TRACE
1132cdf0e10cSrcweir 	struct timespec aTimer;
1133cdf0e10cSrcweir 	mxDevice->startPerfTrace( &aTimer );
1134cdf0e10cSrcweir         #endif
1135cdf0e10cSrcweir 
1136cdf0e10cSrcweir 	if( mpCairo ) {
1137cdf0e10cSrcweir 	    cairo_save( mpCairo.get() );
1138cdf0e10cSrcweir 
1139cdf0e10cSrcweir 	    useStates( viewState, renderState, true );
1140cdf0e10cSrcweir 
1141cdf0e10cSrcweir         Matrix aMatrix;
1142cdf0e10cSrcweir         double w = strokeAttributes.StrokeWidth, h = 0;
1143cdf0e10cSrcweir         cairo_get_matrix( mpCairo.get(), &aMatrix );
1144cdf0e10cSrcweir         cairo_matrix_transform_distance( &aMatrix, &w, &h );
1145cdf0e10cSrcweir  	    cairo_set_line_width( mpCairo.get(), w );
1146cdf0e10cSrcweir 
1147cdf0e10cSrcweir 	    cairo_set_miter_limit( mpCairo.get(), strokeAttributes.MiterLimit );
1148cdf0e10cSrcweir 
1149cdf0e10cSrcweir 	    // FIXME: cairo doesn't handle end cap so far (rodo)
1150cdf0e10cSrcweir 	    switch( strokeAttributes.StartCapType ) {
1151cdf0e10cSrcweir             case rendering::PathCapType::BUTT:
1152cdf0e10cSrcweir                 cairo_set_line_cap( mpCairo.get(), CAIRO_LINE_CAP_BUTT );
1153cdf0e10cSrcweir                 break;
1154cdf0e10cSrcweir             case rendering::PathCapType::ROUND:
1155cdf0e10cSrcweir                 cairo_set_line_cap( mpCairo.get(), CAIRO_LINE_CAP_ROUND );
1156cdf0e10cSrcweir                 break;
1157cdf0e10cSrcweir             case rendering::PathCapType::SQUARE:
1158cdf0e10cSrcweir                 cairo_set_line_cap( mpCairo.get(), CAIRO_LINE_CAP_SQUARE );
1159cdf0e10cSrcweir                 break;
1160cdf0e10cSrcweir 	    }
1161cdf0e10cSrcweir 
1162cdf0e10cSrcweir         bool bNoLineJoin(false);
1163cdf0e10cSrcweir 
1164cdf0e10cSrcweir 	    switch( strokeAttributes.JoinType ) {
1165cdf0e10cSrcweir             // cairo doesn't have join type NONE so we use MITER as it's pretty close
1166cdf0e10cSrcweir             case rendering::PathJoinType::NONE:
1167cdf0e10cSrcweir                 bNoLineJoin = true;
1168cdf0e10cSrcweir             case rendering::PathJoinType::MITER:
1169cdf0e10cSrcweir                 cairo_set_line_join( mpCairo.get(), CAIRO_LINE_JOIN_MITER );
1170cdf0e10cSrcweir                 break;
1171cdf0e10cSrcweir             case rendering::PathJoinType::ROUND:
1172cdf0e10cSrcweir                 cairo_set_line_join( mpCairo.get(), CAIRO_LINE_JOIN_ROUND );
1173cdf0e10cSrcweir                 break;
1174cdf0e10cSrcweir             case rendering::PathJoinType::BEVEL:
1175cdf0e10cSrcweir                 cairo_set_line_join( mpCairo.get(), CAIRO_LINE_JOIN_BEVEL );
1176cdf0e10cSrcweir                 break;
1177cdf0e10cSrcweir 	    }
1178cdf0e10cSrcweir 
1179cdf0e10cSrcweir 	    if( strokeAttributes.DashArray.getLength() > 0 ) {
1180cdf0e10cSrcweir             double* pDashArray = new double[ strokeAttributes.DashArray.getLength() ];
1181cdf0e10cSrcweir             for( sal_Int32 i=0; i<strokeAttributes.DashArray.getLength(); i++ )
1182cdf0e10cSrcweir                 pDashArray[i]=strokeAttributes.DashArray[i];
1183cdf0e10cSrcweir             cairo_set_dash( mpCairo.get(), pDashArray, strokeAttributes.DashArray.getLength(), 0 );
1184cdf0e10cSrcweir             delete[] pDashArray;
1185cdf0e10cSrcweir 	    }
1186cdf0e10cSrcweir 
1187cdf0e10cSrcweir 	    // TODO(rodo) use LineArray of strokeAttributes
1188cdf0e10cSrcweir 
1189cdf0e10cSrcweir 	    doPolyPolygonPath( xPolyPolygon, Stroke, bNoLineJoin );
1190cdf0e10cSrcweir 
1191cdf0e10cSrcweir 	    cairo_restore( mpCairo.get() );
1192cdf0e10cSrcweir 	} else
1193cdf0e10cSrcweir 	    OSL_TRACE ("CanvasHelper called after it was disposed");
1194cdf0e10cSrcweir 
1195cdf0e10cSrcweir #ifdef CAIRO_CANVAS_PERF_TRACE
1196cdf0e10cSrcweir 	    mxDevice->stopPerfTrace( &aTimer, "strokePolyPolygon" );
1197cdf0e10cSrcweir #endif
1198cdf0e10cSrcweir 
1199cdf0e10cSrcweir         // TODO(P1): Provide caching here.
1200cdf0e10cSrcweir         return uno::Reference< rendering::XCachedPrimitive >(NULL);
1201cdf0e10cSrcweir     }
1202cdf0e10cSrcweir 
strokeTexturedPolyPolygon(const rendering::XCanvas *,const uno::Reference<rendering::XPolyPolygon2D> &,const rendering::ViewState &,const rendering::RenderState &,const uno::Sequence<rendering::Texture> &,const rendering::StrokeAttributes &)1203cdf0e10cSrcweir     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::strokeTexturedPolyPolygon( const rendering::XCanvas* 							,
1204cdf0e10cSrcweir                                                                                            const uno::Reference< rendering::XPolyPolygon2D >& 	/*xPolyPolygon*/,
1205cdf0e10cSrcweir                                                                                            const rendering::ViewState& 							/*viewState*/,
1206cdf0e10cSrcweir                                                                                            const rendering::RenderState& 						/*renderState*/,
1207cdf0e10cSrcweir                                                                                            const uno::Sequence< rendering::Texture >& 			/*textures*/,
1208cdf0e10cSrcweir                                                                                            const rendering::StrokeAttributes& 					/*strokeAttributes*/ )
1209cdf0e10cSrcweir     {
1210cdf0e10cSrcweir         // TODO
1211cdf0e10cSrcweir         return uno::Reference< rendering::XCachedPrimitive >(NULL);
1212cdf0e10cSrcweir     }
1213cdf0e10cSrcweir 
strokeTextureMappedPolyPolygon(const rendering::XCanvas *,const uno::Reference<rendering::XPolyPolygon2D> &,const rendering::ViewState &,const rendering::RenderState &,const uno::Sequence<rendering::Texture> &,const uno::Reference<geometry::XMapping2D> &,const rendering::StrokeAttributes &)1214cdf0e10cSrcweir     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::strokeTextureMappedPolyPolygon( const rendering::XCanvas* 							,
1215cdf0e10cSrcweir                                                                                                 const uno::Reference< rendering::XPolyPolygon2D >&	/*xPolyPolygon*/,
1216cdf0e10cSrcweir                                                                                                 const rendering::ViewState& 						/*viewState*/,
1217cdf0e10cSrcweir                                                                                                 const rendering::RenderState& 						/*renderState*/,
1218cdf0e10cSrcweir                                                                                                 const uno::Sequence< rendering::Texture >& 			/*textures*/,
1219cdf0e10cSrcweir                                                                                                 const uno::Reference< geometry::XMapping2D >& 		/*xMapping*/,
1220cdf0e10cSrcweir                                                                                                 const rendering::StrokeAttributes& 					/*strokeAttributes*/ )
1221cdf0e10cSrcweir     {
1222cdf0e10cSrcweir         // TODO
1223cdf0e10cSrcweir         return uno::Reference< rendering::XCachedPrimitive >(NULL);
1224cdf0e10cSrcweir     }
1225cdf0e10cSrcweir 
queryStrokeShapes(const rendering::XCanvas *,const uno::Reference<rendering::XPolyPolygon2D> &,const rendering::ViewState &,const rendering::RenderState &,const rendering::StrokeAttributes &)1226cdf0e10cSrcweir     uno::Reference< rendering::XPolyPolygon2D >   CanvasHelper::queryStrokeShapes( const rendering::XCanvas* 							,
1227cdf0e10cSrcweir                                                                                    const uno::Reference< rendering::XPolyPolygon2D >& 	/*xPolyPolygon*/,
1228cdf0e10cSrcweir                                                                                    const rendering::ViewState& 							/*viewState*/,
1229cdf0e10cSrcweir                                                                                    const rendering::RenderState& 						/*renderState*/,
1230cdf0e10cSrcweir                                                                                    const rendering::StrokeAttributes& 					/*strokeAttributes*/ )
1231cdf0e10cSrcweir     {
1232cdf0e10cSrcweir         // TODO
1233cdf0e10cSrcweir         return uno::Reference< rendering::XPolyPolygon2D >(NULL);
1234cdf0e10cSrcweir     }
1235cdf0e10cSrcweir 
fillPolyPolygon(const rendering::XCanvas *,const uno::Reference<rendering::XPolyPolygon2D> & xPolyPolygon,const rendering::ViewState & viewState,const rendering::RenderState & renderState)1236cdf0e10cSrcweir     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::fillPolyPolygon( const rendering::XCanvas* 							,
1237cdf0e10cSrcweir                                                                                  const uno::Reference< rendering::XPolyPolygon2D >& xPolyPolygon,
1238cdf0e10cSrcweir                                                                                  const rendering::ViewState& 						viewState,
1239cdf0e10cSrcweir                                                                                  const rendering::RenderState& 						renderState )
1240cdf0e10cSrcweir     {
1241cdf0e10cSrcweir 	#ifdef CAIRO_CANVAS_PERF_TRACE
1242cdf0e10cSrcweir 	struct timespec aTimer;
1243cdf0e10cSrcweir 	mxDevice->startPerfTrace( &aTimer );
1244cdf0e10cSrcweir         #endif
1245cdf0e10cSrcweir 
1246cdf0e10cSrcweir 	if( mpCairo ) {
1247cdf0e10cSrcweir 	    cairo_save( mpCairo.get() );
1248cdf0e10cSrcweir 
1249cdf0e10cSrcweir 	    useStates( viewState, renderState, true );
1250cdf0e10cSrcweir 	    doPolyPolygonPath( xPolyPolygon, Fill );
1251cdf0e10cSrcweir 
1252cdf0e10cSrcweir 	    cairo_restore( mpCairo.get() );
1253cdf0e10cSrcweir 	} else
1254cdf0e10cSrcweir 	    OSL_TRACE ("CanvasHelper called after it was disposed");
1255cdf0e10cSrcweir 
1256cdf0e10cSrcweir 	#ifdef CAIRO_CANVAS_PERF_TRACE
1257cdf0e10cSrcweir 	mxDevice->stopPerfTrace( &aTimer, "fillPolyPolygon" );
1258cdf0e10cSrcweir         #endif
1259cdf0e10cSrcweir 
1260cdf0e10cSrcweir         return uno::Reference< rendering::XCachedPrimitive >(NULL);
1261cdf0e10cSrcweir     }
1262cdf0e10cSrcweir 
fillTexturedPolyPolygon(const rendering::XCanvas *,const uno::Reference<rendering::XPolyPolygon2D> & xPolyPolygon,const rendering::ViewState & viewState,const rendering::RenderState & renderState,const uno::Sequence<rendering::Texture> & textures)1263cdf0e10cSrcweir     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::fillTexturedPolyPolygon( const rendering::XCanvas* 							,
1264cdf0e10cSrcweir                                                                                          const uno::Reference< rendering::XPolyPolygon2D >& xPolyPolygon,
1265cdf0e10cSrcweir                                                                                          const rendering::ViewState& 						viewState,
1266cdf0e10cSrcweir                                                                                          const rendering::RenderState& 						renderState,
1267cdf0e10cSrcweir                                                                                          const uno::Sequence< rendering::Texture >& 		textures )
1268cdf0e10cSrcweir     {
1269cdf0e10cSrcweir 	if( mpCairo ) {
1270cdf0e10cSrcweir 	    cairo_save( mpCairo.get() );
1271cdf0e10cSrcweir 
1272cdf0e10cSrcweir 	    useStates( viewState, renderState, true );
1273cdf0e10cSrcweir 	    doPolyPolygonPath( xPolyPolygon, Fill, false, &textures );
1274cdf0e10cSrcweir 
1275cdf0e10cSrcweir 	    cairo_restore( mpCairo.get() );
1276cdf0e10cSrcweir 	}
1277cdf0e10cSrcweir 
1278cdf0e10cSrcweir         return uno::Reference< rendering::XCachedPrimitive >(NULL);
1279cdf0e10cSrcweir     }
1280cdf0e10cSrcweir 
fillTextureMappedPolyPolygon(const rendering::XCanvas *,const uno::Reference<rendering::XPolyPolygon2D> &,const rendering::ViewState &,const rendering::RenderState &,const uno::Sequence<rendering::Texture> &,const uno::Reference<geometry::XMapping2D> &)1281cdf0e10cSrcweir     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::fillTextureMappedPolyPolygon( const rendering::XCanvas* 							,
1282cdf0e10cSrcweir                                                                                               const uno::Reference< rendering::XPolyPolygon2D >& 	/*xPolyPolygon*/,
1283cdf0e10cSrcweir                                                                                               const rendering::ViewState& 							/*viewState*/,
1284cdf0e10cSrcweir                                                                                               const rendering::RenderState& 						/*renderState*/,
1285cdf0e10cSrcweir                                                                                               const uno::Sequence< rendering::Texture >& 			/*textures*/,
1286cdf0e10cSrcweir                                                                                               const uno::Reference< geometry::XMapping2D >& 		/*xMapping*/ )
1287cdf0e10cSrcweir     {
1288cdf0e10cSrcweir         // TODO
1289cdf0e10cSrcweir         return uno::Reference< rendering::XCachedPrimitive >(NULL);
1290cdf0e10cSrcweir     }
1291cdf0e10cSrcweir 
implDrawBitmapSurface(const rendering::XCanvas * pCanvas,const SurfaceSharedPtr & pInputSurface,const rendering::ViewState & viewState,const rendering::RenderState & renderState,const geometry::IntegerSize2D & rSize,bool bModulateColors,bool bHasAlpha)1292cdf0e10cSrcweir     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::implDrawBitmapSurface( const rendering::XCanvas* 	    pCanvas,
1293cdf0e10cSrcweir                                                                                        const SurfaceSharedPtr&        pInputSurface,
1294cdf0e10cSrcweir 																					   const rendering::ViewState&      viewState,
1295cdf0e10cSrcweir 																					   const rendering::RenderState&    renderState,
1296cdf0e10cSrcweir 																					   const geometry::IntegerSize2D&   rSize,
1297cdf0e10cSrcweir 																					   bool bModulateColors,
1298cdf0e10cSrcweir 																					   bool bHasAlpha )
1299cdf0e10cSrcweir     {
1300cdf0e10cSrcweir         SurfaceSharedPtr pSurface=pInputSurface;
1301cdf0e10cSrcweir 		uno::Reference< rendering::XCachedPrimitive > rv = uno::Reference< rendering::XCachedPrimitive >(NULL);
1302cdf0e10cSrcweir         geometry::IntegerSize2D aBitmapSize = rSize;
1303cdf0e10cSrcweir 
1304cdf0e10cSrcweir 		if( mpCairo ) {
1305cdf0e10cSrcweir 			cairo_save( mpCairo.get() );
1306cdf0e10cSrcweir 
1307cdf0e10cSrcweir 			cairo_rectangle( mpCairo.get(), 0, 0, maSize.getX(), maSize.getY() );
1308cdf0e10cSrcweir 			cairo_clip( mpCairo.get() );
1309cdf0e10cSrcweir 
1310cdf0e10cSrcweir 			useStates( viewState, renderState, true );
1311cdf0e10cSrcweir 
1312cdf0e10cSrcweir 			//   	    if( !bHasAlpha )
1313cdf0e10cSrcweir 			//   		cairo_set_operator( mpCairo.get(), CAIRO_OPERATOR_SOURCE );
1314cdf0e10cSrcweir 
1315cdf0e10cSrcweir 			Matrix aMatrix;
1316cdf0e10cSrcweir 
1317cdf0e10cSrcweir 			cairo_get_matrix( mpCairo.get(), &aMatrix );
1318cdf0e10cSrcweir 			if( ! ::rtl::math::approxEqual( aMatrix.xx, 1 ) &&
1319cdf0e10cSrcweir 				! ::rtl::math::approxEqual( aMatrix.yy, 1 ) &&
1320cdf0e10cSrcweir 				::rtl::math::approxEqual( aMatrix.x0, 0 ) &&
1321cdf0e10cSrcweir 				::rtl::math::approxEqual( aMatrix.y0, 0 ) &&
1322cdf0e10cSrcweir 				basegfx::fround( rSize.Width * aMatrix.xx ) > 8 &&
1323cdf0e10cSrcweir 				basegfx::fround( rSize.Height* aMatrix.yy ) > 8 )
1324cdf0e10cSrcweir             {
1325cdf0e10cSrcweir                 double dWidth, dHeight;
1326cdf0e10cSrcweir 
1327cdf0e10cSrcweir                 dWidth = basegfx::fround( rSize.Width * aMatrix.xx );
1328cdf0e10cSrcweir                 dHeight = basegfx::fround( rSize.Height* aMatrix.yy );
1329cdf0e10cSrcweir                 aBitmapSize.Width = static_cast<sal_Int32>( dWidth );
1330cdf0e10cSrcweir                 aBitmapSize.Height = static_cast<sal_Int32>( dHeight );
1331cdf0e10cSrcweir 
1332cdf0e10cSrcweir                 SurfaceSharedPtr pScaledSurface = mpSurfaceProvider->createSurface(
1333cdf0e10cSrcweir                     ::basegfx::B2ISize( aBitmapSize.Width, aBitmapSize.Height ),
1334cdf0e10cSrcweir                     bHasAlpha ? CAIRO_CONTENT_COLOR_ALPHA : CAIRO_CONTENT_COLOR );
1335cdf0e10cSrcweir                 CairoSharedPtr pCairo = pScaledSurface->getCairo();
1336cdf0e10cSrcweir 
1337cdf0e10cSrcweir                 cairo_set_operator( pCairo.get(), CAIRO_OPERATOR_SOURCE );
1338cdf0e10cSrcweir                 // add 0.5px to size to avoid rounding errors in cairo, leading sometimes to random data on the image right/bottom borders
1339cdf0e10cSrcweir                 cairo_scale( pCairo.get(), (dWidth+0.5)/rSize.Width, (dHeight+0.5)/rSize.Height );
1340cdf0e10cSrcweir                 cairo_set_source_surface( pCairo.get(), pSurface->getCairoSurface().get(), 0, 0 );
1341cdf0e10cSrcweir                 cairo_paint( pCairo.get() );
1342cdf0e10cSrcweir 
1343cdf0e10cSrcweir                 pSurface = pScaledSurface;
1344cdf0e10cSrcweir 
1345cdf0e10cSrcweir                 aMatrix.xx = aMatrix.yy = 1;
1346cdf0e10cSrcweir                 cairo_set_matrix( mpCairo.get(), &aMatrix );
1347cdf0e10cSrcweir 
1348cdf0e10cSrcweir                 rv = uno::Reference< rendering::XCachedPrimitive >(
1349cdf0e10cSrcweir                     new CachedBitmap( pSurface, viewState, renderState,
1350cdf0e10cSrcweir                                       // cast away const, need to
1351cdf0e10cSrcweir                                       // change refcount (as this is
1352cdf0e10cSrcweir                                       // ~invisible to client code,
1353cdf0e10cSrcweir                                       // still logically const)
1354cdf0e10cSrcweir                                       const_cast< rendering::XCanvas* >(pCanvas)) );
1355cdf0e10cSrcweir             }
1356cdf0e10cSrcweir 
1357cdf0e10cSrcweir 			if( !bHasAlpha && mbHaveAlpha )
1358cdf0e10cSrcweir             {
1359cdf0e10cSrcweir 				double x, y, width, height;
1360cdf0e10cSrcweir 
1361cdf0e10cSrcweir 				x = y = 0;
1362cdf0e10cSrcweir 				width = aBitmapSize.Width;
1363cdf0e10cSrcweir 				height = aBitmapSize.Height;
1364cdf0e10cSrcweir 				cairo_matrix_transform_point( &aMatrix, &x, &y );
1365cdf0e10cSrcweir 				cairo_matrix_transform_distance( &aMatrix, &width, &height );
1366cdf0e10cSrcweir 
1367cdf0e10cSrcweir 				// in case the bitmap doesn't have alpha and covers whole area
1368cdf0e10cSrcweir 				// we try to change surface to plain rgb
1369cdf0e10cSrcweir 				OSL_TRACE ("chance to change surface to rgb, %f, %f, %f x %f (%d x %d)", x, y, width, height, maSize.getX(), maSize.getY() );
1370cdf0e10cSrcweir 				if( x <= 0 && y <= 0 && x + width >= maSize.getX() && y + height >= maSize.getY() )
1371cdf0e10cSrcweir 				{
1372cdf0e10cSrcweir 					OSL_TRACE ("trying to change surface to rgb");
1373cdf0e10cSrcweir 					if( mpSurfaceProvider ) {
1374cdf0e10cSrcweir 						SurfaceSharedPtr pNewSurface = mpSurfaceProvider->changeSurface( false, false );
1375cdf0e10cSrcweir 
1376cdf0e10cSrcweir 						if( pNewSurface )
1377cdf0e10cSrcweir 							setSurface( pNewSurface, false );
1378cdf0e10cSrcweir 
1379cdf0e10cSrcweir 						// set state to new mpCairo.get()
1380cdf0e10cSrcweir 						useStates( viewState, renderState, true );
1381cdf0e10cSrcweir 						// use the possibly modified matrix
1382cdf0e10cSrcweir 						cairo_set_matrix( mpCairo.get(), &aMatrix );
1383cdf0e10cSrcweir 					}
1384cdf0e10cSrcweir 				}
1385cdf0e10cSrcweir 			}
1386cdf0e10cSrcweir 
1387cdf0e10cSrcweir 			cairo_set_source_surface( mpCairo.get(), pSurface->getCairoSurface().get(), 0, 0 );
1388cdf0e10cSrcweir  			if( !bHasAlpha &&
1389cdf0e10cSrcweir 				::rtl::math::approxEqual( aMatrix.xx, 1 ) &&
1390cdf0e10cSrcweir 				::rtl::math::approxEqual( aMatrix.yy, 1 ) &&
1391cdf0e10cSrcweir 				::rtl::math::approxEqual( aMatrix.x0, 0 ) &&
1392cdf0e10cSrcweir 				::rtl::math::approxEqual( aMatrix.y0, 0 ) )
1393cdf0e10cSrcweir  				cairo_set_operator( mpCairo.get(), CAIRO_OPERATOR_SOURCE );
1394cdf0e10cSrcweir 			cairo_pattern_set_extend( cairo_get_source(mpCairo.get()), CAIRO_EXTEND_PAD );
1395cdf0e10cSrcweir 			cairo_rectangle( mpCairo.get(), 0, 0, aBitmapSize.Width, aBitmapSize.Height );
1396cdf0e10cSrcweir 			cairo_clip( mpCairo.get() );
1397cdf0e10cSrcweir 
1398cdf0e10cSrcweir             if( bModulateColors )
1399cdf0e10cSrcweir                 cairo_paint_with_alpha( mpCairo.get(), renderState.DeviceColor[3] );
1400cdf0e10cSrcweir             else
1401cdf0e10cSrcweir                 cairo_paint( mpCairo.get() );
1402cdf0e10cSrcweir 			cairo_restore( mpCairo.get() );
1403cdf0e10cSrcweir 		} else
1404cdf0e10cSrcweir 			OSL_TRACE ("CanvasHelper called after it was disposed");
1405cdf0e10cSrcweir 
1406cdf0e10cSrcweir         return rv; // uno::Reference< rendering::XCachedPrimitive >(NULL);
1407cdf0e10cSrcweir     }
1408cdf0e10cSrcweir 
drawBitmap(const rendering::XCanvas * pCanvas,const uno::Reference<rendering::XBitmap> & xBitmap,const rendering::ViewState & viewState,const rendering::RenderState & renderState)1409cdf0e10cSrcweir     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::drawBitmap( const rendering::XCanvas* 					pCanvas,
1410cdf0e10cSrcweir                                                                             const uno::Reference< rendering::XBitmap >& xBitmap,
1411cdf0e10cSrcweir                                                                             const rendering::ViewState& 				viewState,
1412cdf0e10cSrcweir                                                                             const rendering::RenderState& 				renderState )
1413cdf0e10cSrcweir     {
1414cdf0e10cSrcweir 	#ifdef CAIRO_CANVAS_PERF_TRACE
1415cdf0e10cSrcweir 	struct timespec aTimer;
1416cdf0e10cSrcweir 	mxDevice->startPerfTrace( &aTimer );
1417cdf0e10cSrcweir         #endif
1418cdf0e10cSrcweir 
1419cdf0e10cSrcweir 	uno::Reference< rendering::XCachedPrimitive > rv;
1420cdf0e10cSrcweir 	unsigned char* data = NULL;
1421cdf0e10cSrcweir 	bool bHasAlpha = false;
1422cdf0e10cSrcweir 	SurfaceSharedPtr pSurface = surfaceFromXBitmap( xBitmap, mpSurfaceProvider, data, bHasAlpha );
1423cdf0e10cSrcweir 	geometry::IntegerSize2D aSize = xBitmap->getSize();
1424cdf0e10cSrcweir 
1425cdf0e10cSrcweir 	if( pSurface ) {
1426cdf0e10cSrcweir 	    rv = implDrawBitmapSurface( pCanvas, pSurface, viewState, renderState, aSize, false, bHasAlpha );
1427cdf0e10cSrcweir 
1428cdf0e10cSrcweir 	    if( data )
1429cdf0e10cSrcweir 		free( data );
1430cdf0e10cSrcweir 	} else
1431cdf0e10cSrcweir 	    rv = uno::Reference< rendering::XCachedPrimitive >(NULL);
1432cdf0e10cSrcweir 
1433cdf0e10cSrcweir 	#ifdef CAIRO_CANVAS_PERF_TRACE
1434cdf0e10cSrcweir 	mxDevice->stopPerfTrace( &aTimer, "drawBitmap" );
1435cdf0e10cSrcweir         #endif
1436cdf0e10cSrcweir 
1437cdf0e10cSrcweir 	return rv;
1438cdf0e10cSrcweir     }
1439cdf0e10cSrcweir 
drawBitmapModulated(const rendering::XCanvas * pCanvas,const uno::Reference<rendering::XBitmap> & xBitmap,const rendering::ViewState & viewState,const rendering::RenderState & renderState)1440cdf0e10cSrcweir     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::drawBitmapModulated( const rendering::XCanvas* 						pCanvas,
1441cdf0e10cSrcweir                                                                                      const uno::Reference< rendering::XBitmap >& 	xBitmap,
1442cdf0e10cSrcweir                                                                                      const rendering::ViewState& 					viewState,
1443cdf0e10cSrcweir                                                                                      const rendering::RenderState& 					renderState )
1444cdf0e10cSrcweir     {
1445cdf0e10cSrcweir #ifdef CAIRO_CANVAS_PERF_TRACE
1446cdf0e10cSrcweir         struct timespec aTimer;
1447cdf0e10cSrcweir         mxDevice->startPerfTrace( &aTimer );
1448cdf0e10cSrcweir #endif
1449cdf0e10cSrcweir 
1450cdf0e10cSrcweir         uno::Reference< rendering::XCachedPrimitive > rv;
1451cdf0e10cSrcweir         unsigned char* data = NULL;
1452cdf0e10cSrcweir         bool bHasAlpha = false;
1453cdf0e10cSrcweir         SurfaceSharedPtr pSurface = surfaceFromXBitmap( xBitmap, mpSurfaceProvider, data, bHasAlpha );
1454cdf0e10cSrcweir         geometry::IntegerSize2D aSize = xBitmap->getSize();
1455cdf0e10cSrcweir 
1456cdf0e10cSrcweir         if( pSurface ) {
1457cdf0e10cSrcweir             rv = implDrawBitmapSurface( pCanvas, pSurface, viewState, renderState, aSize, true, bHasAlpha );
1458cdf0e10cSrcweir 
1459cdf0e10cSrcweir             if( data )
1460cdf0e10cSrcweir                 free( data );
1461cdf0e10cSrcweir         } else
1462cdf0e10cSrcweir             rv = uno::Reference< rendering::XCachedPrimitive >(NULL);
1463cdf0e10cSrcweir 
1464cdf0e10cSrcweir #ifdef CAIRO_CANVAS_PERF_TRACE
1465cdf0e10cSrcweir         mxDevice->stopPerfTrace( &aTimer, "drawBitmap" );
1466cdf0e10cSrcweir #endif
1467cdf0e10cSrcweir 
1468cdf0e10cSrcweir         return rv;
1469cdf0e10cSrcweir     }
1470cdf0e10cSrcweir 
getDevice()1471cdf0e10cSrcweir     uno::Reference< rendering::XGraphicDevice > CanvasHelper::getDevice()
1472cdf0e10cSrcweir     {
1473cdf0e10cSrcweir         return uno::Reference< rendering::XGraphicDevice >(mpDevice);
1474cdf0e10cSrcweir     }
1475cdf0e10cSrcweir 
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 &)1476cdf0e10cSrcweir     void CanvasHelper::copyRect( const rendering::XCanvas* 							,
1477cdf0e10cSrcweir                                  const uno::Reference< rendering::XBitmapCanvas >&	/*sourceCanvas*/,
1478cdf0e10cSrcweir                                  const geometry::RealRectangle2D& 					/*sourceRect*/,
1479cdf0e10cSrcweir                                  const rendering::ViewState& 						/*sourceViewState*/,
1480cdf0e10cSrcweir                                  const rendering::RenderState& 						/*sourceRenderState*/,
1481cdf0e10cSrcweir                                  const geometry::RealRectangle2D& 					/*destRect*/,
1482cdf0e10cSrcweir                                  const rendering::ViewState& 						/*destViewState*/,
1483cdf0e10cSrcweir                                  const rendering::RenderState& 						/*destRenderState*/ )
1484cdf0e10cSrcweir     {
1485cdf0e10cSrcweir         // TODO(F2): copyRect NYI
1486cdf0e10cSrcweir     }
1487cdf0e10cSrcweir 
getSize()1488cdf0e10cSrcweir     geometry::IntegerSize2D CanvasHelper::getSize()
1489cdf0e10cSrcweir     {
1490cdf0e10cSrcweir         if( !mpSurfaceProvider )
1491cdf0e10cSrcweir             geometry::IntegerSize2D(1, 1); // we're disposed
1492cdf0e10cSrcweir 
1493cdf0e10cSrcweir         return ::basegfx::unotools::integerSize2DFromB2ISize( maSize );
1494cdf0e10cSrcweir     }
1495cdf0e10cSrcweir 
getScaledBitmap(const geometry::RealSize2D & newSize,sal_Bool)1496cdf0e10cSrcweir     uno::Reference< rendering::XBitmap > CanvasHelper::getScaledBitmap( const geometry::RealSize2D&	newSize,
1497cdf0e10cSrcweir                                                                         sal_Bool 					/*beFast*/ )
1498cdf0e10cSrcweir     {
1499cdf0e10cSrcweir #ifdef CAIRO_CANVAS_PERF_TRACE
1500cdf0e10cSrcweir 		struct timespec aTimer;
1501cdf0e10cSrcweir 		mxDevice->startPerfTrace( &aTimer );
1502cdf0e10cSrcweir #endif
1503cdf0e10cSrcweir 
1504cdf0e10cSrcweir 		if( mpCairo ) {
1505cdf0e10cSrcweir 			return uno::Reference< rendering::XBitmap >( new CanvasBitmap( ::basegfx::B2ISize( ::canvas::tools::roundUp( newSize.Width ),
1506cdf0e10cSrcweir 																							   ::canvas::tools::roundUp( newSize.Height ) ),
1507cdf0e10cSrcweir 																		   mpSurfaceProvider, mpDevice, false ) );
1508cdf0e10cSrcweir 		} else
1509cdf0e10cSrcweir 			OSL_TRACE ("CanvasHelper called after it was disposed");
1510cdf0e10cSrcweir 
1511cdf0e10cSrcweir #ifdef CAIRO_CANVAS_PERF_TRACE
1512cdf0e10cSrcweir 		mxDevice->stopPerfTrace( &aTimer, "getScaledBitmap" );
1513cdf0e10cSrcweir #endif
1514cdf0e10cSrcweir 
1515cdf0e10cSrcweir 		return uno::Reference< rendering::XBitmap >();
1516cdf0e10cSrcweir     }
1517cdf0e10cSrcweir 
getData(rendering::IntegerBitmapLayout & aLayout,const geometry::IntegerRectangle2D & rect)1518cdf0e10cSrcweir     uno::Sequence< sal_Int8 > CanvasHelper::getData( rendering::IntegerBitmapLayout&     aLayout,
1519cdf0e10cSrcweir                                                      const geometry::IntegerRectangle2D& rect )
1520cdf0e10cSrcweir     {
1521cdf0e10cSrcweir         if( mpCairo ) {
1522cdf0e10cSrcweir             aLayout = getMemoryLayout();
1523cdf0e10cSrcweir 
1524cdf0e10cSrcweir             const sal_Int32 nWidth( rect.X2 - rect.X1 );
1525cdf0e10cSrcweir             const sal_Int32 nHeight( rect.Y2 - rect.Y1 );
1526cdf0e10cSrcweir             uno::Sequence< sal_Int8 > aRes( 4*nWidth*nHeight );
1527cdf0e10cSrcweir             sal_Int8* pData = aRes.getArray();
1528cdf0e10cSrcweir             cairo_surface_t* pImageSurface = cairo_image_surface_create_for_data( (unsigned char *) pData,
1529cdf0e10cSrcweir                                                                                   CAIRO_FORMAT_ARGB32,
1530cdf0e10cSrcweir                                                                                   nWidth, nHeight, 4*nWidth );
1531cdf0e10cSrcweir             cairo_t* pCairo = cairo_create( pImageSurface );
1532cdf0e10cSrcweir             cairo_set_source_surface( pCairo, mpSurface->getCairoSurface().get(), -rect.X1, -rect.Y1);
1533cdf0e10cSrcweir             cairo_paint( pCairo );
1534cdf0e10cSrcweir             cairo_destroy( pCairo );
1535cdf0e10cSrcweir             cairo_surface_destroy( pImageSurface );
1536cdf0e10cSrcweir 
1537cdf0e10cSrcweir             aLayout.ScanLines = nHeight;
1538cdf0e10cSrcweir             aLayout.ScanLineBytes = nWidth*4;
1539cdf0e10cSrcweir             aLayout.ScanLineStride = aLayout.ScanLineBytes;
1540cdf0e10cSrcweir 
1541cdf0e10cSrcweir             return aRes;
1542cdf0e10cSrcweir         }
1543cdf0e10cSrcweir 
1544cdf0e10cSrcweir         return uno::Sequence< sal_Int8 >();
1545cdf0e10cSrcweir     }
1546cdf0e10cSrcweir 
setData(const uno::Sequence<sal_Int8> &,const rendering::IntegerBitmapLayout &,const geometry::IntegerRectangle2D &)1547cdf0e10cSrcweir     void CanvasHelper::setData( const uno::Sequence< sal_Int8 >& 		/*data*/,
1548cdf0e10cSrcweir                                 const rendering::IntegerBitmapLayout&   /*bitmapLayout*/,
1549cdf0e10cSrcweir                                 const geometry::IntegerRectangle2D& 	/*rect*/ )
1550cdf0e10cSrcweir     {
1551cdf0e10cSrcweir     }
1552cdf0e10cSrcweir 
setPixel(const uno::Sequence<sal_Int8> &,const rendering::IntegerBitmapLayout &,const geometry::IntegerPoint2D &)1553cdf0e10cSrcweir     void CanvasHelper::setPixel( const uno::Sequence< sal_Int8 >&       /*color*/,
1554cdf0e10cSrcweir                                  const rendering::IntegerBitmapLayout&  /*bitmapLayout*/,
1555cdf0e10cSrcweir                                  const geometry::IntegerPoint2D&        /*pos*/ )
1556cdf0e10cSrcweir     {
1557cdf0e10cSrcweir     }
1558cdf0e10cSrcweir 
getPixel(rendering::IntegerBitmapLayout &,const geometry::IntegerPoint2D &)1559cdf0e10cSrcweir     uno::Sequence< sal_Int8 > CanvasHelper::getPixel( rendering::IntegerBitmapLayout&   /*bitmapLayout*/,
1560cdf0e10cSrcweir                                                       const geometry::IntegerPoint2D&   /*pos*/ )
1561cdf0e10cSrcweir     {
1562cdf0e10cSrcweir         return uno::Sequence< sal_Int8 >();
1563cdf0e10cSrcweir     }
1564cdf0e10cSrcweir 
getPalette()1565cdf0e10cSrcweir     uno::Reference< rendering::XBitmapPalette > CanvasHelper::getPalette()
1566cdf0e10cSrcweir     {
1567cdf0e10cSrcweir         // TODO(F1): Palette bitmaps NYI
1568cdf0e10cSrcweir         return uno::Reference< rendering::XBitmapPalette >();
1569cdf0e10cSrcweir     }
1570cdf0e10cSrcweir 
1571cdf0e10cSrcweir     namespace
1572cdf0e10cSrcweir     {
1573cdf0e10cSrcweir         class CairoColorSpace : public cppu::WeakImplHelper1< com::sun::star::rendering::XIntegerBitmapColorSpace >
1574cdf0e10cSrcweir         {
1575cdf0e10cSrcweir         private:
1576cdf0e10cSrcweir             uno::Sequence< sal_Int8 >  maComponentTags;
1577cdf0e10cSrcweir             uno::Sequence< sal_Int32 > maBitCounts;
1578cdf0e10cSrcweir 
getType()1579cdf0e10cSrcweir             virtual ::sal_Int8 SAL_CALL getType(  ) throw (uno::RuntimeException)
1580cdf0e10cSrcweir             {
1581cdf0e10cSrcweir                 return rendering::ColorSpaceType::RGB;
1582cdf0e10cSrcweir             }
getComponentTags()1583cdf0e10cSrcweir             virtual uno::Sequence< ::sal_Int8 > SAL_CALL getComponentTags(  ) throw (uno::RuntimeException)
1584cdf0e10cSrcweir             {
1585cdf0e10cSrcweir                 return maComponentTags;
1586cdf0e10cSrcweir             }
getRenderingIntent()1587cdf0e10cSrcweir             virtual ::sal_Int8 SAL_CALL getRenderingIntent(  ) throw (uno::RuntimeException)
1588cdf0e10cSrcweir             {
1589cdf0e10cSrcweir                 return rendering::RenderingIntent::PERCEPTUAL;
1590cdf0e10cSrcweir             }
getProperties()1591cdf0e10cSrcweir             virtual uno::Sequence< beans::PropertyValue > SAL_CALL getProperties(  ) throw (uno::RuntimeException)
1592cdf0e10cSrcweir             {
1593cdf0e10cSrcweir                 return uno::Sequence< beans::PropertyValue >();
1594cdf0e10cSrcweir             }
convertColorSpace(const uno::Sequence<double> & deviceColor,const uno::Reference<rendering::XColorSpace> & targetColorSpace)1595cdf0e10cSrcweir             virtual uno::Sequence< double > SAL_CALL convertColorSpace( const uno::Sequence< double >& deviceColor,
1596cdf0e10cSrcweir                                                                         const uno::Reference< rendering::XColorSpace >& targetColorSpace ) throw (lang::IllegalArgumentException,
1597cdf0e10cSrcweir                                                                                                                                                   uno::RuntimeException)
1598cdf0e10cSrcweir             {
1599cdf0e10cSrcweir                 // TODO(P3): if we know anything about target
1600cdf0e10cSrcweir                 // colorspace, this can be greatly sped up
1601cdf0e10cSrcweir                 uno::Sequence<rendering::ARGBColor> aIntermediate(
1602cdf0e10cSrcweir                     convertToARGB(deviceColor));
1603cdf0e10cSrcweir                 return targetColorSpace->convertFromARGB(aIntermediate);
1604cdf0e10cSrcweir             }
convertToRGB(const uno::Sequence<double> & deviceColor)1605cdf0e10cSrcweir             virtual uno::Sequence< rendering::RGBColor > SAL_CALL convertToRGB( const uno::Sequence< double >& deviceColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
1606cdf0e10cSrcweir             {
1607cdf0e10cSrcweir                 const double*  pIn( deviceColor.getConstArray() );
1608cdf0e10cSrcweir                 const sal_Size nLen( deviceColor.getLength() );
1609cdf0e10cSrcweir                 ENSURE_ARG_OR_THROW2(nLen%4==0,
1610cdf0e10cSrcweir                                      "number of channels no multiple of 4",
1611cdf0e10cSrcweir                                      static_cast<rendering::XColorSpace*>(this), 0);
1612cdf0e10cSrcweir 
1613cdf0e10cSrcweir                 uno::Sequence< rendering::RGBColor > aRes(nLen/4);
1614cdf0e10cSrcweir                 rendering::RGBColor* pOut( aRes.getArray() );
1615cdf0e10cSrcweir                 for( sal_Size i=0; i<nLen; i+=4 )
1616cdf0e10cSrcweir                 {
1617cdf0e10cSrcweir                     const double fAlpha(pIn[3]);
1618cdf0e10cSrcweir                     if( fAlpha == 0.0 )
1619cdf0e10cSrcweir                         *pOut++ = rendering::RGBColor(0.0, 0.0, 0.0);
1620cdf0e10cSrcweir                     else
1621cdf0e10cSrcweir                         *pOut++ = rendering::RGBColor(pIn[2]/fAlpha,pIn[1]/fAlpha,pIn[0]/fAlpha);
1622cdf0e10cSrcweir                     pIn += 4;
1623cdf0e10cSrcweir                 }
1624cdf0e10cSrcweir                 return aRes;
1625cdf0e10cSrcweir             }
convertToARGB(const uno::Sequence<double> & deviceColor)1626cdf0e10cSrcweir             virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertToARGB( const uno::Sequence< double >& deviceColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
1627cdf0e10cSrcweir             {
1628cdf0e10cSrcweir                 const double*  pIn( deviceColor.getConstArray() );
1629cdf0e10cSrcweir                 const sal_Size nLen( deviceColor.getLength() );
1630cdf0e10cSrcweir                 ENSURE_ARG_OR_THROW2(nLen%4==0,
1631cdf0e10cSrcweir                                      "number of channels no multiple of 4",
1632cdf0e10cSrcweir                                      static_cast<rendering::XColorSpace*>(this), 0);
1633cdf0e10cSrcweir 
1634cdf0e10cSrcweir                 uno::Sequence< rendering::ARGBColor > aRes(nLen/4);
1635cdf0e10cSrcweir                 rendering::ARGBColor* pOut( aRes.getArray() );
1636cdf0e10cSrcweir                 for( sal_Size i=0; i<nLen; i+=4 )
1637cdf0e10cSrcweir                 {
1638cdf0e10cSrcweir                     const double fAlpha(pIn[3]);
1639cdf0e10cSrcweir                     if( fAlpha == 0.0 )
1640cdf0e10cSrcweir                         *pOut++ = rendering::ARGBColor(0.0, 0.0, 0.0, 0.0);
1641cdf0e10cSrcweir                     else
1642cdf0e10cSrcweir                         *pOut++ = rendering::ARGBColor(fAlpha,pIn[2]/fAlpha,pIn[1]/fAlpha,pIn[0]/fAlpha);
1643cdf0e10cSrcweir                     pIn += 4;
1644cdf0e10cSrcweir                 }
1645cdf0e10cSrcweir                 return aRes;
1646cdf0e10cSrcweir             }
convertToPARGB(const uno::Sequence<double> & deviceColor)1647cdf0e10cSrcweir             virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertToPARGB( const uno::Sequence< double >& deviceColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
1648cdf0e10cSrcweir             {
1649cdf0e10cSrcweir                 const double*  pIn( deviceColor.getConstArray() );
1650cdf0e10cSrcweir                 const sal_Size nLen( deviceColor.getLength() );
1651cdf0e10cSrcweir                 ENSURE_ARG_OR_THROW2(nLen%4==0,
1652cdf0e10cSrcweir                                      "number of channels no multiple of 4",
1653cdf0e10cSrcweir                                      static_cast<rendering::XColorSpace*>(this), 0);
1654cdf0e10cSrcweir 
1655cdf0e10cSrcweir                 uno::Sequence< rendering::ARGBColor > aRes(nLen/4);
1656cdf0e10cSrcweir                 rendering::ARGBColor* pOut( aRes.getArray() );
1657cdf0e10cSrcweir                 for( sal_Size i=0; i<nLen; i+=4 )
1658cdf0e10cSrcweir                 {
1659cdf0e10cSrcweir                     *pOut++ = rendering::ARGBColor(pIn[3],pIn[2],pIn[1],pIn[1]);
1660cdf0e10cSrcweir                     pIn += 4;
1661cdf0e10cSrcweir                 }
1662cdf0e10cSrcweir                 return aRes;
1663cdf0e10cSrcweir             }
convertFromRGB(const uno::Sequence<rendering::RGBColor> & rgbColor)1664cdf0e10cSrcweir             virtual uno::Sequence< double > SAL_CALL convertFromRGB( const uno::Sequence< rendering::RGBColor >& rgbColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
1665cdf0e10cSrcweir             {
1666cdf0e10cSrcweir                 const rendering::RGBColor* pIn( rgbColor.getConstArray() );
1667cdf0e10cSrcweir                 const sal_Size             nLen( rgbColor.getLength() );
1668cdf0e10cSrcweir 
1669cdf0e10cSrcweir                 uno::Sequence< double > aRes(nLen*4);
1670cdf0e10cSrcweir                 double* pColors=aRes.getArray();
1671cdf0e10cSrcweir                 for( sal_Size i=0; i<nLen; ++i )
1672cdf0e10cSrcweir                 {
1673cdf0e10cSrcweir                     *pColors++ = pIn->Blue;
1674cdf0e10cSrcweir                     *pColors++ = pIn->Green;
1675cdf0e10cSrcweir                     *pColors++ = pIn->Red;
1676cdf0e10cSrcweir                     *pColors++ = 1.0;
1677cdf0e10cSrcweir                     ++pIn;
1678cdf0e10cSrcweir                 }
1679cdf0e10cSrcweir                 return aRes;
1680cdf0e10cSrcweir             }
convertFromARGB(const uno::Sequence<rendering::ARGBColor> & rgbColor)1681cdf0e10cSrcweir             virtual uno::Sequence< double > SAL_CALL convertFromARGB( const uno::Sequence< rendering::ARGBColor >& rgbColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
1682cdf0e10cSrcweir             {
1683cdf0e10cSrcweir                 const rendering::ARGBColor* pIn( rgbColor.getConstArray() );
1684cdf0e10cSrcweir                 const sal_Size              nLen( rgbColor.getLength() );
1685cdf0e10cSrcweir 
1686cdf0e10cSrcweir                 uno::Sequence< double > aRes(nLen*4);
1687cdf0e10cSrcweir                 double* pColors=aRes.getArray();
1688cdf0e10cSrcweir                 for( sal_Size i=0; i<nLen; ++i )
1689cdf0e10cSrcweir                 {
1690cdf0e10cSrcweir                     *pColors++ = pIn->Alpha*pIn->Blue;
1691cdf0e10cSrcweir                     *pColors++ = pIn->Alpha*pIn->Green;
1692cdf0e10cSrcweir                     *pColors++ = pIn->Alpha*pIn->Red;
1693cdf0e10cSrcweir                     *pColors++ = pIn->Alpha;
1694cdf0e10cSrcweir                     ++pIn;
1695cdf0e10cSrcweir                 }
1696cdf0e10cSrcweir                 return aRes;
1697cdf0e10cSrcweir             }
convertFromPARGB(const uno::Sequence<rendering::ARGBColor> & rgbColor)1698cdf0e10cSrcweir             virtual uno::Sequence< double > SAL_CALL convertFromPARGB( const uno::Sequence< rendering::ARGBColor >& rgbColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
1699cdf0e10cSrcweir             {
1700cdf0e10cSrcweir                 const rendering::ARGBColor* pIn( rgbColor.getConstArray() );
1701cdf0e10cSrcweir                 const sal_Size              nLen( rgbColor.getLength() );
1702cdf0e10cSrcweir 
1703cdf0e10cSrcweir                 uno::Sequence< double > aRes(nLen*4);
1704cdf0e10cSrcweir                 double* pColors=aRes.getArray();
1705cdf0e10cSrcweir                 for( sal_Size i=0; i<nLen; ++i )
1706cdf0e10cSrcweir                 {
1707cdf0e10cSrcweir                     *pColors++ = pIn->Blue;
1708cdf0e10cSrcweir                     *pColors++ = pIn->Green;
1709cdf0e10cSrcweir                     *pColors++ = pIn->Red;
1710cdf0e10cSrcweir                     *pColors++ = pIn->Alpha;
1711cdf0e10cSrcweir                     ++pIn;
1712cdf0e10cSrcweir                 }
1713cdf0e10cSrcweir                 return aRes;
1714cdf0e10cSrcweir             }
1715cdf0e10cSrcweir 
1716cdf0e10cSrcweir             // XIntegerBitmapColorSpace
getBitsPerPixel()1717cdf0e10cSrcweir             virtual ::sal_Int32 SAL_CALL getBitsPerPixel(  ) throw (uno::RuntimeException)
1718cdf0e10cSrcweir             {
1719cdf0e10cSrcweir                 return 32;
1720cdf0e10cSrcweir             }
getComponentBitCounts()1721cdf0e10cSrcweir             virtual uno::Sequence< ::sal_Int32 > SAL_CALL getComponentBitCounts(  ) throw (uno::RuntimeException)
1722cdf0e10cSrcweir             {
1723cdf0e10cSrcweir                 return maBitCounts;
1724cdf0e10cSrcweir             }
getEndianness()1725cdf0e10cSrcweir             virtual ::sal_Int8 SAL_CALL getEndianness(  ) throw (uno::RuntimeException)
1726cdf0e10cSrcweir             {
1727cdf0e10cSrcweir                 return util::Endianness::LITTLE;
1728cdf0e10cSrcweir             }
convertFromIntegerColorSpace(const uno::Sequence<::sal_Int8> & deviceColor,const uno::Reference<rendering::XColorSpace> & targetColorSpace)1729cdf0e10cSrcweir             virtual uno::Sequence<double> SAL_CALL convertFromIntegerColorSpace( const uno::Sequence< ::sal_Int8 >& deviceColor,
1730cdf0e10cSrcweir                                                                                  const uno::Reference< rendering::XColorSpace >& targetColorSpace ) throw (lang::IllegalArgumentException,
1731cdf0e10cSrcweir                                                                                                                                                            uno::RuntimeException)
1732cdf0e10cSrcweir             {
1733cdf0e10cSrcweir                 if( dynamic_cast<CairoColorSpace*>(targetColorSpace.get()) )
1734cdf0e10cSrcweir                 {
1735cdf0e10cSrcweir                     const sal_Int8* pIn( deviceColor.getConstArray() );
1736cdf0e10cSrcweir                     const sal_Size  nLen( deviceColor.getLength() );
1737cdf0e10cSrcweir                     ENSURE_ARG_OR_THROW2(nLen%4==0,
1738cdf0e10cSrcweir                                          "number of channels no multiple of 4",
1739cdf0e10cSrcweir                                          static_cast<rendering::XColorSpace*>(this), 0);
1740cdf0e10cSrcweir 
1741cdf0e10cSrcweir                     uno::Sequence<double> aRes(nLen);
1742cdf0e10cSrcweir                     double* pOut( aRes.getArray() );
1743cdf0e10cSrcweir                     for( sal_Size i=0; i<nLen; i+=4 )
1744cdf0e10cSrcweir                     {
1745cdf0e10cSrcweir                         *pOut++ = vcl::unotools::toDoubleColor(*pIn++);
1746cdf0e10cSrcweir                         *pOut++ = vcl::unotools::toDoubleColor(*pIn++);
1747cdf0e10cSrcweir                         *pOut++ = vcl::unotools::toDoubleColor(*pIn++);
1748cdf0e10cSrcweir                         *pOut++ = vcl::unotools::toDoubleColor(*pIn++);
1749cdf0e10cSrcweir                     }
1750cdf0e10cSrcweir                     return aRes;
1751cdf0e10cSrcweir                 }
1752cdf0e10cSrcweir                 else
1753cdf0e10cSrcweir                 {
1754cdf0e10cSrcweir                     // TODO(P3): if we know anything about target
1755cdf0e10cSrcweir                     // colorspace, this can be greatly sped up
1756cdf0e10cSrcweir                     uno::Sequence<rendering::ARGBColor> aIntermediate(
1757cdf0e10cSrcweir                         convertIntegerToARGB(deviceColor));
1758cdf0e10cSrcweir                     return targetColorSpace->convertFromARGB(aIntermediate);
1759cdf0e10cSrcweir                 }
1760cdf0e10cSrcweir             }
convertToIntegerColorSpace(const uno::Sequence<::sal_Int8> & deviceColor,const uno::Reference<rendering::XIntegerBitmapColorSpace> & targetColorSpace)1761cdf0e10cSrcweir             virtual uno::Sequence< ::sal_Int8 > SAL_CALL convertToIntegerColorSpace( const uno::Sequence< ::sal_Int8 >& deviceColor,
1762cdf0e10cSrcweir                                                                                      const uno::Reference< rendering::XIntegerBitmapColorSpace >& targetColorSpace ) throw (lang::IllegalArgumentException,
1763cdf0e10cSrcweir                                                                                                                                                                             uno::RuntimeException)
1764cdf0e10cSrcweir             {
1765cdf0e10cSrcweir                 if( dynamic_cast<CairoColorSpace*>(targetColorSpace.get()) )
1766cdf0e10cSrcweir                 {
1767cdf0e10cSrcweir                     // it's us, so simply pass-through the data
1768cdf0e10cSrcweir                     return deviceColor;
1769cdf0e10cSrcweir                 }
1770cdf0e10cSrcweir                 else
1771cdf0e10cSrcweir                 {
1772cdf0e10cSrcweir                     // TODO(P3): if we know anything about target
1773cdf0e10cSrcweir                     // colorspace, this can be greatly sped up
1774cdf0e10cSrcweir                     uno::Sequence<rendering::ARGBColor> aIntermediate(
1775cdf0e10cSrcweir                         convertIntegerToARGB(deviceColor));
1776cdf0e10cSrcweir                     return targetColorSpace->convertIntegerFromARGB(aIntermediate);
1777cdf0e10cSrcweir                 }
1778cdf0e10cSrcweir             }
convertIntegerToRGB(const uno::Sequence<::sal_Int8> & deviceColor)1779cdf0e10cSrcweir             virtual uno::Sequence< rendering::RGBColor > SAL_CALL convertIntegerToRGB( const uno::Sequence< ::sal_Int8 >& deviceColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
1780cdf0e10cSrcweir             {
1781cdf0e10cSrcweir                 const sal_Int8* pIn( deviceColor.getConstArray() );
1782cdf0e10cSrcweir                 const sal_Size  nLen( deviceColor.getLength() );
1783cdf0e10cSrcweir                 ENSURE_ARG_OR_THROW2(nLen%4==0,
1784cdf0e10cSrcweir                                      "number of channels no multiple of 4",
1785cdf0e10cSrcweir                                      static_cast<rendering::XColorSpace*>(this), 0);
1786cdf0e10cSrcweir 
1787cdf0e10cSrcweir                 uno::Sequence< rendering::RGBColor > aRes(nLen/4);
1788cdf0e10cSrcweir                 rendering::RGBColor* pOut( aRes.getArray() );
1789cdf0e10cSrcweir                 for( sal_Size i=0; i<nLen; i+=4 )
1790cdf0e10cSrcweir                 {
1791cdf0e10cSrcweir                     const double fAlpha((sal_uInt8)pIn[3]);
1792cdf0e10cSrcweir                     if( fAlpha )
1793cdf0e10cSrcweir                         *pOut++ = rendering::RGBColor(
1794cdf0e10cSrcweir                             pIn[2]/fAlpha,
1795cdf0e10cSrcweir                             pIn[1]/fAlpha,
1796cdf0e10cSrcweir                             pIn[0]/fAlpha);
1797cdf0e10cSrcweir                     else
1798cdf0e10cSrcweir                         *pOut++ = rendering::RGBColor(0,0,0);
1799cdf0e10cSrcweir                     pIn += 4;
1800cdf0e10cSrcweir                 }
1801cdf0e10cSrcweir                 return aRes;
1802cdf0e10cSrcweir             }
1803cdf0e10cSrcweir 
convertIntegerToARGB(const uno::Sequence<::sal_Int8> & deviceColor)1804cdf0e10cSrcweir             virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertIntegerToARGB( const uno::Sequence< ::sal_Int8 >& deviceColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
1805cdf0e10cSrcweir             {
1806cdf0e10cSrcweir                 const sal_Int8* pIn( deviceColor.getConstArray() );
1807cdf0e10cSrcweir                 const sal_Size  nLen( deviceColor.getLength() );
1808cdf0e10cSrcweir                 ENSURE_ARG_OR_THROW2(nLen%4==0,
1809cdf0e10cSrcweir                                      "number of channels no multiple of 4",
1810cdf0e10cSrcweir                                      static_cast<rendering::XColorSpace*>(this), 0);
1811cdf0e10cSrcweir 
1812cdf0e10cSrcweir                 uno::Sequence< rendering::ARGBColor > aRes(nLen/4);
1813cdf0e10cSrcweir                 rendering::ARGBColor* pOut( aRes.getArray() );
1814cdf0e10cSrcweir                 for( sal_Size i=0; i<nLen; i+=4 )
1815cdf0e10cSrcweir                 {
1816cdf0e10cSrcweir                     const double fAlpha((sal_uInt8)pIn[3]);
1817cdf0e10cSrcweir                     if( fAlpha )
1818cdf0e10cSrcweir                         *pOut++ = rendering::ARGBColor(
1819cdf0e10cSrcweir                             fAlpha/255.0,
1820cdf0e10cSrcweir                             pIn[2]/fAlpha,
1821cdf0e10cSrcweir                             pIn[1]/fAlpha,
1822cdf0e10cSrcweir                             pIn[0]/fAlpha);
1823cdf0e10cSrcweir                     else
1824cdf0e10cSrcweir                         *pOut++ = rendering::ARGBColor(0,0,0,0);
1825cdf0e10cSrcweir                     pIn += 4;
1826cdf0e10cSrcweir                 }
1827cdf0e10cSrcweir                 return aRes;
1828cdf0e10cSrcweir             }
convertIntegerToPARGB(const uno::Sequence<::sal_Int8> & deviceColor)1829cdf0e10cSrcweir             virtual uno::Sequence< rendering::ARGBColor > SAL_CALL convertIntegerToPARGB( const uno::Sequence< ::sal_Int8 >& deviceColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
1830cdf0e10cSrcweir             {
1831cdf0e10cSrcweir                 const sal_Int8* pIn( deviceColor.getConstArray() );
1832cdf0e10cSrcweir                 const sal_Size  nLen( deviceColor.getLength() );
1833cdf0e10cSrcweir                 ENSURE_ARG_OR_THROW2(nLen%4==0,
1834cdf0e10cSrcweir                                      "number of channels no multiple of 4",
1835cdf0e10cSrcweir                                      static_cast<rendering::XColorSpace*>(this), 0);
1836cdf0e10cSrcweir 
1837cdf0e10cSrcweir                 uno::Sequence< rendering::ARGBColor > aRes(nLen/4);
1838cdf0e10cSrcweir                 rendering::ARGBColor* pOut( aRes.getArray() );
1839cdf0e10cSrcweir                 for( sal_Size i=0; i<nLen; i+=4 )
1840cdf0e10cSrcweir                 {
1841cdf0e10cSrcweir                     *pOut++ = rendering::ARGBColor(
1842cdf0e10cSrcweir                         vcl::unotools::toDoubleColor(pIn[3]),
1843cdf0e10cSrcweir                         vcl::unotools::toDoubleColor(pIn[2]),
1844cdf0e10cSrcweir                         vcl::unotools::toDoubleColor(pIn[1]),
1845cdf0e10cSrcweir                         vcl::unotools::toDoubleColor(pIn[0]));
1846cdf0e10cSrcweir                     pIn += 4;
1847cdf0e10cSrcweir                 }
1848cdf0e10cSrcweir                 return aRes;
1849cdf0e10cSrcweir             }
1850cdf0e10cSrcweir 
convertIntegerFromRGB(const uno::Sequence<rendering::RGBColor> & rgbColor)1851cdf0e10cSrcweir             virtual uno::Sequence< ::sal_Int8 > SAL_CALL convertIntegerFromRGB( const uno::Sequence< rendering::RGBColor >& rgbColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
1852cdf0e10cSrcweir             {
1853cdf0e10cSrcweir                 const rendering::RGBColor* pIn( rgbColor.getConstArray() );
1854cdf0e10cSrcweir                 const sal_Size             nLen( rgbColor.getLength() );
1855cdf0e10cSrcweir 
1856cdf0e10cSrcweir                 uno::Sequence< sal_Int8 > aRes(nLen*4);
1857cdf0e10cSrcweir                 sal_Int8* pColors=aRes.getArray();
1858cdf0e10cSrcweir                 for( sal_Size i=0; i<nLen; ++i )
1859cdf0e10cSrcweir                 {
1860cdf0e10cSrcweir                     *pColors++ = vcl::unotools::toByteColor(pIn->Blue);
1861cdf0e10cSrcweir                     *pColors++ = vcl::unotools::toByteColor(pIn->Green);
1862cdf0e10cSrcweir                     *pColors++ = vcl::unotools::toByteColor(pIn->Red);
1863cdf0e10cSrcweir                     *pColors++ = 255;
1864cdf0e10cSrcweir                     ++pIn;
1865cdf0e10cSrcweir                 }
1866cdf0e10cSrcweir                 return aRes;
1867cdf0e10cSrcweir             }
1868cdf0e10cSrcweir 
convertIntegerFromARGB(const uno::Sequence<rendering::ARGBColor> & rgbColor)1869cdf0e10cSrcweir             virtual uno::Sequence< ::sal_Int8 > SAL_CALL convertIntegerFromARGB( const uno::Sequence< rendering::ARGBColor >& rgbColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
1870cdf0e10cSrcweir             {
1871cdf0e10cSrcweir                 const rendering::ARGBColor* pIn( rgbColor.getConstArray() );
1872cdf0e10cSrcweir                 const sal_Size              nLen( rgbColor.getLength() );
1873cdf0e10cSrcweir 
1874cdf0e10cSrcweir                 uno::Sequence< sal_Int8 > aRes(nLen*4);
1875cdf0e10cSrcweir                 sal_Int8* pColors=aRes.getArray();
1876cdf0e10cSrcweir                 for( sal_Size i=0; i<nLen; ++i )
1877cdf0e10cSrcweir                 {
1878cdf0e10cSrcweir                     const double fAlpha(pIn->Alpha);
1879cdf0e10cSrcweir                     *pColors++ = vcl::unotools::toByteColor(fAlpha*pIn->Blue);
1880cdf0e10cSrcweir                     *pColors++ = vcl::unotools::toByteColor(fAlpha*pIn->Green);
1881cdf0e10cSrcweir                     *pColors++ = vcl::unotools::toByteColor(fAlpha*pIn->Red);
1882cdf0e10cSrcweir                     *pColors++ = vcl::unotools::toByteColor(fAlpha);
1883cdf0e10cSrcweir                     ++pIn;
1884cdf0e10cSrcweir                 }
1885cdf0e10cSrcweir                 return aRes;
1886cdf0e10cSrcweir             }
convertIntegerFromPARGB(const uno::Sequence<rendering::ARGBColor> & rgbColor)1887cdf0e10cSrcweir             virtual uno::Sequence< ::sal_Int8 > SAL_CALL convertIntegerFromPARGB( const uno::Sequence< rendering::ARGBColor >& rgbColor ) throw (lang::IllegalArgumentException, uno::RuntimeException)
1888cdf0e10cSrcweir             {
1889cdf0e10cSrcweir                 const rendering::ARGBColor* pIn( rgbColor.getConstArray() );
1890cdf0e10cSrcweir                 const sal_Size              nLen( rgbColor.getLength() );
1891cdf0e10cSrcweir 
1892cdf0e10cSrcweir                 uno::Sequence< sal_Int8 > aRes(nLen*4);
1893cdf0e10cSrcweir                 sal_Int8* pColors=aRes.getArray();
1894cdf0e10cSrcweir                 for( sal_Size i=0; i<nLen; ++i )
1895cdf0e10cSrcweir                 {
1896cdf0e10cSrcweir                     *pColors++ = vcl::unotools::toByteColor(pIn->Blue);
1897cdf0e10cSrcweir                     *pColors++ = vcl::unotools::toByteColor(pIn->Green);
1898cdf0e10cSrcweir                     *pColors++ = vcl::unotools::toByteColor(pIn->Red);
1899cdf0e10cSrcweir                     *pColors++ = vcl::unotools::toByteColor(pIn->Alpha);
1900cdf0e10cSrcweir                     ++pIn;
1901cdf0e10cSrcweir                 }
1902cdf0e10cSrcweir                 return aRes;
1903cdf0e10cSrcweir             }
1904cdf0e10cSrcweir 
1905cdf0e10cSrcweir         public:
CairoColorSpace()1906cdf0e10cSrcweir             CairoColorSpace() :
1907cdf0e10cSrcweir                 maComponentTags(4),
1908cdf0e10cSrcweir                 maBitCounts(4)
1909cdf0e10cSrcweir             {
1910cdf0e10cSrcweir                 sal_Int8*  pTags = maComponentTags.getArray();
1911cdf0e10cSrcweir                 sal_Int32* pBitCounts = maBitCounts.getArray();
1912cdf0e10cSrcweir                 pTags[0] = rendering::ColorComponentTag::RGB_BLUE;
1913cdf0e10cSrcweir                 pTags[1] = rendering::ColorComponentTag::RGB_GREEN;
1914cdf0e10cSrcweir                 pTags[2] = rendering::ColorComponentTag::RGB_RED;
1915cdf0e10cSrcweir                 pTags[3] = rendering::ColorComponentTag::PREMULTIPLIED_ALPHA;
1916cdf0e10cSrcweir 
1917cdf0e10cSrcweir                 pBitCounts[0] =
1918cdf0e10cSrcweir                     pBitCounts[1] =
1919cdf0e10cSrcweir                     pBitCounts[2] =
1920cdf0e10cSrcweir                     pBitCounts[3] = 8;
1921cdf0e10cSrcweir             }
1922cdf0e10cSrcweir         };
1923cdf0e10cSrcweir 
1924cdf0e10cSrcweir         struct CairoColorSpaceHolder : public rtl::StaticWithInit<uno::Reference<rendering::XIntegerBitmapColorSpace>,
1925cdf0e10cSrcweir                                                                      CairoColorSpaceHolder>
1926cdf0e10cSrcweir         {
operator ()cairocanvas::__anon880f7b480111::CairoColorSpaceHolder1927cdf0e10cSrcweir             uno::Reference<rendering::XIntegerBitmapColorSpace> operator()()
1928cdf0e10cSrcweir             {
1929cdf0e10cSrcweir                 return new CairoColorSpace();
1930cdf0e10cSrcweir             }
1931cdf0e10cSrcweir         };
1932cdf0e10cSrcweir     }
1933cdf0e10cSrcweir 
getMemoryLayout()1934cdf0e10cSrcweir     rendering::IntegerBitmapLayout CanvasHelper::getMemoryLayout()
1935cdf0e10cSrcweir     {
1936cdf0e10cSrcweir         if( !mpCairo )
1937cdf0e10cSrcweir             return rendering::IntegerBitmapLayout(); // we're disposed
1938cdf0e10cSrcweir 
1939cdf0e10cSrcweir         const geometry::IntegerSize2D aSize(getSize());
1940cdf0e10cSrcweir         rendering::IntegerBitmapLayout aLayout;
1941cdf0e10cSrcweir 
1942cdf0e10cSrcweir         aLayout.ScanLines = aSize.Height;
1943cdf0e10cSrcweir         aLayout.ScanLineBytes = aSize.Width*4;
1944cdf0e10cSrcweir         aLayout.ScanLineStride = aLayout.ScanLineBytes;
1945cdf0e10cSrcweir         aLayout.PlaneStride = 0;
1946cdf0e10cSrcweir         aLayout.ColorSpace = CairoColorSpaceHolder::get();
1947cdf0e10cSrcweir         aLayout.Palette.clear();
1948cdf0e10cSrcweir         aLayout.IsMsbFirst = sal_False;
1949cdf0e10cSrcweir 
1950cdf0e10cSrcweir         return aLayout;
1951cdf0e10cSrcweir     }
1952cdf0e10cSrcweir 
flush() const1953cdf0e10cSrcweir     void CanvasHelper::flush() const
1954cdf0e10cSrcweir     {
1955cdf0e10cSrcweir     }
1956cdf0e10cSrcweir 
hasAlpha() const1957cdf0e10cSrcweir     bool CanvasHelper::hasAlpha() const
1958cdf0e10cSrcweir     {
1959cdf0e10cSrcweir         return mbHaveAlpha;
1960cdf0e10cSrcweir     }
1961cdf0e10cSrcweir 
repaint(const SurfaceSharedPtr & pSurface,const rendering::ViewState & viewState,const rendering::RenderState & renderState)1962cdf0e10cSrcweir     bool CanvasHelper::repaint( const SurfaceSharedPtr& pSurface,
1963cdf0e10cSrcweir 								const rendering::ViewState&      viewState,
1964cdf0e10cSrcweir 								const rendering::RenderState&	 renderState )
1965cdf0e10cSrcweir     {
1966cdf0e10cSrcweir 		OSL_TRACE("CanvasHelper::repaint");
1967cdf0e10cSrcweir 
1968cdf0e10cSrcweir 		if( mpCairo ) {
1969cdf0e10cSrcweir 			cairo_save( mpCairo.get() );
1970cdf0e10cSrcweir 
1971cdf0e10cSrcweir 			cairo_rectangle( mpCairo.get(), 0, 0, maSize.getX(), maSize.getY() );
1972cdf0e10cSrcweir 			cairo_clip( mpCairo.get() );
1973cdf0e10cSrcweir 
1974cdf0e10cSrcweir 			useStates( viewState, renderState, true );
1975cdf0e10cSrcweir 
1976cdf0e10cSrcweir 			Matrix aMatrix;
1977cdf0e10cSrcweir 
1978cdf0e10cSrcweir 			cairo_get_matrix( mpCairo.get(), &aMatrix );
1979cdf0e10cSrcweir 			aMatrix.xx = aMatrix.yy = 1;
1980cdf0e10cSrcweir 			cairo_set_matrix( mpCairo.get(), &aMatrix );
1981cdf0e10cSrcweir 
1982cdf0e10cSrcweir 			//   	    if( !bHasAlpha )
1983cdf0e10cSrcweir 			//   		cairo_set_operator( mpCairo.get(), CAIRO_OPERATOR_SOURCE );
1984cdf0e10cSrcweir 
1985cdf0e10cSrcweir 			cairo_set_source_surface( mpCairo.get(), pSurface->getCairoSurface().get(), 0, 0 );
1986cdf0e10cSrcweir 			cairo_paint( mpCairo.get() );
1987cdf0e10cSrcweir 			cairo_restore( mpCairo.get() );
1988cdf0e10cSrcweir 		}
1989cdf0e10cSrcweir 
1990cdf0e10cSrcweir 		return true;
1991cdf0e10cSrcweir     }
1992cdf0e10cSrcweir }
1993