quartz/avidec: Call IMemInputPin::Receive() directly.
[wine.git] / dlls / gdi32 / enhmetafile.c
blobfc3389ff23f58e6bac042c0959caf3e13f2e8246
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 < ARRAY_SIZE(emr_names); 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, DWORD filesize, BOOL on_disk )
251 HENHMETAFILE hmf;
252 ENHMETAFILEOBJ *metaObj;
254 if (filesize < sizeof(*emh))
256 WARN("File too small for emf header\n");
257 return 0;
259 if (emh->iType != EMR_HEADER)
261 SetLastError(ERROR_INVALID_DATA);
262 return 0;
264 if (emh->dSignature != ENHMETA_SIGNATURE ||
265 (emh->nBytes & 3)) /* refuse to load unaligned EMF as Windows does */
267 WARN("Invalid emf header type 0x%08x sig 0x%08x.\n",
268 emh->iType, emh->dSignature);
269 return 0;
271 if (filesize < emh->nBytes)
273 WARN("File truncated (got %u bytes, header says %u)\n", emh->nBytes, filesize);
274 return 0;
277 if (!(metaObj = HeapAlloc( GetProcessHeap(), 0, sizeof(*metaObj) ))) return 0;
279 metaObj->emh = emh;
280 metaObj->on_disk = on_disk;
282 if (!(hmf = alloc_gdi_handle( metaObj, OBJ_ENHMETAFILE, NULL )))
283 HeapFree( GetProcessHeap(), 0, metaObj );
284 return hmf;
287 /****************************************************************************
288 * EMF_Delete_HENHMETAFILE
290 static BOOL EMF_Delete_HENHMETAFILE( HENHMETAFILE hmf )
292 ENHMETAFILEOBJ *metaObj = free_gdi_handle( hmf );
294 if(!metaObj) return FALSE;
296 if(metaObj->on_disk)
297 UnmapViewOfFile( metaObj->emh );
298 else
299 HeapFree( GetProcessHeap(), 0, metaObj->emh );
300 HeapFree( GetProcessHeap(), 0, metaObj );
301 return TRUE;
304 /******************************************************************
305 * EMF_GetEnhMetaHeader
307 * Returns ptr to ENHMETAHEADER associated with HENHMETAFILE
309 static ENHMETAHEADER *EMF_GetEnhMetaHeader( HENHMETAFILE hmf )
311 ENHMETAHEADER *ret = NULL;
312 ENHMETAFILEOBJ *metaObj = GDI_GetObjPtr( hmf, OBJ_ENHMETAFILE );
313 TRACE("hmf %p -> enhmetaObj %p\n", hmf, metaObj);
314 if (metaObj)
316 ret = metaObj->emh;
317 GDI_ReleaseObj( hmf );
319 return ret;
322 /*****************************************************************************
323 * EMF_GetEnhMetaFile
326 static HENHMETAFILE EMF_GetEnhMetaFile( HANDLE hFile )
328 ENHMETAHEADER *emh;
329 HANDLE hMapping;
330 HENHMETAFILE hemf;
331 DWORD filesize;
333 filesize = GetFileSize( hFile, NULL );
335 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
336 emh = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, filesize );
337 CloseHandle( hMapping );
339 if (!emh) return 0;
341 hemf = EMF_Create_HENHMETAFILE( emh, filesize, TRUE );
342 if (!hemf)
343 UnmapViewOfFile( emh );
344 return hemf;
348 /*****************************************************************************
349 * GetEnhMetaFileA (GDI32.@)
353 HENHMETAFILE WINAPI GetEnhMetaFileA(
354 LPCSTR lpszMetaFile /* [in] filename of enhanced metafile */
357 HENHMETAFILE hmf;
358 HANDLE hFile;
360 hFile = CreateFileA(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
361 OPEN_EXISTING, 0, 0);
362 if (hFile == INVALID_HANDLE_VALUE) {
363 WARN("could not open %s\n", lpszMetaFile);
364 return 0;
366 hmf = EMF_GetEnhMetaFile( hFile );
367 CloseHandle( hFile );
368 return hmf;
371 /*****************************************************************************
372 * GetEnhMetaFileW (GDI32.@)
374 HENHMETAFILE WINAPI GetEnhMetaFileW(
375 LPCWSTR lpszMetaFile) /* [in] filename of enhanced metafile */
377 HENHMETAFILE hmf;
378 HANDLE hFile;
380 hFile = CreateFileW(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
381 OPEN_EXISTING, 0, 0);
382 if (hFile == INVALID_HANDLE_VALUE) {
383 WARN("could not open %s\n", debugstr_w(lpszMetaFile));
384 return 0;
386 hmf = EMF_GetEnhMetaFile( hFile );
387 CloseHandle( hFile );
388 return hmf;
391 /*****************************************************************************
392 * GetEnhMetaFileHeader (GDI32.@)
394 * Retrieves the record containing the header for the specified
395 * enhanced-format metafile.
397 * RETURNS
398 * If buf is NULL, returns the size of buffer required.
399 * Otherwise, copy up to bufsize bytes of enhanced metafile header into
400 * buf.
402 UINT WINAPI GetEnhMetaFileHeader(
403 HENHMETAFILE hmf, /* [in] enhanced metafile */
404 UINT bufsize, /* [in] size of buffer */
405 LPENHMETAHEADER buf /* [out] buffer */
408 LPENHMETAHEADER emh;
409 UINT size;
411 emh = EMF_GetEnhMetaHeader(hmf);
412 if(!emh) return FALSE;
413 size = emh->nSize;
414 if (!buf) return size;
415 size = min(size, bufsize);
416 memmove(buf, emh, size);
417 return size;
421 /*****************************************************************************
422 * GetEnhMetaFileDescriptionA (GDI32.@)
424 * See GetEnhMetaFileDescriptionW.
426 UINT WINAPI GetEnhMetaFileDescriptionA(
427 HENHMETAFILE hmf, /* [in] enhanced metafile */
428 UINT size, /* [in] size of buf */
429 LPSTR buf /* [out] buffer to receive description */
432 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
433 DWORD len;
434 WCHAR *descrW;
436 if(!emh) return FALSE;
437 if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
438 descrW = (WCHAR *) ((char *) emh + emh->offDescription);
439 len = WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, NULL, 0, NULL, NULL );
441 if (!buf || !size ) return len;
443 len = min( size, len );
444 WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, buf, len, NULL, NULL );
445 return len;
448 /*****************************************************************************
449 * GetEnhMetaFileDescriptionW (GDI32.@)
451 * Copies the description string of an enhanced metafile into a buffer
452 * _buf_.
454 * RETURNS
455 * If _buf_ is NULL, returns size of _buf_ required. Otherwise, returns
456 * number of characters copied.
458 UINT WINAPI GetEnhMetaFileDescriptionW(
459 HENHMETAFILE hmf, /* [in] enhanced metafile */
460 UINT size, /* [in] size of buf */
461 LPWSTR buf /* [out] buffer to receive description */
464 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
466 if(!emh) return FALSE;
467 if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
468 if (!buf || !size ) return emh->nDescription;
470 memmove(buf, (char *) emh + emh->offDescription, min(size,emh->nDescription)*sizeof(WCHAR));
471 return min(size, emh->nDescription);
474 /****************************************************************************
475 * SetEnhMetaFileBits (GDI32.@)
477 * Creates an enhanced metafile by copying _bufsize_ bytes from _buf_.
479 HENHMETAFILE WINAPI SetEnhMetaFileBits(UINT bufsize, const BYTE *buf)
481 ENHMETAHEADER *emh = HeapAlloc( GetProcessHeap(), 0, bufsize );
482 HENHMETAFILE hmf;
484 if (!emh) return 0;
485 memcpy(emh, buf, bufsize);
486 hmf = EMF_Create_HENHMETAFILE( emh, bufsize, FALSE );
487 if (!hmf)
488 HeapFree( GetProcessHeap(), 0, emh );
489 return hmf;
492 /*****************************************************************************
493 * GetEnhMetaFileBits (GDI32.@)
496 UINT WINAPI GetEnhMetaFileBits(
497 HENHMETAFILE hmf,
498 UINT bufsize,
499 LPBYTE buf
502 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader( hmf );
503 UINT size;
505 if(!emh) return 0;
507 size = emh->nBytes;
508 if( buf == NULL ) return size;
510 size = min( size, bufsize );
511 memmove(buf, emh, size);
512 return size;
515 typedef struct EMF_dc_state
517 INT mode;
518 XFORM world_transform;
519 INT wndOrgX;
520 INT wndOrgY;
521 INT wndExtX;
522 INT wndExtY;
523 INT vportOrgX;
524 INT vportOrgY;
525 INT vportExtX;
526 INT vportExtY;
527 struct EMF_dc_state *next;
528 } EMF_dc_state;
530 typedef struct enum_emh_data
532 XFORM init_transform;
533 EMF_dc_state state;
534 INT save_level;
535 EMF_dc_state *saved_state;
536 } enum_emh_data;
538 #define ENUM_GET_PRIVATE_DATA(ht) \
539 ((enum_emh_data*)(((unsigned char*)(ht))-sizeof (enum_emh_data)))
541 #define WIDTH(rect) ( (rect).right - (rect).left )
542 #define HEIGHT(rect) ( (rect).bottom - (rect).top )
544 #define IS_WIN9X() (GetVersion()&0x80000000)
546 static void EMF_Update_MF_Xform(HDC hdc, const enum_emh_data *info)
548 XFORM mapping_mode_trans, final_trans;
549 double scaleX, scaleY;
551 scaleX = (double)info->state.vportExtX / (double)info->state.wndExtX;
552 scaleY = (double)info->state.vportExtY / (double)info->state.wndExtY;
553 mapping_mode_trans.eM11 = scaleX;
554 mapping_mode_trans.eM12 = 0.0;
555 mapping_mode_trans.eM21 = 0.0;
556 mapping_mode_trans.eM22 = scaleY;
557 mapping_mode_trans.eDx = (double)info->state.vportOrgX - scaleX * (double)info->state.wndOrgX;
558 mapping_mode_trans.eDy = (double)info->state.vportOrgY - scaleY * (double)info->state.wndOrgY;
560 CombineTransform(&final_trans, &info->state.world_transform, &mapping_mode_trans);
561 CombineTransform(&final_trans, &final_trans, &info->init_transform);
563 if (!SetWorldTransform(hdc, &final_trans))
565 ERR("World transform failed!\n");
569 static void EMF_RestoreDC( enum_emh_data *info, INT level )
571 if (abs(level) > info->save_level || level == 0) return;
573 if (level < 0) level = info->save_level + level + 1;
575 while (info->save_level >= level)
577 EMF_dc_state *state = info->saved_state;
578 info->saved_state = state->next;
579 state->next = NULL;
580 if (--info->save_level < level)
581 info->state = *state;
582 HeapFree( GetProcessHeap(), 0, state );
586 static void EMF_SaveDC( enum_emh_data *info )
588 EMF_dc_state *state = HeapAlloc( GetProcessHeap(), 0, sizeof(*state));
589 if (state)
591 *state = info->state;
592 state->next = info->saved_state;
593 info->saved_state = state;
594 info->save_level++;
595 TRACE("save_level %d\n", info->save_level);
599 static void EMF_SetMapMode(HDC hdc, enum_emh_data *info)
601 INT horzSize = GetDeviceCaps( hdc, HORZSIZE );
602 INT vertSize = GetDeviceCaps( hdc, VERTSIZE );
603 INT horzRes = GetDeviceCaps( hdc, HORZRES );
604 INT vertRes = GetDeviceCaps( hdc, VERTRES );
606 TRACE("%d\n", info->state.mode);
608 switch(info->state.mode)
610 case MM_TEXT:
611 info->state.wndExtX = 1;
612 info->state.wndExtY = 1;
613 info->state.vportExtX = 1;
614 info->state.vportExtY = 1;
615 break;
616 case MM_LOMETRIC:
617 case MM_ISOTROPIC:
618 info->state.wndExtX = horzSize * 10;
619 info->state.wndExtY = vertSize * 10;
620 info->state.vportExtX = horzRes;
621 info->state.vportExtY = -vertRes;
622 break;
623 case MM_HIMETRIC:
624 info->state.wndExtX = horzSize * 100;
625 info->state.wndExtY = vertSize * 100;
626 info->state.vportExtX = horzRes;
627 info->state.vportExtY = -vertRes;
628 break;
629 case MM_LOENGLISH:
630 info->state.wndExtX = MulDiv(1000, horzSize, 254);
631 info->state.wndExtY = MulDiv(1000, vertSize, 254);
632 info->state.vportExtX = horzRes;
633 info->state.vportExtY = -vertRes;
634 break;
635 case MM_HIENGLISH:
636 info->state.wndExtX = MulDiv(10000, horzSize, 254);
637 info->state.wndExtY = MulDiv(10000, vertSize, 254);
638 info->state.vportExtX = horzRes;
639 info->state.vportExtY = -vertRes;
640 break;
641 case MM_TWIPS:
642 info->state.wndExtX = MulDiv(14400, horzSize, 254);
643 info->state.wndExtY = MulDiv(14400, vertSize, 254);
644 info->state.vportExtX = horzRes;
645 info->state.vportExtY = -vertRes;
646 break;
647 case MM_ANISOTROPIC:
648 break;
649 default:
650 return;
654 /***********************************************************************
655 * EMF_FixIsotropic
657 * Fix viewport extensions for isotropic mode.
660 static void EMF_FixIsotropic(HDC hdc, enum_emh_data *info)
662 double xdim = fabs((double)info->state.vportExtX * GetDeviceCaps( hdc, HORZSIZE ) /
663 (GetDeviceCaps( hdc, HORZRES ) * info->state.wndExtX));
664 double ydim = fabs((double)info->state.vportExtY * GetDeviceCaps( hdc, VERTSIZE ) /
665 (GetDeviceCaps( hdc, VERTRES ) * info->state.wndExtY));
667 if (xdim > ydim)
669 INT mincx = (info->state.vportExtX >= 0) ? 1 : -1;
670 info->state.vportExtX = floor(info->state.vportExtX * ydim / xdim + 0.5);
671 if (!info->state.vportExtX) info->state.vportExtX = mincx;
673 else
675 INT mincy = (info->state.vportExtY >= 0) ? 1 : -1;
676 info->state.vportExtY = floor(info->state.vportExtY * xdim / ydim + 0.5);
677 if (!info->state.vportExtY) info->state.vportExtY = mincy;
681 /*****************************************************************************
682 * emr_produces_output
684 * Returns TRUE if the record type writes something to the dc. Used by
685 * PlayEnhMetaFileRecord to determine whether it needs to update the
686 * dc's xform when in win9x mode.
688 * FIXME: need to test which records should be here.
690 static BOOL emr_produces_output(int type)
692 switch(type) {
693 case EMR_POLYBEZIER:
694 case EMR_POLYGON:
695 case EMR_POLYLINE:
696 case EMR_POLYBEZIERTO:
697 case EMR_POLYLINETO:
698 case EMR_POLYPOLYLINE:
699 case EMR_POLYPOLYGON:
700 case EMR_SETPIXELV:
701 case EMR_MOVETOEX:
702 case EMR_EXCLUDECLIPRECT:
703 case EMR_INTERSECTCLIPRECT:
704 case EMR_SELECTOBJECT:
705 case EMR_ANGLEARC:
706 case EMR_ELLIPSE:
707 case EMR_RECTANGLE:
708 case EMR_ROUNDRECT:
709 case EMR_ARC:
710 case EMR_CHORD:
711 case EMR_PIE:
712 case EMR_EXTFLOODFILL:
713 case EMR_LINETO:
714 case EMR_ARCTO:
715 case EMR_POLYDRAW:
716 case EMR_GDICOMMENT:
717 case EMR_FILLRGN:
718 case EMR_FRAMERGN:
719 case EMR_INVERTRGN:
720 case EMR_PAINTRGN:
721 case EMR_BITBLT:
722 case EMR_STRETCHBLT:
723 case EMR_MASKBLT:
724 case EMR_PLGBLT:
725 case EMR_SETDIBITSTODEVICE:
726 case EMR_STRETCHDIBITS:
727 case EMR_EXTTEXTOUTA:
728 case EMR_EXTTEXTOUTW:
729 case EMR_POLYBEZIER16:
730 case EMR_POLYGON16:
731 case EMR_POLYLINE16:
732 case EMR_POLYBEZIERTO16:
733 case EMR_POLYLINETO16:
734 case EMR_POLYPOLYLINE16:
735 case EMR_POLYPOLYGON16:
736 case EMR_POLYDRAW16:
737 case EMR_POLYTEXTOUTA:
738 case EMR_POLYTEXTOUTW:
739 case EMR_SMALLTEXTOUT:
740 case EMR_ALPHABLEND:
741 case EMR_TRANSPARENTBLT:
742 return TRUE;
743 default:
744 return FALSE;
749 /*****************************************************************************
750 * PlayEnhMetaFileRecord (GDI32.@)
752 * Render a single enhanced metafile record in the device context hdc.
754 * RETURNS
755 * TRUE (non zero) on success, FALSE on error.
756 * BUGS
757 * Many unimplemented records.
758 * No error handling on record play failures (ie checking return codes)
760 * NOTES
761 * WinNT actually updates the current world transform in this function
762 * whereas Win9x does not.
764 BOOL WINAPI PlayEnhMetaFileRecord(
765 HDC hdc, /* [in] device context in which to render EMF record */
766 LPHANDLETABLE handletable, /* [in] array of handles to be used in rendering record */
767 const ENHMETARECORD *mr, /* [in] EMF record to render */
768 UINT handles /* [in] size of handle array */
771 int type;
772 RECT tmprc;
773 enum_emh_data *info = ENUM_GET_PRIVATE_DATA(handletable);
775 TRACE("hdc = %p, handletable = %p, record = %p, numHandles = %d\n",
776 hdc, handletable, mr, handles);
777 if (!mr) return FALSE;
779 type = mr->iType;
781 TRACE("record %s\n", get_emr_name(type));
782 switch(type)
784 case EMR_HEADER:
785 break;
786 case EMR_EOF:
787 break;
788 case EMR_GDICOMMENT:
790 const EMRGDICOMMENT *lpGdiComment = (const EMRGDICOMMENT *)mr;
791 /* In an enhanced metafile, there can be both public and private GDI comments */
792 GdiComment( hdc, lpGdiComment->cbData, lpGdiComment->Data );
793 break;
795 case EMR_SETMAPMODE:
797 const EMRSETMAPMODE *pSetMapMode = (const EMRSETMAPMODE *)mr;
799 if (info->state.mode == pSetMapMode->iMode &&
800 (info->state.mode == MM_ISOTROPIC || info->state.mode == MM_ANISOTROPIC))
801 break;
802 info->state.mode = pSetMapMode->iMode;
803 EMF_SetMapMode(hdc, info);
805 if (!IS_WIN9X())
806 EMF_Update_MF_Xform(hdc, info);
808 break;
810 case EMR_SETBKMODE:
812 const EMRSETBKMODE *pSetBkMode = (const EMRSETBKMODE *)mr;
813 SetBkMode(hdc, pSetBkMode->iMode);
814 break;
816 case EMR_SETBKCOLOR:
818 const EMRSETBKCOLOR *pSetBkColor = (const EMRSETBKCOLOR *)mr;
819 SetBkColor(hdc, pSetBkColor->crColor);
820 break;
822 case EMR_SETPOLYFILLMODE:
824 const EMRSETPOLYFILLMODE *pSetPolyFillMode = (const EMRSETPOLYFILLMODE *)mr;
825 SetPolyFillMode(hdc, pSetPolyFillMode->iMode);
826 break;
828 case EMR_SETROP2:
830 const EMRSETROP2 *pSetROP2 = (const EMRSETROP2 *)mr;
831 SetROP2(hdc, pSetROP2->iMode);
832 break;
834 case EMR_SETSTRETCHBLTMODE:
836 const EMRSETSTRETCHBLTMODE *pSetStretchBltMode = (const EMRSETSTRETCHBLTMODE *)mr;
837 SetStretchBltMode(hdc, pSetStretchBltMode->iMode);
838 break;
840 case EMR_SETTEXTALIGN:
842 const EMRSETTEXTALIGN *pSetTextAlign = (const EMRSETTEXTALIGN *)mr;
843 SetTextAlign(hdc, pSetTextAlign->iMode);
844 break;
846 case EMR_SETTEXTCOLOR:
848 const EMRSETTEXTCOLOR *pSetTextColor = (const EMRSETTEXTCOLOR *)mr;
849 SetTextColor(hdc, pSetTextColor->crColor);
850 break;
852 case EMR_SAVEDC:
854 if (SaveDC( hdc ))
855 EMF_SaveDC( info );
856 break;
858 case EMR_RESTOREDC:
860 const EMRRESTOREDC *pRestoreDC = (const EMRRESTOREDC *)mr;
861 TRACE("EMR_RESTORE: %d\n", pRestoreDC->iRelative);
862 if (RestoreDC( hdc, pRestoreDC->iRelative ))
863 EMF_RestoreDC( info, pRestoreDC->iRelative );
864 break;
866 case EMR_INTERSECTCLIPRECT:
868 const EMRINTERSECTCLIPRECT *pClipRect = (const EMRINTERSECTCLIPRECT *)mr;
869 TRACE("EMR_INTERSECTCLIPRECT: rect %d,%d - %d, %d\n",
870 pClipRect->rclClip.left, pClipRect->rclClip.top,
871 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
872 IntersectClipRect(hdc, pClipRect->rclClip.left, pClipRect->rclClip.top,
873 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
874 break;
876 case EMR_SELECTOBJECT:
878 const EMRSELECTOBJECT *pSelectObject = (const EMRSELECTOBJECT *)mr;
879 if( pSelectObject->ihObject & 0x80000000 ) {
880 /* High order bit is set - it's a stock object
881 * Strip the high bit to get the index.
882 * See MSDN article Q142319
884 SelectObject( hdc, GetStockObject( pSelectObject->ihObject &
885 0x7fffffff ) );
886 } else {
887 /* High order bit wasn't set - not a stock object
889 SelectObject( hdc,
890 (handletable->objectHandle)[pSelectObject->ihObject] );
892 break;
894 case EMR_DELETEOBJECT:
896 const EMRDELETEOBJECT *pDeleteObject = (const EMRDELETEOBJECT *)mr;
897 DeleteObject( (handletable->objectHandle)[pDeleteObject->ihObject]);
898 (handletable->objectHandle)[pDeleteObject->ihObject] = 0;
899 break;
901 case EMR_SETWINDOWORGEX:
903 const EMRSETWINDOWORGEX *pSetWindowOrgEx = (const EMRSETWINDOWORGEX *)mr;
905 info->state.wndOrgX = pSetWindowOrgEx->ptlOrigin.x;
906 info->state.wndOrgY = pSetWindowOrgEx->ptlOrigin.y;
908 TRACE("SetWindowOrgEx: %d,%d\n", info->state.wndOrgX, info->state.wndOrgY);
910 if (!IS_WIN9X())
911 EMF_Update_MF_Xform(hdc, info);
913 break;
915 case EMR_SETWINDOWEXTEX:
917 const EMRSETWINDOWEXTEX *pSetWindowExtEx = (const EMRSETWINDOWEXTEX *)mr;
919 if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
920 break;
921 info->state.wndExtX = pSetWindowExtEx->szlExtent.cx;
922 info->state.wndExtY = pSetWindowExtEx->szlExtent.cy;
923 if (info->state.mode == MM_ISOTROPIC)
924 EMF_FixIsotropic(hdc, info);
926 TRACE("SetWindowExtEx: %d,%d\n",info->state.wndExtX, info->state.wndExtY);
928 if (!IS_WIN9X())
929 EMF_Update_MF_Xform(hdc, info);
931 break;
933 case EMR_SETVIEWPORTORGEX:
935 const EMRSETVIEWPORTORGEX *pSetViewportOrgEx = (const EMRSETVIEWPORTORGEX *)mr;
937 info->state.vportOrgX = pSetViewportOrgEx->ptlOrigin.x;
938 info->state.vportOrgY = pSetViewportOrgEx->ptlOrigin.y;
939 TRACE("SetViewportOrgEx: %d,%d\n", info->state.vportOrgX, info->state.vportOrgY);
941 if (!IS_WIN9X())
942 EMF_Update_MF_Xform(hdc, info);
944 break;
946 case EMR_SETVIEWPORTEXTEX:
948 const EMRSETVIEWPORTEXTEX *pSetViewportExtEx = (const EMRSETVIEWPORTEXTEX *)mr;
950 if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
951 break;
952 info->state.vportExtX = pSetViewportExtEx->szlExtent.cx;
953 info->state.vportExtY = pSetViewportExtEx->szlExtent.cy;
954 if (info->state.mode == MM_ISOTROPIC)
955 EMF_FixIsotropic(hdc, info);
956 TRACE("SetViewportExtEx: %d,%d\n", info->state.vportExtX, info->state.vportExtY);
958 if (!IS_WIN9X())
959 EMF_Update_MF_Xform(hdc, info);
961 break;
963 case EMR_CREATEPEN:
965 const EMRCREATEPEN *pCreatePen = (const EMRCREATEPEN *)mr;
966 (handletable->objectHandle)[pCreatePen->ihPen] =
967 CreatePenIndirect(&pCreatePen->lopn);
968 break;
970 case EMR_EXTCREATEPEN:
972 const EMREXTCREATEPEN *pPen = (const EMREXTCREATEPEN *)mr;
973 LOGBRUSH lb;
974 lb.lbStyle = pPen->elp.elpBrushStyle;
975 lb.lbColor = pPen->elp.elpColor;
976 lb.lbHatch = pPen->elp.elpHatch;
978 if(pPen->offBmi || pPen->offBits)
979 FIXME("EMR_EXTCREATEPEN: Need to copy brush bitmap\n");
981 (handletable->objectHandle)[pPen->ihPen] =
982 ExtCreatePen(pPen->elp.elpPenStyle, pPen->elp.elpWidth, &lb,
983 pPen->elp.elpNumEntries, pPen->elp.elpNumEntries ? pPen->elp.elpStyleEntry : NULL);
984 break;
986 case EMR_CREATEBRUSHINDIRECT:
988 const EMRCREATEBRUSHINDIRECT *pBrush = (const EMRCREATEBRUSHINDIRECT *)mr;
989 LOGBRUSH brush;
990 brush.lbStyle = pBrush->lb.lbStyle;
991 brush.lbColor = pBrush->lb.lbColor;
992 brush.lbHatch = pBrush->lb.lbHatch;
993 (handletable->objectHandle)[pBrush->ihBrush] = CreateBrushIndirect(&brush);
994 break;
996 case EMR_EXTCREATEFONTINDIRECTW:
998 const EMREXTCREATEFONTINDIRECTW *pFont = (const EMREXTCREATEFONTINDIRECTW *)mr;
999 (handletable->objectHandle)[pFont->ihFont] =
1000 CreateFontIndirectW(&pFont->elfw.elfLogFont);
1001 break;
1003 case EMR_MOVETOEX:
1005 const EMRMOVETOEX *pMoveToEx = (const EMRMOVETOEX *)mr;
1006 MoveToEx(hdc, pMoveToEx->ptl.x, pMoveToEx->ptl.y, NULL);
1007 break;
1009 case EMR_LINETO:
1011 const EMRLINETO *pLineTo = (const EMRLINETO *)mr;
1012 LineTo(hdc, pLineTo->ptl.x, pLineTo->ptl.y);
1013 break;
1015 case EMR_RECTANGLE:
1017 const EMRRECTANGLE *pRect = (const EMRRECTANGLE *)mr;
1018 Rectangle(hdc, pRect->rclBox.left, pRect->rclBox.top,
1019 pRect->rclBox.right, pRect->rclBox.bottom);
1020 break;
1022 case EMR_ELLIPSE:
1024 const EMRELLIPSE *pEllipse = (const EMRELLIPSE *)mr;
1025 Ellipse(hdc, pEllipse->rclBox.left, pEllipse->rclBox.top,
1026 pEllipse->rclBox.right, pEllipse->rclBox.bottom);
1027 break;
1029 case EMR_POLYGON16:
1031 const EMRPOLYGON16 *pPoly = (const EMRPOLYGON16 *)mr;
1032 /* Shouldn't use Polygon16 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 Polygon(hdc, pts, pPoly->cpts);
1042 HeapFree( GetProcessHeap(), 0, pts );
1043 break;
1045 case EMR_POLYLINE16:
1047 const EMRPOLYLINE16 *pPoly = (const EMRPOLYLINE16 *)mr;
1048 /* Shouldn't use Polyline16 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 Polyline(hdc, pts, pPoly->cpts);
1058 HeapFree( GetProcessHeap(), 0, pts );
1059 break;
1061 case EMR_POLYLINETO16:
1063 const EMRPOLYLINETO16 *pPoly = (const EMRPOLYLINETO16 *)mr;
1064 /* Shouldn't use PolylineTo16 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 PolylineTo(hdc, pts, pPoly->cpts);
1074 HeapFree( GetProcessHeap(), 0, pts );
1075 break;
1077 case EMR_POLYBEZIER16:
1079 const EMRPOLYBEZIER16 *pPoly = (const EMRPOLYBEZIER16 *)mr;
1080 /* Shouldn't use PolyBezier16 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 PolyBezier(hdc, pts, pPoly->cpts);
1090 HeapFree( GetProcessHeap(), 0, pts );
1091 break;
1093 case EMR_POLYBEZIERTO16:
1095 const EMRPOLYBEZIERTO16 *pPoly = (const EMRPOLYBEZIERTO16 *)mr;
1096 /* Shouldn't use PolyBezierTo16 since pPoly->cpts is DWORD */
1097 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1098 pPoly->cpts * sizeof(POINT) );
1099 DWORD i;
1100 for(i = 0; i < pPoly->cpts; i++)
1102 pts[i].x = pPoly->apts[i].x;
1103 pts[i].y = pPoly->apts[i].y;
1105 PolyBezierTo(hdc, pts, pPoly->cpts);
1106 HeapFree( GetProcessHeap(), 0, pts );
1107 break;
1109 case EMR_POLYPOLYGON16:
1111 const EMRPOLYPOLYGON16 *pPolyPoly = (const EMRPOLYPOLYGON16 *)mr;
1112 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1113 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1115 const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1116 POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1117 DWORD i;
1118 for(i = 0; i < pPolyPoly->cpts; i++)
1120 pt[i].x = pts[i].x;
1121 pt[i].y = pts[i].y;
1123 PolyPolygon(hdc, pt, (const INT*)pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1124 HeapFree( GetProcessHeap(), 0, pt );
1125 break;
1127 case EMR_POLYPOLYLINE16:
1129 const EMRPOLYPOLYLINE16 *pPolyPoly = (const EMRPOLYPOLYLINE16 *)mr;
1130 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1131 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1133 const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1134 POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1135 DWORD i;
1136 for(i = 0; i < pPolyPoly->cpts; i++)
1138 pt[i].x = pts[i].x;
1139 pt[i].y = pts[i].y;
1141 PolyPolyline(hdc, pt, pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1142 HeapFree( GetProcessHeap(), 0, pt );
1143 break;
1146 case EMR_STRETCHDIBITS:
1148 const EMRSTRETCHDIBITS *pStretchDIBits = (const EMRSTRETCHDIBITS *)mr;
1150 StretchDIBits(hdc,
1151 pStretchDIBits->xDest,
1152 pStretchDIBits->yDest,
1153 pStretchDIBits->cxDest,
1154 pStretchDIBits->cyDest,
1155 pStretchDIBits->xSrc,
1156 pStretchDIBits->ySrc,
1157 pStretchDIBits->cxSrc,
1158 pStretchDIBits->cySrc,
1159 (const BYTE *)mr + pStretchDIBits->offBitsSrc,
1160 (const BITMAPINFO *)((const BYTE *)mr + pStretchDIBits->offBmiSrc),
1161 pStretchDIBits->iUsageSrc,
1162 pStretchDIBits->dwRop);
1163 break;
1166 case EMR_EXTTEXTOUTA:
1168 const EMREXTTEXTOUTA *pExtTextOutA = (const EMREXTTEXTOUTA *)mr;
1169 RECT rc;
1170 const INT *dx = NULL;
1171 int old_mode;
1173 rc.left = pExtTextOutA->emrtext.rcl.left;
1174 rc.top = pExtTextOutA->emrtext.rcl.top;
1175 rc.right = pExtTextOutA->emrtext.rcl.right;
1176 rc.bottom = pExtTextOutA->emrtext.rcl.bottom;
1177 TRACE("EMR_EXTTEXTOUTA: x,y = %d, %d. rect = %s. flags %08x\n",
1178 pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1179 wine_dbgstr_rect(&rc), pExtTextOutA->emrtext.fOptions);
1181 old_mode = SetGraphicsMode(hdc, pExtTextOutA->iGraphicsMode);
1182 /* Reselect the font back into the dc so that the transformation
1183 gets updated. */
1184 SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1186 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1187 * These files can be enumerated and played under Win98 just
1188 * fine, but at least Win2k chokes on them.
1190 if (pExtTextOutA->emrtext.offDx)
1191 dx = (const INT *)((const BYTE *)mr + pExtTextOutA->emrtext.offDx);
1193 ExtTextOutA(hdc, pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1194 pExtTextOutA->emrtext.fOptions, &rc,
1195 (LPCSTR)((const BYTE *)mr + pExtTextOutA->emrtext.offString), pExtTextOutA->emrtext.nChars,
1196 dx);
1198 SetGraphicsMode(hdc, old_mode);
1199 break;
1202 case EMR_EXTTEXTOUTW:
1204 const EMREXTTEXTOUTW *pExtTextOutW = (const EMREXTTEXTOUTW *)mr;
1205 RECT rc;
1206 const INT *dx = NULL;
1207 int old_mode;
1209 rc.left = pExtTextOutW->emrtext.rcl.left;
1210 rc.top = pExtTextOutW->emrtext.rcl.top;
1211 rc.right = pExtTextOutW->emrtext.rcl.right;
1212 rc.bottom = pExtTextOutW->emrtext.rcl.bottom;
1213 TRACE("EMR_EXTTEXTOUTW: x,y = %d, %d. rect = %s. flags %08x\n",
1214 pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1215 wine_dbgstr_rect(&rc), pExtTextOutW->emrtext.fOptions);
1217 old_mode = SetGraphicsMode(hdc, pExtTextOutW->iGraphicsMode);
1218 /* Reselect the font back into the dc so that the transformation
1219 gets updated. */
1220 SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1222 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1223 * These files can be enumerated and played under Win98 just
1224 * fine, but at least Win2k chokes on them.
1226 if (pExtTextOutW->emrtext.offDx)
1227 dx = (const INT *)((const BYTE *)mr + pExtTextOutW->emrtext.offDx);
1229 ExtTextOutW(hdc, pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1230 pExtTextOutW->emrtext.fOptions, &rc,
1231 (LPCWSTR)((const BYTE *)mr + pExtTextOutW->emrtext.offString), pExtTextOutW->emrtext.nChars,
1232 dx);
1234 SetGraphicsMode(hdc, old_mode);
1235 break;
1238 case EMR_CREATEPALETTE:
1240 const EMRCREATEPALETTE *lpCreatePal = (const EMRCREATEPALETTE *)mr;
1242 (handletable->objectHandle)[ lpCreatePal->ihPal ] =
1243 CreatePalette( &lpCreatePal->lgpl );
1245 break;
1248 case EMR_SELECTPALETTE:
1250 const EMRSELECTPALETTE *lpSelectPal = (const EMRSELECTPALETTE *)mr;
1252 if( lpSelectPal->ihPal & 0x80000000 ) {
1253 SelectPalette( hdc, GetStockObject(lpSelectPal->ihPal & 0x7fffffff), TRUE);
1254 } else {
1255 SelectPalette( hdc, (handletable->objectHandle)[lpSelectPal->ihPal], TRUE);
1257 break;
1260 case EMR_REALIZEPALETTE:
1262 RealizePalette( hdc );
1263 break;
1266 case EMR_EXTSELECTCLIPRGN:
1268 const EMREXTSELECTCLIPRGN *lpRgn = (const EMREXTSELECTCLIPRGN *)mr;
1269 HRGN hRgn = 0;
1271 if (mr->nSize >= sizeof(*lpRgn) + sizeof(RGNDATAHEADER))
1272 hRgn = ExtCreateRegion( &info->init_transform, 0, (const RGNDATA *)lpRgn->RgnData );
1274 ExtSelectClipRgn(hdc, hRgn, (INT)(lpRgn->iMode));
1275 /* ExtSelectClipRgn created a copy of the region */
1276 DeleteObject(hRgn);
1277 break;
1280 case EMR_SETMETARGN:
1282 SetMetaRgn( hdc );
1283 break;
1286 case EMR_SETWORLDTRANSFORM:
1288 const EMRSETWORLDTRANSFORM *lpXfrm = (const EMRSETWORLDTRANSFORM *)mr;
1289 info->state.world_transform = lpXfrm->xform;
1291 if (!IS_WIN9X())
1292 EMF_Update_MF_Xform(hdc, info);
1294 break;
1297 case EMR_POLYBEZIER:
1299 const EMRPOLYBEZIER *lpPolyBez = (const EMRPOLYBEZIER *)mr;
1300 PolyBezier(hdc, (const POINT*)lpPolyBez->aptl, (UINT)lpPolyBez->cptl);
1301 break;
1304 case EMR_POLYGON:
1306 const EMRPOLYGON *lpPoly = (const EMRPOLYGON *)mr;
1307 Polygon( hdc, (const POINT*)lpPoly->aptl, (UINT)lpPoly->cptl );
1308 break;
1311 case EMR_POLYLINE:
1313 const EMRPOLYLINE *lpPolyLine = (const EMRPOLYLINE *)mr;
1314 Polyline(hdc, (const POINT*)lpPolyLine->aptl, (UINT)lpPolyLine->cptl);
1315 break;
1318 case EMR_POLYBEZIERTO:
1320 const EMRPOLYBEZIERTO *lpPolyBezierTo = (const EMRPOLYBEZIERTO *)mr;
1321 PolyBezierTo( hdc, (const POINT*)lpPolyBezierTo->aptl,
1322 (UINT)lpPolyBezierTo->cptl );
1323 break;
1326 case EMR_POLYLINETO:
1328 const EMRPOLYLINETO *lpPolyLineTo = (const EMRPOLYLINETO *)mr;
1329 PolylineTo( hdc, (const POINT*)lpPolyLineTo->aptl,
1330 (UINT)lpPolyLineTo->cptl );
1331 break;
1334 case EMR_POLYPOLYLINE:
1336 const EMRPOLYPOLYLINE *pPolyPolyline = (const EMRPOLYPOLYLINE *)mr;
1337 /* NB Points at pPolyPolyline->aPolyCounts + pPolyPolyline->nPolys */
1339 PolyPolyline(hdc, (const POINT*)(pPolyPolyline->aPolyCounts +
1340 pPolyPolyline->nPolys),
1341 pPolyPolyline->aPolyCounts,
1342 pPolyPolyline->nPolys );
1344 break;
1347 case EMR_POLYPOLYGON:
1349 const EMRPOLYPOLYGON *pPolyPolygon = (const EMRPOLYPOLYGON *)mr;
1351 /* NB Points at pPolyPolygon->aPolyCounts + pPolyPolygon->nPolys */
1353 PolyPolygon(hdc, (const POINT*)(pPolyPolygon->aPolyCounts +
1354 pPolyPolygon->nPolys),
1355 (const INT*)pPolyPolygon->aPolyCounts, pPolyPolygon->nPolys );
1356 break;
1359 case EMR_SETBRUSHORGEX:
1361 const EMRSETBRUSHORGEX *lpSetBrushOrgEx = (const EMRSETBRUSHORGEX *)mr;
1363 SetBrushOrgEx( hdc,
1364 (INT)lpSetBrushOrgEx->ptlOrigin.x,
1365 (INT)lpSetBrushOrgEx->ptlOrigin.y,
1366 NULL );
1368 break;
1371 case EMR_SETPIXELV:
1373 const EMRSETPIXELV *lpSetPixelV = (const EMRSETPIXELV *)mr;
1375 SetPixelV( hdc,
1376 (INT)lpSetPixelV->ptlPixel.x,
1377 (INT)lpSetPixelV->ptlPixel.y,
1378 lpSetPixelV->crColor );
1380 break;
1383 case EMR_SETMAPPERFLAGS:
1385 const EMRSETMAPPERFLAGS *lpSetMapperFlags = (const EMRSETMAPPERFLAGS *)mr;
1387 SetMapperFlags( hdc, lpSetMapperFlags->dwFlags );
1389 break;
1392 case EMR_SETCOLORADJUSTMENT:
1394 const EMRSETCOLORADJUSTMENT *lpSetColorAdjust = (const EMRSETCOLORADJUSTMENT *)mr;
1396 SetColorAdjustment( hdc, &lpSetColorAdjust->ColorAdjustment );
1398 break;
1401 case EMR_OFFSETCLIPRGN:
1403 const EMROFFSETCLIPRGN *lpOffsetClipRgn = (const EMROFFSETCLIPRGN *)mr;
1405 OffsetClipRgn( hdc,
1406 (INT)lpOffsetClipRgn->ptlOffset.x,
1407 (INT)lpOffsetClipRgn->ptlOffset.y );
1408 FIXME("OffsetClipRgn\n");
1410 break;
1413 case EMR_EXCLUDECLIPRECT:
1415 const EMREXCLUDECLIPRECT *lpExcludeClipRect = (const EMREXCLUDECLIPRECT *)mr;
1417 ExcludeClipRect( hdc,
1418 lpExcludeClipRect->rclClip.left,
1419 lpExcludeClipRect->rclClip.top,
1420 lpExcludeClipRect->rclClip.right,
1421 lpExcludeClipRect->rclClip.bottom );
1422 FIXME("ExcludeClipRect\n");
1424 break;
1427 case EMR_SCALEVIEWPORTEXTEX:
1429 const EMRSCALEVIEWPORTEXTEX *lpScaleViewportExtEx = (const EMRSCALEVIEWPORTEXTEX *)mr;
1431 if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1432 break;
1433 if (!lpScaleViewportExtEx->xNum || !lpScaleViewportExtEx->xDenom ||
1434 !lpScaleViewportExtEx->yNum || !lpScaleViewportExtEx->yDenom)
1435 break;
1436 info->state.vportExtX = MulDiv(info->state.vportExtX, lpScaleViewportExtEx->xNum,
1437 lpScaleViewportExtEx->xDenom);
1438 info->state.vportExtY = MulDiv(info->state.vportExtY, lpScaleViewportExtEx->yNum,
1439 lpScaleViewportExtEx->yDenom);
1440 if (info->state.vportExtX == 0) info->state.vportExtX = 1;
1441 if (info->state.vportExtY == 0) info->state.vportExtY = 1;
1442 if (info->state.mode == MM_ISOTROPIC)
1443 EMF_FixIsotropic(hdc, info);
1445 TRACE("EMRSCALEVIEWPORTEXTEX %d/%d %d/%d\n",
1446 lpScaleViewportExtEx->xNum,lpScaleViewportExtEx->xDenom,
1447 lpScaleViewportExtEx->yNum,lpScaleViewportExtEx->yDenom);
1449 if (!IS_WIN9X())
1450 EMF_Update_MF_Xform(hdc, info);
1452 break;
1455 case EMR_SCALEWINDOWEXTEX:
1457 const EMRSCALEWINDOWEXTEX *lpScaleWindowExtEx = (const EMRSCALEWINDOWEXTEX *)mr;
1459 if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1460 break;
1461 if (!lpScaleWindowExtEx->xNum || !lpScaleWindowExtEx->xDenom ||
1462 !lpScaleWindowExtEx->yNum || !lpScaleWindowExtEx->yDenom)
1463 break;
1464 info->state.wndExtX = MulDiv(info->state.wndExtX, lpScaleWindowExtEx->xNum,
1465 lpScaleWindowExtEx->xDenom);
1466 info->state.wndExtY = MulDiv(info->state.wndExtY, lpScaleWindowExtEx->yNum,
1467 lpScaleWindowExtEx->yDenom);
1468 if (info->state.wndExtX == 0) info->state.wndExtX = 1;
1469 if (info->state.wndExtY == 0) info->state.wndExtY = 1;
1470 if (info->state.mode == MM_ISOTROPIC)
1471 EMF_FixIsotropic(hdc, info);
1473 TRACE("EMRSCALEWINDOWEXTEX %d/%d %d/%d\n",
1474 lpScaleWindowExtEx->xNum,lpScaleWindowExtEx->xDenom,
1475 lpScaleWindowExtEx->yNum,lpScaleWindowExtEx->yDenom);
1477 if (!IS_WIN9X())
1478 EMF_Update_MF_Xform(hdc, info);
1480 break;
1483 case EMR_MODIFYWORLDTRANSFORM:
1485 const EMRMODIFYWORLDTRANSFORM *lpModifyWorldTrans = (const EMRMODIFYWORLDTRANSFORM *)mr;
1487 switch(lpModifyWorldTrans->iMode) {
1488 case MWT_IDENTITY:
1489 info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
1490 info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
1491 info->state.world_transform.eDx = info->state.world_transform.eDy = 0;
1492 if (!IS_WIN9X())
1493 EMF_Update_MF_Xform(hdc, info);
1494 break;
1495 case MWT_LEFTMULTIPLY:
1496 CombineTransform(&info->state.world_transform, &lpModifyWorldTrans->xform,
1497 &info->state.world_transform);
1498 if (!IS_WIN9X())
1499 ModifyWorldTransform(hdc, &lpModifyWorldTrans->xform, MWT_LEFTMULTIPLY);
1500 break;
1501 case MWT_RIGHTMULTIPLY:
1502 CombineTransform(&info->state.world_transform, &info->state.world_transform,
1503 &lpModifyWorldTrans->xform);
1504 if (!IS_WIN9X())
1505 EMF_Update_MF_Xform(hdc, info);
1506 break;
1507 default:
1508 FIXME("Unknown imode %d\n", lpModifyWorldTrans->iMode);
1509 break;
1511 break;
1514 case EMR_ANGLEARC:
1516 const EMRANGLEARC *lpAngleArc = (const EMRANGLEARC *)mr;
1518 AngleArc( hdc,
1519 (INT)lpAngleArc->ptlCenter.x, (INT)lpAngleArc->ptlCenter.y,
1520 lpAngleArc->nRadius, lpAngleArc->eStartAngle,
1521 lpAngleArc->eSweepAngle );
1523 break;
1526 case EMR_ROUNDRECT:
1528 const EMRROUNDRECT *lpRoundRect = (const EMRROUNDRECT *)mr;
1530 RoundRect( hdc,
1531 lpRoundRect->rclBox.left,
1532 lpRoundRect->rclBox.top,
1533 lpRoundRect->rclBox.right,
1534 lpRoundRect->rclBox.bottom,
1535 lpRoundRect->szlCorner.cx,
1536 lpRoundRect->szlCorner.cy );
1538 break;
1541 case EMR_ARC:
1543 const EMRARC *lpArc = (const EMRARC *)mr;
1545 Arc( hdc,
1546 (INT)lpArc->rclBox.left,
1547 (INT)lpArc->rclBox.top,
1548 (INT)lpArc->rclBox.right,
1549 (INT)lpArc->rclBox.bottom,
1550 (INT)lpArc->ptlStart.x,
1551 (INT)lpArc->ptlStart.y,
1552 (INT)lpArc->ptlEnd.x,
1553 (INT)lpArc->ptlEnd.y );
1555 break;
1558 case EMR_CHORD:
1560 const EMRCHORD *lpChord = (const EMRCHORD *)mr;
1562 Chord( hdc,
1563 (INT)lpChord->rclBox.left,
1564 (INT)lpChord->rclBox.top,
1565 (INT)lpChord->rclBox.right,
1566 (INT)lpChord->rclBox.bottom,
1567 (INT)lpChord->ptlStart.x,
1568 (INT)lpChord->ptlStart.y,
1569 (INT)lpChord->ptlEnd.x,
1570 (INT)lpChord->ptlEnd.y );
1572 break;
1575 case EMR_PIE:
1577 const EMRPIE *lpPie = (const EMRPIE *)mr;
1579 Pie( hdc,
1580 (INT)lpPie->rclBox.left,
1581 (INT)lpPie->rclBox.top,
1582 (INT)lpPie->rclBox.right,
1583 (INT)lpPie->rclBox.bottom,
1584 (INT)lpPie->ptlStart.x,
1585 (INT)lpPie->ptlStart.y,
1586 (INT)lpPie->ptlEnd.x,
1587 (INT)lpPie->ptlEnd.y );
1589 break;
1592 case EMR_ARCTO:
1594 const EMRARC *lpArcTo = (const EMRARC *)mr;
1596 ArcTo( hdc,
1597 (INT)lpArcTo->rclBox.left,
1598 (INT)lpArcTo->rclBox.top,
1599 (INT)lpArcTo->rclBox.right,
1600 (INT)lpArcTo->rclBox.bottom,
1601 (INT)lpArcTo->ptlStart.x,
1602 (INT)lpArcTo->ptlStart.y,
1603 (INT)lpArcTo->ptlEnd.x,
1604 (INT)lpArcTo->ptlEnd.y );
1606 break;
1609 case EMR_EXTFLOODFILL:
1611 const EMREXTFLOODFILL *lpExtFloodFill = (const EMREXTFLOODFILL *)mr;
1613 ExtFloodFill( hdc,
1614 (INT)lpExtFloodFill->ptlStart.x,
1615 (INT)lpExtFloodFill->ptlStart.y,
1616 lpExtFloodFill->crColor,
1617 (UINT)lpExtFloodFill->iMode );
1619 break;
1622 case EMR_POLYDRAW:
1624 const EMRPOLYDRAW *lpPolyDraw = (const EMRPOLYDRAW *)mr;
1625 PolyDraw( hdc,
1626 (const POINT*)lpPolyDraw->aptl,
1627 lpPolyDraw->abTypes,
1628 (INT)lpPolyDraw->cptl );
1630 break;
1633 case EMR_SETARCDIRECTION:
1635 const EMRSETARCDIRECTION *lpSetArcDirection = (const EMRSETARCDIRECTION *)mr;
1636 SetArcDirection( hdc, (INT)lpSetArcDirection->iArcDirection );
1637 break;
1640 case EMR_SETMITERLIMIT:
1642 const EMRSETMITERLIMIT *lpSetMiterLimit = (const EMRSETMITERLIMIT *)mr;
1643 SetMiterLimit( hdc, lpSetMiterLimit->eMiterLimit, NULL );
1644 break;
1647 case EMR_BEGINPATH:
1649 BeginPath( hdc );
1650 break;
1653 case EMR_ENDPATH:
1655 EndPath( hdc );
1656 break;
1659 case EMR_CLOSEFIGURE:
1661 CloseFigure( hdc );
1662 break;
1665 case EMR_FILLPATH:
1667 /*const EMRFILLPATH lpFillPath = (const EMRFILLPATH *)mr;*/
1668 FillPath( hdc );
1669 break;
1672 case EMR_STROKEANDFILLPATH:
1674 /*const EMRSTROKEANDFILLPATH lpStrokeAndFillPath = (const EMRSTROKEANDFILLPATH *)mr;*/
1675 StrokeAndFillPath( hdc );
1676 break;
1679 case EMR_STROKEPATH:
1681 /*const EMRSTROKEPATH lpStrokePath = (const EMRSTROKEPATH *)mr;*/
1682 StrokePath( hdc );
1683 break;
1686 case EMR_FLATTENPATH:
1688 FlattenPath( hdc );
1689 break;
1692 case EMR_WIDENPATH:
1694 WidenPath( hdc );
1695 break;
1698 case EMR_SELECTCLIPPATH:
1700 const EMRSELECTCLIPPATH *lpSelectClipPath = (const EMRSELECTCLIPPATH *)mr;
1701 SelectClipPath( hdc, (INT)lpSelectClipPath->iMode );
1702 break;
1705 case EMR_ABORTPATH:
1707 AbortPath( hdc );
1708 break;
1711 case EMR_CREATECOLORSPACE:
1713 PEMRCREATECOLORSPACE lpCreateColorSpace = (PEMRCREATECOLORSPACE)mr;
1714 (handletable->objectHandle)[lpCreateColorSpace->ihCS] =
1715 CreateColorSpaceA( &lpCreateColorSpace->lcs );
1716 break;
1719 case EMR_SETCOLORSPACE:
1721 const EMRSETCOLORSPACE *lpSetColorSpace = (const EMRSETCOLORSPACE *)mr;
1722 SetColorSpace( hdc,
1723 (handletable->objectHandle)[lpSetColorSpace->ihCS] );
1724 break;
1727 case EMR_DELETECOLORSPACE:
1729 const EMRDELETECOLORSPACE *lpDeleteColorSpace = (const EMRDELETECOLORSPACE *)mr;
1730 DeleteColorSpace( (handletable->objectHandle)[lpDeleteColorSpace->ihCS] );
1731 break;
1734 case EMR_SETICMMODE:
1736 const EMRSETICMMODE *lpSetICMMode = (const EMRSETICMMODE *)mr;
1737 SetICMMode( hdc, (INT)lpSetICMMode->iMode );
1738 break;
1741 case EMR_PIXELFORMAT:
1743 INT iPixelFormat;
1744 const EMRPIXELFORMAT *lpPixelFormat = (const EMRPIXELFORMAT *)mr;
1746 iPixelFormat = ChoosePixelFormat( hdc, &lpPixelFormat->pfd );
1747 SetPixelFormat( hdc, iPixelFormat, &lpPixelFormat->pfd );
1749 break;
1752 case EMR_SETPALETTEENTRIES:
1754 const EMRSETPALETTEENTRIES *lpSetPaletteEntries = (const EMRSETPALETTEENTRIES *)mr;
1756 SetPaletteEntries( (handletable->objectHandle)[lpSetPaletteEntries->ihPal],
1757 (UINT)lpSetPaletteEntries->iStart,
1758 (UINT)lpSetPaletteEntries->cEntries,
1759 lpSetPaletteEntries->aPalEntries );
1761 break;
1764 case EMR_RESIZEPALETTE:
1766 const EMRRESIZEPALETTE *lpResizePalette = (const EMRRESIZEPALETTE *)mr;
1768 ResizePalette( (handletable->objectHandle)[lpResizePalette->ihPal],
1769 (UINT)lpResizePalette->cEntries );
1771 break;
1774 case EMR_CREATEDIBPATTERNBRUSHPT:
1776 const EMRCREATEDIBPATTERNBRUSHPT *lpCreate = (const EMRCREATEDIBPATTERNBRUSHPT *)mr;
1777 LPVOID lpPackedStruct;
1779 /* Check that offsets and data are contained within the record
1780 * (including checking for wrap-arounds).
1782 if ( lpCreate->offBmi + lpCreate->cbBmi > mr->nSize
1783 || lpCreate->offBits + lpCreate->cbBits > mr->nSize
1784 || lpCreate->offBmi + lpCreate->cbBmi < lpCreate->offBmi
1785 || lpCreate->offBits + lpCreate->cbBits < lpCreate->offBits )
1787 ERR("Invalid EMR_CREATEDIBPATTERNBRUSHPT record\n");
1788 break;
1791 /* This is a BITMAPINFO struct followed directly by bitmap bits */
1792 lpPackedStruct = HeapAlloc( GetProcessHeap(), 0,
1793 lpCreate->cbBmi + lpCreate->cbBits );
1794 if(!lpPackedStruct)
1796 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1797 break;
1800 /* Now pack this structure */
1801 memcpy( lpPackedStruct,
1802 ((const BYTE *)lpCreate) + lpCreate->offBmi,
1803 lpCreate->cbBmi );
1804 memcpy( ((BYTE*)lpPackedStruct) + lpCreate->cbBmi,
1805 ((const BYTE *)lpCreate) + lpCreate->offBits,
1806 lpCreate->cbBits );
1808 (handletable->objectHandle)[lpCreate->ihBrush] =
1809 CreateDIBPatternBrushPt( lpPackedStruct,
1810 (UINT)lpCreate->iUsage );
1812 HeapFree(GetProcessHeap(), 0, lpPackedStruct);
1813 break;
1816 case EMR_CREATEMONOBRUSH:
1818 const EMRCREATEMONOBRUSH *pCreateMonoBrush = (const EMRCREATEMONOBRUSH *)mr;
1819 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pCreateMonoBrush->offBmi);
1820 HBITMAP hBmp;
1822 /* Need to check if the bitmap is monochrome, and if the
1823 two colors are really black and white */
1824 if (pCreateMonoBrush->iUsage == DIB_PAL_MONO)
1826 BITMAP bm;
1828 /* Undocumented iUsage indicates a mono bitmap with no palette table,
1829 * aligned to 32 rather than 16 bits.
1831 bm.bmType = 0;
1832 bm.bmWidth = pbi->bmiHeader.biWidth;
1833 bm.bmHeight = abs(pbi->bmiHeader.biHeight);
1834 bm.bmWidthBytes = 4 * ((pbi->bmiHeader.biWidth + 31) / 32);
1835 bm.bmPlanes = pbi->bmiHeader.biPlanes;
1836 bm.bmBitsPixel = pbi->bmiHeader.biBitCount;
1837 bm.bmBits = (BYTE *)mr + pCreateMonoBrush->offBits;
1838 hBmp = CreateBitmapIndirect(&bm);
1840 else if (is_dib_monochrome(pbi))
1842 /* Top-down DIBs have a negative height */
1843 LONG height = pbi->bmiHeader.biHeight;
1845 hBmp = CreateBitmap(pbi->bmiHeader.biWidth, abs(height), 1, 1, NULL);
1846 SetDIBits(hdc, hBmp, 0, pbi->bmiHeader.biHeight,
1847 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1849 else
1851 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1852 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1855 (handletable->objectHandle)[pCreateMonoBrush->ihBrush] = CreatePatternBrush(hBmp);
1857 /* CreatePatternBrush created a copy of the bitmap */
1858 DeleteObject(hBmp);
1859 break;
1862 case EMR_BITBLT:
1864 const EMRBITBLT *pBitBlt = (const EMRBITBLT *)mr;
1866 if(pBitBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1867 PatBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1868 pBitBlt->dwRop);
1869 } else { /* BitBlt */
1870 HDC hdcSrc = CreateCompatibleDC(hdc);
1871 HBRUSH hBrush, hBrushOld;
1872 HBITMAP hBmp = 0, hBmpOld = 0;
1873 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pBitBlt->offBmiSrc);
1875 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1876 SetWorldTransform(hdcSrc, &pBitBlt->xformSrc);
1878 hBrush = CreateSolidBrush(pBitBlt->crBkColorSrc);
1879 hBrushOld = SelectObject(hdcSrc, hBrush);
1880 PatBlt(hdcSrc, pBitBlt->rclBounds.left, pBitBlt->rclBounds.top,
1881 pBitBlt->rclBounds.right - pBitBlt->rclBounds.left,
1882 pBitBlt->rclBounds.bottom - pBitBlt->rclBounds.top, PATCOPY);
1883 SelectObject(hdcSrc, hBrushOld);
1884 DeleteObject(hBrush);
1886 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1887 (const BYTE *)mr + pBitBlt->offBitsSrc, pbi, pBitBlt->iUsageSrc);
1888 hBmpOld = SelectObject(hdcSrc, hBmp);
1890 BitBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1891 hdcSrc, pBitBlt->xSrc, pBitBlt->ySrc, pBitBlt->dwRop);
1893 SelectObject(hdcSrc, hBmpOld);
1894 DeleteObject(hBmp);
1895 DeleteDC(hdcSrc);
1897 break;
1900 case EMR_STRETCHBLT:
1902 const EMRSTRETCHBLT *pStretchBlt = (const EMRSTRETCHBLT *)mr;
1904 TRACE("EMR_STRETCHBLT: %d, %d %dx%d -> %d, %d %dx%d. rop %08x offBitsSrc %d\n",
1905 pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1906 pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1907 pStretchBlt->dwRop, pStretchBlt->offBitsSrc);
1909 if(pStretchBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1910 PatBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1911 pStretchBlt->dwRop);
1912 } else { /* StretchBlt */
1913 HDC hdcSrc = CreateCompatibleDC(hdc);
1914 HBRUSH hBrush, hBrushOld;
1915 HBITMAP hBmp = 0, hBmpOld = 0;
1916 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pStretchBlt->offBmiSrc);
1918 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1919 SetWorldTransform(hdcSrc, &pStretchBlt->xformSrc);
1921 hBrush = CreateSolidBrush(pStretchBlt->crBkColorSrc);
1922 hBrushOld = SelectObject(hdcSrc, hBrush);
1923 PatBlt(hdcSrc, pStretchBlt->rclBounds.left, pStretchBlt->rclBounds.top,
1924 pStretchBlt->rclBounds.right - pStretchBlt->rclBounds.left,
1925 pStretchBlt->rclBounds.bottom - pStretchBlt->rclBounds.top, PATCOPY);
1926 SelectObject(hdcSrc, hBrushOld);
1927 DeleteObject(hBrush);
1929 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1930 (const BYTE *)mr + pStretchBlt->offBitsSrc, pbi, pStretchBlt->iUsageSrc);
1931 hBmpOld = SelectObject(hdcSrc, hBmp);
1933 StretchBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1934 hdcSrc, pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1935 pStretchBlt->dwRop);
1937 SelectObject(hdcSrc, hBmpOld);
1938 DeleteObject(hBmp);
1939 DeleteDC(hdcSrc);
1941 break;
1944 case EMR_ALPHABLEND:
1946 const EMRALPHABLEND *pAlphaBlend = (const EMRALPHABLEND *)mr;
1948 TRACE("EMR_ALPHABLEND: %d, %d %dx%d -> %d, %d %dx%d. blendfn %08x offBitsSrc %d\n",
1949 pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1950 pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1951 pAlphaBlend->dwRop, pAlphaBlend->offBitsSrc);
1953 if(pAlphaBlend->offBmiSrc == 0) {
1954 FIXME("EMR_ALPHABLEND: offBmiSrc == 0\n");
1955 } else {
1956 HDC hdcSrc = CreateCompatibleDC(hdc);
1957 HBITMAP hBmp = 0, hBmpOld = 0;
1958 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pAlphaBlend->offBmiSrc);
1959 void *bits;
1961 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1962 SetWorldTransform(hdcSrc, &pAlphaBlend->xformSrc);
1964 hBmp = CreateDIBSection(hdc, pbi, pAlphaBlend->iUsageSrc, &bits, NULL, 0);
1965 memcpy(bits, (const BYTE *)mr + pAlphaBlend->offBitsSrc, pAlphaBlend->cbBitsSrc);
1966 hBmpOld = SelectObject(hdcSrc, hBmp);
1968 GdiAlphaBlend(hdc, pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1969 hdcSrc, pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1970 *(BLENDFUNCTION *)&pAlphaBlend->dwRop);
1972 SelectObject(hdcSrc, hBmpOld);
1973 DeleteObject(hBmp);
1974 DeleteDC(hdcSrc);
1976 break;
1979 case EMR_MASKBLT:
1981 const EMRMASKBLT *pMaskBlt = (const EMRMASKBLT *)mr;
1982 HDC hdcSrc = CreateCompatibleDC(hdc);
1983 HBRUSH hBrush, hBrushOld;
1984 HBITMAP hBmp, hBmpOld, hBmpMask;
1985 const BITMAPINFO *pbi;
1987 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1988 SetWorldTransform(hdcSrc, &pMaskBlt->xformSrc);
1990 hBrush = CreateSolidBrush(pMaskBlt->crBkColorSrc);
1991 hBrushOld = SelectObject(hdcSrc, hBrush);
1992 PatBlt(hdcSrc, pMaskBlt->rclBounds.left, pMaskBlt->rclBounds.top,
1993 pMaskBlt->rclBounds.right - pMaskBlt->rclBounds.left,
1994 pMaskBlt->rclBounds.bottom - pMaskBlt->rclBounds.top, PATCOPY);
1995 SelectObject(hdcSrc, hBrushOld);
1996 DeleteObject(hBrush);
1998 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiMask);
1999 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
2000 1, 1, NULL);
2001 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
2002 (const BYTE *)mr + pMaskBlt->offBitsMask, pbi, pMaskBlt->iUsageMask);
2004 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiSrc);
2005 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
2006 (const BYTE *)mr + pMaskBlt->offBitsSrc, pbi, pMaskBlt->iUsageSrc);
2007 hBmpOld = SelectObject(hdcSrc, hBmp);
2008 MaskBlt(hdc,
2009 pMaskBlt->xDest,
2010 pMaskBlt->yDest,
2011 pMaskBlt->cxDest,
2012 pMaskBlt->cyDest,
2013 hdcSrc,
2014 pMaskBlt->xSrc,
2015 pMaskBlt->ySrc,
2016 hBmpMask,
2017 pMaskBlt->xMask,
2018 pMaskBlt->yMask,
2019 pMaskBlt->dwRop);
2020 SelectObject(hdcSrc, hBmpOld);
2021 DeleteObject(hBmp);
2022 DeleteObject(hBmpMask);
2023 DeleteDC(hdcSrc);
2024 break;
2027 case EMR_PLGBLT:
2029 const EMRPLGBLT *pPlgBlt = (const EMRPLGBLT *)mr;
2030 HDC hdcSrc = CreateCompatibleDC(hdc);
2031 HBRUSH hBrush, hBrushOld;
2032 HBITMAP hBmp, hBmpOld, hBmpMask;
2033 const BITMAPINFO *pbi;
2034 POINT pts[3];
2036 SetGraphicsMode(hdcSrc, GM_ADVANCED);
2037 SetWorldTransform(hdcSrc, &pPlgBlt->xformSrc);
2039 pts[0].x = pPlgBlt->aptlDest[0].x; pts[0].y = pPlgBlt->aptlDest[0].y;
2040 pts[1].x = pPlgBlt->aptlDest[1].x; pts[1].y = pPlgBlt->aptlDest[1].y;
2041 pts[2].x = pPlgBlt->aptlDest[2].x; pts[2].y = pPlgBlt->aptlDest[2].y;
2043 hBrush = CreateSolidBrush(pPlgBlt->crBkColorSrc);
2044 hBrushOld = SelectObject(hdcSrc, hBrush);
2045 PatBlt(hdcSrc, pPlgBlt->rclBounds.left, pPlgBlt->rclBounds.top,
2046 pPlgBlt->rclBounds.right - pPlgBlt->rclBounds.left,
2047 pPlgBlt->rclBounds.bottom - pPlgBlt->rclBounds.top, PATCOPY);
2048 SelectObject(hdcSrc, hBrushOld);
2049 DeleteObject(hBrush);
2051 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiMask);
2052 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
2053 1, 1, NULL);
2054 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
2055 (const BYTE *)mr + pPlgBlt->offBitsMask, pbi, pPlgBlt->iUsageMask);
2057 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiSrc);
2058 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
2059 (const BYTE *)mr + pPlgBlt->offBitsSrc, pbi, pPlgBlt->iUsageSrc);
2060 hBmpOld = SelectObject(hdcSrc, hBmp);
2061 PlgBlt(hdc,
2062 pts,
2063 hdcSrc,
2064 pPlgBlt->xSrc,
2065 pPlgBlt->ySrc,
2066 pPlgBlt->cxSrc,
2067 pPlgBlt->cySrc,
2068 hBmpMask,
2069 pPlgBlt->xMask,
2070 pPlgBlt->yMask);
2071 SelectObject(hdcSrc, hBmpOld);
2072 DeleteObject(hBmp);
2073 DeleteObject(hBmpMask);
2074 DeleteDC(hdcSrc);
2075 break;
2078 case EMR_SETDIBITSTODEVICE:
2080 const EMRSETDIBITSTODEVICE *pSetDIBitsToDevice = (const EMRSETDIBITSTODEVICE *)mr;
2082 SetDIBitsToDevice(hdc,
2083 pSetDIBitsToDevice->xDest,
2084 pSetDIBitsToDevice->yDest,
2085 pSetDIBitsToDevice->cxSrc,
2086 pSetDIBitsToDevice->cySrc,
2087 pSetDIBitsToDevice->xSrc,
2088 pSetDIBitsToDevice->ySrc,
2089 pSetDIBitsToDevice->iStartScan,
2090 pSetDIBitsToDevice->cScans,
2091 (const BYTE *)mr + pSetDIBitsToDevice->offBitsSrc,
2092 (const BITMAPINFO *)((const BYTE *)mr + pSetDIBitsToDevice->offBmiSrc),
2093 pSetDIBitsToDevice->iUsageSrc);
2094 break;
2097 case EMR_POLYTEXTOUTA:
2099 const EMRPOLYTEXTOUTA *pPolyTextOutA = (const EMRPOLYTEXTOUTA *)mr;
2100 POLYTEXTA *polytextA = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutA->cStrings * sizeof(POLYTEXTA));
2101 LONG i;
2102 XFORM xform, xformOld;
2103 int gModeOld;
2105 gModeOld = SetGraphicsMode(hdc, pPolyTextOutA->iGraphicsMode);
2106 GetWorldTransform(hdc, &xformOld);
2108 xform.eM11 = pPolyTextOutA->exScale;
2109 xform.eM12 = 0.0;
2110 xform.eM21 = 0.0;
2111 xform.eM22 = pPolyTextOutA->eyScale;
2112 xform.eDx = 0.0;
2113 xform.eDy = 0.0;
2114 SetWorldTransform(hdc, &xform);
2116 /* Set up POLYTEXTA structures */
2117 for(i = 0; i < pPolyTextOutA->cStrings; i++)
2119 polytextA[i].x = pPolyTextOutA->aemrtext[i].ptlReference.x;
2120 polytextA[i].y = pPolyTextOutA->aemrtext[i].ptlReference.y;
2121 polytextA[i].n = pPolyTextOutA->aemrtext[i].nChars;
2122 polytextA[i].lpstr = (LPCSTR)((const BYTE *)mr + pPolyTextOutA->aemrtext[i].offString);
2123 polytextA[i].uiFlags = pPolyTextOutA->aemrtext[i].fOptions;
2124 polytextA[i].rcl.left = pPolyTextOutA->aemrtext[i].rcl.left;
2125 polytextA[i].rcl.right = pPolyTextOutA->aemrtext[i].rcl.right;
2126 polytextA[i].rcl.top = pPolyTextOutA->aemrtext[i].rcl.top;
2127 polytextA[i].rcl.bottom = pPolyTextOutA->aemrtext[i].rcl.bottom;
2128 polytextA[i].pdx = (int *)((BYTE *)mr + pPolyTextOutA->aemrtext[i].offDx);
2130 PolyTextOutA(hdc, polytextA, pPolyTextOutA->cStrings);
2131 HeapFree(GetProcessHeap(), 0, polytextA);
2133 SetWorldTransform(hdc, &xformOld);
2134 SetGraphicsMode(hdc, gModeOld);
2135 break;
2138 case EMR_POLYTEXTOUTW:
2140 const EMRPOLYTEXTOUTW *pPolyTextOutW = (const EMRPOLYTEXTOUTW *)mr;
2141 POLYTEXTW *polytextW = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutW->cStrings * sizeof(POLYTEXTW));
2142 LONG i;
2143 XFORM xform, xformOld;
2144 int gModeOld;
2146 gModeOld = SetGraphicsMode(hdc, pPolyTextOutW->iGraphicsMode);
2147 GetWorldTransform(hdc, &xformOld);
2149 xform.eM11 = pPolyTextOutW->exScale;
2150 xform.eM12 = 0.0;
2151 xform.eM21 = 0.0;
2152 xform.eM22 = pPolyTextOutW->eyScale;
2153 xform.eDx = 0.0;
2154 xform.eDy = 0.0;
2155 SetWorldTransform(hdc, &xform);
2157 /* Set up POLYTEXTW structures */
2158 for(i = 0; i < pPolyTextOutW->cStrings; i++)
2160 polytextW[i].x = pPolyTextOutW->aemrtext[i].ptlReference.x;
2161 polytextW[i].y = pPolyTextOutW->aemrtext[i].ptlReference.y;
2162 polytextW[i].n = pPolyTextOutW->aemrtext[i].nChars;
2163 polytextW[i].lpstr = (LPCWSTR)((const BYTE *)mr + pPolyTextOutW->aemrtext[i].offString);
2164 polytextW[i].uiFlags = pPolyTextOutW->aemrtext[i].fOptions;
2165 polytextW[i].rcl.left = pPolyTextOutW->aemrtext[i].rcl.left;
2166 polytextW[i].rcl.right = pPolyTextOutW->aemrtext[i].rcl.right;
2167 polytextW[i].rcl.top = pPolyTextOutW->aemrtext[i].rcl.top;
2168 polytextW[i].rcl.bottom = pPolyTextOutW->aemrtext[i].rcl.bottom;
2169 polytextW[i].pdx = (int *)((BYTE *)mr + pPolyTextOutW->aemrtext[i].offDx);
2171 PolyTextOutW(hdc, polytextW, pPolyTextOutW->cStrings);
2172 HeapFree(GetProcessHeap(), 0, polytextW);
2174 SetWorldTransform(hdc, &xformOld);
2175 SetGraphicsMode(hdc, gModeOld);
2176 break;
2179 case EMR_FILLRGN:
2181 const EMRFILLRGN *pFillRgn = (const EMRFILLRGN *)mr;
2182 HRGN hRgn = ExtCreateRegion(NULL, pFillRgn->cbRgnData, (const RGNDATA *)pFillRgn->RgnData);
2183 FillRgn(hdc,
2184 hRgn,
2185 (handletable->objectHandle)[pFillRgn->ihBrush]);
2186 DeleteObject(hRgn);
2187 break;
2190 case EMR_FRAMERGN:
2192 const EMRFRAMERGN *pFrameRgn = (const EMRFRAMERGN *)mr;
2193 HRGN hRgn = ExtCreateRegion(NULL, pFrameRgn->cbRgnData, (const RGNDATA *)pFrameRgn->RgnData);
2194 FrameRgn(hdc,
2195 hRgn,
2196 (handletable->objectHandle)[pFrameRgn->ihBrush],
2197 pFrameRgn->szlStroke.cx,
2198 pFrameRgn->szlStroke.cy);
2199 DeleteObject(hRgn);
2200 break;
2203 case EMR_INVERTRGN:
2205 const EMRINVERTRGN *pInvertRgn = (const EMRINVERTRGN *)mr;
2206 HRGN hRgn = ExtCreateRegion(NULL, pInvertRgn->cbRgnData, (const RGNDATA *)pInvertRgn->RgnData);
2207 InvertRgn(hdc, hRgn);
2208 DeleteObject(hRgn);
2209 break;
2212 case EMR_PAINTRGN:
2214 const EMRPAINTRGN *pPaintRgn = (const EMRPAINTRGN *)mr;
2215 HRGN hRgn = ExtCreateRegion(NULL, pPaintRgn->cbRgnData, (const RGNDATA *)pPaintRgn->RgnData);
2216 PaintRgn(hdc, hRgn);
2217 DeleteObject(hRgn);
2218 break;
2221 case EMR_SETTEXTJUSTIFICATION:
2223 const EMRSETTEXTJUSTIFICATION *pSetTextJust = (const EMRSETTEXTJUSTIFICATION *)mr;
2224 SetTextJustification(hdc, pSetTextJust->nBreakExtra, pSetTextJust->nBreakCount);
2225 break;
2228 case EMR_SETLAYOUT:
2230 const EMRSETLAYOUT *pSetLayout = (const EMRSETLAYOUT *)mr;
2231 SetLayout(hdc, pSetLayout->iMode);
2232 break;
2235 case EMR_GRADIENTFILL:
2237 EMRGRADIENTFILL *grad = (EMRGRADIENTFILL *)mr;
2238 GdiGradientFill( hdc, grad->Ver, grad->nVer, grad->Ver + grad->nVer,
2239 grad->nTri, grad->ulMode );
2240 break;
2243 case EMR_POLYDRAW16:
2244 case EMR_GLSRECORD:
2245 case EMR_GLSBOUNDEDRECORD:
2246 case EMR_DRAWESCAPE:
2247 case EMR_EXTESCAPE:
2248 case EMR_STARTDOC:
2249 case EMR_SMALLTEXTOUT:
2250 case EMR_FORCEUFIMAPPING:
2251 case EMR_NAMEDESCAPE:
2252 case EMR_COLORCORRECTPALETTE:
2253 case EMR_SETICMPROFILEA:
2254 case EMR_SETICMPROFILEW:
2255 case EMR_TRANSPARENTBLT:
2256 case EMR_SETLINKEDUFI:
2257 case EMR_COLORMATCHTOTARGETW:
2258 case EMR_CREATECOLORSPACEW:
2260 default:
2261 /* From docs: If PlayEnhMetaFileRecord doesn't recognize a
2262 record then ignore and return TRUE. */
2263 FIXME("type %d is unimplemented\n", type);
2264 break;
2266 tmprc.left = tmprc.top = 0;
2267 tmprc.right = tmprc.bottom = 1000;
2268 LPtoDP(hdc, (POINT*)&tmprc, 2);
2269 TRACE("L:0,0 - 1000,1000 -> D:%s\n", wine_dbgstr_rect(&tmprc));
2271 return TRUE;
2275 /*****************************************************************************
2277 * EnumEnhMetaFile (GDI32.@)
2279 * Walk an enhanced metafile, calling a user-specified function _EnhMetaFunc_
2280 * for each
2281 * record. Returns when either every record has been used or
2282 * when _EnhMetaFunc_ returns FALSE.
2285 * RETURNS
2286 * TRUE if every record is used, FALSE if any invocation of _EnhMetaFunc_
2287 * returns FALSE.
2289 * BUGS
2290 * Ignores rect.
2292 * NOTES
2293 * This function behaves differently in Win9x and WinNT.
2295 * In WinNT, the DC's world transform is updated as the EMF changes
2296 * the Window/Viewport Extent and Origin or its world transform.
2297 * The actual Window/Viewport Extent and Origin are left untouched.
2299 * In Win9x, the DC is left untouched, and PlayEnhMetaFileRecord
2300 * updates the scaling itself but only just before a record that
2301 * writes anything to the DC.
2303 * I'm not sure where the data (enum_emh_data) is stored in either
2304 * version. For this implementation, it is stored before the handle
2305 * table, but it could be stored in the DC, in the EMF handle or in
2306 * TLS.
2307 * MJM 5 Oct 2002
2309 BOOL WINAPI EnumEnhMetaFile(
2310 HDC hdc, /* [in] device context to pass to _EnhMetaFunc_ */
2311 HENHMETAFILE hmf, /* [in] EMF to walk */
2312 ENHMFENUMPROC callback, /* [in] callback function */
2313 LPVOID data, /* [in] optional data for callback function */
2314 const RECT *lpRect /* [in] bounding rectangle for rendered metafile */
2317 BOOL ret;
2318 ENHMETAHEADER *emh;
2319 ENHMETARECORD *emr;
2320 DWORD offset;
2321 UINT i;
2322 HANDLETABLE *ht;
2323 INT savedMode = 0;
2324 XFORM savedXform;
2325 HPEN hPen = NULL;
2326 HBRUSH hBrush = NULL;
2327 HFONT hFont = NULL;
2328 HRGN hRgn = NULL;
2329 enum_emh_data *info;
2330 SIZE vp_size, win_size;
2331 POINT vp_org, win_org;
2332 INT mapMode = MM_TEXT, old_align = 0, old_rop2 = 0, old_arcdir = 0, old_polyfill = 0, old_stretchblt = 0;
2333 COLORREF old_text_color = 0, old_bk_color = 0;
2335 if(!lpRect && hdc)
2337 SetLastError(ERROR_INVALID_PARAMETER);
2338 return FALSE;
2341 emh = EMF_GetEnhMetaHeader(hmf);
2342 if(!emh) {
2343 SetLastError(ERROR_INVALID_HANDLE);
2344 return FALSE;
2347 info = HeapAlloc( GetProcessHeap(), 0,
2348 sizeof (enum_emh_data) + sizeof(HANDLETABLE) * emh->nHandles );
2349 if(!info)
2351 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2352 return FALSE;
2354 info->state.mode = MM_TEXT;
2355 info->state.wndOrgX = 0;
2356 info->state.wndOrgY = 0;
2357 info->state.wndExtX = 1;
2358 info->state.wndExtY = 1;
2359 info->state.vportOrgX = 0;
2360 info->state.vportOrgY = 0;
2361 info->state.vportExtX = 1;
2362 info->state.vportExtY = 1;
2363 info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
2364 info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
2365 info->state.world_transform.eDx = info->state.world_transform.eDy = 0;
2367 info->state.next = NULL;
2368 info->save_level = 0;
2369 info->saved_state = NULL;
2371 ht = (HANDLETABLE*) &info[1];
2372 ht->objectHandle[0] = hmf;
2373 for(i = 1; i < emh->nHandles; i++)
2374 ht->objectHandle[i] = NULL;
2376 if(hdc)
2378 savedMode = SetGraphicsMode(hdc, GM_ADVANCED);
2379 GetWorldTransform(hdc, &savedXform);
2380 GetViewportExtEx(hdc, &vp_size);
2381 GetWindowExtEx(hdc, &win_size);
2382 GetViewportOrgEx(hdc, &vp_org);
2383 GetWindowOrgEx(hdc, &win_org);
2384 mapMode = GetMapMode(hdc);
2386 /* save DC */
2387 hPen = GetCurrentObject(hdc, OBJ_PEN);
2388 hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
2389 hFont = GetCurrentObject(hdc, OBJ_FONT);
2391 hRgn = CreateRectRgn(0, 0, 0, 0);
2392 if (!GetClipRgn(hdc, hRgn))
2394 DeleteObject(hRgn);
2395 hRgn = 0;
2398 old_text_color = SetTextColor(hdc, RGB(0,0,0));
2399 old_bk_color = SetBkColor(hdc, RGB(0xff, 0xff, 0xff));
2400 old_align = SetTextAlign(hdc, 0);
2401 old_rop2 = SetROP2(hdc, R2_COPYPEN);
2402 old_arcdir = SetArcDirection(hdc, AD_COUNTERCLOCKWISE);
2403 old_polyfill = SetPolyFillMode(hdc, ALTERNATE);
2404 old_stretchblt = SetStretchBltMode(hdc, BLACKONWHITE);
2406 if ( IS_WIN9X() )
2408 /* Win95 leaves the vp/win ext/org info alone */
2409 info->init_transform.eM11 = 1.0;
2410 info->init_transform.eM12 = 0.0;
2411 info->init_transform.eM21 = 0.0;
2412 info->init_transform.eM22 = 1.0;
2413 info->init_transform.eDx = 0.0;
2414 info->init_transform.eDy = 0.0;
2416 else
2418 /* WinNT combines the vp/win ext/org info into a transform */
2419 double xscale, yscale;
2420 xscale = (double)vp_size.cx / (double)win_size.cx;
2421 yscale = (double)vp_size.cy / (double)win_size.cy;
2422 info->init_transform.eM11 = xscale;
2423 info->init_transform.eM12 = 0.0;
2424 info->init_transform.eM21 = 0.0;
2425 info->init_transform.eM22 = yscale;
2426 info->init_transform.eDx = (double)vp_org.x - xscale * (double)win_org.x;
2427 info->init_transform.eDy = (double)vp_org.y - yscale * (double)win_org.y;
2429 CombineTransform(&info->init_transform, &savedXform, &info->init_transform);
2432 if ( lpRect && WIDTH(emh->rclFrame) && HEIGHT(emh->rclFrame) )
2434 double xSrcPixSize, ySrcPixSize, xscale, yscale;
2435 XFORM xform;
2437 TRACE("rect: %s. rclFrame: (%d,%d)-(%d,%d)\n", wine_dbgstr_rect(lpRect),
2438 emh->rclFrame.left, emh->rclFrame.top, emh->rclFrame.right,
2439 emh->rclFrame.bottom);
2441 xSrcPixSize = (double) emh->szlMillimeters.cx / emh->szlDevice.cx;
2442 ySrcPixSize = (double) emh->szlMillimeters.cy / emh->szlDevice.cy;
2443 xscale = (double) WIDTH(*lpRect) * 100.0 /
2444 WIDTH(emh->rclFrame) * xSrcPixSize;
2445 yscale = (double) HEIGHT(*lpRect) * 100.0 /
2446 HEIGHT(emh->rclFrame) * ySrcPixSize;
2447 TRACE("xscale = %f, yscale = %f\n", xscale, yscale);
2449 xform.eM11 = xscale;
2450 xform.eM12 = 0;
2451 xform.eM21 = 0;
2452 xform.eM22 = yscale;
2453 xform.eDx = (double) lpRect->left - (double) WIDTH(*lpRect) / WIDTH(emh->rclFrame) * emh->rclFrame.left;
2454 xform.eDy = (double) lpRect->top - (double) HEIGHT(*lpRect) / HEIGHT(emh->rclFrame) * emh->rclFrame.top;
2456 CombineTransform(&info->init_transform, &xform, &info->init_transform);
2459 /* WinNT resets the current vp/win org/ext */
2460 if ( !IS_WIN9X() )
2462 SetMapMode(hdc, MM_TEXT);
2463 SetWindowOrgEx(hdc, 0, 0, NULL);
2464 SetViewportOrgEx(hdc, 0, 0, NULL);
2465 EMF_Update_MF_Xform(hdc, info);
2469 ret = TRUE;
2470 offset = 0;
2471 while(ret && offset < emh->nBytes)
2473 emr = (ENHMETARECORD *)((char *)emh + offset);
2475 if (offset + 8 > emh->nBytes ||
2476 offset > offset + emr->nSize ||
2477 offset + emr->nSize > emh->nBytes)
2479 WARN("record truncated\n");
2480 break;
2483 /* In Win9x mode we update the xform if the record will produce output */
2484 if (hdc && IS_WIN9X() && emr_produces_output(emr->iType))
2485 EMF_Update_MF_Xform(hdc, info);
2487 TRACE("Calling EnumFunc with record %s, size %d\n", get_emr_name(emr->iType), emr->nSize);
2488 ret = (*callback)(hdc, ht, emr, emh->nHandles, (LPARAM)data);
2489 offset += emr->nSize;
2492 if (hdc)
2494 SetStretchBltMode(hdc, old_stretchblt);
2495 SetPolyFillMode(hdc, old_polyfill);
2496 SetArcDirection(hdc, old_arcdir);
2497 SetROP2(hdc, old_rop2);
2498 SetTextAlign(hdc, old_align);
2499 SetBkColor(hdc, old_bk_color);
2500 SetTextColor(hdc, old_text_color);
2502 /* restore DC */
2503 SelectObject(hdc, hBrush);
2504 SelectObject(hdc, hPen);
2505 SelectObject(hdc, hFont);
2506 ExtSelectClipRgn(hdc, hRgn, RGN_COPY);
2507 DeleteObject(hRgn);
2509 SetWorldTransform(hdc, &savedXform);
2510 if (savedMode)
2511 SetGraphicsMode(hdc, savedMode);
2512 SetMapMode(hdc, mapMode);
2513 SetWindowOrgEx(hdc, win_org.x, win_org.y, NULL);
2514 SetWindowExtEx(hdc, win_size.cx, win_size.cy, NULL);
2515 SetViewportOrgEx(hdc, vp_org.x, vp_org.y, NULL);
2516 SetViewportExtEx(hdc, vp_size.cx, vp_size.cy, NULL);
2519 for(i = 1; i < emh->nHandles; i++) /* Don't delete element 0 (hmf) */
2520 if( (ht->objectHandle)[i] )
2521 DeleteObject( (ht->objectHandle)[i] );
2523 while (info->saved_state)
2525 EMF_dc_state *state = info->saved_state;
2526 info->saved_state = info->saved_state->next;
2527 HeapFree( GetProcessHeap(), 0, state );
2529 HeapFree( GetProcessHeap(), 0, info );
2530 return ret;
2533 static INT CALLBACK EMF_PlayEnhMetaFileCallback(HDC hdc, HANDLETABLE *ht,
2534 const ENHMETARECORD *emr,
2535 INT handles, LPARAM data)
2537 return PlayEnhMetaFileRecord(hdc, ht, emr, handles);
2540 /**************************************************************************
2541 * PlayEnhMetaFile (GDI32.@)
2543 * Renders an enhanced metafile into a specified rectangle *lpRect
2544 * in device context hdc.
2546 * RETURNS
2547 * Success: TRUE
2548 * Failure: FALSE
2550 BOOL WINAPI PlayEnhMetaFile(
2551 HDC hdc, /* [in] DC to render into */
2552 HENHMETAFILE hmf, /* [in] metafile to render */
2553 const RECT *lpRect /* [in] rectangle to place metafile inside */
2556 return EnumEnhMetaFile(hdc, hmf, EMF_PlayEnhMetaFileCallback, NULL,
2557 lpRect);
2560 /*****************************************************************************
2561 * DeleteEnhMetaFile (GDI32.@)
2563 * Deletes an enhanced metafile and frees the associated storage.
2565 BOOL WINAPI DeleteEnhMetaFile(HENHMETAFILE hmf)
2567 return EMF_Delete_HENHMETAFILE( hmf );
2570 /*****************************************************************************
2571 * CopyEnhMetaFileA (GDI32.@)
2573 * Duplicate an enhanced metafile.
2577 HENHMETAFILE WINAPI CopyEnhMetaFileA(
2578 HENHMETAFILE hmfSrc,
2579 LPCSTR file)
2581 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2582 HENHMETAFILE hmfDst;
2584 if(!emrSrc) return FALSE;
2585 if (!file) {
2586 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2587 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2588 hmfDst = EMF_Create_HENHMETAFILE( emrDst, emrSrc->nBytes, FALSE );
2589 if (!hmfDst)
2590 HeapFree( GetProcessHeap(), 0, emrDst );
2591 } else {
2592 HANDLE hFile;
2593 DWORD w;
2594 hFile = CreateFileA( file, GENERIC_WRITE | GENERIC_READ, 0,
2595 NULL, CREATE_ALWAYS, 0, 0);
2596 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2597 CloseHandle( hFile );
2598 /* Reopen file for reading only, so that apps can share
2599 read access to the file while hmf is still valid */
2600 hFile = CreateFileA( file, GENERIC_READ, FILE_SHARE_READ,
2601 NULL, OPEN_EXISTING, 0, 0);
2602 if(hFile == INVALID_HANDLE_VALUE) {
2603 ERR("Can't reopen emf for reading\n");
2604 return 0;
2606 hmfDst = EMF_GetEnhMetaFile( hFile );
2607 CloseHandle( hFile );
2609 return hmfDst;
2612 /*****************************************************************************
2613 * CopyEnhMetaFileW (GDI32.@)
2615 * See CopyEnhMetaFileA.
2619 HENHMETAFILE WINAPI CopyEnhMetaFileW(
2620 HENHMETAFILE hmfSrc,
2621 LPCWSTR file)
2623 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2624 HENHMETAFILE hmfDst;
2626 if(!emrSrc) return FALSE;
2627 if (!file) {
2628 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2629 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2630 hmfDst = EMF_Create_HENHMETAFILE( emrDst, emrSrc->nBytes, FALSE );
2631 if (!hmfDst)
2632 HeapFree( GetProcessHeap(), 0, emrDst );
2633 } else {
2634 HANDLE hFile;
2635 DWORD w;
2636 hFile = CreateFileW( file, GENERIC_WRITE | GENERIC_READ, 0,
2637 NULL, CREATE_ALWAYS, 0, 0);
2638 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2639 CloseHandle( hFile );
2640 /* Reopen file for reading only, so that apps can share
2641 read access to the file while hmf is still valid */
2642 hFile = CreateFileW( file, GENERIC_READ, FILE_SHARE_READ,
2643 NULL, OPEN_EXISTING, 0, 0);
2644 if(hFile == INVALID_HANDLE_VALUE) {
2645 ERR("Can't reopen emf for reading\n");
2646 return 0;
2648 hmfDst = EMF_GetEnhMetaFile( hFile );
2649 CloseHandle( hFile );
2651 return hmfDst;
2655 /* Struct to be used to be passed in the LPVOID parameter for cbEnhPaletteCopy */
2656 typedef struct tagEMF_PaletteCopy
2658 UINT cEntries;
2659 LPPALETTEENTRY lpPe;
2660 } EMF_PaletteCopy;
2662 /***************************************************************
2663 * Find the EMR_EOF record and then use it to find the
2664 * palette entries for this enhanced metafile.
2665 * The lpData is actually a pointer to an EMF_PaletteCopy struct
2666 * which contains the max number of elements to copy and where
2667 * to copy them to.
2669 * NOTE: To be used by GetEnhMetaFilePaletteEntries only!
2671 static INT CALLBACK cbEnhPaletteCopy( HDC a,
2672 HANDLETABLE *b,
2673 const ENHMETARECORD *lpEMR,
2674 INT c,
2675 LPARAM lpData )
2678 if ( lpEMR->iType == EMR_EOF )
2680 const EMREOF *lpEof = (const EMREOF *)lpEMR;
2681 EMF_PaletteCopy* info = (EMF_PaletteCopy*)lpData;
2682 DWORD dwNumPalToCopy = min( lpEof->nPalEntries, info->cEntries );
2684 TRACE( "copying 0x%08x palettes\n", dwNumPalToCopy );
2686 memcpy( info->lpPe, (LPCSTR)lpEof + lpEof->offPalEntries,
2687 sizeof( *(info->lpPe) ) * dwNumPalToCopy );
2689 /* Update the passed data as a return code */
2690 info->lpPe = NULL; /* Palettes were copied! */
2691 info->cEntries = dwNumPalToCopy;
2693 return FALSE; /* That's all we need */
2696 return TRUE;
2699 /*****************************************************************************
2700 * GetEnhMetaFilePaletteEntries (GDI32.@)
2702 * Copy the palette and report size
2704 * BUGS: Error codes (SetLastError) are not set on failures
2706 UINT WINAPI GetEnhMetaFilePaletteEntries( HENHMETAFILE hEmf,
2707 UINT cEntries,
2708 LPPALETTEENTRY lpPe )
2710 ENHMETAHEADER* enhHeader = EMF_GetEnhMetaHeader( hEmf );
2711 EMF_PaletteCopy infoForCallBack;
2713 TRACE( "(%p,%d,%p)\n", hEmf, cEntries, lpPe );
2715 if (!enhHeader) return 0;
2717 /* First check if there are any palettes associated with
2718 this metafile. */
2719 if ( enhHeader->nPalEntries == 0 ) return 0;
2721 /* Is the user requesting the number of palettes? */
2722 if ( lpPe == NULL ) return enhHeader->nPalEntries;
2724 /* Copy cEntries worth of PALETTEENTRY structs into the buffer */
2725 infoForCallBack.cEntries = cEntries;
2726 infoForCallBack.lpPe = lpPe;
2728 if ( !EnumEnhMetaFile( 0, hEmf, cbEnhPaletteCopy,
2729 &infoForCallBack, 0 ) )
2730 return GDI_ERROR;
2732 /* Verify that the callback executed correctly */
2733 if ( infoForCallBack.lpPe != NULL )
2735 /* Callback proc had error! */
2736 ERR( "cbEnhPaletteCopy didn't execute correctly\n" );
2737 return GDI_ERROR;
2740 return infoForCallBack.cEntries;
2743 /******************************************************************
2744 * extract_emf_from_comment
2746 * If the WMF was created by GetWinMetaFileBits, then extract the
2747 * original EMF that is stored in MFCOMMENT chunks.
2749 static HENHMETAFILE extract_emf_from_comment( const BYTE *buf, UINT mf_size )
2751 METAHEADER *mh = (METAHEADER *)buf;
2752 METARECORD *mr;
2753 emf_in_wmf_comment *chunk;
2754 WORD checksum = 0;
2755 DWORD size = 0, remaining, chunks;
2756 BYTE *emf_bits = NULL, *ptr;
2757 UINT offset;
2758 HENHMETAFILE emf = NULL;
2760 if (mf_size < sizeof(*mh)) return NULL;
2762 for (offset = mh->mtHeaderSize * 2; offset < mf_size; offset += (mr->rdSize * 2))
2764 mr = (METARECORD *)((char *)mh + offset);
2765 chunk = (emf_in_wmf_comment *)(mr->rdParm + 2);
2767 if (mr->rdFunction != META_ESCAPE || mr->rdParm[0] != MFCOMMENT) goto done;
2768 if (chunk->magic != WMFC_MAGIC) goto done;
2770 if (!emf_bits)
2772 size = remaining = chunk->emf_size;
2773 chunks = chunk->num_chunks;
2774 emf_bits = ptr = HeapAlloc( GetProcessHeap(), 0, size );
2775 if (!emf_bits) goto done;
2777 if (chunk->chunk_size > remaining) goto done;
2778 remaining -= chunk->chunk_size;
2779 if (chunk->remaining_size != remaining) goto done;
2780 memcpy( ptr, chunk->emf_data, chunk->chunk_size );
2781 ptr += chunk->chunk_size;
2782 if (--chunks == 0) break;
2785 for (offset = 0; offset < mf_size / 2; offset++)
2786 checksum += *((WORD *)buf + offset);
2787 if (checksum) goto done;
2789 emf = SetEnhMetaFileBits( size, emf_bits );
2791 done:
2792 HeapFree( GetProcessHeap(), 0, emf_bits );
2793 return emf;
2796 typedef struct wmf_in_emf_comment
2798 DWORD ident;
2799 DWORD iComment;
2800 DWORD nVersion;
2801 DWORD nChecksum;
2802 DWORD fFlags;
2803 DWORD cbWinMetaFile;
2804 } wmf_in_emf_comment;
2806 /******************************************************************
2807 * SetWinMetaFileBits (GDI32.@)
2809 * Translate from old style to new style.
2812 HENHMETAFILE WINAPI SetWinMetaFileBits(UINT cbBuffer, const BYTE *lpbBuffer, HDC hdcRef,
2813 const METAFILEPICT *lpmfp)
2815 static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
2816 HMETAFILE hmf = NULL;
2817 HENHMETAFILE ret = NULL;
2818 HDC hdc = NULL, hdcdisp = NULL;
2819 RECT rc, *prcFrame = NULL;
2820 LONG mm, xExt, yExt;
2821 INT horzsize, vertsize, horzres, vertres;
2823 TRACE("(%d, %p, %p, %p)\n", cbBuffer, lpbBuffer, hdcRef, lpmfp);
2825 hmf = SetMetaFileBitsEx(cbBuffer, lpbBuffer);
2826 if(!hmf)
2828 WARN("SetMetaFileBitsEx failed\n");
2829 return NULL;
2832 ret = extract_emf_from_comment( lpbBuffer, cbBuffer );
2833 if (ret) return ret;
2835 if(!hdcRef)
2836 hdcRef = hdcdisp = CreateDCW(szDisplayW, NULL, NULL, NULL);
2838 if (lpmfp)
2840 TRACE("mm = %d %dx%d\n", lpmfp->mm, lpmfp->xExt, lpmfp->yExt);
2842 mm = lpmfp->mm;
2843 xExt = lpmfp->xExt;
2844 yExt = lpmfp->yExt;
2846 else
2848 TRACE("lpmfp == NULL\n");
2850 /* Use the whole device surface */
2851 mm = MM_ANISOTROPIC;
2852 xExt = 0;
2853 yExt = 0;
2856 if (mm == MM_ISOTROPIC || mm == MM_ANISOTROPIC)
2858 if (xExt < 0 || yExt < 0)
2860 /* Use the whole device surface */
2861 xExt = 0;
2862 yExt = 0;
2865 /* Use the x and y extents as the frame box */
2866 if (xExt && yExt)
2868 rc.left = rc.top = 0;
2869 rc.right = xExt;
2870 rc.bottom = yExt;
2871 prcFrame = &rc;
2875 if(!(hdc = CreateEnhMetaFileW(hdcRef, NULL, prcFrame, NULL)))
2877 ERR("CreateEnhMetaFile failed\n");
2878 goto end;
2882 * Write the original METAFILE into the enhanced metafile.
2883 * It is encapsulated in a GDICOMMENT_WINDOWS_METAFILE record.
2885 if (mm != MM_TEXT)
2887 wmf_in_emf_comment *mfcomment;
2888 UINT mfcomment_size;
2890 mfcomment_size = sizeof (*mfcomment) + cbBuffer;
2891 mfcomment = HeapAlloc(GetProcessHeap(), 0, mfcomment_size);
2892 if (mfcomment)
2894 mfcomment->ident = GDICOMMENT_IDENTIFIER;
2895 mfcomment->iComment = GDICOMMENT_WINDOWS_METAFILE;
2896 mfcomment->nVersion = 0x00000300;
2897 mfcomment->nChecksum = 0; /* FIXME */
2898 mfcomment->fFlags = 0;
2899 mfcomment->cbWinMetaFile = cbBuffer;
2900 memcpy(&mfcomment[1], lpbBuffer, cbBuffer);
2901 GdiComment(hdc, mfcomment_size, (BYTE*) mfcomment);
2902 HeapFree(GetProcessHeap(), 0, mfcomment);
2904 SetMapMode(hdc, mm);
2908 horzsize = GetDeviceCaps(hdcRef, HORZSIZE);
2909 vertsize = GetDeviceCaps(hdcRef, VERTSIZE);
2910 horzres = GetDeviceCaps(hdcRef, HORZRES);
2911 vertres = GetDeviceCaps(hdcRef, VERTRES);
2913 if (!xExt || !yExt)
2915 /* Use the whole device surface */
2916 xExt = horzres;
2917 yExt = vertres;
2919 else
2921 xExt = MulDiv(xExt, horzres, 100 * horzsize);
2922 yExt = MulDiv(yExt, vertres, 100 * vertsize);
2925 /* set the initial viewport:window ratio as 1:1 */
2926 SetViewportExtEx(hdc, xExt, yExt, NULL);
2927 SetWindowExtEx(hdc, xExt, yExt, NULL);
2929 PlayMetaFile(hdc, hmf);
2931 ret = CloseEnhMetaFile(hdc);
2932 end:
2933 if (hdcdisp) DeleteDC(hdcdisp);
2934 DeleteMetaFile(hmf);
2935 return ret;