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/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>
49 #include <unotools/tempfile.hxx>
50 #include <vcl/outdev.hxx>
55 std::unique_ptr
<SvMemoryStream
> GraphicHelper::getFormatStrFromGDI_Impl( const GDIMetaFile
* pGDIMeta
, ConvertDataFormat nFormat
)
57 std::unique_ptr
<SvMemoryStream
> pResult
;
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
);
71 void* GraphicHelper::getEnhMetaFileFromGDI_Impl( const GDIMetaFile
* pGDIMeta
)
73 void* pResult
= nullptr;
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
);
87 Graphic
aGraph( *pGDIMeta
);
88 ErrCode nFailed
= GraphicConverter::Export( *pStream
, aGraph
, ConvertDataFormat::EMF
);
93 pResult
= GetEnhMetaFileW( o3tl::toW(aMetaFile
.getStr()) );
97 (void)pGDIMeta
; // unused
105 void* GraphicHelper::getWinMetaFileFromGDI_Impl( const GDIMetaFile
* pGDIMeta
, const Size
& aMetaSize
)
107 void* pResult
= nullptr;
112 SvMemoryStream
pStream( 65535, 65535 );
113 Graphic
aGraph( *pGDIMeta
);
114 ErrCode nFailed
= GraphicConverter::Export( pStream
, aGraph
, ConvertDataFormat::WMF
);
118 sal_Int32 nLength
= pStream
.TellEnd();
121 HMETAFILE hMeta
= SetMetaFileBitsEx( nLength
- 22,
122 static_cast< const unsigned char*>( pStream
.GetData() ) + 22 );
126 HGLOBAL hMemory
= GlobalAlloc( GMEM_DDESHARE
| GMEM_MOVEABLE
, sizeof( METAFILEPICT
) );
130 METAFILEPICT
* pMF
= static_cast<METAFILEPICT
*>(GlobalLock( hMemory
));
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();
144 Size aWinSize
= OutputDevice::LogicToLogic( Size( aMetaSize
.Width(), aMetaSize
.Height() ),
145 pGDIMeta
->GetPrefMapMode(),
147 pMF
->xExt
= aWinSize
.Width();
148 pMF
->yExt
= aWinSize
.Height();
151 GlobalUnlock( hMemory
);
152 pResult
= static_cast<void*>(hMemory
);
155 DeleteMetaFile( hMeta
);
161 (void)pGDIMeta
; // unused
162 (void)aMetaSize
; // unused
171 bool GraphicHelper::getThumbnailFormatFromGDI_Impl(GDIMetaFile
const * pMetaFile
, const uno::Reference
<io::XStream
>& xStream
)
173 bool bResult
= false;
175 if (!pMetaFile
|| !xStream
.is())
178 std::unique_ptr
<SvStream
> pStream(utl::UcbStreamHelper::CreateStream(xStream
));
180 if (pStream
->GetError())
183 BitmapEx aResultBitmap
;
185 bResult
= pMetaFile
->CreateThumbnail(aResultBitmap
, BmpConversion::N8BitColors
, BmpScaleFlag::Default
);
187 if (!bResult
|| aResultBitmap
.IsEmpty())
190 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
192 if (rFilter
.compressAsPNG(aResultBitmap
, *pStream
) != ERRCODE_NONE
)
197 return !pStream
->GetError();
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
);
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
);
229 catch(const uno::Exception
&)
238 OUString
GraphicHelper::getThumbnailReplacementIDByFactoryName_Impl( const OUString
& aFactoryShortName
)
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
;
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */