xref: /trunk/main/svtools/source/graphic/provider.cxx (revision ddde725d65c83fe3ba1186d46f6e3e08f12ba47e)
15900e8ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
35900e8ecSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
45900e8ecSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
55900e8ecSAndrew Rist  * distributed with this work for additional information
65900e8ecSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
75900e8ecSAndrew Rist  * to you under the Apache License, Version 2.0 (the
85900e8ecSAndrew Rist  * "License"); you may not use this file except in compliance
95900e8ecSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
115900e8ecSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
135900e8ecSAndrew Rist  * Unless required by applicable law or agreed to in writing,
145900e8ecSAndrew Rist  * software distributed under the License is distributed on an
155900e8ecSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
165900e8ecSAndrew Rist  * KIND, either express or implied.  See the License for the
175900e8ecSAndrew Rist  * specific language governing permissions and limitations
185900e8ecSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
205900e8ecSAndrew Rist  *************************************************************/
215900e8ecSAndrew Rist 
225900e8ecSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svtools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <rtl/uuid.h>
28cdf0e10cSrcweir #include <vos/mutex.hxx>
29cdf0e10cSrcweir #include <vcl/svapp.hxx>
30cdf0e10cSrcweir #include <vcl/image.hxx>
31cdf0e10cSrcweir #include <vcl/metaact.hxx>
32cdf0e10cSrcweir #include <vcl/msgbox.hxx>
33cdf0e10cSrcweir #include <vcl/imagerepository.hxx>
34cdf0e10cSrcweir #include <tools/rcid.h>
35cdf0e10cSrcweir #include <tools/resid.hxx>
36cdf0e10cSrcweir #include <tools/resmgr.hxx>
37cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
38cdf0e10cSrcweir #include <svtools/filter.hxx>
39cdf0e10cSrcweir #include <svl/solar.hrc>
40cdf0e10cSrcweir #include <vcl/salbtype.hxx>
41cdf0e10cSrcweir #include <vcl/virdev.hxx>
42cdf0e10cSrcweir #include <com/sun/star/io/XStream.hpp>
43cdf0e10cSrcweir #include <com/sun/star/text/GraphicCrop.hpp>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include "descriptor.hxx"
46cdf0e10cSrcweir #include "graphic.hxx"
47cdf0e10cSrcweir #include <svtools/grfmgr.hxx>
48cdf0e10cSrcweir #include "provider.hxx"
49cdf0e10cSrcweir 
50cdf0e10cSrcweir using namespace com::sun::star;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir namespace unographic {
53cdf0e10cSrcweir 
54cdf0e10cSrcweir #define UNO_NAME_GRAPHOBJ_URLPREFIX                             "vnd.sun.star.GraphicObject:"
55cdf0e10cSrcweir 
56cdf0e10cSrcweir // -------------------
57cdf0e10cSrcweir // - GraphicProvider -
58cdf0e10cSrcweir // -------------------
59cdf0e10cSrcweir 
60cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL GraphicProvider_CreateInstance( const uno::Reference< lang::XMultiServiceFactory >& )
61cdf0e10cSrcweir {
62cdf0e10cSrcweir     return SAL_STATIC_CAST( ::cppu::OWeakObject*, new GraphicProvider );
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir GraphicProvider::GraphicProvider()
66cdf0e10cSrcweir {
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir // ------------------------------------------------------------------------------
70cdf0e10cSrcweir 
71cdf0e10cSrcweir GraphicProvider::~GraphicProvider()
72cdf0e10cSrcweir {
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir // ------------------------------------------------------------------------------
76cdf0e10cSrcweir 
77cdf0e10cSrcweir ::rtl::OUString GraphicProvider::getImplementationName_Static()
78cdf0e10cSrcweir     throw()
79cdf0e10cSrcweir {
80cdf0e10cSrcweir     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.graphic.GraphicProvider" ) );
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir // ------------------------------------------------------------------------------
84cdf0e10cSrcweir 
85cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > GraphicProvider::getSupportedServiceNames_Static()
86cdf0e10cSrcweir     throw()
87cdf0e10cSrcweir {
88cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > aSeq( 1 );
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     aSeq.getArray()[ 0 ] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.graphic.GraphicProvider" ) );
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     return aSeq;
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir // ------------------------------------------------------------------------------
96cdf0e10cSrcweir 
97cdf0e10cSrcweir ::rtl::OUString SAL_CALL GraphicProvider::getImplementationName()
98cdf0e10cSrcweir     throw( uno::RuntimeException )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     return getImplementationName_Static();
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir // ------------------------------------------------------------------------------
104cdf0e10cSrcweir 
105cdf0e10cSrcweir sal_Bool SAL_CALL GraphicProvider::supportsService( const ::rtl::OUString& ServiceName )
106cdf0e10cSrcweir     throw( uno::RuntimeException )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString >    aSNL( getSupportedServiceNames() );
109cdf0e10cSrcweir     const ::rtl::OUString*              pArray = aSNL.getConstArray();
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     for( int i = 0; i < aSNL.getLength(); i++ )
112cdf0e10cSrcweir         if( pArray[i] == ServiceName )
113cdf0e10cSrcweir             return true;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     return false;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir // ------------------------------------------------------------------------------
119cdf0e10cSrcweir 
120cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL GraphicProvider::getSupportedServiceNames()
121cdf0e10cSrcweir     throw( uno::RuntimeException )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir     return getSupportedServiceNames_Static();
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir // ------------------------------------------------------------------------------
127cdf0e10cSrcweir 
128cdf0e10cSrcweir uno::Sequence< uno::Type > SAL_CALL GraphicProvider::getTypes()
129cdf0e10cSrcweir     throw(uno::RuntimeException)
130cdf0e10cSrcweir {
131cdf0e10cSrcweir     uno::Sequence< uno::Type >  aTypes( 3 );
132cdf0e10cSrcweir     uno::Type*                  pTypes = aTypes.getArray();
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     *pTypes++ = ::getCppuType((const uno::Reference< lang::XServiceInfo>*)0);
135cdf0e10cSrcweir     *pTypes++ = ::getCppuType((const uno::Reference< lang::XTypeProvider>*)0);
136cdf0e10cSrcweir     *pTypes++ = ::getCppuType((const uno::Reference< graphic::XGraphicProvider>*)0);
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     return aTypes;
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir // ------------------------------------------------------------------------------
142cdf0e10cSrcweir 
143cdf0e10cSrcweir uno::Sequence< sal_Int8 > SAL_CALL GraphicProvider::getImplementationId()
144cdf0e10cSrcweir     throw(uno::RuntimeException)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir     vos::OGuard                         aGuard( Application::GetSolarMutex() );
147cdf0e10cSrcweir     static uno::Sequence< sal_Int8 >    aId;
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     if( aId.getLength() == 0 )
150cdf0e10cSrcweir     {
151cdf0e10cSrcweir         aId.realloc( 16 );
152cdf0e10cSrcweir         rtl_createUuid( reinterpret_cast< sal_uInt8* >( aId.getArray() ), 0, sal_True );
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     return aId;
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir // ------------------------------------------------------------------------------
159cdf0e10cSrcweir 
160cdf0e10cSrcweir uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadGraphicObject( const ::rtl::OUString& rResourceURL ) const
161cdf0e10cSrcweir {
162cdf0e10cSrcweir     uno::Reference< ::graphic::XGraphic >   xRet;
163cdf0e10cSrcweir     if( rResourceURL.compareToAscii( UNO_NAME_GRAPHOBJ_URLPREFIX, RTL_CONSTASCII_LENGTH( UNO_NAME_GRAPHOBJ_URLPREFIX ) ) == 0 )
164cdf0e10cSrcweir     {
165cdf0e10cSrcweir         // graphic manager url
166cdf0e10cSrcweir         String aTmpStr( rResourceURL.copy( sizeof( UNO_NAME_GRAPHOBJ_URLPREFIX ) - 1 ) );
167cdf0e10cSrcweir         ByteString aUniqueID( aTmpStr, RTL_TEXTENCODING_UTF8 );
168cdf0e10cSrcweir         GraphicObject aGrafObj( aUniqueID );
169cdf0e10cSrcweir         // I don't call aGrafObj.GetXGraphic because it will call us back
170cdf0e10cSrcweir         // into implLoadMemory ( with "private:memorygraphic" test )
171cdf0e10cSrcweir         ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic;
172cdf0e10cSrcweir         pUnoGraphic->init( aGrafObj.GetGraphic() );
173cdf0e10cSrcweir         xRet = pUnoGraphic;
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir     return xRet;
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadMemory( const ::rtl::OUString& rResourceURL ) const
179cdf0e10cSrcweir {
180cdf0e10cSrcweir     uno::Reference< ::graphic::XGraphic >   xRet;
181cdf0e10cSrcweir     sal_Int32                               nIndex = 0;
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     if( ( 0 == rResourceURL.getToken( 0, '/', nIndex ).compareToAscii( "private:memorygraphic" ) ) )
184cdf0e10cSrcweir     {
185cdf0e10cSrcweir         sal_Int64 nGraphicAddress = rResourceURL.getToken( 0, '/', nIndex ).toInt64();
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         if( nGraphicAddress )
188cdf0e10cSrcweir         {
189cdf0e10cSrcweir             ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir             pUnoGraphic->init( *reinterpret_cast< ::Graphic* >( nGraphicAddress ) );
192cdf0e10cSrcweir             xRet = pUnoGraphic;
193cdf0e10cSrcweir         }
194cdf0e10cSrcweir     }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir     return xRet;
197cdf0e10cSrcweir }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir // ------------------------------------------------------------------------------
200cdf0e10cSrcweir 
201cdf0e10cSrcweir uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadRepositoryImage( const ::rtl::OUString& rResourceURL ) const
202cdf0e10cSrcweir {
203cdf0e10cSrcweir     uno::Reference< ::graphic::XGraphic >   xRet;
204cdf0e10cSrcweir     sal_Int32                               nIndex = 0;
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     if( ( 0 == rResourceURL.getToken( 0, '/', nIndex ).compareToAscii( "private:graphicrepository" ) ) )
207cdf0e10cSrcweir     {
208cdf0e10cSrcweir         String sPathName( rResourceURL.copy( nIndex ) );
209cdf0e10cSrcweir         BitmapEx aBitmap;
210cdf0e10cSrcweir         if ( ::vcl::ImageRepository::loadImage( sPathName, aBitmap, false ) )
211cdf0e10cSrcweir         {
212cdf0e10cSrcweir             Image aImage( aBitmap );
213cdf0e10cSrcweir             xRet = aImage.GetXGraphic();
214cdf0e10cSrcweir         }
215cdf0e10cSrcweir     }
216cdf0e10cSrcweir     return xRet;
217cdf0e10cSrcweir }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 
220cdf0e10cSrcweir // ------------------------------------------------------------------------------
221cdf0e10cSrcweir 
222cdf0e10cSrcweir uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadStandardImage( const ::rtl::OUString& rResourceURL ) const
223cdf0e10cSrcweir {
224cdf0e10cSrcweir     uno::Reference< ::graphic::XGraphic >   xRet;
225cdf0e10cSrcweir     sal_Int32                               nIndex = 0;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir     if( ( 0 == rResourceURL.getToken( 0, '/', nIndex ).compareToAscii( "private:standardimage" ) ) )
228cdf0e10cSrcweir     {
229cdf0e10cSrcweir         rtl::OUString sImageName( rResourceURL.copy( nIndex ) );
230cdf0e10cSrcweir         if ( sImageName.equalsAscii( "info" ) )
231cdf0e10cSrcweir         {
232cdf0e10cSrcweir             xRet = InfoBox::GetStandardImage().GetXGraphic();
233cdf0e10cSrcweir         }
234cdf0e10cSrcweir         else if ( sImageName.equalsAscii( "warning" ) )
235cdf0e10cSrcweir         {
236cdf0e10cSrcweir             xRet = WarningBox::GetStandardImage().GetXGraphic();
237cdf0e10cSrcweir         }
238cdf0e10cSrcweir         else if ( sImageName.equalsAscii( "error" ) )
239cdf0e10cSrcweir         {
240cdf0e10cSrcweir             xRet = ErrorBox::GetStandardImage().GetXGraphic();
241cdf0e10cSrcweir         }
242cdf0e10cSrcweir         else if ( sImageName.equalsAscii( "query" ) )
243cdf0e10cSrcweir         {
244cdf0e10cSrcweir             xRet = QueryBox::GetStandardImage().GetXGraphic();
245cdf0e10cSrcweir         }
246cdf0e10cSrcweir     }
247cdf0e10cSrcweir     return xRet;
248cdf0e10cSrcweir }
249cdf0e10cSrcweir 
250cdf0e10cSrcweir // ------------------------------------------------------------------------------
251cdf0e10cSrcweir 
252cdf0e10cSrcweir uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadBitmap( const uno::Reference< awt::XBitmap >& xBtm ) const
253cdf0e10cSrcweir {
254cdf0e10cSrcweir     uno::Reference< ::graphic::XGraphic > xRet;
255cdf0e10cSrcweir     uno::Sequence< sal_Int8 > aBmpSeq( xBtm->getDIB() );
256cdf0e10cSrcweir     uno::Sequence< sal_Int8 > aMaskSeq( xBtm->getMaskDIB() );
257cdf0e10cSrcweir     SvMemoryStream aBmpStream( aBmpSeq.getArray(), aBmpSeq.getLength(), STREAM_READ );
258cdf0e10cSrcweir     Bitmap aBmp;
259cdf0e10cSrcweir     aBmpStream >> aBmp;
260cdf0e10cSrcweir 
261cdf0e10cSrcweir     BitmapEx aBmpEx;
262cdf0e10cSrcweir 
263cdf0e10cSrcweir     if( aMaskSeq.getLength() )
264cdf0e10cSrcweir     {
265cdf0e10cSrcweir         SvMemoryStream aMaskStream( aMaskSeq.getArray(), aMaskSeq.getLength(), STREAM_READ );
266cdf0e10cSrcweir         Bitmap aMask;
267cdf0e10cSrcweir         aMaskStream >> aMask;
268cdf0e10cSrcweir         aBmpEx = BitmapEx( aBmp, aMask );
269cdf0e10cSrcweir     }
270cdf0e10cSrcweir     else
271cdf0e10cSrcweir         aBmpEx = BitmapEx( aBmp );
272cdf0e10cSrcweir 
273cdf0e10cSrcweir     if( !aBmpEx.IsEmpty() )
274cdf0e10cSrcweir     {
275cdf0e10cSrcweir         ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic;
276cdf0e10cSrcweir 
277cdf0e10cSrcweir         pUnoGraphic->init( aBmpEx );
278cdf0e10cSrcweir         xRet = pUnoGraphic;
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir     return xRet;
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
283cdf0e10cSrcweir // ------------------------------------------------------------------------------
284cdf0e10cSrcweir 
285cdf0e10cSrcweir uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadResource( const ::rtl::OUString& rResourceURL ) const
286cdf0e10cSrcweir {
287cdf0e10cSrcweir     uno::Reference< ::graphic::XGraphic >   xRet;
288cdf0e10cSrcweir     sal_Int32                               nIndex = 0;
289cdf0e10cSrcweir 
290cdf0e10cSrcweir     if( ( 0 == rResourceURL.getToken( 0, '/', nIndex ).compareToAscii( "private:resource" ) ) )
291cdf0e10cSrcweir     {
292cdf0e10cSrcweir         ByteString aResMgrName( String( rResourceURL.getToken( 0, '/', nIndex ) ), RTL_TEXTENCODING_ASCII_US );
293cdf0e10cSrcweir 
294cdf0e10cSrcweir         ResMgr* pResMgr = ResMgr::CreateResMgr( aResMgrName.GetBuffer(), Application::GetSettings().GetUILocale() );
295cdf0e10cSrcweir 
296cdf0e10cSrcweir         if( pResMgr )
297cdf0e10cSrcweir         {
298cdf0e10cSrcweir             const ::rtl::OUString   aResourceType( rResourceURL.getToken( 0, '/', nIndex ) );
299cdf0e10cSrcweir             const ResId             aResId( rResourceURL.getToken( 0, '/', nIndex ).toInt32(), *pResMgr );
300cdf0e10cSrcweir 
301cdf0e10cSrcweir             if( aResourceType.getLength() )
302cdf0e10cSrcweir             {
303cdf0e10cSrcweir                 BitmapEx aBmpEx;
304cdf0e10cSrcweir 
305cdf0e10cSrcweir                 if( ( 0 == aResourceType.compareToAscii( "bitmap" ) ) ||
306cdf0e10cSrcweir                     ( 0 == aResourceType.compareToAscii( "bitmapex" ) ) )
307cdf0e10cSrcweir                 {
308cdf0e10cSrcweir                     aResId.SetRT( RSC_BITMAP );
309cdf0e10cSrcweir 
310cdf0e10cSrcweir                     if( pResMgr->IsAvailable( aResId ) )
311cdf0e10cSrcweir                     {
312cdf0e10cSrcweir                         aBmpEx = BitmapEx( aResId );
313cdf0e10cSrcweir                     }
314cdf0e10cSrcweir                 }
315cdf0e10cSrcweir                 else if( 0 == aResourceType.compareToAscii( "image" ) )
316cdf0e10cSrcweir                 {
317cdf0e10cSrcweir                     aResId.SetRT( RSC_IMAGE );
318cdf0e10cSrcweir 
319cdf0e10cSrcweir                     if( pResMgr->IsAvailable( aResId ) )
320cdf0e10cSrcweir                     {
321cdf0e10cSrcweir                         const Image aImage( aResId );
322cdf0e10cSrcweir                         aBmpEx = aImage.GetBitmapEx();
323cdf0e10cSrcweir                     }
324cdf0e10cSrcweir                 }
325cdf0e10cSrcweir                 else if( 0 == aResourceType.compareToAscii( "imagelist" ) )
326cdf0e10cSrcweir                 {
327cdf0e10cSrcweir                     aResId.SetRT( RSC_IMAGELIST );
328cdf0e10cSrcweir 
329cdf0e10cSrcweir                     if( pResMgr->IsAvailable( aResId ) )
330cdf0e10cSrcweir                     {
331cdf0e10cSrcweir                         const ImageList aImageList( aResId );
332cdf0e10cSrcweir                         sal_Int32       nImageId = ( nIndex > -1 ) ? rResourceURL.getToken( 0, '/', nIndex ).toInt32() : 0;
333cdf0e10cSrcweir 
334cdf0e10cSrcweir                         if( 0 < nImageId )
335cdf0e10cSrcweir                         {
336cdf0e10cSrcweir                             const Image aImage( aImageList.GetImage( sal::static_int_cast< sal_uInt16 >(nImageId) ) );
337cdf0e10cSrcweir                             aBmpEx = aImage.GetBitmapEx();
338cdf0e10cSrcweir                         }
339cdf0e10cSrcweir                         else
340cdf0e10cSrcweir                         {
341cdf0e10cSrcweir                             aBmpEx = aImageList.GetAsHorizontalStrip();
342cdf0e10cSrcweir                         }
343cdf0e10cSrcweir                     }
344cdf0e10cSrcweir                 }
345cdf0e10cSrcweir 
346cdf0e10cSrcweir                 if( !aBmpEx.IsEmpty() )
347cdf0e10cSrcweir                 {
348cdf0e10cSrcweir                     ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic;
349cdf0e10cSrcweir 
350cdf0e10cSrcweir                     pUnoGraphic->init( aBmpEx );
351cdf0e10cSrcweir                     xRet = pUnoGraphic;
352cdf0e10cSrcweir                 }
353cdf0e10cSrcweir             }
354cdf0e10cSrcweir 
355cdf0e10cSrcweir             delete pResMgr;
356cdf0e10cSrcweir         }
357cdf0e10cSrcweir     }
358cdf0e10cSrcweir 
359cdf0e10cSrcweir     return xRet;
360cdf0e10cSrcweir }
361cdf0e10cSrcweir 
362cdf0e10cSrcweir // ------------------------------------------------------------------------------
363cdf0e10cSrcweir 
364cdf0e10cSrcweir uno::Reference< beans::XPropertySet > SAL_CALL GraphicProvider::queryGraphicDescriptor( const uno::Sequence< beans::PropertyValue >& rMediaProperties )
365cdf0e10cSrcweir     throw ( io::IOException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
366cdf0e10cSrcweir {
367cdf0e10cSrcweir     uno::Reference< beans::XPropertySet > xRet;
368cdf0e10cSrcweir 
369cdf0e10cSrcweir     ::rtl::OUString aURL;
370cdf0e10cSrcweir     uno::Reference< io::XInputStream > xIStm;
371cdf0e10cSrcweir     uno::Reference< awt::XBitmap >xBtm;
372cdf0e10cSrcweir 
373cdf0e10cSrcweir     for( sal_Int32 i = 0; ( i < rMediaProperties.getLength() ) && !xRet.is(); ++i )
374cdf0e10cSrcweir     {
375cdf0e10cSrcweir         const ::rtl::OUString   aName( rMediaProperties[ i ].Name );
376cdf0e10cSrcweir         const uno::Any          aValue( rMediaProperties[ i ].Value );
377cdf0e10cSrcweir 
378cdf0e10cSrcweir         if( COMPARE_EQUAL == aName.compareToAscii( "URL" ) )
379cdf0e10cSrcweir         {
380cdf0e10cSrcweir             aValue >>= aURL;
381cdf0e10cSrcweir         }
382cdf0e10cSrcweir         else if( COMPARE_EQUAL == aName.compareToAscii( "InputStream" ) )
383cdf0e10cSrcweir         {
384cdf0e10cSrcweir             aValue >>= xIStm;
385cdf0e10cSrcweir         }
386cdf0e10cSrcweir         else if( COMPARE_EQUAL == aName.compareToAscii( "Bitmap" ) )
387cdf0e10cSrcweir         {
388cdf0e10cSrcweir             aValue >>= xBtm;
389cdf0e10cSrcweir         }
390cdf0e10cSrcweir     }
391cdf0e10cSrcweir 
392cdf0e10cSrcweir     if( xIStm.is() )
393cdf0e10cSrcweir     {
394cdf0e10cSrcweir         GraphicDescriptor* pDescriptor = new GraphicDescriptor;
395cdf0e10cSrcweir         pDescriptor->init( xIStm, aURL );
396cdf0e10cSrcweir         xRet = pDescriptor;
397cdf0e10cSrcweir     }
398cdf0e10cSrcweir     else if( aURL.getLength() )
399cdf0e10cSrcweir     {
400cdf0e10cSrcweir         uno::Reference< ::graphic::XGraphic > xGraphic( implLoadMemory( aURL ) );
401cdf0e10cSrcweir         if( !xGraphic.is() )
402cdf0e10cSrcweir             xGraphic = implLoadResource( aURL );
403cdf0e10cSrcweir         if( !xGraphic.is() )
404cdf0e10cSrcweir             xGraphic = implLoadGraphicObject( aURL );
405cdf0e10cSrcweir 
406cdf0e10cSrcweir         if ( !xGraphic.is() )
407cdf0e10cSrcweir             xGraphic = implLoadRepositoryImage( aURL );
408cdf0e10cSrcweir 
409cdf0e10cSrcweir         if ( !xGraphic.is() )
410cdf0e10cSrcweir             xGraphic = implLoadStandardImage( aURL );
411cdf0e10cSrcweir 
412cdf0e10cSrcweir         if( xGraphic.is() )
413cdf0e10cSrcweir         {
414cdf0e10cSrcweir             xRet = uno::Reference< beans::XPropertySet >( xGraphic, uno::UNO_QUERY );
415cdf0e10cSrcweir         }
416cdf0e10cSrcweir         else
417cdf0e10cSrcweir         {
418cdf0e10cSrcweir             GraphicDescriptor* pDescriptor = new GraphicDescriptor;
419cdf0e10cSrcweir             pDescriptor->init( aURL );
420cdf0e10cSrcweir             xRet = pDescriptor;
421cdf0e10cSrcweir         }
422cdf0e10cSrcweir     }
423cdf0e10cSrcweir     else if( xBtm.is() )
424cdf0e10cSrcweir     {
425cdf0e10cSrcweir         uno::Reference< ::graphic::XGraphic > xGraphic( implLoadBitmap( xBtm ) );
426cdf0e10cSrcweir         if( xGraphic.is() )
427cdf0e10cSrcweir             xRet = uno::Reference< beans::XPropertySet >( xGraphic, uno::UNO_QUERY );
428cdf0e10cSrcweir     }
429cdf0e10cSrcweir 
430cdf0e10cSrcweir     return xRet;
431cdf0e10cSrcweir }
432cdf0e10cSrcweir 
433cdf0e10cSrcweir // ------------------------------------------------------------------------------
434cdf0e10cSrcweir 
435cdf0e10cSrcweir uno::Reference< ::graphic::XGraphic > SAL_CALL GraphicProvider::queryGraphic( const uno::Sequence< ::beans::PropertyValue >& rMediaProperties )
436cdf0e10cSrcweir     throw ( io::IOException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
437cdf0e10cSrcweir {
438cdf0e10cSrcweir     uno::Reference< ::graphic::XGraphic >   xRet;
439cdf0e10cSrcweir     String                                  aPath;
440cdf0e10cSrcweir     SvStream*                               pIStm = NULL;
441cdf0e10cSrcweir 
442cdf0e10cSrcweir     uno::Reference< io::XInputStream > xIStm;
443cdf0e10cSrcweir     uno::Reference< awt::XBitmap >xBtm;
444cdf0e10cSrcweir 
445cdf0e10cSrcweir     for( sal_Int32 i = 0; ( i < rMediaProperties.getLength() ) && !pIStm && !xRet.is(); ++i )
446cdf0e10cSrcweir     {
447cdf0e10cSrcweir         const ::rtl::OUString   aName( rMediaProperties[ i ].Name );
448cdf0e10cSrcweir         const uno::Any          aValue( rMediaProperties[ i ].Value );
449cdf0e10cSrcweir 
450cdf0e10cSrcweir         if( COMPARE_EQUAL == aName.compareToAscii( "URL" ) )
451cdf0e10cSrcweir         {
452cdf0e10cSrcweir             ::rtl::OUString aURL;
453cdf0e10cSrcweir             aValue >>= aURL;
454cdf0e10cSrcweir             aPath = aURL;
455cdf0e10cSrcweir         }
456cdf0e10cSrcweir         else if( COMPARE_EQUAL == aName.compareToAscii( "InputStream" ) )
457cdf0e10cSrcweir         {
458cdf0e10cSrcweir             aValue >>= xIStm;
459cdf0e10cSrcweir         }
460cdf0e10cSrcweir         else if( COMPARE_EQUAL == aName.compareToAscii( "Bitmap" ) )
461cdf0e10cSrcweir         {
462cdf0e10cSrcweir             aValue >>= xBtm;
463cdf0e10cSrcweir         }
464cdf0e10cSrcweir     }
465cdf0e10cSrcweir 
466cdf0e10cSrcweir     if( xIStm.is() )
467cdf0e10cSrcweir     {
468cdf0e10cSrcweir         pIStm = ::utl::UcbStreamHelper::CreateStream( xIStm );
469cdf0e10cSrcweir     }
470cdf0e10cSrcweir     else if( aPath.Len() )
471cdf0e10cSrcweir     {
472cdf0e10cSrcweir         xRet = implLoadMemory( aPath );
473cdf0e10cSrcweir 
474cdf0e10cSrcweir         if( !xRet.is() )
475cdf0e10cSrcweir             xRet = implLoadGraphicObject( aPath );
476cdf0e10cSrcweir 
477cdf0e10cSrcweir         if( !xRet.is() )
478cdf0e10cSrcweir             xRet = implLoadResource( aPath );
479cdf0e10cSrcweir 
480cdf0e10cSrcweir         if ( !xRet.is() )
481cdf0e10cSrcweir             xRet = implLoadRepositoryImage( aPath );
482cdf0e10cSrcweir 
483cdf0e10cSrcweir         if ( !xRet.is() )
484cdf0e10cSrcweir             xRet = implLoadStandardImage( aPath );
485cdf0e10cSrcweir 
486cdf0e10cSrcweir         if( !xRet.is() )
487cdf0e10cSrcweir             pIStm = ::utl::UcbStreamHelper::CreateStream( aPath, STREAM_READ );
488cdf0e10cSrcweir     }
489cdf0e10cSrcweir     else if( xBtm.is() )
490cdf0e10cSrcweir     {
491cdf0e10cSrcweir         xRet = implLoadBitmap( xBtm );
492cdf0e10cSrcweir     }
493cdf0e10cSrcweir 
494cdf0e10cSrcweir     if( pIStm )
495cdf0e10cSrcweir     {
496cdf0e10cSrcweir         ::GraphicFilter* pFilter = ::GraphicFilter::GetGraphicFilter();
497cdf0e10cSrcweir 
498cdf0e10cSrcweir         if( pFilter )
499cdf0e10cSrcweir         {
500cdf0e10cSrcweir             ::Graphic aVCLGraphic;
501cdf0e10cSrcweir 
502cdf0e10cSrcweir             if( ( pFilter->ImportGraphic( aVCLGraphic, aPath, *pIStm ) == GRFILTER_OK ) &&
503cdf0e10cSrcweir                 ( aVCLGraphic.GetType() != GRAPHIC_NONE ) )
504cdf0e10cSrcweir             {
505cdf0e10cSrcweir                 ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic;
506cdf0e10cSrcweir 
507cdf0e10cSrcweir                 pUnoGraphic->init( aVCLGraphic );
508cdf0e10cSrcweir                 xRet = pUnoGraphic;
509cdf0e10cSrcweir             }
510cdf0e10cSrcweir         }
511cdf0e10cSrcweir 
512cdf0e10cSrcweir         delete pIStm;
513cdf0e10cSrcweir     }
514cdf0e10cSrcweir 
515cdf0e10cSrcweir     return xRet;
516cdf0e10cSrcweir }
517cdf0e10cSrcweir 
518cdf0e10cSrcweir void ImplCalculateCropRect( ::Graphic& rGraphic, const text::GraphicCrop& rGraphicCropLogic, Rectangle& rGraphicCropPixel )
519cdf0e10cSrcweir {
520cdf0e10cSrcweir     if ( rGraphicCropLogic.Left || rGraphicCropLogic.Top || rGraphicCropLogic.Right || rGraphicCropLogic.Bottom )
521cdf0e10cSrcweir     {
522cdf0e10cSrcweir         Size aSourceSizePixel( rGraphic.GetSizePixel() );
523cdf0e10cSrcweir         if ( aSourceSizePixel.Width() && aSourceSizePixel.Height() )
524cdf0e10cSrcweir         {
525cdf0e10cSrcweir             if ( rGraphicCropLogic.Left || rGraphicCropLogic.Top || rGraphicCropLogic.Right || rGraphicCropLogic.Bottom )
526cdf0e10cSrcweir             {
527cdf0e10cSrcweir                 Size aSize100thMM( 0, 0 );
528cdf0e10cSrcweir                 if( rGraphic.GetPrefMapMode().GetMapUnit() != MAP_PIXEL )
529cdf0e10cSrcweir                 {
530cdf0e10cSrcweir                     aSize100thMM = OutputDevice::LogicToLogic( rGraphic.GetPrefSize(), rGraphic.GetPrefMapMode(), MAP_100TH_MM );
531cdf0e10cSrcweir                 }
532cdf0e10cSrcweir                 else
533cdf0e10cSrcweir                 {
534cdf0e10cSrcweir                     aSize100thMM = Application::GetDefaultDevice()->PixelToLogic( rGraphic.GetPrefSize(), MAP_100TH_MM );
535cdf0e10cSrcweir                 }
536cdf0e10cSrcweir                 if ( aSize100thMM.Width() && aSize100thMM.Height() )
537cdf0e10cSrcweir                 {
538cdf0e10cSrcweir                     double fSourceSizePixelWidth = static_cast<double>(aSourceSizePixel.Width());
539cdf0e10cSrcweir                     double fSourceSizePixelHeight= static_cast<double>(aSourceSizePixel.Height());
540cdf0e10cSrcweir                     rGraphicCropPixel.Left() = static_cast< sal_Int32 >((fSourceSizePixelWidth * rGraphicCropLogic.Left ) / aSize100thMM.Width());
541cdf0e10cSrcweir                     rGraphicCropPixel.Top() = static_cast< sal_Int32 >((fSourceSizePixelHeight * rGraphicCropLogic.Top ) / aSize100thMM.Height());
542cdf0e10cSrcweir                     rGraphicCropPixel.Right() = static_cast< sal_Int32 >(( fSourceSizePixelWidth * ( aSize100thMM.Width() - rGraphicCropLogic.Right ) ) / aSize100thMM.Width() );
543cdf0e10cSrcweir                     rGraphicCropPixel.Bottom() = static_cast< sal_Int32 >(( fSourceSizePixelHeight * ( aSize100thMM.Height() - rGraphicCropLogic.Bottom ) ) / aSize100thMM.Height() );
544cdf0e10cSrcweir                 }
545cdf0e10cSrcweir             }
546cdf0e10cSrcweir         }
547cdf0e10cSrcweir     }
548cdf0e10cSrcweir }
549cdf0e10cSrcweir 
550cdf0e10cSrcweir void ImplApplyBitmapScaling( ::Graphic& rGraphic, sal_Int32 nPixelWidth, sal_Int32 nPixelHeight )
551cdf0e10cSrcweir {
552cdf0e10cSrcweir     if ( nPixelWidth && nPixelHeight )
553cdf0e10cSrcweir     {
554cdf0e10cSrcweir         BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
555cdf0e10cSrcweir         MapMode aPrefMapMode( aBmpEx.GetPrefMapMode() );
556cdf0e10cSrcweir         Size    aPrefSize( aBmpEx.GetPrefSize() );
557cdf0e10cSrcweir         aBmpEx.Scale( Size( nPixelWidth, nPixelHeight ) );
558cdf0e10cSrcweir         aBmpEx.SetPrefMapMode( aPrefMapMode );
559cdf0e10cSrcweir         aBmpEx.SetPrefSize( aPrefSize );
560cdf0e10cSrcweir         rGraphic = aBmpEx;
561cdf0e10cSrcweir     }
562cdf0e10cSrcweir }
563cdf0e10cSrcweir 
564cdf0e10cSrcweir void ImplApplyBitmapResolution( ::Graphic& rGraphic, sal_Int32 nImageResolution, const Size& rVisiblePixelSize, const awt::Size& rLogicalSize )
565cdf0e10cSrcweir {
566cdf0e10cSrcweir     if ( nImageResolution && rLogicalSize.Width && rLogicalSize.Height )
567cdf0e10cSrcweir     {
568cdf0e10cSrcweir         const double fImageResolution = static_cast<double>( nImageResolution );
569cdf0e10cSrcweir         const double fSourceDPIX = ( static_cast<double>(rVisiblePixelSize.Width()) * 2540.0 ) / static_cast<double>(rLogicalSize.Width);
570cdf0e10cSrcweir         const double fSourceDPIY = ( static_cast<double>(rVisiblePixelSize.Height()) * 2540.0 ) / static_cast<double>(rLogicalSize.Height);
571cdf0e10cSrcweir         const sal_Int32 nSourcePixelWidth( rGraphic.GetSizePixel().Width() );
572cdf0e10cSrcweir         const sal_Int32 nSourcePixelHeight( rGraphic.GetSizePixel().Height() );
573cdf0e10cSrcweir         const double fSourcePixelWidth = static_cast<double>( nSourcePixelWidth );
574cdf0e10cSrcweir         const double fSourcePixelHeight= static_cast<double>( nSourcePixelHeight );
575cdf0e10cSrcweir 
576cdf0e10cSrcweir         sal_Int32 nDestPixelWidth = nSourcePixelWidth;
577cdf0e10cSrcweir         sal_Int32 nDestPixelHeight = nSourcePixelHeight;
578cdf0e10cSrcweir 
579cdf0e10cSrcweir         // check, if the bitmap DPI exceeds the maximum DPI
580cdf0e10cSrcweir         if( fSourceDPIX > fImageResolution )
581cdf0e10cSrcweir         {
582cdf0e10cSrcweir             nDestPixelWidth = static_cast<sal_Int32>(( fSourcePixelWidth * fImageResolution ) / fSourceDPIX);
583cdf0e10cSrcweir             if ( !nDestPixelWidth || ( nDestPixelWidth > nSourcePixelWidth ) )
584cdf0e10cSrcweir                 nDestPixelWidth = nSourcePixelWidth;
585cdf0e10cSrcweir         }
586cdf0e10cSrcweir         if ( fSourceDPIY > fImageResolution )
587cdf0e10cSrcweir         {
588cdf0e10cSrcweir             nDestPixelHeight= static_cast<sal_Int32>(( fSourcePixelHeight* fImageResolution ) / fSourceDPIY);
589cdf0e10cSrcweir             if ( !nDestPixelHeight || ( nDestPixelHeight > nSourcePixelHeight ) )
590cdf0e10cSrcweir                 nDestPixelHeight = nSourcePixelHeight;
591cdf0e10cSrcweir         }
592cdf0e10cSrcweir         if ( ( nDestPixelWidth != nSourcePixelWidth ) || ( nDestPixelHeight != nSourcePixelHeight ) )
593cdf0e10cSrcweir             ImplApplyBitmapScaling( rGraphic, nDestPixelWidth, nDestPixelHeight );
594cdf0e10cSrcweir     }
595cdf0e10cSrcweir }
596cdf0e10cSrcweir 
597cdf0e10cSrcweir void ImplApplyFilterData( ::Graphic& rGraphic, uno::Sequence< beans::PropertyValue >& rFilterData )
598cdf0e10cSrcweir {
599cdf0e10cSrcweir     /* this method applies following attributes to the graphic, in the first step the
600cdf0e10cSrcweir        cropping area (logical size in 100thmm) is applied, in the second step the resolution
601cdf0e10cSrcweir        is applied, in the third step the graphic is scaled to the corresponding pixelsize.
602cdf0e10cSrcweir        if a parameter value is zero or not available the corresponding step will be skipped */
603cdf0e10cSrcweir 
604cdf0e10cSrcweir     sal_Int32 nPixelWidth = 0;
605cdf0e10cSrcweir     sal_Int32 nPixelHeight= 0;
606cdf0e10cSrcweir     sal_Int32 nImageResolution = 0;
607cdf0e10cSrcweir     awt::Size aLogicalSize( 0, 0 );
608cdf0e10cSrcweir     text::GraphicCrop aCropLogic( 0, 0, 0, 0 );
609cdf0e10cSrcweir     sal_Bool bRemoveCropArea = sal_True;
610cdf0e10cSrcweir 
611cdf0e10cSrcweir     for( sal_Int32 i = 0; i < rFilterData.getLength(); ++i )
612cdf0e10cSrcweir     {
613cdf0e10cSrcweir         const ::rtl::OUString   aName(  rFilterData[ i ].Name );
614cdf0e10cSrcweir         const uno::Any          aValue( rFilterData[ i ].Value );
615cdf0e10cSrcweir 
616cdf0e10cSrcweir         if( COMPARE_EQUAL == aName.compareToAscii( "PixelWidth" ) )
617cdf0e10cSrcweir             aValue >>= nPixelWidth;
618cdf0e10cSrcweir         else if( COMPARE_EQUAL == aName.compareToAscii( "PixelHeight" ) )
619cdf0e10cSrcweir             aValue >>= nPixelHeight;
620cdf0e10cSrcweir         else if( COMPARE_EQUAL == aName.compareToAscii( "LogicalSize" ) )
621cdf0e10cSrcweir             aValue >>= aLogicalSize;
622cdf0e10cSrcweir         else if (COMPARE_EQUAL == aName.compareToAscii( "GraphicCropLogic" ) )
623cdf0e10cSrcweir             aValue >>= aCropLogic;
624cdf0e10cSrcweir         else if (COMPARE_EQUAL == aName.compareToAscii( "RemoveCropArea" ) )
625cdf0e10cSrcweir             aValue >>= bRemoveCropArea;
626cdf0e10cSrcweir         else if (COMPARE_EQUAL == aName.compareToAscii( "ImageResolution" ) )
627cdf0e10cSrcweir             aValue >>= nImageResolution;
628cdf0e10cSrcweir     }
629cdf0e10cSrcweir     if ( rGraphic.GetType() == GRAPHIC_BITMAP )
630cdf0e10cSrcweir     {
631*ddde725dSArmin Le Grand         if(rGraphic.getSvgData().get())
632*ddde725dSArmin Le Grand         {
633*ddde725dSArmin Le Grand             // embedded Svg, no need to scale. Also no method to apply crop data currently
634*ddde725dSArmin Le Grand         }
635*ddde725dSArmin Le Grand         else
636*ddde725dSArmin Le Grand         {
637cdf0e10cSrcweir             Rectangle aCropPixel( Point( 0, 0 ), rGraphic.GetSizePixel() );
638cdf0e10cSrcweir             ImplCalculateCropRect( rGraphic, aCropLogic, aCropPixel );
639cdf0e10cSrcweir             if ( bRemoveCropArea )
640cdf0e10cSrcweir             {
641cdf0e10cSrcweir                 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
642cdf0e10cSrcweir                 aBmpEx.Crop( aCropPixel );
643cdf0e10cSrcweir                 rGraphic = aBmpEx;
644cdf0e10cSrcweir             }
645cdf0e10cSrcweir             Size aVisiblePixelSize( bRemoveCropArea ? rGraphic.GetSizePixel() : aCropPixel.GetSize() );
646cdf0e10cSrcweir             ImplApplyBitmapResolution( rGraphic, nImageResolution, aVisiblePixelSize, aLogicalSize );
647cdf0e10cSrcweir             ImplApplyBitmapScaling( rGraphic, nPixelWidth, nPixelHeight );
648cdf0e10cSrcweir         }
649*ddde725dSArmin Le Grand     }
650cdf0e10cSrcweir     else if ( ( rGraphic.GetType() == GRAPHIC_GDIMETAFILE ) && nImageResolution )
651cdf0e10cSrcweir     {
652cdf0e10cSrcweir         VirtualDevice aDummyVDev;
653cdf0e10cSrcweir         GDIMetaFile aMtf( rGraphic.GetGDIMetaFile() );
654cdf0e10cSrcweir         Size aMtfSize( aDummyVDev.LogicToLogic( aMtf.GetPrefSize(), aMtf.GetPrefMapMode(), MAP_100TH_MM ) );
655cdf0e10cSrcweir         if ( aMtfSize.Width() && aMtfSize.Height() )
656cdf0e10cSrcweir         {
657cdf0e10cSrcweir             MapMode aNewMapMode( MAP_100TH_MM );
658cdf0e10cSrcweir             aNewMapMode.SetScaleX( static_cast< double >( aLogicalSize.Width ) / static_cast< double >( aMtfSize.Width() ) );
659cdf0e10cSrcweir             aNewMapMode.SetScaleY( static_cast< double >( aLogicalSize.Height ) / static_cast< double >( aMtfSize.Height() ) );
660cdf0e10cSrcweir             aDummyVDev.EnableOutput( sal_False );
661cdf0e10cSrcweir             aDummyVDev.SetMapMode( aNewMapMode );
662cdf0e10cSrcweir 
663cdf0e10cSrcweir             for( sal_uInt32 i = 0, nObjCount = aMtf.GetActionCount(); i < nObjCount; i++ )
664cdf0e10cSrcweir             {
665cdf0e10cSrcweir                 MetaAction* pAction = aMtf.GetAction( i );
666cdf0e10cSrcweir                 switch( pAction->GetType() )
667cdf0e10cSrcweir                 {
668cdf0e10cSrcweir                     // only optimizing common bitmap actions:
669cdf0e10cSrcweir                     case( META_MAPMODE_ACTION ):
670cdf0e10cSrcweir                     {
671cdf0e10cSrcweir                         const_cast< MetaAction* >( pAction )->Execute( &aDummyVDev );
672cdf0e10cSrcweir                         break;
673cdf0e10cSrcweir                     }
674cdf0e10cSrcweir                     case( META_PUSH_ACTION ):
675cdf0e10cSrcweir                     {
676cdf0e10cSrcweir                         const MetaPushAction* pA = (const MetaPushAction*)pAction;
677cdf0e10cSrcweir                         aDummyVDev.Push( pA->GetFlags() );
678cdf0e10cSrcweir                         break;
679cdf0e10cSrcweir                     }
680cdf0e10cSrcweir                     case( META_POP_ACTION ):
681cdf0e10cSrcweir                     {
682cdf0e10cSrcweir                         aDummyVDev.Pop();
683cdf0e10cSrcweir                         break;
684cdf0e10cSrcweir                     }
685cdf0e10cSrcweir                     case( META_BMPSCALE_ACTION ):
686cdf0e10cSrcweir                     case( META_BMPEXSCALE_ACTION ):
687cdf0e10cSrcweir                     {
688cdf0e10cSrcweir                         BitmapEx aBmpEx;
689cdf0e10cSrcweir                         Point aPos;
690cdf0e10cSrcweir                         Size aSize;
691cdf0e10cSrcweir                         if ( pAction->GetType() == META_BMPSCALE_ACTION )
692cdf0e10cSrcweir                         {
693cdf0e10cSrcweir                             MetaBmpScaleAction* pScaleAction = dynamic_cast< MetaBmpScaleAction* >( pAction );
694cdf0e10cSrcweir                             aBmpEx = pScaleAction->GetBitmap();
695cdf0e10cSrcweir                             aPos = pScaleAction->GetPoint();
696cdf0e10cSrcweir                             aSize = pScaleAction->GetSize();
697cdf0e10cSrcweir                         }
698cdf0e10cSrcweir                         else
699cdf0e10cSrcweir                         {
700cdf0e10cSrcweir                             MetaBmpExScaleAction* pScaleAction = dynamic_cast< MetaBmpExScaleAction* >( pAction );
701cdf0e10cSrcweir                             aBmpEx = pScaleAction->GetBitmapEx();
702cdf0e10cSrcweir                             aPos = pScaleAction->GetPoint();
703cdf0e10cSrcweir                             aSize = pScaleAction->GetSize();
704cdf0e10cSrcweir                         }
705cdf0e10cSrcweir                         ::Graphic aGraphic( aBmpEx );
706cdf0e10cSrcweir                         const Size aSize100thmm( aDummyVDev.LogicToPixel( aSize ) );
707cdf0e10cSrcweir                         Size aSize100thmm2( aDummyVDev.PixelToLogic( aSize100thmm, MAP_100TH_MM ) );
708cdf0e10cSrcweir 
709cdf0e10cSrcweir                         ImplApplyBitmapResolution( aGraphic, nImageResolution,
710cdf0e10cSrcweir                             aGraphic.GetSizePixel(), awt::Size( aSize100thmm2.Width(), aSize100thmm2.Height() ) );
711cdf0e10cSrcweir 
712cdf0e10cSrcweir                         MetaAction* pNewAction;
713cdf0e10cSrcweir                         if ( pAction->GetType() == META_BMPSCALE_ACTION )
714cdf0e10cSrcweir                             pNewAction = new MetaBmpScaleAction ( aPos, aSize, aGraphic.GetBitmap() );
715cdf0e10cSrcweir                         else
716cdf0e10cSrcweir                             pNewAction = new MetaBmpExScaleAction( aPos, aSize, aGraphic.GetBitmapEx() );
717cdf0e10cSrcweir 
718cdf0e10cSrcweir                         aMtf.ReplaceAction( pNewAction, i );
719cdf0e10cSrcweir                         pAction->Delete();
720cdf0e10cSrcweir                         break;
721cdf0e10cSrcweir                     }
722cdf0e10cSrcweir                     default:
723cdf0e10cSrcweir                     case( META_BMP_ACTION ):
724cdf0e10cSrcweir                     case( META_BMPSCALEPART_ACTION ):
725cdf0e10cSrcweir                     case( META_BMPEX_ACTION ):
726cdf0e10cSrcweir                     case( META_BMPEXSCALEPART_ACTION ):
727cdf0e10cSrcweir                     case( META_MASK_ACTION ):
728cdf0e10cSrcweir                     case( META_MASKSCALE_ACTION ):
729cdf0e10cSrcweir                     break;
730cdf0e10cSrcweir                 }
731cdf0e10cSrcweir             }
732cdf0e10cSrcweir             rGraphic = aMtf;
733cdf0e10cSrcweir         }
734cdf0e10cSrcweir     }
735cdf0e10cSrcweir }
736cdf0e10cSrcweir 
737cdf0e10cSrcweir // ------------------------------------------------------------------------------
738cdf0e10cSrcweir 
739cdf0e10cSrcweir void SAL_CALL GraphicProvider::storeGraphic( const uno::Reference< ::graphic::XGraphic >& rxGraphic, const uno::Sequence< beans::PropertyValue >& rMediaProperties )
740cdf0e10cSrcweir     throw ( io::IOException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
741cdf0e10cSrcweir {
742cdf0e10cSrcweir     SvStream*   pOStm = NULL;
743cdf0e10cSrcweir     String      aPath;
744cdf0e10cSrcweir     sal_Int32   i;
745cdf0e10cSrcweir 
746cdf0e10cSrcweir     for( i = 0; ( i < rMediaProperties.getLength() ) && !pOStm; ++i )
747cdf0e10cSrcweir     {
748cdf0e10cSrcweir         const ::rtl::OUString   aName( rMediaProperties[ i ].Name );
749cdf0e10cSrcweir         const uno::Any          aValue( rMediaProperties[ i ].Value );
750cdf0e10cSrcweir 
751cdf0e10cSrcweir         if( COMPARE_EQUAL == aName.compareToAscii( "URL" ) )
752cdf0e10cSrcweir         {
753cdf0e10cSrcweir             ::rtl::OUString aURL;
754cdf0e10cSrcweir 
755cdf0e10cSrcweir             aValue >>= aURL;
756cdf0e10cSrcweir             pOStm = ::utl::UcbStreamHelper::CreateStream( aURL, STREAM_WRITE | STREAM_TRUNC );
757cdf0e10cSrcweir             aPath = aURL;
758cdf0e10cSrcweir         }
759cdf0e10cSrcweir         else if( COMPARE_EQUAL == aName.compareToAscii( "OutputStream" ) )
760cdf0e10cSrcweir         {
761cdf0e10cSrcweir             uno::Reference< io::XStream > xOStm;
762cdf0e10cSrcweir 
763cdf0e10cSrcweir             aValue >>= xOStm;
764cdf0e10cSrcweir 
765cdf0e10cSrcweir             if( xOStm.is() )
766cdf0e10cSrcweir                 pOStm = ::utl::UcbStreamHelper::CreateStream( xOStm );
767cdf0e10cSrcweir         }
768cdf0e10cSrcweir     }
769cdf0e10cSrcweir 
770cdf0e10cSrcweir     if( pOStm )
771cdf0e10cSrcweir     {
772cdf0e10cSrcweir         uno::Sequence< beans::PropertyValue >   aFilterDataSeq;
773cdf0e10cSrcweir         const char*                             pFilterShortName = NULL;
774cdf0e10cSrcweir 
775cdf0e10cSrcweir         for( i = 0; i < rMediaProperties.getLength(); ++i )
776cdf0e10cSrcweir         {
777cdf0e10cSrcweir             const ::rtl::OUString   aName( rMediaProperties[ i ].Name );
778cdf0e10cSrcweir             const uno::Any          aValue( rMediaProperties[ i ].Value );
779cdf0e10cSrcweir 
780cdf0e10cSrcweir             if( COMPARE_EQUAL == aName.compareToAscii( "FilterData" ) )
781cdf0e10cSrcweir             {
782cdf0e10cSrcweir                 aValue >>= aFilterDataSeq;
783cdf0e10cSrcweir             }
784cdf0e10cSrcweir             else if( COMPARE_EQUAL == aName.compareToAscii( "MimeType" ) )
785cdf0e10cSrcweir             {
786cdf0e10cSrcweir                 ::rtl::OUString aMimeType;
787cdf0e10cSrcweir 
788cdf0e10cSrcweir                 aValue >>= aMimeType;
789cdf0e10cSrcweir 
790cdf0e10cSrcweir                 if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_BMP ) )
791cdf0e10cSrcweir                     pFilterShortName = "bmp";
792cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_EPS ) )
793cdf0e10cSrcweir                     pFilterShortName = "eps";
794cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_GIF ) )
795cdf0e10cSrcweir                     pFilterShortName = "gif";
796cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_JPG ) )
797cdf0e10cSrcweir                     pFilterShortName = "jpg";
798cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_MET ) )
799cdf0e10cSrcweir                     pFilterShortName = "met";
800cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_PNG ) )
801cdf0e10cSrcweir                     pFilterShortName = "png";
802cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_PCT ) )
803cdf0e10cSrcweir                     pFilterShortName = "pct";
804cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_PBM ) )
805cdf0e10cSrcweir                     pFilterShortName = "pbm";
806cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_PGM ) )
807cdf0e10cSrcweir                     pFilterShortName = "pgm";
808cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_PPM ) )
809cdf0e10cSrcweir                     pFilterShortName = "ppm";
810cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_RAS ) )
811cdf0e10cSrcweir                     pFilterShortName = "ras";
812cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_SVM ) )
813cdf0e10cSrcweir                     pFilterShortName = "svm";
814cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_TIF ) )
815cdf0e10cSrcweir                     pFilterShortName = "tif";
816cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_EMF ) )
817cdf0e10cSrcweir                     pFilterShortName = "emf";
818cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_WMF ) )
819cdf0e10cSrcweir                     pFilterShortName = "wmf";
820cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_XPM ) )
821cdf0e10cSrcweir                     pFilterShortName = "xpm";
822cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_SVG ) )
823cdf0e10cSrcweir                     pFilterShortName = "svg";
824cdf0e10cSrcweir                 else if( COMPARE_EQUAL == aMimeType.compareToAscii( MIMETYPE_VCLGRAPHIC ) )
825cdf0e10cSrcweir                     pFilterShortName = MIMETYPE_VCLGRAPHIC;
826cdf0e10cSrcweir             }
827cdf0e10cSrcweir         }
828cdf0e10cSrcweir 
829cdf0e10cSrcweir         if( pFilterShortName )
830cdf0e10cSrcweir         {
831cdf0e10cSrcweir             ::GraphicFilter* pFilter = ::GraphicFilter::GetGraphicFilter();
832cdf0e10cSrcweir 
833cdf0e10cSrcweir             if( pFilter )
834cdf0e10cSrcweir             {
835cdf0e10cSrcweir                 const uno::Reference< XInterface >  xIFace( rxGraphic, uno::UNO_QUERY );
836cdf0e10cSrcweir                 const ::Graphic*                    pGraphic = ::unographic::Graphic::getImplementation( xIFace );
837cdf0e10cSrcweir 
838cdf0e10cSrcweir                 if( pGraphic && ( pGraphic->GetType() != GRAPHIC_NONE ) )
839cdf0e10cSrcweir                 {
840cdf0e10cSrcweir                     ::Graphic aGraphic( *pGraphic );
841cdf0e10cSrcweir                     ImplApplyFilterData( aGraphic, aFilterDataSeq );
842cdf0e10cSrcweir 
843cdf0e10cSrcweir                     /* sj: using a temporary memory stream, because some graphic filters are seeking behind
844cdf0e10cSrcweir                        stream end (which leads to an invalid argument exception then). */
845cdf0e10cSrcweir                     SvMemoryStream aMemStrm;
846cdf0e10cSrcweir                     aMemStrm.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
847cdf0e10cSrcweir                     if( 0 == strcmp( pFilterShortName, MIMETYPE_VCLGRAPHIC ) )
848cdf0e10cSrcweir                         aMemStrm << aGraphic;
849cdf0e10cSrcweir                     else
850cdf0e10cSrcweir                     {
851cdf0e10cSrcweir                         pFilter->ExportGraphic( aGraphic, aPath, aMemStrm,
852cdf0e10cSrcweir                                                 pFilter->GetExportFormatNumberForShortName( ::rtl::OUString::createFromAscii( pFilterShortName ) ),
853cdf0e10cSrcweir                                                     ( aFilterDataSeq.getLength() ? &aFilterDataSeq : NULL ) );
854cdf0e10cSrcweir                     }
855cdf0e10cSrcweir                     aMemStrm.Seek( STREAM_SEEK_TO_END );
856cdf0e10cSrcweir                     pOStm->Write( aMemStrm.GetData(), aMemStrm.Tell() );
857cdf0e10cSrcweir                 }
858cdf0e10cSrcweir             }
859cdf0e10cSrcweir         }
860cdf0e10cSrcweir         delete pOStm;
861cdf0e10cSrcweir     }
862cdf0e10cSrcweir }
863cdf0e10cSrcweir 
864cdf0e10cSrcweir }
865