Line data Source code
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 :
20 :
21 : #include <cstdio>
22 :
23 : #include <comphelper/servicedecl.hxx>
24 : #include <uno/environment.h>
25 : #include <com/sun/star/drawing/XDrawPage.hpp>
26 : #include <com/sun/star/drawing/XDrawView.hpp>
27 : #include <com/sun/star/frame/Desktop.hpp>
28 : #include <com/sun/star/frame/XController.hpp>
29 : #include <com/sun/star/view/XSelectionSupplier.hpp>
30 : #include <com/sun/star/drawing/XDrawSubController.hpp>
31 : #include <com/sun/star/container/XNamed.hpp>
32 : #include <com/sun/star/uno/XComponentContext.hpp>
33 : #include <com/sun/star/drawing/framework/XControllerManager.hpp>
34 : #include <com/sun/star/drawing/framework/XConfigurationController.hpp>
35 : #include <com/sun/star/drawing/framework/XConfiguration.hpp>
36 : #include <com/sun/star/drawing/framework/AnchorBindingMode.hpp>
37 : #include <com/sun/star/drawing/framework/XResourceId.hpp>
38 : #include <com/sun/star/drawing/framework/XResource.hpp>
39 : #include <com/sun/star/drawing/framework/XView.hpp>
40 : #include <com/sun/star/drawing/framework/ResourceId.hpp>
41 : #include <comphelper/processfactory.hxx>
42 :
43 : #include <unotools/mediadescriptor.hxx>
44 : #include <unotools/ucbstreamhelper.hxx>
45 : #include <unotools/streamwrap.hxx>
46 : #include <tools/zcodec.hxx>
47 :
48 : #include <osl/mutex.hxx>
49 :
50 : #include "svgfilter.hxx"
51 : #include "svgwriter.hxx"
52 :
53 : using namespace ::com::sun::star;
54 :
55 : namespace
56 : {
57 : static const char constFilterName[] = "svg_Scalable_Vector_Graphics";
58 : }
59 :
60 0 : SVGFilter::SVGFilter( const Reference< XComponentContext >& rxCtx ) :
61 : mxContext( rxCtx ),
62 : mpSVGDoc( NULL ),
63 : mpSVGExport( NULL ),
64 : mpSVGFontExport( NULL ),
65 : mpSVGWriter( NULL ),
66 : mpDefaultSdrPage( NULL ),
67 : mpSdrModel( NULL ),
68 : mbPresentation( sal_False ),
69 : mbSinglePage( sal_False ),
70 : mnVisiblePage( -1 ),
71 0 : mpObjects( NULL )
72 : {
73 0 : }
74 :
75 0 : SVGFilter::~SVGFilter()
76 : {
77 : DBG_ASSERT( mpSVGDoc == NULL, "mpSVGDoc not destroyed" );
78 : DBG_ASSERT( mpSVGExport == NULL, "mpSVGExport not destroyed" );
79 : DBG_ASSERT( mpSVGFontExport == NULL, "mpSVGFontExport not destroyed" );
80 : DBG_ASSERT( mpSVGWriter == NULL, "mpSVGWriter not destroyed" );
81 : DBG_ASSERT( mpObjects == NULL, "mpObjects not destroyed" );
82 0 : }
83 :
84 0 : sal_Bool SAL_CALL SVGFilter::filter( const Sequence< PropertyValue >& rDescriptor )
85 : throw (RuntimeException, std::exception)
86 : {
87 0 : SolarMutexGuard aGuard;
88 0 : Window* pFocusWindow = Application::GetFocusWindow();
89 : sal_Bool bRet;
90 :
91 0 : if( pFocusWindow )
92 0 : pFocusWindow->EnterWait();
93 :
94 0 : if( mxDstDoc.is() )
95 0 : bRet = implImport( rDescriptor );
96 : #ifndef DISABLE_EXPORT
97 0 : else if( mxSrcDoc.is() )
98 : {
99 : // #i124608# detext selection
100 0 : sal_Bool bSelectionOnly = sal_False;
101 0 : bool bGotSelection(false);
102 :
103 : // #i124608# extract Single selection wanted from dialog return values
104 0 : for ( sal_Int32 nInd = 0; nInd < rDescriptor.getLength(); nInd++ )
105 : {
106 0 : if ( rDescriptor[nInd].Name == "SelectionOnly" )
107 : {
108 0 : rDescriptor[nInd].Value >>= bSelectionOnly;
109 : }
110 : }
111 :
112 0 : uno::Reference< frame::XDesktop2 > xDesktop(frame::Desktop::create(mxContext));
113 0 : uno::Reference< frame::XFrame > xFrame(xDesktop->getCurrentFrame(),
114 0 : uno::UNO_QUERY_THROW);
115 0 : uno::Reference<frame::XController > xController(xFrame->getController(),
116 0 : uno::UNO_QUERY_THROW);
117 :
118 0 : if( !mSelectedPages.hasElements() )
119 : {
120 : uno::Reference<drawing::XDrawView > xDrawView(xController,
121 0 : uno::UNO_QUERY_THROW);
122 : uno::Reference<drawing::framework::XControllerManager> xManager(xController,
123 0 : uno::UNO_QUERY_THROW);
124 0 : uno::Reference<drawing::framework::XConfigurationController> xConfigController(xManager->getConfigurationController());
125 :
126 : // which view configuration are we in?
127 : //
128 : // * traverse Impress resources to find slide preview pane, grab selection from there
129 : // * otherwise, fallback to current slide
130 : //
131 : uno::Sequence<uno::Reference<drawing::framework::XResourceId> > aResIds(
132 0 : xConfigController->getCurrentConfiguration()->getResources(
133 : uno::Reference<drawing::framework::XResourceId>(),
134 : "",
135 0 : drawing::framework::AnchorBindingMode_INDIRECT));
136 :
137 0 : for( sal_Int32 i=0; i<aResIds.getLength(); ++i )
138 : {
139 : // can we somehow obtain the slidesorter from the Impress framework?
140 0 : if( aResIds[i]->getResourceURL() == "private:resource/view/SlideSorter" )
141 : {
142 : // got it, grab current selection from there
143 : uno::Reference<drawing::framework::XResource> xRes(
144 0 : xConfigController->getResource(aResIds[i]));
145 :
146 : uno::Reference< view::XSelectionSupplier > xSelectionSupplier(
147 : xRes,
148 0 : uno::UNO_QUERY );
149 0 : if( xSelectionSupplier.is() )
150 : {
151 0 : uno::Any aSelection = xSelectionSupplier->getSelection();
152 0 : if( aSelection.hasValue() )
153 : {
154 0 : ObjectSequence aSelectedPageSequence;
155 0 : aSelection >>= aSelectedPageSequence;
156 0 : mSelectedPages.realloc( aSelectedPageSequence.getLength() );
157 0 : for( sal_Int32 j=0; j<mSelectedPages.getLength(); ++j )
158 : {
159 0 : uno::Reference< drawing::XDrawPage > xDrawPage( aSelectedPageSequence[j],
160 0 : uno::UNO_QUERY );
161 0 : mSelectedPages[j] = xDrawPage;
162 0 : }
163 :
164 : // and stop looping. it is likely not getting better
165 0 : break;
166 0 : }
167 0 : }
168 : }
169 : }
170 :
171 0 : if( !mSelectedPages.hasElements() )
172 : {
173 : // apparently failed to glean selection - fallback to current page
174 0 : mSelectedPages.realloc( 1 );
175 0 : mSelectedPages[0] = xDrawView->getCurrentPage();
176 0 : }
177 : }
178 :
179 : /*
180 : * Export all slides, or requested "PagePos"
181 : */
182 0 : if( !mSelectedPages.hasElements() )
183 : {
184 0 : sal_Int32 nLength = rDescriptor.getLength();
185 0 : const PropertyValue* pValue = rDescriptor.getConstArray();
186 0 : sal_Int32 nPageToExport = -1;
187 :
188 0 : for ( sal_Int32 i = 0 ; i < nLength; ++i)
189 : {
190 0 : if ( pValue[ i ].Name == "PagePos" )
191 : {
192 0 : pValue[ i ].Value >>= nPageToExport;
193 : }
194 : }
195 :
196 0 : uno::Reference< drawing::XMasterPagesSupplier > xMasterPagesSupplier( mxSrcDoc, uno::UNO_QUERY );
197 0 : uno::Reference< drawing::XDrawPagesSupplier > xDrawPagesSupplier( mxSrcDoc, uno::UNO_QUERY );
198 :
199 0 : if( xMasterPagesSupplier.is() && xDrawPagesSupplier.is() )
200 : {
201 0 : uno::Reference< drawing::XDrawPages > xMasterPages( xMasterPagesSupplier->getMasterPages(), uno::UNO_QUERY );
202 0 : uno::Reference< drawing::XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), uno::UNO_QUERY );
203 0 : if( xMasterPages.is() && xMasterPages->getCount() &&
204 0 : xDrawPages.is() && xDrawPages->getCount() )
205 : {
206 0 : sal_Int32 nDPCount = xDrawPages->getCount();
207 :
208 0 : mSelectedPages.realloc( nPageToExport != -1 ? 1 : nDPCount );
209 : sal_Int32 i;
210 0 : for( i = 0; i < nDPCount; ++i )
211 : {
212 0 : if( nPageToExport != -1 && nPageToExport == i )
213 : {
214 0 : uno::Reference< drawing::XDrawPage > xDrawPage( xDrawPages->getByIndex( i ), uno::UNO_QUERY );
215 0 : mSelectedPages[0] = xDrawPage;
216 : }
217 : else
218 : {
219 0 : uno::Reference< drawing::XDrawPage > xDrawPage( xDrawPages->getByIndex( i ), uno::UNO_QUERY );
220 0 : mSelectedPages[i] = xDrawPage;
221 : }
222 : }
223 0 : }
224 0 : }
225 : }
226 :
227 0 : if (bSelectionOnly)
228 : {
229 : // #i124608# when selection only is wanted, get the current object selection
230 : // from the DrawView
231 0 : Reference< view::XSelectionSupplier > xSelection (xController, UNO_QUERY);
232 :
233 0 : if (xSelection.is())
234 : {
235 0 : uno::Any aSelection;
236 :
237 0 : if (xSelection->getSelection() >>= aSelection)
238 : {
239 0 : bGotSelection = ( aSelection >>= maShapeSelection );
240 0 : }
241 0 : }
242 : }
243 :
244 0 : if(bSelectionOnly && bGotSelection && 0 == maShapeSelection->getCount())
245 : {
246 : // #i124608# export selection, got maShapeSelection but no shape selected -> nothing
247 : // to export, we are done (maybe return true, but a hint that nothing was done
248 : // may be useful; it may have happened by error)
249 0 : bRet = sal_False;
250 : }
251 : else {
252 : /*
253 : * We get all master page that are targeted by at least one draw page.
254 : * The master page are put in an unordered set.
255 : */
256 0 : ObjectSet aMasterPageTargetSet;
257 0 : for( sal_Int32 i = 0; i < mSelectedPages.getLength(); ++i )
258 : {
259 0 : uno::Reference< drawing::XMasterPageTarget > xMasterPageTarget( mSelectedPages[i], uno::UNO_QUERY );
260 0 : if( xMasterPageTarget.is() )
261 : {
262 0 : aMasterPageTargetSet.insert( xMasterPageTarget->getMasterPage() );
263 : }
264 0 : }
265 : // Later we move them to a uno::Sequence so we can get them by index
266 0 : mMasterPageTargets.realloc( aMasterPageTargetSet.size() );
267 0 : ObjectSet::const_iterator aElem = aMasterPageTargetSet.begin();
268 0 : for( sal_Int32 i = 0; aElem != aMasterPageTargetSet.end(); ++aElem, ++i)
269 : {
270 0 : uno::Reference< drawing::XDrawPage > xMasterPage( *aElem, uno::UNO_QUERY );
271 0 : mMasterPageTargets[i] = xMasterPage;
272 0 : }
273 :
274 0 : bRet = implExport( rDescriptor );
275 0 : }
276 : }
277 : #endif
278 : else
279 0 : bRet = sal_False;
280 :
281 0 : if( pFocusWindow )
282 0 : pFocusWindow->LeaveWait();
283 :
284 0 : return bRet;
285 : }
286 :
287 0 : void SAL_CALL SVGFilter::cancel( ) throw (RuntimeException, std::exception)
288 : {
289 0 : }
290 :
291 0 : void SAL_CALL SVGFilter::setSourceDocument( const Reference< XComponent >& xDoc )
292 : throw (IllegalArgumentException, RuntimeException, std::exception)
293 : {
294 0 : mxSrcDoc = xDoc;
295 0 : }
296 :
297 0 : void SAL_CALL SVGFilter::setTargetDocument( const Reference< XComponent >& xDoc )
298 : throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException, std::exception)
299 : {
300 0 : mxDstDoc = xDoc;
301 0 : }
302 :
303 0 : bool SVGFilter::isStreamGZip(uno::Reference<io::XInputStream> xInput)
304 : {
305 0 : uno::Reference<io::XSeekable> xSeek(xInput, uno::UNO_QUERY);
306 0 : if(xSeek.is())
307 0 : xSeek->seek(0);
308 :
309 0 : uno::Sequence<sal_Int8> aBuffer(2);
310 0 : const sal_uInt64 nBytes = xInput->readBytes(aBuffer, 2);
311 0 : if (nBytes == 2)
312 : {
313 0 : const sal_Int8* pBuffer = aBuffer.getConstArray();
314 0 : if (pBuffer[0] == 0x1F && static_cast<sal_uInt8>(pBuffer[1]) == 0x8B)
315 0 : return true;
316 : }
317 0 : return false;
318 : }
319 :
320 0 : bool SVGFilter::isStreamSvg(uno::Reference<io::XInputStream> xInput)
321 : {
322 0 : uno::Reference<io::XSeekable> xSeek(xInput, uno::UNO_QUERY);
323 0 : if(xSeek.is())
324 0 : xSeek->seek(0);
325 :
326 0 : const sal_Int32 nLookAhead = 1024;
327 0 : uno::Sequence<sal_Int8> aBuffer(nLookAhead);
328 0 : const sal_uInt64 nBytes = xInput->readBytes(aBuffer, nLookAhead);
329 0 : const sal_Int8* pBuffer = aBuffer.getConstArray();
330 :
331 0 : sal_Int8 aMagic1[] = {'<', 's', 'v', 'g'};
332 0 : sal_Int32 aMagic1Size = sizeof(aMagic1) / sizeof(*aMagic1);
333 :
334 0 : if (std::search(pBuffer, pBuffer + nBytes, aMagic1, aMagic1 + aMagic1Size) != pBuffer + nBytes )
335 0 : return true;
336 :
337 0 : sal_Int8 aMagic2[] = {'D', 'O', 'C', 'T', 'Y', 'P', 'E', ' ', 's', 'v', 'g'};
338 0 : sal_Int32 aMagic2Size = sizeof(aMagic2) / sizeof(*aMagic2);
339 :
340 0 : if (std::search(pBuffer, pBuffer + nBytes, aMagic2, aMagic2 + aMagic2Size) != pBuffer + nBytes)
341 0 : return true;
342 :
343 0 : return false;
344 : }
345 :
346 0 : OUString SAL_CALL SVGFilter::detect(Sequence<PropertyValue>& rDescriptor) throw (RuntimeException, std::exception)
347 : {
348 0 : utl::MediaDescriptor aMediaDescriptor(rDescriptor);
349 0 : uno::Reference<io::XInputStream> xInput(aMediaDescriptor[utl::MediaDescriptor::PROP_INPUTSTREAM()], UNO_QUERY);
350 :
351 0 : if (!xInput.is())
352 0 : return OUString();
353 :
354 0 : if (isStreamGZip(xInput))
355 : {
356 0 : boost::scoped_ptr<SvStream> aStream(utl::UcbStreamHelper::CreateStream(xInput, true ));
357 0 : if(!aStream.get())
358 0 : return OUString();
359 :
360 0 : SvStream* pMemoryStream = new SvMemoryStream;
361 0 : uno::Reference<io::XSeekable> xSeek(xInput, uno::UNO_QUERY);
362 0 : if (!xSeek.is())
363 0 : return OUString();
364 0 : xSeek->seek(0);
365 :
366 0 : GZCodec aCodec;
367 0 : aCodec.BeginCompression();
368 0 : aCodec.Decompress(*aStream.get(), *pMemoryStream);
369 0 : aCodec.EndCompression();
370 0 : pMemoryStream->Seek(STREAM_SEEK_TO_BEGIN);
371 0 : uno::Reference<io::XInputStream> xDecompressedInput(new utl::OSeekableInputStreamWrapper(pMemoryStream, true));
372 :
373 0 : if (xDecompressedInput.is() && isStreamSvg(xDecompressedInput))
374 0 : return OUString(constFilterName);
375 : }
376 : else
377 : {
378 0 : if (isStreamSvg(xInput))
379 0 : return OUString(constFilterName);
380 : }
381 0 : return OUString();
382 : }
383 :
384 : #define SVG_FILTER_IMPL_NAME "com.sun.star.comp.Draw.SVGFilter"
385 : #define SVG_WRITER_IMPL_NAME "com.sun.star.comp.Draw.SVGWriter"
386 :
387 : namespace sdecl = comphelper::service_decl;
388 0 : sdecl::class_<SVGFilter> serviceFilterImpl;
389 0 : const sdecl::ServiceDecl svgFilter(
390 : serviceFilterImpl,
391 : SVG_FILTER_IMPL_NAME,
392 : "com.sun.star.document.ImportFilter;"
393 : "com.sun.star.document.ExportFilter;"
394 : "com.sun.star.document.ExtendedTypeDetection" );
395 :
396 0 : sdecl::class_<SVGWriter, sdecl::with_args<true> > serviceWriterImpl;
397 0 : const sdecl::ServiceDecl svgWriter(
398 : serviceWriterImpl,
399 : SVG_WRITER_IMPL_NAME,
400 : "com.sun.star.svg.SVGWriter" );
401 :
402 : // The C shared lib entry points
403 0 : extern "C" SAL_DLLPUBLIC_EXPORT void* SAL_CALL svgfilter_component_getFactory(
404 : sal_Char const* pImplName,
405 : ::com::sun::star::lang::XMultiServiceFactory* pServiceManager,
406 : ::com::sun::star::registry::XRegistryKey* pRegistryKey )
407 : {
408 0 : if ( rtl_str_compare (pImplName, SVG_FILTER_IMPL_NAME) == 0 )
409 : {
410 0 : return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey, svgFilter );
411 : }
412 0 : else if ( rtl_str_compare (pImplName, SVG_WRITER_IMPL_NAME) == 0 )
413 : {
414 0 : return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey, svgWriter );
415 : }
416 0 : return NULL;
417 0 : }
418 :
419 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|