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 "GraphicExportFilter.hxx"
22 #include <com/sun/star/drawing/GraphicExportFilter.hpp>
23 #include <com/sun/star/frame/XModel.hpp>
25 #include <vcl/graphicfilter.hxx>
26 #include <svl/outstrm.hxx>
27 #include <svtools/DocumentToGraphicRenderer.hxx>
31 GraphicExportFilter::GraphicExportFilter( const uno::Reference
< uno::XComponentContext
> & rxContext
)
32 : mxContext(rxContext
)
35 , mbSelectionOnly(false)
38 GraphicExportFilter::~GraphicExportFilter()
41 void GraphicExportFilter::gatherProperties( const uno::Sequence
< beans::PropertyValue
> & rProperties
)
43 OUString aInternalFilterName
;
45 for ( sal_Int32 i
= 0; i
< rProperties
.getLength(); i
++ )
47 const beans::PropertyValue
& rProperty
= rProperties
[i
];
49 if ( rProperty
.Name
== "FilterName" )
51 rProperty
.Value
>>= aInternalFilterName
;
52 const sal_Int32 nLen
= aInternalFilterName
.getLength();
53 aInternalFilterName
= aInternalFilterName
.replaceFirst("calc_", "");
54 if (aInternalFilterName
.getLength() == nLen
)
55 aInternalFilterName
= aInternalFilterName
.replaceFirst("writer_", "");
56 if (aInternalFilterName
.getLength() == nLen
)
57 aInternalFilterName
= aInternalFilterName
.replaceFirst("web_", "");
58 if (aInternalFilterName
.getLength() == nLen
)
59 aInternalFilterName
= aInternalFilterName
.replaceFirst("draw_", "");
60 if (aInternalFilterName
.getLength() == nLen
)
61 aInternalFilterName
= aInternalFilterName
.replaceFirst("impress_", "");
63 else if ( rProperty
.Name
== "FilterData" )
65 rProperty
.Value
>>= maFilterDataSequence
;
67 else if ( rProperty
.Name
== "OutputStream" )
69 rProperty
.Value
>>= mxOutputStream
;
71 else if ( rProperty
.Name
== "SelectionOnly" )
73 rProperty
.Value
>>= mbSelectionOnly
;
77 for ( sal_Int32 i
= 0; i
< maFilterDataSequence
.getLength(); i
++ )
79 if ( maFilterDataSequence
[i
].Name
== "PixelWidth" )
81 maFilterDataSequence
[i
].Value
>>= mnTargetWidth
;
83 else if ( maFilterDataSequence
[i
].Name
== "PixelHeight" )
85 maFilterDataSequence
[i
].Value
>>= mnTargetHeight
;
89 if ( !aInternalFilterName
.isEmpty() )
91 GraphicFilter
aGraphicFilter( true );
93 sal_uInt16 nFilterCount
= aGraphicFilter
.GetExportFormatCount();
96 for ( nFormat
= 0; nFormat
< nFilterCount
; nFormat
++ )
98 if ( aGraphicFilter
.GetExportInternalFilterName( nFormat
) == aInternalFilterName
)
101 if ( nFormat
< nFilterCount
)
103 maFilterExtension
= aGraphicFilter
.GetExportFormatShortName( nFormat
);
108 sal_Bool SAL_CALL
GraphicExportFilter::filter( const uno::Sequence
< beans::PropertyValue
> & rDescriptor
)
110 gatherProperties(rDescriptor
);
112 if (mbSelectionOnly
&& mxDocument
.is())
114 uno::Reference
< frame::XModel
> xModel( mxDocument
, uno::UNO_QUERY
);
117 uno::Reference
< frame::XController
> xController( xModel
->getCurrentController());
118 if (xController
.is())
120 uno::Reference
< drawing::XShapes
> xShapes
;
121 uno::Reference
< drawing::XShape
> xShape
;
122 if (DocumentToGraphicRenderer::isShapeSelected( xShapes
, xShape
, xController
))
123 return filterExportShape( rDescriptor
, xShapes
, xShape
);
128 return filterRenderDocument();
131 bool GraphicExportFilter::filterRenderDocument() const
133 DocumentToGraphicRenderer
aRenderer( mxDocument
, mbSelectionOnly
);
134 sal_Int32 nCurrentPage
= aRenderer
.getCurrentPage();
135 Size aDocumentSizePixel
= aRenderer
.getDocumentSizeInPixels(nCurrentPage
);
137 Size
aTargetSizePixel(mnTargetWidth
, mnTargetHeight
);
139 if (mnTargetWidth
== 0 || mnTargetHeight
== 0)
140 aTargetSizePixel
= aDocumentSizePixel
;
142 Graphic aGraphic
= aRenderer
.renderToGraphic(nCurrentPage
, aDocumentSizePixel
, aTargetSizePixel
, COL_WHITE
, /*bExtOutDevData=*/false);
144 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
146 sal_uInt16 nFilterFormat
= rFilter
.GetExportFormatNumberForShortName( maFilterExtension
);
148 SvMemoryStream aMemStream
;
149 const GraphicConversionParameters
aParameters(aTargetSizePixel
, true, true);
151 const ErrCode nResult
= rFilter
.ExportGraphic( aGraphic
.GetBitmapEx(aParameters
), OUString(), aMemStream
,
152 nFilterFormat
, &maFilterDataSequence
);
154 if ( nResult
== ERRCODE_NONE
)
156 SvOutputStream
aOutputStream( mxOutputStream
);
158 aOutputStream
.WriteStream( aMemStream
);
166 bool GraphicExportFilter::filterExportShape(
167 const css::uno::Sequence
< css::beans::PropertyValue
> & rDescriptor
,
168 const css::uno::Reference
< css::drawing::XShapes
> & rxShapes
,
169 const css::uno::Reference
< css::drawing::XShape
> & rxShape
) const
171 uno::Reference
< lang::XComponent
> xSourceDoc
;
173 xSourceDoc
.set( rxShapes
, uno::UNO_QUERY_THROW
);
174 else if (rxShape
.is())
175 xSourceDoc
.set( rxShape
, uno::UNO_QUERY_THROW
);
176 if (!xSourceDoc
.is())
179 uno::Reference
< drawing::XGraphicExportFilter
> xGraphicExporter
=
180 drawing::GraphicExportFilter::create( mxContext
);
181 if (!xGraphicExporter
.is())
184 // Need to replace the internal filter name with the short name
186 uno::Sequence
< beans::PropertyValue
> aDescriptor( rDescriptor
);
187 for (sal_Int32 i
= 0; i
< aDescriptor
.getLength(); ++i
)
189 if (aDescriptor
[i
].Name
== "FilterName")
191 aDescriptor
[i
].Value
<<= maFilterExtension
;
196 xGraphicExporter
->setSourceDocument( xSourceDoc
);
197 return xGraphicExporter
->filter( aDescriptor
);
200 void SAL_CALL
GraphicExportFilter::cancel( )
204 void SAL_CALL
GraphicExportFilter::setSourceDocument( const uno::Reference
< lang::XComponent
> & xDocument
)
206 mxDocument
= xDocument
;
209 void SAL_CALL
GraphicExportFilter::initialize( const uno::Sequence
< uno::Any
> & )
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */