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 )
329 emr.emr.iType = EMR_SETPIXELV;
330 emr.emr.nSize = sizeof(emr);
335 if (EMFDRV_WriteRecord( dev, &emr.emr )) {
337 bounds.left = bounds.right = x;
338 bounds.top = bounds.bottom = y;
339 EMFDRV_UpdateBBox( dev, &bounds );
345 /**********************************************************************
348 * Helper for EMFDRV_Poly{line|gon}
351 EMFDRV_Polylinegon( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
358 size = sizeof(EMRPOLYLINE) + sizeof(POINTL) * (count - 1);
360 emr = HeapAlloc( GetProcessHeap(), 0, size );
361 emr->emr.iType = iType;
362 emr->emr.nSize = size;
364 emr->rclBounds.left = emr->rclBounds.right = pt[0].x;
365 emr->rclBounds.top = emr->rclBounds.bottom = pt[0].y;
367 for(i = 1; i < count; i++) {
368 if(pt[i].x < emr->rclBounds.left)
369 emr->rclBounds.left = pt[i].x;
370 else if(pt[i].x > emr->rclBounds.right)
371 emr->rclBounds.right = pt[i].x;
372 if(pt[i].y < emr->rclBounds.top)
373 emr->rclBounds.top = pt[i].y;
374 else if(pt[i].y > emr->rclBounds.bottom)
375 emr->rclBounds.bottom = pt[i].y;
379 memcpy(emr->aptl, pt, count * sizeof(POINTL));
381 ret = EMFDRV_WriteRecord( dev, &emr->emr );
383 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
384 HeapFree( GetProcessHeap(), 0, emr );
389 /**********************************************************************
393 EMFDRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
395 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINE );
398 /**********************************************************************
402 EMFDRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
404 if(count < 2) return FALSE;
405 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYGON );
409 /**********************************************************************
410 * EMFDRV_PolyPolylinegon
412 * Helper for EMFDRV_PolyPoly{line|gon}
415 EMFDRV_PolyPolylinegon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys,
418 EMRPOLYPOLYLINE *emr;
419 DWORD cptl = 0, poly, size, point;
424 bounds.left = bounds.right = pt[0].x;
425 bounds.top = bounds.bottom = pt[0].y;
428 for(poly = 0; poly < polys; poly++) {
429 cptl += counts[poly];
430 for(point = 0; point < counts[poly]; point++) {
431 if(bounds.left > pts->x) bounds.left = pts->x;
432 else if(bounds.right < pts->x) bounds.right = pts->x;
433 if(bounds.top > pts->y) bounds.top = pts->y;
434 else if(bounds.bottom < pts->y) bounds.bottom = pts->y;
439 size = sizeof(EMRPOLYPOLYLINE) + (polys - 1) * sizeof(DWORD) +
440 (cptl - 1) * sizeof(POINTL);
442 emr = HeapAlloc( GetProcessHeap(), 0, size );
444 emr->emr.iType = iType;
445 emr->emr.nSize = size;
446 emr->rclBounds = bounds;
449 memcpy(emr->aPolyCounts, counts, polys * sizeof(DWORD));
450 memcpy(emr->aPolyCounts + polys, pt, cptl * sizeof(POINTL));
451 ret = EMFDRV_WriteRecord( dev, &emr->emr );
453 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
454 HeapFree( GetProcessHeap(), 0, emr );
458 /**********************************************************************
459 * EMFDRV_PolyPolyline
462 EMFDRV_PolyPolyline(PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polys)
464 return EMFDRV_PolyPolylinegon( dev, pt, (INT *)counts, polys,
468 /**********************************************************************
472 EMFDRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys )
474 return EMFDRV_PolyPolylinegon( dev, pt, counts, polys, EMR_POLYPOLYGON );
478 /**********************************************************************
479 * EMFDRV_ExtFloodFill
482 EMFDRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
486 emr.emr.iType = EMR_EXTFLOODFILL;
487 emr.emr.nSize = sizeof(emr);
491 emr.iMode = fillType;
493 return EMFDRV_WriteRecord( dev, &emr.emr );
497 /*********************************************************************
500 BOOL EMFDRV_FillRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush )
503 DWORD size, rgnsize, index;
506 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
507 if(!index) return FALSE;
509 rgnsize = GetRegionData( hrgn, 0, NULL );
510 size = rgnsize + offsetof(EMRFILLRGN,RgnData);
511 emr = HeapAlloc( GetProcessHeap(), 0, size );
513 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
515 emr->emr.iType = EMR_FILLRGN;
516 emr->emr.nSize = size;
517 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
518 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
519 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
520 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
521 emr->cbRgnData = rgnsize;
522 emr->ihBrush = index;
524 ret = EMFDRV_WriteRecord( dev, &emr->emr );
526 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
527 HeapFree( GetProcessHeap(), 0, emr );
530 /*********************************************************************
533 BOOL EMFDRV_FrameRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush, INT width, INT height )
536 DWORD size, rgnsize, index;
539 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
540 if(!index) return FALSE;
542 rgnsize = GetRegionData( hrgn, 0, NULL );
543 size = rgnsize + offsetof(EMRFRAMERGN,RgnData);
544 emr = HeapAlloc( GetProcessHeap(), 0, size );
546 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
548 emr->emr.iType = EMR_FRAMERGN;
549 emr->emr.nSize = size;
550 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
551 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
552 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
553 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
554 emr->cbRgnData = rgnsize;
555 emr->ihBrush = index;
556 emr->szlStroke.cx = width;
557 emr->szlStroke.cy = height;
559 ret = EMFDRV_WriteRecord( dev, &emr->emr );
561 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
562 HeapFree( GetProcessHeap(), 0, emr );
566 /*********************************************************************
567 * EMFDRV_PaintInvertRgn
569 * Helper for EMFDRV_{Paint|Invert}Rgn
571 static BOOL EMFDRV_PaintInvertRgn( PHYSDEV dev, HRGN hrgn, DWORD iType )
578 rgnsize = GetRegionData( hrgn, 0, NULL );
579 size = rgnsize + offsetof(EMRINVERTRGN,RgnData);
580 emr = HeapAlloc( GetProcessHeap(), 0, size );
582 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
584 emr->emr.iType = iType;
585 emr->emr.nSize = size;
586 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
587 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
588 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
589 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
590 emr->cbRgnData = rgnsize;
592 ret = EMFDRV_WriteRecord( dev, &emr->emr );
594 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
595 HeapFree( GetProcessHeap(), 0, emr );
599 /**********************************************************************
603 EMFDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
605 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_PAINTRGN );
608 /**********************************************************************
612 EMFDRV_InvertRgn( PHYSDEV dev, HRGN hrgn )
614 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_INVERTRGN );
617 /**********************************************************************
621 EMFDRV_SetBkColor( PHYSDEV dev, COLORREF color )
625 emr.emr.iType = EMR_SETBKCOLOR;
626 emr.emr.nSize = sizeof(emr);
629 return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
633 /**********************************************************************
634 * EMFDRV_SetTextColor
637 EMFDRV_SetTextColor( PHYSDEV dev, COLORREF color )
641 emr.emr.iType = EMR_SETTEXTCOLOR;
642 emr.emr.nSize = sizeof(emr);
645 return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
648 /**********************************************************************
651 BOOL EMFDRV_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags,
652 const RECT *lprect, LPCWSTR str, UINT count,
655 EMREXTTEXTOUTW *pemr;
658 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
661 const UINT textAlign = GetTextAlign(physDev->hdc);
663 nSize = sizeof(*pemr) + ((count+1) & ~1) * sizeof(WCHAR) + count * sizeof(INT);
665 TRACE("%s count %d nSize = %ld\n", debugstr_wn(str, count), count, nSize);
666 pemr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nSize);
668 pemr->emr.iType = EMR_EXTTEXTOUTW;
669 pemr->emr.nSize = nSize;
671 pemr->iGraphicsMode = GetGraphicsMode(physDev->hdc);
672 pemr->exScale = pemr->eyScale = 1.0; /* FIXME */
674 pemr->emrtext.ptlReference.x = x;
675 pemr->emrtext.ptlReference.y = y;
676 pemr->emrtext.nChars = count;
677 pemr->emrtext.offString = sizeof(*pemr);
678 memcpy((char*)pemr + pemr->emrtext.offString, str, count * sizeof(WCHAR));
679 pemr->emrtext.fOptions = flags;
681 pemr->emrtext.rcl.left = pemr->emrtext.rcl.top = 0;
682 pemr->emrtext.rcl.right = pemr->emrtext.rcl.bottom = -1;
684 pemr->emrtext.rcl.left = lprect->left;
685 pemr->emrtext.rcl.top = lprect->top;
686 pemr->emrtext.rcl.right = lprect->right;
687 pemr->emrtext.rcl.bottom = lprect->bottom;
690 pemr->emrtext.offDx = pemr->emrtext.offString + ((count+1) & ~1) * sizeof(WCHAR);
694 memcpy((char*)pemr + pemr->emrtext.offDx, lpDx, count * sizeof(INT));
695 for (i = 0; i < count; i++) {
696 textWidth += lpDx[i];
698 GetTextExtentPoint32W(physDev->hdc, str, count, &strSize);
699 textHeight = strSize.cy;
703 INT *dx = (INT *)((char*)pemr + pemr->emrtext.offDx);
705 for (i = 0; i < count; i++) {
706 GetTextExtentPoint32W(physDev->hdc, str + i, 1, &charSize);
708 textWidth += charSize.cx;
709 textHeight = max(textHeight, charSize.cy);
713 switch (textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER)) {
715 pemr->rclBounds.left = x - (textWidth / 2) - 1;
716 pemr->rclBounds.right = x + (textWidth / 2) + 1;
720 pemr->rclBounds.left = x - textWidth - 1;
721 pemr->rclBounds.right = x;
724 default: { /* TA_LEFT */
725 pemr->rclBounds.left = x;
726 pemr->rclBounds.right = x + textWidth + 1;
730 switch (textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE)) {
733 GetTextMetricsW(physDev->hdc, &tm);
734 /* Play safe here... it's better to have a bounding box */
735 /* that is too big than too small. */
736 pemr->rclBounds.top = y - textHeight - 1;
737 pemr->rclBounds.bottom = y + tm.tmDescent + 1;
741 pemr->rclBounds.top = y - textHeight - 1;
742 pemr->rclBounds.bottom = y;
745 default: { /* TA_TOP */
746 pemr->rclBounds.top = y;
747 pemr->rclBounds.bottom = y + textHeight + 1;
751 ret = EMFDRV_WriteRecord( dev, &pemr->emr );
753 EMFDRV_UpdateBBox( dev, &pemr->rclBounds );
754 HeapFree( GetProcessHeap(), 0, pemr );
758 /**********************************************************************
759 * EMFDRV_SetArcDirection
761 INT EMFDRV_SetArcDirection(PHYSDEV dev, INT arcDirection)
763 EMRSETARCDIRECTION emr;
765 emr.emr.iType = EMR_SETARCDIRECTION;
766 emr.emr.nSize = sizeof(emr);
767 emr.iArcDirection = arcDirection;
769 EMFDRV_WriteRecord(dev, &emr.emr);
771 /* We don't know the old arc direction and we don't care... */