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
25 #include "enhmfdrv/enhmetafiledrv.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile
);
30 /**********************************************************************
34 EMFDRV_MoveTo(PHYSDEV dev
, INT x
, INT y
)
38 emr
.emr
.iType
= EMR_MOVETOEX
;
39 emr
.emr
.nSize
= sizeof(emr
);
43 return EMFDRV_WriteRecord( dev
, &emr
.emr
);
46 /***********************************************************************
50 EMFDRV_LineTo( PHYSDEV dev
, INT x
, INT y
)
54 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dev
;
57 emr
.emr
.iType
= EMR_LINETO
;
58 emr
.emr
.nSize
= sizeof(emr
);
62 if(!EMFDRV_WriteRecord( dev
, &emr
.emr
))
65 bounds
.left
= min(x
, dc
->CursPosX
);
66 bounds
.top
= min(y
, dc
->CursPosY
);
67 bounds
.right
= max(x
, dc
->CursPosX
);
68 bounds
.bottom
= max(y
, dc
->CursPosY
);
70 EMFDRV_UpdateBBox( dev
, &bounds
);
76 /***********************************************************************
80 EMFDRV_ArcChordPie( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
,
81 INT xstart
, INT ystart
, INT xend
, INT yend
, DWORD iType
)
83 INT temp
, xCentre
, yCentre
, i
;
84 double angleStart
, angleEnd
;
85 double xinterStart
, yinterStart
, xinterEnd
, yinterEnd
;
88 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dev
;
91 if(left
== right
|| top
== bottom
) return FALSE
;
93 if(left
> right
) {temp
= left
; left
= right
; right
= temp
;}
94 if(top
> bottom
) {temp
= top
; top
= bottom
; bottom
= temp
;}
96 if(dc
->GraphicsMode
== GM_COMPATIBLE
) {
101 emr
.emr
.iType
= iType
;
102 emr
.emr
.nSize
= sizeof(emr
);
103 emr
.rclBox
.left
= left
;
104 emr
.rclBox
.top
= top
;
105 emr
.rclBox
.right
= right
;
106 emr
.rclBox
.bottom
= bottom
;
107 emr
.ptlStart
.x
= xstart
;
108 emr
.ptlStart
.y
= ystart
;
113 /* Now calculate the BBox */
114 xCentre
= (left
+ right
+ 1) / 2;
115 yCentre
= (top
+ bottom
+ 1) / 2;
122 /* invert y co-ords to get angle anti-clockwise from x-axis */
123 angleStart
= atan2( -(double)ystart
, (double)xstart
);
124 angleEnd
= atan2( -(double)yend
, (double)xend
);
126 /* These are the intercepts of the start/end lines with the arc */
128 xinterStart
= (right
- left
+ 1)/2 * cos(angleStart
) + xCentre
;
129 yinterStart
= -(bottom
- top
+ 1)/2 * sin(angleStart
) + yCentre
;
130 xinterEnd
= (right
- left
+ 1)/2 * cos(angleEnd
) + xCentre
;
131 yinterEnd
= -(bottom
- top
+ 1)/2 * sin(angleEnd
) + yCentre
;
133 if(angleStart
< 0) angleStart
+= 2 * M_PI
;
134 if(angleEnd
< 0) angleEnd
+= 2 * M_PI
;
135 if(angleEnd
< angleStart
) angleEnd
+= 2 * M_PI
;
137 bounds
.left
= min(xinterStart
, xinterEnd
);
138 bounds
.top
= min(yinterStart
, yinterEnd
);
139 bounds
.right
= max(xinterStart
, xinterEnd
);
140 bounds
.bottom
= max(yinterStart
, yinterEnd
);
142 for(i
= 0; i
<= 8; i
++) {
143 if(i
* M_PI
/ 2 < angleStart
) /* loop until we're past start */
145 if(i
* M_PI
/ 2 > angleEnd
) /* if we're past end we're finished */
148 /* the arc touches the rectangle at the start of quadrant i, so adjust
149 BBox to reflect this. */
153 bounds
.right
= right
;
162 bounds
.bottom
= bottom
;
167 /* If we're drawing a pie then make sure we include the centre */
168 if(iType
== EMR_PIE
) {
169 if(bounds
.left
> xCentre
) bounds
.left
= xCentre
;
170 else if(bounds
.right
< xCentre
) bounds
.right
= xCentre
;
171 if(bounds
.top
> yCentre
) bounds
.top
= yCentre
;
172 else if(bounds
.bottom
< yCentre
) bounds
.right
= yCentre
;
174 if(!EMFDRV_WriteRecord( dev
, &emr
.emr
))
176 EMFDRV_UpdateBBox( dev
, &bounds
);
181 /***********************************************************************
185 EMFDRV_Arc( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
,
186 INT xstart
, INT ystart
, INT xend
, INT yend
)
188 return EMFDRV_ArcChordPie( dev
, left
, top
, right
, bottom
, xstart
, ystart
,
189 xend
, yend
, EMR_ARC
);
192 /***********************************************************************
196 EMFDRV_Pie( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
,
197 INT xstart
, INT ystart
, INT xend
, INT yend
)
199 return EMFDRV_ArcChordPie( dev
, left
, top
, right
, bottom
, xstart
, ystart
,
200 xend
, yend
, EMR_PIE
);
204 /***********************************************************************
208 EMFDRV_Chord( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
,
209 INT xstart
, INT ystart
, INT xend
, INT yend
)
211 return EMFDRV_ArcChordPie( dev
, left
, top
, right
, bottom
, xstart
, ystart
,
212 xend
, yend
, EMR_CHORD
);
215 /***********************************************************************
219 EMFDRV_Ellipse( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
)
223 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dev
;
224 DC
*dc
= physDev
->dc
;
226 TRACE("%d,%d - %d,%d\n", left
, top
, right
, bottom
);
228 if(left
== right
|| top
== bottom
) return FALSE
;
230 if(left
> right
) {temp
= left
; left
= right
; right
= temp
;}
231 if(top
> bottom
) {temp
= top
; top
= bottom
; bottom
= temp
;}
233 if(dc
->GraphicsMode
== GM_COMPATIBLE
) {
238 emr
.emr
.iType
= EMR_ELLIPSE
;
239 emr
.emr
.nSize
= sizeof(emr
);
240 emr
.rclBox
.left
= left
;
241 emr
.rclBox
.top
= top
;
242 emr
.rclBox
.right
= right
;
243 emr
.rclBox
.bottom
= bottom
;
245 EMFDRV_UpdateBBox( dev
, &emr
.rclBox
);
246 return EMFDRV_WriteRecord( dev
, &emr
.emr
);
249 /***********************************************************************
253 EMFDRV_Rectangle(PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
)
257 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dev
;
258 DC
*dc
= physDev
->dc
;
260 TRACE("%d,%d - %d,%d\n", left
, top
, right
, bottom
);
262 if(left
== right
|| top
== bottom
) return FALSE
;
264 if(left
> right
) {temp
= left
; left
= right
; right
= temp
;}
265 if(top
> bottom
) {temp
= top
; top
= bottom
; bottom
= temp
;}
267 if(dc
->GraphicsMode
== GM_COMPATIBLE
) {
272 emr
.emr
.iType
= EMR_RECTANGLE
;
273 emr
.emr
.nSize
= sizeof(emr
);
274 emr
.rclBox
.left
= left
;
275 emr
.rclBox
.top
= top
;
276 emr
.rclBox
.right
= right
;
277 emr
.rclBox
.bottom
= bottom
;
279 EMFDRV_UpdateBBox( dev
, &emr
.rclBox
);
280 return EMFDRV_WriteRecord( dev
, &emr
.emr
);
283 /***********************************************************************
287 EMFDRV_RoundRect( PHYSDEV dev
, INT left
, INT top
, INT right
,
288 INT bottom
, INT ell_width
, INT ell_height
)
292 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dev
;
293 DC
*dc
= physDev
->dc
;
295 if(left
== right
|| top
== bottom
) return FALSE
;
297 if(left
> right
) {temp
= left
; left
= right
; right
= temp
;}
298 if(top
> bottom
) {temp
= top
; top
= bottom
; bottom
= temp
;}
300 if(dc
->GraphicsMode
== GM_COMPATIBLE
) {
305 emr
.emr
.iType
= EMR_ROUNDRECT
;
306 emr
.emr
.nSize
= sizeof(emr
);
307 emr
.rclBox
.left
= left
;
308 emr
.rclBox
.top
= top
;
309 emr
.rclBox
.right
= right
;
310 emr
.rclBox
.bottom
= bottom
;
311 emr
.szlCorner
.cx
= ell_width
;
312 emr
.szlCorner
.cy
= ell_height
;
314 EMFDRV_UpdateBBox( dev
, &emr
.rclBox
);
315 return EMFDRV_WriteRecord( dev
, &emr
.emr
);
318 /***********************************************************************
322 EMFDRV_SetPixel( PHYSDEV dev
, INT x
, INT y
, COLORREF color
)
328 /**********************************************************************
331 * Helper for EMFDRV_Poly{line|gon}
334 EMFDRV_Polylinegon( PHYSDEV dev
, const POINT
* pt
, INT count
, DWORD iType
)
341 size
= sizeof(EMRPOLYLINE
) + sizeof(POINTL
) * (count
- 1);
343 emr
= HeapAlloc( GetProcessHeap(), 0, size
);
344 emr
->emr
.iType
= iType
;
345 emr
->emr
.nSize
= size
;
347 emr
->rclBounds
.left
= emr
->rclBounds
.right
= pt
[0].x
;
348 emr
->rclBounds
.top
= emr
->rclBounds
.bottom
= pt
[0].y
;
350 for(i
= 1; i
< count
; i
++) {
351 if(pt
[i
].x
< emr
->rclBounds
.left
)
352 emr
->rclBounds
.left
= pt
[i
].x
;
353 else if(pt
[i
].x
> emr
->rclBounds
.right
)
354 emr
->rclBounds
.right
= pt
[i
].x
;
355 if(pt
[i
].y
< emr
->rclBounds
.top
)
356 emr
->rclBounds
.top
= pt
[i
].y
;
357 else if(pt
[i
].y
> emr
->rclBounds
.bottom
)
358 emr
->rclBounds
.bottom
= pt
[i
].y
;
362 memcpy(emr
->aptl
, pt
, count
* sizeof(POINTL
));
364 ret
= EMFDRV_WriteRecord( dev
, &emr
->emr
);
366 EMFDRV_UpdateBBox( dev
, &emr
->rclBounds
);
367 HeapFree( GetProcessHeap(), 0, emr
);
372 /**********************************************************************
376 EMFDRV_Polyline( PHYSDEV dev
, const POINT
* pt
, INT count
)
378 return EMFDRV_Polylinegon( dev
, pt
, count
, EMR_POLYLINE
);
381 /**********************************************************************
385 EMFDRV_Polygon( PHYSDEV dev
, const POINT
* pt
, INT count
)
387 if(count
< 2) return FALSE
;
388 return EMFDRV_Polylinegon( dev
, pt
, count
, EMR_POLYGON
);
392 /**********************************************************************
393 * EMFDRV_PolyPolylinegon
395 * Helper for EMFDRV_PolyPoly{line|gon}
398 EMFDRV_PolyPolylinegon( PHYSDEV dev
, const POINT
* pt
, const INT
* counts
, UINT polys
,
401 EMRPOLYPOLYLINE
*emr
;
402 DWORD cptl
= 0, poly
, size
, point
;
407 bounds
.left
= bounds
.right
= pt
[0].x
;
408 bounds
.top
= bounds
.bottom
= pt
[0].y
;
411 for(poly
= 0; poly
< polys
; poly
++) {
412 cptl
+= counts
[poly
];
413 for(point
= 0; point
< counts
[poly
]; point
++) {
414 if(bounds
.left
> pts
->x
) bounds
.left
= pts
->x
;
415 else if(bounds
.right
< pts
->x
) bounds
.right
= pts
->x
;
416 if(bounds
.top
> pts
->y
) bounds
.top
= pts
->y
;
417 else if(bounds
.bottom
< pts
->y
) bounds
.bottom
= pts
->y
;
422 size
= sizeof(EMRPOLYPOLYLINE
) + (polys
- 1) * sizeof(DWORD
) +
423 (cptl
- 1) * sizeof(POINTL
);
425 emr
= HeapAlloc( GetProcessHeap(), 0, size
);
427 emr
->emr
.iType
= iType
;
428 emr
->emr
.nSize
= size
;
429 emr
->rclBounds
= bounds
;
432 memcpy(emr
->aPolyCounts
, counts
, polys
* sizeof(DWORD
));
433 memcpy(emr
->aPolyCounts
+ polys
, pt
, cptl
* sizeof(POINTL
));
434 ret
= EMFDRV_WriteRecord( dev
, &emr
->emr
);
436 EMFDRV_UpdateBBox( dev
, &emr
->rclBounds
);
437 HeapFree( GetProcessHeap(), 0, emr
);
441 /**********************************************************************
442 * EMFDRV_PolyPolyline
445 EMFDRV_PolyPolyline(PHYSDEV dev
, const POINT
* pt
, const DWORD
* counts
, DWORD polys
)
447 return EMFDRV_PolyPolylinegon( dev
, pt
, (INT
*)counts
, polys
,
451 /**********************************************************************
455 EMFDRV_PolyPolygon( PHYSDEV dev
, const POINT
* pt
, const INT
* counts
, UINT polys
)
457 return EMFDRV_PolyPolylinegon( dev
, pt
, counts
, polys
, EMR_POLYPOLYGON
);
461 /**********************************************************************
462 * EMFDRV_ExtFloodFill
465 EMFDRV_ExtFloodFill( PHYSDEV dev
, INT x
, INT y
, COLORREF color
, UINT fillType
)
469 emr
.emr
.iType
= EMR_EXTFLOODFILL
;
470 emr
.emr
.nSize
= sizeof(emr
);
474 emr
.iMode
= fillType
;
476 return EMFDRV_WriteRecord( dev
, &emr
.emr
);
480 /*********************************************************************
483 BOOL
EMFDRV_FillRgn( PHYSDEV dev
, HRGN hrgn
, HBRUSH hbrush
)
486 DWORD size
, rgnsize
, index
;
489 index
= EMFDRV_CreateBrushIndirect( dev
, hbrush
);
490 if(!index
) return FALSE
;
492 rgnsize
= GetRegionData( hrgn
, 0, NULL
);
493 size
= rgnsize
+ sizeof(EMRFILLRGN
) - 1;
494 emr
= HeapAlloc( GetProcessHeap(), 0, size
);
496 GetRegionData( hrgn
, rgnsize
, (RGNDATA
*)&emr
->RgnData
);
498 emr
->emr
.iType
= EMR_FILLRGN
;
499 emr
->emr
.nSize
= size
;
500 emr
->rclBounds
.left
= ((RGNDATA
*)&emr
->RgnData
)->rdh
.rcBound
.left
;
501 emr
->rclBounds
.top
= ((RGNDATA
*)&emr
->RgnData
)->rdh
.rcBound
.top
;
502 emr
->rclBounds
.right
= ((RGNDATA
*)&emr
->RgnData
)->rdh
.rcBound
.right
- 1;
503 emr
->rclBounds
.bottom
= ((RGNDATA
*)&emr
->RgnData
)->rdh
.rcBound
.bottom
- 1;
504 emr
->cbRgnData
= rgnsize
;
505 emr
->ihBrush
= index
;
507 ret
= EMFDRV_WriteRecord( dev
, &emr
->emr
);
509 EMFDRV_UpdateBBox( dev
, &emr
->rclBounds
);
510 HeapFree( GetProcessHeap(), 0, emr
);
513 /*********************************************************************
516 BOOL
EMFDRV_FrameRgn( PHYSDEV dev
, HRGN hrgn
, HBRUSH hbrush
, INT width
, INT height
)
519 DWORD size
, rgnsize
, index
;
522 index
= EMFDRV_CreateBrushIndirect( dev
, hbrush
);
523 if(!index
) return FALSE
;
525 rgnsize
= GetRegionData( hrgn
, 0, NULL
);
526 size
= rgnsize
+ sizeof(EMRFRAMERGN
) - 1;
527 emr
= HeapAlloc( GetProcessHeap(), 0, size
);
529 GetRegionData( hrgn
, rgnsize
, (RGNDATA
*)&emr
->RgnData
);
531 emr
->emr
.iType
= EMR_FRAMERGN
;
532 emr
->emr
.nSize
= size
;
533 emr
->rclBounds
.left
= ((RGNDATA
*)&emr
->RgnData
)->rdh
.rcBound
.left
;
534 emr
->rclBounds
.top
= ((RGNDATA
*)&emr
->RgnData
)->rdh
.rcBound
.top
;
535 emr
->rclBounds
.right
= ((RGNDATA
*)&emr
->RgnData
)->rdh
.rcBound
.right
- 1;
536 emr
->rclBounds
.bottom
= ((RGNDATA
*)&emr
->RgnData
)->rdh
.rcBound
.bottom
- 1;
537 emr
->cbRgnData
= rgnsize
;
538 emr
->ihBrush
= index
;
539 emr
->szlStroke
.cx
= width
;
540 emr
->szlStroke
.cy
= height
;
542 ret
= EMFDRV_WriteRecord( dev
, &emr
->emr
);
544 EMFDRV_UpdateBBox( dev
, &emr
->rclBounds
);
545 HeapFree( GetProcessHeap(), 0, emr
);
549 /*********************************************************************
550 * EMFDRV_PaintInvertRgn
552 * Helper for EMFDRV_{Paint|Invert}Rgn
554 static BOOL
EMFDRV_PaintInvertRgn( PHYSDEV dev
, HRGN hrgn
, DWORD iType
)
561 rgnsize
= GetRegionData( hrgn
, 0, NULL
);
562 size
= rgnsize
+ sizeof(EMRINVERTRGN
) - 1;
563 emr
= HeapAlloc( GetProcessHeap(), 0, size
);
565 GetRegionData( hrgn
, rgnsize
, (RGNDATA
*)&emr
->RgnData
);
567 emr
->emr
.iType
= iType
;
568 emr
->emr
.nSize
= size
;
569 emr
->rclBounds
.left
= ((RGNDATA
*)&emr
->RgnData
)->rdh
.rcBound
.left
;
570 emr
->rclBounds
.top
= ((RGNDATA
*)&emr
->RgnData
)->rdh
.rcBound
.top
;
571 emr
->rclBounds
.right
= ((RGNDATA
*)&emr
->RgnData
)->rdh
.rcBound
.right
- 1;
572 emr
->rclBounds
.bottom
= ((RGNDATA
*)&emr
->RgnData
)->rdh
.rcBound
.bottom
- 1;
573 emr
->cbRgnData
= rgnsize
;
575 ret
= EMFDRV_WriteRecord( dev
, &emr
->emr
);
577 EMFDRV_UpdateBBox( dev
, &emr
->rclBounds
);
578 HeapFree( GetProcessHeap(), 0, emr
);
582 /**********************************************************************
586 EMFDRV_PaintRgn( PHYSDEV dev
, HRGN hrgn
)
588 return EMFDRV_PaintInvertRgn( dev
, hrgn
, EMR_PAINTRGN
);
591 /**********************************************************************
595 EMFDRV_InvertRgn( PHYSDEV dev
, HRGN hrgn
)
597 return EMFDRV_PaintInvertRgn( dev
, hrgn
, EMR_INVERTRGN
);
600 /**********************************************************************
604 EMFDRV_SetBkColor( PHYSDEV dev
, COLORREF color
)
608 emr
.emr
.iType
= EMR_SETBKCOLOR
;
609 emr
.emr
.nSize
= sizeof(emr
);
612 return EMFDRV_WriteRecord( dev
, &emr
.emr
) ? color
: CLR_INVALID
;
616 /**********************************************************************
617 * EMFDRV_SetTextColor
620 EMFDRV_SetTextColor( PHYSDEV dev
, COLORREF color
)
624 emr
.emr
.iType
= EMR_SETTEXTCOLOR
;
625 emr
.emr
.nSize
= sizeof(emr
);
628 return EMFDRV_WriteRecord( dev
, &emr
.emr
) ? color
: CLR_INVALID
;