winecoreaudio: Implement widOpen and widClose.
[wine/multimedia.git] / dlls / gdi32 / enhmetafile.c
blob5c1294fc23aafb91abe4e047dca8027f7e7921e6
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 GDIOBJHDR header;
54 ENHMETAHEADER *emh;
55 BOOL on_disk; /* true if metafile is on disk */
56 } ENHMETAFILEOBJ;
58 static const struct emr_name {
59 DWORD type;
60 const char *name;
61 } emr_names[] = {
62 #define X(p) {p, #p}
63 X(EMR_HEADER),
64 X(EMR_POLYBEZIER),
65 X(EMR_POLYGON),
66 X(EMR_POLYLINE),
67 X(EMR_POLYBEZIERTO),
68 X(EMR_POLYLINETO),
69 X(EMR_POLYPOLYLINE),
70 X(EMR_POLYPOLYGON),
71 X(EMR_SETWINDOWEXTEX),
72 X(EMR_SETWINDOWORGEX),
73 X(EMR_SETVIEWPORTEXTEX),
74 X(EMR_SETVIEWPORTORGEX),
75 X(EMR_SETBRUSHORGEX),
76 X(EMR_EOF),
77 X(EMR_SETPIXELV),
78 X(EMR_SETMAPPERFLAGS),
79 X(EMR_SETMAPMODE),
80 X(EMR_SETBKMODE),
81 X(EMR_SETPOLYFILLMODE),
82 X(EMR_SETROP2),
83 X(EMR_SETSTRETCHBLTMODE),
84 X(EMR_SETTEXTALIGN),
85 X(EMR_SETCOLORADJUSTMENT),
86 X(EMR_SETTEXTCOLOR),
87 X(EMR_SETBKCOLOR),
88 X(EMR_OFFSETCLIPRGN),
89 X(EMR_MOVETOEX),
90 X(EMR_SETMETARGN),
91 X(EMR_EXCLUDECLIPRECT),
92 X(EMR_INTERSECTCLIPRECT),
93 X(EMR_SCALEVIEWPORTEXTEX),
94 X(EMR_SCALEWINDOWEXTEX),
95 X(EMR_SAVEDC),
96 X(EMR_RESTOREDC),
97 X(EMR_SETWORLDTRANSFORM),
98 X(EMR_MODIFYWORLDTRANSFORM),
99 X(EMR_SELECTOBJECT),
100 X(EMR_CREATEPEN),
101 X(EMR_CREATEBRUSHINDIRECT),
102 X(EMR_DELETEOBJECT),
103 X(EMR_ANGLEARC),
104 X(EMR_ELLIPSE),
105 X(EMR_RECTANGLE),
106 X(EMR_ROUNDRECT),
107 X(EMR_ARC),
108 X(EMR_CHORD),
109 X(EMR_PIE),
110 X(EMR_SELECTPALETTE),
111 X(EMR_CREATEPALETTE),
112 X(EMR_SETPALETTEENTRIES),
113 X(EMR_RESIZEPALETTE),
114 X(EMR_REALIZEPALETTE),
115 X(EMR_EXTFLOODFILL),
116 X(EMR_LINETO),
117 X(EMR_ARCTO),
118 X(EMR_POLYDRAW),
119 X(EMR_SETARCDIRECTION),
120 X(EMR_SETMITERLIMIT),
121 X(EMR_BEGINPATH),
122 X(EMR_ENDPATH),
123 X(EMR_CLOSEFIGURE),
124 X(EMR_FILLPATH),
125 X(EMR_STROKEANDFILLPATH),
126 X(EMR_STROKEPATH),
127 X(EMR_FLATTENPATH),
128 X(EMR_WIDENPATH),
129 X(EMR_SELECTCLIPPATH),
130 X(EMR_ABORTPATH),
131 X(EMR_GDICOMMENT),
132 X(EMR_FILLRGN),
133 X(EMR_FRAMERGN),
134 X(EMR_INVERTRGN),
135 X(EMR_PAINTRGN),
136 X(EMR_EXTSELECTCLIPRGN),
137 X(EMR_BITBLT),
138 X(EMR_STRETCHBLT),
139 X(EMR_MASKBLT),
140 X(EMR_PLGBLT),
141 X(EMR_SETDIBITSTODEVICE),
142 X(EMR_STRETCHDIBITS),
143 X(EMR_EXTCREATEFONTINDIRECTW),
144 X(EMR_EXTTEXTOUTA),
145 X(EMR_EXTTEXTOUTW),
146 X(EMR_POLYBEZIER16),
147 X(EMR_POLYGON16),
148 X(EMR_POLYLINE16),
149 X(EMR_POLYBEZIERTO16),
150 X(EMR_POLYLINETO16),
151 X(EMR_POLYPOLYLINE16),
152 X(EMR_POLYPOLYGON16),
153 X(EMR_POLYDRAW16),
154 X(EMR_CREATEMONOBRUSH),
155 X(EMR_CREATEDIBPATTERNBRUSHPT),
156 X(EMR_EXTCREATEPEN),
157 X(EMR_POLYTEXTOUTA),
158 X(EMR_POLYTEXTOUTW),
159 X(EMR_SETICMMODE),
160 X(EMR_CREATECOLORSPACE),
161 X(EMR_SETCOLORSPACE),
162 X(EMR_DELETECOLORSPACE),
163 X(EMR_GLSRECORD),
164 X(EMR_GLSBOUNDEDRECORD),
165 X(EMR_PIXELFORMAT),
166 X(EMR_DRAWESCAPE),
167 X(EMR_EXTESCAPE),
168 X(EMR_STARTDOC),
169 X(EMR_SMALLTEXTOUT),
170 X(EMR_FORCEUFIMAPPING),
171 X(EMR_NAMEDESCAPE),
172 X(EMR_COLORCORRECTPALETTE),
173 X(EMR_SETICMPROFILEA),
174 X(EMR_SETICMPROFILEW),
175 X(EMR_ALPHABLEND),
176 X(EMR_SETLAYOUT),
177 X(EMR_TRANSPARENTBLT),
178 X(EMR_RESERVED_117),
179 X(EMR_GRADIENTFILL),
180 X(EMR_SETLINKEDUFI),
181 X(EMR_SETTEXTJUSTIFICATION),
182 X(EMR_COLORMATCHTOTARGETW),
183 X(EMR_CREATECOLORSPACEW)
184 #undef X
187 /****************************************************************************
188 * get_emr_name
190 static const char *get_emr_name(DWORD type)
192 unsigned int i;
193 for(i = 0; i < sizeof(emr_names) / sizeof(emr_names[0]); i++)
194 if(type == emr_names[i].type) return emr_names[i].name;
195 TRACE("Unknown record type %d\n", type);
196 return NULL;
199 /***********************************************************************
200 * is_dib_monochrome
202 * Returns whether a DIB can be converted to a monochrome DDB.
204 * A DIB can be converted if its color table contains only black and
205 * white. Black must be the first color in the color table.
207 * Note : If the first color in the color table is white followed by
208 * black, we can't convert it to a monochrome DDB with
209 * SetDIBits, because black and white would be inverted.
211 static inline BOOL is_dib_monochrome( const BITMAPINFO* info )
213 if (info->bmiHeader.biBitCount != 1) return FALSE;
215 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
217 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO *) info)->bmciColors;
219 /* Check if the first color is black */
220 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
222 rgb++;
223 /* Check if the second color is white */
224 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
225 && (rgb->rgbtBlue == 0xff));
227 else return FALSE;
229 else /* assume BITMAPINFOHEADER */
231 const RGBQUAD *rgb = info->bmiColors;
233 /* Check if the first color is black */
234 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
235 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
237 rgb++;
239 /* Check if the second color is white */
240 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
241 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
243 else return FALSE;
247 /****************************************************************************
248 * EMF_Create_HENHMETAFILE
250 HENHMETAFILE EMF_Create_HENHMETAFILE(ENHMETAHEADER *emh, BOOL on_disk )
252 HENHMETAFILE hmf = 0;
253 ENHMETAFILEOBJ *metaObj = GDI_AllocObject( sizeof(ENHMETAFILEOBJ),
254 ENHMETAFILE_MAGIC,
255 (HGDIOBJ *)&hmf, NULL );
256 if (metaObj)
258 metaObj->emh = emh;
259 metaObj->on_disk = on_disk;
260 GDI_ReleaseObj( hmf );
262 return hmf;
265 /****************************************************************************
266 * EMF_Delete_HENHMETAFILE
268 static BOOL EMF_Delete_HENHMETAFILE( HENHMETAFILE hmf )
270 ENHMETAFILEOBJ *metaObj = (ENHMETAFILEOBJ *)GDI_GetObjPtr( hmf,
271 ENHMETAFILE_MAGIC );
272 if(!metaObj) return FALSE;
274 if(metaObj->on_disk)
275 UnmapViewOfFile( metaObj->emh );
276 else
277 HeapFree( GetProcessHeap(), 0, metaObj->emh );
278 return GDI_FreeObject( hmf, metaObj );
281 /******************************************************************
282 * EMF_GetEnhMetaHeader
284 * Returns ptr to ENHMETAHEADER associated with HENHMETAFILE
286 static ENHMETAHEADER *EMF_GetEnhMetaHeader( HENHMETAFILE hmf )
288 ENHMETAHEADER *ret = NULL;
289 ENHMETAFILEOBJ *metaObj = (ENHMETAFILEOBJ *)GDI_GetObjPtr( hmf, ENHMETAFILE_MAGIC );
290 TRACE("hmf %p -> enhmetaObj %p\n", hmf, metaObj);
291 if (metaObj)
293 ret = metaObj->emh;
294 GDI_ReleaseObj( hmf );
296 return ret;
299 /*****************************************************************************
300 * EMF_GetEnhMetaFile
303 static HENHMETAFILE EMF_GetEnhMetaFile( HANDLE hFile )
305 ENHMETAHEADER *emh;
306 HANDLE hMapping;
308 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
309 emh = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
310 CloseHandle( hMapping );
312 if (!emh) return 0;
314 if (emh->iType != EMR_HEADER || emh->dSignature != ENHMETA_SIGNATURE) {
315 WARN("Invalid emf header type 0x%08x sig 0x%08x.\n",
316 emh->iType, emh->dSignature);
317 goto err;
320 /* refuse to load unaligned EMF as Windows does */
321 if (emh->nBytes & 3)
323 WARN("Refusing to load unaligned EMF\n");
324 goto err;
327 return EMF_Create_HENHMETAFILE( emh, TRUE );
329 err:
330 UnmapViewOfFile( emh );
331 return 0;
335 /*****************************************************************************
336 * GetEnhMetaFileA (GDI32.@)
340 HENHMETAFILE WINAPI GetEnhMetaFileA(
341 LPCSTR lpszMetaFile /* [in] filename of enhanced metafile */
344 HENHMETAFILE hmf;
345 HANDLE hFile;
347 hFile = CreateFileA(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
348 OPEN_EXISTING, 0, 0);
349 if (hFile == INVALID_HANDLE_VALUE) {
350 WARN("could not open %s\n", lpszMetaFile);
351 return 0;
353 hmf = EMF_GetEnhMetaFile( hFile );
354 CloseHandle( hFile );
355 return hmf;
358 /*****************************************************************************
359 * GetEnhMetaFileW (GDI32.@)
361 HENHMETAFILE WINAPI GetEnhMetaFileW(
362 LPCWSTR lpszMetaFile) /* [in] filename of enhanced metafile */
364 HENHMETAFILE hmf;
365 HANDLE hFile;
367 hFile = CreateFileW(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
368 OPEN_EXISTING, 0, 0);
369 if (hFile == INVALID_HANDLE_VALUE) {
370 WARN("could not open %s\n", debugstr_w(lpszMetaFile));
371 return 0;
373 hmf = EMF_GetEnhMetaFile( hFile );
374 CloseHandle( hFile );
375 return hmf;
378 /*****************************************************************************
379 * GetEnhMetaFileHeader (GDI32.@)
381 * Retrieves the record containing the header for the specified
382 * enhanced-format metafile.
384 * RETURNS
385 * If buf is NULL, returns the size of buffer required.
386 * Otherwise, copy up to bufsize bytes of enhanced metafile header into
387 * buf.
389 UINT WINAPI GetEnhMetaFileHeader(
390 HENHMETAFILE hmf, /* [in] enhanced metafile */
391 UINT bufsize, /* [in] size of buffer */
392 LPENHMETAHEADER buf /* [out] buffer */
395 LPENHMETAHEADER emh;
396 UINT size;
398 emh = EMF_GetEnhMetaHeader(hmf);
399 if(!emh) return FALSE;
400 size = emh->nSize;
401 if (!buf) return size;
402 size = min(size, bufsize);
403 memmove(buf, emh, size);
404 return size;
408 /*****************************************************************************
409 * GetEnhMetaFileDescriptionA (GDI32.@)
411 * See GetEnhMetaFileDescriptionW.
413 UINT WINAPI GetEnhMetaFileDescriptionA(
414 HENHMETAFILE hmf, /* [in] enhanced metafile */
415 UINT size, /* [in] size of buf */
416 LPSTR buf /* [out] buffer to receive description */
419 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
420 DWORD len;
421 WCHAR *descrW;
423 if(!emh) return FALSE;
424 if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
425 descrW = (WCHAR *) ((char *) emh + emh->offDescription);
426 len = WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, NULL, 0, NULL, NULL );
428 if (!buf || !size ) return len;
430 len = min( size, len );
431 WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, buf, len, NULL, NULL );
432 return len;
435 /*****************************************************************************
436 * GetEnhMetaFileDescriptionW (GDI32.@)
438 * Copies the description string of an enhanced metafile into a buffer
439 * _buf_.
441 * RETURNS
442 * If _buf_ is NULL, returns size of _buf_ required. Otherwise, returns
443 * number of characters copied.
445 UINT WINAPI GetEnhMetaFileDescriptionW(
446 HENHMETAFILE hmf, /* [in] enhanced metafile */
447 UINT size, /* [in] size of buf */
448 LPWSTR buf /* [out] buffer to receive description */
451 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
453 if(!emh) return FALSE;
454 if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
455 if (!buf || !size ) return emh->nDescription;
457 memmove(buf, (char *) emh + emh->offDescription, min(size,emh->nDescription)*sizeof(WCHAR));
458 return min(size, emh->nDescription);
461 /****************************************************************************
462 * SetEnhMetaFileBits (GDI32.@)
464 * Creates an enhanced metafile by copying _bufsize_ bytes from _buf_.
466 HENHMETAFILE WINAPI SetEnhMetaFileBits(UINT bufsize, const BYTE *buf)
468 ENHMETAHEADER *emh = HeapAlloc( GetProcessHeap(), 0, bufsize );
469 memmove(emh, buf, bufsize);
470 return EMF_Create_HENHMETAFILE( emh, FALSE );
473 /*****************************************************************************
474 * GetEnhMetaFileBits (GDI32.@)
477 UINT WINAPI GetEnhMetaFileBits(
478 HENHMETAFILE hmf,
479 UINT bufsize,
480 LPBYTE buf
483 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader( hmf );
484 UINT size;
486 if(!emh) return 0;
488 size = emh->nBytes;
489 if( buf == NULL ) return size;
491 size = min( size, bufsize );
492 memmove(buf, emh, size);
493 return size;
496 typedef struct enum_emh_data
498 INT mode;
499 XFORM init_transform;
500 XFORM world_transform;
501 INT wndOrgX;
502 INT wndOrgY;
503 INT wndExtX;
504 INT wndExtY;
505 INT vportOrgX;
506 INT vportOrgY;
507 INT vportExtX;
508 INT vportExtY;
509 } enum_emh_data;
511 #define ENUM_GET_PRIVATE_DATA(ht) \
512 ((enum_emh_data*)(((unsigned char*)(ht))-sizeof (enum_emh_data)))
514 #define WIDTH(rect) ( (rect).right - (rect).left )
515 #define HEIGHT(rect) ( (rect).bottom - (rect).top )
517 #define IS_WIN9X() (GetVersion()&0x80000000)
519 static void EMF_Update_MF_Xform(HDC hdc, enum_emh_data *info)
521 XFORM mapping_mode_trans, final_trans;
522 FLOAT scaleX, scaleY;
524 scaleX = (FLOAT)info->vportExtX / (FLOAT)info->wndExtX;
525 scaleY = (FLOAT)info->vportExtY / (FLOAT)info->wndExtY;
526 mapping_mode_trans.eM11 = scaleX;
527 mapping_mode_trans.eM12 = 0.0;
528 mapping_mode_trans.eM21 = 0.0;
529 mapping_mode_trans.eM22 = scaleY;
530 mapping_mode_trans.eDx = (FLOAT)info->vportOrgX - scaleX * (FLOAT)info->wndOrgX;
531 mapping_mode_trans.eDy = (FLOAT)info->vportOrgY - scaleY * (FLOAT)info->wndOrgY;
533 CombineTransform(&final_trans, &info->world_transform, &mapping_mode_trans);
534 CombineTransform(&final_trans, &final_trans, &info->init_transform);
536 if (!SetWorldTransform(hdc, &final_trans))
538 ERR("World transform failed!\n");
542 static void EMF_SetMapMode(HDC hdc, enum_emh_data *info)
544 INT horzSize = GetDeviceCaps( hdc, HORZSIZE );
545 INT vertSize = GetDeviceCaps( hdc, VERTSIZE );
546 INT horzRes = GetDeviceCaps( hdc, HORZRES );
547 INT vertRes = GetDeviceCaps( hdc, VERTRES );
549 TRACE("%d\n",info->mode);
551 switch(info->mode)
553 case MM_TEXT:
554 info->wndExtX = 1;
555 info->wndExtY = 1;
556 info->vportExtX = 1;
557 info->vportExtY = 1;
558 break;
559 case MM_LOMETRIC:
560 case MM_ISOTROPIC:
561 info->wndExtX = horzSize * 10;
562 info->wndExtY = vertSize * 10;
563 info->vportExtX = horzRes;
564 info->vportExtY = -vertRes;
565 break;
566 case MM_HIMETRIC:
567 info->wndExtX = horzSize * 100;
568 info->wndExtY = vertSize * 100;
569 info->vportExtX = horzRes;
570 info->vportExtY = -vertRes;
571 break;
572 case MM_LOENGLISH:
573 info->wndExtX = MulDiv(1000, horzSize, 254);
574 info->wndExtY = MulDiv(1000, vertSize, 254);
575 info->vportExtX = horzRes;
576 info->vportExtY = -vertRes;
577 break;
578 case MM_HIENGLISH:
579 info->wndExtX = MulDiv(10000, horzSize, 254);
580 info->wndExtY = MulDiv(10000, vertSize, 254);
581 info->vportExtX = horzRes;
582 info->vportExtY = -vertRes;
583 break;
584 case MM_TWIPS:
585 info->wndExtX = MulDiv(14400, horzSize, 254);
586 info->wndExtY = MulDiv(14400, vertSize, 254);
587 info->vportExtX = horzRes;
588 info->vportExtY = -vertRes;
589 break;
590 case MM_ANISOTROPIC:
591 break;
592 default:
593 return;
597 /***********************************************************************
598 * EMF_FixIsotropic
600 * Fix viewport extensions for isotropic mode.
603 static void EMF_FixIsotropic(HDC hdc, enum_emh_data *info)
605 double xdim = fabs((double)info->vportExtX * GetDeviceCaps( hdc, HORZSIZE ) /
606 (GetDeviceCaps( hdc, HORZRES ) * info->wndExtX));
607 double ydim = fabs((double)info->vportExtY * GetDeviceCaps( hdc, VERTSIZE ) /
608 (GetDeviceCaps( hdc, VERTRES ) * info->wndExtY));
610 if (xdim > ydim)
612 INT mincx = (info->vportExtX >= 0) ? 1 : -1;
613 info->vportExtX = floor(info->vportExtX * ydim / xdim + 0.5);
614 if (!info->vportExtX) info->vportExtX = mincx;
616 else
618 INT mincy = (info->vportExtY >= 0) ? 1 : -1;
619 info->vportExtY = floor(info->vportExtY * xdim / ydim + 0.5);
620 if (!info->vportExtY) info->vportExtY = mincy;
624 /*****************************************************************************
625 * emr_produces_output
627 * Returns TRUE if the record type writes something to the dc. Used by
628 * PlayEnhMetaFileRecord to determine whether it needs to update the
629 * dc's xform when in win9x mode.
631 * FIXME: need to test which records should be here.
633 static BOOL emr_produces_output(int type)
635 switch(type) {
636 case EMR_POLYBEZIER:
637 case EMR_POLYGON:
638 case EMR_POLYLINE:
639 case EMR_POLYBEZIERTO:
640 case EMR_POLYLINETO:
641 case EMR_POLYPOLYLINE:
642 case EMR_POLYPOLYGON:
643 case EMR_SETPIXELV:
644 case EMR_MOVETOEX:
645 case EMR_EXCLUDECLIPRECT:
646 case EMR_INTERSECTCLIPRECT:
647 case EMR_SELECTOBJECT:
648 case EMR_ANGLEARC:
649 case EMR_ELLIPSE:
650 case EMR_RECTANGLE:
651 case EMR_ROUNDRECT:
652 case EMR_ARC:
653 case EMR_CHORD:
654 case EMR_PIE:
655 case EMR_EXTFLOODFILL:
656 case EMR_LINETO:
657 case EMR_ARCTO:
658 case EMR_POLYDRAW:
659 case EMR_FILLRGN:
660 case EMR_FRAMERGN:
661 case EMR_INVERTRGN:
662 case EMR_PAINTRGN:
663 case EMR_BITBLT:
664 case EMR_STRETCHBLT:
665 case EMR_MASKBLT:
666 case EMR_PLGBLT:
667 case EMR_SETDIBITSTODEVICE:
668 case EMR_STRETCHDIBITS:
669 case EMR_EXTTEXTOUTA:
670 case EMR_EXTTEXTOUTW:
671 case EMR_POLYBEZIER16:
672 case EMR_POLYGON16:
673 case EMR_POLYLINE16:
674 case EMR_POLYBEZIERTO16:
675 case EMR_POLYLINETO16:
676 case EMR_POLYPOLYLINE16:
677 case EMR_POLYPOLYGON16:
678 case EMR_POLYDRAW16:
679 case EMR_POLYTEXTOUTA:
680 case EMR_POLYTEXTOUTW:
681 case EMR_SMALLTEXTOUT:
682 case EMR_ALPHABLEND:
683 case EMR_TRANSPARENTBLT:
684 return TRUE;
685 default:
686 return FALSE;
691 /*****************************************************************************
692 * PlayEnhMetaFileRecord (GDI32.@)
694 * Render a single enhanced metafile record in the device context hdc.
696 * RETURNS
697 * TRUE (non zero) on success, FALSE on error.
698 * BUGS
699 * Many unimplemented records.
700 * No error handling on record play failures (ie checking return codes)
702 * NOTES
703 * WinNT actually updates the current world transform in this function
704 * whereas Win9x does not.
706 BOOL WINAPI PlayEnhMetaFileRecord(
707 HDC hdc, /* [in] device context in which to render EMF record */
708 LPHANDLETABLE handletable, /* [in] array of handles to be used in rendering record */
709 const ENHMETARECORD *mr, /* [in] EMF record to render */
710 UINT handles /* [in] size of handle array */
713 int type;
714 RECT tmprc;
715 enum_emh_data *info = ENUM_GET_PRIVATE_DATA(handletable);
717 TRACE("hdc = %p, handletable = %p, record = %p, numHandles = %d\n",
718 hdc, handletable, mr, handles);
719 if (!mr) return FALSE;
721 type = mr->iType;
723 /* In Win9x mode we update the xform if the record will produce output */
724 if ( IS_WIN9X() && emr_produces_output(type) )
725 EMF_Update_MF_Xform(hdc, info);
727 TRACE("record %s\n", get_emr_name(type));
728 switch(type)
730 case EMR_HEADER:
731 break;
732 case EMR_EOF:
733 break;
734 case EMR_GDICOMMENT:
736 const EMRGDICOMMENT *lpGdiComment = (const EMRGDICOMMENT *)mr;
737 /* In an enhanced metafile, there can be both public and private GDI comments */
738 GdiComment( hdc, lpGdiComment->cbData, lpGdiComment->Data );
739 break;
741 case EMR_SETMAPMODE:
743 const EMRSETMAPMODE *pSetMapMode = (const EMRSETMAPMODE *)mr;
745 if(info->mode == pSetMapMode->iMode && (info->mode == MM_ISOTROPIC || info->mode == MM_ANISOTROPIC))
746 break;
747 info->mode = pSetMapMode->iMode;
748 EMF_SetMapMode(hdc, info);
749 break;
751 case EMR_SETBKMODE:
753 const EMRSETBKMODE *pSetBkMode = (const EMRSETBKMODE *)mr;
754 SetBkMode(hdc, pSetBkMode->iMode);
755 break;
757 case EMR_SETBKCOLOR:
759 const EMRSETBKCOLOR *pSetBkColor = (const EMRSETBKCOLOR *)mr;
760 SetBkColor(hdc, pSetBkColor->crColor);
761 break;
763 case EMR_SETPOLYFILLMODE:
765 const EMRSETPOLYFILLMODE *pSetPolyFillMode = (const EMRSETPOLYFILLMODE *)mr;
766 SetPolyFillMode(hdc, pSetPolyFillMode->iMode);
767 break;
769 case EMR_SETROP2:
771 const EMRSETROP2 *pSetROP2 = (const EMRSETROP2 *)mr;
772 SetROP2(hdc, pSetROP2->iMode);
773 break;
775 case EMR_SETSTRETCHBLTMODE:
777 const EMRSETSTRETCHBLTMODE *pSetStretchBltMode = (const EMRSETSTRETCHBLTMODE *)mr;
778 SetStretchBltMode(hdc, pSetStretchBltMode->iMode);
779 break;
781 case EMR_SETTEXTALIGN:
783 const EMRSETTEXTALIGN *pSetTextAlign = (const EMRSETTEXTALIGN *)mr;
784 SetTextAlign(hdc, pSetTextAlign->iMode);
785 break;
787 case EMR_SETTEXTCOLOR:
789 const EMRSETTEXTCOLOR *pSetTextColor = (const EMRSETTEXTCOLOR *)mr;
790 SetTextColor(hdc, pSetTextColor->crColor);
791 break;
793 case EMR_SAVEDC:
795 SaveDC(hdc);
796 break;
798 case EMR_RESTOREDC:
800 const EMRRESTOREDC *pRestoreDC = (const EMRRESTOREDC *)mr;
801 TRACE("EMR_RESTORE: %d\n", pRestoreDC->iRelative);
802 RestoreDC(hdc, pRestoreDC->iRelative);
803 break;
805 case EMR_INTERSECTCLIPRECT:
807 const EMRINTERSECTCLIPRECT *pClipRect = (const EMRINTERSECTCLIPRECT *)mr;
808 TRACE("EMR_INTERSECTCLIPRECT: rect %d,%d - %d, %d\n",
809 pClipRect->rclClip.left, pClipRect->rclClip.top,
810 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
811 IntersectClipRect(hdc, pClipRect->rclClip.left, pClipRect->rclClip.top,
812 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
813 break;
815 case EMR_SELECTOBJECT:
817 const EMRSELECTOBJECT *pSelectObject = (const EMRSELECTOBJECT *)mr;
818 if( pSelectObject->ihObject & 0x80000000 ) {
819 /* High order bit is set - it's a stock object
820 * Strip the high bit to get the index.
821 * See MSDN article Q142319
823 SelectObject( hdc, GetStockObject( pSelectObject->ihObject &
824 0x7fffffff ) );
825 } else {
826 /* High order bit wasn't set - not a stock object
828 SelectObject( hdc,
829 (handletable->objectHandle)[pSelectObject->ihObject] );
831 break;
833 case EMR_DELETEOBJECT:
835 const EMRDELETEOBJECT *pDeleteObject = (const EMRDELETEOBJECT *)mr;
836 DeleteObject( (handletable->objectHandle)[pDeleteObject->ihObject]);
837 (handletable->objectHandle)[pDeleteObject->ihObject] = 0;
838 break;
840 case EMR_SETWINDOWORGEX:
842 const EMRSETWINDOWORGEX *pSetWindowOrgEx = (const EMRSETWINDOWORGEX *)mr;
844 info->wndOrgX = pSetWindowOrgEx->ptlOrigin.x;
845 info->wndOrgY = pSetWindowOrgEx->ptlOrigin.y;
847 TRACE("SetWindowOrgEx: %d,%d\n",info->wndOrgX,info->wndOrgY);
848 break;
850 case EMR_SETWINDOWEXTEX:
852 const EMRSETWINDOWEXTEX *pSetWindowExtEx = (const EMRSETWINDOWEXTEX *)mr;
854 if(info->mode != MM_ISOTROPIC && info->mode != MM_ANISOTROPIC)
855 break;
856 info->wndExtX = pSetWindowExtEx->szlExtent.cx;
857 info->wndExtY = pSetWindowExtEx->szlExtent.cy;
858 if (info->mode == MM_ISOTROPIC)
859 EMF_FixIsotropic(hdc, info);
861 TRACE("SetWindowExtEx: %d,%d\n",info->wndExtX,info->wndExtY);
862 break;
864 case EMR_SETVIEWPORTORGEX:
866 const EMRSETVIEWPORTORGEX *pSetViewportOrgEx = (const EMRSETVIEWPORTORGEX *)mr;
867 enum_emh_data *info = ENUM_GET_PRIVATE_DATA(handletable);
869 info->vportOrgX = pSetViewportOrgEx->ptlOrigin.x;
870 info->vportOrgY = pSetViewportOrgEx->ptlOrigin.y;
871 TRACE("SetViewportOrgEx: %d,%d\n",info->vportOrgX,info->vportOrgY);
872 break;
874 case EMR_SETVIEWPORTEXTEX:
876 const EMRSETVIEWPORTEXTEX *pSetViewportExtEx = (const EMRSETVIEWPORTEXTEX *)mr;
877 enum_emh_data *info = ENUM_GET_PRIVATE_DATA(handletable);
879 if(info->mode != MM_ISOTROPIC && info->mode != MM_ANISOTROPIC)
880 break;
881 info->vportExtX = pSetViewportExtEx->szlExtent.cx;
882 info->vportExtY = pSetViewportExtEx->szlExtent.cy;
883 if (info->mode == MM_ISOTROPIC)
884 EMF_FixIsotropic(hdc, info);
885 TRACE("SetViewportExtEx: %d,%d\n",info->vportExtX,info->vportExtY);
886 break;
888 case EMR_CREATEPEN:
890 const EMRCREATEPEN *pCreatePen = (const EMRCREATEPEN *)mr;
891 (handletable->objectHandle)[pCreatePen->ihPen] =
892 CreatePenIndirect(&pCreatePen->lopn);
893 break;
895 case EMR_EXTCREATEPEN:
897 const EMREXTCREATEPEN *pPen = (const EMREXTCREATEPEN *)mr;
898 LOGBRUSH lb;
899 lb.lbStyle = pPen->elp.elpBrushStyle;
900 lb.lbColor = pPen->elp.elpColor;
901 lb.lbHatch = pPen->elp.elpHatch;
903 if(pPen->offBmi || pPen->offBits)
904 FIXME("EMR_EXTCREATEPEN: Need to copy brush bitmap\n");
906 (handletable->objectHandle)[pPen->ihPen] =
907 ExtCreatePen(pPen->elp.elpPenStyle, pPen->elp.elpWidth, &lb,
908 pPen->elp.elpNumEntries, pPen->elp.elpStyleEntry);
909 break;
911 case EMR_CREATEBRUSHINDIRECT:
913 const EMRCREATEBRUSHINDIRECT *pBrush = (const EMRCREATEBRUSHINDIRECT *)mr;
914 LOGBRUSH brush;
915 brush.lbStyle = pBrush->lb.lbStyle;
916 brush.lbColor = pBrush->lb.lbColor;
917 brush.lbHatch = pBrush->lb.lbHatch;
918 (handletable->objectHandle)[pBrush->ihBrush] = CreateBrushIndirect(&brush);
919 break;
921 case EMR_EXTCREATEFONTINDIRECTW:
923 const EMREXTCREATEFONTINDIRECTW *pFont = (const EMREXTCREATEFONTINDIRECTW *)mr;
924 (handletable->objectHandle)[pFont->ihFont] =
925 CreateFontIndirectW(&pFont->elfw.elfLogFont);
926 break;
928 case EMR_MOVETOEX:
930 const EMRMOVETOEX *pMoveToEx = (const EMRMOVETOEX *)mr;
931 MoveToEx(hdc, pMoveToEx->ptl.x, pMoveToEx->ptl.y, NULL);
932 break;
934 case EMR_LINETO:
936 const EMRLINETO *pLineTo = (const EMRLINETO *)mr;
937 LineTo(hdc, pLineTo->ptl.x, pLineTo->ptl.y);
938 break;
940 case EMR_RECTANGLE:
942 const EMRRECTANGLE *pRect = (const EMRRECTANGLE *)mr;
943 Rectangle(hdc, pRect->rclBox.left, pRect->rclBox.top,
944 pRect->rclBox.right, pRect->rclBox.bottom);
945 break;
947 case EMR_ELLIPSE:
949 const EMRELLIPSE *pEllipse = (const EMRELLIPSE *)mr;
950 Ellipse(hdc, pEllipse->rclBox.left, pEllipse->rclBox.top,
951 pEllipse->rclBox.right, pEllipse->rclBox.bottom);
952 break;
954 case EMR_POLYGON16:
956 const EMRPOLYGON16 *pPoly = (const EMRPOLYGON16 *)mr;
957 /* Shouldn't use Polygon16 since pPoly->cpts is DWORD */
958 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
959 pPoly->cpts * sizeof(POINT) );
960 DWORD i;
961 for(i = 0; i < pPoly->cpts; i++)
963 pts[i].x = pPoly->apts[i].x;
964 pts[i].y = pPoly->apts[i].y;
966 Polygon(hdc, pts, pPoly->cpts);
967 HeapFree( GetProcessHeap(), 0, pts );
968 break;
970 case EMR_POLYLINE16:
972 const EMRPOLYLINE16 *pPoly = (const EMRPOLYLINE16 *)mr;
973 /* Shouldn't use Polyline16 since pPoly->cpts is DWORD */
974 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
975 pPoly->cpts * sizeof(POINT) );
976 DWORD i;
977 for(i = 0; i < pPoly->cpts; i++)
979 pts[i].x = pPoly->apts[i].x;
980 pts[i].y = pPoly->apts[i].y;
982 Polyline(hdc, pts, pPoly->cpts);
983 HeapFree( GetProcessHeap(), 0, pts );
984 break;
986 case EMR_POLYLINETO16:
988 const EMRPOLYLINETO16 *pPoly = (const EMRPOLYLINETO16 *)mr;
989 /* Shouldn't use PolylineTo16 since pPoly->cpts is DWORD */
990 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
991 pPoly->cpts * sizeof(POINT) );
992 DWORD i;
993 for(i = 0; i < pPoly->cpts; i++)
995 pts[i].x = pPoly->apts[i].x;
996 pts[i].y = pPoly->apts[i].y;
998 PolylineTo(hdc, pts, pPoly->cpts);
999 HeapFree( GetProcessHeap(), 0, pts );
1000 break;
1002 case EMR_POLYBEZIER16:
1004 const EMRPOLYBEZIER16 *pPoly = (const EMRPOLYBEZIER16 *)mr;
1005 /* Shouldn't use PolyBezier16 since pPoly->cpts is DWORD */
1006 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1007 pPoly->cpts * sizeof(POINT) );
1008 DWORD i;
1009 for(i = 0; i < pPoly->cpts; i++)
1011 pts[i].x = pPoly->apts[i].x;
1012 pts[i].y = pPoly->apts[i].y;
1014 PolyBezier(hdc, pts, pPoly->cpts);
1015 HeapFree( GetProcessHeap(), 0, pts );
1016 break;
1018 case EMR_POLYBEZIERTO16:
1020 const EMRPOLYBEZIERTO16 *pPoly = (const EMRPOLYBEZIERTO16 *)mr;
1021 /* Shouldn't use PolyBezierTo16 since pPoly->cpts is DWORD */
1022 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1023 pPoly->cpts * sizeof(POINT) );
1024 DWORD i;
1025 for(i = 0; i < pPoly->cpts; i++)
1027 pts[i].x = pPoly->apts[i].x;
1028 pts[i].y = pPoly->apts[i].y;
1030 PolyBezierTo(hdc, pts, pPoly->cpts);
1031 HeapFree( GetProcessHeap(), 0, pts );
1032 break;
1034 case EMR_POLYPOLYGON16:
1036 const EMRPOLYPOLYGON16 *pPolyPoly = (const EMRPOLYPOLYGON16 *)mr;
1037 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1038 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1040 POINT16 *pts16 = (POINT16 *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1041 POINT *pts = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1042 DWORD i;
1043 for(i = 0; i < pPolyPoly->cpts; i++)
1045 pts[i].x = pts16[i].x;
1046 pts[i].y = pts16[i].y;
1048 PolyPolygon(hdc, pts, (INT*)pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1049 HeapFree( GetProcessHeap(), 0, pts );
1050 break;
1052 case EMR_POLYPOLYLINE16:
1054 const EMRPOLYPOLYLINE16 *pPolyPoly = (const EMRPOLYPOLYLINE16 *)mr;
1055 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1056 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1058 POINT16 *pts16 = (POINT16 *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1059 POINT *pts = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1060 DWORD i;
1061 for(i = 0; i < pPolyPoly->cpts; i++)
1063 pts[i].x = pts16[i].x;
1064 pts[i].y = pts16[i].y;
1066 PolyPolyline(hdc, pts, pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1067 HeapFree( GetProcessHeap(), 0, pts );
1068 break;
1071 case EMR_STRETCHDIBITS:
1073 const EMRSTRETCHDIBITS *pStretchDIBits = (const EMRSTRETCHDIBITS *)mr;
1075 StretchDIBits(hdc,
1076 pStretchDIBits->xDest,
1077 pStretchDIBits->yDest,
1078 pStretchDIBits->cxDest,
1079 pStretchDIBits->cyDest,
1080 pStretchDIBits->xSrc,
1081 pStretchDIBits->ySrc,
1082 pStretchDIBits->cxSrc,
1083 pStretchDIBits->cySrc,
1084 (const BYTE *)mr + pStretchDIBits->offBitsSrc,
1085 (const BITMAPINFO *)((const BYTE *)mr + pStretchDIBits->offBmiSrc),
1086 pStretchDIBits->iUsageSrc,
1087 pStretchDIBits->dwRop);
1088 break;
1091 case EMR_EXTTEXTOUTA:
1093 const EMREXTTEXTOUTA *pExtTextOutA = (const EMREXTTEXTOUTA *)mr;
1094 RECT rc;
1095 const INT *dx = NULL;
1097 rc.left = pExtTextOutA->emrtext.rcl.left;
1098 rc.top = pExtTextOutA->emrtext.rcl.top;
1099 rc.right = pExtTextOutA->emrtext.rcl.right;
1100 rc.bottom = pExtTextOutA->emrtext.rcl.bottom;
1101 TRACE("EMR_EXTTEXTOUTA: x,y = %d, %d. rect = %d, %d - %d, %d. flags %08x\n",
1102 pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1103 rc.left, rc.top, rc.right, rc.bottom, pExtTextOutA->emrtext.fOptions);
1105 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1106 * These files can be enumerated and played under Win98 just
1107 * fine, but at least Win2k chokes on them.
1109 if (pExtTextOutA->emrtext.offDx)
1110 dx = (const INT *)((const BYTE *)mr + pExtTextOutA->emrtext.offDx);
1112 ExtTextOutA(hdc, pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1113 pExtTextOutA->emrtext.fOptions, &rc,
1114 (LPCSTR)((const BYTE *)mr + pExtTextOutA->emrtext.offString), pExtTextOutA->emrtext.nChars,
1115 dx);
1116 break;
1119 case EMR_EXTTEXTOUTW:
1121 const EMREXTTEXTOUTW *pExtTextOutW = (const EMREXTTEXTOUTW *)mr;
1122 RECT rc;
1123 const INT *dx = NULL;
1125 rc.left = pExtTextOutW->emrtext.rcl.left;
1126 rc.top = pExtTextOutW->emrtext.rcl.top;
1127 rc.right = pExtTextOutW->emrtext.rcl.right;
1128 rc.bottom = pExtTextOutW->emrtext.rcl.bottom;
1129 TRACE("EMR_EXTTEXTOUTW: x,y = %d, %d. rect = %d, %d - %d, %d. flags %08x\n",
1130 pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1131 rc.left, rc.top, rc.right, rc.bottom, pExtTextOutW->emrtext.fOptions);
1133 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1134 * These files can be enumerated and played under Win98 just
1135 * fine, but at least Win2k chokes on them.
1137 if (pExtTextOutW->emrtext.offDx)
1138 dx = (const INT *)((const BYTE *)mr + pExtTextOutW->emrtext.offDx);
1140 ExtTextOutW(hdc, pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1141 pExtTextOutW->emrtext.fOptions, &rc,
1142 (LPCWSTR)((const BYTE *)mr + pExtTextOutW->emrtext.offString), pExtTextOutW->emrtext.nChars,
1143 dx);
1144 break;
1147 case EMR_CREATEPALETTE:
1149 const EMRCREATEPALETTE *lpCreatePal = (const EMRCREATEPALETTE *)mr;
1151 (handletable->objectHandle)[ lpCreatePal->ihPal ] =
1152 CreatePalette( &lpCreatePal->lgpl );
1154 break;
1157 case EMR_SELECTPALETTE:
1159 const EMRSELECTPALETTE *lpSelectPal = (const EMRSELECTPALETTE *)mr;
1161 if( lpSelectPal->ihPal & 0x80000000 ) {
1162 SelectPalette( hdc, GetStockObject(lpSelectPal->ihPal & 0x7fffffff), TRUE);
1163 } else {
1164 SelectPalette( hdc, (handletable->objectHandle)[lpSelectPal->ihPal], TRUE);
1166 break;
1169 case EMR_REALIZEPALETTE:
1171 RealizePalette( hdc );
1172 break;
1175 case EMR_EXTSELECTCLIPRGN:
1177 #if 0
1178 const EMREXTSELECTCLIPRGN lpRgn = (const EMREXTSELECTCLIPRGN *)mr;
1179 HRGN hRgn = ExtCreateRegion(NULL, lpRgn->cbRgnData, (RGNDATA *)lpRgn->RgnData);
1181 ExtSelectClipRgn(hdc, hRgn, (INT)(lpRgn->iMode));
1182 /* ExtSelectClipRgn created a copy of the region */
1183 DeleteObject(hRgn);
1184 #endif
1185 FIXME("ExtSelectClipRgn\n");
1186 break;
1189 case EMR_SETMETARGN:
1191 SetMetaRgn( hdc );
1192 break;
1195 case EMR_SETWORLDTRANSFORM:
1197 const EMRSETWORLDTRANSFORM *lpXfrm = (const EMRSETWORLDTRANSFORM *)mr;
1198 info->world_transform = lpXfrm->xform;
1199 break;
1202 case EMR_POLYBEZIER:
1204 const EMRPOLYBEZIER *lpPolyBez = (const EMRPOLYBEZIER *)mr;
1205 PolyBezier(hdc, (const POINT*)lpPolyBez->aptl, (UINT)lpPolyBez->cptl);
1206 break;
1209 case EMR_POLYGON:
1211 const EMRPOLYGON *lpPoly = (const EMRPOLYGON *)mr;
1212 Polygon( hdc, (const POINT*)lpPoly->aptl, (UINT)lpPoly->cptl );
1213 break;
1216 case EMR_POLYLINE:
1218 const EMRPOLYLINE *lpPolyLine = (const EMRPOLYLINE *)mr;
1219 Polyline(hdc, (const POINT*)lpPolyLine->aptl, (UINT)lpPolyLine->cptl);
1220 break;
1223 case EMR_POLYBEZIERTO:
1225 const EMRPOLYBEZIERTO *lpPolyBezierTo = (const EMRPOLYBEZIERTO *)mr;
1226 PolyBezierTo( hdc, (const POINT*)lpPolyBezierTo->aptl,
1227 (UINT)lpPolyBezierTo->cptl );
1228 break;
1231 case EMR_POLYLINETO:
1233 const EMRPOLYLINETO *lpPolyLineTo = (const EMRPOLYLINETO *)mr;
1234 PolylineTo( hdc, (const POINT*)lpPolyLineTo->aptl,
1235 (UINT)lpPolyLineTo->cptl );
1236 break;
1239 case EMR_POLYPOLYLINE:
1241 const EMRPOLYPOLYLINE *pPolyPolyline = (const EMRPOLYPOLYLINE *)mr;
1242 /* NB Points at pPolyPolyline->aPolyCounts + pPolyPolyline->nPolys */
1244 PolyPolyline(hdc, (LPPOINT)(pPolyPolyline->aPolyCounts +
1245 pPolyPolyline->nPolys),
1246 pPolyPolyline->aPolyCounts,
1247 pPolyPolyline->nPolys );
1249 break;
1252 case EMR_POLYPOLYGON:
1254 const EMRPOLYPOLYGON *pPolyPolygon = (const EMRPOLYPOLYGON *)mr;
1256 /* NB Points at pPolyPolygon->aPolyCounts + pPolyPolygon->nPolys */
1258 PolyPolygon(hdc, (LPPOINT)(pPolyPolygon->aPolyCounts +
1259 pPolyPolygon->nPolys),
1260 (INT*)pPolyPolygon->aPolyCounts, pPolyPolygon->nPolys );
1261 break;
1264 case EMR_SETBRUSHORGEX:
1266 const EMRSETBRUSHORGEX *lpSetBrushOrgEx = (const EMRSETBRUSHORGEX *)mr;
1268 SetBrushOrgEx( hdc,
1269 (INT)lpSetBrushOrgEx->ptlOrigin.x,
1270 (INT)lpSetBrushOrgEx->ptlOrigin.y,
1271 NULL );
1273 break;
1276 case EMR_SETPIXELV:
1278 const EMRSETPIXELV *lpSetPixelV = (const EMRSETPIXELV *)mr;
1280 SetPixelV( hdc,
1281 (INT)lpSetPixelV->ptlPixel.x,
1282 (INT)lpSetPixelV->ptlPixel.y,
1283 lpSetPixelV->crColor );
1285 break;
1288 case EMR_SETMAPPERFLAGS:
1290 const EMRSETMAPPERFLAGS *lpSetMapperFlags = (const EMRSETMAPPERFLAGS *)mr;
1292 SetMapperFlags( hdc, lpSetMapperFlags->dwFlags );
1294 break;
1297 case EMR_SETCOLORADJUSTMENT:
1299 const EMRSETCOLORADJUSTMENT *lpSetColorAdjust = (const EMRSETCOLORADJUSTMENT *)mr;
1301 SetColorAdjustment( hdc, &lpSetColorAdjust->ColorAdjustment );
1303 break;
1306 case EMR_OFFSETCLIPRGN:
1308 const EMROFFSETCLIPRGN *lpOffsetClipRgn = (const EMROFFSETCLIPRGN *)mr;
1310 OffsetClipRgn( hdc,
1311 (INT)lpOffsetClipRgn->ptlOffset.x,
1312 (INT)lpOffsetClipRgn->ptlOffset.y );
1313 FIXME("OffsetClipRgn\n");
1315 break;
1318 case EMR_EXCLUDECLIPRECT:
1320 const EMREXCLUDECLIPRECT *lpExcludeClipRect = (const EMREXCLUDECLIPRECT *)mr;
1322 ExcludeClipRect( hdc,
1323 lpExcludeClipRect->rclClip.left,
1324 lpExcludeClipRect->rclClip.top,
1325 lpExcludeClipRect->rclClip.right,
1326 lpExcludeClipRect->rclClip.bottom );
1327 FIXME("ExcludeClipRect\n");
1329 break;
1332 case EMR_SCALEVIEWPORTEXTEX:
1334 const EMRSCALEVIEWPORTEXTEX *lpScaleViewportExtEx = (const EMRSCALEVIEWPORTEXTEX *)mr;
1336 if ((info->mode != MM_ISOTROPIC) && (info->mode != MM_ANISOTROPIC))
1337 break;
1338 if (!lpScaleViewportExtEx->xNum || !lpScaleViewportExtEx->xDenom ||
1339 !lpScaleViewportExtEx->yNum || !lpScaleViewportExtEx->yDenom)
1340 break;
1341 info->vportExtX = MulDiv(info->vportExtX, lpScaleViewportExtEx->xNum,
1342 lpScaleViewportExtEx->xDenom);
1343 info->vportExtY = MulDiv(info->vportExtY, lpScaleViewportExtEx->yNum,
1344 lpScaleViewportExtEx->yDenom);
1345 if (info->vportExtX == 0) info->vportExtX = 1;
1346 if (info->vportExtY == 0) info->vportExtY = 1;
1347 if (info->mode == MM_ISOTROPIC)
1348 EMF_FixIsotropic(hdc, info);
1350 TRACE("EMRSCALEVIEWPORTEXTEX %d/%d %d/%d\n",
1351 lpScaleViewportExtEx->xNum,lpScaleViewportExtEx->xDenom,
1352 lpScaleViewportExtEx->yNum,lpScaleViewportExtEx->yDenom);
1354 break;
1357 case EMR_SCALEWINDOWEXTEX:
1359 const EMRSCALEWINDOWEXTEX *lpScaleWindowExtEx = (const EMRSCALEWINDOWEXTEX *)mr;
1361 if ((info->mode != MM_ISOTROPIC) && (info->mode != MM_ANISOTROPIC))
1362 break;
1363 if (!lpScaleWindowExtEx->xNum || !lpScaleWindowExtEx->xDenom ||
1364 !lpScaleWindowExtEx->xNum || !lpScaleWindowExtEx->yDenom)
1365 break;
1366 info->wndExtX = MulDiv(info->wndExtX, lpScaleWindowExtEx->xNum,
1367 lpScaleWindowExtEx->xDenom);
1368 info->wndExtY = MulDiv(info->wndExtY, lpScaleWindowExtEx->yNum,
1369 lpScaleWindowExtEx->yDenom);
1370 if (info->wndExtX == 0) info->wndExtX = 1;
1371 if (info->wndExtY == 0) info->wndExtY = 1;
1372 if (info->mode == MM_ISOTROPIC)
1373 EMF_FixIsotropic(hdc, info);
1375 TRACE("EMRSCALEWINDOWEXTEX %d/%d %d/%d\n",
1376 lpScaleWindowExtEx->xNum,lpScaleWindowExtEx->xDenom,
1377 lpScaleWindowExtEx->yNum,lpScaleWindowExtEx->yDenom);
1379 break;
1382 case EMR_MODIFYWORLDTRANSFORM:
1384 const EMRMODIFYWORLDTRANSFORM *lpModifyWorldTrans = (const EMRMODIFYWORLDTRANSFORM *)mr;
1386 switch(lpModifyWorldTrans->iMode) {
1387 case MWT_IDENTITY:
1388 info->world_transform.eM11 = info->world_transform.eM22 = 1;
1389 info->world_transform.eM12 = info->world_transform.eM21 = 0;
1390 info->world_transform.eDx = info->world_transform.eDy = 0;
1391 break;
1392 case MWT_LEFTMULTIPLY:
1393 CombineTransform(&info->world_transform, &lpModifyWorldTrans->xform,
1394 &info->world_transform);
1395 break;
1396 case MWT_RIGHTMULTIPLY:
1397 CombineTransform(&info->world_transform, &info->world_transform,
1398 &lpModifyWorldTrans->xform);
1399 break;
1400 default:
1401 FIXME("Unknown imode %d\n", lpModifyWorldTrans->iMode);
1402 break;
1404 break;
1407 case EMR_ANGLEARC:
1409 const EMRANGLEARC *lpAngleArc = (const EMRANGLEARC *)mr;
1411 AngleArc( hdc,
1412 (INT)lpAngleArc->ptlCenter.x, (INT)lpAngleArc->ptlCenter.y,
1413 lpAngleArc->nRadius, lpAngleArc->eStartAngle,
1414 lpAngleArc->eSweepAngle );
1416 break;
1419 case EMR_ROUNDRECT:
1421 const EMRROUNDRECT *lpRoundRect = (const EMRROUNDRECT *)mr;
1423 RoundRect( hdc,
1424 lpRoundRect->rclBox.left,
1425 lpRoundRect->rclBox.top,
1426 lpRoundRect->rclBox.right,
1427 lpRoundRect->rclBox.bottom,
1428 lpRoundRect->szlCorner.cx,
1429 lpRoundRect->szlCorner.cy );
1431 break;
1434 case EMR_ARC:
1436 const EMRARC *lpArc = (const EMRARC *)mr;
1438 Arc( hdc,
1439 (INT)lpArc->rclBox.left,
1440 (INT)lpArc->rclBox.top,
1441 (INT)lpArc->rclBox.right,
1442 (INT)lpArc->rclBox.bottom,
1443 (INT)lpArc->ptlStart.x,
1444 (INT)lpArc->ptlStart.y,
1445 (INT)lpArc->ptlEnd.x,
1446 (INT)lpArc->ptlEnd.y );
1448 break;
1451 case EMR_CHORD:
1453 const EMRCHORD *lpChord = (const EMRCHORD *)mr;
1455 Chord( hdc,
1456 (INT)lpChord->rclBox.left,
1457 (INT)lpChord->rclBox.top,
1458 (INT)lpChord->rclBox.right,
1459 (INT)lpChord->rclBox.bottom,
1460 (INT)lpChord->ptlStart.x,
1461 (INT)lpChord->ptlStart.y,
1462 (INT)lpChord->ptlEnd.x,
1463 (INT)lpChord->ptlEnd.y );
1465 break;
1468 case EMR_PIE:
1470 const EMRPIE *lpPie = (const EMRPIE *)mr;
1472 Pie( hdc,
1473 (INT)lpPie->rclBox.left,
1474 (INT)lpPie->rclBox.top,
1475 (INT)lpPie->rclBox.right,
1476 (INT)lpPie->rclBox.bottom,
1477 (INT)lpPie->ptlStart.x,
1478 (INT)lpPie->ptlStart.y,
1479 (INT)lpPie->ptlEnd.x,
1480 (INT)lpPie->ptlEnd.y );
1482 break;
1485 case EMR_ARCTO:
1487 const EMRARC *lpArcTo = (const EMRARC *)mr;
1489 ArcTo( hdc,
1490 (INT)lpArcTo->rclBox.left,
1491 (INT)lpArcTo->rclBox.top,
1492 (INT)lpArcTo->rclBox.right,
1493 (INT)lpArcTo->rclBox.bottom,
1494 (INT)lpArcTo->ptlStart.x,
1495 (INT)lpArcTo->ptlStart.y,
1496 (INT)lpArcTo->ptlEnd.x,
1497 (INT)lpArcTo->ptlEnd.y );
1499 break;
1502 case EMR_EXTFLOODFILL:
1504 const EMREXTFLOODFILL *lpExtFloodFill = (const EMREXTFLOODFILL *)mr;
1506 ExtFloodFill( hdc,
1507 (INT)lpExtFloodFill->ptlStart.x,
1508 (INT)lpExtFloodFill->ptlStart.y,
1509 lpExtFloodFill->crColor,
1510 (UINT)lpExtFloodFill->iMode );
1512 break;
1515 case EMR_POLYDRAW:
1517 const EMRPOLYDRAW *lpPolyDraw = (const EMRPOLYDRAW *)mr;
1518 PolyDraw( hdc,
1519 (const POINT*)lpPolyDraw->aptl,
1520 lpPolyDraw->abTypes,
1521 (INT)lpPolyDraw->cptl );
1523 break;
1526 case EMR_SETARCDIRECTION:
1528 const EMRSETARCDIRECTION *lpSetArcDirection = (const EMRSETARCDIRECTION *)mr;
1529 SetArcDirection( hdc, (INT)lpSetArcDirection->iArcDirection );
1530 break;
1533 case EMR_SETMITERLIMIT:
1535 const EMRSETMITERLIMIT *lpSetMiterLimit = (const EMRSETMITERLIMIT *)mr;
1536 SetMiterLimit( hdc, lpSetMiterLimit->eMiterLimit, NULL );
1537 break;
1540 case EMR_BEGINPATH:
1542 BeginPath( hdc );
1543 break;
1546 case EMR_ENDPATH:
1548 EndPath( hdc );
1549 break;
1552 case EMR_CLOSEFIGURE:
1554 CloseFigure( hdc );
1555 break;
1558 case EMR_FILLPATH:
1560 /*const EMRFILLPATH lpFillPath = (const EMRFILLPATH *)mr;*/
1561 FillPath( hdc );
1562 break;
1565 case EMR_STROKEANDFILLPATH:
1567 /*const EMRSTROKEANDFILLPATH lpStrokeAndFillPath = (const EMRSTROKEANDFILLPATH *)mr;*/
1568 StrokeAndFillPath( hdc );
1569 break;
1572 case EMR_STROKEPATH:
1574 /*const EMRSTROKEPATH lpStrokePath = (const EMRSTROKEPATH *)mr;*/
1575 StrokePath( hdc );
1576 break;
1579 case EMR_FLATTENPATH:
1581 FlattenPath( hdc );
1582 break;
1585 case EMR_WIDENPATH:
1587 WidenPath( hdc );
1588 break;
1591 case EMR_SELECTCLIPPATH:
1593 const EMRSELECTCLIPPATH *lpSelectClipPath = (const EMRSELECTCLIPPATH *)mr;
1594 SelectClipPath( hdc, (INT)lpSelectClipPath->iMode );
1595 break;
1598 case EMR_ABORTPATH:
1600 AbortPath( hdc );
1601 break;
1604 case EMR_CREATECOLORSPACE:
1606 PEMRCREATECOLORSPACE lpCreateColorSpace = (PEMRCREATECOLORSPACE)mr;
1607 (handletable->objectHandle)[lpCreateColorSpace->ihCS] =
1608 CreateColorSpaceA( &lpCreateColorSpace->lcs );
1609 break;
1612 case EMR_SETCOLORSPACE:
1614 const EMRSETCOLORSPACE *lpSetColorSpace = (const EMRSETCOLORSPACE *)mr;
1615 SetColorSpace( hdc,
1616 (handletable->objectHandle)[lpSetColorSpace->ihCS] );
1617 break;
1620 case EMR_DELETECOLORSPACE:
1622 const EMRDELETECOLORSPACE *lpDeleteColorSpace = (const EMRDELETECOLORSPACE *)mr;
1623 DeleteColorSpace( (handletable->objectHandle)[lpDeleteColorSpace->ihCS] );
1624 break;
1627 case EMR_SETICMMODE:
1629 const EMRSETICMMODE *lpSetICMMode = (const EMRSETICMMODE *)mr;
1630 SetICMMode( hdc, (INT)lpSetICMMode->iMode );
1631 break;
1634 case EMR_PIXELFORMAT:
1636 INT iPixelFormat;
1637 const EMRPIXELFORMAT *lpPixelFormat = (const EMRPIXELFORMAT *)mr;
1639 iPixelFormat = ChoosePixelFormat( hdc, &lpPixelFormat->pfd );
1640 SetPixelFormat( hdc, iPixelFormat, &lpPixelFormat->pfd );
1642 break;
1645 case EMR_SETPALETTEENTRIES:
1647 const EMRSETPALETTEENTRIES *lpSetPaletteEntries = (const EMRSETPALETTEENTRIES *)mr;
1649 SetPaletteEntries( (handletable->objectHandle)[lpSetPaletteEntries->ihPal],
1650 (UINT)lpSetPaletteEntries->iStart,
1651 (UINT)lpSetPaletteEntries->cEntries,
1652 lpSetPaletteEntries->aPalEntries );
1654 break;
1657 case EMR_RESIZEPALETTE:
1659 const EMRRESIZEPALETTE *lpResizePalette = (const EMRRESIZEPALETTE *)mr;
1661 ResizePalette( (handletable->objectHandle)[lpResizePalette->ihPal],
1662 (UINT)lpResizePalette->cEntries );
1664 break;
1667 case EMR_CREATEDIBPATTERNBRUSHPT:
1669 const EMRCREATEDIBPATTERNBRUSHPT *lpCreate = (const EMRCREATEDIBPATTERNBRUSHPT *)mr;
1670 LPVOID lpPackedStruct;
1672 /* check that offsets and data are contained within the record */
1673 if ( !( (lpCreate->cbBmi>=0) && (lpCreate->cbBits>=0) &&
1674 (lpCreate->offBmi>=0) && (lpCreate->offBits>=0) &&
1675 ((lpCreate->offBmi +lpCreate->cbBmi ) <= mr->nSize) &&
1676 ((lpCreate->offBits+lpCreate->cbBits) <= mr->nSize) ) )
1678 ERR("Invalid EMR_CREATEDIBPATTERNBRUSHPT record\n");
1679 break;
1682 /* This is a BITMAPINFO struct followed directly by bitmap bits */
1683 lpPackedStruct = HeapAlloc( GetProcessHeap(), 0,
1684 lpCreate->cbBmi + lpCreate->cbBits );
1685 if(!lpPackedStruct)
1687 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1688 break;
1691 /* Now pack this structure */
1692 memcpy( lpPackedStruct,
1693 ((const BYTE *)lpCreate) + lpCreate->offBmi,
1694 lpCreate->cbBmi );
1695 memcpy( ((BYTE*)lpPackedStruct) + lpCreate->cbBmi,
1696 ((const BYTE *)lpCreate) + lpCreate->offBits,
1697 lpCreate->cbBits );
1699 (handletable->objectHandle)[lpCreate->ihBrush] =
1700 CreateDIBPatternBrushPt( lpPackedStruct,
1701 (UINT)lpCreate->iUsage );
1703 HeapFree(GetProcessHeap(), 0, lpPackedStruct);
1704 break;
1707 case EMR_CREATEMONOBRUSH:
1709 const EMRCREATEMONOBRUSH *pCreateMonoBrush = (const EMRCREATEMONOBRUSH *)mr;
1710 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pCreateMonoBrush->offBmi);
1711 HBITMAP hBmp;
1713 /* Need to check if the bitmap is monochrome, and if the
1714 two colors are really black and white */
1715 if (pCreateMonoBrush->iUsage == DIB_PAL_MONO)
1717 BITMAP bm;
1719 /* Undocumented iUsage indicates a mono bitmap with no palette table,
1720 * aligned to 32 rather than 16 bits.
1722 bm.bmType = 0;
1723 bm.bmWidth = pbi->bmiHeader.biWidth;
1724 bm.bmHeight = abs(pbi->bmiHeader.biHeight);
1725 bm.bmWidthBytes = 4 * ((pbi->bmiHeader.biWidth + 31) / 32);
1726 bm.bmPlanes = pbi->bmiHeader.biPlanes;
1727 bm.bmBitsPixel = pbi->bmiHeader.biBitCount;
1728 bm.bmBits = (BYTE *)mr + pCreateMonoBrush->offBits;
1729 hBmp = CreateBitmapIndirect(&bm);
1731 else if (is_dib_monochrome(pbi))
1733 /* Top-down DIBs have a negative height */
1734 LONG height = pbi->bmiHeader.biHeight;
1736 hBmp = CreateBitmap(pbi->bmiHeader.biWidth, abs(height), 1, 1, NULL);
1737 SetDIBits(hdc, hBmp, 0, pbi->bmiHeader.biHeight,
1738 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1740 else
1742 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1743 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1746 (handletable->objectHandle)[pCreateMonoBrush->ihBrush] = CreatePatternBrush(hBmp);
1748 /* CreatePatternBrush created a copy of the bitmap */
1749 DeleteObject(hBmp);
1750 break;
1753 case EMR_BITBLT:
1755 const EMRBITBLT *pBitBlt = (const EMRBITBLT *)mr;
1757 if(pBitBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1758 PatBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1759 pBitBlt->dwRop);
1760 } else { /* BitBlt */
1761 HDC hdcSrc = CreateCompatibleDC(hdc);
1762 HBRUSH hBrush, hBrushOld;
1763 HBITMAP hBmp = 0, hBmpOld = 0;
1764 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pBitBlt->offBmiSrc);
1766 SetWorldTransform(hdcSrc, &pBitBlt->xformSrc);
1768 hBrush = CreateSolidBrush(pBitBlt->crBkColorSrc);
1769 hBrushOld = SelectObject(hdcSrc, hBrush);
1770 PatBlt(hdcSrc, pBitBlt->rclBounds.left, pBitBlt->rclBounds.top,
1771 pBitBlt->rclBounds.right - pBitBlt->rclBounds.left,
1772 pBitBlt->rclBounds.bottom - pBitBlt->rclBounds.top, PATCOPY);
1773 SelectObject(hdcSrc, hBrushOld);
1774 DeleteObject(hBrush);
1776 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1777 (const BYTE *)mr + pBitBlt->offBitsSrc, pbi, pBitBlt->iUsageSrc);
1778 hBmpOld = SelectObject(hdcSrc, hBmp);
1780 BitBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1781 hdcSrc, pBitBlt->xSrc, pBitBlt->ySrc, pBitBlt->dwRop);
1783 SelectObject(hdcSrc, hBmpOld);
1784 DeleteObject(hBmp);
1785 DeleteDC(hdcSrc);
1787 break;
1790 case EMR_STRETCHBLT:
1792 const EMRSTRETCHBLT *pStretchBlt = (const EMRSTRETCHBLT *)mr;
1794 TRACE("EMR_STRETCHBLT: %d, %d %dx%d -> %d, %d %dx%d. rop %08x offBitsSrc %d\n",
1795 pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1796 pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1797 pStretchBlt->dwRop, pStretchBlt->offBitsSrc);
1799 if(pStretchBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1800 PatBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1801 pStretchBlt->dwRop);
1802 } else { /* StretchBlt */
1803 HDC hdcSrc = CreateCompatibleDC(hdc);
1804 HBRUSH hBrush, hBrushOld;
1805 HBITMAP hBmp = 0, hBmpOld = 0;
1806 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pStretchBlt->offBmiSrc);
1808 SetWorldTransform(hdcSrc, &pStretchBlt->xformSrc);
1810 hBrush = CreateSolidBrush(pStretchBlt->crBkColorSrc);
1811 hBrushOld = SelectObject(hdcSrc, hBrush);
1812 PatBlt(hdcSrc, pStretchBlt->rclBounds.left, pStretchBlt->rclBounds.top,
1813 pStretchBlt->rclBounds.right - pStretchBlt->rclBounds.left,
1814 pStretchBlt->rclBounds.bottom - pStretchBlt->rclBounds.top, PATCOPY);
1815 SelectObject(hdcSrc, hBrushOld);
1816 DeleteObject(hBrush);
1818 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1819 (const BYTE *)mr + pStretchBlt->offBitsSrc, pbi, pStretchBlt->iUsageSrc);
1820 hBmpOld = SelectObject(hdcSrc, hBmp);
1822 StretchBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1823 hdcSrc, pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1824 pStretchBlt->dwRop);
1826 SelectObject(hdcSrc, hBmpOld);
1827 DeleteObject(hBmp);
1828 DeleteDC(hdcSrc);
1830 break;
1833 case EMR_ALPHABLEND:
1835 const EMRALPHABLEND *pAlphaBlend = (const EMRALPHABLEND *)mr;
1837 TRACE("EMR_ALPHABLEND: %d, %d %dx%d -> %d, %d %dx%d. blendfn %08x offBitsSrc %d\n",
1838 pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1839 pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1840 pAlphaBlend->dwRop, pAlphaBlend->offBitsSrc);
1842 if(pAlphaBlend->offBmiSrc == 0) {
1843 FIXME("EMR_ALPHABLEND: offBmiSrc == 0\n");
1844 } else {
1845 HDC hdcSrc = CreateCompatibleDC(hdc);
1846 HBITMAP hBmp = 0, hBmpOld = 0;
1847 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pAlphaBlend->offBmiSrc);
1848 BLENDFUNCTION blendfn;
1849 void *bits;
1851 SetWorldTransform(hdcSrc, &pAlphaBlend->xformSrc);
1853 hBmp = CreateDIBSection(hdc, pbi, pAlphaBlend->iUsageSrc, &bits, NULL, 0);
1854 memcpy(bits, (const BYTE *)mr + pAlphaBlend->offBitsSrc, pAlphaBlend->cbBitsSrc);
1855 hBmpOld = SelectObject(hdcSrc, hBmp);
1857 blendfn.BlendOp = (pAlphaBlend->dwRop >> 24) & 0xff;
1858 blendfn.BlendFlags = (pAlphaBlend->dwRop >> 16) & 0xff;
1859 blendfn.SourceConstantAlpha = (pAlphaBlend->dwRop >> 8) & 0xff;
1860 blendfn.AlphaFormat = (pAlphaBlend->dwRop) & 0xff;
1862 GdiAlphaBlend(hdc, pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1863 hdcSrc, pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1864 blendfn);
1866 SelectObject(hdcSrc, hBmpOld);
1867 DeleteObject(hBmp);
1868 DeleteDC(hdcSrc);
1870 break;
1873 case EMR_MASKBLT:
1875 const EMRMASKBLT *pMaskBlt = (const EMRMASKBLT *)mr;
1876 HDC hdcSrc = CreateCompatibleDC(hdc);
1877 HBRUSH hBrush, hBrushOld;
1878 HBITMAP hBmp, hBmpOld, hBmpMask;
1879 const BITMAPINFO *pbi;
1881 SetWorldTransform(hdcSrc, &pMaskBlt->xformSrc);
1883 hBrush = CreateSolidBrush(pMaskBlt->crBkColorSrc);
1884 hBrushOld = SelectObject(hdcSrc, hBrush);
1885 PatBlt(hdcSrc, pMaskBlt->rclBounds.left, pMaskBlt->rclBounds.top,
1886 pMaskBlt->rclBounds.right - pMaskBlt->rclBounds.left,
1887 pMaskBlt->rclBounds.bottom - pMaskBlt->rclBounds.top, PATCOPY);
1888 SelectObject(hdcSrc, hBrushOld);
1889 DeleteObject(hBrush);
1891 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiMask);
1892 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
1893 1, 1, NULL);
1894 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
1895 (const BYTE *)mr + pMaskBlt->offBitsMask, pbi, pMaskBlt->iUsageMask);
1897 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiSrc);
1898 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1899 (const BYTE *)mr + pMaskBlt->offBitsSrc, pbi, pMaskBlt->iUsageSrc);
1900 hBmpOld = SelectObject(hdcSrc, hBmp);
1901 MaskBlt(hdc,
1902 pMaskBlt->xDest,
1903 pMaskBlt->yDest,
1904 pMaskBlt->cxDest,
1905 pMaskBlt->cyDest,
1906 hdcSrc,
1907 pMaskBlt->xSrc,
1908 pMaskBlt->ySrc,
1909 hBmpMask,
1910 pMaskBlt->xMask,
1911 pMaskBlt->yMask,
1912 pMaskBlt->dwRop);
1913 SelectObject(hdcSrc, hBmpOld);
1914 DeleteObject(hBmp);
1915 DeleteObject(hBmpMask);
1916 DeleteDC(hdcSrc);
1917 break;
1920 case EMR_PLGBLT:
1922 const EMRPLGBLT *pPlgBlt = (const EMRPLGBLT *)mr;
1923 HDC hdcSrc = CreateCompatibleDC(hdc);
1924 HBRUSH hBrush, hBrushOld;
1925 HBITMAP hBmp, hBmpOld, hBmpMask;
1926 const BITMAPINFO *pbi;
1927 POINT pts[3];
1929 SetWorldTransform(hdcSrc, &pPlgBlt->xformSrc);
1931 pts[0].x = pPlgBlt->aptlDest[0].x; pts[0].y = pPlgBlt->aptlDest[0].y;
1932 pts[1].x = pPlgBlt->aptlDest[1].x; pts[1].y = pPlgBlt->aptlDest[1].y;
1933 pts[2].x = pPlgBlt->aptlDest[2].x; pts[2].y = pPlgBlt->aptlDest[2].y;
1935 hBrush = CreateSolidBrush(pPlgBlt->crBkColorSrc);
1936 hBrushOld = SelectObject(hdcSrc, hBrush);
1937 PatBlt(hdcSrc, pPlgBlt->rclBounds.left, pPlgBlt->rclBounds.top,
1938 pPlgBlt->rclBounds.right - pPlgBlt->rclBounds.left,
1939 pPlgBlt->rclBounds.bottom - pPlgBlt->rclBounds.top, PATCOPY);
1940 SelectObject(hdcSrc, hBrushOld);
1941 DeleteObject(hBrush);
1943 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiMask);
1944 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
1945 1, 1, NULL);
1946 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
1947 (const BYTE *)mr + pPlgBlt->offBitsMask, pbi, pPlgBlt->iUsageMask);
1949 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiSrc);
1950 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1951 (const BYTE *)mr + pPlgBlt->offBitsSrc, pbi, pPlgBlt->iUsageSrc);
1952 hBmpOld = SelectObject(hdcSrc, hBmp);
1953 PlgBlt(hdc,
1954 pts,
1955 hdcSrc,
1956 pPlgBlt->xSrc,
1957 pPlgBlt->ySrc,
1958 pPlgBlt->cxSrc,
1959 pPlgBlt->cySrc,
1960 hBmpMask,
1961 pPlgBlt->xMask,
1962 pPlgBlt->yMask);
1963 SelectObject(hdcSrc, hBmpOld);
1964 DeleteObject(hBmp);
1965 DeleteObject(hBmpMask);
1966 DeleteDC(hdcSrc);
1967 break;
1970 case EMR_SETDIBITSTODEVICE:
1972 const EMRSETDIBITSTODEVICE *pSetDIBitsToDevice = (const EMRSETDIBITSTODEVICE *)mr;
1974 SetDIBitsToDevice(hdc,
1975 pSetDIBitsToDevice->xDest,
1976 pSetDIBitsToDevice->yDest,
1977 pSetDIBitsToDevice->cxSrc,
1978 pSetDIBitsToDevice->cySrc,
1979 pSetDIBitsToDevice->xSrc,
1980 pSetDIBitsToDevice->ySrc,
1981 pSetDIBitsToDevice->iStartScan,
1982 pSetDIBitsToDevice->cScans,
1983 (const BYTE *)mr + pSetDIBitsToDevice->offBitsSrc,
1984 (const BITMAPINFO *)((const BYTE *)mr + pSetDIBitsToDevice->offBmiSrc),
1985 pSetDIBitsToDevice->iUsageSrc);
1986 break;
1989 case EMR_POLYTEXTOUTA:
1991 const EMRPOLYTEXTOUTA *pPolyTextOutA = (const EMRPOLYTEXTOUTA *)mr;
1992 POLYTEXTA *polytextA = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutA->cStrings * sizeof(POLYTEXTA));
1993 LONG i;
1994 XFORM xform, xformOld;
1995 int gModeOld;
1997 gModeOld = SetGraphicsMode(hdc, pPolyTextOutA->iGraphicsMode);
1998 GetWorldTransform(hdc, &xformOld);
2000 xform.eM11 = pPolyTextOutA->exScale;
2001 xform.eM12 = 0.0;
2002 xform.eM21 = 0.0;
2003 xform.eM22 = pPolyTextOutA->eyScale;
2004 xform.eDx = 0.0;
2005 xform.eDy = 0.0;
2006 SetWorldTransform(hdc, &xform);
2008 /* Set up POLYTEXTA structures */
2009 for(i = 0; i < pPolyTextOutA->cStrings; i++)
2011 polytextA[i].x = pPolyTextOutA->aemrtext[i].ptlReference.x;
2012 polytextA[i].y = pPolyTextOutA->aemrtext[i].ptlReference.y;
2013 polytextA[i].n = pPolyTextOutA->aemrtext[i].nChars;
2014 polytextA[i].lpstr = (LPCSTR)((const BYTE *)mr + pPolyTextOutA->aemrtext[i].offString);
2015 polytextA[i].uiFlags = pPolyTextOutA->aemrtext[i].fOptions;
2016 polytextA[i].rcl.left = pPolyTextOutA->aemrtext[i].rcl.left;
2017 polytextA[i].rcl.right = pPolyTextOutA->aemrtext[i].rcl.right;
2018 polytextA[i].rcl.top = pPolyTextOutA->aemrtext[i].rcl.top;
2019 polytextA[i].rcl.bottom = pPolyTextOutA->aemrtext[i].rcl.bottom;
2020 polytextA[i].pdx = (int *)((BYTE *)mr + pPolyTextOutA->aemrtext[i].offDx);
2022 PolyTextOutA(hdc, polytextA, pPolyTextOutA->cStrings);
2023 HeapFree(GetProcessHeap(), 0, polytextA);
2025 SetWorldTransform(hdc, &xformOld);
2026 SetGraphicsMode(hdc, gModeOld);
2027 break;
2030 case EMR_POLYTEXTOUTW:
2032 const EMRPOLYTEXTOUTW *pPolyTextOutW = (const EMRPOLYTEXTOUTW *)mr;
2033 POLYTEXTW *polytextW = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutW->cStrings * sizeof(POLYTEXTW));
2034 LONG i;
2035 XFORM xform, xformOld;
2036 int gModeOld;
2038 gModeOld = SetGraphicsMode(hdc, pPolyTextOutW->iGraphicsMode);
2039 GetWorldTransform(hdc, &xformOld);
2041 xform.eM11 = pPolyTextOutW->exScale;
2042 xform.eM12 = 0.0;
2043 xform.eM21 = 0.0;
2044 xform.eM22 = pPolyTextOutW->eyScale;
2045 xform.eDx = 0.0;
2046 xform.eDy = 0.0;
2047 SetWorldTransform(hdc, &xform);
2049 /* Set up POLYTEXTW structures */
2050 for(i = 0; i < pPolyTextOutW->cStrings; i++)
2052 polytextW[i].x = pPolyTextOutW->aemrtext[i].ptlReference.x;
2053 polytextW[i].y = pPolyTextOutW->aemrtext[i].ptlReference.y;
2054 polytextW[i].n = pPolyTextOutW->aemrtext[i].nChars;
2055 polytextW[i].lpstr = (LPCWSTR)((const BYTE *)mr + pPolyTextOutW->aemrtext[i].offString);
2056 polytextW[i].uiFlags = pPolyTextOutW->aemrtext[i].fOptions;
2057 polytextW[i].rcl.left = pPolyTextOutW->aemrtext[i].rcl.left;
2058 polytextW[i].rcl.right = pPolyTextOutW->aemrtext[i].rcl.right;
2059 polytextW[i].rcl.top = pPolyTextOutW->aemrtext[i].rcl.top;
2060 polytextW[i].rcl.bottom = pPolyTextOutW->aemrtext[i].rcl.bottom;
2061 polytextW[i].pdx = (int *)((BYTE *)mr + pPolyTextOutW->aemrtext[i].offDx);
2063 PolyTextOutW(hdc, polytextW, pPolyTextOutW->cStrings);
2064 HeapFree(GetProcessHeap(), 0, polytextW);
2066 SetWorldTransform(hdc, &xformOld);
2067 SetGraphicsMode(hdc, gModeOld);
2068 break;
2071 case EMR_FILLRGN:
2073 const EMRFILLRGN *pFillRgn = (const EMRFILLRGN *)mr;
2074 HRGN hRgn = ExtCreateRegion(NULL, pFillRgn->cbRgnData, (RGNDATA *)pFillRgn->RgnData);
2075 FillRgn(hdc,
2076 hRgn,
2077 (handletable->objectHandle)[pFillRgn->ihBrush]);
2078 DeleteObject(hRgn);
2079 break;
2082 case EMR_FRAMERGN:
2084 const EMRFRAMERGN *pFrameRgn = (const EMRFRAMERGN *)mr;
2085 HRGN hRgn = ExtCreateRegion(NULL, pFrameRgn->cbRgnData, (RGNDATA *)pFrameRgn->RgnData);
2086 FrameRgn(hdc,
2087 hRgn,
2088 (handletable->objectHandle)[pFrameRgn->ihBrush],
2089 pFrameRgn->szlStroke.cx,
2090 pFrameRgn->szlStroke.cy);
2091 DeleteObject(hRgn);
2092 break;
2095 case EMR_INVERTRGN:
2097 const EMRINVERTRGN *pInvertRgn = (const EMRINVERTRGN *)mr;
2098 HRGN hRgn = ExtCreateRegion(NULL, pInvertRgn->cbRgnData, (RGNDATA *)pInvertRgn->RgnData);
2099 InvertRgn(hdc, hRgn);
2100 DeleteObject(hRgn);
2101 break;
2104 case EMR_PAINTRGN:
2106 const EMRPAINTRGN *pPaintRgn = (const EMRPAINTRGN *)mr;
2107 HRGN hRgn = ExtCreateRegion(NULL, pPaintRgn->cbRgnData, (RGNDATA *)pPaintRgn->RgnData);
2108 PaintRgn(hdc, hRgn);
2109 DeleteObject(hRgn);
2110 break;
2113 case EMR_SETTEXTJUSTIFICATION:
2115 const EMRSETTEXTJUSTIFICATION *pSetTextJust = (const EMRSETTEXTJUSTIFICATION *)mr;
2116 SetTextJustification(hdc, pSetTextJust->nBreakExtra, pSetTextJust->nBreakCount);
2117 break;
2120 case EMR_SETLAYOUT:
2122 const EMRSETLAYOUT *pSetLayout = (const EMRSETLAYOUT *)mr;
2123 SetLayout(hdc, pSetLayout->iMode);
2124 break;
2127 case EMR_POLYDRAW16:
2128 case EMR_GLSRECORD:
2129 case EMR_GLSBOUNDEDRECORD:
2130 case EMR_DRAWESCAPE :
2131 case EMR_EXTESCAPE:
2132 case EMR_STARTDOC:
2133 case EMR_SMALLTEXTOUT:
2134 case EMR_FORCEUFIMAPPING:
2135 case EMR_NAMEDESCAPE:
2136 case EMR_COLORCORRECTPALETTE:
2137 case EMR_SETICMPROFILEA:
2138 case EMR_SETICMPROFILEW:
2139 case EMR_TRANSPARENTBLT:
2140 case EMR_GRADIENTFILL:
2141 case EMR_SETLINKEDUFI:
2142 case EMR_COLORMATCHTOTARGETW:
2143 case EMR_CREATECOLORSPACEW:
2145 default:
2146 /* From docs: If PlayEnhMetaFileRecord doesn't recognize a
2147 record then ignore and return TRUE. */
2148 FIXME("type %d is unimplemented\n", type);
2149 break;
2151 tmprc.left = tmprc.top = 0;
2152 tmprc.right = tmprc.bottom = 1000;
2153 LPtoDP(hdc, (POINT*)&tmprc, 2);
2154 TRACE("L:0,0 - 1000,1000 -> D:%d,%d - %d,%d\n", tmprc.left,
2155 tmprc.top, tmprc.right, tmprc.bottom);
2157 if ( !IS_WIN9X() )
2159 /* WinNT - update the transform (win9x updates when the next graphics output
2160 record is played). */
2161 EMF_Update_MF_Xform(hdc, info);
2164 return TRUE;
2168 /*****************************************************************************
2170 * EnumEnhMetaFile (GDI32.@)
2172 * Walk an enhanced metafile, calling a user-specified function _EnhMetaFunc_
2173 * for each
2174 * record. Returns when either every record has been used or
2175 * when _EnhMetaFunc_ returns FALSE.
2178 * RETURNS
2179 * TRUE if every record is used, FALSE if any invocation of _EnhMetaFunc_
2180 * returns FALSE.
2182 * BUGS
2183 * Ignores rect.
2185 * NOTES
2186 * This function behaves differently in Win9x and WinNT.
2188 * In WinNT, the DC's world transform is updated as the EMF changes
2189 * the Window/Viewport Extent and Origin or it's world transform.
2190 * The actual Window/Viewport Extent and Origin are left untouched.
2192 * In Win9x, the DC is left untouched, and PlayEnhMetaFileRecord
2193 * updates the scaling itself but only just before a record that
2194 * writes anything to the DC.
2196 * I'm not sure where the data (enum_emh_data) is stored in either
2197 * version. For this implementation, it is stored before the handle
2198 * table, but it could be stored in the DC, in the EMF handle or in
2199 * TLS.
2200 * MJM 5 Oct 2002
2202 BOOL WINAPI EnumEnhMetaFile(
2203 HDC hdc, /* [in] device context to pass to _EnhMetaFunc_ */
2204 HENHMETAFILE hmf, /* [in] EMF to walk */
2205 ENHMFENUMPROC callback, /* [in] callback function */
2206 LPVOID data, /* [in] optional data for callback function */
2207 const RECT *lpRect /* [in] bounding rectangle for rendered metafile */
2210 BOOL ret;
2211 ENHMETAHEADER *emh;
2212 ENHMETARECORD *emr;
2213 DWORD offset;
2214 UINT i;
2215 HANDLETABLE *ht;
2216 INT savedMode = 0;
2217 XFORM savedXform;
2218 HPEN hPen = NULL;
2219 HBRUSH hBrush = NULL;
2220 HFONT hFont = NULL;
2221 enum_emh_data *info;
2222 SIZE vp_size, win_size;
2223 POINT vp_org, win_org;
2224 INT mapMode = MM_TEXT, old_align = 0, old_rop2 = 0, old_arcdir = 0, old_polyfill = 0, old_stretchblt = 0;
2225 COLORREF old_text_color = 0, old_bk_color = 0;
2227 if(!lpRect && hdc)
2229 SetLastError(ERROR_INVALID_PARAMETER);
2230 return FALSE;
2233 emh = EMF_GetEnhMetaHeader(hmf);
2234 if(!emh) {
2235 SetLastError(ERROR_INVALID_HANDLE);
2236 return FALSE;
2239 info = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
2240 sizeof (enum_emh_data) + sizeof(HANDLETABLE) * emh->nHandles );
2241 if(!info)
2243 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2244 return FALSE;
2246 info->wndOrgX = 0;
2247 info->wndOrgY = 0;
2248 info->wndExtX = 1;
2249 info->wndExtY = 1;
2250 info->vportOrgX = 0;
2251 info->vportOrgY = 0;
2252 info->vportExtX = 1;
2253 info->vportExtY = 1;
2254 info->world_transform.eM11 = info->world_transform.eM22 = 1;
2255 info->world_transform.eM12 = info->world_transform.eM21 = 0;
2256 info->world_transform.eDx = info->world_transform.eDy = 0;
2258 ht = (HANDLETABLE*) &info[1];
2259 ht->objectHandle[0] = hmf;
2261 if(hdc)
2263 savedMode = SetGraphicsMode(hdc, GM_ADVANCED);
2264 GetWorldTransform(hdc, &savedXform);
2265 GetViewportExtEx(hdc, &vp_size);
2266 GetWindowExtEx(hdc, &win_size);
2267 GetViewportOrgEx(hdc, &vp_org);
2268 GetWindowOrgEx(hdc, &win_org);
2269 mapMode = GetMapMode(hdc);
2271 /* save the current pen, brush and font */
2272 hPen = GetCurrentObject(hdc, OBJ_PEN);
2273 hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
2274 hFont = GetCurrentObject(hdc, OBJ_FONT);
2276 old_text_color = SetTextColor(hdc, RGB(0,0,0));
2277 old_bk_color = SetBkColor(hdc, RGB(0xff, 0xff, 0xff));
2278 old_align = SetTextAlign(hdc, 0);
2279 old_rop2 = SetROP2(hdc, R2_COPYPEN);
2280 old_arcdir = SetArcDirection(hdc, AD_COUNTERCLOCKWISE);
2281 old_polyfill = SetPolyFillMode(hdc, ALTERNATE);
2282 old_stretchblt = SetStretchBltMode(hdc, BLACKONWHITE);
2285 info->mode = MM_TEXT;
2287 if ( IS_WIN9X() )
2289 /* Win95 leaves the vp/win ext/org info alone */
2290 info->init_transform.eM11 = 1.0;
2291 info->init_transform.eM12 = 0.0;
2292 info->init_transform.eM21 = 0.0;
2293 info->init_transform.eM22 = 1.0;
2294 info->init_transform.eDx = 0.0;
2295 info->init_transform.eDy = 0.0;
2297 else
2299 /* WinNT combines the vp/win ext/org info into a transform */
2300 FLOAT xscale, yscale;
2301 xscale = (FLOAT)vp_size.cx / (FLOAT)win_size.cx;
2302 yscale = (FLOAT)vp_size.cy / (FLOAT)win_size.cy;
2303 info->init_transform.eM11 = xscale;
2304 info->init_transform.eM12 = 0.0;
2305 info->init_transform.eM21 = 0.0;
2306 info->init_transform.eM22 = yscale;
2307 info->init_transform.eDx = (FLOAT)vp_org.x - xscale * (FLOAT)win_org.x;
2308 info->init_transform.eDy = (FLOAT)vp_org.y - yscale * (FLOAT)win_org.y;
2310 CombineTransform(&info->init_transform, &savedXform, &info->init_transform);
2313 if ( lpRect && WIDTH(emh->rclFrame) && HEIGHT(emh->rclFrame) )
2315 FLOAT xSrcPixSize, ySrcPixSize, xscale, yscale;
2316 XFORM xform;
2318 TRACE("rect: %d,%d - %d,%d. rclFrame: %d,%d - %d,%d\n",
2319 lpRect->left, lpRect->top, lpRect->right, lpRect->bottom,
2320 emh->rclFrame.left, emh->rclFrame.top, emh->rclFrame.right,
2321 emh->rclFrame.bottom);
2323 xSrcPixSize = (FLOAT) emh->szlMillimeters.cx / emh->szlDevice.cx;
2324 ySrcPixSize = (FLOAT) emh->szlMillimeters.cy / emh->szlDevice.cy;
2325 xscale = (FLOAT) WIDTH(*lpRect) * 100.0 /
2326 WIDTH(emh->rclFrame) * xSrcPixSize;
2327 yscale = (FLOAT) HEIGHT(*lpRect) * 100.0 /
2328 HEIGHT(emh->rclFrame) * ySrcPixSize;
2329 TRACE("xscale = %f, yscale = %f\n", xscale, yscale);
2331 xform.eM11 = xscale;
2332 xform.eM12 = 0;
2333 xform.eM21 = 0;
2334 xform.eM22 = yscale;
2335 xform.eDx = (FLOAT) lpRect->left - (FLOAT) WIDTH(*lpRect) / WIDTH(emh->rclFrame) * emh->rclFrame.left;
2336 xform.eDy = (FLOAT) lpRect->top - (FLOAT) HEIGHT(*lpRect) / HEIGHT(emh->rclFrame) * emh->rclFrame.top;
2338 CombineTransform(&info->init_transform, &xform, &info->init_transform);
2341 /* WinNT resets the current vp/win org/ext */
2342 if ( !IS_WIN9X() && hdc )
2344 SetMapMode(hdc, MM_TEXT);
2345 SetWindowOrgEx(hdc, 0, 0, NULL);
2346 SetViewportOrgEx(hdc, 0, 0, NULL);
2347 EMF_Update_MF_Xform(hdc, info);
2350 ret = TRUE;
2351 offset = 0;
2352 while(ret && offset < emh->nBytes)
2354 emr = (ENHMETARECORD *)((char *)emh + offset);
2355 TRACE("Calling EnumFunc with record %s, size %d\n", get_emr_name(emr->iType), emr->nSize);
2356 ret = (*callback)(hdc, ht, emr, emh->nHandles, (LPARAM)data);
2357 offset += emr->nSize;
2360 if (hdc)
2362 SetStretchBltMode(hdc, old_stretchblt);
2363 SetPolyFillMode(hdc, old_polyfill);
2364 SetArcDirection(hdc, old_arcdir);
2365 SetROP2(hdc, old_rop2);
2366 SetTextAlign(hdc, old_align);
2367 SetBkColor(hdc, old_bk_color);
2368 SetTextColor(hdc, old_text_color);
2370 /* restore pen, brush and font */
2371 SelectObject(hdc, hBrush);
2372 SelectObject(hdc, hPen);
2373 SelectObject(hdc, hFont);
2375 SetWorldTransform(hdc, &savedXform);
2376 if (savedMode)
2377 SetGraphicsMode(hdc, savedMode);
2378 SetMapMode(hdc, mapMode);
2379 SetWindowOrgEx(hdc, win_org.x, win_org.y, NULL);
2380 SetWindowExtEx(hdc, win_size.cx, win_size.cy, NULL);
2381 SetViewportOrgEx(hdc, vp_org.x, vp_org.y, NULL);
2382 SetViewportExtEx(hdc, vp_size.cx, vp_size.cy, NULL);
2385 for(i = 1; i < emh->nHandles; i++) /* Don't delete element 0 (hmf) */
2386 if( (ht->objectHandle)[i] )
2387 DeleteObject( (ht->objectHandle)[i] );
2389 HeapFree( GetProcessHeap(), 0, info );
2390 return ret;
2393 static INT CALLBACK EMF_PlayEnhMetaFileCallback(HDC hdc, HANDLETABLE *ht,
2394 const ENHMETARECORD *emr,
2395 INT handles, LPARAM data)
2397 return PlayEnhMetaFileRecord(hdc, ht, emr, handles);
2400 /**************************************************************************
2401 * PlayEnhMetaFile (GDI32.@)
2403 * Renders an enhanced metafile into a specified rectangle *lpRect
2404 * in device context hdc.
2406 * RETURNS
2407 * Success: TRUE
2408 * Failure: FALSE
2410 BOOL WINAPI PlayEnhMetaFile(
2411 HDC hdc, /* [in] DC to render into */
2412 HENHMETAFILE hmf, /* [in] metafile to render */
2413 const RECT *lpRect /* [in] rectangle to place metafile inside */
2416 return EnumEnhMetaFile(hdc, hmf, EMF_PlayEnhMetaFileCallback, NULL,
2417 lpRect);
2420 /*****************************************************************************
2421 * DeleteEnhMetaFile (GDI32.@)
2423 * Deletes an enhanced metafile and frees the associated storage.
2425 BOOL WINAPI DeleteEnhMetaFile(HENHMETAFILE hmf)
2427 return EMF_Delete_HENHMETAFILE( hmf );
2430 /*****************************************************************************
2431 * CopyEnhMetaFileA (GDI32.@)
2433 * Duplicate an enhanced metafile.
2437 HENHMETAFILE WINAPI CopyEnhMetaFileA(
2438 HENHMETAFILE hmfSrc,
2439 LPCSTR file)
2441 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2442 HENHMETAFILE hmfDst;
2444 if(!emrSrc) return FALSE;
2445 if (!file) {
2446 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2447 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2448 hmfDst = EMF_Create_HENHMETAFILE( emrDst, FALSE );
2449 } else {
2450 HANDLE hFile;
2451 DWORD w;
2452 hFile = CreateFileA( file, GENERIC_WRITE | GENERIC_READ, 0,
2453 NULL, CREATE_ALWAYS, 0, 0);
2454 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2455 CloseHandle( hFile );
2456 /* Reopen file for reading only, so that apps can share
2457 read access to the file while hmf is still valid */
2458 hFile = CreateFileA( file, GENERIC_READ, FILE_SHARE_READ,
2459 NULL, OPEN_EXISTING, 0, 0);
2460 if(hFile == INVALID_HANDLE_VALUE) {
2461 ERR("Can't reopen emf for reading\n");
2462 return 0;
2464 hmfDst = EMF_GetEnhMetaFile( hFile );
2465 CloseHandle( hFile );
2467 return hmfDst;
2470 /*****************************************************************************
2471 * CopyEnhMetaFileW (GDI32.@)
2473 * See CopyEnhMetaFileA.
2477 HENHMETAFILE WINAPI CopyEnhMetaFileW(
2478 HENHMETAFILE hmfSrc,
2479 LPCWSTR file)
2481 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2482 HENHMETAFILE hmfDst;
2484 if(!emrSrc) return FALSE;
2485 if (!file) {
2486 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2487 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2488 hmfDst = EMF_Create_HENHMETAFILE( emrDst, FALSE );
2489 } else {
2490 HANDLE hFile;
2491 DWORD w;
2492 hFile = CreateFileW( file, GENERIC_WRITE | GENERIC_READ, 0,
2493 NULL, CREATE_ALWAYS, 0, 0);
2494 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2495 CloseHandle( hFile );
2496 /* Reopen file for reading only, so that apps can share
2497 read access to the file while hmf is still valid */
2498 hFile = CreateFileW( file, GENERIC_READ, FILE_SHARE_READ,
2499 NULL, OPEN_EXISTING, 0, 0);
2500 if(hFile == INVALID_HANDLE_VALUE) {
2501 ERR("Can't reopen emf for reading\n");
2502 return 0;
2504 hmfDst = EMF_GetEnhMetaFile( hFile );
2505 CloseHandle( hFile );
2507 return hmfDst;
2511 /* Struct to be used to be passed in the LPVOID parameter for cbEnhPaletteCopy */
2512 typedef struct tagEMF_PaletteCopy
2514 UINT cEntries;
2515 LPPALETTEENTRY lpPe;
2516 } EMF_PaletteCopy;
2518 /***************************************************************
2519 * Find the EMR_EOF record and then use it to find the
2520 * palette entries for this enhanced metafile.
2521 * The lpData is actually a pointer to an EMF_PaletteCopy struct
2522 * which contains the max number of elements to copy and where
2523 * to copy them to.
2525 * NOTE: To be used by GetEnhMetaFilePaletteEntries only!
2527 static INT CALLBACK cbEnhPaletteCopy( HDC a,
2528 HANDLETABLE *b,
2529 const ENHMETARECORD *lpEMR,
2530 INT c,
2531 LPARAM lpData )
2534 if ( lpEMR->iType == EMR_EOF )
2536 const EMREOF *lpEof = (const EMREOF *)lpEMR;
2537 EMF_PaletteCopy* info = (EMF_PaletteCopy*)lpData;
2538 DWORD dwNumPalToCopy = min( lpEof->nPalEntries, info->cEntries );
2540 TRACE( "copying 0x%08x palettes\n", dwNumPalToCopy );
2542 memcpy( (LPVOID)info->lpPe,
2543 (LPCVOID)(((LPCSTR)lpEof) + lpEof->offPalEntries),
2544 sizeof( *(info->lpPe) ) * dwNumPalToCopy );
2546 /* Update the passed data as a return code */
2547 info->lpPe = NULL; /* Palettes were copied! */
2548 info->cEntries = (UINT)dwNumPalToCopy;
2550 return FALSE; /* That's all we need */
2553 return TRUE;
2556 /*****************************************************************************
2557 * GetEnhMetaFilePaletteEntries (GDI32.@)
2559 * Copy the palette and report size
2561 * BUGS: Error codes (SetLastError) are not set on failures
2563 UINT WINAPI GetEnhMetaFilePaletteEntries( HENHMETAFILE hEmf,
2564 UINT cEntries,
2565 LPPALETTEENTRY lpPe )
2567 ENHMETAHEADER* enhHeader = EMF_GetEnhMetaHeader( hEmf );
2568 EMF_PaletteCopy infoForCallBack;
2570 TRACE( "(%p,%d,%p)\n", hEmf, cEntries, lpPe );
2572 if (!enhHeader) return 0;
2574 /* First check if there are any palettes associated with
2575 this metafile. */
2576 if ( enhHeader->nPalEntries == 0 ) return 0;
2578 /* Is the user requesting the number of palettes? */
2579 if ( lpPe == NULL ) return (UINT)enhHeader->nPalEntries;
2581 /* Copy cEntries worth of PALETTEENTRY structs into the buffer */
2582 infoForCallBack.cEntries = cEntries;
2583 infoForCallBack.lpPe = lpPe;
2585 if ( !EnumEnhMetaFile( 0, hEmf, cbEnhPaletteCopy,
2586 &infoForCallBack, 0 ) )
2587 return GDI_ERROR;
2589 /* Verify that the callback executed correctly */
2590 if ( infoForCallBack.lpPe != NULL )
2592 /* Callback proc had error! */
2593 ERR( "cbEnhPaletteCopy didn't execute correctly\n" );
2594 return GDI_ERROR;
2597 return infoForCallBack.cEntries;
2600 typedef struct gdi_mf_comment
2602 DWORD ident;
2603 DWORD iComment;
2604 DWORD nVersion;
2605 DWORD nChecksum;
2606 DWORD fFlags;
2607 DWORD cbWinMetaFile;
2608 } gdi_mf_comment;
2610 /******************************************************************
2611 * SetWinMetaFileBits (GDI32.@)
2613 * Translate from old style to new style.
2616 HENHMETAFILE WINAPI SetWinMetaFileBits(UINT cbBuffer,
2617 CONST BYTE *lpbBuffer,
2618 HDC hdcRef,
2619 CONST METAFILEPICT *lpmfp
2622 static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
2623 HMETAFILE hmf = NULL;
2624 HENHMETAFILE ret = NULL;
2625 HDC hdc = NULL, hdcdisp = NULL;
2626 RECT rc, *prcFrame = NULL;
2627 gdi_mf_comment *mfcomment;
2628 UINT mfcomment_size;
2629 LONG mm, xExt, yExt;
2631 TRACE("(%d, %p, %p, %p)\n", cbBuffer, lpbBuffer, hdcRef, lpmfp);
2633 hmf = SetMetaFileBitsEx(cbBuffer, lpbBuffer);
2634 if(!hmf)
2636 WARN("SetMetaFileBitsEx failed\n");
2637 return NULL;
2640 if(!hdcRef)
2641 hdcRef = hdcdisp = CreateDCW(szDisplayW, NULL, NULL, NULL);
2643 if (lpmfp)
2645 TRACE("mm = %d %dx%d\n", lpmfp->mm, lpmfp->xExt, lpmfp->yExt);
2647 mm = lpmfp->mm;
2648 xExt = lpmfp->xExt;
2649 yExt = lpmfp->yExt;
2651 else
2653 TRACE("lpmfp == NULL\n");
2655 /* Use the whole device surface */
2656 mm = MM_ANISOTROPIC;
2657 xExt = 0;
2658 yExt = 0;
2661 if (mm == MM_ISOTROPIC || mm == MM_ANISOTROPIC)
2663 if (xExt < 0 || yExt < 0)
2665 /* Use the whole device surface */
2666 xExt = 0;
2667 yExt = 0;
2670 /* Use the x and y extents as the frame box */
2671 if (xExt && yExt)
2673 rc.left = rc.top = 0;
2674 rc.right = xExt;
2675 rc.bottom = yExt;
2676 prcFrame = &rc;
2680 if(!(hdc = CreateEnhMetaFileW(hdcRef, NULL, prcFrame, NULL)))
2682 ERR("CreateEnhMetaFile failed\n");
2683 goto end;
2687 * Write the original METAFILE into the enhanced metafile.
2688 * It is encapsulated in a GDICOMMENT_WINDOWS_METAFILE record.
2690 mfcomment_size = sizeof (gdi_mf_comment) + cbBuffer;
2691 mfcomment = HeapAlloc(GetProcessHeap(), 0, mfcomment_size);
2692 if(mfcomment)
2694 mfcomment->ident = GDICOMMENT_IDENTIFIER;
2695 mfcomment->iComment = GDICOMMENT_WINDOWS_METAFILE;
2696 mfcomment->nVersion = 0x00000300;
2697 mfcomment->nChecksum = 0; /* FIXME */
2698 mfcomment->fFlags = 0;
2699 mfcomment->cbWinMetaFile = cbBuffer;
2700 memcpy(&mfcomment[1], lpbBuffer, cbBuffer);
2701 GdiComment(hdc, mfcomment_size, (BYTE*) mfcomment);
2702 HeapFree(GetProcessHeap(), 0, mfcomment);
2705 if (mm != MM_TEXT)
2706 SetMapMode(hdc, mm);
2708 if (mm == MM_ISOTROPIC || mm == MM_ANISOTROPIC)
2710 INT horzsize, vertsize, horzres, vertres;
2712 horzsize = GetDeviceCaps(hdcRef, HORZSIZE);
2713 vertsize = GetDeviceCaps(hdcRef, VERTSIZE);
2714 horzres = GetDeviceCaps(hdcRef, HORZRES);
2715 vertres = GetDeviceCaps(hdcRef, VERTRES);
2717 if (!xExt || !yExt)
2719 /* Use the whole device surface */
2720 xExt = horzres;
2721 yExt = vertres;
2723 else
2725 xExt = MulDiv(xExt, horzres, 100 * horzsize);
2726 yExt = MulDiv(yExt, vertres, 100 * vertsize);
2729 /* set the initial viewport:window ratio as 1:1 */
2730 SetViewportExtEx(hdc, xExt, yExt, NULL);
2731 SetWindowExtEx(hdc, xExt, yExt, NULL);
2734 PlayMetaFile(hdc, hmf);
2736 ret = CloseEnhMetaFile(hdc);
2737 end:
2738 if (hdcdisp) DeleteDC(hdcdisp);
2739 DeleteMetaFile(hmf);
2740 return ret;