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"
22 #include <toolkit/helper/vclunohelper.hxx>
23 #include <vcl/svapp.hxx>
24 #include <cppcanvas/vclfactory.hxx>
25 #include <cppuhelper/supportsservice.hxx>
27 using namespace ::com::sun::star
;
28 using namespace ::com::sun::star::uno
;
30 namespace sd::presenter
{
32 //===== SlideRenderer ==========================================================
34 SlideRenderer::SlideRenderer ()
38 SlideRenderer::~SlideRenderer()
42 //----- XInitialization -------------------------------------------------------
44 void SAL_CALL
SlideRenderer::initialize (const Sequence
<Any
>& rArguments
)
48 if (rArguments
.hasElements())
50 throw RuntimeException(u
"SlideRenderer: invalid number of arguments"_ustr
,
51 static_cast<XWeak
*>(this));
55 OUString
SlideRenderer::getImplementationName()
57 return u
"com.sun.star.comp.Draw.SlideRenderer"_ustr
;
60 sal_Bool
SlideRenderer::supportsService(OUString
const & ServiceName
)
62 return cppu::supportsService(this, ServiceName
);
65 css::uno::Sequence
<OUString
> SlideRenderer::getSupportedServiceNames()
67 return {u
"com.sun.star.drawing.SlideRenderer"_ustr
};
70 //----- XSlideRenderer --------------------------------------------------------
72 Reference
<awt::XBitmap
> SlideRenderer::createPreview (
73 const Reference
<drawing::XDrawPage
>& rxSlide
,
74 const awt::Size
& rMaximalSize
,
75 sal_Int16 nSuperSampleFactor
)
78 SolarMutexGuard aGuard
;
80 return VCLUnoHelper::CreateBitmap(
81 CreatePreview(rxSlide
, rMaximalSize
, nSuperSampleFactor
));
84 Reference
<rendering::XBitmap
> SlideRenderer::createPreviewForCanvas (
85 const Reference
<drawing::XDrawPage
>& rxSlide
,
86 const awt::Size
& rMaximalSize
,
87 sal_Int16 nSuperSampleFactor
,
88 const Reference
<rendering::XCanvas
>& rxCanvas
)
91 SolarMutexGuard aGuard
;
93 cppcanvas::CanvasSharedPtr
pCanvas (
94 cppcanvas::VCLFactory::createCanvas(rxCanvas
));
96 return cppcanvas::VCLFactory::createBitmap(
98 CreatePreview(rxSlide
, rMaximalSize
, nSuperSampleFactor
))->getUNOBitmap();
103 awt::Size SAL_CALL
SlideRenderer::calculatePreviewSize (
104 double nSlideAspectRatio
,
105 const awt::Size
& rMaximalSize
)
107 if (rMaximalSize
.Width
<= 0
108 || rMaximalSize
.Height
<= 0
109 || nSlideAspectRatio
<= 0)
111 return awt::Size(0,0);
114 const double nWindowAspectRatio (double(rMaximalSize
.Width
) / double(rMaximalSize
.Height
));
115 if (nSlideAspectRatio
< nWindowAspectRatio
)
117 sal::static_int_cast
<sal_Int32
>(rMaximalSize
.Height
* nSlideAspectRatio
),
118 rMaximalSize
.Height
);
122 sal::static_int_cast
<sal_Int32
>(rMaximalSize
.Width
/ nSlideAspectRatio
));
125 BitmapEx
SlideRenderer::CreatePreview (
126 const Reference
<drawing::XDrawPage
>& rxSlide
,
127 const awt::Size
& rMaximalSize
,
128 sal_Int16 nSuperSampleFactor
)
130 const SdPage
* pPage
= SdPage::getImplementation(rxSlide
);
131 if (pPage
== nullptr)
132 throw lang::IllegalArgumentException(u
"SlideRenderer::createPreview() called with invalid slide"_ustr
,
133 static_cast<XWeak
*>(this),
136 // Determine the size of the current slide and its aspect ratio.
137 Size aPageSize
= pPage
->GetSize();
138 if (aPageSize
.Height() <= 0)
139 throw lang::IllegalArgumentException(u
"SlideRenderer::createPreview() called with invalid size"_ustr
,
140 static_cast<XWeak
*>(this),
143 // Compare with the aspect ratio of the window (which rMaximalSize
144 // assumed to be) and calculate the size of the preview so that it
145 // a) will have the aspect ratio of the page and
146 // b) will be as large as possible.
147 awt::Size
aPreviewSize (calculatePreviewSize(
148 double(aPageSize
.Width()) / double(aPageSize
.Height()),
150 if (aPreviewSize
.Width
<= 0 || aPreviewSize
.Height
<= 0)
153 // Make sure that the super sample factor has a sane value.
154 sal_Int16
nFactor (nSuperSampleFactor
);
157 else if (nFactor
> 10)
160 // Create the preview. When the super sample factor n is greater than 1
161 // then a preview is created in size (n*width, n*height) and then scaled
162 // down to (width, height). This is a poor mans antialiasing for the
163 // time being. When we have true antialiasing support this workaround
165 const Image aPreview
= maPreviewRenderer
.RenderPage (
167 Size(aPreviewSize
.Width
*nFactor
, aPreviewSize
.Height
*nFactor
),
170 return aPreview
.GetBitmapEx();
173 BitmapEx aScaledPreview
= aPreview
.GetBitmapEx();
174 aScaledPreview
.Scale(
175 Size(aPreviewSize
.Width
,aPreviewSize
.Height
),
176 BmpScaleFlag::BestQuality
);
177 return aScaledPreview
;
181 void SlideRenderer::ThrowIfDisposed()
185 throw lang::DisposedException (u
"SlideRenderer object has already been disposed"_ustr
,
186 static_cast<uno::XWeak
*>(this));
190 } // end of namespace ::sd::presenter
193 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
194 com_sun_star_comp_Draw_SlideRenderer_get_implementation(css::uno::XComponentContext
*,
195 css::uno::Sequence
<css::uno::Any
> const &)
197 return cppu::acquire(new sd::presenter::SlideRenderer
);
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */