Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svtools / source / graphic / renderer.cxx
blobd031026b13176b2835c7ed778e01f131886926f6
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 .
21 #include <com/sun/star/awt/Rectangle.hpp>
22 #include <com/sun/star/awt/XDevice.hpp>
23 #include <com/sun/star/graphic/XGraphic.hpp>
24 #include <com/sun/star/graphic/XGraphicRenderer.hpp>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <tools/gen.hxx>
28 #include <vcl/svapp.hxx>
29 #include <toolkit/helper/vclunohelper.hxx>
30 #include <comphelper/propertysethelper.hxx>
31 #include <comphelper/propertysetinfo.hxx>
32 #include <cppuhelper/supportsservice.hxx>
33 #include <cppuhelper/weakagg.hxx>
34 #include <rtl/ref.hxx>
35 #include <vcl/GraphicObject.hxx>
36 #include <vcl/outdev.hxx>
38 #define UNOGRAPHIC_DEVICE 1
39 #define UNOGRAPHIC_DESTINATIONRECT 2
40 #define UNOGRAPHIC_RENDERDATA 3
42 using namespace ::com::sun::star;
44 namespace {
46 class GraphicRendererVCL : public ::cppu::OWeakAggObject,
47 public css::lang::XServiceInfo,
48 public css::lang::XTypeProvider,
49 public ::comphelper::PropertySetHelper,
50 public css::graphic::XGraphicRenderer
52 static rtl::Reference<::comphelper::PropertySetInfo> createPropertySetInfo();
54 public:
56 GraphicRendererVCL();
58 // XInterface
59 virtual css::uno::Any SAL_CALL queryAggregation( const css::uno::Type & rType ) override;
60 virtual css::uno::Any SAL_CALL queryInterface( const css::uno::Type & rType ) override;
61 virtual void SAL_CALL acquire() noexcept override;
62 virtual void SAL_CALL release() noexcept override;
64 // XServiceInfo
65 virtual OUString SAL_CALL getImplementationName() override;
66 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
67 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
69 // XTypeProvider
70 virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes( ) override;
71 virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId( ) override;
73 // PropertySetHelper
74 virtual void _setPropertyValues( const comphelper::PropertyMapEntry** ppEntries, const css::uno::Any* pValues ) override;
75 virtual void _getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, css::uno::Any* pValue ) override;
77 // XGraphicRenderer
78 virtual void SAL_CALL render( const css::uno::Reference< css::graphic::XGraphic >& Graphic ) override;
80 private:
82 css::uno::Reference< css::awt::XDevice > mxDevice;
84 VclPtr<OutputDevice> mpOutDev;
85 tools::Rectangle maDestRect;
86 css::uno::Any maRenderData;
89 GraphicRendererVCL::GraphicRendererVCL() :
90 ::comphelper::PropertySetHelper( createPropertySetInfo() ),
91 mpOutDev( nullptr )
95 uno::Any SAL_CALL GraphicRendererVCL::queryAggregation( const uno::Type & rType )
97 uno::Any aAny;
99 if( rType == cppu::UnoType<lang::XServiceInfo>::get())
100 aAny <<= uno::Reference< lang::XServiceInfo >(this);
101 else if( rType == cppu::UnoType<lang::XTypeProvider>::get())
102 aAny <<= uno::Reference< lang::XTypeProvider >(this);
103 else if( rType == cppu::UnoType<beans::XPropertySet>::get())
104 aAny <<= uno::Reference< beans::XPropertySet >(this);
105 else if( rType == cppu::UnoType<beans::XPropertyState>::get())
106 aAny <<= uno::Reference< beans::XPropertyState >(this);
107 else if( rType == cppu::UnoType<beans::XMultiPropertySet>::get())
108 aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
109 else if( rType == cppu::UnoType<graphic::XGraphicRenderer>::get())
110 aAny <<= uno::Reference< graphic::XGraphicRenderer >(this);
111 else
112 aAny = OWeakAggObject::queryAggregation( rType );
114 return aAny;
118 uno::Any SAL_CALL GraphicRendererVCL::queryInterface( const uno::Type & rType )
120 return OWeakAggObject::queryInterface( rType );
124 void SAL_CALL GraphicRendererVCL::acquire()
125 noexcept
127 OWeakAggObject::acquire();
131 void SAL_CALL GraphicRendererVCL::release()
132 noexcept
134 OWeakAggObject::release();
138 OUString SAL_CALL GraphicRendererVCL::getImplementationName()
140 return "com.sun.star.comp.graphic.GraphicRendererVCL";
143 sal_Bool SAL_CALL GraphicRendererVCL::supportsService( const OUString& ServiceName )
145 return cppu::supportsService(this, ServiceName);
149 uno::Sequence< OUString > SAL_CALL GraphicRendererVCL::getSupportedServiceNames()
151 return { "com.sun.star.graphic.GraphicRendererVCL" };
155 uno::Sequence< uno::Type > SAL_CALL GraphicRendererVCL::getTypes()
157 static const uno::Sequence< uno::Type > aTypes {
158 cppu::UnoType<uno::XAggregation>::get(),
159 cppu::UnoType<lang::XServiceInfo>::get(),
160 cppu::UnoType<lang::XTypeProvider>::get(),
161 cppu::UnoType<beans::XPropertySet>::get(),
162 cppu::UnoType<beans::XPropertyState>::get(),
163 cppu::UnoType<beans::XMultiPropertySet>::get(),
164 cppu::UnoType<graphic::XGraphicRenderer>::get() };
165 return aTypes;
168 uno::Sequence< sal_Int8 > SAL_CALL GraphicRendererVCL::getImplementationId()
170 return css::uno::Sequence<sal_Int8>();
174 rtl::Reference<::comphelper::PropertySetInfo> GraphicRendererVCL::createPropertySetInfo()
176 static ::comphelper::PropertyMapEntry const aEntries[] =
178 { OUString("Device"), UNOGRAPHIC_DEVICE, cppu::UnoType<uno::Any>::get(), 0, 0 },
179 { OUString("DestinationRect"), UNOGRAPHIC_DESTINATIONRECT, cppu::UnoType<awt::Rectangle>::get(), 0, 0 },
180 { OUString("RenderData"), UNOGRAPHIC_RENDERDATA, cppu::UnoType<uno::Any>::get(), 0, 0 },
183 return rtl::Reference<::comphelper::PropertySetInfo>( new ::comphelper::PropertySetInfo(aEntries) );
187 void GraphicRendererVCL::_setPropertyValues( const comphelper::PropertyMapEntry** ppEntries, const uno::Any* pValues )
189 SolarMutexGuard aGuard;
191 while( *ppEntries )
193 switch( (*ppEntries)->mnHandle )
195 case UNOGRAPHIC_DEVICE:
197 uno::Reference< awt::XDevice > xDevice;
199 if( ( *pValues >>= xDevice ) && xDevice.is() )
201 mxDevice = xDevice;
202 mpOutDev = VCLUnoHelper::GetOutputDevice( xDevice );
204 else
206 mxDevice.clear();
207 mpOutDev = nullptr;
210 break;
212 case UNOGRAPHIC_DESTINATIONRECT:
214 awt::Rectangle aAWTRect;
216 if( *pValues >>= aAWTRect )
218 maDestRect = tools::Rectangle( Point( aAWTRect.X, aAWTRect.Y ),
219 Size( aAWTRect.Width, aAWTRect.Height ) );
222 break;
224 case UNOGRAPHIC_RENDERDATA:
226 maRenderData = *pValues;
228 break;
231 ++ppEntries;
232 ++pValues;
237 void GraphicRendererVCL::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValues )
239 SolarMutexGuard aGuard;
241 while( *ppEntries )
243 switch( (*ppEntries)->mnHandle )
245 case UNOGRAPHIC_DEVICE:
247 if( mxDevice.is() )
248 *pValues <<= mxDevice;
250 break;
252 case UNOGRAPHIC_DESTINATIONRECT:
254 const awt::Rectangle aAWTRect( maDestRect.Left(), maDestRect.Top(),
255 maDestRect.GetWidth(), maDestRect.GetHeight() );
257 *pValues <<= aAWTRect;
259 break;
261 case UNOGRAPHIC_RENDERDATA:
263 *pValues = maRenderData;
265 break;
268 ++ppEntries;
269 ++pValues;
273 void SAL_CALL GraphicRendererVCL::render( const uno::Reference< graphic::XGraphic >& rxGraphic )
275 if( mpOutDev && mxDevice.is() && rxGraphic.is() )
277 Graphic aGraphic(rxGraphic);
278 if (!aGraphic.IsNone())
280 GraphicObject aGraphicObject(std::move(aGraphic));
281 aGraphicObject.Draw(*mpOutDev, maDestRect.TopLeft(), maDestRect.GetSize());
288 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
289 com_sun_star_comp_graphic_GraphicRendererVCL_get_implementation(
290 css::uno::XComponentContext *,
291 css::uno::Sequence<css::uno::Any> const &)
293 return cppu::acquire(new GraphicRendererVCL);
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */