mshtml: Use wide-char string literals.
[wine.git] / dlls / gdi32 / enhmetafile.c
blobf515a444c90b219cd11b2c45712a17cbb29df3aa
1 /*
2 * Enhanced metafile functions
3 * Copyright 1998 Douglas Ridgway
4 * 1999 Huw D M Davies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * NOTES:
22 * The enhanced format consists of the following elements:
24 * A header
25 * A table of handles to GDI objects
26 * An array of metafile records
27 * A private palette
30 * The standard format consists of a header and an array of metafile records.
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <assert.h>
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wingdi.h"
41 #include "winnls.h"
42 #include "winerror.h"
43 #include "gdi_private.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
48 typedef struct
50 ENHMETAHEADER *emh;
51 BOOL on_disk; /* true if metafile is on disk */
52 } ENHMETAFILEOBJ;
54 static const struct emr_name {
55 DWORD type;
56 const char *name;
57 } emr_names[] = {
58 #define X(p) {p, #p}
59 X(EMR_HEADER),
60 X(EMR_POLYBEZIER),
61 X(EMR_POLYGON),
62 X(EMR_POLYLINE),
63 X(EMR_POLYBEZIERTO),
64 X(EMR_POLYLINETO),
65 X(EMR_POLYPOLYLINE),
66 X(EMR_POLYPOLYGON),
67 X(EMR_SETWINDOWEXTEX),
68 X(EMR_SETWINDOWORGEX),
69 X(EMR_SETVIEWPORTEXTEX),
70 X(EMR_SETVIEWPORTORGEX),
71 X(EMR_SETBRUSHORGEX),
72 X(EMR_EOF),
73 X(EMR_SETPIXELV),
74 X(EMR_SETMAPPERFLAGS),
75 X(EMR_SETMAPMODE),
76 X(EMR_SETBKMODE),
77 X(EMR_SETPOLYFILLMODE),
78 X(EMR_SETROP2),
79 X(EMR_SETSTRETCHBLTMODE),
80 X(EMR_SETTEXTALIGN),
81 X(EMR_SETCOLORADJUSTMENT),
82 X(EMR_SETTEXTCOLOR),
83 X(EMR_SETBKCOLOR),
84 X(EMR_OFFSETCLIPRGN),
85 X(EMR_MOVETOEX),
86 X(EMR_SETMETARGN),
87 X(EMR_EXCLUDECLIPRECT),
88 X(EMR_INTERSECTCLIPRECT),
89 X(EMR_SCALEVIEWPORTEXTEX),
90 X(EMR_SCALEWINDOWEXTEX),
91 X(EMR_SAVEDC),
92 X(EMR_RESTOREDC),
93 X(EMR_SETWORLDTRANSFORM),
94 X(EMR_MODIFYWORLDTRANSFORM),
95 X(EMR_SELECTOBJECT),
96 X(EMR_CREATEPEN),
97 X(EMR_CREATEBRUSHINDIRECT),
98 X(EMR_DELETEOBJECT),
99 X(EMR_ANGLEARC),
100 X(EMR_ELLIPSE),
101 X(EMR_RECTANGLE),
102 X(EMR_ROUNDRECT),
103 X(EMR_ARC),
104 X(EMR_CHORD),
105 X(EMR_PIE),
106 X(EMR_SELECTPALETTE),
107 X(EMR_CREATEPALETTE),
108 X(EMR_SETPALETTEENTRIES),
109 X(EMR_RESIZEPALETTE),
110 X(EMR_REALIZEPALETTE),
111 X(EMR_EXTFLOODFILL),
112 X(EMR_LINETO),
113 X(EMR_ARCTO),
114 X(EMR_POLYDRAW),
115 X(EMR_SETARCDIRECTION),
116 X(EMR_SETMITERLIMIT),
117 X(EMR_BEGINPATH),
118 X(EMR_ENDPATH),
119 X(EMR_CLOSEFIGURE),
120 X(EMR_FILLPATH),
121 X(EMR_STROKEANDFILLPATH),
122 X(EMR_STROKEPATH),
123 X(EMR_FLATTENPATH),
124 X(EMR_WIDENPATH),
125 X(EMR_SELECTCLIPPATH),
126 X(EMR_ABORTPATH),
127 X(EMR_GDICOMMENT),
128 X(EMR_FILLRGN),
129 X(EMR_FRAMERGN),
130 X(EMR_INVERTRGN),
131 X(EMR_PAINTRGN),
132 X(EMR_EXTSELECTCLIPRGN),
133 X(EMR_BITBLT),
134 X(EMR_STRETCHBLT),
135 X(EMR_MASKBLT),
136 X(EMR_PLGBLT),
137 X(EMR_SETDIBITSTODEVICE),
138 X(EMR_STRETCHDIBITS),
139 X(EMR_EXTCREATEFONTINDIRECTW),
140 X(EMR_EXTTEXTOUTA),
141 X(EMR_EXTTEXTOUTW),
142 X(EMR_POLYBEZIER16),
143 X(EMR_POLYGON16),
144 X(EMR_POLYLINE16),
145 X(EMR_POLYBEZIERTO16),
146 X(EMR_POLYLINETO16),
147 X(EMR_POLYPOLYLINE16),
148 X(EMR_POLYPOLYGON16),
149 X(EMR_POLYDRAW16),
150 X(EMR_CREATEMONOBRUSH),
151 X(EMR_CREATEDIBPATTERNBRUSHPT),
152 X(EMR_EXTCREATEPEN),
153 X(EMR_POLYTEXTOUTA),
154 X(EMR_POLYTEXTOUTW),
155 X(EMR_SETICMMODE),
156 X(EMR_CREATECOLORSPACE),
157 X(EMR_SETCOLORSPACE),
158 X(EMR_DELETECOLORSPACE),
159 X(EMR_GLSRECORD),
160 X(EMR_GLSBOUNDEDRECORD),
161 X(EMR_PIXELFORMAT),
162 X(EMR_DRAWESCAPE),
163 X(EMR_EXTESCAPE),
164 X(EMR_STARTDOC),
165 X(EMR_SMALLTEXTOUT),
166 X(EMR_FORCEUFIMAPPING),
167 X(EMR_NAMEDESCAPE),
168 X(EMR_COLORCORRECTPALETTE),
169 X(EMR_SETICMPROFILEA),
170 X(EMR_SETICMPROFILEW),
171 X(EMR_ALPHABLEND),
172 X(EMR_SETLAYOUT),
173 X(EMR_TRANSPARENTBLT),
174 X(EMR_RESERVED_117),
175 X(EMR_GRADIENTFILL),
176 X(EMR_SETLINKEDUFI),
177 X(EMR_SETTEXTJUSTIFICATION),
178 X(EMR_COLORMATCHTOTARGETW),
179 X(EMR_CREATECOLORSPACEW)
180 #undef X
183 /****************************************************************************
184 * get_emr_name
186 static const char *get_emr_name(DWORD type)
188 unsigned int i;
189 for(i = 0; i < ARRAY_SIZE(emr_names); i++)
190 if(type == emr_names[i].type) return emr_names[i].name;
191 TRACE("Unknown record type %d\n", type);
192 return NULL;
195 /***********************************************************************
196 * is_dib_monochrome
198 * Returns whether a DIB can be converted to a monochrome DDB.
200 * A DIB can be converted if its color table contains only black and
201 * white. Black must be the first color in the color table.
203 * Note : If the first color in the color table is white followed by
204 * black, we can't convert it to a monochrome DDB with
205 * SetDIBits, because black and white would be inverted.
207 static inline BOOL is_dib_monochrome( const BITMAPINFO* info )
209 if (info->bmiHeader.biBitCount != 1) return FALSE;
211 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
213 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO *) info)->bmciColors;
215 /* Check if the first color is black */
216 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
218 rgb++;
219 /* Check if the second color is white */
220 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
221 && (rgb->rgbtBlue == 0xff));
223 else return FALSE;
225 else /* assume BITMAPINFOHEADER */
227 const RGBQUAD *rgb = info->bmiColors;
229 /* Check if the first color is black */
230 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
231 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
233 rgb++;
235 /* Check if the second color is white */
236 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
237 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
239 else return FALSE;
243 /****************************************************************************
244 * EMF_Create_HENHMETAFILE
246 HENHMETAFILE EMF_Create_HENHMETAFILE(ENHMETAHEADER *emh, DWORD filesize, BOOL on_disk )
248 HENHMETAFILE hmf;
249 ENHMETAFILEOBJ *metaObj;
251 if (filesize < sizeof(*emh))
253 WARN("File too small for emf header\n");
254 return 0;
256 if (emh->iType != EMR_HEADER)
258 SetLastError(ERROR_INVALID_DATA);
259 return 0;
261 if (emh->dSignature != ENHMETA_SIGNATURE ||
262 (emh->nBytes & 3)) /* refuse to load unaligned EMF as Windows does */
264 WARN("Invalid emf header type 0x%08x sig 0x%08x.\n",
265 emh->iType, emh->dSignature);
266 return 0;
268 if (filesize < emh->nBytes)
270 WARN("File truncated (got %u bytes, header says %u)\n", emh->nBytes, filesize);
271 return 0;
274 if (!(metaObj = HeapAlloc( GetProcessHeap(), 0, sizeof(*metaObj) ))) return 0;
276 metaObj->emh = emh;
277 metaObj->on_disk = on_disk;
279 if (!(hmf = alloc_gdi_handle( metaObj, OBJ_ENHMETAFILE, NULL )))
280 HeapFree( GetProcessHeap(), 0, metaObj );
281 return hmf;
284 /****************************************************************************
285 * EMF_Delete_HENHMETAFILE
287 static BOOL EMF_Delete_HENHMETAFILE( HENHMETAFILE hmf )
289 ENHMETAFILEOBJ *metaObj = free_gdi_handle( hmf );
291 if(!metaObj) return FALSE;
293 if(metaObj->on_disk)
294 UnmapViewOfFile( metaObj->emh );
295 else
296 HeapFree( GetProcessHeap(), 0, metaObj->emh );
297 HeapFree( GetProcessHeap(), 0, metaObj );
298 return TRUE;
301 /******************************************************************
302 * EMF_GetEnhMetaHeader
304 * Returns ptr to ENHMETAHEADER associated with HENHMETAFILE
306 static ENHMETAHEADER *EMF_GetEnhMetaHeader( HENHMETAFILE hmf )
308 ENHMETAHEADER *ret = NULL;
309 ENHMETAFILEOBJ *metaObj = GDI_GetObjPtr( hmf, OBJ_ENHMETAFILE );
310 TRACE("hmf %p -> enhmetaObj %p\n", hmf, metaObj);
311 if (metaObj)
313 ret = metaObj->emh;
314 GDI_ReleaseObj( hmf );
316 return ret;
319 /*****************************************************************************
320 * EMF_GetEnhMetaFile
323 static HENHMETAFILE EMF_GetEnhMetaFile( HANDLE hFile )
325 ENHMETAHEADER *emh;
326 HANDLE hMapping;
327 HENHMETAFILE hemf;
328 DWORD filesize;
330 filesize = GetFileSize( hFile, NULL );
332 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
333 emh = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, filesize );
334 CloseHandle( hMapping );
336 if (!emh) return 0;
338 hemf = EMF_Create_HENHMETAFILE( emh, filesize, TRUE );
339 if (!hemf)
340 UnmapViewOfFile( emh );
341 return hemf;
345 /*****************************************************************************
346 * GetEnhMetaFileA (GDI32.@)
350 HENHMETAFILE WINAPI GetEnhMetaFileA(
351 LPCSTR lpszMetaFile /* [in] filename of enhanced metafile */
354 HENHMETAFILE hmf;
355 HANDLE hFile;
357 hFile = CreateFileA(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
358 OPEN_EXISTING, 0, 0);
359 if (hFile == INVALID_HANDLE_VALUE) {
360 WARN("could not open %s\n", lpszMetaFile);
361 return 0;
363 hmf = EMF_GetEnhMetaFile( hFile );
364 CloseHandle( hFile );
365 return hmf;
368 /*****************************************************************************
369 * GetEnhMetaFileW (GDI32.@)
371 HENHMETAFILE WINAPI GetEnhMetaFileW(
372 LPCWSTR lpszMetaFile) /* [in] filename of enhanced metafile */
374 HENHMETAFILE hmf;
375 HANDLE hFile;
377 hFile = CreateFileW(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
378 OPEN_EXISTING, 0, 0);
379 if (hFile == INVALID_HANDLE_VALUE) {
380 WARN("could not open %s\n", debugstr_w(lpszMetaFile));
381 return 0;
383 hmf = EMF_GetEnhMetaFile( hFile );
384 CloseHandle( hFile );
385 return hmf;
388 /*****************************************************************************
389 * GetEnhMetaFileHeader (GDI32.@)
391 * Retrieves the record containing the header for the specified
392 * enhanced-format metafile.
394 * RETURNS
395 * If buf is NULL, returns the size of buffer required.
396 * Otherwise, copy up to bufsize bytes of enhanced metafile header into
397 * buf.
399 UINT WINAPI GetEnhMetaFileHeader(
400 HENHMETAFILE hmf, /* [in] enhanced metafile */
401 UINT bufsize, /* [in] size of buffer */
402 LPENHMETAHEADER buf /* [out] buffer */
405 LPENHMETAHEADER emh;
406 UINT size;
408 emh = EMF_GetEnhMetaHeader(hmf);
409 if(!emh) return FALSE;
410 size = emh->nSize;
411 if (!buf) return size;
412 size = min(size, bufsize);
413 memmove(buf, emh, size);
414 return size;
418 /*****************************************************************************
419 * GetEnhMetaFileDescriptionA (GDI32.@)
421 * See GetEnhMetaFileDescriptionW.
423 UINT WINAPI GetEnhMetaFileDescriptionA(
424 HENHMETAFILE hmf, /* [in] enhanced metafile */
425 UINT size, /* [in] size of buf */
426 LPSTR buf /* [out] buffer to receive description */
429 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
430 DWORD len;
431 WCHAR *descrW;
433 if(!emh) return FALSE;
434 if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
435 descrW = (WCHAR *) ((char *) emh + emh->offDescription);
436 len = WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, NULL, 0, NULL, NULL );
438 if (!buf || !size ) return len;
440 len = min( size, len );
441 WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, buf, len, NULL, NULL );
442 return len;
445 /*****************************************************************************
446 * GetEnhMetaFileDescriptionW (GDI32.@)
448 * Copies the description string of an enhanced metafile into a buffer
449 * _buf_.
451 * RETURNS
452 * If _buf_ is NULL, returns size of _buf_ required. Otherwise, returns
453 * number of characters copied.
455 UINT WINAPI GetEnhMetaFileDescriptionW(
456 HENHMETAFILE hmf, /* [in] enhanced metafile */
457 UINT size, /* [in] size of buf */
458 LPWSTR buf /* [out] buffer to receive description */
461 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
463 if(!emh) return FALSE;
464 if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
465 if (!buf || !size ) return emh->nDescription;
467 memmove(buf, (char *) emh + emh->offDescription, min(size,emh->nDescription)*sizeof(WCHAR));
468 return min(size, emh->nDescription);
471 /****************************************************************************
472 * SetEnhMetaFileBits (GDI32.@)
474 * Creates an enhanced metafile by copying _bufsize_ bytes from _buf_.
476 HENHMETAFILE WINAPI SetEnhMetaFileBits(UINT bufsize, const BYTE *buf)
478 ENHMETAHEADER *emh = HeapAlloc( GetProcessHeap(), 0, bufsize );
479 HENHMETAFILE hmf;
481 if (!emh) return 0;
482 memcpy(emh, buf, bufsize);
483 hmf = EMF_Create_HENHMETAFILE( emh, bufsize, FALSE );
484 if (!hmf)
485 HeapFree( GetProcessHeap(), 0, emh );
486 return hmf;
489 /*****************************************************************************
490 * GetEnhMetaFileBits (GDI32.@)
493 UINT WINAPI GetEnhMetaFileBits(
494 HENHMETAFILE hmf,
495 UINT bufsize,
496 LPBYTE buf
499 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader( hmf );
500 UINT size;
502 if(!emh) return 0;
504 size = emh->nBytes;
505 if( buf == NULL ) return size;
507 size = min( size, bufsize );
508 memmove(buf, emh, size);
509 return size;
512 typedef struct EMF_dc_state
514 INT mode;
515 XFORM world_transform;
516 INT wndOrgX;
517 INT wndOrgY;
518 INT wndExtX;
519 INT wndExtY;
520 INT vportOrgX;
521 INT vportOrgY;
522 INT vportExtX;
523 INT vportExtY;
524 struct EMF_dc_state *next;
525 } EMF_dc_state;
527 typedef struct enum_emh_data
529 XFORM init_transform;
530 EMF_dc_state state;
531 INT save_level;
532 EMF_dc_state *saved_state;
533 } enum_emh_data;
535 #define ENUM_GET_PRIVATE_DATA(ht) \
536 ((enum_emh_data*)(((unsigned char*)(ht))-sizeof (enum_emh_data)))
538 #define WIDTH(rect) ( (rect).right - (rect).left )
539 #define HEIGHT(rect) ( (rect).bottom - (rect).top )
541 #define IS_WIN9X() (GetVersion()&0x80000000)
543 static void EMF_Update_MF_Xform(HDC hdc, const enum_emh_data *info)
545 XFORM mapping_mode_trans, final_trans;
546 double scaleX, scaleY;
548 scaleX = (double)info->state.vportExtX / (double)info->state.wndExtX;
549 scaleY = (double)info->state.vportExtY / (double)info->state.wndExtY;
550 mapping_mode_trans.eM11 = scaleX;
551 mapping_mode_trans.eM12 = 0.0;
552 mapping_mode_trans.eM21 = 0.0;
553 mapping_mode_trans.eM22 = scaleY;
554 mapping_mode_trans.eDx = (double)info->state.vportOrgX - scaleX * (double)info->state.wndOrgX;
555 mapping_mode_trans.eDy = (double)info->state.vportOrgY - scaleY * (double)info->state.wndOrgY;
557 CombineTransform(&final_trans, &info->state.world_transform, &mapping_mode_trans);
558 CombineTransform(&final_trans, &final_trans, &info->init_transform);
560 if (!SetWorldTransform(hdc, &final_trans))
562 ERR("World transform failed!\n");
566 static void EMF_RestoreDC( enum_emh_data *info, INT level )
568 if (abs(level) > info->save_level || level == 0) return;
570 if (level < 0) level = info->save_level + level + 1;
572 while (info->save_level >= level)
574 EMF_dc_state *state = info->saved_state;
575 info->saved_state = state->next;
576 state->next = NULL;
577 if (--info->save_level < level)
578 info->state = *state;
579 HeapFree( GetProcessHeap(), 0, state );
583 static void EMF_SaveDC( enum_emh_data *info )
585 EMF_dc_state *state = HeapAlloc( GetProcessHeap(), 0, sizeof(*state));
586 if (state)
588 *state = info->state;
589 state->next = info->saved_state;
590 info->saved_state = state;
591 info->save_level++;
592 TRACE("save_level %d\n", info->save_level);
596 static void EMF_SetMapMode(HDC hdc, enum_emh_data *info)
598 INT horzSize = GetDeviceCaps( hdc, HORZSIZE );
599 INT vertSize = GetDeviceCaps( hdc, VERTSIZE );
600 INT horzRes = GetDeviceCaps( hdc, HORZRES );
601 INT vertRes = GetDeviceCaps( hdc, VERTRES );
603 TRACE("%d\n", info->state.mode);
605 switch(info->state.mode)
607 case MM_TEXT:
608 info->state.wndExtX = 1;
609 info->state.wndExtY = 1;
610 info->state.vportExtX = 1;
611 info->state.vportExtY = 1;
612 break;
613 case MM_LOMETRIC:
614 case MM_ISOTROPIC:
615 info->state.wndExtX = horzSize * 10;
616 info->state.wndExtY = vertSize * 10;
617 info->state.vportExtX = horzRes;
618 info->state.vportExtY = -vertRes;
619 break;
620 case MM_HIMETRIC:
621 info->state.wndExtX = horzSize * 100;
622 info->state.wndExtY = vertSize * 100;
623 info->state.vportExtX = horzRes;
624 info->state.vportExtY = -vertRes;
625 break;
626 case MM_LOENGLISH:
627 info->state.wndExtX = MulDiv(1000, horzSize, 254);
628 info->state.wndExtY = MulDiv(1000, vertSize, 254);
629 info->state.vportExtX = horzRes;
630 info->state.vportExtY = -vertRes;
631 break;
632 case MM_HIENGLISH:
633 info->state.wndExtX = MulDiv(10000, horzSize, 254);
634 info->state.wndExtY = MulDiv(10000, vertSize, 254);
635 info->state.vportExtX = horzRes;
636 info->state.vportExtY = -vertRes;
637 break;
638 case MM_TWIPS:
639 info->state.wndExtX = MulDiv(14400, horzSize, 254);
640 info->state.wndExtY = MulDiv(14400, vertSize, 254);
641 info->state.vportExtX = horzRes;
642 info->state.vportExtY = -vertRes;
643 break;
644 case MM_ANISOTROPIC:
645 break;
646 default:
647 return;
651 /***********************************************************************
652 * EMF_FixIsotropic
654 * Fix viewport extensions for isotropic mode.
657 static void EMF_FixIsotropic(HDC hdc, enum_emh_data *info)
659 double xdim = fabs((double)info->state.vportExtX * GetDeviceCaps( hdc, HORZSIZE ) /
660 (GetDeviceCaps( hdc, HORZRES ) * info->state.wndExtX));
661 double ydim = fabs((double)info->state.vportExtY * GetDeviceCaps( hdc, VERTSIZE ) /
662 (GetDeviceCaps( hdc, VERTRES ) * info->state.wndExtY));
664 if (xdim > ydim)
666 INT mincx = (info->state.vportExtX >= 0) ? 1 : -1;
667 info->state.vportExtX = floor(info->state.vportExtX * ydim / xdim + 0.5);
668 if (!info->state.vportExtX) info->state.vportExtX = mincx;
670 else
672 INT mincy = (info->state.vportExtY >= 0) ? 1 : -1;
673 info->state.vportExtY = floor(info->state.vportExtY * xdim / ydim + 0.5);
674 if (!info->state.vportExtY) info->state.vportExtY = mincy;
678 /*****************************************************************************
679 * emr_produces_output
681 * Returns TRUE if the record type writes something to the dc. Used by
682 * PlayEnhMetaFileRecord to determine whether it needs to update the
683 * dc's xform when in win9x mode.
685 * FIXME: need to test which records should be here.
687 static BOOL emr_produces_output(int type)
689 switch(type) {
690 case EMR_POLYBEZIER:
691 case EMR_POLYGON:
692 case EMR_POLYLINE:
693 case EMR_POLYBEZIERTO:
694 case EMR_POLYLINETO:
695 case EMR_POLYPOLYLINE:
696 case EMR_POLYPOLYGON:
697 case EMR_SETPIXELV:
698 case EMR_MOVETOEX:
699 case EMR_EXCLUDECLIPRECT:
700 case EMR_INTERSECTCLIPRECT:
701 case EMR_SELECTOBJECT:
702 case EMR_ANGLEARC:
703 case EMR_ELLIPSE:
704 case EMR_RECTANGLE:
705 case EMR_ROUNDRECT:
706 case EMR_ARC:
707 case EMR_CHORD:
708 case EMR_PIE:
709 case EMR_EXTFLOODFILL:
710 case EMR_LINETO:
711 case EMR_ARCTO:
712 case EMR_POLYDRAW:
713 case EMR_GDICOMMENT:
714 case EMR_FILLRGN:
715 case EMR_FRAMERGN:
716 case EMR_INVERTRGN:
717 case EMR_PAINTRGN:
718 case EMR_BITBLT:
719 case EMR_STRETCHBLT:
720 case EMR_MASKBLT:
721 case EMR_PLGBLT:
722 case EMR_SETDIBITSTODEVICE:
723 case EMR_STRETCHDIBITS:
724 case EMR_EXTTEXTOUTA:
725 case EMR_EXTTEXTOUTW:
726 case EMR_POLYBEZIER16:
727 case EMR_POLYGON16:
728 case EMR_POLYLINE16:
729 case EMR_POLYBEZIERTO16:
730 case EMR_POLYLINETO16:
731 case EMR_POLYPOLYLINE16:
732 case EMR_POLYPOLYGON16:
733 case EMR_POLYDRAW16:
734 case EMR_POLYTEXTOUTA:
735 case EMR_POLYTEXTOUTW:
736 case EMR_SMALLTEXTOUT:
737 case EMR_ALPHABLEND:
738 case EMR_TRANSPARENTBLT:
739 return TRUE;
740 default:
741 return FALSE;
746 /*****************************************************************************
747 * PlayEnhMetaFileRecord (GDI32.@)
749 * Render a single enhanced metafile record in the device context hdc.
751 * RETURNS
752 * TRUE (non zero) on success, FALSE on error.
753 * BUGS
754 * Many unimplemented records.
755 * No error handling on record play failures (ie checking return codes)
757 * NOTES
758 * WinNT actually updates the current world transform in this function
759 * whereas Win9x does not.
761 BOOL WINAPI PlayEnhMetaFileRecord(
762 HDC hdc, /* [in] device context in which to render EMF record */
763 LPHANDLETABLE handletable, /* [in] array of handles to be used in rendering record */
764 const ENHMETARECORD *mr, /* [in] EMF record to render */
765 UINT handles /* [in] size of handle array */
768 int type;
769 RECT tmprc;
770 enum_emh_data *info = ENUM_GET_PRIVATE_DATA(handletable);
772 TRACE("hdc = %p, handletable = %p, record = %p, numHandles = %d\n",
773 hdc, handletable, mr, handles);
774 if (!mr) return FALSE;
776 type = mr->iType;
778 TRACE("record %s\n", get_emr_name(type));
779 switch(type)
781 case EMR_HEADER:
782 break;
783 case EMR_EOF:
784 break;
785 case EMR_GDICOMMENT:
787 const EMRGDICOMMENT *lpGdiComment = (const EMRGDICOMMENT *)mr;
788 /* In an enhanced metafile, there can be both public and private GDI comments */
789 GdiComment( hdc, lpGdiComment->cbData, lpGdiComment->Data );
790 break;
792 case EMR_SETMAPMODE:
794 const EMRSETMAPMODE *pSetMapMode = (const EMRSETMAPMODE *)mr;
796 if (info->state.mode == pSetMapMode->iMode &&
797 (info->state.mode == MM_ISOTROPIC || info->state.mode == MM_ANISOTROPIC))
798 break;
799 info->state.mode = pSetMapMode->iMode;
800 EMF_SetMapMode(hdc, info);
802 if (!IS_WIN9X())
803 EMF_Update_MF_Xform(hdc, info);
805 break;
807 case EMR_SETBKMODE:
809 const EMRSETBKMODE *pSetBkMode = (const EMRSETBKMODE *)mr;
810 SetBkMode(hdc, pSetBkMode->iMode);
811 break;
813 case EMR_SETBKCOLOR:
815 const EMRSETBKCOLOR *pSetBkColor = (const EMRSETBKCOLOR *)mr;
816 SetBkColor(hdc, pSetBkColor->crColor);
817 break;
819 case EMR_SETPOLYFILLMODE:
821 const EMRSETPOLYFILLMODE *pSetPolyFillMode = (const EMRSETPOLYFILLMODE *)mr;
822 SetPolyFillMode(hdc, pSetPolyFillMode->iMode);
823 break;
825 case EMR_SETROP2:
827 const EMRSETROP2 *pSetROP2 = (const EMRSETROP2 *)mr;
828 SetROP2(hdc, pSetROP2->iMode);
829 break;
831 case EMR_SETSTRETCHBLTMODE:
833 const EMRSETSTRETCHBLTMODE *pSetStretchBltMode = (const EMRSETSTRETCHBLTMODE *)mr;
834 SetStretchBltMode(hdc, pSetStretchBltMode->iMode);
835 break;
837 case EMR_SETTEXTALIGN:
839 const EMRSETTEXTALIGN *pSetTextAlign = (const EMRSETTEXTALIGN *)mr;
840 SetTextAlign(hdc, pSetTextAlign->iMode);
841 break;
843 case EMR_SETTEXTCOLOR:
845 const EMRSETTEXTCOLOR *pSetTextColor = (const EMRSETTEXTCOLOR *)mr;
846 SetTextColor(hdc, pSetTextColor->crColor);
847 break;
849 case EMR_SAVEDC:
851 if (SaveDC( hdc ))
852 EMF_SaveDC( info );
853 break;
855 case EMR_RESTOREDC:
857 const EMRRESTOREDC *pRestoreDC = (const EMRRESTOREDC *)mr;
858 TRACE("EMR_RESTORE: %d\n", pRestoreDC->iRelative);
859 if (RestoreDC( hdc, pRestoreDC->iRelative ))
860 EMF_RestoreDC( info, pRestoreDC->iRelative );
861 break;
863 case EMR_INTERSECTCLIPRECT:
865 const EMRINTERSECTCLIPRECT *pClipRect = (const EMRINTERSECTCLIPRECT *)mr;
866 TRACE("EMR_INTERSECTCLIPRECT: rect %d,%d - %d, %d\n",
867 pClipRect->rclClip.left, pClipRect->rclClip.top,
868 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
869 IntersectClipRect(hdc, pClipRect->rclClip.left, pClipRect->rclClip.top,
870 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
871 break;
873 case EMR_SELECTOBJECT:
875 const EMRSELECTOBJECT *pSelectObject = (const EMRSELECTOBJECT *)mr;
876 if( pSelectObject->ihObject & 0x80000000 ) {
877 /* High order bit is set - it's a stock object
878 * Strip the high bit to get the index.
879 * See MSDN article Q142319
881 SelectObject( hdc, GetStockObject( pSelectObject->ihObject &
882 0x7fffffff ) );
883 } else {
884 /* High order bit wasn't set - not a stock object
886 SelectObject( hdc,
887 (handletable->objectHandle)[pSelectObject->ihObject] );
889 break;
891 case EMR_DELETEOBJECT:
893 const EMRDELETEOBJECT *pDeleteObject = (const EMRDELETEOBJECT *)mr;
894 DeleteObject( (handletable->objectHandle)[pDeleteObject->ihObject]);
895 (handletable->objectHandle)[pDeleteObject->ihObject] = 0;
896 break;
898 case EMR_SETWINDOWORGEX:
900 const EMRSETWINDOWORGEX *pSetWindowOrgEx = (const EMRSETWINDOWORGEX *)mr;
902 info->state.wndOrgX = pSetWindowOrgEx->ptlOrigin.x;
903 info->state.wndOrgY = pSetWindowOrgEx->ptlOrigin.y;
905 TRACE("SetWindowOrgEx: %d,%d\n", info->state.wndOrgX, info->state.wndOrgY);
907 if (!IS_WIN9X())
908 EMF_Update_MF_Xform(hdc, info);
910 break;
912 case EMR_SETWINDOWEXTEX:
914 const EMRSETWINDOWEXTEX *pSetWindowExtEx = (const EMRSETWINDOWEXTEX *)mr;
916 if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
917 break;
918 info->state.wndExtX = pSetWindowExtEx->szlExtent.cx;
919 info->state.wndExtY = pSetWindowExtEx->szlExtent.cy;
920 if (info->state.mode == MM_ISOTROPIC)
921 EMF_FixIsotropic(hdc, info);
923 TRACE("SetWindowExtEx: %d,%d\n",info->state.wndExtX, info->state.wndExtY);
925 if (!IS_WIN9X())
926 EMF_Update_MF_Xform(hdc, info);
928 break;
930 case EMR_SETVIEWPORTORGEX:
932 const EMRSETVIEWPORTORGEX *pSetViewportOrgEx = (const EMRSETVIEWPORTORGEX *)mr;
934 info->state.vportOrgX = pSetViewportOrgEx->ptlOrigin.x;
935 info->state.vportOrgY = pSetViewportOrgEx->ptlOrigin.y;
936 TRACE("SetViewportOrgEx: %d,%d\n", info->state.vportOrgX, info->state.vportOrgY);
938 if (!IS_WIN9X())
939 EMF_Update_MF_Xform(hdc, info);
941 break;
943 case EMR_SETVIEWPORTEXTEX:
945 const EMRSETVIEWPORTEXTEX *pSetViewportExtEx = (const EMRSETVIEWPORTEXTEX *)mr;
947 if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
948 break;
949 info->state.vportExtX = pSetViewportExtEx->szlExtent.cx;
950 info->state.vportExtY = pSetViewportExtEx->szlExtent.cy;
951 if (info->state.mode == MM_ISOTROPIC)
952 EMF_FixIsotropic(hdc, info);
953 TRACE("SetViewportExtEx: %d,%d\n", info->state.vportExtX, info->state.vportExtY);
955 if (!IS_WIN9X())
956 EMF_Update_MF_Xform(hdc, info);
958 break;
960 case EMR_CREATEPEN:
962 const EMRCREATEPEN *pCreatePen = (const EMRCREATEPEN *)mr;
963 (handletable->objectHandle)[pCreatePen->ihPen] =
964 CreatePenIndirect(&pCreatePen->lopn);
965 break;
967 case EMR_EXTCREATEPEN:
969 const EMREXTCREATEPEN *pPen = (const EMREXTCREATEPEN *)mr;
970 LOGBRUSH lb;
971 lb.lbStyle = pPen->elp.elpBrushStyle;
972 lb.lbColor = pPen->elp.elpColor;
973 lb.lbHatch = pPen->elp.elpHatch;
975 if(pPen->offBmi || pPen->offBits)
976 FIXME("EMR_EXTCREATEPEN: Need to copy brush bitmap\n");
978 (handletable->objectHandle)[pPen->ihPen] =
979 ExtCreatePen(pPen->elp.elpPenStyle, pPen->elp.elpWidth, &lb,
980 pPen->elp.elpNumEntries, pPen->elp.elpNumEntries ? pPen->elp.elpStyleEntry : NULL);
981 break;
983 case EMR_CREATEBRUSHINDIRECT:
985 const EMRCREATEBRUSHINDIRECT *pBrush = (const EMRCREATEBRUSHINDIRECT *)mr;
986 LOGBRUSH brush;
987 brush.lbStyle = pBrush->lb.lbStyle;
988 brush.lbColor = pBrush->lb.lbColor;
989 brush.lbHatch = pBrush->lb.lbHatch;
990 (handletable->objectHandle)[pBrush->ihBrush] = CreateBrushIndirect(&brush);
991 break;
993 case EMR_EXTCREATEFONTINDIRECTW:
995 const EMREXTCREATEFONTINDIRECTW *pFont = (const EMREXTCREATEFONTINDIRECTW *)mr;
996 (handletable->objectHandle)[pFont->ihFont] =
997 CreateFontIndirectW(&pFont->elfw.elfLogFont);
998 break;
1000 case EMR_MOVETOEX:
1002 const EMRMOVETOEX *pMoveToEx = (const EMRMOVETOEX *)mr;
1003 MoveToEx(hdc, pMoveToEx->ptl.x, pMoveToEx->ptl.y, NULL);
1004 break;
1006 case EMR_LINETO:
1008 const EMRLINETO *pLineTo = (const EMRLINETO *)mr;
1009 LineTo(hdc, pLineTo->ptl.x, pLineTo->ptl.y);
1010 break;
1012 case EMR_RECTANGLE:
1014 const EMRRECTANGLE *pRect = (const EMRRECTANGLE *)mr;
1015 Rectangle(hdc, pRect->rclBox.left, pRect->rclBox.top,
1016 pRect->rclBox.right, pRect->rclBox.bottom);
1017 break;
1019 case EMR_ELLIPSE:
1021 const EMRELLIPSE *pEllipse = (const EMRELLIPSE *)mr;
1022 Ellipse(hdc, pEllipse->rclBox.left, pEllipse->rclBox.top,
1023 pEllipse->rclBox.right, pEllipse->rclBox.bottom);
1024 break;
1026 case EMR_POLYGON16:
1028 const EMRPOLYGON16 *pPoly = (const EMRPOLYGON16 *)mr;
1029 /* Shouldn't use Polygon16 since pPoly->cpts is DWORD */
1030 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1031 pPoly->cpts * sizeof(POINT) );
1032 DWORD i;
1033 for(i = 0; i < pPoly->cpts; i++)
1035 pts[i].x = pPoly->apts[i].x;
1036 pts[i].y = pPoly->apts[i].y;
1038 Polygon(hdc, pts, pPoly->cpts);
1039 HeapFree( GetProcessHeap(), 0, pts );
1040 break;
1042 case EMR_POLYLINE16:
1044 const EMRPOLYLINE16 *pPoly = (const EMRPOLYLINE16 *)mr;
1045 /* Shouldn't use Polyline16 since pPoly->cpts is DWORD */
1046 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1047 pPoly->cpts * sizeof(POINT) );
1048 DWORD i;
1049 for(i = 0; i < pPoly->cpts; i++)
1051 pts[i].x = pPoly->apts[i].x;
1052 pts[i].y = pPoly->apts[i].y;
1054 Polyline(hdc, pts, pPoly->cpts);
1055 HeapFree( GetProcessHeap(), 0, pts );
1056 break;
1058 case EMR_POLYLINETO16:
1060 const EMRPOLYLINETO16 *pPoly = (const EMRPOLYLINETO16 *)mr;
1061 /* Shouldn't use PolylineTo16 since pPoly->cpts is DWORD */
1062 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1063 pPoly->cpts * sizeof(POINT) );
1064 DWORD i;
1065 for(i = 0; i < pPoly->cpts; i++)
1067 pts[i].x = pPoly->apts[i].x;
1068 pts[i].y = pPoly->apts[i].y;
1070 PolylineTo(hdc, pts, pPoly->cpts);
1071 HeapFree( GetProcessHeap(), 0, pts );
1072 break;
1074 case EMR_POLYBEZIER16:
1076 const EMRPOLYBEZIER16 *pPoly = (const EMRPOLYBEZIER16 *)mr;
1077 /* Shouldn't use PolyBezier16 since pPoly->cpts is DWORD */
1078 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1079 pPoly->cpts * sizeof(POINT) );
1080 DWORD i;
1081 for(i = 0; i < pPoly->cpts; i++)
1083 pts[i].x = pPoly->apts[i].x;
1084 pts[i].y = pPoly->apts[i].y;
1086 PolyBezier(hdc, pts, pPoly->cpts);
1087 HeapFree( GetProcessHeap(), 0, pts );
1088 break;
1090 case EMR_POLYBEZIERTO16:
1092 const EMRPOLYBEZIERTO16 *pPoly = (const EMRPOLYBEZIERTO16 *)mr;
1093 /* Shouldn't use PolyBezierTo16 since pPoly->cpts is DWORD */
1094 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1095 pPoly->cpts * sizeof(POINT) );
1096 DWORD i;
1097 for(i = 0; i < pPoly->cpts; i++)
1099 pts[i].x = pPoly->apts[i].x;
1100 pts[i].y = pPoly->apts[i].y;
1102 PolyBezierTo(hdc, pts, pPoly->cpts);
1103 HeapFree( GetProcessHeap(), 0, pts );
1104 break;
1106 case EMR_POLYPOLYGON16:
1108 const EMRPOLYPOLYGON16 *pPolyPoly = (const EMRPOLYPOLYGON16 *)mr;
1109 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1110 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1112 const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1113 POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1114 DWORD i;
1115 for(i = 0; i < pPolyPoly->cpts; i++)
1117 pt[i].x = pts[i].x;
1118 pt[i].y = pts[i].y;
1120 PolyPolygon(hdc, pt, (const INT*)pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1121 HeapFree( GetProcessHeap(), 0, pt );
1122 break;
1124 case EMR_POLYPOLYLINE16:
1126 const EMRPOLYPOLYLINE16 *pPolyPoly = (const EMRPOLYPOLYLINE16 *)mr;
1127 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1128 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1130 const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1131 POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1132 DWORD i;
1133 for(i = 0; i < pPolyPoly->cpts; i++)
1135 pt[i].x = pts[i].x;
1136 pt[i].y = pts[i].y;
1138 PolyPolyline(hdc, pt, pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1139 HeapFree( GetProcessHeap(), 0, pt );
1140 break;
1143 case EMR_POLYDRAW16:
1145 const EMRPOLYDRAW16 *pPolyDraw16 = (const EMRPOLYDRAW16 *)mr;
1146 const POINTS *ptl = pPolyDraw16->apts;
1147 POINT *pts = HeapAlloc(GetProcessHeap(), 0, pPolyDraw16->cpts * sizeof(POINT));
1148 DWORD i;
1150 /* NB abTypes array doesn't start at pPolyDraw16->abTypes. It's actually
1151 pPolyDraw16->apts + pPolyDraw16->cpts. */
1152 const BYTE *types = (BYTE*)(pPolyDraw16->apts + pPolyDraw16->cpts);
1154 if (!pts)
1155 break;
1157 for (i = 0; i < pPolyDraw16->cpts; ++i)
1159 pts[i].x = ptl[i].x;
1160 pts[i].y = ptl[i].y;
1163 PolyDraw(hdc, pts, types, pPolyDraw16->cpts);
1164 HeapFree(GetProcessHeap(), 0, pts);
1165 break;
1168 case EMR_STRETCHDIBITS:
1170 const EMRSTRETCHDIBITS *pStretchDIBits = (const EMRSTRETCHDIBITS *)mr;
1172 StretchDIBits(hdc,
1173 pStretchDIBits->xDest,
1174 pStretchDIBits->yDest,
1175 pStretchDIBits->cxDest,
1176 pStretchDIBits->cyDest,
1177 pStretchDIBits->xSrc,
1178 pStretchDIBits->ySrc,
1179 pStretchDIBits->cxSrc,
1180 pStretchDIBits->cySrc,
1181 (const BYTE *)mr + pStretchDIBits->offBitsSrc,
1182 (const BITMAPINFO *)((const BYTE *)mr + pStretchDIBits->offBmiSrc),
1183 pStretchDIBits->iUsageSrc,
1184 pStretchDIBits->dwRop);
1185 break;
1188 case EMR_EXTTEXTOUTA:
1190 const EMREXTTEXTOUTA *pExtTextOutA = (const EMREXTTEXTOUTA *)mr;
1191 RECT rc;
1192 const INT *dx = NULL;
1193 int old_mode;
1195 rc.left = pExtTextOutA->emrtext.rcl.left;
1196 rc.top = pExtTextOutA->emrtext.rcl.top;
1197 rc.right = pExtTextOutA->emrtext.rcl.right;
1198 rc.bottom = pExtTextOutA->emrtext.rcl.bottom;
1199 TRACE("EMR_EXTTEXTOUTA: x,y = %d, %d. rect = %s. flags %08x\n",
1200 pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1201 wine_dbgstr_rect(&rc), pExtTextOutA->emrtext.fOptions);
1203 old_mode = SetGraphicsMode(hdc, pExtTextOutA->iGraphicsMode);
1204 /* Reselect the font back into the dc so that the transformation
1205 gets updated. */
1206 SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1208 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1209 * These files can be enumerated and played under Win98 just
1210 * fine, but at least Win2k chokes on them.
1212 if (pExtTextOutA->emrtext.offDx)
1213 dx = (const INT *)((const BYTE *)mr + pExtTextOutA->emrtext.offDx);
1215 ExtTextOutA(hdc, pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1216 pExtTextOutA->emrtext.fOptions, &rc,
1217 (LPCSTR)((const BYTE *)mr + pExtTextOutA->emrtext.offString), pExtTextOutA->emrtext.nChars,
1218 dx);
1220 SetGraphicsMode(hdc, old_mode);
1221 break;
1224 case EMR_EXTTEXTOUTW:
1226 const EMREXTTEXTOUTW *pExtTextOutW = (const EMREXTTEXTOUTW *)mr;
1227 RECT rc;
1228 const INT *dx = NULL;
1229 int old_mode;
1231 rc.left = pExtTextOutW->emrtext.rcl.left;
1232 rc.top = pExtTextOutW->emrtext.rcl.top;
1233 rc.right = pExtTextOutW->emrtext.rcl.right;
1234 rc.bottom = pExtTextOutW->emrtext.rcl.bottom;
1235 TRACE("EMR_EXTTEXTOUTW: x,y = %d, %d. rect = %s. flags %08x\n",
1236 pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1237 wine_dbgstr_rect(&rc), pExtTextOutW->emrtext.fOptions);
1239 old_mode = SetGraphicsMode(hdc, pExtTextOutW->iGraphicsMode);
1240 /* Reselect the font back into the dc so that the transformation
1241 gets updated. */
1242 SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1244 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1245 * These files can be enumerated and played under Win98 just
1246 * fine, but at least Win2k chokes on them.
1248 if (pExtTextOutW->emrtext.offDx)
1249 dx = (const INT *)((const BYTE *)mr + pExtTextOutW->emrtext.offDx);
1251 ExtTextOutW(hdc, pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1252 pExtTextOutW->emrtext.fOptions, &rc,
1253 (LPCWSTR)((const BYTE *)mr + pExtTextOutW->emrtext.offString), pExtTextOutW->emrtext.nChars,
1254 dx);
1256 SetGraphicsMode(hdc, old_mode);
1257 break;
1260 case EMR_CREATEPALETTE:
1262 const EMRCREATEPALETTE *lpCreatePal = (const EMRCREATEPALETTE *)mr;
1264 (handletable->objectHandle)[ lpCreatePal->ihPal ] =
1265 CreatePalette( &lpCreatePal->lgpl );
1267 break;
1270 case EMR_SELECTPALETTE:
1272 const EMRSELECTPALETTE *lpSelectPal = (const EMRSELECTPALETTE *)mr;
1274 if( lpSelectPal->ihPal & 0x80000000 ) {
1275 SelectPalette( hdc, GetStockObject(lpSelectPal->ihPal & 0x7fffffff), TRUE);
1276 } else {
1277 SelectPalette( hdc, (handletable->objectHandle)[lpSelectPal->ihPal], TRUE);
1279 break;
1282 case EMR_REALIZEPALETTE:
1284 RealizePalette( hdc );
1285 break;
1288 case EMR_EXTSELECTCLIPRGN:
1290 const EMREXTSELECTCLIPRGN *lpRgn = (const EMREXTSELECTCLIPRGN *)mr;
1291 HRGN hRgn = 0;
1293 if (mr->nSize >= sizeof(*lpRgn) + sizeof(RGNDATAHEADER))
1294 hRgn = ExtCreateRegion( &info->init_transform, 0, (const RGNDATA *)lpRgn->RgnData );
1296 ExtSelectClipRgn(hdc, hRgn, (INT)(lpRgn->iMode));
1297 /* ExtSelectClipRgn created a copy of the region */
1298 DeleteObject(hRgn);
1299 break;
1302 case EMR_SETMETARGN:
1304 SetMetaRgn( hdc );
1305 break;
1308 case EMR_SETWORLDTRANSFORM:
1310 const EMRSETWORLDTRANSFORM *lpXfrm = (const EMRSETWORLDTRANSFORM *)mr;
1311 info->state.world_transform = lpXfrm->xform;
1313 if (!IS_WIN9X())
1314 EMF_Update_MF_Xform(hdc, info);
1316 break;
1319 case EMR_POLYBEZIER:
1321 const EMRPOLYBEZIER *lpPolyBez = (const EMRPOLYBEZIER *)mr;
1322 PolyBezier(hdc, (const POINT*)lpPolyBez->aptl, (UINT)lpPolyBez->cptl);
1323 break;
1326 case EMR_POLYGON:
1328 const EMRPOLYGON *lpPoly = (const EMRPOLYGON *)mr;
1329 Polygon( hdc, (const POINT*)lpPoly->aptl, (UINT)lpPoly->cptl );
1330 break;
1333 case EMR_POLYLINE:
1335 const EMRPOLYLINE *lpPolyLine = (const EMRPOLYLINE *)mr;
1336 Polyline(hdc, (const POINT*)lpPolyLine->aptl, (UINT)lpPolyLine->cptl);
1337 break;
1340 case EMR_POLYBEZIERTO:
1342 const EMRPOLYBEZIERTO *lpPolyBezierTo = (const EMRPOLYBEZIERTO *)mr;
1343 PolyBezierTo( hdc, (const POINT*)lpPolyBezierTo->aptl,
1344 (UINT)lpPolyBezierTo->cptl );
1345 break;
1348 case EMR_POLYLINETO:
1350 const EMRPOLYLINETO *lpPolyLineTo = (const EMRPOLYLINETO *)mr;
1351 PolylineTo( hdc, (const POINT*)lpPolyLineTo->aptl,
1352 (UINT)lpPolyLineTo->cptl );
1353 break;
1356 case EMR_POLYPOLYLINE:
1358 const EMRPOLYPOLYLINE *pPolyPolyline = (const EMRPOLYPOLYLINE *)mr;
1359 /* NB Points at pPolyPolyline->aPolyCounts + pPolyPolyline->nPolys */
1361 PolyPolyline(hdc, (const POINT*)(pPolyPolyline->aPolyCounts +
1362 pPolyPolyline->nPolys),
1363 pPolyPolyline->aPolyCounts,
1364 pPolyPolyline->nPolys );
1366 break;
1369 case EMR_POLYPOLYGON:
1371 const EMRPOLYPOLYGON *pPolyPolygon = (const EMRPOLYPOLYGON *)mr;
1373 /* NB Points at pPolyPolygon->aPolyCounts + pPolyPolygon->nPolys */
1375 PolyPolygon(hdc, (const POINT*)(pPolyPolygon->aPolyCounts +
1376 pPolyPolygon->nPolys),
1377 (const INT*)pPolyPolygon->aPolyCounts, pPolyPolygon->nPolys );
1378 break;
1381 case EMR_SETBRUSHORGEX:
1383 const EMRSETBRUSHORGEX *lpSetBrushOrgEx = (const EMRSETBRUSHORGEX *)mr;
1385 SetBrushOrgEx( hdc,
1386 (INT)lpSetBrushOrgEx->ptlOrigin.x,
1387 (INT)lpSetBrushOrgEx->ptlOrigin.y,
1388 NULL );
1390 break;
1393 case EMR_SETPIXELV:
1395 const EMRSETPIXELV *lpSetPixelV = (const EMRSETPIXELV *)mr;
1397 SetPixelV( hdc,
1398 (INT)lpSetPixelV->ptlPixel.x,
1399 (INT)lpSetPixelV->ptlPixel.y,
1400 lpSetPixelV->crColor );
1402 break;
1405 case EMR_SETMAPPERFLAGS:
1407 const EMRSETMAPPERFLAGS *lpSetMapperFlags = (const EMRSETMAPPERFLAGS *)mr;
1409 SetMapperFlags( hdc, lpSetMapperFlags->dwFlags );
1411 break;
1414 case EMR_SETCOLORADJUSTMENT:
1416 const EMRSETCOLORADJUSTMENT *lpSetColorAdjust = (const EMRSETCOLORADJUSTMENT *)mr;
1418 SetColorAdjustment( hdc, &lpSetColorAdjust->ColorAdjustment );
1420 break;
1423 case EMR_OFFSETCLIPRGN:
1425 const EMROFFSETCLIPRGN *lpOffsetClipRgn = (const EMROFFSETCLIPRGN *)mr;
1427 OffsetClipRgn( hdc,
1428 (INT)lpOffsetClipRgn->ptlOffset.x,
1429 (INT)lpOffsetClipRgn->ptlOffset.y );
1430 FIXME("OffsetClipRgn\n");
1432 break;
1435 case EMR_EXCLUDECLIPRECT:
1437 const EMREXCLUDECLIPRECT *lpExcludeClipRect = (const EMREXCLUDECLIPRECT *)mr;
1439 ExcludeClipRect( hdc,
1440 lpExcludeClipRect->rclClip.left,
1441 lpExcludeClipRect->rclClip.top,
1442 lpExcludeClipRect->rclClip.right,
1443 lpExcludeClipRect->rclClip.bottom );
1444 FIXME("ExcludeClipRect\n");
1446 break;
1449 case EMR_SCALEVIEWPORTEXTEX:
1451 const EMRSCALEVIEWPORTEXTEX *lpScaleViewportExtEx = (const EMRSCALEVIEWPORTEXTEX *)mr;
1453 if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1454 break;
1455 if (!lpScaleViewportExtEx->xNum || !lpScaleViewportExtEx->xDenom ||
1456 !lpScaleViewportExtEx->yNum || !lpScaleViewportExtEx->yDenom)
1457 break;
1458 info->state.vportExtX = MulDiv(info->state.vportExtX, lpScaleViewportExtEx->xNum,
1459 lpScaleViewportExtEx->xDenom);
1460 info->state.vportExtY = MulDiv(info->state.vportExtY, lpScaleViewportExtEx->yNum,
1461 lpScaleViewportExtEx->yDenom);
1462 if (info->state.vportExtX == 0) info->state.vportExtX = 1;
1463 if (info->state.vportExtY == 0) info->state.vportExtY = 1;
1464 if (info->state.mode == MM_ISOTROPIC)
1465 EMF_FixIsotropic(hdc, info);
1467 TRACE("EMRSCALEVIEWPORTEXTEX %d/%d %d/%d\n",
1468 lpScaleViewportExtEx->xNum,lpScaleViewportExtEx->xDenom,
1469 lpScaleViewportExtEx->yNum,lpScaleViewportExtEx->yDenom);
1471 if (!IS_WIN9X())
1472 EMF_Update_MF_Xform(hdc, info);
1474 break;
1477 case EMR_SCALEWINDOWEXTEX:
1479 const EMRSCALEWINDOWEXTEX *lpScaleWindowExtEx = (const EMRSCALEWINDOWEXTEX *)mr;
1481 if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1482 break;
1483 if (!lpScaleWindowExtEx->xNum || !lpScaleWindowExtEx->xDenom ||
1484 !lpScaleWindowExtEx->yNum || !lpScaleWindowExtEx->yDenom)
1485 break;
1486 info->state.wndExtX = MulDiv(info->state.wndExtX, lpScaleWindowExtEx->xNum,
1487 lpScaleWindowExtEx->xDenom);
1488 info->state.wndExtY = MulDiv(info->state.wndExtY, lpScaleWindowExtEx->yNum,
1489 lpScaleWindowExtEx->yDenom);
1490 if (info->state.wndExtX == 0) info->state.wndExtX = 1;
1491 if (info->state.wndExtY == 0) info->state.wndExtY = 1;
1492 if (info->state.mode == MM_ISOTROPIC)
1493 EMF_FixIsotropic(hdc, info);
1495 TRACE("EMRSCALEWINDOWEXTEX %d/%d %d/%d\n",
1496 lpScaleWindowExtEx->xNum,lpScaleWindowExtEx->xDenom,
1497 lpScaleWindowExtEx->yNum,lpScaleWindowExtEx->yDenom);
1499 if (!IS_WIN9X())
1500 EMF_Update_MF_Xform(hdc, info);
1502 break;
1505 case EMR_MODIFYWORLDTRANSFORM:
1507 const EMRMODIFYWORLDTRANSFORM *lpModifyWorldTrans = (const EMRMODIFYWORLDTRANSFORM *)mr;
1509 switch(lpModifyWorldTrans->iMode) {
1510 case MWT_IDENTITY:
1511 info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
1512 info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
1513 info->state.world_transform.eDx = info->state.world_transform.eDy = 0;
1514 if (!IS_WIN9X())
1515 EMF_Update_MF_Xform(hdc, info);
1516 break;
1517 case MWT_LEFTMULTIPLY:
1518 CombineTransform(&info->state.world_transform, &lpModifyWorldTrans->xform,
1519 &info->state.world_transform);
1520 if (!IS_WIN9X())
1521 ModifyWorldTransform(hdc, &lpModifyWorldTrans->xform, MWT_LEFTMULTIPLY);
1522 break;
1523 case MWT_RIGHTMULTIPLY:
1524 CombineTransform(&info->state.world_transform, &info->state.world_transform,
1525 &lpModifyWorldTrans->xform);
1526 if (!IS_WIN9X())
1527 EMF_Update_MF_Xform(hdc, info);
1528 break;
1529 default:
1530 FIXME("Unknown imode %d\n", lpModifyWorldTrans->iMode);
1531 break;
1533 break;
1536 case EMR_ANGLEARC:
1538 const EMRANGLEARC *lpAngleArc = (const EMRANGLEARC *)mr;
1540 AngleArc( hdc,
1541 (INT)lpAngleArc->ptlCenter.x, (INT)lpAngleArc->ptlCenter.y,
1542 lpAngleArc->nRadius, lpAngleArc->eStartAngle,
1543 lpAngleArc->eSweepAngle );
1545 break;
1548 case EMR_ROUNDRECT:
1550 const EMRROUNDRECT *lpRoundRect = (const EMRROUNDRECT *)mr;
1552 RoundRect( hdc,
1553 lpRoundRect->rclBox.left,
1554 lpRoundRect->rclBox.top,
1555 lpRoundRect->rclBox.right,
1556 lpRoundRect->rclBox.bottom,
1557 lpRoundRect->szlCorner.cx,
1558 lpRoundRect->szlCorner.cy );
1560 break;
1563 case EMR_ARC:
1565 const EMRARC *lpArc = (const EMRARC *)mr;
1567 Arc( hdc,
1568 (INT)lpArc->rclBox.left,
1569 (INT)lpArc->rclBox.top,
1570 (INT)lpArc->rclBox.right,
1571 (INT)lpArc->rclBox.bottom,
1572 (INT)lpArc->ptlStart.x,
1573 (INT)lpArc->ptlStart.y,
1574 (INT)lpArc->ptlEnd.x,
1575 (INT)lpArc->ptlEnd.y );
1577 break;
1580 case EMR_CHORD:
1582 const EMRCHORD *lpChord = (const EMRCHORD *)mr;
1584 Chord( hdc,
1585 (INT)lpChord->rclBox.left,
1586 (INT)lpChord->rclBox.top,
1587 (INT)lpChord->rclBox.right,
1588 (INT)lpChord->rclBox.bottom,
1589 (INT)lpChord->ptlStart.x,
1590 (INT)lpChord->ptlStart.y,
1591 (INT)lpChord->ptlEnd.x,
1592 (INT)lpChord->ptlEnd.y );
1594 break;
1597 case EMR_PIE:
1599 const EMRPIE *lpPie = (const EMRPIE *)mr;
1601 Pie( hdc,
1602 (INT)lpPie->rclBox.left,
1603 (INT)lpPie->rclBox.top,
1604 (INT)lpPie->rclBox.right,
1605 (INT)lpPie->rclBox.bottom,
1606 (INT)lpPie->ptlStart.x,
1607 (INT)lpPie->ptlStart.y,
1608 (INT)lpPie->ptlEnd.x,
1609 (INT)lpPie->ptlEnd.y );
1611 break;
1614 case EMR_ARCTO:
1616 const EMRARC *lpArcTo = (const EMRARC *)mr;
1618 ArcTo( hdc,
1619 (INT)lpArcTo->rclBox.left,
1620 (INT)lpArcTo->rclBox.top,
1621 (INT)lpArcTo->rclBox.right,
1622 (INT)lpArcTo->rclBox.bottom,
1623 (INT)lpArcTo->ptlStart.x,
1624 (INT)lpArcTo->ptlStart.y,
1625 (INT)lpArcTo->ptlEnd.x,
1626 (INT)lpArcTo->ptlEnd.y );
1628 break;
1631 case EMR_EXTFLOODFILL:
1633 const EMREXTFLOODFILL *lpExtFloodFill = (const EMREXTFLOODFILL *)mr;
1635 ExtFloodFill( hdc,
1636 (INT)lpExtFloodFill->ptlStart.x,
1637 (INT)lpExtFloodFill->ptlStart.y,
1638 lpExtFloodFill->crColor,
1639 (UINT)lpExtFloodFill->iMode );
1641 break;
1644 case EMR_POLYDRAW:
1646 const EMRPOLYDRAW *lpPolyDraw = (const EMRPOLYDRAW *)mr;
1648 /* NB abTypes array doesn't start at lpPolyDraw->abTypes. It's actually
1649 lpPolyDraw->aptl + lpPolyDraw->cptl. */
1650 PolyDraw( hdc,
1651 (const POINT*)lpPolyDraw->aptl,
1652 (BYTE*)(lpPolyDraw->aptl + lpPolyDraw->cptl),
1653 (INT)lpPolyDraw->cptl );
1655 break;
1658 case EMR_SETARCDIRECTION:
1660 const EMRSETARCDIRECTION *lpSetArcDirection = (const EMRSETARCDIRECTION *)mr;
1661 SetArcDirection( hdc, (INT)lpSetArcDirection->iArcDirection );
1662 break;
1665 case EMR_SETMITERLIMIT:
1667 const EMRSETMITERLIMIT *lpSetMiterLimit = (const EMRSETMITERLIMIT *)mr;
1668 SetMiterLimit( hdc, lpSetMiterLimit->eMiterLimit, NULL );
1669 break;
1672 case EMR_BEGINPATH:
1674 BeginPath( hdc );
1675 break;
1678 case EMR_ENDPATH:
1680 EndPath( hdc );
1681 break;
1684 case EMR_CLOSEFIGURE:
1686 CloseFigure( hdc );
1687 break;
1690 case EMR_FILLPATH:
1692 /*const EMRFILLPATH lpFillPath = (const EMRFILLPATH *)mr;*/
1693 FillPath( hdc );
1694 break;
1697 case EMR_STROKEANDFILLPATH:
1699 /*const EMRSTROKEANDFILLPATH lpStrokeAndFillPath = (const EMRSTROKEANDFILLPATH *)mr;*/
1700 StrokeAndFillPath( hdc );
1701 break;
1704 case EMR_STROKEPATH:
1706 /*const EMRSTROKEPATH lpStrokePath = (const EMRSTROKEPATH *)mr;*/
1707 StrokePath( hdc );
1708 break;
1711 case EMR_FLATTENPATH:
1713 FlattenPath( hdc );
1714 break;
1717 case EMR_WIDENPATH:
1719 WidenPath( hdc );
1720 break;
1723 case EMR_SELECTCLIPPATH:
1725 const EMRSELECTCLIPPATH *lpSelectClipPath = (const EMRSELECTCLIPPATH *)mr;
1726 SelectClipPath( hdc, (INT)lpSelectClipPath->iMode );
1727 break;
1730 case EMR_ABORTPATH:
1732 AbortPath( hdc );
1733 break;
1736 case EMR_CREATECOLORSPACE:
1738 PEMRCREATECOLORSPACE lpCreateColorSpace = (PEMRCREATECOLORSPACE)mr;
1739 (handletable->objectHandle)[lpCreateColorSpace->ihCS] =
1740 CreateColorSpaceA( &lpCreateColorSpace->lcs );
1741 break;
1744 case EMR_SETCOLORSPACE:
1746 const EMRSETCOLORSPACE *lpSetColorSpace = (const EMRSETCOLORSPACE *)mr;
1747 SetColorSpace( hdc,
1748 (handletable->objectHandle)[lpSetColorSpace->ihCS] );
1749 break;
1752 case EMR_DELETECOLORSPACE:
1754 const EMRDELETECOLORSPACE *lpDeleteColorSpace = (const EMRDELETECOLORSPACE *)mr;
1755 DeleteColorSpace( (handletable->objectHandle)[lpDeleteColorSpace->ihCS] );
1756 break;
1759 case EMR_SETICMMODE:
1761 const EMRSETICMMODE *lpSetICMMode = (const EMRSETICMMODE *)mr;
1762 SetICMMode( hdc, (INT)lpSetICMMode->iMode );
1763 break;
1766 case EMR_PIXELFORMAT:
1768 INT iPixelFormat;
1769 const EMRPIXELFORMAT *lpPixelFormat = (const EMRPIXELFORMAT *)mr;
1771 iPixelFormat = ChoosePixelFormat( hdc, &lpPixelFormat->pfd );
1772 SetPixelFormat( hdc, iPixelFormat, &lpPixelFormat->pfd );
1774 break;
1777 case EMR_SETPALETTEENTRIES:
1779 const EMRSETPALETTEENTRIES *lpSetPaletteEntries = (const EMRSETPALETTEENTRIES *)mr;
1781 SetPaletteEntries( (handletable->objectHandle)[lpSetPaletteEntries->ihPal],
1782 (UINT)lpSetPaletteEntries->iStart,
1783 (UINT)lpSetPaletteEntries->cEntries,
1784 lpSetPaletteEntries->aPalEntries );
1786 break;
1789 case EMR_RESIZEPALETTE:
1791 const EMRRESIZEPALETTE *lpResizePalette = (const EMRRESIZEPALETTE *)mr;
1793 ResizePalette( (handletable->objectHandle)[lpResizePalette->ihPal],
1794 (UINT)lpResizePalette->cEntries );
1796 break;
1799 case EMR_CREATEDIBPATTERNBRUSHPT:
1801 const EMRCREATEDIBPATTERNBRUSHPT *lpCreate = (const EMRCREATEDIBPATTERNBRUSHPT *)mr;
1802 LPVOID lpPackedStruct;
1804 /* Check that offsets and data are contained within the record
1805 * (including checking for wrap-arounds).
1807 if ( lpCreate->offBmi + lpCreate->cbBmi > mr->nSize
1808 || lpCreate->offBits + lpCreate->cbBits > mr->nSize
1809 || lpCreate->offBmi + lpCreate->cbBmi < lpCreate->offBmi
1810 || lpCreate->offBits + lpCreate->cbBits < lpCreate->offBits )
1812 ERR("Invalid EMR_CREATEDIBPATTERNBRUSHPT record\n");
1813 break;
1816 /* This is a BITMAPINFO struct followed directly by bitmap bits */
1817 lpPackedStruct = HeapAlloc( GetProcessHeap(), 0,
1818 lpCreate->cbBmi + lpCreate->cbBits );
1819 if(!lpPackedStruct)
1821 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1822 break;
1825 /* Now pack this structure */
1826 memcpy( lpPackedStruct,
1827 ((const BYTE *)lpCreate) + lpCreate->offBmi,
1828 lpCreate->cbBmi );
1829 memcpy( ((BYTE*)lpPackedStruct) + lpCreate->cbBmi,
1830 ((const BYTE *)lpCreate) + lpCreate->offBits,
1831 lpCreate->cbBits );
1833 (handletable->objectHandle)[lpCreate->ihBrush] =
1834 CreateDIBPatternBrushPt( lpPackedStruct,
1835 (UINT)lpCreate->iUsage );
1837 HeapFree(GetProcessHeap(), 0, lpPackedStruct);
1838 break;
1841 case EMR_CREATEMONOBRUSH:
1843 const EMRCREATEMONOBRUSH *pCreateMonoBrush = (const EMRCREATEMONOBRUSH *)mr;
1844 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pCreateMonoBrush->offBmi);
1845 HBITMAP hBmp;
1847 /* Need to check if the bitmap is monochrome, and if the
1848 two colors are really black and white */
1849 if (pCreateMonoBrush->iUsage == DIB_PAL_MONO)
1851 BITMAP bm;
1853 /* Undocumented iUsage indicates a mono bitmap with no palette table,
1854 * aligned to 32 rather than 16 bits.
1856 bm.bmType = 0;
1857 bm.bmWidth = pbi->bmiHeader.biWidth;
1858 bm.bmHeight = abs(pbi->bmiHeader.biHeight);
1859 bm.bmWidthBytes = 4 * ((pbi->bmiHeader.biWidth + 31) / 32);
1860 bm.bmPlanes = pbi->bmiHeader.biPlanes;
1861 bm.bmBitsPixel = pbi->bmiHeader.biBitCount;
1862 bm.bmBits = (BYTE *)mr + pCreateMonoBrush->offBits;
1863 hBmp = CreateBitmapIndirect(&bm);
1865 else if (is_dib_monochrome(pbi))
1867 /* Top-down DIBs have a negative height */
1868 LONG height = pbi->bmiHeader.biHeight;
1870 hBmp = CreateBitmap(pbi->bmiHeader.biWidth, abs(height), 1, 1, NULL);
1871 SetDIBits(hdc, hBmp, 0, pbi->bmiHeader.biHeight,
1872 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1874 else
1876 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1877 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1880 (handletable->objectHandle)[pCreateMonoBrush->ihBrush] = CreatePatternBrush(hBmp);
1882 /* CreatePatternBrush created a copy of the bitmap */
1883 DeleteObject(hBmp);
1884 break;
1887 case EMR_BITBLT:
1889 const EMRBITBLT *pBitBlt = (const EMRBITBLT *)mr;
1891 if(pBitBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1892 PatBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1893 pBitBlt->dwRop);
1894 } else { /* BitBlt */
1895 HDC hdcSrc = CreateCompatibleDC(hdc);
1896 HBRUSH hBrush, hBrushOld;
1897 HBITMAP hBmp = 0, hBmpOld = 0;
1898 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pBitBlt->offBmiSrc);
1900 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1901 SetWorldTransform(hdcSrc, &pBitBlt->xformSrc);
1903 hBrush = CreateSolidBrush(pBitBlt->crBkColorSrc);
1904 hBrushOld = SelectObject(hdcSrc, hBrush);
1905 PatBlt(hdcSrc, pBitBlt->rclBounds.left, pBitBlt->rclBounds.top,
1906 pBitBlt->rclBounds.right - pBitBlt->rclBounds.left,
1907 pBitBlt->rclBounds.bottom - pBitBlt->rclBounds.top, PATCOPY);
1908 SelectObject(hdcSrc, hBrushOld);
1909 DeleteObject(hBrush);
1911 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1912 (const BYTE *)mr + pBitBlt->offBitsSrc, pbi, pBitBlt->iUsageSrc);
1913 hBmpOld = SelectObject(hdcSrc, hBmp);
1915 BitBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1916 hdcSrc, pBitBlt->xSrc, pBitBlt->ySrc, pBitBlt->dwRop);
1918 SelectObject(hdcSrc, hBmpOld);
1919 DeleteObject(hBmp);
1920 DeleteDC(hdcSrc);
1922 break;
1925 case EMR_STRETCHBLT:
1927 const EMRSTRETCHBLT *pStretchBlt = (const EMRSTRETCHBLT *)mr;
1929 TRACE("EMR_STRETCHBLT: %d, %d %dx%d -> %d, %d %dx%d. rop %08x offBitsSrc %d\n",
1930 pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1931 pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1932 pStretchBlt->dwRop, pStretchBlt->offBitsSrc);
1934 if(pStretchBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1935 PatBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1936 pStretchBlt->dwRop);
1937 } else { /* StretchBlt */
1938 HDC hdcSrc = CreateCompatibleDC(hdc);
1939 HBRUSH hBrush, hBrushOld;
1940 HBITMAP hBmp = 0, hBmpOld = 0;
1941 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pStretchBlt->offBmiSrc);
1943 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1944 SetWorldTransform(hdcSrc, &pStretchBlt->xformSrc);
1946 hBrush = CreateSolidBrush(pStretchBlt->crBkColorSrc);
1947 hBrushOld = SelectObject(hdcSrc, hBrush);
1948 PatBlt(hdcSrc, pStretchBlt->rclBounds.left, pStretchBlt->rclBounds.top,
1949 pStretchBlt->rclBounds.right - pStretchBlt->rclBounds.left,
1950 pStretchBlt->rclBounds.bottom - pStretchBlt->rclBounds.top, PATCOPY);
1951 SelectObject(hdcSrc, hBrushOld);
1952 DeleteObject(hBrush);
1954 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1955 (const BYTE *)mr + pStretchBlt->offBitsSrc, pbi, pStretchBlt->iUsageSrc);
1956 hBmpOld = SelectObject(hdcSrc, hBmp);
1958 StretchBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1959 hdcSrc, pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1960 pStretchBlt->dwRop);
1962 SelectObject(hdcSrc, hBmpOld);
1963 DeleteObject(hBmp);
1964 DeleteDC(hdcSrc);
1966 break;
1969 case EMR_ALPHABLEND:
1971 const EMRALPHABLEND *pAlphaBlend = (const EMRALPHABLEND *)mr;
1973 TRACE("EMR_ALPHABLEND: %d, %d %dx%d -> %d, %d %dx%d. blendfn %08x offBitsSrc %d\n",
1974 pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1975 pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1976 pAlphaBlend->dwRop, pAlphaBlend->offBitsSrc);
1978 if(pAlphaBlend->offBmiSrc == 0) {
1979 FIXME("EMR_ALPHABLEND: offBmiSrc == 0\n");
1980 } else {
1981 HDC hdcSrc = CreateCompatibleDC(hdc);
1982 HBITMAP hBmp = 0, hBmpOld = 0;
1983 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pAlphaBlend->offBmiSrc);
1984 void *bits;
1986 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1987 SetWorldTransform(hdcSrc, &pAlphaBlend->xformSrc);
1989 hBmp = CreateDIBSection(hdc, pbi, pAlphaBlend->iUsageSrc, &bits, NULL, 0);
1990 memcpy(bits, (const BYTE *)mr + pAlphaBlend->offBitsSrc, pAlphaBlend->cbBitsSrc);
1991 hBmpOld = SelectObject(hdcSrc, hBmp);
1993 GdiAlphaBlend(hdc, pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1994 hdcSrc, pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1995 *(BLENDFUNCTION *)&pAlphaBlend->dwRop);
1997 SelectObject(hdcSrc, hBmpOld);
1998 DeleteObject(hBmp);
1999 DeleteDC(hdcSrc);
2001 break;
2004 case EMR_MASKBLT:
2006 const EMRMASKBLT *pMaskBlt = (const EMRMASKBLT *)mr;
2007 HDC hdcSrc = CreateCompatibleDC(hdc);
2008 HBRUSH hBrush, hBrushOld;
2009 HBITMAP hBmp, hBmpOld, hBmpMask;
2010 const BITMAPINFO *pbi;
2012 SetGraphicsMode(hdcSrc, GM_ADVANCED);
2013 SetWorldTransform(hdcSrc, &pMaskBlt->xformSrc);
2015 hBrush = CreateSolidBrush(pMaskBlt->crBkColorSrc);
2016 hBrushOld = SelectObject(hdcSrc, hBrush);
2017 PatBlt(hdcSrc, pMaskBlt->rclBounds.left, pMaskBlt->rclBounds.top,
2018 pMaskBlt->rclBounds.right - pMaskBlt->rclBounds.left,
2019 pMaskBlt->rclBounds.bottom - pMaskBlt->rclBounds.top, PATCOPY);
2020 SelectObject(hdcSrc, hBrushOld);
2021 DeleteObject(hBrush);
2023 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiMask);
2024 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
2025 1, 1, NULL);
2026 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
2027 (const BYTE *)mr + pMaskBlt->offBitsMask, pbi, pMaskBlt->iUsageMask);
2029 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiSrc);
2030 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
2031 (const BYTE *)mr + pMaskBlt->offBitsSrc, pbi, pMaskBlt->iUsageSrc);
2032 hBmpOld = SelectObject(hdcSrc, hBmp);
2033 MaskBlt(hdc,
2034 pMaskBlt->xDest,
2035 pMaskBlt->yDest,
2036 pMaskBlt->cxDest,
2037 pMaskBlt->cyDest,
2038 hdcSrc,
2039 pMaskBlt->xSrc,
2040 pMaskBlt->ySrc,
2041 hBmpMask,
2042 pMaskBlt->xMask,
2043 pMaskBlt->yMask,
2044 pMaskBlt->dwRop);
2045 SelectObject(hdcSrc, hBmpOld);
2046 DeleteObject(hBmp);
2047 DeleteObject(hBmpMask);
2048 DeleteDC(hdcSrc);
2049 break;
2052 case EMR_PLGBLT:
2054 const EMRPLGBLT *pPlgBlt = (const EMRPLGBLT *)mr;
2055 HDC hdcSrc = CreateCompatibleDC(hdc);
2056 HBRUSH hBrush, hBrushOld;
2057 HBITMAP hBmp, hBmpOld, hBmpMask;
2058 const BITMAPINFO *pbi;
2059 POINT pts[3];
2061 SetGraphicsMode(hdcSrc, GM_ADVANCED);
2062 SetWorldTransform(hdcSrc, &pPlgBlt->xformSrc);
2064 pts[0].x = pPlgBlt->aptlDest[0].x; pts[0].y = pPlgBlt->aptlDest[0].y;
2065 pts[1].x = pPlgBlt->aptlDest[1].x; pts[1].y = pPlgBlt->aptlDest[1].y;
2066 pts[2].x = pPlgBlt->aptlDest[2].x; pts[2].y = pPlgBlt->aptlDest[2].y;
2068 hBrush = CreateSolidBrush(pPlgBlt->crBkColorSrc);
2069 hBrushOld = SelectObject(hdcSrc, hBrush);
2070 PatBlt(hdcSrc, pPlgBlt->rclBounds.left, pPlgBlt->rclBounds.top,
2071 pPlgBlt->rclBounds.right - pPlgBlt->rclBounds.left,
2072 pPlgBlt->rclBounds.bottom - pPlgBlt->rclBounds.top, PATCOPY);
2073 SelectObject(hdcSrc, hBrushOld);
2074 DeleteObject(hBrush);
2076 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiMask);
2077 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
2078 1, 1, NULL);
2079 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
2080 (const BYTE *)mr + pPlgBlt->offBitsMask, pbi, pPlgBlt->iUsageMask);
2082 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiSrc);
2083 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
2084 (const BYTE *)mr + pPlgBlt->offBitsSrc, pbi, pPlgBlt->iUsageSrc);
2085 hBmpOld = SelectObject(hdcSrc, hBmp);
2086 PlgBlt(hdc,
2087 pts,
2088 hdcSrc,
2089 pPlgBlt->xSrc,
2090 pPlgBlt->ySrc,
2091 pPlgBlt->cxSrc,
2092 pPlgBlt->cySrc,
2093 hBmpMask,
2094 pPlgBlt->xMask,
2095 pPlgBlt->yMask);
2096 SelectObject(hdcSrc, hBmpOld);
2097 DeleteObject(hBmp);
2098 DeleteObject(hBmpMask);
2099 DeleteDC(hdcSrc);
2100 break;
2103 case EMR_SETDIBITSTODEVICE:
2105 const EMRSETDIBITSTODEVICE *pSetDIBitsToDevice = (const EMRSETDIBITSTODEVICE *)mr;
2107 SetDIBitsToDevice(hdc,
2108 pSetDIBitsToDevice->xDest,
2109 pSetDIBitsToDevice->yDest,
2110 pSetDIBitsToDevice->cxSrc,
2111 pSetDIBitsToDevice->cySrc,
2112 pSetDIBitsToDevice->xSrc,
2113 pSetDIBitsToDevice->ySrc,
2114 pSetDIBitsToDevice->iStartScan,
2115 pSetDIBitsToDevice->cScans,
2116 (const BYTE *)mr + pSetDIBitsToDevice->offBitsSrc,
2117 (const BITMAPINFO *)((const BYTE *)mr + pSetDIBitsToDevice->offBmiSrc),
2118 pSetDIBitsToDevice->iUsageSrc);
2119 break;
2122 case EMR_POLYTEXTOUTA:
2124 const EMRPOLYTEXTOUTA *pPolyTextOutA = (const EMRPOLYTEXTOUTA *)mr;
2125 POLYTEXTA *polytextA = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutA->cStrings * sizeof(POLYTEXTA));
2126 LONG i;
2127 XFORM xform, xformOld;
2128 int gModeOld;
2130 gModeOld = SetGraphicsMode(hdc, pPolyTextOutA->iGraphicsMode);
2131 GetWorldTransform(hdc, &xformOld);
2133 xform.eM11 = pPolyTextOutA->exScale;
2134 xform.eM12 = 0.0;
2135 xform.eM21 = 0.0;
2136 xform.eM22 = pPolyTextOutA->eyScale;
2137 xform.eDx = 0.0;
2138 xform.eDy = 0.0;
2139 SetWorldTransform(hdc, &xform);
2141 /* Set up POLYTEXTA structures */
2142 for(i = 0; i < pPolyTextOutA->cStrings; i++)
2144 polytextA[i].x = pPolyTextOutA->aemrtext[i].ptlReference.x;
2145 polytextA[i].y = pPolyTextOutA->aemrtext[i].ptlReference.y;
2146 polytextA[i].n = pPolyTextOutA->aemrtext[i].nChars;
2147 polytextA[i].lpstr = (LPCSTR)((const BYTE *)mr + pPolyTextOutA->aemrtext[i].offString);
2148 polytextA[i].uiFlags = pPolyTextOutA->aemrtext[i].fOptions;
2149 polytextA[i].rcl.left = pPolyTextOutA->aemrtext[i].rcl.left;
2150 polytextA[i].rcl.right = pPolyTextOutA->aemrtext[i].rcl.right;
2151 polytextA[i].rcl.top = pPolyTextOutA->aemrtext[i].rcl.top;
2152 polytextA[i].rcl.bottom = pPolyTextOutA->aemrtext[i].rcl.bottom;
2153 polytextA[i].pdx = (int *)((BYTE *)mr + pPolyTextOutA->aemrtext[i].offDx);
2155 PolyTextOutA(hdc, polytextA, pPolyTextOutA->cStrings);
2156 HeapFree(GetProcessHeap(), 0, polytextA);
2158 SetWorldTransform(hdc, &xformOld);
2159 SetGraphicsMode(hdc, gModeOld);
2160 break;
2163 case EMR_POLYTEXTOUTW:
2165 const EMRPOLYTEXTOUTW *pPolyTextOutW = (const EMRPOLYTEXTOUTW *)mr;
2166 POLYTEXTW *polytextW = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutW->cStrings * sizeof(POLYTEXTW));
2167 LONG i;
2168 XFORM xform, xformOld;
2169 int gModeOld;
2171 gModeOld = SetGraphicsMode(hdc, pPolyTextOutW->iGraphicsMode);
2172 GetWorldTransform(hdc, &xformOld);
2174 xform.eM11 = pPolyTextOutW->exScale;
2175 xform.eM12 = 0.0;
2176 xform.eM21 = 0.0;
2177 xform.eM22 = pPolyTextOutW->eyScale;
2178 xform.eDx = 0.0;
2179 xform.eDy = 0.0;
2180 SetWorldTransform(hdc, &xform);
2182 /* Set up POLYTEXTW structures */
2183 for(i = 0; i < pPolyTextOutW->cStrings; i++)
2185 polytextW[i].x = pPolyTextOutW->aemrtext[i].ptlReference.x;
2186 polytextW[i].y = pPolyTextOutW->aemrtext[i].ptlReference.y;
2187 polytextW[i].n = pPolyTextOutW->aemrtext[i].nChars;
2188 polytextW[i].lpstr = (LPCWSTR)((const BYTE *)mr + pPolyTextOutW->aemrtext[i].offString);
2189 polytextW[i].uiFlags = pPolyTextOutW->aemrtext[i].fOptions;
2190 polytextW[i].rcl.left = pPolyTextOutW->aemrtext[i].rcl.left;
2191 polytextW[i].rcl.right = pPolyTextOutW->aemrtext[i].rcl.right;
2192 polytextW[i].rcl.top = pPolyTextOutW->aemrtext[i].rcl.top;
2193 polytextW[i].rcl.bottom = pPolyTextOutW->aemrtext[i].rcl.bottom;
2194 polytextW[i].pdx = (int *)((BYTE *)mr + pPolyTextOutW->aemrtext[i].offDx);
2196 PolyTextOutW(hdc, polytextW, pPolyTextOutW->cStrings);
2197 HeapFree(GetProcessHeap(), 0, polytextW);
2199 SetWorldTransform(hdc, &xformOld);
2200 SetGraphicsMode(hdc, gModeOld);
2201 break;
2204 case EMR_FILLRGN:
2206 const EMRFILLRGN *pFillRgn = (const EMRFILLRGN *)mr;
2207 HRGN hRgn = ExtCreateRegion(NULL, pFillRgn->cbRgnData, (const RGNDATA *)pFillRgn->RgnData);
2208 FillRgn(hdc,
2209 hRgn,
2210 (handletable->objectHandle)[pFillRgn->ihBrush]);
2211 DeleteObject(hRgn);
2212 break;
2215 case EMR_FRAMERGN:
2217 const EMRFRAMERGN *pFrameRgn = (const EMRFRAMERGN *)mr;
2218 HRGN hRgn = ExtCreateRegion(NULL, pFrameRgn->cbRgnData, (const RGNDATA *)pFrameRgn->RgnData);
2219 FrameRgn(hdc,
2220 hRgn,
2221 (handletable->objectHandle)[pFrameRgn->ihBrush],
2222 pFrameRgn->szlStroke.cx,
2223 pFrameRgn->szlStroke.cy);
2224 DeleteObject(hRgn);
2225 break;
2228 case EMR_INVERTRGN:
2230 const EMRINVERTRGN *pInvertRgn = (const EMRINVERTRGN *)mr;
2231 HRGN hRgn = ExtCreateRegion(NULL, pInvertRgn->cbRgnData, (const RGNDATA *)pInvertRgn->RgnData);
2232 InvertRgn(hdc, hRgn);
2233 DeleteObject(hRgn);
2234 break;
2237 case EMR_PAINTRGN:
2239 const EMRPAINTRGN *pPaintRgn = (const EMRPAINTRGN *)mr;
2240 HRGN hRgn = ExtCreateRegion(NULL, pPaintRgn->cbRgnData, (const RGNDATA *)pPaintRgn->RgnData);
2241 PaintRgn(hdc, hRgn);
2242 DeleteObject(hRgn);
2243 break;
2246 case EMR_SETTEXTJUSTIFICATION:
2248 const EMRSETTEXTJUSTIFICATION *pSetTextJust = (const EMRSETTEXTJUSTIFICATION *)mr;
2249 SetTextJustification(hdc, pSetTextJust->nBreakExtra, pSetTextJust->nBreakCount);
2250 break;
2253 case EMR_SETLAYOUT:
2255 const EMRSETLAYOUT *pSetLayout = (const EMRSETLAYOUT *)mr;
2256 SetLayout(hdc, pSetLayout->iMode);
2257 break;
2260 case EMR_GRADIENTFILL:
2262 EMRGRADIENTFILL *grad = (EMRGRADIENTFILL *)mr;
2263 GdiGradientFill( hdc, grad->Ver, grad->nVer, grad->Ver + grad->nVer,
2264 grad->nTri, grad->ulMode );
2265 break;
2268 case EMR_GLSRECORD:
2269 case EMR_GLSBOUNDEDRECORD:
2270 case EMR_DRAWESCAPE:
2271 case EMR_EXTESCAPE:
2272 case EMR_STARTDOC:
2273 case EMR_SMALLTEXTOUT:
2274 case EMR_FORCEUFIMAPPING:
2275 case EMR_NAMEDESCAPE:
2276 case EMR_COLORCORRECTPALETTE:
2277 case EMR_SETICMPROFILEA:
2278 case EMR_SETICMPROFILEW:
2279 case EMR_TRANSPARENTBLT:
2280 case EMR_SETLINKEDUFI:
2281 case EMR_COLORMATCHTOTARGETW:
2282 case EMR_CREATECOLORSPACEW:
2284 default:
2285 /* From docs: If PlayEnhMetaFileRecord doesn't recognize a
2286 record then ignore and return TRUE. */
2287 FIXME("type %d is unimplemented\n", type);
2288 break;
2290 tmprc.left = tmprc.top = 0;
2291 tmprc.right = tmprc.bottom = 1000;
2292 LPtoDP(hdc, (POINT*)&tmprc, 2);
2293 TRACE("L:0,0 - 1000,1000 -> D:%s\n", wine_dbgstr_rect(&tmprc));
2295 return TRUE;
2299 /*****************************************************************************
2301 * EnumEnhMetaFile (GDI32.@)
2303 * Walk an enhanced metafile, calling a user-specified function _EnhMetaFunc_
2304 * for each
2305 * record. Returns when either every record has been used or
2306 * when _EnhMetaFunc_ returns FALSE.
2309 * RETURNS
2310 * TRUE if every record is used, FALSE if any invocation of _EnhMetaFunc_
2311 * returns FALSE.
2313 * BUGS
2314 * Ignores rect.
2316 * NOTES
2317 * This function behaves differently in Win9x and WinNT.
2319 * In WinNT, the DC's world transform is updated as the EMF changes
2320 * the Window/Viewport Extent and Origin or its world transform.
2321 * The actual Window/Viewport Extent and Origin are left untouched.
2323 * In Win9x, the DC is left untouched, and PlayEnhMetaFileRecord
2324 * updates the scaling itself but only just before a record that
2325 * writes anything to the DC.
2327 * I'm not sure where the data (enum_emh_data) is stored in either
2328 * version. For this implementation, it is stored before the handle
2329 * table, but it could be stored in the DC, in the EMF handle or in
2330 * TLS.
2331 * MJM 5 Oct 2002
2333 BOOL WINAPI EnumEnhMetaFile(
2334 HDC hdc, /* [in] device context to pass to _EnhMetaFunc_ */
2335 HENHMETAFILE hmf, /* [in] EMF to walk */
2336 ENHMFENUMPROC callback, /* [in] callback function */
2337 LPVOID data, /* [in] optional data for callback function */
2338 const RECT *lpRect /* [in] bounding rectangle for rendered metafile */
2341 BOOL ret;
2342 ENHMETAHEADER *emh;
2343 ENHMETARECORD *emr;
2344 DWORD offset;
2345 UINT i;
2346 HANDLETABLE *ht;
2347 INT savedMode = 0;
2348 XFORM savedXform;
2349 HPEN hPen = NULL;
2350 HBRUSH hBrush = NULL;
2351 HFONT hFont = NULL;
2352 HRGN hRgn = NULL;
2353 enum_emh_data *info;
2354 SIZE vp_size, win_size;
2355 POINT vp_org, win_org;
2356 INT mapMode = MM_TEXT, old_align = 0, old_rop2 = 0, old_arcdir = 0, old_polyfill = 0, old_stretchblt = 0;
2357 COLORREF old_text_color = 0, old_bk_color = 0;
2359 if(!lpRect && hdc)
2361 SetLastError(ERROR_INVALID_PARAMETER);
2362 return FALSE;
2365 emh = EMF_GetEnhMetaHeader(hmf);
2366 if(!emh) {
2367 SetLastError(ERROR_INVALID_HANDLE);
2368 return FALSE;
2371 info = HeapAlloc( GetProcessHeap(), 0,
2372 sizeof (enum_emh_data) + sizeof(HANDLETABLE) * emh->nHandles );
2373 if(!info)
2375 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2376 return FALSE;
2378 info->state.mode = MM_TEXT;
2379 info->state.wndOrgX = 0;
2380 info->state.wndOrgY = 0;
2381 info->state.wndExtX = 1;
2382 info->state.wndExtY = 1;
2383 info->state.vportOrgX = 0;
2384 info->state.vportOrgY = 0;
2385 info->state.vportExtX = 1;
2386 info->state.vportExtY = 1;
2387 info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
2388 info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
2389 info->state.world_transform.eDx = info->state.world_transform.eDy = 0;
2391 info->state.next = NULL;
2392 info->save_level = 0;
2393 info->saved_state = NULL;
2395 ht = (HANDLETABLE*) &info[1];
2396 ht->objectHandle[0] = hmf;
2397 for(i = 1; i < emh->nHandles; i++)
2398 ht->objectHandle[i] = NULL;
2400 if(hdc)
2402 savedMode = SetGraphicsMode(hdc, GM_ADVANCED);
2403 GetWorldTransform(hdc, &savedXform);
2404 GetViewportExtEx(hdc, &vp_size);
2405 GetWindowExtEx(hdc, &win_size);
2406 GetViewportOrgEx(hdc, &vp_org);
2407 GetWindowOrgEx(hdc, &win_org);
2408 mapMode = GetMapMode(hdc);
2410 /* save DC */
2411 hPen = GetCurrentObject(hdc, OBJ_PEN);
2412 hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
2413 hFont = GetCurrentObject(hdc, OBJ_FONT);
2415 hRgn = CreateRectRgn(0, 0, 0, 0);
2416 if (!GetClipRgn(hdc, hRgn))
2418 DeleteObject(hRgn);
2419 hRgn = 0;
2422 old_text_color = SetTextColor(hdc, RGB(0,0,0));
2423 old_bk_color = SetBkColor(hdc, RGB(0xff, 0xff, 0xff));
2424 old_align = SetTextAlign(hdc, 0);
2425 old_rop2 = SetROP2(hdc, R2_COPYPEN);
2426 old_arcdir = SetArcDirection(hdc, AD_COUNTERCLOCKWISE);
2427 old_polyfill = SetPolyFillMode(hdc, ALTERNATE);
2428 old_stretchblt = SetStretchBltMode(hdc, BLACKONWHITE);
2430 if ( IS_WIN9X() )
2432 /* Win95 leaves the vp/win ext/org info alone */
2433 info->init_transform.eM11 = 1.0;
2434 info->init_transform.eM12 = 0.0;
2435 info->init_transform.eM21 = 0.0;
2436 info->init_transform.eM22 = 1.0;
2437 info->init_transform.eDx = 0.0;
2438 info->init_transform.eDy = 0.0;
2440 else
2442 /* WinNT combines the vp/win ext/org info into a transform */
2443 double xscale, yscale;
2444 xscale = (double)vp_size.cx / (double)win_size.cx;
2445 yscale = (double)vp_size.cy / (double)win_size.cy;
2446 info->init_transform.eM11 = xscale;
2447 info->init_transform.eM12 = 0.0;
2448 info->init_transform.eM21 = 0.0;
2449 info->init_transform.eM22 = yscale;
2450 info->init_transform.eDx = (double)vp_org.x - xscale * (double)win_org.x;
2451 info->init_transform.eDy = (double)vp_org.y - yscale * (double)win_org.y;
2453 CombineTransform(&info->init_transform, &savedXform, &info->init_transform);
2456 if ( lpRect && WIDTH(emh->rclFrame) && HEIGHT(emh->rclFrame) )
2458 double xSrcPixSize, ySrcPixSize, xscale, yscale;
2459 XFORM xform;
2461 TRACE("rect: %s. rclFrame: (%d,%d)-(%d,%d)\n", wine_dbgstr_rect(lpRect),
2462 emh->rclFrame.left, emh->rclFrame.top, emh->rclFrame.right,
2463 emh->rclFrame.bottom);
2465 xSrcPixSize = (double) emh->szlMillimeters.cx / emh->szlDevice.cx;
2466 ySrcPixSize = (double) emh->szlMillimeters.cy / emh->szlDevice.cy;
2467 xscale = (double) WIDTH(*lpRect) * 100.0 /
2468 WIDTH(emh->rclFrame) * xSrcPixSize;
2469 yscale = (double) HEIGHT(*lpRect) * 100.0 /
2470 HEIGHT(emh->rclFrame) * ySrcPixSize;
2471 TRACE("xscale = %f, yscale = %f\n", xscale, yscale);
2473 xform.eM11 = xscale;
2474 xform.eM12 = 0;
2475 xform.eM21 = 0;
2476 xform.eM22 = yscale;
2477 xform.eDx = (double) lpRect->left - (double) WIDTH(*lpRect) / WIDTH(emh->rclFrame) * emh->rclFrame.left;
2478 xform.eDy = (double) lpRect->top - (double) HEIGHT(*lpRect) / HEIGHT(emh->rclFrame) * emh->rclFrame.top;
2480 CombineTransform(&info->init_transform, &xform, &info->init_transform);
2483 /* WinNT resets the current vp/win org/ext */
2484 if ( !IS_WIN9X() )
2486 SetMapMode(hdc, MM_TEXT);
2487 SetWindowOrgEx(hdc, 0, 0, NULL);
2488 SetViewportOrgEx(hdc, 0, 0, NULL);
2489 EMF_Update_MF_Xform(hdc, info);
2493 ret = TRUE;
2494 offset = 0;
2495 while(ret && offset < emh->nBytes)
2497 emr = (ENHMETARECORD *)((char *)emh + offset);
2499 if (offset + 8 > emh->nBytes ||
2500 offset > offset + emr->nSize ||
2501 offset + emr->nSize > emh->nBytes)
2503 WARN("record truncated\n");
2504 break;
2507 /* In Win9x mode we update the xform if the record will produce output */
2508 if (hdc && IS_WIN9X() && emr_produces_output(emr->iType))
2509 EMF_Update_MF_Xform(hdc, info);
2511 TRACE("Calling EnumFunc with record %s, size %d\n", get_emr_name(emr->iType), emr->nSize);
2512 ret = (*callback)(hdc, ht, emr, emh->nHandles, (LPARAM)data);
2513 offset += emr->nSize;
2516 if (hdc)
2518 SetStretchBltMode(hdc, old_stretchblt);
2519 SetPolyFillMode(hdc, old_polyfill);
2520 SetArcDirection(hdc, old_arcdir);
2521 SetROP2(hdc, old_rop2);
2522 SetTextAlign(hdc, old_align);
2523 SetBkColor(hdc, old_bk_color);
2524 SetTextColor(hdc, old_text_color);
2526 /* restore DC */
2527 SelectObject(hdc, hBrush);
2528 SelectObject(hdc, hPen);
2529 SelectObject(hdc, hFont);
2530 ExtSelectClipRgn(hdc, hRgn, RGN_COPY);
2531 DeleteObject(hRgn);
2533 SetWorldTransform(hdc, &savedXform);
2534 if (savedMode)
2535 SetGraphicsMode(hdc, savedMode);
2536 SetMapMode(hdc, mapMode);
2537 SetWindowOrgEx(hdc, win_org.x, win_org.y, NULL);
2538 SetWindowExtEx(hdc, win_size.cx, win_size.cy, NULL);
2539 SetViewportOrgEx(hdc, vp_org.x, vp_org.y, NULL);
2540 SetViewportExtEx(hdc, vp_size.cx, vp_size.cy, NULL);
2543 for(i = 1; i < emh->nHandles; i++) /* Don't delete element 0 (hmf) */
2544 if( (ht->objectHandle)[i] )
2545 DeleteObject( (ht->objectHandle)[i] );
2547 while (info->saved_state)
2549 EMF_dc_state *state = info->saved_state;
2550 info->saved_state = info->saved_state->next;
2551 HeapFree( GetProcessHeap(), 0, state );
2553 HeapFree( GetProcessHeap(), 0, info );
2554 return ret;
2557 static INT CALLBACK EMF_PlayEnhMetaFileCallback(HDC hdc, HANDLETABLE *ht,
2558 const ENHMETARECORD *emr,
2559 INT handles, LPARAM data)
2561 return PlayEnhMetaFileRecord(hdc, ht, emr, handles);
2564 /**************************************************************************
2565 * PlayEnhMetaFile (GDI32.@)
2567 * Renders an enhanced metafile into a specified rectangle *lpRect
2568 * in device context hdc.
2570 * RETURNS
2571 * Success: TRUE
2572 * Failure: FALSE
2574 BOOL WINAPI PlayEnhMetaFile(
2575 HDC hdc, /* [in] DC to render into */
2576 HENHMETAFILE hmf, /* [in] metafile to render */
2577 const RECT *lpRect /* [in] rectangle to place metafile inside */
2580 return EnumEnhMetaFile(hdc, hmf, EMF_PlayEnhMetaFileCallback, NULL,
2581 lpRect);
2584 /*****************************************************************************
2585 * DeleteEnhMetaFile (GDI32.@)
2587 * Deletes an enhanced metafile and frees the associated storage.
2589 BOOL WINAPI DeleteEnhMetaFile(HENHMETAFILE hmf)
2591 return EMF_Delete_HENHMETAFILE( hmf );
2594 /*****************************************************************************
2595 * CopyEnhMetaFileA (GDI32.@)
2597 * Duplicate an enhanced metafile.
2601 HENHMETAFILE WINAPI CopyEnhMetaFileA(
2602 HENHMETAFILE hmfSrc,
2603 LPCSTR file)
2605 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2606 HENHMETAFILE hmfDst;
2608 if(!emrSrc) return FALSE;
2609 if (!file) {
2610 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2611 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2612 hmfDst = EMF_Create_HENHMETAFILE( emrDst, emrSrc->nBytes, FALSE );
2613 if (!hmfDst)
2614 HeapFree( GetProcessHeap(), 0, emrDst );
2615 } else {
2616 HANDLE hFile;
2617 DWORD w;
2618 hFile = CreateFileA( file, GENERIC_WRITE | GENERIC_READ, 0,
2619 NULL, CREATE_ALWAYS, 0, 0);
2620 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2621 CloseHandle( hFile );
2622 /* Reopen file for reading only, so that apps can share
2623 read access to the file while hmf is still valid */
2624 hFile = CreateFileA( file, GENERIC_READ, FILE_SHARE_READ,
2625 NULL, OPEN_EXISTING, 0, 0);
2626 if(hFile == INVALID_HANDLE_VALUE) {
2627 ERR("Can't reopen emf for reading\n");
2628 return 0;
2630 hmfDst = EMF_GetEnhMetaFile( hFile );
2631 CloseHandle( hFile );
2633 return hmfDst;
2636 /*****************************************************************************
2637 * CopyEnhMetaFileW (GDI32.@)
2639 * See CopyEnhMetaFileA.
2643 HENHMETAFILE WINAPI CopyEnhMetaFileW(
2644 HENHMETAFILE hmfSrc,
2645 LPCWSTR file)
2647 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2648 HENHMETAFILE hmfDst;
2650 if(!emrSrc) return FALSE;
2651 if (!file) {
2652 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2653 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2654 hmfDst = EMF_Create_HENHMETAFILE( emrDst, emrSrc->nBytes, FALSE );
2655 if (!hmfDst)
2656 HeapFree( GetProcessHeap(), 0, emrDst );
2657 } else {
2658 HANDLE hFile;
2659 DWORD w;
2660 hFile = CreateFileW( file, GENERIC_WRITE | GENERIC_READ, 0,
2661 NULL, CREATE_ALWAYS, 0, 0);
2662 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2663 CloseHandle( hFile );
2664 /* Reopen file for reading only, so that apps can share
2665 read access to the file while hmf is still valid */
2666 hFile = CreateFileW( file, GENERIC_READ, FILE_SHARE_READ,
2667 NULL, OPEN_EXISTING, 0, 0);
2668 if(hFile == INVALID_HANDLE_VALUE) {
2669 ERR("Can't reopen emf for reading\n");
2670 return 0;
2672 hmfDst = EMF_GetEnhMetaFile( hFile );
2673 CloseHandle( hFile );
2675 return hmfDst;
2679 /* Struct to be used to be passed in the LPVOID parameter for cbEnhPaletteCopy */
2680 typedef struct tagEMF_PaletteCopy
2682 UINT cEntries;
2683 LPPALETTEENTRY lpPe;
2684 } EMF_PaletteCopy;
2686 /***************************************************************
2687 * Find the EMR_EOF record and then use it to find the
2688 * palette entries for this enhanced metafile.
2689 * The lpData is actually a pointer to an EMF_PaletteCopy struct
2690 * which contains the max number of elements to copy and where
2691 * to copy them to.
2693 * NOTE: To be used by GetEnhMetaFilePaletteEntries only!
2695 static INT CALLBACK cbEnhPaletteCopy( HDC a,
2696 HANDLETABLE *b,
2697 const ENHMETARECORD *lpEMR,
2698 INT c,
2699 LPARAM lpData )
2702 if ( lpEMR->iType == EMR_EOF )
2704 const EMREOF *lpEof = (const EMREOF *)lpEMR;
2705 EMF_PaletteCopy* info = (EMF_PaletteCopy*)lpData;
2706 DWORD dwNumPalToCopy = min( lpEof->nPalEntries, info->cEntries );
2708 TRACE( "copying 0x%08x palettes\n", dwNumPalToCopy );
2710 memcpy( info->lpPe, (LPCSTR)lpEof + lpEof->offPalEntries,
2711 sizeof( *(info->lpPe) ) * dwNumPalToCopy );
2713 /* Update the passed data as a return code */
2714 info->lpPe = NULL; /* Palettes were copied! */
2715 info->cEntries = dwNumPalToCopy;
2717 return FALSE; /* That's all we need */
2720 return TRUE;
2723 /*****************************************************************************
2724 * GetEnhMetaFilePaletteEntries (GDI32.@)
2726 * Copy the palette and report size
2728 * BUGS: Error codes (SetLastError) are not set on failures
2730 UINT WINAPI GetEnhMetaFilePaletteEntries( HENHMETAFILE hEmf,
2731 UINT cEntries,
2732 LPPALETTEENTRY lpPe )
2734 ENHMETAHEADER* enhHeader = EMF_GetEnhMetaHeader( hEmf );
2735 EMF_PaletteCopy infoForCallBack;
2737 TRACE( "(%p,%d,%p)\n", hEmf, cEntries, lpPe );
2739 if (!enhHeader) return 0;
2741 /* First check if there are any palettes associated with
2742 this metafile. */
2743 if ( enhHeader->nPalEntries == 0 ) return 0;
2745 /* Is the user requesting the number of palettes? */
2746 if ( lpPe == NULL ) return enhHeader->nPalEntries;
2748 /* Copy cEntries worth of PALETTEENTRY structs into the buffer */
2749 infoForCallBack.cEntries = cEntries;
2750 infoForCallBack.lpPe = lpPe;
2752 if ( !EnumEnhMetaFile( 0, hEmf, cbEnhPaletteCopy,
2753 &infoForCallBack, 0 ) )
2754 return GDI_ERROR;
2756 /* Verify that the callback executed correctly */
2757 if ( infoForCallBack.lpPe != NULL )
2759 /* Callback proc had error! */
2760 ERR( "cbEnhPaletteCopy didn't execute correctly\n" );
2761 return GDI_ERROR;
2764 return infoForCallBack.cEntries;
2767 /******************************************************************
2768 * extract_emf_from_comment
2770 * If the WMF was created by GetWinMetaFileBits, then extract the
2771 * original EMF that is stored in MFCOMMENT chunks.
2773 static HENHMETAFILE extract_emf_from_comment( const BYTE *buf, UINT mf_size )
2775 METAHEADER *mh = (METAHEADER *)buf;
2776 METARECORD *mr;
2777 emf_in_wmf_comment *chunk;
2778 WORD checksum = 0;
2779 DWORD size = 0, remaining, chunks;
2780 BYTE *emf_bits = NULL, *ptr;
2781 UINT offset;
2782 HENHMETAFILE emf = NULL;
2784 if (mf_size < sizeof(*mh)) return NULL;
2786 for (offset = mh->mtHeaderSize * 2; offset < mf_size; offset += (mr->rdSize * 2))
2788 mr = (METARECORD *)((char *)mh + offset);
2789 chunk = (emf_in_wmf_comment *)(mr->rdParm + 2);
2791 if (mr->rdFunction != META_ESCAPE || mr->rdParm[0] != MFCOMMENT) goto done;
2792 if (chunk->magic != WMFC_MAGIC) goto done;
2794 if (!emf_bits)
2796 size = remaining = chunk->emf_size;
2797 chunks = chunk->num_chunks;
2798 emf_bits = ptr = HeapAlloc( GetProcessHeap(), 0, size );
2799 if (!emf_bits) goto done;
2801 if (chunk->chunk_size > remaining) goto done;
2802 remaining -= chunk->chunk_size;
2803 if (chunk->remaining_size != remaining) goto done;
2804 memcpy( ptr, chunk->emf_data, chunk->chunk_size );
2805 ptr += chunk->chunk_size;
2806 if (--chunks == 0) break;
2809 for (offset = 0; offset < mf_size / 2; offset++)
2810 checksum += *((WORD *)buf + offset);
2811 if (checksum) goto done;
2813 emf = SetEnhMetaFileBits( size, emf_bits );
2815 done:
2816 HeapFree( GetProcessHeap(), 0, emf_bits );
2817 return emf;
2820 typedef struct wmf_in_emf_comment
2822 DWORD ident;
2823 DWORD iComment;
2824 DWORD nVersion;
2825 DWORD nChecksum;
2826 DWORD fFlags;
2827 DWORD cbWinMetaFile;
2828 } wmf_in_emf_comment;
2830 /******************************************************************
2831 * SetWinMetaFileBits (GDI32.@)
2833 * Translate from old style to new style.
2836 HENHMETAFILE WINAPI SetWinMetaFileBits(UINT cbBuffer, const BYTE *lpbBuffer, HDC hdcRef,
2837 const METAFILEPICT *lpmfp)
2839 HMETAFILE hmf = NULL;
2840 HENHMETAFILE ret = NULL;
2841 HDC hdc = NULL, hdcdisp = NULL;
2842 RECT rc, *prcFrame = NULL;
2843 LONG mm, xExt, yExt;
2844 INT horzsize, vertsize, horzres, vertres;
2846 TRACE("(%d, %p, %p, %p)\n", cbBuffer, lpbBuffer, hdcRef, lpmfp);
2848 hmf = SetMetaFileBitsEx(cbBuffer, lpbBuffer);
2849 if(!hmf)
2851 WARN("SetMetaFileBitsEx failed\n");
2852 return NULL;
2855 ret = extract_emf_from_comment( lpbBuffer, cbBuffer );
2856 if (ret) return ret;
2858 if(!hdcRef)
2859 hdcRef = hdcdisp = CreateDCW(L"DISPLAY", NULL, NULL, NULL);
2861 if (lpmfp)
2863 TRACE("mm = %d %dx%d\n", lpmfp->mm, lpmfp->xExt, lpmfp->yExt);
2865 mm = lpmfp->mm;
2866 xExt = lpmfp->xExt;
2867 yExt = lpmfp->yExt;
2869 else
2871 TRACE("lpmfp == NULL\n");
2873 /* Use the whole device surface */
2874 mm = MM_ANISOTROPIC;
2875 xExt = 0;
2876 yExt = 0;
2879 if (mm == MM_ISOTROPIC || mm == MM_ANISOTROPIC)
2881 if (xExt < 0 || yExt < 0)
2883 /* Use the whole device surface */
2884 xExt = 0;
2885 yExt = 0;
2888 /* Use the x and y extents as the frame box */
2889 if (xExt && yExt)
2891 rc.left = rc.top = 0;
2892 rc.right = xExt;
2893 rc.bottom = yExt;
2894 prcFrame = &rc;
2898 if(!(hdc = CreateEnhMetaFileW(hdcRef, NULL, prcFrame, NULL)))
2900 ERR("CreateEnhMetaFile failed\n");
2901 goto end;
2905 * Write the original METAFILE into the enhanced metafile.
2906 * It is encapsulated in a GDICOMMENT_WINDOWS_METAFILE record.
2908 if (mm != MM_TEXT)
2910 wmf_in_emf_comment *mfcomment;
2911 UINT mfcomment_size;
2913 mfcomment_size = sizeof (*mfcomment) + cbBuffer;
2914 mfcomment = HeapAlloc(GetProcessHeap(), 0, mfcomment_size);
2915 if (mfcomment)
2917 mfcomment->ident = GDICOMMENT_IDENTIFIER;
2918 mfcomment->iComment = GDICOMMENT_WINDOWS_METAFILE;
2919 mfcomment->nVersion = 0x00000300;
2920 mfcomment->nChecksum = 0; /* FIXME */
2921 mfcomment->fFlags = 0;
2922 mfcomment->cbWinMetaFile = cbBuffer;
2923 memcpy(&mfcomment[1], lpbBuffer, cbBuffer);
2924 GdiComment(hdc, mfcomment_size, (BYTE*) mfcomment);
2925 HeapFree(GetProcessHeap(), 0, mfcomment);
2927 SetMapMode(hdc, mm);
2931 horzsize = GetDeviceCaps(hdcRef, HORZSIZE);
2932 vertsize = GetDeviceCaps(hdcRef, VERTSIZE);
2933 horzres = GetDeviceCaps(hdcRef, HORZRES);
2934 vertres = GetDeviceCaps(hdcRef, VERTRES);
2936 if (!xExt || !yExt)
2938 /* Use the whole device surface */
2939 xExt = horzres;
2940 yExt = vertres;
2942 else
2944 xExt = MulDiv(xExt, horzres, 100 * horzsize);
2945 yExt = MulDiv(yExt, vertres, 100 * vertsize);
2948 /* set the initial viewport:window ratio as 1:1 */
2949 SetViewportExtEx(hdc, xExt, yExt, NULL);
2950 SetWindowExtEx(hdc, xExt, yExt, NULL);
2952 PlayMetaFile(hdc, hmf);
2954 ret = CloseEnhMetaFile(hdc);
2955 end:
2956 if (hdcdisp) DeleteDC(hdcdisp);
2957 DeleteMetaFile(hmf);
2958 return ret;