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