LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / doc / graphhelp.cxx
blobb309625206a5f523829048d0246fa332525459ce
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 OUString const aStr(".emf");
81 ::utl::TempFile aTempFile( OUString(), true, &aStr );
83 OUString aMetaFile = aTempFile.GetFileName();
84 OUString aMetaURL = aTempFile.GetURL();
86 std::unique_ptr<SvStream> pStream = ::utl::UcbStreamHelper::CreateStream( aMetaURL, StreamMode::STD_READWRITE );
87 if ( pStream )
89 Graphic aGraph( *pGDIMeta );
90 ErrCode nFailed = GraphicConverter::Export( *pStream, aGraph, ConvertDataFormat::EMF );
91 pStream->Flush();
92 pStream.reset();
94 if ( !nFailed )
95 pResult = GetEnhMetaFileW( o3tl::toW(aMetaFile.getStr()) );
98 #else
99 (void)pGDIMeta; // unused
100 #endif
102 return pResult;
106 // static
107 void* GraphicHelper::getWinMetaFileFromGDI_Impl( const GDIMetaFile* pGDIMeta, const Size& aMetaSize )
109 void* pResult = nullptr;
111 #ifdef _WIN32
112 if ( pGDIMeta )
114 SvMemoryStream pStream( 65535, 65535 );
115 Graphic aGraph( *pGDIMeta );
116 ErrCode nFailed = GraphicConverter::Export( pStream, aGraph, ConvertDataFormat::WMF );
117 pStream.Flush();
118 if ( !nFailed )
120 sal_Int32 nLength = pStream.TellEnd();
121 if ( nLength > 22 )
123 HMETAFILE hMeta = SetMetaFileBitsEx( nLength - 22,
124 static_cast< const unsigned char*>( pStream.GetData() ) + 22 );
126 if ( hMeta )
128 HGLOBAL hMemory = GlobalAlloc( GMEM_DDESHARE | GMEM_MOVEABLE, sizeof( METAFILEPICT ) );
130 if ( hMemory )
132 METAFILEPICT* pMF = static_cast<METAFILEPICT*>(GlobalLock( hMemory ));
134 pMF->hMF = hMeta;
135 pMF->mm = MM_ANISOTROPIC;
137 MapMode aWinMode( MapUnit::Map100thMM );
139 if ( aWinMode == pGDIMeta->GetPrefMapMode() )
141 pMF->xExt = aMetaSize.Width();
142 pMF->yExt = aMetaSize.Height();
144 else
146 Size aWinSize = OutputDevice::LogicToLogic( Size( aMetaSize.Width(), aMetaSize.Height() ),
147 pGDIMeta->GetPrefMapMode(),
148 aWinMode );
149 pMF->xExt = aWinSize.Width();
150 pMF->yExt = aWinSize.Height();
153 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::getThumbnailFormatFromGDI_Impl(GDIMetaFile const * pMetaFile, const uno::Reference<io::XStream>& xStream)
175 bool bResult = false;
177 if (!pMetaFile || !xStream.is())
178 return false;
180 std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xStream));
182 if (pStream->GetError())
183 return false;
185 BitmapEx aResultBitmap;
187 bResult = pMetaFile->CreateThumbnail(aResultBitmap, BmpConversion::N8BitColors, BmpScaleFlag::Default);
189 if (!bResult || aResultBitmap.IsEmpty())
190 return false;
192 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
194 if (rFilter.compressAsPNG(aResultBitmap, *pStream) != ERRCODE_NONE)
195 return false;
197 pStream->Flush();
199 return !pStream->GetError();
202 // static
203 bool GraphicHelper::getThumbnailReplacement_Impl(std::u16string_view rResID, const uno::Reference< io::XStream >& xStream )
205 bool bResult = false;
206 if (!rResID.empty() && xStream.is())
208 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
211 uno::Reference< graphic::XGraphicProvider > xGraphProvider(graphic::GraphicProvider::create(xContext));
212 const OUString aURL{OUString::Concat("private:graphicrepository/") + rResID};
214 uno::Sequence< beans::PropertyValue > aMediaProps{ comphelper::makePropertyValue("URL",
215 aURL) };
217 uno::Reference< graphic::XGraphic > xGraphic = xGraphProvider->queryGraphic( aMediaProps );
218 if ( xGraphic.is() )
220 uno::Sequence< beans::PropertyValue > aStoreProps{
221 comphelper::makePropertyValue("OutputStream", xStream),
222 comphelper::makePropertyValue("MimeType", 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( std::u16string_view aFactoryShortName )
240 OUString sResult;
242 if ( aFactoryShortName == u"scalc" )
244 sResult = BMP_128X128_CALC_DOC;
246 else if ( aFactoryShortName == u"sdraw" )
248 sResult = BMP_128X128_DRAW_DOC;
250 else if ( aFactoryShortName == u"simpress" )
252 sResult = BMP_128X128_IMPRESS_DOC;
254 else if ( aFactoryShortName == u"smath" )
256 sResult = BMP_128X128_MATH_DOC;
258 else if ( aFactoryShortName == u"swriter" || o3tl::starts_with(aFactoryShortName, u"swriter/") )
260 sResult = BMP_128X128_WRITER_DOC;
263 return sResult;
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */