graphics.c 23.4 KB
Newer Older
1 2 3 4
/*
 * Enhanced MetaFile driver graphics functions
 *
 * Copyright 1999 Huw D M Davies
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 20
 */

21 22 23
#include "config.h"
#include "wine/port.h"

24
#include <stdlib.h>
25 26
#include <string.h>

27
#include "gdi.h"
28
#include "enhmfdrv/enhmetafiledrv.h"
29
#include "wine/debug.h"
30

31
WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
32 33

/**********************************************************************
34
 *	     EMFDRV_MoveTo
35 36
 */
BOOL
37
EMFDRV_MoveTo(PHYSDEV dev, INT x, INT y)
38 39 40 41 42 43 44 45
{
    EMRMOVETOEX emr;

    emr.emr.iType = EMR_MOVETOEX;
    emr.emr.nSize = sizeof(emr);
    emr.ptl.x = x;
    emr.ptl.y = y;

46
    return EMFDRV_WriteRecord( dev, &emr.emr );
47 48 49 50 51 52
}

/***********************************************************************
 *           EMFDRV_LineTo
 */
BOOL
53
EMFDRV_LineTo( PHYSDEV dev, INT x, INT y )
54
{
55
    POINT pt;
56 57
    EMRLINETO emr;
    RECTL bounds;
58
    EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
59 60 61 62 63 64

    emr.emr.iType = EMR_LINETO;
    emr.emr.nSize = sizeof(emr);
    emr.ptl.x = x;
    emr.ptl.y = y;

65
    if(!EMFDRV_WriteRecord( dev, &emr.emr ))
66 67
    	return FALSE;

68 69 70 71 72 73
    GetCurrentPositionEx(physDev->hdc, &pt);

    bounds.left   = min(x, pt.x);
    bounds.top    = min(y, pt.y);
    bounds.right  = max(x, pt.x);
    bounds.bottom = max(y, pt.y);
74

75
    EMFDRV_UpdateBBox( dev, &bounds );
76 77 78 79 80 81 82 83 84

    return TRUE;
}


/***********************************************************************
 *           EMFDRV_ArcChordPie
 */
static BOOL
85
EMFDRV_ArcChordPie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
86 87 88 89 90 91 92
		    INT xstart, INT ystart, INT xend, INT yend, DWORD iType )
{
    INT temp, xCentre, yCentre, i;
    double angleStart, angleEnd;
    double xinterStart, yinterStart, xinterEnd, yinterEnd;
    EMRARC emr;
    RECTL bounds;
93
    EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
94 95 96 97 98 99

    if(left == right || top == bottom) return FALSE;

    if(left > right) {temp = left; left = right; right = temp;}
    if(top > bottom) {temp = top; top = bottom; bottom = temp;}

100
    if(GetGraphicsMode(physDev->hdc) == GM_COMPATIBLE) {
101 102 103
        right--;
	bottom--;
    }
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

    emr.emr.iType     = iType;
    emr.emr.nSize     = sizeof(emr);
    emr.rclBox.left   = left;
    emr.rclBox.top    = top;
    emr.rclBox.right  = right;
    emr.rclBox.bottom = bottom;
    emr.ptlStart.x    = xstart;
    emr.ptlStart.y    = ystart;
    emr.ptlEnd.x      = xend;
    emr.ptlEnd.x      = yend;


    /* Now calculate the BBox */
    xCentre = (left + right + 1) / 2;
    yCentre = (top + bottom + 1) / 2;

    xstart -= xCentre;
    ystart -= yCentre;
    xend   -= xCentre;
    yend   -= yCentre;

    /* invert y co-ords to get angle anti-clockwise from x-axis */
    angleStart = atan2( -(double)ystart, (double)xstart);
    angleEnd   = atan2( -(double)yend, (double)xend);

    /* These are the intercepts of the start/end lines with the arc */

    xinterStart = (right - left + 1)/2 * cos(angleStart) + xCentre;
    yinterStart = -(bottom - top + 1)/2 * sin(angleStart) + yCentre;
    xinterEnd   = (right - left + 1)/2 * cos(angleEnd) + xCentre;
    yinterEnd   = -(bottom - top + 1)/2 * sin(angleEnd) + yCentre;

    if(angleStart < 0) angleStart += 2 * M_PI;
    if(angleEnd < 0) angleEnd += 2 * M_PI;
    if(angleEnd < angleStart) angleEnd += 2 * M_PI;

141 142 143 144
    bounds.left   = min(xinterStart, xinterEnd);
    bounds.top    = min(yinterStart, yinterEnd);
    bounds.right  = max(xinterStart, xinterEnd);
    bounds.bottom = max(yinterStart, yinterEnd);
145

146 147 148 149 150 151 152 153 154
    for(i = 0; i <= 8; i++) {
        if(i * M_PI / 2 < angleStart) /* loop until we're past start */
	    continue;
	if(i * M_PI / 2 > angleEnd)   /* if we're past end we're finished */
	    break;

	/* the arc touches the rectangle at the start of quadrant i, so adjust
	   BBox to reflect this. */

155
	switch(i % 4) {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	case 0:
	    bounds.right = right;
	    break;
	case 1:
	    bounds.top = top;
	    break;
	case 2:
	    bounds.left = left;
	    break;
	case 3:
	    bounds.bottom = bottom;
	    break;
	}
    }

    /* If we're drawing a pie then make sure we include the centre */
    if(iType == EMR_PIE) {
        if(bounds.left > xCentre) bounds.left = xCentre;
	else if(bounds.right < xCentre) bounds.right = xCentre;
	if(bounds.top > yCentre) bounds.top = yCentre;
	else if(bounds.bottom < yCentre) bounds.right = yCentre;
    }
178
    if(!EMFDRV_WriteRecord( dev, &emr.emr ))
179
        return FALSE;
180
    EMFDRV_UpdateBBox( dev, &bounds );
181 182 183 184 185 186 187 188
    return TRUE;
}


/***********************************************************************
 *           EMFDRV_Arc
 */
BOOL
189
EMFDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
190 191
	    INT xstart, INT ystart, INT xend, INT yend )
{
192
    return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
193 194 195 196 197 198 199
			       xend, yend, EMR_ARC );
}

/***********************************************************************
 *           EMFDRV_Pie
 */
BOOL
200
EMFDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
201 202
	    INT xstart, INT ystart, INT xend, INT yend )
{
203
    return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
204 205 206 207 208 209 210 211
			       xend, yend, EMR_PIE );
}


/***********************************************************************
 *           EMFDRV_Chord
 */
BOOL
212
EMFDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
213 214
             INT xstart, INT ystart, INT xend, INT yend )
{
215
    return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
216 217 218 219 220 221 222
			       xend, yend, EMR_CHORD );
}

/***********************************************************************
 *           EMFDRV_Ellipse
 */
BOOL
223
EMFDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
224 225 226
{
    EMRELLIPSE emr;
    INT temp;
227
    EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
228

229
    TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
230 231 232 233 234 235

    if(left == right || top == bottom) return FALSE;

    if(left > right) {temp = left; left = right; right = temp;}
    if(top > bottom) {temp = top; top = bottom; bottom = temp;}

236
    if(GetGraphicsMode(physDev->hdc) == GM_COMPATIBLE) {
237 238 239
        right--;
	bottom--;
    }
240 241 242 243 244 245 246 247

    emr.emr.iType     = EMR_ELLIPSE;
    emr.emr.nSize     = sizeof(emr);
    emr.rclBox.left   = left;
    emr.rclBox.top    = top;
    emr.rclBox.right  = right;
    emr.rclBox.bottom = bottom;

248 249
    EMFDRV_UpdateBBox( dev, &emr.rclBox );
    return EMFDRV_WriteRecord( dev, &emr.emr );
250 251 252 253 254 255
}

/***********************************************************************
 *           EMFDRV_Rectangle
 */
BOOL
256
EMFDRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
257 258 259
{
    EMRRECTANGLE emr;
    INT temp;
260
    EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
261

262
    TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
263 264 265 266 267 268

    if(left == right || top == bottom) return FALSE;

    if(left > right) {temp = left; left = right; right = temp;}
    if(top > bottom) {temp = top; top = bottom; bottom = temp;}

269
    if(GetGraphicsMode(physDev->hdc) == GM_COMPATIBLE) {
270 271 272
        right--;
	bottom--;
    }
273 274 275 276 277 278 279 280

    emr.emr.iType     = EMR_RECTANGLE;
    emr.emr.nSize     = sizeof(emr);
    emr.rclBox.left   = left;
    emr.rclBox.top    = top;
    emr.rclBox.right  = right;
    emr.rclBox.bottom = bottom;

281 282
    EMFDRV_UpdateBBox( dev, &emr.rclBox );
    return EMFDRV_WriteRecord( dev, &emr.emr );
283 284 285 286 287
}

/***********************************************************************
 *           EMFDRV_RoundRect
 */
288
BOOL
289
EMFDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
290 291 292 293
		  INT bottom, INT ell_width, INT ell_height )
{
    EMRROUNDRECT emr;
    INT temp;
294
    EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
295 296 297 298 299 300

    if(left == right || top == bottom) return FALSE;

    if(left > right) {temp = left; left = right; right = temp;}
    if(top > bottom) {temp = top; top = bottom; bottom = temp;}

301
    if(GetGraphicsMode(physDev->hdc) == GM_COMPATIBLE) {
302 303 304
        right--;
	bottom--;
    }
305 306 307 308 309 310 311 312 313 314

    emr.emr.iType     = EMR_ROUNDRECT;
    emr.emr.nSize     = sizeof(emr);
    emr.rclBox.left   = left;
    emr.rclBox.top    = top;
    emr.rclBox.right  = right;
    emr.rclBox.bottom = bottom;
    emr.szlCorner.cx  = ell_width;
    emr.szlCorner.cy  = ell_height;

315 316
    EMFDRV_UpdateBBox( dev, &emr.rclBox );
    return EMFDRV_WriteRecord( dev, &emr.emr );
317 318 319 320 321 322
}

/***********************************************************************
 *           EMFDRV_SetPixel
 */
COLORREF
323
EMFDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
324
{
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    EMRSETPIXELV emr;

    emr.emr.iType  = EMR_SETPIXELV;
    emr.emr.nSize  = sizeof(emr);
    emr.ptlPixel.x = x;
    emr.ptlPixel.y = y;
    emr.crColor = color;

    if (EMFDRV_WriteRecord( dev, &emr.emr )) {
        RECTL bounds;
        bounds.left = bounds.right = x;
        bounds.top = bounds.bottom = y;
        EMFDRV_UpdateBBox( dev, &bounds );
        return color;
    }
    return -1;
341 342 343 344 345 346 347 348
}

/**********************************************************************
 *          EMFDRV_Polylinegon
 *
 * Helper for EMFDRV_Poly{line|gon}
 */
static BOOL
349
EMFDRV_Polylinegon( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
350 351 352 353 354 355 356 357
{
    EMRPOLYLINE *emr;
    DWORD size;
    INT i;
    BOOL ret;

    size = sizeof(EMRPOLYLINE) + sizeof(POINTL) * (count - 1);

358
    emr = HeapAlloc( GetProcessHeap(), 0, size );
359 360
    emr->emr.iType = iType;
    emr->emr.nSize = size;
361

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    emr->rclBounds.left = emr->rclBounds.right = pt[0].x;
    emr->rclBounds.top = emr->rclBounds.bottom = pt[0].y;

    for(i = 1; i < count; i++) {
        if(pt[i].x < emr->rclBounds.left)
	    emr->rclBounds.left = pt[i].x;
	else if(pt[i].x > emr->rclBounds.right)
	    emr->rclBounds.right = pt[i].x;
	if(pt[i].y < emr->rclBounds.top)
	    emr->rclBounds.top = pt[i].y;
	else if(pt[i].y > emr->rclBounds.bottom)
	    emr->rclBounds.bottom = pt[i].y;
    }

    emr->cptl = count;
377
    memcpy(emr->aptl, pt, count * sizeof(POINTL));
378

379
    ret = EMFDRV_WriteRecord( dev, &emr->emr );
380
    if(ret)
381
        EMFDRV_UpdateBBox( dev, &emr->rclBounds );
382
    HeapFree( GetProcessHeap(), 0, emr );
383 384 385 386
    return ret;
}


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
/**********************************************************************
 *          EMFDRV_Polylinegon16
 *
 * Helper for EMFDRV_Poly{line|gon}
 *
 *  This is not a legacy function!
 *  We are using SHORT integers to save space.
 */
static BOOL
EMFDRV_Polylinegon16( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
{
    EMRPOLYLINE16 *emr;
    DWORD size;
    INT i;
    BOOL ret;

    /* check whether all points fit in the SHORT int POINT structure */
    for(i = 0; i < count; i++) {
        if( ((pt[i].x+0x8000) & ~0xffff ) || 
            ((pt[i].y+0x8000) & ~0xffff ) )
            return FALSE;
    }

    size = sizeof(EMRPOLYLINE16) + sizeof(POINTS) * (count - 1);

    emr = HeapAlloc( GetProcessHeap(), 0, size );
    emr->emr.iType = iType;
    emr->emr.nSize = size;

    emr->rclBounds.left = emr->rclBounds.right = pt[0].x;
    emr->rclBounds.top = emr->rclBounds.bottom = pt[0].y;

    for(i = 1; i < count; i++) {
        if(pt[i].x < emr->rclBounds.left)
	    emr->rclBounds.left = pt[i].x;
	else if(pt[i].x > emr->rclBounds.right)
	    emr->rclBounds.right = pt[i].x;
	if(pt[i].y < emr->rclBounds.top)
	    emr->rclBounds.top = pt[i].y;
	else if(pt[i].y > emr->rclBounds.bottom)
	    emr->rclBounds.bottom = pt[i].y;
    }

    emr->cpts = count;
    for(i = 0; i < count; i++ ) {
        emr->apts[i].x = pt[i].x;
        emr->apts[i].y = pt[i].y;
    }

    ret = EMFDRV_WriteRecord( dev, &emr->emr );
    if(ret)
        EMFDRV_UpdateBBox( dev, &emr->rclBounds );
    HeapFree( GetProcessHeap(), 0, emr );
    return ret;
}


444 445 446 447
/**********************************************************************
 *          EMFDRV_Polyline
 */
BOOL
448
EMFDRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
449
{
450 451
    if( EMFDRV_Polylinegon16( dev, pt, count, EMR_POLYLINE16 ) )
        return TRUE;
452
    return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINE );
453 454 455 456 457 458
}

/**********************************************************************
 *          EMFDRV_Polygon
 */
BOOL
459
EMFDRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
460 461
{
    if(count < 2) return FALSE;
462 463
    if( EMFDRV_Polylinegon16( dev, pt, count, EMR_POLYGON16 ) )
        return TRUE;
464
    return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYGON );
465 466 467 468 469 470 471 472
}


/**********************************************************************
 *          EMFDRV_PolyPolylinegon
 *
 * Helper for EMFDRV_PolyPoly{line|gon}
 */
473
static BOOL
474
EMFDRV_PolyPolylinegon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys,
475 476 477
			DWORD iType)
{
    EMRPOLYPOLYLINE *emr;
478 479
    DWORD cptl = 0, poly, size;
    INT point;
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
    RECTL bounds;
    const POINT *pts;
    BOOL ret;

    bounds.left = bounds.right = pt[0].x;
    bounds.top = bounds.bottom = pt[0].y;

    pts = pt;
    for(poly = 0; poly < polys; poly++) {
        cptl += counts[poly];
	for(point = 0; point < counts[poly]; point++) {
	    if(bounds.left > pts->x) bounds.left = pts->x;
	    else if(bounds.right < pts->x) bounds.right = pts->x;
	    if(bounds.top > pts->y) bounds.top = pts->y;
	    else if(bounds.bottom < pts->y) bounds.bottom = pts->y;
	    pts++;
	}
    }
498 499

    size = sizeof(EMRPOLYPOLYLINE) + (polys - 1) * sizeof(DWORD) +
500 501
      (cptl - 1) * sizeof(POINTL);

502
    emr = HeapAlloc( GetProcessHeap(), 0, size );
503 504 505 506 507 508 509

    emr->emr.iType = iType;
    emr->emr.nSize = size;
    emr->rclBounds = bounds;
    emr->nPolys = polys;
    emr->cptl = cptl;
    memcpy(emr->aPolyCounts, counts, polys * sizeof(DWORD));
510
    memcpy(emr->aPolyCounts + polys, pt, cptl * sizeof(POINTL));
511
    ret = EMFDRV_WriteRecord( dev, &emr->emr );
512
    if(ret)
513
        EMFDRV_UpdateBBox( dev, &emr->rclBounds );
514
    HeapFree( GetProcessHeap(), 0, emr );
515 516 517 518 519 520
    return ret;
}

/**********************************************************************
 *          EMFDRV_PolyPolyline
 */
521
BOOL
522
EMFDRV_PolyPolyline(PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polys)
523
{
Eric Pouech's avatar
Eric Pouech committed
524
    return EMFDRV_PolyPolylinegon( dev, pt, (const INT *)counts, polys,
525 526 527 528 529 530
				   EMR_POLYPOLYLINE );
}

/**********************************************************************
 *          EMFDRV_PolyPolygon
 */
531
BOOL
532
EMFDRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys )
533
{
534
    return EMFDRV_PolyPolylinegon( dev, pt, counts, polys, EMR_POLYPOLYGON );
535 536 537 538 539 540
}


/**********************************************************************
 *          EMFDRV_ExtFloodFill
 */
541
BOOL
542
EMFDRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
543 544 545 546 547 548 549 550 551 552
{
    EMREXTFLOODFILL emr;

    emr.emr.iType = EMR_EXTFLOODFILL;
    emr.emr.nSize = sizeof(emr);
    emr.ptlStart.x = x;
    emr.ptlStart.y = y;
    emr.crColor = color;
    emr.iMode = fillType;

553
    return EMFDRV_WriteRecord( dev, &emr.emr );
554 555 556 557 558 559
}


/*********************************************************************
 *          EMFDRV_FillRgn
 */
560
BOOL EMFDRV_FillRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush )
561 562 563 564 565
{
    EMRFILLRGN *emr;
    DWORD size, rgnsize, index;
    BOOL ret;

566
    index = EMFDRV_CreateBrushIndirect( dev, hbrush );
567 568 569
    if(!index) return FALSE;

    rgnsize = GetRegionData( hrgn, 0, NULL );
570
    size = rgnsize + offsetof(EMRFILLRGN,RgnData);
571
    emr = HeapAlloc( GetProcessHeap(), 0, size );
572 573 574 575 576 577 578 579 580 581 582

    GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );

    emr->emr.iType = EMR_FILLRGN;
    emr->emr.nSize = size;
    emr->rclBounds.left   = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
    emr->rclBounds.top    = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
    emr->rclBounds.right  = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
    emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
    emr->cbRgnData = rgnsize;
    emr->ihBrush = index;
583

584
    ret = EMFDRV_WriteRecord( dev, &emr->emr );
585
    if(ret)
586
        EMFDRV_UpdateBBox( dev, &emr->rclBounds );
587
    HeapFree( GetProcessHeap(), 0, emr );
588 589 590 591 592
    return ret;
}
/*********************************************************************
 *          EMFDRV_FrameRgn
 */
593
BOOL EMFDRV_FrameRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush, INT width, INT height )
594 595 596 597 598
{
    EMRFRAMERGN *emr;
    DWORD size, rgnsize, index;
    BOOL ret;

599
    index = EMFDRV_CreateBrushIndirect( dev, hbrush );
600 601 602
    if(!index) return FALSE;

    rgnsize = GetRegionData( hrgn, 0, NULL );
603
    size = rgnsize + offsetof(EMRFRAMERGN,RgnData);
604
    emr = HeapAlloc( GetProcessHeap(), 0, size );
605 606 607 608 609 610 611 612 613 614 615 616 617 618

    GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );

    emr->emr.iType = EMR_FRAMERGN;
    emr->emr.nSize = size;
    emr->rclBounds.left   = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
    emr->rclBounds.top    = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
    emr->rclBounds.right  = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
    emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
    emr->cbRgnData = rgnsize;
    emr->ihBrush = index;
    emr->szlStroke.cx = width;
    emr->szlStroke.cy = height;

619
    ret = EMFDRV_WriteRecord( dev, &emr->emr );
620
    if(ret)
621
        EMFDRV_UpdateBBox( dev, &emr->rclBounds );
622
    HeapFree( GetProcessHeap(), 0, emr );
623 624 625 626 627 628 629 630
    return ret;
}

/*********************************************************************
 *          EMFDRV_PaintInvertRgn
 *
 * Helper for EMFDRV_{Paint|Invert}Rgn
 */
631
static BOOL EMFDRV_PaintInvertRgn( PHYSDEV dev, HRGN hrgn, DWORD iType )
632 633 634 635 636 637 638
{
    EMRINVERTRGN *emr;
    DWORD size, rgnsize;
    BOOL ret;


    rgnsize = GetRegionData( hrgn, 0, NULL );
639
    size = rgnsize + offsetof(EMRINVERTRGN,RgnData);
640
    emr = HeapAlloc( GetProcessHeap(), 0, size );
641 642 643 644 645 646 647 648 649 650

    GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );

    emr->emr.iType = iType;
    emr->emr.nSize = size;
    emr->rclBounds.left   = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
    emr->rclBounds.top    = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
    emr->rclBounds.right  = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
    emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
    emr->cbRgnData = rgnsize;
651

652
    ret = EMFDRV_WriteRecord( dev, &emr->emr );
653
    if(ret)
654
        EMFDRV_UpdateBBox( dev, &emr->rclBounds );
655
    HeapFree( GetProcessHeap(), 0, emr );
656 657 658 659 660 661 662
    return ret;
}

/**********************************************************************
 *          EMFDRV_PaintRgn
 */
BOOL
663
EMFDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
664
{
665
    return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_PAINTRGN );
666 667 668 669 670 671
}

/**********************************************************************
 *          EMFDRV_InvertRgn
 */
BOOL
672
EMFDRV_InvertRgn( PHYSDEV dev, HRGN hrgn )
673
{
674
    return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_INVERTRGN );
675 676 677 678 679 680
}

/**********************************************************************
 *          EMFDRV_SetBkColor
 */
COLORREF
681
EMFDRV_SetBkColor( PHYSDEV dev, COLORREF color )
682 683 684 685 686 687 688
{
    EMRSETBKCOLOR emr;

    emr.emr.iType = EMR_SETBKCOLOR;
    emr.emr.nSize = sizeof(emr);
    emr.crColor = color;

689
    return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
690 691 692 693 694 695 696
}


/**********************************************************************
 *          EMFDRV_SetTextColor
 */
COLORREF
697
EMFDRV_SetTextColor( PHYSDEV dev, COLORREF color )
698 699 700 701 702 703 704
{
    EMRSETTEXTCOLOR emr;

    emr.emr.iType = EMR_SETTEXTCOLOR;
    emr.emr.nSize = sizeof(emr);
    emr.crColor = color;

705
    return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
706
}
707 708 709 710 711 712

/**********************************************************************
 *          EMFDRV_ExtTextOut
 */
BOOL EMFDRV_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags,
			const RECT *lprect, LPCWSTR str, UINT count,
713
			const INT *lpDx )
714 715 716 717 718
{
    EMREXTTEXTOUTW *pemr;
    DWORD nSize;
    BOOL ret;
    EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
719 720 721
    int textHeight = 0;
    int textWidth = 0;
    const UINT textAlign = GetTextAlign(physDev->hdc);
722

723
    nSize = sizeof(*pemr) + ((count+1) & ~1) * sizeof(WCHAR) + count * sizeof(INT);
724

725 726
    TRACE("%s %s count %d nSize = %ld\n", debugstr_wn(str, count),
           wine_dbgstr_rect(lprect), count, nSize);
727
    pemr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nSize);
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750

    pemr->emr.iType = EMR_EXTTEXTOUTW;
    pemr->emr.nSize = nSize;

    pemr->iGraphicsMode = GetGraphicsMode(physDev->hdc);
    pemr->exScale = pemr->eyScale = 1.0; /* FIXME */

    pemr->emrtext.ptlReference.x = x;
    pemr->emrtext.ptlReference.y = y;
    pemr->emrtext.nChars = count;
    pemr->emrtext.offString = sizeof(*pemr);
    memcpy((char*)pemr + pemr->emrtext.offString, str, count * sizeof(WCHAR));
    pemr->emrtext.fOptions = flags;
    if(!lprect) {
        pemr->emrtext.rcl.left = pemr->emrtext.rcl.top = 0;
        pemr->emrtext.rcl.right = pemr->emrtext.rcl.bottom = -1;
    } else {
        pemr->emrtext.rcl.left = lprect->left;
        pemr->emrtext.rcl.top = lprect->top;
        pemr->emrtext.rcl.right = lprect->right;
        pemr->emrtext.rcl.bottom = lprect->bottom;
    }

751 752 753 754 755 756 757 758 759 760 761 762
    pemr->emrtext.offDx = pemr->emrtext.offString + ((count+1) & ~1) * sizeof(WCHAR);
    if(lpDx) {
        UINT i;
        SIZE strSize;
        memcpy((char*)pemr + pemr->emrtext.offDx, lpDx, count * sizeof(INT));
        for (i = 0; i < count; i++) {
            textWidth += lpDx[i];
        }
        GetTextExtentPoint32W(physDev->hdc, str, count, &strSize);
        textHeight = strSize.cy;
    }
    else {
763 764
        UINT i;
        INT *dx = (INT *)((char*)pemr + pemr->emrtext.offDx);
765 766 767 768 769 770
        SIZE charSize;
        for (i = 0; i < count; i++) {
            GetTextExtentPoint32W(physDev->hdc, str + i, 1, &charSize);
            dx[i] = charSize.cx;
            textWidth += charSize.cx;
            textHeight = max(textHeight, charSize.cy);
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 799 800 801 802 803 804 805 806 807 808 809 810 811
    switch (textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER)) {
    case TA_CENTER: {
        pemr->rclBounds.left  = x - (textWidth / 2) - 1;
        pemr->rclBounds.right = x + (textWidth / 2) + 1;
        break;
    }
    case TA_RIGHT: {
        pemr->rclBounds.left  = x - textWidth - 1;
        pemr->rclBounds.right = x;
        break;
    }
    default: { /* TA_LEFT */
        pemr->rclBounds.left  = x;
        pemr->rclBounds.right = x + textWidth + 1;
    }
    }

    switch (textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE)) {
    case TA_BASELINE: {
        TEXTMETRICW tm;
        GetTextMetricsW(physDev->hdc, &tm);
        /* Play safe here... it's better to have a bounding box */
        /* that is too big than too small. */
        pemr->rclBounds.top    = y - textHeight - 1;
        pemr->rclBounds.bottom = y + tm.tmDescent + 1;
        break;
    }
    case TA_BOTTOM: {
        pemr->rclBounds.top    = y - textHeight - 1;
        pemr->rclBounds.bottom = y;
        break;
    }
    default: { /* TA_TOP */
        pemr->rclBounds.top    = y;
        pemr->rclBounds.bottom = y + textHeight + 1;
    }
    }

812 813 814 815 816 817
    ret = EMFDRV_WriteRecord( dev, &pemr->emr );
    if(ret)
        EMFDRV_UpdateBBox( dev, &pemr->rclBounds );
    HeapFree( GetProcessHeap(), 0, pemr );
    return ret;
}
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834

/**********************************************************************
 *          EMFDRV_SetArcDirection
 */
INT EMFDRV_SetArcDirection(PHYSDEV dev, INT arcDirection)
{
    EMRSETARCDIRECTION emr;

    emr.emr.iType = EMR_SETARCDIRECTION;
    emr.emr.nSize = sizeof(emr);
    emr.iArcDirection = arcDirection;

    EMFDRV_WriteRecord(dev, &emr.emr);

    /* We don't know the old arc direction and we don't care... */ 
    return 0;
}