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