1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_cppcanvas.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <canvasgraphichelper.hxx>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <com/sun/star/rendering/XGraphicDevice.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/rendering/XCanvas.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/rendering/XPolyPolygon2D.hpp>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <canvas/canvastools.hxx>
38*cdf0e10cSrcweir #include <basegfx/tools/canvastools.hxx>
39*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <cppcanvas/polypolygon.hxx>
42*cdf0e10cSrcweir #include "tools.hxx"
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using namespace ::com::sun::star;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir /* Implementation of CanvasGraphicHelper class */
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace cppcanvas
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir     namespace internal
53*cdf0e10cSrcweir     {
54*cdf0e10cSrcweir         CanvasGraphicHelper::CanvasGraphicHelper( const CanvasSharedPtr& rParentCanvas ) :
55*cdf0e10cSrcweir             maClipPolyPolygon(),
56*cdf0e10cSrcweir             mpCanvas( rParentCanvas ),
57*cdf0e10cSrcweir             mxGraphicDevice()
58*cdf0e10cSrcweir         {
59*cdf0e10cSrcweir             OSL_ENSURE( mpCanvas.get() != NULL &&
60*cdf0e10cSrcweir                         mpCanvas->getUNOCanvas().is(),
61*cdf0e10cSrcweir                         "CanvasGraphicHelper::CanvasGraphicHelper: no valid canvas" );
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir             if( mpCanvas.get() != NULL &&
64*cdf0e10cSrcweir                 mpCanvas->getUNOCanvas().is() )
65*cdf0e10cSrcweir             {
66*cdf0e10cSrcweir                 mxGraphicDevice = mpCanvas->getUNOCanvas()->getDevice();
67*cdf0e10cSrcweir             }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir             ::canvas::tools::initRenderState( maRenderState );
70*cdf0e10cSrcweir         }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir         void CanvasGraphicHelper::setTransformation( const ::basegfx::B2DHomMatrix& rMatrix )
73*cdf0e10cSrcweir         {
74*cdf0e10cSrcweir             ::canvas::tools::setRenderStateTransform( maRenderState, rMatrix );
75*cdf0e10cSrcweir         }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir         ::basegfx::B2DHomMatrix CanvasGraphicHelper::getTransformation() const
78*cdf0e10cSrcweir         {
79*cdf0e10cSrcweir             ::basegfx::B2DHomMatrix aMatrix;
80*cdf0e10cSrcweir             return ::canvas::tools::getRenderStateTransform( aMatrix,
81*cdf0e10cSrcweir                                                              maRenderState );
82*cdf0e10cSrcweir         }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir         void CanvasGraphicHelper::setClip( const ::basegfx::B2DPolyPolygon& rClipPoly )
85*cdf0e10cSrcweir         {
86*cdf0e10cSrcweir             // TODO(T3): not thread-safe. B2DPolyPolygon employs copy-on-write
87*cdf0e10cSrcweir             maClipPolyPolygon.reset( rClipPoly );
88*cdf0e10cSrcweir             maRenderState.Clip.clear();
89*cdf0e10cSrcweir         }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         void CanvasGraphicHelper::setClip()
92*cdf0e10cSrcweir         {
93*cdf0e10cSrcweir             maClipPolyPolygon.reset();
94*cdf0e10cSrcweir             maRenderState.Clip.clear();
95*cdf0e10cSrcweir         }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir         ::basegfx::B2DPolyPolygon const* CanvasGraphicHelper::getClip() const
98*cdf0e10cSrcweir         {
99*cdf0e10cSrcweir             return !maClipPolyPolygon ? NULL : &(*maClipPolyPolygon);
100*cdf0e10cSrcweir         }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir         const rendering::RenderState& CanvasGraphicHelper::getRenderState() const
103*cdf0e10cSrcweir         {
104*cdf0e10cSrcweir             if( maClipPolyPolygon && !maRenderState.Clip.is() )
105*cdf0e10cSrcweir             {
106*cdf0e10cSrcweir                 uno::Reference< rendering::XCanvas > xCanvas( mpCanvas->getUNOCanvas() );
107*cdf0e10cSrcweir                 if( !xCanvas.is() )
108*cdf0e10cSrcweir                     return maRenderState;
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir                 maRenderState.Clip = ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon(
111*cdf0e10cSrcweir                     xCanvas->getDevice(),
112*cdf0e10cSrcweir                     *maClipPolyPolygon );
113*cdf0e10cSrcweir             }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir             return maRenderState;
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir         void CanvasGraphicHelper::setRGBAColor( Color::IntSRGBA aColor )
119*cdf0e10cSrcweir         {
120*cdf0e10cSrcweir             maRenderState.DeviceColor = tools::intSRGBAToDoubleSequence( mxGraphicDevice,
121*cdf0e10cSrcweir                                                                          aColor );
122*cdf0e10cSrcweir         }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir         Color::IntSRGBA CanvasGraphicHelper::getRGBAColor() const
125*cdf0e10cSrcweir         {
126*cdf0e10cSrcweir             return tools::doubleSequenceToIntSRGBA( mxGraphicDevice,
127*cdf0e10cSrcweir                                                     maRenderState.DeviceColor );
128*cdf0e10cSrcweir         }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir         void CanvasGraphicHelper::setCompositeOp( CompositeOp aOp )
131*cdf0e10cSrcweir         {
132*cdf0e10cSrcweir             maRenderState.CompositeOperation = (sal_Int8)aOp;
133*cdf0e10cSrcweir         }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir         CanvasGraphic::CompositeOp CanvasGraphicHelper::getCompositeOp() const
136*cdf0e10cSrcweir         {
137*cdf0e10cSrcweir             return static_cast<CompositeOp>(maRenderState.CompositeOperation);
138*cdf0e10cSrcweir         }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir         CanvasSharedPtr CanvasGraphicHelper::getCanvas() const
141*cdf0e10cSrcweir         {
142*cdf0e10cSrcweir             return mpCanvas;
143*cdf0e10cSrcweir         }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir         uno::Reference< rendering::XGraphicDevice >	CanvasGraphicHelper::getGraphicDevice() const
146*cdf0e10cSrcweir         {
147*cdf0e10cSrcweir             return mxGraphicDevice;
148*cdf0e10cSrcweir         }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir     }
151*cdf0e10cSrcweir }
152