summaryrefslogtreecommitdiff
path: root/Xprint/pcl/PclPrint.c
blob: f21d6c22a02177c758c1dd83c2af1b69a3717552 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/* $Xorg: PclPrint.c,v 1.3 2000/08/17 19:48:08 cpqbld Exp $ */
/*******************************************************************
**
**    *********************************************************
**    *
**    *  File:		PclPrint.c
**    *
**    *  Contents:	Print extension code of Pcl driver
**    *
**    *  Created:	2/03/95
**    *
**    *********************************************************
** 
********************************************************************/
/*
(c) Copyright 1996 Hewlett-Packard Company
(c) Copyright 1996 International Business Machines Corp.
(c) Copyright 1996 Sun Microsystems, Inc.
(c) Copyright 1996 Novell, Inc.
(c) Copyright 1996 Digital Equipment Corp.
(c) Copyright 1996 Fujitsu Limited
(c) Copyright 1996 Hitachi, Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the names of the copyright holders shall
not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization from said
copyright holders.
*/
/* $XFree86: xc/programs/Xserver/Xprint/pcl/PclPrint.c,v 1.7 2001/10/28 03:32:55 tsi Exp $ */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <X11/Xprotostr.h>

#define NEED_EVENTS
#include "Xproto.h"
#undef NEED_EVENTS

#include "Pcl.h"

#include "windowstr.h"
#include "attributes.h"
#include "AttrValid.h"
#include "Oid.h"

int
PclStartJob(
     XpContextPtr pCon,
     Bool sendClientData,
     ClientPtr client)
{
    PclContextPrivPtr pConPriv = 
      (PclContextPrivPtr)pCon->devPrivates[PclContextPrivateIndex].ptr;
    PclPaletteMap *pal;
    
    /*
     * Check for existing page file, and delete it if it exists.
     */
    if(pConPriv->pageFileName != (char *)NULL)
    {
	if(pConPriv->pPageFile != (FILE *)NULL)
	{
	    fclose(pConPriv->pPageFile);
	    pConPriv->pPageFile = (FILE *)NULL;
	}
	unlink(pConPriv->pageFileName);
	xfree(pConPriv->pageFileName);
	pConPriv->pageFileName = (char *)NULL;
    }

    /* 
     * Create a temporary file to store the printer output.
     */
    if (!XpOpenTmpFile("w+", &pConPriv->jobFileName, &pConPriv->pJobFile))
	return BadAlloc;

    /*
     * Create/Initialize the SoftFontInfo structure
     */
    pConPriv->pSoftFontInfo = PclCreateSoftFontInfo();

    /*
     * Set up the colormap handling
     */
    pConPriv->palettes = NULL;
    pConPriv->nextPaletteId = 4;
    pConPriv->currentPalette = 0;

    pal = &( pConPriv->staticGrayPalette );
    pal->paletteId = 1;
    pal->downloaded = 0;
    
    pal = &( pConPriv->trueColorPalette );
    pal->paletteId = 2;
    pal->downloaded = 0;
    
    pal = &( pConPriv->specialTrueColorPalette );
    pal->paletteId = 3;
    pal->downloaded = 0;

    return Success;
}

int
PclEndJob(
     XpContextPtr pCon,
     Bool cancel)
{
    PclContextPrivPtr priv = (PclContextPrivPtr)
      pCon->devPrivates[PclContextPrivateIndex].ptr;

#ifdef CCP_DEBUG
    FILE *xpoutput;
#endif
    FILE *fp;
    int retVal;
    char *fileName, *trailer;
    struct stat statBuf;
    int n;
    PclPaletteMapPtr p;

    trailer = "\033%-12345X@PJL RESET\n";

    if( cancel == True )
      {
	  if( priv->getDocClient != (ClientPtr)NULL ) {
	      XpFinishDocData( priv->getDocClient );

	      priv->getDocClient = NULL;
	      priv->getDocBufSize = 0;
	  }

	  return Success;
      }
    
    if( priv->getDocClient != (ClientPtr)NULL && priv->getDocBufSize > 0  )
      {
	  /*
	   * We need to stash the trailer information somewhere...
	   */
	  if (!XpOpenTmpFile("w+", &fileName, &fp))
	      return BadAlloc;

#ifndef XP_PCL_LJ3
	  SEND_PCL( fp, trailer );
	  rewind( fp );

	  retVal = XpSendDocumentData( priv->getDocClient, fp,
				      strlen( trailer ),
				      priv->getDocBufSize );
#endif /* XP_PCL_LJ3 */

	  fclose( fp );
	  unlink( fileName );
	  xfree( fileName );

	  if( priv->getDocClient != (ClientPtr)NULL ) {
	      XpFinishDocData( priv->getDocClient );

	      priv->getDocClient = NULL;
	      priv->getDocBufSize = 0;
	  }

	  return retVal;
      }
    
#ifndef XP_PCL_LJ3
    SEND_PCL( priv->pJobFile, trailer );
#endif /* XP_PCL_LJ3 */
    
    /*
     * Submit the job to the spooler
     */
    fflush( priv->pJobFile );
    
    /*
     * Dump the job file to another output file, for testing
     * purposes.
     */
    rewind( priv->pJobFile );
    n = stat( priv->jobFileName, &statBuf );
    
#ifdef CCP_DEBUG
    unlink( "/users/prince/XpOutput" );
    xpoutput = fopen( "/users/prince/XpOutput", "w" );
    
    rewind( priv->pJobFile );
    n = TransferBytes( priv->pJobFile, xpoutput,
		      (int)statBuf.st_size );
    fclose( xpoutput );
#endif
    
    XpSubmitJob( priv->jobFileName, pCon );
    fclose( priv->pJobFile );
    unlink( priv->jobFileName );
    xfree( priv->jobFileName );
    priv->jobFileName = NULL;

    PclDestroySoftFontInfo(priv->pSoftFontInfo);
    priv->pSoftFontInfo = (PclSoftFontInfoPtr) NULL;

    /*
     * Clear out the colormap cache
     */
    p = priv->palettes;
    while( p )
      {
	  p->downloaded = 0;
	  p = p->next;
      }

    return Success;
}

/* StartPage 
 *
 * If page file exists
 *     close page file
 *     set page file pointer = NULL
 *     unlink page file
 * Create a new page file
 *     Send the page header information to the page file
 * ClearArea the window and all descendant windows
 */
int
PclStartPage(
     XpContextPtr pCon,
     WindowPtr pWin)
{
    PclContextPrivPtr pConPriv = (PclContextPrivPtr)
      pCon->devPrivates[PclContextPrivateIndex].ptr;
    PclWindowPrivPtr pWinPriv =
      (PclWindowPrivPtr)pWin->devPrivates[PclWindowPrivateIndex].ptr;
    xRectangle repro;
    char t[80];
    XpOid orient, plex, tray, medium;
    int dir, plexNum, num;
    
    /*
     * Put a pointer to the context in the window private structure
     */
    pWinPriv->validContext = 1;
    pWinPriv->context = pCon;
    
    /*
     * Clear out the old page file, if necessary
     */
    if(pConPriv->pPageFile != (FILE *)NULL)
    {
	fclose(pConPriv->pPageFile);
	pConPriv->pPageFile = (FILE *)NULL;
    }
    if(pConPriv->pageFileName != (char *)NULL)
    {
	unlink(pConPriv->pageFileName);
	pConPriv->pageFileName = (char *)NULL;
    }

    /*
     * Make up a new page file.
     */
    if (!XpOpenTmpFile("w+", &pConPriv->pageFileName, &pConPriv->pPageFile))
	return BadAlloc;

    /*
     * Reset the GC cached in the context private struct.
     */
    pConPriv->validGC = 0;

    /*
     * Set the page orientation
     */
    orient = XpGetContentOrientation( pCon );
    switch( orient )
      {
	case xpoid_val_content_orientation_landscape:
	  dir = 1;
	  break;
	case xpoid_val_content_orientation_reverse_portrait:
	  dir = 2;
	  break;
	case xpoid_val_content_orientation_reverse_landscape:
	  dir = 3;
	  break;
	case xpoid_val_content_orientation_portrait:
	default:
	  dir = 0;
	  break;
      }
    sprintf( t, "\033&l%dO", dir );
    SEND_PCL( pConPriv->pPageFile, t );
    
    /*
     * Set the duplexing method.  Since PCL wants to think of it in
     * terms of the "binding edge," and the attribute store thinks in
     * "duplex/tumble," this is a little complicated.
     *
     * Actually, this has no bearing on the output, since the HP1600C
     * will only print on one side of the paper, and ignore all
     * requests to enable duplexing.  But, in an attempt to keep this
     * driver somewhat generic, we'll enable it anyway.
     */
    plex = XpGetPlex( pCon );
    
    if( plex == xpoid_val_plex_duplex )
      {
	  if( dir == 0 || dir == 2 )
	    plexNum = 1;
	  else
	    plexNum = 2;
       }
    else if( plex == xpoid_val_plex_tumble )
      {
	  if( dir == 0 || dir == 2 )
	    plexNum = 2;
	  else
	    plexNum = 1;
      }
    else
      plexNum = 0;
    sprintf( t, "\033&l%dS", plexNum );
    SEND_PCL( pConPriv->pPageFile, t );

    /*
     * Set the input tray or medium.  If XpGetPageSize gives us a valid medium,
     * we can just send that to the printer, and let the printer handle the
     * details.  Otherwise, we select the tray returned from XpGetPageSize,
     * which will be either a tray that should contain the correct medium
     * (possibly with operator intervention), or the default tray from the
     * config files.
     */
    medium = XpGetPageSize( pCon, &tray, NULL );
    if( medium != xpoid_none )
      {
	  switch( medium )
	    {
	      case xpoid_val_medium_size_na_legal:
		num = 3;
		break;
	      case xpoid_val_medium_size_iso_a3:
		num = 27;
		break;
	      case xpoid_val_medium_size_iso_a4:
		num = 26;
		break;
	      case xpoid_val_medium_size_executive:
		num = 1;
		break;
	      case xpoid_val_medium_size_ledger:
		num = 6;
		break;
	      case xpoid_val_medium_size_monarch_envelope:
		num = 80;
		break;
	      case xpoid_val_medium_size_na_number_10_envelope:
		num = 81;
		break;
	      case xpoid_val_medium_size_iso_designated_long:
		num = 90;
		break;
	      case xpoid_val_medium_size_iso_c5:
		num = 91;
		break;
	      case xpoid_val_medium_size_iso_b5:
		num = 100;
		break;
	      case xpoid_val_medium_size_jis_b5:
		num = 45;
		break;
	      case xpoid_val_medium_size_na_letter:
	      default:
		num = 2;
		break;
	    }
	  sprintf( t, "\033&l%dA", num );
	  SEND_PCL( pConPriv->pPageFile, t );
      }
    else
      {
	  switch( tray )
	    {
	      case xpoid_val_input_tray_manual:
		num = 2;
		break;
	      case xpoid_val_input_tray_envelope:
		num = 3;
		break;
	      case xpoid_val_input_tray_large_capacity:
		num = 5;
		break;
	      case xpoid_val_input_tray_bottom:
		num = 4;
		break;
	      case xpoid_val_input_tray_main:
	      default:
		num = 1;
		break;
	    }
	  sprintf( t, "\033&l%dH", num );
	  SEND_PCL( pConPriv->pPageFile, t );
      }
    
    /*
     * Set the scaling factors so that the HP-GL/2 coordinate system
     * matches the X coordinate system, both in axis orientation and
     * in unit<->pixel conversion.
     */
    XpGetReproductionArea( pCon, &repro );

    sprintf( t, "\033&l0E\033*p%dx%dY", repro.x - 75, repro.y );
    SEND_PCL( pConPriv->pPageFile, t );

    sprintf( t, "\033*c%dx%dY\033*c0T", (int)(repro.width / 300.0 * 720.0),
	    (int)(repro.height / 300.0 * 720.0) );
    SEND_PCL( pConPriv->pPageFile, t );
    
    sprintf( t, "\033%%0BSC%d,%d,%d,%d;\033%%0A", repro.x, repro.x +
	    repro.width, repro.y + repro.height, repro.y );
    SEND_PCL( pConPriv->pPageFile, t );

    return Success;
}

/*
 * When sending the generated PCL code back to the client, we send everything
 * that we have generated so far for the job.  After sending the data, we clean
 * out the job file, to avoid repeatedly sending the same data.
 */

static int
SendDocData( PclContextPrivPtr pPriv )
{
    struct stat statBuf;
    int ret;
    
    rewind( pPriv->pJobFile );
    if( stat( pPriv->jobFileName, &statBuf ) < 0 )
      return BadAlloc;
    
    ret = XpSendDocumentData( pPriv->getDocClient, pPriv->pJobFile,
			     (int)statBuf.st_size, pPriv->getDocBufSize );

    /*
     * Clean out the job file
     */
    fclose( pPriv->pJobFile );
    unlink( pPriv->jobFileName );

    xfree(pPriv->jobFileName);

    if (!XpOpenTmpFile("w+", &pPriv->jobFileName, &pPriv->pJobFile))
	return BadAlloc;

    return ret;
}

/*
 * EndPage:
 *
 * Write page trailer to page file
 * Write page file to job file
 */
int
PclEndPage(
     XpContextPtr pCon,
     WindowPtr pWin)
{
    PclContextPrivPtr pConPriv = (PclContextPrivPtr)
      pCon->devPrivates[PclContextPrivateIndex].ptr;

    struct stat statBuf;

    /*
     * Send the page trailer to the page file.
     */
    SEND_PCL( pConPriv->pPageFile, "\014" );
    fflush( pConPriv->pPageFile );
    
    /*
     * Write the page file contents to the job file, or to the
     * whatever client has called GetDocumentData.
     *
     * pWinPriv->pPageFile must first be set to the start of the page file.
     */
    rewind(pConPriv->pPageFile);
    if(stat(pConPriv->pageFileName, &statBuf) < 0)
      return BadAlloc;

    if(TransferBytes(pConPriv->pPageFile, pConPriv->pJobFile, 
       (int)statBuf.st_size) != (int)statBuf.st_size)
      return BadAlloc;

    if( pConPriv->getDocClient != (ClientPtr)NULL &&
       pConPriv->getDocBufSize > 0 )
      {
	  return SendDocData( pConPriv );
      }
    
    return Success;
}

/*
 * The PclStartDoc() and PclEndDoc() functions serve basically as NOOP
 * placeholders.  This driver doesn't deal with the notion of multiple
 * documents per page.
 */

int
PclStartDoc(XpContextPtr pCon,
	    XPDocumentType type)
{
    PclContextPrivPtr pConPriv = (PclContextPrivPtr)
      pCon->devPrivates[PclContextPrivateIndex].ptr;
    
#ifndef XP_PCL_LJ3
    /*
     * Set the printer resolution for the page.  Since we can only
     * render color at 300dpi, we just hard-code this.
     */
    SEND_PCL( pConPriv->pJobFile, 
	     "\033%-12345X@PJL SET RESOLUTION = 300\r\n" );
#endif /* XP_PCL_LJ3 */
    
    /*
     * Initialize HP-GL/2
     */
    SEND_PCL( pConPriv->pJobFile, "\033E\033%0BIN,SP1,TR0;\033%0A" );

    /*
     * Stash the type of the document (used by PutDocumentData operation)
     */
    pConPriv->isRaw = (type == XPDocRaw);
    
    return Success;
}

int
PclEndDoc(
     XpContextPtr pCon,
     Bool cancel)
{
    /*
     * XXX What should I do if I get cancel == TRUE?
     */
    return Success;
}

/*
 * PclDocumentData()
 *
 * Hand any pre-generated PDL down to the spool files, formatting it
 * as necessary to fit the given window.
 *
 */

#define DOC_PCL 1
#define DOC_HPGL 2

int
PclDocumentData(
     XpContextPtr pCon,
     DrawablePtr pDraw,
     char *pData,
     int len_data,
     char *pFmt,
     int len_fmt,
     char *pOpt,
     int len_opt,
     ClientPtr client)
{
    int type = 0;
    PclContextPrivPtr pPriv = (PclContextPrivPtr)
      pCon->devPrivates[PclContextPrivateIndex].ptr;
    XpOidDocFmtList *formats;
    XpOidDocFmt *f;
    char t[80];
    xRectangle repro;
    
    /*
     * Verify the input format
     */
    formats = XpGetDocFmtListAttr( pCon, XPPrinterAttr,
				  (pPriv->isRaw) ?
				  xpoid_att_xp_raw_formats_supported :
				  xpoid_att_xp_embedded_formats_supported, 
				  NULL );
    f = XpOidDocFmtNew( pFmt );
    if( !XpOidDocFmtListHasFmt( formats, f ) )
      {
	  XpOidDocFmtListDelete( formats );
	  XpOidDocFmtDelete( f );
	  return BadMatch;
      }
    XpOidDocFmtListDelete( formats );
    
    if( !(pPriv->isRaw) )
      {
	  if( !strcmp( f->format, "PCL" ) )
	    type = DOC_PCL;
	  else if( !strcmp( f->format, "HPGL" ) )
	    type = DOC_HPGL;
	  else
	    {
		XpOidDocFmtDelete( f );
		return BadMatch;
	    }
	  
	  switch( type )
	    {
	      case DOC_HPGL:
		/*
		 * Move the picture frame to the appropriate place on the page,
		 * then assume that the embedded code will scale it properly.
		 */
		sprintf( t, "\033&l0E\033*p%dx%dY", 
			pDraw->x - 75,
			pDraw->y );
		SEND_PCL( pPriv->pPageFile, t );
		
		sprintf( t, "\033*c%dx%dY\033*coT",
			(int)( pDraw->width / 300.0 * 720.0 ),
			(int)( pDraw->height / 300.0 * 720.0 ) );
		SEND_PCL( pPriv->pPageFile, t );
		break;
	    }
      }
    
    
    /*
     * Send the data down the pipe
     */
    SEND_PCL_COUNT( pPriv->pPageFile, pData, len_data );
    
    /*
     * If it's not a raw document, clean up the embedding
     */
    if( !(pPriv->isRaw) )
      switch( type )
	{
	  case DOC_HPGL:
	    /*
	     * Reset the picture frame
	     */
	    XpGetReproductionArea( pCon, &repro );
	    
	    sprintf( t, "\033&l0E\033*p%dx%dY", repro.x - 75, repro.y );
	    SEND_PCL( pPriv->pPageFile, t );
	    
	    sprintf( t, "\033*c%dx%dY\033*c0T",
		    (int)(repro.width / 300.0 * 720.0),
		    (int)(repro.height / 300.0 * 720.0) );
	    SEND_PCL( pPriv->pPageFile, t );
	    
	    sprintf( t, "\033%%0BSC%d,%d,%d,%d;\033%%0A", repro.x, repro.x +
		    repro.width, repro.y + repro.height, repro.y );
	    SEND_PCL( pPriv->pPageFile, t );
	    break;
	}
    
    XpOidDocFmtDelete( f );
    return Success;
}

/*
 * 
 * PclGetDocumentData()
 *
 * This function allows the driver to send the generated PCL back to
 * the client.
 *
 * XXX This function is barely spec'ed, much less implemented!
 */

int
PclGetDocumentData(
     XpContextPtr pCon,
     ClientPtr client,
     int maxBufferSize)
{
    PclContextPrivPtr pPriv = (PclContextPrivPtr)
      pCon->devPrivates[PclContextPrivateIndex].ptr;
    
    pPriv->getDocClient = client;
    pPriv->getDocBufSize = maxBufferSize;
    
    return Success;
}