Bump version to 24.04.3.4
[LibreOffice.git] / sfx2 / source / doc / graphhelp.cxx
blob9d7600ae2da7d7e3f74e7f7bad5b71ebaf2931a5
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 <comphelper/propertyvalue.hxx>
42 #include <o3tl/char16_t2wchar_t.hxx>
43 #include <o3tl/string_view.hxx>
45 #include "graphhelp.hxx"
46 #include <bitmaps.hlst>
48 #include <memory>
50 #if defined _WIN32
51 #include <unotools/tempfile.hxx>
52 #include <vcl/outdev.hxx>
53 #endif
55 using namespace css;
57 std::unique_ptr<SvMemoryStream> GraphicHelper::getFormatStrFromGDI_Impl( const GDIMetaFile* pGDIMeta, ConvertDataFormat nFormat )
59 std::unique_ptr<SvMemoryStream> pResult;
60 if ( pGDIMeta )
62 std::unique_ptr<SvMemoryStream> pStream(new SvMemoryStream( 65535, 65535 ));
63 Graphic aGraph( *pGDIMeta );
64 if ( GraphicConverter::Export( *pStream, aGraph, nFormat ) == ERRCODE_NONE )
65 pResult = std::move(pStream);
68 return pResult;
72 // static
73 void* GraphicHelper::getEnhMetaFileFromGDI_Impl( const GDIMetaFile* pGDIMeta )
75 void* pResult = nullptr;
77 #ifdef _WIN32
78 if ( pGDIMeta )
80 ::utl::TempFileNamed aTempFile( u"", true, u".emf" );
82 OUString aMetaFile = aTempFile.GetFileName();
83 OUString aMetaURL = aTempFile.GetURL();
85 std::unique_ptr<SvStream> pStream = ::utl::UcbStreamHelper::CreateStream( aMetaURL, StreamMode::STD_READWRITE );
86 if ( pStream )
88 Graphic aGraph( *pGDIMeta );
89 ErrCode nFailed = GraphicConverter::Export( *pStream, aGraph, ConvertDataFormat::EMF );
90 pStream->Flush();
91 pStream.reset();
93 if ( !nFailed )
94 pResult = GetEnhMetaFileW( o3tl::toW(aMetaFile.getStr()) );
97 #else
98 (void)pGDIMeta; // unused
99 #endif
101 return pResult;
105 // static
106 void* GraphicHelper::getWinMetaFileFromGDI_Impl( const GDIMetaFile* pGDIMeta, const Size& aMetaSize )
108 void* pResult = nullptr;
110 #ifdef _WIN32
111 if ( pGDIMeta )
113 SvMemoryStream pStream( 65535, 65535 );
114 Graphic aGraph( *pGDIMeta );
115 ErrCode nFailed = GraphicConverter::Export( pStream, aGraph, ConvertDataFormat::WMF );
116 pStream.Flush();
117 if ( !nFailed )
119 sal_uInt64 nLength = pStream.TellEnd();
120 if ( nLength > 22 )
122 HMETAFILE hMeta = SetMetaFileBitsEx( nLength - 22,
123 static_cast< const unsigned char*>( pStream.GetData() ) + 22 );
125 if ( hMeta )
127 HGLOBAL hMemory = GlobalAlloc( GMEM_DDESHARE | GMEM_MOVEABLE, sizeof( METAFILEPICT ) );
129 if ( hMemory )
131 if (METAFILEPICT* pMF = static_cast<METAFILEPICT*>(GlobalLock(hMemory)))
133 pMF->hMF = hMeta;
134 pMF->mm = MM_ANISOTROPIC;
136 MapMode aWinMode( MapUnit::Map100thMM );
138 if ( aWinMode == pGDIMeta->GetPrefMapMode() )
140 pMF->xExt = aMetaSize.Width();
141 pMF->yExt = aMetaSize.Height();
143 else
145 Size aWinSize = OutputDevice::LogicToLogic( Size( aMetaSize.Width(), aMetaSize.Height() ),
146 pGDIMeta->GetPrefMapMode(),
147 aWinMode );
148 pMF->xExt = aWinSize.Width();
149 pMF->yExt = aWinSize.Height();
152 GlobalUnlock(hMemory);
154 pResult = static_cast<void*>(hMemory);
156 else
157 DeleteMetaFile( hMeta );
162 #else
163 (void)pGDIMeta; // unused
164 (void)aMetaSize; // unused
165 #endif
168 return pResult;
172 // static
173 bool GraphicHelper::getThumbnailFormatFromBitmap_Impl(const BitmapEx& rBitmap, const uno::Reference<io::XStream>& xStream)
175 if (rBitmap.IsEmpty() || !xStream.is())
176 return false;
178 std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xStream));
180 if (pStream->GetError())
181 return false;
183 BitmapEx bitmap(rBitmap);
184 bitmap.Convert(BmpConversion::N8BitColors);
186 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
188 if (rFilter.compressAsPNG(bitmap, *pStream) != ERRCODE_NONE)
189 return false;
191 pStream->FlushBuffer();
193 return !pStream->GetError();
196 // static
197 bool GraphicHelper::getThumbnailReplacement_Impl(std::u16string_view rResID, const uno::Reference< io::XStream >& xStream )
199 bool bResult = false;
200 if (!rResID.empty() && xStream.is())
202 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
205 uno::Reference< graphic::XGraphicProvider > xGraphProvider(graphic::GraphicProvider::create(xContext));
206 const OUString aURL{OUString::Concat("private:graphicrepository/") + rResID};
208 uno::Sequence< beans::PropertyValue > aMediaProps{ comphelper::makePropertyValue("URL",
209 aURL) };
211 uno::Reference< graphic::XGraphic > xGraphic = xGraphProvider->queryGraphic( aMediaProps );
212 if ( xGraphic.is() )
214 uno::Sequence< beans::PropertyValue > aStoreProps{
215 comphelper::makePropertyValue("OutputStream", xStream),
216 comphelper::makePropertyValue("MimeType", OUString("image/png"))
219 xGraphProvider->storeGraphic( xGraphic, aStoreProps );
220 bResult = true;
223 catch(const uno::Exception&)
228 return bResult;
231 // static
232 OUString GraphicHelper::getThumbnailReplacementIDByFactoryName_Impl( std::u16string_view aFactoryShortName )
234 OUString sResult;
236 if ( aFactoryShortName == u"scalc" )
238 sResult = BMP_128X128_CALC_DOC;
240 else if ( aFactoryShortName == u"sdraw" )
242 sResult = BMP_128X128_DRAW_DOC;
244 else if ( aFactoryShortName == u"simpress" )
246 sResult = BMP_128X128_IMPRESS_DOC;
248 else if ( aFactoryShortName == u"smath" )
250 sResult = BMP_128X128_MATH_DOC;
252 else if ( aFactoryShortName == u"swriter" || o3tl::starts_with(aFactoryShortName, u"swriter/") )
254 sResult = BMP_128X128_WRITER_DOC;
257 return sResult;
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */