gdi32: Simplify computation of the DIB header size for internal BITMAPINFO structures.
[wine/multimedia.git] / dlls / gdi32 / enhmfdrv / graphics.c
blob71fd118958145660e6fe228d332cad84cf04c79d
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 <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "enhmfdrv/enhmetafiledrv.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
36 /**********************************************************************
37 * EMFDRV_MoveTo
39 BOOL EMFDRV_MoveTo(PHYSDEV dev, INT x, INT y)
41 EMRMOVETOEX emr;
43 emr.emr.iType = EMR_MOVETOEX;
44 emr.emr.nSize = sizeof(emr);
45 emr.ptl.x = x;
46 emr.ptl.y = y;
48 return EMFDRV_WriteRecord( dev, &emr.emr );
51 /***********************************************************************
52 * EMFDRV_LineTo
54 BOOL EMFDRV_LineTo( PHYSDEV dev, INT x, INT y )
56 POINT pt;
57 EMRLINETO emr;
58 RECTL bounds;
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( dev->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;
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(GetGraphicsMode(dev->hdc) == GM_COMPATIBLE) {
100 right--;
101 bottom--;
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;
112 emr.ptlEnd.x = xend;
113 emr.ptlEnd.y = yend;
116 /* Now calculate the BBox */
117 xCentre = (left + right + 1) / 2;
118 yCentre = (top + bottom + 1) / 2;
120 xstart -= xCentre;
121 ystart -= yCentre;
122 xend -= xCentre;
123 yend -= yCentre;
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 */
147 continue;
148 if(i * M_PI / 2 > angleEnd) /* if we're past end we're finished */
149 break;
151 /* the arc touches the rectangle at the start of quadrant i, so adjust
152 BBox to reflect this. */
154 switch(i % 4) {
155 case 0:
156 bounds.right = right;
157 break;
158 case 1:
159 bounds.top = top;
160 break;
161 case 2:
162 bounds.left = left;
163 break;
164 case 3:
165 bounds.bottom = bottom;
166 break;
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 ))
178 return FALSE;
179 EMFDRV_UpdateBBox( dev, &bounds );
180 return TRUE;
184 /***********************************************************************
185 * EMFDRV_Arc
187 BOOL EMFDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
188 INT xstart, INT ystart, INT xend, INT yend )
190 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
191 xend, yend, EMR_ARC );
194 /***********************************************************************
195 * EMFDRV_Pie
197 BOOL EMFDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
198 INT xstart, INT ystart, INT xend, INT yend )
200 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
201 xend, yend, EMR_PIE );
205 /***********************************************************************
206 * EMFDRV_Chord
208 BOOL 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 /***********************************************************************
216 * EMFDRV_Ellipse
218 BOOL EMFDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
220 EMRELLIPSE emr;
221 INT temp;
223 TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
225 if(left == right || top == bottom) return FALSE;
227 if(left > right) {temp = left; left = right; right = temp;}
228 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
230 if(GetGraphicsMode( dev->hdc ) == GM_COMPATIBLE) {
231 right--;
232 bottom--;
235 emr.emr.iType = EMR_ELLIPSE;
236 emr.emr.nSize = sizeof(emr);
237 emr.rclBox.left = left;
238 emr.rclBox.top = top;
239 emr.rclBox.right = right;
240 emr.rclBox.bottom = bottom;
242 EMFDRV_UpdateBBox( dev, &emr.rclBox );
243 return EMFDRV_WriteRecord( dev, &emr.emr );
246 /***********************************************************************
247 * EMFDRV_Rectangle
249 BOOL EMFDRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
251 EMRRECTANGLE emr;
252 INT temp;
254 TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
256 if(left == right || top == bottom) return FALSE;
258 if(left > right) {temp = left; left = right; right = temp;}
259 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
261 if(GetGraphicsMode( dev->hdc ) == GM_COMPATIBLE) {
262 right--;
263 bottom--;
266 emr.emr.iType = EMR_RECTANGLE;
267 emr.emr.nSize = sizeof(emr);
268 emr.rclBox.left = left;
269 emr.rclBox.top = top;
270 emr.rclBox.right = right;
271 emr.rclBox.bottom = bottom;
273 EMFDRV_UpdateBBox( dev, &emr.rclBox );
274 return EMFDRV_WriteRecord( dev, &emr.emr );
277 /***********************************************************************
278 * EMFDRV_RoundRect
280 BOOL EMFDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
281 INT bottom, INT ell_width, INT ell_height )
283 EMRROUNDRECT emr;
284 INT temp;
286 if(left == right || top == bottom) return FALSE;
288 if(left > right) {temp = left; left = right; right = temp;}
289 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
291 if(GetGraphicsMode( dev->hdc ) == GM_COMPATIBLE) {
292 right--;
293 bottom--;
296 emr.emr.iType = EMR_ROUNDRECT;
297 emr.emr.nSize = sizeof(emr);
298 emr.rclBox.left = left;
299 emr.rclBox.top = top;
300 emr.rclBox.right = right;
301 emr.rclBox.bottom = bottom;
302 emr.szlCorner.cx = ell_width;
303 emr.szlCorner.cy = ell_height;
305 EMFDRV_UpdateBBox( dev, &emr.rclBox );
306 return EMFDRV_WriteRecord( dev, &emr.emr );
309 /***********************************************************************
310 * EMFDRV_SetPixel
312 COLORREF EMFDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
314 EMRSETPIXELV emr;
316 emr.emr.iType = EMR_SETPIXELV;
317 emr.emr.nSize = sizeof(emr);
318 emr.ptlPixel.x = x;
319 emr.ptlPixel.y = y;
320 emr.crColor = color;
322 if (EMFDRV_WriteRecord( dev, &emr.emr )) {
323 RECTL bounds;
324 bounds.left = bounds.right = x;
325 bounds.top = bounds.bottom = y;
326 EMFDRV_UpdateBBox( dev, &bounds );
327 return color;
329 return -1;
332 /**********************************************************************
333 * EMFDRV_Polylinegon
335 * Helper for EMFDRV_Poly{line|gon}
337 static BOOL
338 EMFDRV_Polylinegon( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
340 EMRPOLYLINE *emr;
341 DWORD size;
342 INT i;
343 BOOL ret;
345 size = sizeof(EMRPOLYLINE) + sizeof(POINTL) * (count - 1);
347 emr = HeapAlloc( GetProcessHeap(), 0, size );
348 emr->emr.iType = iType;
349 emr->emr.nSize = size;
351 emr->rclBounds.left = emr->rclBounds.right = pt[0].x;
352 emr->rclBounds.top = emr->rclBounds.bottom = pt[0].y;
354 for(i = 1; i < count; i++) {
355 if(pt[i].x < emr->rclBounds.left)
356 emr->rclBounds.left = pt[i].x;
357 else if(pt[i].x > emr->rclBounds.right)
358 emr->rclBounds.right = pt[i].x;
359 if(pt[i].y < emr->rclBounds.top)
360 emr->rclBounds.top = pt[i].y;
361 else if(pt[i].y > emr->rclBounds.bottom)
362 emr->rclBounds.bottom = pt[i].y;
365 emr->cptl = count;
366 memcpy(emr->aptl, pt, count * sizeof(POINTL));
368 ret = EMFDRV_WriteRecord( dev, &emr->emr );
369 if(ret)
370 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
371 HeapFree( GetProcessHeap(), 0, emr );
372 return ret;
376 /**********************************************************************
377 * EMFDRV_Polylinegon16
379 * Helper for EMFDRV_Poly{line|gon}
381 * This is not a legacy function!
382 * We are using SHORT integers to save space.
384 static BOOL
385 EMFDRV_Polylinegon16( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
387 EMRPOLYLINE16 *emr;
388 DWORD size;
389 INT i;
390 BOOL ret;
392 /* check whether all points fit in the SHORT int POINT structure */
393 for(i = 0; i < count; i++) {
394 if( ((pt[i].x+0x8000) & ~0xffff ) ||
395 ((pt[i].y+0x8000) & ~0xffff ) )
396 return FALSE;
399 size = sizeof(EMRPOLYLINE16) + sizeof(POINTS) * (count - 1);
401 emr = HeapAlloc( GetProcessHeap(), 0, size );
402 emr->emr.iType = iType;
403 emr->emr.nSize = size;
405 emr->rclBounds.left = emr->rclBounds.right = pt[0].x;
406 emr->rclBounds.top = emr->rclBounds.bottom = pt[0].y;
408 for(i = 1; i < count; i++) {
409 if(pt[i].x < emr->rclBounds.left)
410 emr->rclBounds.left = pt[i].x;
411 else if(pt[i].x > emr->rclBounds.right)
412 emr->rclBounds.right = pt[i].x;
413 if(pt[i].y < emr->rclBounds.top)
414 emr->rclBounds.top = pt[i].y;
415 else if(pt[i].y > emr->rclBounds.bottom)
416 emr->rclBounds.bottom = pt[i].y;
419 emr->cpts = count;
420 for(i = 0; i < count; i++ ) {
421 emr->apts[i].x = pt[i].x;
422 emr->apts[i].y = pt[i].y;
425 ret = EMFDRV_WriteRecord( dev, &emr->emr );
426 if(ret)
427 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
428 HeapFree( GetProcessHeap(), 0, emr );
429 return ret;
433 /**********************************************************************
434 * EMFDRV_Polyline
436 BOOL EMFDRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
438 if( EMFDRV_Polylinegon16( dev, pt, count, EMR_POLYLINE16 ) )
439 return TRUE;
440 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINE );
443 /**********************************************************************
444 * EMFDRV_Polygon
446 BOOL EMFDRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
448 if(count < 2) return FALSE;
449 if( EMFDRV_Polylinegon16( dev, pt, count, EMR_POLYGON16 ) )
450 return TRUE;
451 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYGON );
455 /**********************************************************************
456 * EMFDRV_PolyPolylinegon
458 * Helper for EMFDRV_PolyPoly{line|gon}
460 static BOOL
461 EMFDRV_PolyPolylinegon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys,
462 DWORD iType)
464 EMRPOLYPOLYLINE *emr;
465 DWORD cptl = 0, poly, size;
466 INT point;
467 RECTL bounds;
468 const POINT *pts;
469 BOOL ret;
471 bounds.left = bounds.right = pt[0].x;
472 bounds.top = bounds.bottom = pt[0].y;
474 pts = pt;
475 for(poly = 0; poly < polys; poly++) {
476 cptl += counts[poly];
477 for(point = 0; point < counts[poly]; point++) {
478 if(bounds.left > pts->x) bounds.left = pts->x;
479 else if(bounds.right < pts->x) bounds.right = pts->x;
480 if(bounds.top > pts->y) bounds.top = pts->y;
481 else if(bounds.bottom < pts->y) bounds.bottom = pts->y;
482 pts++;
486 size = sizeof(EMRPOLYPOLYLINE) + (polys - 1) * sizeof(DWORD) +
487 (cptl - 1) * sizeof(POINTL);
489 emr = HeapAlloc( GetProcessHeap(), 0, size );
491 emr->emr.iType = iType;
492 emr->emr.nSize = size;
493 emr->rclBounds = bounds;
494 emr->nPolys = polys;
495 emr->cptl = cptl;
496 memcpy(emr->aPolyCounts, counts, polys * sizeof(DWORD));
497 memcpy(emr->aPolyCounts + polys, pt, cptl * sizeof(POINTL));
498 ret = EMFDRV_WriteRecord( dev, &emr->emr );
499 if(ret)
500 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
501 HeapFree( GetProcessHeap(), 0, emr );
502 return ret;
505 /**********************************************************************
506 * EMFDRV_PolyPolyline
508 BOOL EMFDRV_PolyPolyline(PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polys)
510 return EMFDRV_PolyPolylinegon( dev, pt, (const INT *)counts, polys,
511 EMR_POLYPOLYLINE );
514 /**********************************************************************
515 * EMFDRV_PolyPolygon
517 BOOL EMFDRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys )
519 return EMFDRV_PolyPolylinegon( dev, pt, counts, polys, EMR_POLYPOLYGON );
523 /**********************************************************************
524 * EMFDRV_ExtFloodFill
526 BOOL EMFDRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
528 EMREXTFLOODFILL emr;
530 emr.emr.iType = EMR_EXTFLOODFILL;
531 emr.emr.nSize = sizeof(emr);
532 emr.ptlStart.x = x;
533 emr.ptlStart.y = y;
534 emr.crColor = color;
535 emr.iMode = fillType;
537 return EMFDRV_WriteRecord( dev, &emr.emr );
541 /*********************************************************************
542 * EMFDRV_FillRgn
544 BOOL EMFDRV_FillRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush )
546 EMRFILLRGN *emr;
547 DWORD size, rgnsize, index;
548 BOOL ret;
550 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
551 if(!index) return FALSE;
553 rgnsize = GetRegionData( hrgn, 0, NULL );
554 size = rgnsize + offsetof(EMRFILLRGN,RgnData);
555 emr = HeapAlloc( GetProcessHeap(), 0, size );
557 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
559 emr->emr.iType = EMR_FILLRGN;
560 emr->emr.nSize = size;
561 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
562 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
563 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
564 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
565 emr->cbRgnData = rgnsize;
566 emr->ihBrush = index;
568 ret = EMFDRV_WriteRecord( dev, &emr->emr );
569 if(ret)
570 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
571 HeapFree( GetProcessHeap(), 0, emr );
572 return ret;
574 /*********************************************************************
575 * EMFDRV_FrameRgn
577 BOOL EMFDRV_FrameRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush, INT width, INT height )
579 EMRFRAMERGN *emr;
580 DWORD size, rgnsize, index;
581 BOOL ret;
583 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
584 if(!index) return FALSE;
586 rgnsize = GetRegionData( hrgn, 0, NULL );
587 size = rgnsize + offsetof(EMRFRAMERGN,RgnData);
588 emr = HeapAlloc( GetProcessHeap(), 0, size );
590 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
592 emr->emr.iType = EMR_FRAMERGN;
593 emr->emr.nSize = size;
594 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
595 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
596 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
597 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
598 emr->cbRgnData = rgnsize;
599 emr->ihBrush = index;
600 emr->szlStroke.cx = width;
601 emr->szlStroke.cy = height;
603 ret = EMFDRV_WriteRecord( dev, &emr->emr );
604 if(ret)
605 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
606 HeapFree( GetProcessHeap(), 0, emr );
607 return ret;
610 /*********************************************************************
611 * EMFDRV_PaintInvertRgn
613 * Helper for EMFDRV_{Paint|Invert}Rgn
615 static BOOL EMFDRV_PaintInvertRgn( PHYSDEV dev, HRGN hrgn, DWORD iType )
617 EMRINVERTRGN *emr;
618 DWORD size, rgnsize;
619 BOOL ret;
622 rgnsize = GetRegionData( hrgn, 0, NULL );
623 size = rgnsize + offsetof(EMRINVERTRGN,RgnData);
624 emr = HeapAlloc( GetProcessHeap(), 0, size );
626 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
628 emr->emr.iType = iType;
629 emr->emr.nSize = size;
630 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
631 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
632 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
633 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
634 emr->cbRgnData = rgnsize;
636 ret = EMFDRV_WriteRecord( dev, &emr->emr );
637 if(ret)
638 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
639 HeapFree( GetProcessHeap(), 0, emr );
640 return ret;
643 /**********************************************************************
644 * EMFDRV_PaintRgn
646 BOOL EMFDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
648 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_PAINTRGN );
651 /**********************************************************************
652 * EMFDRV_InvertRgn
654 BOOL EMFDRV_InvertRgn( PHYSDEV dev, HRGN hrgn )
656 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_INVERTRGN );
659 /**********************************************************************
660 * EMFDRV_ExtTextOut
662 BOOL EMFDRV_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags, const RECT *lprect,
663 LPCWSTR str, UINT count, const INT *lpDx )
665 EMREXTTEXTOUTW *pemr;
666 DWORD nSize;
667 BOOL ret;
668 int textHeight = 0;
669 int textWidth = 0;
670 const UINT textAlign = GetTextAlign( dev->hdc );
671 const INT graphicsMode = GetGraphicsMode( dev->hdc );
672 FLOAT exScale, eyScale;
674 nSize = sizeof(*pemr) + ((count+1) & ~1) * sizeof(WCHAR) + count * sizeof(INT);
676 TRACE("%s %s count %d nSize = %d\n", debugstr_wn(str, count),
677 wine_dbgstr_rect(lprect), count, nSize);
678 pemr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nSize);
680 if (graphicsMode == GM_COMPATIBLE)
682 const INT horzSize = GetDeviceCaps( dev->hdc, HORZSIZE );
683 const INT horzRes = GetDeviceCaps( dev->hdc, HORZRES );
684 const INT vertSize = GetDeviceCaps( dev->hdc, VERTSIZE );
685 const INT vertRes = GetDeviceCaps( dev->hdc, VERTRES );
686 SIZE wndext, vportext;
688 GetViewportExtEx( dev->hdc, &vportext );
689 GetWindowExtEx( dev->hdc, &wndext );
690 exScale = 100.0 * ((FLOAT)horzSize / (FLOAT)horzRes) /
691 ((FLOAT)wndext.cx / (FLOAT)vportext.cx);
692 eyScale = 100.0 * ((FLOAT)vertSize / (FLOAT)vertRes) /
693 ((FLOAT)wndext.cy / (FLOAT)vportext.cy);
695 else
697 exScale = 0.0;
698 eyScale = 0.0;
701 pemr->emr.iType = EMR_EXTTEXTOUTW;
702 pemr->emr.nSize = nSize;
703 pemr->iGraphicsMode = graphicsMode;
704 pemr->exScale = exScale;
705 pemr->eyScale = eyScale;
706 pemr->emrtext.ptlReference.x = x;
707 pemr->emrtext.ptlReference.y = y;
708 pemr->emrtext.nChars = count;
709 pemr->emrtext.offString = sizeof(*pemr);
710 memcpy((char*)pemr + pemr->emrtext.offString, str, count * sizeof(WCHAR));
711 pemr->emrtext.fOptions = flags;
712 if(!lprect) {
713 pemr->emrtext.rcl.left = pemr->emrtext.rcl.top = 0;
714 pemr->emrtext.rcl.right = pemr->emrtext.rcl.bottom = -1;
715 } else {
716 pemr->emrtext.rcl.left = lprect->left;
717 pemr->emrtext.rcl.top = lprect->top;
718 pemr->emrtext.rcl.right = lprect->right;
719 pemr->emrtext.rcl.bottom = lprect->bottom;
722 pemr->emrtext.offDx = pemr->emrtext.offString + ((count+1) & ~1) * sizeof(WCHAR);
723 if(lpDx) {
724 UINT i;
725 SIZE strSize;
726 memcpy((char*)pemr + pemr->emrtext.offDx, lpDx, count * sizeof(INT));
727 for (i = 0; i < count; i++) {
728 textWidth += lpDx[i];
730 if (GetTextExtentPoint32W( dev->hdc, str, count, &strSize ))
731 textHeight = strSize.cy;
733 else {
734 UINT i;
735 INT *dx = (INT *)((char*)pemr + pemr->emrtext.offDx);
736 SIZE charSize;
737 for (i = 0; i < count; i++) {
738 if (GetTextExtentPoint32W( dev->hdc, str + i, 1, &charSize )) {
739 dx[i] = charSize.cx;
740 textWidth += charSize.cx;
741 textHeight = max(textHeight, charSize.cy);
746 if (!lprect)
748 pemr->rclBounds.left = pemr->rclBounds.top = 0;
749 pemr->rclBounds.right = pemr->rclBounds.bottom = -1;
750 goto no_bounds;
753 switch (textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER)) {
754 case TA_CENTER: {
755 pemr->rclBounds.left = x - (textWidth / 2) - 1;
756 pemr->rclBounds.right = x + (textWidth / 2) + 1;
757 break;
759 case TA_RIGHT: {
760 pemr->rclBounds.left = x - textWidth - 1;
761 pemr->rclBounds.right = x;
762 break;
764 default: { /* TA_LEFT */
765 pemr->rclBounds.left = x;
766 pemr->rclBounds.right = x + textWidth + 1;
770 switch (textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE)) {
771 case TA_BASELINE: {
772 TEXTMETRICW tm;
773 if (!GetTextMetricsW( dev->hdc, &tm ))
774 tm.tmDescent = 0;
775 /* Play safe here... it's better to have a bounding box */
776 /* that is too big than too small. */
777 pemr->rclBounds.top = y - textHeight - 1;
778 pemr->rclBounds.bottom = y + tm.tmDescent + 1;
779 break;
781 case TA_BOTTOM: {
782 pemr->rclBounds.top = y - textHeight - 1;
783 pemr->rclBounds.bottom = y;
784 break;
786 default: { /* TA_TOP */
787 pemr->rclBounds.top = y;
788 pemr->rclBounds.bottom = y + textHeight + 1;
791 EMFDRV_UpdateBBox( dev, &pemr->rclBounds );
793 no_bounds:
794 ret = EMFDRV_WriteRecord( dev, &pemr->emr );
795 HeapFree( GetProcessHeap(), 0, pemr );
796 return ret;