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 <rtl/math.hxx>
28 
29 #include <com/sun/star/rendering/XCanvas.hpp>
30 #include <com/sun/star/rendering/PathJoinType.hpp>
31 #include <com/sun/star/rendering/PathCapType.hpp>
32 
33 #include <basegfx/matrix/b2dhommatrix.hxx>
34 #include <basegfx/tools/canvastools.hxx>
35 
36 #include "implpolypolygon.hxx"
37 #include "tools.hxx"
38 
39 
40 using namespace ::com::sun::star;
41 
42 
43 namespace cppcanvas
44 {
45     namespace internal
46     {
ImplPolyPolygon(const CanvasSharedPtr & rParentCanvas,const uno::Reference<rendering::XPolyPolygon2D> & rPolyPoly)47         ImplPolyPolygon::ImplPolyPolygon( const CanvasSharedPtr& rParentCanvas,
48                                           const uno::Reference< rendering::XPolyPolygon2D >& rPolyPoly ) :
49             CanvasGraphicHelper( rParentCanvas ),
50             mxPolyPoly( rPolyPoly ),
51             maStrokeAttributes(1.0,
52                                10.0,
53                                uno::Sequence< double >(),
54                                uno::Sequence< double >(),
55                                rendering::PathCapType::ROUND,
56                                rendering::PathCapType::ROUND,
57                                rendering::PathJoinType::ROUND ),
58             maFillColor(),
59             maStrokeColor(),
60             mbFillColorSet( false ),
61             mbStrokeColorSet( false )
62         {
63             OSL_ENSURE( mxPolyPoly.is(), "PolyPolygonImpl::PolyPolygonImpl: no valid polygon" );
64         }
65 
~ImplPolyPolygon()66         ImplPolyPolygon::~ImplPolyPolygon()
67         {
68         }
69 
addPolygon(const::basegfx::B2DPolygon & rPoly)70         void ImplPolyPolygon::addPolygon( const ::basegfx::B2DPolygon& rPoly )
71         {
72             OSL_ENSURE( mxPolyPoly.is(),
73                         "ImplPolyPolygon::addPolygon(): Invalid polygon" );
74 
75             if( !mxPolyPoly.is() )
76                 return;
77 
78             uno::Reference< rendering::XGraphicDevice > xDevice( getGraphicDevice() );
79 
80             OSL_ENSURE( xDevice.is(),
81                         "ImplPolyPolygon::addPolygon(): Invalid graphic device" );
82 
83             if( !xDevice.is() )
84                 return;
85 
86             mxPolyPoly->addPolyPolygon( geometry::RealPoint2D(0.0, 0.0),
87                                         ::basegfx::unotools::xPolyPolygonFromB2DPolygon(
88                                             xDevice,
89                                             rPoly) );
90         }
91 
addPolyPolygon(const::basegfx::B2DPolyPolygon & rPoly)92         void ImplPolyPolygon::addPolyPolygon( const ::basegfx::B2DPolyPolygon& rPoly )
93         {
94             OSL_ENSURE( mxPolyPoly.is(),
95                         "ImplPolyPolygon::addPolyPolygon(): Invalid polygon" );
96 
97             if( !mxPolyPoly.is() )
98                 return;
99 
100             uno::Reference< rendering::XGraphicDevice > xDevice( getGraphicDevice() );
101 
102             OSL_ENSURE( xDevice.is(),
103                         "ImplPolyPolygon::addPolyPolygon(): Invalid graphic device" );
104 
105             if( !xDevice.is() )
106                 return;
107 
108             mxPolyPoly->addPolyPolygon( geometry::RealPoint2D(0.0, 0.0),
109                                         ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon(
110                                             xDevice,
111                                             rPoly) );
112         }
113 
setRGBAFillColor(Color::IntSRGBA aColor)114         void ImplPolyPolygon::setRGBAFillColor( Color::IntSRGBA aColor )
115         {
116             maFillColor = tools::intSRGBAToDoubleSequence( getGraphicDevice(),
117                                                            aColor );
118             mbFillColorSet = true;
119         }
120 
setRGBALineColor(Color::IntSRGBA aColor)121         void ImplPolyPolygon::setRGBALineColor( Color::IntSRGBA aColor )
122         {
123             maStrokeColor = tools::intSRGBAToDoubleSequence( getGraphicDevice(),
124                                                              aColor );
125             mbStrokeColorSet = true;
126         }
127 
getRGBAFillColor() const128         Color::IntSRGBA ImplPolyPolygon::getRGBAFillColor() const
129         {
130             return tools::doubleSequenceToIntSRGBA( getGraphicDevice(),
131                                                     maFillColor );
132         }
133 
getRGBALineColor() const134         Color::IntSRGBA ImplPolyPolygon::getRGBALineColor() const
135         {
136             return tools::doubleSequenceToIntSRGBA( getGraphicDevice(),
137                                                     maStrokeColor );
138         }
139 
setStrokeWidth(const double & rStrokeWidth)140         void ImplPolyPolygon::setStrokeWidth( const double& rStrokeWidth )
141         {
142             maStrokeAttributes.StrokeWidth = rStrokeWidth;
143         }
144 
getStrokeWidth() const145         double ImplPolyPolygon::getStrokeWidth() const
146         {
147             return maStrokeAttributes.StrokeWidth;
148         }
149 
draw() const150         bool ImplPolyPolygon::draw() const
151         {
152             CanvasSharedPtr pCanvas( getCanvas() );
153 
154             OSL_ENSURE( pCanvas.get() != NULL &&
155                         pCanvas->getUNOCanvas().is(),
156                         "ImplBitmap::draw: invalid canvas" );
157 
158             if( pCanvas.get() == NULL ||
159                 !pCanvas->getUNOCanvas().is() )
160                 return false;
161 
162             if( mbFillColorSet )
163             {
164                 rendering::RenderState aLocalState( getRenderState() );
165                 aLocalState.DeviceColor = maFillColor;
166 
167                 pCanvas->getUNOCanvas()->fillPolyPolygon( mxPolyPoly,
168                                                           pCanvas->getViewState(),
169                                                           aLocalState );
170             }
171 
172             if( mbStrokeColorSet )
173             {
174                 rendering::RenderState aLocalState( getRenderState() );
175                 aLocalState.DeviceColor = maStrokeColor;
176 
177                 if( ::rtl::math::approxEqual(maStrokeAttributes.StrokeWidth, 1.0) )
178                     pCanvas->getUNOCanvas()->drawPolyPolygon( mxPolyPoly,
179                                                               pCanvas->getViewState(),
180                                                               aLocalState );
181                 else
182                     pCanvas->getUNOCanvas()->strokePolyPolygon( mxPolyPoly,
183                                                                 pCanvas->getViewState(),
184                                                                 aLocalState,
185                                                                 maStrokeAttributes );
186             }
187 
188             return true;
189         }
190 
getUNOPolyPolygon() const191         uno::Reference< rendering::XPolyPolygon2D > ImplPolyPolygon::getUNOPolyPolygon() const
192         {
193             return mxPolyPoly;
194         }
195 
196     }
197 }
198