build fix
[LibreOffice.git] / sfx2 / source / doc / graphhelp.cxx
blob542c8ff4aef284c254d7d4aa637edea6ee567867
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #ifdef _WIN32
22 #include <prewin.h>
23 #include <postwin.h>
24 #endif
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"
54 #include "doc.hrc"
56 #include <memory>
58 using namespace css;
60 SvMemoryStream* GraphicHelper::getFormatStrFromGDI_Impl( const GDIMetaFile* pGDIMeta, ConvertDataFormat nFormat )
62 SvMemoryStream* pResult = nullptr;
63 if ( pGDIMeta )
65 SvMemoryStream* pStream = new SvMemoryStream( 65535, 65535 );
66 Graphic aGraph( *pGDIMeta );
67 if ( GraphicConverter::Export( *pStream, aGraph, nFormat ) == 0 )
68 pResult = pStream;
69 else
70 delete pStream;
73 return pResult;
77 // static
78 void* GraphicHelper::getEnhMetaFileFromGDI_Impl( const GDIMetaFile* pGDIMeta )
80 (void)pGDIMeta; // unused
81 void* pResult = nullptr;
83 #ifdef _WIN32
84 if ( pGDIMeta )
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 );
94 if ( pStream )
96 Graphic aGraph( *pGDIMeta );
97 bool bFailed = GraphicConverter::Export( *pStream, aGraph, ConvertDataFormat::EMF );
98 pStream->Flush();
99 delete pStream;
101 if ( !bFailed )
102 pResult = GetEnhMetaFileA( aWinFile.getStr() );
105 #endif
107 return pResult;
111 // static
112 void* GraphicHelper::getWinMetaFileFromGDI_Impl( const GDIMetaFile* pGDIMeta, const Size& aMetaSize )
114 (void)pGDIMeta; // unused
115 (void)aMetaSize; // unused
116 void* pResult = nullptr;
118 #ifdef _WIN32
119 if ( pGDIMeta )
121 SvMemoryStream* pStream = new SvMemoryStream( 65535, 65535 );
122 Graphic aGraph( *pGDIMeta );
123 bool bFailed = GraphicConverter::Export( *pStream, aGraph, ConvertDataFormat::WMF );
124 pStream->Flush();
125 if ( !bFailed )
127 sal_Int32 nLength = pStream->Seek( STREAM_SEEK_TO_END );
128 if ( nLength > 22 )
130 HMETAFILE hMeta = SetMetaFileBitsEx( nLength - 22,
131 ( static_cast< const unsigned char*>( pStream->GetData() ) ) + 22 );
133 if ( hMeta )
135 HGLOBAL hMemory = GlobalAlloc( GMEM_DDESHARE | GMEM_MOVEABLE, sizeof( METAFILEPICT ) );
137 if ( hMemory )
139 METAFILEPICT* pMF = static_cast<METAFILEPICT*>(GlobalLock( hMemory ));
141 pMF->hMF = hMeta;
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();
152 else
154 Size aWinSize = OutputDevice::LogicToLogic( Size( aMetaSize.Width(), aMetaSize.Height() ),
155 pGDIMeta->GetPrefMapMode(),
156 aWinMode );
157 pMF->xExt = aWinSize.Width();
158 pMF->yExt = aWinSize.Height();
161 GlobalUnlock( hMemory );
162 pResult = static_cast<void*>(hMemory);
164 else
165 DeleteMetaFile( hMeta );
170 delete pStream;
172 #endif
175 return pResult;
179 // static
180 bool GraphicHelper::getThumbnailFormatFromGDI_Impl(GDIMetaFile* pMetaFile, const uno::Reference<io::XStream>& xStream)
182 bool bResult = false;
184 if (!pMetaFile || !xStream.is())
185 return false;
187 std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xStream));
189 if (pStream->GetError())
190 return false;
192 BitmapEx aResultBitmap;
194 bResult = pMetaFile->CreateThumbnail(aResultBitmap, 256, BMP_CONVERSION_8BIT_COLORS, BmpScaleFlag::Default);
196 if (!bResult || aResultBitmap.IsEmpty())
197 return false;
199 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
201 if (rFilter.compressAsPNG(aResultBitmap, *pStream.get()) != GRFILTER_OK)
202 return false;
204 pStream->Flush();
206 return !pStream->GetError();
209 // static
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 );
227 if ( xGraphic.is() )
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 );
236 bResult = true;
239 catch(const uno::Exception&)
244 return bResult;
248 // static
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;
274 return nResult;
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */