009f69f8b6be22262f70513f8d9997524811e77a
[reactos.git] / reactos / win32ss / gdi / gdi32 / wine / enhmfdrv / graphics.c
1 /*
2 * Enhanced MetaFile driver graphics functions
3 *
4 * Copyright 1999 Huw D M Davies
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "enhmfdrv/enhmetafiledrv.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
35
36 static const RECTL empty_bounds = { 0, 0, -1, -1 };
37
38 /* determine if we can use 16-bit points to store all the input points */
39 static BOOL can_use_short_points( const POINT *pts, UINT count )
40 {
41 UINT i;
42
43 for (i = 0; i < count; i++)
44 if (((pts[i].x + 0x8000) & ~0xffff) || ((pts[i].y + 0x8000) & ~0xffff))
45 return FALSE;
46 return TRUE;
47 }
48
49 /* store points in either long or short format; return a pointer to the end of the stored data */
50 static void *store_points( POINTL *dest, const POINT *pts, UINT count, BOOL short_points )
51 {
52 if (short_points)
53 {
54 UINT i;
55 POINTS *dest_short = (POINTS *)dest;
56
57 for (i = 0; i < count; i++)
58 {
59 dest_short[i].x = pts[i].x;
60 dest_short[i].y = pts[i].y;
61 }
62 return dest_short + count;
63 }
64 else
65 {
66 memcpy( dest, pts, count * sizeof(*dest) );
67 return dest + count;
68 }
69 }
70
71 /* compute the bounds of an array of points, optionally including the current position */
72 static void get_points_bounds( RECTL *bounds, const POINT *pts, UINT count, HDC hdc )
73 {
74 UINT i;
75
76 if (hdc)
77 {
78 POINT cur_pt;
79 GetCurrentPositionEx( hdc, &cur_pt );
80 bounds->left = bounds->right = cur_pt.x;
81 bounds->top = bounds->bottom = cur_pt.y;
82 }
83 else if (count)
84 {
85 bounds->left = bounds->right = pts[0].x;
86 bounds->top = bounds->bottom = pts[0].y;
87 }
88 else *bounds = empty_bounds;
89
90 for (i = 0; i < count; i++)
91 {
92 bounds->left = min( bounds->left, pts[i].x );
93 bounds->right = max( bounds->right, pts[i].x );
94 bounds->top = min( bounds->top, pts[i].y );
95 bounds->bottom = max( bounds->bottom, pts[i].y );
96 }
97 }
98
99 /**********************************************************************
100 * EMFDRV_MoveTo
101 */
102 BOOL EMFDRV_MoveTo(PHYSDEV dev, INT x, INT y)
103 {
104 EMRMOVETOEX emr;
105
106 emr.emr.iType = EMR_MOVETOEX;
107 emr.emr.nSize = sizeof(emr);
108 emr.ptl.x = x;
109 emr.ptl.y = y;
110
111 return EMFDRV_WriteRecord( dev, &emr.emr );
112 }
113
114 /***********************************************************************
115 * EMFDRV_LineTo
116 */
117 BOOL EMFDRV_LineTo( PHYSDEV dev, INT x, INT y )
118 {
119 POINT pt;
120 EMRLINETO emr;
121 RECTL bounds;
122
123 emr.emr.iType = EMR_LINETO;
124 emr.emr.nSize = sizeof(emr);
125 emr.ptl.x = x;
126 emr.ptl.y = y;
127
128 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
129 return FALSE;
130
131 GetCurrentPositionEx( dev->hdc, &pt );
132
133 bounds.left = min(x, pt.x);
134 bounds.top = min(y, pt.y);
135 bounds.right = max(x, pt.x);
136 bounds.bottom = max(y, pt.y);
137
138 EMFDRV_UpdateBBox( dev, &bounds );
139
140 return TRUE;
141 }
142
143
144 /***********************************************************************
145 * EMFDRV_ArcChordPie
146 */
147 static BOOL
148 EMFDRV_ArcChordPie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
149 INT xstart, INT ystart, INT xend, INT yend, DWORD iType )
150 {
151 INT temp, xCentre, yCentre, i;
152 double angleStart, angleEnd;
153 double xinterStart, yinterStart, xinterEnd, yinterEnd;
154 EMRARC emr;
155 RECTL bounds;
156
157 if(left == right || top == bottom) return FALSE;
158
159 if(left > right) {temp = left; left = right; right = temp;}
160 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
161
162 if(GetGraphicsMode(dev->hdc) == GM_COMPATIBLE) {
163 right--;
164 bottom--;
165 }
166
167 emr.emr.iType = iType;
168 emr.emr.nSize = sizeof(emr);
169 emr.rclBox.left = left;
170 emr.rclBox.top = top;
171 emr.rclBox.right = right;
172 emr.rclBox.bottom = bottom;
173 emr.ptlStart.x = xstart;
174 emr.ptlStart.y = ystart;
175 emr.ptlEnd.x = xend;
176 emr.ptlEnd.y = yend;
177
178
179 /* Now calculate the BBox */
180 xCentre = (left + right + 1) / 2;
181 yCentre = (top + bottom + 1) / 2;
182
183 xstart -= xCentre;
184 ystart -= yCentre;
185 xend -= xCentre;
186 yend -= yCentre;
187
188 /* invert y co-ords to get angle anti-clockwise from x-axis */
189 angleStart = atan2( -(double)ystart, (double)xstart);
190 angleEnd = atan2( -(double)yend, (double)xend);
191
192 /* These are the intercepts of the start/end lines with the arc */
193
194 xinterStart = (right - left + 1)/2 * cos(angleStart) + xCentre;
195 yinterStart = -(bottom - top + 1)/2 * sin(angleStart) + yCentre;
196 xinterEnd = (right - left + 1)/2 * cos(angleEnd) + xCentre;
197 yinterEnd = -(bottom - top + 1)/2 * sin(angleEnd) + yCentre;
198
199 if(angleStart < 0) angleStart += 2 * M_PI;
200 if(angleEnd < 0) angleEnd += 2 * M_PI;
201 if(angleEnd < angleStart) angleEnd += 2 * M_PI;
202
203 bounds.left = min(xinterStart, xinterEnd);
204 bounds.top = min(yinterStart, yinterEnd);
205 bounds.right = max(xinterStart, xinterEnd);
206 bounds.bottom = max(yinterStart, yinterEnd);
207
208 for(i = 0; i <= 8; i++) {
209 if(i * M_PI / 2 < angleStart) /* loop until we're past start */
210 continue;
211 if(i * M_PI / 2 > angleEnd) /* if we're past end we're finished */
212 break;
213
214 /* the arc touches the rectangle at the start of quadrant i, so adjust
215 BBox to reflect this. */
216
217 switch(i % 4) {
218 case 0:
219 bounds.right = right;
220 break;
221 case 1:
222 bounds.top = top;
223 break;
224 case 2:
225 bounds.left = left;
226 break;
227 case 3:
228 bounds.bottom = bottom;
229 break;
230 }
231 }
232
233 /* If we're drawing a pie then make sure we include the centre */
234 if(iType == EMR_PIE) {
235 if(bounds.left > xCentre) bounds.left = xCentre;
236 else if(bounds.right < xCentre) bounds.right = xCentre;
237 if(bounds.top > yCentre) bounds.top = yCentre;
238 else if(bounds.bottom < yCentre) bounds.bottom = yCentre;
239 }
240 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
241 return FALSE;
242 EMFDRV_UpdateBBox( dev, &bounds );
243 return TRUE;
244 }
245
246
247 /***********************************************************************
248 * EMFDRV_Arc
249 */
250 BOOL EMFDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
251 INT xstart, INT ystart, INT xend, INT yend )
252 {
253 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
254 xend, yend, EMR_ARC );
255 }
256
257 /***********************************************************************
258 * EMFDRV_Pie
259 */
260 BOOL EMFDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
261 INT xstart, INT ystart, INT xend, INT yend )
262 {
263 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
264 xend, yend, EMR_PIE );
265 }
266
267
268 /***********************************************************************
269 * EMFDRV_Chord
270 */
271 BOOL EMFDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
272 INT xstart, INT ystart, INT xend, INT yend )
273 {
274 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
275 xend, yend, EMR_CHORD );
276 }
277
278 /***********************************************************************
279 * EMFDRV_Ellipse
280 */
281 BOOL EMFDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
282 {
283 EMRELLIPSE emr;
284 INT temp;
285
286 TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
287
288 if(left == right || top == bottom) return FALSE;
289
290 if(left > right) {temp = left; left = right; right = temp;}
291 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
292
293 if(GetGraphicsMode( dev->hdc ) == GM_COMPATIBLE) {
294 right--;
295 bottom--;
296 }
297
298 emr.emr.iType = EMR_ELLIPSE;
299 emr.emr.nSize = sizeof(emr);
300 emr.rclBox.left = left;
301 emr.rclBox.top = top;
302 emr.rclBox.right = right;
303 emr.rclBox.bottom = bottom;
304
305 EMFDRV_UpdateBBox( dev, &emr.rclBox );
306 return EMFDRV_WriteRecord( dev, &emr.emr );
307 }
308
309 /***********************************************************************
310 * EMFDRV_Rectangle
311 */
312 BOOL EMFDRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
313 {
314 EMRRECTANGLE emr;
315 INT temp;
316
317 TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
318
319 if(left == right || top == bottom) return FALSE;
320
321 if(left > right) {temp = left; left = right; right = temp;}
322 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
323
324 if(GetGraphicsMode( dev->hdc ) == GM_COMPATIBLE) {
325 right--;
326 bottom--;
327 }
328
329 emr.emr.iType = EMR_RECTANGLE;
330 emr.emr.nSize = sizeof(emr);
331 emr.rclBox.left = left;
332 emr.rclBox.top = top;
333 emr.rclBox.right = right;
334 emr.rclBox.bottom = bottom;
335
336 EMFDRV_UpdateBBox( dev, &emr.rclBox );
337 return EMFDRV_WriteRecord( dev, &emr.emr );
338 }
339
340 /***********************************************************************
341 * EMFDRV_RoundRect
342 */
343 BOOL EMFDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
344 INT bottom, INT ell_width, INT ell_height )
345 {
346 EMRROUNDRECT emr;
347 INT temp;
348
349 if(left == right || top == bottom) return FALSE;
350
351 if(left > right) {temp = left; left = right; right = temp;}
352 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
353
354 if(GetGraphicsMode( dev->hdc ) == GM_COMPATIBLE) {
355 right--;
356 bottom--;
357 }
358
359 emr.emr.iType = EMR_ROUNDRECT;
360 emr.emr.nSize = sizeof(emr);
361 emr.rclBox.left = left;
362 emr.rclBox.top = top;
363 emr.rclBox.right = right;
364 emr.rclBox.bottom = bottom;
365 emr.szlCorner.cx = ell_width;
366 emr.szlCorner.cy = ell_height;
367
368 EMFDRV_UpdateBBox( dev, &emr.rclBox );
369 return EMFDRV_WriteRecord( dev, &emr.emr );
370 }
371
372 /***********************************************************************
373 * EMFDRV_SetPixel
374 */
375 COLORREF EMFDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
376 {
377 EMRSETPIXELV emr;
378
379 emr.emr.iType = EMR_SETPIXELV;
380 emr.emr.nSize = sizeof(emr);
381 emr.ptlPixel.x = x;
382 emr.ptlPixel.y = y;
383 emr.crColor = color;
384
385 if (EMFDRV_WriteRecord( dev, &emr.emr )) {
386 RECTL bounds;
387 bounds.left = bounds.right = x;
388 bounds.top = bounds.bottom = y;
389 EMFDRV_UpdateBBox( dev, &bounds );
390 return color;
391 }
392 return -1;
393 }
394
395 /**********************************************************************
396 * EMFDRV_Polylinegon
397 *
398 * Helper for EMFDRV_Poly{line|gon}
399 */
400 static BOOL
401 EMFDRV_Polylinegon( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
402 {
403 EMRPOLYLINE *emr;
404 DWORD size;
405 INT i;
406 BOOL ret;
407
408 size = sizeof(EMRPOLYLINE) + sizeof(POINTL) * (count - 1);
409
410 emr = HeapAlloc( GetProcessHeap(), 0, size );
411 emr->emr.iType = iType;
412 emr->emr.nSize = size;
413
414 emr->rclBounds.left = emr->rclBounds.right = pt[0].x;
415 emr->rclBounds.top = emr->rclBounds.bottom = pt[0].y;
416
417 for(i = 1; i < count; i++) {
418 if(pt[i].x < emr->rclBounds.left)
419 emr->rclBounds.left = pt[i].x;
420 else if(pt[i].x > emr->rclBounds.right)
421 emr->rclBounds.right = pt[i].x;
422 if(pt[i].y < emr->rclBounds.top)
423 emr->rclBounds.top = pt[i].y;
424 else if(pt[i].y > emr->rclBounds.bottom)
425 emr->rclBounds.bottom = pt[i].y;
426 }
427
428 emr->cptl = count;
429 memcpy(emr->aptl, pt, count * sizeof(POINTL));
430
431 ret = EMFDRV_WriteRecord( dev, &emr->emr );
432 if(ret)
433 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
434 HeapFree( GetProcessHeap(), 0, emr );
435 return ret;
436 }
437
438
439 /**********************************************************************
440 * EMFDRV_Polylinegon16
441 *
442 * Helper for EMFDRV_Poly{line|gon}
443 *
444 * This is not a legacy function!
445 * We are using SHORT integers to save space.
446 */
447 static BOOL
448 EMFDRV_Polylinegon16( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
449 {
450 EMRPOLYLINE16 *emr;
451 DWORD size;
452 INT i;
453 BOOL ret;
454
455 /* check whether all points fit in the SHORT int POINT structure */
456 for(i = 0; i < count; i++) {
457 if( ((pt[i].x+0x8000) & ~0xffff ) ||
458 ((pt[i].y+0x8000) & ~0xffff ) )
459 return FALSE;
460 }
461
462 size = sizeof(EMRPOLYLINE16) + sizeof(POINTS) * (count - 1);
463
464 emr = HeapAlloc( GetProcessHeap(), 0, size );
465 emr->emr.iType = iType;
466 emr->emr.nSize = size;
467
468 emr->rclBounds.left = emr->rclBounds.right = pt[0].x;
469 emr->rclBounds.top = emr->rclBounds.bottom = pt[0].y;
470
471 for(i = 1; i < count; i++) {
472 if(pt[i].x < emr->rclBounds.left)
473 emr->rclBounds.left = pt[i].x;
474 else if(pt[i].x > emr->rclBounds.right)
475 emr->rclBounds.right = pt[i].x;
476 if(pt[i].y < emr->rclBounds.top)
477 emr->rclBounds.top = pt[i].y;
478 else if(pt[i].y > emr->rclBounds.bottom)
479 emr->rclBounds.bottom = pt[i].y;
480 }
481
482 emr->cpts = count;
483 for(i = 0; i < count; i++ ) {
484 emr->apts[i].x = pt[i].x;
485 emr->apts[i].y = pt[i].y;
486 }
487
488 ret = EMFDRV_WriteRecord( dev, &emr->emr );
489 if(ret)
490 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
491 HeapFree( GetProcessHeap(), 0, emr );
492 return ret;
493 }
494
495
496 /**********************************************************************
497 * EMFDRV_Polyline
498 */
499 BOOL EMFDRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
500 {
501 if( EMFDRV_Polylinegon16( dev, pt, count, EMR_POLYLINE16 ) )
502 return TRUE;
503 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINE );
504 }
505
506 /**********************************************************************
507 * EMFDRV_Polygon
508 */
509 BOOL EMFDRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
510 {
511 if(count < 2) return FALSE;
512 if( EMFDRV_Polylinegon16( dev, pt, count, EMR_POLYGON16 ) )
513 return TRUE;
514 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYGON );
515 }
516
517 /**********************************************************************
518 * EMFDRV_PolyBezier
519 */
520 BOOL EMFDRV_PolyBezier( PHYSDEV dev, const POINT *pts, DWORD count )
521 {
522 if(EMFDRV_Polylinegon16( dev, pts, count, EMR_POLYBEZIER16 ))
523 return TRUE;
524 return EMFDRV_Polylinegon( dev, pts, count, EMR_POLYBEZIER );
525 }
526
527 /**********************************************************************
528 * EMFDRV_PolyBezierTo
529 */
530 BOOL EMFDRV_PolyBezierTo( PHYSDEV dev, const POINT *pts, DWORD count )
531 {
532 if(EMFDRV_Polylinegon16( dev, pts, count, EMR_POLYBEZIERTO16 ))
533 return TRUE;
534 return EMFDRV_Polylinegon( dev, pts, count, EMR_POLYBEZIERTO );
535 }
536
537
538 /**********************************************************************
539 * EMFDRV_PolyPolylinegon
540 *
541 * Helper for EMFDRV_PolyPoly{line|gon}
542 */
543 static BOOL
544 EMFDRV_PolyPolylinegon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys,
545 DWORD iType)
546 {
547 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
548 EMRPOLYPOLYLINE *emr;
549 DWORD cptl = 0, poly, size;
550 BOOL ret, use_small_emr, bounds_valid = TRUE;
551
552 for(poly = 0; poly < polys; poly++) {
553 cptl += counts[poly];
554 if(counts[poly] < 2) bounds_valid = FALSE;
555 }
556 if(!cptl) bounds_valid = FALSE;
557 use_small_emr = can_use_short_points( pt, cptl );
558
559 size = FIELD_OFFSET(EMRPOLYPOLYLINE, aPolyCounts[polys]);
560 if(use_small_emr)
561 size += cptl * sizeof(POINTS);
562 else
563 size += cptl * sizeof(POINTL);
564
565 emr = HeapAlloc( GetProcessHeap(), 0, size );
566
567 emr->emr.iType = iType;
568 if(use_small_emr) emr->emr.iType += EMR_POLYPOLYLINE16 - EMR_POLYPOLYLINE;
569
570 emr->emr.nSize = size;
571 if(bounds_valid && !physDev->path)
572 get_points_bounds( &emr->rclBounds, pt, cptl, 0 );
573 else
574 emr->rclBounds = empty_bounds;
575 emr->nPolys = polys;
576 emr->cptl = cptl;
577
578 if(polys)
579 {
580 memcpy( emr->aPolyCounts, counts, polys * sizeof(DWORD) );
581 store_points( (POINTL *)(emr->aPolyCounts + polys), pt, cptl, use_small_emr );
582 }
583
584 ret = EMFDRV_WriteRecord( dev, &emr->emr );
585 if(ret && !bounds_valid)
586 {
587 ret = FALSE;
588 SetLastError( ERROR_INVALID_PARAMETER );
589 }
590 if(ret && !physDev->path)
591 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
592 HeapFree( GetProcessHeap(), 0, emr );
593 return ret;
594 }
595
596 /**********************************************************************
597 * EMFDRV_PolyPolyline
598 */
599 BOOL EMFDRV_PolyPolyline(PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polys)
600 {
601 return EMFDRV_PolyPolylinegon( dev, pt, (const INT *)counts, polys,
602 EMR_POLYPOLYLINE );
603 }
604
605 /**********************************************************************
606 * EMFDRV_PolyPolygon
607 */
608 BOOL EMFDRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys )
609 {
610 return EMFDRV_PolyPolylinegon( dev, pt, counts, polys, EMR_POLYPOLYGON );
611 }
612
613
614 /**********************************************************************
615 * EMFDRV_ExtFloodFill
616 */
617 BOOL EMFDRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
618 {
619 EMREXTFLOODFILL emr;
620
621 emr.emr.iType = EMR_EXTFLOODFILL;
622 emr.emr.nSize = sizeof(emr);
623 emr.ptlStart.x = x;
624 emr.ptlStart.y = y;
625 emr.crColor = color;
626 emr.iMode = fillType;
627
628 return EMFDRV_WriteRecord( dev, &emr.emr );
629 }
630
631
632 /*********************************************************************
633 * EMFDRV_FillRgn
634 */
635 BOOL EMFDRV_FillRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush )
636 {
637 EMRFILLRGN *emr;
638 DWORD size, rgnsize, index;
639 BOOL ret;
640
641 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
642 if(!index) return FALSE;
643
644 rgnsize = GetRegionData( hrgn, 0, NULL );
645 size = rgnsize + offsetof(EMRFILLRGN,RgnData);
646 emr = HeapAlloc( GetProcessHeap(), 0, size );
647
648 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
649
650 emr->emr.iType = EMR_FILLRGN;
651 emr->emr.nSize = size;
652 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
653 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
654 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
655 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
656 emr->cbRgnData = rgnsize;
657 emr->ihBrush = index;
658
659 ret = EMFDRV_WriteRecord( dev, &emr->emr );
660 if(ret)
661 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
662 HeapFree( GetProcessHeap(), 0, emr );
663 return ret;
664 }
665 /*********************************************************************
666 * EMFDRV_FrameRgn
667 */
668 BOOL EMFDRV_FrameRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush, INT width, INT height )
669 {
670 EMRFRAMERGN *emr;
671 DWORD size, rgnsize, index;
672 BOOL ret;
673
674 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
675 if(!index) return FALSE;
676
677 rgnsize = GetRegionData( hrgn, 0, NULL );
678 size = rgnsize + offsetof(EMRFRAMERGN,RgnData);
679 emr = HeapAlloc( GetProcessHeap(), 0, size );
680
681 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
682
683 emr->emr.iType = EMR_FRAMERGN;
684 emr->emr.nSize = size;
685 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
686 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
687 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
688 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
689 emr->cbRgnData = rgnsize;
690 emr->ihBrush = index;
691 emr->szlStroke.cx = width;
692 emr->szlStroke.cy = height;
693
694 ret = EMFDRV_WriteRecord( dev, &emr->emr );
695 if(ret)
696 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
697 HeapFree( GetProcessHeap(), 0, emr );
698 return ret;
699 }
700
701 /*********************************************************************
702 * EMFDRV_PaintInvertRgn
703 *
704 * Helper for EMFDRV_{Paint|Invert}Rgn
705 */
706 static BOOL EMFDRV_PaintInvertRgn( PHYSDEV dev, HRGN hrgn, DWORD iType )
707 {
708 EMRINVERTRGN *emr;
709 DWORD size, rgnsize;
710 BOOL ret;
711
712
713 rgnsize = GetRegionData( hrgn, 0, NULL );
714 size = rgnsize + offsetof(EMRINVERTRGN,RgnData);
715 emr = HeapAlloc( GetProcessHeap(), 0, size );
716
717 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
718
719 emr->emr.iType = iType;
720 emr->emr.nSize = size;
721 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
722 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
723 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
724 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
725 emr->cbRgnData = rgnsize;
726
727 ret = EMFDRV_WriteRecord( dev, &emr->emr );
728 if(ret)
729 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
730 HeapFree( GetProcessHeap(), 0, emr );
731 return ret;
732 }
733
734 /**********************************************************************
735 * EMFDRV_PaintRgn
736 */
737 BOOL EMFDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
738 {
739 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_PAINTRGN );
740 }
741
742 /**********************************************************************
743 * EMFDRV_InvertRgn
744 */
745 BOOL EMFDRV_InvertRgn( PHYSDEV dev, HRGN hrgn )
746 {
747 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_INVERTRGN );
748 }
749
750 /**********************************************************************
751 * EMFDRV_ExtTextOut
752 */
753 BOOL EMFDRV_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags, const RECT *lprect,
754 LPCWSTR str, UINT count, const INT *lpDx )
755 {
756 EMREXTTEXTOUTW *pemr;
757 DWORD nSize;
758 BOOL ret;
759 int textHeight = 0;
760 int textWidth = 0;
761 const UINT textAlign = GetTextAlign( dev->hdc );
762 const INT graphicsMode = GetGraphicsMode( dev->hdc );
763 FLOAT exScale, eyScale;
764
765 nSize = sizeof(*pemr) + ((count+1) & ~1) * sizeof(WCHAR) + count * sizeof(INT);
766
767 TRACE("%s %s count %d nSize = %d\n", debugstr_wn(str, count),
768 wine_dbgstr_rect(lprect), count, nSize);
769 pemr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nSize);
770
771 if (graphicsMode == GM_COMPATIBLE)
772 {
773 const INT horzSize = GetDeviceCaps( dev->hdc, HORZSIZE );
774 const INT horzRes = GetDeviceCaps( dev->hdc, HORZRES );
775 const INT vertSize = GetDeviceCaps( dev->hdc, VERTSIZE );
776 const INT vertRes = GetDeviceCaps( dev->hdc, VERTRES );
777 SIZE wndext, vportext;
778
779 GetViewportExtEx( dev->hdc, &vportext );
780 GetWindowExtEx( dev->hdc, &wndext );
781 exScale = 100.0 * ((FLOAT)horzSize / (FLOAT)horzRes) /
782 ((FLOAT)wndext.cx / (FLOAT)vportext.cx);
783 eyScale = 100.0 * ((FLOAT)vertSize / (FLOAT)vertRes) /
784 ((FLOAT)wndext.cy / (FLOAT)vportext.cy);
785 }
786 else
787 {
788 exScale = 0.0;
789 eyScale = 0.0;
790 }
791
792 pemr->emr.iType = EMR_EXTTEXTOUTW;
793 pemr->emr.nSize = nSize;
794 pemr->iGraphicsMode = graphicsMode;
795 pemr->exScale = exScale;
796 pemr->eyScale = eyScale;
797 pemr->emrtext.ptlReference.x = x;
798 pemr->emrtext.ptlReference.y = y;
799 pemr->emrtext.nChars = count;
800 pemr->emrtext.offString = sizeof(*pemr);
801 memcpy((char*)pemr + pemr->emrtext.offString, str, count * sizeof(WCHAR));
802 pemr->emrtext.fOptions = flags;
803 if(!lprect) {
804 pemr->emrtext.rcl.left = pemr->emrtext.rcl.top = 0;
805 pemr->emrtext.rcl.right = pemr->emrtext.rcl.bottom = -1;
806 } else {
807 pemr->emrtext.rcl.left = lprect->left;
808 pemr->emrtext.rcl.top = lprect->top;
809 pemr->emrtext.rcl.right = lprect->right;
810 pemr->emrtext.rcl.bottom = lprect->bottom;
811 }
812
813 pemr->emrtext.offDx = pemr->emrtext.offString + ((count+1) & ~1) * sizeof(WCHAR);
814 if(lpDx) {
815 UINT i;
816 SIZE strSize;
817 memcpy((char*)pemr + pemr->emrtext.offDx, lpDx, count * sizeof(INT));
818 for (i = 0; i < count; i++) {
819 textWidth += lpDx[i];
820 }
821 if (GetTextExtentPoint32W( dev->hdc, str, count, &strSize ))
822 textHeight = strSize.cy;
823 }
824 else {
825 UINT i;
826 INT *dx = (INT *)((char*)pemr + pemr->emrtext.offDx);
827 SIZE charSize;
828 for (i = 0; i < count; i++) {
829 if (GetTextExtentPoint32W( dev->hdc, str + i, 1, &charSize )) {
830 dx[i] = charSize.cx;
831 textWidth += charSize.cx;
832 textHeight = max(textHeight, charSize.cy);
833 }
834 }
835 }
836
837 if (!lprect)
838 {
839 pemr->rclBounds.left = pemr->rclBounds.top = 0;
840 pemr->rclBounds.right = pemr->rclBounds.bottom = -1;
841 goto no_bounds;
842 }
843
844 switch (textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER)) {
845 case TA_CENTER: {
846 pemr->rclBounds.left = x - (textWidth / 2) - 1;
847 pemr->rclBounds.right = x + (textWidth / 2) + 1;
848 break;
849 }
850 case TA_RIGHT: {
851 pemr->rclBounds.left = x - textWidth - 1;
852 pemr->rclBounds.right = x;
853 break;
854 }
855 default: { /* TA_LEFT */
856 pemr->rclBounds.left = x;
857 pemr->rclBounds.right = x + textWidth + 1;
858 }
859 }
860
861 switch (textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE)) {
862 case TA_BASELINE: {
863 TEXTMETRICW tm;
864 if (!GetTextMetricsW( dev->hdc, &tm ))
865 tm.tmDescent = 0;
866 /* Play safe here... it's better to have a bounding box */
867 /* that is too big than too small. */
868 pemr->rclBounds.top = y - textHeight - 1;
869 pemr->rclBounds.bottom = y + tm.tmDescent + 1;
870 break;
871 }
872 case TA_BOTTOM: {
873 pemr->rclBounds.top = y - textHeight - 1;
874 pemr->rclBounds.bottom = y;
875 break;
876 }
877 default: { /* TA_TOP */
878 pemr->rclBounds.top = y;
879 pemr->rclBounds.bottom = y + textHeight + 1;
880 }
881 }
882 EMFDRV_UpdateBBox( dev, &pemr->rclBounds );
883
884 no_bounds:
885 ret = EMFDRV_WriteRecord( dev, &pemr->emr );
886 HeapFree( GetProcessHeap(), 0, pemr );
887 return ret;
888 }