d3d11: Remove null dxgi object checks.
[wine.git] / dlls / gdi32 / enhmetafile.c
blob336f2d389fd3ab9b402978e7ff0eef76c5f82197
1 /*
2 * Enhanced metafile functions
3 * Copyright 1998 Douglas Ridgway
4 * 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
20 * NOTES:
22 * The enhanced format consists of the following elements:
24 * A header
25 * A table of handles to GDI objects
26 * An array of metafile records
27 * A private palette
30 * The standard format consists of a header and an array of metafile records.
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <assert.h>
38 #include <math.h>
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wingdi.h"
43 #include "winnls.h"
44 #include "winerror.h"
45 #include "gdi_private.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
52 static CRITICAL_SECTION enhmetafile_cs;
53 static CRITICAL_SECTION_DEBUG critsect_debug =
55 0, 0, &enhmetafile_cs,
56 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
57 0, 0, { (DWORD_PTR)(__FILE__ ": enhmetafile_cs") }
59 static CRITICAL_SECTION enhmetafile_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
61 typedef struct
63 ENHMETAHEADER *emh;
64 BOOL on_disk; /* true if metafile is on disk */
65 } ENHMETAFILEOBJ;
67 static const struct emr_name {
68 DWORD type;
69 const char *name;
70 } emr_names[] = {
71 #define X(p) {p, #p}
72 X(EMR_HEADER),
73 X(EMR_POLYBEZIER),
74 X(EMR_POLYGON),
75 X(EMR_POLYLINE),
76 X(EMR_POLYBEZIERTO),
77 X(EMR_POLYLINETO),
78 X(EMR_POLYPOLYLINE),
79 X(EMR_POLYPOLYGON),
80 X(EMR_SETWINDOWEXTEX),
81 X(EMR_SETWINDOWORGEX),
82 X(EMR_SETVIEWPORTEXTEX),
83 X(EMR_SETVIEWPORTORGEX),
84 X(EMR_SETBRUSHORGEX),
85 X(EMR_EOF),
86 X(EMR_SETPIXELV),
87 X(EMR_SETMAPPERFLAGS),
88 X(EMR_SETMAPMODE),
89 X(EMR_SETBKMODE),
90 X(EMR_SETPOLYFILLMODE),
91 X(EMR_SETROP2),
92 X(EMR_SETSTRETCHBLTMODE),
93 X(EMR_SETTEXTALIGN),
94 X(EMR_SETCOLORADJUSTMENT),
95 X(EMR_SETTEXTCOLOR),
96 X(EMR_SETBKCOLOR),
97 X(EMR_OFFSETCLIPRGN),
98 X(EMR_MOVETOEX),
99 X(EMR_SETMETARGN),
100 X(EMR_EXCLUDECLIPRECT),
101 X(EMR_INTERSECTCLIPRECT),
102 X(EMR_SCALEVIEWPORTEXTEX),
103 X(EMR_SCALEWINDOWEXTEX),
104 X(EMR_SAVEDC),
105 X(EMR_RESTOREDC),
106 X(EMR_SETWORLDTRANSFORM),
107 X(EMR_MODIFYWORLDTRANSFORM),
108 X(EMR_SELECTOBJECT),
109 X(EMR_CREATEPEN),
110 X(EMR_CREATEBRUSHINDIRECT),
111 X(EMR_DELETEOBJECT),
112 X(EMR_ANGLEARC),
113 X(EMR_ELLIPSE),
114 X(EMR_RECTANGLE),
115 X(EMR_ROUNDRECT),
116 X(EMR_ARC),
117 X(EMR_CHORD),
118 X(EMR_PIE),
119 X(EMR_SELECTPALETTE),
120 X(EMR_CREATEPALETTE),
121 X(EMR_SETPALETTEENTRIES),
122 X(EMR_RESIZEPALETTE),
123 X(EMR_REALIZEPALETTE),
124 X(EMR_EXTFLOODFILL),
125 X(EMR_LINETO),
126 X(EMR_ARCTO),
127 X(EMR_POLYDRAW),
128 X(EMR_SETARCDIRECTION),
129 X(EMR_SETMITERLIMIT),
130 X(EMR_BEGINPATH),
131 X(EMR_ENDPATH),
132 X(EMR_CLOSEFIGURE),
133 X(EMR_FILLPATH),
134 X(EMR_STROKEANDFILLPATH),
135 X(EMR_STROKEPATH),
136 X(EMR_FLATTENPATH),
137 X(EMR_WIDENPATH),
138 X(EMR_SELECTCLIPPATH),
139 X(EMR_ABORTPATH),
140 X(EMR_GDICOMMENT),
141 X(EMR_FILLRGN),
142 X(EMR_FRAMERGN),
143 X(EMR_INVERTRGN),
144 X(EMR_PAINTRGN),
145 X(EMR_EXTSELECTCLIPRGN),
146 X(EMR_BITBLT),
147 X(EMR_STRETCHBLT),
148 X(EMR_MASKBLT),
149 X(EMR_PLGBLT),
150 X(EMR_SETDIBITSTODEVICE),
151 X(EMR_STRETCHDIBITS),
152 X(EMR_EXTCREATEFONTINDIRECTW),
153 X(EMR_EXTTEXTOUTA),
154 X(EMR_EXTTEXTOUTW),
155 X(EMR_POLYBEZIER16),
156 X(EMR_POLYGON16),
157 X(EMR_POLYLINE16),
158 X(EMR_POLYBEZIERTO16),
159 X(EMR_POLYLINETO16),
160 X(EMR_POLYPOLYLINE16),
161 X(EMR_POLYPOLYGON16),
162 X(EMR_POLYDRAW16),
163 X(EMR_CREATEMONOBRUSH),
164 X(EMR_CREATEDIBPATTERNBRUSHPT),
165 X(EMR_EXTCREATEPEN),
166 X(EMR_POLYTEXTOUTA),
167 X(EMR_POLYTEXTOUTW),
168 X(EMR_SETICMMODE),
169 X(EMR_CREATECOLORSPACE),
170 X(EMR_SETCOLORSPACE),
171 X(EMR_DELETECOLORSPACE),
172 X(EMR_GLSRECORD),
173 X(EMR_GLSBOUNDEDRECORD),
174 X(EMR_PIXELFORMAT),
175 X(EMR_DRAWESCAPE),
176 X(EMR_EXTESCAPE),
177 X(EMR_STARTDOC),
178 X(EMR_SMALLTEXTOUT),
179 X(EMR_FORCEUFIMAPPING),
180 X(EMR_NAMEDESCAPE),
181 X(EMR_COLORCORRECTPALETTE),
182 X(EMR_SETICMPROFILEA),
183 X(EMR_SETICMPROFILEW),
184 X(EMR_ALPHABLEND),
185 X(EMR_SETLAYOUT),
186 X(EMR_TRANSPARENTBLT),
187 X(EMR_RESERVED_117),
188 X(EMR_GRADIENTFILL),
189 X(EMR_SETLINKEDUFI),
190 X(EMR_SETTEXTJUSTIFICATION),
191 X(EMR_COLORMATCHTOTARGETW),
192 X(EMR_CREATECOLORSPACEW)
193 #undef X
196 /****************************************************************************
197 * get_emr_name
199 static const char *get_emr_name(DWORD type)
201 unsigned int i;
202 for(i = 0; i < ARRAY_SIZE(emr_names); i++)
203 if(type == emr_names[i].type) return emr_names[i].name;
204 TRACE("Unknown record type %ld\n", type);
205 return NULL;
208 /***********************************************************************
209 * is_dib_monochrome
211 * Returns whether a DIB can be converted to a monochrome DDB.
213 * A DIB can be converted if its color table contains only black and
214 * white. Black must be the first color in the color table.
216 * Note : If the first color in the color table is white followed by
217 * black, we can't convert it to a monochrome DDB with
218 * SetDIBits, because black and white would be inverted.
220 static inline BOOL is_dib_monochrome( const BITMAPINFO* info )
222 if (info->bmiHeader.biBitCount != 1) return FALSE;
224 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
226 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO *) info)->bmciColors;
228 /* Check if the first color is black */
229 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
231 rgb++;
232 /* Check if the second color is white */
233 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
234 && (rgb->rgbtBlue == 0xff));
236 else return FALSE;
238 else /* assume BITMAPINFOHEADER */
240 const RGBQUAD *rgb = info->bmiColors;
242 /* Check if the first color is black */
243 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
244 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
246 rgb++;
248 /* Check if the second color is white */
249 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
250 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
252 else return FALSE;
256 /****************************************************************************
257 * EMF_Create_HENHMETAFILE
259 HENHMETAFILE EMF_Create_HENHMETAFILE(ENHMETAHEADER *emh, DWORD filesize, BOOL on_disk )
261 HENHMETAFILE hmf;
262 ENHMETAFILEOBJ *metaObj;
264 if (filesize < sizeof(*emh))
266 WARN("File too small for emf header\n");
267 return 0;
269 if (emh->iType != EMR_HEADER)
271 SetLastError(ERROR_INVALID_DATA);
272 return 0;
274 if (emh->dSignature != ENHMETA_SIGNATURE ||
275 (emh->nBytes & 3)) /* refuse to load unaligned EMF as Windows does */
277 WARN("Invalid emf header type 0x%08lx sig 0x%08lx.\n",
278 emh->iType, emh->dSignature);
279 return 0;
281 if (filesize < emh->nBytes)
283 WARN("File truncated (got %lu bytes, header says %lu)\n", emh->nBytes, filesize);
284 return 0;
287 if (!(metaObj = HeapAlloc( GetProcessHeap(), 0, sizeof(*metaObj) ))) return 0;
289 metaObj->emh = emh;
290 metaObj->on_disk = on_disk;
292 if ((hmf = NtGdiCreateClientObj( NTGDI_OBJ_ENHMETAFILE )))
293 set_gdi_client_ptr( hmf, metaObj );
294 else
295 HeapFree( GetProcessHeap(), 0, metaObj );
296 return hmf;
299 /****************************************************************************
300 * EMF_Delete_HENHMETAFILE
302 static BOOL EMF_Delete_HENHMETAFILE( HENHMETAFILE hmf )
304 ENHMETAFILEOBJ *metafile;
306 EnterCriticalSection( &enhmetafile_cs );
307 if (!(metafile = get_gdi_client_ptr( hmf, NTGDI_OBJ_ENHMETAFILE )) ||
308 !NtGdiDeleteClientObj( hmf ))
310 LeaveCriticalSection( &enhmetafile_cs );
311 SetLastError( ERROR_INVALID_HANDLE );
312 return FALSE;
315 if (metafile->on_disk)
316 UnmapViewOfFile( metafile->emh );
317 else
318 HeapFree( GetProcessHeap(), 0, metafile->emh );
319 HeapFree( GetProcessHeap(), 0, metafile );
320 LeaveCriticalSection( &enhmetafile_cs );
321 return TRUE;
324 /******************************************************************
325 * EMF_GetEnhMetaHeader
327 * Returns ptr to ENHMETAHEADER associated with HENHMETAFILE
329 static ENHMETAHEADER *EMF_GetEnhMetaHeader( HENHMETAFILE hmf )
331 ENHMETAHEADER *ret = NULL;
332 ENHMETAFILEOBJ *metafile;
334 EnterCriticalSection( &enhmetafile_cs );
335 if ((metafile = get_gdi_client_ptr( hmf, NTGDI_OBJ_ENHMETAFILE )))
337 TRACE( "hmf %p -> enhmetafile %p\n", hmf, metafile );
338 ret = metafile->emh;
340 else SetLastError( ERROR_INVALID_HANDLE );
341 LeaveCriticalSection( &enhmetafile_cs );
342 return ret;
345 /*****************************************************************************
346 * EMF_GetEnhMetaFile
349 static HENHMETAFILE EMF_GetEnhMetaFile( HANDLE hFile )
351 ENHMETAHEADER *emh;
352 HANDLE hMapping;
353 HENHMETAFILE hemf;
354 DWORD filesize;
356 filesize = GetFileSize( hFile, NULL );
358 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
359 emh = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, filesize );
360 CloseHandle( hMapping );
362 if (!emh) return 0;
364 hemf = EMF_Create_HENHMETAFILE( emh, filesize, TRUE );
365 if (!hemf)
366 UnmapViewOfFile( emh );
367 return hemf;
371 /*****************************************************************************
372 * GetEnhMetaFileA (GDI32.@)
376 HENHMETAFILE WINAPI GetEnhMetaFileA(
377 LPCSTR lpszMetaFile /* [in] filename of enhanced metafile */
380 HENHMETAFILE hmf;
381 HANDLE hFile;
383 hFile = CreateFileA(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
384 OPEN_EXISTING, 0, 0);
385 if (hFile == INVALID_HANDLE_VALUE) {
386 WARN("could not open %s\n", lpszMetaFile);
387 return 0;
389 hmf = EMF_GetEnhMetaFile( hFile );
390 CloseHandle( hFile );
391 return hmf;
394 /*****************************************************************************
395 * GetEnhMetaFileW (GDI32.@)
397 HENHMETAFILE WINAPI GetEnhMetaFileW(
398 LPCWSTR lpszMetaFile) /* [in] filename of enhanced metafile */
400 HENHMETAFILE hmf;
401 HANDLE hFile;
403 hFile = CreateFileW(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
404 OPEN_EXISTING, 0, 0);
405 if (hFile == INVALID_HANDLE_VALUE) {
406 WARN("could not open %s\n", debugstr_w(lpszMetaFile));
407 return 0;
409 hmf = EMF_GetEnhMetaFile( hFile );
410 CloseHandle( hFile );
411 return hmf;
414 /*****************************************************************************
415 * GetEnhMetaFileHeader (GDI32.@)
417 * Retrieves the record containing the header for the specified
418 * enhanced-format metafile.
420 * RETURNS
421 * If buf is NULL, returns the size of buffer required.
422 * Otherwise, copy up to bufsize bytes of enhanced metafile header into
423 * buf.
425 UINT WINAPI GetEnhMetaFileHeader(
426 HENHMETAFILE hmf, /* [in] enhanced metafile */
427 UINT bufsize, /* [in] size of buffer */
428 LPENHMETAHEADER buf /* [out] buffer */
431 LPENHMETAHEADER emh;
432 UINT size;
434 emh = EMF_GetEnhMetaHeader(hmf);
435 if(!emh) return FALSE;
436 size = emh->nSize;
437 if (!buf) return size;
438 size = min(size, bufsize);
439 memmove(buf, emh, size);
440 return size;
444 /*****************************************************************************
445 * GetEnhMetaFileDescriptionA (GDI32.@)
447 * See GetEnhMetaFileDescriptionW.
449 UINT WINAPI GetEnhMetaFileDescriptionA(
450 HENHMETAFILE hmf, /* [in] enhanced metafile */
451 UINT size, /* [in] size of buf */
452 LPSTR buf /* [out] buffer to receive description */
455 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
456 DWORD len;
457 WCHAR *descrW;
459 if(!emh) return FALSE;
460 if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
461 descrW = (WCHAR *) ((char *) emh + emh->offDescription);
462 len = WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, NULL, 0, NULL, NULL );
464 if (!buf || !size ) return len;
466 len = min( size, len );
467 WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, buf, len, NULL, NULL );
468 return len;
471 /*****************************************************************************
472 * GetEnhMetaFileDescriptionW (GDI32.@)
474 * Copies the description string of an enhanced metafile into a buffer
475 * _buf_.
477 * RETURNS
478 * If _buf_ is NULL, returns size of _buf_ required. Otherwise, returns
479 * number of characters copied.
481 UINT WINAPI GetEnhMetaFileDescriptionW(
482 HENHMETAFILE hmf, /* [in] enhanced metafile */
483 UINT size, /* [in] size of buf */
484 LPWSTR buf /* [out] buffer to receive description */
487 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
489 if(!emh) return FALSE;
490 if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
491 if (!buf || !size ) return emh->nDescription;
493 memmove(buf, (char *) emh + emh->offDescription, min(size,emh->nDescription)*sizeof(WCHAR));
494 return min(size, emh->nDescription);
497 /****************************************************************************
498 * SetEnhMetaFileBits (GDI32.@)
500 * Creates an enhanced metafile by copying _bufsize_ bytes from _buf_.
502 HENHMETAFILE WINAPI SetEnhMetaFileBits(UINT bufsize, const BYTE *buf)
504 ENHMETAHEADER *emh = HeapAlloc( GetProcessHeap(), 0, bufsize );
505 HENHMETAFILE hmf;
507 if (!emh) return 0;
508 memcpy(emh, buf, bufsize);
509 hmf = EMF_Create_HENHMETAFILE( emh, bufsize, FALSE );
510 if (!hmf)
511 HeapFree( GetProcessHeap(), 0, emh );
512 return hmf;
515 /*****************************************************************************
516 * GetEnhMetaFileBits (GDI32.@)
519 UINT WINAPI GetEnhMetaFileBits(
520 HENHMETAFILE hmf,
521 UINT bufsize,
522 LPBYTE buf
525 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader( hmf );
526 UINT size;
528 if(!emh) return 0;
530 size = emh->nBytes;
531 if( buf == NULL ) return size;
533 size = min( size, bufsize );
534 memmove(buf, emh, size);
535 return size;
538 typedef struct EMF_dc_state
540 INT mode;
541 XFORM world_transform;
542 INT wndOrgX;
543 INT wndOrgY;
544 INT wndExtX;
545 INT wndExtY;
546 INT vportOrgX;
547 INT vportOrgY;
548 INT vportExtX;
549 INT vportExtY;
550 struct EMF_dc_state *next;
551 } EMF_dc_state;
553 typedef struct enum_emh_data
555 XFORM init_transform;
556 EMF_dc_state state;
557 INT save_level;
558 EMF_dc_state *saved_state;
559 } enum_emh_data;
561 #define ENUM_GET_PRIVATE_DATA(ht) \
562 ((enum_emh_data*)(((unsigned char*)(ht))-sizeof (enum_emh_data)))
564 #define WIDTH(rect) ( (rect).right - (rect).left )
565 #define HEIGHT(rect) ( (rect).bottom - (rect).top )
567 #define IS_WIN9X() (GetVersion()&0x80000000)
569 static void EMF_Update_MF_Xform(HDC hdc, const enum_emh_data *info)
571 XFORM mapping_mode_trans, final_trans;
572 double scaleX, scaleY;
574 scaleX = (double)info->state.vportExtX / (double)info->state.wndExtX;
575 scaleY = (double)info->state.vportExtY / (double)info->state.wndExtY;
576 mapping_mode_trans.eM11 = scaleX;
577 mapping_mode_trans.eM12 = 0.0;
578 mapping_mode_trans.eM21 = 0.0;
579 mapping_mode_trans.eM22 = scaleY;
580 mapping_mode_trans.eDx = (double)info->state.vportOrgX - scaleX * (double)info->state.wndOrgX;
581 mapping_mode_trans.eDy = (double)info->state.vportOrgY - scaleY * (double)info->state.wndOrgY;
583 CombineTransform(&final_trans, &info->state.world_transform, &mapping_mode_trans);
584 CombineTransform(&final_trans, &final_trans, &info->init_transform);
586 if (!SetWorldTransform(hdc, &final_trans))
588 ERR("World transform failed!\n");
592 static void EMF_RestoreDC( enum_emh_data *info, INT level )
594 if (abs(level) > info->save_level || level == 0) return;
596 if (level < 0) level = info->save_level + level + 1;
598 while (info->save_level >= level)
600 EMF_dc_state *state = info->saved_state;
601 info->saved_state = state->next;
602 state->next = NULL;
603 if (--info->save_level < level)
604 info->state = *state;
605 HeapFree( GetProcessHeap(), 0, state );
609 static void EMF_SaveDC( enum_emh_data *info )
611 EMF_dc_state *state = HeapAlloc( GetProcessHeap(), 0, sizeof(*state));
612 if (state)
614 *state = info->state;
615 state->next = info->saved_state;
616 info->saved_state = state;
617 info->save_level++;
618 TRACE("save_level %d\n", info->save_level);
622 static void EMF_SetMapMode(HDC hdc, enum_emh_data *info)
624 INT horzSize = GetDeviceCaps( hdc, HORZSIZE );
625 INT vertSize = GetDeviceCaps( hdc, VERTSIZE );
626 INT horzRes = GetDeviceCaps( hdc, HORZRES );
627 INT vertRes = GetDeviceCaps( hdc, VERTRES );
629 TRACE("%d\n", info->state.mode);
631 switch(info->state.mode)
633 case MM_TEXT:
634 info->state.wndExtX = 1;
635 info->state.wndExtY = 1;
636 info->state.vportExtX = 1;
637 info->state.vportExtY = 1;
638 break;
639 case MM_LOMETRIC:
640 case MM_ISOTROPIC:
641 info->state.wndExtX = horzSize * 10;
642 info->state.wndExtY = vertSize * 10;
643 info->state.vportExtX = horzRes;
644 info->state.vportExtY = -vertRes;
645 break;
646 case MM_HIMETRIC:
647 info->state.wndExtX = horzSize * 100;
648 info->state.wndExtY = vertSize * 100;
649 info->state.vportExtX = horzRes;
650 info->state.vportExtY = -vertRes;
651 break;
652 case MM_LOENGLISH:
653 info->state.wndExtX = MulDiv(1000, horzSize, 254);
654 info->state.wndExtY = MulDiv(1000, vertSize, 254);
655 info->state.vportExtX = horzRes;
656 info->state.vportExtY = -vertRes;
657 break;
658 case MM_HIENGLISH:
659 info->state.wndExtX = MulDiv(10000, horzSize, 254);
660 info->state.wndExtY = MulDiv(10000, vertSize, 254);
661 info->state.vportExtX = horzRes;
662 info->state.vportExtY = -vertRes;
663 break;
664 case MM_TWIPS:
665 info->state.wndExtX = MulDiv(14400, horzSize, 254);
666 info->state.wndExtY = MulDiv(14400, vertSize, 254);
667 info->state.vportExtX = horzRes;
668 info->state.vportExtY = -vertRes;
669 break;
670 case MM_ANISOTROPIC:
671 break;
672 default:
673 return;
677 /***********************************************************************
678 * EMF_FixIsotropic
680 * Fix viewport extensions for isotropic mode.
683 static void EMF_FixIsotropic(HDC hdc, enum_emh_data *info)
685 double xdim = fabs((double)info->state.vportExtX * GetDeviceCaps( hdc, HORZSIZE ) /
686 (GetDeviceCaps( hdc, HORZRES ) * info->state.wndExtX));
687 double ydim = fabs((double)info->state.vportExtY * GetDeviceCaps( hdc, VERTSIZE ) /
688 (GetDeviceCaps( hdc, VERTRES ) * info->state.wndExtY));
690 if (xdim > ydim)
692 INT mincx = (info->state.vportExtX >= 0) ? 1 : -1;
693 info->state.vportExtX = floor(info->state.vportExtX * ydim / xdim + 0.5);
694 if (!info->state.vportExtX) info->state.vportExtX = mincx;
696 else
698 INT mincy = (info->state.vportExtY >= 0) ? 1 : -1;
699 info->state.vportExtY = floor(info->state.vportExtY * xdim / ydim + 0.5);
700 if (!info->state.vportExtY) info->state.vportExtY = mincy;
704 /*****************************************************************************
705 * emr_produces_output
707 * Returns TRUE if the record type writes something to the dc. Used by
708 * PlayEnhMetaFileRecord to determine whether it needs to update the
709 * dc's xform when in win9x mode.
711 * FIXME: need to test which records should be here.
713 static BOOL emr_produces_output(int type)
715 switch(type) {
716 case EMR_POLYBEZIER:
717 case EMR_POLYGON:
718 case EMR_POLYLINE:
719 case EMR_POLYBEZIERTO:
720 case EMR_POLYLINETO:
721 case EMR_POLYPOLYLINE:
722 case EMR_POLYPOLYGON:
723 case EMR_SETPIXELV:
724 case EMR_MOVETOEX:
725 case EMR_EXCLUDECLIPRECT:
726 case EMR_INTERSECTCLIPRECT:
727 case EMR_SELECTOBJECT:
728 case EMR_ANGLEARC:
729 case EMR_ELLIPSE:
730 case EMR_RECTANGLE:
731 case EMR_ROUNDRECT:
732 case EMR_ARC:
733 case EMR_CHORD:
734 case EMR_PIE:
735 case EMR_EXTFLOODFILL:
736 case EMR_LINETO:
737 case EMR_ARCTO:
738 case EMR_POLYDRAW:
739 case EMR_GDICOMMENT:
740 case EMR_FILLRGN:
741 case EMR_FRAMERGN:
742 case EMR_INVERTRGN:
743 case EMR_PAINTRGN:
744 case EMR_BITBLT:
745 case EMR_STRETCHBLT:
746 case EMR_MASKBLT:
747 case EMR_PLGBLT:
748 case EMR_SETDIBITSTODEVICE:
749 case EMR_STRETCHDIBITS:
750 case EMR_EXTTEXTOUTA:
751 case EMR_EXTTEXTOUTW:
752 case EMR_POLYBEZIER16:
753 case EMR_POLYGON16:
754 case EMR_POLYLINE16:
755 case EMR_POLYBEZIERTO16:
756 case EMR_POLYLINETO16:
757 case EMR_POLYPOLYLINE16:
758 case EMR_POLYPOLYGON16:
759 case EMR_POLYDRAW16:
760 case EMR_POLYTEXTOUTA:
761 case EMR_POLYTEXTOUTW:
762 case EMR_SMALLTEXTOUT:
763 case EMR_ALPHABLEND:
764 case EMR_TRANSPARENTBLT:
765 return TRUE;
766 default:
767 return FALSE;
772 /*****************************************************************************
773 * PlayEnhMetaFileRecord (GDI32.@)
775 * Render a single enhanced metafile record in the device context hdc.
777 * RETURNS
778 * TRUE (non zero) on success, FALSE on error.
779 * BUGS
780 * Many unimplemented records.
781 * No error handling on record play failures (ie checking return codes)
783 * NOTES
784 * WinNT actually updates the current world transform in this function
785 * whereas Win9x does not.
787 BOOL WINAPI PlayEnhMetaFileRecord(
788 HDC hdc, /* [in] device context in which to render EMF record */
789 LPHANDLETABLE handletable, /* [in] array of handles to be used in rendering record */
790 const ENHMETARECORD *mr, /* [in] EMF record to render */
791 UINT handles /* [in] size of handle array */
794 int type;
795 RECT tmprc;
796 enum_emh_data *info = ENUM_GET_PRIVATE_DATA(handletable);
798 TRACE("hdc = %p, handletable = %p, record = %p, numHandles = %d\n",
799 hdc, handletable, mr, handles);
800 if (!mr) return FALSE;
802 type = mr->iType;
804 TRACE("record %s\n", get_emr_name(type));
805 switch(type)
807 case EMR_HEADER:
808 break;
809 case EMR_EOF:
810 break;
811 case EMR_GDICOMMENT:
813 const EMRGDICOMMENT *lpGdiComment = (const EMRGDICOMMENT *)mr;
814 /* In an enhanced metafile, there can be both public and private GDI comments */
815 GdiComment( hdc, lpGdiComment->cbData, lpGdiComment->Data );
816 break;
818 case EMR_SETMAPMODE:
820 const EMRSETMAPMODE *pSetMapMode = (const EMRSETMAPMODE *)mr;
822 if (info->state.mode == pSetMapMode->iMode &&
823 (info->state.mode == MM_ISOTROPIC || info->state.mode == MM_ANISOTROPIC))
824 break;
825 info->state.mode = pSetMapMode->iMode;
826 EMF_SetMapMode(hdc, info);
828 if (!IS_WIN9X())
829 EMF_Update_MF_Xform(hdc, info);
831 break;
833 case EMR_SETBKMODE:
835 const EMRSETBKMODE *pSetBkMode = (const EMRSETBKMODE *)mr;
836 SetBkMode(hdc, pSetBkMode->iMode);
837 break;
839 case EMR_SETBKCOLOR:
841 const EMRSETBKCOLOR *pSetBkColor = (const EMRSETBKCOLOR *)mr;
842 SetBkColor(hdc, pSetBkColor->crColor);
843 break;
845 case EMR_SETPOLYFILLMODE:
847 const EMRSETPOLYFILLMODE *pSetPolyFillMode = (const EMRSETPOLYFILLMODE *)mr;
848 SetPolyFillMode(hdc, pSetPolyFillMode->iMode);
849 break;
851 case EMR_SETROP2:
853 const EMRSETROP2 *pSetROP2 = (const EMRSETROP2 *)mr;
854 SetROP2(hdc, pSetROP2->iMode);
855 break;
857 case EMR_SETSTRETCHBLTMODE:
859 const EMRSETSTRETCHBLTMODE *pSetStretchBltMode = (const EMRSETSTRETCHBLTMODE *)mr;
860 SetStretchBltMode(hdc, pSetStretchBltMode->iMode);
861 break;
863 case EMR_SETTEXTALIGN:
865 const EMRSETTEXTALIGN *pSetTextAlign = (const EMRSETTEXTALIGN *)mr;
866 SetTextAlign(hdc, pSetTextAlign->iMode);
867 break;
869 case EMR_SETTEXTCOLOR:
871 const EMRSETTEXTCOLOR *pSetTextColor = (const EMRSETTEXTCOLOR *)mr;
872 SetTextColor(hdc, pSetTextColor->crColor);
873 break;
875 case EMR_SAVEDC:
877 if (SaveDC( hdc ))
878 EMF_SaveDC( info );
879 break;
881 case EMR_RESTOREDC:
883 const EMRRESTOREDC *pRestoreDC = (const EMRRESTOREDC *)mr;
884 TRACE("EMR_RESTORE: %ld\n", pRestoreDC->iRelative);
885 if (RestoreDC( hdc, pRestoreDC->iRelative ))
886 EMF_RestoreDC( info, pRestoreDC->iRelative );
887 break;
889 case EMR_INTERSECTCLIPRECT:
891 const EMRINTERSECTCLIPRECT *pClipRect = (const EMRINTERSECTCLIPRECT *)mr;
892 TRACE("EMR_INTERSECTCLIPRECT: rect %ld,%ld - %ld, %ld\n",
893 pClipRect->rclClip.left, pClipRect->rclClip.top,
894 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
895 IntersectClipRect(hdc, pClipRect->rclClip.left, pClipRect->rclClip.top,
896 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
897 break;
899 case EMR_SELECTOBJECT:
901 const EMRSELECTOBJECT *pSelectObject = (const EMRSELECTOBJECT *)mr;
902 if( pSelectObject->ihObject & 0x80000000 ) {
903 /* High order bit is set - it's a stock object
904 * Strip the high bit to get the index.
905 * See MSDN article Q142319
907 SelectObject( hdc, GetStockObject( pSelectObject->ihObject &
908 0x7fffffff ) );
909 } else {
910 /* High order bit wasn't set - not a stock object
912 SelectObject( hdc,
913 (handletable->objectHandle)[pSelectObject->ihObject] );
915 break;
917 case EMR_DELETEOBJECT:
919 const EMRDELETEOBJECT *pDeleteObject = (const EMRDELETEOBJECT *)mr;
920 DeleteObject( (handletable->objectHandle)[pDeleteObject->ihObject]);
921 (handletable->objectHandle)[pDeleteObject->ihObject] = 0;
922 break;
924 case EMR_SETWINDOWORGEX:
926 const EMRSETWINDOWORGEX *pSetWindowOrgEx = (const EMRSETWINDOWORGEX *)mr;
928 info->state.wndOrgX = pSetWindowOrgEx->ptlOrigin.x;
929 info->state.wndOrgY = pSetWindowOrgEx->ptlOrigin.y;
931 TRACE("SetWindowOrgEx: %d,%d\n", info->state.wndOrgX, info->state.wndOrgY);
933 if (!IS_WIN9X())
934 EMF_Update_MF_Xform(hdc, info);
936 break;
938 case EMR_SETWINDOWEXTEX:
940 const EMRSETWINDOWEXTEX *pSetWindowExtEx = (const EMRSETWINDOWEXTEX *)mr;
942 if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
943 break;
944 info->state.wndExtX = pSetWindowExtEx->szlExtent.cx;
945 info->state.wndExtY = pSetWindowExtEx->szlExtent.cy;
946 if (info->state.mode == MM_ISOTROPIC)
947 EMF_FixIsotropic(hdc, info);
949 TRACE("SetWindowExtEx: %d,%d\n",info->state.wndExtX, info->state.wndExtY);
951 if (!IS_WIN9X())
952 EMF_Update_MF_Xform(hdc, info);
954 break;
956 case EMR_SETVIEWPORTORGEX:
958 const EMRSETVIEWPORTORGEX *pSetViewportOrgEx = (const EMRSETVIEWPORTORGEX *)mr;
960 info->state.vportOrgX = pSetViewportOrgEx->ptlOrigin.x;
961 info->state.vportOrgY = pSetViewportOrgEx->ptlOrigin.y;
962 TRACE("SetViewportOrgEx: %d,%d\n", info->state.vportOrgX, info->state.vportOrgY);
964 if (!IS_WIN9X())
965 EMF_Update_MF_Xform(hdc, info);
967 break;
969 case EMR_SETVIEWPORTEXTEX:
971 const EMRSETVIEWPORTEXTEX *pSetViewportExtEx = (const EMRSETVIEWPORTEXTEX *)mr;
973 if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
974 break;
975 info->state.vportExtX = pSetViewportExtEx->szlExtent.cx;
976 info->state.vportExtY = pSetViewportExtEx->szlExtent.cy;
977 if (info->state.mode == MM_ISOTROPIC)
978 EMF_FixIsotropic(hdc, info);
979 TRACE("SetViewportExtEx: %d,%d\n", info->state.vportExtX, info->state.vportExtY);
981 if (!IS_WIN9X())
982 EMF_Update_MF_Xform(hdc, info);
984 break;
986 case EMR_CREATEPEN:
988 const EMRCREATEPEN *pCreatePen = (const EMRCREATEPEN *)mr;
989 (handletable->objectHandle)[pCreatePen->ihPen] =
990 CreatePenIndirect(&pCreatePen->lopn);
991 break;
993 case EMR_EXTCREATEPEN:
995 const EMREXTCREATEPEN *pPen = (const EMREXTCREATEPEN *)mr;
996 LOGBRUSH lb;
997 lb.lbStyle = pPen->elp.elpBrushStyle;
998 lb.lbColor = pPen->elp.elpColor;
999 lb.lbHatch = pPen->elp.elpHatch;
1001 if(pPen->offBmi || pPen->offBits)
1002 FIXME("EMR_EXTCREATEPEN: Need to copy brush bitmap\n");
1004 (handletable->objectHandle)[pPen->ihPen] =
1005 ExtCreatePen(pPen->elp.elpPenStyle, pPen->elp.elpWidth, &lb,
1006 pPen->elp.elpNumEntries, pPen->elp.elpNumEntries ? pPen->elp.elpStyleEntry : NULL);
1007 break;
1009 case EMR_CREATEBRUSHINDIRECT:
1011 const EMRCREATEBRUSHINDIRECT *pBrush = (const EMRCREATEBRUSHINDIRECT *)mr;
1012 LOGBRUSH brush;
1013 brush.lbStyle = pBrush->lb.lbStyle;
1014 brush.lbColor = pBrush->lb.lbColor;
1015 brush.lbHatch = pBrush->lb.lbHatch;
1016 (handletable->objectHandle)[pBrush->ihBrush] = CreateBrushIndirect(&brush);
1017 break;
1019 case EMR_EXTCREATEFONTINDIRECTW:
1021 const EMREXTCREATEFONTINDIRECTW *pFont = (const EMREXTCREATEFONTINDIRECTW *)mr;
1022 (handletable->objectHandle)[pFont->ihFont] =
1023 CreateFontIndirectW(&pFont->elfw.elfLogFont);
1024 break;
1026 case EMR_MOVETOEX:
1028 const EMRMOVETOEX *pMoveToEx = (const EMRMOVETOEX *)mr;
1029 MoveToEx(hdc, pMoveToEx->ptl.x, pMoveToEx->ptl.y, NULL);
1030 break;
1032 case EMR_LINETO:
1034 const EMRLINETO *pLineTo = (const EMRLINETO *)mr;
1035 LineTo(hdc, pLineTo->ptl.x, pLineTo->ptl.y);
1036 break;
1038 case EMR_RECTANGLE:
1040 const EMRRECTANGLE *pRect = (const EMRRECTANGLE *)mr;
1041 Rectangle(hdc, pRect->rclBox.left, pRect->rclBox.top,
1042 pRect->rclBox.right, pRect->rclBox.bottom);
1043 break;
1045 case EMR_ELLIPSE:
1047 const EMRELLIPSE *pEllipse = (const EMRELLIPSE *)mr;
1048 Ellipse(hdc, pEllipse->rclBox.left, pEllipse->rclBox.top,
1049 pEllipse->rclBox.right, pEllipse->rclBox.bottom);
1050 break;
1052 case EMR_POLYGON16:
1054 const EMRPOLYGON16 *pPoly = (const EMRPOLYGON16 *)mr;
1055 /* Shouldn't use Polygon16 since pPoly->cpts is DWORD */
1056 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1057 pPoly->cpts * sizeof(POINT) );
1058 DWORD i;
1059 for(i = 0; i < pPoly->cpts; i++)
1061 pts[i].x = pPoly->apts[i].x;
1062 pts[i].y = pPoly->apts[i].y;
1064 Polygon(hdc, pts, pPoly->cpts);
1065 HeapFree( GetProcessHeap(), 0, pts );
1066 break;
1068 case EMR_POLYLINE16:
1070 const EMRPOLYLINE16 *pPoly = (const EMRPOLYLINE16 *)mr;
1071 /* Shouldn't use Polyline16 since pPoly->cpts is DWORD */
1072 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1073 pPoly->cpts * sizeof(POINT) );
1074 DWORD i;
1075 for(i = 0; i < pPoly->cpts; i++)
1077 pts[i].x = pPoly->apts[i].x;
1078 pts[i].y = pPoly->apts[i].y;
1080 Polyline(hdc, pts, pPoly->cpts);
1081 HeapFree( GetProcessHeap(), 0, pts );
1082 break;
1084 case EMR_POLYLINETO16:
1086 const EMRPOLYLINETO16 *pPoly = (const EMRPOLYLINETO16 *)mr;
1087 /* Shouldn't use PolylineTo16 since pPoly->cpts is DWORD */
1088 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1089 pPoly->cpts * sizeof(POINT) );
1090 DWORD i;
1091 for(i = 0; i < pPoly->cpts; i++)
1093 pts[i].x = pPoly->apts[i].x;
1094 pts[i].y = pPoly->apts[i].y;
1096 PolylineTo(hdc, pts, pPoly->cpts);
1097 HeapFree( GetProcessHeap(), 0, pts );
1098 break;
1100 case EMR_POLYBEZIER16:
1102 const EMRPOLYBEZIER16 *pPoly = (const EMRPOLYBEZIER16 *)mr;
1103 /* Shouldn't use PolyBezier16 since pPoly->cpts is DWORD */
1104 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1105 pPoly->cpts * sizeof(POINT) );
1106 DWORD i;
1107 for(i = 0; i < pPoly->cpts; i++)
1109 pts[i].x = pPoly->apts[i].x;
1110 pts[i].y = pPoly->apts[i].y;
1112 PolyBezier(hdc, pts, pPoly->cpts);
1113 HeapFree( GetProcessHeap(), 0, pts );
1114 break;
1116 case EMR_POLYBEZIERTO16:
1118 const EMRPOLYBEZIERTO16 *pPoly = (const EMRPOLYBEZIERTO16 *)mr;
1119 /* Shouldn't use PolyBezierTo16 since pPoly->cpts is DWORD */
1120 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1121 pPoly->cpts * sizeof(POINT) );
1122 DWORD i;
1123 for(i = 0; i < pPoly->cpts; i++)
1125 pts[i].x = pPoly->apts[i].x;
1126 pts[i].y = pPoly->apts[i].y;
1128 PolyBezierTo(hdc, pts, pPoly->cpts);
1129 HeapFree( GetProcessHeap(), 0, pts );
1130 break;
1132 case EMR_POLYPOLYGON16:
1134 const EMRPOLYPOLYGON16 *pPolyPoly = (const EMRPOLYPOLYGON16 *)mr;
1135 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1136 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1138 const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1139 POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1140 DWORD i;
1141 for(i = 0; i < pPolyPoly->cpts; i++)
1143 pt[i].x = pts[i].x;
1144 pt[i].y = pts[i].y;
1146 PolyPolygon(hdc, pt, (const INT*)pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1147 HeapFree( GetProcessHeap(), 0, pt );
1148 break;
1150 case EMR_POLYPOLYLINE16:
1152 const EMRPOLYPOLYLINE16 *pPolyPoly = (const EMRPOLYPOLYLINE16 *)mr;
1153 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1154 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1156 const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1157 POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1158 DWORD i;
1159 for(i = 0; i < pPolyPoly->cpts; i++)
1161 pt[i].x = pts[i].x;
1162 pt[i].y = pts[i].y;
1164 PolyPolyline(hdc, pt, pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1165 HeapFree( GetProcessHeap(), 0, pt );
1166 break;
1169 case EMR_POLYDRAW16:
1171 const EMRPOLYDRAW16 *pPolyDraw16 = (const EMRPOLYDRAW16 *)mr;
1172 const POINTS *ptl = pPolyDraw16->apts;
1173 POINT *pts = HeapAlloc(GetProcessHeap(), 0, pPolyDraw16->cpts * sizeof(POINT));
1174 DWORD i;
1176 /* NB abTypes array doesn't start at pPolyDraw16->abTypes. It's actually
1177 pPolyDraw16->apts + pPolyDraw16->cpts. */
1178 const BYTE *types = (BYTE*)(pPolyDraw16->apts + pPolyDraw16->cpts);
1180 if (!pts)
1181 break;
1183 for (i = 0; i < pPolyDraw16->cpts; ++i)
1185 pts[i].x = ptl[i].x;
1186 pts[i].y = ptl[i].y;
1189 PolyDraw(hdc, pts, types, pPolyDraw16->cpts);
1190 HeapFree(GetProcessHeap(), 0, pts);
1191 break;
1194 case EMR_STRETCHDIBITS:
1196 const EMRSTRETCHDIBITS *pStretchDIBits = (const EMRSTRETCHDIBITS *)mr;
1198 StretchDIBits(hdc,
1199 pStretchDIBits->xDest,
1200 pStretchDIBits->yDest,
1201 pStretchDIBits->cxDest,
1202 pStretchDIBits->cyDest,
1203 pStretchDIBits->xSrc,
1204 pStretchDIBits->ySrc,
1205 pStretchDIBits->cxSrc,
1206 pStretchDIBits->cySrc,
1207 (const BYTE *)mr + pStretchDIBits->offBitsSrc,
1208 (const BITMAPINFO *)((const BYTE *)mr + pStretchDIBits->offBmiSrc),
1209 pStretchDIBits->iUsageSrc,
1210 pStretchDIBits->dwRop);
1211 break;
1214 case EMR_EXTTEXTOUTA:
1216 const EMREXTTEXTOUTA *pExtTextOutA = (const EMREXTTEXTOUTA *)mr;
1217 RECT rc;
1218 const INT *dx = NULL;
1219 int old_mode;
1221 rc.left = pExtTextOutA->emrtext.rcl.left;
1222 rc.top = pExtTextOutA->emrtext.rcl.top;
1223 rc.right = pExtTextOutA->emrtext.rcl.right;
1224 rc.bottom = pExtTextOutA->emrtext.rcl.bottom;
1225 TRACE("EMR_EXTTEXTOUTA: x,y = %ld, %ld. rect = %s. flags %08lx\n",
1226 pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1227 wine_dbgstr_rect(&rc), pExtTextOutA->emrtext.fOptions);
1229 old_mode = SetGraphicsMode(hdc, pExtTextOutA->iGraphicsMode);
1230 /* Reselect the font back into the dc so that the transformation
1231 gets updated. */
1232 SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1234 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1235 * These files can be enumerated and played under Win98 just
1236 * fine, but at least Win2k chokes on them.
1238 if (pExtTextOutA->emrtext.offDx)
1239 dx = (const INT *)((const BYTE *)mr + pExtTextOutA->emrtext.offDx);
1241 ExtTextOutA(hdc, pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1242 pExtTextOutA->emrtext.fOptions, &rc,
1243 (LPCSTR)((const BYTE *)mr + pExtTextOutA->emrtext.offString), pExtTextOutA->emrtext.nChars,
1244 dx);
1246 SetGraphicsMode(hdc, old_mode);
1247 break;
1250 case EMR_EXTTEXTOUTW:
1252 const EMREXTTEXTOUTW *pExtTextOutW = (const EMREXTTEXTOUTW *)mr;
1253 RECT rc;
1254 const INT *dx = NULL;
1255 int old_mode;
1257 rc.left = pExtTextOutW->emrtext.rcl.left;
1258 rc.top = pExtTextOutW->emrtext.rcl.top;
1259 rc.right = pExtTextOutW->emrtext.rcl.right;
1260 rc.bottom = pExtTextOutW->emrtext.rcl.bottom;
1261 TRACE("EMR_EXTTEXTOUTW: x,y = %ld, %ld. rect = %s. flags %08lx\n",
1262 pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1263 wine_dbgstr_rect(&rc), pExtTextOutW->emrtext.fOptions);
1265 old_mode = SetGraphicsMode(hdc, pExtTextOutW->iGraphicsMode);
1266 /* Reselect the font back into the dc so that the transformation
1267 gets updated. */
1268 SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1270 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1271 * These files can be enumerated and played under Win98 just
1272 * fine, but at least Win2k chokes on them.
1274 if (pExtTextOutW->emrtext.offDx)
1275 dx = (const INT *)((const BYTE *)mr + pExtTextOutW->emrtext.offDx);
1277 ExtTextOutW(hdc, pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1278 pExtTextOutW->emrtext.fOptions, &rc,
1279 (LPCWSTR)((const BYTE *)mr + pExtTextOutW->emrtext.offString), pExtTextOutW->emrtext.nChars,
1280 dx);
1282 SetGraphicsMode(hdc, old_mode);
1283 break;
1286 case EMR_CREATEPALETTE:
1288 const EMRCREATEPALETTE *lpCreatePal = (const EMRCREATEPALETTE *)mr;
1290 (handletable->objectHandle)[ lpCreatePal->ihPal ] =
1291 CreatePalette( &lpCreatePal->lgpl );
1293 break;
1296 case EMR_SELECTPALETTE:
1298 const EMRSELECTPALETTE *lpSelectPal = (const EMRSELECTPALETTE *)mr;
1300 if( lpSelectPal->ihPal & 0x80000000 ) {
1301 SelectPalette( hdc, GetStockObject(lpSelectPal->ihPal & 0x7fffffff), TRUE);
1302 } else {
1303 SelectPalette( hdc, (handletable->objectHandle)[lpSelectPal->ihPal], TRUE);
1305 break;
1308 case EMR_REALIZEPALETTE:
1310 RealizePalette( hdc );
1311 break;
1314 case EMR_EXTSELECTCLIPRGN:
1316 const EMREXTSELECTCLIPRGN *lpRgn = (const EMREXTSELECTCLIPRGN *)mr;
1317 HRGN hRgn = 0;
1319 if (mr->nSize >= sizeof(*lpRgn) + sizeof(RGNDATAHEADER))
1320 hRgn = ExtCreateRegion( &info->init_transform, 0, (const RGNDATA *)lpRgn->RgnData );
1322 ExtSelectClipRgn(hdc, hRgn, (INT)(lpRgn->iMode));
1323 /* ExtSelectClipRgn created a copy of the region */
1324 DeleteObject(hRgn);
1325 break;
1328 case EMR_SETMETARGN:
1330 SetMetaRgn( hdc );
1331 break;
1334 case EMR_SETWORLDTRANSFORM:
1336 const EMRSETWORLDTRANSFORM *lpXfrm = (const EMRSETWORLDTRANSFORM *)mr;
1337 info->state.world_transform = lpXfrm->xform;
1339 if (!IS_WIN9X())
1340 EMF_Update_MF_Xform(hdc, info);
1342 break;
1345 case EMR_POLYBEZIER:
1347 const EMRPOLYBEZIER *lpPolyBez = (const EMRPOLYBEZIER *)mr;
1348 PolyBezier(hdc, (const POINT*)lpPolyBez->aptl, (UINT)lpPolyBez->cptl);
1349 break;
1352 case EMR_POLYGON:
1354 const EMRPOLYGON *lpPoly = (const EMRPOLYGON *)mr;
1355 Polygon( hdc, (const POINT*)lpPoly->aptl, (UINT)lpPoly->cptl );
1356 break;
1359 case EMR_POLYLINE:
1361 const EMRPOLYLINE *lpPolyLine = (const EMRPOLYLINE *)mr;
1362 Polyline(hdc, (const POINT*)lpPolyLine->aptl, (UINT)lpPolyLine->cptl);
1363 break;
1366 case EMR_POLYBEZIERTO:
1368 const EMRPOLYBEZIERTO *lpPolyBezierTo = (const EMRPOLYBEZIERTO *)mr;
1369 PolyBezierTo( hdc, (const POINT*)lpPolyBezierTo->aptl,
1370 (UINT)lpPolyBezierTo->cptl );
1371 break;
1374 case EMR_POLYLINETO:
1376 const EMRPOLYLINETO *lpPolyLineTo = (const EMRPOLYLINETO *)mr;
1377 PolylineTo( hdc, (const POINT*)lpPolyLineTo->aptl,
1378 (UINT)lpPolyLineTo->cptl );
1379 break;
1382 case EMR_POLYPOLYLINE:
1384 const EMRPOLYPOLYLINE *pPolyPolyline = (const EMRPOLYPOLYLINE *)mr;
1385 /* NB Points at pPolyPolyline->aPolyCounts + pPolyPolyline->nPolys */
1387 PolyPolyline(hdc, (const POINT*)(pPolyPolyline->aPolyCounts +
1388 pPolyPolyline->nPolys),
1389 pPolyPolyline->aPolyCounts,
1390 pPolyPolyline->nPolys );
1392 break;
1395 case EMR_POLYPOLYGON:
1397 const EMRPOLYPOLYGON *pPolyPolygon = (const EMRPOLYPOLYGON *)mr;
1399 /* NB Points at pPolyPolygon->aPolyCounts + pPolyPolygon->nPolys */
1401 PolyPolygon(hdc, (const POINT*)(pPolyPolygon->aPolyCounts +
1402 pPolyPolygon->nPolys),
1403 (const INT*)pPolyPolygon->aPolyCounts, pPolyPolygon->nPolys );
1404 break;
1407 case EMR_SETBRUSHORGEX:
1409 const EMRSETBRUSHORGEX *lpSetBrushOrgEx = (const EMRSETBRUSHORGEX *)mr;
1411 SetBrushOrgEx( hdc,
1412 (INT)lpSetBrushOrgEx->ptlOrigin.x,
1413 (INT)lpSetBrushOrgEx->ptlOrigin.y,
1414 NULL );
1416 break;
1419 case EMR_SETPIXELV:
1421 const EMRSETPIXELV *lpSetPixelV = (const EMRSETPIXELV *)mr;
1423 SetPixelV( hdc,
1424 (INT)lpSetPixelV->ptlPixel.x,
1425 (INT)lpSetPixelV->ptlPixel.y,
1426 lpSetPixelV->crColor );
1428 break;
1431 case EMR_SETMAPPERFLAGS:
1433 const EMRSETMAPPERFLAGS *lpSetMapperFlags = (const EMRSETMAPPERFLAGS *)mr;
1435 SetMapperFlags( hdc, lpSetMapperFlags->dwFlags );
1437 break;
1440 case EMR_SETCOLORADJUSTMENT:
1442 const EMRSETCOLORADJUSTMENT *lpSetColorAdjust = (const EMRSETCOLORADJUSTMENT *)mr;
1444 NtGdiSetColorAdjustment( hdc, &lpSetColorAdjust->ColorAdjustment );
1446 break;
1449 case EMR_OFFSETCLIPRGN:
1451 const EMROFFSETCLIPRGN *lpOffsetClipRgn = (const EMROFFSETCLIPRGN *)mr;
1453 OffsetClipRgn( hdc,
1454 (INT)lpOffsetClipRgn->ptlOffset.x,
1455 (INT)lpOffsetClipRgn->ptlOffset.y );
1456 FIXME("OffsetClipRgn\n");
1458 break;
1461 case EMR_EXCLUDECLIPRECT:
1463 const EMREXCLUDECLIPRECT *lpExcludeClipRect = (const EMREXCLUDECLIPRECT *)mr;
1465 ExcludeClipRect( hdc,
1466 lpExcludeClipRect->rclClip.left,
1467 lpExcludeClipRect->rclClip.top,
1468 lpExcludeClipRect->rclClip.right,
1469 lpExcludeClipRect->rclClip.bottom );
1470 FIXME("ExcludeClipRect\n");
1472 break;
1475 case EMR_SCALEVIEWPORTEXTEX:
1477 const EMRSCALEVIEWPORTEXTEX *lpScaleViewportExtEx = (const EMRSCALEVIEWPORTEXTEX *)mr;
1479 if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1480 break;
1481 if (!lpScaleViewportExtEx->xNum || !lpScaleViewportExtEx->xDenom ||
1482 !lpScaleViewportExtEx->yNum || !lpScaleViewportExtEx->yDenom)
1483 break;
1484 info->state.vportExtX = MulDiv(info->state.vportExtX, lpScaleViewportExtEx->xNum,
1485 lpScaleViewportExtEx->xDenom);
1486 info->state.vportExtY = MulDiv(info->state.vportExtY, lpScaleViewportExtEx->yNum,
1487 lpScaleViewportExtEx->yDenom);
1488 if (info->state.vportExtX == 0) info->state.vportExtX = 1;
1489 if (info->state.vportExtY == 0) info->state.vportExtY = 1;
1490 if (info->state.mode == MM_ISOTROPIC)
1491 EMF_FixIsotropic(hdc, info);
1493 TRACE("EMRSCALEVIEWPORTEXTEX %ld/%ld %ld/%ld\n",
1494 lpScaleViewportExtEx->xNum,lpScaleViewportExtEx->xDenom,
1495 lpScaleViewportExtEx->yNum,lpScaleViewportExtEx->yDenom);
1497 if (!IS_WIN9X())
1498 EMF_Update_MF_Xform(hdc, info);
1500 break;
1503 case EMR_SCALEWINDOWEXTEX:
1505 const EMRSCALEWINDOWEXTEX *lpScaleWindowExtEx = (const EMRSCALEWINDOWEXTEX *)mr;
1507 if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1508 break;
1509 if (!lpScaleWindowExtEx->xNum || !lpScaleWindowExtEx->xDenom ||
1510 !lpScaleWindowExtEx->yNum || !lpScaleWindowExtEx->yDenom)
1511 break;
1512 info->state.wndExtX = MulDiv(info->state.wndExtX, lpScaleWindowExtEx->xNum,
1513 lpScaleWindowExtEx->xDenom);
1514 info->state.wndExtY = MulDiv(info->state.wndExtY, lpScaleWindowExtEx->yNum,
1515 lpScaleWindowExtEx->yDenom);
1516 if (info->state.wndExtX == 0) info->state.wndExtX = 1;
1517 if (info->state.wndExtY == 0) info->state.wndExtY = 1;
1518 if (info->state.mode == MM_ISOTROPIC)
1519 EMF_FixIsotropic(hdc, info);
1521 TRACE("EMRSCALEWINDOWEXTEX %ld/%ld %ld/%ld\n",
1522 lpScaleWindowExtEx->xNum,lpScaleWindowExtEx->xDenom,
1523 lpScaleWindowExtEx->yNum,lpScaleWindowExtEx->yDenom);
1525 if (!IS_WIN9X())
1526 EMF_Update_MF_Xform(hdc, info);
1528 break;
1531 case EMR_MODIFYWORLDTRANSFORM:
1533 const EMRMODIFYWORLDTRANSFORM *lpModifyWorldTrans = (const EMRMODIFYWORLDTRANSFORM *)mr;
1535 switch(lpModifyWorldTrans->iMode) {
1536 case MWT_IDENTITY:
1537 info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
1538 info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
1539 info->state.world_transform.eDx = info->state.world_transform.eDy = 0;
1540 if (!IS_WIN9X())
1541 EMF_Update_MF_Xform(hdc, info);
1542 break;
1543 case MWT_LEFTMULTIPLY:
1544 CombineTransform(&info->state.world_transform, &lpModifyWorldTrans->xform,
1545 &info->state.world_transform);
1546 if (!IS_WIN9X())
1547 ModifyWorldTransform(hdc, &lpModifyWorldTrans->xform, MWT_LEFTMULTIPLY);
1548 break;
1549 case MWT_RIGHTMULTIPLY:
1550 CombineTransform(&info->state.world_transform, &info->state.world_transform,
1551 &lpModifyWorldTrans->xform);
1552 if (!IS_WIN9X())
1553 EMF_Update_MF_Xform(hdc, info);
1554 break;
1555 default:
1556 FIXME("Unknown imode %ld\n", lpModifyWorldTrans->iMode);
1557 break;
1559 break;
1562 case EMR_ANGLEARC:
1564 const EMRANGLEARC *lpAngleArc = (const EMRANGLEARC *)mr;
1566 AngleArc( hdc,
1567 (INT)lpAngleArc->ptlCenter.x, (INT)lpAngleArc->ptlCenter.y,
1568 lpAngleArc->nRadius, lpAngleArc->eStartAngle,
1569 lpAngleArc->eSweepAngle );
1571 break;
1574 case EMR_ROUNDRECT:
1576 const EMRROUNDRECT *lpRoundRect = (const EMRROUNDRECT *)mr;
1578 RoundRect( hdc,
1579 lpRoundRect->rclBox.left,
1580 lpRoundRect->rclBox.top,
1581 lpRoundRect->rclBox.right,
1582 lpRoundRect->rclBox.bottom,
1583 lpRoundRect->szlCorner.cx,
1584 lpRoundRect->szlCorner.cy );
1586 break;
1589 case EMR_ARC:
1591 const EMRARC *lpArc = (const EMRARC *)mr;
1593 Arc( hdc,
1594 (INT)lpArc->rclBox.left,
1595 (INT)lpArc->rclBox.top,
1596 (INT)lpArc->rclBox.right,
1597 (INT)lpArc->rclBox.bottom,
1598 (INT)lpArc->ptlStart.x,
1599 (INT)lpArc->ptlStart.y,
1600 (INT)lpArc->ptlEnd.x,
1601 (INT)lpArc->ptlEnd.y );
1603 break;
1606 case EMR_CHORD:
1608 const EMRCHORD *lpChord = (const EMRCHORD *)mr;
1610 Chord( hdc,
1611 (INT)lpChord->rclBox.left,
1612 (INT)lpChord->rclBox.top,
1613 (INT)lpChord->rclBox.right,
1614 (INT)lpChord->rclBox.bottom,
1615 (INT)lpChord->ptlStart.x,
1616 (INT)lpChord->ptlStart.y,
1617 (INT)lpChord->ptlEnd.x,
1618 (INT)lpChord->ptlEnd.y );
1620 break;
1623 case EMR_PIE:
1625 const EMRPIE *lpPie = (const EMRPIE *)mr;
1627 Pie( hdc,
1628 (INT)lpPie->rclBox.left,
1629 (INT)lpPie->rclBox.top,
1630 (INT)lpPie->rclBox.right,
1631 (INT)lpPie->rclBox.bottom,
1632 (INT)lpPie->ptlStart.x,
1633 (INT)lpPie->ptlStart.y,
1634 (INT)lpPie->ptlEnd.x,
1635 (INT)lpPie->ptlEnd.y );
1637 break;
1640 case EMR_ARCTO:
1642 const EMRARC *lpArcTo = (const EMRARC *)mr;
1644 ArcTo( hdc,
1645 (INT)lpArcTo->rclBox.left,
1646 (INT)lpArcTo->rclBox.top,
1647 (INT)lpArcTo->rclBox.right,
1648 (INT)lpArcTo->rclBox.bottom,
1649 (INT)lpArcTo->ptlStart.x,
1650 (INT)lpArcTo->ptlStart.y,
1651 (INT)lpArcTo->ptlEnd.x,
1652 (INT)lpArcTo->ptlEnd.y );
1654 break;
1657 case EMR_EXTFLOODFILL:
1659 const EMREXTFLOODFILL *lpExtFloodFill = (const EMREXTFLOODFILL *)mr;
1661 ExtFloodFill( hdc,
1662 (INT)lpExtFloodFill->ptlStart.x,
1663 (INT)lpExtFloodFill->ptlStart.y,
1664 lpExtFloodFill->crColor,
1665 (UINT)lpExtFloodFill->iMode );
1667 break;
1670 case EMR_POLYDRAW:
1672 const EMRPOLYDRAW *lpPolyDraw = (const EMRPOLYDRAW *)mr;
1674 /* NB abTypes array doesn't start at lpPolyDraw->abTypes. It's actually
1675 lpPolyDraw->aptl + lpPolyDraw->cptl. */
1676 PolyDraw( hdc,
1677 (const POINT*)lpPolyDraw->aptl,
1678 (BYTE*)(lpPolyDraw->aptl + lpPolyDraw->cptl),
1679 (INT)lpPolyDraw->cptl );
1681 break;
1684 case EMR_SETARCDIRECTION:
1686 const EMRSETARCDIRECTION *lpSetArcDirection = (const EMRSETARCDIRECTION *)mr;
1687 SetArcDirection( hdc, (INT)lpSetArcDirection->iArcDirection );
1688 break;
1691 case EMR_SETMITERLIMIT:
1693 const EMRSETMITERLIMIT *lpSetMiterLimit = (const EMRSETMITERLIMIT *)mr;
1694 SetMiterLimit( hdc, lpSetMiterLimit->eMiterLimit, NULL );
1695 break;
1698 case EMR_BEGINPATH:
1700 BeginPath( hdc );
1701 break;
1704 case EMR_ENDPATH:
1706 EndPath( hdc );
1707 break;
1710 case EMR_CLOSEFIGURE:
1712 CloseFigure( hdc );
1713 break;
1716 case EMR_FILLPATH:
1718 /*const EMRFILLPATH lpFillPath = (const EMRFILLPATH *)mr;*/
1719 FillPath( hdc );
1720 break;
1723 case EMR_STROKEANDFILLPATH:
1725 /*const EMRSTROKEANDFILLPATH lpStrokeAndFillPath = (const EMRSTROKEANDFILLPATH *)mr;*/
1726 StrokeAndFillPath( hdc );
1727 break;
1730 case EMR_STROKEPATH:
1732 /*const EMRSTROKEPATH lpStrokePath = (const EMRSTROKEPATH *)mr;*/
1733 StrokePath( hdc );
1734 break;
1737 case EMR_FLATTENPATH:
1739 FlattenPath( hdc );
1740 break;
1743 case EMR_WIDENPATH:
1745 WidenPath( hdc );
1746 break;
1749 case EMR_SELECTCLIPPATH:
1751 const EMRSELECTCLIPPATH *lpSelectClipPath = (const EMRSELECTCLIPPATH *)mr;
1752 SelectClipPath( hdc, (INT)lpSelectClipPath->iMode );
1753 break;
1756 case EMR_ABORTPATH:
1758 AbortPath( hdc );
1759 break;
1762 case EMR_CREATECOLORSPACE:
1764 PEMRCREATECOLORSPACE lpCreateColorSpace = (PEMRCREATECOLORSPACE)mr;
1765 (handletable->objectHandle)[lpCreateColorSpace->ihCS] =
1766 CreateColorSpaceA( &lpCreateColorSpace->lcs );
1767 break;
1770 case EMR_SETCOLORSPACE:
1772 const EMRSETCOLORSPACE *lpSetColorSpace = (const EMRSETCOLORSPACE *)mr;
1773 SetColorSpace( hdc,
1774 (handletable->objectHandle)[lpSetColorSpace->ihCS] );
1775 break;
1778 case EMR_DELETECOLORSPACE:
1780 const EMRDELETECOLORSPACE *lpDeleteColorSpace = (const EMRDELETECOLORSPACE *)mr;
1781 DeleteColorSpace( (handletable->objectHandle)[lpDeleteColorSpace->ihCS] );
1782 break;
1785 case EMR_SETICMMODE:
1787 const EMRSETICMMODE *lpSetICMMode = (const EMRSETICMMODE *)mr;
1788 SetICMMode( hdc, (INT)lpSetICMMode->iMode );
1789 break;
1792 case EMR_PIXELFORMAT:
1794 INT iPixelFormat;
1795 const EMRPIXELFORMAT *lpPixelFormat = (const EMRPIXELFORMAT *)mr;
1797 iPixelFormat = ChoosePixelFormat( hdc, &lpPixelFormat->pfd );
1798 SetPixelFormat( hdc, iPixelFormat, &lpPixelFormat->pfd );
1800 break;
1803 case EMR_SETPALETTEENTRIES:
1805 const EMRSETPALETTEENTRIES *lpSetPaletteEntries = (const EMRSETPALETTEENTRIES *)mr;
1807 SetPaletteEntries( (handletable->objectHandle)[lpSetPaletteEntries->ihPal],
1808 (UINT)lpSetPaletteEntries->iStart,
1809 (UINT)lpSetPaletteEntries->cEntries,
1810 lpSetPaletteEntries->aPalEntries );
1812 break;
1815 case EMR_RESIZEPALETTE:
1817 const EMRRESIZEPALETTE *lpResizePalette = (const EMRRESIZEPALETTE *)mr;
1819 NtGdiResizePalette( handletable->objectHandle[lpResizePalette->ihPal],
1820 lpResizePalette->cEntries );
1822 break;
1825 case EMR_CREATEDIBPATTERNBRUSHPT:
1827 const EMRCREATEDIBPATTERNBRUSHPT *lpCreate = (const EMRCREATEDIBPATTERNBRUSHPT *)mr;
1828 LPVOID lpPackedStruct;
1830 /* Check that offsets and data are contained within the record
1831 * (including checking for wrap-arounds).
1833 if ( lpCreate->offBmi + lpCreate->cbBmi > mr->nSize
1834 || lpCreate->offBits + lpCreate->cbBits > mr->nSize
1835 || lpCreate->offBmi + lpCreate->cbBmi < lpCreate->offBmi
1836 || lpCreate->offBits + lpCreate->cbBits < lpCreate->offBits )
1838 ERR("Invalid EMR_CREATEDIBPATTERNBRUSHPT record\n");
1839 break;
1842 /* This is a BITMAPINFO struct followed directly by bitmap bits */
1843 lpPackedStruct = HeapAlloc( GetProcessHeap(), 0,
1844 lpCreate->cbBmi + lpCreate->cbBits );
1845 if(!lpPackedStruct)
1847 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1848 break;
1851 /* Now pack this structure */
1852 memcpy( lpPackedStruct,
1853 ((const BYTE *)lpCreate) + lpCreate->offBmi,
1854 lpCreate->cbBmi );
1855 memcpy( ((BYTE*)lpPackedStruct) + lpCreate->cbBmi,
1856 ((const BYTE *)lpCreate) + lpCreate->offBits,
1857 lpCreate->cbBits );
1859 (handletable->objectHandle)[lpCreate->ihBrush] =
1860 CreateDIBPatternBrushPt( lpPackedStruct,
1861 (UINT)lpCreate->iUsage );
1863 HeapFree(GetProcessHeap(), 0, lpPackedStruct);
1864 break;
1867 case EMR_CREATEMONOBRUSH:
1869 const EMRCREATEMONOBRUSH *pCreateMonoBrush = (const EMRCREATEMONOBRUSH *)mr;
1870 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pCreateMonoBrush->offBmi);
1871 HBITMAP hBmp;
1873 /* Need to check if the bitmap is monochrome, and if the
1874 two colors are really black and white */
1875 if (pCreateMonoBrush->iUsage == DIB_PAL_MONO)
1877 BITMAP bm;
1879 /* Undocumented iUsage indicates a mono bitmap with no palette table,
1880 * aligned to 32 rather than 16 bits.
1882 bm.bmType = 0;
1883 bm.bmWidth = pbi->bmiHeader.biWidth;
1884 bm.bmHeight = abs(pbi->bmiHeader.biHeight);
1885 bm.bmWidthBytes = 4 * ((pbi->bmiHeader.biWidth + 31) / 32);
1886 bm.bmPlanes = pbi->bmiHeader.biPlanes;
1887 bm.bmBitsPixel = pbi->bmiHeader.biBitCount;
1888 bm.bmBits = (BYTE *)mr + pCreateMonoBrush->offBits;
1889 hBmp = CreateBitmapIndirect(&bm);
1891 else if (is_dib_monochrome(pbi))
1893 /* Top-down DIBs have a negative height */
1894 LONG height = pbi->bmiHeader.biHeight;
1896 hBmp = CreateBitmap(pbi->bmiHeader.biWidth, abs(height), 1, 1, NULL);
1897 SetDIBits(hdc, hBmp, 0, pbi->bmiHeader.biHeight,
1898 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1900 else
1902 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1903 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1906 (handletable->objectHandle)[pCreateMonoBrush->ihBrush] = CreatePatternBrush(hBmp);
1908 /* CreatePatternBrush created a copy of the bitmap */
1909 DeleteObject(hBmp);
1910 break;
1913 case EMR_BITBLT:
1915 const EMRBITBLT *pBitBlt = (const EMRBITBLT *)mr;
1917 if(pBitBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1918 PatBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1919 pBitBlt->dwRop);
1920 } else { /* BitBlt */
1921 HDC hdcSrc = NtGdiCreateCompatibleDC( hdc );
1922 HBRUSH hBrush, hBrushOld;
1923 HBITMAP hBmp = 0, hBmpOld = 0;
1924 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pBitBlt->offBmiSrc);
1926 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1927 SetWorldTransform(hdcSrc, &pBitBlt->xformSrc);
1929 hBrush = CreateSolidBrush(pBitBlt->crBkColorSrc);
1930 hBrushOld = SelectObject(hdcSrc, hBrush);
1931 PatBlt(hdcSrc, pBitBlt->rclBounds.left, pBitBlt->rclBounds.top,
1932 pBitBlt->rclBounds.right - pBitBlt->rclBounds.left,
1933 pBitBlt->rclBounds.bottom - pBitBlt->rclBounds.top, PATCOPY);
1934 SelectObject(hdcSrc, hBrushOld);
1935 DeleteObject(hBrush);
1937 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1938 (const BYTE *)mr + pBitBlt->offBitsSrc, pbi, pBitBlt->iUsageSrc);
1939 hBmpOld = SelectObject(hdcSrc, hBmp);
1941 BitBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1942 hdcSrc, pBitBlt->xSrc, pBitBlt->ySrc, pBitBlt->dwRop);
1944 SelectObject(hdcSrc, hBmpOld);
1945 DeleteObject(hBmp);
1946 DeleteDC(hdcSrc);
1948 break;
1951 case EMR_STRETCHBLT:
1953 const EMRSTRETCHBLT *pStretchBlt = (const EMRSTRETCHBLT *)mr;
1955 TRACE("EMR_STRETCHBLT: %ld, %ld %ldx%ld -> %ld, %ld %ldx%ld. rop %08lx offBitsSrc %ld\n",
1956 pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1957 pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1958 pStretchBlt->dwRop, pStretchBlt->offBitsSrc);
1960 if(pStretchBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1961 PatBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1962 pStretchBlt->dwRop);
1963 } else { /* StretchBlt */
1964 HDC hdcSrc = NtGdiCreateCompatibleDC( hdc );
1965 HBRUSH hBrush, hBrushOld;
1966 HBITMAP hBmp = 0, hBmpOld = 0;
1967 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pStretchBlt->offBmiSrc);
1969 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1970 SetWorldTransform(hdcSrc, &pStretchBlt->xformSrc);
1972 hBrush = CreateSolidBrush(pStretchBlt->crBkColorSrc);
1973 hBrushOld = SelectObject(hdcSrc, hBrush);
1974 PatBlt(hdcSrc, pStretchBlt->rclBounds.left, pStretchBlt->rclBounds.top,
1975 pStretchBlt->rclBounds.right - pStretchBlt->rclBounds.left,
1976 pStretchBlt->rclBounds.bottom - pStretchBlt->rclBounds.top, PATCOPY);
1977 SelectObject(hdcSrc, hBrushOld);
1978 DeleteObject(hBrush);
1980 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1981 (const BYTE *)mr + pStretchBlt->offBitsSrc, pbi, pStretchBlt->iUsageSrc);
1982 hBmpOld = SelectObject(hdcSrc, hBmp);
1984 StretchBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1985 hdcSrc, pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1986 pStretchBlt->dwRop);
1988 SelectObject(hdcSrc, hBmpOld);
1989 DeleteObject(hBmp);
1990 DeleteDC(hdcSrc);
1992 break;
1995 case EMR_ALPHABLEND:
1997 const EMRALPHABLEND *pAlphaBlend = (const EMRALPHABLEND *)mr;
1999 TRACE("EMR_ALPHABLEND: %ld, %ld %ldx%ld -> %ld, %ld %ldx%ld. blendfn %08lx offBitsSrc %ld\n",
2000 pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
2001 pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
2002 pAlphaBlend->dwRop, pAlphaBlend->offBitsSrc);
2004 if(pAlphaBlend->offBmiSrc == 0) {
2005 FIXME("EMR_ALPHABLEND: offBmiSrc == 0\n");
2006 } else {
2007 HDC hdcSrc = NtGdiCreateCompatibleDC( hdc );
2008 HBITMAP hBmp = 0, hBmpOld = 0;
2009 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pAlphaBlend->offBmiSrc);
2010 void *bits;
2012 SetGraphicsMode(hdcSrc, GM_ADVANCED);
2013 SetWorldTransform(hdcSrc, &pAlphaBlend->xformSrc);
2015 hBmp = CreateDIBSection(hdc, pbi, pAlphaBlend->iUsageSrc, &bits, NULL, 0);
2016 memcpy(bits, (const BYTE *)mr + pAlphaBlend->offBitsSrc, pAlphaBlend->cbBitsSrc);
2017 hBmpOld = SelectObject(hdcSrc, hBmp);
2019 GdiAlphaBlend(hdc, pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
2020 hdcSrc, pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
2021 *(BLENDFUNCTION *)&pAlphaBlend->dwRop);
2023 SelectObject(hdcSrc, hBmpOld);
2024 DeleteObject(hBmp);
2025 DeleteDC(hdcSrc);
2027 break;
2030 case EMR_MASKBLT:
2032 const EMRMASKBLT *pMaskBlt = (const EMRMASKBLT *)mr;
2033 HDC hdcSrc = NtGdiCreateCompatibleDC( hdc );
2034 HBRUSH hBrush, hBrushOld;
2035 HBITMAP hBmp, hBmpOld, hBmpMask;
2036 const BITMAPINFO *pbi;
2038 SetGraphicsMode(hdcSrc, GM_ADVANCED);
2039 SetWorldTransform(hdcSrc, &pMaskBlt->xformSrc);
2041 hBrush = CreateSolidBrush(pMaskBlt->crBkColorSrc);
2042 hBrushOld = SelectObject(hdcSrc, hBrush);
2043 PatBlt(hdcSrc, pMaskBlt->rclBounds.left, pMaskBlt->rclBounds.top,
2044 pMaskBlt->rclBounds.right - pMaskBlt->rclBounds.left,
2045 pMaskBlt->rclBounds.bottom - pMaskBlt->rclBounds.top, PATCOPY);
2046 SelectObject(hdcSrc, hBrushOld);
2047 DeleteObject(hBrush);
2049 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiMask);
2050 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
2051 1, 1, NULL);
2052 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
2053 (const BYTE *)mr + pMaskBlt->offBitsMask, pbi, pMaskBlt->iUsageMask);
2055 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiSrc);
2056 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
2057 (const BYTE *)mr + pMaskBlt->offBitsSrc, pbi, pMaskBlt->iUsageSrc);
2058 hBmpOld = SelectObject(hdcSrc, hBmp);
2059 MaskBlt(hdc,
2060 pMaskBlt->xDest,
2061 pMaskBlt->yDest,
2062 pMaskBlt->cxDest,
2063 pMaskBlt->cyDest,
2064 hdcSrc,
2065 pMaskBlt->xSrc,
2066 pMaskBlt->ySrc,
2067 hBmpMask,
2068 pMaskBlt->xMask,
2069 pMaskBlt->yMask,
2070 pMaskBlt->dwRop);
2071 SelectObject(hdcSrc, hBmpOld);
2072 DeleteObject(hBmp);
2073 DeleteObject(hBmpMask);
2074 DeleteDC(hdcSrc);
2075 break;
2078 case EMR_PLGBLT:
2080 const EMRPLGBLT *pPlgBlt = (const EMRPLGBLT *)mr;
2081 HDC hdcSrc = NtGdiCreateCompatibleDC( hdc );
2082 HBRUSH hBrush, hBrushOld;
2083 HBITMAP hBmp, hBmpOld, hBmpMask;
2084 const BITMAPINFO *pbi;
2085 POINT pts[3];
2087 SetGraphicsMode(hdcSrc, GM_ADVANCED);
2088 SetWorldTransform(hdcSrc, &pPlgBlt->xformSrc);
2090 pts[0].x = pPlgBlt->aptlDest[0].x; pts[0].y = pPlgBlt->aptlDest[0].y;
2091 pts[1].x = pPlgBlt->aptlDest[1].x; pts[1].y = pPlgBlt->aptlDest[1].y;
2092 pts[2].x = pPlgBlt->aptlDest[2].x; pts[2].y = pPlgBlt->aptlDest[2].y;
2094 hBrush = CreateSolidBrush(pPlgBlt->crBkColorSrc);
2095 hBrushOld = SelectObject(hdcSrc, hBrush);
2096 PatBlt(hdcSrc, pPlgBlt->rclBounds.left, pPlgBlt->rclBounds.top,
2097 pPlgBlt->rclBounds.right - pPlgBlt->rclBounds.left,
2098 pPlgBlt->rclBounds.bottom - pPlgBlt->rclBounds.top, PATCOPY);
2099 SelectObject(hdcSrc, hBrushOld);
2100 DeleteObject(hBrush);
2102 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiMask);
2103 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
2104 1, 1, NULL);
2105 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
2106 (const BYTE *)mr + pPlgBlt->offBitsMask, pbi, pPlgBlt->iUsageMask);
2108 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiSrc);
2109 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
2110 (const BYTE *)mr + pPlgBlt->offBitsSrc, pbi, pPlgBlt->iUsageSrc);
2111 hBmpOld = SelectObject(hdcSrc, hBmp);
2112 PlgBlt(hdc,
2113 pts,
2114 hdcSrc,
2115 pPlgBlt->xSrc,
2116 pPlgBlt->ySrc,
2117 pPlgBlt->cxSrc,
2118 pPlgBlt->cySrc,
2119 hBmpMask,
2120 pPlgBlt->xMask,
2121 pPlgBlt->yMask);
2122 SelectObject(hdcSrc, hBmpOld);
2123 DeleteObject(hBmp);
2124 DeleteObject(hBmpMask);
2125 DeleteDC(hdcSrc);
2126 break;
2129 case EMR_SETDIBITSTODEVICE:
2131 const EMRSETDIBITSTODEVICE *pSetDIBitsToDevice = (const EMRSETDIBITSTODEVICE *)mr;
2133 SetDIBitsToDevice(hdc,
2134 pSetDIBitsToDevice->xDest,
2135 pSetDIBitsToDevice->yDest,
2136 pSetDIBitsToDevice->cxSrc,
2137 pSetDIBitsToDevice->cySrc,
2138 pSetDIBitsToDevice->xSrc,
2139 pSetDIBitsToDevice->ySrc,
2140 pSetDIBitsToDevice->iStartScan,
2141 pSetDIBitsToDevice->cScans,
2142 (const BYTE *)mr + pSetDIBitsToDevice->offBitsSrc,
2143 (const BITMAPINFO *)((const BYTE *)mr + pSetDIBitsToDevice->offBmiSrc),
2144 pSetDIBitsToDevice->iUsageSrc);
2145 break;
2148 case EMR_POLYTEXTOUTA:
2150 const EMRPOLYTEXTOUTA *pPolyTextOutA = (const EMRPOLYTEXTOUTA *)mr;
2151 POLYTEXTA *polytextA = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutA->cStrings * sizeof(POLYTEXTA));
2152 LONG i;
2153 XFORM xform, xformOld;
2154 int gModeOld;
2156 gModeOld = SetGraphicsMode(hdc, pPolyTextOutA->iGraphicsMode);
2157 GetWorldTransform(hdc, &xformOld);
2159 xform.eM11 = pPolyTextOutA->exScale;
2160 xform.eM12 = 0.0;
2161 xform.eM21 = 0.0;
2162 xform.eM22 = pPolyTextOutA->eyScale;
2163 xform.eDx = 0.0;
2164 xform.eDy = 0.0;
2165 SetWorldTransform(hdc, &xform);
2167 /* Set up POLYTEXTA structures */
2168 for(i = 0; i < pPolyTextOutA->cStrings; i++)
2170 polytextA[i].x = pPolyTextOutA->aemrtext[i].ptlReference.x;
2171 polytextA[i].y = pPolyTextOutA->aemrtext[i].ptlReference.y;
2172 polytextA[i].n = pPolyTextOutA->aemrtext[i].nChars;
2173 polytextA[i].lpstr = (LPCSTR)((const BYTE *)mr + pPolyTextOutA->aemrtext[i].offString);
2174 polytextA[i].uiFlags = pPolyTextOutA->aemrtext[i].fOptions;
2175 polytextA[i].rcl.left = pPolyTextOutA->aemrtext[i].rcl.left;
2176 polytextA[i].rcl.right = pPolyTextOutA->aemrtext[i].rcl.right;
2177 polytextA[i].rcl.top = pPolyTextOutA->aemrtext[i].rcl.top;
2178 polytextA[i].rcl.bottom = pPolyTextOutA->aemrtext[i].rcl.bottom;
2179 polytextA[i].pdx = (int *)((BYTE *)mr + pPolyTextOutA->aemrtext[i].offDx);
2181 PolyTextOutA(hdc, polytextA, pPolyTextOutA->cStrings);
2182 HeapFree(GetProcessHeap(), 0, polytextA);
2184 SetWorldTransform(hdc, &xformOld);
2185 SetGraphicsMode(hdc, gModeOld);
2186 break;
2189 case EMR_POLYTEXTOUTW:
2191 const EMRPOLYTEXTOUTW *pPolyTextOutW = (const EMRPOLYTEXTOUTW *)mr;
2192 POLYTEXTW *polytextW = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutW->cStrings * sizeof(POLYTEXTW));
2193 LONG i;
2194 XFORM xform, xformOld;
2195 int gModeOld;
2197 gModeOld = SetGraphicsMode(hdc, pPolyTextOutW->iGraphicsMode);
2198 GetWorldTransform(hdc, &xformOld);
2200 xform.eM11 = pPolyTextOutW->exScale;
2201 xform.eM12 = 0.0;
2202 xform.eM21 = 0.0;
2203 xform.eM22 = pPolyTextOutW->eyScale;
2204 xform.eDx = 0.0;
2205 xform.eDy = 0.0;
2206 SetWorldTransform(hdc, &xform);
2208 /* Set up POLYTEXTW structures */
2209 for(i = 0; i < pPolyTextOutW->cStrings; i++)
2211 polytextW[i].x = pPolyTextOutW->aemrtext[i].ptlReference.x;
2212 polytextW[i].y = pPolyTextOutW->aemrtext[i].ptlReference.y;
2213 polytextW[i].n = pPolyTextOutW->aemrtext[i].nChars;
2214 polytextW[i].lpstr = (LPCWSTR)((const BYTE *)mr + pPolyTextOutW->aemrtext[i].offString);
2215 polytextW[i].uiFlags = pPolyTextOutW->aemrtext[i].fOptions;
2216 polytextW[i].rcl.left = pPolyTextOutW->aemrtext[i].rcl.left;
2217 polytextW[i].rcl.right = pPolyTextOutW->aemrtext[i].rcl.right;
2218 polytextW[i].rcl.top = pPolyTextOutW->aemrtext[i].rcl.top;
2219 polytextW[i].rcl.bottom = pPolyTextOutW->aemrtext[i].rcl.bottom;
2220 polytextW[i].pdx = (int *)((BYTE *)mr + pPolyTextOutW->aemrtext[i].offDx);
2222 PolyTextOutW(hdc, polytextW, pPolyTextOutW->cStrings);
2223 HeapFree(GetProcessHeap(), 0, polytextW);
2225 SetWorldTransform(hdc, &xformOld);
2226 SetGraphicsMode(hdc, gModeOld);
2227 break;
2230 case EMR_FILLRGN:
2232 const EMRFILLRGN *pFillRgn = (const EMRFILLRGN *)mr;
2233 HRGN hRgn = ExtCreateRegion(NULL, pFillRgn->cbRgnData, (const RGNDATA *)pFillRgn->RgnData);
2234 FillRgn(hdc,
2235 hRgn,
2236 (handletable->objectHandle)[pFillRgn->ihBrush]);
2237 DeleteObject(hRgn);
2238 break;
2241 case EMR_FRAMERGN:
2243 const EMRFRAMERGN *pFrameRgn = (const EMRFRAMERGN *)mr;
2244 HRGN hRgn = ExtCreateRegion(NULL, pFrameRgn->cbRgnData, (const RGNDATA *)pFrameRgn->RgnData);
2245 FrameRgn(hdc,
2246 hRgn,
2247 (handletable->objectHandle)[pFrameRgn->ihBrush],
2248 pFrameRgn->szlStroke.cx,
2249 pFrameRgn->szlStroke.cy);
2250 DeleteObject(hRgn);
2251 break;
2254 case EMR_INVERTRGN:
2256 const EMRINVERTRGN *pInvertRgn = (const EMRINVERTRGN *)mr;
2257 HRGN hRgn = ExtCreateRegion(NULL, pInvertRgn->cbRgnData, (const RGNDATA *)pInvertRgn->RgnData);
2258 InvertRgn(hdc, hRgn);
2259 DeleteObject(hRgn);
2260 break;
2263 case EMR_PAINTRGN:
2265 const EMRPAINTRGN *pPaintRgn = (const EMRPAINTRGN *)mr;
2266 HRGN hRgn = ExtCreateRegion(NULL, pPaintRgn->cbRgnData, (const RGNDATA *)pPaintRgn->RgnData);
2267 PaintRgn(hdc, hRgn);
2268 DeleteObject(hRgn);
2269 break;
2272 case EMR_SETTEXTJUSTIFICATION:
2274 const EMRSETTEXTJUSTIFICATION *pSetTextJust = (const EMRSETTEXTJUSTIFICATION *)mr;
2275 SetTextJustification(hdc, pSetTextJust->nBreakExtra, pSetTextJust->nBreakCount);
2276 break;
2279 case EMR_SETLAYOUT:
2281 const EMRSETLAYOUT *pSetLayout = (const EMRSETLAYOUT *)mr;
2282 SetLayout(hdc, pSetLayout->iMode);
2283 break;
2286 case EMR_GRADIENTFILL:
2288 EMRGRADIENTFILL *grad = (EMRGRADIENTFILL *)mr;
2289 GdiGradientFill( hdc, grad->Ver, grad->nVer, grad->Ver + grad->nVer,
2290 grad->nTri, grad->ulMode );
2291 break;
2294 case EMR_GLSRECORD:
2295 case EMR_GLSBOUNDEDRECORD:
2296 case EMR_DRAWESCAPE:
2297 case EMR_EXTESCAPE:
2298 case EMR_STARTDOC:
2299 case EMR_SMALLTEXTOUT:
2300 case EMR_FORCEUFIMAPPING:
2301 case EMR_NAMEDESCAPE:
2302 case EMR_COLORCORRECTPALETTE:
2303 case EMR_SETICMPROFILEA:
2304 case EMR_SETICMPROFILEW:
2305 case EMR_TRANSPARENTBLT:
2306 case EMR_SETLINKEDUFI:
2307 case EMR_COLORMATCHTOTARGETW:
2308 case EMR_CREATECOLORSPACEW:
2310 default:
2311 /* From docs: If PlayEnhMetaFileRecord doesn't recognize a
2312 record then ignore and return TRUE. */
2313 FIXME("type %d is unimplemented\n", type);
2314 break;
2316 tmprc.left = tmprc.top = 0;
2317 tmprc.right = tmprc.bottom = 1000;
2318 LPtoDP(hdc, (POINT*)&tmprc, 2);
2319 TRACE("L:0,0 - 1000,1000 -> D:%s\n", wine_dbgstr_rect(&tmprc));
2321 return TRUE;
2325 /*****************************************************************************
2327 * EnumEnhMetaFile (GDI32.@)
2329 * Walk an enhanced metafile, calling a user-specified function _EnhMetaFunc_
2330 * for each
2331 * record. Returns when either every record has been used or
2332 * when _EnhMetaFunc_ returns FALSE.
2335 * RETURNS
2336 * TRUE if every record is used, FALSE if any invocation of _EnhMetaFunc_
2337 * returns FALSE.
2339 * BUGS
2340 * Ignores rect.
2342 * NOTES
2343 * This function behaves differently in Win9x and WinNT.
2345 * In WinNT, the DC's world transform is updated as the EMF changes
2346 * the Window/Viewport Extent and Origin or its world transform.
2347 * The actual Window/Viewport Extent and Origin are left untouched.
2349 * In Win9x, the DC is left untouched, and PlayEnhMetaFileRecord
2350 * updates the scaling itself but only just before a record that
2351 * writes anything to the DC.
2353 * I'm not sure where the data (enum_emh_data) is stored in either
2354 * version. For this implementation, it is stored before the handle
2355 * table, but it could be stored in the DC, in the EMF handle or in
2356 * TLS.
2357 * MJM 5 Oct 2002
2359 BOOL WINAPI EnumEnhMetaFile(
2360 HDC hdc, /* [in] device context to pass to _EnhMetaFunc_ */
2361 HENHMETAFILE hmf, /* [in] EMF to walk */
2362 ENHMFENUMPROC callback, /* [in] callback function */
2363 LPVOID data, /* [in] optional data for callback function */
2364 const RECT *lpRect /* [in] bounding rectangle for rendered metafile */
2367 BOOL ret;
2368 ENHMETAHEADER *emh;
2369 ENHMETARECORD *emr;
2370 DWORD offset;
2371 UINT i;
2372 HANDLETABLE *ht;
2373 INT savedMode = 0;
2374 XFORM savedXform;
2375 HPEN hPen = NULL;
2376 HBRUSH hBrush = NULL;
2377 HFONT hFont = NULL;
2378 HRGN hRgn = NULL;
2379 enum_emh_data *info;
2380 SIZE vp_size, win_size;
2381 POINT vp_org, win_org;
2382 INT mapMode = MM_TEXT, old_align = 0, old_rop2 = 0, old_arcdir = 0, old_polyfill = 0, old_stretchblt = 0;
2383 COLORREF old_text_color = 0, old_bk_color = 0;
2385 if(!lpRect && hdc)
2387 SetLastError(ERROR_INVALID_PARAMETER);
2388 return FALSE;
2391 emh = EMF_GetEnhMetaHeader(hmf);
2392 if(!emh) {
2393 SetLastError(ERROR_INVALID_HANDLE);
2394 return FALSE;
2397 info = HeapAlloc( GetProcessHeap(), 0,
2398 sizeof (enum_emh_data) + sizeof(HANDLETABLE) * emh->nHandles );
2399 if(!info)
2401 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2402 return FALSE;
2404 info->state.mode = MM_TEXT;
2405 info->state.wndOrgX = 0;
2406 info->state.wndOrgY = 0;
2407 info->state.wndExtX = 1;
2408 info->state.wndExtY = 1;
2409 info->state.vportOrgX = 0;
2410 info->state.vportOrgY = 0;
2411 info->state.vportExtX = 1;
2412 info->state.vportExtY = 1;
2413 info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
2414 info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
2415 info->state.world_transform.eDx = info->state.world_transform.eDy = 0;
2417 info->state.next = NULL;
2418 info->save_level = 0;
2419 info->saved_state = NULL;
2420 info->init_transform = info->state.world_transform;
2422 ht = (HANDLETABLE*) &info[1];
2423 ht->objectHandle[0] = hmf;
2424 for(i = 1; i < emh->nHandles; i++)
2425 ht->objectHandle[i] = NULL;
2427 if (hdc && !is_meta_dc( hdc ))
2429 savedMode = SetGraphicsMode(hdc, GM_ADVANCED);
2430 GetWorldTransform(hdc, &savedXform);
2431 GetViewportExtEx(hdc, &vp_size);
2432 GetWindowExtEx(hdc, &win_size);
2433 GetViewportOrgEx(hdc, &vp_org);
2434 GetWindowOrgEx(hdc, &win_org);
2435 mapMode = GetMapMode(hdc);
2437 /* save DC */
2438 hPen = GetCurrentObject(hdc, OBJ_PEN);
2439 hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
2440 hFont = GetCurrentObject(hdc, OBJ_FONT);
2442 hRgn = NtGdiCreateRectRgn(0, 0, 0, 0);
2443 if (!GetClipRgn(hdc, hRgn))
2445 DeleteObject(hRgn);
2446 hRgn = 0;
2449 old_text_color = SetTextColor(hdc, RGB(0,0,0));
2450 old_bk_color = SetBkColor(hdc, RGB(0xff, 0xff, 0xff));
2451 old_align = SetTextAlign(hdc, 0);
2452 old_rop2 = SetROP2(hdc, R2_COPYPEN);
2453 old_arcdir = SetArcDirection(hdc, AD_COUNTERCLOCKWISE);
2454 old_polyfill = SetPolyFillMode(hdc, ALTERNATE);
2455 old_stretchblt = SetStretchBltMode(hdc, BLACKONWHITE);
2457 if (!IS_WIN9X())
2459 /* WinNT combines the vp/win ext/org info into a transform */
2460 double xscale, yscale;
2461 xscale = (double)vp_size.cx / (double)win_size.cx;
2462 yscale = (double)vp_size.cy / (double)win_size.cy;
2463 info->init_transform.eM11 = xscale;
2464 info->init_transform.eM12 = 0.0;
2465 info->init_transform.eM21 = 0.0;
2466 info->init_transform.eM22 = yscale;
2467 info->init_transform.eDx = (double)vp_org.x - xscale * (double)win_org.x;
2468 info->init_transform.eDy = (double)vp_org.y - yscale * (double)win_org.y;
2470 CombineTransform(&info->init_transform, &savedXform, &info->init_transform);
2473 if ( lpRect && WIDTH(emh->rclFrame) && HEIGHT(emh->rclFrame) )
2475 double xSrcPixSize, ySrcPixSize, xscale, yscale;
2476 XFORM xform;
2478 TRACE("rect: %s. rclFrame: (%ld,%ld)-(%ld,%ld)\n", wine_dbgstr_rect(lpRect),
2479 emh->rclFrame.left, emh->rclFrame.top, emh->rclFrame.right,
2480 emh->rclFrame.bottom);
2482 xSrcPixSize = (double) emh->szlMillimeters.cx / emh->szlDevice.cx;
2483 ySrcPixSize = (double) emh->szlMillimeters.cy / emh->szlDevice.cy;
2484 xscale = (double) WIDTH(*lpRect) * 100.0 /
2485 WIDTH(emh->rclFrame) * xSrcPixSize;
2486 yscale = (double) HEIGHT(*lpRect) * 100.0 /
2487 HEIGHT(emh->rclFrame) * ySrcPixSize;
2488 TRACE("xscale = %f, yscale = %f\n", xscale, yscale);
2490 xform.eM11 = xscale;
2491 xform.eM12 = 0;
2492 xform.eM21 = 0;
2493 xform.eM22 = yscale;
2494 xform.eDx = (double) lpRect->left - (double) WIDTH(*lpRect) / WIDTH(emh->rclFrame) * emh->rclFrame.left;
2495 xform.eDy = (double) lpRect->top - (double) HEIGHT(*lpRect) / HEIGHT(emh->rclFrame) * emh->rclFrame.top;
2497 CombineTransform(&info->init_transform, &xform, &info->init_transform);
2500 /* WinNT resets the current vp/win org/ext */
2501 if (!IS_WIN9X())
2503 SetMapMode(hdc, MM_TEXT);
2504 SetWindowOrgEx(hdc, 0, 0, NULL);
2505 SetViewportOrgEx(hdc, 0, 0, NULL);
2506 EMF_Update_MF_Xform(hdc, info);
2510 ret = TRUE;
2511 offset = 0;
2512 while(ret && offset < emh->nBytes)
2514 emr = (ENHMETARECORD *)((char *)emh + offset);
2516 if (offset + 8 > emh->nBytes ||
2517 offset > offset + emr->nSize ||
2518 offset + emr->nSize > emh->nBytes)
2520 WARN("record truncated\n");
2521 break;
2524 /* In Win9x mode we update the xform if the record will produce output */
2525 if (hdc && IS_WIN9X() && emr_produces_output(emr->iType))
2526 EMF_Update_MF_Xform(hdc, info);
2528 TRACE("Calling EnumFunc with record %s, size %ld\n", get_emr_name(emr->iType), emr->nSize);
2529 ret = (*callback)(hdc, ht, emr, emh->nHandles, (LPARAM)data);
2530 offset += emr->nSize;
2533 if (hdc && !is_meta_dc( hdc ))
2535 SetStretchBltMode(hdc, old_stretchblt);
2536 SetPolyFillMode(hdc, old_polyfill);
2537 SetArcDirection(hdc, old_arcdir);
2538 SetROP2(hdc, old_rop2);
2539 SetTextAlign(hdc, old_align);
2540 SetBkColor(hdc, old_bk_color);
2541 SetTextColor(hdc, old_text_color);
2543 /* restore DC */
2544 SelectObject(hdc, hBrush);
2545 SelectObject(hdc, hPen);
2546 SelectObject(hdc, hFont);
2547 ExtSelectClipRgn(hdc, hRgn, RGN_COPY);
2548 DeleteObject(hRgn);
2550 SetWorldTransform(hdc, &savedXform);
2551 if (savedMode)
2552 SetGraphicsMode(hdc, savedMode);
2553 SetMapMode(hdc, mapMode);
2554 SetWindowOrgEx(hdc, win_org.x, win_org.y, NULL);
2555 SetWindowExtEx(hdc, win_size.cx, win_size.cy, NULL);
2556 SetViewportOrgEx(hdc, vp_org.x, vp_org.y, NULL);
2557 SetViewportExtEx(hdc, vp_size.cx, vp_size.cy, NULL);
2560 for(i = 1; i < emh->nHandles; i++) /* Don't delete element 0 (hmf) */
2561 if( (ht->objectHandle)[i] )
2562 DeleteObject( (ht->objectHandle)[i] );
2564 while (info->saved_state)
2566 EMF_dc_state *state = info->saved_state;
2567 info->saved_state = info->saved_state->next;
2568 HeapFree( GetProcessHeap(), 0, state );
2570 HeapFree( GetProcessHeap(), 0, info );
2571 return ret;
2574 static INT CALLBACK EMF_PlayEnhMetaFileCallback(HDC hdc, HANDLETABLE *ht,
2575 const ENHMETARECORD *emr,
2576 INT handles, LPARAM data)
2578 return PlayEnhMetaFileRecord(hdc, ht, emr, handles);
2581 /**************************************************************************
2582 * PlayEnhMetaFile (GDI32.@)
2584 * Renders an enhanced metafile into a specified rectangle *lpRect
2585 * in device context hdc.
2587 * RETURNS
2588 * Success: TRUE
2589 * Failure: FALSE
2591 BOOL WINAPI PlayEnhMetaFile(
2592 HDC hdc, /* [in] DC to render into */
2593 HENHMETAFILE hmf, /* [in] metafile to render */
2594 const RECT *lpRect /* [in] rectangle to place metafile inside */
2597 return EnumEnhMetaFile(hdc, hmf, EMF_PlayEnhMetaFileCallback, NULL,
2598 lpRect);
2601 /*****************************************************************************
2602 * DeleteEnhMetaFile (GDI32.@)
2604 * Deletes an enhanced metafile and frees the associated storage.
2606 BOOL WINAPI DeleteEnhMetaFile(HENHMETAFILE hmf)
2608 return EMF_Delete_HENHMETAFILE( hmf );
2611 /*****************************************************************************
2612 * CopyEnhMetaFileA (GDI32.@)
2614 * Duplicate an enhanced metafile.
2618 HENHMETAFILE WINAPI CopyEnhMetaFileA(
2619 HENHMETAFILE hmfSrc,
2620 LPCSTR file)
2622 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2623 HENHMETAFILE hmfDst;
2625 if(!emrSrc) return FALSE;
2626 if (!file) {
2627 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2628 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2629 hmfDst = EMF_Create_HENHMETAFILE( emrDst, emrSrc->nBytes, FALSE );
2630 if (!hmfDst)
2631 HeapFree( GetProcessHeap(), 0, emrDst );
2632 } else {
2633 HANDLE hFile;
2634 DWORD w;
2635 hFile = CreateFileA( file, GENERIC_WRITE | GENERIC_READ, 0,
2636 NULL, CREATE_ALWAYS, 0, 0);
2637 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2638 CloseHandle( hFile );
2639 /* Reopen file for reading only, so that apps can share
2640 read access to the file while hmf is still valid */
2641 hFile = CreateFileA( file, GENERIC_READ, FILE_SHARE_READ,
2642 NULL, OPEN_EXISTING, 0, 0);
2643 if(hFile == INVALID_HANDLE_VALUE) {
2644 ERR("Can't reopen emf for reading\n");
2645 return 0;
2647 hmfDst = EMF_GetEnhMetaFile( hFile );
2648 CloseHandle( hFile );
2650 return hmfDst;
2653 /*****************************************************************************
2654 * CopyEnhMetaFileW (GDI32.@)
2656 * See CopyEnhMetaFileA.
2660 HENHMETAFILE WINAPI CopyEnhMetaFileW(
2661 HENHMETAFILE hmfSrc,
2662 LPCWSTR file)
2664 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2665 HENHMETAFILE hmfDst;
2667 if(!emrSrc) return FALSE;
2668 if (!file) {
2669 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2670 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2671 hmfDst = EMF_Create_HENHMETAFILE( emrDst, emrSrc->nBytes, FALSE );
2672 if (!hmfDst)
2673 HeapFree( GetProcessHeap(), 0, emrDst );
2674 } else {
2675 HANDLE hFile;
2676 DWORD w;
2677 hFile = CreateFileW( file, GENERIC_WRITE | GENERIC_READ, 0,
2678 NULL, CREATE_ALWAYS, 0, 0);
2679 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2680 CloseHandle( hFile );
2681 /* Reopen file for reading only, so that apps can share
2682 read access to the file while hmf is still valid */
2683 hFile = CreateFileW( file, GENERIC_READ, FILE_SHARE_READ,
2684 NULL, OPEN_EXISTING, 0, 0);
2685 if(hFile == INVALID_HANDLE_VALUE) {
2686 ERR("Can't reopen emf for reading\n");
2687 return 0;
2689 hmfDst = EMF_GetEnhMetaFile( hFile );
2690 CloseHandle( hFile );
2692 return hmfDst;
2696 /* Struct to be used to be passed in the LPVOID parameter for cbEnhPaletteCopy */
2697 typedef struct tagEMF_PaletteCopy
2699 UINT cEntries;
2700 LPPALETTEENTRY lpPe;
2701 } EMF_PaletteCopy;
2703 /***************************************************************
2704 * Find the EMR_EOF record and then use it to find the
2705 * palette entries for this enhanced metafile.
2706 * The lpData is actually a pointer to an EMF_PaletteCopy struct
2707 * which contains the max number of elements to copy and where
2708 * to copy them to.
2710 * NOTE: To be used by GetEnhMetaFilePaletteEntries only!
2712 static INT CALLBACK cbEnhPaletteCopy( HDC a,
2713 HANDLETABLE *b,
2714 const ENHMETARECORD *lpEMR,
2715 INT c,
2716 LPARAM lpData )
2719 if ( lpEMR->iType == EMR_EOF )
2721 const EMREOF *lpEof = (const EMREOF *)lpEMR;
2722 EMF_PaletteCopy* info = (EMF_PaletteCopy*)lpData;
2723 DWORD dwNumPalToCopy = min( lpEof->nPalEntries, info->cEntries );
2725 TRACE( "copying 0x%08lx palettes\n", dwNumPalToCopy );
2727 memcpy( info->lpPe, (LPCSTR)lpEof + lpEof->offPalEntries,
2728 sizeof( *(info->lpPe) ) * dwNumPalToCopy );
2730 /* Update the passed data as a return code */
2731 info->lpPe = NULL; /* Palettes were copied! */
2732 info->cEntries = dwNumPalToCopy;
2734 return FALSE; /* That's all we need */
2737 return TRUE;
2740 /*****************************************************************************
2741 * GetEnhMetaFilePaletteEntries (GDI32.@)
2743 * Copy the palette and report size
2745 * BUGS: Error codes (SetLastError) are not set on failures
2747 UINT WINAPI GetEnhMetaFilePaletteEntries( HENHMETAFILE hEmf,
2748 UINT cEntries,
2749 LPPALETTEENTRY lpPe )
2751 ENHMETAHEADER* enhHeader = EMF_GetEnhMetaHeader( hEmf );
2752 EMF_PaletteCopy infoForCallBack;
2754 TRACE( "(%p,%d,%p)\n", hEmf, cEntries, lpPe );
2756 if (!enhHeader) return 0;
2758 /* First check if there are any palettes associated with
2759 this metafile. */
2760 if ( enhHeader->nPalEntries == 0 ) return 0;
2762 /* Is the user requesting the number of palettes? */
2763 if ( lpPe == NULL ) return enhHeader->nPalEntries;
2765 /* Copy cEntries worth of PALETTEENTRY structs into the buffer */
2766 infoForCallBack.cEntries = cEntries;
2767 infoForCallBack.lpPe = lpPe;
2769 if ( !EnumEnhMetaFile( 0, hEmf, cbEnhPaletteCopy,
2770 &infoForCallBack, 0 ) )
2771 return GDI_ERROR;
2773 /* Verify that the callback executed correctly */
2774 if ( infoForCallBack.lpPe != NULL )
2776 /* Callback proc had error! */
2777 ERR( "cbEnhPaletteCopy didn't execute correctly\n" );
2778 return GDI_ERROR;
2781 return infoForCallBack.cEntries;
2784 /******************************************************************
2785 * extract_emf_from_comment
2787 * If the WMF was created by GetWinMetaFileBits, then extract the
2788 * original EMF that is stored in MFCOMMENT chunks.
2790 static HENHMETAFILE extract_emf_from_comment( const BYTE *buf, UINT mf_size )
2792 METAHEADER *mh = (METAHEADER *)buf;
2793 METARECORD *mr;
2794 emf_in_wmf_comment *chunk;
2795 WORD checksum = 0;
2796 DWORD size = 0, remaining, chunks;
2797 BYTE *emf_bits = NULL, *ptr;
2798 UINT offset;
2799 HENHMETAFILE emf = NULL;
2801 if (mf_size < sizeof(*mh)) return NULL;
2803 for (offset = mh->mtHeaderSize * 2; offset < mf_size; offset += (mr->rdSize * 2))
2805 mr = (METARECORD *)((char *)mh + offset);
2806 chunk = (emf_in_wmf_comment *)(mr->rdParm + 2);
2808 if (mr->rdFunction != META_ESCAPE || mr->rdParm[0] != MFCOMMENT) goto done;
2809 if (chunk->comment_id != WMFC_MAGIC) goto done;
2811 if (!emf_bits)
2813 size = remaining = chunk->emf_size;
2814 chunks = chunk->num_chunks;
2815 emf_bits = ptr = HeapAlloc( GetProcessHeap(), 0, size );
2816 if (!emf_bits) goto done;
2818 if (chunk->chunk_size > remaining) goto done;
2819 remaining -= chunk->chunk_size;
2820 if (chunk->remaining_size != remaining) goto done;
2821 memcpy( ptr, chunk->emf_data, chunk->chunk_size );
2822 ptr += chunk->chunk_size;
2823 if (--chunks == 0) break;
2826 for (offset = 0; offset < mf_size / 2; offset++)
2827 checksum += *((WORD *)buf + offset);
2828 if (checksum) goto done;
2830 emf = SetEnhMetaFileBits( size, emf_bits );
2832 done:
2833 HeapFree( GetProcessHeap(), 0, emf_bits );
2834 return emf;
2837 typedef struct wmf_in_emf_comment
2839 DWORD ident;
2840 DWORD iComment;
2841 DWORD nVersion;
2842 DWORD nChecksum;
2843 DWORD fFlags;
2844 DWORD cbWinMetaFile;
2845 } wmf_in_emf_comment;
2847 /******************************************************************
2848 * SetWinMetaFileBits (GDI32.@)
2850 * Translate from old style to new style.
2853 HENHMETAFILE WINAPI SetWinMetaFileBits(UINT cbBuffer, const BYTE *lpbBuffer, HDC hdcRef,
2854 const METAFILEPICT *lpmfp)
2856 HMETAFILE hmf = NULL;
2857 HENHMETAFILE ret = NULL;
2858 HDC hdc = NULL, hdcdisp = NULL;
2859 RECT rc, *prcFrame = NULL;
2860 LONG mm, xExt, yExt;
2861 INT horzsize, vertsize, horzres, vertres;
2863 TRACE("(%d, %p, %p, %p)\n", cbBuffer, lpbBuffer, hdcRef, lpmfp);
2865 hmf = SetMetaFileBitsEx(cbBuffer, lpbBuffer);
2866 if(!hmf)
2868 WARN("SetMetaFileBitsEx failed\n");
2869 return NULL;
2872 ret = extract_emf_from_comment( lpbBuffer, cbBuffer );
2873 if (ret) return ret;
2875 if(!hdcRef)
2876 hdcRef = hdcdisp = CreateDCW(L"DISPLAY", NULL, NULL, NULL);
2878 if (lpmfp)
2880 TRACE("mm = %ld %ldx%ld\n", lpmfp->mm, lpmfp->xExt, lpmfp->yExt);
2882 mm = lpmfp->mm;
2883 xExt = lpmfp->xExt;
2884 yExt = lpmfp->yExt;
2886 else
2888 TRACE("lpmfp == NULL\n");
2890 /* Use the whole device surface */
2891 mm = MM_ANISOTROPIC;
2892 xExt = 0;
2893 yExt = 0;
2896 if (mm == MM_ISOTROPIC || mm == MM_ANISOTROPIC)
2898 if (xExt < 0 || yExt < 0)
2900 /* Use the whole device surface */
2901 xExt = 0;
2902 yExt = 0;
2905 /* Use the x and y extents as the frame box */
2906 if (xExt && yExt)
2908 rc.left = rc.top = 0;
2909 rc.right = xExt;
2910 rc.bottom = yExt;
2911 prcFrame = &rc;
2915 if(!(hdc = CreateEnhMetaFileW(hdcRef, NULL, prcFrame, NULL)))
2917 ERR("CreateEnhMetaFile failed\n");
2918 goto end;
2922 * Write the original METAFILE into the enhanced metafile.
2923 * It is encapsulated in a GDICOMMENT_WINDOWS_METAFILE record.
2925 if (mm != MM_TEXT)
2927 wmf_in_emf_comment *mfcomment;
2928 UINT mfcomment_size;
2930 mfcomment_size = sizeof (*mfcomment) + cbBuffer;
2931 mfcomment = HeapAlloc(GetProcessHeap(), 0, mfcomment_size);
2932 if (mfcomment)
2934 mfcomment->ident = GDICOMMENT_IDENTIFIER;
2935 mfcomment->iComment = GDICOMMENT_WINDOWS_METAFILE;
2936 mfcomment->nVersion = 0x00000300;
2937 mfcomment->nChecksum = 0; /* FIXME */
2938 mfcomment->fFlags = 0;
2939 mfcomment->cbWinMetaFile = cbBuffer;
2940 memcpy(&mfcomment[1], lpbBuffer, cbBuffer);
2941 GdiComment(hdc, mfcomment_size, (BYTE*) mfcomment);
2942 HeapFree(GetProcessHeap(), 0, mfcomment);
2944 SetMapMode(hdc, mm);
2948 horzsize = GetDeviceCaps(hdcRef, HORZSIZE);
2949 vertsize = GetDeviceCaps(hdcRef, VERTSIZE);
2950 horzres = GetDeviceCaps(hdcRef, HORZRES);
2951 vertres = GetDeviceCaps(hdcRef, VERTRES);
2953 if (!xExt || !yExt)
2955 /* Use the whole device surface */
2956 xExt = horzres;
2957 yExt = vertres;
2959 else
2961 xExt = MulDiv(xExt, horzres, 100 * horzsize);
2962 yExt = MulDiv(yExt, vertres, 100 * vertsize);
2965 /* set the initial viewport:window ratio as 1:1 */
2966 SetViewportExtEx(hdc, xExt, yExt, NULL);
2967 SetWindowExtEx(hdc, xExt, yExt, NULL);
2969 PlayMetaFile(hdc, hmf);
2971 ret = CloseEnhMetaFile(hdc);
2972 end:
2973 if (hdcdisp) DeleteDC(hdcdisp);
2974 DeleteMetaFile(hmf);
2975 return ret;