1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cppcanvas.hxx"
26 
27 #include <canvasgraphichelper.hxx>
28 
29 #include <com/sun/star/rendering/XGraphicDevice.hpp>
30 #include <com/sun/star/rendering/XCanvas.hpp>
31 #include <com/sun/star/rendering/XPolyPolygon2D.hpp>
32 
33 #include <canvas/canvastools.hxx>
34 #include <basegfx/tools/canvastools.hxx>
35 #include <basegfx/matrix/b2dhommatrix.hxx>
36 
37 #include <cppcanvas/polypolygon.hxx>
38 #include "tools.hxx"
39 
40 
41 using namespace ::com::sun::star;
42 
43 /* Implementation of CanvasGraphicHelper class */
44 
45 namespace cppcanvas
46 {
47 
48     namespace internal
49     {
CanvasGraphicHelper(const CanvasSharedPtr & rParentCanvas)50         CanvasGraphicHelper::CanvasGraphicHelper( const CanvasSharedPtr& rParentCanvas ) :
51             maClipPolyPolygon(),
52             mpCanvas( rParentCanvas ),
53             mxGraphicDevice()
54         {
55             OSL_ENSURE( mpCanvas.get() != NULL &&
56                         mpCanvas->getUNOCanvas().is(),
57                         "CanvasGraphicHelper::CanvasGraphicHelper: no valid canvas" );
58 
59             if( mpCanvas.get() != NULL &&
60                 mpCanvas->getUNOCanvas().is() )
61             {
62                 mxGraphicDevice = mpCanvas->getUNOCanvas()->getDevice();
63             }
64 
65             ::canvas::tools::initRenderState( maRenderState );
66         }
67 
setTransformation(const::basegfx::B2DHomMatrix & rMatrix)68         void CanvasGraphicHelper::setTransformation( const ::basegfx::B2DHomMatrix& rMatrix )
69         {
70             ::canvas::tools::setRenderStateTransform( maRenderState, rMatrix );
71         }
72 
getTransformation() const73         ::basegfx::B2DHomMatrix CanvasGraphicHelper::getTransformation() const
74         {
75             ::basegfx::B2DHomMatrix aMatrix;
76             return ::canvas::tools::getRenderStateTransform( aMatrix,
77                                                              maRenderState );
78         }
79 
setClip(const::basegfx::B2DPolyPolygon & rClipPoly)80         void CanvasGraphicHelper::setClip( const ::basegfx::B2DPolyPolygon& rClipPoly )
81         {
82             // TODO(T3): not thread-safe. B2DPolyPolygon employs copy-on-write
83             maClipPolyPolygon.reset( rClipPoly );
84             maRenderState.Clip.clear();
85         }
86 
setClip()87         void CanvasGraphicHelper::setClip()
88         {
89             maClipPolyPolygon.reset();
90             maRenderState.Clip.clear();
91         }
92 
getClip() const93         ::basegfx::B2DPolyPolygon const* CanvasGraphicHelper::getClip() const
94         {
95             return !maClipPolyPolygon ? NULL : &(*maClipPolyPolygon);
96         }
97 
getRenderState() const98         const rendering::RenderState& CanvasGraphicHelper::getRenderState() const
99         {
100             if( maClipPolyPolygon && !maRenderState.Clip.is() )
101             {
102                 uno::Reference< rendering::XCanvas > xCanvas( mpCanvas->getUNOCanvas() );
103                 if( !xCanvas.is() )
104                     return maRenderState;
105 
106                 maRenderState.Clip = ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon(
107                     xCanvas->getDevice(),
108                     *maClipPolyPolygon );
109             }
110 
111             return maRenderState;
112         }
113 
setRGBAColor(Color::IntSRGBA aColor)114         void CanvasGraphicHelper::setRGBAColor( Color::IntSRGBA aColor )
115         {
116             maRenderState.DeviceColor = tools::intSRGBAToDoubleSequence( mxGraphicDevice,
117                                                                          aColor );
118         }
119 
getRGBAColor() const120         Color::IntSRGBA CanvasGraphicHelper::getRGBAColor() const
121         {
122             return tools::doubleSequenceToIntSRGBA( mxGraphicDevice,
123                                                     maRenderState.DeviceColor );
124         }
125 
setCompositeOp(CompositeOp aOp)126         void CanvasGraphicHelper::setCompositeOp( CompositeOp aOp )
127         {
128             maRenderState.CompositeOperation = (sal_Int8)aOp;
129         }
130 
getCompositeOp() const131         CanvasGraphic::CompositeOp CanvasGraphicHelper::getCompositeOp() const
132         {
133             return static_cast<CompositeOp>(maRenderState.CompositeOperation);
134         }
135 
getCanvas() const136         CanvasSharedPtr CanvasGraphicHelper::getCanvas() const
137         {
138             return mpCanvas;
139         }
140 
getGraphicDevice() const141         uno::Reference< rendering::XGraphicDevice >	CanvasGraphicHelper::getGraphicDevice() const
142         {
143             return mxGraphicDevice;
144         }
145 
146     }
147 }
148