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 "SlideRenderer.hxx"
23 #include <toolkit/helper/vclunohelper.hxx>
24 #include <com/sun/star/rendering/XBitmapCanvas.hpp>
25 #include <vcl/svapp.hxx>
26 #include <cppcanvas/vclfactory.hxx>
27 #include <cppuhelper/supportsservice.hxx>
29 using namespace ::com::sun::star
;
30 using namespace ::com::sun::star::uno
;
32 namespace sd
{ namespace presenter
{
34 //===== SlideRenderer ==========================================================
36 SlideRenderer::SlideRenderer ()
37 : SlideRendererInterfaceBase(m_aMutex
),
42 SlideRenderer::~SlideRenderer()
46 void SAL_CALL
SlideRenderer::disposing()
50 //----- XInitialization -------------------------------------------------------
52 void SAL_CALL
SlideRenderer::initialize (const Sequence
<Any
>& rArguments
)
56 if (rArguments
.hasElements())
58 throw RuntimeException("SlideRenderer: invalid number of arguments",
59 static_cast<XWeak
*>(this));
63 OUString
SlideRenderer::getImplementationName()
65 return OUString("com.sun.star.comp.Draw.SlideRenderer");
68 sal_Bool
SlideRenderer::supportsService(OUString
const & ServiceName
)
70 return cppu::supportsService(this, ServiceName
);
73 css::uno::Sequence
<OUString
> SlideRenderer::getSupportedServiceNames()
75 return css::uno::Sequence
<OUString
>{"com.sun.star.drawing.SlideRenderer"};
78 //----- XSlideRenderer --------------------------------------------------------
80 Reference
<awt::XBitmap
> SlideRenderer::createPreview (
81 const Reference
<drawing::XDrawPage
>& rxSlide
,
82 const awt::Size
& rMaximalSize
,
83 sal_Int16 nSuperSampleFactor
)
86 SolarMutexGuard aGuard
;
88 return VCLUnoHelper::CreateBitmap(
89 CreatePreview(rxSlide
, rMaximalSize
, nSuperSampleFactor
));
92 Reference
<rendering::XBitmap
> SlideRenderer::createPreviewForCanvas (
93 const Reference
<drawing::XDrawPage
>& rxSlide
,
94 const awt::Size
& rMaximalSize
,
95 sal_Int16 nSuperSampleFactor
,
96 const Reference
<rendering::XCanvas
>& rxCanvas
)
99 SolarMutexGuard aGuard
;
101 cppcanvas::CanvasSharedPtr
pCanvas (
102 cppcanvas::VCLFactory::createCanvas(rxCanvas
));
103 if (pCanvas
.get() != nullptr)
104 return cppcanvas::VCLFactory::createBitmap(
106 CreatePreview(rxSlide
, rMaximalSize
, nSuperSampleFactor
))->getUNOBitmap();
111 awt::Size SAL_CALL
SlideRenderer::calculatePreviewSize (
112 double nSlideAspectRatio
,
113 const awt::Size
& rMaximalSize
)
115 if (rMaximalSize
.Width
<= 0
116 || rMaximalSize
.Height
<= 0
117 || nSlideAspectRatio
<= 0)
119 return awt::Size(0,0);
122 const double nWindowAspectRatio (double(rMaximalSize
.Width
) / double(rMaximalSize
.Height
));
123 if (nSlideAspectRatio
< nWindowAspectRatio
)
125 sal::static_int_cast
<sal_Int32
>(rMaximalSize
.Height
* nSlideAspectRatio
),
126 rMaximalSize
.Height
);
130 sal::static_int_cast
<sal_Int32
>(rMaximalSize
.Width
/ nSlideAspectRatio
));
133 BitmapEx
SlideRenderer::CreatePreview (
134 const Reference
<drawing::XDrawPage
>& rxSlide
,
135 const awt::Size
& rMaximalSize
,
136 sal_Int16 nSuperSampleFactor
)
138 const SdPage
* pPage
= SdPage::getImplementation(rxSlide
);
139 if (pPage
== nullptr)
140 throw lang::IllegalArgumentException("SlideRenderer::createPreview() called with invalid slide",
141 static_cast<XWeak
*>(this),
144 // Determine the size of the current slide and its aspect ratio.
145 Size aPageSize
= pPage
->GetSize();
146 if (aPageSize
.Height() <= 0)
147 throw lang::IllegalArgumentException("SlideRenderer::createPreview() called with invalid size",
148 static_cast<XWeak
*>(this),
151 // Compare with the aspect ratio of the window (which rMaximalSize
152 // assumed to be) and calculate the size of the preview so that it
153 // a) will have the aspect ratio of the page and
154 // b) will be as large as possible.
155 awt::Size
aPreviewSize (calculatePreviewSize(
156 double(aPageSize
.Width()) / double(aPageSize
.Height()),
158 if (aPreviewSize
.Width
<= 0 || aPreviewSize
.Height
<= 0)
161 // Make sure that the super sample factor has a sane value.
162 sal_Int16
nFactor (nSuperSampleFactor
);
165 else if (nFactor
> 10)
168 // Create the preview. When the super sample factor n is greater than 1
169 // then a preview is created in size (n*width, n*height) and then scaled
170 // down to (width, height). This is a poor mans antialiasing for the
171 // time being. When we have true antialiasing support this workaround
173 const Image aPreview
= maPreviewRenderer
.RenderPage (
175 Size(aPreviewSize
.Width
*nFactor
, aPreviewSize
.Height
*nFactor
),
178 return aPreview
.GetBitmapEx();
181 BitmapEx aScaledPreview
= aPreview
.GetBitmapEx();
182 aScaledPreview
.Scale(
183 Size(aPreviewSize
.Width
,aPreviewSize
.Height
),
184 BmpScaleFlag::BestQuality
);
185 return aScaledPreview
;
189 void SlideRenderer::ThrowIfDisposed()
191 if (SlideRendererInterfaceBase::rBHelper
.bDisposed
|| SlideRendererInterfaceBase::rBHelper
.bInDispose
)
193 throw lang::DisposedException ("SlideRenderer object has already been disposed",
194 static_cast<uno::XWeak
*>(this));
198 } } // end of namespace ::sd::presenter
201 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
202 com_sun_star_comp_Draw_SlideRenderer_get_implementation(css::uno::XComponentContext
*,
203 css::uno::Sequence
<css::uno::Any
> const &)
205 return cppu::acquire(new sd::presenter::SlideRenderer
);
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */