Update the address of the Free Software Foundation.
[wine/testsucceed.git] / dlls / gdi / enhmfdrv / graphics.c
blob025e3585effa2c562c65b2614cb90c374b2d9ff7
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 <stdlib.h>
25 #include <string.h>
27 #include "gdi.h"
28 #include "enhmfdrv/enhmetafiledrv.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
33 /**********************************************************************
34 * EMFDRV_MoveTo
36 BOOL
37 EMFDRV_MoveTo(PHYSDEV dev, INT x, INT y)
39 EMRMOVETOEX emr;
41 emr.emr.iType = EMR_MOVETOEX;
42 emr.emr.nSize = sizeof(emr);
43 emr.ptl.x = x;
44 emr.ptl.y = y;
46 return EMFDRV_WriteRecord( dev, &emr.emr );
49 /***********************************************************************
50 * EMFDRV_LineTo
52 BOOL
53 EMFDRV_LineTo( PHYSDEV dev, INT x, INT y )
55 POINT pt;
56 EMRLINETO emr;
57 RECTL bounds;
58 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
60 emr.emr.iType = EMR_LINETO;
61 emr.emr.nSize = sizeof(emr);
62 emr.ptl.x = x;
63 emr.ptl.y = y;
65 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
66 return FALSE;
68 GetCurrentPositionEx(physDev->hdc, &pt);
70 bounds.left = min(x, pt.x);
71 bounds.top = min(y, pt.y);
72 bounds.right = max(x, pt.x);
73 bounds.bottom = max(y, pt.y);
75 EMFDRV_UpdateBBox( dev, &bounds );
77 return TRUE;
81 /***********************************************************************
82 * EMFDRV_ArcChordPie
84 static BOOL
85 EMFDRV_ArcChordPie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
86 INT xstart, INT ystart, INT xend, INT yend, DWORD iType )
88 INT temp, xCentre, yCentre, i;
89 double angleStart, angleEnd;
90 double xinterStart, yinterStart, xinterEnd, yinterEnd;
91 EMRARC emr;
92 RECTL bounds;
93 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
95 if(left == right || top == bottom) return FALSE;
97 if(left > right) {temp = left; left = right; right = temp;}
98 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
100 if(GetGraphicsMode(physDev->hdc) == GM_COMPATIBLE) {
101 right--;
102 bottom--;
105 emr.emr.iType = iType;
106 emr.emr.nSize = sizeof(emr);
107 emr.rclBox.left = left;
108 emr.rclBox.top = top;
109 emr.rclBox.right = right;
110 emr.rclBox.bottom = bottom;
111 emr.ptlStart.x = xstart;
112 emr.ptlStart.y = ystart;
113 emr.ptlEnd.x = xend;
114 emr.ptlEnd.x = yend;
117 /* Now calculate the BBox */
118 xCentre = (left + right + 1) / 2;
119 yCentre = (top + bottom + 1) / 2;
121 xstart -= xCentre;
122 ystart -= yCentre;
123 xend -= xCentre;
124 yend -= yCentre;
126 /* invert y co-ords to get angle anti-clockwise from x-axis */
127 angleStart = atan2( -(double)ystart, (double)xstart);
128 angleEnd = atan2( -(double)yend, (double)xend);
130 /* These are the intercepts of the start/end lines with the arc */
132 xinterStart = (right - left + 1)/2 * cos(angleStart) + xCentre;
133 yinterStart = -(bottom - top + 1)/2 * sin(angleStart) + yCentre;
134 xinterEnd = (right - left + 1)/2 * cos(angleEnd) + xCentre;
135 yinterEnd = -(bottom - top + 1)/2 * sin(angleEnd) + yCentre;
137 if(angleStart < 0) angleStart += 2 * M_PI;
138 if(angleEnd < 0) angleEnd += 2 * M_PI;
139 if(angleEnd < angleStart) angleEnd += 2 * M_PI;
141 bounds.left = min(xinterStart, xinterEnd);
142 bounds.top = min(yinterStart, yinterEnd);
143 bounds.right = max(xinterStart, xinterEnd);
144 bounds.bottom = max(yinterStart, yinterEnd);
146 for(i = 0; i <= 8; i++) {
147 if(i * M_PI / 2 < angleStart) /* loop until we're past start */
148 continue;
149 if(i * M_PI / 2 > angleEnd) /* if we're past end we're finished */
150 break;
152 /* the arc touches the rectangle at the start of quadrant i, so adjust
153 BBox to reflect this. */
155 switch(i % 4) {
156 case 0:
157 bounds.right = right;
158 break;
159 case 1:
160 bounds.top = top;
161 break;
162 case 2:
163 bounds.left = left;
164 break;
165 case 3:
166 bounds.bottom = bottom;
167 break;
171 /* If we're drawing a pie then make sure we include the centre */
172 if(iType == EMR_PIE) {
173 if(bounds.left > xCentre) bounds.left = xCentre;
174 else if(bounds.right < xCentre) bounds.right = xCentre;
175 if(bounds.top > yCentre) bounds.top = yCentre;
176 else if(bounds.bottom < yCentre) bounds.right = yCentre;
178 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
179 return FALSE;
180 EMFDRV_UpdateBBox( dev, &bounds );
181 return TRUE;
185 /***********************************************************************
186 * EMFDRV_Arc
188 BOOL
189 EMFDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
190 INT xstart, INT ystart, INT xend, INT yend )
192 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
193 xend, yend, EMR_ARC );
196 /***********************************************************************
197 * EMFDRV_Pie
199 BOOL
200 EMFDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
201 INT xstart, INT ystart, INT xend, INT yend )
203 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
204 xend, yend, EMR_PIE );
208 /***********************************************************************
209 * EMFDRV_Chord
211 BOOL
212 EMFDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
213 INT xstart, INT ystart, INT xend, INT yend )
215 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
216 xend, yend, EMR_CHORD );
219 /***********************************************************************
220 * EMFDRV_Ellipse
222 BOOL
223 EMFDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
225 EMRELLIPSE emr;
226 INT temp;
227 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
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(GetGraphicsMode(physDev->hdc) == GM_COMPATIBLE) {
237 right--;
238 bottom--;
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 /***********************************************************************
253 * EMFDRV_Rectangle
255 BOOL
256 EMFDRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
258 EMRRECTANGLE emr;
259 INT temp;
260 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
262 TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
264 if(left == right || top == bottom) return FALSE;
266 if(left > right) {temp = left; left = right; right = temp;}
267 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
269 if(GetGraphicsMode(physDev->hdc) == GM_COMPATIBLE) {
270 right--;
271 bottom--;
274 emr.emr.iType = EMR_RECTANGLE;
275 emr.emr.nSize = sizeof(emr);
276 emr.rclBox.left = left;
277 emr.rclBox.top = top;
278 emr.rclBox.right = right;
279 emr.rclBox.bottom = bottom;
281 EMFDRV_UpdateBBox( dev, &emr.rclBox );
282 return EMFDRV_WriteRecord( dev, &emr.emr );
285 /***********************************************************************
286 * EMFDRV_RoundRect
288 BOOL
289 EMFDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
290 INT bottom, INT ell_width, INT ell_height )
292 EMRROUNDRECT emr;
293 INT temp;
294 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
296 if(left == right || top == bottom) return FALSE;
298 if(left > right) {temp = left; left = right; right = temp;}
299 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
301 if(GetGraphicsMode(physDev->hdc) == GM_COMPATIBLE) {
302 right--;
303 bottom--;
306 emr.emr.iType = EMR_ROUNDRECT;
307 emr.emr.nSize = sizeof(emr);
308 emr.rclBox.left = left;
309 emr.rclBox.top = top;
310 emr.rclBox.right = right;
311 emr.rclBox.bottom = bottom;
312 emr.szlCorner.cx = ell_width;
313 emr.szlCorner.cy = ell_height;
315 EMFDRV_UpdateBBox( dev, &emr.rclBox );
316 return EMFDRV_WriteRecord( dev, &emr.emr );
319 /***********************************************************************
320 * EMFDRV_SetPixel
322 COLORREF
323 EMFDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
325 EMRSETPIXELV emr;
327 emr.emr.iType = EMR_SETPIXELV;
328 emr.emr.nSize = sizeof(emr);
329 emr.ptlPixel.x = x;
330 emr.ptlPixel.y = y;
331 emr.crColor = color;
333 if (EMFDRV_WriteRecord( dev, &emr.emr )) {
334 RECTL bounds;
335 bounds.left = bounds.right = x;
336 bounds.top = bounds.bottom = y;
337 EMFDRV_UpdateBBox( dev, &bounds );
338 return color;
340 return -1;
343 /**********************************************************************
344 * EMFDRV_Polylinegon
346 * Helper for EMFDRV_Poly{line|gon}
348 static BOOL
349 EMFDRV_Polylinegon( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
351 EMRPOLYLINE *emr;
352 DWORD size;
353 INT i;
354 BOOL ret;
356 size = sizeof(EMRPOLYLINE) + sizeof(POINTL) * (count - 1);
358 emr = HeapAlloc( GetProcessHeap(), 0, size );
359 emr->emr.iType = iType;
360 emr->emr.nSize = size;
362 emr->rclBounds.left = emr->rclBounds.right = pt[0].x;
363 emr->rclBounds.top = emr->rclBounds.bottom = pt[0].y;
365 for(i = 1; i < count; i++) {
366 if(pt[i].x < emr->rclBounds.left)
367 emr->rclBounds.left = pt[i].x;
368 else if(pt[i].x > emr->rclBounds.right)
369 emr->rclBounds.right = pt[i].x;
370 if(pt[i].y < emr->rclBounds.top)
371 emr->rclBounds.top = pt[i].y;
372 else if(pt[i].y > emr->rclBounds.bottom)
373 emr->rclBounds.bottom = pt[i].y;
376 emr->cptl = count;
377 memcpy(emr->aptl, pt, count * sizeof(POINTL));
379 ret = EMFDRV_WriteRecord( dev, &emr->emr );
380 if(ret)
381 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
382 HeapFree( GetProcessHeap(), 0, emr );
383 return ret;
387 /**********************************************************************
388 * EMFDRV_Polylinegon16
390 * Helper for EMFDRV_Poly{line|gon}
392 * This is not a legacy function!
393 * We are using SHORT integers to save space.
395 static BOOL
396 EMFDRV_Polylinegon16( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
398 EMRPOLYLINE16 *emr;
399 DWORD size;
400 INT i;
401 BOOL ret;
403 /* check whether all points fit in the SHORT int POINT structure */
404 for(i = 0; i < count; i++) {
405 if( ((pt[i].x+0x8000) & ~0xffff ) ||
406 ((pt[i].y+0x8000) & ~0xffff ) )
407 return FALSE;
410 size = sizeof(EMRPOLYLINE16) + sizeof(POINTS) * (count - 1);
412 emr = HeapAlloc( GetProcessHeap(), 0, size );
413 emr->emr.iType = iType;
414 emr->emr.nSize = size;
416 emr->rclBounds.left = emr->rclBounds.right = pt[0].x;
417 emr->rclBounds.top = emr->rclBounds.bottom = pt[0].y;
419 for(i = 1; i < count; i++) {
420 if(pt[i].x < emr->rclBounds.left)
421 emr->rclBounds.left = pt[i].x;
422 else if(pt[i].x > emr->rclBounds.right)
423 emr->rclBounds.right = pt[i].x;
424 if(pt[i].y < emr->rclBounds.top)
425 emr->rclBounds.top = pt[i].y;
426 else if(pt[i].y > emr->rclBounds.bottom)
427 emr->rclBounds.bottom = pt[i].y;
430 emr->cpts = count;
431 for(i = 0; i < count; i++ ) {
432 emr->apts[i].x = pt[i].x;
433 emr->apts[i].y = pt[i].y;
436 ret = EMFDRV_WriteRecord( dev, &emr->emr );
437 if(ret)
438 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
439 HeapFree( GetProcessHeap(), 0, emr );
440 return ret;
444 /**********************************************************************
445 * EMFDRV_Polyline
447 BOOL
448 EMFDRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
450 if( EMFDRV_Polylinegon16( dev, pt, count, EMR_POLYLINE16 ) )
451 return TRUE;
452 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINE );
455 /**********************************************************************
456 * EMFDRV_Polygon
458 BOOL
459 EMFDRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
461 if(count < 2) return FALSE;
462 if( EMFDRV_Polylinegon16( dev, pt, count, EMR_POLYGON16 ) )
463 return TRUE;
464 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYGON );
468 /**********************************************************************
469 * EMFDRV_PolyPolylinegon
471 * Helper for EMFDRV_PolyPoly{line|gon}
473 static BOOL
474 EMFDRV_PolyPolylinegon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys,
475 DWORD iType)
477 EMRPOLYPOLYLINE *emr;
478 DWORD cptl = 0, poly, size;
479 INT point;
480 RECTL bounds;
481 const POINT *pts;
482 BOOL ret;
484 bounds.left = bounds.right = pt[0].x;
485 bounds.top = bounds.bottom = pt[0].y;
487 pts = pt;
488 for(poly = 0; poly < polys; poly++) {
489 cptl += counts[poly];
490 for(point = 0; point < counts[poly]; point++) {
491 if(bounds.left > pts->x) bounds.left = pts->x;
492 else if(bounds.right < pts->x) bounds.right = pts->x;
493 if(bounds.top > pts->y) bounds.top = pts->y;
494 else if(bounds.bottom < pts->y) bounds.bottom = pts->y;
495 pts++;
499 size = sizeof(EMRPOLYPOLYLINE) + (polys - 1) * sizeof(DWORD) +
500 (cptl - 1) * sizeof(POINTL);
502 emr = HeapAlloc( GetProcessHeap(), 0, size );
504 emr->emr.iType = iType;
505 emr->emr.nSize = size;
506 emr->rclBounds = bounds;
507 emr->nPolys = polys;
508 emr->cptl = cptl;
509 memcpy(emr->aPolyCounts, counts, polys * sizeof(DWORD));
510 memcpy(emr->aPolyCounts + polys, pt, cptl * sizeof(POINTL));
511 ret = EMFDRV_WriteRecord( dev, &emr->emr );
512 if(ret)
513 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
514 HeapFree( GetProcessHeap(), 0, emr );
515 return ret;
518 /**********************************************************************
519 * EMFDRV_PolyPolyline
521 BOOL
522 EMFDRV_PolyPolyline(PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polys)
524 return EMFDRV_PolyPolylinegon( dev, pt, (const INT *)counts, polys,
525 EMR_POLYPOLYLINE );
528 /**********************************************************************
529 * EMFDRV_PolyPolygon
531 BOOL
532 EMFDRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys )
534 return EMFDRV_PolyPolylinegon( dev, pt, counts, polys, EMR_POLYPOLYGON );
538 /**********************************************************************
539 * EMFDRV_ExtFloodFill
541 BOOL
542 EMFDRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
544 EMREXTFLOODFILL emr;
546 emr.emr.iType = EMR_EXTFLOODFILL;
547 emr.emr.nSize = sizeof(emr);
548 emr.ptlStart.x = x;
549 emr.ptlStart.y = y;
550 emr.crColor = color;
551 emr.iMode = fillType;
553 return EMFDRV_WriteRecord( dev, &emr.emr );
557 /*********************************************************************
558 * EMFDRV_FillRgn
560 BOOL EMFDRV_FillRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush )
562 EMRFILLRGN *emr;
563 DWORD size, rgnsize, index;
564 BOOL ret;
566 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
567 if(!index) return FALSE;
569 rgnsize = GetRegionData( hrgn, 0, NULL );
570 size = rgnsize + offsetof(EMRFILLRGN,RgnData);
571 emr = HeapAlloc( GetProcessHeap(), 0, size );
573 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
575 emr->emr.iType = EMR_FILLRGN;
576 emr->emr.nSize = size;
577 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
578 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
579 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
580 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
581 emr->cbRgnData = rgnsize;
582 emr->ihBrush = index;
584 ret = EMFDRV_WriteRecord( dev, &emr->emr );
585 if(ret)
586 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
587 HeapFree( GetProcessHeap(), 0, emr );
588 return ret;
590 /*********************************************************************
591 * EMFDRV_FrameRgn
593 BOOL EMFDRV_FrameRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush, INT width, INT height )
595 EMRFRAMERGN *emr;
596 DWORD size, rgnsize, index;
597 BOOL ret;
599 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
600 if(!index) return FALSE;
602 rgnsize = GetRegionData( hrgn, 0, NULL );
603 size = rgnsize + offsetof(EMRFRAMERGN,RgnData);
604 emr = HeapAlloc( GetProcessHeap(), 0, size );
606 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
608 emr->emr.iType = EMR_FRAMERGN;
609 emr->emr.nSize = size;
610 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
611 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
612 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
613 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
614 emr->cbRgnData = rgnsize;
615 emr->ihBrush = index;
616 emr->szlStroke.cx = width;
617 emr->szlStroke.cy = height;
619 ret = EMFDRV_WriteRecord( dev, &emr->emr );
620 if(ret)
621 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
622 HeapFree( GetProcessHeap(), 0, emr );
623 return ret;
626 /*********************************************************************
627 * EMFDRV_PaintInvertRgn
629 * Helper for EMFDRV_{Paint|Invert}Rgn
631 static BOOL EMFDRV_PaintInvertRgn( PHYSDEV dev, HRGN hrgn, DWORD iType )
633 EMRINVERTRGN *emr;
634 DWORD size, rgnsize;
635 BOOL ret;
638 rgnsize = GetRegionData( hrgn, 0, NULL );
639 size = rgnsize + offsetof(EMRINVERTRGN,RgnData);
640 emr = HeapAlloc( GetProcessHeap(), 0, size );
642 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
644 emr->emr.iType = iType;
645 emr->emr.nSize = size;
646 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
647 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
648 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
649 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
650 emr->cbRgnData = rgnsize;
652 ret = EMFDRV_WriteRecord( dev, &emr->emr );
653 if(ret)
654 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
655 HeapFree( GetProcessHeap(), 0, emr );
656 return ret;
659 /**********************************************************************
660 * EMFDRV_PaintRgn
662 BOOL
663 EMFDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
665 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_PAINTRGN );
668 /**********************************************************************
669 * EMFDRV_InvertRgn
671 BOOL
672 EMFDRV_InvertRgn( PHYSDEV dev, HRGN hrgn )
674 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_INVERTRGN );
677 /**********************************************************************
678 * EMFDRV_SetBkColor
680 COLORREF
681 EMFDRV_SetBkColor( PHYSDEV dev, COLORREF color )
683 EMRSETBKCOLOR emr;
685 emr.emr.iType = EMR_SETBKCOLOR;
686 emr.emr.nSize = sizeof(emr);
687 emr.crColor = color;
689 return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
693 /**********************************************************************
694 * EMFDRV_SetTextColor
696 COLORREF
697 EMFDRV_SetTextColor( PHYSDEV dev, COLORREF color )
699 EMRSETTEXTCOLOR emr;
701 emr.emr.iType = EMR_SETTEXTCOLOR;
702 emr.emr.nSize = sizeof(emr);
703 emr.crColor = color;
705 return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
708 /**********************************************************************
709 * EMFDRV_ExtTextOut
711 BOOL EMFDRV_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags,
712 const RECT *lprect, LPCWSTR str, UINT count,
713 const INT *lpDx )
715 EMREXTTEXTOUTW *pemr;
716 DWORD nSize;
717 BOOL ret;
718 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
719 int textHeight = 0;
720 int textWidth = 0;
721 const UINT textAlign = GetTextAlign(physDev->hdc);
723 nSize = sizeof(*pemr) + ((count+1) & ~1) * sizeof(WCHAR) + count * sizeof(INT);
725 TRACE("%s %s count %d nSize = %ld\n", debugstr_wn(str, count),
726 wine_dbgstr_rect(lprect), count, nSize);
727 pemr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nSize);
729 pemr->emr.iType = EMR_EXTTEXTOUTW;
730 pemr->emr.nSize = nSize;
732 pemr->iGraphicsMode = GetGraphicsMode(physDev->hdc);
733 pemr->exScale = pemr->eyScale = 1.0; /* FIXME */
735 pemr->emrtext.ptlReference.x = x;
736 pemr->emrtext.ptlReference.y = y;
737 pemr->emrtext.nChars = count;
738 pemr->emrtext.offString = sizeof(*pemr);
739 memcpy((char*)pemr + pemr->emrtext.offString, str, count * sizeof(WCHAR));
740 pemr->emrtext.fOptions = flags;
741 if(!lprect) {
742 pemr->emrtext.rcl.left = pemr->emrtext.rcl.top = 0;
743 pemr->emrtext.rcl.right = pemr->emrtext.rcl.bottom = -1;
744 } else {
745 pemr->emrtext.rcl.left = lprect->left;
746 pemr->emrtext.rcl.top = lprect->top;
747 pemr->emrtext.rcl.right = lprect->right;
748 pemr->emrtext.rcl.bottom = lprect->bottom;
751 pemr->emrtext.offDx = pemr->emrtext.offString + ((count+1) & ~1) * sizeof(WCHAR);
752 if(lpDx) {
753 UINT i;
754 SIZE strSize;
755 memcpy((char*)pemr + pemr->emrtext.offDx, lpDx, count * sizeof(INT));
756 for (i = 0; i < count; i++) {
757 textWidth += lpDx[i];
759 GetTextExtentPoint32W(physDev->hdc, str, count, &strSize);
760 textHeight = strSize.cy;
762 else {
763 UINT i;
764 INT *dx = (INT *)((char*)pemr + pemr->emrtext.offDx);
765 SIZE charSize;
766 for (i = 0; i < count; i++) {
767 GetTextExtentPoint32W(physDev->hdc, str + i, 1, &charSize);
768 dx[i] = charSize.cx;
769 textWidth += charSize.cx;
770 textHeight = max(textHeight, charSize.cy);
774 switch (textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER)) {
775 case TA_CENTER: {
776 pemr->rclBounds.left = x - (textWidth / 2) - 1;
777 pemr->rclBounds.right = x + (textWidth / 2) + 1;
778 break;
780 case TA_RIGHT: {
781 pemr->rclBounds.left = x - textWidth - 1;
782 pemr->rclBounds.right = x;
783 break;
785 default: { /* TA_LEFT */
786 pemr->rclBounds.left = x;
787 pemr->rclBounds.right = x + textWidth + 1;
791 switch (textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE)) {
792 case TA_BASELINE: {
793 TEXTMETRICW tm;
794 GetTextMetricsW(physDev->hdc, &tm);
795 /* Play safe here... it's better to have a bounding box */
796 /* that is too big than too small. */
797 pemr->rclBounds.top = y - textHeight - 1;
798 pemr->rclBounds.bottom = y + tm.tmDescent + 1;
799 break;
801 case TA_BOTTOM: {
802 pemr->rclBounds.top = y - textHeight - 1;
803 pemr->rclBounds.bottom = y;
804 break;
806 default: { /* TA_TOP */
807 pemr->rclBounds.top = y;
808 pemr->rclBounds.bottom = y + textHeight + 1;
812 ret = EMFDRV_WriteRecord( dev, &pemr->emr );
813 if(ret)
814 EMFDRV_UpdateBBox( dev, &pemr->rclBounds );
815 HeapFree( GetProcessHeap(), 0, pemr );
816 return ret;
819 /**********************************************************************
820 * EMFDRV_SetArcDirection
822 INT EMFDRV_SetArcDirection(PHYSDEV dev, INT arcDirection)
824 EMRSETARCDIRECTION emr;
826 emr.emr.iType = EMR_SETARCDIRECTION;
827 emr.emr.nSize = sizeof(emr);
828 emr.iArcDirection = arcDirection;
830 EMFDRV_WriteRecord(dev, &emr.emr);
832 /* We don't know the old arc direction and we don't care... */
833 return 0;