Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #ifdef WNT
22 :
23 : #undef WB_LEFT
24 : #undef WB_RIGHT
25 :
26 : #include <tools/presys.h>
27 : #if defined _MSC_VER
28 : #pragma warning(push, 1)
29 : #endif
30 : #include <windows.h>
31 : #if defined _MSC_VER
32 : #pragma warning(pop)
33 : #endif
34 : #include <tools/postsys.h>
35 :
36 : #endif
37 : #include <com/sun/star/uno/Exception.hpp>
38 : #include <com/sun/star/datatransfer/XTransferable.hpp>
39 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 : #include <com/sun/star/graphic/GraphicProvider.hpp>
41 : #include <com/sun/star/graphic/XGraphicProvider.hpp>
42 : #include <com/sun/star/graphic/XGraphic.hpp>
43 : #include <com/sun/star/io/XStream.hpp>
44 :
45 :
46 : #include <osl/thread.h>
47 : #include <vcl/gdimtf.hxx>
48 : #include <vcl/graph.hxx>
49 : #include <vcl/cvtgrf.hxx>
50 : #include <vcl/outdev.hxx>
51 : #include <vcl/virdev.hxx>
52 : #include <vcl/bitmapex.hxx>
53 :
54 : #include <tools/stream.hxx>
55 : #include <tools/helpers.hxx>
56 : #include <unotools/tempfile.hxx>
57 : #include <unotools/ucbstreamhelper.hxx>
58 : #include <unotools/streamwrap.hxx>
59 : #include <comphelper/processfactory.hxx>
60 :
61 :
62 : #include <sfx2/sfxresid.hxx>
63 : #include "graphhelp.hxx"
64 : #include "doc.hrc"
65 :
66 : using namespace ::com::sun::star;
67 :
68 0 : SvMemoryStream* GraphicHelper::getFormatStrFromGDI_Impl( const GDIMetaFile* pGDIMeta, sal_uInt32 nFormat )
69 : {
70 0 : SvMemoryStream* pResult = NULL;
71 0 : if ( pGDIMeta )
72 : {
73 0 : SvMemoryStream* pStream = new SvMemoryStream( 65535, 65535 );
74 0 : if ( pStream )
75 : {
76 0 : Graphic aGraph( *pGDIMeta );
77 0 : if ( GraphicConverter::Export( *pStream, aGraph, nFormat ) == 0 )
78 0 : pResult = pStream;
79 : else
80 0 : delete pStream;
81 : }
82 : }
83 :
84 0 : return pResult;
85 : }
86 :
87 :
88 : // static
89 0 : void* GraphicHelper::getEnhMetaFileFromGDI_Impl( const GDIMetaFile* pGDIMeta )
90 : {
91 : (void)pGDIMeta; // unused
92 0 : void* pResult = NULL;
93 :
94 : #ifdef WNT
95 : if ( pGDIMeta )
96 : {
97 : OUString const aStr(".emf");
98 : ::utl::TempFile aTempFile( OUString(),
99 : &aStr,
100 : NULL,
101 : sal_False );
102 :
103 : OUString aMetaFile = aTempFile.GetFileName();
104 : OUString aMetaURL = aTempFile.GetURL();
105 : OString aWinFile = OUStringToOString( aMetaFile, osl_getThreadTextEncoding() );
106 :
107 : SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( aMetaURL, STREAM_STD_READWRITE );
108 : if ( pStream )
109 : {
110 : Graphic aGraph( *pGDIMeta );
111 : sal_Bool bFailed = (sal_Bool)GraphicConverter::Export( *pStream, aGraph, CVT_EMF );
112 : pStream->Flush();
113 : delete pStream;
114 :
115 : if ( !bFailed )
116 : pResult = GetEnhMetaFileA( aWinFile.getStr() );
117 : }
118 : }
119 : #endif
120 :
121 0 : return pResult;
122 : }
123 :
124 :
125 : // static
126 0 : void* GraphicHelper::getWinMetaFileFromGDI_Impl( const GDIMetaFile* pGDIMeta, const Size& aMetaSize )
127 : {
128 : (void)pGDIMeta; // unused
129 : (void)aMetaSize; // unused
130 0 : void* pResult = NULL;
131 :
132 : #ifdef WNT
133 : if ( pGDIMeta )
134 : {
135 : SvMemoryStream* pStream = new SvMemoryStream( 65535, 65535 );
136 : if ( pStream )
137 : {
138 : Graphic aGraph( *pGDIMeta );
139 : sal_Bool bFailed = (sal_Bool)GraphicConverter::Export( *pStream, aGraph, CVT_WMF );
140 : pStream->Flush();
141 : if ( !bFailed )
142 : {
143 : sal_Int32 nLength = pStream->Seek( STREAM_SEEK_TO_END );
144 : if ( nLength > 22 )
145 : {
146 : HMETAFILE hMeta = SetMetaFileBitsEx( nLength - 22,
147 : ( reinterpret_cast< const unsigned char*>( pStream->GetData() ) ) + 22 );
148 :
149 : if ( hMeta )
150 : {
151 : HGLOBAL hMemory = GlobalAlloc( GMEM_DDESHARE | GMEM_MOVEABLE, sizeof( METAFILEPICT ) );
152 :
153 : if ( hMemory )
154 : {
155 : METAFILEPICT* pMF = (METAFILEPICT*)GlobalLock( hMemory );
156 :
157 : pMF->hMF = hMeta;
158 : pMF->mm = MM_ANISOTROPIC;
159 :
160 : MapMode aMetaMode = pGDIMeta->GetPrefMapMode();
161 : MapMode aWinMode( MAP_100TH_MM );
162 :
163 : if ( aWinMode == pGDIMeta->GetPrefMapMode() )
164 : {
165 : pMF->xExt = aMetaSize.Width();
166 : pMF->yExt = aMetaSize.Height();
167 : }
168 : else
169 : {
170 : Size aWinSize = OutputDevice::LogicToLogic( Size( aMetaSize.Width(), aMetaSize.Height() ),
171 : pGDIMeta->GetPrefMapMode(),
172 : aWinMode );
173 : pMF->xExt = aWinSize.Width();
174 : pMF->yExt = aWinSize.Height();
175 : }
176 :
177 : GlobalUnlock( hMemory );
178 : pResult = (void*)hMemory;
179 : }
180 : else
181 : DeleteMetaFile( hMeta );
182 : }
183 : }
184 : }
185 :
186 : delete pStream;
187 : }
188 : }
189 :
190 : #endif
191 :
192 :
193 0 : return pResult;
194 : }
195 :
196 :
197 : // static
198 0 : bool GraphicHelper::supportsMetaFileHandle_Impl()
199 : {
200 : #ifdef WNT
201 : return sal_True;
202 : #else
203 0 : return false;
204 : #endif
205 : }
206 :
207 :
208 : // static
209 0 : bool GraphicHelper::getThumbnailFormatFromGDI_Impl( GDIMetaFile* pMetaFile,
210 : const uno::Reference< io::XStream >& xStream )
211 : {
212 0 : bool bResult = false;
213 0 : SvStream* pStream = NULL;
214 :
215 0 : if ( xStream.is() )
216 0 : pStream = ::utl::UcbStreamHelper::CreateStream( xStream );
217 :
218 0 : if ( pMetaFile && pStream && !pStream->GetError() )
219 : {
220 0 : BitmapEx aResultBitmap;
221 :
222 0 : bResult = pMetaFile->CreateThumbnail(aResultBitmap);
223 :
224 0 : if ( bResult )
225 0 : bResult = ( !aResultBitmap.IsEmpty()
226 0 : && GraphicConverter::Export( *pStream, aResultBitmap, CVT_PNG ) == 0
227 0 : && ( pStream->Flush(), !pStream->GetError() ) );
228 :
229 0 : delete pStream;
230 : }
231 :
232 0 : return bResult;
233 : }
234 :
235 : // static
236 0 : bool GraphicHelper::getThumbnailReplacement_Impl( sal_Int32 nResID, const uno::Reference< io::XStream >& xStream )
237 : {
238 0 : bool bResult = false;
239 0 : if ( nResID && xStream.is() )
240 : {
241 0 : uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
242 : try
243 : {
244 0 : uno::Reference< graphic::XGraphicProvider > xGraphProvider(graphic::GraphicProvider::create(xContext));
245 0 : OUString aURL("private:resource/sfx/bitmapex/");
246 0 : aURL += OUString::number( nResID );
247 :
248 0 : uno::Sequence< beans::PropertyValue > aMediaProps( 1 );
249 0 : aMediaProps[0].Name = "URL";
250 0 : aMediaProps[0].Value <<= aURL;
251 :
252 0 : uno::Reference< graphic::XGraphic > xGraphic = xGraphProvider->queryGraphic( aMediaProps );
253 0 : if ( xGraphic.is() )
254 : {
255 0 : uno::Sequence< beans::PropertyValue > aStoreProps( 2 );
256 0 : aStoreProps[0].Name = "OutputStream";
257 0 : aStoreProps[0].Value <<= xStream;
258 0 : aStoreProps[1].Name = "MimeType";
259 0 : aStoreProps[1].Value <<= OUString("image/png");
260 :
261 0 : xGraphProvider->storeGraphic( xGraphic, aStoreProps );
262 0 : bResult = true;
263 0 : }
264 : }
265 0 : catch(const uno::Exception&)
266 : {
267 0 : }
268 : }
269 :
270 0 : return bResult;
271 : }
272 :
273 :
274 : // static
275 0 : sal_uInt16 GraphicHelper::getThumbnailReplacementIDByFactoryName_Impl( const OUString& aFactoryShortName, bool /*bIsTemplate*/ )
276 : {
277 0 : sal_uInt16 nResult = 0;
278 :
279 0 : if ( aFactoryShortName == "scalc" )
280 : {
281 0 : nResult = BMP_128X128_CALC_DOC;
282 : }
283 0 : else if ( aFactoryShortName == "sdraw" )
284 : {
285 0 : nResult = BMP_128X128_DRAW_DOC;
286 : }
287 0 : else if ( aFactoryShortName == "simpress" )
288 : {
289 0 : nResult = BMP_128X128_IMPRESS_DOC;
290 : }
291 0 : else if ( aFactoryShortName == "smath" )
292 : {
293 0 : nResult = BMP_128X128_MATH_DOC;
294 : }
295 0 : else if ( aFactoryShortName == "swriter" || aFactoryShortName.startsWith("swriter/") )
296 : {
297 0 : nResult = BMP_128X128_WRITER_DOC;
298 : }
299 :
300 0 : return nResult;
301 : }
302 :
303 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|