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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
28 #include "enhmfdrv/enhmetafiledrv.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
33 /**********************************************************************
37 EMFDRV_MoveTo(PHYSDEV dev, INT x, INT y)
41 emr.emr.iType = EMR_MOVETOEX;
42 emr.emr.nSize = sizeof(emr);
46 return EMFDRV_WriteRecord( dev, &emr.emr );
49 /***********************************************************************
53 EMFDRV_LineTo( PHYSDEV dev, INT x, INT y )
57 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
60 emr.emr.iType = EMR_LINETO;
61 emr.emr.nSize = sizeof(emr);
65 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
68 bounds.left = min(x, dc->CursPosX);
69 bounds.top = min(y, dc->CursPosY);
70 bounds.right = max(x, dc->CursPosX);
71 bounds.bottom = max(y, dc->CursPosY);
73 EMFDRV_UpdateBBox( dev, &bounds );
79 /***********************************************************************
83 EMFDRV_ArcChordPie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
84 INT xstart, INT ystart, INT xend, INT yend, DWORD iType )
86 INT temp, xCentre, yCentre, i;
87 double angleStart, angleEnd;
88 double xinterStart, yinterStart, xinterEnd, yinterEnd;
91 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
94 if(left == right || top == bottom) return FALSE;
96 if(left > right) {temp = left; left = right; right = temp;}
97 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
99 if(dc->GraphicsMode == GM_COMPATIBLE) {
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;
116 /* Now calculate the BBox */
117 xCentre = (left + right + 1) / 2;
118 yCentre = (top + bottom + 1) / 2;
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);
129 /* These are the intercepts of the start/end lines with the arc */
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;
136 if(angleStart < 0) angleStart += 2 * M_PI;
137 if(angleEnd < 0) angleEnd += 2 * M_PI;
138 if(angleEnd < angleStart) angleEnd += 2 * M_PI;
140 bounds.left = min(xinterStart, xinterEnd);
141 bounds.top = min(yinterStart, yinterEnd);
142 bounds.right = max(xinterStart, xinterEnd);
143 bounds.bottom = max(yinterStart, yinterEnd);
145 for(i = 0; i <= 8; i++) {
146 if(i * M_PI / 2 < angleStart) /* loop until we're past start */
148 if(i * M_PI / 2 > angleEnd) /* if we're past end we're finished */
151 /* the arc touches the rectangle at the start of quadrant i, so adjust
152 BBox to reflect this. */
156 bounds.right = right;
165 bounds.bottom = bottom;
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.right = yCentre;
177 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
179 EMFDRV_UpdateBBox( dev, &bounds );
184 /***********************************************************************
188 EMFDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
189 INT xstart, INT ystart, INT xend, INT yend )
191 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
192 xend, yend, EMR_ARC );
195 /***********************************************************************
199 EMFDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
200 INT xstart, INT ystart, INT xend, INT yend )
202 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
203 xend, yend, EMR_PIE );
207 /***********************************************************************
211 EMFDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
212 INT xstart, INT ystart, INT xend, INT yend )
214 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
215 xend, yend, EMR_CHORD );
218 /***********************************************************************
222 EMFDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
226 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
227 DC *dc = physDev->dc;
229 TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
231 if(left == right || top == bottom) return FALSE;
233 if(left > right) {temp = left; left = right; right = temp;}
234 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
236 if(dc->GraphicsMode == GM_COMPATIBLE) {
241 emr.emr.iType = EMR_ELLIPSE;
242 emr.emr.nSize = sizeof(emr);
243 emr.rclBox.left = left;
244 emr.rclBox.top = top;
245 emr.rclBox.right = right;
246 emr.rclBox.bottom = bottom;
248 EMFDRV_UpdateBBox( dev, &emr.rclBox );
249 return EMFDRV_WriteRecord( dev, &emr.emr );
252 /***********************************************************************
256 EMFDRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
260 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
261 DC *dc = physDev->dc;
263 TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
265 if(left == right || top == bottom) return FALSE;
267 if(left > right) {temp = left; left = right; right = temp;}
268 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
270 if(dc->GraphicsMode == GM_COMPATIBLE) {
275 emr.emr.iType = EMR_RECTANGLE;
276 emr.emr.nSize = sizeof(emr);
277 emr.rclBox.left = left;
278 emr.rclBox.top = top;
279 emr.rclBox.right = right;
280 emr.rclBox.bottom = bottom;
282 EMFDRV_UpdateBBox( dev, &emr.rclBox );
283 return EMFDRV_WriteRecord( dev, &emr.emr );
286 /***********************************************************************
290 EMFDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
291 INT bottom, INT ell_width, INT ell_height )
295 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
296 DC *dc = physDev->dc;
298 if(left == right || top == bottom) return FALSE;
300 if(left > right) {temp = left; left = right; right = temp;}
301 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
303 if(dc->GraphicsMode == GM_COMPATIBLE) {
308 emr.emr.iType = EMR_ROUNDRECT;
309 emr.emr.nSize = sizeof(emr);
310 emr.rclBox.left = left;
311 emr.rclBox.top = top;
312 emr.rclBox.right = right;
313 emr.rclBox.bottom = bottom;
314 emr.szlCorner.cx = ell_width;
315 emr.szlCorner.cy = ell_height;
317 EMFDRV_UpdateBBox( dev, &emr.rclBox );
318 return EMFDRV_WriteRecord( dev, &emr.emr );
321 /***********************************************************************
325 EMFDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
331 /**********************************************************************
334 * Helper for EMFDRV_Poly{line|gon}
337 EMFDRV_Polylinegon( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
344 size = sizeof(EMRPOLYLINE) + sizeof(POINTL) * (count - 1);
346 emr = HeapAlloc( GetProcessHeap(), 0, size );
347 emr->emr.iType = iType;
348 emr->emr.nSize = size;
350 emr->rclBounds.left = emr->rclBounds.right = pt[0].x;
351 emr->rclBounds.top = emr->rclBounds.bottom = pt[0].y;
353 for(i = 1; i < count; i++) {
354 if(pt[i].x < emr->rclBounds.left)
355 emr->rclBounds.left = pt[i].x;
356 else if(pt[i].x > emr->rclBounds.right)
357 emr->rclBounds.right = pt[i].x;
358 if(pt[i].y < emr->rclBounds.top)
359 emr->rclBounds.top = pt[i].y;
360 else if(pt[i].y > emr->rclBounds.bottom)
361 emr->rclBounds.bottom = pt[i].y;
365 memcpy(emr->aptl, pt, count * sizeof(POINTL));
367 ret = EMFDRV_WriteRecord( dev, &emr->emr );
369 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
370 HeapFree( GetProcessHeap(), 0, emr );
375 /**********************************************************************
379 EMFDRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
381 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINE );
384 /**********************************************************************
388 EMFDRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
390 if(count < 2) return FALSE;
391 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYGON );
395 /**********************************************************************
396 * EMFDRV_PolyPolylinegon
398 * Helper for EMFDRV_PolyPoly{line|gon}
401 EMFDRV_PolyPolylinegon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys,
404 EMRPOLYPOLYLINE *emr;
405 DWORD cptl = 0, poly, size, point;
410 bounds.left = bounds.right = pt[0].x;
411 bounds.top = bounds.bottom = pt[0].y;
414 for(poly = 0; poly < polys; poly++) {
415 cptl += counts[poly];
416 for(point = 0; point < counts[poly]; point++) {
417 if(bounds.left > pts->x) bounds.left = pts->x;
418 else if(bounds.right < pts->x) bounds.right = pts->x;
419 if(bounds.top > pts->y) bounds.top = pts->y;
420 else if(bounds.bottom < pts->y) bounds.bottom = pts->y;
425 size = sizeof(EMRPOLYPOLYLINE) + (polys - 1) * sizeof(DWORD) +
426 (cptl - 1) * sizeof(POINTL);
428 emr = HeapAlloc( GetProcessHeap(), 0, size );
430 emr->emr.iType = iType;
431 emr->emr.nSize = size;
432 emr->rclBounds = bounds;
435 memcpy(emr->aPolyCounts, counts, polys * sizeof(DWORD));
436 memcpy(emr->aPolyCounts + polys, pt, cptl * sizeof(POINTL));
437 ret = EMFDRV_WriteRecord( dev, &emr->emr );
439 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
440 HeapFree( GetProcessHeap(), 0, emr );
444 /**********************************************************************
445 * EMFDRV_PolyPolyline
448 EMFDRV_PolyPolyline(PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polys)
450 return EMFDRV_PolyPolylinegon( dev, pt, (INT *)counts, polys,
454 /**********************************************************************
458 EMFDRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys )
460 return EMFDRV_PolyPolylinegon( dev, pt, counts, polys, EMR_POLYPOLYGON );
464 /**********************************************************************
465 * EMFDRV_ExtFloodFill
468 EMFDRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
472 emr.emr.iType = EMR_EXTFLOODFILL;
473 emr.emr.nSize = sizeof(emr);
477 emr.iMode = fillType;
479 return EMFDRV_WriteRecord( dev, &emr.emr );
483 /*********************************************************************
486 BOOL EMFDRV_FillRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush )
489 DWORD size, rgnsize, index;
492 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
493 if(!index) return FALSE;
495 rgnsize = GetRegionData( hrgn, 0, NULL );
496 size = rgnsize + offsetof(EMRFILLRGN,RgnData);
497 emr = HeapAlloc( GetProcessHeap(), 0, size );
499 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
501 emr->emr.iType = EMR_FILLRGN;
502 emr->emr.nSize = size;
503 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
504 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
505 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
506 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
507 emr->cbRgnData = rgnsize;
508 emr->ihBrush = index;
510 ret = EMFDRV_WriteRecord( dev, &emr->emr );
512 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
513 HeapFree( GetProcessHeap(), 0, emr );
516 /*********************************************************************
519 BOOL EMFDRV_FrameRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush, INT width, INT height )
522 DWORD size, rgnsize, index;
525 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
526 if(!index) return FALSE;
528 rgnsize = GetRegionData( hrgn, 0, NULL );
529 size = rgnsize + offsetof(EMRFRAMERGN,RgnData);
530 emr = HeapAlloc( GetProcessHeap(), 0, size );
532 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
534 emr->emr.iType = EMR_FRAMERGN;
535 emr->emr.nSize = size;
536 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
537 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
538 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
539 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
540 emr->cbRgnData = rgnsize;
541 emr->ihBrush = index;
542 emr->szlStroke.cx = width;
543 emr->szlStroke.cy = height;
545 ret = EMFDRV_WriteRecord( dev, &emr->emr );
547 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
548 HeapFree( GetProcessHeap(), 0, emr );
552 /*********************************************************************
553 * EMFDRV_PaintInvertRgn
555 * Helper for EMFDRV_{Paint|Invert}Rgn
557 static BOOL EMFDRV_PaintInvertRgn( PHYSDEV dev, HRGN hrgn, DWORD iType )
564 rgnsize = GetRegionData( hrgn, 0, NULL );
565 size = rgnsize + offsetof(EMRINVERTRGN,RgnData);
566 emr = HeapAlloc( GetProcessHeap(), 0, size );
568 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
570 emr->emr.iType = iType;
571 emr->emr.nSize = size;
572 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
573 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
574 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
575 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
576 emr->cbRgnData = rgnsize;
578 ret = EMFDRV_WriteRecord( dev, &emr->emr );
580 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
581 HeapFree( GetProcessHeap(), 0, emr );
585 /**********************************************************************
589 EMFDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
591 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_PAINTRGN );
594 /**********************************************************************
598 EMFDRV_InvertRgn( PHYSDEV dev, HRGN hrgn )
600 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_INVERTRGN );
603 /**********************************************************************
607 EMFDRV_SetBkColor( PHYSDEV dev, COLORREF color )
611 emr.emr.iType = EMR_SETBKCOLOR;
612 emr.emr.nSize = sizeof(emr);
615 return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
619 /**********************************************************************
620 * EMFDRV_SetTextColor
623 EMFDRV_SetTextColor( PHYSDEV dev, COLORREF color )
627 emr.emr.iType = EMR_SETTEXTCOLOR;
628 emr.emr.nSize = sizeof(emr);
631 return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
634 /**********************************************************************
637 BOOL EMFDRV_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags,
638 const RECT *lprect, LPCWSTR str, UINT count,
641 EMREXTTEXTOUTW *pemr;
644 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
646 nSize = sizeof(*pemr) + (count+1)/2 * 2 * sizeof(WCHAR) +
647 (lpDx ? count * sizeof(INT) : 0);
649 TRACE("%s count %d nSize = %ld\n", debugstr_wn(str, count), count, nSize);
650 pemr = HeapAlloc(GetProcessHeap(), 0, nSize);
652 pemr->emr.iType = EMR_EXTTEXTOUTW;
653 pemr->emr.nSize = nSize;
655 /* FIXME: Calculation of these requires honouring alignment mode, escapement etc. */
656 pemr->rclBounds.left = pemr->rclBounds.right = x;
657 pemr->rclBounds.top = pemr->rclBounds.bottom = y;
659 pemr->iGraphicsMode = GetGraphicsMode(physDev->hdc);
660 pemr->exScale = pemr->eyScale = 1.0; /* FIXME */
662 pemr->emrtext.ptlReference.x = x;
663 pemr->emrtext.ptlReference.y = y;
664 pemr->emrtext.nChars = count;
665 pemr->emrtext.offString = sizeof(*pemr);
666 memcpy((char*)pemr + pemr->emrtext.offString, str, count * sizeof(WCHAR));
667 pemr->emrtext.fOptions = flags;
669 pemr->emrtext.rcl.left = pemr->emrtext.rcl.top = 0;
670 pemr->emrtext.rcl.right = pemr->emrtext.rcl.bottom = -1;
672 pemr->emrtext.rcl.left = lprect->left;
673 pemr->emrtext.rcl.top = lprect->top;
674 pemr->emrtext.rcl.right = lprect->right;
675 pemr->emrtext.rcl.bottom = lprect->bottom;
679 pemr->emrtext.offDx = sizeof(*pemr) + (count+1)/2 * 2 * sizeof(WCHAR);
680 memcpy((char*)pemr + pemr->emrtext.offDx, lpDx, count * sizeof(INT));
682 pemr->emrtext.offDx = 0; /* FIXME: actually Windows fills out the array
683 using GetCharWidth in this case */
686 ret = EMFDRV_WriteRecord( dev, &pemr->emr );
688 EMFDRV_UpdateBBox( dev, &pemr->rclBounds );
689 HeapFree( GetProcessHeap(), 0, pemr );