LibreOffice Module filter (master) 1
GraphicExportFilter.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21
22#include <com/sun/star/drawing/GraphicExportFilter.hpp>
23#include <com/sun/star/drawing/XShape.hpp>
24#include <com/sun/star/drawing/XShapes.hpp>
25#include <com/sun/star/frame/XModel.hpp>
26
28#include <utility>
29#include <vcl/graphicfilter.hxx>
30#include <svl/outstrm.hxx>
32
33using namespace css;
34
35GraphicExportFilter::GraphicExportFilter( uno::Reference< uno::XComponentContext > xContext )
36 : mxContext(std::move(xContext))
37 , mnTargetWidth(0)
38 , mnTargetHeight(0)
39 , mbSelectionOnly(false)
40{}
41
43{}
44
45// XServiceInfo
47{
49}
51{
52 return "com.sun.star.comp.GraphicExportFilter";
53}
54css::uno::Sequence< OUString > GraphicExportFilter::getSupportedServiceNames()
55{
56 return { "com.sun.star.document.ExportFilter" };
57}
58
59void GraphicExportFilter::gatherProperties( const uno::Sequence< beans::PropertyValue > & rProperties )
60{
61 OUString aInternalFilterName;
62
63 for ( const beans::PropertyValue& rProperty : rProperties )
64 {
65 if ( rProperty.Name == "FilterName" )
66 {
67 rProperty.Value >>= aInternalFilterName;
68 const sal_Int32 nLen = aInternalFilterName.getLength();
69 aInternalFilterName = aInternalFilterName.replaceFirst("calc_", "");
70 if (aInternalFilterName.getLength() == nLen)
71 aInternalFilterName = aInternalFilterName.replaceFirst("writer_", "");
72 if (aInternalFilterName.getLength() == nLen)
73 aInternalFilterName = aInternalFilterName.replaceFirst("web_", "");
74 if (aInternalFilterName.getLength() == nLen)
75 aInternalFilterName = aInternalFilterName.replaceFirst("draw_", "");
76 if (aInternalFilterName.getLength() == nLen)
77 aInternalFilterName = aInternalFilterName.replaceFirst("impress_", "");
78 }
79 else if ( rProperty.Name == "FilterData" )
80 {
81 rProperty.Value >>= maFilterDataSequence;
82 }
83 else if ( rProperty.Name == "OutputStream" )
84 {
85 rProperty.Value >>= mxOutputStream;
86 }
87 else if ( rProperty.Name == "SelectionOnly" )
88 {
89 rProperty.Value >>= mbSelectionOnly;
90 }
91 }
92
93 for ( const beans::PropertyValue& rProp : std::as_const(maFilterDataSequence) )
94 {
95 if ( rProp.Name == "PixelWidth" )
96 {
97 rProp.Value >>= mnTargetWidth;
98 }
99 else if ( rProp.Name == "PixelHeight" )
100 {
101 rProp.Value >>= mnTargetHeight;
102 }
103 }
104
105 if ( aInternalFilterName.isEmpty() )
106 return;
107
108 GraphicFilter aGraphicFilter( true );
109
110 sal_uInt16 nFilterCount = aGraphicFilter.GetExportFormatCount();
111 sal_uInt16 nFormat;
112
113 for ( nFormat = 0; nFormat < nFilterCount; nFormat++ )
114 {
115 if ( aGraphicFilter.GetExportInternalFilterName( nFormat ) == aInternalFilterName )
116 break;
117 }
118 if ( nFormat < nFilterCount )
119 {
120 maFilterExtension = aGraphicFilter.GetExportFormatShortName( nFormat );
121 }
122}
123
124sal_Bool SAL_CALL GraphicExportFilter::filter( const uno::Sequence< beans::PropertyValue > & rDescriptor )
125{
126 gatherProperties(rDescriptor);
127
128 if (mbSelectionOnly && mxDocument.is())
129 {
130 uno::Reference< frame::XModel > xModel( mxDocument, uno::UNO_QUERY);
131 if (xModel.is())
132 {
133 uno::Reference< frame::XController > xController( xModel->getCurrentController());
134 if (xController.is())
135 {
136 uno::Reference< drawing::XShapes > xShapes;
137 uno::Reference< drawing::XShape > xShape;
139 return filterExportShape( rDescriptor, xShapes, xShape);
140 }
141 }
142 }
143
144 return filterRenderDocument();
145}
146
148{
150 sal_Int32 nCurrentPage = aRenderer.getCurrentPage();
151 Size aDocumentSizePixel = aRenderer.getDocumentSizeInPixels(nCurrentPage);
152
153 Size aTargetSizePixel(mnTargetWidth, mnTargetHeight);
154
155 if (mnTargetWidth == 0 || mnTargetHeight == 0)
156 aTargetSizePixel = aDocumentSizePixel;
157
158 Graphic aGraphic = aRenderer.renderToGraphic(nCurrentPage, aDocumentSizePixel, aTargetSizePixel, COL_WHITE, /*bExtOutDevData=*/false);
159
161
162 sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName( maFilterExtension );
163
164 SvMemoryStream aMemStream;
165 const GraphicConversionParameters aParameters(aTargetSizePixel, true, true);
166
167 const ErrCode nResult = rFilter.ExportGraphic( aGraphic.GetBitmapEx(aParameters), u"", aMemStream,
168 nFilterFormat, &maFilterDataSequence );
169
170 if ( nResult == ERRCODE_NONE )
171 {
172 SvOutputStream aOutputStream( mxOutputStream );
173 aMemStream.Seek(0);
174 aOutputStream.WriteStream( aMemStream );
175
176 return true;
177 }
178
179 return false;
180}
181
183 const css::uno::Sequence< css::beans::PropertyValue > & rDescriptor,
184 const css::uno::Reference< css::drawing::XShapes > & rxShapes,
185 const css::uno::Reference< css::drawing::XShape > & rxShape ) const
186{
187 uno::Reference< lang::XComponent > xSourceDoc;
188 if (rxShapes.is())
189 xSourceDoc.set( rxShapes, uno::UNO_QUERY_THROW );
190 else if (rxShape.is())
191 xSourceDoc.set( rxShape, uno::UNO_QUERY_THROW );
192 if (!xSourceDoc.is())
193 return false;
194
195 uno::Reference< drawing::XGraphicExportFilter > xGraphicExporter =
196 drawing::GraphicExportFilter::create( mxContext );
197 if (!xGraphicExporter.is())
198 return false;
199
200 // Need to replace the internal filter name with the short name
201 // (extension).
202 uno::Sequence< beans::PropertyValue > aDescriptor( rDescriptor);
203 for (sal_Int32 i = 0; i < aDescriptor.getLength(); ++i)
204 {
205 if (aDescriptor[i].Name == "FilterName")
206 {
207 aDescriptor.getArray()[i].Value <<= maFilterExtension;
208 break;
209 }
210 }
211
212 xGraphicExporter->setSourceDocument( xSourceDoc );
213 return xGraphicExporter->filter( aDescriptor );
214}
215
217{
218}
219
220void SAL_CALL GraphicExportFilter::setSourceDocument( const uno::Reference< lang::XComponent > & xDocument )
221{
222 mxDocument = xDocument;
223}
224
225void SAL_CALL GraphicExportFilter::initialize( const uno::Sequence< uno::Any > & )
226{
227}
228
229extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
231 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
232{
233 return cppu::acquire(new GraphicExportFilter(context));
234}
235/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * filter_GraphicExportFilter_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
constexpr OUStringLiteral sServiceName
Graphic renderToGraphic(sal_Int32 nCurrentPage, Size aDocumentSizePixel, Size aTargetSizePixel, Color aPageColor, bool bExtOutDevData)
Size getDocumentSizeInPixels(sal_Int32 nCurrentPage)
static bool isShapeSelected(css::uno::Reference< css::drawing::XShapes > &rxShapes, css::uno::Reference< css::drawing::XShape > &rxShape, const css::uno::Reference< css::frame::XController > &rxController)
css::uno::Sequence< css::beans::PropertyValue > maFilterDataSequence
virtual void SAL_CALL cancel() override
virtual ~GraphicExportFilter() override
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &rArguments) override
bool filterExportShape(const css::uno::Sequence< css::beans::PropertyValue > &rDescriptor, const css::uno::Reference< css::drawing::XShapes > &rxShapes, const css::uno::Reference< css::drawing::XShape > &rxShape) const
virtual sal_Bool SAL_CALL filter(const css::uno::Sequence< css::beans::PropertyValue > &rDescriptor) override
virtual OUString SAL_CALL getImplementationName() override
void gatherProperties(const css::uno::Sequence< css::beans::PropertyValue > &rDescriptor)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual void SAL_CALL setSourceDocument(const css::uno::Reference< css::lang::XComponent > &xDocument) override
css::uno::Reference< css::io::XOutputStream > mxOutputStream
css::uno::Reference< css::lang::XComponent > mxDocument
GraphicExportFilter(css::uno::Reference< css::uno::XComponentContext > xContext)
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
sal_uInt16 GetExportFormatNumberForShortName(std::u16string_view rShortName)
static GraphicFilter & GetGraphicFilter()
ErrCode ExportGraphic(const Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat, const css::uno::Sequence< css::beans::PropertyValue > *pFilterData=nullptr)
sal_uInt16 GetExportFormatCount() const
OUString GetExportFormatShortName(sal_uInt16 nFormat)
OUString GetExportInternalFilterName(sal_uInt16 nFormat)
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
sal_uInt64 Seek(sal_uInt64 nPos)
constexpr ::Color COL_WHITE(0xFF, 0xFF, 0xFF)
uno::Reference< uno::XComponentContext > mxContext
float u
#define ERRCODE_NONE
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
Definition: gentoken.py:48
Reference< XController > xController
Reference< XModel > xModel
OUString Name
unsigned char sal_Bool