1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
26 #include <com/sun/star/uno/Exception.hpp>
27 #include <com/sun/star/datatransfer/XTransferable.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/graphic/GraphicProvider.hpp>
30 #include <com/sun/star/graphic/XGraphicProvider.hpp>
31 #include <com/sun/star/graphic/XGraphic.hpp>
32 #include <com/sun/star/io/XStream.hpp>
35 #include <osl/thread.h>
36 #include <vcl/gdimtf.hxx>
37 #include <vcl/graph.hxx>
38 #include <vcl/cvtgrf.hxx>
39 #include <vcl/outdev.hxx>
40 #include <vcl/virdev.hxx>
41 #include <vcl/bitmapex.hxx>
42 #include <vcl/graphicfilter.hxx>
44 #include <tools/stream.hxx>
45 #include <tools/helpers.hxx>
46 #include <unotools/tempfile.hxx>
47 #include <unotools/ucbstreamhelper.hxx>
48 #include <unotools/streamwrap.hxx>
49 #include <comphelper/processfactory.hxx>
52 #include <sfx2/sfxresid.hxx>
53 #include "graphhelp.hxx"
60 SvMemoryStream
* GraphicHelper::getFormatStrFromGDI_Impl( const GDIMetaFile
* pGDIMeta
, ConvertDataFormat nFormat
)
62 SvMemoryStream
* pResult
= nullptr;
65 SvMemoryStream
* pStream
= new SvMemoryStream( 65535, 65535 );
66 Graphic
aGraph( *pGDIMeta
);
67 if ( GraphicConverter::Export( *pStream
, aGraph
, nFormat
) == 0 )
78 void* GraphicHelper::getEnhMetaFileFromGDI_Impl( const GDIMetaFile
* pGDIMeta
)
80 (void)pGDIMeta
; // unused
81 void* pResult
= nullptr;
86 OUString
const aStr(".emf");
87 ::utl::TempFile
aTempFile( OUString(), true, &aStr
);
89 OUString aMetaFile
= aTempFile
.GetFileName();
90 OUString aMetaURL
= aTempFile
.GetURL();
91 OString aWinFile
= OUStringToOString( aMetaFile
, osl_getThreadTextEncoding() );
93 SvStream
* pStream
= ::utl::UcbStreamHelper::CreateStream( aMetaURL
, StreamMode::STD_READWRITE
);
96 Graphic
aGraph( *pGDIMeta
);
97 bool bFailed
= GraphicConverter::Export( *pStream
, aGraph
, ConvertDataFormat::EMF
);
102 pResult
= GetEnhMetaFileA( aWinFile
.getStr() );
112 void* GraphicHelper::getWinMetaFileFromGDI_Impl( const GDIMetaFile
* pGDIMeta
, const Size
& aMetaSize
)
114 (void)pGDIMeta
; // unused
115 (void)aMetaSize
; // unused
116 void* pResult
= nullptr;
121 SvMemoryStream
* pStream
= new SvMemoryStream( 65535, 65535 );
122 Graphic
aGraph( *pGDIMeta
);
123 bool bFailed
= GraphicConverter::Export( *pStream
, aGraph
, ConvertDataFormat::WMF
);
127 sal_Int32 nLength
= pStream
->Seek( STREAM_SEEK_TO_END
);
130 HMETAFILE hMeta
= SetMetaFileBitsEx( nLength
- 22,
131 ( static_cast< const unsigned char*>( pStream
->GetData() ) ) + 22 );
135 HGLOBAL hMemory
= GlobalAlloc( GMEM_DDESHARE
| GMEM_MOVEABLE
, sizeof( METAFILEPICT
) );
139 METAFILEPICT
* pMF
= static_cast<METAFILEPICT
*>(GlobalLock( hMemory
));
142 pMF
->mm
= MM_ANISOTROPIC
;
144 MapMode aMetaMode
= pGDIMeta
->GetPrefMapMode();
145 MapMode
aWinMode( MapUnit::Map100thMM
);
147 if ( aWinMode
== pGDIMeta
->GetPrefMapMode() )
149 pMF
->xExt
= aMetaSize
.Width();
150 pMF
->yExt
= aMetaSize
.Height();
154 Size aWinSize
= OutputDevice::LogicToLogic( Size( aMetaSize
.Width(), aMetaSize
.Height() ),
155 pGDIMeta
->GetPrefMapMode(),
157 pMF
->xExt
= aWinSize
.Width();
158 pMF
->yExt
= aWinSize
.Height();
161 GlobalUnlock( hMemory
);
162 pResult
= static_cast<void*>(hMemory
);
165 DeleteMetaFile( hMeta
);
180 bool GraphicHelper::getThumbnailFormatFromGDI_Impl(GDIMetaFile
* pMetaFile
, const uno::Reference
<io::XStream
>& xStream
)
182 bool bResult
= false;
184 if (!pMetaFile
|| !xStream
.is())
187 std::unique_ptr
<SvStream
> pStream(utl::UcbStreamHelper::CreateStream(xStream
));
189 if (pStream
->GetError())
192 BitmapEx aResultBitmap
;
194 bResult
= pMetaFile
->CreateThumbnail(aResultBitmap
, 256, BMP_CONVERSION_8BIT_COLORS
, BmpScaleFlag::Default
);
196 if (!bResult
|| aResultBitmap
.IsEmpty())
199 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
201 if (rFilter
.compressAsPNG(aResultBitmap
, *pStream
.get()) != GRFILTER_OK
)
206 return !pStream
->GetError();
210 bool GraphicHelper::getThumbnailReplacement_Impl( sal_Int32 nResID
, const uno::Reference
< io::XStream
>& xStream
)
212 bool bResult
= false;
213 if ( nResID
&& xStream
.is() )
215 uno::Reference
< uno::XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
218 uno::Reference
< graphic::XGraphicProvider
> xGraphProvider(graphic::GraphicProvider::create(xContext
));
219 OUString
aURL("private:resource/sfx/bitmapex/");
220 aURL
+= OUString::number( nResID
);
222 uno::Sequence
< beans::PropertyValue
> aMediaProps( 1 );
223 aMediaProps
[0].Name
= "URL";
224 aMediaProps
[0].Value
<<= aURL
;
226 uno::Reference
< graphic::XGraphic
> xGraphic
= xGraphProvider
->queryGraphic( aMediaProps
);
229 uno::Sequence
< beans::PropertyValue
> aStoreProps( 2 );
230 aStoreProps
[0].Name
= "OutputStream";
231 aStoreProps
[0].Value
<<= xStream
;
232 aStoreProps
[1].Name
= "MimeType";
233 aStoreProps
[1].Value
<<= OUString("image/png");
235 xGraphProvider
->storeGraphic( xGraphic
, aStoreProps
);
239 catch(const uno::Exception
&)
249 sal_uInt16
GraphicHelper::getThumbnailReplacementIDByFactoryName_Impl( const OUString
& aFactoryShortName
, bool /*bIsTemplate*/ )
251 sal_uInt16 nResult
= 0;
253 if ( aFactoryShortName
== "scalc" )
255 nResult
= BMP_128X128_CALC_DOC
;
257 else if ( aFactoryShortName
== "sdraw" )
259 nResult
= BMP_128X128_DRAW_DOC
;
261 else if ( aFactoryShortName
== "simpress" )
263 nResult
= BMP_128X128_IMPRESS_DOC
;
265 else if ( aFactoryShortName
== "smath" )
267 nResult
= BMP_128X128_MATH_DOC
;
269 else if ( aFactoryShortName
== "swriter" || aFactoryShortName
.startsWith("swriter/") )
271 nResult
= BMP_128X128_WRITER_DOC
;
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */