ddraw/tests: Add another invalid arguments test for surface QI.
[wine.git] / dlls / gdi32 / enhmetafile.c
blob111fa436bdd308a80b5738f89df6b3b7dd25274a
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 "config.h"
35 #include "wine/port.h"
37 #include <stdarg.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <assert.h>
41 #include "windef.h"
42 #include "winbase.h"
43 #include "wingdi.h"
44 #include "winnls.h"
45 #include "winerror.h"
46 #include "gdi_private.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
51 typedef struct
53 ENHMETAHEADER *emh;
54 BOOL on_disk; /* true if metafile is on disk */
55 } ENHMETAFILEOBJ;
57 static const struct emr_name {
58 DWORD type;
59 const char *name;
60 } emr_names[] = {
61 #define X(p) {p, #p}
62 X(EMR_HEADER),
63 X(EMR_POLYBEZIER),
64 X(EMR_POLYGON),
65 X(EMR_POLYLINE),
66 X(EMR_POLYBEZIERTO),
67 X(EMR_POLYLINETO),
68 X(EMR_POLYPOLYLINE),
69 X(EMR_POLYPOLYGON),
70 X(EMR_SETWINDOWEXTEX),
71 X(EMR_SETWINDOWORGEX),
72 X(EMR_SETVIEWPORTEXTEX),
73 X(EMR_SETVIEWPORTORGEX),
74 X(EMR_SETBRUSHORGEX),
75 X(EMR_EOF),
76 X(EMR_SETPIXELV),
77 X(EMR_SETMAPPERFLAGS),
78 X(EMR_SETMAPMODE),
79 X(EMR_SETBKMODE),
80 X(EMR_SETPOLYFILLMODE),
81 X(EMR_SETROP2),
82 X(EMR_SETSTRETCHBLTMODE),
83 X(EMR_SETTEXTALIGN),
84 X(EMR_SETCOLORADJUSTMENT),
85 X(EMR_SETTEXTCOLOR),
86 X(EMR_SETBKCOLOR),
87 X(EMR_OFFSETCLIPRGN),
88 X(EMR_MOVETOEX),
89 X(EMR_SETMETARGN),
90 X(EMR_EXCLUDECLIPRECT),
91 X(EMR_INTERSECTCLIPRECT),
92 X(EMR_SCALEVIEWPORTEXTEX),
93 X(EMR_SCALEWINDOWEXTEX),
94 X(EMR_SAVEDC),
95 X(EMR_RESTOREDC),
96 X(EMR_SETWORLDTRANSFORM),
97 X(EMR_MODIFYWORLDTRANSFORM),
98 X(EMR_SELECTOBJECT),
99 X(EMR_CREATEPEN),
100 X(EMR_CREATEBRUSHINDIRECT),
101 X(EMR_DELETEOBJECT),
102 X(EMR_ANGLEARC),
103 X(EMR_ELLIPSE),
104 X(EMR_RECTANGLE),
105 X(EMR_ROUNDRECT),
106 X(EMR_ARC),
107 X(EMR_CHORD),
108 X(EMR_PIE),
109 X(EMR_SELECTPALETTE),
110 X(EMR_CREATEPALETTE),
111 X(EMR_SETPALETTEENTRIES),
112 X(EMR_RESIZEPALETTE),
113 X(EMR_REALIZEPALETTE),
114 X(EMR_EXTFLOODFILL),
115 X(EMR_LINETO),
116 X(EMR_ARCTO),
117 X(EMR_POLYDRAW),
118 X(EMR_SETARCDIRECTION),
119 X(EMR_SETMITERLIMIT),
120 X(EMR_BEGINPATH),
121 X(EMR_ENDPATH),
122 X(EMR_CLOSEFIGURE),
123 X(EMR_FILLPATH),
124 X(EMR_STROKEANDFILLPATH),
125 X(EMR_STROKEPATH),
126 X(EMR_FLATTENPATH),
127 X(EMR_WIDENPATH),
128 X(EMR_SELECTCLIPPATH),
129 X(EMR_ABORTPATH),
130 X(EMR_GDICOMMENT),
131 X(EMR_FILLRGN),
132 X(EMR_FRAMERGN),
133 X(EMR_INVERTRGN),
134 X(EMR_PAINTRGN),
135 X(EMR_EXTSELECTCLIPRGN),
136 X(EMR_BITBLT),
137 X(EMR_STRETCHBLT),
138 X(EMR_MASKBLT),
139 X(EMR_PLGBLT),
140 X(EMR_SETDIBITSTODEVICE),
141 X(EMR_STRETCHDIBITS),
142 X(EMR_EXTCREATEFONTINDIRECTW),
143 X(EMR_EXTTEXTOUTA),
144 X(EMR_EXTTEXTOUTW),
145 X(EMR_POLYBEZIER16),
146 X(EMR_POLYGON16),
147 X(EMR_POLYLINE16),
148 X(EMR_POLYBEZIERTO16),
149 X(EMR_POLYLINETO16),
150 X(EMR_POLYPOLYLINE16),
151 X(EMR_POLYPOLYGON16),
152 X(EMR_POLYDRAW16),
153 X(EMR_CREATEMONOBRUSH),
154 X(EMR_CREATEDIBPATTERNBRUSHPT),
155 X(EMR_EXTCREATEPEN),
156 X(EMR_POLYTEXTOUTA),
157 X(EMR_POLYTEXTOUTW),
158 X(EMR_SETICMMODE),
159 X(EMR_CREATECOLORSPACE),
160 X(EMR_SETCOLORSPACE),
161 X(EMR_DELETECOLORSPACE),
162 X(EMR_GLSRECORD),
163 X(EMR_GLSBOUNDEDRECORD),
164 X(EMR_PIXELFORMAT),
165 X(EMR_DRAWESCAPE),
166 X(EMR_EXTESCAPE),
167 X(EMR_STARTDOC),
168 X(EMR_SMALLTEXTOUT),
169 X(EMR_FORCEUFIMAPPING),
170 X(EMR_NAMEDESCAPE),
171 X(EMR_COLORCORRECTPALETTE),
172 X(EMR_SETICMPROFILEA),
173 X(EMR_SETICMPROFILEW),
174 X(EMR_ALPHABLEND),
175 X(EMR_SETLAYOUT),
176 X(EMR_TRANSPARENTBLT),
177 X(EMR_RESERVED_117),
178 X(EMR_GRADIENTFILL),
179 X(EMR_SETLINKEDUFI),
180 X(EMR_SETTEXTJUSTIFICATION),
181 X(EMR_COLORMATCHTOTARGETW),
182 X(EMR_CREATECOLORSPACEW)
183 #undef X
186 /****************************************************************************
187 * get_emr_name
189 static const char *get_emr_name(DWORD type)
191 unsigned int i;
192 for(i = 0; i < sizeof(emr_names) / sizeof(emr_names[0]); i++)
193 if(type == emr_names[i].type) return emr_names[i].name;
194 TRACE("Unknown record type %d\n", type);
195 return NULL;
198 /***********************************************************************
199 * is_dib_monochrome
201 * Returns whether a DIB can be converted to a monochrome DDB.
203 * A DIB can be converted if its color table contains only black and
204 * white. Black must be the first color in the color table.
206 * Note : If the first color in the color table is white followed by
207 * black, we can't convert it to a monochrome DDB with
208 * SetDIBits, because black and white would be inverted.
210 static inline BOOL is_dib_monochrome( const BITMAPINFO* info )
212 if (info->bmiHeader.biBitCount != 1) return FALSE;
214 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
216 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO *) info)->bmciColors;
218 /* Check if the first color is black */
219 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
221 rgb++;
222 /* Check if the second color is white */
223 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
224 && (rgb->rgbtBlue == 0xff));
226 else return FALSE;
228 else /* assume BITMAPINFOHEADER */
230 const RGBQUAD *rgb = info->bmiColors;
232 /* Check if the first color is black */
233 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
234 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
236 rgb++;
238 /* Check if the second color is white */
239 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
240 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
242 else return FALSE;
246 /****************************************************************************
247 * EMF_Create_HENHMETAFILE
249 HENHMETAFILE EMF_Create_HENHMETAFILE(ENHMETAHEADER *emh, BOOL on_disk )
251 HENHMETAFILE hmf;
252 ENHMETAFILEOBJ *metaObj;
254 if (emh->iType != EMR_HEADER)
256 SetLastError(ERROR_INVALID_DATA);
257 return 0;
259 if (emh->dSignature != ENHMETA_SIGNATURE ||
260 (emh->nBytes & 3)) /* refuse to load unaligned EMF as Windows does */
262 WARN("Invalid emf header type 0x%08x sig 0x%08x.\n",
263 emh->iType, emh->dSignature);
264 return 0;
267 if (!(metaObj = HeapAlloc( GetProcessHeap(), 0, sizeof(*metaObj) ))) return 0;
269 metaObj->emh = emh;
270 metaObj->on_disk = on_disk;
272 if (!(hmf = alloc_gdi_handle( metaObj, OBJ_ENHMETAFILE, NULL )))
273 HeapFree( GetProcessHeap(), 0, metaObj );
274 return hmf;
277 /****************************************************************************
278 * EMF_Delete_HENHMETAFILE
280 static BOOL EMF_Delete_HENHMETAFILE( HENHMETAFILE hmf )
282 ENHMETAFILEOBJ *metaObj = free_gdi_handle( hmf );
284 if(!metaObj) return FALSE;
286 if(metaObj->on_disk)
287 UnmapViewOfFile( metaObj->emh );
288 else
289 HeapFree( GetProcessHeap(), 0, metaObj->emh );
290 return HeapFree( GetProcessHeap(), 0, metaObj );
293 /******************************************************************
294 * EMF_GetEnhMetaHeader
296 * Returns ptr to ENHMETAHEADER associated with HENHMETAFILE
298 static ENHMETAHEADER *EMF_GetEnhMetaHeader( HENHMETAFILE hmf )
300 ENHMETAHEADER *ret = NULL;
301 ENHMETAFILEOBJ *metaObj = GDI_GetObjPtr( hmf, OBJ_ENHMETAFILE );
302 TRACE("hmf %p -> enhmetaObj %p\n", hmf, metaObj);
303 if (metaObj)
305 ret = metaObj->emh;
306 GDI_ReleaseObj( hmf );
308 return ret;
311 /*****************************************************************************
312 * EMF_GetEnhMetaFile
315 static HENHMETAFILE EMF_GetEnhMetaFile( HANDLE hFile )
317 ENHMETAHEADER *emh;
318 HANDLE hMapping;
319 HENHMETAFILE hemf;
321 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
322 emh = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
323 CloseHandle( hMapping );
325 if (!emh) return 0;
327 hemf = EMF_Create_HENHMETAFILE( emh, TRUE );
328 if (!hemf)
329 UnmapViewOfFile( emh );
330 return hemf;
334 /*****************************************************************************
335 * GetEnhMetaFileA (GDI32.@)
339 HENHMETAFILE WINAPI GetEnhMetaFileA(
340 LPCSTR lpszMetaFile /* [in] filename of enhanced metafile */
343 HENHMETAFILE hmf;
344 HANDLE hFile;
346 hFile = CreateFileA(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
347 OPEN_EXISTING, 0, 0);
348 if (hFile == INVALID_HANDLE_VALUE) {
349 WARN("could not open %s\n", lpszMetaFile);
350 return 0;
352 hmf = EMF_GetEnhMetaFile( hFile );
353 CloseHandle( hFile );
354 return hmf;
357 /*****************************************************************************
358 * GetEnhMetaFileW (GDI32.@)
360 HENHMETAFILE WINAPI GetEnhMetaFileW(
361 LPCWSTR lpszMetaFile) /* [in] filename of enhanced metafile */
363 HENHMETAFILE hmf;
364 HANDLE hFile;
366 hFile = CreateFileW(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
367 OPEN_EXISTING, 0, 0);
368 if (hFile == INVALID_HANDLE_VALUE) {
369 WARN("could not open %s\n", debugstr_w(lpszMetaFile));
370 return 0;
372 hmf = EMF_GetEnhMetaFile( hFile );
373 CloseHandle( hFile );
374 return hmf;
377 /*****************************************************************************
378 * GetEnhMetaFileHeader (GDI32.@)
380 * Retrieves the record containing the header for the specified
381 * enhanced-format metafile.
383 * RETURNS
384 * If buf is NULL, returns the size of buffer required.
385 * Otherwise, copy up to bufsize bytes of enhanced metafile header into
386 * buf.
388 UINT WINAPI GetEnhMetaFileHeader(
389 HENHMETAFILE hmf, /* [in] enhanced metafile */
390 UINT bufsize, /* [in] size of buffer */
391 LPENHMETAHEADER buf /* [out] buffer */
394 LPENHMETAHEADER emh;
395 UINT size;
397 emh = EMF_GetEnhMetaHeader(hmf);
398 if(!emh) return FALSE;
399 size = emh->nSize;
400 if (!buf) return size;
401 size = min(size, bufsize);
402 memmove(buf, emh, size);
403 return size;
407 /*****************************************************************************
408 * GetEnhMetaFileDescriptionA (GDI32.@)
410 * See GetEnhMetaFileDescriptionW.
412 UINT WINAPI GetEnhMetaFileDescriptionA(
413 HENHMETAFILE hmf, /* [in] enhanced metafile */
414 UINT size, /* [in] size of buf */
415 LPSTR buf /* [out] buffer to receive description */
418 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
419 DWORD len;
420 WCHAR *descrW;
422 if(!emh) return FALSE;
423 if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
424 descrW = (WCHAR *) ((char *) emh + emh->offDescription);
425 len = WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, NULL, 0, NULL, NULL );
427 if (!buf || !size ) return len;
429 len = min( size, len );
430 WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, buf, len, NULL, NULL );
431 return len;
434 /*****************************************************************************
435 * GetEnhMetaFileDescriptionW (GDI32.@)
437 * Copies the description string of an enhanced metafile into a buffer
438 * _buf_.
440 * RETURNS
441 * If _buf_ is NULL, returns size of _buf_ required. Otherwise, returns
442 * number of characters copied.
444 UINT WINAPI GetEnhMetaFileDescriptionW(
445 HENHMETAFILE hmf, /* [in] enhanced metafile */
446 UINT size, /* [in] size of buf */
447 LPWSTR buf /* [out] buffer to receive description */
450 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
452 if(!emh) return FALSE;
453 if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
454 if (!buf || !size ) return emh->nDescription;
456 memmove(buf, (char *) emh + emh->offDescription, min(size,emh->nDescription)*sizeof(WCHAR));
457 return min(size, emh->nDescription);
460 /****************************************************************************
461 * SetEnhMetaFileBits (GDI32.@)
463 * Creates an enhanced metafile by copying _bufsize_ bytes from _buf_.
465 HENHMETAFILE WINAPI SetEnhMetaFileBits(UINT bufsize, const BYTE *buf)
467 ENHMETAHEADER *emh = HeapAlloc( GetProcessHeap(), 0, bufsize );
468 HENHMETAFILE hmf;
469 memmove(emh, buf, bufsize);
470 hmf = EMF_Create_HENHMETAFILE( emh, FALSE );
471 if (!hmf)
472 HeapFree( GetProcessHeap(), 0, emh );
473 return hmf;
476 /*****************************************************************************
477 * GetEnhMetaFileBits (GDI32.@)
480 UINT WINAPI GetEnhMetaFileBits(
481 HENHMETAFILE hmf,
482 UINT bufsize,
483 LPBYTE buf
486 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader( hmf );
487 UINT size;
489 if(!emh) return 0;
491 size = emh->nBytes;
492 if( buf == NULL ) return size;
494 size = min( size, bufsize );
495 memmove(buf, emh, size);
496 return size;
499 typedef struct EMF_dc_state
501 INT mode;
502 XFORM world_transform;
503 INT wndOrgX;
504 INT wndOrgY;
505 INT wndExtX;
506 INT wndExtY;
507 INT vportOrgX;
508 INT vportOrgY;
509 INT vportExtX;
510 INT vportExtY;
511 struct EMF_dc_state *next;
512 } EMF_dc_state;
514 typedef struct enum_emh_data
516 XFORM init_transform;
517 EMF_dc_state state;
518 INT save_level;
519 EMF_dc_state *saved_state;
520 } enum_emh_data;
522 #define ENUM_GET_PRIVATE_DATA(ht) \
523 ((enum_emh_data*)(((unsigned char*)(ht))-sizeof (enum_emh_data)))
525 #define WIDTH(rect) ( (rect).right - (rect).left )
526 #define HEIGHT(rect) ( (rect).bottom - (rect).top )
528 #define IS_WIN9X() (GetVersion()&0x80000000)
530 static void EMF_Update_MF_Xform(HDC hdc, const enum_emh_data *info)
532 XFORM mapping_mode_trans, final_trans;
533 double scaleX, scaleY;
535 scaleX = (double)info->state.vportExtX / (double)info->state.wndExtX;
536 scaleY = (double)info->state.vportExtY / (double)info->state.wndExtY;
537 mapping_mode_trans.eM11 = scaleX;
538 mapping_mode_trans.eM12 = 0.0;
539 mapping_mode_trans.eM21 = 0.0;
540 mapping_mode_trans.eM22 = scaleY;
541 mapping_mode_trans.eDx = (double)info->state.vportOrgX - scaleX * (double)info->state.wndOrgX;
542 mapping_mode_trans.eDy = (double)info->state.vportOrgY - scaleY * (double)info->state.wndOrgY;
544 CombineTransform(&final_trans, &info->state.world_transform, &mapping_mode_trans);
545 CombineTransform(&final_trans, &final_trans, &info->init_transform);
547 if (!SetWorldTransform(hdc, &final_trans))
549 ERR("World transform failed!\n");
553 static void EMF_RestoreDC( enum_emh_data *info, INT level )
555 if (abs(level) > info->save_level || level == 0) return;
557 if (level < 0) level = info->save_level + level + 1;
559 while (info->save_level >= level)
561 EMF_dc_state *state = info->saved_state;
562 info->saved_state = state->next;
563 state->next = NULL;
564 if (--info->save_level < level)
565 info->state = *state;
566 HeapFree( GetProcessHeap(), 0, state );
570 static void EMF_SaveDC( enum_emh_data *info )
572 EMF_dc_state *state = HeapAlloc( GetProcessHeap(), 0, sizeof(*state));
573 if (state)
575 *state = info->state;
576 state->next = info->saved_state;
577 info->saved_state = state;
578 info->save_level++;
579 TRACE("save_level %d\n", info->save_level);
583 static void EMF_SetMapMode(HDC hdc, enum_emh_data *info)
585 INT horzSize = GetDeviceCaps( hdc, HORZSIZE );
586 INT vertSize = GetDeviceCaps( hdc, VERTSIZE );
587 INT horzRes = GetDeviceCaps( hdc, HORZRES );
588 INT vertRes = GetDeviceCaps( hdc, VERTRES );
590 TRACE("%d\n", info->state.mode);
592 switch(info->state.mode)
594 case MM_TEXT:
595 info->state.wndExtX = 1;
596 info->state.wndExtY = 1;
597 info->state.vportExtX = 1;
598 info->state.vportExtY = 1;
599 break;
600 case MM_LOMETRIC:
601 case MM_ISOTROPIC:
602 info->state.wndExtX = horzSize * 10;
603 info->state.wndExtY = vertSize * 10;
604 info->state.vportExtX = horzRes;
605 info->state.vportExtY = -vertRes;
606 break;
607 case MM_HIMETRIC:
608 info->state.wndExtX = horzSize * 100;
609 info->state.wndExtY = vertSize * 100;
610 info->state.vportExtX = horzRes;
611 info->state.vportExtY = -vertRes;
612 break;
613 case MM_LOENGLISH:
614 info->state.wndExtX = MulDiv(1000, horzSize, 254);
615 info->state.wndExtY = MulDiv(1000, vertSize, 254);
616 info->state.vportExtX = horzRes;
617 info->state.vportExtY = -vertRes;
618 break;
619 case MM_HIENGLISH:
620 info->state.wndExtX = MulDiv(10000, horzSize, 254);
621 info->state.wndExtY = MulDiv(10000, vertSize, 254);
622 info->state.vportExtX = horzRes;
623 info->state.vportExtY = -vertRes;
624 break;
625 case MM_TWIPS:
626 info->state.wndExtX = MulDiv(14400, horzSize, 254);
627 info->state.wndExtY = MulDiv(14400, vertSize, 254);
628 info->state.vportExtX = horzRes;
629 info->state.vportExtY = -vertRes;
630 break;
631 case MM_ANISOTROPIC:
632 break;
633 default:
634 return;
638 /***********************************************************************
639 * EMF_FixIsotropic
641 * Fix viewport extensions for isotropic mode.
644 static void EMF_FixIsotropic(HDC hdc, enum_emh_data *info)
646 double xdim = fabs((double)info->state.vportExtX * GetDeviceCaps( hdc, HORZSIZE ) /
647 (GetDeviceCaps( hdc, HORZRES ) * info->state.wndExtX));
648 double ydim = fabs((double)info->state.vportExtY * GetDeviceCaps( hdc, VERTSIZE ) /
649 (GetDeviceCaps( hdc, VERTRES ) * info->state.wndExtY));
651 if (xdim > ydim)
653 INT mincx = (info->state.vportExtX >= 0) ? 1 : -1;
654 info->state.vportExtX = floor(info->state.vportExtX * ydim / xdim + 0.5);
655 if (!info->state.vportExtX) info->state.vportExtX = mincx;
657 else
659 INT mincy = (info->state.vportExtY >= 0) ? 1 : -1;
660 info->state.vportExtY = floor(info->state.vportExtY * xdim / ydim + 0.5);
661 if (!info->state.vportExtY) info->state.vportExtY = mincy;
665 /*****************************************************************************
666 * emr_produces_output
668 * Returns TRUE if the record type writes something to the dc. Used by
669 * PlayEnhMetaFileRecord to determine whether it needs to update the
670 * dc's xform when in win9x mode.
672 * FIXME: need to test which records should be here.
674 static BOOL emr_produces_output(int type)
676 switch(type) {
677 case EMR_POLYBEZIER:
678 case EMR_POLYGON:
679 case EMR_POLYLINE:
680 case EMR_POLYBEZIERTO:
681 case EMR_POLYLINETO:
682 case EMR_POLYPOLYLINE:
683 case EMR_POLYPOLYGON:
684 case EMR_SETPIXELV:
685 case EMR_MOVETOEX:
686 case EMR_EXCLUDECLIPRECT:
687 case EMR_INTERSECTCLIPRECT:
688 case EMR_SELECTOBJECT:
689 case EMR_ANGLEARC:
690 case EMR_ELLIPSE:
691 case EMR_RECTANGLE:
692 case EMR_ROUNDRECT:
693 case EMR_ARC:
694 case EMR_CHORD:
695 case EMR_PIE:
696 case EMR_EXTFLOODFILL:
697 case EMR_LINETO:
698 case EMR_ARCTO:
699 case EMR_POLYDRAW:
700 case EMR_GDICOMMENT:
701 case EMR_FILLRGN:
702 case EMR_FRAMERGN:
703 case EMR_INVERTRGN:
704 case EMR_PAINTRGN:
705 case EMR_BITBLT:
706 case EMR_STRETCHBLT:
707 case EMR_MASKBLT:
708 case EMR_PLGBLT:
709 case EMR_SETDIBITSTODEVICE:
710 case EMR_STRETCHDIBITS:
711 case EMR_EXTTEXTOUTA:
712 case EMR_EXTTEXTOUTW:
713 case EMR_POLYBEZIER16:
714 case EMR_POLYGON16:
715 case EMR_POLYLINE16:
716 case EMR_POLYBEZIERTO16:
717 case EMR_POLYLINETO16:
718 case EMR_POLYPOLYLINE16:
719 case EMR_POLYPOLYGON16:
720 case EMR_POLYDRAW16:
721 case EMR_POLYTEXTOUTA:
722 case EMR_POLYTEXTOUTW:
723 case EMR_SMALLTEXTOUT:
724 case EMR_ALPHABLEND:
725 case EMR_TRANSPARENTBLT:
726 return TRUE;
727 default:
728 return FALSE;
733 /*****************************************************************************
734 * PlayEnhMetaFileRecord (GDI32.@)
736 * Render a single enhanced metafile record in the device context hdc.
738 * RETURNS
739 * TRUE (non zero) on success, FALSE on error.
740 * BUGS
741 * Many unimplemented records.
742 * No error handling on record play failures (ie checking return codes)
744 * NOTES
745 * WinNT actually updates the current world transform in this function
746 * whereas Win9x does not.
748 BOOL WINAPI PlayEnhMetaFileRecord(
749 HDC hdc, /* [in] device context in which to render EMF record */
750 LPHANDLETABLE handletable, /* [in] array of handles to be used in rendering record */
751 const ENHMETARECORD *mr, /* [in] EMF record to render */
752 UINT handles /* [in] size of handle array */
755 int type;
756 RECT tmprc;
757 enum_emh_data *info = ENUM_GET_PRIVATE_DATA(handletable);
759 TRACE("hdc = %p, handletable = %p, record = %p, numHandles = %d\n",
760 hdc, handletable, mr, handles);
761 if (!mr) return FALSE;
763 type = mr->iType;
765 TRACE("record %s\n", get_emr_name(type));
766 switch(type)
768 case EMR_HEADER:
769 break;
770 case EMR_EOF:
771 break;
772 case EMR_GDICOMMENT:
774 const EMRGDICOMMENT *lpGdiComment = (const EMRGDICOMMENT *)mr;
775 /* In an enhanced metafile, there can be both public and private GDI comments */
776 GdiComment( hdc, lpGdiComment->cbData, lpGdiComment->Data );
777 break;
779 case EMR_SETMAPMODE:
781 const EMRSETMAPMODE *pSetMapMode = (const EMRSETMAPMODE *)mr;
783 if (info->state.mode == pSetMapMode->iMode &&
784 (info->state.mode == MM_ISOTROPIC || info->state.mode == MM_ANISOTROPIC))
785 break;
786 info->state.mode = pSetMapMode->iMode;
787 EMF_SetMapMode(hdc, info);
789 if (!IS_WIN9X())
790 EMF_Update_MF_Xform(hdc, info);
792 break;
794 case EMR_SETBKMODE:
796 const EMRSETBKMODE *pSetBkMode = (const EMRSETBKMODE *)mr;
797 SetBkMode(hdc, pSetBkMode->iMode);
798 break;
800 case EMR_SETBKCOLOR:
802 const EMRSETBKCOLOR *pSetBkColor = (const EMRSETBKCOLOR *)mr;
803 SetBkColor(hdc, pSetBkColor->crColor);
804 break;
806 case EMR_SETPOLYFILLMODE:
808 const EMRSETPOLYFILLMODE *pSetPolyFillMode = (const EMRSETPOLYFILLMODE *)mr;
809 SetPolyFillMode(hdc, pSetPolyFillMode->iMode);
810 break;
812 case EMR_SETROP2:
814 const EMRSETROP2 *pSetROP2 = (const EMRSETROP2 *)mr;
815 SetROP2(hdc, pSetROP2->iMode);
816 break;
818 case EMR_SETSTRETCHBLTMODE:
820 const EMRSETSTRETCHBLTMODE *pSetStretchBltMode = (const EMRSETSTRETCHBLTMODE *)mr;
821 SetStretchBltMode(hdc, pSetStretchBltMode->iMode);
822 break;
824 case EMR_SETTEXTALIGN:
826 const EMRSETTEXTALIGN *pSetTextAlign = (const EMRSETTEXTALIGN *)mr;
827 SetTextAlign(hdc, pSetTextAlign->iMode);
828 break;
830 case EMR_SETTEXTCOLOR:
832 const EMRSETTEXTCOLOR *pSetTextColor = (const EMRSETTEXTCOLOR *)mr;
833 SetTextColor(hdc, pSetTextColor->crColor);
834 break;
836 case EMR_SAVEDC:
838 if (SaveDC( hdc ))
839 EMF_SaveDC( info );
840 break;
842 case EMR_RESTOREDC:
844 const EMRRESTOREDC *pRestoreDC = (const EMRRESTOREDC *)mr;
845 TRACE("EMR_RESTORE: %d\n", pRestoreDC->iRelative);
846 if (RestoreDC( hdc, pRestoreDC->iRelative ))
847 EMF_RestoreDC( info, pRestoreDC->iRelative );
848 break;
850 case EMR_INTERSECTCLIPRECT:
852 const EMRINTERSECTCLIPRECT *pClipRect = (const EMRINTERSECTCLIPRECT *)mr;
853 TRACE("EMR_INTERSECTCLIPRECT: rect %d,%d - %d, %d\n",
854 pClipRect->rclClip.left, pClipRect->rclClip.top,
855 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
856 IntersectClipRect(hdc, pClipRect->rclClip.left, pClipRect->rclClip.top,
857 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
858 break;
860 case EMR_SELECTOBJECT:
862 const EMRSELECTOBJECT *pSelectObject = (const EMRSELECTOBJECT *)mr;
863 if( pSelectObject->ihObject & 0x80000000 ) {
864 /* High order bit is set - it's a stock object
865 * Strip the high bit to get the index.
866 * See MSDN article Q142319
868 SelectObject( hdc, GetStockObject( pSelectObject->ihObject &
869 0x7fffffff ) );
870 } else {
871 /* High order bit wasn't set - not a stock object
873 SelectObject( hdc,
874 (handletable->objectHandle)[pSelectObject->ihObject] );
876 break;
878 case EMR_DELETEOBJECT:
880 const EMRDELETEOBJECT *pDeleteObject = (const EMRDELETEOBJECT *)mr;
881 DeleteObject( (handletable->objectHandle)[pDeleteObject->ihObject]);
882 (handletable->objectHandle)[pDeleteObject->ihObject] = 0;
883 break;
885 case EMR_SETWINDOWORGEX:
887 const EMRSETWINDOWORGEX *pSetWindowOrgEx = (const EMRSETWINDOWORGEX *)mr;
889 info->state.wndOrgX = pSetWindowOrgEx->ptlOrigin.x;
890 info->state.wndOrgY = pSetWindowOrgEx->ptlOrigin.y;
892 TRACE("SetWindowOrgEx: %d,%d\n", info->state.wndOrgX, info->state.wndOrgY);
894 if (!IS_WIN9X())
895 EMF_Update_MF_Xform(hdc, info);
897 break;
899 case EMR_SETWINDOWEXTEX:
901 const EMRSETWINDOWEXTEX *pSetWindowExtEx = (const EMRSETWINDOWEXTEX *)mr;
903 if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
904 break;
905 info->state.wndExtX = pSetWindowExtEx->szlExtent.cx;
906 info->state.wndExtY = pSetWindowExtEx->szlExtent.cy;
907 if (info->state.mode == MM_ISOTROPIC)
908 EMF_FixIsotropic(hdc, info);
910 TRACE("SetWindowExtEx: %d,%d\n",info->state.wndExtX, info->state.wndExtY);
912 if (!IS_WIN9X())
913 EMF_Update_MF_Xform(hdc, info);
915 break;
917 case EMR_SETVIEWPORTORGEX:
919 const EMRSETVIEWPORTORGEX *pSetViewportOrgEx = (const EMRSETVIEWPORTORGEX *)mr;
921 info->state.vportOrgX = pSetViewportOrgEx->ptlOrigin.x;
922 info->state.vportOrgY = pSetViewportOrgEx->ptlOrigin.y;
923 TRACE("SetViewportOrgEx: %d,%d\n", info->state.vportOrgX, info->state.vportOrgY);
925 if (!IS_WIN9X())
926 EMF_Update_MF_Xform(hdc, info);
928 break;
930 case EMR_SETVIEWPORTEXTEX:
932 const EMRSETVIEWPORTEXTEX *pSetViewportExtEx = (const EMRSETVIEWPORTEXTEX *)mr;
934 if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
935 break;
936 info->state.vportExtX = pSetViewportExtEx->szlExtent.cx;
937 info->state.vportExtY = pSetViewportExtEx->szlExtent.cy;
938 if (info->state.mode == MM_ISOTROPIC)
939 EMF_FixIsotropic(hdc, info);
940 TRACE("SetViewportExtEx: %d,%d\n", info->state.vportExtX, info->state.vportExtY);
942 if (!IS_WIN9X())
943 EMF_Update_MF_Xform(hdc, info);
945 break;
947 case EMR_CREATEPEN:
949 const EMRCREATEPEN *pCreatePen = (const EMRCREATEPEN *)mr;
950 (handletable->objectHandle)[pCreatePen->ihPen] =
951 CreatePenIndirect(&pCreatePen->lopn);
952 break;
954 case EMR_EXTCREATEPEN:
956 const EMREXTCREATEPEN *pPen = (const EMREXTCREATEPEN *)mr;
957 LOGBRUSH lb;
958 lb.lbStyle = pPen->elp.elpBrushStyle;
959 lb.lbColor = pPen->elp.elpColor;
960 lb.lbHatch = pPen->elp.elpHatch;
962 if(pPen->offBmi || pPen->offBits)
963 FIXME("EMR_EXTCREATEPEN: Need to copy brush bitmap\n");
965 (handletable->objectHandle)[pPen->ihPen] =
966 ExtCreatePen(pPen->elp.elpPenStyle, pPen->elp.elpWidth, &lb,
967 pPen->elp.elpNumEntries, pPen->elp.elpNumEntries ? pPen->elp.elpStyleEntry : NULL);
968 break;
970 case EMR_CREATEBRUSHINDIRECT:
972 const EMRCREATEBRUSHINDIRECT *pBrush = (const EMRCREATEBRUSHINDIRECT *)mr;
973 LOGBRUSH brush;
974 brush.lbStyle = pBrush->lb.lbStyle;
975 brush.lbColor = pBrush->lb.lbColor;
976 brush.lbHatch = pBrush->lb.lbHatch;
977 (handletable->objectHandle)[pBrush->ihBrush] = CreateBrushIndirect(&brush);
978 break;
980 case EMR_EXTCREATEFONTINDIRECTW:
982 const EMREXTCREATEFONTINDIRECTW *pFont = (const EMREXTCREATEFONTINDIRECTW *)mr;
983 (handletable->objectHandle)[pFont->ihFont] =
984 CreateFontIndirectW(&pFont->elfw.elfLogFont);
985 break;
987 case EMR_MOVETOEX:
989 const EMRMOVETOEX *pMoveToEx = (const EMRMOVETOEX *)mr;
990 MoveToEx(hdc, pMoveToEx->ptl.x, pMoveToEx->ptl.y, NULL);
991 break;
993 case EMR_LINETO:
995 const EMRLINETO *pLineTo = (const EMRLINETO *)mr;
996 LineTo(hdc, pLineTo->ptl.x, pLineTo->ptl.y);
997 break;
999 case EMR_RECTANGLE:
1001 const EMRRECTANGLE *pRect = (const EMRRECTANGLE *)mr;
1002 Rectangle(hdc, pRect->rclBox.left, pRect->rclBox.top,
1003 pRect->rclBox.right, pRect->rclBox.bottom);
1004 break;
1006 case EMR_ELLIPSE:
1008 const EMRELLIPSE *pEllipse = (const EMRELLIPSE *)mr;
1009 Ellipse(hdc, pEllipse->rclBox.left, pEllipse->rclBox.top,
1010 pEllipse->rclBox.right, pEllipse->rclBox.bottom);
1011 break;
1013 case EMR_POLYGON16:
1015 const EMRPOLYGON16 *pPoly = (const EMRPOLYGON16 *)mr;
1016 /* Shouldn't use Polygon16 since pPoly->cpts is DWORD */
1017 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1018 pPoly->cpts * sizeof(POINT) );
1019 DWORD i;
1020 for(i = 0; i < pPoly->cpts; i++)
1022 pts[i].x = pPoly->apts[i].x;
1023 pts[i].y = pPoly->apts[i].y;
1025 Polygon(hdc, pts, pPoly->cpts);
1026 HeapFree( GetProcessHeap(), 0, pts );
1027 break;
1029 case EMR_POLYLINE16:
1031 const EMRPOLYLINE16 *pPoly = (const EMRPOLYLINE16 *)mr;
1032 /* Shouldn't use Polyline16 since pPoly->cpts is DWORD */
1033 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1034 pPoly->cpts * sizeof(POINT) );
1035 DWORD i;
1036 for(i = 0; i < pPoly->cpts; i++)
1038 pts[i].x = pPoly->apts[i].x;
1039 pts[i].y = pPoly->apts[i].y;
1041 Polyline(hdc, pts, pPoly->cpts);
1042 HeapFree( GetProcessHeap(), 0, pts );
1043 break;
1045 case EMR_POLYLINETO16:
1047 const EMRPOLYLINETO16 *pPoly = (const EMRPOLYLINETO16 *)mr;
1048 /* Shouldn't use PolylineTo16 since pPoly->cpts is DWORD */
1049 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1050 pPoly->cpts * sizeof(POINT) );
1051 DWORD i;
1052 for(i = 0; i < pPoly->cpts; i++)
1054 pts[i].x = pPoly->apts[i].x;
1055 pts[i].y = pPoly->apts[i].y;
1057 PolylineTo(hdc, pts, pPoly->cpts);
1058 HeapFree( GetProcessHeap(), 0, pts );
1059 break;
1061 case EMR_POLYBEZIER16:
1063 const EMRPOLYBEZIER16 *pPoly = (const EMRPOLYBEZIER16 *)mr;
1064 /* Shouldn't use PolyBezier16 since pPoly->cpts is DWORD */
1065 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1066 pPoly->cpts * sizeof(POINT) );
1067 DWORD i;
1068 for(i = 0; i < pPoly->cpts; i++)
1070 pts[i].x = pPoly->apts[i].x;
1071 pts[i].y = pPoly->apts[i].y;
1073 PolyBezier(hdc, pts, pPoly->cpts);
1074 HeapFree( GetProcessHeap(), 0, pts );
1075 break;
1077 case EMR_POLYBEZIERTO16:
1079 const EMRPOLYBEZIERTO16 *pPoly = (const EMRPOLYBEZIERTO16 *)mr;
1080 /* Shouldn't use PolyBezierTo16 since pPoly->cpts is DWORD */
1081 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1082 pPoly->cpts * sizeof(POINT) );
1083 DWORD i;
1084 for(i = 0; i < pPoly->cpts; i++)
1086 pts[i].x = pPoly->apts[i].x;
1087 pts[i].y = pPoly->apts[i].y;
1089 PolyBezierTo(hdc, pts, pPoly->cpts);
1090 HeapFree( GetProcessHeap(), 0, pts );
1091 break;
1093 case EMR_POLYPOLYGON16:
1095 const EMRPOLYPOLYGON16 *pPolyPoly = (const EMRPOLYPOLYGON16 *)mr;
1096 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1097 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1099 const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1100 POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1101 DWORD i;
1102 for(i = 0; i < pPolyPoly->cpts; i++)
1104 pt[i].x = pts[i].x;
1105 pt[i].y = pts[i].y;
1107 PolyPolygon(hdc, pt, (const INT*)pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1108 HeapFree( GetProcessHeap(), 0, pt );
1109 break;
1111 case EMR_POLYPOLYLINE16:
1113 const EMRPOLYPOLYLINE16 *pPolyPoly = (const EMRPOLYPOLYLINE16 *)mr;
1114 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1115 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1117 const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1118 POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1119 DWORD i;
1120 for(i = 0; i < pPolyPoly->cpts; i++)
1122 pt[i].x = pts[i].x;
1123 pt[i].y = pts[i].y;
1125 PolyPolyline(hdc, pt, pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1126 HeapFree( GetProcessHeap(), 0, pt );
1127 break;
1130 case EMR_STRETCHDIBITS:
1132 const EMRSTRETCHDIBITS *pStretchDIBits = (const EMRSTRETCHDIBITS *)mr;
1134 StretchDIBits(hdc,
1135 pStretchDIBits->xDest,
1136 pStretchDIBits->yDest,
1137 pStretchDIBits->cxDest,
1138 pStretchDIBits->cyDest,
1139 pStretchDIBits->xSrc,
1140 pStretchDIBits->ySrc,
1141 pStretchDIBits->cxSrc,
1142 pStretchDIBits->cySrc,
1143 (const BYTE *)mr + pStretchDIBits->offBitsSrc,
1144 (const BITMAPINFO *)((const BYTE *)mr + pStretchDIBits->offBmiSrc),
1145 pStretchDIBits->iUsageSrc,
1146 pStretchDIBits->dwRop);
1147 break;
1150 case EMR_EXTTEXTOUTA:
1152 const EMREXTTEXTOUTA *pExtTextOutA = (const EMREXTTEXTOUTA *)mr;
1153 RECT rc;
1154 const INT *dx = NULL;
1155 int old_mode;
1157 rc.left = pExtTextOutA->emrtext.rcl.left;
1158 rc.top = pExtTextOutA->emrtext.rcl.top;
1159 rc.right = pExtTextOutA->emrtext.rcl.right;
1160 rc.bottom = pExtTextOutA->emrtext.rcl.bottom;
1161 TRACE("EMR_EXTTEXTOUTA: x,y = %d, %d. rect = %s. flags %08x\n",
1162 pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1163 wine_dbgstr_rect(&rc), pExtTextOutA->emrtext.fOptions);
1165 old_mode = SetGraphicsMode(hdc, pExtTextOutA->iGraphicsMode);
1166 /* Reselect the font back into the dc so that the transformation
1167 gets updated. */
1168 SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1170 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1171 * These files can be enumerated and played under Win98 just
1172 * fine, but at least Win2k chokes on them.
1174 if (pExtTextOutA->emrtext.offDx)
1175 dx = (const INT *)((const BYTE *)mr + pExtTextOutA->emrtext.offDx);
1177 ExtTextOutA(hdc, pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1178 pExtTextOutA->emrtext.fOptions, &rc,
1179 (LPCSTR)((const BYTE *)mr + pExtTextOutA->emrtext.offString), pExtTextOutA->emrtext.nChars,
1180 dx);
1182 SetGraphicsMode(hdc, old_mode);
1183 break;
1186 case EMR_EXTTEXTOUTW:
1188 const EMREXTTEXTOUTW *pExtTextOutW = (const EMREXTTEXTOUTW *)mr;
1189 RECT rc;
1190 const INT *dx = NULL;
1191 int old_mode;
1193 rc.left = pExtTextOutW->emrtext.rcl.left;
1194 rc.top = pExtTextOutW->emrtext.rcl.top;
1195 rc.right = pExtTextOutW->emrtext.rcl.right;
1196 rc.bottom = pExtTextOutW->emrtext.rcl.bottom;
1197 TRACE("EMR_EXTTEXTOUTW: x,y = %d, %d. rect = %s. flags %08x\n",
1198 pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1199 wine_dbgstr_rect(&rc), pExtTextOutW->emrtext.fOptions);
1201 old_mode = SetGraphicsMode(hdc, pExtTextOutW->iGraphicsMode);
1202 /* Reselect the font back into the dc so that the transformation
1203 gets updated. */
1204 SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1206 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1207 * These files can be enumerated and played under Win98 just
1208 * fine, but at least Win2k chokes on them.
1210 if (pExtTextOutW->emrtext.offDx)
1211 dx = (const INT *)((const BYTE *)mr + pExtTextOutW->emrtext.offDx);
1213 ExtTextOutW(hdc, pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1214 pExtTextOutW->emrtext.fOptions, &rc,
1215 (LPCWSTR)((const BYTE *)mr + pExtTextOutW->emrtext.offString), pExtTextOutW->emrtext.nChars,
1216 dx);
1218 SetGraphicsMode(hdc, old_mode);
1219 break;
1222 case EMR_CREATEPALETTE:
1224 const EMRCREATEPALETTE *lpCreatePal = (const EMRCREATEPALETTE *)mr;
1226 (handletable->objectHandle)[ lpCreatePal->ihPal ] =
1227 CreatePalette( &lpCreatePal->lgpl );
1229 break;
1232 case EMR_SELECTPALETTE:
1234 const EMRSELECTPALETTE *lpSelectPal = (const EMRSELECTPALETTE *)mr;
1236 if( lpSelectPal->ihPal & 0x80000000 ) {
1237 SelectPalette( hdc, GetStockObject(lpSelectPal->ihPal & 0x7fffffff), TRUE);
1238 } else {
1239 SelectPalette( hdc, (handletable->objectHandle)[lpSelectPal->ihPal], TRUE);
1241 break;
1244 case EMR_REALIZEPALETTE:
1246 RealizePalette( hdc );
1247 break;
1250 case EMR_EXTSELECTCLIPRGN:
1252 const EMREXTSELECTCLIPRGN *lpRgn = (const EMREXTSELECTCLIPRGN *)mr;
1253 HRGN hRgn = 0;
1255 if (mr->nSize >= sizeof(*lpRgn) + sizeof(RGNDATAHEADER))
1256 hRgn = ExtCreateRegion( &info->init_transform, 0, (const RGNDATA *)lpRgn->RgnData );
1258 ExtSelectClipRgn(hdc, hRgn, (INT)(lpRgn->iMode));
1259 /* ExtSelectClipRgn created a copy of the region */
1260 DeleteObject(hRgn);
1261 break;
1264 case EMR_SETMETARGN:
1266 SetMetaRgn( hdc );
1267 break;
1270 case EMR_SETWORLDTRANSFORM:
1272 const EMRSETWORLDTRANSFORM *lpXfrm = (const EMRSETWORLDTRANSFORM *)mr;
1273 info->state.world_transform = lpXfrm->xform;
1275 if (!IS_WIN9X())
1276 EMF_Update_MF_Xform(hdc, info);
1278 break;
1281 case EMR_POLYBEZIER:
1283 const EMRPOLYBEZIER *lpPolyBez = (const EMRPOLYBEZIER *)mr;
1284 PolyBezier(hdc, (const POINT*)lpPolyBez->aptl, (UINT)lpPolyBez->cptl);
1285 break;
1288 case EMR_POLYGON:
1290 const EMRPOLYGON *lpPoly = (const EMRPOLYGON *)mr;
1291 Polygon( hdc, (const POINT*)lpPoly->aptl, (UINT)lpPoly->cptl );
1292 break;
1295 case EMR_POLYLINE:
1297 const EMRPOLYLINE *lpPolyLine = (const EMRPOLYLINE *)mr;
1298 Polyline(hdc, (const POINT*)lpPolyLine->aptl, (UINT)lpPolyLine->cptl);
1299 break;
1302 case EMR_POLYBEZIERTO:
1304 const EMRPOLYBEZIERTO *lpPolyBezierTo = (const EMRPOLYBEZIERTO *)mr;
1305 PolyBezierTo( hdc, (const POINT*)lpPolyBezierTo->aptl,
1306 (UINT)lpPolyBezierTo->cptl );
1307 break;
1310 case EMR_POLYLINETO:
1312 const EMRPOLYLINETO *lpPolyLineTo = (const EMRPOLYLINETO *)mr;
1313 PolylineTo( hdc, (const POINT*)lpPolyLineTo->aptl,
1314 (UINT)lpPolyLineTo->cptl );
1315 break;
1318 case EMR_POLYPOLYLINE:
1320 const EMRPOLYPOLYLINE *pPolyPolyline = (const EMRPOLYPOLYLINE *)mr;
1321 /* NB Points at pPolyPolyline->aPolyCounts + pPolyPolyline->nPolys */
1323 PolyPolyline(hdc, (const POINT*)(pPolyPolyline->aPolyCounts +
1324 pPolyPolyline->nPolys),
1325 pPolyPolyline->aPolyCounts,
1326 pPolyPolyline->nPolys );
1328 break;
1331 case EMR_POLYPOLYGON:
1333 const EMRPOLYPOLYGON *pPolyPolygon = (const EMRPOLYPOLYGON *)mr;
1335 /* NB Points at pPolyPolygon->aPolyCounts + pPolyPolygon->nPolys */
1337 PolyPolygon(hdc, (const POINT*)(pPolyPolygon->aPolyCounts +
1338 pPolyPolygon->nPolys),
1339 (const INT*)pPolyPolygon->aPolyCounts, pPolyPolygon->nPolys );
1340 break;
1343 case EMR_SETBRUSHORGEX:
1345 const EMRSETBRUSHORGEX *lpSetBrushOrgEx = (const EMRSETBRUSHORGEX *)mr;
1347 SetBrushOrgEx( hdc,
1348 (INT)lpSetBrushOrgEx->ptlOrigin.x,
1349 (INT)lpSetBrushOrgEx->ptlOrigin.y,
1350 NULL );
1352 break;
1355 case EMR_SETPIXELV:
1357 const EMRSETPIXELV *lpSetPixelV = (const EMRSETPIXELV *)mr;
1359 SetPixelV( hdc,
1360 (INT)lpSetPixelV->ptlPixel.x,
1361 (INT)lpSetPixelV->ptlPixel.y,
1362 lpSetPixelV->crColor );
1364 break;
1367 case EMR_SETMAPPERFLAGS:
1369 const EMRSETMAPPERFLAGS *lpSetMapperFlags = (const EMRSETMAPPERFLAGS *)mr;
1371 SetMapperFlags( hdc, lpSetMapperFlags->dwFlags );
1373 break;
1376 case EMR_SETCOLORADJUSTMENT:
1378 const EMRSETCOLORADJUSTMENT *lpSetColorAdjust = (const EMRSETCOLORADJUSTMENT *)mr;
1380 SetColorAdjustment( hdc, &lpSetColorAdjust->ColorAdjustment );
1382 break;
1385 case EMR_OFFSETCLIPRGN:
1387 const EMROFFSETCLIPRGN *lpOffsetClipRgn = (const EMROFFSETCLIPRGN *)mr;
1389 OffsetClipRgn( hdc,
1390 (INT)lpOffsetClipRgn->ptlOffset.x,
1391 (INT)lpOffsetClipRgn->ptlOffset.y );
1392 FIXME("OffsetClipRgn\n");
1394 break;
1397 case EMR_EXCLUDECLIPRECT:
1399 const EMREXCLUDECLIPRECT *lpExcludeClipRect = (const EMREXCLUDECLIPRECT *)mr;
1401 ExcludeClipRect( hdc,
1402 lpExcludeClipRect->rclClip.left,
1403 lpExcludeClipRect->rclClip.top,
1404 lpExcludeClipRect->rclClip.right,
1405 lpExcludeClipRect->rclClip.bottom );
1406 FIXME("ExcludeClipRect\n");
1408 break;
1411 case EMR_SCALEVIEWPORTEXTEX:
1413 const EMRSCALEVIEWPORTEXTEX *lpScaleViewportExtEx = (const EMRSCALEVIEWPORTEXTEX *)mr;
1415 if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1416 break;
1417 if (!lpScaleViewportExtEx->xNum || !lpScaleViewportExtEx->xDenom ||
1418 !lpScaleViewportExtEx->yNum || !lpScaleViewportExtEx->yDenom)
1419 break;
1420 info->state.vportExtX = MulDiv(info->state.vportExtX, lpScaleViewportExtEx->xNum,
1421 lpScaleViewportExtEx->xDenom);
1422 info->state.vportExtY = MulDiv(info->state.vportExtY, lpScaleViewportExtEx->yNum,
1423 lpScaleViewportExtEx->yDenom);
1424 if (info->state.vportExtX == 0) info->state.vportExtX = 1;
1425 if (info->state.vportExtY == 0) info->state.vportExtY = 1;
1426 if (info->state.mode == MM_ISOTROPIC)
1427 EMF_FixIsotropic(hdc, info);
1429 TRACE("EMRSCALEVIEWPORTEXTEX %d/%d %d/%d\n",
1430 lpScaleViewportExtEx->xNum,lpScaleViewportExtEx->xDenom,
1431 lpScaleViewportExtEx->yNum,lpScaleViewportExtEx->yDenom);
1433 if (!IS_WIN9X())
1434 EMF_Update_MF_Xform(hdc, info);
1436 break;
1439 case EMR_SCALEWINDOWEXTEX:
1441 const EMRSCALEWINDOWEXTEX *lpScaleWindowExtEx = (const EMRSCALEWINDOWEXTEX *)mr;
1443 if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1444 break;
1445 if (!lpScaleWindowExtEx->xNum || !lpScaleWindowExtEx->xDenom ||
1446 !lpScaleWindowExtEx->yNum || !lpScaleWindowExtEx->yDenom)
1447 break;
1448 info->state.wndExtX = MulDiv(info->state.wndExtX, lpScaleWindowExtEx->xNum,
1449 lpScaleWindowExtEx->xDenom);
1450 info->state.wndExtY = MulDiv(info->state.wndExtY, lpScaleWindowExtEx->yNum,
1451 lpScaleWindowExtEx->yDenom);
1452 if (info->state.wndExtX == 0) info->state.wndExtX = 1;
1453 if (info->state.wndExtY == 0) info->state.wndExtY = 1;
1454 if (info->state.mode == MM_ISOTROPIC)
1455 EMF_FixIsotropic(hdc, info);
1457 TRACE("EMRSCALEWINDOWEXTEX %d/%d %d/%d\n",
1458 lpScaleWindowExtEx->xNum,lpScaleWindowExtEx->xDenom,
1459 lpScaleWindowExtEx->yNum,lpScaleWindowExtEx->yDenom);
1461 if (!IS_WIN9X())
1462 EMF_Update_MF_Xform(hdc, info);
1464 break;
1467 case EMR_MODIFYWORLDTRANSFORM:
1469 const EMRMODIFYWORLDTRANSFORM *lpModifyWorldTrans = (const EMRMODIFYWORLDTRANSFORM *)mr;
1471 switch(lpModifyWorldTrans->iMode) {
1472 case MWT_IDENTITY:
1473 info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
1474 info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
1475 info->state.world_transform.eDx = info->state.world_transform.eDy = 0;
1476 if (!IS_WIN9X())
1477 EMF_Update_MF_Xform(hdc, info);
1478 break;
1479 case MWT_LEFTMULTIPLY:
1480 CombineTransform(&info->state.world_transform, &lpModifyWorldTrans->xform,
1481 &info->state.world_transform);
1482 if (!IS_WIN9X())
1483 ModifyWorldTransform(hdc, &lpModifyWorldTrans->xform, MWT_LEFTMULTIPLY);
1484 break;
1485 case MWT_RIGHTMULTIPLY:
1486 CombineTransform(&info->state.world_transform, &info->state.world_transform,
1487 &lpModifyWorldTrans->xform);
1488 if (!IS_WIN9X())
1489 EMF_Update_MF_Xform(hdc, info);
1490 break;
1491 default:
1492 FIXME("Unknown imode %d\n", lpModifyWorldTrans->iMode);
1493 break;
1495 break;
1498 case EMR_ANGLEARC:
1500 const EMRANGLEARC *lpAngleArc = (const EMRANGLEARC *)mr;
1502 AngleArc( hdc,
1503 (INT)lpAngleArc->ptlCenter.x, (INT)lpAngleArc->ptlCenter.y,
1504 lpAngleArc->nRadius, lpAngleArc->eStartAngle,
1505 lpAngleArc->eSweepAngle );
1507 break;
1510 case EMR_ROUNDRECT:
1512 const EMRROUNDRECT *lpRoundRect = (const EMRROUNDRECT *)mr;
1514 RoundRect( hdc,
1515 lpRoundRect->rclBox.left,
1516 lpRoundRect->rclBox.top,
1517 lpRoundRect->rclBox.right,
1518 lpRoundRect->rclBox.bottom,
1519 lpRoundRect->szlCorner.cx,
1520 lpRoundRect->szlCorner.cy );
1522 break;
1525 case EMR_ARC:
1527 const EMRARC *lpArc = (const EMRARC *)mr;
1529 Arc( hdc,
1530 (INT)lpArc->rclBox.left,
1531 (INT)lpArc->rclBox.top,
1532 (INT)lpArc->rclBox.right,
1533 (INT)lpArc->rclBox.bottom,
1534 (INT)lpArc->ptlStart.x,
1535 (INT)lpArc->ptlStart.y,
1536 (INT)lpArc->ptlEnd.x,
1537 (INT)lpArc->ptlEnd.y );
1539 break;
1542 case EMR_CHORD:
1544 const EMRCHORD *lpChord = (const EMRCHORD *)mr;
1546 Chord( hdc,
1547 (INT)lpChord->rclBox.left,
1548 (INT)lpChord->rclBox.top,
1549 (INT)lpChord->rclBox.right,
1550 (INT)lpChord->rclBox.bottom,
1551 (INT)lpChord->ptlStart.x,
1552 (INT)lpChord->ptlStart.y,
1553 (INT)lpChord->ptlEnd.x,
1554 (INT)lpChord->ptlEnd.y );
1556 break;
1559 case EMR_PIE:
1561 const EMRPIE *lpPie = (const EMRPIE *)mr;
1563 Pie( hdc,
1564 (INT)lpPie->rclBox.left,
1565 (INT)lpPie->rclBox.top,
1566 (INT)lpPie->rclBox.right,
1567 (INT)lpPie->rclBox.bottom,
1568 (INT)lpPie->ptlStart.x,
1569 (INT)lpPie->ptlStart.y,
1570 (INT)lpPie->ptlEnd.x,
1571 (INT)lpPie->ptlEnd.y );
1573 break;
1576 case EMR_ARCTO:
1578 const EMRARC *lpArcTo = (const EMRARC *)mr;
1580 ArcTo( hdc,
1581 (INT)lpArcTo->rclBox.left,
1582 (INT)lpArcTo->rclBox.top,
1583 (INT)lpArcTo->rclBox.right,
1584 (INT)lpArcTo->rclBox.bottom,
1585 (INT)lpArcTo->ptlStart.x,
1586 (INT)lpArcTo->ptlStart.y,
1587 (INT)lpArcTo->ptlEnd.x,
1588 (INT)lpArcTo->ptlEnd.y );
1590 break;
1593 case EMR_EXTFLOODFILL:
1595 const EMREXTFLOODFILL *lpExtFloodFill = (const EMREXTFLOODFILL *)mr;
1597 ExtFloodFill( hdc,
1598 (INT)lpExtFloodFill->ptlStart.x,
1599 (INT)lpExtFloodFill->ptlStart.y,
1600 lpExtFloodFill->crColor,
1601 (UINT)lpExtFloodFill->iMode );
1603 break;
1606 case EMR_POLYDRAW:
1608 const EMRPOLYDRAW *lpPolyDraw = (const EMRPOLYDRAW *)mr;
1609 PolyDraw( hdc,
1610 (const POINT*)lpPolyDraw->aptl,
1611 lpPolyDraw->abTypes,
1612 (INT)lpPolyDraw->cptl );
1614 break;
1617 case EMR_SETARCDIRECTION:
1619 const EMRSETARCDIRECTION *lpSetArcDirection = (const EMRSETARCDIRECTION *)mr;
1620 SetArcDirection( hdc, (INT)lpSetArcDirection->iArcDirection );
1621 break;
1624 case EMR_SETMITERLIMIT:
1626 const EMRSETMITERLIMIT *lpSetMiterLimit = (const EMRSETMITERLIMIT *)mr;
1627 SetMiterLimit( hdc, lpSetMiterLimit->eMiterLimit, NULL );
1628 break;
1631 case EMR_BEGINPATH:
1633 BeginPath( hdc );
1634 break;
1637 case EMR_ENDPATH:
1639 EndPath( hdc );
1640 break;
1643 case EMR_CLOSEFIGURE:
1645 CloseFigure( hdc );
1646 break;
1649 case EMR_FILLPATH:
1651 /*const EMRFILLPATH lpFillPath = (const EMRFILLPATH *)mr;*/
1652 FillPath( hdc );
1653 break;
1656 case EMR_STROKEANDFILLPATH:
1658 /*const EMRSTROKEANDFILLPATH lpStrokeAndFillPath = (const EMRSTROKEANDFILLPATH *)mr;*/
1659 StrokeAndFillPath( hdc );
1660 break;
1663 case EMR_STROKEPATH:
1665 /*const EMRSTROKEPATH lpStrokePath = (const EMRSTROKEPATH *)mr;*/
1666 StrokePath( hdc );
1667 break;
1670 case EMR_FLATTENPATH:
1672 FlattenPath( hdc );
1673 break;
1676 case EMR_WIDENPATH:
1678 WidenPath( hdc );
1679 break;
1682 case EMR_SELECTCLIPPATH:
1684 const EMRSELECTCLIPPATH *lpSelectClipPath = (const EMRSELECTCLIPPATH *)mr;
1685 SelectClipPath( hdc, (INT)lpSelectClipPath->iMode );
1686 break;
1689 case EMR_ABORTPATH:
1691 AbortPath( hdc );
1692 break;
1695 case EMR_CREATECOLORSPACE:
1697 PEMRCREATECOLORSPACE lpCreateColorSpace = (PEMRCREATECOLORSPACE)mr;
1698 (handletable->objectHandle)[lpCreateColorSpace->ihCS] =
1699 CreateColorSpaceA( &lpCreateColorSpace->lcs );
1700 break;
1703 case EMR_SETCOLORSPACE:
1705 const EMRSETCOLORSPACE *lpSetColorSpace = (const EMRSETCOLORSPACE *)mr;
1706 SetColorSpace( hdc,
1707 (handletable->objectHandle)[lpSetColorSpace->ihCS] );
1708 break;
1711 case EMR_DELETECOLORSPACE:
1713 const EMRDELETECOLORSPACE *lpDeleteColorSpace = (const EMRDELETECOLORSPACE *)mr;
1714 DeleteColorSpace( (handletable->objectHandle)[lpDeleteColorSpace->ihCS] );
1715 break;
1718 case EMR_SETICMMODE:
1720 const EMRSETICMMODE *lpSetICMMode = (const EMRSETICMMODE *)mr;
1721 SetICMMode( hdc, (INT)lpSetICMMode->iMode );
1722 break;
1725 case EMR_PIXELFORMAT:
1727 INT iPixelFormat;
1728 const EMRPIXELFORMAT *lpPixelFormat = (const EMRPIXELFORMAT *)mr;
1730 iPixelFormat = ChoosePixelFormat( hdc, &lpPixelFormat->pfd );
1731 SetPixelFormat( hdc, iPixelFormat, &lpPixelFormat->pfd );
1733 break;
1736 case EMR_SETPALETTEENTRIES:
1738 const EMRSETPALETTEENTRIES *lpSetPaletteEntries = (const EMRSETPALETTEENTRIES *)mr;
1740 SetPaletteEntries( (handletable->objectHandle)[lpSetPaletteEntries->ihPal],
1741 (UINT)lpSetPaletteEntries->iStart,
1742 (UINT)lpSetPaletteEntries->cEntries,
1743 lpSetPaletteEntries->aPalEntries );
1745 break;
1748 case EMR_RESIZEPALETTE:
1750 const EMRRESIZEPALETTE *lpResizePalette = (const EMRRESIZEPALETTE *)mr;
1752 ResizePalette( (handletable->objectHandle)[lpResizePalette->ihPal],
1753 (UINT)lpResizePalette->cEntries );
1755 break;
1758 case EMR_CREATEDIBPATTERNBRUSHPT:
1760 const EMRCREATEDIBPATTERNBRUSHPT *lpCreate = (const EMRCREATEDIBPATTERNBRUSHPT *)mr;
1761 LPVOID lpPackedStruct;
1763 /* Check that offsets and data are contained within the record
1764 * (including checking for wrap-arounds).
1766 if ( lpCreate->offBmi + lpCreate->cbBmi > mr->nSize
1767 || lpCreate->offBits + lpCreate->cbBits > mr->nSize
1768 || lpCreate->offBmi + lpCreate->cbBmi < lpCreate->offBmi
1769 || lpCreate->offBits + lpCreate->cbBits < lpCreate->offBits )
1771 ERR("Invalid EMR_CREATEDIBPATTERNBRUSHPT record\n");
1772 break;
1775 /* This is a BITMAPINFO struct followed directly by bitmap bits */
1776 lpPackedStruct = HeapAlloc( GetProcessHeap(), 0,
1777 lpCreate->cbBmi + lpCreate->cbBits );
1778 if(!lpPackedStruct)
1780 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1781 break;
1784 /* Now pack this structure */
1785 memcpy( lpPackedStruct,
1786 ((const BYTE *)lpCreate) + lpCreate->offBmi,
1787 lpCreate->cbBmi );
1788 memcpy( ((BYTE*)lpPackedStruct) + lpCreate->cbBmi,
1789 ((const BYTE *)lpCreate) + lpCreate->offBits,
1790 lpCreate->cbBits );
1792 (handletable->objectHandle)[lpCreate->ihBrush] =
1793 CreateDIBPatternBrushPt( lpPackedStruct,
1794 (UINT)lpCreate->iUsage );
1796 HeapFree(GetProcessHeap(), 0, lpPackedStruct);
1797 break;
1800 case EMR_CREATEMONOBRUSH:
1802 const EMRCREATEMONOBRUSH *pCreateMonoBrush = (const EMRCREATEMONOBRUSH *)mr;
1803 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pCreateMonoBrush->offBmi);
1804 HBITMAP hBmp;
1806 /* Need to check if the bitmap is monochrome, and if the
1807 two colors are really black and white */
1808 if (pCreateMonoBrush->iUsage == DIB_PAL_MONO)
1810 BITMAP bm;
1812 /* Undocumented iUsage indicates a mono bitmap with no palette table,
1813 * aligned to 32 rather than 16 bits.
1815 bm.bmType = 0;
1816 bm.bmWidth = pbi->bmiHeader.biWidth;
1817 bm.bmHeight = abs(pbi->bmiHeader.biHeight);
1818 bm.bmWidthBytes = 4 * ((pbi->bmiHeader.biWidth + 31) / 32);
1819 bm.bmPlanes = pbi->bmiHeader.biPlanes;
1820 bm.bmBitsPixel = pbi->bmiHeader.biBitCount;
1821 bm.bmBits = (BYTE *)mr + pCreateMonoBrush->offBits;
1822 hBmp = CreateBitmapIndirect(&bm);
1824 else if (is_dib_monochrome(pbi))
1826 /* Top-down DIBs have a negative height */
1827 LONG height = pbi->bmiHeader.biHeight;
1829 hBmp = CreateBitmap(pbi->bmiHeader.biWidth, abs(height), 1, 1, NULL);
1830 SetDIBits(hdc, hBmp, 0, pbi->bmiHeader.biHeight,
1831 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1833 else
1835 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1836 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1839 (handletable->objectHandle)[pCreateMonoBrush->ihBrush] = CreatePatternBrush(hBmp);
1841 /* CreatePatternBrush created a copy of the bitmap */
1842 DeleteObject(hBmp);
1843 break;
1846 case EMR_BITBLT:
1848 const EMRBITBLT *pBitBlt = (const EMRBITBLT *)mr;
1850 if(pBitBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1851 PatBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1852 pBitBlt->dwRop);
1853 } else { /* BitBlt */
1854 HDC hdcSrc = CreateCompatibleDC(hdc);
1855 HBRUSH hBrush, hBrushOld;
1856 HBITMAP hBmp = 0, hBmpOld = 0;
1857 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pBitBlt->offBmiSrc);
1859 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1860 SetWorldTransform(hdcSrc, &pBitBlt->xformSrc);
1862 hBrush = CreateSolidBrush(pBitBlt->crBkColorSrc);
1863 hBrushOld = SelectObject(hdcSrc, hBrush);
1864 PatBlt(hdcSrc, pBitBlt->rclBounds.left, pBitBlt->rclBounds.top,
1865 pBitBlt->rclBounds.right - pBitBlt->rclBounds.left,
1866 pBitBlt->rclBounds.bottom - pBitBlt->rclBounds.top, PATCOPY);
1867 SelectObject(hdcSrc, hBrushOld);
1868 DeleteObject(hBrush);
1870 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1871 (const BYTE *)mr + pBitBlt->offBitsSrc, pbi, pBitBlt->iUsageSrc);
1872 hBmpOld = SelectObject(hdcSrc, hBmp);
1874 BitBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1875 hdcSrc, pBitBlt->xSrc, pBitBlt->ySrc, pBitBlt->dwRop);
1877 SelectObject(hdcSrc, hBmpOld);
1878 DeleteObject(hBmp);
1879 DeleteDC(hdcSrc);
1881 break;
1884 case EMR_STRETCHBLT:
1886 const EMRSTRETCHBLT *pStretchBlt = (const EMRSTRETCHBLT *)mr;
1888 TRACE("EMR_STRETCHBLT: %d, %d %dx%d -> %d, %d %dx%d. rop %08x offBitsSrc %d\n",
1889 pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1890 pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1891 pStretchBlt->dwRop, pStretchBlt->offBitsSrc);
1893 if(pStretchBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1894 PatBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1895 pStretchBlt->dwRop);
1896 } else { /* StretchBlt */
1897 HDC hdcSrc = CreateCompatibleDC(hdc);
1898 HBRUSH hBrush, hBrushOld;
1899 HBITMAP hBmp = 0, hBmpOld = 0;
1900 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pStretchBlt->offBmiSrc);
1902 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1903 SetWorldTransform(hdcSrc, &pStretchBlt->xformSrc);
1905 hBrush = CreateSolidBrush(pStretchBlt->crBkColorSrc);
1906 hBrushOld = SelectObject(hdcSrc, hBrush);
1907 PatBlt(hdcSrc, pStretchBlt->rclBounds.left, pStretchBlt->rclBounds.top,
1908 pStretchBlt->rclBounds.right - pStretchBlt->rclBounds.left,
1909 pStretchBlt->rclBounds.bottom - pStretchBlt->rclBounds.top, PATCOPY);
1910 SelectObject(hdcSrc, hBrushOld);
1911 DeleteObject(hBrush);
1913 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1914 (const BYTE *)mr + pStretchBlt->offBitsSrc, pbi, pStretchBlt->iUsageSrc);
1915 hBmpOld = SelectObject(hdcSrc, hBmp);
1917 StretchBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1918 hdcSrc, pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1919 pStretchBlt->dwRop);
1921 SelectObject(hdcSrc, hBmpOld);
1922 DeleteObject(hBmp);
1923 DeleteDC(hdcSrc);
1925 break;
1928 case EMR_ALPHABLEND:
1930 const EMRALPHABLEND *pAlphaBlend = (const EMRALPHABLEND *)mr;
1932 TRACE("EMR_ALPHABLEND: %d, %d %dx%d -> %d, %d %dx%d. blendfn %08x offBitsSrc %d\n",
1933 pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1934 pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1935 pAlphaBlend->dwRop, pAlphaBlend->offBitsSrc);
1937 if(pAlphaBlend->offBmiSrc == 0) {
1938 FIXME("EMR_ALPHABLEND: offBmiSrc == 0\n");
1939 } else {
1940 HDC hdcSrc = CreateCompatibleDC(hdc);
1941 HBITMAP hBmp = 0, hBmpOld = 0;
1942 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pAlphaBlend->offBmiSrc);
1943 void *bits;
1945 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1946 SetWorldTransform(hdcSrc, &pAlphaBlend->xformSrc);
1948 hBmp = CreateDIBSection(hdc, pbi, pAlphaBlend->iUsageSrc, &bits, NULL, 0);
1949 memcpy(bits, (const BYTE *)mr + pAlphaBlend->offBitsSrc, pAlphaBlend->cbBitsSrc);
1950 hBmpOld = SelectObject(hdcSrc, hBmp);
1952 GdiAlphaBlend(hdc, pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1953 hdcSrc, pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1954 *(BLENDFUNCTION *)&pAlphaBlend->dwRop);
1956 SelectObject(hdcSrc, hBmpOld);
1957 DeleteObject(hBmp);
1958 DeleteDC(hdcSrc);
1960 break;
1963 case EMR_MASKBLT:
1965 const EMRMASKBLT *pMaskBlt = (const EMRMASKBLT *)mr;
1966 HDC hdcSrc = CreateCompatibleDC(hdc);
1967 HBRUSH hBrush, hBrushOld;
1968 HBITMAP hBmp, hBmpOld, hBmpMask;
1969 const BITMAPINFO *pbi;
1971 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1972 SetWorldTransform(hdcSrc, &pMaskBlt->xformSrc);
1974 hBrush = CreateSolidBrush(pMaskBlt->crBkColorSrc);
1975 hBrushOld = SelectObject(hdcSrc, hBrush);
1976 PatBlt(hdcSrc, pMaskBlt->rclBounds.left, pMaskBlt->rclBounds.top,
1977 pMaskBlt->rclBounds.right - pMaskBlt->rclBounds.left,
1978 pMaskBlt->rclBounds.bottom - pMaskBlt->rclBounds.top, PATCOPY);
1979 SelectObject(hdcSrc, hBrushOld);
1980 DeleteObject(hBrush);
1982 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiMask);
1983 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
1984 1, 1, NULL);
1985 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
1986 (const BYTE *)mr + pMaskBlt->offBitsMask, pbi, pMaskBlt->iUsageMask);
1988 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiSrc);
1989 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1990 (const BYTE *)mr + pMaskBlt->offBitsSrc, pbi, pMaskBlt->iUsageSrc);
1991 hBmpOld = SelectObject(hdcSrc, hBmp);
1992 MaskBlt(hdc,
1993 pMaskBlt->xDest,
1994 pMaskBlt->yDest,
1995 pMaskBlt->cxDest,
1996 pMaskBlt->cyDest,
1997 hdcSrc,
1998 pMaskBlt->xSrc,
1999 pMaskBlt->ySrc,
2000 hBmpMask,
2001 pMaskBlt->xMask,
2002 pMaskBlt->yMask,
2003 pMaskBlt->dwRop);
2004 SelectObject(hdcSrc, hBmpOld);
2005 DeleteObject(hBmp);
2006 DeleteObject(hBmpMask);
2007 DeleteDC(hdcSrc);
2008 break;
2011 case EMR_PLGBLT:
2013 const EMRPLGBLT *pPlgBlt = (const EMRPLGBLT *)mr;
2014 HDC hdcSrc = CreateCompatibleDC(hdc);
2015 HBRUSH hBrush, hBrushOld;
2016 HBITMAP hBmp, hBmpOld, hBmpMask;
2017 const BITMAPINFO *pbi;
2018 POINT pts[3];
2020 SetGraphicsMode(hdcSrc, GM_ADVANCED);
2021 SetWorldTransform(hdcSrc, &pPlgBlt->xformSrc);
2023 pts[0].x = pPlgBlt->aptlDest[0].x; pts[0].y = pPlgBlt->aptlDest[0].y;
2024 pts[1].x = pPlgBlt->aptlDest[1].x; pts[1].y = pPlgBlt->aptlDest[1].y;
2025 pts[2].x = pPlgBlt->aptlDest[2].x; pts[2].y = pPlgBlt->aptlDest[2].y;
2027 hBrush = CreateSolidBrush(pPlgBlt->crBkColorSrc);
2028 hBrushOld = SelectObject(hdcSrc, hBrush);
2029 PatBlt(hdcSrc, pPlgBlt->rclBounds.left, pPlgBlt->rclBounds.top,
2030 pPlgBlt->rclBounds.right - pPlgBlt->rclBounds.left,
2031 pPlgBlt->rclBounds.bottom - pPlgBlt->rclBounds.top, PATCOPY);
2032 SelectObject(hdcSrc, hBrushOld);
2033 DeleteObject(hBrush);
2035 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiMask);
2036 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
2037 1, 1, NULL);
2038 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
2039 (const BYTE *)mr + pPlgBlt->offBitsMask, pbi, pPlgBlt->iUsageMask);
2041 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiSrc);
2042 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
2043 (const BYTE *)mr + pPlgBlt->offBitsSrc, pbi, pPlgBlt->iUsageSrc);
2044 hBmpOld = SelectObject(hdcSrc, hBmp);
2045 PlgBlt(hdc,
2046 pts,
2047 hdcSrc,
2048 pPlgBlt->xSrc,
2049 pPlgBlt->ySrc,
2050 pPlgBlt->cxSrc,
2051 pPlgBlt->cySrc,
2052 hBmpMask,
2053 pPlgBlt->xMask,
2054 pPlgBlt->yMask);
2055 SelectObject(hdcSrc, hBmpOld);
2056 DeleteObject(hBmp);
2057 DeleteObject(hBmpMask);
2058 DeleteDC(hdcSrc);
2059 break;
2062 case EMR_SETDIBITSTODEVICE:
2064 const EMRSETDIBITSTODEVICE *pSetDIBitsToDevice = (const EMRSETDIBITSTODEVICE *)mr;
2066 SetDIBitsToDevice(hdc,
2067 pSetDIBitsToDevice->xDest,
2068 pSetDIBitsToDevice->yDest,
2069 pSetDIBitsToDevice->cxSrc,
2070 pSetDIBitsToDevice->cySrc,
2071 pSetDIBitsToDevice->xSrc,
2072 pSetDIBitsToDevice->ySrc,
2073 pSetDIBitsToDevice->iStartScan,
2074 pSetDIBitsToDevice->cScans,
2075 (const BYTE *)mr + pSetDIBitsToDevice->offBitsSrc,
2076 (const BITMAPINFO *)((const BYTE *)mr + pSetDIBitsToDevice->offBmiSrc),
2077 pSetDIBitsToDevice->iUsageSrc);
2078 break;
2081 case EMR_POLYTEXTOUTA:
2083 const EMRPOLYTEXTOUTA *pPolyTextOutA = (const EMRPOLYTEXTOUTA *)mr;
2084 POLYTEXTA *polytextA = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutA->cStrings * sizeof(POLYTEXTA));
2085 LONG i;
2086 XFORM xform, xformOld;
2087 int gModeOld;
2089 gModeOld = SetGraphicsMode(hdc, pPolyTextOutA->iGraphicsMode);
2090 GetWorldTransform(hdc, &xformOld);
2092 xform.eM11 = pPolyTextOutA->exScale;
2093 xform.eM12 = 0.0;
2094 xform.eM21 = 0.0;
2095 xform.eM22 = pPolyTextOutA->eyScale;
2096 xform.eDx = 0.0;
2097 xform.eDy = 0.0;
2098 SetWorldTransform(hdc, &xform);
2100 /* Set up POLYTEXTA structures */
2101 for(i = 0; i < pPolyTextOutA->cStrings; i++)
2103 polytextA[i].x = pPolyTextOutA->aemrtext[i].ptlReference.x;
2104 polytextA[i].y = pPolyTextOutA->aemrtext[i].ptlReference.y;
2105 polytextA[i].n = pPolyTextOutA->aemrtext[i].nChars;
2106 polytextA[i].lpstr = (LPCSTR)((const BYTE *)mr + pPolyTextOutA->aemrtext[i].offString);
2107 polytextA[i].uiFlags = pPolyTextOutA->aemrtext[i].fOptions;
2108 polytextA[i].rcl.left = pPolyTextOutA->aemrtext[i].rcl.left;
2109 polytextA[i].rcl.right = pPolyTextOutA->aemrtext[i].rcl.right;
2110 polytextA[i].rcl.top = pPolyTextOutA->aemrtext[i].rcl.top;
2111 polytextA[i].rcl.bottom = pPolyTextOutA->aemrtext[i].rcl.bottom;
2112 polytextA[i].pdx = (int *)((BYTE *)mr + pPolyTextOutA->aemrtext[i].offDx);
2114 PolyTextOutA(hdc, polytextA, pPolyTextOutA->cStrings);
2115 HeapFree(GetProcessHeap(), 0, polytextA);
2117 SetWorldTransform(hdc, &xformOld);
2118 SetGraphicsMode(hdc, gModeOld);
2119 break;
2122 case EMR_POLYTEXTOUTW:
2124 const EMRPOLYTEXTOUTW *pPolyTextOutW = (const EMRPOLYTEXTOUTW *)mr;
2125 POLYTEXTW *polytextW = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutW->cStrings * sizeof(POLYTEXTW));
2126 LONG i;
2127 XFORM xform, xformOld;
2128 int gModeOld;
2130 gModeOld = SetGraphicsMode(hdc, pPolyTextOutW->iGraphicsMode);
2131 GetWorldTransform(hdc, &xformOld);
2133 xform.eM11 = pPolyTextOutW->exScale;
2134 xform.eM12 = 0.0;
2135 xform.eM21 = 0.0;
2136 xform.eM22 = pPolyTextOutW->eyScale;
2137 xform.eDx = 0.0;
2138 xform.eDy = 0.0;
2139 SetWorldTransform(hdc, &xform);
2141 /* Set up POLYTEXTW structures */
2142 for(i = 0; i < pPolyTextOutW->cStrings; i++)
2144 polytextW[i].x = pPolyTextOutW->aemrtext[i].ptlReference.x;
2145 polytextW[i].y = pPolyTextOutW->aemrtext[i].ptlReference.y;
2146 polytextW[i].n = pPolyTextOutW->aemrtext[i].nChars;
2147 polytextW[i].lpstr = (LPCWSTR)((const BYTE *)mr + pPolyTextOutW->aemrtext[i].offString);
2148 polytextW[i].uiFlags = pPolyTextOutW->aemrtext[i].fOptions;
2149 polytextW[i].rcl.left = pPolyTextOutW->aemrtext[i].rcl.left;
2150 polytextW[i].rcl.right = pPolyTextOutW->aemrtext[i].rcl.right;
2151 polytextW[i].rcl.top = pPolyTextOutW->aemrtext[i].rcl.top;
2152 polytextW[i].rcl.bottom = pPolyTextOutW->aemrtext[i].rcl.bottom;
2153 polytextW[i].pdx = (int *)((BYTE *)mr + pPolyTextOutW->aemrtext[i].offDx);
2155 PolyTextOutW(hdc, polytextW, pPolyTextOutW->cStrings);
2156 HeapFree(GetProcessHeap(), 0, polytextW);
2158 SetWorldTransform(hdc, &xformOld);
2159 SetGraphicsMode(hdc, gModeOld);
2160 break;
2163 case EMR_FILLRGN:
2165 const EMRFILLRGN *pFillRgn = (const EMRFILLRGN *)mr;
2166 HRGN hRgn = ExtCreateRegion(NULL, pFillRgn->cbRgnData, (const RGNDATA *)pFillRgn->RgnData);
2167 FillRgn(hdc,
2168 hRgn,
2169 (handletable->objectHandle)[pFillRgn->ihBrush]);
2170 DeleteObject(hRgn);
2171 break;
2174 case EMR_FRAMERGN:
2176 const EMRFRAMERGN *pFrameRgn = (const EMRFRAMERGN *)mr;
2177 HRGN hRgn = ExtCreateRegion(NULL, pFrameRgn->cbRgnData, (const RGNDATA *)pFrameRgn->RgnData);
2178 FrameRgn(hdc,
2179 hRgn,
2180 (handletable->objectHandle)[pFrameRgn->ihBrush],
2181 pFrameRgn->szlStroke.cx,
2182 pFrameRgn->szlStroke.cy);
2183 DeleteObject(hRgn);
2184 break;
2187 case EMR_INVERTRGN:
2189 const EMRINVERTRGN *pInvertRgn = (const EMRINVERTRGN *)mr;
2190 HRGN hRgn = ExtCreateRegion(NULL, pInvertRgn->cbRgnData, (const RGNDATA *)pInvertRgn->RgnData);
2191 InvertRgn(hdc, hRgn);
2192 DeleteObject(hRgn);
2193 break;
2196 case EMR_PAINTRGN:
2198 const EMRPAINTRGN *pPaintRgn = (const EMRPAINTRGN *)mr;
2199 HRGN hRgn = ExtCreateRegion(NULL, pPaintRgn->cbRgnData, (const RGNDATA *)pPaintRgn->RgnData);
2200 PaintRgn(hdc, hRgn);
2201 DeleteObject(hRgn);
2202 break;
2205 case EMR_SETTEXTJUSTIFICATION:
2207 const EMRSETTEXTJUSTIFICATION *pSetTextJust = (const EMRSETTEXTJUSTIFICATION *)mr;
2208 SetTextJustification(hdc, pSetTextJust->nBreakExtra, pSetTextJust->nBreakCount);
2209 break;
2212 case EMR_SETLAYOUT:
2214 const EMRSETLAYOUT *pSetLayout = (const EMRSETLAYOUT *)mr;
2215 SetLayout(hdc, pSetLayout->iMode);
2216 break;
2219 case EMR_GRADIENTFILL:
2221 EMRGRADIENTFILL *grad = (EMRGRADIENTFILL *)mr;
2222 GdiGradientFill( hdc, grad->Ver, grad->nVer, grad->Ver + grad->nVer,
2223 grad->nTri, grad->ulMode );
2224 break;
2227 case EMR_POLYDRAW16:
2228 case EMR_GLSRECORD:
2229 case EMR_GLSBOUNDEDRECORD:
2230 case EMR_DRAWESCAPE:
2231 case EMR_EXTESCAPE:
2232 case EMR_STARTDOC:
2233 case EMR_SMALLTEXTOUT:
2234 case EMR_FORCEUFIMAPPING:
2235 case EMR_NAMEDESCAPE:
2236 case EMR_COLORCORRECTPALETTE:
2237 case EMR_SETICMPROFILEA:
2238 case EMR_SETICMPROFILEW:
2239 case EMR_TRANSPARENTBLT:
2240 case EMR_SETLINKEDUFI:
2241 case EMR_COLORMATCHTOTARGETW:
2242 case EMR_CREATECOLORSPACEW:
2244 default:
2245 /* From docs: If PlayEnhMetaFileRecord doesn't recognize a
2246 record then ignore and return TRUE. */
2247 FIXME("type %d is unimplemented\n", type);
2248 break;
2250 tmprc.left = tmprc.top = 0;
2251 tmprc.right = tmprc.bottom = 1000;
2252 LPtoDP(hdc, (POINT*)&tmprc, 2);
2253 TRACE("L:0,0 - 1000,1000 -> D:%s\n", wine_dbgstr_rect(&tmprc));
2255 return TRUE;
2259 /*****************************************************************************
2261 * EnumEnhMetaFile (GDI32.@)
2263 * Walk an enhanced metafile, calling a user-specified function _EnhMetaFunc_
2264 * for each
2265 * record. Returns when either every record has been used or
2266 * when _EnhMetaFunc_ returns FALSE.
2269 * RETURNS
2270 * TRUE if every record is used, FALSE if any invocation of _EnhMetaFunc_
2271 * returns FALSE.
2273 * BUGS
2274 * Ignores rect.
2276 * NOTES
2277 * This function behaves differently in Win9x and WinNT.
2279 * In WinNT, the DC's world transform is updated as the EMF changes
2280 * the Window/Viewport Extent and Origin or its world transform.
2281 * The actual Window/Viewport Extent and Origin are left untouched.
2283 * In Win9x, the DC is left untouched, and PlayEnhMetaFileRecord
2284 * updates the scaling itself but only just before a record that
2285 * writes anything to the DC.
2287 * I'm not sure where the data (enum_emh_data) is stored in either
2288 * version. For this implementation, it is stored before the handle
2289 * table, but it could be stored in the DC, in the EMF handle or in
2290 * TLS.
2291 * MJM 5 Oct 2002
2293 BOOL WINAPI EnumEnhMetaFile(
2294 HDC hdc, /* [in] device context to pass to _EnhMetaFunc_ */
2295 HENHMETAFILE hmf, /* [in] EMF to walk */
2296 ENHMFENUMPROC callback, /* [in] callback function */
2297 LPVOID data, /* [in] optional data for callback function */
2298 const RECT *lpRect /* [in] bounding rectangle for rendered metafile */
2301 BOOL ret;
2302 ENHMETAHEADER *emh;
2303 ENHMETARECORD *emr;
2304 DWORD offset;
2305 UINT i;
2306 HANDLETABLE *ht;
2307 INT savedMode = 0;
2308 XFORM savedXform;
2309 HPEN hPen = NULL;
2310 HBRUSH hBrush = NULL;
2311 HFONT hFont = NULL;
2312 HRGN hRgn = NULL;
2313 enum_emh_data *info;
2314 SIZE vp_size, win_size;
2315 POINT vp_org, win_org;
2316 INT mapMode = MM_TEXT, old_align = 0, old_rop2 = 0, old_arcdir = 0, old_polyfill = 0, old_stretchblt = 0;
2317 COLORREF old_text_color = 0, old_bk_color = 0;
2319 if(!lpRect && hdc)
2321 SetLastError(ERROR_INVALID_PARAMETER);
2322 return FALSE;
2325 emh = EMF_GetEnhMetaHeader(hmf);
2326 if(!emh) {
2327 SetLastError(ERROR_INVALID_HANDLE);
2328 return FALSE;
2331 info = HeapAlloc( GetProcessHeap(), 0,
2332 sizeof (enum_emh_data) + sizeof(HANDLETABLE) * emh->nHandles );
2333 if(!info)
2335 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2336 return FALSE;
2338 info->state.mode = MM_TEXT;
2339 info->state.wndOrgX = 0;
2340 info->state.wndOrgY = 0;
2341 info->state.wndExtX = 1;
2342 info->state.wndExtY = 1;
2343 info->state.vportOrgX = 0;
2344 info->state.vportOrgY = 0;
2345 info->state.vportExtX = 1;
2346 info->state.vportExtY = 1;
2347 info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
2348 info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
2349 info->state.world_transform.eDx = info->state.world_transform.eDy = 0;
2351 info->state.next = NULL;
2352 info->save_level = 0;
2353 info->saved_state = NULL;
2355 ht = (HANDLETABLE*) &info[1];
2356 ht->objectHandle[0] = hmf;
2357 for(i = 1; i < emh->nHandles; i++)
2358 ht->objectHandle[i] = NULL;
2360 if(hdc)
2362 savedMode = SetGraphicsMode(hdc, GM_ADVANCED);
2363 GetWorldTransform(hdc, &savedXform);
2364 GetViewportExtEx(hdc, &vp_size);
2365 GetWindowExtEx(hdc, &win_size);
2366 GetViewportOrgEx(hdc, &vp_org);
2367 GetWindowOrgEx(hdc, &win_org);
2368 mapMode = GetMapMode(hdc);
2370 /* save DC */
2371 hPen = GetCurrentObject(hdc, OBJ_PEN);
2372 hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
2373 hFont = GetCurrentObject(hdc, OBJ_FONT);
2375 hRgn = CreateRectRgn(0, 0, 0, 0);
2376 if (!GetClipRgn(hdc, hRgn))
2378 DeleteObject(hRgn);
2379 hRgn = 0;
2382 old_text_color = SetTextColor(hdc, RGB(0,0,0));
2383 old_bk_color = SetBkColor(hdc, RGB(0xff, 0xff, 0xff));
2384 old_align = SetTextAlign(hdc, 0);
2385 old_rop2 = SetROP2(hdc, R2_COPYPEN);
2386 old_arcdir = SetArcDirection(hdc, AD_COUNTERCLOCKWISE);
2387 old_polyfill = SetPolyFillMode(hdc, ALTERNATE);
2388 old_stretchblt = SetStretchBltMode(hdc, BLACKONWHITE);
2390 if ( IS_WIN9X() )
2392 /* Win95 leaves the vp/win ext/org info alone */
2393 info->init_transform.eM11 = 1.0;
2394 info->init_transform.eM12 = 0.0;
2395 info->init_transform.eM21 = 0.0;
2396 info->init_transform.eM22 = 1.0;
2397 info->init_transform.eDx = 0.0;
2398 info->init_transform.eDy = 0.0;
2400 else
2402 /* WinNT combines the vp/win ext/org info into a transform */
2403 double xscale, yscale;
2404 xscale = (double)vp_size.cx / (double)win_size.cx;
2405 yscale = (double)vp_size.cy / (double)win_size.cy;
2406 info->init_transform.eM11 = xscale;
2407 info->init_transform.eM12 = 0.0;
2408 info->init_transform.eM21 = 0.0;
2409 info->init_transform.eM22 = yscale;
2410 info->init_transform.eDx = (double)vp_org.x - xscale * (double)win_org.x;
2411 info->init_transform.eDy = (double)vp_org.y - yscale * (double)win_org.y;
2413 CombineTransform(&info->init_transform, &savedXform, &info->init_transform);
2416 if ( lpRect && WIDTH(emh->rclFrame) && HEIGHT(emh->rclFrame) )
2418 double xSrcPixSize, ySrcPixSize, xscale, yscale;
2419 XFORM xform;
2421 TRACE("rect: %s. rclFrame: (%d,%d)-(%d,%d)\n", wine_dbgstr_rect(lpRect),
2422 emh->rclFrame.left, emh->rclFrame.top, emh->rclFrame.right,
2423 emh->rclFrame.bottom);
2425 xSrcPixSize = (double) emh->szlMillimeters.cx / emh->szlDevice.cx;
2426 ySrcPixSize = (double) emh->szlMillimeters.cy / emh->szlDevice.cy;
2427 xscale = (double) WIDTH(*lpRect) * 100.0 /
2428 WIDTH(emh->rclFrame) * xSrcPixSize;
2429 yscale = (double) HEIGHT(*lpRect) * 100.0 /
2430 HEIGHT(emh->rclFrame) * ySrcPixSize;
2431 TRACE("xscale = %f, yscale = %f\n", xscale, yscale);
2433 xform.eM11 = xscale;
2434 xform.eM12 = 0;
2435 xform.eM21 = 0;
2436 xform.eM22 = yscale;
2437 xform.eDx = (double) lpRect->left - (double) WIDTH(*lpRect) / WIDTH(emh->rclFrame) * emh->rclFrame.left;
2438 xform.eDy = (double) lpRect->top - (double) HEIGHT(*lpRect) / HEIGHT(emh->rclFrame) * emh->rclFrame.top;
2440 CombineTransform(&info->init_transform, &xform, &info->init_transform);
2443 /* WinNT resets the current vp/win org/ext */
2444 if ( !IS_WIN9X() )
2446 SetMapMode(hdc, MM_TEXT);
2447 SetWindowOrgEx(hdc, 0, 0, NULL);
2448 SetViewportOrgEx(hdc, 0, 0, NULL);
2449 EMF_Update_MF_Xform(hdc, info);
2453 ret = TRUE;
2454 offset = 0;
2455 while(ret && offset < emh->nBytes)
2457 emr = (ENHMETARECORD *)((char *)emh + offset);
2459 /* In Win9x mode we update the xform if the record will produce output */
2460 if (hdc && IS_WIN9X() && emr_produces_output(emr->iType))
2461 EMF_Update_MF_Xform(hdc, info);
2463 TRACE("Calling EnumFunc with record %s, size %d\n", get_emr_name(emr->iType), emr->nSize);
2464 ret = (*callback)(hdc, ht, emr, emh->nHandles, (LPARAM)data);
2465 offset += emr->nSize;
2468 if (hdc)
2470 SetStretchBltMode(hdc, old_stretchblt);
2471 SetPolyFillMode(hdc, old_polyfill);
2472 SetArcDirection(hdc, old_arcdir);
2473 SetROP2(hdc, old_rop2);
2474 SetTextAlign(hdc, old_align);
2475 SetBkColor(hdc, old_bk_color);
2476 SetTextColor(hdc, old_text_color);
2478 /* restore DC */
2479 SelectObject(hdc, hBrush);
2480 SelectObject(hdc, hPen);
2481 SelectObject(hdc, hFont);
2482 ExtSelectClipRgn(hdc, hRgn, RGN_COPY);
2483 DeleteObject(hRgn);
2485 SetWorldTransform(hdc, &savedXform);
2486 if (savedMode)
2487 SetGraphicsMode(hdc, savedMode);
2488 SetMapMode(hdc, mapMode);
2489 SetWindowOrgEx(hdc, win_org.x, win_org.y, NULL);
2490 SetWindowExtEx(hdc, win_size.cx, win_size.cy, NULL);
2491 SetViewportOrgEx(hdc, vp_org.x, vp_org.y, NULL);
2492 SetViewportExtEx(hdc, vp_size.cx, vp_size.cy, NULL);
2495 for(i = 1; i < emh->nHandles; i++) /* Don't delete element 0 (hmf) */
2496 if( (ht->objectHandle)[i] )
2497 DeleteObject( (ht->objectHandle)[i] );
2499 while (info->saved_state)
2501 EMF_dc_state *state = info->saved_state;
2502 info->saved_state = info->saved_state->next;
2503 HeapFree( GetProcessHeap(), 0, state );
2505 HeapFree( GetProcessHeap(), 0, info );
2506 return ret;
2509 static INT CALLBACK EMF_PlayEnhMetaFileCallback(HDC hdc, HANDLETABLE *ht,
2510 const ENHMETARECORD *emr,
2511 INT handles, LPARAM data)
2513 return PlayEnhMetaFileRecord(hdc, ht, emr, handles);
2516 /**************************************************************************
2517 * PlayEnhMetaFile (GDI32.@)
2519 * Renders an enhanced metafile into a specified rectangle *lpRect
2520 * in device context hdc.
2522 * RETURNS
2523 * Success: TRUE
2524 * Failure: FALSE
2526 BOOL WINAPI PlayEnhMetaFile(
2527 HDC hdc, /* [in] DC to render into */
2528 HENHMETAFILE hmf, /* [in] metafile to render */
2529 const RECT *lpRect /* [in] rectangle to place metafile inside */
2532 return EnumEnhMetaFile(hdc, hmf, EMF_PlayEnhMetaFileCallback, NULL,
2533 lpRect);
2536 /*****************************************************************************
2537 * DeleteEnhMetaFile (GDI32.@)
2539 * Deletes an enhanced metafile and frees the associated storage.
2541 BOOL WINAPI DeleteEnhMetaFile(HENHMETAFILE hmf)
2543 return EMF_Delete_HENHMETAFILE( hmf );
2546 /*****************************************************************************
2547 * CopyEnhMetaFileA (GDI32.@)
2549 * Duplicate an enhanced metafile.
2553 HENHMETAFILE WINAPI CopyEnhMetaFileA(
2554 HENHMETAFILE hmfSrc,
2555 LPCSTR file)
2557 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2558 HENHMETAFILE hmfDst;
2560 if(!emrSrc) return FALSE;
2561 if (!file) {
2562 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2563 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2564 hmfDst = EMF_Create_HENHMETAFILE( emrDst, FALSE );
2565 if (!hmfDst)
2566 HeapFree( GetProcessHeap(), 0, emrDst );
2567 } else {
2568 HANDLE hFile;
2569 DWORD w;
2570 hFile = CreateFileA( file, GENERIC_WRITE | GENERIC_READ, 0,
2571 NULL, CREATE_ALWAYS, 0, 0);
2572 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2573 CloseHandle( hFile );
2574 /* Reopen file for reading only, so that apps can share
2575 read access to the file while hmf is still valid */
2576 hFile = CreateFileA( file, GENERIC_READ, FILE_SHARE_READ,
2577 NULL, OPEN_EXISTING, 0, 0);
2578 if(hFile == INVALID_HANDLE_VALUE) {
2579 ERR("Can't reopen emf for reading\n");
2580 return 0;
2582 hmfDst = EMF_GetEnhMetaFile( hFile );
2583 CloseHandle( hFile );
2585 return hmfDst;
2588 /*****************************************************************************
2589 * CopyEnhMetaFileW (GDI32.@)
2591 * See CopyEnhMetaFileA.
2595 HENHMETAFILE WINAPI CopyEnhMetaFileW(
2596 HENHMETAFILE hmfSrc,
2597 LPCWSTR file)
2599 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2600 HENHMETAFILE hmfDst;
2602 if(!emrSrc) return FALSE;
2603 if (!file) {
2604 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2605 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2606 hmfDst = EMF_Create_HENHMETAFILE( emrDst, FALSE );
2607 if (!hmfDst)
2608 HeapFree( GetProcessHeap(), 0, emrDst );
2609 } else {
2610 HANDLE hFile;
2611 DWORD w;
2612 hFile = CreateFileW( file, GENERIC_WRITE | GENERIC_READ, 0,
2613 NULL, CREATE_ALWAYS, 0, 0);
2614 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2615 CloseHandle( hFile );
2616 /* Reopen file for reading only, so that apps can share
2617 read access to the file while hmf is still valid */
2618 hFile = CreateFileW( file, GENERIC_READ, FILE_SHARE_READ,
2619 NULL, OPEN_EXISTING, 0, 0);
2620 if(hFile == INVALID_HANDLE_VALUE) {
2621 ERR("Can't reopen emf for reading\n");
2622 return 0;
2624 hmfDst = EMF_GetEnhMetaFile( hFile );
2625 CloseHandle( hFile );
2627 return hmfDst;
2631 /* Struct to be used to be passed in the LPVOID parameter for cbEnhPaletteCopy */
2632 typedef struct tagEMF_PaletteCopy
2634 UINT cEntries;
2635 LPPALETTEENTRY lpPe;
2636 } EMF_PaletteCopy;
2638 /***************************************************************
2639 * Find the EMR_EOF record and then use it to find the
2640 * palette entries for this enhanced metafile.
2641 * The lpData is actually a pointer to an EMF_PaletteCopy struct
2642 * which contains the max number of elements to copy and where
2643 * to copy them to.
2645 * NOTE: To be used by GetEnhMetaFilePaletteEntries only!
2647 static INT CALLBACK cbEnhPaletteCopy( HDC a,
2648 HANDLETABLE *b,
2649 const ENHMETARECORD *lpEMR,
2650 INT c,
2651 LPARAM lpData )
2654 if ( lpEMR->iType == EMR_EOF )
2656 const EMREOF *lpEof = (const EMREOF *)lpEMR;
2657 EMF_PaletteCopy* info = (EMF_PaletteCopy*)lpData;
2658 DWORD dwNumPalToCopy = min( lpEof->nPalEntries, info->cEntries );
2660 TRACE( "copying 0x%08x palettes\n", dwNumPalToCopy );
2662 memcpy( info->lpPe, (LPCSTR)lpEof + lpEof->offPalEntries,
2663 sizeof( *(info->lpPe) ) * dwNumPalToCopy );
2665 /* Update the passed data as a return code */
2666 info->lpPe = NULL; /* Palettes were copied! */
2667 info->cEntries = dwNumPalToCopy;
2669 return FALSE; /* That's all we need */
2672 return TRUE;
2675 /*****************************************************************************
2676 * GetEnhMetaFilePaletteEntries (GDI32.@)
2678 * Copy the palette and report size
2680 * BUGS: Error codes (SetLastError) are not set on failures
2682 UINT WINAPI GetEnhMetaFilePaletteEntries( HENHMETAFILE hEmf,
2683 UINT cEntries,
2684 LPPALETTEENTRY lpPe )
2686 ENHMETAHEADER* enhHeader = EMF_GetEnhMetaHeader( hEmf );
2687 EMF_PaletteCopy infoForCallBack;
2689 TRACE( "(%p,%d,%p)\n", hEmf, cEntries, lpPe );
2691 if (!enhHeader) return 0;
2693 /* First check if there are any palettes associated with
2694 this metafile. */
2695 if ( enhHeader->nPalEntries == 0 ) return 0;
2697 /* Is the user requesting the number of palettes? */
2698 if ( lpPe == NULL ) return enhHeader->nPalEntries;
2700 /* Copy cEntries worth of PALETTEENTRY structs into the buffer */
2701 infoForCallBack.cEntries = cEntries;
2702 infoForCallBack.lpPe = lpPe;
2704 if ( !EnumEnhMetaFile( 0, hEmf, cbEnhPaletteCopy,
2705 &infoForCallBack, 0 ) )
2706 return GDI_ERROR;
2708 /* Verify that the callback executed correctly */
2709 if ( infoForCallBack.lpPe != NULL )
2711 /* Callback proc had error! */
2712 ERR( "cbEnhPaletteCopy didn't execute correctly\n" );
2713 return GDI_ERROR;
2716 return infoForCallBack.cEntries;
2719 /******************************************************************
2720 * extract_emf_from_comment
2722 * If the WMF was created by GetWinMetaFileBits, then extract the
2723 * original EMF that is stored in MFCOMMENT chunks.
2725 static HENHMETAFILE extract_emf_from_comment( const BYTE *buf, UINT mf_size )
2727 METAHEADER *mh = (METAHEADER *)buf;
2728 METARECORD *mr;
2729 emf_in_wmf_comment *chunk;
2730 WORD checksum = 0;
2731 DWORD size = 0, remaining, chunks;
2732 BYTE *emf_bits = NULL, *ptr;
2733 UINT offset;
2734 HENHMETAFILE emf = NULL;
2736 if (mf_size < sizeof(*mh)) return NULL;
2738 for (offset = mh->mtHeaderSize * 2; offset < mf_size; offset += (mr->rdSize * 2))
2740 mr = (METARECORD *)((char *)mh + offset);
2741 chunk = (emf_in_wmf_comment *)(mr->rdParm + 2);
2743 if (mr->rdFunction != META_ESCAPE || mr->rdParm[0] != MFCOMMENT) goto done;
2744 if (chunk->magic != WMFC_MAGIC) goto done;
2746 if (!emf_bits)
2748 size = remaining = chunk->emf_size;
2749 chunks = chunk->num_chunks;
2750 emf_bits = ptr = HeapAlloc( GetProcessHeap(), 0, size );
2751 if (!emf_bits) goto done;
2753 if (chunk->chunk_size > remaining) goto done;
2754 remaining -= chunk->chunk_size;
2755 if (chunk->remaining_size != remaining) goto done;
2756 memcpy( ptr, chunk->emf_data, chunk->chunk_size );
2757 ptr += chunk->chunk_size;
2758 if (--chunks == 0) break;
2761 for (offset = 0; offset < mf_size / 2; offset++)
2762 checksum += *((WORD *)buf + offset);
2763 if (checksum) goto done;
2765 emf = SetEnhMetaFileBits( size, emf_bits );
2767 done:
2768 HeapFree( GetProcessHeap(), 0, emf_bits );
2769 return emf;
2772 typedef struct wmf_in_emf_comment
2774 DWORD ident;
2775 DWORD iComment;
2776 DWORD nVersion;
2777 DWORD nChecksum;
2778 DWORD fFlags;
2779 DWORD cbWinMetaFile;
2780 } wmf_in_emf_comment;
2782 /******************************************************************
2783 * SetWinMetaFileBits (GDI32.@)
2785 * Translate from old style to new style.
2788 HENHMETAFILE WINAPI SetWinMetaFileBits(UINT cbBuffer, const BYTE *lpbBuffer, HDC hdcRef,
2789 const METAFILEPICT *lpmfp)
2791 static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
2792 HMETAFILE hmf = NULL;
2793 HENHMETAFILE ret = NULL;
2794 HDC hdc = NULL, hdcdisp = NULL;
2795 RECT rc, *prcFrame = NULL;
2796 LONG mm, xExt, yExt;
2797 INT horzsize, vertsize, horzres, vertres;
2799 TRACE("(%d, %p, %p, %p)\n", cbBuffer, lpbBuffer, hdcRef, lpmfp);
2801 hmf = SetMetaFileBitsEx(cbBuffer, lpbBuffer);
2802 if(!hmf)
2804 WARN("SetMetaFileBitsEx failed\n");
2805 return NULL;
2808 ret = extract_emf_from_comment( lpbBuffer, cbBuffer );
2809 if (ret) return ret;
2811 if(!hdcRef)
2812 hdcRef = hdcdisp = CreateDCW(szDisplayW, NULL, NULL, NULL);
2814 if (lpmfp)
2816 TRACE("mm = %d %dx%d\n", lpmfp->mm, lpmfp->xExt, lpmfp->yExt);
2818 mm = lpmfp->mm;
2819 xExt = lpmfp->xExt;
2820 yExt = lpmfp->yExt;
2822 else
2824 TRACE("lpmfp == NULL\n");
2826 /* Use the whole device surface */
2827 mm = MM_ANISOTROPIC;
2828 xExt = 0;
2829 yExt = 0;
2832 if (mm == MM_ISOTROPIC || mm == MM_ANISOTROPIC)
2834 if (xExt < 0 || yExt < 0)
2836 /* Use the whole device surface */
2837 xExt = 0;
2838 yExt = 0;
2841 /* Use the x and y extents as the frame box */
2842 if (xExt && yExt)
2844 rc.left = rc.top = 0;
2845 rc.right = xExt;
2846 rc.bottom = yExt;
2847 prcFrame = &rc;
2851 if(!(hdc = CreateEnhMetaFileW(hdcRef, NULL, prcFrame, NULL)))
2853 ERR("CreateEnhMetaFile failed\n");
2854 goto end;
2858 * Write the original METAFILE into the enhanced metafile.
2859 * It is encapsulated in a GDICOMMENT_WINDOWS_METAFILE record.
2861 if (mm != MM_TEXT)
2863 wmf_in_emf_comment *mfcomment;
2864 UINT mfcomment_size;
2866 mfcomment_size = sizeof (*mfcomment) + cbBuffer;
2867 mfcomment = HeapAlloc(GetProcessHeap(), 0, mfcomment_size);
2868 if (mfcomment)
2870 mfcomment->ident = GDICOMMENT_IDENTIFIER;
2871 mfcomment->iComment = GDICOMMENT_WINDOWS_METAFILE;
2872 mfcomment->nVersion = 0x00000300;
2873 mfcomment->nChecksum = 0; /* FIXME */
2874 mfcomment->fFlags = 0;
2875 mfcomment->cbWinMetaFile = cbBuffer;
2876 memcpy(&mfcomment[1], lpbBuffer, cbBuffer);
2877 GdiComment(hdc, mfcomment_size, (BYTE*) mfcomment);
2878 HeapFree(GetProcessHeap(), 0, mfcomment);
2880 SetMapMode(hdc, mm);
2884 horzsize = GetDeviceCaps(hdcRef, HORZSIZE);
2885 vertsize = GetDeviceCaps(hdcRef, VERTSIZE);
2886 horzres = GetDeviceCaps(hdcRef, HORZRES);
2887 vertres = GetDeviceCaps(hdcRef, VERTRES);
2889 if (!xExt || !yExt)
2891 /* Use the whole device surface */
2892 xExt = horzres;
2893 yExt = vertres;
2895 else
2897 xExt = MulDiv(xExt, horzres, 100 * horzsize);
2898 yExt = MulDiv(yExt, vertres, 100 * vertsize);
2901 /* set the initial viewport:window ratio as 1:1 */
2902 SetViewportExtEx(hdc, xExt, yExt, NULL);
2903 SetWindowExtEx(hdc, xExt, yExt, NULL);
2905 PlayMetaFile(hdc, hmf);
2907 ret = CloseEnhMetaFile(hdc);
2908 end:
2909 if (hdcdisp) DeleteDC(hdcdisp);
2910 DeleteMetaFile(hmf);
2911 return ret;