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