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