1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: svgfilter.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_filter.hxx"
36 #include <comphelper/servicedecl.hxx>
37 #include <uno/environment.h>
38 #include <com/sun/star/drawing/XDrawPage.hpp>
39 #include <com/sun/star/drawing/XDrawView.hpp>
40 #include <com/sun/star/frame/XDesktop.hdl>
41 #include <com/sun/star/frame/XController.hdl>
42 #include <vos/mutex.hxx>
44 #include "svgfilter.hxx"
46 using ::rtl::OUString
;
47 using namespace ::com::sun::star
;
53 SVGFilter::SVGFilter( const Reference
< XComponentContext
>& rxCtx
) :
54 mxMSF( rxCtx
->getServiceManager(),
55 uno::UNO_QUERY_THROW
),
58 mpSVGFontExport( NULL
),
60 mpDefaultSdrPage( NULL
),
62 mbPresentation( sal_False
)
66 // -----------------------------------------------------------------------------
68 SVGFilter::~SVGFilter()
70 DBG_ASSERT( mpSVGDoc
== NULL
, "mpSVGDoc not destroyed" );
71 DBG_ASSERT( mpSVGExport
== NULL
, "mpSVGExport not destroyed" );
72 DBG_ASSERT( mpSVGFontExport
== NULL
, "mpSVGFontExport not destroyed" );
73 DBG_ASSERT( mpSVGWriter
== NULL
, "mpSVGWriter not destroyed" );
74 DBG_ASSERT( mpObjects
== NULL
, "mpObjects not destroyed" );
77 // -----------------------------------------------------------------------------
79 sal_Bool SAL_CALL
SVGFilter::filter( const Sequence
< PropertyValue
>& rDescriptor
)
80 throw (RuntimeException
)
82 vos::OGuard
aGuard( Application::GetSolarMutex() );
83 Window
* pFocusWindow
= Application::GetFocusWindow();
84 sal_Int16 nCurrentPageNumber
= -1;
88 pFocusWindow
->EnterWait();
91 bRet
= implImport( rDescriptor
);
95 uno::Reference
< frame::XDesktop
> xDesktop( mxMSF
->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.frame.Desktop" ) ),
99 uno::Reference
< frame::XFrame
> xFrame( xDesktop
->getCurrentFrame() );
103 uno::Reference
< frame::XController
> xController( xFrame
->getController() );
105 if( xController
.is() )
107 uno::Reference
< drawing::XDrawView
> xDrawView( xController
, uno::UNO_QUERY
);
111 uno::Reference
< drawing::XDrawPage
> xDrawPage( xDrawView
->getCurrentPage() );
115 uno::Reference
< beans::XPropertySet
>( xDrawPage
, uno::UNO_QUERY
)->
116 getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Number" ) ) ) >>= nCurrentPageNumber
;
123 Sequence
< PropertyValue
> aNewDescritor( rDescriptor
);
125 if( nCurrentPageNumber
> 0 )
127 const sal_uInt32 nOldLength
= rDescriptor
.getLength();
129 aNewDescritor
.realloc( nOldLength
+ 1 );
130 aNewDescritor
[ nOldLength
].Name
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PagePos" ) );
131 aNewDescritor
[ nOldLength
].Value
<<= static_cast< sal_Int16
>( nCurrentPageNumber
- 1 );
134 bRet
= implExport( aNewDescritor
);
140 pFocusWindow
->LeaveWait();
145 // -----------------------------------------------------------------------------
147 void SAL_CALL
SVGFilter::cancel( ) throw (RuntimeException
)
151 // -----------------------------------------------------------------------------
153 void SAL_CALL
SVGFilter::setSourceDocument( const Reference
< XComponent
>& xDoc
)
154 throw (IllegalArgumentException
, RuntimeException
)
159 // -----------------------------------------------------------------------------
161 void SAL_CALL
SVGFilter::setTargetDocument( const Reference
< XComponent
>& xDoc
)
162 throw (::com::sun::star::lang::IllegalArgumentException
, RuntimeException
)
167 // -----------------------------------------------------------------------------
169 rtl::OUString SAL_CALL
SVGFilter::detect( Sequence
< PropertyValue
>& io_rDescriptor
) throw (RuntimeException
)
171 uno::Reference
< io::XInputStream
> xInput
;
174 const beans::PropertyValue
* pAttribs
= io_rDescriptor
.getConstArray();
175 const sal_Int32 nAttribs
= io_rDescriptor
.getLength();
176 for( sal_Int32 i
= 0; i
< nAttribs
; i
++ )
178 if( pAttribs
[i
].Name
.equalsAscii( "InputStream" ) )
179 pAttribs
[i
].Value
>>= xInput
;
183 return rtl::OUString();
185 uno::Reference
< io::XSeekable
> xSeek( xInput
, uno::UNO_QUERY
);
189 // read the first 1024 bytes & check a few magic string
190 // constants (heuristically)
191 const sal_Int32 nLookAhead
= 1024;
192 uno::Sequence
< sal_Int8
> aBuf( nLookAhead
);
193 const sal_uInt64 nBytes
=xInput
->readBytes(aBuf
, nLookAhead
);
194 const sal_Int8
* const pBuf
=aBuf
.getConstArray();
196 sal_Int8 aMagic1
[] = {'<', 's', 'v', 'g'};
197 if( std::search(pBuf
, pBuf
+nBytes
,
198 aMagic1
, aMagic1
+sizeof(aMagic1
)/sizeof(*aMagic1
)) != pBuf
+nBytes
)
199 return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("svg_Scalable_Vector_Graphics") );
201 sal_Int8 aMagic2
[] = {'D', 'O', 'C', 'T', 'Y', 'P', 'E', ' ', 's', 'v', 'g'};
202 if( std::search(pBuf
, pBuf
+nBytes
,
203 aMagic2
, aMagic2
+sizeof(aMagic2
)/sizeof(*aMagic2
)) != pBuf
+nBytes
)
204 return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("svg_Scalable_Vector_Graphics") );
206 return rtl::OUString();
209 // -----------------------------------------------------------------------------
211 class FilterConfigItem
;
212 extern "C" SAL_DLLPUBLIC_EXPORT BOOL __LOADONCALLAPI
GraphicImport(SvStream
& rStream
, Graphic
& rGraphic
, FilterConfigItem
*, BOOL
)
214 return importSvg( rStream
, rGraphic
);
217 // -----------------------------------------------------------------------------
219 namespace sdecl
= comphelper::service_decl
;
220 sdecl::class_
<SVGFilter
> serviceImpl
;
221 const sdecl::ServiceDecl
svgFilter(
223 "com.sun.star.comp.Draw.SVGFilter",
224 "com.sun.star.document.ImportFilter;"
225 "com.sun.star.document.ExportFilter;"
226 "com.sun.star.document.ExtendedTypeDetection" );
228 // The C shared lib entry points
229 COMPHELPER_SERVICEDECL_EXPORTS1(svgFilter
)