mf/session: Forward more events to the application.
[wine/zf.git] / dlls / gdi32 / enhmfdrv / graphics.c
blob148030cea3b75759bdbe91fdaa50fc221ec4649a
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 <stdarg.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "enhmfdrv/enhmetafiledrv.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
33 static const RECTL empty_bounds = { 0, 0, -1, -1 };
35 /* determine if we can use 16-bit points to store all the input points */
36 static BOOL can_use_short_points( const POINT *pts, UINT count )
38 UINT i;
40 for (i = 0; i < count; i++)
41 if (((pts[i].x + 0x8000) & ~0xffff) || ((pts[i].y + 0x8000) & ~0xffff))
42 return FALSE;
43 return TRUE;
46 /* store points in either long or short format; return a pointer to the end of the stored data */
47 static void *store_points( POINTL *dest, const POINT *pts, UINT count, BOOL short_points )
49 if (short_points)
51 UINT i;
52 POINTS *dest_short = (POINTS *)dest;
54 for (i = 0; i < count; i++)
56 dest_short[i].x = pts[i].x;
57 dest_short[i].y = pts[i].y;
59 return dest_short + count;
61 else
63 memcpy( dest, pts, count * sizeof(*dest) );
64 return dest + count;
68 /* compute the bounds of an array of points, optionally including the current position */
69 static void get_points_bounds( RECTL *bounds, const POINT *pts, UINT count, DC *dc )
71 UINT i;
73 if (dc)
75 bounds->left = bounds->right = dc->cur_pos.x;
76 bounds->top = bounds->bottom = dc->cur_pos.y;
78 else if (count)
80 bounds->left = bounds->right = pts[0].x;
81 bounds->top = bounds->bottom = pts[0].y;
83 else *bounds = empty_bounds;
85 for (i = 0; i < count; i++)
87 bounds->left = min( bounds->left, pts[i].x );
88 bounds->right = max( bounds->right, pts[i].x );
89 bounds->top = min( bounds->top, pts[i].y );
90 bounds->bottom = max( bounds->bottom, pts[i].y );
94 /* helper for path stroke and fill functions */
95 static BOOL emfdrv_stroke_and_fill_path( PHYSDEV dev, INT type )
97 DC *dc = get_physdev_dc( dev );
98 EMRSTROKEANDFILLPATH emr;
99 struct gdi_path *path;
100 POINT *points;
101 BYTE *flags;
103 emr.emr.iType = type;
104 emr.emr.nSize = sizeof(emr);
106 if ((path = get_gdi_flat_path( dc, NULL )))
108 int count = get_gdi_path_data( path, &points, &flags );
109 get_points_bounds( &emr.rclBounds, points, count, 0 );
110 free_gdi_path( path );
112 else emr.rclBounds = empty_bounds;
114 if (!EMFDRV_WriteRecord( dev, &emr.emr )) return FALSE;
115 if (!path) return FALSE;
116 EMFDRV_UpdateBBox( dev, &emr.rclBounds );
117 return TRUE;
120 /**********************************************************************
121 * EMFDRV_MoveTo
123 BOOL CDECL EMFDRV_MoveTo(PHYSDEV dev, INT x, INT y)
125 EMRMOVETOEX emr;
127 emr.emr.iType = EMR_MOVETOEX;
128 emr.emr.nSize = sizeof(emr);
129 emr.ptl.x = x;
130 emr.ptl.y = y;
132 return EMFDRV_WriteRecord( dev, &emr.emr );
135 /***********************************************************************
136 * EMFDRV_LineTo
138 BOOL CDECL EMFDRV_LineTo( PHYSDEV dev, INT x, INT y )
140 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
141 DC *dc = get_physdev_dc( dev );
142 POINT pt;
143 EMRLINETO emr;
144 RECTL bounds;
146 emr.emr.iType = EMR_LINETO;
147 emr.emr.nSize = sizeof(emr);
148 emr.ptl.x = x;
149 emr.ptl.y = y;
151 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
152 return FALSE;
154 pt = dc->cur_pos;
156 bounds.left = min(x, pt.x);
157 bounds.top = min(y, pt.y);
158 bounds.right = max(x, pt.x);
159 bounds.bottom = max(y, pt.y);
161 if(!physDev->path)
162 EMFDRV_UpdateBBox( dev, &bounds );
164 return TRUE;
168 /***********************************************************************
169 * EMFDRV_ArcChordPie
171 static BOOL
172 EMFDRV_ArcChordPie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
173 INT xstart, INT ystart, INT xend, INT yend, DWORD iType )
175 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
176 DC *dc = get_physdev_dc( dev );
177 INT temp, xCentre, yCentre, i;
178 double angleStart, angleEnd;
179 double xinterStart, yinterStart, xinterEnd, yinterEnd;
180 EMRARC emr;
181 RECTL bounds;
183 if(left == right || top == bottom) return FALSE;
185 if(left > right) {temp = left; left = right; right = temp;}
186 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
188 if(dc->GraphicsMode == GM_COMPATIBLE) {
189 right--;
190 bottom--;
193 emr.emr.iType = iType;
194 emr.emr.nSize = sizeof(emr);
195 emr.rclBox.left = left;
196 emr.rclBox.top = top;
197 emr.rclBox.right = right;
198 emr.rclBox.bottom = bottom;
199 emr.ptlStart.x = xstart;
200 emr.ptlStart.y = ystart;
201 emr.ptlEnd.x = xend;
202 emr.ptlEnd.y = yend;
205 /* Now calculate the BBox */
206 xCentre = (left + right + 1) / 2;
207 yCentre = (top + bottom + 1) / 2;
209 xstart -= xCentre;
210 ystart -= yCentre;
211 xend -= xCentre;
212 yend -= yCentre;
214 /* invert y co-ords to get angle anti-clockwise from x-axis */
215 angleStart = atan2( -(double)ystart, (double)xstart);
216 angleEnd = atan2( -(double)yend, (double)xend);
218 /* These are the intercepts of the start/end lines with the arc */
220 xinterStart = (right - left + 1)/2 * cos(angleStart) + xCentre;
221 yinterStart = -(bottom - top + 1)/2 * sin(angleStart) + yCentre;
222 xinterEnd = (right - left + 1)/2 * cos(angleEnd) + xCentre;
223 yinterEnd = -(bottom - top + 1)/2 * sin(angleEnd) + yCentre;
225 if(angleStart < 0) angleStart += 2 * M_PI;
226 if(angleEnd < 0) angleEnd += 2 * M_PI;
227 if(angleEnd < angleStart) angleEnd += 2 * M_PI;
229 bounds.left = min(xinterStart, xinterEnd);
230 bounds.top = min(yinterStart, yinterEnd);
231 bounds.right = max(xinterStart, xinterEnd);
232 bounds.bottom = max(yinterStart, yinterEnd);
234 for(i = 0; i <= 8; i++) {
235 if(i * M_PI / 2 < angleStart) /* loop until we're past start */
236 continue;
237 if(i * M_PI / 2 > angleEnd) /* if we're past end we're finished */
238 break;
240 /* the arc touches the rectangle at the start of quadrant i, so adjust
241 BBox to reflect this. */
243 switch(i % 4) {
244 case 0:
245 bounds.right = right;
246 break;
247 case 1:
248 bounds.top = top;
249 break;
250 case 2:
251 bounds.left = left;
252 break;
253 case 3:
254 bounds.bottom = bottom;
255 break;
259 /* If we're drawing a pie then make sure we include the centre */
260 if(iType == EMR_PIE) {
261 if(bounds.left > xCentre) bounds.left = xCentre;
262 else if(bounds.right < xCentre) bounds.right = xCentre;
263 if(bounds.top > yCentre) bounds.top = yCentre;
264 else if(bounds.bottom < yCentre) bounds.bottom = yCentre;
266 if (iType == EMR_ARCTO)
268 POINT pt;
269 pt = dc->cur_pos;
270 bounds.left = min( bounds.left, pt.x );
271 bounds.top = min( bounds.top, pt.y );
272 bounds.right = max( bounds.right, pt.x );
273 bounds.bottom = max( bounds.bottom, pt.y );
275 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
276 return FALSE;
277 if(!physDev->path)
278 EMFDRV_UpdateBBox( dev, &bounds );
279 return TRUE;
283 /***********************************************************************
284 * EMFDRV_Arc
286 BOOL CDECL EMFDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
287 INT xstart, INT ystart, INT xend, INT yend )
289 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
290 xend, yend, EMR_ARC );
293 /***********************************************************************
294 * EMFDRV_ArcTo
296 BOOL CDECL EMFDRV_ArcTo( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
297 INT xstart, INT ystart, INT xend, INT yend )
299 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
300 xend, yend, EMR_ARCTO );
303 /***********************************************************************
304 * EMFDRV_Pie
306 BOOL CDECL EMFDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
307 INT xstart, INT ystart, INT xend, INT yend )
309 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
310 xend, yend, EMR_PIE );
314 /***********************************************************************
315 * EMFDRV_Chord
317 BOOL CDECL EMFDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
318 INT xstart, INT ystart, INT xend, INT yend )
320 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
321 xend, yend, EMR_CHORD );
324 /***********************************************************************
325 * EMFDRV_AngleArc
327 BOOL CDECL EMFDRV_AngleArc( PHYSDEV dev, INT x, INT y, DWORD radius, FLOAT start, FLOAT sweep )
329 EMRANGLEARC emr;
331 emr.emr.iType = EMR_ANGLEARC;
332 emr.emr.nSize = sizeof( emr );
333 emr.ptlCenter.x = x;
334 emr.ptlCenter.y = y;
335 emr.nRadius = radius;
336 emr.eStartAngle = start;
337 emr.eSweepAngle = sweep;
339 return EMFDRV_WriteRecord( dev, &emr.emr );
342 /***********************************************************************
343 * EMFDRV_Ellipse
345 BOOL CDECL EMFDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
347 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
348 DC *dc = get_physdev_dc( dev );
349 EMRELLIPSE emr;
350 INT temp;
352 TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
354 if(left == right || top == bottom) return FALSE;
356 if(left > right) {temp = left; left = right; right = temp;}
357 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
359 if(dc->GraphicsMode == GM_COMPATIBLE) {
360 right--;
361 bottom--;
364 emr.emr.iType = EMR_ELLIPSE;
365 emr.emr.nSize = sizeof(emr);
366 emr.rclBox.left = left;
367 emr.rclBox.top = top;
368 emr.rclBox.right = right;
369 emr.rclBox.bottom = bottom;
371 if(!physDev->path)
372 EMFDRV_UpdateBBox( dev, &emr.rclBox );
373 return EMFDRV_WriteRecord( dev, &emr.emr );
376 /***********************************************************************
377 * EMFDRV_Rectangle
379 BOOL CDECL EMFDRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
381 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
382 DC *dc = get_physdev_dc( 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(dc->GraphicsMode == 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 CDECL 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 DC *dc = get_physdev_dc( dev );
418 EMRROUNDRECT emr;
419 INT temp;
421 if(left == right || top == bottom) return FALSE;
423 if(left > right) {temp = left; left = right; right = temp;}
424 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
426 if(dc->GraphicsMode == GM_COMPATIBLE) {
427 right--;
428 bottom--;
431 emr.emr.iType = EMR_ROUNDRECT;
432 emr.emr.nSize = sizeof(emr);
433 emr.rclBox.left = left;
434 emr.rclBox.top = top;
435 emr.rclBox.right = right;
436 emr.rclBox.bottom = bottom;
437 emr.szlCorner.cx = ell_width;
438 emr.szlCorner.cy = ell_height;
440 if(!physDev->path)
441 EMFDRV_UpdateBBox( dev, &emr.rclBox );
442 return EMFDRV_WriteRecord( dev, &emr.emr );
445 /***********************************************************************
446 * EMFDRV_SetPixel
448 COLORREF CDECL EMFDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
450 EMRSETPIXELV emr;
452 emr.emr.iType = EMR_SETPIXELV;
453 emr.emr.nSize = sizeof(emr);
454 emr.ptlPixel.x = x;
455 emr.ptlPixel.y = y;
456 emr.crColor = color;
458 if (EMFDRV_WriteRecord( dev, &emr.emr )) {
459 RECTL bounds;
460 bounds.left = bounds.right = x;
461 bounds.top = bounds.bottom = y;
462 EMFDRV_UpdateBBox( dev, &bounds );
463 return color;
465 return -1;
468 /**********************************************************************
469 * EMFDRV_Polylinegon
471 * Helper for EMFDRV_Poly{line|gon}
473 static BOOL
474 EMFDRV_Polylinegon( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
476 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
477 DC *dc = get_physdev_dc( dev );
478 EMRPOLYLINE *emr;
479 DWORD size;
480 BOOL ret, use_small_emr = can_use_short_points( pt, count );
482 size = use_small_emr ? offsetof( EMRPOLYLINE16, apts[count] ) : offsetof( EMRPOLYLINE, aptl[count] );
484 emr = HeapAlloc( GetProcessHeap(), 0, size );
485 emr->emr.iType = use_small_emr ? iType + EMR_POLYLINE16 - EMR_POLYLINE : iType;
486 emr->emr.nSize = size;
487 emr->cptl = count;
489 store_points( emr->aptl, pt, count, use_small_emr );
491 if (!physDev->path)
492 get_points_bounds( &emr->rclBounds, pt, count,
493 (iType == EMR_POLYBEZIERTO || iType == EMR_POLYLINETO) ? dc : 0 );
494 else
495 emr->rclBounds = empty_bounds;
497 ret = EMFDRV_WriteRecord( dev, &emr->emr );
498 if (ret && !physDev->path)
499 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
500 HeapFree( GetProcessHeap(), 0, emr );
501 return ret;
505 /**********************************************************************
506 * EMFDRV_Polyline
508 BOOL CDECL EMFDRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
510 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINE );
513 /**********************************************************************
514 * EMFDRV_PolylineTo
516 BOOL CDECL EMFDRV_PolylineTo( PHYSDEV dev, const POINT* pt, INT count )
518 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINETO );
521 /**********************************************************************
522 * EMFDRV_Polygon
524 BOOL CDECL EMFDRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
526 if(count < 2) return FALSE;
527 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYGON );
530 /**********************************************************************
531 * EMFDRV_PolyBezier
533 BOOL CDECL EMFDRV_PolyBezier( PHYSDEV dev, const POINT *pts, DWORD count )
535 return EMFDRV_Polylinegon( dev, pts, count, EMR_POLYBEZIER );
538 /**********************************************************************
539 * EMFDRV_PolyBezierTo
541 BOOL CDECL EMFDRV_PolyBezierTo( PHYSDEV dev, const POINT *pts, DWORD count )
543 return EMFDRV_Polylinegon( dev, pts, count, EMR_POLYBEZIERTO );
547 /**********************************************************************
548 * EMFDRV_PolyPolylinegon
550 * Helper for EMFDRV_PolyPoly{line|gon}
552 static BOOL
553 EMFDRV_PolyPolylinegon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys,
554 DWORD iType)
556 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
557 EMRPOLYPOLYLINE *emr;
558 DWORD cptl = 0, poly, size;
559 BOOL ret, use_small_emr, bounds_valid = TRUE;
561 for(poly = 0; poly < polys; poly++) {
562 cptl += counts[poly];
563 if(counts[poly] < 2) bounds_valid = FALSE;
565 if(!cptl) bounds_valid = FALSE;
566 use_small_emr = can_use_short_points( pt, cptl );
568 size = FIELD_OFFSET(EMRPOLYPOLYLINE, aPolyCounts[polys]);
569 if(use_small_emr)
570 size += cptl * sizeof(POINTS);
571 else
572 size += cptl * sizeof(POINTL);
574 emr = HeapAlloc( GetProcessHeap(), 0, size );
576 emr->emr.iType = iType;
577 if(use_small_emr) emr->emr.iType += EMR_POLYPOLYLINE16 - EMR_POLYPOLYLINE;
579 emr->emr.nSize = size;
580 if(bounds_valid && !physDev->path)
581 get_points_bounds( &emr->rclBounds, pt, cptl, 0 );
582 else
583 emr->rclBounds = empty_bounds;
584 emr->nPolys = polys;
585 emr->cptl = cptl;
587 if(polys)
589 memcpy( emr->aPolyCounts, counts, polys * sizeof(DWORD) );
590 store_points( (POINTL *)(emr->aPolyCounts + polys), pt, cptl, use_small_emr );
593 ret = EMFDRV_WriteRecord( dev, &emr->emr );
594 if(ret && !bounds_valid)
596 ret = FALSE;
597 SetLastError( ERROR_INVALID_PARAMETER );
599 if(ret && !physDev->path)
600 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
601 HeapFree( GetProcessHeap(), 0, emr );
602 return ret;
605 /**********************************************************************
606 * EMFDRV_PolyPolyline
608 BOOL CDECL EMFDRV_PolyPolyline(PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polys)
610 return EMFDRV_PolyPolylinegon( dev, pt, (const INT *)counts, polys,
611 EMR_POLYPOLYLINE );
614 /**********************************************************************
615 * EMFDRV_PolyPolygon
617 BOOL CDECL EMFDRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys )
619 return EMFDRV_PolyPolylinegon( dev, pt, counts, polys, EMR_POLYPOLYGON );
623 /**********************************************************************
624 * EMFDRV_PolyDraw
626 BOOL CDECL EMFDRV_PolyDraw( PHYSDEV dev, const POINT *pts, const BYTE *types, DWORD count )
628 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
629 EMRPOLYDRAW *emr;
630 BOOL ret;
631 BYTE *types_dest;
632 BOOL use_small_emr = can_use_short_points( pts, count );
633 DWORD size;
635 size = use_small_emr ? offsetof( EMRPOLYDRAW16, apts[count] ) : offsetof( EMRPOLYDRAW, aptl[count] );
636 size += (count + 3) & ~3;
638 if (!(emr = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
640 emr->emr.iType = use_small_emr ? EMR_POLYDRAW16 : EMR_POLYDRAW;
641 emr->emr.nSize = size;
642 emr->cptl = count;
644 types_dest = store_points( emr->aptl, pts, count, use_small_emr );
645 memcpy( types_dest, types, count );
646 if (count & 3) memset( types_dest + count, 0, 4 - (count & 3) );
648 if (!physDev->path)
649 get_points_bounds( &emr->rclBounds, pts, count, 0 );
650 else
651 emr->rclBounds = empty_bounds;
653 ret = EMFDRV_WriteRecord( dev, &emr->emr );
654 if (ret && !physDev->path) EMFDRV_UpdateBBox( dev, &emr->rclBounds );
655 HeapFree( GetProcessHeap(), 0, emr );
656 return ret;
660 /**********************************************************************
661 * EMFDRV_ExtFloodFill
663 BOOL CDECL EMFDRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
665 EMREXTFLOODFILL emr;
667 emr.emr.iType = EMR_EXTFLOODFILL;
668 emr.emr.nSize = sizeof(emr);
669 emr.ptlStart.x = x;
670 emr.ptlStart.y = y;
671 emr.crColor = color;
672 emr.iMode = fillType;
674 return EMFDRV_WriteRecord( dev, &emr.emr );
678 /*********************************************************************
679 * EMFDRV_FillRgn
681 BOOL CDECL EMFDRV_FillRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush )
683 EMRFILLRGN *emr;
684 DWORD size, rgnsize, index;
685 BOOL ret;
687 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
688 if(!index) return FALSE;
690 rgnsize = GetRegionData( hrgn, 0, NULL );
691 size = rgnsize + offsetof(EMRFILLRGN,RgnData);
692 emr = HeapAlloc( GetProcessHeap(), 0, size );
694 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
696 emr->emr.iType = EMR_FILLRGN;
697 emr->emr.nSize = size;
698 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
699 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
700 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
701 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
702 emr->cbRgnData = rgnsize;
703 emr->ihBrush = index;
705 ret = EMFDRV_WriteRecord( dev, &emr->emr );
706 if(ret)
707 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
708 HeapFree( GetProcessHeap(), 0, emr );
709 return ret;
711 /*********************************************************************
712 * EMFDRV_FrameRgn
714 BOOL CDECL EMFDRV_FrameRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush, INT width, INT height )
716 EMRFRAMERGN *emr;
717 DWORD size, rgnsize, index;
718 BOOL ret;
720 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
721 if(!index) return FALSE;
723 rgnsize = GetRegionData( hrgn, 0, NULL );
724 size = rgnsize + offsetof(EMRFRAMERGN,RgnData);
725 emr = HeapAlloc( GetProcessHeap(), 0, size );
727 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
729 emr->emr.iType = EMR_FRAMERGN;
730 emr->emr.nSize = size;
731 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
732 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
733 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
734 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
735 emr->cbRgnData = rgnsize;
736 emr->ihBrush = index;
737 emr->szlStroke.cx = width;
738 emr->szlStroke.cy = height;
740 ret = EMFDRV_WriteRecord( dev, &emr->emr );
741 if(ret)
742 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
743 HeapFree( GetProcessHeap(), 0, emr );
744 return ret;
747 /*********************************************************************
748 * EMFDRV_PaintInvertRgn
750 * Helper for EMFDRV_{Paint|Invert}Rgn
752 static BOOL EMFDRV_PaintInvertRgn( PHYSDEV dev, HRGN hrgn, DWORD iType )
754 EMRINVERTRGN *emr;
755 DWORD size, rgnsize;
756 BOOL ret;
759 rgnsize = GetRegionData( hrgn, 0, NULL );
760 size = rgnsize + offsetof(EMRINVERTRGN,RgnData);
761 emr = HeapAlloc( GetProcessHeap(), 0, size );
763 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
765 emr->emr.iType = iType;
766 emr->emr.nSize = size;
767 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
768 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
769 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
770 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
771 emr->cbRgnData = rgnsize;
773 ret = EMFDRV_WriteRecord( dev, &emr->emr );
774 if(ret)
775 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
776 HeapFree( GetProcessHeap(), 0, emr );
777 return ret;
780 /**********************************************************************
781 * EMFDRV_PaintRgn
783 BOOL CDECL EMFDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
785 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_PAINTRGN );
788 /**********************************************************************
789 * EMFDRV_InvertRgn
791 BOOL CDECL EMFDRV_InvertRgn( PHYSDEV dev, HRGN hrgn )
793 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_INVERTRGN );
796 /**********************************************************************
797 * EMFDRV_ExtTextOut
799 BOOL CDECL EMFDRV_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags, const RECT *lprect,
800 LPCWSTR str, UINT count, const INT *lpDx )
802 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
803 DC *dc = get_physdev_dc( dev );
804 EMREXTTEXTOUTW *pemr;
805 DWORD nSize;
806 BOOL ret;
807 int textHeight = 0;
808 int textWidth = 0;
809 const UINT textAlign = dc->textAlign;
810 const INT graphicsMode = dc->GraphicsMode;
811 FLOAT exScale, eyScale;
813 nSize = sizeof(*pemr) + ((count+1) & ~1) * sizeof(WCHAR) + count * sizeof(INT);
815 TRACE("%s %s count %d nSize = %d\n", debugstr_wn(str, count),
816 wine_dbgstr_rect(lprect), count, nSize);
817 pemr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nSize);
819 if (graphicsMode == GM_COMPATIBLE)
821 const INT horzSize = GetDeviceCaps( dev->hdc, HORZSIZE );
822 const INT horzRes = GetDeviceCaps( dev->hdc, HORZRES );
823 const INT vertSize = GetDeviceCaps( dev->hdc, VERTSIZE );
824 const INT vertRes = GetDeviceCaps( dev->hdc, VERTRES );
825 SIZE wndext, vportext;
827 GetViewportExtEx( dev->hdc, &vportext );
828 GetWindowExtEx( dev->hdc, &wndext );
829 exScale = 100.0 * ((FLOAT)horzSize / (FLOAT)horzRes) /
830 ((FLOAT)wndext.cx / (FLOAT)vportext.cx);
831 eyScale = 100.0 * ((FLOAT)vertSize / (FLOAT)vertRes) /
832 ((FLOAT)wndext.cy / (FLOAT)vportext.cy);
834 else
836 exScale = 0.0;
837 eyScale = 0.0;
840 pemr->emr.iType = EMR_EXTTEXTOUTW;
841 pemr->emr.nSize = nSize;
842 pemr->iGraphicsMode = graphicsMode;
843 pemr->exScale = exScale;
844 pemr->eyScale = eyScale;
845 pemr->emrtext.ptlReference.x = x;
846 pemr->emrtext.ptlReference.y = y;
847 pemr->emrtext.nChars = count;
848 pemr->emrtext.offString = sizeof(*pemr);
849 memcpy((char*)pemr + pemr->emrtext.offString, str, count * sizeof(WCHAR));
850 pemr->emrtext.fOptions = flags;
851 if(!lprect) {
852 pemr->emrtext.rcl.left = pemr->emrtext.rcl.top = 0;
853 pemr->emrtext.rcl.right = pemr->emrtext.rcl.bottom = -1;
854 } else {
855 pemr->emrtext.rcl.left = lprect->left;
856 pemr->emrtext.rcl.top = lprect->top;
857 pemr->emrtext.rcl.right = lprect->right;
858 pemr->emrtext.rcl.bottom = lprect->bottom;
861 pemr->emrtext.offDx = pemr->emrtext.offString + ((count+1) & ~1) * sizeof(WCHAR);
862 if(lpDx) {
863 UINT i;
864 SIZE strSize;
865 memcpy((char*)pemr + pemr->emrtext.offDx, lpDx, count * sizeof(INT));
866 for (i = 0; i < count; i++) {
867 textWidth += lpDx[i];
869 if (GetTextExtentPoint32W( dev->hdc, str, count, &strSize ))
870 textHeight = strSize.cy;
872 else {
873 UINT i;
874 INT *dx = (INT *)((char*)pemr + pemr->emrtext.offDx);
875 SIZE charSize;
876 for (i = 0; i < count; i++) {
877 if (GetTextExtentPoint32W( dev->hdc, str + i, 1, &charSize )) {
878 dx[i] = charSize.cx;
879 textWidth += charSize.cx;
880 textHeight = max(textHeight, charSize.cy);
885 if (physDev->path)
887 pemr->rclBounds.left = pemr->rclBounds.top = 0;
888 pemr->rclBounds.right = pemr->rclBounds.bottom = -1;
889 goto no_bounds;
892 /* FIXME: handle font escapement */
893 switch (textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER)) {
894 case TA_CENTER: {
895 pemr->rclBounds.left = x - (textWidth / 2) - 1;
896 pemr->rclBounds.right = x + (textWidth / 2) + 1;
897 break;
899 case TA_RIGHT: {
900 pemr->rclBounds.left = x - textWidth - 1;
901 pemr->rclBounds.right = x;
902 break;
904 default: { /* TA_LEFT */
905 pemr->rclBounds.left = x;
906 pemr->rclBounds.right = x + textWidth + 1;
910 switch (textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE)) {
911 case TA_BASELINE: {
912 TEXTMETRICW tm;
913 if (!GetTextMetricsW( dev->hdc, &tm ))
914 tm.tmDescent = 0;
915 /* Play safe here... it's better to have a bounding box */
916 /* that is too big than too small. */
917 pemr->rclBounds.top = y - textHeight - 1;
918 pemr->rclBounds.bottom = y + tm.tmDescent + 1;
919 break;
921 case TA_BOTTOM: {
922 pemr->rclBounds.top = y - textHeight - 1;
923 pemr->rclBounds.bottom = y;
924 break;
926 default: { /* TA_TOP */
927 pemr->rclBounds.top = y;
928 pemr->rclBounds.bottom = y + textHeight + 1;
931 EMFDRV_UpdateBBox( dev, &pemr->rclBounds );
933 no_bounds:
934 ret = EMFDRV_WriteRecord( dev, &pemr->emr );
935 HeapFree( GetProcessHeap(), 0, pemr );
936 return ret;
939 /**********************************************************************
940 * EMFDRV_GradientFill
942 BOOL CDECL EMFDRV_GradientFill( PHYSDEV dev, TRIVERTEX *vert_array, ULONG nvert,
943 void *grad_array, ULONG ngrad, ULONG mode )
945 EMRGRADIENTFILL *emr;
946 ULONG i, pt, size, num_pts = ngrad * (mode == GRADIENT_FILL_TRIANGLE ? 3 : 2);
947 const ULONG *pts = (const ULONG *)grad_array;
948 BOOL ret;
950 size = FIELD_OFFSET(EMRGRADIENTFILL, Ver[nvert]) + num_pts * sizeof(pts[0]);
952 emr = HeapAlloc( GetProcessHeap(), 0, size );
953 if (!emr) return FALSE;
955 for (i = 0; i < num_pts; i++)
957 pt = pts[i];
959 if (i == 0)
961 emr->rclBounds.left = emr->rclBounds.right = vert_array[pt].x;
962 emr->rclBounds.top = emr->rclBounds.bottom = vert_array[pt].y;
964 else
966 if (vert_array[pt].x < emr->rclBounds.left)
967 emr->rclBounds.left = vert_array[pt].x;
968 else if (vert_array[pt].x > emr->rclBounds.right)
969 emr->rclBounds.right = vert_array[pt].x;
970 if (vert_array[pt].y < emr->rclBounds.top)
971 emr->rclBounds.top = vert_array[pt].y;
972 else if (vert_array[pt].y > emr->rclBounds.bottom)
973 emr->rclBounds.bottom = vert_array[pt].y;
976 emr->rclBounds.right--;
977 emr->rclBounds.bottom--;
979 emr->emr.iType = EMR_GRADIENTFILL;
980 emr->emr.nSize = size;
981 emr->nVer = nvert;
982 emr->nTri = ngrad;
983 emr->ulMode = mode;
984 memcpy( emr->Ver, vert_array, nvert * sizeof(vert_array[0]) );
985 memcpy( emr->Ver + nvert, pts, num_pts * sizeof(pts[0]) );
987 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
988 ret = EMFDRV_WriteRecord( dev, &emr->emr );
989 HeapFree( GetProcessHeap(), 0, emr );
990 return ret;
993 /**********************************************************************
994 * EMFDRV_FillPath
996 BOOL CDECL EMFDRV_FillPath( PHYSDEV dev )
998 return emfdrv_stroke_and_fill_path( dev, EMR_FILLPATH );
1001 /**********************************************************************
1002 * EMFDRV_StrokeAndFillPath
1004 BOOL CDECL EMFDRV_StrokeAndFillPath( PHYSDEV dev )
1006 return emfdrv_stroke_and_fill_path( dev, EMR_STROKEANDFILLPATH );
1009 /**********************************************************************
1010 * EMFDRV_StrokePath
1012 BOOL CDECL EMFDRV_StrokePath( PHYSDEV dev )
1014 return emfdrv_stroke_and_fill_path( dev, EMR_STROKEPATH );