Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <stdio.h>
31 : :
32 : : //
33 : : #include <tools/solar.h>
34 : : #include <vcl/svapp.hxx>
35 : : #include <vcl/bitmap.hxx>
36 : : #include <vcl/bitmapex.hxx>
37 : : #include <vcl/animate.hxx>
38 : : #include <vcl/gdimtf.hxx>
39 : : #include <vcl/graph.h>
40 : : #include <vcl/window.hxx>
41 : : #include <vcl/graph.hxx>
42 : : #include <vcl/metaact.hxx>
43 : : #include <vcl/virdev.hxx>
44 : : #include <vcl/cvtgrf.hxx>
45 : : #include <vcl/bmpacc.hxx>
46 : : #include <svtools/fltcall.hxx>
47 : : #include <tools/tempfile.hxx>
48 : : #include <osl/process.h>
49 : : #include <osl/file.hxx>
50 : :
51 : : /*************************************************************************
52 : : |*
53 : : |* ImpSearchEntry()
54 : : |*
55 : : |* Beschreibung Prueft ob im Speicherbereich pSource der nComp Bytes
56 : : |* gross ist eine Zeichenkette(pDest) mit der l�nge nSize
57 : : |* liegt. Geprueft wird NON-CASE-SENSITIVE und der Rueck-
58 : : |* gabewert ist die Adresse an der die Zeichekette gefunden
59 : : |* wurde oder NULL
60 : : |*
61 : : *************************************************************************/
62 : :
63 : 0 : static sal_uInt8* ImplSearchEntry( sal_uInt8* pSource, sal_uInt8* pDest, sal_uLong nComp, sal_uLong nSize )
64 : : {
65 : 0 : while ( nComp-- >= nSize )
66 : : {
67 : : sal_uLong i;
68 : 0 : for ( i = 0; i < nSize; i++ )
69 : : {
70 : 0 : if ( ( pSource[i]&~0x20 ) != ( pDest[i]&~0x20 ) )
71 : 0 : break;
72 : : }
73 : 0 : if ( i == nSize )
74 : 0 : return pSource;
75 : 0 : pSource++;
76 : : }
77 : 0 : return NULL;
78 : : }
79 : :
80 : : //--------------------------------------------------------------------------
81 : : // SecurityCount is the buffersize of the buffer in which we will parse for a number
82 : 0 : static long ImplGetNumber( sal_uInt8 **pBuf, int& nSecurityCount )
83 : : {
84 : 0 : sal_Bool bValid = sal_True;
85 : 0 : sal_Bool bNegative = sal_False;
86 : 0 : long nRetValue = 0;
87 : 0 : while ( ( --nSecurityCount ) && ( ( **pBuf == ' ' ) || ( **pBuf == 0x9 ) ) )
88 : 0 : (*pBuf)++;
89 : 0 : sal_uInt8 nByte = **pBuf;
90 : 0 : while ( nSecurityCount && ( nByte != ' ' ) && ( nByte != 0x9 ) && ( nByte != 0xd ) && ( nByte != 0xa ) )
91 : : {
92 : 0 : switch ( nByte )
93 : : {
94 : : case '.' :
95 : : // we'll only use the integer format
96 : 0 : bValid = sal_False;
97 : 0 : break;
98 : : case '-' :
99 : 0 : bNegative = sal_True;
100 : 0 : break;
101 : : default :
102 : 0 : if ( ( nByte < '0' ) || ( nByte > '9' ) )
103 : 0 : nSecurityCount = 1; // error parsing the bounding box values
104 : 0 : else if ( bValid )
105 : : {
106 : 0 : nRetValue *= 10;
107 : 0 : nRetValue += nByte - '0';
108 : : }
109 : 0 : break;
110 : : }
111 : 0 : nSecurityCount--;
112 : 0 : nByte = *(++(*pBuf));
113 : : }
114 : 0 : if ( bNegative )
115 : 0 : nRetValue = -nRetValue;
116 : 0 : return nRetValue;
117 : : }
118 : :
119 : : //--------------------------------------------------------------------------
120 : :
121 : 0 : static int ImplGetLen( sal_uInt8* pBuf, int nMax )
122 : : {
123 : 0 : int nLen = 0;
124 : 0 : while( nLen != nMax )
125 : : {
126 : 0 : sal_uInt8 nDat = *pBuf++;
127 : 0 : if ( nDat == 0x0a || nDat == 0x25 )
128 : 0 : break;
129 : 0 : nLen++;
130 : : }
131 : 0 : return nLen;
132 : : }
133 : :
134 : 0 : static void MakeAsMeta(Graphic &rGraphic)
135 : : {
136 : 0 : VirtualDevice aVDev;
137 : 0 : GDIMetaFile aMtf;
138 : 0 : Bitmap aBmp( rGraphic.GetBitmap() );
139 : 0 : Size aSize = aBmp.GetPrefSize();
140 : :
141 : 0 : if( !aSize.Width() || !aSize.Height() )
142 : : aSize = Application::GetDefaultDevice()->PixelToLogic(
143 : 0 : aBmp.GetSizePixel(), MAP_100TH_MM );
144 : : else
145 : 0 : aSize = Application::GetDefaultDevice()->LogicToLogic( aSize,
146 : 0 : aBmp.GetPrefMapMode(), MAP_100TH_MM );
147 : :
148 : 0 : aVDev.EnableOutput( sal_False );
149 : 0 : aMtf.Record( &aVDev );
150 : 0 : aVDev.DrawBitmap( Point(), aSize, rGraphic.GetBitmap() );
151 : 0 : aMtf.Stop();
152 : 0 : aMtf.WindStart();
153 : 0 : aMtf.SetPrefMapMode( MAP_100TH_MM );
154 : 0 : aMtf.SetPrefSize( aSize );
155 : 0 : rGraphic = aMtf;
156 : 0 : }
157 : :
158 : 0 : static oslProcessError runProcessWithPathSearch(const rtl::OUString &rProgName,
159 : : rtl_uString* pArgs[], sal_uInt32 nArgs, oslProcess *pProcess,
160 : : oslFileHandle *pIn, oslFileHandle *pOut, oslFileHandle *pErr)
161 : : {
162 : : #ifdef WNT
163 : : /*
164 : : * ooo#72096
165 : : * On Window the underlying SearchPath searches in order of...
166 : : * The directory from which the application loaded.
167 : : * The current directory.
168 : : * The Windows system directory.
169 : : * The Windows directory.
170 : : * The directories that are listed in the PATH environment variable.
171 : : *
172 : : * Because one of our programs is called "convert" and there is a convert
173 : : * in the windows system directory, we want to explicitly search the PATH
174 : : * to avoid picking up on that one if ImageMagick's convert preceeds it in
175 : : * PATH.
176 : : *
177 : : */
178 : : rtl::OUString url;
179 : : rtl::OUString path(reinterpret_cast<const sal_Unicode*>(_wgetenv(L"PATH")));
180 : :
181 : : oslFileError err = osl_searchFileURL(rProgName.pData, path.pData, &url.pData);
182 : : if (err != osl_File_E_None)
183 : : return osl_Process_E_NotFound;
184 : : return osl_executeProcess_WithRedirectedIO(url.pData,
185 : : pArgs, nArgs, osl_Process_HIDDEN,
186 : : osl_getCurrentSecurity(), 0, 0, 0, pProcess, pIn, pOut, pErr);
187 : : #else
188 : : return osl_executeProcess_WithRedirectedIO(rProgName.pData,
189 : : pArgs, nArgs, osl_Process_SEARCHPATH | osl_Process_HIDDEN,
190 : 0 : osl_getCurrentSecurity(), 0, 0, 0, pProcess, pIn, pOut, pErr);
191 : : #endif
192 : : }
193 : :
194 : : #if defined(WNT)
195 : : # define EXESUFFIX ".exe"
196 : : #else
197 : : # define EXESUFFIX ""
198 : : #endif
199 : :
200 : 0 : static bool RenderAsEMF(const sal_uInt8* pBuf, sal_uInt32 nBytesRead, Graphic &rGraphic)
201 : : {
202 : 0 : TempFile aTemp;
203 : 0 : aTemp.EnableKillingFile();
204 : : rtl::OUString fileName =
205 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("pstoedit" EXESUFFIX));
206 : : rtl::OUString arg1 =
207 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-f"));
208 : : rtl::OUString arg2 =
209 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("emf:-OO"));
210 : : rtl::OUString arg3 =
211 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-"));
212 : 0 : rtl::OUString output;
213 : 0 : osl::FileBase::getSystemPathFromFileURL(aTemp.GetName(), output);
214 : : rtl_uString *args[] =
215 : : {
216 : : arg1.pData, arg2.pData, arg3.pData, output.pData
217 : 0 : };
218 : : oslProcess aProcess;
219 : 0 : oslFileHandle pIn = NULL;
220 : 0 : oslFileHandle pOut = NULL;
221 : 0 : oslFileHandle pErr = NULL;
222 : : oslProcessError eErr = runProcessWithPathSearch(fileName,
223 : : args, sizeof(args)/sizeof(rtl_uString *),
224 : 0 : &aProcess, &pIn, &pOut, &pErr);
225 : :
226 : 0 : if (eErr!=osl_Process_E_None)
227 : 0 : return false;
228 : :
229 : 0 : bool bRet = false;
230 : : sal_uInt64 nCount;
231 : 0 : osl_writeFile(pIn, pBuf, nBytesRead, &nCount);
232 : 0 : if (pIn) osl_closeFile(pIn);
233 : 0 : bool bEMFSupported=true;
234 : 0 : if (pOut)
235 : : {
236 : 0 : rtl::ByteSequence seq;
237 : 0 : if (osl_File_E_None == osl_readLine(pOut, (sal_Sequence **)&seq))
238 : : {
239 : 0 : rtl::OString line( (const sal_Char *) seq.getConstArray(), seq.getLength() );
240 : 0 : if (line.indexOf(rtl::OString("Unsupported output format")) == 0)
241 : 0 : bEMFSupported=false;
242 : : }
243 : 0 : osl_closeFile(pOut);
244 : : }
245 : 0 : if (pErr) osl_closeFile(pErr);
246 : 0 : if (nCount == nBytesRead && bEMFSupported)
247 : : {
248 : 0 : SvFileStream aFile(output, STREAM_READ);
249 : 0 : if (GraphicConverter::Import(aFile, rGraphic, CVT_EMF) == ERRCODE_NONE)
250 : 0 : bRet = true;
251 : : }
252 : 0 : osl_joinProcess(aProcess);
253 : 0 : osl_freeProcessHandle(aProcess);
254 : 0 : return bRet;
255 : : }
256 : :
257 : 0 : static bool RenderAsPNGThroughHelper(const sal_uInt8* pBuf, sal_uInt32 nBytesRead,
258 : : Graphic &rGraphic, rtl::OUString &rProgName, rtl_uString *pArgs[], size_t nArgs)
259 : : {
260 : : oslProcess aProcess;
261 : 0 : oslFileHandle pIn = NULL;
262 : 0 : oslFileHandle pOut = NULL;
263 : 0 : oslFileHandle pErr = NULL;
264 : : oslProcessError eErr = runProcessWithPathSearch(rProgName,
265 : : pArgs, nArgs,
266 : 0 : &aProcess, &pIn, &pOut, &pErr);
267 : 0 : if (eErr!=osl_Process_E_None)
268 : 0 : return false;
269 : :
270 : 0 : bool bRet = false;
271 : : sal_uInt64 nCount;
272 : 0 : osl_writeFile(pIn, pBuf, nBytesRead, &nCount);
273 : 0 : if (pIn) osl_closeFile(pIn);
274 : 0 : if (nCount == nBytesRead)
275 : : {
276 : 0 : SvMemoryStream aMemStm;
277 : : sal_uInt8 aBuf[32000];
278 : 0 : oslFileError eFileErr = osl_readFile(pOut, aBuf, 32000, &nCount);
279 : 0 : while (eFileErr == osl_File_E_None && nCount)
280 : : {
281 : 0 : aMemStm.Write(aBuf, sal::static_int_cast< sal_Size >(nCount));
282 : 0 : eFileErr = osl_readFile(pOut, aBuf, 32000, &nCount);
283 : : }
284 : :
285 : 0 : aMemStm.Seek(0);
286 : 0 : if (
287 : 0 : aMemStm.GetEndOfData() &&
288 : 0 : GraphicConverter::Import(aMemStm, rGraphic, CVT_PNG) == ERRCODE_NONE
289 : : )
290 : : {
291 : 0 : MakeAsMeta(rGraphic);
292 : 0 : bRet = true;
293 : 0 : }
294 : : }
295 : 0 : if (pOut) osl_closeFile(pOut);
296 : 0 : if (pErr) osl_closeFile(pErr);
297 : 0 : osl_joinProcess(aProcess);
298 : 0 : osl_freeProcessHandle(aProcess);
299 : 0 : return bRet;
300 : : }
301 : :
302 : 0 : static bool RenderAsPNGThroughConvert(const sal_uInt8* pBuf, sal_uInt32 nBytesRead,
303 : : Graphic &rGraphic)
304 : : {
305 : : rtl::OUString fileName =
306 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("convert" EXESUFFIX));
307 : : // density in pixel/inch
308 : 0 : rtl::OUString arg1 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-density"));
309 : : // since the preview is also used for PDF-Export & printing on non-PS-printers,
310 : : // use some better quality - 300x300 should allow some resizing as well
311 : 0 : rtl::OUString arg2 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("300x300"));
312 : : // read eps from STDIN
313 : 0 : rtl::OUString arg3 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("eps:-"));
314 : : // write png to STDOUT
315 : 0 : rtl::OUString arg4 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("png:-"));
316 : : rtl_uString *args[] =
317 : : {
318 : : arg1.pData, arg2.pData, arg3.pData, arg4.pData
319 : 0 : };
320 : : return RenderAsPNGThroughHelper(pBuf, nBytesRead, rGraphic, fileName, args,
321 : 0 : sizeof(args)/sizeof(rtl_uString *));
322 : : }
323 : :
324 : 0 : static bool RenderAsPNGThroughGS(const sal_uInt8* pBuf, sal_uInt32 nBytesRead,
325 : : Graphic &rGraphic)
326 : : {
327 : : #ifdef WNT
328 : : rtl::OUString fileName =
329 : : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("gswin32c" EXESUFFIX));
330 : : #else
331 : : rtl::OUString fileName =
332 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("gs" EXESUFFIX));
333 : : #endif
334 : : rtl::OUString arg1 =
335 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-q"));
336 : : rtl::OUString arg2 =
337 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-dBATCH"));
338 : : rtl::OUString arg3 =
339 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-dNOPAUSE"));
340 : : rtl::OUString arg4 =
341 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-dPARANOIDSAFER"));
342 : : rtl::OUString arg5 =
343 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-dEPSCrop"));
344 : : rtl::OUString arg6 =
345 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-dTextAlphaBits=4"));
346 : : rtl::OUString arg7 =
347 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-dGraphicsAlphaBits=4"));
348 : : rtl::OUString arg8 =
349 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-r300x300"));
350 : : rtl::OUString arg9 =
351 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-sDEVICE=png256"));
352 : : rtl::OUString arg10 =
353 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-sOutputFile=-"));
354 : : rtl::OUString arg11 =
355 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-"));
356 : : rtl_uString *args[] =
357 : : {
358 : : arg1.pData, arg2.pData, arg3.pData, arg4.pData, arg5.pData,
359 : : arg6.pData, arg7.pData, arg8.pData, arg9.pData, arg10.pData,
360 : : arg11.pData
361 : 0 : };
362 : : return RenderAsPNGThroughHelper(pBuf, nBytesRead, rGraphic, fileName, args,
363 : 0 : sizeof(args)/sizeof(rtl_uString *));
364 : : }
365 : :
366 : 0 : static bool RenderAsPNG(const sal_uInt8* pBuf, sal_uInt32 nBytesRead, Graphic &rGraphic)
367 : : {
368 : 0 : if (RenderAsPNGThroughConvert(pBuf, nBytesRead, rGraphic))
369 : 0 : return true;
370 : : else
371 : 0 : return RenderAsPNGThroughGS(pBuf, nBytesRead, rGraphic);
372 : : }
373 : :
374 : : // this method adds a replacement action containing the original wmf or tiff replacement,
375 : : // so the original eps can be written when storing to ODF.
376 : 0 : void CreateMtfReplacementAction( GDIMetaFile& rMtf, SvStream& rStrm, sal_uInt32 nOrigPos, sal_uInt32 nPSSize,
377 : : sal_uInt32 nPosWMF, sal_uInt32 nSizeWMF, sal_uInt32 nPosTIFF, sal_uInt32 nSizeTIFF )
378 : : {
379 : 0 : rtl::OString aComment(RTL_CONSTASCII_STRINGPARAM("EPSReplacementGraphic"));
380 : 0 : if ( nSizeWMF || nSizeTIFF )
381 : : {
382 : 0 : SvMemoryStream aReplacement( nSizeWMF + nSizeTIFF + 28 );
383 : 0 : sal_uInt32 nMagic = 0xc6d3d0c5;
384 : 0 : sal_uInt32 nPPos = 28 + nSizeWMF + nSizeTIFF;
385 : 0 : sal_uInt32 nWPos = nSizeWMF ? 28 : 0;
386 : 0 : sal_uInt32 nTPos = nSizeTIFF ? 28 + nSizeWMF : 0;
387 : :
388 : 0 : aReplacement << nMagic << nPPos << nPSSize
389 : 0 : << nWPos << nSizeWMF
390 : 0 : << nTPos << nSizeTIFF;
391 : 0 : if ( nSizeWMF )
392 : : {
393 : 0 : sal_uInt8* pBuf = new sal_uInt8[ nSizeWMF ];
394 : 0 : rStrm.Seek( nOrigPos + nPosWMF );
395 : 0 : rStrm.Read( pBuf, nSizeWMF );
396 : 0 : aReplacement.Write( pBuf, nSizeWMF );
397 : 0 : delete[] pBuf;
398 : : }
399 : 0 : if ( nSizeTIFF )
400 : : {
401 : 0 : sal_uInt8* pBuf = new sal_uInt8[ nSizeTIFF ];
402 : 0 : rStrm.Seek( nOrigPos + nPosTIFF );
403 : 0 : rStrm.Read( pBuf, nSizeTIFF );
404 : 0 : aReplacement.Write( pBuf, nSizeTIFF );
405 : 0 : delete[] pBuf;
406 : : }
407 : 0 : rMtf.AddAction( (MetaAction*)( new MetaCommentAction( aComment, 0, (const sal_uInt8*)aReplacement.GetData(), aReplacement.Tell() ) ) );
408 : : }
409 : : else
410 : 0 : rMtf.AddAction( (MetaAction*)( new MetaCommentAction( aComment, 0, NULL, 0 ) ) );
411 : 0 : }
412 : :
413 : : //there is no preview -> make a red box
414 : 0 : void MakePreview(sal_uInt8* pBuf, sal_uInt32 nBytesRead,
415 : : long nWidth, long nHeight, Graphic &rGraphic)
416 : : {
417 : 0 : GDIMetaFile aMtf;
418 : 0 : VirtualDevice aVDev;
419 : 0 : Font aFont;
420 : :
421 : 0 : aVDev.EnableOutput( sal_False );
422 : 0 : aMtf.Record( &aVDev );
423 : 0 : aVDev.SetLineColor( Color( COL_RED ) );
424 : 0 : aVDev.SetFillColor();
425 : :
426 : 0 : aFont.SetColor( COL_LIGHTRED );
427 : : // aFont.SetSize( Size( 0, 32 ) );
428 : :
429 : 0 : aVDev.Push( PUSH_FONT );
430 : 0 : aVDev.SetFont( aFont );
431 : :
432 : 0 : Rectangle aRect( Point( 1, 1 ), Size( nWidth - 2, nHeight - 2 ) );
433 : 0 : aVDev.DrawRect( aRect );
434 : :
435 : 0 : String aString;
436 : : int nLen;
437 : 0 : sal_uInt8* pDest = ImplSearchEntry( pBuf, (sal_uInt8*)"%%Title:", nBytesRead - 32, 8 );
438 : 0 : if ( pDest )
439 : : {
440 : 0 : pDest += 8;
441 : 0 : if ( *pDest == ' ' )
442 : 0 : pDest++;
443 : 0 : nLen = ImplGetLen( pDest, 32 );
444 : 0 : sal_uInt8 aOldValue(pDest[ nLen ]); pDest[ nLen ] = 0;
445 : 0 : if ( strcmp( (const char*)pDest, "none" ) != 0 )
446 : : {
447 : 0 : aString.AppendAscii( " Title:" );
448 : 0 : aString.AppendAscii( (char*)pDest );
449 : 0 : aString.AppendAscii( "\n" );
450 : : }
451 : 0 : pDest[ nLen ] = aOldValue;
452 : : }
453 : 0 : pDest = ImplSearchEntry( pBuf, (sal_uInt8*)"%%Creator:", nBytesRead - 32, 10 );
454 : 0 : if ( pDest )
455 : : {
456 : 0 : pDest += 10;
457 : 0 : if ( *pDest == ' ' )
458 : 0 : pDest++;
459 : 0 : nLen = ImplGetLen( pDest, 32 );
460 : 0 : sal_uInt8 aOldValue(pDest[ nLen ]); pDest[ nLen ] = 0;
461 : 0 : aString.AppendAscii( " Creator:" );
462 : 0 : aString.AppendAscii( (char*)pDest );
463 : 0 : aString.AppendAscii( "\n" );
464 : 0 : pDest[ nLen ] = aOldValue;
465 : : }
466 : 0 : pDest = ImplSearchEntry( pBuf, (sal_uInt8*)"%%CreationDate:", nBytesRead - 32, 15 );
467 : 0 : if ( pDest )
468 : : {
469 : 0 : pDest += 15;
470 : 0 : if ( *pDest == ' ' )
471 : 0 : pDest++;
472 : 0 : nLen = ImplGetLen( pDest, 32 );
473 : 0 : sal_uInt8 aOldValue(pDest[ nLen ]); pDest[ nLen ] = 0;
474 : 0 : if ( strcmp( (const char*)pDest, "none" ) != 0 )
475 : : {
476 : 0 : aString.AppendAscii( " CreationDate:" );
477 : 0 : aString.AppendAscii( (char*)pDest );
478 : 0 : aString.AppendAscii( "\n" );
479 : : }
480 : 0 : pDest[ nLen ] = aOldValue;
481 : : }
482 : 0 : pDest = ImplSearchEntry( pBuf, (sal_uInt8*)"%%LanguageLevel:", nBytesRead - 4, 16 );
483 : 0 : if ( pDest )
484 : : {
485 : 0 : pDest += 16;
486 : 0 : int nCount = 4;
487 : 0 : long nNumber = ImplGetNumber( &pDest, nCount );
488 : 0 : if ( nCount && ( (sal_uInt32)nNumber < 10 ) )
489 : : {
490 : 0 : aString.AppendAscii( " LanguageLevel:" );
491 : 0 : aString.Append( UniString::CreateFromInt32( nNumber ) );
492 : : }
493 : : }
494 : 0 : aVDev.DrawText( aRect, aString, TEXT_DRAW_CLIP | TEXT_DRAW_MULTILINE );
495 : 0 : aVDev.Pop();
496 : 0 : aMtf.Stop();
497 : 0 : aMtf.WindStart();
498 : 0 : aMtf.SetPrefMapMode( MAP_POINT );
499 : 0 : aMtf.SetPrefSize( Size( nWidth, nHeight ) );
500 : 0 : rGraphic = aMtf;
501 : 0 : }
502 : :
503 : :
504 : : //================== GraphicImport - die exportierte Funktion ================
505 : :
506 : : extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool __LOADONCALLAPI
507 : 0 : GraphicImport(SvStream & rStream, Graphic & rGraphic, FilterConfigItem*, sal_Bool)
508 : : {
509 : 0 : if ( rStream.GetError() )
510 : 0 : return sal_False;
511 : :
512 : 0 : Graphic aGraphic;
513 : 0 : sal_Bool bRetValue = sal_False;
514 : 0 : sal_Bool bHasPreview = sal_False;
515 : 0 : sal_Bool bGraphicLinkCreated = sal_False;
516 : : sal_uInt32 nSignature, nPSStreamPos, nPSSize;
517 : 0 : sal_uInt32 nSizeWMF = 0;
518 : 0 : sal_uInt32 nPosWMF = 0;
519 : 0 : sal_uInt32 nSizeTIFF = 0;
520 : 0 : sal_uInt32 nPosTIFF = 0;
521 : 0 : sal_uInt32 nOrigPos = nPSStreamPos = rStream.Tell();
522 : 0 : sal_uInt16 nOldFormat = rStream.GetNumberFormatInt();
523 : 0 : rStream.SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN );
524 : 0 : rStream >> nSignature;
525 : 0 : if ( nSignature == 0xc6d3d0c5 )
526 : : {
527 : 0 : rStream >> nPSStreamPos >> nPSSize >> nPosWMF >> nSizeWMF;
528 : :
529 : : // first we try to get the metafile grafix
530 : :
531 : 0 : if ( nSizeWMF )
532 : : {
533 : 0 : if ( nPosWMF != 0 )
534 : : {
535 : 0 : rStream.Seek( nOrigPos + nPosWMF );
536 : 0 : if ( GraphicConverter::Import( rStream, aGraphic, CVT_WMF ) == ERRCODE_NONE )
537 : 0 : bHasPreview = bRetValue = sal_True;
538 : : }
539 : : }
540 : : else
541 : : {
542 : 0 : rStream >> nPosTIFF >> nSizeTIFF;
543 : :
544 : : // else we have to get the tiff grafix
545 : :
546 : 0 : if ( nPosTIFF && nSizeTIFF )
547 : : {
548 : 0 : rStream.Seek( nOrigPos + nPosTIFF );
549 : 0 : if ( GraphicConverter::Import( rStream, aGraphic, CVT_TIF ) == ERRCODE_NONE )
550 : : {
551 : 0 : MakeAsMeta(aGraphic);
552 : 0 : rStream.Seek( nOrigPos + nPosTIFF );
553 : 0 : bHasPreview = bRetValue = sal_True;
554 : : }
555 : : }
556 : : }
557 : : }
558 : : else
559 : : {
560 : 0 : nPSStreamPos = nOrigPos; // no preview available _>so we must get the size manually
561 : 0 : nPSSize = rStream.Seek( STREAM_SEEK_TO_END ) - nOrigPos;
562 : : }
563 : 0 : sal_uInt8* pHeader = new sal_uInt8[ 22 ];
564 : 0 : rStream.Seek( nPSStreamPos );
565 : 0 : rStream.Read( pHeader, 22 ); // check PostScript header
566 : 0 : if ( ImplSearchEntry( pHeader, (sal_uInt8*)"%!PS-Adobe", 10, 10 ) &&
567 : 0 : ImplSearchEntry( &pHeader[ 15 ], (sal_uInt8*)"EPS", 3, 3 ) )
568 : : {
569 : 0 : rStream.Seek( nPSStreamPos );
570 : 0 : sal_uInt8* pBuf = new sal_uInt8[ nPSSize ];
571 : 0 : if ( pBuf )
572 : : {
573 : 0 : sal_uInt32 nBufStartPos = rStream.Tell();
574 : 0 : sal_uInt32 nBytesRead = rStream.Read( pBuf, nPSSize );
575 : 0 : if ( nBytesRead == nPSSize )
576 : : {
577 : 0 : int nSecurityCount = 32;
578 : 0 : if ( !bHasPreview ) // if there is no tiff/wmf preview, we will parse for an preview in the eps prolog
579 : : {
580 : 0 : sal_uInt8* pDest = ImplSearchEntry( pBuf, (sal_uInt8*)"%%BeginPreview:", nBytesRead - 32, 15 );
581 : 0 : if ( pDest )
582 : : {
583 : 0 : pDest += 15;
584 : 0 : long nWidth = ImplGetNumber( &pDest, nSecurityCount );
585 : 0 : long nHeight = ImplGetNumber( &pDest, nSecurityCount );
586 : 0 : long nBitDepth = ImplGetNumber( &pDest, nSecurityCount );
587 : 0 : long nScanLines = ImplGetNumber( &pDest, nSecurityCount );
588 : 0 : pDest = ImplSearchEntry( pDest, (sal_uInt8*)"%", 16, 1 ); // go to the first Scanline
589 : 0 : if ( nSecurityCount && pDest && nWidth && nHeight && ( ( nBitDepth == 1 ) || ( nBitDepth == 8 ) ) && nScanLines )
590 : : {
591 : 0 : rStream.Seek( nBufStartPos + ( pDest - pBuf ) );
592 : :
593 : 0 : Bitmap aBitmap( Size( nWidth, nHeight ), 1 );
594 : 0 : BitmapWriteAccess* pAcc = aBitmap.AcquireWriteAccess();
595 : 0 : if ( pAcc )
596 : : {
597 : : int nBitsLeft;
598 : 0 : sal_Bool bIsValid = sal_True;
599 : 0 : sal_uInt8 nDat = 0;
600 : : char nByte;
601 : 0 : for ( long y = 0; bIsValid && ( y < nHeight ); y++ )
602 : : {
603 : 0 : nBitsLeft = 0;
604 : 0 : for ( long x = 0; x < nWidth; x++ )
605 : : {
606 : 0 : if ( --nBitsLeft < 0 )
607 : : {
608 : 0 : while ( bIsValid && ( nBitsLeft != 7 ) )
609 : : {
610 : 0 : rStream >> nByte;
611 : 0 : switch ( nByte )
612 : : {
613 : : case 0x0a :
614 : 0 : if ( --nScanLines < 0 )
615 : 0 : bIsValid = sal_False;
616 : : case 0x09 :
617 : : case 0x0d :
618 : : case 0x20 :
619 : : case 0x25 :
620 : 0 : break;
621 : : default:
622 : : {
623 : 0 : if ( nByte >= '0' )
624 : : {
625 : 0 : if ( nByte > '9' )
626 : : {
627 : 0 : nByte &=~0x20; // case none sensitive for hexadezimal values
628 : 0 : nByte -= ( 'A' - 10 );
629 : 0 : if ( nByte > 15 )
630 : 0 : bIsValid = sal_False;
631 : : }
632 : : else
633 : 0 : nByte -= '0';
634 : 0 : nBitsLeft += 4;
635 : 0 : nDat <<= 4;
636 : 0 : nDat |= ( nByte ^ 0xf ); // in epsi a zero bit represents white color
637 : : }
638 : : else
639 : 0 : bIsValid = sal_False;
640 : : }
641 : 0 : break;
642 : : }
643 : : }
644 : : }
645 : 0 : if ( nBitDepth == 1 )
646 : 0 : pAcc->SetPixel( y, x, sal::static_int_cast< sal_uInt8 >(( nDat >> nBitsLeft ) & 1) );
647 : : else
648 : : {
649 : 0 : pAcc->SetPixel( y, x, ( nDat ) ? 1 : 0 ); // nBitDepth == 8
650 : 0 : nBitsLeft = 0;
651 : : }
652 : : }
653 : : }
654 : 0 : if ( bIsValid )
655 : : {
656 : 0 : VirtualDevice aVDev;
657 : 0 : GDIMetaFile aMtf;
658 : 0 : Size aSize;
659 : 0 : aVDev.EnableOutput( sal_False );
660 : 0 : aMtf.Record( &aVDev );
661 : 0 : aSize = aBitmap.GetPrefSize();
662 : 0 : if( !aSize.Width() || !aSize.Height() )
663 : 0 : aSize = Application::GetDefaultDevice()->PixelToLogic( aBitmap.GetSizePixel(), MAP_100TH_MM );
664 : : else
665 : 0 : aSize = Application::GetDefaultDevice()->LogicToLogic( aSize, aBitmap.GetPrefMapMode(), MAP_100TH_MM );
666 : 0 : aVDev.DrawBitmap( Point(), aSize, aBitmap );
667 : 0 : aMtf.Stop();
668 : 0 : aMtf.WindStart();
669 : 0 : aMtf.SetPrefMapMode( MAP_100TH_MM );
670 : 0 : aMtf.SetPrefSize( aSize );
671 : 0 : aGraphic = aMtf;
672 : 0 : bHasPreview = bRetValue = sal_True;
673 : : }
674 : 0 : aBitmap.ReleaseAccess( pAcc );
675 : 0 : }
676 : : }
677 : : }
678 : : }
679 : :
680 : 0 : sal_uInt8* pDest = ImplSearchEntry( pBuf, (sal_uInt8*)"%%BoundingBox:", nBytesRead, 14 );
681 : 0 : if ( pDest )
682 : : {
683 : 0 : nSecurityCount = 100;
684 : : long nNumb[4];
685 : 0 : nNumb[0] = nNumb[1] = nNumb[2] = nNumb[3] = 0;
686 : 0 : pDest += 14;
687 : 0 : for ( int i = 0; ( i < 4 ) && nSecurityCount; i++ )
688 : : {
689 : 0 : nNumb[ i ] = ImplGetNumber( &pDest, nSecurityCount );
690 : : }
691 : 0 : if ( nSecurityCount)
692 : : {
693 : 0 : bGraphicLinkCreated = sal_True;
694 : 0 : GfxLink aGfxLink( pBuf, nPSSize, GFX_LINK_TYPE_EPS_BUFFER, sal_True ) ;
695 : 0 : GDIMetaFile aMtf;
696 : :
697 : 0 : long nWidth = nNumb[2] - nNumb[0] + 1;
698 : 0 : long nHeight = nNumb[3] - nNumb[1] + 1;
699 : :
700 : : // if there is no preview -> try with gs to make one
701 : 0 : if( !bHasPreview )
702 : : {
703 : 0 : bHasPreview = RenderAsEMF(pBuf, nBytesRead, aGraphic);
704 : 0 : if (!bHasPreview)
705 : 0 : bHasPreview = RenderAsPNG(pBuf, nBytesRead, aGraphic);
706 : : }
707 : :
708 : : // if there is no preview -> make a red box
709 : 0 : if( !bHasPreview )
710 : : {
711 : : MakePreview(pBuf, nBytesRead, nWidth, nHeight,
712 : 0 : aGraphic);
713 : : }
714 : :
715 : : aMtf.AddAction( (MetaAction*)( new MetaEPSAction( Point(), Size( nWidth, nHeight ),
716 : 0 : aGfxLink, aGraphic.GetGDIMetaFile() ) ) );
717 : 0 : CreateMtfReplacementAction( aMtf, rStream, nOrigPos, nPSSize, nPosWMF, nSizeWMF, nPosTIFF, nSizeTIFF );
718 : 0 : aMtf.WindStart();
719 : 0 : aMtf.SetPrefMapMode( MAP_POINT );
720 : 0 : aMtf.SetPrefSize( Size( nWidth, nHeight ) );
721 : 0 : rGraphic = aMtf;
722 : 0 : bRetValue = sal_True;
723 : : }
724 : : }
725 : : }
726 : : }
727 : 0 : if ( !bGraphicLinkCreated )
728 : 0 : delete[] pBuf;
729 : : }
730 : 0 : delete[] pHeader;
731 : 0 : rStream.SetNumberFormatInt(nOldFormat);
732 : 0 : rStream.Seek( nOrigPos );
733 : 0 : return ( bRetValue );
734 : : }
735 : :
736 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|