Removed W->A from DEFWND_ImmIsUIMessageW.
[wine] / dlls / gdi / enhmfdrv / graphics.c
1 /*
2  * Enhanced MetaFile driver graphics functions
3  *
4  * Copyright 1999 Huw D M Davies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26
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     EMRMOVETOEX emr;
40
41     emr.emr.iType = EMR_MOVETOEX;
42     emr.emr.nSize = sizeof(emr);
43     emr.ptl.x = x;
44     emr.ptl.y = y;
45
46     return EMFDRV_WriteRecord( dev, &emr.emr );
47 }
48
49 /***********************************************************************
50  *           EMFDRV_LineTo
51  */
52 BOOL
53 EMFDRV_LineTo( PHYSDEV dev, INT x, INT y )
54 {
55     POINT pt;
56     EMRLINETO emr;
57     RECTL bounds;
58     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
59
60     emr.emr.iType = EMR_LINETO;
61     emr.emr.nSize = sizeof(emr);
62     emr.ptl.x = x;
63     emr.ptl.y = y;
64
65     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
66         return FALSE;
67
68     GetCurrentPositionEx(physDev->hdc, &pt);
69
70     bounds.left   = min(x, pt.x);
71     bounds.top    = min(y, pt.y);
72     bounds.right  = max(x, pt.x);
73     bounds.bottom = max(y, pt.y);
74
75     EMFDRV_UpdateBBox( dev, &bounds );
76
77     return TRUE;
78 }
79
80
81 /***********************************************************************
82  *           EMFDRV_ArcChordPie
83  */
84 static BOOL
85 EMFDRV_ArcChordPie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
86                     INT xstart, INT ystart, INT xend, INT yend, DWORD iType )
87 {
88     INT temp, xCentre, yCentre, i;
89     double angleStart, angleEnd;
90     double xinterStart, yinterStart, xinterEnd, yinterEnd;
91     EMRARC emr;
92     RECTL bounds;
93     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
94
95     if(left == right || top == bottom) return FALSE;
96
97     if(left > right) {temp = left; left = right; right = temp;}
98     if(top > bottom) {temp = top; top = bottom; bottom = temp;}
99
100     if(GetGraphicsMode(physDev->hdc) == GM_COMPATIBLE) {
101         right--;
102         bottom--;
103     }
104
105     emr.emr.iType     = iType;
106     emr.emr.nSize     = sizeof(emr);
107     emr.rclBox.left   = left;
108     emr.rclBox.top    = top;
109     emr.rclBox.right  = right;
110     emr.rclBox.bottom = bottom;
111     emr.ptlStart.x    = xstart;
112     emr.ptlStart.y    = ystart;
113     emr.ptlEnd.x      = xend;
114     emr.ptlEnd.x      = yend;
115
116
117     /* Now calculate the BBox */
118     xCentre = (left + right + 1) / 2;
119     yCentre = (top + bottom + 1) / 2;
120
121     xstart -= xCentre;
122     ystart -= yCentre;
123     xend   -= xCentre;
124     yend   -= yCentre;
125
126     /* invert y co-ords to get angle anti-clockwise from x-axis */
127     angleStart = atan2( -(double)ystart, (double)xstart);
128     angleEnd   = atan2( -(double)yend, (double)xend);
129
130     /* These are the intercepts of the start/end lines with the arc */
131
132     xinterStart = (right - left + 1)/2 * cos(angleStart) + xCentre;
133     yinterStart = -(bottom - top + 1)/2 * sin(angleStart) + yCentre;
134     xinterEnd   = (right - left + 1)/2 * cos(angleEnd) + xCentre;
135     yinterEnd   = -(bottom - top + 1)/2 * sin(angleEnd) + yCentre;
136
137     if(angleStart < 0) angleStart += 2 * M_PI;
138     if(angleEnd < 0) angleEnd += 2 * M_PI;
139     if(angleEnd < angleStart) angleEnd += 2 * M_PI;
140
141     bounds.left   = min(xinterStart, xinterEnd);
142     bounds.top    = min(yinterStart, yinterEnd);
143     bounds.right  = max(xinterStart, xinterEnd);
144     bounds.bottom = max(yinterStart, yinterEnd);
145
146     for(i = 0; i <= 8; i++) {
147         if(i * M_PI / 2 < angleStart) /* loop until we're past start */
148             continue;
149         if(i * M_PI / 2 > angleEnd)   /* if we're past end we're finished */
150             break;
151
152         /* the arc touches the rectangle at the start of quadrant i, so adjust
153            BBox to reflect this. */
154
155         switch(i % 4) {
156         case 0:
157             bounds.right = right;
158             break;
159         case 1:
160             bounds.top = top;
161             break;
162         case 2:
163             bounds.left = left;
164             break;
165         case 3:
166             bounds.bottom = bottom;
167             break;
168         }
169     }
170
171     /* If we're drawing a pie then make sure we include the centre */
172     if(iType == EMR_PIE) {
173         if(bounds.left > xCentre) bounds.left = xCentre;
174         else if(bounds.right < xCentre) bounds.right = xCentre;
175         if(bounds.top > yCentre) bounds.top = yCentre;
176         else if(bounds.bottom < yCentre) bounds.right = yCentre;
177     }
178     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
179         return FALSE;
180     EMFDRV_UpdateBBox( dev, &bounds );
181     return TRUE;
182 }
183
184
185 /***********************************************************************
186  *           EMFDRV_Arc
187  */
188 BOOL
189 EMFDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
190             INT xstart, INT ystart, INT xend, INT yend )
191 {
192     return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
193                                xend, yend, EMR_ARC );
194 }
195
196 /***********************************************************************
197  *           EMFDRV_Pie
198  */
199 BOOL
200 EMFDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
201             INT xstart, INT ystart, INT xend, INT yend )
202 {
203     return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
204                                xend, yend, EMR_PIE );
205 }
206
207
208 /***********************************************************************
209  *           EMFDRV_Chord
210  */
211 BOOL
212 EMFDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
213              INT xstart, INT ystart, INT xend, INT yend )
214 {
215     return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
216                                xend, yend, EMR_CHORD );
217 }
218
219 /***********************************************************************
220  *           EMFDRV_Ellipse
221  */
222 BOOL
223 EMFDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
224 {
225     EMRELLIPSE emr;
226     INT temp;
227     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
228
229     TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
230
231     if(left == right || top == bottom) return FALSE;
232
233     if(left > right) {temp = left; left = right; right = temp;}
234     if(top > bottom) {temp = top; top = bottom; bottom = temp;}
235
236     if(GetGraphicsMode(physDev->hdc) == GM_COMPATIBLE) {
237         right--;
238         bottom--;
239     }
240
241     emr.emr.iType     = EMR_ELLIPSE;
242     emr.emr.nSize     = sizeof(emr);
243     emr.rclBox.left   = left;
244     emr.rclBox.top    = top;
245     emr.rclBox.right  = right;
246     emr.rclBox.bottom = bottom;
247
248     EMFDRV_UpdateBBox( dev, &emr.rclBox );
249     return EMFDRV_WriteRecord( dev, &emr.emr );
250 }
251
252 /***********************************************************************
253  *           EMFDRV_Rectangle
254  */
255 BOOL
256 EMFDRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
257 {
258     EMRRECTANGLE emr;
259     INT temp;
260     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
261
262     TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
263
264     if(left == right || top == bottom) return FALSE;
265
266     if(left > right) {temp = left; left = right; right = temp;}
267     if(top > bottom) {temp = top; top = bottom; bottom = temp;}
268
269     if(GetGraphicsMode(physDev->hdc) == GM_COMPATIBLE) {
270         right--;
271         bottom--;
272     }
273
274     emr.emr.iType     = EMR_RECTANGLE;
275     emr.emr.nSize     = sizeof(emr);
276     emr.rclBox.left   = left;
277     emr.rclBox.top    = top;
278     emr.rclBox.right  = right;
279     emr.rclBox.bottom = bottom;
280
281     EMFDRV_UpdateBBox( dev, &emr.rclBox );
282     return EMFDRV_WriteRecord( dev, &emr.emr );
283 }
284
285 /***********************************************************************
286  *           EMFDRV_RoundRect
287  */
288 BOOL
289 EMFDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
290                   INT bottom, INT ell_width, INT ell_height )
291 {
292     EMRROUNDRECT emr;
293     INT temp;
294     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
295
296     if(left == right || top == bottom) return FALSE;
297
298     if(left > right) {temp = left; left = right; right = temp;}
299     if(top > bottom) {temp = top; top = bottom; bottom = temp;}
300
301     if(GetGraphicsMode(physDev->hdc) == GM_COMPATIBLE) {
302         right--;
303         bottom--;
304     }
305
306     emr.emr.iType     = EMR_ROUNDRECT;
307     emr.emr.nSize     = sizeof(emr);
308     emr.rclBox.left   = left;
309     emr.rclBox.top    = top;
310     emr.rclBox.right  = right;
311     emr.rclBox.bottom = bottom;
312     emr.szlCorner.cx  = ell_width;
313     emr.szlCorner.cy  = ell_height;
314
315     EMFDRV_UpdateBBox( dev, &emr.rclBox );
316     return EMFDRV_WriteRecord( dev, &emr.emr );
317 }
318
319 /***********************************************************************
320  *           EMFDRV_SetPixel
321  */
322 COLORREF
323 EMFDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
324 {
325     EMRSETPIXELV emr;
326
327     emr.emr.iType  = EMR_SETPIXELV;
328     emr.emr.nSize  = sizeof(emr);
329     emr.ptlPixel.x = x;
330     emr.ptlPixel.y = y;
331     emr.crColor = color;
332
333     if (EMFDRV_WriteRecord( dev, &emr.emr )) {
334         RECTL bounds;
335         bounds.left = bounds.right = x;
336         bounds.top = bounds.bottom = y;
337         EMFDRV_UpdateBBox( dev, &bounds );
338         return color;
339     }
340     return -1;
341 }
342
343 /**********************************************************************
344  *          EMFDRV_Polylinegon
345  *
346  * Helper for EMFDRV_Poly{line|gon}
347  */
348 static BOOL
349 EMFDRV_Polylinegon( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
350 {
351     EMRPOLYLINE *emr;
352     DWORD size;
353     INT i;
354     BOOL ret;
355
356     size = sizeof(EMRPOLYLINE) + sizeof(POINTL) * (count - 1);
357
358     emr = HeapAlloc( GetProcessHeap(), 0, size );
359     emr->emr.iType = iType;
360     emr->emr.nSize = size;
361
362     emr->rclBounds.left = emr->rclBounds.right = pt[0].x;
363     emr->rclBounds.top = emr->rclBounds.bottom = pt[0].y;
364
365     for(i = 1; i < count; i++) {
366         if(pt[i].x < emr->rclBounds.left)
367             emr->rclBounds.left = pt[i].x;
368         else if(pt[i].x > emr->rclBounds.right)
369             emr->rclBounds.right = pt[i].x;
370         if(pt[i].y < emr->rclBounds.top)
371             emr->rclBounds.top = pt[i].y;
372         else if(pt[i].y > emr->rclBounds.bottom)
373             emr->rclBounds.bottom = pt[i].y;
374     }
375
376     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     return ret;
384 }
385
386
387 /**********************************************************************
388  *          EMFDRV_Polylinegon16
389  *
390  * Helper for EMFDRV_Poly{line|gon}
391  *
392  *  This is not a legacy function!
393  *  We are using SHORT integers to save space.
394  */
395 static BOOL
396 EMFDRV_Polylinegon16( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
397 {
398     EMRPOLYLINE16 *emr;
399     DWORD size;
400     INT i;
401     BOOL ret;
402
403     /* check whether all points fit in the SHORT int POINT structure */
404     for(i = 0; i < count; i++) {
405         if( ((pt[i].x+0x8000) & ~0xffff ) || 
406             ((pt[i].y+0x8000) & ~0xffff ) )
407             return FALSE;
408     }
409
410     size = sizeof(EMRPOLYLINE16) + sizeof(POINTS) * (count - 1);
411
412     emr = HeapAlloc( GetProcessHeap(), 0, size );
413     emr->emr.iType = iType;
414     emr->emr.nSize = size;
415
416     emr->rclBounds.left = emr->rclBounds.right = pt[0].x;
417     emr->rclBounds.top = emr->rclBounds.bottom = pt[0].y;
418
419     for(i = 1; i < count; i++) {
420         if(pt[i].x < emr->rclBounds.left)
421             emr->rclBounds.left = pt[i].x;
422         else if(pt[i].x > emr->rclBounds.right)
423             emr->rclBounds.right = pt[i].x;
424         if(pt[i].y < emr->rclBounds.top)
425             emr->rclBounds.top = pt[i].y;
426         else if(pt[i].y > emr->rclBounds.bottom)
427             emr->rclBounds.bottom = pt[i].y;
428     }
429
430     emr->cpts = count;
431     for(i = 0; i < count; i++ ) {
432         emr->apts[i].x = pt[i].x;
433         emr->apts[i].y = pt[i].y;
434     }
435
436     ret = EMFDRV_WriteRecord( dev, &emr->emr );
437     if(ret)
438         EMFDRV_UpdateBBox( dev, &emr->rclBounds );
439     HeapFree( GetProcessHeap(), 0, emr );
440     return ret;
441 }
442
443
444 /**********************************************************************
445  *          EMFDRV_Polyline
446  */
447 BOOL
448 EMFDRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
449 {
450     if( EMFDRV_Polylinegon16( dev, pt, count, EMR_POLYLINE16 ) )
451         return TRUE;
452     return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINE );
453 }
454
455 /**********************************************************************
456  *          EMFDRV_Polygon
457  */
458 BOOL
459 EMFDRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
460 {
461     if(count < 2) return FALSE;
462     return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYGON );
463 }
464
465
466 /**********************************************************************
467  *          EMFDRV_PolyPolylinegon
468  *
469  * Helper for EMFDRV_PolyPoly{line|gon}
470  */
471 static BOOL
472 EMFDRV_PolyPolylinegon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys,
473                         DWORD iType)
474 {
475     EMRPOLYPOLYLINE *emr;
476     DWORD cptl = 0, poly, size;
477     INT point;
478     RECTL bounds;
479     const POINT *pts;
480     BOOL ret;
481
482     bounds.left = bounds.right = pt[0].x;
483     bounds.top = bounds.bottom = pt[0].y;
484
485     pts = pt;
486     for(poly = 0; poly < polys; poly++) {
487         cptl += counts[poly];
488         for(point = 0; point < counts[poly]; point++) {
489             if(bounds.left > pts->x) bounds.left = pts->x;
490             else if(bounds.right < pts->x) bounds.right = pts->x;
491             if(bounds.top > pts->y) bounds.top = pts->y;
492             else if(bounds.bottom < pts->y) bounds.bottom = pts->y;
493             pts++;
494         }
495     }
496
497     size = sizeof(EMRPOLYPOLYLINE) + (polys - 1) * sizeof(DWORD) +
498       (cptl - 1) * sizeof(POINTL);
499
500     emr = HeapAlloc( GetProcessHeap(), 0, size );
501
502     emr->emr.iType = iType;
503     emr->emr.nSize = size;
504     emr->rclBounds = bounds;
505     emr->nPolys = polys;
506     emr->cptl = cptl;
507     memcpy(emr->aPolyCounts, counts, polys * sizeof(DWORD));
508     memcpy(emr->aPolyCounts + polys, pt, cptl * sizeof(POINTL));
509     ret = EMFDRV_WriteRecord( dev, &emr->emr );
510     if(ret)
511         EMFDRV_UpdateBBox( dev, &emr->rclBounds );
512     HeapFree( GetProcessHeap(), 0, emr );
513     return ret;
514 }
515
516 /**********************************************************************
517  *          EMFDRV_PolyPolyline
518  */
519 BOOL
520 EMFDRV_PolyPolyline(PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polys)
521 {
522     return EMFDRV_PolyPolylinegon( dev, pt, (INT *)counts, polys,
523                                    EMR_POLYPOLYLINE );
524 }
525
526 /**********************************************************************
527  *          EMFDRV_PolyPolygon
528  */
529 BOOL
530 EMFDRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys )
531 {
532     return EMFDRV_PolyPolylinegon( dev, pt, counts, polys, EMR_POLYPOLYGON );
533 }
534
535
536 /**********************************************************************
537  *          EMFDRV_ExtFloodFill
538  */
539 BOOL
540 EMFDRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
541 {
542     EMREXTFLOODFILL emr;
543
544     emr.emr.iType = EMR_EXTFLOODFILL;
545     emr.emr.nSize = sizeof(emr);
546     emr.ptlStart.x = x;
547     emr.ptlStart.y = y;
548     emr.crColor = color;
549     emr.iMode = fillType;
550
551     return EMFDRV_WriteRecord( dev, &emr.emr );
552 }
553
554
555 /*********************************************************************
556  *          EMFDRV_FillRgn
557  */
558 BOOL EMFDRV_FillRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush )
559 {
560     EMRFILLRGN *emr;
561     DWORD size, rgnsize, index;
562     BOOL ret;
563
564     index = EMFDRV_CreateBrushIndirect( dev, hbrush );
565     if(!index) return FALSE;
566
567     rgnsize = GetRegionData( hrgn, 0, NULL );
568     size = rgnsize + offsetof(EMRFILLRGN,RgnData);
569     emr = HeapAlloc( GetProcessHeap(), 0, size );
570
571     GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
572
573     emr->emr.iType = EMR_FILLRGN;
574     emr->emr.nSize = size;
575     emr->rclBounds.left   = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
576     emr->rclBounds.top    = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
577     emr->rclBounds.right  = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
578     emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
579     emr->cbRgnData = rgnsize;
580     emr->ihBrush = index;
581
582     ret = EMFDRV_WriteRecord( dev, &emr->emr );
583     if(ret)
584         EMFDRV_UpdateBBox( dev, &emr->rclBounds );
585     HeapFree( GetProcessHeap(), 0, emr );
586     return ret;
587 }
588 /*********************************************************************
589  *          EMFDRV_FrameRgn
590  */
591 BOOL EMFDRV_FrameRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush, INT width, INT height )
592 {
593     EMRFRAMERGN *emr;
594     DWORD size, rgnsize, index;
595     BOOL ret;
596
597     index = EMFDRV_CreateBrushIndirect( dev, hbrush );
598     if(!index) return FALSE;
599
600     rgnsize = GetRegionData( hrgn, 0, NULL );
601     size = rgnsize + offsetof(EMRFRAMERGN,RgnData);
602     emr = HeapAlloc( GetProcessHeap(), 0, size );
603
604     GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
605
606     emr->emr.iType = EMR_FRAMERGN;
607     emr->emr.nSize = size;
608     emr->rclBounds.left   = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
609     emr->rclBounds.top    = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
610     emr->rclBounds.right  = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
611     emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
612     emr->cbRgnData = rgnsize;
613     emr->ihBrush = index;
614     emr->szlStroke.cx = width;
615     emr->szlStroke.cy = height;
616
617     ret = EMFDRV_WriteRecord( dev, &emr->emr );
618     if(ret)
619         EMFDRV_UpdateBBox( dev, &emr->rclBounds );
620     HeapFree( GetProcessHeap(), 0, emr );
621     return ret;
622 }
623
624 /*********************************************************************
625  *          EMFDRV_PaintInvertRgn
626  *
627  * Helper for EMFDRV_{Paint|Invert}Rgn
628  */
629 static BOOL EMFDRV_PaintInvertRgn( PHYSDEV dev, HRGN hrgn, DWORD iType )
630 {
631     EMRINVERTRGN *emr;
632     DWORD size, rgnsize;
633     BOOL ret;
634
635
636     rgnsize = GetRegionData( hrgn, 0, NULL );
637     size = rgnsize + offsetof(EMRINVERTRGN,RgnData);
638     emr = HeapAlloc( GetProcessHeap(), 0, size );
639
640     GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
641
642     emr->emr.iType = iType;
643     emr->emr.nSize = size;
644     emr->rclBounds.left   = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
645     emr->rclBounds.top    = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
646     emr->rclBounds.right  = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
647     emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
648     emr->cbRgnData = rgnsize;
649
650     ret = EMFDRV_WriteRecord( dev, &emr->emr );
651     if(ret)
652         EMFDRV_UpdateBBox( dev, &emr->rclBounds );
653     HeapFree( GetProcessHeap(), 0, emr );
654     return ret;
655 }
656
657 /**********************************************************************
658  *          EMFDRV_PaintRgn
659  */
660 BOOL
661 EMFDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
662 {
663     return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_PAINTRGN );
664 }
665
666 /**********************************************************************
667  *          EMFDRV_InvertRgn
668  */
669 BOOL
670 EMFDRV_InvertRgn( PHYSDEV dev, HRGN hrgn )
671 {
672     return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_INVERTRGN );
673 }
674
675 /**********************************************************************
676  *          EMFDRV_SetBkColor
677  */
678 COLORREF
679 EMFDRV_SetBkColor( PHYSDEV dev, COLORREF color )
680 {
681     EMRSETBKCOLOR emr;
682
683     emr.emr.iType = EMR_SETBKCOLOR;
684     emr.emr.nSize = sizeof(emr);
685     emr.crColor = color;
686
687     return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
688 }
689
690
691 /**********************************************************************
692  *          EMFDRV_SetTextColor
693  */
694 COLORREF
695 EMFDRV_SetTextColor( PHYSDEV dev, COLORREF color )
696 {
697     EMRSETTEXTCOLOR emr;
698
699     emr.emr.iType = EMR_SETTEXTCOLOR;
700     emr.emr.nSize = sizeof(emr);
701     emr.crColor = color;
702
703     return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
704 }
705
706 /**********************************************************************
707  *          EMFDRV_ExtTextOut
708  */
709 BOOL EMFDRV_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags,
710                         const RECT *lprect, LPCWSTR str, UINT count,
711                         const INT *lpDx, INT breakExtra )
712 {
713     EMREXTTEXTOUTW *pemr;
714     DWORD nSize;
715     BOOL ret;
716     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
717     int textHeight = 0;
718     int textWidth = 0;
719     const UINT textAlign = GetTextAlign(physDev->hdc);
720
721     nSize = sizeof(*pemr) + ((count+1) & ~1) * sizeof(WCHAR) + count * sizeof(INT);
722
723     TRACE("%s count %d nSize = %ld\n", debugstr_wn(str, count), count, nSize);
724     pemr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nSize);
725
726     pemr->emr.iType = EMR_EXTTEXTOUTW;
727     pemr->emr.nSize = nSize;
728
729     pemr->iGraphicsMode = GetGraphicsMode(physDev->hdc);
730     pemr->exScale = pemr->eyScale = 1.0; /* FIXME */
731
732     pemr->emrtext.ptlReference.x = x;
733     pemr->emrtext.ptlReference.y = y;
734     pemr->emrtext.nChars = count;
735     pemr->emrtext.offString = sizeof(*pemr);
736     memcpy((char*)pemr + pemr->emrtext.offString, str, count * sizeof(WCHAR));
737     pemr->emrtext.fOptions = flags;
738     if(!lprect) {
739         pemr->emrtext.rcl.left = pemr->emrtext.rcl.top = 0;
740         pemr->emrtext.rcl.right = pemr->emrtext.rcl.bottom = -1;
741     } else {
742         pemr->emrtext.rcl.left = lprect->left;
743         pemr->emrtext.rcl.top = lprect->top;
744         pemr->emrtext.rcl.right = lprect->right;
745         pemr->emrtext.rcl.bottom = lprect->bottom;
746     }
747
748     pemr->emrtext.offDx = pemr->emrtext.offString + ((count+1) & ~1) * sizeof(WCHAR);
749     if(lpDx) {
750         UINT i;
751         SIZE strSize;
752         memcpy((char*)pemr + pemr->emrtext.offDx, lpDx, count * sizeof(INT));
753         for (i = 0; i < count; i++) {
754             textWidth += lpDx[i];
755         }
756         GetTextExtentPoint32W(physDev->hdc, str, count, &strSize);
757         textHeight = strSize.cy;
758     }
759     else {
760         UINT i;
761         INT *dx = (INT *)((char*)pemr + pemr->emrtext.offDx);
762         SIZE charSize;
763         for (i = 0; i < count; i++) {
764             GetTextExtentPoint32W(physDev->hdc, str + i, 1, &charSize);
765             dx[i] = charSize.cx;
766             textWidth += charSize.cx;
767             textHeight = max(textHeight, charSize.cy);
768         }
769     }
770
771     switch (textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER)) {
772     case TA_CENTER: {
773         pemr->rclBounds.left  = x - (textWidth / 2) - 1;
774         pemr->rclBounds.right = x + (textWidth / 2) + 1;
775         break;
776     }
777     case TA_RIGHT: {
778         pemr->rclBounds.left  = x - textWidth - 1;
779         pemr->rclBounds.right = x;
780         break;
781     }
782     default: { /* TA_LEFT */
783         pemr->rclBounds.left  = x;
784         pemr->rclBounds.right = x + textWidth + 1;
785     }
786     }
787
788     switch (textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE)) {
789     case TA_BASELINE: {
790         TEXTMETRICW tm;
791         GetTextMetricsW(physDev->hdc, &tm);
792         /* Play safe here... it's better to have a bounding box */
793         /* that is too big than too small. */
794         pemr->rclBounds.top    = y - textHeight - 1;
795         pemr->rclBounds.bottom = y + tm.tmDescent + 1;
796         break;
797     }
798     case TA_BOTTOM: {
799         pemr->rclBounds.top    = y - textHeight - 1;
800         pemr->rclBounds.bottom = y;
801         break;
802     }
803     default: { /* TA_TOP */
804         pemr->rclBounds.top    = y;
805         pemr->rclBounds.bottom = y + textHeight + 1;
806     }
807     }
808
809     ret = EMFDRV_WriteRecord( dev, &pemr->emr );
810     if(ret)
811         EMFDRV_UpdateBBox( dev, &pemr->rclBounds );
812     HeapFree( GetProcessHeap(), 0, pemr );
813     return ret;
814 }
815
816 /**********************************************************************
817  *          EMFDRV_SetArcDirection
818  */
819 INT EMFDRV_SetArcDirection(PHYSDEV dev, INT arcDirection)
820 {
821     EMRSETARCDIRECTION emr;
822
823     emr.emr.iType = EMR_SETARCDIRECTION;
824     emr.emr.nSize = sizeof(emr);
825     emr.iArcDirection = arcDirection;
826
827     EMFDRV_WriteRecord(dev, &emr.emr);
828
829     /* We don't know the old arc direction and we don't care... */ 
830     return 0;
831 }