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