bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / presenter / SlideRenderer.cxx
blobb60da234939a5555cf3195732e97c730dd8a7719
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
21 #include "facreg.hxx"
22 #include "sdpage.hxx"
23 #include <toolkit/helper/vclunohelper.hxx>
24 #include <com/sun/star/rendering/XBitmapCanvas.hpp>
25 #include <osl/mutex.hxx>
26 #include <vcl/svapp.hxx>
27 #include <cppcanvas/vclfactory.hxx>
28 #include <cppuhelper/supportsservice.hxx>
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::uno;
33 namespace sd { namespace presenter {
35 //===== SlideRenderer ==========================================================
37 SlideRenderer::SlideRenderer (const Reference<XComponentContext>& rxContext)
38 : SlideRendererInterfaceBase(m_aMutex),
39 maPreviewRenderer()
41 (void)rxContext;
44 SlideRenderer::~SlideRenderer()
48 void SAL_CALL SlideRenderer::disposing()
52 //----- XInitialization -------------------------------------------------------
54 void SAL_CALL SlideRenderer::initialize (const Sequence<Any>& rArguments)
55 throw (Exception, RuntimeException, std::exception)
57 ThrowIfDisposed();
59 if (rArguments.getLength() != 0)
61 throw RuntimeException("SlideRenderer: invalid number of arguments",
62 static_cast<XWeak*>(this));
66 OUString SlideRenderer::getImplementationName()
67 throw (css::uno::RuntimeException, std::exception)
69 return OUString("com.sun.star.comp.Draw.SlideRenderer");
72 sal_Bool SlideRenderer::supportsService(OUString const & ServiceName)
73 throw (css::uno::RuntimeException, std::exception)
75 return cppu::supportsService(this, ServiceName);
78 css::uno::Sequence<OUString> SlideRenderer::getSupportedServiceNames()
79 throw (css::uno::RuntimeException, std::exception)
81 return css::uno::Sequence<OUString>{"com.sun.star.drawing.SlideRenderer"};
84 //----- XSlideRenderer --------------------------------------------------------
86 Reference<awt::XBitmap> SlideRenderer::createPreview (
87 const Reference<drawing::XDrawPage>& rxSlide,
88 const awt::Size& rMaximalSize,
89 sal_Int16 nSuperSampleFactor)
90 throw (css::uno::RuntimeException, std::exception)
92 ThrowIfDisposed();
93 SolarMutexGuard aGuard;
95 return VCLUnoHelper::CreateBitmap(
96 CreatePreview(rxSlide, rMaximalSize, nSuperSampleFactor));
99 Reference<rendering::XBitmap> SlideRenderer::createPreviewForCanvas (
100 const Reference<drawing::XDrawPage>& rxSlide,
101 const awt::Size& rMaximalSize,
102 sal_Int16 nSuperSampleFactor,
103 const Reference<rendering::XCanvas>& rxCanvas)
104 throw (css::uno::RuntimeException, std::exception)
106 ThrowIfDisposed();
107 SolarMutexGuard aGuard;
109 cppcanvas::CanvasSharedPtr pCanvas (
110 cppcanvas::VCLFactory::createCanvas(rxCanvas));
111 if (pCanvas.get() != NULL)
112 return cppcanvas::VCLFactory::createBitmap(
113 pCanvas,
114 CreatePreview(rxSlide, rMaximalSize, nSuperSampleFactor))->getUNOBitmap();
115 else
116 return NULL;
119 awt::Size SAL_CALL SlideRenderer::calculatePreviewSize (
120 double nSlideAspectRatio,
121 const awt::Size& rMaximalSize)
122 throw (css::uno::RuntimeException, std::exception)
124 if (rMaximalSize.Width <= 0
125 || rMaximalSize.Height <= 0
126 || nSlideAspectRatio <= 0)
128 return awt::Size(0,0);
131 const double nWindowAspectRatio (double(rMaximalSize.Width) / double(rMaximalSize.Height));
132 if (nSlideAspectRatio < nWindowAspectRatio)
133 return awt::Size(
134 sal::static_int_cast<sal_Int32>(rMaximalSize.Height * nSlideAspectRatio),
135 rMaximalSize.Height);
136 else
137 return awt::Size(
138 rMaximalSize.Width,
139 sal::static_int_cast<sal_Int32>(rMaximalSize.Width / nSlideAspectRatio));
142 BitmapEx SlideRenderer::CreatePreview (
143 const Reference<drawing::XDrawPage>& rxSlide,
144 const awt::Size& rMaximalSize,
145 sal_Int16 nSuperSampleFactor)
146 throw (css::uno::RuntimeException,
147 std::exception)
149 const SdPage* pPage = SdPage::getImplementation(rxSlide);
150 if (pPage == NULL)
151 throw lang::IllegalArgumentException("SlideRenderer::createPreview() called with invalid slide",
152 static_cast<XWeak*>(this),
155 // Determine the size of the current slide and its aspect ratio.
156 Size aPageSize = pPage->GetSize();
157 if (aPageSize.Height() <= 0)
158 throw lang::IllegalArgumentException("SlideRenderer::createPreview() called with invalid size",
159 static_cast<XWeak*>(this),
162 // Compare with the aspect ratio of the window (which rMaximalSize
163 // assumed to be) and calculate the size of the preview so that it
164 // a) will have the aspect ratio of the page and
165 // b) will be as large as possible.
166 awt::Size aPreviewSize (calculatePreviewSize(
167 double(aPageSize.Width()) / double(aPageSize.Height()),
168 rMaximalSize));
169 if (aPreviewSize.Width <= 0 || aPreviewSize.Height <= 0)
170 return BitmapEx();
172 // Make sure that the super sample factor has a sane value.
173 sal_Int16 nFactor (nSuperSampleFactor);
174 if (nFactor < 1)
175 nFactor = 1;
176 else if (nFactor > 10)
177 nFactor = 10;
179 // Create the preview. When the super sample factor n is greater than 1
180 // then a preview is created in size (n*width, n*height) and then scaled
181 // down to (width, height). This is a poor mans antialiasing for the
182 // time being. When we have true antialiasing support this workaround
183 // can be removed.
184 const Image aPreview = maPreviewRenderer.RenderPage (
185 pPage,
186 Size(aPreviewSize.Width*nFactor, aPreviewSize.Height*nFactor),
187 OUString());
188 if (nFactor == 1)
189 return aPreview.GetBitmapEx();
190 else
192 BitmapEx aScaledPreview = aPreview.GetBitmapEx();
193 aScaledPreview.Scale(
194 Size(aPreviewSize.Width,aPreviewSize.Height),
195 BmpScaleFlag::BestQuality);
196 return aScaledPreview;
200 void SlideRenderer::ThrowIfDisposed()
201 throw (::com::sun::star::lang::DisposedException)
203 if (SlideRendererInterfaceBase::rBHelper.bDisposed || SlideRendererInterfaceBase::rBHelper.bInDispose)
205 throw lang::DisposedException ("SlideRenderer object has already been disposed",
206 static_cast<uno::XWeak*>(this));
210 } } // end of namespace ::sd::presenter
213 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
214 com_sun_star_comp_Draw_SlideRenderer_get_implementation(::com::sun::star::uno::XComponentContext* context,
215 ::com::sun::star::uno::Sequence<css::uno::Any> const &)
217 return cppu::acquire(new sd::presenter::SlideRenderer(context));
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */