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