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 "PresenterGeometryHelper.hxx"
23 #include <com/sun/star/rendering/CompositeOperation.hpp>
24 #include <osl/diagnose.h>
26 using namespace ::com::sun::star
;
27 using namespace ::com::sun::star::uno
;
29 namespace sdext::presenter
{
31 PresenterCanvasHelper::PresenterCanvasHelper()
33 geometry::AffineMatrix2D(1,0,0, 0,1,0),
36 geometry::AffineMatrix2D(1,0,0, 0,1,0),
39 rendering::CompositeOperation::SOURCE
)
43 PresenterCanvasHelper::~PresenterCanvasHelper()
47 void PresenterCanvasHelper::Paint (
48 const SharedBitmapDescriptor
& rpBitmap
,
49 const css::uno::Reference
<css::rendering::XCanvas
>& rxCanvas
,
50 const css::awt::Rectangle
& rRepaintBox
,
51 const css::awt::Rectangle
& rOuterBoundingBox
,
52 const css::awt::Rectangle
& rContentBoundingBox
) const
54 PaintRectangle(rpBitmap
,rxCanvas
,rRepaintBox
,rOuterBoundingBox
,rContentBoundingBox
,
55 maDefaultViewState
, maDefaultRenderState
);
58 void PresenterCanvasHelper::PaintRectangle (
59 const SharedBitmapDescriptor
& rpBitmap
,
60 const css::uno::Reference
<css::rendering::XCanvas
>& rxCanvas
,
61 const css::awt::Rectangle
& rRepaintBox
,
62 const css::awt::Rectangle
& rOuterBoundingBox
,
63 const css::awt::Rectangle
& rContentBoundingBox
,
64 const css::rendering::ViewState
& rDefaultViewState
,
65 const css::rendering::RenderState
& rDefaultRenderState
)
70 if ( ! rxCanvas
.is() || ! rxCanvas
->getDevice().is())
73 // Create a clip polypolygon that has the content box as hole.
74 ::std::vector
<awt::Rectangle
> aRectangles
;
75 aRectangles
.reserve(2);
76 aRectangles
.push_back(
77 PresenterGeometryHelper::Intersection(rRepaintBox
, rOuterBoundingBox
));
78 if (rContentBoundingBox
.Width
> 0 && rContentBoundingBox
.Height
> 0)
79 aRectangles
.push_back(
80 PresenterGeometryHelper::Intersection(rRepaintBox
, rContentBoundingBox
));
81 Reference
<rendering::XPolyPolygon2D
> xPolyPolygon (
82 PresenterGeometryHelper::CreatePolygon(
84 rxCanvas
->getDevice()));
85 if ( ! xPolyPolygon
.is())
87 xPolyPolygon
->setFillRule(rendering::FillRule_EVEN_ODD
);
89 if (rpBitmap
->GetNormalBitmap().is())
91 if (rpBitmap
->meHorizontalTexturingMode
== PresenterBitmapDescriptor::Repeat
92 || rpBitmap
->meVerticalTexturingMode
== PresenterBitmapDescriptor::Repeat
)
95 rpBitmap
->GetNormalBitmap(),
101 rDefaultRenderState
);
106 rpBitmap
->GetNormalBitmap(),
107 awt::Point(rOuterBoundingBox
.X
, rOuterBoundingBox
.Y
),
112 rDefaultRenderState
);
118 rpBitmap
->maReplacementColor
,
123 rDefaultRenderState
);
127 void PresenterCanvasHelper::PaintTiledBitmap (
128 const css::uno::Reference
<css::rendering::XBitmap
>& rxTexture
,
129 const css::uno::Reference
<css::rendering::XCanvas
>& rxCanvas
,
130 const css::awt::Rectangle
& rRepaintBox
,
131 const css::uno::Reference
<css::rendering::XPolyPolygon2D
>& rxPolygon
,
132 const css::awt::Rectangle
& rHole
,
133 const css::rendering::ViewState
& rDefaultViewState
,
134 const css::rendering::RenderState
& rDefaultRenderState
)
136 if ( ! rxCanvas
.is() || ! rxCanvas
->getDevice().is())
139 if ( ! rxTexture
.is())
142 if ( ! rxPolygon
.is())
145 rendering::ViewState
aViewState (rDefaultViewState
);
146 aViewState
.Clip
= rxPolygon
;
148 // Create a local render state at which the location of the bitmap is
150 rendering::RenderState
aRenderState (rDefaultRenderState
);
152 // Tile the bitmap over the repaint box.
153 const geometry::IntegerSize2D
aBitmapSize (rxTexture
->getSize());
154 if( aBitmapSize
.Width
< 1 || aBitmapSize
.Height
< 1)
157 const sal_Int32 nLeft
= (rRepaintBox
.X
/ aBitmapSize
.Width
) * aBitmapSize
.Width
;
158 const sal_Int32 nTop
= (rRepaintBox
.Y
/ aBitmapSize
.Height
) * aBitmapSize
.Height
;
159 const sal_Int32 nRight
= ((rRepaintBox
.X
+ rRepaintBox
.Width
- 1 + aBitmapSize
.Width
- 1)
160 / aBitmapSize
.Width
) * aBitmapSize
.Width
;
161 const sal_Int32 nBottom
= ((rRepaintBox
.Y
+ rRepaintBox
.Height
- 1 + aBitmapSize
.Height
- 1)
162 / aBitmapSize
.Height
) * aBitmapSize
.Height
;
164 for (sal_Int32 nY
=nTop
; nY
<=nBottom
; nY
+=aBitmapSize
.Height
)
165 for (sal_Int32 nX
=nLeft
; nX
<=nRight
; nX
+=aBitmapSize
.Width
)
167 if (PresenterGeometryHelper::IsInside(
168 awt::Rectangle(nX
,nY
,aBitmapSize
.Width
,aBitmapSize
.Height
),
173 aRenderState
.AffineTransform
.m02
= nX
;
174 aRenderState
.AffineTransform
.m12
= nY
;
175 rxCanvas
->drawBitmap(
182 void PresenterCanvasHelper::PaintBitmap (
183 const css::uno::Reference
<css::rendering::XBitmap
>& rxBitmap
,
184 const awt::Point
& rLocation
,
185 const css::uno::Reference
<css::rendering::XCanvas
>& rxCanvas
,
186 const css::awt::Rectangle
& rRepaintBox
,
187 const css::uno::Reference
<css::rendering::XPolyPolygon2D
>& rxPolygon
,
188 const css::rendering::ViewState
& rDefaultViewState
,
189 const css::rendering::RenderState
& rDefaultRenderState
)
191 if ( ! rxCanvas
.is() || ! rxCanvas
->getDevice().is())
194 if ( ! rxBitmap
.is())
197 if ( ! rxPolygon
.is())
200 // Set the repaint box as clip rectangle at the view state.
201 rendering::ViewState
aViewState (rDefaultViewState
);
202 aViewState
.Clip
= PresenterGeometryHelper::CreatePolygon(rRepaintBox
, rxCanvas
->getDevice());
204 // Setup the rendering state so that the bitmap is painted top left in
205 // the polygon bounding box.
206 rendering::RenderState
aRenderState (rDefaultRenderState
);
207 aRenderState
.AffineTransform
= geometry::AffineMatrix2D(1,0, rLocation
.X
, 0,1,rLocation
.Y
);
208 aRenderState
.Clip
= rxPolygon
;
210 rxCanvas
->drawBitmap(
216 void PresenterCanvasHelper::PaintColor (
217 const css::util::Color nColor
,
218 const css::uno::Reference
<css::rendering::XCanvas
>& rxCanvas
,
219 const css::awt::Rectangle
& rRepaintBox
,
220 const css::uno::Reference
<css::rendering::XPolyPolygon2D
>& rxPolygon
,
221 const css::rendering::ViewState
& rDefaultViewState
,
222 const css::rendering::RenderState
& rDefaultRenderState
)
224 if ( ! rxCanvas
.is() || ! rxCanvas
->getDevice().is())
227 if ( ! rxPolygon
.is())
230 // Set the repaint box as clip rectangle at the view state.
231 rendering::ViewState
aViewState (rDefaultViewState
);
232 aViewState
.Clip
= PresenterGeometryHelper::CreatePolygon(rRepaintBox
, rxCanvas
->getDevice());
234 // Setup the rendering state to use the given color.
235 rendering::RenderState
aRenderState (rDefaultRenderState
);
236 SetDeviceColor(aRenderState
, nColor
);
238 rxCanvas
->fillPolyPolygon(
244 void PresenterCanvasHelper::SetDeviceColor(
245 rendering::RenderState
& rRenderState
,
246 const util::Color aColor
)
248 // Other component counts then 4 (RGBA) are not accepted (anymore).
250 OSL_ASSERT(rRenderState
.DeviceColor
.getLength() == 4);
251 if (rRenderState
.DeviceColor
.getLength() == 4)
253 auto pDeviceColor
= rRenderState
.DeviceColor
.getArray();
254 pDeviceColor
[0] = ((aColor
>> 16) & 0x0ff) / 255.0;
255 pDeviceColor
[1] = ((aColor
>> 8) & 0x0ff) / 255.0;
256 pDeviceColor
[2] = ((aColor
>> 0) & 0x0ff) / 255.0;
257 pDeviceColor
[3] = 1.0 - ((aColor
>> 24) & 0x0ff) / 255.0;
261 css::geometry::RealRectangle2D
PresenterCanvasHelper::GetTextBoundingBox (
262 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
,
263 const OUString
& rsText
,
264 const sal_Int8 nTextDirection
)
266 if (rxFont
.is() && !rsText
.isEmpty())
268 rendering::StringContext
aContext (rsText
, 0, rsText
.getLength());
269 Reference
<rendering::XTextLayout
> xLayout (
270 rxFont
->createTextLayout(aContext
, nTextDirection
, 0));
271 return xLayout
->queryTextBounds();
275 return geometry::RealRectangle2D(0,0,0,0);
279 css::geometry::RealSize2D
PresenterCanvasHelper::GetTextSize (
280 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
,
281 const OUString
& rsText
)
283 const geometry::RealRectangle2D
aTextBBox (GetTextBoundingBox(rxFont
, rsText
));
284 return css::geometry::RealSize2D(aTextBBox
.X2
- aTextBBox
.X1
, aTextBBox
.Y2
- aTextBBox
.Y1
);
287 } // end of namespace sdext::presenter
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */