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_sdext.hxx"
26 
27 #include "PresenterCanvasHelper.hxx"
28 
29 #include "PresenterController.hxx"
30 #include "PresenterGeometryHelper.hxx"
31 #include <com/sun/star/rendering/CompositeOperation.hpp>
32 #include <com/sun/star/rendering/TextDirection.hpp>
33 #include <com/sun/star/rendering/TexturingMode.hpp>
34 
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
37 
38 namespace sdext { namespace presenter {
39 
PresenterCanvasHelper(void)40 PresenterCanvasHelper::PresenterCanvasHelper (void)
41     : maDefaultViewState(
42         geometry::AffineMatrix2D(1,0,0, 0,1,0),
43         NULL),
44       maDefaultRenderState(
45           geometry::AffineMatrix2D(1,0,0, 0,1,0),
46           NULL,
47           Sequence<double>(4),
48           rendering::CompositeOperation::SOURCE)
49 {
50 }
51 
52 
53 
54 
~PresenterCanvasHelper(void)55 PresenterCanvasHelper::~PresenterCanvasHelper (void)
56 {
57 }
58 
59 
60 
61 
Paint(const SharedBitmapDescriptor & rpBitmap,const css::uno::Reference<css::rendering::XCanvas> & rxCanvas,const css::awt::Rectangle & rRepaintBox,const css::awt::Rectangle & rOuterBoundingBox,const css::awt::Rectangle & rContentBoundingBox) const62 void PresenterCanvasHelper::Paint (
63     const SharedBitmapDescriptor& rpBitmap,
64     const css::uno::Reference<css::rendering::XCanvas>& rxCanvas,
65     const css::awt::Rectangle& rRepaintBox,
66     const css::awt::Rectangle& rOuterBoundingBox,
67     const css::awt::Rectangle& rContentBoundingBox) const
68 {
69     PaintRectangle(rpBitmap,rxCanvas,rRepaintBox,rOuterBoundingBox,rContentBoundingBox,
70         maDefaultViewState, maDefaultRenderState);
71 }
72 
73 
74 
75 
PaintRectangle(const SharedBitmapDescriptor & rpBitmap,const css::uno::Reference<css::rendering::XCanvas> & rxCanvas,const css::awt::Rectangle & rRepaintBox,const css::awt::Rectangle & rOuterBoundingBox,const css::awt::Rectangle & rContentBoundingBox,const css::rendering::ViewState & rDefaultViewState,const css::rendering::RenderState & rDefaultRenderState)76 void PresenterCanvasHelper::PaintRectangle (
77     const SharedBitmapDescriptor& rpBitmap,
78     const css::uno::Reference<css::rendering::XCanvas>& rxCanvas,
79     const css::awt::Rectangle& rRepaintBox,
80     const css::awt::Rectangle& rOuterBoundingBox,
81     const css::awt::Rectangle& rContentBoundingBox,
82     const css::rendering::ViewState& rDefaultViewState,
83     const css::rendering::RenderState& rDefaultRenderState)
84 {
85     if (rpBitmap.get() == NULL)
86         return;
87 
88     if ( ! rxCanvas.is() || ! rxCanvas->getDevice().is())
89         return;
90 
91     // Create a clip polypolygon that has the content box as hole.
92     ::std::vector<awt::Rectangle> aRectangles;
93     aRectangles.reserve(2);
94     aRectangles.push_back(
95         PresenterGeometryHelper::Intersection(rRepaintBox, rOuterBoundingBox));
96     if (rContentBoundingBox.Width > 0 && rContentBoundingBox.Height > 0)
97         aRectangles.push_back(
98             PresenterGeometryHelper::Intersection(rRepaintBox, rContentBoundingBox));
99     Reference<rendering::XPolyPolygon2D> xPolyPolygon (
100         PresenterGeometryHelper::CreatePolygon(
101             aRectangles,
102             rxCanvas->getDevice()));
103     if ( ! xPolyPolygon.is())
104         return;
105     xPolyPolygon->setFillRule(rendering::FillRule_EVEN_ODD);
106 
107     if (rpBitmap->GetNormalBitmap().is())
108     {
109         if (rpBitmap->meHorizontalTexturingMode == PresenterBitmapDescriptor::Repeat
110             || rpBitmap->meVerticalTexturingMode == PresenterBitmapDescriptor::Repeat)
111         {
112             PaintTiledBitmap(
113                 Reference<rendering::XBitmap>(rpBitmap->GetNormalBitmap(), UNO_QUERY),
114                 rxCanvas,
115                 rRepaintBox,
116                 xPolyPolygon,
117                 rContentBoundingBox,
118                 rDefaultViewState,
119                 rDefaultRenderState);
120         }
121         else
122         {
123             PaintBitmap(
124                 Reference<rendering::XBitmap>(rpBitmap->GetNormalBitmap(), UNO_QUERY),
125                 awt::Point(rOuterBoundingBox.X, rOuterBoundingBox.Y),
126                 rxCanvas,
127                 rRepaintBox,
128                 xPolyPolygon,
129                 rDefaultViewState,
130                 rDefaultRenderState);
131         }
132     }
133     else
134     {
135         PaintColor(
136             rpBitmap->maReplacementColor,
137             rxCanvas,
138             rRepaintBox,
139             xPolyPolygon,
140             rDefaultViewState,
141             rDefaultRenderState);
142     }
143 }
144 
145 
146 
147 
PaintTiledBitmap(const css::uno::Reference<css::rendering::XBitmap> & rxTexture,const css::uno::Reference<css::rendering::XCanvas> & rxCanvas,const css::awt::Rectangle & rRepaintBox,const css::uno::Reference<css::rendering::XPolyPolygon2D> & rxPolygon,const css::awt::Rectangle & rHole,const css::rendering::ViewState & rDefaultViewState,const css::rendering::RenderState & rDefaultRenderState)148 void PresenterCanvasHelper::PaintTiledBitmap (
149     const css::uno::Reference<css::rendering::XBitmap>& rxTexture,
150     const css::uno::Reference<css::rendering::XCanvas>& rxCanvas,
151     const css::awt::Rectangle& rRepaintBox,
152     const css::uno::Reference<css::rendering::XPolyPolygon2D>& rxPolygon,
153     const css::awt::Rectangle& rHole,
154     const css::rendering::ViewState& rDefaultViewState,
155     const css::rendering::RenderState& rDefaultRenderState)
156 {
157     if ( ! rxCanvas.is() || ! rxCanvas->getDevice().is())
158         return;
159 
160     if ( ! rxTexture.is())
161         return;
162 
163     if ( ! rxPolygon.is())
164         return;
165 
166     rendering::ViewState aViewState (rDefaultViewState);
167     aViewState.Clip = rxPolygon;
168 
169     // Create a local render state at which the location of the bitmap is
170     // set.
171     rendering::RenderState aRenderState (rDefaultRenderState);
172 
173 
174     // Tile the bitmap over the repaint box.
175     const geometry::IntegerSize2D aBitmapSize (rxTexture->getSize());
176     const sal_Int32 nLeft = (rRepaintBox.X / aBitmapSize.Width) * aBitmapSize.Width;
177     const sal_Int32 nTop = (rRepaintBox.Y / aBitmapSize.Height) * aBitmapSize.Height;
178     const sal_Int32 nRight = ((rRepaintBox.X + rRepaintBox.Width - 1 + aBitmapSize.Width - 1)
179         / aBitmapSize.Width) * aBitmapSize.Width;
180     const sal_Int32 nBottom = ((rRepaintBox.Y + rRepaintBox.Height - 1 + aBitmapSize.Height - 1)
181         / aBitmapSize.Height) * aBitmapSize.Height;
182 
183     for (sal_Int32 nY=nTop; nY<=nBottom; nY+=aBitmapSize.Height)
184         for (sal_Int32 nX=nLeft; nX<=nRight; nX+=aBitmapSize.Width)
185         {
186             if (PresenterGeometryHelper::IsInside(
187                 awt::Rectangle(nX,nY,aBitmapSize.Width,aBitmapSize.Height),
188                 rHole))
189             {
190                 continue;
191             }
192             aRenderState.AffineTransform.m02 = nX;
193             aRenderState.AffineTransform.m12 = nY;
194             rxCanvas->drawBitmap(
195                 rxTexture,
196                 aViewState,
197                 aRenderState);
198         }
199 }
200 
201 
202 
203 
PaintBitmap(const css::uno::Reference<css::rendering::XBitmap> & rxBitmap,const awt::Point & rLocation,const css::uno::Reference<css::rendering::XCanvas> & rxCanvas,const css::awt::Rectangle & rRepaintBox,const css::uno::Reference<css::rendering::XPolyPolygon2D> & rxPolygon,const css::rendering::ViewState & rDefaultViewState,const css::rendering::RenderState & rDefaultRenderState)204 void PresenterCanvasHelper::PaintBitmap (
205     const css::uno::Reference<css::rendering::XBitmap>& rxBitmap,
206     const awt::Point& rLocation,
207     const css::uno::Reference<css::rendering::XCanvas>& rxCanvas,
208     const css::awt::Rectangle& rRepaintBox,
209     const css::uno::Reference<css::rendering::XPolyPolygon2D>& rxPolygon,
210     const css::rendering::ViewState& rDefaultViewState,
211     const css::rendering::RenderState& rDefaultRenderState)
212 {
213     if ( ! rxCanvas.is() || ! rxCanvas->getDevice().is())
214         return;
215 
216     if ( ! rxBitmap.is())
217         return;
218 
219     if ( ! rxPolygon.is())
220         return;
221 
222     // Set the repaint box as clip rectangle at the view state.
223     rendering::ViewState aViewState (rDefaultViewState);
224     aViewState.Clip = PresenterGeometryHelper::CreatePolygon(rRepaintBox, rxCanvas->getDevice());
225 
226 
227     // Setup the rendering state so that the bitmap is painted top left in
228     // the polygon bounding box.
229     rendering::RenderState aRenderState (rDefaultRenderState);
230     aRenderState.AffineTransform = geometry::AffineMatrix2D(1,0, rLocation.X, 0,1,rLocation.Y);
231     aRenderState.Clip = rxPolygon;
232 
233     rxCanvas->drawBitmap(
234         rxBitmap,
235         aViewState,
236         aRenderState);
237 }
238 
239 
240 
241 
PaintColor(const css::util::Color nColor,const css::uno::Reference<css::rendering::XCanvas> & rxCanvas,const css::awt::Rectangle & rRepaintBox,const css::uno::Reference<css::rendering::XPolyPolygon2D> & rxPolygon,const css::rendering::ViewState & rDefaultViewState,const css::rendering::RenderState & rDefaultRenderState)242 void PresenterCanvasHelper::PaintColor (
243     const css::util::Color nColor,
244     const css::uno::Reference<css::rendering::XCanvas>& rxCanvas,
245     const css::awt::Rectangle& rRepaintBox,
246     const css::uno::Reference<css::rendering::XPolyPolygon2D>& rxPolygon,
247     const css::rendering::ViewState& rDefaultViewState,
248     const css::rendering::RenderState& rDefaultRenderState)
249 {
250     if ( ! rxCanvas.is() || ! rxCanvas->getDevice().is())
251         return;
252 
253     if ( ! rxPolygon.is())
254         return;
255 
256     // Set the repaint box as clip rectangle at the view state.
257     rendering::ViewState aViewState (rDefaultViewState);
258     aViewState.Clip = PresenterGeometryHelper::CreatePolygon(rRepaintBox, rxCanvas->getDevice());
259 
260 
261     // Setup the rendering state to use the given color.
262     rendering::RenderState aRenderState (rDefaultRenderState);
263     SetDeviceColor(aRenderState, nColor);
264 
265     rxCanvas->fillPolyPolygon(
266         rxPolygon,
267         aViewState,
268         aRenderState);
269 }
270 
271 
272 
273 
SetDeviceColor(rendering::RenderState & rRenderState,const util::Color aColor)274 void PresenterCanvasHelper::SetDeviceColor(
275     rendering::RenderState& rRenderState,
276     const util::Color aColor)
277 {
278     // Other component counts then 4 (RGBA) are not accepted (anymore).
279 
280     OSL_ASSERT(rRenderState.DeviceColor.getLength() == 4);
281     if (rRenderState.DeviceColor.getLength() == 4)
282     {
283         rRenderState.DeviceColor[0] = ((aColor >> 16) & 0x0ff) / 255.0;
284         rRenderState.DeviceColor[1] = ((aColor >> 8) & 0x0ff) / 255.0;
285         rRenderState.DeviceColor[2] = ((aColor >> 0) & 0x0ff) / 255.0;
286         rRenderState.DeviceColor[3] = 1.0 - ((aColor >> 24) & 0x0ff) / 255.0;
287     }
288 }
289 
290 
291 
292 
GetTextBoundingBox(const css::uno::Reference<css::rendering::XCanvasFont> & rxFont,const::rtl::OUString & rsText,const sal_Int8 nTextDirection)293 css::geometry::RealRectangle2D PresenterCanvasHelper::GetTextBoundingBox (
294     const css::uno::Reference<css::rendering::XCanvasFont>& rxFont,
295     const ::rtl::OUString& rsText,
296     const sal_Int8 nTextDirection)
297 {
298     if (rxFont.is() && rsText.getLength() > 0)
299     {
300         rendering::StringContext aContext (rsText, 0, rsText.getLength());
301         Reference<rendering::XTextLayout> xLayout (
302             rxFont->createTextLayout(aContext, nTextDirection, 0));
303         return xLayout->queryTextBounds();
304     }
305     else
306     {
307         return geometry::RealRectangle2D(0,0,0,0);
308     }
309 }
310 
311 
312 
313 
GetTextSize(const css::uno::Reference<css::rendering::XCanvasFont> & rxFont,const::rtl::OUString & rsText,const sal_Int8 nTextDirection)314 css::geometry::RealSize2D PresenterCanvasHelper::GetTextSize (
315     const css::uno::Reference<css::rendering::XCanvasFont>& rxFont,
316     const ::rtl::OUString& rsText,
317     const sal_Int8 nTextDirection)
318 {
319     const geometry::RealRectangle2D aTextBBox (GetTextBoundingBox(rxFont, rsText, nTextDirection));
320     return css::geometry::RealSize2D(aTextBBox.X2 - aTextBBox.X1, aTextBBox.Y2 - aTextBBox.Y1);
321 }
322 
323 
324 } } // end of namespace sdext::presenter
325