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