gdi32: Change get_gdi_flat_path() to return an opaque path pointer.
[wine.git] / dlls / gdi32 / enhmfdrv / graphics.c
blobf74e86b086b610526576fdf7b1b49771ff3b6276
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, HDC hdc )
74 UINT i;
76 if (hdc)
78 POINT cur_pt;
79 GetCurrentPositionEx( hdc, &cur_pt );
80 bounds->left = bounds->right = cur_pt.x;
81 bounds->top = bounds->bottom = cur_pt.y;
83 else if (count)
85 bounds->left = bounds->right = pts[0].x;
86 bounds->top = bounds->bottom = pts[0].y;
88 else *bounds = empty_bounds;
90 for (i = 0; i < count; i++)
92 bounds->left = min( bounds->left, pts[i].x );
93 bounds->right = max( bounds->right, pts[i].x );
94 bounds->top = min( bounds->top, pts[i].y );
95 bounds->bottom = max( bounds->bottom, pts[i].y );
99 /* helper for path stroke and fill functions */
100 static BOOL emfdrv_stroke_and_fill_path( PHYSDEV dev, INT type )
102 EMRSTROKEANDFILLPATH emr;
103 struct gdi_path *path;
104 POINT *points;
105 BYTE *flags;
107 emr.emr.iType = type;
108 emr.emr.nSize = sizeof(emr);
110 if ((path = get_gdi_flat_path( dev->hdc, NULL )))
112 int count = get_gdi_path_data( path, &points, &flags );
113 get_points_bounds( &emr.rclBounds, points, count, 0 );
114 free_gdi_path( path );
116 else emr.rclBounds = empty_bounds;
118 if (!EMFDRV_WriteRecord( dev, &emr.emr )) return FALSE;
119 if (!path) return FALSE;
120 EMFDRV_UpdateBBox( dev, &emr.rclBounds );
121 return TRUE;
124 /**********************************************************************
125 * EMFDRV_MoveTo
127 BOOL EMFDRV_MoveTo(PHYSDEV dev, INT x, INT y)
129 EMRMOVETOEX emr;
131 emr.emr.iType = EMR_MOVETOEX;
132 emr.emr.nSize = sizeof(emr);
133 emr.ptl.x = x;
134 emr.ptl.y = y;
136 return EMFDRV_WriteRecord( dev, &emr.emr );
139 /***********************************************************************
140 * EMFDRV_LineTo
142 BOOL EMFDRV_LineTo( PHYSDEV dev, INT x, INT y )
144 EMFDRV_PDEVICE *physDev = get_emf_physdev( 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 GetCurrentPositionEx( dev->hdc, &pt );
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 INT temp, xCentre, yCentre, i;
180 double angleStart, angleEnd;
181 double xinterStart, yinterStart, xinterEnd, yinterEnd;
182 EMRARC emr;
183 RECTL bounds;
185 if(left == right || top == bottom) return FALSE;
187 if(left > right) {temp = left; left = right; right = temp;}
188 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
190 if(GetGraphicsMode(dev->hdc) == GM_COMPATIBLE) {
191 right--;
192 bottom--;
195 emr.emr.iType = iType;
196 emr.emr.nSize = sizeof(emr);
197 emr.rclBox.left = left;
198 emr.rclBox.top = top;
199 emr.rclBox.right = right;
200 emr.rclBox.bottom = bottom;
201 emr.ptlStart.x = xstart;
202 emr.ptlStart.y = ystart;
203 emr.ptlEnd.x = xend;
204 emr.ptlEnd.y = yend;
207 /* Now calculate the BBox */
208 xCentre = (left + right + 1) / 2;
209 yCentre = (top + bottom + 1) / 2;
211 xstart -= xCentre;
212 ystart -= yCentre;
213 xend -= xCentre;
214 yend -= yCentre;
216 /* invert y co-ords to get angle anti-clockwise from x-axis */
217 angleStart = atan2( -(double)ystart, (double)xstart);
218 angleEnd = atan2( -(double)yend, (double)xend);
220 /* These are the intercepts of the start/end lines with the arc */
222 xinterStart = (right - left + 1)/2 * cos(angleStart) + xCentre;
223 yinterStart = -(bottom - top + 1)/2 * sin(angleStart) + yCentre;
224 xinterEnd = (right - left + 1)/2 * cos(angleEnd) + xCentre;
225 yinterEnd = -(bottom - top + 1)/2 * sin(angleEnd) + yCentre;
227 if(angleStart < 0) angleStart += 2 * M_PI;
228 if(angleEnd < 0) angleEnd += 2 * M_PI;
229 if(angleEnd < angleStart) angleEnd += 2 * M_PI;
231 bounds.left = min(xinterStart, xinterEnd);
232 bounds.top = min(yinterStart, yinterEnd);
233 bounds.right = max(xinterStart, xinterEnd);
234 bounds.bottom = max(yinterStart, yinterEnd);
236 for(i = 0; i <= 8; i++) {
237 if(i * M_PI / 2 < angleStart) /* loop until we're past start */
238 continue;
239 if(i * M_PI / 2 > angleEnd) /* if we're past end we're finished */
240 break;
242 /* the arc touches the rectangle at the start of quadrant i, so adjust
243 BBox to reflect this. */
245 switch(i % 4) {
246 case 0:
247 bounds.right = right;
248 break;
249 case 1:
250 bounds.top = top;
251 break;
252 case 2:
253 bounds.left = left;
254 break;
255 case 3:
256 bounds.bottom = bottom;
257 break;
261 /* If we're drawing a pie then make sure we include the centre */
262 if(iType == EMR_PIE) {
263 if(bounds.left > xCentre) bounds.left = xCentre;
264 else if(bounds.right < xCentre) bounds.right = xCentre;
265 if(bounds.top > yCentre) bounds.top = yCentre;
266 else if(bounds.bottom < yCentre) bounds.bottom = yCentre;
268 if (iType == EMR_ARCTO)
270 POINT pt;
271 GetCurrentPositionEx( dev->hdc, &pt );
272 bounds.left = min( bounds.left, pt.x );
273 bounds.top = min( bounds.top, pt.y );
274 bounds.right = max( bounds.right, pt.x );
275 bounds.bottom = max( bounds.bottom, pt.y );
277 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
278 return FALSE;
279 if(!physDev->path)
280 EMFDRV_UpdateBBox( dev, &bounds );
281 return TRUE;
285 /***********************************************************************
286 * EMFDRV_Arc
288 BOOL EMFDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
289 INT xstart, INT ystart, INT xend, INT yend )
291 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
292 xend, yend, EMR_ARC );
295 /***********************************************************************
296 * EMFDRV_ArcTo
298 BOOL EMFDRV_ArcTo( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
299 INT xstart, INT ystart, INT xend, INT yend )
301 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
302 xend, yend, EMR_ARCTO );
305 /***********************************************************************
306 * EMFDRV_Pie
308 BOOL EMFDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
309 INT xstart, INT ystart, INT xend, INT yend )
311 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
312 xend, yend, EMR_PIE );
316 /***********************************************************************
317 * EMFDRV_Chord
319 BOOL EMFDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
320 INT xstart, INT ystart, INT xend, INT yend )
322 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
323 xend, yend, EMR_CHORD );
326 /***********************************************************************
327 * EMFDRV_AngleArc
329 BOOL EMFDRV_AngleArc( PHYSDEV dev, INT x, INT y, DWORD radius, FLOAT start, FLOAT sweep )
331 EMRANGLEARC emr;
333 emr.emr.iType = EMR_ANGLEARC;
334 emr.emr.nSize = sizeof( emr );
335 emr.ptlCenter.x = x;
336 emr.ptlCenter.y = y;
337 emr.nRadius = radius;
338 emr.eStartAngle = start;
339 emr.eSweepAngle = sweep;
341 return EMFDRV_WriteRecord( dev, &emr.emr );
344 /***********************************************************************
345 * EMFDRV_Ellipse
347 BOOL EMFDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
349 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
350 EMRELLIPSE emr;
351 INT temp;
353 TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
355 if(left == right || top == bottom) return FALSE;
357 if(left > right) {temp = left; left = right; right = temp;}
358 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
360 if(GetGraphicsMode( dev->hdc ) == GM_COMPATIBLE) {
361 right--;
362 bottom--;
365 emr.emr.iType = EMR_ELLIPSE;
366 emr.emr.nSize = sizeof(emr);
367 emr.rclBox.left = left;
368 emr.rclBox.top = top;
369 emr.rclBox.right = right;
370 emr.rclBox.bottom = bottom;
372 if(!physDev->path)
373 EMFDRV_UpdateBBox( dev, &emr.rclBox );
374 return EMFDRV_WriteRecord( dev, &emr.emr );
377 /***********************************************************************
378 * EMFDRV_Rectangle
380 BOOL EMFDRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
382 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
383 EMRRECTANGLE emr;
384 INT temp;
386 TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
388 if(left == right || top == bottom) return FALSE;
390 if(left > right) {temp = left; left = right; right = temp;}
391 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
393 if(GetGraphicsMode( dev->hdc ) == GM_COMPATIBLE) {
394 right--;
395 bottom--;
398 emr.emr.iType = EMR_RECTANGLE;
399 emr.emr.nSize = sizeof(emr);
400 emr.rclBox.left = left;
401 emr.rclBox.top = top;
402 emr.rclBox.right = right;
403 emr.rclBox.bottom = bottom;
405 if(!physDev->path)
406 EMFDRV_UpdateBBox( dev, &emr.rclBox );
407 return EMFDRV_WriteRecord( dev, &emr.emr );
410 /***********************************************************************
411 * EMFDRV_RoundRect
413 BOOL EMFDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
414 INT bottom, INT ell_width, INT ell_height )
416 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
417 EMRROUNDRECT emr;
418 INT temp;
420 if(left == right || top == bottom) return FALSE;
422 if(left > right) {temp = left; left = right; right = temp;}
423 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
425 if(GetGraphicsMode( dev->hdc ) == GM_COMPATIBLE) {
426 right--;
427 bottom--;
430 emr.emr.iType = EMR_ROUNDRECT;
431 emr.emr.nSize = sizeof(emr);
432 emr.rclBox.left = left;
433 emr.rclBox.top = top;
434 emr.rclBox.right = right;
435 emr.rclBox.bottom = bottom;
436 emr.szlCorner.cx = ell_width;
437 emr.szlCorner.cy = ell_height;
439 if(!physDev->path)
440 EMFDRV_UpdateBBox( dev, &emr.rclBox );
441 return EMFDRV_WriteRecord( dev, &emr.emr );
444 /***********************************************************************
445 * EMFDRV_SetPixel
447 COLORREF EMFDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
449 EMRSETPIXELV emr;
451 emr.emr.iType = EMR_SETPIXELV;
452 emr.emr.nSize = sizeof(emr);
453 emr.ptlPixel.x = x;
454 emr.ptlPixel.y = y;
455 emr.crColor = color;
457 if (EMFDRV_WriteRecord( dev, &emr.emr )) {
458 RECTL bounds;
459 bounds.left = bounds.right = x;
460 bounds.top = bounds.bottom = y;
461 EMFDRV_UpdateBBox( dev, &bounds );
462 return color;
464 return -1;
467 /**********************************************************************
468 * EMFDRV_Polylinegon
470 * Helper for EMFDRV_Poly{line|gon}
472 static BOOL
473 EMFDRV_Polylinegon( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
475 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
476 EMRPOLYLINE *emr;
477 DWORD size;
478 BOOL ret, use_small_emr = can_use_short_points( pt, count );
480 size = use_small_emr ? offsetof( EMRPOLYLINE16, apts[count] ) : offsetof( EMRPOLYLINE, aptl[count] );
482 emr = HeapAlloc( GetProcessHeap(), 0, size );
483 emr->emr.iType = use_small_emr ? iType + EMR_POLYLINE16 - EMR_POLYLINE : iType;
484 emr->emr.nSize = size;
485 emr->cptl = count;
487 store_points( emr->aptl, pt, count, use_small_emr );
489 if (!physDev->path)
490 get_points_bounds( &emr->rclBounds, pt, count,
491 (iType == EMR_POLYBEZIERTO || iType == EMR_POLYLINETO) ? dev->hdc : 0 );
492 else
493 emr->rclBounds = empty_bounds;
495 ret = EMFDRV_WriteRecord( dev, &emr->emr );
496 if (ret && !physDev->path)
497 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
498 HeapFree( GetProcessHeap(), 0, emr );
499 return ret;
503 /**********************************************************************
504 * EMFDRV_Polyline
506 BOOL EMFDRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
508 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINE );
511 /**********************************************************************
512 * EMFDRV_PolylineTo
514 BOOL EMFDRV_PolylineTo( PHYSDEV dev, const POINT* pt, INT count )
516 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINETO );
519 /**********************************************************************
520 * EMFDRV_Polygon
522 BOOL EMFDRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
524 if(count < 2) return FALSE;
525 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYGON );
528 /**********************************************************************
529 * EMFDRV_PolyBezier
531 BOOL EMFDRV_PolyBezier( PHYSDEV dev, const POINT *pts, DWORD count )
533 return EMFDRV_Polylinegon( dev, pts, count, EMR_POLYBEZIER );
536 /**********************************************************************
537 * EMFDRV_PolyBezierTo
539 BOOL EMFDRV_PolyBezierTo( PHYSDEV dev, const POINT *pts, DWORD count )
541 return EMFDRV_Polylinegon( dev, pts, count, EMR_POLYBEZIERTO );
545 /**********************************************************************
546 * EMFDRV_PolyPolylinegon
548 * Helper for EMFDRV_PolyPoly{line|gon}
550 static BOOL
551 EMFDRV_PolyPolylinegon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys,
552 DWORD iType)
554 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
555 EMRPOLYPOLYLINE *emr;
556 DWORD cptl = 0, poly, size;
557 BOOL ret, use_small_emr, bounds_valid = TRUE;
559 for(poly = 0; poly < polys; poly++) {
560 cptl += counts[poly];
561 if(counts[poly] < 2) bounds_valid = FALSE;
563 if(!cptl) bounds_valid = FALSE;
564 use_small_emr = can_use_short_points( pt, cptl );
566 size = FIELD_OFFSET(EMRPOLYPOLYLINE, aPolyCounts[polys]);
567 if(use_small_emr)
568 size += cptl * sizeof(POINTS);
569 else
570 size += cptl * sizeof(POINTL);
572 emr = HeapAlloc( GetProcessHeap(), 0, size );
574 emr->emr.iType = iType;
575 if(use_small_emr) emr->emr.iType += EMR_POLYPOLYLINE16 - EMR_POLYPOLYLINE;
577 emr->emr.nSize = size;
578 if(bounds_valid && !physDev->path)
579 get_points_bounds( &emr->rclBounds, pt, cptl, 0 );
580 else
581 emr->rclBounds = empty_bounds;
582 emr->nPolys = polys;
583 emr->cptl = cptl;
585 if(polys)
587 memcpy( emr->aPolyCounts, counts, polys * sizeof(DWORD) );
588 store_points( (POINTL *)(emr->aPolyCounts + polys), pt, cptl, use_small_emr );
591 ret = EMFDRV_WriteRecord( dev, &emr->emr );
592 if(ret && !bounds_valid)
594 ret = FALSE;
595 SetLastError( ERROR_INVALID_PARAMETER );
597 if(ret && !physDev->path)
598 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
599 HeapFree( GetProcessHeap(), 0, emr );
600 return ret;
603 /**********************************************************************
604 * EMFDRV_PolyPolyline
606 BOOL EMFDRV_PolyPolyline(PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polys)
608 return EMFDRV_PolyPolylinegon( dev, pt, (const INT *)counts, polys,
609 EMR_POLYPOLYLINE );
612 /**********************************************************************
613 * EMFDRV_PolyPolygon
615 BOOL EMFDRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys )
617 return EMFDRV_PolyPolylinegon( dev, pt, counts, polys, EMR_POLYPOLYGON );
621 /**********************************************************************
622 * EMFDRV_PolyDraw
624 BOOL EMFDRV_PolyDraw( PHYSDEV dev, const POINT *pts, const BYTE *types, DWORD count )
626 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
627 EMRPOLYDRAW *emr;
628 BOOL ret;
629 BYTE *types_dest;
630 BOOL use_small_emr = can_use_short_points( pts, count );
631 DWORD size;
633 size = use_small_emr ? offsetof( EMRPOLYDRAW16, apts[count] ) : offsetof( EMRPOLYDRAW, aptl[count] );
634 size += (count + 3) & ~3;
636 if (!(emr = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
638 emr->emr.iType = use_small_emr ? EMR_POLYDRAW16 : EMR_POLYDRAW;
639 emr->emr.nSize = size;
640 emr->cptl = count;
642 types_dest = store_points( emr->aptl, pts, count, use_small_emr );
643 memcpy( types_dest, types, count );
644 if (count & 3) memset( types_dest + count, 0, 4 - (count & 3) );
646 if (!physDev->path)
647 get_points_bounds( &emr->rclBounds, pts, count, 0 );
648 else
649 emr->rclBounds = empty_bounds;
651 ret = EMFDRV_WriteRecord( dev, &emr->emr );
652 if (ret && !physDev->path) EMFDRV_UpdateBBox( dev, &emr->rclBounds );
653 HeapFree( GetProcessHeap(), 0, emr );
654 return ret;
658 /**********************************************************************
659 * EMFDRV_ExtFloodFill
661 BOOL EMFDRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
663 EMREXTFLOODFILL emr;
665 emr.emr.iType = EMR_EXTFLOODFILL;
666 emr.emr.nSize = sizeof(emr);
667 emr.ptlStart.x = x;
668 emr.ptlStart.y = y;
669 emr.crColor = color;
670 emr.iMode = fillType;
672 return EMFDRV_WriteRecord( dev, &emr.emr );
676 /*********************************************************************
677 * EMFDRV_FillRgn
679 BOOL EMFDRV_FillRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush )
681 EMRFILLRGN *emr;
682 DWORD size, rgnsize, index;
683 BOOL ret;
685 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
686 if(!index) return FALSE;
688 rgnsize = GetRegionData( hrgn, 0, NULL );
689 size = rgnsize + offsetof(EMRFILLRGN,RgnData);
690 emr = HeapAlloc( GetProcessHeap(), 0, size );
692 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
694 emr->emr.iType = EMR_FILLRGN;
695 emr->emr.nSize = size;
696 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
697 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
698 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
699 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
700 emr->cbRgnData = rgnsize;
701 emr->ihBrush = index;
703 ret = EMFDRV_WriteRecord( dev, &emr->emr );
704 if(ret)
705 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
706 HeapFree( GetProcessHeap(), 0, emr );
707 return ret;
709 /*********************************************************************
710 * EMFDRV_FrameRgn
712 BOOL EMFDRV_FrameRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush, INT width, INT height )
714 EMRFRAMERGN *emr;
715 DWORD size, rgnsize, index;
716 BOOL ret;
718 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
719 if(!index) return FALSE;
721 rgnsize = GetRegionData( hrgn, 0, NULL );
722 size = rgnsize + offsetof(EMRFRAMERGN,RgnData);
723 emr = HeapAlloc( GetProcessHeap(), 0, size );
725 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
727 emr->emr.iType = EMR_FRAMERGN;
728 emr->emr.nSize = size;
729 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
730 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
731 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
732 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
733 emr->cbRgnData = rgnsize;
734 emr->ihBrush = index;
735 emr->szlStroke.cx = width;
736 emr->szlStroke.cy = height;
738 ret = EMFDRV_WriteRecord( dev, &emr->emr );
739 if(ret)
740 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
741 HeapFree( GetProcessHeap(), 0, emr );
742 return ret;
745 /*********************************************************************
746 * EMFDRV_PaintInvertRgn
748 * Helper for EMFDRV_{Paint|Invert}Rgn
750 static BOOL EMFDRV_PaintInvertRgn( PHYSDEV dev, HRGN hrgn, DWORD iType )
752 EMRINVERTRGN *emr;
753 DWORD size, rgnsize;
754 BOOL ret;
757 rgnsize = GetRegionData( hrgn, 0, NULL );
758 size = rgnsize + offsetof(EMRINVERTRGN,RgnData);
759 emr = HeapAlloc( GetProcessHeap(), 0, size );
761 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
763 emr->emr.iType = iType;
764 emr->emr.nSize = size;
765 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
766 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
767 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
768 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
769 emr->cbRgnData = rgnsize;
771 ret = EMFDRV_WriteRecord( dev, &emr->emr );
772 if(ret)
773 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
774 HeapFree( GetProcessHeap(), 0, emr );
775 return ret;
778 /**********************************************************************
779 * EMFDRV_PaintRgn
781 BOOL EMFDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
783 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_PAINTRGN );
786 /**********************************************************************
787 * EMFDRV_InvertRgn
789 BOOL EMFDRV_InvertRgn( PHYSDEV dev, HRGN hrgn )
791 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_INVERTRGN );
794 /**********************************************************************
795 * EMFDRV_ExtTextOut
797 BOOL EMFDRV_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags, const RECT *lprect,
798 LPCWSTR str, UINT count, const INT *lpDx )
800 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
801 EMREXTTEXTOUTW *pemr;
802 DWORD nSize;
803 BOOL ret;
804 int textHeight = 0;
805 int textWidth = 0;
806 const UINT textAlign = GetTextAlign( dev->hdc );
807 const INT graphicsMode = GetGraphicsMode( dev->hdc );
808 FLOAT exScale, eyScale;
810 nSize = sizeof(*pemr) + ((count+1) & ~1) * sizeof(WCHAR) + count * sizeof(INT);
812 TRACE("%s %s count %d nSize = %d\n", debugstr_wn(str, count),
813 wine_dbgstr_rect(lprect), count, nSize);
814 pemr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nSize);
816 if (graphicsMode == GM_COMPATIBLE)
818 const INT horzSize = GetDeviceCaps( dev->hdc, HORZSIZE );
819 const INT horzRes = GetDeviceCaps( dev->hdc, HORZRES );
820 const INT vertSize = GetDeviceCaps( dev->hdc, VERTSIZE );
821 const INT vertRes = GetDeviceCaps( dev->hdc, VERTRES );
822 SIZE wndext, vportext;
824 GetViewportExtEx( dev->hdc, &vportext );
825 GetWindowExtEx( dev->hdc, &wndext );
826 exScale = 100.0 * ((FLOAT)horzSize / (FLOAT)horzRes) /
827 ((FLOAT)wndext.cx / (FLOAT)vportext.cx);
828 eyScale = 100.0 * ((FLOAT)vertSize / (FLOAT)vertRes) /
829 ((FLOAT)wndext.cy / (FLOAT)vportext.cy);
831 else
833 exScale = 0.0;
834 eyScale = 0.0;
837 pemr->emr.iType = EMR_EXTTEXTOUTW;
838 pemr->emr.nSize = nSize;
839 pemr->iGraphicsMode = graphicsMode;
840 pemr->exScale = exScale;
841 pemr->eyScale = eyScale;
842 pemr->emrtext.ptlReference.x = x;
843 pemr->emrtext.ptlReference.y = y;
844 pemr->emrtext.nChars = count;
845 pemr->emrtext.offString = sizeof(*pemr);
846 memcpy((char*)pemr + pemr->emrtext.offString, str, count * sizeof(WCHAR));
847 pemr->emrtext.fOptions = flags;
848 if(!lprect) {
849 pemr->emrtext.rcl.left = pemr->emrtext.rcl.top = 0;
850 pemr->emrtext.rcl.right = pemr->emrtext.rcl.bottom = -1;
851 } else {
852 pemr->emrtext.rcl.left = lprect->left;
853 pemr->emrtext.rcl.top = lprect->top;
854 pemr->emrtext.rcl.right = lprect->right;
855 pemr->emrtext.rcl.bottom = lprect->bottom;
858 pemr->emrtext.offDx = pemr->emrtext.offString + ((count+1) & ~1) * sizeof(WCHAR);
859 if(lpDx) {
860 UINT i;
861 SIZE strSize;
862 memcpy((char*)pemr + pemr->emrtext.offDx, lpDx, count * sizeof(INT));
863 for (i = 0; i < count; i++) {
864 textWidth += lpDx[i];
866 if (GetTextExtentPoint32W( dev->hdc, str, count, &strSize ))
867 textHeight = strSize.cy;
869 else {
870 UINT i;
871 INT *dx = (INT *)((char*)pemr + pemr->emrtext.offDx);
872 SIZE charSize;
873 for (i = 0; i < count; i++) {
874 if (GetTextExtentPoint32W( dev->hdc, str + i, 1, &charSize )) {
875 dx[i] = charSize.cx;
876 textWidth += charSize.cx;
877 textHeight = max(textHeight, charSize.cy);
882 if (physDev->path)
884 pemr->rclBounds.left = pemr->rclBounds.top = 0;
885 pemr->rclBounds.right = pemr->rclBounds.bottom = -1;
886 goto no_bounds;
889 /* FIXME: handle font escapement */
890 switch (textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER)) {
891 case TA_CENTER: {
892 pemr->rclBounds.left = x - (textWidth / 2) - 1;
893 pemr->rclBounds.right = x + (textWidth / 2) + 1;
894 break;
896 case TA_RIGHT: {
897 pemr->rclBounds.left = x - textWidth - 1;
898 pemr->rclBounds.right = x;
899 break;
901 default: { /* TA_LEFT */
902 pemr->rclBounds.left = x;
903 pemr->rclBounds.right = x + textWidth + 1;
907 switch (textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE)) {
908 case TA_BASELINE: {
909 TEXTMETRICW tm;
910 if (!GetTextMetricsW( dev->hdc, &tm ))
911 tm.tmDescent = 0;
912 /* Play safe here... it's better to have a bounding box */
913 /* that is too big than too small. */
914 pemr->rclBounds.top = y - textHeight - 1;
915 pemr->rclBounds.bottom = y + tm.tmDescent + 1;
916 break;
918 case TA_BOTTOM: {
919 pemr->rclBounds.top = y - textHeight - 1;
920 pemr->rclBounds.bottom = y;
921 break;
923 default: { /* TA_TOP */
924 pemr->rclBounds.top = y;
925 pemr->rclBounds.bottom = y + textHeight + 1;
928 EMFDRV_UpdateBBox( dev, &pemr->rclBounds );
930 no_bounds:
931 ret = EMFDRV_WriteRecord( dev, &pemr->emr );
932 HeapFree( GetProcessHeap(), 0, pemr );
933 return ret;
936 /**********************************************************************
937 * EMFDRV_GradientFill
939 BOOL EMFDRV_GradientFill( PHYSDEV dev, TRIVERTEX *vert_array, ULONG nvert,
940 void *grad_array, ULONG ngrad, ULONG mode )
942 EMRGRADIENTFILL *emr;
943 ULONG i, pt, size, num_pts = ngrad * (mode == GRADIENT_FILL_TRIANGLE ? 3 : 2);
944 const ULONG *pts = (const ULONG *)grad_array;
945 BOOL ret;
947 size = FIELD_OFFSET(EMRGRADIENTFILL, Ver[nvert]) + num_pts * sizeof(pts[0]);
949 emr = HeapAlloc( GetProcessHeap(), 0, size );
950 if (!emr) return FALSE;
952 for (i = 0; i < num_pts; i++)
954 pt = pts[i];
956 if (i == 0)
958 emr->rclBounds.left = emr->rclBounds.right = vert_array[pt].x;
959 emr->rclBounds.top = emr->rclBounds.bottom = vert_array[pt].y;
961 else
963 if (vert_array[pt].x < emr->rclBounds.left)
964 emr->rclBounds.left = vert_array[pt].x;
965 else if (vert_array[pt].x > emr->rclBounds.right)
966 emr->rclBounds.right = vert_array[pt].x;
967 if (vert_array[pt].y < emr->rclBounds.top)
968 emr->rclBounds.top = vert_array[pt].y;
969 else if (vert_array[pt].y > emr->rclBounds.bottom)
970 emr->rclBounds.bottom = vert_array[pt].y;
973 emr->rclBounds.right--;
974 emr->rclBounds.bottom--;
976 emr->emr.iType = EMR_GRADIENTFILL;
977 emr->emr.nSize = size;
978 emr->nVer = nvert;
979 emr->nTri = ngrad;
980 emr->ulMode = mode;
981 memcpy( emr->Ver, vert_array, nvert * sizeof(vert_array[0]) );
982 memcpy( emr->Ver + nvert, pts, num_pts * sizeof(pts[0]) );
984 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
985 ret = EMFDRV_WriteRecord( dev, &emr->emr );
986 HeapFree( GetProcessHeap(), 0, emr );
987 return ret;
990 /**********************************************************************
991 * EMFDRV_FillPath
993 BOOL EMFDRV_FillPath( PHYSDEV dev )
995 return emfdrv_stroke_and_fill_path( dev, EMR_FILLPATH );
998 /**********************************************************************
999 * EMFDRV_StrokeAndFillPath
1001 BOOL EMFDRV_StrokeAndFillPath( PHYSDEV dev )
1003 return emfdrv_stroke_and_fill_path( dev, EMR_STROKEANDFILLPATH );
1006 /**********************************************************************
1007 * EMFDRV_StrokePath
1009 BOOL EMFDRV_StrokePath( PHYSDEV dev )
1011 return emfdrv_stroke_and_fill_path( dev, EMR_STROKEPATH );