cmd: Fix subdirectory prefix in for loops.
[wine.git] / dlls / gdi32 / enhmetafile.c
blob2d8a9b04fb2c2f3494ab359e387811cb7259e648
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, 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;
483 memmove(emh, buf, bufsize);
484 hmf = EMF_Create_HENHMETAFILE( emh, bufsize, FALSE );
485 if (!hmf)
486 HeapFree( GetProcessHeap(), 0, emh );
487 return hmf;
490 /*****************************************************************************
491 * GetEnhMetaFileBits (GDI32.@)
494 UINT WINAPI GetEnhMetaFileBits(
495 HENHMETAFILE hmf,
496 UINT bufsize,
497 LPBYTE buf
500 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader( hmf );
501 UINT size;
503 if(!emh) return 0;
505 size = emh->nBytes;
506 if( buf == NULL ) return size;
508 size = min( size, bufsize );
509 memmove(buf, emh, size);
510 return size;
513 typedef struct EMF_dc_state
515 INT mode;
516 XFORM world_transform;
517 INT wndOrgX;
518 INT wndOrgY;
519 INT wndExtX;
520 INT wndExtY;
521 INT vportOrgX;
522 INT vportOrgY;
523 INT vportExtX;
524 INT vportExtY;
525 struct EMF_dc_state *next;
526 } EMF_dc_state;
528 typedef struct enum_emh_data
530 XFORM init_transform;
531 EMF_dc_state state;
532 INT save_level;
533 EMF_dc_state *saved_state;
534 } enum_emh_data;
536 #define ENUM_GET_PRIVATE_DATA(ht) \
537 ((enum_emh_data*)(((unsigned char*)(ht))-sizeof (enum_emh_data)))
539 #define WIDTH(rect) ( (rect).right - (rect).left )
540 #define HEIGHT(rect) ( (rect).bottom - (rect).top )
542 #define IS_WIN9X() (GetVersion()&0x80000000)
544 static void EMF_Update_MF_Xform(HDC hdc, const enum_emh_data *info)
546 XFORM mapping_mode_trans, final_trans;
547 double scaleX, scaleY;
549 scaleX = (double)info->state.vportExtX / (double)info->state.wndExtX;
550 scaleY = (double)info->state.vportExtY / (double)info->state.wndExtY;
551 mapping_mode_trans.eM11 = scaleX;
552 mapping_mode_trans.eM12 = 0.0;
553 mapping_mode_trans.eM21 = 0.0;
554 mapping_mode_trans.eM22 = scaleY;
555 mapping_mode_trans.eDx = (double)info->state.vportOrgX - scaleX * (double)info->state.wndOrgX;
556 mapping_mode_trans.eDy = (double)info->state.vportOrgY - scaleY * (double)info->state.wndOrgY;
558 CombineTransform(&final_trans, &info->state.world_transform, &mapping_mode_trans);
559 CombineTransform(&final_trans, &final_trans, &info->init_transform);
561 if (!SetWorldTransform(hdc, &final_trans))
563 ERR("World transform failed!\n");
567 static void EMF_RestoreDC( enum_emh_data *info, INT level )
569 if (abs(level) > info->save_level || level == 0) return;
571 if (level < 0) level = info->save_level + level + 1;
573 while (info->save_level >= level)
575 EMF_dc_state *state = info->saved_state;
576 info->saved_state = state->next;
577 state->next = NULL;
578 if (--info->save_level < level)
579 info->state = *state;
580 HeapFree( GetProcessHeap(), 0, state );
584 static void EMF_SaveDC( enum_emh_data *info )
586 EMF_dc_state *state = HeapAlloc( GetProcessHeap(), 0, sizeof(*state));
587 if (state)
589 *state = info->state;
590 state->next = info->saved_state;
591 info->saved_state = state;
592 info->save_level++;
593 TRACE("save_level %d\n", info->save_level);
597 static void EMF_SetMapMode(HDC hdc, enum_emh_data *info)
599 INT horzSize = GetDeviceCaps( hdc, HORZSIZE );
600 INT vertSize = GetDeviceCaps( hdc, VERTSIZE );
601 INT horzRes = GetDeviceCaps( hdc, HORZRES );
602 INT vertRes = GetDeviceCaps( hdc, VERTRES );
604 TRACE("%d\n", info->state.mode);
606 switch(info->state.mode)
608 case MM_TEXT:
609 info->state.wndExtX = 1;
610 info->state.wndExtY = 1;
611 info->state.vportExtX = 1;
612 info->state.vportExtY = 1;
613 break;
614 case MM_LOMETRIC:
615 case MM_ISOTROPIC:
616 info->state.wndExtX = horzSize * 10;
617 info->state.wndExtY = vertSize * 10;
618 info->state.vportExtX = horzRes;
619 info->state.vportExtY = -vertRes;
620 break;
621 case MM_HIMETRIC:
622 info->state.wndExtX = horzSize * 100;
623 info->state.wndExtY = vertSize * 100;
624 info->state.vportExtX = horzRes;
625 info->state.vportExtY = -vertRes;
626 break;
627 case MM_LOENGLISH:
628 info->state.wndExtX = MulDiv(1000, horzSize, 254);
629 info->state.wndExtY = MulDiv(1000, vertSize, 254);
630 info->state.vportExtX = horzRes;
631 info->state.vportExtY = -vertRes;
632 break;
633 case MM_HIENGLISH:
634 info->state.wndExtX = MulDiv(10000, horzSize, 254);
635 info->state.wndExtY = MulDiv(10000, vertSize, 254);
636 info->state.vportExtX = horzRes;
637 info->state.vportExtY = -vertRes;
638 break;
639 case MM_TWIPS:
640 info->state.wndExtX = MulDiv(14400, horzSize, 254);
641 info->state.wndExtY = MulDiv(14400, vertSize, 254);
642 info->state.vportExtX = horzRes;
643 info->state.vportExtY = -vertRes;
644 break;
645 case MM_ANISOTROPIC:
646 break;
647 default:
648 return;
652 /***********************************************************************
653 * EMF_FixIsotropic
655 * Fix viewport extensions for isotropic mode.
658 static void EMF_FixIsotropic(HDC hdc, enum_emh_data *info)
660 double xdim = fabs((double)info->state.vportExtX * GetDeviceCaps( hdc, HORZSIZE ) /
661 (GetDeviceCaps( hdc, HORZRES ) * info->state.wndExtX));
662 double ydim = fabs((double)info->state.vportExtY * GetDeviceCaps( hdc, VERTSIZE ) /
663 (GetDeviceCaps( hdc, VERTRES ) * info->state.wndExtY));
665 if (xdim > ydim)
667 INT mincx = (info->state.vportExtX >= 0) ? 1 : -1;
668 info->state.vportExtX = floor(info->state.vportExtX * ydim / xdim + 0.5);
669 if (!info->state.vportExtX) info->state.vportExtX = mincx;
671 else
673 INT mincy = (info->state.vportExtY >= 0) ? 1 : -1;
674 info->state.vportExtY = floor(info->state.vportExtY * xdim / ydim + 0.5);
675 if (!info->state.vportExtY) info->state.vportExtY = mincy;
679 /*****************************************************************************
680 * emr_produces_output
682 * Returns TRUE if the record type writes something to the dc. Used by
683 * PlayEnhMetaFileRecord to determine whether it needs to update the
684 * dc's xform when in win9x mode.
686 * FIXME: need to test which records should be here.
688 static BOOL emr_produces_output(int type)
690 switch(type) {
691 case EMR_POLYBEZIER:
692 case EMR_POLYGON:
693 case EMR_POLYLINE:
694 case EMR_POLYBEZIERTO:
695 case EMR_POLYLINETO:
696 case EMR_POLYPOLYLINE:
697 case EMR_POLYPOLYGON:
698 case EMR_SETPIXELV:
699 case EMR_MOVETOEX:
700 case EMR_EXCLUDECLIPRECT:
701 case EMR_INTERSECTCLIPRECT:
702 case EMR_SELECTOBJECT:
703 case EMR_ANGLEARC:
704 case EMR_ELLIPSE:
705 case EMR_RECTANGLE:
706 case EMR_ROUNDRECT:
707 case EMR_ARC:
708 case EMR_CHORD:
709 case EMR_PIE:
710 case EMR_EXTFLOODFILL:
711 case EMR_LINETO:
712 case EMR_ARCTO:
713 case EMR_POLYDRAW:
714 case EMR_GDICOMMENT:
715 case EMR_FILLRGN:
716 case EMR_FRAMERGN:
717 case EMR_INVERTRGN:
718 case EMR_PAINTRGN:
719 case EMR_BITBLT:
720 case EMR_STRETCHBLT:
721 case EMR_MASKBLT:
722 case EMR_PLGBLT:
723 case EMR_SETDIBITSTODEVICE:
724 case EMR_STRETCHDIBITS:
725 case EMR_EXTTEXTOUTA:
726 case EMR_EXTTEXTOUTW:
727 case EMR_POLYBEZIER16:
728 case EMR_POLYGON16:
729 case EMR_POLYLINE16:
730 case EMR_POLYBEZIERTO16:
731 case EMR_POLYLINETO16:
732 case EMR_POLYPOLYLINE16:
733 case EMR_POLYPOLYGON16:
734 case EMR_POLYDRAW16:
735 case EMR_POLYTEXTOUTA:
736 case EMR_POLYTEXTOUTW:
737 case EMR_SMALLTEXTOUT:
738 case EMR_ALPHABLEND:
739 case EMR_TRANSPARENTBLT:
740 return TRUE;
741 default:
742 return FALSE;
747 /*****************************************************************************
748 * PlayEnhMetaFileRecord (GDI32.@)
750 * Render a single enhanced metafile record in the device context hdc.
752 * RETURNS
753 * TRUE (non zero) on success, FALSE on error.
754 * BUGS
755 * Many unimplemented records.
756 * No error handling on record play failures (ie checking return codes)
758 * NOTES
759 * WinNT actually updates the current world transform in this function
760 * whereas Win9x does not.
762 BOOL WINAPI PlayEnhMetaFileRecord(
763 HDC hdc, /* [in] device context in which to render EMF record */
764 LPHANDLETABLE handletable, /* [in] array of handles to be used in rendering record */
765 const ENHMETARECORD *mr, /* [in] EMF record to render */
766 UINT handles /* [in] size of handle array */
769 int type;
770 RECT tmprc;
771 enum_emh_data *info = ENUM_GET_PRIVATE_DATA(handletable);
773 TRACE("hdc = %p, handletable = %p, record = %p, numHandles = %d\n",
774 hdc, handletable, mr, handles);
775 if (!mr) return FALSE;
777 type = mr->iType;
779 TRACE("record %s\n", get_emr_name(type));
780 switch(type)
782 case EMR_HEADER:
783 break;
784 case EMR_EOF:
785 break;
786 case EMR_GDICOMMENT:
788 const EMRGDICOMMENT *lpGdiComment = (const EMRGDICOMMENT *)mr;
789 /* In an enhanced metafile, there can be both public and private GDI comments */
790 GdiComment( hdc, lpGdiComment->cbData, lpGdiComment->Data );
791 break;
793 case EMR_SETMAPMODE:
795 const EMRSETMAPMODE *pSetMapMode = (const EMRSETMAPMODE *)mr;
797 if (info->state.mode == pSetMapMode->iMode &&
798 (info->state.mode == MM_ISOTROPIC || info->state.mode == MM_ANISOTROPIC))
799 break;
800 info->state.mode = pSetMapMode->iMode;
801 EMF_SetMapMode(hdc, info);
803 if (!IS_WIN9X())
804 EMF_Update_MF_Xform(hdc, info);
806 break;
808 case EMR_SETBKMODE:
810 const EMRSETBKMODE *pSetBkMode = (const EMRSETBKMODE *)mr;
811 SetBkMode(hdc, pSetBkMode->iMode);
812 break;
814 case EMR_SETBKCOLOR:
816 const EMRSETBKCOLOR *pSetBkColor = (const EMRSETBKCOLOR *)mr;
817 SetBkColor(hdc, pSetBkColor->crColor);
818 break;
820 case EMR_SETPOLYFILLMODE:
822 const EMRSETPOLYFILLMODE *pSetPolyFillMode = (const EMRSETPOLYFILLMODE *)mr;
823 SetPolyFillMode(hdc, pSetPolyFillMode->iMode);
824 break;
826 case EMR_SETROP2:
828 const EMRSETROP2 *pSetROP2 = (const EMRSETROP2 *)mr;
829 SetROP2(hdc, pSetROP2->iMode);
830 break;
832 case EMR_SETSTRETCHBLTMODE:
834 const EMRSETSTRETCHBLTMODE *pSetStretchBltMode = (const EMRSETSTRETCHBLTMODE *)mr;
835 SetStretchBltMode(hdc, pSetStretchBltMode->iMode);
836 break;
838 case EMR_SETTEXTALIGN:
840 const EMRSETTEXTALIGN *pSetTextAlign = (const EMRSETTEXTALIGN *)mr;
841 SetTextAlign(hdc, pSetTextAlign->iMode);
842 break;
844 case EMR_SETTEXTCOLOR:
846 const EMRSETTEXTCOLOR *pSetTextColor = (const EMRSETTEXTCOLOR *)mr;
847 SetTextColor(hdc, pSetTextColor->crColor);
848 break;
850 case EMR_SAVEDC:
852 if (SaveDC( hdc ))
853 EMF_SaveDC( info );
854 break;
856 case EMR_RESTOREDC:
858 const EMRRESTOREDC *pRestoreDC = (const EMRRESTOREDC *)mr;
859 TRACE("EMR_RESTORE: %d\n", pRestoreDC->iRelative);
860 if (RestoreDC( hdc, pRestoreDC->iRelative ))
861 EMF_RestoreDC( info, pRestoreDC->iRelative );
862 break;
864 case EMR_INTERSECTCLIPRECT:
866 const EMRINTERSECTCLIPRECT *pClipRect = (const EMRINTERSECTCLIPRECT *)mr;
867 TRACE("EMR_INTERSECTCLIPRECT: rect %d,%d - %d, %d\n",
868 pClipRect->rclClip.left, pClipRect->rclClip.top,
869 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
870 IntersectClipRect(hdc, pClipRect->rclClip.left, pClipRect->rclClip.top,
871 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
872 break;
874 case EMR_SELECTOBJECT:
876 const EMRSELECTOBJECT *pSelectObject = (const EMRSELECTOBJECT *)mr;
877 if( pSelectObject->ihObject & 0x80000000 ) {
878 /* High order bit is set - it's a stock object
879 * Strip the high bit to get the index.
880 * See MSDN article Q142319
882 SelectObject( hdc, GetStockObject( pSelectObject->ihObject &
883 0x7fffffff ) );
884 } else {
885 /* High order bit wasn't set - not a stock object
887 SelectObject( hdc,
888 (handletable->objectHandle)[pSelectObject->ihObject] );
890 break;
892 case EMR_DELETEOBJECT:
894 const EMRDELETEOBJECT *pDeleteObject = (const EMRDELETEOBJECT *)mr;
895 DeleteObject( (handletable->objectHandle)[pDeleteObject->ihObject]);
896 (handletable->objectHandle)[pDeleteObject->ihObject] = 0;
897 break;
899 case EMR_SETWINDOWORGEX:
901 const EMRSETWINDOWORGEX *pSetWindowOrgEx = (const EMRSETWINDOWORGEX *)mr;
903 info->state.wndOrgX = pSetWindowOrgEx->ptlOrigin.x;
904 info->state.wndOrgY = pSetWindowOrgEx->ptlOrigin.y;
906 TRACE("SetWindowOrgEx: %d,%d\n", info->state.wndOrgX, info->state.wndOrgY);
908 if (!IS_WIN9X())
909 EMF_Update_MF_Xform(hdc, info);
911 break;
913 case EMR_SETWINDOWEXTEX:
915 const EMRSETWINDOWEXTEX *pSetWindowExtEx = (const EMRSETWINDOWEXTEX *)mr;
917 if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
918 break;
919 info->state.wndExtX = pSetWindowExtEx->szlExtent.cx;
920 info->state.wndExtY = pSetWindowExtEx->szlExtent.cy;
921 if (info->state.mode == MM_ISOTROPIC)
922 EMF_FixIsotropic(hdc, info);
924 TRACE("SetWindowExtEx: %d,%d\n",info->state.wndExtX, info->state.wndExtY);
926 if (!IS_WIN9X())
927 EMF_Update_MF_Xform(hdc, info);
929 break;
931 case EMR_SETVIEWPORTORGEX:
933 const EMRSETVIEWPORTORGEX *pSetViewportOrgEx = (const EMRSETVIEWPORTORGEX *)mr;
935 info->state.vportOrgX = pSetViewportOrgEx->ptlOrigin.x;
936 info->state.vportOrgY = pSetViewportOrgEx->ptlOrigin.y;
937 TRACE("SetViewportOrgEx: %d,%d\n", info->state.vportOrgX, info->state.vportOrgY);
939 if (!IS_WIN9X())
940 EMF_Update_MF_Xform(hdc, info);
942 break;
944 case EMR_SETVIEWPORTEXTEX:
946 const EMRSETVIEWPORTEXTEX *pSetViewportExtEx = (const EMRSETVIEWPORTEXTEX *)mr;
948 if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
949 break;
950 info->state.vportExtX = pSetViewportExtEx->szlExtent.cx;
951 info->state.vportExtY = pSetViewportExtEx->szlExtent.cy;
952 if (info->state.mode == MM_ISOTROPIC)
953 EMF_FixIsotropic(hdc, info);
954 TRACE("SetViewportExtEx: %d,%d\n", info->state.vportExtX, info->state.vportExtY);
956 if (!IS_WIN9X())
957 EMF_Update_MF_Xform(hdc, info);
959 break;
961 case EMR_CREATEPEN:
963 const EMRCREATEPEN *pCreatePen = (const EMRCREATEPEN *)mr;
964 (handletable->objectHandle)[pCreatePen->ihPen] =
965 CreatePenIndirect(&pCreatePen->lopn);
966 break;
968 case EMR_EXTCREATEPEN:
970 const EMREXTCREATEPEN *pPen = (const EMREXTCREATEPEN *)mr;
971 LOGBRUSH lb;
972 lb.lbStyle = pPen->elp.elpBrushStyle;
973 lb.lbColor = pPen->elp.elpColor;
974 lb.lbHatch = pPen->elp.elpHatch;
976 if(pPen->offBmi || pPen->offBits)
977 FIXME("EMR_EXTCREATEPEN: Need to copy brush bitmap\n");
979 (handletable->objectHandle)[pPen->ihPen] =
980 ExtCreatePen(pPen->elp.elpPenStyle, pPen->elp.elpWidth, &lb,
981 pPen->elp.elpNumEntries, pPen->elp.elpNumEntries ? pPen->elp.elpStyleEntry : NULL);
982 break;
984 case EMR_CREATEBRUSHINDIRECT:
986 const EMRCREATEBRUSHINDIRECT *pBrush = (const EMRCREATEBRUSHINDIRECT *)mr;
987 LOGBRUSH brush;
988 brush.lbStyle = pBrush->lb.lbStyle;
989 brush.lbColor = pBrush->lb.lbColor;
990 brush.lbHatch = pBrush->lb.lbHatch;
991 (handletable->objectHandle)[pBrush->ihBrush] = CreateBrushIndirect(&brush);
992 break;
994 case EMR_EXTCREATEFONTINDIRECTW:
996 const EMREXTCREATEFONTINDIRECTW *pFont = (const EMREXTCREATEFONTINDIRECTW *)mr;
997 (handletable->objectHandle)[pFont->ihFont] =
998 CreateFontIndirectW(&pFont->elfw.elfLogFont);
999 break;
1001 case EMR_MOVETOEX:
1003 const EMRMOVETOEX *pMoveToEx = (const EMRMOVETOEX *)mr;
1004 MoveToEx(hdc, pMoveToEx->ptl.x, pMoveToEx->ptl.y, NULL);
1005 break;
1007 case EMR_LINETO:
1009 const EMRLINETO *pLineTo = (const EMRLINETO *)mr;
1010 LineTo(hdc, pLineTo->ptl.x, pLineTo->ptl.y);
1011 break;
1013 case EMR_RECTANGLE:
1015 const EMRRECTANGLE *pRect = (const EMRRECTANGLE *)mr;
1016 Rectangle(hdc, pRect->rclBox.left, pRect->rclBox.top,
1017 pRect->rclBox.right, pRect->rclBox.bottom);
1018 break;
1020 case EMR_ELLIPSE:
1022 const EMRELLIPSE *pEllipse = (const EMRELLIPSE *)mr;
1023 Ellipse(hdc, pEllipse->rclBox.left, pEllipse->rclBox.top,
1024 pEllipse->rclBox.right, pEllipse->rclBox.bottom);
1025 break;
1027 case EMR_POLYGON16:
1029 const EMRPOLYGON16 *pPoly = (const EMRPOLYGON16 *)mr;
1030 /* Shouldn't use Polygon16 since pPoly->cpts is DWORD */
1031 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1032 pPoly->cpts * sizeof(POINT) );
1033 DWORD i;
1034 for(i = 0; i < pPoly->cpts; i++)
1036 pts[i].x = pPoly->apts[i].x;
1037 pts[i].y = pPoly->apts[i].y;
1039 Polygon(hdc, pts, pPoly->cpts);
1040 HeapFree( GetProcessHeap(), 0, pts );
1041 break;
1043 case EMR_POLYLINE16:
1045 const EMRPOLYLINE16 *pPoly = (const EMRPOLYLINE16 *)mr;
1046 /* Shouldn't use Polyline16 since pPoly->cpts is DWORD */
1047 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1048 pPoly->cpts * sizeof(POINT) );
1049 DWORD i;
1050 for(i = 0; i < pPoly->cpts; i++)
1052 pts[i].x = pPoly->apts[i].x;
1053 pts[i].y = pPoly->apts[i].y;
1055 Polyline(hdc, pts, pPoly->cpts);
1056 HeapFree( GetProcessHeap(), 0, pts );
1057 break;
1059 case EMR_POLYLINETO16:
1061 const EMRPOLYLINETO16 *pPoly = (const EMRPOLYLINETO16 *)mr;
1062 /* Shouldn't use PolylineTo16 since pPoly->cpts is DWORD */
1063 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1064 pPoly->cpts * sizeof(POINT) );
1065 DWORD i;
1066 for(i = 0; i < pPoly->cpts; i++)
1068 pts[i].x = pPoly->apts[i].x;
1069 pts[i].y = pPoly->apts[i].y;
1071 PolylineTo(hdc, pts, pPoly->cpts);
1072 HeapFree( GetProcessHeap(), 0, pts );
1073 break;
1075 case EMR_POLYBEZIER16:
1077 const EMRPOLYBEZIER16 *pPoly = (const EMRPOLYBEZIER16 *)mr;
1078 /* Shouldn't use PolyBezier16 since pPoly->cpts is DWORD */
1079 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1080 pPoly->cpts * sizeof(POINT) );
1081 DWORD i;
1082 for(i = 0; i < pPoly->cpts; i++)
1084 pts[i].x = pPoly->apts[i].x;
1085 pts[i].y = pPoly->apts[i].y;
1087 PolyBezier(hdc, pts, pPoly->cpts);
1088 HeapFree( GetProcessHeap(), 0, pts );
1089 break;
1091 case EMR_POLYBEZIERTO16:
1093 const EMRPOLYBEZIERTO16 *pPoly = (const EMRPOLYBEZIERTO16 *)mr;
1094 /* Shouldn't use PolyBezierTo16 since pPoly->cpts is DWORD */
1095 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1096 pPoly->cpts * sizeof(POINT) );
1097 DWORD i;
1098 for(i = 0; i < pPoly->cpts; i++)
1100 pts[i].x = pPoly->apts[i].x;
1101 pts[i].y = pPoly->apts[i].y;
1103 PolyBezierTo(hdc, pts, pPoly->cpts);
1104 HeapFree( GetProcessHeap(), 0, pts );
1105 break;
1107 case EMR_POLYPOLYGON16:
1109 const EMRPOLYPOLYGON16 *pPolyPoly = (const EMRPOLYPOLYGON16 *)mr;
1110 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1111 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1113 const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1114 POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1115 DWORD i;
1116 for(i = 0; i < pPolyPoly->cpts; i++)
1118 pt[i].x = pts[i].x;
1119 pt[i].y = pts[i].y;
1121 PolyPolygon(hdc, pt, (const INT*)pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1122 HeapFree( GetProcessHeap(), 0, pt );
1123 break;
1125 case EMR_POLYPOLYLINE16:
1127 const EMRPOLYPOLYLINE16 *pPolyPoly = (const EMRPOLYPOLYLINE16 *)mr;
1128 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1129 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1131 const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1132 POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1133 DWORD i;
1134 for(i = 0; i < pPolyPoly->cpts; i++)
1136 pt[i].x = pts[i].x;
1137 pt[i].y = pts[i].y;
1139 PolyPolyline(hdc, pt, pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1140 HeapFree( GetProcessHeap(), 0, pt );
1141 break;
1144 case EMR_STRETCHDIBITS:
1146 const EMRSTRETCHDIBITS *pStretchDIBits = (const EMRSTRETCHDIBITS *)mr;
1148 StretchDIBits(hdc,
1149 pStretchDIBits->xDest,
1150 pStretchDIBits->yDest,
1151 pStretchDIBits->cxDest,
1152 pStretchDIBits->cyDest,
1153 pStretchDIBits->xSrc,
1154 pStretchDIBits->ySrc,
1155 pStretchDIBits->cxSrc,
1156 pStretchDIBits->cySrc,
1157 (const BYTE *)mr + pStretchDIBits->offBitsSrc,
1158 (const BITMAPINFO *)((const BYTE *)mr + pStretchDIBits->offBmiSrc),
1159 pStretchDIBits->iUsageSrc,
1160 pStretchDIBits->dwRop);
1161 break;
1164 case EMR_EXTTEXTOUTA:
1166 const EMREXTTEXTOUTA *pExtTextOutA = (const EMREXTTEXTOUTA *)mr;
1167 RECT rc;
1168 const INT *dx = NULL;
1169 int old_mode;
1171 rc.left = pExtTextOutA->emrtext.rcl.left;
1172 rc.top = pExtTextOutA->emrtext.rcl.top;
1173 rc.right = pExtTextOutA->emrtext.rcl.right;
1174 rc.bottom = pExtTextOutA->emrtext.rcl.bottom;
1175 TRACE("EMR_EXTTEXTOUTA: x,y = %d, %d. rect = %s. flags %08x\n",
1176 pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1177 wine_dbgstr_rect(&rc), pExtTextOutA->emrtext.fOptions);
1179 old_mode = SetGraphicsMode(hdc, pExtTextOutA->iGraphicsMode);
1180 /* Reselect the font back into the dc so that the transformation
1181 gets updated. */
1182 SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1184 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1185 * These files can be enumerated and played under Win98 just
1186 * fine, but at least Win2k chokes on them.
1188 if (pExtTextOutA->emrtext.offDx)
1189 dx = (const INT *)((const BYTE *)mr + pExtTextOutA->emrtext.offDx);
1191 ExtTextOutA(hdc, pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1192 pExtTextOutA->emrtext.fOptions, &rc,
1193 (LPCSTR)((const BYTE *)mr + pExtTextOutA->emrtext.offString), pExtTextOutA->emrtext.nChars,
1194 dx);
1196 SetGraphicsMode(hdc, old_mode);
1197 break;
1200 case EMR_EXTTEXTOUTW:
1202 const EMREXTTEXTOUTW *pExtTextOutW = (const EMREXTTEXTOUTW *)mr;
1203 RECT rc;
1204 const INT *dx = NULL;
1205 int old_mode;
1207 rc.left = pExtTextOutW->emrtext.rcl.left;
1208 rc.top = pExtTextOutW->emrtext.rcl.top;
1209 rc.right = pExtTextOutW->emrtext.rcl.right;
1210 rc.bottom = pExtTextOutW->emrtext.rcl.bottom;
1211 TRACE("EMR_EXTTEXTOUTW: x,y = %d, %d. rect = %s. flags %08x\n",
1212 pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1213 wine_dbgstr_rect(&rc), pExtTextOutW->emrtext.fOptions);
1215 old_mode = SetGraphicsMode(hdc, pExtTextOutW->iGraphicsMode);
1216 /* Reselect the font back into the dc so that the transformation
1217 gets updated. */
1218 SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1220 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1221 * These files can be enumerated and played under Win98 just
1222 * fine, but at least Win2k chokes on them.
1224 if (pExtTextOutW->emrtext.offDx)
1225 dx = (const INT *)((const BYTE *)mr + pExtTextOutW->emrtext.offDx);
1227 ExtTextOutW(hdc, pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1228 pExtTextOutW->emrtext.fOptions, &rc,
1229 (LPCWSTR)((const BYTE *)mr + pExtTextOutW->emrtext.offString), pExtTextOutW->emrtext.nChars,
1230 dx);
1232 SetGraphicsMode(hdc, old_mode);
1233 break;
1236 case EMR_CREATEPALETTE:
1238 const EMRCREATEPALETTE *lpCreatePal = (const EMRCREATEPALETTE *)mr;
1240 (handletable->objectHandle)[ lpCreatePal->ihPal ] =
1241 CreatePalette( &lpCreatePal->lgpl );
1243 break;
1246 case EMR_SELECTPALETTE:
1248 const EMRSELECTPALETTE *lpSelectPal = (const EMRSELECTPALETTE *)mr;
1250 if( lpSelectPal->ihPal & 0x80000000 ) {
1251 SelectPalette( hdc, GetStockObject(lpSelectPal->ihPal & 0x7fffffff), TRUE);
1252 } else {
1253 SelectPalette( hdc, (handletable->objectHandle)[lpSelectPal->ihPal], TRUE);
1255 break;
1258 case EMR_REALIZEPALETTE:
1260 RealizePalette( hdc );
1261 break;
1264 case EMR_EXTSELECTCLIPRGN:
1266 const EMREXTSELECTCLIPRGN *lpRgn = (const EMREXTSELECTCLIPRGN *)mr;
1267 HRGN hRgn = 0;
1269 if (mr->nSize >= sizeof(*lpRgn) + sizeof(RGNDATAHEADER))
1270 hRgn = ExtCreateRegion( &info->init_transform, 0, (const RGNDATA *)lpRgn->RgnData );
1272 ExtSelectClipRgn(hdc, hRgn, (INT)(lpRgn->iMode));
1273 /* ExtSelectClipRgn created a copy of the region */
1274 DeleteObject(hRgn);
1275 break;
1278 case EMR_SETMETARGN:
1280 SetMetaRgn( hdc );
1281 break;
1284 case EMR_SETWORLDTRANSFORM:
1286 const EMRSETWORLDTRANSFORM *lpXfrm = (const EMRSETWORLDTRANSFORM *)mr;
1287 info->state.world_transform = lpXfrm->xform;
1289 if (!IS_WIN9X())
1290 EMF_Update_MF_Xform(hdc, info);
1292 break;
1295 case EMR_POLYBEZIER:
1297 const EMRPOLYBEZIER *lpPolyBez = (const EMRPOLYBEZIER *)mr;
1298 PolyBezier(hdc, (const POINT*)lpPolyBez->aptl, (UINT)lpPolyBez->cptl);
1299 break;
1302 case EMR_POLYGON:
1304 const EMRPOLYGON *lpPoly = (const EMRPOLYGON *)mr;
1305 Polygon( hdc, (const POINT*)lpPoly->aptl, (UINT)lpPoly->cptl );
1306 break;
1309 case EMR_POLYLINE:
1311 const EMRPOLYLINE *lpPolyLine = (const EMRPOLYLINE *)mr;
1312 Polyline(hdc, (const POINT*)lpPolyLine->aptl, (UINT)lpPolyLine->cptl);
1313 break;
1316 case EMR_POLYBEZIERTO:
1318 const EMRPOLYBEZIERTO *lpPolyBezierTo = (const EMRPOLYBEZIERTO *)mr;
1319 PolyBezierTo( hdc, (const POINT*)lpPolyBezierTo->aptl,
1320 (UINT)lpPolyBezierTo->cptl );
1321 break;
1324 case EMR_POLYLINETO:
1326 const EMRPOLYLINETO *lpPolyLineTo = (const EMRPOLYLINETO *)mr;
1327 PolylineTo( hdc, (const POINT*)lpPolyLineTo->aptl,
1328 (UINT)lpPolyLineTo->cptl );
1329 break;
1332 case EMR_POLYPOLYLINE:
1334 const EMRPOLYPOLYLINE *pPolyPolyline = (const EMRPOLYPOLYLINE *)mr;
1335 /* NB Points at pPolyPolyline->aPolyCounts + pPolyPolyline->nPolys */
1337 PolyPolyline(hdc, (const POINT*)(pPolyPolyline->aPolyCounts +
1338 pPolyPolyline->nPolys),
1339 pPolyPolyline->aPolyCounts,
1340 pPolyPolyline->nPolys );
1342 break;
1345 case EMR_POLYPOLYGON:
1347 const EMRPOLYPOLYGON *pPolyPolygon = (const EMRPOLYPOLYGON *)mr;
1349 /* NB Points at pPolyPolygon->aPolyCounts + pPolyPolygon->nPolys */
1351 PolyPolygon(hdc, (const POINT*)(pPolyPolygon->aPolyCounts +
1352 pPolyPolygon->nPolys),
1353 (const INT*)pPolyPolygon->aPolyCounts, pPolyPolygon->nPolys );
1354 break;
1357 case EMR_SETBRUSHORGEX:
1359 const EMRSETBRUSHORGEX *lpSetBrushOrgEx = (const EMRSETBRUSHORGEX *)mr;
1361 SetBrushOrgEx( hdc,
1362 (INT)lpSetBrushOrgEx->ptlOrigin.x,
1363 (INT)lpSetBrushOrgEx->ptlOrigin.y,
1364 NULL );
1366 break;
1369 case EMR_SETPIXELV:
1371 const EMRSETPIXELV *lpSetPixelV = (const EMRSETPIXELV *)mr;
1373 SetPixelV( hdc,
1374 (INT)lpSetPixelV->ptlPixel.x,
1375 (INT)lpSetPixelV->ptlPixel.y,
1376 lpSetPixelV->crColor );
1378 break;
1381 case EMR_SETMAPPERFLAGS:
1383 const EMRSETMAPPERFLAGS *lpSetMapperFlags = (const EMRSETMAPPERFLAGS *)mr;
1385 SetMapperFlags( hdc, lpSetMapperFlags->dwFlags );
1387 break;
1390 case EMR_SETCOLORADJUSTMENT:
1392 const EMRSETCOLORADJUSTMENT *lpSetColorAdjust = (const EMRSETCOLORADJUSTMENT *)mr;
1394 SetColorAdjustment( hdc, &lpSetColorAdjust->ColorAdjustment );
1396 break;
1399 case EMR_OFFSETCLIPRGN:
1401 const EMROFFSETCLIPRGN *lpOffsetClipRgn = (const EMROFFSETCLIPRGN *)mr;
1403 OffsetClipRgn( hdc,
1404 (INT)lpOffsetClipRgn->ptlOffset.x,
1405 (INT)lpOffsetClipRgn->ptlOffset.y );
1406 FIXME("OffsetClipRgn\n");
1408 break;
1411 case EMR_EXCLUDECLIPRECT:
1413 const EMREXCLUDECLIPRECT *lpExcludeClipRect = (const EMREXCLUDECLIPRECT *)mr;
1415 ExcludeClipRect( hdc,
1416 lpExcludeClipRect->rclClip.left,
1417 lpExcludeClipRect->rclClip.top,
1418 lpExcludeClipRect->rclClip.right,
1419 lpExcludeClipRect->rclClip.bottom );
1420 FIXME("ExcludeClipRect\n");
1422 break;
1425 case EMR_SCALEVIEWPORTEXTEX:
1427 const EMRSCALEVIEWPORTEXTEX *lpScaleViewportExtEx = (const EMRSCALEVIEWPORTEXTEX *)mr;
1429 if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1430 break;
1431 if (!lpScaleViewportExtEx->xNum || !lpScaleViewportExtEx->xDenom ||
1432 !lpScaleViewportExtEx->yNum || !lpScaleViewportExtEx->yDenom)
1433 break;
1434 info->state.vportExtX = MulDiv(info->state.vportExtX, lpScaleViewportExtEx->xNum,
1435 lpScaleViewportExtEx->xDenom);
1436 info->state.vportExtY = MulDiv(info->state.vportExtY, lpScaleViewportExtEx->yNum,
1437 lpScaleViewportExtEx->yDenom);
1438 if (info->state.vportExtX == 0) info->state.vportExtX = 1;
1439 if (info->state.vportExtY == 0) info->state.vportExtY = 1;
1440 if (info->state.mode == MM_ISOTROPIC)
1441 EMF_FixIsotropic(hdc, info);
1443 TRACE("EMRSCALEVIEWPORTEXTEX %d/%d %d/%d\n",
1444 lpScaleViewportExtEx->xNum,lpScaleViewportExtEx->xDenom,
1445 lpScaleViewportExtEx->yNum,lpScaleViewportExtEx->yDenom);
1447 if (!IS_WIN9X())
1448 EMF_Update_MF_Xform(hdc, info);
1450 break;
1453 case EMR_SCALEWINDOWEXTEX:
1455 const EMRSCALEWINDOWEXTEX *lpScaleWindowExtEx = (const EMRSCALEWINDOWEXTEX *)mr;
1457 if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1458 break;
1459 if (!lpScaleWindowExtEx->xNum || !lpScaleWindowExtEx->xDenom ||
1460 !lpScaleWindowExtEx->yNum || !lpScaleWindowExtEx->yDenom)
1461 break;
1462 info->state.wndExtX = MulDiv(info->state.wndExtX, lpScaleWindowExtEx->xNum,
1463 lpScaleWindowExtEx->xDenom);
1464 info->state.wndExtY = MulDiv(info->state.wndExtY, lpScaleWindowExtEx->yNum,
1465 lpScaleWindowExtEx->yDenom);
1466 if (info->state.wndExtX == 0) info->state.wndExtX = 1;
1467 if (info->state.wndExtY == 0) info->state.wndExtY = 1;
1468 if (info->state.mode == MM_ISOTROPIC)
1469 EMF_FixIsotropic(hdc, info);
1471 TRACE("EMRSCALEWINDOWEXTEX %d/%d %d/%d\n",
1472 lpScaleWindowExtEx->xNum,lpScaleWindowExtEx->xDenom,
1473 lpScaleWindowExtEx->yNum,lpScaleWindowExtEx->yDenom);
1475 if (!IS_WIN9X())
1476 EMF_Update_MF_Xform(hdc, info);
1478 break;
1481 case EMR_MODIFYWORLDTRANSFORM:
1483 const EMRMODIFYWORLDTRANSFORM *lpModifyWorldTrans = (const EMRMODIFYWORLDTRANSFORM *)mr;
1485 switch(lpModifyWorldTrans->iMode) {
1486 case MWT_IDENTITY:
1487 info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
1488 info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
1489 info->state.world_transform.eDx = info->state.world_transform.eDy = 0;
1490 if (!IS_WIN9X())
1491 EMF_Update_MF_Xform(hdc, info);
1492 break;
1493 case MWT_LEFTMULTIPLY:
1494 CombineTransform(&info->state.world_transform, &lpModifyWorldTrans->xform,
1495 &info->state.world_transform);
1496 if (!IS_WIN9X())
1497 ModifyWorldTransform(hdc, &lpModifyWorldTrans->xform, MWT_LEFTMULTIPLY);
1498 break;
1499 case MWT_RIGHTMULTIPLY:
1500 CombineTransform(&info->state.world_transform, &info->state.world_transform,
1501 &lpModifyWorldTrans->xform);
1502 if (!IS_WIN9X())
1503 EMF_Update_MF_Xform(hdc, info);
1504 break;
1505 default:
1506 FIXME("Unknown imode %d\n", lpModifyWorldTrans->iMode);
1507 break;
1509 break;
1512 case EMR_ANGLEARC:
1514 const EMRANGLEARC *lpAngleArc = (const EMRANGLEARC *)mr;
1516 AngleArc( hdc,
1517 (INT)lpAngleArc->ptlCenter.x, (INT)lpAngleArc->ptlCenter.y,
1518 lpAngleArc->nRadius, lpAngleArc->eStartAngle,
1519 lpAngleArc->eSweepAngle );
1521 break;
1524 case EMR_ROUNDRECT:
1526 const EMRROUNDRECT *lpRoundRect = (const EMRROUNDRECT *)mr;
1528 RoundRect( hdc,
1529 lpRoundRect->rclBox.left,
1530 lpRoundRect->rclBox.top,
1531 lpRoundRect->rclBox.right,
1532 lpRoundRect->rclBox.bottom,
1533 lpRoundRect->szlCorner.cx,
1534 lpRoundRect->szlCorner.cy );
1536 break;
1539 case EMR_ARC:
1541 const EMRARC *lpArc = (const EMRARC *)mr;
1543 Arc( hdc,
1544 (INT)lpArc->rclBox.left,
1545 (INT)lpArc->rclBox.top,
1546 (INT)lpArc->rclBox.right,
1547 (INT)lpArc->rclBox.bottom,
1548 (INT)lpArc->ptlStart.x,
1549 (INT)lpArc->ptlStart.y,
1550 (INT)lpArc->ptlEnd.x,
1551 (INT)lpArc->ptlEnd.y );
1553 break;
1556 case EMR_CHORD:
1558 const EMRCHORD *lpChord = (const EMRCHORD *)mr;
1560 Chord( hdc,
1561 (INT)lpChord->rclBox.left,
1562 (INT)lpChord->rclBox.top,
1563 (INT)lpChord->rclBox.right,
1564 (INT)lpChord->rclBox.bottom,
1565 (INT)lpChord->ptlStart.x,
1566 (INT)lpChord->ptlStart.y,
1567 (INT)lpChord->ptlEnd.x,
1568 (INT)lpChord->ptlEnd.y );
1570 break;
1573 case EMR_PIE:
1575 const EMRPIE *lpPie = (const EMRPIE *)mr;
1577 Pie( hdc,
1578 (INT)lpPie->rclBox.left,
1579 (INT)lpPie->rclBox.top,
1580 (INT)lpPie->rclBox.right,
1581 (INT)lpPie->rclBox.bottom,
1582 (INT)lpPie->ptlStart.x,
1583 (INT)lpPie->ptlStart.y,
1584 (INT)lpPie->ptlEnd.x,
1585 (INT)lpPie->ptlEnd.y );
1587 break;
1590 case EMR_ARCTO:
1592 const EMRARC *lpArcTo = (const EMRARC *)mr;
1594 ArcTo( hdc,
1595 (INT)lpArcTo->rclBox.left,
1596 (INT)lpArcTo->rclBox.top,
1597 (INT)lpArcTo->rclBox.right,
1598 (INT)lpArcTo->rclBox.bottom,
1599 (INT)lpArcTo->ptlStart.x,
1600 (INT)lpArcTo->ptlStart.y,
1601 (INT)lpArcTo->ptlEnd.x,
1602 (INT)lpArcTo->ptlEnd.y );
1604 break;
1607 case EMR_EXTFLOODFILL:
1609 const EMREXTFLOODFILL *lpExtFloodFill = (const EMREXTFLOODFILL *)mr;
1611 ExtFloodFill( hdc,
1612 (INT)lpExtFloodFill->ptlStart.x,
1613 (INT)lpExtFloodFill->ptlStart.y,
1614 lpExtFloodFill->crColor,
1615 (UINT)lpExtFloodFill->iMode );
1617 break;
1620 case EMR_POLYDRAW:
1622 const EMRPOLYDRAW *lpPolyDraw = (const EMRPOLYDRAW *)mr;
1623 PolyDraw( hdc,
1624 (const POINT*)lpPolyDraw->aptl,
1625 lpPolyDraw->abTypes,
1626 (INT)lpPolyDraw->cptl );
1628 break;
1631 case EMR_SETARCDIRECTION:
1633 const EMRSETARCDIRECTION *lpSetArcDirection = (const EMRSETARCDIRECTION *)mr;
1634 SetArcDirection( hdc, (INT)lpSetArcDirection->iArcDirection );
1635 break;
1638 case EMR_SETMITERLIMIT:
1640 const EMRSETMITERLIMIT *lpSetMiterLimit = (const EMRSETMITERLIMIT *)mr;
1641 SetMiterLimit( hdc, lpSetMiterLimit->eMiterLimit, NULL );
1642 break;
1645 case EMR_BEGINPATH:
1647 BeginPath( hdc );
1648 break;
1651 case EMR_ENDPATH:
1653 EndPath( hdc );
1654 break;
1657 case EMR_CLOSEFIGURE:
1659 CloseFigure( hdc );
1660 break;
1663 case EMR_FILLPATH:
1665 /*const EMRFILLPATH lpFillPath = (const EMRFILLPATH *)mr;*/
1666 FillPath( hdc );
1667 break;
1670 case EMR_STROKEANDFILLPATH:
1672 /*const EMRSTROKEANDFILLPATH lpStrokeAndFillPath = (const EMRSTROKEANDFILLPATH *)mr;*/
1673 StrokeAndFillPath( hdc );
1674 break;
1677 case EMR_STROKEPATH:
1679 /*const EMRSTROKEPATH lpStrokePath = (const EMRSTROKEPATH *)mr;*/
1680 StrokePath( hdc );
1681 break;
1684 case EMR_FLATTENPATH:
1686 FlattenPath( hdc );
1687 break;
1690 case EMR_WIDENPATH:
1692 WidenPath( hdc );
1693 break;
1696 case EMR_SELECTCLIPPATH:
1698 const EMRSELECTCLIPPATH *lpSelectClipPath = (const EMRSELECTCLIPPATH *)mr;
1699 SelectClipPath( hdc, (INT)lpSelectClipPath->iMode );
1700 break;
1703 case EMR_ABORTPATH:
1705 AbortPath( hdc );
1706 break;
1709 case EMR_CREATECOLORSPACE:
1711 PEMRCREATECOLORSPACE lpCreateColorSpace = (PEMRCREATECOLORSPACE)mr;
1712 (handletable->objectHandle)[lpCreateColorSpace->ihCS] =
1713 CreateColorSpaceA( &lpCreateColorSpace->lcs );
1714 break;
1717 case EMR_SETCOLORSPACE:
1719 const EMRSETCOLORSPACE *lpSetColorSpace = (const EMRSETCOLORSPACE *)mr;
1720 SetColorSpace( hdc,
1721 (handletable->objectHandle)[lpSetColorSpace->ihCS] );
1722 break;
1725 case EMR_DELETECOLORSPACE:
1727 const EMRDELETECOLORSPACE *lpDeleteColorSpace = (const EMRDELETECOLORSPACE *)mr;
1728 DeleteColorSpace( (handletable->objectHandle)[lpDeleteColorSpace->ihCS] );
1729 break;
1732 case EMR_SETICMMODE:
1734 const EMRSETICMMODE *lpSetICMMode = (const EMRSETICMMODE *)mr;
1735 SetICMMode( hdc, (INT)lpSetICMMode->iMode );
1736 break;
1739 case EMR_PIXELFORMAT:
1741 INT iPixelFormat;
1742 const EMRPIXELFORMAT *lpPixelFormat = (const EMRPIXELFORMAT *)mr;
1744 iPixelFormat = ChoosePixelFormat( hdc, &lpPixelFormat->pfd );
1745 SetPixelFormat( hdc, iPixelFormat, &lpPixelFormat->pfd );
1747 break;
1750 case EMR_SETPALETTEENTRIES:
1752 const EMRSETPALETTEENTRIES *lpSetPaletteEntries = (const EMRSETPALETTEENTRIES *)mr;
1754 SetPaletteEntries( (handletable->objectHandle)[lpSetPaletteEntries->ihPal],
1755 (UINT)lpSetPaletteEntries->iStart,
1756 (UINT)lpSetPaletteEntries->cEntries,
1757 lpSetPaletteEntries->aPalEntries );
1759 break;
1762 case EMR_RESIZEPALETTE:
1764 const EMRRESIZEPALETTE *lpResizePalette = (const EMRRESIZEPALETTE *)mr;
1766 ResizePalette( (handletable->objectHandle)[lpResizePalette->ihPal],
1767 (UINT)lpResizePalette->cEntries );
1769 break;
1772 case EMR_CREATEDIBPATTERNBRUSHPT:
1774 const EMRCREATEDIBPATTERNBRUSHPT *lpCreate = (const EMRCREATEDIBPATTERNBRUSHPT *)mr;
1775 LPVOID lpPackedStruct;
1777 /* Check that offsets and data are contained within the record
1778 * (including checking for wrap-arounds).
1780 if ( lpCreate->offBmi + lpCreate->cbBmi > mr->nSize
1781 || lpCreate->offBits + lpCreate->cbBits > mr->nSize
1782 || lpCreate->offBmi + lpCreate->cbBmi < lpCreate->offBmi
1783 || lpCreate->offBits + lpCreate->cbBits < lpCreate->offBits )
1785 ERR("Invalid EMR_CREATEDIBPATTERNBRUSHPT record\n");
1786 break;
1789 /* This is a BITMAPINFO struct followed directly by bitmap bits */
1790 lpPackedStruct = HeapAlloc( GetProcessHeap(), 0,
1791 lpCreate->cbBmi + lpCreate->cbBits );
1792 if(!lpPackedStruct)
1794 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1795 break;
1798 /* Now pack this structure */
1799 memcpy( lpPackedStruct,
1800 ((const BYTE *)lpCreate) + lpCreate->offBmi,
1801 lpCreate->cbBmi );
1802 memcpy( ((BYTE*)lpPackedStruct) + lpCreate->cbBmi,
1803 ((const BYTE *)lpCreate) + lpCreate->offBits,
1804 lpCreate->cbBits );
1806 (handletable->objectHandle)[lpCreate->ihBrush] =
1807 CreateDIBPatternBrushPt( lpPackedStruct,
1808 (UINT)lpCreate->iUsage );
1810 HeapFree(GetProcessHeap(), 0, lpPackedStruct);
1811 break;
1814 case EMR_CREATEMONOBRUSH:
1816 const EMRCREATEMONOBRUSH *pCreateMonoBrush = (const EMRCREATEMONOBRUSH *)mr;
1817 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pCreateMonoBrush->offBmi);
1818 HBITMAP hBmp;
1820 /* Need to check if the bitmap is monochrome, and if the
1821 two colors are really black and white */
1822 if (pCreateMonoBrush->iUsage == DIB_PAL_MONO)
1824 BITMAP bm;
1826 /* Undocumented iUsage indicates a mono bitmap with no palette table,
1827 * aligned to 32 rather than 16 bits.
1829 bm.bmType = 0;
1830 bm.bmWidth = pbi->bmiHeader.biWidth;
1831 bm.bmHeight = abs(pbi->bmiHeader.biHeight);
1832 bm.bmWidthBytes = 4 * ((pbi->bmiHeader.biWidth + 31) / 32);
1833 bm.bmPlanes = pbi->bmiHeader.biPlanes;
1834 bm.bmBitsPixel = pbi->bmiHeader.biBitCount;
1835 bm.bmBits = (BYTE *)mr + pCreateMonoBrush->offBits;
1836 hBmp = CreateBitmapIndirect(&bm);
1838 else if (is_dib_monochrome(pbi))
1840 /* Top-down DIBs have a negative height */
1841 LONG height = pbi->bmiHeader.biHeight;
1843 hBmp = CreateBitmap(pbi->bmiHeader.biWidth, abs(height), 1, 1, NULL);
1844 SetDIBits(hdc, hBmp, 0, pbi->bmiHeader.biHeight,
1845 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1847 else
1849 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1850 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1853 (handletable->objectHandle)[pCreateMonoBrush->ihBrush] = CreatePatternBrush(hBmp);
1855 /* CreatePatternBrush created a copy of the bitmap */
1856 DeleteObject(hBmp);
1857 break;
1860 case EMR_BITBLT:
1862 const EMRBITBLT *pBitBlt = (const EMRBITBLT *)mr;
1864 if(pBitBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1865 PatBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1866 pBitBlt->dwRop);
1867 } else { /* BitBlt */
1868 HDC hdcSrc = CreateCompatibleDC(hdc);
1869 HBRUSH hBrush, hBrushOld;
1870 HBITMAP hBmp = 0, hBmpOld = 0;
1871 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pBitBlt->offBmiSrc);
1873 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1874 SetWorldTransform(hdcSrc, &pBitBlt->xformSrc);
1876 hBrush = CreateSolidBrush(pBitBlt->crBkColorSrc);
1877 hBrushOld = SelectObject(hdcSrc, hBrush);
1878 PatBlt(hdcSrc, pBitBlt->rclBounds.left, pBitBlt->rclBounds.top,
1879 pBitBlt->rclBounds.right - pBitBlt->rclBounds.left,
1880 pBitBlt->rclBounds.bottom - pBitBlt->rclBounds.top, PATCOPY);
1881 SelectObject(hdcSrc, hBrushOld);
1882 DeleteObject(hBrush);
1884 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1885 (const BYTE *)mr + pBitBlt->offBitsSrc, pbi, pBitBlt->iUsageSrc);
1886 hBmpOld = SelectObject(hdcSrc, hBmp);
1888 BitBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1889 hdcSrc, pBitBlt->xSrc, pBitBlt->ySrc, pBitBlt->dwRop);
1891 SelectObject(hdcSrc, hBmpOld);
1892 DeleteObject(hBmp);
1893 DeleteDC(hdcSrc);
1895 break;
1898 case EMR_STRETCHBLT:
1900 const EMRSTRETCHBLT *pStretchBlt = (const EMRSTRETCHBLT *)mr;
1902 TRACE("EMR_STRETCHBLT: %d, %d %dx%d -> %d, %d %dx%d. rop %08x offBitsSrc %d\n",
1903 pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1904 pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1905 pStretchBlt->dwRop, pStretchBlt->offBitsSrc);
1907 if(pStretchBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1908 PatBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1909 pStretchBlt->dwRop);
1910 } else { /* StretchBlt */
1911 HDC hdcSrc = CreateCompatibleDC(hdc);
1912 HBRUSH hBrush, hBrushOld;
1913 HBITMAP hBmp = 0, hBmpOld = 0;
1914 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pStretchBlt->offBmiSrc);
1916 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1917 SetWorldTransform(hdcSrc, &pStretchBlt->xformSrc);
1919 hBrush = CreateSolidBrush(pStretchBlt->crBkColorSrc);
1920 hBrushOld = SelectObject(hdcSrc, hBrush);
1921 PatBlt(hdcSrc, pStretchBlt->rclBounds.left, pStretchBlt->rclBounds.top,
1922 pStretchBlt->rclBounds.right - pStretchBlt->rclBounds.left,
1923 pStretchBlt->rclBounds.bottom - pStretchBlt->rclBounds.top, PATCOPY);
1924 SelectObject(hdcSrc, hBrushOld);
1925 DeleteObject(hBrush);
1927 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1928 (const BYTE *)mr + pStretchBlt->offBitsSrc, pbi, pStretchBlt->iUsageSrc);
1929 hBmpOld = SelectObject(hdcSrc, hBmp);
1931 StretchBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1932 hdcSrc, pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1933 pStretchBlt->dwRop);
1935 SelectObject(hdcSrc, hBmpOld);
1936 DeleteObject(hBmp);
1937 DeleteDC(hdcSrc);
1939 break;
1942 case EMR_ALPHABLEND:
1944 const EMRALPHABLEND *pAlphaBlend = (const EMRALPHABLEND *)mr;
1946 TRACE("EMR_ALPHABLEND: %d, %d %dx%d -> %d, %d %dx%d. blendfn %08x offBitsSrc %d\n",
1947 pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1948 pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1949 pAlphaBlend->dwRop, pAlphaBlend->offBitsSrc);
1951 if(pAlphaBlend->offBmiSrc == 0) {
1952 FIXME("EMR_ALPHABLEND: offBmiSrc == 0\n");
1953 } else {
1954 HDC hdcSrc = CreateCompatibleDC(hdc);
1955 HBITMAP hBmp = 0, hBmpOld = 0;
1956 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pAlphaBlend->offBmiSrc);
1957 void *bits;
1959 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1960 SetWorldTransform(hdcSrc, &pAlphaBlend->xformSrc);
1962 hBmp = CreateDIBSection(hdc, pbi, pAlphaBlend->iUsageSrc, &bits, NULL, 0);
1963 memcpy(bits, (const BYTE *)mr + pAlphaBlend->offBitsSrc, pAlphaBlend->cbBitsSrc);
1964 hBmpOld = SelectObject(hdcSrc, hBmp);
1966 GdiAlphaBlend(hdc, pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1967 hdcSrc, pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1968 *(BLENDFUNCTION *)&pAlphaBlend->dwRop);
1970 SelectObject(hdcSrc, hBmpOld);
1971 DeleteObject(hBmp);
1972 DeleteDC(hdcSrc);
1974 break;
1977 case EMR_MASKBLT:
1979 const EMRMASKBLT *pMaskBlt = (const EMRMASKBLT *)mr;
1980 HDC hdcSrc = CreateCompatibleDC(hdc);
1981 HBRUSH hBrush, hBrushOld;
1982 HBITMAP hBmp, hBmpOld, hBmpMask;
1983 const BITMAPINFO *pbi;
1985 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1986 SetWorldTransform(hdcSrc, &pMaskBlt->xformSrc);
1988 hBrush = CreateSolidBrush(pMaskBlt->crBkColorSrc);
1989 hBrushOld = SelectObject(hdcSrc, hBrush);
1990 PatBlt(hdcSrc, pMaskBlt->rclBounds.left, pMaskBlt->rclBounds.top,
1991 pMaskBlt->rclBounds.right - pMaskBlt->rclBounds.left,
1992 pMaskBlt->rclBounds.bottom - pMaskBlt->rclBounds.top, PATCOPY);
1993 SelectObject(hdcSrc, hBrushOld);
1994 DeleteObject(hBrush);
1996 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiMask);
1997 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
1998 1, 1, NULL);
1999 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
2000 (const BYTE *)mr + pMaskBlt->offBitsMask, pbi, pMaskBlt->iUsageMask);
2002 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiSrc);
2003 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
2004 (const BYTE *)mr + pMaskBlt->offBitsSrc, pbi, pMaskBlt->iUsageSrc);
2005 hBmpOld = SelectObject(hdcSrc, hBmp);
2006 MaskBlt(hdc,
2007 pMaskBlt->xDest,
2008 pMaskBlt->yDest,
2009 pMaskBlt->cxDest,
2010 pMaskBlt->cyDest,
2011 hdcSrc,
2012 pMaskBlt->xSrc,
2013 pMaskBlt->ySrc,
2014 hBmpMask,
2015 pMaskBlt->xMask,
2016 pMaskBlt->yMask,
2017 pMaskBlt->dwRop);
2018 SelectObject(hdcSrc, hBmpOld);
2019 DeleteObject(hBmp);
2020 DeleteObject(hBmpMask);
2021 DeleteDC(hdcSrc);
2022 break;
2025 case EMR_PLGBLT:
2027 const EMRPLGBLT *pPlgBlt = (const EMRPLGBLT *)mr;
2028 HDC hdcSrc = CreateCompatibleDC(hdc);
2029 HBRUSH hBrush, hBrushOld;
2030 HBITMAP hBmp, hBmpOld, hBmpMask;
2031 const BITMAPINFO *pbi;
2032 POINT pts[3];
2034 SetGraphicsMode(hdcSrc, GM_ADVANCED);
2035 SetWorldTransform(hdcSrc, &pPlgBlt->xformSrc);
2037 pts[0].x = pPlgBlt->aptlDest[0].x; pts[0].y = pPlgBlt->aptlDest[0].y;
2038 pts[1].x = pPlgBlt->aptlDest[1].x; pts[1].y = pPlgBlt->aptlDest[1].y;
2039 pts[2].x = pPlgBlt->aptlDest[2].x; pts[2].y = pPlgBlt->aptlDest[2].y;
2041 hBrush = CreateSolidBrush(pPlgBlt->crBkColorSrc);
2042 hBrushOld = SelectObject(hdcSrc, hBrush);
2043 PatBlt(hdcSrc, pPlgBlt->rclBounds.left, pPlgBlt->rclBounds.top,
2044 pPlgBlt->rclBounds.right - pPlgBlt->rclBounds.left,
2045 pPlgBlt->rclBounds.bottom - pPlgBlt->rclBounds.top, PATCOPY);
2046 SelectObject(hdcSrc, hBrushOld);
2047 DeleteObject(hBrush);
2049 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiMask);
2050 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
2051 1, 1, NULL);
2052 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
2053 (const BYTE *)mr + pPlgBlt->offBitsMask, pbi, pPlgBlt->iUsageMask);
2055 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiSrc);
2056 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
2057 (const BYTE *)mr + pPlgBlt->offBitsSrc, pbi, pPlgBlt->iUsageSrc);
2058 hBmpOld = SelectObject(hdcSrc, hBmp);
2059 PlgBlt(hdc,
2060 pts,
2061 hdcSrc,
2062 pPlgBlt->xSrc,
2063 pPlgBlt->ySrc,
2064 pPlgBlt->cxSrc,
2065 pPlgBlt->cySrc,
2066 hBmpMask,
2067 pPlgBlt->xMask,
2068 pPlgBlt->yMask);
2069 SelectObject(hdcSrc, hBmpOld);
2070 DeleteObject(hBmp);
2071 DeleteObject(hBmpMask);
2072 DeleteDC(hdcSrc);
2073 break;
2076 case EMR_SETDIBITSTODEVICE:
2078 const EMRSETDIBITSTODEVICE *pSetDIBitsToDevice = (const EMRSETDIBITSTODEVICE *)mr;
2080 SetDIBitsToDevice(hdc,
2081 pSetDIBitsToDevice->xDest,
2082 pSetDIBitsToDevice->yDest,
2083 pSetDIBitsToDevice->cxSrc,
2084 pSetDIBitsToDevice->cySrc,
2085 pSetDIBitsToDevice->xSrc,
2086 pSetDIBitsToDevice->ySrc,
2087 pSetDIBitsToDevice->iStartScan,
2088 pSetDIBitsToDevice->cScans,
2089 (const BYTE *)mr + pSetDIBitsToDevice->offBitsSrc,
2090 (const BITMAPINFO *)((const BYTE *)mr + pSetDIBitsToDevice->offBmiSrc),
2091 pSetDIBitsToDevice->iUsageSrc);
2092 break;
2095 case EMR_POLYTEXTOUTA:
2097 const EMRPOLYTEXTOUTA *pPolyTextOutA = (const EMRPOLYTEXTOUTA *)mr;
2098 POLYTEXTA *polytextA = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutA->cStrings * sizeof(POLYTEXTA));
2099 LONG i;
2100 XFORM xform, xformOld;
2101 int gModeOld;
2103 gModeOld = SetGraphicsMode(hdc, pPolyTextOutA->iGraphicsMode);
2104 GetWorldTransform(hdc, &xformOld);
2106 xform.eM11 = pPolyTextOutA->exScale;
2107 xform.eM12 = 0.0;
2108 xform.eM21 = 0.0;
2109 xform.eM22 = pPolyTextOutA->eyScale;
2110 xform.eDx = 0.0;
2111 xform.eDy = 0.0;
2112 SetWorldTransform(hdc, &xform);
2114 /* Set up POLYTEXTA structures */
2115 for(i = 0; i < pPolyTextOutA->cStrings; i++)
2117 polytextA[i].x = pPolyTextOutA->aemrtext[i].ptlReference.x;
2118 polytextA[i].y = pPolyTextOutA->aemrtext[i].ptlReference.y;
2119 polytextA[i].n = pPolyTextOutA->aemrtext[i].nChars;
2120 polytextA[i].lpstr = (LPCSTR)((const BYTE *)mr + pPolyTextOutA->aemrtext[i].offString);
2121 polytextA[i].uiFlags = pPolyTextOutA->aemrtext[i].fOptions;
2122 polytextA[i].rcl.left = pPolyTextOutA->aemrtext[i].rcl.left;
2123 polytextA[i].rcl.right = pPolyTextOutA->aemrtext[i].rcl.right;
2124 polytextA[i].rcl.top = pPolyTextOutA->aemrtext[i].rcl.top;
2125 polytextA[i].rcl.bottom = pPolyTextOutA->aemrtext[i].rcl.bottom;
2126 polytextA[i].pdx = (int *)((BYTE *)mr + pPolyTextOutA->aemrtext[i].offDx);
2128 PolyTextOutA(hdc, polytextA, pPolyTextOutA->cStrings);
2129 HeapFree(GetProcessHeap(), 0, polytextA);
2131 SetWorldTransform(hdc, &xformOld);
2132 SetGraphicsMode(hdc, gModeOld);
2133 break;
2136 case EMR_POLYTEXTOUTW:
2138 const EMRPOLYTEXTOUTW *pPolyTextOutW = (const EMRPOLYTEXTOUTW *)mr;
2139 POLYTEXTW *polytextW = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutW->cStrings * sizeof(POLYTEXTW));
2140 LONG i;
2141 XFORM xform, xformOld;
2142 int gModeOld;
2144 gModeOld = SetGraphicsMode(hdc, pPolyTextOutW->iGraphicsMode);
2145 GetWorldTransform(hdc, &xformOld);
2147 xform.eM11 = pPolyTextOutW->exScale;
2148 xform.eM12 = 0.0;
2149 xform.eM21 = 0.0;
2150 xform.eM22 = pPolyTextOutW->eyScale;
2151 xform.eDx = 0.0;
2152 xform.eDy = 0.0;
2153 SetWorldTransform(hdc, &xform);
2155 /* Set up POLYTEXTW structures */
2156 for(i = 0; i < pPolyTextOutW->cStrings; i++)
2158 polytextW[i].x = pPolyTextOutW->aemrtext[i].ptlReference.x;
2159 polytextW[i].y = pPolyTextOutW->aemrtext[i].ptlReference.y;
2160 polytextW[i].n = pPolyTextOutW->aemrtext[i].nChars;
2161 polytextW[i].lpstr = (LPCWSTR)((const BYTE *)mr + pPolyTextOutW->aemrtext[i].offString);
2162 polytextW[i].uiFlags = pPolyTextOutW->aemrtext[i].fOptions;
2163 polytextW[i].rcl.left = pPolyTextOutW->aemrtext[i].rcl.left;
2164 polytextW[i].rcl.right = pPolyTextOutW->aemrtext[i].rcl.right;
2165 polytextW[i].rcl.top = pPolyTextOutW->aemrtext[i].rcl.top;
2166 polytextW[i].rcl.bottom = pPolyTextOutW->aemrtext[i].rcl.bottom;
2167 polytextW[i].pdx = (int *)((BYTE *)mr + pPolyTextOutW->aemrtext[i].offDx);
2169 PolyTextOutW(hdc, polytextW, pPolyTextOutW->cStrings);
2170 HeapFree(GetProcessHeap(), 0, polytextW);
2172 SetWorldTransform(hdc, &xformOld);
2173 SetGraphicsMode(hdc, gModeOld);
2174 break;
2177 case EMR_FILLRGN:
2179 const EMRFILLRGN *pFillRgn = (const EMRFILLRGN *)mr;
2180 HRGN hRgn = ExtCreateRegion(NULL, pFillRgn->cbRgnData, (const RGNDATA *)pFillRgn->RgnData);
2181 FillRgn(hdc,
2182 hRgn,
2183 (handletable->objectHandle)[pFillRgn->ihBrush]);
2184 DeleteObject(hRgn);
2185 break;
2188 case EMR_FRAMERGN:
2190 const EMRFRAMERGN *pFrameRgn = (const EMRFRAMERGN *)mr;
2191 HRGN hRgn = ExtCreateRegion(NULL, pFrameRgn->cbRgnData, (const RGNDATA *)pFrameRgn->RgnData);
2192 FrameRgn(hdc,
2193 hRgn,
2194 (handletable->objectHandle)[pFrameRgn->ihBrush],
2195 pFrameRgn->szlStroke.cx,
2196 pFrameRgn->szlStroke.cy);
2197 DeleteObject(hRgn);
2198 break;
2201 case EMR_INVERTRGN:
2203 const EMRINVERTRGN *pInvertRgn = (const EMRINVERTRGN *)mr;
2204 HRGN hRgn = ExtCreateRegion(NULL, pInvertRgn->cbRgnData, (const RGNDATA *)pInvertRgn->RgnData);
2205 InvertRgn(hdc, hRgn);
2206 DeleteObject(hRgn);
2207 break;
2210 case EMR_PAINTRGN:
2212 const EMRPAINTRGN *pPaintRgn = (const EMRPAINTRGN *)mr;
2213 HRGN hRgn = ExtCreateRegion(NULL, pPaintRgn->cbRgnData, (const RGNDATA *)pPaintRgn->RgnData);
2214 PaintRgn(hdc, hRgn);
2215 DeleteObject(hRgn);
2216 break;
2219 case EMR_SETTEXTJUSTIFICATION:
2221 const EMRSETTEXTJUSTIFICATION *pSetTextJust = (const EMRSETTEXTJUSTIFICATION *)mr;
2222 SetTextJustification(hdc, pSetTextJust->nBreakExtra, pSetTextJust->nBreakCount);
2223 break;
2226 case EMR_SETLAYOUT:
2228 const EMRSETLAYOUT *pSetLayout = (const EMRSETLAYOUT *)mr;
2229 SetLayout(hdc, pSetLayout->iMode);
2230 break;
2233 case EMR_GRADIENTFILL:
2235 EMRGRADIENTFILL *grad = (EMRGRADIENTFILL *)mr;
2236 GdiGradientFill( hdc, grad->Ver, grad->nVer, grad->Ver + grad->nVer,
2237 grad->nTri, grad->ulMode );
2238 break;
2241 case EMR_POLYDRAW16:
2242 case EMR_GLSRECORD:
2243 case EMR_GLSBOUNDEDRECORD:
2244 case EMR_DRAWESCAPE:
2245 case EMR_EXTESCAPE:
2246 case EMR_STARTDOC:
2247 case EMR_SMALLTEXTOUT:
2248 case EMR_FORCEUFIMAPPING:
2249 case EMR_NAMEDESCAPE:
2250 case EMR_COLORCORRECTPALETTE:
2251 case EMR_SETICMPROFILEA:
2252 case EMR_SETICMPROFILEW:
2253 case EMR_TRANSPARENTBLT:
2254 case EMR_SETLINKEDUFI:
2255 case EMR_COLORMATCHTOTARGETW:
2256 case EMR_CREATECOLORSPACEW:
2258 default:
2259 /* From docs: If PlayEnhMetaFileRecord doesn't recognize a
2260 record then ignore and return TRUE. */
2261 FIXME("type %d is unimplemented\n", type);
2262 break;
2264 tmprc.left = tmprc.top = 0;
2265 tmprc.right = tmprc.bottom = 1000;
2266 LPtoDP(hdc, (POINT*)&tmprc, 2);
2267 TRACE("L:0,0 - 1000,1000 -> D:%s\n", wine_dbgstr_rect(&tmprc));
2269 return TRUE;
2273 /*****************************************************************************
2275 * EnumEnhMetaFile (GDI32.@)
2277 * Walk an enhanced metafile, calling a user-specified function _EnhMetaFunc_
2278 * for each
2279 * record. Returns when either every record has been used or
2280 * when _EnhMetaFunc_ returns FALSE.
2283 * RETURNS
2284 * TRUE if every record is used, FALSE if any invocation of _EnhMetaFunc_
2285 * returns FALSE.
2287 * BUGS
2288 * Ignores rect.
2290 * NOTES
2291 * This function behaves differently in Win9x and WinNT.
2293 * In WinNT, the DC's world transform is updated as the EMF changes
2294 * the Window/Viewport Extent and Origin or its world transform.
2295 * The actual Window/Viewport Extent and Origin are left untouched.
2297 * In Win9x, the DC is left untouched, and PlayEnhMetaFileRecord
2298 * updates the scaling itself but only just before a record that
2299 * writes anything to the DC.
2301 * I'm not sure where the data (enum_emh_data) is stored in either
2302 * version. For this implementation, it is stored before the handle
2303 * table, but it could be stored in the DC, in the EMF handle or in
2304 * TLS.
2305 * MJM 5 Oct 2002
2307 BOOL WINAPI EnumEnhMetaFile(
2308 HDC hdc, /* [in] device context to pass to _EnhMetaFunc_ */
2309 HENHMETAFILE hmf, /* [in] EMF to walk */
2310 ENHMFENUMPROC callback, /* [in] callback function */
2311 LPVOID data, /* [in] optional data for callback function */
2312 const RECT *lpRect /* [in] bounding rectangle for rendered metafile */
2315 BOOL ret;
2316 ENHMETAHEADER *emh;
2317 ENHMETARECORD *emr;
2318 DWORD offset;
2319 UINT i;
2320 HANDLETABLE *ht;
2321 INT savedMode = 0;
2322 XFORM savedXform;
2323 HPEN hPen = NULL;
2324 HBRUSH hBrush = NULL;
2325 HFONT hFont = NULL;
2326 HRGN hRgn = NULL;
2327 enum_emh_data *info;
2328 SIZE vp_size, win_size;
2329 POINT vp_org, win_org;
2330 INT mapMode = MM_TEXT, old_align = 0, old_rop2 = 0, old_arcdir = 0, old_polyfill = 0, old_stretchblt = 0;
2331 COLORREF old_text_color = 0, old_bk_color = 0;
2333 if(!lpRect && hdc)
2335 SetLastError(ERROR_INVALID_PARAMETER);
2336 return FALSE;
2339 emh = EMF_GetEnhMetaHeader(hmf);
2340 if(!emh) {
2341 SetLastError(ERROR_INVALID_HANDLE);
2342 return FALSE;
2345 info = HeapAlloc( GetProcessHeap(), 0,
2346 sizeof (enum_emh_data) + sizeof(HANDLETABLE) * emh->nHandles );
2347 if(!info)
2349 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2350 return FALSE;
2352 info->state.mode = MM_TEXT;
2353 info->state.wndOrgX = 0;
2354 info->state.wndOrgY = 0;
2355 info->state.wndExtX = 1;
2356 info->state.wndExtY = 1;
2357 info->state.vportOrgX = 0;
2358 info->state.vportOrgY = 0;
2359 info->state.vportExtX = 1;
2360 info->state.vportExtY = 1;
2361 info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
2362 info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
2363 info->state.world_transform.eDx = info->state.world_transform.eDy = 0;
2365 info->state.next = NULL;
2366 info->save_level = 0;
2367 info->saved_state = NULL;
2369 ht = (HANDLETABLE*) &info[1];
2370 ht->objectHandle[0] = hmf;
2371 for(i = 1; i < emh->nHandles; i++)
2372 ht->objectHandle[i] = NULL;
2374 if(hdc)
2376 savedMode = SetGraphicsMode(hdc, GM_ADVANCED);
2377 GetWorldTransform(hdc, &savedXform);
2378 GetViewportExtEx(hdc, &vp_size);
2379 GetWindowExtEx(hdc, &win_size);
2380 GetViewportOrgEx(hdc, &vp_org);
2381 GetWindowOrgEx(hdc, &win_org);
2382 mapMode = GetMapMode(hdc);
2384 /* save DC */
2385 hPen = GetCurrentObject(hdc, OBJ_PEN);
2386 hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
2387 hFont = GetCurrentObject(hdc, OBJ_FONT);
2389 hRgn = CreateRectRgn(0, 0, 0, 0);
2390 if (!GetClipRgn(hdc, hRgn))
2392 DeleteObject(hRgn);
2393 hRgn = 0;
2396 old_text_color = SetTextColor(hdc, RGB(0,0,0));
2397 old_bk_color = SetBkColor(hdc, RGB(0xff, 0xff, 0xff));
2398 old_align = SetTextAlign(hdc, 0);
2399 old_rop2 = SetROP2(hdc, R2_COPYPEN);
2400 old_arcdir = SetArcDirection(hdc, AD_COUNTERCLOCKWISE);
2401 old_polyfill = SetPolyFillMode(hdc, ALTERNATE);
2402 old_stretchblt = SetStretchBltMode(hdc, BLACKONWHITE);
2404 if ( IS_WIN9X() )
2406 /* Win95 leaves the vp/win ext/org info alone */
2407 info->init_transform.eM11 = 1.0;
2408 info->init_transform.eM12 = 0.0;
2409 info->init_transform.eM21 = 0.0;
2410 info->init_transform.eM22 = 1.0;
2411 info->init_transform.eDx = 0.0;
2412 info->init_transform.eDy = 0.0;
2414 else
2416 /* WinNT combines the vp/win ext/org info into a transform */
2417 double xscale, yscale;
2418 xscale = (double)vp_size.cx / (double)win_size.cx;
2419 yscale = (double)vp_size.cy / (double)win_size.cy;
2420 info->init_transform.eM11 = xscale;
2421 info->init_transform.eM12 = 0.0;
2422 info->init_transform.eM21 = 0.0;
2423 info->init_transform.eM22 = yscale;
2424 info->init_transform.eDx = (double)vp_org.x - xscale * (double)win_org.x;
2425 info->init_transform.eDy = (double)vp_org.y - yscale * (double)win_org.y;
2427 CombineTransform(&info->init_transform, &savedXform, &info->init_transform);
2430 if ( lpRect && WIDTH(emh->rclFrame) && HEIGHT(emh->rclFrame) )
2432 double xSrcPixSize, ySrcPixSize, xscale, yscale;
2433 XFORM xform;
2435 TRACE("rect: %s. rclFrame: (%d,%d)-(%d,%d)\n", wine_dbgstr_rect(lpRect),
2436 emh->rclFrame.left, emh->rclFrame.top, emh->rclFrame.right,
2437 emh->rclFrame.bottom);
2439 xSrcPixSize = (double) emh->szlMillimeters.cx / emh->szlDevice.cx;
2440 ySrcPixSize = (double) emh->szlMillimeters.cy / emh->szlDevice.cy;
2441 xscale = (double) WIDTH(*lpRect) * 100.0 /
2442 WIDTH(emh->rclFrame) * xSrcPixSize;
2443 yscale = (double) HEIGHT(*lpRect) * 100.0 /
2444 HEIGHT(emh->rclFrame) * ySrcPixSize;
2445 TRACE("xscale = %f, yscale = %f\n", xscale, yscale);
2447 xform.eM11 = xscale;
2448 xform.eM12 = 0;
2449 xform.eM21 = 0;
2450 xform.eM22 = yscale;
2451 xform.eDx = (double) lpRect->left - (double) WIDTH(*lpRect) / WIDTH(emh->rclFrame) * emh->rclFrame.left;
2452 xform.eDy = (double) lpRect->top - (double) HEIGHT(*lpRect) / HEIGHT(emh->rclFrame) * emh->rclFrame.top;
2454 CombineTransform(&info->init_transform, &xform, &info->init_transform);
2457 /* WinNT resets the current vp/win org/ext */
2458 if ( !IS_WIN9X() )
2460 SetMapMode(hdc, MM_TEXT);
2461 SetWindowOrgEx(hdc, 0, 0, NULL);
2462 SetViewportOrgEx(hdc, 0, 0, NULL);
2463 EMF_Update_MF_Xform(hdc, info);
2467 ret = TRUE;
2468 offset = 0;
2469 while(ret && offset < emh->nBytes)
2471 emr = (ENHMETARECORD *)((char *)emh + offset);
2473 if (offset + 8 > emh->nBytes ||
2474 offset > offset + emr->nSize ||
2475 offset + emr->nSize > emh->nBytes)
2477 WARN("record truncated\n");
2478 break;
2481 /* In Win9x mode we update the xform if the record will produce output */
2482 if (hdc && IS_WIN9X() && emr_produces_output(emr->iType))
2483 EMF_Update_MF_Xform(hdc, info);
2485 TRACE("Calling EnumFunc with record %s, size %d\n", get_emr_name(emr->iType), emr->nSize);
2486 ret = (*callback)(hdc, ht, emr, emh->nHandles, (LPARAM)data);
2487 offset += emr->nSize;
2490 if (hdc)
2492 SetStretchBltMode(hdc, old_stretchblt);
2493 SetPolyFillMode(hdc, old_polyfill);
2494 SetArcDirection(hdc, old_arcdir);
2495 SetROP2(hdc, old_rop2);
2496 SetTextAlign(hdc, old_align);
2497 SetBkColor(hdc, old_bk_color);
2498 SetTextColor(hdc, old_text_color);
2500 /* restore DC */
2501 SelectObject(hdc, hBrush);
2502 SelectObject(hdc, hPen);
2503 SelectObject(hdc, hFont);
2504 ExtSelectClipRgn(hdc, hRgn, RGN_COPY);
2505 DeleteObject(hRgn);
2507 SetWorldTransform(hdc, &savedXform);
2508 if (savedMode)
2509 SetGraphicsMode(hdc, savedMode);
2510 SetMapMode(hdc, mapMode);
2511 SetWindowOrgEx(hdc, win_org.x, win_org.y, NULL);
2512 SetWindowExtEx(hdc, win_size.cx, win_size.cy, NULL);
2513 SetViewportOrgEx(hdc, vp_org.x, vp_org.y, NULL);
2514 SetViewportExtEx(hdc, vp_size.cx, vp_size.cy, NULL);
2517 for(i = 1; i < emh->nHandles; i++) /* Don't delete element 0 (hmf) */
2518 if( (ht->objectHandle)[i] )
2519 DeleteObject( (ht->objectHandle)[i] );
2521 while (info->saved_state)
2523 EMF_dc_state *state = info->saved_state;
2524 info->saved_state = info->saved_state->next;
2525 HeapFree( GetProcessHeap(), 0, state );
2527 HeapFree( GetProcessHeap(), 0, info );
2528 return ret;
2531 static INT CALLBACK EMF_PlayEnhMetaFileCallback(HDC hdc, HANDLETABLE *ht,
2532 const ENHMETARECORD *emr,
2533 INT handles, LPARAM data)
2535 return PlayEnhMetaFileRecord(hdc, ht, emr, handles);
2538 /**************************************************************************
2539 * PlayEnhMetaFile (GDI32.@)
2541 * Renders an enhanced metafile into a specified rectangle *lpRect
2542 * in device context hdc.
2544 * RETURNS
2545 * Success: TRUE
2546 * Failure: FALSE
2548 BOOL WINAPI PlayEnhMetaFile(
2549 HDC hdc, /* [in] DC to render into */
2550 HENHMETAFILE hmf, /* [in] metafile to render */
2551 const RECT *lpRect /* [in] rectangle to place metafile inside */
2554 return EnumEnhMetaFile(hdc, hmf, EMF_PlayEnhMetaFileCallback, NULL,
2555 lpRect);
2558 /*****************************************************************************
2559 * DeleteEnhMetaFile (GDI32.@)
2561 * Deletes an enhanced metafile and frees the associated storage.
2563 BOOL WINAPI DeleteEnhMetaFile(HENHMETAFILE hmf)
2565 return EMF_Delete_HENHMETAFILE( hmf );
2568 /*****************************************************************************
2569 * CopyEnhMetaFileA (GDI32.@)
2571 * Duplicate an enhanced metafile.
2575 HENHMETAFILE WINAPI CopyEnhMetaFileA(
2576 HENHMETAFILE hmfSrc,
2577 LPCSTR file)
2579 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2580 HENHMETAFILE hmfDst;
2582 if(!emrSrc) return FALSE;
2583 if (!file) {
2584 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2585 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2586 hmfDst = EMF_Create_HENHMETAFILE( emrDst, emrSrc->nBytes, FALSE );
2587 if (!hmfDst)
2588 HeapFree( GetProcessHeap(), 0, emrDst );
2589 } else {
2590 HANDLE hFile;
2591 DWORD w;
2592 hFile = CreateFileA( file, GENERIC_WRITE | GENERIC_READ, 0,
2593 NULL, CREATE_ALWAYS, 0, 0);
2594 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2595 CloseHandle( hFile );
2596 /* Reopen file for reading only, so that apps can share
2597 read access to the file while hmf is still valid */
2598 hFile = CreateFileA( file, GENERIC_READ, FILE_SHARE_READ,
2599 NULL, OPEN_EXISTING, 0, 0);
2600 if(hFile == INVALID_HANDLE_VALUE) {
2601 ERR("Can't reopen emf for reading\n");
2602 return 0;
2604 hmfDst = EMF_GetEnhMetaFile( hFile );
2605 CloseHandle( hFile );
2607 return hmfDst;
2610 /*****************************************************************************
2611 * CopyEnhMetaFileW (GDI32.@)
2613 * See CopyEnhMetaFileA.
2617 HENHMETAFILE WINAPI CopyEnhMetaFileW(
2618 HENHMETAFILE hmfSrc,
2619 LPCWSTR file)
2621 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2622 HENHMETAFILE hmfDst;
2624 if(!emrSrc) return FALSE;
2625 if (!file) {
2626 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2627 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2628 hmfDst = EMF_Create_HENHMETAFILE( emrDst, emrSrc->nBytes, FALSE );
2629 if (!hmfDst)
2630 HeapFree( GetProcessHeap(), 0, emrDst );
2631 } else {
2632 HANDLE hFile;
2633 DWORD w;
2634 hFile = CreateFileW( file, GENERIC_WRITE | GENERIC_READ, 0,
2635 NULL, CREATE_ALWAYS, 0, 0);
2636 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2637 CloseHandle( hFile );
2638 /* Reopen file for reading only, so that apps can share
2639 read access to the file while hmf is still valid */
2640 hFile = CreateFileW( file, GENERIC_READ, FILE_SHARE_READ,
2641 NULL, OPEN_EXISTING, 0, 0);
2642 if(hFile == INVALID_HANDLE_VALUE) {
2643 ERR("Can't reopen emf for reading\n");
2644 return 0;
2646 hmfDst = EMF_GetEnhMetaFile( hFile );
2647 CloseHandle( hFile );
2649 return hmfDst;
2653 /* Struct to be used to be passed in the LPVOID parameter for cbEnhPaletteCopy */
2654 typedef struct tagEMF_PaletteCopy
2656 UINT cEntries;
2657 LPPALETTEENTRY lpPe;
2658 } EMF_PaletteCopy;
2660 /***************************************************************
2661 * Find the EMR_EOF record and then use it to find the
2662 * palette entries for this enhanced metafile.
2663 * The lpData is actually a pointer to an EMF_PaletteCopy struct
2664 * which contains the max number of elements to copy and where
2665 * to copy them to.
2667 * NOTE: To be used by GetEnhMetaFilePaletteEntries only!
2669 static INT CALLBACK cbEnhPaletteCopy( HDC a,
2670 HANDLETABLE *b,
2671 const ENHMETARECORD *lpEMR,
2672 INT c,
2673 LPARAM lpData )
2676 if ( lpEMR->iType == EMR_EOF )
2678 const EMREOF *lpEof = (const EMREOF *)lpEMR;
2679 EMF_PaletteCopy* info = (EMF_PaletteCopy*)lpData;
2680 DWORD dwNumPalToCopy = min( lpEof->nPalEntries, info->cEntries );
2682 TRACE( "copying 0x%08x palettes\n", dwNumPalToCopy );
2684 memcpy( info->lpPe, (LPCSTR)lpEof + lpEof->offPalEntries,
2685 sizeof( *(info->lpPe) ) * dwNumPalToCopy );
2687 /* Update the passed data as a return code */
2688 info->lpPe = NULL; /* Palettes were copied! */
2689 info->cEntries = dwNumPalToCopy;
2691 return FALSE; /* That's all we need */
2694 return TRUE;
2697 /*****************************************************************************
2698 * GetEnhMetaFilePaletteEntries (GDI32.@)
2700 * Copy the palette and report size
2702 * BUGS: Error codes (SetLastError) are not set on failures
2704 UINT WINAPI GetEnhMetaFilePaletteEntries( HENHMETAFILE hEmf,
2705 UINT cEntries,
2706 LPPALETTEENTRY lpPe )
2708 ENHMETAHEADER* enhHeader = EMF_GetEnhMetaHeader( hEmf );
2709 EMF_PaletteCopy infoForCallBack;
2711 TRACE( "(%p,%d,%p)\n", hEmf, cEntries, lpPe );
2713 if (!enhHeader) return 0;
2715 /* First check if there are any palettes associated with
2716 this metafile. */
2717 if ( enhHeader->nPalEntries == 0 ) return 0;
2719 /* Is the user requesting the number of palettes? */
2720 if ( lpPe == NULL ) return enhHeader->nPalEntries;
2722 /* Copy cEntries worth of PALETTEENTRY structs into the buffer */
2723 infoForCallBack.cEntries = cEntries;
2724 infoForCallBack.lpPe = lpPe;
2726 if ( !EnumEnhMetaFile( 0, hEmf, cbEnhPaletteCopy,
2727 &infoForCallBack, 0 ) )
2728 return GDI_ERROR;
2730 /* Verify that the callback executed correctly */
2731 if ( infoForCallBack.lpPe != NULL )
2733 /* Callback proc had error! */
2734 ERR( "cbEnhPaletteCopy didn't execute correctly\n" );
2735 return GDI_ERROR;
2738 return infoForCallBack.cEntries;
2741 /******************************************************************
2742 * extract_emf_from_comment
2744 * If the WMF was created by GetWinMetaFileBits, then extract the
2745 * original EMF that is stored in MFCOMMENT chunks.
2747 static HENHMETAFILE extract_emf_from_comment( const BYTE *buf, UINT mf_size )
2749 METAHEADER *mh = (METAHEADER *)buf;
2750 METARECORD *mr;
2751 emf_in_wmf_comment *chunk;
2752 WORD checksum = 0;
2753 DWORD size = 0, remaining, chunks;
2754 BYTE *emf_bits = NULL, *ptr;
2755 UINT offset;
2756 HENHMETAFILE emf = NULL;
2758 if (mf_size < sizeof(*mh)) return NULL;
2760 for (offset = mh->mtHeaderSize * 2; offset < mf_size; offset += (mr->rdSize * 2))
2762 mr = (METARECORD *)((char *)mh + offset);
2763 chunk = (emf_in_wmf_comment *)(mr->rdParm + 2);
2765 if (mr->rdFunction != META_ESCAPE || mr->rdParm[0] != MFCOMMENT) goto done;
2766 if (chunk->magic != WMFC_MAGIC) goto done;
2768 if (!emf_bits)
2770 size = remaining = chunk->emf_size;
2771 chunks = chunk->num_chunks;
2772 emf_bits = ptr = HeapAlloc( GetProcessHeap(), 0, size );
2773 if (!emf_bits) goto done;
2775 if (chunk->chunk_size > remaining) goto done;
2776 remaining -= chunk->chunk_size;
2777 if (chunk->remaining_size != remaining) goto done;
2778 memcpy( ptr, chunk->emf_data, chunk->chunk_size );
2779 ptr += chunk->chunk_size;
2780 if (--chunks == 0) break;
2783 for (offset = 0; offset < mf_size / 2; offset++)
2784 checksum += *((WORD *)buf + offset);
2785 if (checksum) goto done;
2787 emf = SetEnhMetaFileBits( size, emf_bits );
2789 done:
2790 HeapFree( GetProcessHeap(), 0, emf_bits );
2791 return emf;
2794 typedef struct wmf_in_emf_comment
2796 DWORD ident;
2797 DWORD iComment;
2798 DWORD nVersion;
2799 DWORD nChecksum;
2800 DWORD fFlags;
2801 DWORD cbWinMetaFile;
2802 } wmf_in_emf_comment;
2804 /******************************************************************
2805 * SetWinMetaFileBits (GDI32.@)
2807 * Translate from old style to new style.
2810 HENHMETAFILE WINAPI SetWinMetaFileBits(UINT cbBuffer, const BYTE *lpbBuffer, HDC hdcRef,
2811 const METAFILEPICT *lpmfp)
2813 static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
2814 HMETAFILE hmf = NULL;
2815 HENHMETAFILE ret = NULL;
2816 HDC hdc = NULL, hdcdisp = NULL;
2817 RECT rc, *prcFrame = NULL;
2818 LONG mm, xExt, yExt;
2819 INT horzsize, vertsize, horzres, vertres;
2821 TRACE("(%d, %p, %p, %p)\n", cbBuffer, lpbBuffer, hdcRef, lpmfp);
2823 hmf = SetMetaFileBitsEx(cbBuffer, lpbBuffer);
2824 if(!hmf)
2826 WARN("SetMetaFileBitsEx failed\n");
2827 return NULL;
2830 ret = extract_emf_from_comment( lpbBuffer, cbBuffer );
2831 if (ret) return ret;
2833 if(!hdcRef)
2834 hdcRef = hdcdisp = CreateDCW(szDisplayW, NULL, NULL, NULL);
2836 if (lpmfp)
2838 TRACE("mm = %d %dx%d\n", lpmfp->mm, lpmfp->xExt, lpmfp->yExt);
2840 mm = lpmfp->mm;
2841 xExt = lpmfp->xExt;
2842 yExt = lpmfp->yExt;
2844 else
2846 TRACE("lpmfp == NULL\n");
2848 /* Use the whole device surface */
2849 mm = MM_ANISOTROPIC;
2850 xExt = 0;
2851 yExt = 0;
2854 if (mm == MM_ISOTROPIC || mm == MM_ANISOTROPIC)
2856 if (xExt < 0 || yExt < 0)
2858 /* Use the whole device surface */
2859 xExt = 0;
2860 yExt = 0;
2863 /* Use the x and y extents as the frame box */
2864 if (xExt && yExt)
2866 rc.left = rc.top = 0;
2867 rc.right = xExt;
2868 rc.bottom = yExt;
2869 prcFrame = &rc;
2873 if(!(hdc = CreateEnhMetaFileW(hdcRef, NULL, prcFrame, NULL)))
2875 ERR("CreateEnhMetaFile failed\n");
2876 goto end;
2880 * Write the original METAFILE into the enhanced metafile.
2881 * It is encapsulated in a GDICOMMENT_WINDOWS_METAFILE record.
2883 if (mm != MM_TEXT)
2885 wmf_in_emf_comment *mfcomment;
2886 UINT mfcomment_size;
2888 mfcomment_size = sizeof (*mfcomment) + cbBuffer;
2889 mfcomment = HeapAlloc(GetProcessHeap(), 0, mfcomment_size);
2890 if (mfcomment)
2892 mfcomment->ident = GDICOMMENT_IDENTIFIER;
2893 mfcomment->iComment = GDICOMMENT_WINDOWS_METAFILE;
2894 mfcomment->nVersion = 0x00000300;
2895 mfcomment->nChecksum = 0; /* FIXME */
2896 mfcomment->fFlags = 0;
2897 mfcomment->cbWinMetaFile = cbBuffer;
2898 memcpy(&mfcomment[1], lpbBuffer, cbBuffer);
2899 GdiComment(hdc, mfcomment_size, (BYTE*) mfcomment);
2900 HeapFree(GetProcessHeap(), 0, mfcomment);
2902 SetMapMode(hdc, mm);
2906 horzsize = GetDeviceCaps(hdcRef, HORZSIZE);
2907 vertsize = GetDeviceCaps(hdcRef, VERTSIZE);
2908 horzres = GetDeviceCaps(hdcRef, HORZRES);
2909 vertres = GetDeviceCaps(hdcRef, VERTRES);
2911 if (!xExt || !yExt)
2913 /* Use the whole device surface */
2914 xExt = horzres;
2915 yExt = vertres;
2917 else
2919 xExt = MulDiv(xExt, horzres, 100 * horzsize);
2920 yExt = MulDiv(yExt, vertres, 100 * vertsize);
2923 /* set the initial viewport:window ratio as 1:1 */
2924 SetViewportExtEx(hdc, xExt, yExt, NULL);
2925 SetWindowExtEx(hdc, xExt, yExt, NULL);
2927 PlayMetaFile(hdc, hmf);
2929 ret = CloseEnhMetaFile(hdc);
2930 end:
2931 if (hdcdisp) DeleteDC(hdcdisp);
2932 DeleteMetaFile(hmf);
2933 return ret;