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 : #include <stdio.h>
22 : #include <sys/types.h>
23 : #include <sys/stat.h>
24 : #include <fcntl.h>
25 : #include <unistd.h>
26 :
27 : #include "psputil.hxx"
28 : #include "glyphset.hxx"
29 :
30 : #include "generic/printerjob.hxx"
31 : #include "generic/printergfx.hxx"
32 : #include "vcl/ppdparser.hxx"
33 : #include "vcl/strhelper.hxx"
34 : #include "vcl/printerinfomanager.hxx"
35 :
36 : #include "rtl/ustring.hxx"
37 : #include "rtl/strbuf.hxx"
38 : #include "rtl/ustrbuf.hxx"
39 :
40 : #include <osl/thread.h>
41 : #include <osl/security.hxx>
42 : #include <sal/alloca.h>
43 : #include <sal/macros.h>
44 :
45 : #include <algorithm>
46 : #include <vector>
47 :
48 : using namespace psp;
49 :
50 : using ::rtl::OUString;
51 : using ::rtl::OUStringToOString;
52 : using ::rtl::OString;
53 : using ::rtl::OStringBuffer;
54 :
55 : // forward declaration
56 :
57 : #define nBLOCKSIZE 0x2000
58 :
59 : namespace psp
60 : {
61 :
62 : sal_Bool
63 0 : AppendPS (FILE* pDst, osl::File* pSrc, sal_uChar* pBuffer,
64 : sal_uInt32 nBlockSize = nBLOCKSIZE)
65 : {
66 0 : if ((pDst == NULL) || (pSrc == NULL))
67 0 : return sal_False;
68 :
69 0 : if (pSrc->setPos(osl_Pos_Absolut, 0) != osl::FileBase::E_None)
70 0 : return sal_False;
71 :
72 0 : if (nBlockSize == 0)
73 0 : nBlockSize = nBLOCKSIZE;
74 0 : if (pBuffer == NULL)
75 0 : pBuffer = (sal_uChar*)alloca (nBlockSize);
76 :
77 0 : sal_uInt64 nIn = 0;
78 0 : sal_uInt64 nOut = 0;
79 0 : do
80 : {
81 0 : pSrc->read (pBuffer, nBlockSize, nIn);
82 0 : if (nIn > 0)
83 0 : nOut = fwrite (pBuffer, 1, sal::static_int_cast<sal_uInt32>(nIn), pDst);
84 : }
85 : while ((nIn > 0) && (nIn == nOut));
86 :
87 0 : return sal_True;
88 : }
89 :
90 : } // namespace psp
91 :
92 : /*
93 : * private convenience routines for file handling
94 : */
95 :
96 : osl::File*
97 0 : PrinterJob::CreateSpoolFile (const rtl::OUString& rName, const rtl::OUString& rExtension)
98 : {
99 0 : osl::File* pFile = NULL;
100 :
101 0 : rtl::OUString aFile = rName + rExtension;
102 0 : rtl::OUString aFileURL;
103 0 : osl::File::RC nError = osl::File::getFileURLFromSystemPath( aFile, aFileURL );
104 0 : if (nError != osl::File::E_None)
105 0 : return NULL;
106 0 : aFileURL = maSpoolDirName + rtl::OUString("/") + aFileURL;
107 :
108 0 : pFile = new osl::File (aFileURL);
109 0 : nError = pFile->open (osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create);
110 0 : if (nError != osl::File::E_None)
111 : {
112 0 : delete pFile;
113 0 : return NULL;
114 : }
115 :
116 : pFile->setAttributes (aFileURL,
117 0 : osl_File_Attribute_OwnWrite | osl_File_Attribute_OwnRead);
118 0 : return pFile;
119 : }
120 :
121 : /*
122 : * public methods of PrinterJob: for use in PrinterGfx
123 : */
124 :
125 : void
126 0 : PrinterJob::GetScale (double &rXScale, double &rYScale) const
127 : {
128 0 : rXScale = mfXScale;
129 0 : rYScale = mfYScale;
130 0 : }
131 :
132 : sal_uInt16
133 0 : PrinterJob::GetDepth () const
134 : {
135 0 : sal_Int32 nLevel = GetPostscriptLevel();
136 0 : sal_Bool bColor = IsColorPrinter ();
137 :
138 0 : return nLevel > 1 && bColor ? 24 : 8;
139 : }
140 :
141 : sal_uInt16
142 0 : PrinterJob::GetPostscriptLevel (const JobData *pJobData) const
143 : {
144 0 : sal_uInt16 nPSLevel = 2;
145 :
146 0 : if( pJobData == NULL )
147 0 : pJobData = &m_aLastJobData;
148 :
149 0 : if( pJobData->m_nPSLevel )
150 0 : nPSLevel = pJobData->m_nPSLevel;
151 : else
152 0 : if( pJobData->m_pParser )
153 0 : nPSLevel = pJobData->m_pParser->getLanguageLevel();
154 :
155 0 : return nPSLevel;
156 : }
157 :
158 : sal_Bool
159 0 : PrinterJob::IsColorPrinter () const
160 : {
161 0 : sal_Bool bColor = sal_False;
162 :
163 0 : if( m_aLastJobData.m_nColorDevice )
164 0 : bColor = m_aLastJobData.m_nColorDevice == -1 ? sal_False : sal_True;
165 0 : else if( m_aLastJobData.m_pParser )
166 0 : bColor = m_aLastJobData.m_pParser->isColorDevice() ? sal_True : sal_False;
167 :
168 0 : return bColor;
169 : }
170 :
171 : osl::File*
172 0 : PrinterJob::GetCurrentPageHeader ()
173 : {
174 0 : return maHeaderList.back();
175 : }
176 :
177 : osl::File*
178 0 : PrinterJob::GetCurrentPageBody ()
179 : {
180 0 : return maPageList.back();
181 : }
182 :
183 : /*
184 : * public methods of PrinterJob: the actual job / spool handling
185 : */
186 :
187 0 : PrinterJob::PrinterJob () :
188 : mpJobHeader( NULL ),
189 : mpJobTrailer( NULL ),
190 0 : m_bQuickJob( false )
191 : {
192 0 : }
193 :
194 : /* remove all our temporary files, uses external program "rm", since
195 : osl functionality is inadequate */
196 : void
197 0 : removeSpoolDir (const rtl::OUString& rSpoolDir)
198 : {
199 0 : rtl::OUString aSysPath;
200 0 : if( osl::File::E_None != osl::File::getSystemPathFromFileURL( rSpoolDir, aSysPath ) )
201 : {
202 : // Conversion did not work, as this is quite a dangerous action,
203 : // we should abort here ....
204 : OSL_FAIL( "psprint: couldn't remove spool directory" );
205 0 : return;
206 : }
207 : rtl::OString aSysPathByte =
208 0 : rtl::OUStringToOString (aSysPath, osl_getThreadTextEncoding());
209 : sal_Char pSystem [128];
210 0 : sal_Int32 nChar = 0;
211 :
212 0 : nChar = psp::appendStr ("rm -rf ", pSystem);
213 0 : nChar += psp::appendStr (aSysPathByte.getStr(), pSystem + nChar);
214 :
215 0 : if (system (pSystem) == -1)
216 0 : OSL_FAIL( "psprint: couldn't remove spool directory" );
217 : }
218 :
219 : /* creates a spool directory with a "pidgin random" value based on
220 : current system time */
221 : rtl::OUString
222 0 : createSpoolDir ()
223 : {
224 : TimeValue aCur;
225 0 : osl_getSystemTime( &aCur );
226 0 : sal_Int32 nRand = aCur.Seconds ^ (aCur.Nanosec/1000);
227 :
228 0 : rtl::OUString aTmpDir;
229 0 : osl_getTempDirURL( &aTmpDir.pData );
230 :
231 0 : do
232 : {
233 0 : rtl::OUStringBuffer aDir( aTmpDir.getLength() + 16 );
234 0 : aDir.append( aTmpDir );
235 0 : aDir.appendAscii( "/psp" );
236 0 : aDir.append(nRand);
237 0 : rtl::OUString aResult = aDir.makeStringAndClear();
238 0 : if( osl::Directory::create( aResult ) == osl::FileBase::E_None )
239 : {
240 : osl::File::setAttributes( aResult,
241 : osl_File_Attribute_OwnWrite
242 : | osl_File_Attribute_OwnRead
243 0 : | osl_File_Attribute_OwnExe );
244 0 : return aResult;
245 : }
246 0 : nRand++;
247 : } while( nRand );
248 0 : return rtl::OUString();
249 : }
250 :
251 0 : PrinterJob::~PrinterJob ()
252 : {
253 0 : std::list< osl::File* >::iterator pPage;
254 0 : for (pPage = maPageList.begin(); pPage != maPageList.end(); ++pPage)
255 : {
256 : //(*pPage)->remove();
257 0 : delete *pPage;
258 : }
259 0 : for (pPage = maHeaderList.begin(); pPage != maHeaderList.end(); ++pPage)
260 : {
261 : //(*pPage)->remove();
262 0 : delete *pPage;
263 : }
264 : // mpJobHeader->remove();
265 0 : delete mpJobHeader;
266 : // mpJobTrailer->remove();
267 0 : delete mpJobTrailer;
268 :
269 : // XXX should really call osl::remove routines
270 0 : if( !maSpoolDirName.isEmpty() )
271 0 : removeSpoolDir (maSpoolDirName);
272 :
273 : // osl::Directory::remove (maSpoolDirName);
274 0 : }
275 :
276 0 : static void WriteLocalTimePS( osl::File *rFile )
277 : {
278 : TimeValue m_start_time, tLocal;
279 : oslDateTime date_time;
280 0 : if (osl_getSystemTime( &m_start_time ) &&
281 0 : osl_getLocalTimeFromSystemTime( &m_start_time, &tLocal ) &&
282 0 : osl_getDateTimeFromTimeValue( &tLocal, &date_time ))
283 : {
284 : char ar[ 256 ];
285 : snprintf(
286 : ar, sizeof (ar),
287 : "%04d-%02d-%02d %02d:%02d:%02d ",
288 : date_time.Year, date_time.Month, date_time.Day,
289 0 : date_time.Hours, date_time.Minutes, date_time.Seconds );
290 0 : WritePS( rFile, ar );
291 : }
292 : else
293 0 : WritePS( rFile, "Unknown-Time" );
294 0 : }
295 :
296 0 : static bool isAscii( const rtl::OUString& rStr )
297 : {
298 0 : sal_Int32 nLen = rStr.getLength();
299 0 : for( sal_Int32 i = 0; i < nLen; i++ )
300 0 : if( rStr[i] > 127 )
301 0 : return false;
302 0 : return true;
303 : }
304 :
305 : sal_Bool
306 0 : PrinterJob::StartJob (
307 : const rtl::OUString& rFileName,
308 : int nMode,
309 : const rtl::OUString& rJobName,
310 : const rtl::OUString& rAppName,
311 : const JobData& rSetupData,
312 : PrinterGfx* pGraphics,
313 : bool bIsQuickJob
314 : )
315 : {
316 0 : m_bQuickJob = bIsQuickJob;
317 0 : mnMaxWidthPt = mnMaxHeightPt = 0;
318 0 : mnLandscapes = mnPortraits = 0;
319 0 : m_pGraphics = pGraphics;
320 0 : InitPaperSize (rSetupData);
321 :
322 : // create file container for document header and trailer
323 0 : maFileName = rFileName;
324 0 : mnFileMode = nMode;
325 0 : maSpoolDirName = createSpoolDir ();
326 0 : maJobTitle = rJobName;
327 :
328 0 : rtl::OUString aExt(".ps");
329 0 : mpJobHeader = CreateSpoolFile (rtl::OUString("psp_head"), aExt);
330 0 : mpJobTrailer = CreateSpoolFile (rtl::OUString("psp_tail"), aExt);
331 0 : if( ! (mpJobHeader && mpJobTrailer) ) // existing files are removed in destructor
332 0 : return sal_False;
333 :
334 : // write document header according to Document Structuring Conventions (DSC)
335 : WritePS (mpJobHeader,
336 : "%!PS-Adobe-3.0\n"
337 0 : "%%BoundingBox: (atend)\n" );
338 :
339 0 : rtl::OUString aFilterWS;
340 :
341 : // Creator (this application)
342 0 : aFilterWS = WhitespaceToSpace( rAppName, sal_False );
343 0 : WritePS (mpJobHeader, "%%Creator: (");
344 0 : WritePS (mpJobHeader, aFilterWS);
345 0 : WritePS (mpJobHeader, ")\n");
346 :
347 : // For (user name)
348 0 : osl::Security aSecurity;
349 0 : rtl::OUString aUserName;
350 0 : if( aSecurity.getUserName( aUserName ) )
351 : {
352 0 : WritePS (mpJobHeader, "%%For: (");
353 0 : WritePS (mpJobHeader, aUserName);
354 0 : WritePS (mpJobHeader, ")\n");
355 : }
356 :
357 : // Creation Date (locale independent local time)
358 0 : WritePS (mpJobHeader, "%%CreationDate: (");
359 0 : WriteLocalTimePS (mpJobHeader);
360 0 : WritePS (mpJobHeader, ")\n");
361 :
362 : // Document Title
363 : /* #i74335#
364 : * The title should be clean ascii; rJobName however may
365 : * contain any Unicode character. So implement the following
366 : * algorithm:
367 : * use rJobName, if it contains only ascii
368 : * use the filename, if it contains only ascii
369 : * else omit %%Title
370 : */
371 0 : aFilterWS = WhitespaceToSpace( rJobName, sal_False );
372 0 : rtl::OUString aTitle( aFilterWS );
373 0 : if( ! isAscii( aTitle ) )
374 : {
375 0 : sal_Int32 nIndex = 0;
376 0 : while( nIndex != -1 )
377 0 : aTitle = rFileName.getToken( 0, '/', nIndex );
378 0 : aTitle = WhitespaceToSpace( aTitle, sal_False );
379 0 : if( ! isAscii( aTitle ) )
380 0 : aTitle = rtl::OUString();
381 : }
382 :
383 0 : maJobTitle = aFilterWS;
384 0 : if( !aTitle.isEmpty() )
385 : {
386 0 : WritePS (mpJobHeader, "%%Title: (");
387 0 : WritePS (mpJobHeader, aTitle);
388 0 : WritePS (mpJobHeader, ")\n");
389 : }
390 :
391 : // Language Level
392 : sal_Char pLevel[16];
393 0 : sal_Int32 nSz = getValueOf(GetPostscriptLevel(&rSetupData), pLevel);
394 0 : pLevel[nSz++] = '\n';
395 0 : pLevel[nSz ] = '\0';
396 0 : WritePS (mpJobHeader, "%%LanguageLevel: ");
397 0 : WritePS (mpJobHeader, pLevel);
398 :
399 : // Other
400 0 : WritePS (mpJobHeader, "%%DocumentData: Clean7Bit\n");
401 0 : WritePS (mpJobHeader, "%%Pages: (atend)\n");
402 0 : WritePS (mpJobHeader, "%%Orientation: (atend)\n");
403 0 : WritePS (mpJobHeader, "%%PageOrder: Ascend\n");
404 0 : WritePS (mpJobHeader, "%%EndComments\n");
405 :
406 : // write Prolog
407 0 : writeProlog (mpJobHeader, rSetupData);
408 :
409 : // mark last job setup as not set
410 0 : m_aLastJobData.m_pParser = NULL;
411 0 : m_aLastJobData.m_aContext.setParser( NULL );
412 :
413 0 : return sal_True;
414 : }
415 :
416 : sal_Bool
417 0 : PrinterJob::EndJob ()
418 : {
419 : // no pages ? that really means no print job
420 0 : if( maPageList.empty() )
421 0 : return sal_False;
422 :
423 : // write document setup (done here because it
424 : // includes the accumulated fonts
425 0 : if( mpJobHeader )
426 0 : writeSetup( mpJobHeader, m_aDocumentJobData );
427 0 : m_pGraphics->OnEndJob();
428 0 : if( ! (mpJobHeader && mpJobTrailer) )
429 0 : return sal_False;
430 :
431 : // write document trailer according to Document Structuring Conventions (DSC)
432 0 : rtl::OStringBuffer aTrailer(512);
433 0 : aTrailer.append( "%%Trailer\n" );
434 0 : aTrailer.append( "%%BoundingBox: 0 0 " );
435 0 : aTrailer.append( (sal_Int32)mnMaxWidthPt );
436 0 : aTrailer.append( " " );
437 0 : aTrailer.append( (sal_Int32)mnMaxHeightPt );
438 0 : if( mnLandscapes > mnPortraits )
439 0 : aTrailer.append("\n%%Orientation: Landscape");
440 : else
441 0 : aTrailer.append("\n%%Orientation: Portrait");
442 0 : aTrailer.append( "\n%%Pages: " );
443 0 : aTrailer.append( (sal_Int32)maPageList.size() );
444 0 : aTrailer.append( "\n%%EOF\n" );
445 0 : WritePS (mpJobTrailer, aTrailer.getStr());
446 :
447 : /*
448 : * spool the set of files to their final destination, this is U**X dependent
449 : */
450 :
451 0 : FILE* pDestFILE = NULL;
452 :
453 : /* create a destination either as file or as a pipe */
454 0 : sal_Bool bSpoolToFile = !maFileName.isEmpty();
455 0 : if (bSpoolToFile)
456 : {
457 : const rtl::OString aFileName = rtl::OUStringToOString (maFileName,
458 0 : osl_getThreadTextEncoding());
459 0 : if( mnFileMode )
460 : {
461 0 : int nFile = open( aFileName.getStr(), O_CREAT | O_EXCL | O_RDWR, mnFileMode );
462 0 : if( nFile != -1 )
463 : {
464 0 : pDestFILE = fdopen( nFile, "w" );
465 0 : if( pDestFILE == NULL )
466 : {
467 0 : close( nFile );
468 0 : unlink( aFileName.getStr() );
469 0 : return sal_False;
470 : }
471 : }
472 : else
473 0 : chmod( aFileName.getStr(), mnFileMode );
474 : }
475 0 : if (pDestFILE == NULL)
476 0 : pDestFILE = fopen (aFileName.getStr(), "w");
477 :
478 0 : if (pDestFILE == NULL)
479 0 : return sal_False;
480 : }
481 : else
482 : {
483 0 : PrinterInfoManager& rPrinterInfoManager = PrinterInfoManager::get ();
484 0 : pDestFILE = rPrinterInfoManager.startSpool( m_aLastJobData.m_aPrinterName, m_bQuickJob );
485 0 : if (pDestFILE == NULL)
486 0 : return sal_False;
487 : }
488 :
489 : /* spool the document parts to the destination */
490 :
491 : sal_uChar pBuffer[ nBLOCKSIZE ];
492 :
493 0 : AppendPS (pDestFILE, mpJobHeader, pBuffer);
494 0 : mpJobHeader->close();
495 :
496 0 : sal_Bool bSuccess = sal_True;
497 0 : std::list< osl::File* >::iterator pPageBody;
498 0 : std::list< osl::File* >::iterator pPageHead;
499 0 : for (pPageBody = maPageList.begin(), pPageHead = maHeaderList.begin();
500 0 : pPageBody != maPageList.end() && pPageHead != maHeaderList.end();
501 : ++pPageBody, ++pPageHead)
502 : {
503 0 : if( *pPageHead )
504 : {
505 0 : osl::File::RC nError = (*pPageHead)->open(osl_File_OpenFlag_Read);
506 0 : if (nError == osl::File::E_None)
507 : {
508 0 : AppendPS (pDestFILE, *pPageHead, pBuffer);
509 0 : (*pPageHead)->close();
510 : }
511 : }
512 : else
513 0 : bSuccess = sal_False;
514 0 : if( *pPageBody )
515 : {
516 0 : osl::File::RC nError = (*pPageBody)->open(osl_File_OpenFlag_Read);
517 0 : if (nError == osl::File::E_None)
518 : {
519 0 : AppendPS (pDestFILE, *pPageBody, pBuffer);
520 0 : (*pPageBody)->close();
521 : }
522 : }
523 : else
524 0 : bSuccess = sal_False;
525 : }
526 :
527 0 : AppendPS (pDestFILE, mpJobTrailer, pBuffer);
528 0 : mpJobTrailer->close();
529 :
530 : /* well done */
531 :
532 0 : if (bSpoolToFile)
533 0 : fclose (pDestFILE);
534 : else
535 : {
536 0 : PrinterInfoManager& rPrinterInfoManager = PrinterInfoManager::get();
537 0 : if (0 == rPrinterInfoManager.endSpool( m_aLastJobData.m_aPrinterName,
538 0 : maJobTitle, pDestFILE, m_aDocumentJobData, true ))
539 : {
540 0 : bSuccess = sal_False;
541 : }
542 : }
543 :
544 0 : return bSuccess;
545 : }
546 :
547 : sal_Bool
548 0 : PrinterJob::AbortJob ()
549 : {
550 0 : m_pGraphics->OnEndJob();
551 0 : return sal_False;
552 : }
553 :
554 : void
555 0 : PrinterJob::InitPaperSize (const JobData& rJobSetup)
556 : {
557 0 : int nRes = rJobSetup.m_aContext.getRenderResolution ();
558 :
559 0 : rtl::OUString aPaper;
560 : int nWidth, nHeight;
561 0 : rJobSetup.m_aContext.getPageSize (aPaper, nWidth, nHeight);
562 :
563 0 : int nLeft = 0, nRight = 0, nUpper = 0, nLower = 0;
564 0 : const PPDParser* pParser = rJobSetup.m_aContext.getParser();
565 0 : if (pParser != NULL)
566 0 : pParser->getMargins (aPaper, nLeft, nRight, nUpper, nLower);
567 :
568 0 : mnResolution = nRes;
569 :
570 0 : mnWidthPt = nWidth;
571 0 : mnHeightPt = nHeight;
572 :
573 0 : if( mnWidthPt > mnMaxWidthPt )
574 0 : mnMaxWidthPt = mnWidthPt;
575 0 : if( mnHeightPt > mnMaxHeightPt )
576 0 : mnMaxHeightPt = mnHeightPt;
577 :
578 0 : mnLMarginPt = nLeft;
579 0 : mnRMarginPt = nRight;
580 0 : mnTMarginPt = nUpper;
581 0 : mnBMarginPt = nLower;
582 :
583 0 : mfXScale = (double)72.0 / (double)mnResolution;
584 0 : mfYScale = -1.0 * (double)72.0 / (double)mnResolution;
585 0 : }
586 :
587 :
588 : sal_Bool
589 0 : PrinterJob::StartPage (const JobData& rJobSetup)
590 : {
591 0 : InitPaperSize (rJobSetup);
592 :
593 0 : rtl::OUString aPageNo = rtl::OUString::valueOf ((sal_Int32)maPageList.size()+1); // sequential page number must start with 1
594 0 : rtl::OUString aExt = aPageNo + rtl::OUString(".ps");
595 :
596 0 : osl::File* pPageHeader = CreateSpoolFile ( rtl::OUString("psp_pghead"), aExt);
597 0 : osl::File* pPageBody = CreateSpoolFile ( rtl::OUString("psp_pgbody"), aExt);
598 :
599 0 : maHeaderList.push_back (pPageHeader);
600 0 : maPageList.push_back (pPageBody);
601 :
602 0 : if( ! (pPageHeader && pPageBody) )
603 0 : return sal_False;
604 :
605 : // write page header according to Document Structuring Conventions (DSC)
606 0 : WritePS (pPageHeader, "%%Page: ");
607 0 : WritePS (pPageHeader, aPageNo);
608 0 : WritePS (pPageHeader, " ");
609 0 : WritePS (pPageHeader, aPageNo);
610 0 : WritePS (pPageHeader, "\n");
611 :
612 0 : if( rJobSetup.m_eOrientation == orientation::Landscape )
613 : {
614 0 : WritePS (pPageHeader, "%%PageOrientation: Landscape\n");
615 0 : mnLandscapes++;
616 : }
617 : else
618 : {
619 0 : WritePS (pPageHeader, "%%PageOrientation: Portrait\n");
620 0 : mnPortraits++;
621 : }
622 :
623 : sal_Char pBBox [256];
624 0 : sal_Int32 nChar = 0;
625 :
626 0 : nChar = psp::appendStr ("%%PageBoundingBox: ", pBBox);
627 0 : nChar += psp::getValueOf (mnLMarginPt, pBBox + nChar);
628 0 : nChar += psp::appendStr (" ", pBBox + nChar);
629 0 : nChar += psp::getValueOf (mnBMarginPt, pBBox + nChar);
630 0 : nChar += psp::appendStr (" ", pBBox + nChar);
631 0 : nChar += psp::getValueOf (mnWidthPt - mnRMarginPt, pBBox + nChar);
632 0 : nChar += psp::appendStr (" ", pBBox + nChar);
633 0 : nChar += psp::getValueOf (mnHeightPt - mnTMarginPt, pBBox + nChar);
634 0 : nChar += psp::appendStr ("\n", pBBox + nChar);
635 :
636 0 : WritePS (pPageHeader, pBBox);
637 :
638 : /* #i7262# #i65491# write setup only before first page
639 : * (to %%Begin(End)Setup, instead of %%Begin(End)PageSetup)
640 : * don't do this in StartJob since the jobsetup there may be
641 : * different.
642 : */
643 0 : bool bWriteFeatures = true;
644 0 : if( 1 == maPageList.size() )
645 : {
646 0 : m_aDocumentJobData = rJobSetup;
647 0 : bWriteFeatures = false;
648 : }
649 :
650 0 : if ( writePageSetup( pPageHeader, rJobSetup, bWriteFeatures ) )
651 : {
652 0 : m_aLastJobData = rJobSetup;
653 0 : return true;
654 : }
655 :
656 0 : return false;
657 : }
658 :
659 : sal_Bool
660 0 : PrinterJob::EndPage ()
661 : {
662 0 : m_pGraphics->OnEndPage();
663 :
664 0 : osl::File* pPageHeader = maHeaderList.back();
665 0 : osl::File* pPageBody = maPageList.back();
666 :
667 0 : if( ! (pPageBody && pPageHeader) )
668 0 : return sal_False;
669 :
670 : // copy page to paper and write page trailer according to DSC
671 :
672 : sal_Char pTrailer[256];
673 0 : sal_Int32 nChar = 0;
674 0 : nChar = psp::appendStr ("grestore grestore\n", pTrailer);
675 0 : nChar += psp::appendStr ("showpage\n", pTrailer + nChar);
676 0 : nChar += psp::appendStr ("%%PageTrailer\n\n", pTrailer + nChar);
677 0 : WritePS (pPageBody, pTrailer);
678 :
679 : // this page is done for now, close it to avoid having too many open fd's
680 :
681 0 : pPageHeader->close();
682 0 : pPageBody->close();
683 :
684 0 : return sal_True;
685 : }
686 :
687 : struct less_ppd_key : public ::std::binary_function<double, double, bool>
688 : {
689 0 : bool operator()(const PPDKey* left, const PPDKey* right)
690 0 : { return left->getOrderDependency() < right->getOrderDependency(); }
691 : };
692 :
693 0 : static bool writeFeature( osl::File* pFile, const PPDKey* pKey, const PPDValue* pValue, bool bUseIncluseFeature )
694 : {
695 0 : if( ! pKey || ! pValue )
696 0 : return true;
697 :
698 0 : OStringBuffer aFeature(256);
699 0 : aFeature.append( "[{\n" );
700 0 : if( bUseIncluseFeature )
701 0 : aFeature.append( "%%IncludeFeature:" );
702 : else
703 0 : aFeature.append( "%%BeginFeature:" );
704 0 : aFeature.append( " *" );
705 0 : aFeature.append( OUStringToOString( pKey->getKey(), RTL_TEXTENCODING_ASCII_US ) );
706 0 : aFeature.append( ' ' );
707 0 : aFeature.append( OUStringToOString( pValue->m_aOption, RTL_TEXTENCODING_ASCII_US ) );
708 0 : if( !bUseIncluseFeature )
709 : {
710 0 : aFeature.append( '\n' );
711 0 : aFeature.append( OUStringToOString( pValue->m_aValue, RTL_TEXTENCODING_ASCII_US ) );
712 0 : aFeature.append( "\n%%EndFeature" );
713 : }
714 0 : aFeature.append( "\n} stopped cleartomark\n" );
715 0 : sal_uInt64 nWritten = 0;
716 0 : return pFile->write( aFeature.getStr(), aFeature.getLength(), nWritten )
717 0 : || nWritten != (sal_uInt64)aFeature.getLength() ? false : true;
718 : }
719 :
720 0 : bool PrinterJob::writeFeatureList( osl::File* pFile, const JobData& rJob, bool bDocumentSetup )
721 : {
722 0 : bool bSuccess = true;
723 :
724 : // emit features ordered to OrderDependency
725 : // ignore features that are set to default
726 :
727 : // sanity check
728 0 : if( rJob.m_pParser == rJob.m_aContext.getParser() &&
729 : rJob.m_pParser &&
730 : ( m_aLastJobData.m_pParser == rJob.m_pParser || m_aLastJobData.m_pParser == NULL )
731 : )
732 : {
733 : int i;
734 0 : int nKeys = rJob.m_aContext.countValuesModified();
735 0 : ::std::vector< const PPDKey* > aKeys( nKeys );
736 0 : for( i = 0; i < nKeys; i++ )
737 0 : aKeys[i] = rJob.m_aContext.getModifiedKey( i );
738 0 : ::std::sort( aKeys.begin(), aKeys.end(), less_ppd_key() );
739 :
740 0 : for( i = 0; i < nKeys && bSuccess; i++ )
741 : {
742 0 : const PPDKey* pKey = aKeys[i];
743 0 : bool bEmit = false;
744 0 : if( bDocumentSetup )
745 : {
746 0 : if( pKey->getSetupType() == PPDKey::DocumentSetup )
747 0 : bEmit = true;
748 : }
749 0 : if( pKey->getSetupType() == PPDKey::PageSetup ||
750 0 : pKey->getSetupType() == PPDKey::AnySetup )
751 0 : bEmit = true;
752 0 : if( bEmit )
753 : {
754 0 : const PPDValue* pValue = rJob.m_aContext.getValue( pKey );
755 0 : if( pValue
756 : && pValue->m_eType == eInvocation
757 : && ( m_aLastJobData.m_pParser == NULL
758 0 : || m_aLastJobData.m_aContext.getValue( pKey ) != pValue
759 : || bDocumentSetup
760 : )
761 : )
762 : {
763 : // try to avoid PS level 2 feature commands if level is set to 1
764 0 : if( GetPostscriptLevel( &rJob ) == 1 )
765 : {
766 : bool bHavePS2 =
767 0 : ( pValue->m_aValue.SearchAscii( "<<" ) != STRING_NOTFOUND )
768 : ||
769 0 : ( pValue->m_aValue.SearchAscii( ">>" ) != STRING_NOTFOUND );
770 0 : if( bHavePS2 )
771 0 : continue;
772 : }
773 0 : bSuccess = writeFeature( pFile, pKey, pValue, PrinterInfoManager::get().getUseIncludeFeature() );
774 : }
775 : }
776 0 : }
777 : }
778 : else
779 0 : bSuccess = false;
780 :
781 0 : return bSuccess;
782 : }
783 :
784 0 : bool PrinterJob::writePageSetup( osl::File* pFile, const JobData& rJob, bool bWriteFeatures )
785 : {
786 0 : bool bSuccess = true;
787 :
788 0 : WritePS (pFile, "%%BeginPageSetup\n%\n");
789 0 : if ( bWriteFeatures )
790 0 : bSuccess = writeFeatureList( pFile, rJob, false );
791 0 : WritePS (pFile, "%%EndPageSetup\n");
792 :
793 : sal_Char pTranslate [128];
794 0 : sal_Int32 nChar = 0;
795 :
796 0 : if( rJob.m_eOrientation == orientation::Portrait )
797 : {
798 0 : nChar = psp::appendStr ("gsave\n[", pTranslate);
799 0 : nChar += psp::getValueOfDouble ( pTranslate + nChar, mfXScale, 5);
800 0 : nChar += psp::appendStr (" 0 0 ", pTranslate + nChar);
801 0 : nChar += psp::getValueOfDouble ( pTranslate + nChar, mfYScale, 5);
802 0 : nChar += psp::appendStr (" ", pTranslate + nChar);
803 0 : nChar += psp::getValueOf (mnRMarginPt, pTranslate + nChar);
804 0 : nChar += psp::appendStr (" ", pTranslate + nChar);
805 : nChar += psp::getValueOf (mnHeightPt-mnTMarginPt,
806 0 : pTranslate + nChar);
807 : nChar += psp::appendStr ("] concat\ngsave\n",
808 0 : pTranslate + nChar);
809 : }
810 : else
811 : {
812 0 : nChar = psp::appendStr ("gsave\n", pTranslate);
813 0 : nChar += psp::appendStr ("[ 0 ", pTranslate + nChar);
814 0 : nChar += psp::getValueOfDouble ( pTranslate + nChar, -mfYScale, 5);
815 0 : nChar += psp::appendStr (" ", pTranslate + nChar);
816 0 : nChar += psp::getValueOfDouble ( pTranslate + nChar, mfXScale, 5);
817 0 : nChar += psp::appendStr (" 0 ", pTranslate + nChar );
818 0 : nChar += psp::getValueOfDouble ( pTranslate + nChar, mnLMarginPt, 5 );
819 0 : nChar += psp::appendStr (" ", pTranslate + nChar);
820 0 : nChar += psp::getValueOf (mnBMarginPt, pTranslate + nChar );
821 : nChar += psp::appendStr ("] concat\ngsave\n",
822 0 : pTranslate + nChar);
823 : }
824 :
825 0 : WritePS (pFile, pTranslate);
826 :
827 0 : return bSuccess;
828 : }
829 :
830 0 : void PrinterJob::writeJobPatch( osl::File* pFile, const JobData& rJobData )
831 : {
832 0 : if( ! PrinterInfoManager::get().getUseJobPatch() )
833 : return;
834 :
835 0 : const PPDKey* pKey = NULL;
836 :
837 0 : if( rJobData.m_pParser )
838 0 : pKey = rJobData.m_pParser->getKey( OUString( "JobPatchFile" ) );
839 0 : if( ! pKey )
840 : return;
841 :
842 : // order the patch files
843 : // according to PPD spec the JobPatchFile options must be int
844 : // and should be emitted in order
845 0 : std::list< sal_Int32 > patch_order;
846 0 : int nValueCount = pKey->countValues();
847 0 : for( int i = 0; i < nValueCount; i++ )
848 : {
849 0 : const PPDValue* pVal = pKey->getValue( i );
850 0 : patch_order.push_back( pVal->m_aOption.ToInt32() );
851 0 : if( patch_order.back() == 0 && ! pVal->m_aOption.EqualsAscii( "0" ) )
852 : {
853 0 : WritePS( pFile, "% Warning: left out JobPatchFile option \"" );
854 0 : OString aOption = OUStringToOString( pVal->m_aOption, RTL_TEXTENCODING_ASCII_US );
855 0 : WritePS( pFile, aOption.getStr() );
856 : WritePS( pFile,
857 : "\"\n% as it violates the PPD spec;\n"
858 0 : "% JobPatchFile options need to be numbered for ordering.\n" );
859 : }
860 : }
861 :
862 0 : patch_order.sort();
863 0 : patch_order.unique();
864 :
865 0 : while( patch_order.begin() != patch_order.end() )
866 : {
867 : // note: this discards patch files not adhering to the "int" scheme
868 : // as there won't be a value for them
869 0 : writeFeature( pFile, pKey, pKey->getValue( OUString::valueOf( patch_order.front() ) ), false );
870 0 : patch_order.pop_front();
871 0 : }
872 : }
873 :
874 0 : bool PrinterJob::writeProlog (osl::File* pFile, const JobData& rJobData )
875 : {
876 0 : WritePS( pFile, "%%BeginProlog\n" );
877 :
878 : // JobPatchFile feature needs to be emitted at begin of prolog
879 0 : writeJobPatch( pFile, rJobData );
880 :
881 : static const sal_Char pProlog[] = {
882 : "%%BeginResource: procset PSPrint-Prolog 1.0 0\n"
883 : "/ISO1252Encoding [\n"
884 : "/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef\n"
885 : "/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef\n"
886 : "/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef\n"
887 : "/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef\n"
888 : "/space /exclam /quotedbl /numbersign /dollar /percent /ampersand /quotesingle\n"
889 : "/parenleft /parenright /asterisk /plus /comma /hyphen /period /slash\n"
890 : "/zero /one /two /three /four /five /six /seven\n"
891 : "/eight /nine /colon /semicolon /less /equal /greater /question\n"
892 : "/at /A /B /C /D /E /F /G\n"
893 : "/H /I /J /K /L /M /N /O\n"
894 : "/P /Q /R /S /T /U /V /W\n"
895 : "/X /Y /Z /bracketleft /backslash /bracketright /asciicircum /underscore\n"
896 : "/grave /a /b /c /d /e /f /g\n"
897 : "/h /i /j /k /l /m /n /o\n"
898 : "/p /q /r /s /t /u /v /w\n"
899 : "/x /y /z /braceleft /bar /braceright /asciitilde /unused\n"
900 : "/Euro /unused /quotesinglbase /florin /quotedblbase /ellipsis /dagger /daggerdbl\n"
901 : "/circumflex /perthousand /Scaron /guilsinglleft /OE /unused /Zcaron /unused\n"
902 : "/unused /quoteleft /quoteright /quotedblleft /quotedblright /bullet /endash /emdash\n"
903 : "/tilde /trademark /scaron /guilsinglright /oe /unused /zcaron /Ydieresis\n"
904 : "/space /exclamdown /cent /sterling /currency /yen /brokenbar /section\n"
905 : "/dieresis /copyright /ordfeminine /guillemotleft /logicalnot /hyphen /registered /macron\n"
906 : "/degree /plusminus /twosuperior /threesuperior /acute /mu /paragraph /periodcentered\n"
907 : "/cedilla /onesuperior /ordmasculine /guillemotright /onequarter /onehalf /threequarters /questiondown\n"
908 : "/Agrave /Aacute /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla\n"
909 : "/Egrave /Eacute /Ecircumflex /Edieresis /Igrave /Iacute /Icircumflex /Idieresis\n"
910 : "/Eth /Ntilde /Ograve /Oacute /Ocircumflex /Otilde /Odieresis /multiply\n"
911 : "/Oslash /Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn /germandbls\n"
912 : "/agrave /aacute /acircumflex /atilde /adieresis /aring /ae /ccedilla\n"
913 : "/egrave /eacute /ecircumflex /edieresis /igrave /iacute /icircumflex /idieresis\n"
914 : "/eth /ntilde /ograve /oacute /ocircumflex /otilde /odieresis /divide\n"
915 : "/oslash /ugrave /uacute /ucircumflex /udieresis /yacute /thorn /ydieresis] def\n"
916 : "\n"
917 : "/psp_definefont { exch dup findfont dup length dict begin { 1 index /FID ne\n"
918 : "{ def } { pop pop } ifelse } forall /Encoding 3 -1 roll def\n"
919 : "currentdict end exch pop definefont pop } def\n"
920 : "\n"
921 : "/pathdict dup 8 dict def load begin\n"
922 : "/rcmd { { currentfile 1 string readstring pop 0 get dup 32 gt { exit }\n"
923 : "{ pop } ifelse } loop dup 126 eq { pop exit } if 65 sub dup 16#3 and 1\n"
924 : "add exch dup 16#C and -2 bitshift 16#3 and 1 add exch 16#10 and 16#10\n"
925 : "eq 3 1 roll exch } def\n"
926 : "/rhex { dup 1 sub exch currentfile exch string readhexstring pop dup 0\n"
927 : "get dup 16#80 and 16#80 eq dup 3 1 roll { 16#7f and } if 2 index 0 3\n"
928 : "-1 roll put 3 1 roll 0 0 1 5 -1 roll { 2 index exch get add 256 mul }\n"
929 : "for 256 div exch pop exch { neg } if } def\n"
930 : "/xcmd { rcmd exch rhex exch rhex exch 5 -1 roll add exch 4 -1 roll add\n"
931 : "1 index 1 index 5 -1 roll { moveto } { lineto } ifelse } def end\n"
932 : "/readpath { 0 0 pathdict begin { xcmd } loop end pop pop } def\n"
933 : "\n"
934 : "systemdict /languagelevel known not {\n"
935 : "/xshow { exch dup length 0 1 3 -1 roll 1 sub { dup 3 index exch get\n"
936 : "exch 2 index exch get 1 string dup 0 4 -1 roll put currentpoint 3 -1\n"
937 : "roll show moveto 0 rmoveto } for pop pop } def\n"
938 : "/rectangle { 4 -2 roll moveto 1 index 0 rlineto 0 exch rlineto neg 0\n"
939 : "rlineto closepath } def\n"
940 : "/rectfill { rectangle fill } def\n"
941 : "/rectstroke { rectangle stroke } def } if\n"
942 : "/bshow { currentlinewidth 3 1 roll currentpoint 3 index show moveto\n"
943 : "setlinewidth false charpath stroke setlinewidth } def\n"
944 : "/bxshow { currentlinewidth 4 1 roll setlinewidth exch dup length 1 sub\n"
945 : "0 1 3 -1 roll { 1 string 2 index 2 index get 1 index exch 0 exch put dup\n"
946 : "currentpoint 3 -1 roll show moveto currentpoint 3 -1 roll false charpath\n"
947 : "stroke moveto 2 index exch get 0 rmoveto } for pop pop setlinewidth } def\n"
948 : "\n"
949 : "/psp_lzwfilter { currentfile /ASCII85Decode filter /LZWDecode filter } def\n"
950 : "/psp_ascii85filter { currentfile /ASCII85Decode filter } def\n"
951 : "/psp_lzwstring { psp_lzwfilter 1024 string readstring } def\n"
952 : "/psp_ascii85string { psp_ascii85filter 1024 string readstring } def\n"
953 : "/psp_imagedict {\n"
954 : "/psp_bitspercomponent { 3 eq { 1 }{ 8 } ifelse } def\n"
955 : "/psp_decodearray { [ [0 1 0 1 0 1] [0 255] [0 1] [0 255] ] exch get }\n"
956 : "def 7 dict dup\n"
957 : "/ImageType 1 put dup\n"
958 : "/Width 7 -1 roll put dup\n"
959 : "/Height 5 index put dup\n"
960 : "/BitsPerComponent 4 index psp_bitspercomponent put dup\n"
961 : "/Decode 5 -1 roll psp_decodearray put dup\n"
962 : "/ImageMatrix [1 0 0 1 0 0] dup 5 8 -1 roll put put dup\n"
963 : "/DataSource 4 -1 roll 1 eq { psp_lzwfilter } { psp_ascii85filter } ifelse put\n"
964 : "} def\n"
965 : "%%EndResource\n"
966 : "%%EndProlog\n"
967 : };
968 0 : WritePS (pFile, pProlog);
969 :
970 0 : return true;
971 : }
972 :
973 0 : bool PrinterJob::writeSetup( osl::File* pFile, const JobData& rJob )
974 : {
975 0 : WritePS (pFile, "%%BeginSetup\n%\n");
976 :
977 : // download fonts
978 0 : std::list< rtl::OString > aFonts[2];
979 0 : m_pGraphics->writeResources( pFile, aFonts[0], aFonts[1] );
980 :
981 0 : for( int i = 0; i < 2; i++ )
982 : {
983 0 : if( !aFonts[i].empty() )
984 : {
985 0 : std::list< rtl::OString >::const_iterator it = aFonts[i].begin();
986 0 : rtl::OStringBuffer aLine( 256 );
987 0 : if( i == 0 )
988 0 : aLine.append( "%%DocumentSuppliedResources: font " );
989 : else
990 0 : aLine.append( "%%DocumentNeededResources: font " );
991 0 : aLine.append( *it );
992 0 : aLine.append( "\n" );
993 0 : WritePS ( pFile, aLine.getStr() );
994 0 : while( (++it) != aFonts[i].end() )
995 : {
996 0 : aLine.setLength(0);
997 0 : aLine.append( "%%+ font " );
998 0 : aLine.append( *it );
999 0 : aLine.append( "\n" );
1000 0 : WritePS ( pFile, aLine.getStr() );
1001 0 : }
1002 : }
1003 : }
1004 :
1005 0 : bool bSuccess = true;
1006 : // in case of external print dialog the number of copies is prepended
1007 : // to the job, let us not complicate things by emitting our own copy count
1008 0 : bool bExternalDialog = PrinterInfoManager::get().checkFeatureToken( GetPrinterName(), "external_dialog" );
1009 0 : if( ! bExternalDialog && rJob.m_nCopies > 1 )
1010 : {
1011 : // setup code
1012 0 : rtl::OStringBuffer aLine(RTL_CONSTASCII_STRINGPARAM("/#copies "));
1013 0 : aLine.append(static_cast<sal_Int32>(rJob.m_nCopies));
1014 0 : aLine.append(RTL_CONSTASCII_STRINGPARAM(" def\n"));
1015 0 : sal_uInt64 nWritten = 0;
1016 0 : bSuccess = pFile->write(aLine.getStr(), aLine.getLength(), nWritten)
1017 0 : || nWritten != static_cast<sal_uInt64>(aLine.getLength()) ?
1018 0 : false : true;
1019 :
1020 0 : if( bSuccess && GetPostscriptLevel( &rJob ) >= 2 )
1021 0 : WritePS (pFile, "<< /NumCopies null /Policies << /NumCopies 1 >> >> setpagedevice\n" );
1022 : }
1023 :
1024 0 : bool bFeatureSuccess = writeFeatureList( pFile, rJob, true );
1025 :
1026 0 : WritePS (pFile, "%%EndSetup\n");
1027 :
1028 0 : return bSuccess && bFeatureSuccess;
1029 : }
1030 :
1031 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|