PclGC.c 23.3 KB
Newer Older
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
/* $Xorg: PclGC.c,v 1.4 2000/08/17 19:48:08 cpqbld Exp $ */
/*******************************************************************
**
**    *********************************************************
**    *
**    *  File:		PclGC.c
**    *
**    *  Contents:
**    *                 Graphics Context handling for the PCL driver
**    *
**    *  Created:	10/11/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.
*/
Egbert Eich's avatar
Egbert Eich committed
47
/* $XFree86: xc/programs/Xserver/Xprint/pcl/PclGC.c,v 1.9 2001/01/19 18:34:28 dawes Exp $ */
48

49 50 51 52
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

53 54 55 56 57 58
#include "gcstruct.h"

#include "Pcl.h"
#include "pixmapstr.h"
#include "colormapst.h"
#include "windowstr.h"
59
#include "fb.h"
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
#include "scrnintstr.h"
#include "resource.h"

static GCOps PclGCOps = 
{
    PclFillSpans,
    PclSetSpans,
    PclPutImage,
    PclCopyArea,
    PclCopyPlane,
    PclPolyPoint,
    PclPolyLine,
    PclPolySegment,
    PclPolyRectangle,
    PclPolyArc,
    PclFillPolygon,
    PclPolyFillRect,
    PclPolyFillArc,
    PclPolyText8,
    PclPolyText16,
    PclImageText8,
    PclImageText16,
    PclImageGlyphBlt,
    PclPolyGlyphBlt,
    PclPushPixels
}
;


static GCFuncs PclGCFuncs = 
{
    PclValidateGC,
    miChangeGC,
    miCopyGC,
    PclDestroyGC,
    miChangeClip,
    miDestroyClip,
    miCopyClip,
}
;

Bool
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
102
PclCreateGC(GCPtr pGC)
103
{
104 105
    if (fbCreateGC(pGC) == FALSE)
        return FALSE;
106 107 108 109 110 111 112 113 114 115 116 117 118

    pGC->clientClip = NULL;
    pGC->clientClipType = CT_NONE;

    pGC->ops = &PclGCOps;
    pGC->funcs = &PclGCFuncs;

    return TRUE;
}

void
PclDestroyGC(GCPtr pGC)
{
119
    /* fb doesn't specialize DestroyGC */
120 121 122 123 124
    miDestroyGC( pGC );
}


int
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
125 126 127 128 129
PclGetDrawablePrivateStuff(
     DrawablePtr pDrawable,
     GC *gc,
     unsigned long *valid,
     FILE **file)
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
{
    XpContextPtr pCon;
    PclContextPrivPtr cPriv;
    
    switch( pDrawable->type )
      {
	case DRAWABLE_PIXMAP:
	  /*
	   * If we ever get here, something is wrong.
	   */
	  return FALSE;

	case DRAWABLE_WINDOW:
	  pCon = PclGetContextFromWindow( (WindowPtr)pDrawable );

	  if( pCon == NULL )
	    return FALSE;
	  else
	    {
		cPriv = pCon->devPrivates[PclContextPrivateIndex].ptr;
		*gc = cPriv->lastGC;
		*valid = cPriv->validGC;
		*file = cPriv->pPageFile;
		return TRUE;
	    }
	  
	default:
	  return FALSE;
      }
}

void
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
162 163 164
PclSetDrawablePrivateGC(
     DrawablePtr pDrawable,
     GC gc)
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
{
    PixmapPtr pix;
    XpContextPtr pCon;
    PclPixmapPrivPtr pixPriv;
    PclContextPrivPtr pPriv;
    int i;
    
    switch( pDrawable->type )
      {
	case DRAWABLE_PIXMAP:
	  pix = (PixmapPtr)pDrawable;
	  pixPriv = pix->devPrivates[PclPixmapPrivateIndex].ptr;
	  
	  pixPriv->lastGC = gc;
	  pixPriv->validGC = 1;
	  break;

	case DRAWABLE_WINDOW:
	  pCon = PclGetContextFromWindow( (WindowPtr)pDrawable );
	  pPriv = ((PclContextPrivPtr)
		   (pCon->devPrivates[PclContextPrivateIndex].ptr));
	  
	  pPriv->validGC = 1;
	  pPriv->lastGC = gc;

	  /*
	   * Store the dash list separately, to avoid having it freed
	   * out from under us.
	   */
	  if( pPriv->dash != NULL )
	    xfree( pPriv->dash );
	  if( gc.numInDashList != 0 )
	    {
		pPriv->dash = (unsigned char *)xalloc( sizeof( unsigned char ) 
						      * gc.numInDashList );
		for( i = 0; i < gc.numInDashList; i++ )
		  pPriv->dash[i] = gc.dash[i];
	    }
	  else
	    pPriv->dash = NULL;
	  

	  /*
	   * Store the dash list separately, to avoid having it freed
	   * out from under us.
	   */
	  if( pPriv->dash != NULL )
	    xfree( pPriv->dash );
	  if( gc.numInDashList != 0 )
	    {
		pPriv->dash = (unsigned char *)xalloc( sizeof( unsigned char ) 
						      * gc.numInDashList );
		for( i = 0; i < gc.numInDashList; i++ )
		  pPriv->dash[i] = gc.dash[i];
	    }
	  else
	    pPriv->dash = NULL;
	  
	  break;
      }
}

static void
PclSendPattern(char *bits,
	       int sz,
	       int depth,
	       int h,
	       int w,
	       int patNum,
	       FILE *outFile)
{
    char t[80], *row, *mod;
    int w2;
    int i, j;
    
    SEND_PCL( outFile, "\033%0A" );

    if( depth == 1 )
      {
	  /* Each row must be word-aligned */
	  w2 = ( w / 8 ) + ( ( w%8 ) ? 1 : 0 );
/*
	  if( w2 % 2 )
	    w2++;
*/
	  
	  sprintf( t, "\033*c%dg%dW", patNum, h * w2 + 8 );
	  SEND_PCL( outFile, t );
	  
	  sprintf( t, "%c%c%c%c%c%c%c%c", 0, 0, 1, 0, h>>8, h&0xff, w>>8,
		  w&0xff );
	  SEND_PCL_COUNT( outFile, t, 8 );
	  
	  for( row = bits, i = 0; i < h; i++, row += BitmapBytePad( w ) )
	    SEND_PCL_COUNT( outFile, row, w2 );
      }
    else if( depth == 8 )
      {
	  w2 = ( w % 2 ) ? w + 1 : w;
	  
	  sprintf( t, "\033*c%dg%dW", patNum, h * w2 + 8 );
	  SEND_PCL( outFile, t );
	  
	  sprintf( t, "%c%c%c%c%c%c%c%c", 1, 0, 8, 0, h>>8, h&0xff,
		  w>>8, w&0xff );
	  SEND_PCL_COUNT( outFile, t, 8 );
	  
	  for( row = bits, i = 0; i < h; i++, 
	      row += PixmapBytePad( w, 8 ) )
	    SEND_PCL_COUNT( outFile, row, w2 );
      }
    else
      {
	  w2 = ( w % 2 ) ? w + 1 : w;
	  
	  sprintf( t, "\033*c%dg%dW", patNum, h * w2 + 8 );
	  SEND_PCL( outFile, t );
	  
	  sprintf( t, "%c%c%c%c%c%c%c%c", 1, 0, 8, 0, h>>8, h&0xff,
		  w>>8, w&0xff );
	  SEND_PCL_COUNT( outFile, t, 8 );
	  
	  mod = (char *)xalloc( w2 );
	  
	  for( row = bits, i = 0; i < h; i++, 
	      row += PixmapBytePad( w, 24 ) )
	    {
		char r, g, b;
		for( j = 0; j < w2; j++ ) {
		  r = ((row[j*4+1] >> 5) & 0x7) << 5;
		  g = ((row[j*4+2] >> 5) & 0x7) << 2;
		  b = ((row[j*4+3] >> 6) & 0x3);
		  mod[j] = r | g | b;
		}
		SEND_PCL_COUNT( outFile, mod, w2 );
	    }
	  
	  xfree( mod );
      }
    
    SEND_PCL( outFile, "\033%0B" );
}

int
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
309 310 311 312
PclUpdateDrawableGC(
     GCPtr pGC,
     DrawablePtr pDrawable,
     FILE **outFile)
313
{
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
314
    Mask changeMask = 0;
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
    GC dGC;
    unsigned long valid;
    int i;
    XpContextPtr pCon;
    PclContextPrivPtr cPriv;
    PclGCPrivPtr gcPriv = (PclGCPrivPtr)
      (pGC->devPrivates[PclGCPrivateIndex].ptr);
    
    if( !PclGetDrawablePrivateStuff( pDrawable, &dGC, &valid, outFile ) )
      return FALSE;

    pCon = PclGetContextFromWindow( (WindowPtr)pDrawable );
    cPriv = pCon->devPrivates[PclContextPrivateIndex].ptr;

    /*
     * Here's where we update the colormap.  Since there can be
     * different colormaps installed on each window, we need to check
     * before each drawing request that the correct palette is active in
     * the printer.  This is as good a place as any.
     */
    if( !PclUpdateColormap( pDrawable, pCon, pGC, *outFile ) )
      return FALSE;

    /*
     * If the drawable's last GC is NULL, this means that this is
     * the first time the drawable is being used.  Therefore, we need
     * to emit PCL for all the GC fields.
     */
    if( valid == 0 )
      changeMask = ~0;

    /*
     * If we have two different GC structures, there is no alternative
     * but to scan through them both to determine the changeMask.
     */
    else
      {
	  if( dGC.alu != pGC->alu )
	    changeMask |= GCFunction;
	  if( dGC.fgPixel != pGC->fgPixel )
	    changeMask |= GCForeground;
	  if( dGC.bgPixel != pGC->bgPixel )
	    changeMask |= GCBackground;
	  if( dGC.lineWidth != pGC->lineWidth )
	    changeMask |= GCLineWidth;
	  if( dGC.lineStyle != pGC->lineStyle )
	    changeMask |= GCLineStyle;
	  if( dGC.capStyle != pGC->capStyle )
	    changeMask |= GCCapStyle;
	  if( dGC.joinStyle != pGC->joinStyle )
	    changeMask |= GCJoinStyle;
	  if( dGC.fillStyle != pGC->fillStyle )
	    changeMask |= GCFillStyle;
	  if( dGC.tile.pixmap != pGC->tile.pixmap )
	    changeMask |= GCTile;
	  if( dGC.stipple != pGC->stipple )
	    changeMask |= GCStipple;
	  if( dGC.patOrg.x != pGC->patOrg.x )
	    changeMask |= GCTileStipXOrigin;
	  if( dGC.patOrg.y != pGC->patOrg.y )
	    changeMask |= GCTileStipYOrigin;
	  
	  if( dGC.numInDashList == pGC->numInDashList )
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
378
	  {
379 380 381 382 383 384
	    for( i = 0; i < dGC.numInDashList; i++ )
	      if( cPriv->dash[i] != pGC->dash[i] )
		{
		    changeMask |= GCDashList;
		    break;
		}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
	  else
	    changeMask |= GCDashList;
      }

    /*
     * Once the changeMask has been determined, we scan it and emit
     * the appropriate PCL code to set the drawing attributes.
     */

    /* Must be in HP-GL/2 mode to set attributes */
    SEND_PCL( *outFile, "\033%0B" );

    if( changeMask & GCFunction )
      {
#ifdef XP_PCL_COLOR

	  if( pGC->alu == GXclear )
	    SEND_PCL( *outFile, "SP0;" );
	  else
	    SEND_PCL( *outFile, "SP1;" );
#else
	  if( pGC->alu == GXclear )
	    SEND_PCL( *outFile, "SP0;" );
	  else
	    SEND_PCL( *outFile, "SP1;" );
#endif /* XP_PCL_COLOR */
      }
    
#if 0
    if( changeMask & GCFunction )
      {
	  int rop = -1;
	  char t[10];
	  
	  switch( pGC->alu )
	    {
	      case GXclear:
		rop = 1;
		break;
	      case GXand:
		rop = 136;
		break;
	      case GXandReverse:
		rop = 68;
		break;
	      case GXcopy:
		rop = 204;
		break;
	      case GXandInverted:
		rop = 34;
		break;
	      case GXnoop:
		rop = 170;
		break;
	      case GXxor:
		rop = 238;
		break;
	      case GXor:
		rop = 238;
		break;
	      case GXnor:
		rop = 17;
		break;
	      case GXequiv:
		rop = 153;
		break;
	      case GXinvert:
		rop = 85;
		break;
	      case GXorReverse:
		rop = 221;
		break;
	      case GXcopyInverted:
		rop = 51;
		break;
	      case GXorInverted:
		rop = 187;
		break;
	      case GXnand:
		rop = 119;
		break;
	      case GXset:
		rop = 0;
		break;
	    }
	  if( rop != -1 )
	    {
		sprintf( t, "MC1,%d;", rop );
		SEND_PCL( *outFile, t );
#endif

    if( changeMask & GCForeground )
      switch( pGC->fgPixel )
	{
	  case 1:
	    SEND_PCL( *outFile, "SP1;" );
	    break;
	  default:
	    SEND_PCL( *outFile, "SP0;" );
	    break;
	}
    
    if( changeMask & GCForeground )
      {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
490 491 492
#ifdef XP_PCL_COLOR
	  ColormapPtr cmap;
	  Colormap c;
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	  char t[40];

	  c = wColormap( ((WindowPtr)pDrawable) );
	  cmap = (ColormapPtr)LookupIDByType( c, RT_COLORMAP );

	  if( cmap->class == TrueColor )
	    {
	      if( pGC->fillStyle != FillTiled || pGC->tileIsPixel ) {
		unsigned short r, g, b;
	
		r = (pGC->fgPixel & cmap->pVisual->redMask)
						>> (cmap->pVisual->offsetRed );
		g = (pGC->fgPixel & cmap->pVisual->greenMask)
						>> (cmap->pVisual->offsetGreen);
		b = (pGC->fgPixel & cmap->pVisual->blueMask)
						>> (cmap->pVisual->offsetBlue);

		PclLookUp(cmap, cPriv, &r, &g, &b);
		sprintf( t, "\033%%0A\033*v%ua%ub%uc0I\033%%0B", r, g, b);
		SEND_PCL( *outFile, t );
	     }
	    }
	  else /* PseudoColor or StaticGray */
	    {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
517
		sprintf( t, "SP%ld;", (long) pGC->fgPixel );
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
		SEND_PCL( *outFile, t );
	    }
#else
	  ScreenPtr screen;
	  screen = pDrawable->pScreen;
	  if ( pGC->fgPixel == screen->whitePixel )
	    SEND_PCL( *outFile, "SP0;");
	  else
	    SEND_PCL( *outFile, "SP1;");
#endif /* XP_PCL_COLOR */
      }
    
    if( changeMask & GCJoinStyle )
      switch( pGC->joinStyle )
	{
	  case JoinMiter:
	    SEND_PCL( *outFile, "LA2,1;" );
	    break;
	  case JoinRound:
	    SEND_PCL( *outFile, "LA2,4;" );
	    break;
	  case JoinBevel:
	    SEND_PCL( *outFile, "LA2,5;" );
	    break;
	}
    
    if( changeMask & GCCapStyle )
      switch( pGC->capStyle )
	{
	  case CapNotLast:
	  case CapButt:
	    SEND_PCL( *outFile, "LA1,1;" );
	    break;
	  case CapRound:
	    SEND_PCL( *outFile, "LA1,4;" );
	    break;
	  case CapProjecting:
	    SEND_PCL( *outFile, "LA1,2;" );
	    break;
	}

    if( changeMask & GCLineWidth )
      {
	  float penWidth, pixelsPerMM;
	  ScreenPtr screen;
	  char temp[30];
	  
	  if( pGC->lineWidth == 0 || pGC->lineWidth == 1 )
	    /* A pen width of 0.0 mm gives a one-pixel-wide line */
	    penWidth = 0.0;
	  else
	    {
		screen = pDrawable->pScreen;
		pixelsPerMM = (float)screen->width / (float)screen->mmWidth;
		
		penWidth = pGC->lineWidth / pixelsPerMM;
	    }
	  sprintf( temp, "PW%g;", penWidth );
	  SEND_PCL( *outFile, temp );
      }
    
    if( changeMask & GCLineStyle )
      {
	  int i, num = pGC->numInDashList;
	  double total;
	  char t[30];
	  
	  switch( pGC->lineStyle )
	    {
	      case LineSolid:
		SEND_PCL( *outFile, "LT;" );
		break;
	      case LineOnOffDash:
		/*
		 * Calculate the pattern length of the dashes, in pixels,
		 * then convert to mm
		 */
		for( i = 0, total = 0.0; i < 20 && i < num; i++ )
		  total += pGC->dash[i];
		if( num % 2 )
		  for( i = num; i < 20 && i < num + num; i++ )
		    total += pGC->dash[i-num];

		total *= ( (double)pDrawable->pScreen->mmWidth /
			  (double)pDrawable->pScreen->width );

		sprintf( t, "LT8,%f,1;", total );
		SEND_PCL( *outFile, t );
		break;
	    }
      }
    

    if( changeMask & GCFillStyle )
      switch( pGC->fillStyle )
	{
	  case FillSolid:
	    SEND_PCL( *outFile, "FT1;TR0;CF;" );
	    break;
	  case FillTiled:
	    SEND_PCL( *outFile, "FT22,100;TR0;CF2,0;" );
	    break;
	  case FillOpaqueStippled:
	    SEND_PCL( *outFile, "FT22,101;TR0;CF2,0;" );
	    if( pGC->fgPixel != gcPriv->stippleFg ||
	       pGC->bgPixel != gcPriv->stippleBg )
	      changeMask |= GCStipple;
	    break;
	  case FillStippled:
	    SEND_PCL( *outFile, "FT22,102;TR1;CF2,0;" );
	    break;
	}

    if( changeMask & GCTile && !pGC->tileIsPixel )
      {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
633 634
	  char *bits;
	  int h, w, sz;
635 636 637 638

	  h = pGC->tile.pixmap->drawable.height;
	  w = pGC->tile.pixmap->drawable.width;

639 640 641 642 643 644
          sz = h * PixmapBytePad(w, pGC->tile.pixmap->drawable.depth);
          bits = (char *)xalloc(sz);
          fbGetImage(&(pGC->tile.pixmap->drawable), 0, 0, w, h, XYPixmap, ~0,
                     bits);
          PclSendPattern( bits, sz, 1, h, w, 100, *outFile );
	  xfree( bits );
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
      }

    if( changeMask & ( GCTileStipXOrigin | GCTileStipYOrigin ) )
      {
	  char t[30];
	  
	  sprintf( t, "AC%d,%d;", pGC->patOrg.x, pGC->patOrg.y );
	  SEND_PCL( *outFile, t );
      }

    /*
     * We have to resend the stipple pattern either when the stipple itself
     * changes, or if we're in FillOpaqueStippled mode and either the
     * foreground or the background color changes.
     */
    if( changeMask & GCStipple || 
       ( pGC->fillStyle == FillOpaqueStippled &&
	( pGC->fgPixel != gcPriv->stippleFg || 
	 pGC->bgPixel != gcPriv->stippleBg ) ) )
      {
	  int h, w, i, sz, w2;
	  char *bits, *row, t[30];
	  PixmapPtr scratchPix;
	  GCPtr scratchGC;
	  
	  if( pGC->stipple != NULL )
	    {
		SEND_PCL( *outFile, "\033%0A" );
		
		h = pGC->stipple->drawable.height;
		w = pGC->stipple->drawable.width;
		sz = h * BitmapBytePad( w );

		bits = (char *)xalloc( sz );
679
		fbGetImage( &(pGC->stipple->drawable), 0, 0, w, h, XYPixmap, ~0, bits );
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 709 710 711 712 713 714 715 716

		w2 = ( w / 8 ) + ( ( w%8 ) ? 1 : 0 );
		/*
		 * XXX The PCL docs say that I need to word-align each
		 * XXX row, but I get garbage when I do...
		 */
		/*
		if( w2 % 2 )
		  w2++;
		*/
                
		sprintf( t, "\033*c102g%dW", h * w2 + 8 );
		SEND_PCL( *outFile, t );
		
		sprintf( t, "%c%c%c%c%c%c%c%c", 0, 0, 1, 0, h>>8, h&0xff, w>>8,
			w&0xff );
		SEND_PCL_COUNT( *outFile, t, 8 );
		
		for( row = bits, i = 0; i < h; i++, row += BitmapBytePad( w ) )
		  SEND_PCL_COUNT( *outFile, row, w2 );

		SEND_PCL( *outFile, "\033%0B" );
		
		xfree( bits );

		/*
		 * Also do the opaque stipple, as a tile
		 */
		if( pGC->depth != 1 )
		  sz = h * PixmapBytePad( w, pGC->depth );
		bits = (char *)xalloc( sz );
		
		scratchPix = 
		  (*pGC->pScreen->CreatePixmap)( pGC->pScreen,
						w, h, pGC->depth );
		scratchGC = GetScratchGC( pGC->depth, pGC->pScreen );
		CopyGC( pGC, scratchGC, ~0L );
717 718 719 720 721 722

                fbValidateGC(scratchGC, ~0L, (DrawablePtr)scratchPix);
		fbCopyPlane(&(pGC->stipple->drawable), (DrawablePtr)scratchPix,
                            scratchGC, 0, 0, w, h, 0, 0, 1);
		fbGetImage(&(scratchPix->drawable), 0, 0, w, h, XYPixmap, ~0,
			   bits);
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
		PclSendPattern( bits, sz, pGC->depth, h, w, 101, *outFile );
		FreeScratchGC( scratchGC );
		(*pGC->pScreen->DestroyPixmap)( scratchPix );
		xfree( bits );
	    }
      }

    if( changeMask & ( GCTileStipXOrigin | GCTileStipYOrigin ) )
      {
	  char t[30];
	  
	  sprintf( t, "AC%d,%d;", pGC->patOrg.x, pGC->patOrg.y );
	  SEND_PCL( *outFile, t );
      }
    
    if( changeMask & GCDashList )
      {
	  int num = pGC->numInDashList;
	  double total;
	  char dashes[20];
	  char t[100], t2[20];

	  /* Make up the doubled dash list, if necessary */
	  for( i = 0; i < 20 && i < num; i++ )
	    dashes[i] = pGC->dash[i];
	  
	  if( num % 2 )
	    {
		for( i = num; i < 20 && i < num + num; i++ )
		  dashes[i] = dashes[i-num];
		if( ( num *= 2 ) > 20 )
		  num = 20;
	    }
	  
	  /* Add up dash lengths to get percentage */
	  for( i = 0, total = 0; i < num; i++ )
	    total += dashes[i];
	  
	  /* Build up the HP-GL/2 for the dash list */
	  strcpy( t, "UL8" );
	  for( i = 0; i < num; i++ )
	    {
		sprintf( t2, ",%d", 
			(int)( ( (double)dashes[i] / total * 100.0 ) + 0.5 ) );
		strcat( t, t2 );
	    }
	  strcat( t, ";" );
	  SEND_PCL( *outFile, t );
      }
   

    /* Go back to PCL mode */
    SEND_PCL( *outFile, "\033%0A" );
    
    /*
     * Update the drawable's private information, which includes
     * erasing the drawable's private changeMask, since all the
     * changes have been made.
     */
    if( changeMask )
      PclSetDrawablePrivateGC( pDrawable, *pGC );
    
    return TRUE;
}

/*
 * PclComputeCompositeClip()
 *
 * I'd like to use the miComputeCompositeClip function, but it sticks
 * things into the mi GC privates, where the PCL driver can't get at
 * it.  So, rather than mess around with the mi code, I ripped it out
 * and made the appropriate changes here.
 */


void
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
799 800 801
PclComputeCompositeClip(
    GCPtr           pGC,
    DrawablePtr     pDrawable)
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
{
    if (pDrawable->type == DRAWABLE_WINDOW)
    {
	WindowPtr       pWin = (WindowPtr) pDrawable;
	RegionPtr       pregWin;
	Bool            freeTmpClip, freeCompClip;

	if (pGC->subWindowMode == IncludeInferiors)
	{
	    pregWin = NotClippedByChildren(pWin);
	    freeTmpClip = TRUE;
	}
	else
	{
	    pregWin = &pWin->clipList;
	    freeTmpClip = FALSE;
	}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
819
	freeCompClip = pGC->freeCompClip;
820 821 822 823 824 825 826 827 828 829 830

	/*
	 * if there is no client clip, we can get by with just keeping the
	 * pointer we got, and remembering whether or not should destroy (or
	 * maybe re-use) it later.  this way, we avoid unnecessary copying of
	 * regions.  (this wins especially if many clients clip by children
	 * and have no client clip.)
	 */
	if (pGC->clientClipType == CT_NONE)
	{
	    if (freeCompClip)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
831 832 833
		REGION_DESTROY(pGC->pScreen, pGC->pCompositeClip);
	    pGC->pCompositeClip = pregWin;
	    pGC->freeCompClip = freeTmpClip;
834 835 836 837 838 839 840 841 842 843 844 845
	}
	else
	{
	    /*
	     * we need one 'real' region to put into the composite clip. if
	     * pregWin the current composite clip are real, we can get rid of
	     * one. if pregWin is real and the current composite clip isn't,
	     * use pregWin for the composite clip. if the current composite
	     * clip is real and pregWin isn't, use the current composite
	     * clip. if neither is real, create a new region.
	     */

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
846
	    REGION_TRANSLATE(pGC->pScreen, pGC->clientClip,
847 848 849 850 851
					 pDrawable->x + pGC->clipOrg.x,
					 pDrawable->y + pGC->clipOrg.y);

	    if (freeCompClip)
	    {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
852
		REGION_INTERSECT(pGC->pScreen, pGC->pCompositeClip,
853 854
					    pregWin, pGC->clientClip);
		if (freeTmpClip)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
855
		    REGION_DESTROY(pGC->pScreen, pregWin);
856 857 858
	    }
	    else if (freeTmpClip)
	    {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
859 860 861
		REGION_INTERSECT(pGC->pScreen, pregWin, pregWin,
				 pGC->clientClip);
		pGC->pCompositeClip = pregWin;
862 863 864
	    }
	    else
	    {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
865 866
		pGC->pCompositeClip = REGION_CREATE(pGC->pScreen, NullBox, 0);
		REGION_INTERSECT(pGC->pScreen, pGC->pCompositeClip,
867 868
				       pregWin, pGC->clientClip);
	    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
869 870
	    pGC->freeCompClip = TRUE;
	    REGION_TRANSLATE(pGC->pScreen, pGC->clientClip,
871 872 873 874 875 876 877 878 879 880 881 882 883 884
					 -(pDrawable->x + pGC->clipOrg.x),
					 -(pDrawable->y + pGC->clipOrg.y));
	}
    }	/* end of composite clip for a window */
    else
    {
	BoxRec          pixbounds;

	/* XXX should we translate by drawable.x/y here ? */
	pixbounds.x1 = 0;
	pixbounds.y1 = 0;
	pixbounds.x2 = pDrawable->width;
	pixbounds.y2 = pDrawable->height;

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
885
	if (pGC->freeCompClip)
886
	{
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
887
	    REGION_RESET(pGC->pScreen, pGC->pCompositeClip, &pixbounds);
888 889 890
	}
	else
	{
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
891 892
	    pGC->freeCompClip = TRUE;
	    pGC->pCompositeClip = REGION_CREATE(pGC->pScreen, &pixbounds, 1);
893 894 895 896
	}

	if (pGC->clientClipType == CT_REGION)
	{
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
897
	    REGION_TRANSLATE(pGC->pScreen, pGC->pCompositeClip,
898
					 -pGC->clipOrg.x, -pGC->clipOrg.y);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
899 900 901
	    REGION_INTERSECT(pGC->pScreen, pGC->pCompositeClip,
				pGC->pCompositeClip, pGC->clientClip);
	    REGION_TRANSLATE(pGC->pScreen, pGC->pCompositeClip,
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
					 pGC->clipOrg.x, pGC->clipOrg.y);
	}
    }	/* end of composite clip for pixmap */
} 

/*
 * PclValidateGC()
 *
 * Unlike many screen GCValidate routines, this function should not need
 * to mess with setting the drawing functions.  Different drawing
 * functions are usually needed to optimize such things as drawing
 * wide or dashed lines; this functionality will be handled primarily
 * by the printer itself, while the necessary PCL code to set the
 * attributes will be done in PclUpdateDrawableGC().
 */

/*ARGSUSED*/
void
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
920 921 922 923
PclValidateGC(
     GCPtr pGC,
     unsigned long changes,
     DrawablePtr pDrawable)
924 925 926 927 928 929 930
{
    /*
     * Pixmaps should be handled by their respective validation
     * functions.
     */
    if( pDrawable->type == DRAWABLE_PIXMAP )
      {
931
          fbValidateGC(pGC, ~0, pDrawable);
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
	  return;
      }

    /*
     * Reset the drawing operations
     */
    pGC->ops = &PclGCOps;
    
    /*
     * Validate the information, and correct it if necessary.
     */

    /*
     * If necessary, compute the composite clip region.  (Code ripped
     * from migc.c)
     */
    if ((changes & (GCClipXOrigin|GCClipYOrigin|GCClipMask|GCSubwindowMode)) ||
	(pDrawable->serialNumber != (pGC->serialNumber & DRAWABLE_SERIAL_BITS))
       )
    {
	PclComputeCompositeClip(pGC, pDrawable);
    }

    /*
     * PCL does not directly support the DoubleDash line style, nor is
     * there an easy way to simulate it, so we'll just change it to a
     * LineOnOffDash, which is supported by PCL.
     */
    if( ( changes & GCLineStyle ) && ( pGC->lineStyle == LineDoubleDash ) )
      pGC->lineStyle = LineOnOffDash;
    
    /*
     * Update the drawable's changeMask to reflect the changes made to the GC.
     */
/*
    PclSetDrawablePrivateGC( pDrawable, *pGC, changes );
*/
}