1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "PresenterCanvasHelper.hxx"
22 #include "PresenterController.hxx"
23 #include "PresenterGeometryHelper.hxx"
24 #include <com/sun/star/rendering/CompositeOperation.hpp>
25 #include <com/sun/star/rendering/TextDirection.hpp>
26 #include <com/sun/star/rendering/TexturingMode.hpp>
28 using namespace ::com::sun::star
;
29 using namespace ::com::sun::star::uno
;
31 namespace sdext
{ namespace presenter
{
33 PresenterCanvasHelper::PresenterCanvasHelper()
35 geometry::AffineMatrix2D(1,0,0, 0,1,0),
38 geometry::AffineMatrix2D(1,0,0, 0,1,0),
41 rendering::CompositeOperation::SOURCE
)
45 PresenterCanvasHelper::~PresenterCanvasHelper()
49 void PresenterCanvasHelper::Paint (
50 const SharedBitmapDescriptor
& rpBitmap
,
51 const css::uno::Reference
<css::rendering::XCanvas
>& rxCanvas
,
52 const css::awt::Rectangle
& rRepaintBox
,
53 const css::awt::Rectangle
& rOuterBoundingBox
,
54 const css::awt::Rectangle
& rContentBoundingBox
) const
56 PaintRectangle(rpBitmap
,rxCanvas
,rRepaintBox
,rOuterBoundingBox
,rContentBoundingBox
,
57 maDefaultViewState
, maDefaultRenderState
);
60 void PresenterCanvasHelper::PaintRectangle (
61 const SharedBitmapDescriptor
& rpBitmap
,
62 const css::uno::Reference
<css::rendering::XCanvas
>& rxCanvas
,
63 const css::awt::Rectangle
& rRepaintBox
,
64 const css::awt::Rectangle
& rOuterBoundingBox
,
65 const css::awt::Rectangle
& rContentBoundingBox
,
66 const css::rendering::ViewState
& rDefaultViewState
,
67 const css::rendering::RenderState
& rDefaultRenderState
)
69 if (rpBitmap
.get() == NULL
)
72 if ( ! rxCanvas
.is() || ! rxCanvas
->getDevice().is())
75 // Create a clip polypolygon that has the content box as hole.
76 ::std::vector
<awt::Rectangle
> aRectangles
;
77 aRectangles
.reserve(2);
78 aRectangles
.push_back(
79 PresenterGeometryHelper::Intersection(rRepaintBox
, rOuterBoundingBox
));
80 if (rContentBoundingBox
.Width
> 0 && rContentBoundingBox
.Height
> 0)
81 aRectangles
.push_back(
82 PresenterGeometryHelper::Intersection(rRepaintBox
, rContentBoundingBox
));
83 Reference
<rendering::XPolyPolygon2D
> xPolyPolygon (
84 PresenterGeometryHelper::CreatePolygon(
86 rxCanvas
->getDevice()));
87 if ( ! xPolyPolygon
.is())
89 xPolyPolygon
->setFillRule(rendering::FillRule_EVEN_ODD
);
91 if (rpBitmap
->GetNormalBitmap().is())
93 if (rpBitmap
->meHorizontalTexturingMode
== PresenterBitmapDescriptor::Repeat
94 || rpBitmap
->meVerticalTexturingMode
== PresenterBitmapDescriptor::Repeat
)
97 Reference
<rendering::XBitmap
>(rpBitmap
->GetNormalBitmap(), UNO_QUERY
),
103 rDefaultRenderState
);
108 Reference
<rendering::XBitmap
>(rpBitmap
->GetNormalBitmap(), UNO_QUERY
),
109 awt::Point(rOuterBoundingBox
.X
, rOuterBoundingBox
.Y
),
114 rDefaultRenderState
);
120 rpBitmap
->maReplacementColor
,
125 rDefaultRenderState
);
129 void PresenterCanvasHelper::PaintTiledBitmap (
130 const css::uno::Reference
<css::rendering::XBitmap
>& rxTexture
,
131 const css::uno::Reference
<css::rendering::XCanvas
>& rxCanvas
,
132 const css::awt::Rectangle
& rRepaintBox
,
133 const css::uno::Reference
<css::rendering::XPolyPolygon2D
>& rxPolygon
,
134 const css::awt::Rectangle
& rHole
,
135 const css::rendering::ViewState
& rDefaultViewState
,
136 const css::rendering::RenderState
& rDefaultRenderState
)
138 if ( ! rxCanvas
.is() || ! rxCanvas
->getDevice().is())
141 if ( ! rxTexture
.is())
144 if ( ! rxPolygon
.is())
147 rendering::ViewState
aViewState (rDefaultViewState
);
148 aViewState
.Clip
= rxPolygon
;
150 // Create a local render state at which the location of the bitmap is
152 rendering::RenderState
aRenderState (rDefaultRenderState
);
154 // Tile the bitmap over the repaint box.
155 const geometry::IntegerSize2D
aBitmapSize (rxTexture
->getSize());
156 if( aBitmapSize
.Width
< 1 || aBitmapSize
.Height
< 1)
159 const sal_Int32 nLeft
= (rRepaintBox
.X
/ aBitmapSize
.Width
) * aBitmapSize
.Width
;
160 const sal_Int32 nTop
= (rRepaintBox
.Y
/ aBitmapSize
.Height
) * aBitmapSize
.Height
;
161 const sal_Int32 nRight
= ((rRepaintBox
.X
+ rRepaintBox
.Width
- 1 + aBitmapSize
.Width
- 1)
162 / aBitmapSize
.Width
) * aBitmapSize
.Width
;
163 const sal_Int32 nBottom
= ((rRepaintBox
.Y
+ rRepaintBox
.Height
- 1 + aBitmapSize
.Height
- 1)
164 / aBitmapSize
.Height
) * aBitmapSize
.Height
;
166 for (sal_Int32 nY
=nTop
; nY
<=nBottom
; nY
+=aBitmapSize
.Height
)
167 for (sal_Int32 nX
=nLeft
; nX
<=nRight
; nX
+=aBitmapSize
.Width
)
169 if (PresenterGeometryHelper::IsInside(
170 awt::Rectangle(nX
,nY
,aBitmapSize
.Width
,aBitmapSize
.Height
),
175 aRenderState
.AffineTransform
.m02
= nX
;
176 aRenderState
.AffineTransform
.m12
= nY
;
177 rxCanvas
->drawBitmap(
184 void PresenterCanvasHelper::PaintBitmap (
185 const css::uno::Reference
<css::rendering::XBitmap
>& rxBitmap
,
186 const awt::Point
& rLocation
,
187 const css::uno::Reference
<css::rendering::XCanvas
>& rxCanvas
,
188 const css::awt::Rectangle
& rRepaintBox
,
189 const css::uno::Reference
<css::rendering::XPolyPolygon2D
>& rxPolygon
,
190 const css::rendering::ViewState
& rDefaultViewState
,
191 const css::rendering::RenderState
& rDefaultRenderState
)
193 if ( ! rxCanvas
.is() || ! rxCanvas
->getDevice().is())
196 if ( ! rxBitmap
.is())
199 if ( ! rxPolygon
.is())
202 // Set the repaint box as clip rectangle at the view state.
203 rendering::ViewState
aViewState (rDefaultViewState
);
204 aViewState
.Clip
= PresenterGeometryHelper::CreatePolygon(rRepaintBox
, rxCanvas
->getDevice());
206 // Setup the rendering state so that the bitmap is painted top left in
207 // the polygon bounding box.
208 rendering::RenderState
aRenderState (rDefaultRenderState
);
209 aRenderState
.AffineTransform
= geometry::AffineMatrix2D(1,0, rLocation
.X
, 0,1,rLocation
.Y
);
210 aRenderState
.Clip
= rxPolygon
;
212 rxCanvas
->drawBitmap(
218 void PresenterCanvasHelper::PaintColor (
219 const css::util::Color nColor
,
220 const css::uno::Reference
<css::rendering::XCanvas
>& rxCanvas
,
221 const css::awt::Rectangle
& rRepaintBox
,
222 const css::uno::Reference
<css::rendering::XPolyPolygon2D
>& rxPolygon
,
223 const css::rendering::ViewState
& rDefaultViewState
,
224 const css::rendering::RenderState
& rDefaultRenderState
)
226 if ( ! rxCanvas
.is() || ! rxCanvas
->getDevice().is())
229 if ( ! rxPolygon
.is())
232 // Set the repaint box as clip rectangle at the view state.
233 rendering::ViewState
aViewState (rDefaultViewState
);
234 aViewState
.Clip
= PresenterGeometryHelper::CreatePolygon(rRepaintBox
, rxCanvas
->getDevice());
236 // Setup the rendering state to use the given color.
237 rendering::RenderState
aRenderState (rDefaultRenderState
);
238 SetDeviceColor(aRenderState
, nColor
);
240 rxCanvas
->fillPolyPolygon(
246 void PresenterCanvasHelper::SetDeviceColor(
247 rendering::RenderState
& rRenderState
,
248 const util::Color aColor
)
250 // Other component counts then 4 (RGBA) are not accepted (anymore).
252 OSL_ASSERT(rRenderState
.DeviceColor
.getLength() == 4);
253 if (rRenderState
.DeviceColor
.getLength() == 4)
255 rRenderState
.DeviceColor
[0] = ((aColor
>> 16) & 0x0ff) / 255.0;
256 rRenderState
.DeviceColor
[1] = ((aColor
>> 8) & 0x0ff) / 255.0;
257 rRenderState
.DeviceColor
[2] = ((aColor
>> 0) & 0x0ff) / 255.0;
258 rRenderState
.DeviceColor
[3] = 1.0 - ((aColor
>> 24) & 0x0ff) / 255.0;
262 css::geometry::RealRectangle2D
PresenterCanvasHelper::GetTextBoundingBox (
263 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
,
264 const OUString
& rsText
,
265 const sal_Int8 nTextDirection
)
267 if (rxFont
.is() && !rsText
.isEmpty())
269 rendering::StringContext
aContext (rsText
, 0, rsText
.getLength());
270 Reference
<rendering::XTextLayout
> xLayout (
271 rxFont
->createTextLayout(aContext
, nTextDirection
, 0));
272 return xLayout
->queryTextBounds();
276 return geometry::RealRectangle2D(0,0,0,0);
280 css::geometry::RealSize2D
PresenterCanvasHelper::GetTextSize (
281 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
,
282 const OUString
& rsText
,
283 const sal_Int8 nTextDirection
)
285 const geometry::RealRectangle2D
aTextBBox (GetTextBoundingBox(rxFont
, rsText
, nTextDirection
));
286 return css::geometry::RealSize2D(aTextBBox
.X2
- aTextBBox
.X1
, aTextBBox
.Y2
- aTextBBox
.Y1
);
289 } } // end of namespace sdext::presenter
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */