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