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