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 <comphelper/propertyvalue.hxx>
42 #include <o3tl/char16_t2wchar_t.hxx>
43 #include <o3tl/string_view.hxx>
45 #include "graphhelp.hxx"
46 #include <bitmaps.hlst>
51 #include <unotools/tempfile.hxx>
52 #include <vcl/outdev.hxx>
57 std::unique_ptr
<SvMemoryStream
> GraphicHelper::getFormatStrFromGDI_Impl( const GDIMetaFile
* pGDIMeta
, ConvertDataFormat nFormat
)
59 std::unique_ptr
<SvMemoryStream
> pResult
;
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
);
73 void* GraphicHelper::getEnhMetaFileFromGDI_Impl( const GDIMetaFile
* pGDIMeta
)
75 void* pResult
= nullptr;
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
);
89 Graphic
aGraph( *pGDIMeta
);
90 ErrCode nFailed
= GraphicConverter::Export( *pStream
, aGraph
, ConvertDataFormat::EMF
);
95 pResult
= GetEnhMetaFileW( o3tl::toW(aMetaFile
.getStr()) );
99 (void)pGDIMeta
; // unused
107 void* GraphicHelper::getWinMetaFileFromGDI_Impl( const GDIMetaFile
* pGDIMeta
, const Size
& aMetaSize
)
109 void* pResult
= nullptr;
114 SvMemoryStream
pStream( 65535, 65535 );
115 Graphic
aGraph( *pGDIMeta
);
116 ErrCode nFailed
= GraphicConverter::Export( pStream
, aGraph
, ConvertDataFormat::WMF
);
120 sal_Int32 nLength
= pStream
.TellEnd();
123 HMETAFILE hMeta
= SetMetaFileBitsEx( nLength
- 22,
124 static_cast< const unsigned char*>( pStream
.GetData() ) + 22 );
128 HGLOBAL hMemory
= GlobalAlloc( GMEM_DDESHARE
| GMEM_MOVEABLE
, sizeof( METAFILEPICT
) );
132 METAFILEPICT
* pMF
= static_cast<METAFILEPICT
*>(GlobalLock( hMemory
));
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();
146 Size aWinSize
= OutputDevice::LogicToLogic( Size( aMetaSize
.Width(), aMetaSize
.Height() ),
147 pGDIMeta
->GetPrefMapMode(),
149 pMF
->xExt
= aWinSize
.Width();
150 pMF
->yExt
= aWinSize
.Height();
153 GlobalUnlock( hMemory
);
154 pResult
= static_cast<void*>(hMemory
);
157 DeleteMetaFile( hMeta
);
163 (void)pGDIMeta
; // unused
164 (void)aMetaSize
; // unused
173 bool GraphicHelper::getThumbnailFormatFromGDI_Impl(GDIMetaFile
const * pMetaFile
, const uno::Reference
<io::XStream
>& xStream
)
175 bool bResult
= false;
177 if (!pMetaFile
|| !xStream
.is())
180 std::unique_ptr
<SvStream
> pStream(utl::UcbStreamHelper::CreateStream(xStream
));
182 if (pStream
->GetError())
185 BitmapEx aResultBitmap
;
187 bResult
= pMetaFile
->CreateThumbnail(aResultBitmap
, BmpConversion::N8BitColors
, BmpScaleFlag::Default
);
189 if (!bResult
|| aResultBitmap
.IsEmpty())
192 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
194 if (rFilter
.compressAsPNG(aResultBitmap
, *pStream
) != ERRCODE_NONE
)
199 return !pStream
->GetError();
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",
217 uno::Reference
< graphic::XGraphic
> xGraphic
= xGraphProvider
->queryGraphic( aMediaProps
);
220 uno::Sequence
< beans::PropertyValue
> aStoreProps
{
221 comphelper::makePropertyValue("OutputStream", xStream
),
222 comphelper::makePropertyValue("MimeType", OUString("image/png"))
225 xGraphProvider
->storeGraphic( xGraphic
, aStoreProps
);
229 catch(const uno::Exception
&)
238 OUString
GraphicHelper::getThumbnailReplacementIDByFactoryName_Impl( std::u16string_view aFactoryShortName
)
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
;
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */