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 #include "precompiled_sd.hxx"
25
26 #include "SlideRenderer.hxx"
27 #include "sdpage.hxx"
28 #include <toolkit/helper/vclunohelper.hxx>
29 #include <vos/mutex.hxx>
30 #include <vcl/svapp.hxx>
31 #include <cppcanvas/vclfactory.hxx>
32
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::uno;
35 using ::rtl::OUString;
36
37 namespace sd { namespace presenter {
38
39 //===== Service ===============================================================
40
SlideRenderer_createInstance(const Reference<XComponentContext> & rxContext)41 Reference<XInterface> SAL_CALL SlideRenderer_createInstance (
42 const Reference<XComponentContext>& rxContext)
43 {
44 return Reference<XInterface>(static_cast<XWeak*>(new SlideRenderer(rxContext)));
45 }
46
47
SlideRenderer_getImplementationName(void)48 ::rtl::OUString SlideRenderer_getImplementationName (void) throw(RuntimeException)
49 {
50 return OUString::createFromAscii("com.sun.star.comp.Draw.SlideRenderer");
51 }
52
53
SlideRenderer_getSupportedServiceNames(void)54 Sequence<rtl::OUString> SAL_CALL SlideRenderer_getSupportedServiceNames (void)
55 throw (RuntimeException)
56 {
57 static const ::rtl::OUString sServiceName(
58 ::rtl::OUString::createFromAscii("com.sun.star.drawing.SlideRenderer"));
59 return Sequence<rtl::OUString>(&sServiceName, 1);
60 }
61
62
63 //===== SlideRenderer ==========================================================
64
SlideRenderer(const Reference<XComponentContext> & rxContext)65 SlideRenderer::SlideRenderer (const Reference<XComponentContext>& rxContext)
66 : SlideRendererInterfaceBase(m_aMutex),
67 maPreviewRenderer()
68 {
69 (void)rxContext;
70 }
71
72
~SlideRenderer(void)73 SlideRenderer::~SlideRenderer (void)
74 {
75 }
76
77
disposing(void)78 void SAL_CALL SlideRenderer::disposing (void)
79 {
80 }
81
82
83 //----- XInitialization -------------------------------------------------------
84
initialize(const Sequence<Any> & rArguments)85 void SAL_CALL SlideRenderer::initialize (const Sequence<Any>& rArguments)
86 throw (Exception, RuntimeException)
87 {
88 ThrowIfDisposed();
89
90 if (rArguments.getLength() == 0)
91 {
92 try
93 {
94 }
95 catch (RuntimeException&)
96 {
97 throw;
98 }
99 }
100 else
101 {
102 throw RuntimeException(
103 OUString::createFromAscii("SlideRenderer: invalid number of arguments"),
104 static_cast<XWeak*>(this));
105 }
106 }
107
108
109 //----- XSlideRenderer --------------------------------------------------------
110
createPreview(const Reference<drawing::XDrawPage> & rxSlide,const awt::Size & rMaximalSize,sal_Int16 nSuperSampleFactor)111 Reference<awt::XBitmap> SlideRenderer::createPreview (
112 const Reference<drawing::XDrawPage>& rxSlide,
113 const awt::Size& rMaximalSize,
114 sal_Int16 nSuperSampleFactor)
115 throw (css::uno::RuntimeException)
116 {
117 ThrowIfDisposed();
118 ::vos::OGuard aGuard (Application::GetSolarMutex());
119
120 return VCLUnoHelper::CreateBitmap(
121 CreatePreview(rxSlide, rMaximalSize, nSuperSampleFactor));
122 }
123
124
createPreviewForCanvas(const Reference<drawing::XDrawPage> & rxSlide,const awt::Size & rMaximalSize,sal_Int16 nSuperSampleFactor,const Reference<rendering::XCanvas> & rxCanvas)125 Reference<rendering::XBitmap> SlideRenderer::createPreviewForCanvas (
126 const Reference<drawing::XDrawPage>& rxSlide,
127 const awt::Size& rMaximalSize,
128 sal_Int16 nSuperSampleFactor,
129 const Reference<rendering::XCanvas>& rxCanvas)
130 throw (css::uno::RuntimeException)
131 {
132 ThrowIfDisposed();
133 ::vos::OGuard aGuard (Application::GetSolarMutex());
134
135 cppcanvas::BitmapCanvasSharedPtr pCanvas (cppcanvas::VCLFactory::getInstance().createCanvas(
136 Reference<rendering::XBitmapCanvas>(rxCanvas, UNO_QUERY)));
137 if (pCanvas.get() != NULL)
138 return cppcanvas::VCLFactory::getInstance().createBitmap(
139 pCanvas,
140 CreatePreview(rxSlide, rMaximalSize, nSuperSampleFactor))->getUNOBitmap();
141 else
142 return NULL;
143 }
144
145
calculatePreviewSize(double nSlideAspectRatio,const awt::Size & rMaximalSize)146 awt::Size SAL_CALL SlideRenderer::calculatePreviewSize (
147 double nSlideAspectRatio,
148 const awt::Size& rMaximalSize)
149 throw (css::uno::RuntimeException)
150 {
151 if (rMaximalSize.Width <= 0
152 || rMaximalSize.Height <= 0
153 || nSlideAspectRatio <= 0)
154 {
155 return awt::Size(0,0);
156 }
157
158 const double nWindowAspectRatio (double(rMaximalSize.Width) / double(rMaximalSize.Height));
159 if (nSlideAspectRatio < nWindowAspectRatio)
160 return awt::Size(
161 sal::static_int_cast<sal_Int32>(rMaximalSize.Height * nSlideAspectRatio),
162 rMaximalSize.Height);
163 else
164 return awt::Size(
165 rMaximalSize.Width,
166 sal::static_int_cast<sal_Int32>(rMaximalSize.Width / nSlideAspectRatio));
167 }
168
169
170 //-----------------------------------------------------------------------------
171
CreatePreview(const Reference<drawing::XDrawPage> & rxSlide,const awt::Size & rMaximalSize,sal_Int16 nSuperSampleFactor)172 BitmapEx SlideRenderer::CreatePreview (
173 const Reference<drawing::XDrawPage>& rxSlide,
174 const awt::Size& rMaximalSize,
175 sal_Int16 nSuperSampleFactor)
176 throw (css::uno::RuntimeException)
177 {
178 const SdPage* pPage = SdPage::getImplementation(rxSlide);
179 if (pPage == NULL)
180 throw lang::IllegalArgumentException(
181 OUString::createFromAscii("SlideRenderer::createPreview() called with invalid slide"),
182 static_cast<XWeak*>(this),
183 0);
184
185 // Determine the size of the current slide and its aspect ratio.
186 Size aPageSize = pPage->GetSize();
187 if (aPageSize.Height() <= 0)
188 throw lang::IllegalArgumentException(
189 OUString::createFromAscii("SlideRenderer::createPreview() called with invalid size"),
190 static_cast<XWeak*>(this),
191 1);
192
193 // Compare with the aspect ratio of the window (which rMaximalSize
194 // assumed to be) and calculate the size of the preview so that it
195 // a) will have the aspect ratio of the page and
196 // b) will be as large as possible.
197 awt::Size aPreviewSize (calculatePreviewSize(
198 double(aPageSize.Width()) / double(aPageSize.Height()),
199 rMaximalSize));
200 if (aPreviewSize.Width <= 0 || aPreviewSize.Height <= 0)
201 return BitmapEx();
202
203 // Make sure that the super sample factor has a sane value.
204 sal_Int16 nFactor (nSuperSampleFactor);
205 if (nFactor < 1)
206 nFactor = 1;
207 else if (nFactor > 10)
208 nFactor = 10;
209
210 // Create the preview. When the super sample factor n is greater than 1
211 // then a preview is created in size (n*width, n*height) and then scaled
212 // down to (width, height). This is a poor mans antialiasing for the
213 // time being. When we have true antialiasing support this workaround
214 // can be removed.
215 const Image aPreview = maPreviewRenderer.RenderPage (
216 pPage,
217 Size(aPreviewSize.Width*nFactor, aPreviewSize.Height*nFactor),
218 ::rtl::OUString());
219 if (nFactor == 1)
220 return aPreview.GetBitmapEx();
221 else
222 {
223 BitmapEx aScaledPreview = aPreview.GetBitmapEx();
224 aScaledPreview.Scale(
225 Size(aPreviewSize.Width,aPreviewSize.Height),
226 BMP_SCALE_FASTESTINTERPOLATE);
227 return aScaledPreview;
228 }
229 }
230
231
ThrowIfDisposed(void)232 void SlideRenderer::ThrowIfDisposed (void)
233 throw (::com::sun::star::lang::DisposedException)
234 {
235 if (SlideRendererInterfaceBase::rBHelper.bDisposed || SlideRendererInterfaceBase::rBHelper.bInDispose)
236 {
237 throw lang::DisposedException (
238 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
239 "SlideRenderer object has already been disposed")),
240 static_cast<uno::XWeak*>(this));
241 }
242 }
243
244
245 } } // end of namespace ::sd::presenter
246
247 /* vim: set noet sw=4 ts=4: */
248