Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / doc / graphhelp.cxx
blob6e7789122a3bd546c9c2a36545f4f03d80df1ef7
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/graphic/GraphicProvider.hpp>
28 #include <com/sun/star/graphic/XGraphicProvider.hpp>
29 #include <com/sun/star/graphic/XGraphic.hpp>
32 #include <vcl/gdimtf.hxx>
33 #include <vcl/graph.hxx>
34 #include <vcl/cvtgrf.hxx>
35 #include <vcl/bitmapex.hxx>
36 #include <vcl/graphicfilter.hxx>
38 #include <tools/stream.hxx>
39 #include <unotools/ucbstreamhelper.hxx>
40 #include <comphelper/processfactory.hxx>
41 #include <o3tl/char16_t2wchar_t.hxx>
43 #include "graphhelp.hxx"
44 #include <bitmaps.hlst>
46 #include <memory>
48 #if defined _WIN32
49 #include <unotools/tempfile.hxx>
50 #include <vcl/outdev.hxx>
51 #endif
53 using namespace css;
55 std::unique_ptr<SvMemoryStream> GraphicHelper::getFormatStrFromGDI_Impl( const GDIMetaFile* pGDIMeta, ConvertDataFormat nFormat )
57 std::unique_ptr<SvMemoryStream> pResult;
58 if ( pGDIMeta )
60 std::unique_ptr<SvMemoryStream> pStream(new SvMemoryStream( 65535, 65535 ));
61 Graphic aGraph( *pGDIMeta );
62 if ( GraphicConverter::Export( *pStream, aGraph, nFormat ) == ERRCODE_NONE )
63 pResult = std::move(pStream);
66 return pResult;
70 // static
71 void* GraphicHelper::getEnhMetaFileFromGDI_Impl( const GDIMetaFile* pGDIMeta )
73 void* pResult = nullptr;
75 #ifdef _WIN32
76 if ( pGDIMeta )
78 OUString const aStr(".emf");
79 ::utl::TempFile aTempFile( OUString(), true, &aStr );
81 OUString aMetaFile = aTempFile.GetFileName();
82 OUString aMetaURL = aTempFile.GetURL();
84 std::unique_ptr<SvStream> pStream = ::utl::UcbStreamHelper::CreateStream( aMetaURL, StreamMode::STD_READWRITE );
85 if ( pStream )
87 Graphic aGraph( *pGDIMeta );
88 ErrCode nFailed = GraphicConverter::Export( *pStream, aGraph, ConvertDataFormat::EMF );
89 pStream->Flush();
90 pStream.reset();
92 if ( !nFailed )
93 pResult = GetEnhMetaFileW( o3tl::toW(aMetaFile.getStr()) );
96 #else
97 (void)pGDIMeta; // unused
98 #endif
100 return pResult;
104 // static
105 void* GraphicHelper::getWinMetaFileFromGDI_Impl( const GDIMetaFile* pGDIMeta, const Size& aMetaSize )
107 void* pResult = nullptr;
109 #ifdef _WIN32
110 if ( pGDIMeta )
112 SvMemoryStream pStream( 65535, 65535 );
113 Graphic aGraph( *pGDIMeta );
114 ErrCode nFailed = GraphicConverter::Export( pStream, aGraph, ConvertDataFormat::WMF );
115 pStream.Flush();
116 if ( !nFailed )
118 sal_Int32 nLength = pStream.TellEnd();
119 if ( nLength > 22 )
121 HMETAFILE hMeta = SetMetaFileBitsEx( nLength - 22,
122 static_cast< const unsigned char*>( pStream.GetData() ) + 22 );
124 if ( hMeta )
126 HGLOBAL hMemory = GlobalAlloc( GMEM_DDESHARE | GMEM_MOVEABLE, sizeof( METAFILEPICT ) );
128 if ( hMemory )
130 METAFILEPICT* pMF = static_cast<METAFILEPICT*>(GlobalLock( hMemory ));
132 pMF->hMF = hMeta;
133 pMF->mm = MM_ANISOTROPIC;
135 MapMode aWinMode( MapUnit::Map100thMM );
137 if ( aWinMode == pGDIMeta->GetPrefMapMode() )
139 pMF->xExt = aMetaSize.Width();
140 pMF->yExt = aMetaSize.Height();
142 else
144 Size aWinSize = OutputDevice::LogicToLogic( Size( aMetaSize.Width(), aMetaSize.Height() ),
145 pGDIMeta->GetPrefMapMode(),
146 aWinMode );
147 pMF->xExt = aWinSize.Width();
148 pMF->yExt = aWinSize.Height();
151 GlobalUnlock( hMemory );
152 pResult = static_cast<void*>(hMemory);
154 else
155 DeleteMetaFile( hMeta );
160 #else
161 (void)pGDIMeta; // unused
162 (void)aMetaSize; // unused
163 #endif
166 return pResult;
170 // static
171 bool GraphicHelper::getThumbnailFormatFromGDI_Impl(GDIMetaFile const * pMetaFile, const uno::Reference<io::XStream>& xStream)
173 bool bResult = false;
175 if (!pMetaFile || !xStream.is())
176 return false;
178 std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xStream));
180 if (pStream->GetError())
181 return false;
183 BitmapEx aResultBitmap;
185 bResult = pMetaFile->CreateThumbnail(aResultBitmap, BmpConversion::N8BitColors, BmpScaleFlag::Default);
187 if (!bResult || aResultBitmap.IsEmpty())
188 return false;
190 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
192 if (rFilter.compressAsPNG(aResultBitmap, *pStream) != ERRCODE_NONE)
193 return false;
195 pStream->Flush();
197 return !pStream->GetError();
200 // static
201 bool GraphicHelper::getThumbnailReplacement_Impl(const OUString& rResID, const uno::Reference< io::XStream >& xStream )
203 bool bResult = false;
204 if (!rResID.isEmpty() && xStream.is())
206 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
209 uno::Reference< graphic::XGraphicProvider > xGraphProvider(graphic::GraphicProvider::create(xContext));
210 const OUString aURL{"private:graphicrepository/" + rResID};
212 uno::Sequence< beans::PropertyValue > aMediaProps( 1 );
213 aMediaProps[0].Name = "URL";
214 aMediaProps[0].Value <<= aURL;
216 uno::Reference< graphic::XGraphic > xGraphic = xGraphProvider->queryGraphic( aMediaProps );
217 if ( xGraphic.is() )
219 uno::Sequence< beans::PropertyValue > aStoreProps( 2 );
220 aStoreProps[0].Name = "OutputStream";
221 aStoreProps[0].Value <<= xStream;
222 aStoreProps[1].Name = "MimeType";
223 aStoreProps[1].Value <<= OUString("image/png");
225 xGraphProvider->storeGraphic( xGraphic, aStoreProps );
226 bResult = true;
229 catch(const uno::Exception&)
234 return bResult;
237 // static
238 OUString GraphicHelper::getThumbnailReplacementIDByFactoryName_Impl( const OUString& aFactoryShortName )
240 OUString sResult;
242 if ( aFactoryShortName == "scalc" )
244 sResult = BMP_128X128_CALC_DOC;
246 else if ( aFactoryShortName == "sdraw" )
248 sResult = BMP_128X128_DRAW_DOC;
250 else if ( aFactoryShortName == "simpress" )
252 sResult = BMP_128X128_IMPRESS_DOC;
254 else if ( aFactoryShortName == "smath" )
256 sResult = BMP_128X128_MATH_DOC;
258 else if ( aFactoryShortName == "swriter" || aFactoryShortName.startsWith("swriter/") )
260 sResult = BMP_128X128_WRITER_DOC;
263 return sResult;
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */