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