xref: /aoo41x/main/filter/source/svg/svgimport.cxx (revision 9e0fc027)
1*9e0fc027SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9e0fc027SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9e0fc027SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9e0fc027SAndrew Rist  * distributed with this work for additional information
6*9e0fc027SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9e0fc027SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9e0fc027SAndrew Rist  * "License"); you may not use this file except in compliance
9*9e0fc027SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9e0fc027SAndrew Rist  *
11*9e0fc027SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9e0fc027SAndrew Rist  *
13*9e0fc027SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9e0fc027SAndrew Rist  * software distributed under the License is distributed on an
15*9e0fc027SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9e0fc027SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9e0fc027SAndrew Rist  * specific language governing permissions and limitations
18*9e0fc027SAndrew Rist  * under the License.
19*9e0fc027SAndrew Rist  *
20*9e0fc027SAndrew Rist  *************************************************************/
21*9e0fc027SAndrew Rist 
22*9e0fc027SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_filter.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "svgfilter.hxx"
28cdf0e10cSrcweir #include "rtl/ref.hxx"
29cdf0e10cSrcweir #include "jvmaccess/virtualmachine.hxx"
30cdf0e10cSrcweir // -------------
31cdf0e10cSrcweir // - SVGFilter -
32cdf0e10cSrcweir // -------------
33cdf0e10cSrcweir 
implImport(const Sequence<PropertyValue> & rDescriptor)34cdf0e10cSrcweir sal_Bool SVGFilter::implImport( const Sequence< PropertyValue >& rDescriptor )
35cdf0e10cSrcweir 	throw (RuntimeException)
36cdf0e10cSrcweir {
37cdf0e10cSrcweir     Reference< XMultiServiceFactory >	xServiceFactory( ::comphelper::getProcessServiceFactory() ) ;
38cdf0e10cSrcweir 	rtl::OUString							aTmpFileName;
39cdf0e10cSrcweir 	String								aFileName;
40cdf0e10cSrcweir 	sal_Int32							nLength = rDescriptor.getLength();
41cdf0e10cSrcweir 	const PropertyValue*				pValue = rDescriptor.getConstArray();
42cdf0e10cSrcweir 	sal_Bool							bRet = sal_False;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 	for( sal_Int32 i = 0 ; ( i < nLength ) && !aTmpFileName.getLength(); i++)
45cdf0e10cSrcweir 		if( pValue[ i ].Name.equalsAscii( "FileName" ) )
46cdf0e10cSrcweir 			pValue[ i ].Value >>= aTmpFileName;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir     if( aTmpFileName.getLength() && xServiceFactory.is() )
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	    Reference< XJavaVM >    xJavaVM( xServiceFactory->createInstance( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine") ) ), UNO_QUERY );
52cdf0e10cSrcweir         Sequence< sal_Int8 >    aProcessID( 17 );
53cdf0e10cSrcweir 		String					aLocalFile;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 		if( ::utl::LocalFileHelper::ConvertURLToPhysicalName( aTmpFileName, aLocalFile ) && aLocalFile.Len() )
56cdf0e10cSrcweir         {
57cdf0e10cSrcweir 			rtl_getGlobalProcessId( (sal_uInt8 *) aProcessID.getArray() );
58cdf0e10cSrcweir             aProcessID[16] = 0;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir             OSL_ENSURE(sizeof (sal_Int64)
61cdf0e10cSrcweir                        >= sizeof (jvmaccess::VirtualMachine *),
62cdf0e10cSrcweir                        "Pointer cannot be represented as sal_Int64");
63cdf0e10cSrcweir             sal_Int64 nPointer = reinterpret_cast< sal_Int64 >(
64cdf0e10cSrcweir                 static_cast< jvmaccess::VirtualMachine * >(0));
65cdf0e10cSrcweir             xJavaVM->getJavaVM(aProcessID) >>= nPointer;
66cdf0e10cSrcweir             rtl::Reference<jvmaccess::VirtualMachine> _virtualMachine =
67cdf0e10cSrcweir                 reinterpret_cast< jvmaccess::VirtualMachine * >(nPointer);
68cdf0e10cSrcweir             if (!_virtualMachine.is())
69cdf0e10cSrcweir                 return bRet;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir             jobjectArray    aArgs;
72cdf0e10cSrcweir             jclass          aClass;
73cdf0e10cSrcweir             jmethodID       aMId;
74cdf0e10cSrcweir             jstring         aJStr;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir             try
77cdf0e10cSrcweir             {
78cdf0e10cSrcweir                 jvmaccess::VirtualMachine::AttachGuard vmGuard(_virtualMachine);
79cdf0e10cSrcweir 
80cdf0e10cSrcweir                 JNIEnv * pEnv = vmGuard.getEnvironment();
81cdf0e10cSrcweir 
82cdf0e10cSrcweir                 aClass = pEnv->FindClass( "SOTranscoder" );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir                 if( aClass )
85cdf0e10cSrcweir                 {
86cdf0e10cSrcweir 					aMId = pEnv->GetStaticMethodID( aClass, "main", "([Ljava/lang/String;)V" );
87cdf0e10cSrcweir 					if ( aMId )
88cdf0e10cSrcweir 					{
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 						::utl::TempFile aTempFile;
91cdf0e10cSrcweir 						String			aOutputURL( aTempFile.GetURL() );
92cdf0e10cSrcweir 						String			aOutputFile;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 						aTempFile.EnableKillingFile();
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 						if( ::utl::LocalFileHelper::ConvertURLToPhysicalName( aOutputURL, aOutputFile ) && aOutputFile.Len() )
97cdf0e10cSrcweir 						{
98cdf0e10cSrcweir 							aJStr = pEnv->NewStringUTF( ByteString( aLocalFile.GetBuffer(), RTL_TEXTENCODING_UTF8 ).GetBuffer() );
99cdf0e10cSrcweir 							aArgs = static_cast<jobjectArray>(pEnv->NewObjectArray( 2, pEnv->FindClass( "java/lang/String" ), aJStr ));
100cdf0e10cSrcweir 							aJStr = pEnv->NewStringUTF( ByteString( aOutputFile.GetBuffer(), RTL_TEXTENCODING_UTF8 ).GetBuffer() );
101cdf0e10cSrcweir 							pEnv->SetObjectArrayElement( aArgs, 1, aJStr );
102cdf0e10cSrcweir 							pEnv->CallStaticVoidMethod( aClass, aMId, aArgs );
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 							Graphic		aGraphic;
105cdf0e10cSrcweir 							SvStream*	pIStm = ::utl::UcbStreamHelper::CreateStream( aOutputURL, STREAM_READ );
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 							if( pIStm )
108cdf0e10cSrcweir 							{
109cdf0e10cSrcweir 								GraphicConverter::Import( *pIStm, aGraphic );
110cdf0e10cSrcweir 								delete pIStm;
111cdf0e10cSrcweir 							}
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 							Reference< XDrawPagesSupplier > xDrawPagesSupplier( mxDstDoc, UNO_QUERY );
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 							if( xDrawPagesSupplier.is() && ( aGraphic.GetType() != GRAPHIC_NONE ) )
116cdf0e10cSrcweir 							{
117cdf0e10cSrcweir 								Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages() );
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 								if( xDrawPages.is() && xDrawPages->getCount() )
120cdf0e10cSrcweir 								{
121cdf0e10cSrcweir 									Reference< XDrawPage >	xDrawPage;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 									if( xDrawPages->getByIndex( 0 ) >>= xDrawPage )
124cdf0e10cSrcweir 									{
125cdf0e10cSrcweir 										Reference< XShapes >		xShapes( xDrawPage, UNO_QUERY );
126cdf0e10cSrcweir 										Reference< XPropertySet>	xPagePropSet( xDrawPage, UNO_QUERY );
127cdf0e10cSrcweir 										Reference< XShape >			xShape( Reference< XMultiServiceFactory >( mxDstDoc, UNO_QUERY )->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.drawing.GraphicObjectShape" ) ) ), UNO_QUERY );
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 										if( xPagePropSet.is() && xShapes.is() && xShape.is() )
130cdf0e10cSrcweir 										{
131cdf0e10cSrcweir 											Reference< XPropertySet >	xPropSet( xShape, UNO_QUERY );
132cdf0e10cSrcweir 											sal_Int32					nPageWidth = 0, nPageHeight = 0;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 											xPagePropSet->getPropertyValue( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Width" ) ) ) >>= nPageWidth;
135cdf0e10cSrcweir 											xPagePropSet->getPropertyValue( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Height" ) ) ) >>= nPageHeight;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 											if( xPropSet.is() && nPageWidth && nPageHeight )
138cdf0e10cSrcweir 											{
139cdf0e10cSrcweir 												xShapes->add( xShape );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 												::com::sun::star::awt::Point	aPos;
142cdf0e10cSrcweir 												::com::sun::star::awt::Size		aSize;
143cdf0e10cSrcweir 												GraphicObject					aGraphObj( aGraphic );
144cdf0e10cSrcweir 												String							aGraphURL( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.GraphicObject:" ) );
145cdf0e10cSrcweir 												Any								aValue;
146cdf0e10cSrcweir 												Size							aGraphicSize;
147cdf0e10cSrcweir 												const MapMode					aTargetMapMode( MAP_100TH_MM );
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 												if( aGraphObj.GetPrefMapMode().GetMapUnit() == MAP_PIXEL )
150cdf0e10cSrcweir 													aGraphicSize = Application::GetDefaultDevice()->PixelToLogic( aGraphObj.GetPrefSize(), aTargetMapMode );
151cdf0e10cSrcweir 												else
152cdf0e10cSrcweir 													aGraphicSize = OutputDevice::LogicToLogic( aGraphObj.GetPrefSize(), aGraphObj.GetPrefMapMode(), aTargetMapMode );
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 												aGraphURL += String( aGraphObj.GetUniqueID(), RTL_TEXTENCODING_ASCII_US );
155cdf0e10cSrcweir 												aValue <<= rtl::OUString( aGraphURL );
156cdf0e10cSrcweir 												xPropSet->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "GraphicURL" ) ), aValue );
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 												aPos.X = ( nPageWidth - aGraphicSize.Width() ) >> 1;
159cdf0e10cSrcweir 												aPos.Y = ( nPageHeight - aGraphicSize.Height() ) >> 1;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 												aSize.Width = aGraphicSize.Width();
162cdf0e10cSrcweir 												aSize.Height = aGraphicSize.Height();
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 												xShape->setPosition( aPos );
165cdf0e10cSrcweir 												xShape->setSize( aSize );
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 												bRet = sal_True;
168cdf0e10cSrcweir 											}
169cdf0e10cSrcweir 										}
170cdf0e10cSrcweir 									}
171cdf0e10cSrcweir 								}
172cdf0e10cSrcweir 							}
173cdf0e10cSrcweir 						}
174cdf0e10cSrcweir                     }
175cdf0e10cSrcweir                 }
176cdf0e10cSrcweir             }
177cdf0e10cSrcweir             catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
178cdf0e10cSrcweir             {
179cdf0e10cSrcweir             }
180cdf0e10cSrcweir         }
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir     return bRet;
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
185