ntdll: Add a wrapper to call the unhandled exception filter.
[wine.git] / dlls / gdi32 / enhmfdrv / graphics.c
blob68bf4d29d4cbde1bd442a1e83e09df28ba78ba77
1 /*
2 * Enhanced MetaFile driver graphics functions
4 * Copyright 1999 Huw D M Davies
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "enhmfdrv/enhmetafiledrv.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
36 static const RECTL empty_bounds = { 0, 0, -1, -1 };
38 /* determine if we can use 16-bit points to store all the input points */
39 static BOOL can_use_short_points( const POINT *pts, UINT count )
41 UINT i;
43 for (i = 0; i < count; i++)
44 if (((pts[i].x + 0x8000) & ~0xffff) || ((pts[i].y + 0x8000) & ~0xffff))
45 return FALSE;
46 return TRUE;
49 /* store points in either long or short format; return a pointer to the end of the stored data */
50 static void *store_points( POINTL *dest, const POINT *pts, UINT count, BOOL short_points )
52 if (short_points)
54 UINT i;
55 POINTS *dest_short = (POINTS *)dest;
57 for (i = 0; i < count; i++)
59 dest_short[i].x = pts[i].x;
60 dest_short[i].y = pts[i].y;
62 return dest_short + count;
64 else
66 memcpy( dest, pts, count * sizeof(*dest) );
67 return dest + count;
71 /* compute the bounds of an array of points, optionally including the current position */
72 static void get_points_bounds( RECTL *bounds, const POINT *pts, UINT count, DC *dc )
74 UINT i;
76 if (dc)
78 bounds->left = bounds->right = dc->cur_pos.x;
79 bounds->top = bounds->bottom = dc->cur_pos.y;
81 else if (count)
83 bounds->left = bounds->right = pts[0].x;
84 bounds->top = bounds->bottom = pts[0].y;
86 else *bounds = empty_bounds;
88 for (i = 0; i < count; i++)
90 bounds->left = min( bounds->left, pts[i].x );
91 bounds->right = max( bounds->right, pts[i].x );
92 bounds->top = min( bounds->top, pts[i].y );
93 bounds->bottom = max( bounds->bottom, pts[i].y );
97 /* helper for path stroke and fill functions */
98 static BOOL emfdrv_stroke_and_fill_path( PHYSDEV dev, INT type )
100 DC *dc = get_physdev_dc( dev );
101 EMRSTROKEANDFILLPATH emr;
102 struct gdi_path *path;
103 POINT *points;
104 BYTE *flags;
106 emr.emr.iType = type;
107 emr.emr.nSize = sizeof(emr);
109 if ((path = get_gdi_flat_path( dc, NULL )))
111 int count = get_gdi_path_data( path, &points, &flags );
112 get_points_bounds( &emr.rclBounds, points, count, 0 );
113 free_gdi_path( path );
115 else emr.rclBounds = empty_bounds;
117 if (!EMFDRV_WriteRecord( dev, &emr.emr )) return FALSE;
118 if (!path) return FALSE;
119 EMFDRV_UpdateBBox( dev, &emr.rclBounds );
120 return TRUE;
123 /**********************************************************************
124 * EMFDRV_MoveTo
126 BOOL EMFDRV_MoveTo(PHYSDEV dev, INT x, INT y)
128 EMRMOVETOEX emr;
130 emr.emr.iType = EMR_MOVETOEX;
131 emr.emr.nSize = sizeof(emr);
132 emr.ptl.x = x;
133 emr.ptl.y = y;
135 return EMFDRV_WriteRecord( dev, &emr.emr );
138 /***********************************************************************
139 * EMFDRV_LineTo
141 BOOL EMFDRV_LineTo( PHYSDEV dev, INT x, INT y )
143 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
144 DC *dc = get_physdev_dc( dev );
145 POINT pt;
146 EMRLINETO emr;
147 RECTL bounds;
149 emr.emr.iType = EMR_LINETO;
150 emr.emr.nSize = sizeof(emr);
151 emr.ptl.x = x;
152 emr.ptl.y = y;
154 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
155 return FALSE;
157 pt = dc->cur_pos;
159 bounds.left = min(x, pt.x);
160 bounds.top = min(y, pt.y);
161 bounds.right = max(x, pt.x);
162 bounds.bottom = max(y, pt.y);
164 if(!physDev->path)
165 EMFDRV_UpdateBBox( dev, &bounds );
167 return TRUE;
171 /***********************************************************************
172 * EMFDRV_ArcChordPie
174 static BOOL
175 EMFDRV_ArcChordPie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
176 INT xstart, INT ystart, INT xend, INT yend, DWORD iType )
178 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
179 DC *dc = get_physdev_dc( dev );
180 INT temp, xCentre, yCentre, i;
181 double angleStart, angleEnd;
182 double xinterStart, yinterStart, xinterEnd, yinterEnd;
183 EMRARC emr;
184 RECTL bounds;
186 if(left == right || top == bottom) return FALSE;
188 if(left > right) {temp = left; left = right; right = temp;}
189 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
191 if(dc->GraphicsMode == GM_COMPATIBLE) {
192 right--;
193 bottom--;
196 emr.emr.iType = iType;
197 emr.emr.nSize = sizeof(emr);
198 emr.rclBox.left = left;
199 emr.rclBox.top = top;
200 emr.rclBox.right = right;
201 emr.rclBox.bottom = bottom;
202 emr.ptlStart.x = xstart;
203 emr.ptlStart.y = ystart;
204 emr.ptlEnd.x = xend;
205 emr.ptlEnd.y = yend;
208 /* Now calculate the BBox */
209 xCentre = (left + right + 1) / 2;
210 yCentre = (top + bottom + 1) / 2;
212 xstart -= xCentre;
213 ystart -= yCentre;
214 xend -= xCentre;
215 yend -= yCentre;
217 /* invert y co-ords to get angle anti-clockwise from x-axis */
218 angleStart = atan2( -(double)ystart, (double)xstart);
219 angleEnd = atan2( -(double)yend, (double)xend);
221 /* These are the intercepts of the start/end lines with the arc */
223 xinterStart = (right - left + 1)/2 * cos(angleStart) + xCentre;
224 yinterStart = -(bottom - top + 1)/2 * sin(angleStart) + yCentre;
225 xinterEnd = (right - left + 1)/2 * cos(angleEnd) + xCentre;
226 yinterEnd = -(bottom - top + 1)/2 * sin(angleEnd) + yCentre;
228 if(angleStart < 0) angleStart += 2 * M_PI;
229 if(angleEnd < 0) angleEnd += 2 * M_PI;
230 if(angleEnd < angleStart) angleEnd += 2 * M_PI;
232 bounds.left = min(xinterStart, xinterEnd);
233 bounds.top = min(yinterStart, yinterEnd);
234 bounds.right = max(xinterStart, xinterEnd);
235 bounds.bottom = max(yinterStart, yinterEnd);
237 for(i = 0; i <= 8; i++) {
238 if(i * M_PI / 2 < angleStart) /* loop until we're past start */
239 continue;
240 if(i * M_PI / 2 > angleEnd) /* if we're past end we're finished */
241 break;
243 /* the arc touches the rectangle at the start of quadrant i, so adjust
244 BBox to reflect this. */
246 switch(i % 4) {
247 case 0:
248 bounds.right = right;
249 break;
250 case 1:
251 bounds.top = top;
252 break;
253 case 2:
254 bounds.left = left;
255 break;
256 case 3:
257 bounds.bottom = bottom;
258 break;
262 /* If we're drawing a pie then make sure we include the centre */
263 if(iType == EMR_PIE) {
264 if(bounds.left > xCentre) bounds.left = xCentre;
265 else if(bounds.right < xCentre) bounds.right = xCentre;
266 if(bounds.top > yCentre) bounds.top = yCentre;
267 else if(bounds.bottom < yCentre) bounds.bottom = yCentre;
269 if (iType == EMR_ARCTO)
271 POINT pt;
272 pt = dc->cur_pos;
273 bounds.left = min( bounds.left, pt.x );
274 bounds.top = min( bounds.top, pt.y );
275 bounds.right = max( bounds.right, pt.x );
276 bounds.bottom = max( bounds.bottom, pt.y );
278 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
279 return FALSE;
280 if(!physDev->path)
281 EMFDRV_UpdateBBox( dev, &bounds );
282 return TRUE;
286 /***********************************************************************
287 * EMFDRV_Arc
289 BOOL EMFDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
290 INT xstart, INT ystart, INT xend, INT yend )
292 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
293 xend, yend, EMR_ARC );
296 /***********************************************************************
297 * EMFDRV_ArcTo
299 BOOL EMFDRV_ArcTo( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
300 INT xstart, INT ystart, INT xend, INT yend )
302 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
303 xend, yend, EMR_ARCTO );
306 /***********************************************************************
307 * EMFDRV_Pie
309 BOOL EMFDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
310 INT xstart, INT ystart, INT xend, INT yend )
312 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
313 xend, yend, EMR_PIE );
317 /***********************************************************************
318 * EMFDRV_Chord
320 BOOL EMFDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
321 INT xstart, INT ystart, INT xend, INT yend )
323 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
324 xend, yend, EMR_CHORD );
327 /***********************************************************************
328 * EMFDRV_AngleArc
330 BOOL EMFDRV_AngleArc( PHYSDEV dev, INT x, INT y, DWORD radius, FLOAT start, FLOAT sweep )
332 EMRANGLEARC emr;
334 emr.emr.iType = EMR_ANGLEARC;
335 emr.emr.nSize = sizeof( emr );
336 emr.ptlCenter.x = x;
337 emr.ptlCenter.y = y;
338 emr.nRadius = radius;
339 emr.eStartAngle = start;
340 emr.eSweepAngle = sweep;
342 return EMFDRV_WriteRecord( dev, &emr.emr );
345 /***********************************************************************
346 * EMFDRV_Ellipse
348 BOOL EMFDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
350 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
351 DC *dc = get_physdev_dc( dev );
352 EMRELLIPSE emr;
353 INT temp;
355 TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
357 if(left == right || top == bottom) return FALSE;
359 if(left > right) {temp = left; left = right; right = temp;}
360 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
362 if(dc->GraphicsMode == GM_COMPATIBLE) {
363 right--;
364 bottom--;
367 emr.emr.iType = EMR_ELLIPSE;
368 emr.emr.nSize = sizeof(emr);
369 emr.rclBox.left = left;
370 emr.rclBox.top = top;
371 emr.rclBox.right = right;
372 emr.rclBox.bottom = bottom;
374 if(!physDev->path)
375 EMFDRV_UpdateBBox( dev, &emr.rclBox );
376 return EMFDRV_WriteRecord( dev, &emr.emr );
379 /***********************************************************************
380 * EMFDRV_Rectangle
382 BOOL EMFDRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
384 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
385 DC *dc = get_physdev_dc( dev );
386 EMRRECTANGLE emr;
387 INT temp;
389 TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
391 if(left == right || top == bottom) return FALSE;
393 if(left > right) {temp = left; left = right; right = temp;}
394 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
396 if(dc->GraphicsMode == GM_COMPATIBLE) {
397 right--;
398 bottom--;
401 emr.emr.iType = EMR_RECTANGLE;
402 emr.emr.nSize = sizeof(emr);
403 emr.rclBox.left = left;
404 emr.rclBox.top = top;
405 emr.rclBox.right = right;
406 emr.rclBox.bottom = bottom;
408 if(!physDev->path)
409 EMFDRV_UpdateBBox( dev, &emr.rclBox );
410 return EMFDRV_WriteRecord( dev, &emr.emr );
413 /***********************************************************************
414 * EMFDRV_RoundRect
416 BOOL EMFDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
417 INT bottom, INT ell_width, INT ell_height )
419 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
420 DC *dc = get_physdev_dc( dev );
421 EMRROUNDRECT emr;
422 INT temp;
424 if(left == right || top == bottom) return FALSE;
426 if(left > right) {temp = left; left = right; right = temp;}
427 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
429 if(dc->GraphicsMode == GM_COMPATIBLE) {
430 right--;
431 bottom--;
434 emr.emr.iType = EMR_ROUNDRECT;
435 emr.emr.nSize = sizeof(emr);
436 emr.rclBox.left = left;
437 emr.rclBox.top = top;
438 emr.rclBox.right = right;
439 emr.rclBox.bottom = bottom;
440 emr.szlCorner.cx = ell_width;
441 emr.szlCorner.cy = ell_height;
443 if(!physDev->path)
444 EMFDRV_UpdateBBox( dev, &emr.rclBox );
445 return EMFDRV_WriteRecord( dev, &emr.emr );
448 /***********************************************************************
449 * EMFDRV_SetPixel
451 COLORREF EMFDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
453 EMRSETPIXELV emr;
455 emr.emr.iType = EMR_SETPIXELV;
456 emr.emr.nSize = sizeof(emr);
457 emr.ptlPixel.x = x;
458 emr.ptlPixel.y = y;
459 emr.crColor = color;
461 if (EMFDRV_WriteRecord( dev, &emr.emr )) {
462 RECTL bounds;
463 bounds.left = bounds.right = x;
464 bounds.top = bounds.bottom = y;
465 EMFDRV_UpdateBBox( dev, &bounds );
466 return color;
468 return -1;
471 /**********************************************************************
472 * EMFDRV_Polylinegon
474 * Helper for EMFDRV_Poly{line|gon}
476 static BOOL
477 EMFDRV_Polylinegon( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
479 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
480 DC *dc = get_physdev_dc( dev );
481 EMRPOLYLINE *emr;
482 DWORD size;
483 BOOL ret, use_small_emr = can_use_short_points( pt, count );
485 size = use_small_emr ? offsetof( EMRPOLYLINE16, apts[count] ) : offsetof( EMRPOLYLINE, aptl[count] );
487 emr = HeapAlloc( GetProcessHeap(), 0, size );
488 emr->emr.iType = use_small_emr ? iType + EMR_POLYLINE16 - EMR_POLYLINE : iType;
489 emr->emr.nSize = size;
490 emr->cptl = count;
492 store_points( emr->aptl, pt, count, use_small_emr );
494 if (!physDev->path)
495 get_points_bounds( &emr->rclBounds, pt, count,
496 (iType == EMR_POLYBEZIERTO || iType == EMR_POLYLINETO) ? dc : 0 );
497 else
498 emr->rclBounds = empty_bounds;
500 ret = EMFDRV_WriteRecord( dev, &emr->emr );
501 if (ret && !physDev->path)
502 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
503 HeapFree( GetProcessHeap(), 0, emr );
504 return ret;
508 /**********************************************************************
509 * EMFDRV_Polyline
511 BOOL EMFDRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
513 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINE );
516 /**********************************************************************
517 * EMFDRV_PolylineTo
519 BOOL EMFDRV_PolylineTo( PHYSDEV dev, const POINT* pt, INT count )
521 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINETO );
524 /**********************************************************************
525 * EMFDRV_Polygon
527 BOOL EMFDRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
529 if(count < 2) return FALSE;
530 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYGON );
533 /**********************************************************************
534 * EMFDRV_PolyBezier
536 BOOL EMFDRV_PolyBezier( PHYSDEV dev, const POINT *pts, DWORD count )
538 return EMFDRV_Polylinegon( dev, pts, count, EMR_POLYBEZIER );
541 /**********************************************************************
542 * EMFDRV_PolyBezierTo
544 BOOL EMFDRV_PolyBezierTo( PHYSDEV dev, const POINT *pts, DWORD count )
546 return EMFDRV_Polylinegon( dev, pts, count, EMR_POLYBEZIERTO );
550 /**********************************************************************
551 * EMFDRV_PolyPolylinegon
553 * Helper for EMFDRV_PolyPoly{line|gon}
555 static BOOL
556 EMFDRV_PolyPolylinegon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys,
557 DWORD iType)
559 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
560 EMRPOLYPOLYLINE *emr;
561 DWORD cptl = 0, poly, size;
562 BOOL ret, use_small_emr, bounds_valid = TRUE;
564 for(poly = 0; poly < polys; poly++) {
565 cptl += counts[poly];
566 if(counts[poly] < 2) bounds_valid = FALSE;
568 if(!cptl) bounds_valid = FALSE;
569 use_small_emr = can_use_short_points( pt, cptl );
571 size = FIELD_OFFSET(EMRPOLYPOLYLINE, aPolyCounts[polys]);
572 if(use_small_emr)
573 size += cptl * sizeof(POINTS);
574 else
575 size += cptl * sizeof(POINTL);
577 emr = HeapAlloc( GetProcessHeap(), 0, size );
579 emr->emr.iType = iType;
580 if(use_small_emr) emr->emr.iType += EMR_POLYPOLYLINE16 - EMR_POLYPOLYLINE;
582 emr->emr.nSize = size;
583 if(bounds_valid && !physDev->path)
584 get_points_bounds( &emr->rclBounds, pt, cptl, 0 );
585 else
586 emr->rclBounds = empty_bounds;
587 emr->nPolys = polys;
588 emr->cptl = cptl;
590 if(polys)
592 memcpy( emr->aPolyCounts, counts, polys * sizeof(DWORD) );
593 store_points( (POINTL *)(emr->aPolyCounts + polys), pt, cptl, use_small_emr );
596 ret = EMFDRV_WriteRecord( dev, &emr->emr );
597 if(ret && !bounds_valid)
599 ret = FALSE;
600 SetLastError( ERROR_INVALID_PARAMETER );
602 if(ret && !physDev->path)
603 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
604 HeapFree( GetProcessHeap(), 0, emr );
605 return ret;
608 /**********************************************************************
609 * EMFDRV_PolyPolyline
611 BOOL EMFDRV_PolyPolyline(PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polys)
613 return EMFDRV_PolyPolylinegon( dev, pt, (const INT *)counts, polys,
614 EMR_POLYPOLYLINE );
617 /**********************************************************************
618 * EMFDRV_PolyPolygon
620 BOOL EMFDRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys )
622 return EMFDRV_PolyPolylinegon( dev, pt, counts, polys, EMR_POLYPOLYGON );
626 /**********************************************************************
627 * EMFDRV_PolyDraw
629 BOOL EMFDRV_PolyDraw( PHYSDEV dev, const POINT *pts, const BYTE *types, DWORD count )
631 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
632 EMRPOLYDRAW *emr;
633 BOOL ret;
634 BYTE *types_dest;
635 BOOL use_small_emr = can_use_short_points( pts, count );
636 DWORD size;
638 size = use_small_emr ? offsetof( EMRPOLYDRAW16, apts[count] ) : offsetof( EMRPOLYDRAW, aptl[count] );
639 size += (count + 3) & ~3;
641 if (!(emr = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
643 emr->emr.iType = use_small_emr ? EMR_POLYDRAW16 : EMR_POLYDRAW;
644 emr->emr.nSize = size;
645 emr->cptl = count;
647 types_dest = store_points( emr->aptl, pts, count, use_small_emr );
648 memcpy( types_dest, types, count );
649 if (count & 3) memset( types_dest + count, 0, 4 - (count & 3) );
651 if (!physDev->path)
652 get_points_bounds( &emr->rclBounds, pts, count, 0 );
653 else
654 emr->rclBounds = empty_bounds;
656 ret = EMFDRV_WriteRecord( dev, &emr->emr );
657 if (ret && !physDev->path) EMFDRV_UpdateBBox( dev, &emr->rclBounds );
658 HeapFree( GetProcessHeap(), 0, emr );
659 return ret;
663 /**********************************************************************
664 * EMFDRV_ExtFloodFill
666 BOOL EMFDRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
668 EMREXTFLOODFILL emr;
670 emr.emr.iType = EMR_EXTFLOODFILL;
671 emr.emr.nSize = sizeof(emr);
672 emr.ptlStart.x = x;
673 emr.ptlStart.y = y;
674 emr.crColor = color;
675 emr.iMode = fillType;
677 return EMFDRV_WriteRecord( dev, &emr.emr );
681 /*********************************************************************
682 * EMFDRV_FillRgn
684 BOOL EMFDRV_FillRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush )
686 EMRFILLRGN *emr;
687 DWORD size, rgnsize, index;
688 BOOL ret;
690 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
691 if(!index) return FALSE;
693 rgnsize = GetRegionData( hrgn, 0, NULL );
694 size = rgnsize + offsetof(EMRFILLRGN,RgnData);
695 emr = HeapAlloc( GetProcessHeap(), 0, size );
697 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
699 emr->emr.iType = EMR_FILLRGN;
700 emr->emr.nSize = size;
701 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
702 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
703 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
704 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
705 emr->cbRgnData = rgnsize;
706 emr->ihBrush = index;
708 ret = EMFDRV_WriteRecord( dev, &emr->emr );
709 if(ret)
710 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
711 HeapFree( GetProcessHeap(), 0, emr );
712 return ret;
714 /*********************************************************************
715 * EMFDRV_FrameRgn
717 BOOL EMFDRV_FrameRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush, INT width, INT height )
719 EMRFRAMERGN *emr;
720 DWORD size, rgnsize, index;
721 BOOL ret;
723 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
724 if(!index) return FALSE;
726 rgnsize = GetRegionData( hrgn, 0, NULL );
727 size = rgnsize + offsetof(EMRFRAMERGN,RgnData);
728 emr = HeapAlloc( GetProcessHeap(), 0, size );
730 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
732 emr->emr.iType = EMR_FRAMERGN;
733 emr->emr.nSize = size;
734 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
735 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
736 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
737 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
738 emr->cbRgnData = rgnsize;
739 emr->ihBrush = index;
740 emr->szlStroke.cx = width;
741 emr->szlStroke.cy = height;
743 ret = EMFDRV_WriteRecord( dev, &emr->emr );
744 if(ret)
745 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
746 HeapFree( GetProcessHeap(), 0, emr );
747 return ret;
750 /*********************************************************************
751 * EMFDRV_PaintInvertRgn
753 * Helper for EMFDRV_{Paint|Invert}Rgn
755 static BOOL EMFDRV_PaintInvertRgn( PHYSDEV dev, HRGN hrgn, DWORD iType )
757 EMRINVERTRGN *emr;
758 DWORD size, rgnsize;
759 BOOL ret;
762 rgnsize = GetRegionData( hrgn, 0, NULL );
763 size = rgnsize + offsetof(EMRINVERTRGN,RgnData);
764 emr = HeapAlloc( GetProcessHeap(), 0, size );
766 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
768 emr->emr.iType = iType;
769 emr->emr.nSize = size;
770 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
771 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
772 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
773 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
774 emr->cbRgnData = rgnsize;
776 ret = EMFDRV_WriteRecord( dev, &emr->emr );
777 if(ret)
778 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
779 HeapFree( GetProcessHeap(), 0, emr );
780 return ret;
783 /**********************************************************************
784 * EMFDRV_PaintRgn
786 BOOL EMFDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
788 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_PAINTRGN );
791 /**********************************************************************
792 * EMFDRV_InvertRgn
794 BOOL EMFDRV_InvertRgn( PHYSDEV dev, HRGN hrgn )
796 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_INVERTRGN );
799 /**********************************************************************
800 * EMFDRV_ExtTextOut
802 BOOL EMFDRV_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags, const RECT *lprect,
803 LPCWSTR str, UINT count, const INT *lpDx )
805 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
806 DC *dc = get_physdev_dc( dev );
807 EMREXTTEXTOUTW *pemr;
808 DWORD nSize;
809 BOOL ret;
810 int textHeight = 0;
811 int textWidth = 0;
812 const UINT textAlign = dc->textAlign;
813 const INT graphicsMode = dc->GraphicsMode;
814 FLOAT exScale, eyScale;
816 nSize = sizeof(*pemr) + ((count+1) & ~1) * sizeof(WCHAR) + count * sizeof(INT);
818 TRACE("%s %s count %d nSize = %d\n", debugstr_wn(str, count),
819 wine_dbgstr_rect(lprect), count, nSize);
820 pemr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nSize);
822 if (graphicsMode == GM_COMPATIBLE)
824 const INT horzSize = GetDeviceCaps( dev->hdc, HORZSIZE );
825 const INT horzRes = GetDeviceCaps( dev->hdc, HORZRES );
826 const INT vertSize = GetDeviceCaps( dev->hdc, VERTSIZE );
827 const INT vertRes = GetDeviceCaps( dev->hdc, VERTRES );
828 SIZE wndext, vportext;
830 GetViewportExtEx( dev->hdc, &vportext );
831 GetWindowExtEx( dev->hdc, &wndext );
832 exScale = 100.0 * ((FLOAT)horzSize / (FLOAT)horzRes) /
833 ((FLOAT)wndext.cx / (FLOAT)vportext.cx);
834 eyScale = 100.0 * ((FLOAT)vertSize / (FLOAT)vertRes) /
835 ((FLOAT)wndext.cy / (FLOAT)vportext.cy);
837 else
839 exScale = 0.0;
840 eyScale = 0.0;
843 pemr->emr.iType = EMR_EXTTEXTOUTW;
844 pemr->emr.nSize = nSize;
845 pemr->iGraphicsMode = graphicsMode;
846 pemr->exScale = exScale;
847 pemr->eyScale = eyScale;
848 pemr->emrtext.ptlReference.x = x;
849 pemr->emrtext.ptlReference.y = y;
850 pemr->emrtext.nChars = count;
851 pemr->emrtext.offString = sizeof(*pemr);
852 memcpy((char*)pemr + pemr->emrtext.offString, str, count * sizeof(WCHAR));
853 pemr->emrtext.fOptions = flags;
854 if(!lprect) {
855 pemr->emrtext.rcl.left = pemr->emrtext.rcl.top = 0;
856 pemr->emrtext.rcl.right = pemr->emrtext.rcl.bottom = -1;
857 } else {
858 pemr->emrtext.rcl.left = lprect->left;
859 pemr->emrtext.rcl.top = lprect->top;
860 pemr->emrtext.rcl.right = lprect->right;
861 pemr->emrtext.rcl.bottom = lprect->bottom;
864 pemr->emrtext.offDx = pemr->emrtext.offString + ((count+1) & ~1) * sizeof(WCHAR);
865 if(lpDx) {
866 UINT i;
867 SIZE strSize;
868 memcpy((char*)pemr + pemr->emrtext.offDx, lpDx, count * sizeof(INT));
869 for (i = 0; i < count; i++) {
870 textWidth += lpDx[i];
872 if (GetTextExtentPoint32W( dev->hdc, str, count, &strSize ))
873 textHeight = strSize.cy;
875 else {
876 UINT i;
877 INT *dx = (INT *)((char*)pemr + pemr->emrtext.offDx);
878 SIZE charSize;
879 for (i = 0; i < count; i++) {
880 if (GetTextExtentPoint32W( dev->hdc, str + i, 1, &charSize )) {
881 dx[i] = charSize.cx;
882 textWidth += charSize.cx;
883 textHeight = max(textHeight, charSize.cy);
888 if (physDev->path)
890 pemr->rclBounds.left = pemr->rclBounds.top = 0;
891 pemr->rclBounds.right = pemr->rclBounds.bottom = -1;
892 goto no_bounds;
895 /* FIXME: handle font escapement */
896 switch (textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER)) {
897 case TA_CENTER: {
898 pemr->rclBounds.left = x - (textWidth / 2) - 1;
899 pemr->rclBounds.right = x + (textWidth / 2) + 1;
900 break;
902 case TA_RIGHT: {
903 pemr->rclBounds.left = x - textWidth - 1;
904 pemr->rclBounds.right = x;
905 break;
907 default: { /* TA_LEFT */
908 pemr->rclBounds.left = x;
909 pemr->rclBounds.right = x + textWidth + 1;
913 switch (textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE)) {
914 case TA_BASELINE: {
915 TEXTMETRICW tm;
916 if (!GetTextMetricsW( dev->hdc, &tm ))
917 tm.tmDescent = 0;
918 /* Play safe here... it's better to have a bounding box */
919 /* that is too big than too small. */
920 pemr->rclBounds.top = y - textHeight - 1;
921 pemr->rclBounds.bottom = y + tm.tmDescent + 1;
922 break;
924 case TA_BOTTOM: {
925 pemr->rclBounds.top = y - textHeight - 1;
926 pemr->rclBounds.bottom = y;
927 break;
929 default: { /* TA_TOP */
930 pemr->rclBounds.top = y;
931 pemr->rclBounds.bottom = y + textHeight + 1;
934 EMFDRV_UpdateBBox( dev, &pemr->rclBounds );
936 no_bounds:
937 ret = EMFDRV_WriteRecord( dev, &pemr->emr );
938 HeapFree( GetProcessHeap(), 0, pemr );
939 return ret;
942 /**********************************************************************
943 * EMFDRV_GradientFill
945 BOOL EMFDRV_GradientFill( PHYSDEV dev, TRIVERTEX *vert_array, ULONG nvert,
946 void *grad_array, ULONG ngrad, ULONG mode )
948 EMRGRADIENTFILL *emr;
949 ULONG i, pt, size, num_pts = ngrad * (mode == GRADIENT_FILL_TRIANGLE ? 3 : 2);
950 const ULONG *pts = (const ULONG *)grad_array;
951 BOOL ret;
953 size = FIELD_OFFSET(EMRGRADIENTFILL, Ver[nvert]) + num_pts * sizeof(pts[0]);
955 emr = HeapAlloc( GetProcessHeap(), 0, size );
956 if (!emr) return FALSE;
958 for (i = 0; i < num_pts; i++)
960 pt = pts[i];
962 if (i == 0)
964 emr->rclBounds.left = emr->rclBounds.right = vert_array[pt].x;
965 emr->rclBounds.top = emr->rclBounds.bottom = vert_array[pt].y;
967 else
969 if (vert_array[pt].x < emr->rclBounds.left)
970 emr->rclBounds.left = vert_array[pt].x;
971 else if (vert_array[pt].x > emr->rclBounds.right)
972 emr->rclBounds.right = vert_array[pt].x;
973 if (vert_array[pt].y < emr->rclBounds.top)
974 emr->rclBounds.top = vert_array[pt].y;
975 else if (vert_array[pt].y > emr->rclBounds.bottom)
976 emr->rclBounds.bottom = vert_array[pt].y;
979 emr->rclBounds.right--;
980 emr->rclBounds.bottom--;
982 emr->emr.iType = EMR_GRADIENTFILL;
983 emr->emr.nSize = size;
984 emr->nVer = nvert;
985 emr->nTri = ngrad;
986 emr->ulMode = mode;
987 memcpy( emr->Ver, vert_array, nvert * sizeof(vert_array[0]) );
988 memcpy( emr->Ver + nvert, pts, num_pts * sizeof(pts[0]) );
990 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
991 ret = EMFDRV_WriteRecord( dev, &emr->emr );
992 HeapFree( GetProcessHeap(), 0, emr );
993 return ret;
996 /**********************************************************************
997 * EMFDRV_FillPath
999 BOOL EMFDRV_FillPath( PHYSDEV dev )
1001 return emfdrv_stroke_and_fill_path( dev, EMR_FILLPATH );
1004 /**********************************************************************
1005 * EMFDRV_StrokeAndFillPath
1007 BOOL EMFDRV_StrokeAndFillPath( PHYSDEV dev )
1009 return emfdrv_stroke_and_fill_path( dev, EMR_STROKEANDFILLPATH );
1012 /**********************************************************************
1013 * EMFDRV_StrokePath
1015 BOOL EMFDRV_StrokePath( PHYSDEV dev )
1017 return emfdrv_stroke_and_fill_path( dev, EMR_STROKEPATH );