riched20: Return paragraph ptrs from the remaining table insert functions.
[wine.git] / dlls / gdi32 / enhmetafile.c
blob666828db1f4caa114d21a1f0d201744c5bb9a2ee
1 /*
2 * Enhanced metafile functions
3 * Copyright 1998 Douglas Ridgway
4 * 1999 Huw D M Davies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * NOTES:
22 * The enhanced format consists of the following elements:
24 * A header
25 * A table of handles to GDI objects
26 * An array of metafile records
27 * A private palette
30 * The standard format consists of a header and an array of metafile records.
34 #include "config.h"
35 #include "wine/port.h"
37 #include <stdarg.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <assert.h>
41 #include "windef.h"
42 #include "winbase.h"
43 #include "wingdi.h"
44 #include "winnls.h"
45 #include "winerror.h"
46 #include "gdi_private.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
51 typedef struct
53 ENHMETAHEADER *emh;
54 BOOL on_disk; /* true if metafile is on disk */
55 } ENHMETAFILEOBJ;
57 static const struct emr_name {
58 DWORD type;
59 const char *name;
60 } emr_names[] = {
61 #define X(p) {p, #p}
62 X(EMR_HEADER),
63 X(EMR_POLYBEZIER),
64 X(EMR_POLYGON),
65 X(EMR_POLYLINE),
66 X(EMR_POLYBEZIERTO),
67 X(EMR_POLYLINETO),
68 X(EMR_POLYPOLYLINE),
69 X(EMR_POLYPOLYGON),
70 X(EMR_SETWINDOWEXTEX),
71 X(EMR_SETWINDOWORGEX),
72 X(EMR_SETVIEWPORTEXTEX),
73 X(EMR_SETVIEWPORTORGEX),
74 X(EMR_SETBRUSHORGEX),
75 X(EMR_EOF),
76 X(EMR_SETPIXELV),
77 X(EMR_SETMAPPERFLAGS),
78 X(EMR_SETMAPMODE),
79 X(EMR_SETBKMODE),
80 X(EMR_SETPOLYFILLMODE),
81 X(EMR_SETROP2),
82 X(EMR_SETSTRETCHBLTMODE),
83 X(EMR_SETTEXTALIGN),
84 X(EMR_SETCOLORADJUSTMENT),
85 X(EMR_SETTEXTCOLOR),
86 X(EMR_SETBKCOLOR),
87 X(EMR_OFFSETCLIPRGN),
88 X(EMR_MOVETOEX),
89 X(EMR_SETMETARGN),
90 X(EMR_EXCLUDECLIPRECT),
91 X(EMR_INTERSECTCLIPRECT),
92 X(EMR_SCALEVIEWPORTEXTEX),
93 X(EMR_SCALEWINDOWEXTEX),
94 X(EMR_SAVEDC),
95 X(EMR_RESTOREDC),
96 X(EMR_SETWORLDTRANSFORM),
97 X(EMR_MODIFYWORLDTRANSFORM),
98 X(EMR_SELECTOBJECT),
99 X(EMR_CREATEPEN),
100 X(EMR_CREATEBRUSHINDIRECT),
101 X(EMR_DELETEOBJECT),
102 X(EMR_ANGLEARC),
103 X(EMR_ELLIPSE),
104 X(EMR_RECTANGLE),
105 X(EMR_ROUNDRECT),
106 X(EMR_ARC),
107 X(EMR_CHORD),
108 X(EMR_PIE),
109 X(EMR_SELECTPALETTE),
110 X(EMR_CREATEPALETTE),
111 X(EMR_SETPALETTEENTRIES),
112 X(EMR_RESIZEPALETTE),
113 X(EMR_REALIZEPALETTE),
114 X(EMR_EXTFLOODFILL),
115 X(EMR_LINETO),
116 X(EMR_ARCTO),
117 X(EMR_POLYDRAW),
118 X(EMR_SETARCDIRECTION),
119 X(EMR_SETMITERLIMIT),
120 X(EMR_BEGINPATH),
121 X(EMR_ENDPATH),
122 X(EMR_CLOSEFIGURE),
123 X(EMR_FILLPATH),
124 X(EMR_STROKEANDFILLPATH),
125 X(EMR_STROKEPATH),
126 X(EMR_FLATTENPATH),
127 X(EMR_WIDENPATH),
128 X(EMR_SELECTCLIPPATH),
129 X(EMR_ABORTPATH),
130 X(EMR_GDICOMMENT),
131 X(EMR_FILLRGN),
132 X(EMR_FRAMERGN),
133 X(EMR_INVERTRGN),
134 X(EMR_PAINTRGN),
135 X(EMR_EXTSELECTCLIPRGN),
136 X(EMR_BITBLT),
137 X(EMR_STRETCHBLT),
138 X(EMR_MASKBLT),
139 X(EMR_PLGBLT),
140 X(EMR_SETDIBITSTODEVICE),
141 X(EMR_STRETCHDIBITS),
142 X(EMR_EXTCREATEFONTINDIRECTW),
143 X(EMR_EXTTEXTOUTA),
144 X(EMR_EXTTEXTOUTW),
145 X(EMR_POLYBEZIER16),
146 X(EMR_POLYGON16),
147 X(EMR_POLYLINE16),
148 X(EMR_POLYBEZIERTO16),
149 X(EMR_POLYLINETO16),
150 X(EMR_POLYPOLYLINE16),
151 X(EMR_POLYPOLYGON16),
152 X(EMR_POLYDRAW16),
153 X(EMR_CREATEMONOBRUSH),
154 X(EMR_CREATEDIBPATTERNBRUSHPT),
155 X(EMR_EXTCREATEPEN),
156 X(EMR_POLYTEXTOUTA),
157 X(EMR_POLYTEXTOUTW),
158 X(EMR_SETICMMODE),
159 X(EMR_CREATECOLORSPACE),
160 X(EMR_SETCOLORSPACE),
161 X(EMR_DELETECOLORSPACE),
162 X(EMR_GLSRECORD),
163 X(EMR_GLSBOUNDEDRECORD),
164 X(EMR_PIXELFORMAT),
165 X(EMR_DRAWESCAPE),
166 X(EMR_EXTESCAPE),
167 X(EMR_STARTDOC),
168 X(EMR_SMALLTEXTOUT),
169 X(EMR_FORCEUFIMAPPING),
170 X(EMR_NAMEDESCAPE),
171 X(EMR_COLORCORRECTPALETTE),
172 X(EMR_SETICMPROFILEA),
173 X(EMR_SETICMPROFILEW),
174 X(EMR_ALPHABLEND),
175 X(EMR_SETLAYOUT),
176 X(EMR_TRANSPARENTBLT),
177 X(EMR_RESERVED_117),
178 X(EMR_GRADIENTFILL),
179 X(EMR_SETLINKEDUFI),
180 X(EMR_SETTEXTJUSTIFICATION),
181 X(EMR_COLORMATCHTOTARGETW),
182 X(EMR_CREATECOLORSPACEW)
183 #undef X
186 /****************************************************************************
187 * get_emr_name
189 static const char *get_emr_name(DWORD type)
191 unsigned int i;
192 for(i = 0; i < ARRAY_SIZE(emr_names); i++)
193 if(type == emr_names[i].type) return emr_names[i].name;
194 TRACE("Unknown record type %d\n", type);
195 return NULL;
198 /***********************************************************************
199 * is_dib_monochrome
201 * Returns whether a DIB can be converted to a monochrome DDB.
203 * A DIB can be converted if its color table contains only black and
204 * white. Black must be the first color in the color table.
206 * Note : If the first color in the color table is white followed by
207 * black, we can't convert it to a monochrome DDB with
208 * SetDIBits, because black and white would be inverted.
210 static inline BOOL is_dib_monochrome( const BITMAPINFO* info )
212 if (info->bmiHeader.biBitCount != 1) return FALSE;
214 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
216 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO *) info)->bmciColors;
218 /* Check if the first color is black */
219 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
221 rgb++;
222 /* Check if the second color is white */
223 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
224 && (rgb->rgbtBlue == 0xff));
226 else return FALSE;
228 else /* assume BITMAPINFOHEADER */
230 const RGBQUAD *rgb = info->bmiColors;
232 /* Check if the first color is black */
233 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
234 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
236 rgb++;
238 /* Check if the second color is white */
239 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
240 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
242 else return FALSE;
246 /****************************************************************************
247 * EMF_Create_HENHMETAFILE
249 HENHMETAFILE EMF_Create_HENHMETAFILE(ENHMETAHEADER *emh, DWORD filesize, BOOL on_disk )
251 HENHMETAFILE hmf;
252 ENHMETAFILEOBJ *metaObj;
254 if (filesize < sizeof(*emh))
256 WARN("File too small for emf header\n");
257 return 0;
259 if (emh->iType != EMR_HEADER)
261 SetLastError(ERROR_INVALID_DATA);
262 return 0;
264 if (emh->dSignature != ENHMETA_SIGNATURE ||
265 (emh->nBytes & 3)) /* refuse to load unaligned EMF as Windows does */
267 WARN("Invalid emf header type 0x%08x sig 0x%08x.\n",
268 emh->iType, emh->dSignature);
269 return 0;
271 if (filesize < emh->nBytes)
273 WARN("File truncated (got %u bytes, header says %u)\n", emh->nBytes, filesize);
274 return 0;
277 if (!(metaObj = HeapAlloc( GetProcessHeap(), 0, sizeof(*metaObj) ))) return 0;
279 metaObj->emh = emh;
280 metaObj->on_disk = on_disk;
282 if (!(hmf = alloc_gdi_handle( metaObj, OBJ_ENHMETAFILE, NULL )))
283 HeapFree( GetProcessHeap(), 0, metaObj );
284 return hmf;
287 /****************************************************************************
288 * EMF_Delete_HENHMETAFILE
290 static BOOL EMF_Delete_HENHMETAFILE( HENHMETAFILE hmf )
292 ENHMETAFILEOBJ *metaObj = free_gdi_handle( hmf );
294 if(!metaObj) return FALSE;
296 if(metaObj->on_disk)
297 UnmapViewOfFile( metaObj->emh );
298 else
299 HeapFree( GetProcessHeap(), 0, metaObj->emh );
300 HeapFree( GetProcessHeap(), 0, metaObj );
301 return TRUE;
304 /******************************************************************
305 * EMF_GetEnhMetaHeader
307 * Returns ptr to ENHMETAHEADER associated with HENHMETAFILE
309 static ENHMETAHEADER *EMF_GetEnhMetaHeader( HENHMETAFILE hmf )
311 ENHMETAHEADER *ret = NULL;
312 ENHMETAFILEOBJ *metaObj = GDI_GetObjPtr( hmf, OBJ_ENHMETAFILE );
313 TRACE("hmf %p -> enhmetaObj %p\n", hmf, metaObj);
314 if (metaObj)
316 ret = metaObj->emh;
317 GDI_ReleaseObj( hmf );
319 return ret;
322 /*****************************************************************************
323 * EMF_GetEnhMetaFile
326 static HENHMETAFILE EMF_GetEnhMetaFile( HANDLE hFile )
328 ENHMETAHEADER *emh;
329 HANDLE hMapping;
330 HENHMETAFILE hemf;
331 DWORD filesize;
333 filesize = GetFileSize( hFile, NULL );
335 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
336 emh = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, filesize );
337 CloseHandle( hMapping );
339 if (!emh) return 0;
341 hemf = EMF_Create_HENHMETAFILE( emh, filesize, TRUE );
342 if (!hemf)
343 UnmapViewOfFile( emh );
344 return hemf;
348 /*****************************************************************************
349 * GetEnhMetaFileA (GDI32.@)
353 HENHMETAFILE WINAPI GetEnhMetaFileA(
354 LPCSTR lpszMetaFile /* [in] filename of enhanced metafile */
357 HENHMETAFILE hmf;
358 HANDLE hFile;
360 hFile = CreateFileA(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
361 OPEN_EXISTING, 0, 0);
362 if (hFile == INVALID_HANDLE_VALUE) {
363 WARN("could not open %s\n", lpszMetaFile);
364 return 0;
366 hmf = EMF_GetEnhMetaFile( hFile );
367 CloseHandle( hFile );
368 return hmf;
371 /*****************************************************************************
372 * GetEnhMetaFileW (GDI32.@)
374 HENHMETAFILE WINAPI GetEnhMetaFileW(
375 LPCWSTR lpszMetaFile) /* [in] filename of enhanced metafile */
377 HENHMETAFILE hmf;
378 HANDLE hFile;
380 hFile = CreateFileW(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
381 OPEN_EXISTING, 0, 0);
382 if (hFile == INVALID_HANDLE_VALUE) {
383 WARN("could not open %s\n", debugstr_w(lpszMetaFile));
384 return 0;
386 hmf = EMF_GetEnhMetaFile( hFile );
387 CloseHandle( hFile );
388 return hmf;
391 /*****************************************************************************
392 * GetEnhMetaFileHeader (GDI32.@)
394 * Retrieves the record containing the header for the specified
395 * enhanced-format metafile.
397 * RETURNS
398 * If buf is NULL, returns the size of buffer required.
399 * Otherwise, copy up to bufsize bytes of enhanced metafile header into
400 * buf.
402 UINT WINAPI GetEnhMetaFileHeader(
403 HENHMETAFILE hmf, /* [in] enhanced metafile */
404 UINT bufsize, /* [in] size of buffer */
405 LPENHMETAHEADER buf /* [out] buffer */
408 LPENHMETAHEADER emh;
409 UINT size;
411 emh = EMF_GetEnhMetaHeader(hmf);
412 if(!emh) return FALSE;
413 size = emh->nSize;
414 if (!buf) return size;
415 size = min(size, bufsize);
416 memmove(buf, emh, size);
417 return size;
421 /*****************************************************************************
422 * GetEnhMetaFileDescriptionA (GDI32.@)
424 * See GetEnhMetaFileDescriptionW.
426 UINT WINAPI GetEnhMetaFileDescriptionA(
427 HENHMETAFILE hmf, /* [in] enhanced metafile */
428 UINT size, /* [in] size of buf */
429 LPSTR buf /* [out] buffer to receive description */
432 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
433 DWORD len;
434 WCHAR *descrW;
436 if(!emh) return FALSE;
437 if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
438 descrW = (WCHAR *) ((char *) emh + emh->offDescription);
439 len = WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, NULL, 0, NULL, NULL );
441 if (!buf || !size ) return len;
443 len = min( size, len );
444 WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, buf, len, NULL, NULL );
445 return len;
448 /*****************************************************************************
449 * GetEnhMetaFileDescriptionW (GDI32.@)
451 * Copies the description string of an enhanced metafile into a buffer
452 * _buf_.
454 * RETURNS
455 * If _buf_ is NULL, returns size of _buf_ required. Otherwise, returns
456 * number of characters copied.
458 UINT WINAPI GetEnhMetaFileDescriptionW(
459 HENHMETAFILE hmf, /* [in] enhanced metafile */
460 UINT size, /* [in] size of buf */
461 LPWSTR buf /* [out] buffer to receive description */
464 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
466 if(!emh) return FALSE;
467 if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
468 if (!buf || !size ) return emh->nDescription;
470 memmove(buf, (char *) emh + emh->offDescription, min(size,emh->nDescription)*sizeof(WCHAR));
471 return min(size, emh->nDescription);
474 /****************************************************************************
475 * SetEnhMetaFileBits (GDI32.@)
477 * Creates an enhanced metafile by copying _bufsize_ bytes from _buf_.
479 HENHMETAFILE WINAPI SetEnhMetaFileBits(UINT bufsize, const BYTE *buf)
481 ENHMETAHEADER *emh = HeapAlloc( GetProcessHeap(), 0, bufsize );
482 HENHMETAFILE hmf;
484 if (!emh) return 0;
485 memcpy(emh, buf, bufsize);
486 hmf = EMF_Create_HENHMETAFILE( emh, bufsize, FALSE );
487 if (!hmf)
488 HeapFree( GetProcessHeap(), 0, emh );
489 return hmf;
492 /*****************************************************************************
493 * GetEnhMetaFileBits (GDI32.@)
496 UINT WINAPI GetEnhMetaFileBits(
497 HENHMETAFILE hmf,
498 UINT bufsize,
499 LPBYTE buf
502 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader( hmf );
503 UINT size;
505 if(!emh) return 0;
507 size = emh->nBytes;
508 if( buf == NULL ) return size;
510 size = min( size, bufsize );
511 memmove(buf, emh, size);
512 return size;
515 typedef struct EMF_dc_state
517 INT mode;
518 XFORM world_transform;
519 INT wndOrgX;
520 INT wndOrgY;
521 INT wndExtX;
522 INT wndExtY;
523 INT vportOrgX;
524 INT vportOrgY;
525 INT vportExtX;
526 INT vportExtY;
527 struct EMF_dc_state *next;
528 } EMF_dc_state;
530 typedef struct enum_emh_data
532 XFORM init_transform;
533 EMF_dc_state state;
534 INT save_level;
535 EMF_dc_state *saved_state;
536 } enum_emh_data;
538 #define ENUM_GET_PRIVATE_DATA(ht) \
539 ((enum_emh_data*)(((unsigned char*)(ht))-sizeof (enum_emh_data)))
541 #define WIDTH(rect) ( (rect).right - (rect).left )
542 #define HEIGHT(rect) ( (rect).bottom - (rect).top )
544 #define IS_WIN9X() (GetVersion()&0x80000000)
546 static void EMF_Update_MF_Xform(HDC hdc, const enum_emh_data *info)
548 XFORM mapping_mode_trans, final_trans;
549 double scaleX, scaleY;
551 scaleX = (double)info->state.vportExtX / (double)info->state.wndExtX;
552 scaleY = (double)info->state.vportExtY / (double)info->state.wndExtY;
553 mapping_mode_trans.eM11 = scaleX;
554 mapping_mode_trans.eM12 = 0.0;
555 mapping_mode_trans.eM21 = 0.0;
556 mapping_mode_trans.eM22 = scaleY;
557 mapping_mode_trans.eDx = (double)info->state.vportOrgX - scaleX * (double)info->state.wndOrgX;
558 mapping_mode_trans.eDy = (double)info->state.vportOrgY - scaleY * (double)info->state.wndOrgY;
560 CombineTransform(&final_trans, &info->state.world_transform, &mapping_mode_trans);
561 CombineTransform(&final_trans, &final_trans, &info->init_transform);
563 if (!SetWorldTransform(hdc, &final_trans))
565 ERR("World transform failed!\n");
569 static void EMF_RestoreDC( enum_emh_data *info, INT level )
571 if (abs(level) > info->save_level || level == 0) return;
573 if (level < 0) level = info->save_level + level + 1;
575 while (info->save_level >= level)
577 EMF_dc_state *state = info->saved_state;
578 info->saved_state = state->next;
579 state->next = NULL;
580 if (--info->save_level < level)
581 info->state = *state;
582 HeapFree( GetProcessHeap(), 0, state );
586 static void EMF_SaveDC( enum_emh_data *info )
588 EMF_dc_state *state = HeapAlloc( GetProcessHeap(), 0, sizeof(*state));
589 if (state)
591 *state = info->state;
592 state->next = info->saved_state;
593 info->saved_state = state;
594 info->save_level++;
595 TRACE("save_level %d\n", info->save_level);
599 static void EMF_SetMapMode(HDC hdc, enum_emh_data *info)
601 INT horzSize = GetDeviceCaps( hdc, HORZSIZE );
602 INT vertSize = GetDeviceCaps( hdc, VERTSIZE );
603 INT horzRes = GetDeviceCaps( hdc, HORZRES );
604 INT vertRes = GetDeviceCaps( hdc, VERTRES );
606 TRACE("%d\n", info->state.mode);
608 switch(info->state.mode)
610 case MM_TEXT:
611 info->state.wndExtX = 1;
612 info->state.wndExtY = 1;
613 info->state.vportExtX = 1;
614 info->state.vportExtY = 1;
615 break;
616 case MM_LOMETRIC:
617 case MM_ISOTROPIC:
618 info->state.wndExtX = horzSize * 10;
619 info->state.wndExtY = vertSize * 10;
620 info->state.vportExtX = horzRes;
621 info->state.vportExtY = -vertRes;
622 break;
623 case MM_HIMETRIC:
624 info->state.wndExtX = horzSize * 100;
625 info->state.wndExtY = vertSize * 100;
626 info->state.vportExtX = horzRes;
627 info->state.vportExtY = -vertRes;
628 break;
629 case MM_LOENGLISH:
630 info->state.wndExtX = MulDiv(1000, horzSize, 254);
631 info->state.wndExtY = MulDiv(1000, vertSize, 254);
632 info->state.vportExtX = horzRes;
633 info->state.vportExtY = -vertRes;
634 break;
635 case MM_HIENGLISH:
636 info->state.wndExtX = MulDiv(10000, horzSize, 254);
637 info->state.wndExtY = MulDiv(10000, vertSize, 254);
638 info->state.vportExtX = horzRes;
639 info->state.vportExtY = -vertRes;
640 break;
641 case MM_TWIPS:
642 info->state.wndExtX = MulDiv(14400, horzSize, 254);
643 info->state.wndExtY = MulDiv(14400, vertSize, 254);
644 info->state.vportExtX = horzRes;
645 info->state.vportExtY = -vertRes;
646 break;
647 case MM_ANISOTROPIC:
648 break;
649 default:
650 return;
654 /***********************************************************************
655 * EMF_FixIsotropic
657 * Fix viewport extensions for isotropic mode.
660 static void EMF_FixIsotropic(HDC hdc, enum_emh_data *info)
662 double xdim = fabs((double)info->state.vportExtX * GetDeviceCaps( hdc, HORZSIZE ) /
663 (GetDeviceCaps( hdc, HORZRES ) * info->state.wndExtX));
664 double ydim = fabs((double)info->state.vportExtY * GetDeviceCaps( hdc, VERTSIZE ) /
665 (GetDeviceCaps( hdc, VERTRES ) * info->state.wndExtY));
667 if (xdim > ydim)
669 INT mincx = (info->state.vportExtX >= 0) ? 1 : -1;
670 info->state.vportExtX = floor(info->state.vportExtX * ydim / xdim + 0.5);
671 if (!info->state.vportExtX) info->state.vportExtX = mincx;
673 else
675 INT mincy = (info->state.vportExtY >= 0) ? 1 : -1;
676 info->state.vportExtY = floor(info->state.vportExtY * xdim / ydim + 0.5);
677 if (!info->state.vportExtY) info->state.vportExtY = mincy;
681 /*****************************************************************************
682 * emr_produces_output
684 * Returns TRUE if the record type writes something to the dc. Used by
685 * PlayEnhMetaFileRecord to determine whether it needs to update the
686 * dc's xform when in win9x mode.
688 * FIXME: need to test which records should be here.
690 static BOOL emr_produces_output(int type)
692 switch(type) {
693 case EMR_POLYBEZIER:
694 case EMR_POLYGON:
695 case EMR_POLYLINE:
696 case EMR_POLYBEZIERTO:
697 case EMR_POLYLINETO:
698 case EMR_POLYPOLYLINE:
699 case EMR_POLYPOLYGON:
700 case EMR_SETPIXELV:
701 case EMR_MOVETOEX:
702 case EMR_EXCLUDECLIPRECT:
703 case EMR_INTERSECTCLIPRECT:
704 case EMR_SELECTOBJECT:
705 case EMR_ANGLEARC:
706 case EMR_ELLIPSE:
707 case EMR_RECTANGLE:
708 case EMR_ROUNDRECT:
709 case EMR_ARC:
710 case EMR_CHORD:
711 case EMR_PIE:
712 case EMR_EXTFLOODFILL:
713 case EMR_LINETO:
714 case EMR_ARCTO:
715 case EMR_POLYDRAW:
716 case EMR_GDICOMMENT:
717 case EMR_FILLRGN:
718 case EMR_FRAMERGN:
719 case EMR_INVERTRGN:
720 case EMR_PAINTRGN:
721 case EMR_BITBLT:
722 case EMR_STRETCHBLT:
723 case EMR_MASKBLT:
724 case EMR_PLGBLT:
725 case EMR_SETDIBITSTODEVICE:
726 case EMR_STRETCHDIBITS:
727 case EMR_EXTTEXTOUTA:
728 case EMR_EXTTEXTOUTW:
729 case EMR_POLYBEZIER16:
730 case EMR_POLYGON16:
731 case EMR_POLYLINE16:
732 case EMR_POLYBEZIERTO16:
733 case EMR_POLYLINETO16:
734 case EMR_POLYPOLYLINE16:
735 case EMR_POLYPOLYGON16:
736 case EMR_POLYDRAW16:
737 case EMR_POLYTEXTOUTA:
738 case EMR_POLYTEXTOUTW:
739 case EMR_SMALLTEXTOUT:
740 case EMR_ALPHABLEND:
741 case EMR_TRANSPARENTBLT:
742 return TRUE;
743 default:
744 return FALSE;
749 /*****************************************************************************
750 * PlayEnhMetaFileRecord (GDI32.@)
752 * Render a single enhanced metafile record in the device context hdc.
754 * RETURNS
755 * TRUE (non zero) on success, FALSE on error.
756 * BUGS
757 * Many unimplemented records.
758 * No error handling on record play failures (ie checking return codes)
760 * NOTES
761 * WinNT actually updates the current world transform in this function
762 * whereas Win9x does not.
764 BOOL WINAPI PlayEnhMetaFileRecord(
765 HDC hdc, /* [in] device context in which to render EMF record */
766 LPHANDLETABLE handletable, /* [in] array of handles to be used in rendering record */
767 const ENHMETARECORD *mr, /* [in] EMF record to render */
768 UINT handles /* [in] size of handle array */
771 int type;
772 RECT tmprc;
773 enum_emh_data *info = ENUM_GET_PRIVATE_DATA(handletable);
775 TRACE("hdc = %p, handletable = %p, record = %p, numHandles = %d\n",
776 hdc, handletable, mr, handles);
777 if (!mr) return FALSE;
779 type = mr->iType;
781 TRACE("record %s\n", get_emr_name(type));
782 switch(type)
784 case EMR_HEADER:
785 break;
786 case EMR_EOF:
787 break;
788 case EMR_GDICOMMENT:
790 const EMRGDICOMMENT *lpGdiComment = (const EMRGDICOMMENT *)mr;
791 /* In an enhanced metafile, there can be both public and private GDI comments */
792 GdiComment( hdc, lpGdiComment->cbData, lpGdiComment->Data );
793 break;
795 case EMR_SETMAPMODE:
797 const EMRSETMAPMODE *pSetMapMode = (const EMRSETMAPMODE *)mr;
799 if (info->state.mode == pSetMapMode->iMode &&
800 (info->state.mode == MM_ISOTROPIC || info->state.mode == MM_ANISOTROPIC))
801 break;
802 info->state.mode = pSetMapMode->iMode;
803 EMF_SetMapMode(hdc, info);
805 if (!IS_WIN9X())
806 EMF_Update_MF_Xform(hdc, info);
808 break;
810 case EMR_SETBKMODE:
812 const EMRSETBKMODE *pSetBkMode = (const EMRSETBKMODE *)mr;
813 SetBkMode(hdc, pSetBkMode->iMode);
814 break;
816 case EMR_SETBKCOLOR:
818 const EMRSETBKCOLOR *pSetBkColor = (const EMRSETBKCOLOR *)mr;
819 SetBkColor(hdc, pSetBkColor->crColor);
820 break;
822 case EMR_SETPOLYFILLMODE:
824 const EMRSETPOLYFILLMODE *pSetPolyFillMode = (const EMRSETPOLYFILLMODE *)mr;
825 SetPolyFillMode(hdc, pSetPolyFillMode->iMode);
826 break;
828 case EMR_SETROP2:
830 const EMRSETROP2 *pSetROP2 = (const EMRSETROP2 *)mr;
831 SetROP2(hdc, pSetROP2->iMode);
832 break;
834 case EMR_SETSTRETCHBLTMODE:
836 const EMRSETSTRETCHBLTMODE *pSetStretchBltMode = (const EMRSETSTRETCHBLTMODE *)mr;
837 SetStretchBltMode(hdc, pSetStretchBltMode->iMode);
838 break;
840 case EMR_SETTEXTALIGN:
842 const EMRSETTEXTALIGN *pSetTextAlign = (const EMRSETTEXTALIGN *)mr;
843 SetTextAlign(hdc, pSetTextAlign->iMode);
844 break;
846 case EMR_SETTEXTCOLOR:
848 const EMRSETTEXTCOLOR *pSetTextColor = (const EMRSETTEXTCOLOR *)mr;
849 SetTextColor(hdc, pSetTextColor->crColor);
850 break;
852 case EMR_SAVEDC:
854 if (SaveDC( hdc ))
855 EMF_SaveDC( info );
856 break;
858 case EMR_RESTOREDC:
860 const EMRRESTOREDC *pRestoreDC = (const EMRRESTOREDC *)mr;
861 TRACE("EMR_RESTORE: %d\n", pRestoreDC->iRelative);
862 if (RestoreDC( hdc, pRestoreDC->iRelative ))
863 EMF_RestoreDC( info, pRestoreDC->iRelative );
864 break;
866 case EMR_INTERSECTCLIPRECT:
868 const EMRINTERSECTCLIPRECT *pClipRect = (const EMRINTERSECTCLIPRECT *)mr;
869 TRACE("EMR_INTERSECTCLIPRECT: rect %d,%d - %d, %d\n",
870 pClipRect->rclClip.left, pClipRect->rclClip.top,
871 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
872 IntersectClipRect(hdc, pClipRect->rclClip.left, pClipRect->rclClip.top,
873 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
874 break;
876 case EMR_SELECTOBJECT:
878 const EMRSELECTOBJECT *pSelectObject = (const EMRSELECTOBJECT *)mr;
879 if( pSelectObject->ihObject & 0x80000000 ) {
880 /* High order bit is set - it's a stock object
881 * Strip the high bit to get the index.
882 * See MSDN article Q142319
884 SelectObject( hdc, GetStockObject( pSelectObject->ihObject &
885 0x7fffffff ) );
886 } else {
887 /* High order bit wasn't set - not a stock object
889 SelectObject( hdc,
890 (handletable->objectHandle)[pSelectObject->ihObject] );
892 break;
894 case EMR_DELETEOBJECT:
896 const EMRDELETEOBJECT *pDeleteObject = (const EMRDELETEOBJECT *)mr;
897 DeleteObject( (handletable->objectHandle)[pDeleteObject->ihObject]);
898 (handletable->objectHandle)[pDeleteObject->ihObject] = 0;
899 break;
901 case EMR_SETWINDOWORGEX:
903 const EMRSETWINDOWORGEX *pSetWindowOrgEx = (const EMRSETWINDOWORGEX *)mr;
905 info->state.wndOrgX = pSetWindowOrgEx->ptlOrigin.x;
906 info->state.wndOrgY = pSetWindowOrgEx->ptlOrigin.y;
908 TRACE("SetWindowOrgEx: %d,%d\n", info->state.wndOrgX, info->state.wndOrgY);
910 if (!IS_WIN9X())
911 EMF_Update_MF_Xform(hdc, info);
913 break;
915 case EMR_SETWINDOWEXTEX:
917 const EMRSETWINDOWEXTEX *pSetWindowExtEx = (const EMRSETWINDOWEXTEX *)mr;
919 if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
920 break;
921 info->state.wndExtX = pSetWindowExtEx->szlExtent.cx;
922 info->state.wndExtY = pSetWindowExtEx->szlExtent.cy;
923 if (info->state.mode == MM_ISOTROPIC)
924 EMF_FixIsotropic(hdc, info);
926 TRACE("SetWindowExtEx: %d,%d\n",info->state.wndExtX, info->state.wndExtY);
928 if (!IS_WIN9X())
929 EMF_Update_MF_Xform(hdc, info);
931 break;
933 case EMR_SETVIEWPORTORGEX:
935 const EMRSETVIEWPORTORGEX *pSetViewportOrgEx = (const EMRSETVIEWPORTORGEX *)mr;
937 info->state.vportOrgX = pSetViewportOrgEx->ptlOrigin.x;
938 info->state.vportOrgY = pSetViewportOrgEx->ptlOrigin.y;
939 TRACE("SetViewportOrgEx: %d,%d\n", info->state.vportOrgX, info->state.vportOrgY);
941 if (!IS_WIN9X())
942 EMF_Update_MF_Xform(hdc, info);
944 break;
946 case EMR_SETVIEWPORTEXTEX:
948 const EMRSETVIEWPORTEXTEX *pSetViewportExtEx = (const EMRSETVIEWPORTEXTEX *)mr;
950 if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
951 break;
952 info->state.vportExtX = pSetViewportExtEx->szlExtent.cx;
953 info->state.vportExtY = pSetViewportExtEx->szlExtent.cy;
954 if (info->state.mode == MM_ISOTROPIC)
955 EMF_FixIsotropic(hdc, info);
956 TRACE("SetViewportExtEx: %d,%d\n", info->state.vportExtX, info->state.vportExtY);
958 if (!IS_WIN9X())
959 EMF_Update_MF_Xform(hdc, info);
961 break;
963 case EMR_CREATEPEN:
965 const EMRCREATEPEN *pCreatePen = (const EMRCREATEPEN *)mr;
966 (handletable->objectHandle)[pCreatePen->ihPen] =
967 CreatePenIndirect(&pCreatePen->lopn);
968 break;
970 case EMR_EXTCREATEPEN:
972 const EMREXTCREATEPEN *pPen = (const EMREXTCREATEPEN *)mr;
973 LOGBRUSH lb;
974 lb.lbStyle = pPen->elp.elpBrushStyle;
975 lb.lbColor = pPen->elp.elpColor;
976 lb.lbHatch = pPen->elp.elpHatch;
978 if(pPen->offBmi || pPen->offBits)
979 FIXME("EMR_EXTCREATEPEN: Need to copy brush bitmap\n");
981 (handletable->objectHandle)[pPen->ihPen] =
982 ExtCreatePen(pPen->elp.elpPenStyle, pPen->elp.elpWidth, &lb,
983 pPen->elp.elpNumEntries, pPen->elp.elpNumEntries ? pPen->elp.elpStyleEntry : NULL);
984 break;
986 case EMR_CREATEBRUSHINDIRECT:
988 const EMRCREATEBRUSHINDIRECT *pBrush = (const EMRCREATEBRUSHINDIRECT *)mr;
989 LOGBRUSH brush;
990 brush.lbStyle = pBrush->lb.lbStyle;
991 brush.lbColor = pBrush->lb.lbColor;
992 brush.lbHatch = pBrush->lb.lbHatch;
993 (handletable->objectHandle)[pBrush->ihBrush] = CreateBrushIndirect(&brush);
994 break;
996 case EMR_EXTCREATEFONTINDIRECTW:
998 const EMREXTCREATEFONTINDIRECTW *pFont = (const EMREXTCREATEFONTINDIRECTW *)mr;
999 (handletable->objectHandle)[pFont->ihFont] =
1000 CreateFontIndirectW(&pFont->elfw.elfLogFont);
1001 break;
1003 case EMR_MOVETOEX:
1005 const EMRMOVETOEX *pMoveToEx = (const EMRMOVETOEX *)mr;
1006 MoveToEx(hdc, pMoveToEx->ptl.x, pMoveToEx->ptl.y, NULL);
1007 break;
1009 case EMR_LINETO:
1011 const EMRLINETO *pLineTo = (const EMRLINETO *)mr;
1012 LineTo(hdc, pLineTo->ptl.x, pLineTo->ptl.y);
1013 break;
1015 case EMR_RECTANGLE:
1017 const EMRRECTANGLE *pRect = (const EMRRECTANGLE *)mr;
1018 Rectangle(hdc, pRect->rclBox.left, pRect->rclBox.top,
1019 pRect->rclBox.right, pRect->rclBox.bottom);
1020 break;
1022 case EMR_ELLIPSE:
1024 const EMRELLIPSE *pEllipse = (const EMRELLIPSE *)mr;
1025 Ellipse(hdc, pEllipse->rclBox.left, pEllipse->rclBox.top,
1026 pEllipse->rclBox.right, pEllipse->rclBox.bottom);
1027 break;
1029 case EMR_POLYGON16:
1031 const EMRPOLYGON16 *pPoly = (const EMRPOLYGON16 *)mr;
1032 /* Shouldn't use Polygon16 since pPoly->cpts is DWORD */
1033 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1034 pPoly->cpts * sizeof(POINT) );
1035 DWORD i;
1036 for(i = 0; i < pPoly->cpts; i++)
1038 pts[i].x = pPoly->apts[i].x;
1039 pts[i].y = pPoly->apts[i].y;
1041 Polygon(hdc, pts, pPoly->cpts);
1042 HeapFree( GetProcessHeap(), 0, pts );
1043 break;
1045 case EMR_POLYLINE16:
1047 const EMRPOLYLINE16 *pPoly = (const EMRPOLYLINE16 *)mr;
1048 /* Shouldn't use Polyline16 since pPoly->cpts is DWORD */
1049 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1050 pPoly->cpts * sizeof(POINT) );
1051 DWORD i;
1052 for(i = 0; i < pPoly->cpts; i++)
1054 pts[i].x = pPoly->apts[i].x;
1055 pts[i].y = pPoly->apts[i].y;
1057 Polyline(hdc, pts, pPoly->cpts);
1058 HeapFree( GetProcessHeap(), 0, pts );
1059 break;
1061 case EMR_POLYLINETO16:
1063 const EMRPOLYLINETO16 *pPoly = (const EMRPOLYLINETO16 *)mr;
1064 /* Shouldn't use PolylineTo16 since pPoly->cpts is DWORD */
1065 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1066 pPoly->cpts * sizeof(POINT) );
1067 DWORD i;
1068 for(i = 0; i < pPoly->cpts; i++)
1070 pts[i].x = pPoly->apts[i].x;
1071 pts[i].y = pPoly->apts[i].y;
1073 PolylineTo(hdc, pts, pPoly->cpts);
1074 HeapFree( GetProcessHeap(), 0, pts );
1075 break;
1077 case EMR_POLYBEZIER16:
1079 const EMRPOLYBEZIER16 *pPoly = (const EMRPOLYBEZIER16 *)mr;
1080 /* Shouldn't use PolyBezier16 since pPoly->cpts is DWORD */
1081 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1082 pPoly->cpts * sizeof(POINT) );
1083 DWORD i;
1084 for(i = 0; i < pPoly->cpts; i++)
1086 pts[i].x = pPoly->apts[i].x;
1087 pts[i].y = pPoly->apts[i].y;
1089 PolyBezier(hdc, pts, pPoly->cpts);
1090 HeapFree( GetProcessHeap(), 0, pts );
1091 break;
1093 case EMR_POLYBEZIERTO16:
1095 const EMRPOLYBEZIERTO16 *pPoly = (const EMRPOLYBEZIERTO16 *)mr;
1096 /* Shouldn't use PolyBezierTo16 since pPoly->cpts is DWORD */
1097 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1098 pPoly->cpts * sizeof(POINT) );
1099 DWORD i;
1100 for(i = 0; i < pPoly->cpts; i++)
1102 pts[i].x = pPoly->apts[i].x;
1103 pts[i].y = pPoly->apts[i].y;
1105 PolyBezierTo(hdc, pts, pPoly->cpts);
1106 HeapFree( GetProcessHeap(), 0, pts );
1107 break;
1109 case EMR_POLYPOLYGON16:
1111 const EMRPOLYPOLYGON16 *pPolyPoly = (const EMRPOLYPOLYGON16 *)mr;
1112 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1113 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1115 const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1116 POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1117 DWORD i;
1118 for(i = 0; i < pPolyPoly->cpts; i++)
1120 pt[i].x = pts[i].x;
1121 pt[i].y = pts[i].y;
1123 PolyPolygon(hdc, pt, (const INT*)pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1124 HeapFree( GetProcessHeap(), 0, pt );
1125 break;
1127 case EMR_POLYPOLYLINE16:
1129 const EMRPOLYPOLYLINE16 *pPolyPoly = (const EMRPOLYPOLYLINE16 *)mr;
1130 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1131 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1133 const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1134 POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1135 DWORD i;
1136 for(i = 0; i < pPolyPoly->cpts; i++)
1138 pt[i].x = pts[i].x;
1139 pt[i].y = pts[i].y;
1141 PolyPolyline(hdc, pt, pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1142 HeapFree( GetProcessHeap(), 0, pt );
1143 break;
1146 case EMR_POLYDRAW16:
1148 const EMRPOLYDRAW16 *pPolyDraw16 = (const EMRPOLYDRAW16 *)mr;
1149 const POINTS *ptl = pPolyDraw16->apts;
1150 POINT *pts = HeapAlloc(GetProcessHeap(), 0, pPolyDraw16->cpts * sizeof(POINT));
1151 DWORD i;
1153 /* NB abTypes array doesn't start at pPolyDraw16->abTypes. It's actually
1154 pPolyDraw16->apts + pPolyDraw16->cpts. */
1155 const BYTE *types = (BYTE*)(pPolyDraw16->apts + pPolyDraw16->cpts);
1157 if (!pts)
1158 break;
1160 for (i = 0; i < pPolyDraw16->cpts; ++i)
1162 pts[i].x = ptl[i].x;
1163 pts[i].y = ptl[i].y;
1166 PolyDraw(hdc, pts, types, pPolyDraw16->cpts);
1167 HeapFree(GetProcessHeap(), 0, pts);
1168 break;
1171 case EMR_STRETCHDIBITS:
1173 const EMRSTRETCHDIBITS *pStretchDIBits = (const EMRSTRETCHDIBITS *)mr;
1175 StretchDIBits(hdc,
1176 pStretchDIBits->xDest,
1177 pStretchDIBits->yDest,
1178 pStretchDIBits->cxDest,
1179 pStretchDIBits->cyDest,
1180 pStretchDIBits->xSrc,
1181 pStretchDIBits->ySrc,
1182 pStretchDIBits->cxSrc,
1183 pStretchDIBits->cySrc,
1184 (const BYTE *)mr + pStretchDIBits->offBitsSrc,
1185 (const BITMAPINFO *)((const BYTE *)mr + pStretchDIBits->offBmiSrc),
1186 pStretchDIBits->iUsageSrc,
1187 pStretchDIBits->dwRop);
1188 break;
1191 case EMR_EXTTEXTOUTA:
1193 const EMREXTTEXTOUTA *pExtTextOutA = (const EMREXTTEXTOUTA *)mr;
1194 RECT rc;
1195 const INT *dx = NULL;
1196 int old_mode;
1198 rc.left = pExtTextOutA->emrtext.rcl.left;
1199 rc.top = pExtTextOutA->emrtext.rcl.top;
1200 rc.right = pExtTextOutA->emrtext.rcl.right;
1201 rc.bottom = pExtTextOutA->emrtext.rcl.bottom;
1202 TRACE("EMR_EXTTEXTOUTA: x,y = %d, %d. rect = %s. flags %08x\n",
1203 pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1204 wine_dbgstr_rect(&rc), pExtTextOutA->emrtext.fOptions);
1206 old_mode = SetGraphicsMode(hdc, pExtTextOutA->iGraphicsMode);
1207 /* Reselect the font back into the dc so that the transformation
1208 gets updated. */
1209 SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1211 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1212 * These files can be enumerated and played under Win98 just
1213 * fine, but at least Win2k chokes on them.
1215 if (pExtTextOutA->emrtext.offDx)
1216 dx = (const INT *)((const BYTE *)mr + pExtTextOutA->emrtext.offDx);
1218 ExtTextOutA(hdc, pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1219 pExtTextOutA->emrtext.fOptions, &rc,
1220 (LPCSTR)((const BYTE *)mr + pExtTextOutA->emrtext.offString), pExtTextOutA->emrtext.nChars,
1221 dx);
1223 SetGraphicsMode(hdc, old_mode);
1224 break;
1227 case EMR_EXTTEXTOUTW:
1229 const EMREXTTEXTOUTW *pExtTextOutW = (const EMREXTTEXTOUTW *)mr;
1230 RECT rc;
1231 const INT *dx = NULL;
1232 int old_mode;
1234 rc.left = pExtTextOutW->emrtext.rcl.left;
1235 rc.top = pExtTextOutW->emrtext.rcl.top;
1236 rc.right = pExtTextOutW->emrtext.rcl.right;
1237 rc.bottom = pExtTextOutW->emrtext.rcl.bottom;
1238 TRACE("EMR_EXTTEXTOUTW: x,y = %d, %d. rect = %s. flags %08x\n",
1239 pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1240 wine_dbgstr_rect(&rc), pExtTextOutW->emrtext.fOptions);
1242 old_mode = SetGraphicsMode(hdc, pExtTextOutW->iGraphicsMode);
1243 /* Reselect the font back into the dc so that the transformation
1244 gets updated. */
1245 SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1247 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1248 * These files can be enumerated and played under Win98 just
1249 * fine, but at least Win2k chokes on them.
1251 if (pExtTextOutW->emrtext.offDx)
1252 dx = (const INT *)((const BYTE *)mr + pExtTextOutW->emrtext.offDx);
1254 ExtTextOutW(hdc, pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1255 pExtTextOutW->emrtext.fOptions, &rc,
1256 (LPCWSTR)((const BYTE *)mr + pExtTextOutW->emrtext.offString), pExtTextOutW->emrtext.nChars,
1257 dx);
1259 SetGraphicsMode(hdc, old_mode);
1260 break;
1263 case EMR_CREATEPALETTE:
1265 const EMRCREATEPALETTE *lpCreatePal = (const EMRCREATEPALETTE *)mr;
1267 (handletable->objectHandle)[ lpCreatePal->ihPal ] =
1268 CreatePalette( &lpCreatePal->lgpl );
1270 break;
1273 case EMR_SELECTPALETTE:
1275 const EMRSELECTPALETTE *lpSelectPal = (const EMRSELECTPALETTE *)mr;
1277 if( lpSelectPal->ihPal & 0x80000000 ) {
1278 SelectPalette( hdc, GetStockObject(lpSelectPal->ihPal & 0x7fffffff), TRUE);
1279 } else {
1280 SelectPalette( hdc, (handletable->objectHandle)[lpSelectPal->ihPal], TRUE);
1282 break;
1285 case EMR_REALIZEPALETTE:
1287 RealizePalette( hdc );
1288 break;
1291 case EMR_EXTSELECTCLIPRGN:
1293 const EMREXTSELECTCLIPRGN *lpRgn = (const EMREXTSELECTCLIPRGN *)mr;
1294 HRGN hRgn = 0;
1296 if (mr->nSize >= sizeof(*lpRgn) + sizeof(RGNDATAHEADER))
1297 hRgn = ExtCreateRegion( &info->init_transform, 0, (const RGNDATA *)lpRgn->RgnData );
1299 ExtSelectClipRgn(hdc, hRgn, (INT)(lpRgn->iMode));
1300 /* ExtSelectClipRgn created a copy of the region */
1301 DeleteObject(hRgn);
1302 break;
1305 case EMR_SETMETARGN:
1307 SetMetaRgn( hdc );
1308 break;
1311 case EMR_SETWORLDTRANSFORM:
1313 const EMRSETWORLDTRANSFORM *lpXfrm = (const EMRSETWORLDTRANSFORM *)mr;
1314 info->state.world_transform = lpXfrm->xform;
1316 if (!IS_WIN9X())
1317 EMF_Update_MF_Xform(hdc, info);
1319 break;
1322 case EMR_POLYBEZIER:
1324 const EMRPOLYBEZIER *lpPolyBez = (const EMRPOLYBEZIER *)mr;
1325 PolyBezier(hdc, (const POINT*)lpPolyBez->aptl, (UINT)lpPolyBez->cptl);
1326 break;
1329 case EMR_POLYGON:
1331 const EMRPOLYGON *lpPoly = (const EMRPOLYGON *)mr;
1332 Polygon( hdc, (const POINT*)lpPoly->aptl, (UINT)lpPoly->cptl );
1333 break;
1336 case EMR_POLYLINE:
1338 const EMRPOLYLINE *lpPolyLine = (const EMRPOLYLINE *)mr;
1339 Polyline(hdc, (const POINT*)lpPolyLine->aptl, (UINT)lpPolyLine->cptl);
1340 break;
1343 case EMR_POLYBEZIERTO:
1345 const EMRPOLYBEZIERTO *lpPolyBezierTo = (const EMRPOLYBEZIERTO *)mr;
1346 PolyBezierTo( hdc, (const POINT*)lpPolyBezierTo->aptl,
1347 (UINT)lpPolyBezierTo->cptl );
1348 break;
1351 case EMR_POLYLINETO:
1353 const EMRPOLYLINETO *lpPolyLineTo = (const EMRPOLYLINETO *)mr;
1354 PolylineTo( hdc, (const POINT*)lpPolyLineTo->aptl,
1355 (UINT)lpPolyLineTo->cptl );
1356 break;
1359 case EMR_POLYPOLYLINE:
1361 const EMRPOLYPOLYLINE *pPolyPolyline = (const EMRPOLYPOLYLINE *)mr;
1362 /* NB Points at pPolyPolyline->aPolyCounts + pPolyPolyline->nPolys */
1364 PolyPolyline(hdc, (const POINT*)(pPolyPolyline->aPolyCounts +
1365 pPolyPolyline->nPolys),
1366 pPolyPolyline->aPolyCounts,
1367 pPolyPolyline->nPolys );
1369 break;
1372 case EMR_POLYPOLYGON:
1374 const EMRPOLYPOLYGON *pPolyPolygon = (const EMRPOLYPOLYGON *)mr;
1376 /* NB Points at pPolyPolygon->aPolyCounts + pPolyPolygon->nPolys */
1378 PolyPolygon(hdc, (const POINT*)(pPolyPolygon->aPolyCounts +
1379 pPolyPolygon->nPolys),
1380 (const INT*)pPolyPolygon->aPolyCounts, pPolyPolygon->nPolys );
1381 break;
1384 case EMR_SETBRUSHORGEX:
1386 const EMRSETBRUSHORGEX *lpSetBrushOrgEx = (const EMRSETBRUSHORGEX *)mr;
1388 SetBrushOrgEx( hdc,
1389 (INT)lpSetBrushOrgEx->ptlOrigin.x,
1390 (INT)lpSetBrushOrgEx->ptlOrigin.y,
1391 NULL );
1393 break;
1396 case EMR_SETPIXELV:
1398 const EMRSETPIXELV *lpSetPixelV = (const EMRSETPIXELV *)mr;
1400 SetPixelV( hdc,
1401 (INT)lpSetPixelV->ptlPixel.x,
1402 (INT)lpSetPixelV->ptlPixel.y,
1403 lpSetPixelV->crColor );
1405 break;
1408 case EMR_SETMAPPERFLAGS:
1410 const EMRSETMAPPERFLAGS *lpSetMapperFlags = (const EMRSETMAPPERFLAGS *)mr;
1412 SetMapperFlags( hdc, lpSetMapperFlags->dwFlags );
1414 break;
1417 case EMR_SETCOLORADJUSTMENT:
1419 const EMRSETCOLORADJUSTMENT *lpSetColorAdjust = (const EMRSETCOLORADJUSTMENT *)mr;
1421 SetColorAdjustment( hdc, &lpSetColorAdjust->ColorAdjustment );
1423 break;
1426 case EMR_OFFSETCLIPRGN:
1428 const EMROFFSETCLIPRGN *lpOffsetClipRgn = (const EMROFFSETCLIPRGN *)mr;
1430 OffsetClipRgn( hdc,
1431 (INT)lpOffsetClipRgn->ptlOffset.x,
1432 (INT)lpOffsetClipRgn->ptlOffset.y );
1433 FIXME("OffsetClipRgn\n");
1435 break;
1438 case EMR_EXCLUDECLIPRECT:
1440 const EMREXCLUDECLIPRECT *lpExcludeClipRect = (const EMREXCLUDECLIPRECT *)mr;
1442 ExcludeClipRect( hdc,
1443 lpExcludeClipRect->rclClip.left,
1444 lpExcludeClipRect->rclClip.top,
1445 lpExcludeClipRect->rclClip.right,
1446 lpExcludeClipRect->rclClip.bottom );
1447 FIXME("ExcludeClipRect\n");
1449 break;
1452 case EMR_SCALEVIEWPORTEXTEX:
1454 const EMRSCALEVIEWPORTEXTEX *lpScaleViewportExtEx = (const EMRSCALEVIEWPORTEXTEX *)mr;
1456 if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1457 break;
1458 if (!lpScaleViewportExtEx->xNum || !lpScaleViewportExtEx->xDenom ||
1459 !lpScaleViewportExtEx->yNum || !lpScaleViewportExtEx->yDenom)
1460 break;
1461 info->state.vportExtX = MulDiv(info->state.vportExtX, lpScaleViewportExtEx->xNum,
1462 lpScaleViewportExtEx->xDenom);
1463 info->state.vportExtY = MulDiv(info->state.vportExtY, lpScaleViewportExtEx->yNum,
1464 lpScaleViewportExtEx->yDenom);
1465 if (info->state.vportExtX == 0) info->state.vportExtX = 1;
1466 if (info->state.vportExtY == 0) info->state.vportExtY = 1;
1467 if (info->state.mode == MM_ISOTROPIC)
1468 EMF_FixIsotropic(hdc, info);
1470 TRACE("EMRSCALEVIEWPORTEXTEX %d/%d %d/%d\n",
1471 lpScaleViewportExtEx->xNum,lpScaleViewportExtEx->xDenom,
1472 lpScaleViewportExtEx->yNum,lpScaleViewportExtEx->yDenom);
1474 if (!IS_WIN9X())
1475 EMF_Update_MF_Xform(hdc, info);
1477 break;
1480 case EMR_SCALEWINDOWEXTEX:
1482 const EMRSCALEWINDOWEXTEX *lpScaleWindowExtEx = (const EMRSCALEWINDOWEXTEX *)mr;
1484 if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1485 break;
1486 if (!lpScaleWindowExtEx->xNum || !lpScaleWindowExtEx->xDenom ||
1487 !lpScaleWindowExtEx->yNum || !lpScaleWindowExtEx->yDenom)
1488 break;
1489 info->state.wndExtX = MulDiv(info->state.wndExtX, lpScaleWindowExtEx->xNum,
1490 lpScaleWindowExtEx->xDenom);
1491 info->state.wndExtY = MulDiv(info->state.wndExtY, lpScaleWindowExtEx->yNum,
1492 lpScaleWindowExtEx->yDenom);
1493 if (info->state.wndExtX == 0) info->state.wndExtX = 1;
1494 if (info->state.wndExtY == 0) info->state.wndExtY = 1;
1495 if (info->state.mode == MM_ISOTROPIC)
1496 EMF_FixIsotropic(hdc, info);
1498 TRACE("EMRSCALEWINDOWEXTEX %d/%d %d/%d\n",
1499 lpScaleWindowExtEx->xNum,lpScaleWindowExtEx->xDenom,
1500 lpScaleWindowExtEx->yNum,lpScaleWindowExtEx->yDenom);
1502 if (!IS_WIN9X())
1503 EMF_Update_MF_Xform(hdc, info);
1505 break;
1508 case EMR_MODIFYWORLDTRANSFORM:
1510 const EMRMODIFYWORLDTRANSFORM *lpModifyWorldTrans = (const EMRMODIFYWORLDTRANSFORM *)mr;
1512 switch(lpModifyWorldTrans->iMode) {
1513 case MWT_IDENTITY:
1514 info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
1515 info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
1516 info->state.world_transform.eDx = info->state.world_transform.eDy = 0;
1517 if (!IS_WIN9X())
1518 EMF_Update_MF_Xform(hdc, info);
1519 break;
1520 case MWT_LEFTMULTIPLY:
1521 CombineTransform(&info->state.world_transform, &lpModifyWorldTrans->xform,
1522 &info->state.world_transform);
1523 if (!IS_WIN9X())
1524 ModifyWorldTransform(hdc, &lpModifyWorldTrans->xform, MWT_LEFTMULTIPLY);
1525 break;
1526 case MWT_RIGHTMULTIPLY:
1527 CombineTransform(&info->state.world_transform, &info->state.world_transform,
1528 &lpModifyWorldTrans->xform);
1529 if (!IS_WIN9X())
1530 EMF_Update_MF_Xform(hdc, info);
1531 break;
1532 default:
1533 FIXME("Unknown imode %d\n", lpModifyWorldTrans->iMode);
1534 break;
1536 break;
1539 case EMR_ANGLEARC:
1541 const EMRANGLEARC *lpAngleArc = (const EMRANGLEARC *)mr;
1543 AngleArc( hdc,
1544 (INT)lpAngleArc->ptlCenter.x, (INT)lpAngleArc->ptlCenter.y,
1545 lpAngleArc->nRadius, lpAngleArc->eStartAngle,
1546 lpAngleArc->eSweepAngle );
1548 break;
1551 case EMR_ROUNDRECT:
1553 const EMRROUNDRECT *lpRoundRect = (const EMRROUNDRECT *)mr;
1555 RoundRect( hdc,
1556 lpRoundRect->rclBox.left,
1557 lpRoundRect->rclBox.top,
1558 lpRoundRect->rclBox.right,
1559 lpRoundRect->rclBox.bottom,
1560 lpRoundRect->szlCorner.cx,
1561 lpRoundRect->szlCorner.cy );
1563 break;
1566 case EMR_ARC:
1568 const EMRARC *lpArc = (const EMRARC *)mr;
1570 Arc( hdc,
1571 (INT)lpArc->rclBox.left,
1572 (INT)lpArc->rclBox.top,
1573 (INT)lpArc->rclBox.right,
1574 (INT)lpArc->rclBox.bottom,
1575 (INT)lpArc->ptlStart.x,
1576 (INT)lpArc->ptlStart.y,
1577 (INT)lpArc->ptlEnd.x,
1578 (INT)lpArc->ptlEnd.y );
1580 break;
1583 case EMR_CHORD:
1585 const EMRCHORD *lpChord = (const EMRCHORD *)mr;
1587 Chord( hdc,
1588 (INT)lpChord->rclBox.left,
1589 (INT)lpChord->rclBox.top,
1590 (INT)lpChord->rclBox.right,
1591 (INT)lpChord->rclBox.bottom,
1592 (INT)lpChord->ptlStart.x,
1593 (INT)lpChord->ptlStart.y,
1594 (INT)lpChord->ptlEnd.x,
1595 (INT)lpChord->ptlEnd.y );
1597 break;
1600 case EMR_PIE:
1602 const EMRPIE *lpPie = (const EMRPIE *)mr;
1604 Pie( hdc,
1605 (INT)lpPie->rclBox.left,
1606 (INT)lpPie->rclBox.top,
1607 (INT)lpPie->rclBox.right,
1608 (INT)lpPie->rclBox.bottom,
1609 (INT)lpPie->ptlStart.x,
1610 (INT)lpPie->ptlStart.y,
1611 (INT)lpPie->ptlEnd.x,
1612 (INT)lpPie->ptlEnd.y );
1614 break;
1617 case EMR_ARCTO:
1619 const EMRARC *lpArcTo = (const EMRARC *)mr;
1621 ArcTo( hdc,
1622 (INT)lpArcTo->rclBox.left,
1623 (INT)lpArcTo->rclBox.top,
1624 (INT)lpArcTo->rclBox.right,
1625 (INT)lpArcTo->rclBox.bottom,
1626 (INT)lpArcTo->ptlStart.x,
1627 (INT)lpArcTo->ptlStart.y,
1628 (INT)lpArcTo->ptlEnd.x,
1629 (INT)lpArcTo->ptlEnd.y );
1631 break;
1634 case EMR_EXTFLOODFILL:
1636 const EMREXTFLOODFILL *lpExtFloodFill = (const EMREXTFLOODFILL *)mr;
1638 ExtFloodFill( hdc,
1639 (INT)lpExtFloodFill->ptlStart.x,
1640 (INT)lpExtFloodFill->ptlStart.y,
1641 lpExtFloodFill->crColor,
1642 (UINT)lpExtFloodFill->iMode );
1644 break;
1647 case EMR_POLYDRAW:
1649 const EMRPOLYDRAW *lpPolyDraw = (const EMRPOLYDRAW *)mr;
1651 /* NB abTypes array doesn't start at lpPolyDraw->abTypes. It's actually
1652 lpPolyDraw->aptl + lpPolyDraw->cptl. */
1653 PolyDraw( hdc,
1654 (const POINT*)lpPolyDraw->aptl,
1655 (BYTE*)(lpPolyDraw->aptl + lpPolyDraw->cptl),
1656 (INT)lpPolyDraw->cptl );
1658 break;
1661 case EMR_SETARCDIRECTION:
1663 const EMRSETARCDIRECTION *lpSetArcDirection = (const EMRSETARCDIRECTION *)mr;
1664 SetArcDirection( hdc, (INT)lpSetArcDirection->iArcDirection );
1665 break;
1668 case EMR_SETMITERLIMIT:
1670 const EMRSETMITERLIMIT *lpSetMiterLimit = (const EMRSETMITERLIMIT *)mr;
1671 SetMiterLimit( hdc, lpSetMiterLimit->eMiterLimit, NULL );
1672 break;
1675 case EMR_BEGINPATH:
1677 BeginPath( hdc );
1678 break;
1681 case EMR_ENDPATH:
1683 EndPath( hdc );
1684 break;
1687 case EMR_CLOSEFIGURE:
1689 CloseFigure( hdc );
1690 break;
1693 case EMR_FILLPATH:
1695 /*const EMRFILLPATH lpFillPath = (const EMRFILLPATH *)mr;*/
1696 FillPath( hdc );
1697 break;
1700 case EMR_STROKEANDFILLPATH:
1702 /*const EMRSTROKEANDFILLPATH lpStrokeAndFillPath = (const EMRSTROKEANDFILLPATH *)mr;*/
1703 StrokeAndFillPath( hdc );
1704 break;
1707 case EMR_STROKEPATH:
1709 /*const EMRSTROKEPATH lpStrokePath = (const EMRSTROKEPATH *)mr;*/
1710 StrokePath( hdc );
1711 break;
1714 case EMR_FLATTENPATH:
1716 FlattenPath( hdc );
1717 break;
1720 case EMR_WIDENPATH:
1722 WidenPath( hdc );
1723 break;
1726 case EMR_SELECTCLIPPATH:
1728 const EMRSELECTCLIPPATH *lpSelectClipPath = (const EMRSELECTCLIPPATH *)mr;
1729 SelectClipPath( hdc, (INT)lpSelectClipPath->iMode );
1730 break;
1733 case EMR_ABORTPATH:
1735 AbortPath( hdc );
1736 break;
1739 case EMR_CREATECOLORSPACE:
1741 PEMRCREATECOLORSPACE lpCreateColorSpace = (PEMRCREATECOLORSPACE)mr;
1742 (handletable->objectHandle)[lpCreateColorSpace->ihCS] =
1743 CreateColorSpaceA( &lpCreateColorSpace->lcs );
1744 break;
1747 case EMR_SETCOLORSPACE:
1749 const EMRSETCOLORSPACE *lpSetColorSpace = (const EMRSETCOLORSPACE *)mr;
1750 SetColorSpace( hdc,
1751 (handletable->objectHandle)[lpSetColorSpace->ihCS] );
1752 break;
1755 case EMR_DELETECOLORSPACE:
1757 const EMRDELETECOLORSPACE *lpDeleteColorSpace = (const EMRDELETECOLORSPACE *)mr;
1758 DeleteColorSpace( (handletable->objectHandle)[lpDeleteColorSpace->ihCS] );
1759 break;
1762 case EMR_SETICMMODE:
1764 const EMRSETICMMODE *lpSetICMMode = (const EMRSETICMMODE *)mr;
1765 SetICMMode( hdc, (INT)lpSetICMMode->iMode );
1766 break;
1769 case EMR_PIXELFORMAT:
1771 INT iPixelFormat;
1772 const EMRPIXELFORMAT *lpPixelFormat = (const EMRPIXELFORMAT *)mr;
1774 iPixelFormat = ChoosePixelFormat( hdc, &lpPixelFormat->pfd );
1775 SetPixelFormat( hdc, iPixelFormat, &lpPixelFormat->pfd );
1777 break;
1780 case EMR_SETPALETTEENTRIES:
1782 const EMRSETPALETTEENTRIES *lpSetPaletteEntries = (const EMRSETPALETTEENTRIES *)mr;
1784 SetPaletteEntries( (handletable->objectHandle)[lpSetPaletteEntries->ihPal],
1785 (UINT)lpSetPaletteEntries->iStart,
1786 (UINT)lpSetPaletteEntries->cEntries,
1787 lpSetPaletteEntries->aPalEntries );
1789 break;
1792 case EMR_RESIZEPALETTE:
1794 const EMRRESIZEPALETTE *lpResizePalette = (const EMRRESIZEPALETTE *)mr;
1796 ResizePalette( (handletable->objectHandle)[lpResizePalette->ihPal],
1797 (UINT)lpResizePalette->cEntries );
1799 break;
1802 case EMR_CREATEDIBPATTERNBRUSHPT:
1804 const EMRCREATEDIBPATTERNBRUSHPT *lpCreate = (const EMRCREATEDIBPATTERNBRUSHPT *)mr;
1805 LPVOID lpPackedStruct;
1807 /* Check that offsets and data are contained within the record
1808 * (including checking for wrap-arounds).
1810 if ( lpCreate->offBmi + lpCreate->cbBmi > mr->nSize
1811 || lpCreate->offBits + lpCreate->cbBits > mr->nSize
1812 || lpCreate->offBmi + lpCreate->cbBmi < lpCreate->offBmi
1813 || lpCreate->offBits + lpCreate->cbBits < lpCreate->offBits )
1815 ERR("Invalid EMR_CREATEDIBPATTERNBRUSHPT record\n");
1816 break;
1819 /* This is a BITMAPINFO struct followed directly by bitmap bits */
1820 lpPackedStruct = HeapAlloc( GetProcessHeap(), 0,
1821 lpCreate->cbBmi + lpCreate->cbBits );
1822 if(!lpPackedStruct)
1824 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1825 break;
1828 /* Now pack this structure */
1829 memcpy( lpPackedStruct,
1830 ((const BYTE *)lpCreate) + lpCreate->offBmi,
1831 lpCreate->cbBmi );
1832 memcpy( ((BYTE*)lpPackedStruct) + lpCreate->cbBmi,
1833 ((const BYTE *)lpCreate) + lpCreate->offBits,
1834 lpCreate->cbBits );
1836 (handletable->objectHandle)[lpCreate->ihBrush] =
1837 CreateDIBPatternBrushPt( lpPackedStruct,
1838 (UINT)lpCreate->iUsage );
1840 HeapFree(GetProcessHeap(), 0, lpPackedStruct);
1841 break;
1844 case EMR_CREATEMONOBRUSH:
1846 const EMRCREATEMONOBRUSH *pCreateMonoBrush = (const EMRCREATEMONOBRUSH *)mr;
1847 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pCreateMonoBrush->offBmi);
1848 HBITMAP hBmp;
1850 /* Need to check if the bitmap is monochrome, and if the
1851 two colors are really black and white */
1852 if (pCreateMonoBrush->iUsage == DIB_PAL_MONO)
1854 BITMAP bm;
1856 /* Undocumented iUsage indicates a mono bitmap with no palette table,
1857 * aligned to 32 rather than 16 bits.
1859 bm.bmType = 0;
1860 bm.bmWidth = pbi->bmiHeader.biWidth;
1861 bm.bmHeight = abs(pbi->bmiHeader.biHeight);
1862 bm.bmWidthBytes = 4 * ((pbi->bmiHeader.biWidth + 31) / 32);
1863 bm.bmPlanes = pbi->bmiHeader.biPlanes;
1864 bm.bmBitsPixel = pbi->bmiHeader.biBitCount;
1865 bm.bmBits = (BYTE *)mr + pCreateMonoBrush->offBits;
1866 hBmp = CreateBitmapIndirect(&bm);
1868 else if (is_dib_monochrome(pbi))
1870 /* Top-down DIBs have a negative height */
1871 LONG height = pbi->bmiHeader.biHeight;
1873 hBmp = CreateBitmap(pbi->bmiHeader.biWidth, abs(height), 1, 1, NULL);
1874 SetDIBits(hdc, hBmp, 0, pbi->bmiHeader.biHeight,
1875 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1877 else
1879 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1880 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1883 (handletable->objectHandle)[pCreateMonoBrush->ihBrush] = CreatePatternBrush(hBmp);
1885 /* CreatePatternBrush created a copy of the bitmap */
1886 DeleteObject(hBmp);
1887 break;
1890 case EMR_BITBLT:
1892 const EMRBITBLT *pBitBlt = (const EMRBITBLT *)mr;
1894 if(pBitBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1895 PatBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1896 pBitBlt->dwRop);
1897 } else { /* BitBlt */
1898 HDC hdcSrc = CreateCompatibleDC(hdc);
1899 HBRUSH hBrush, hBrushOld;
1900 HBITMAP hBmp = 0, hBmpOld = 0;
1901 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pBitBlt->offBmiSrc);
1903 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1904 SetWorldTransform(hdcSrc, &pBitBlt->xformSrc);
1906 hBrush = CreateSolidBrush(pBitBlt->crBkColorSrc);
1907 hBrushOld = SelectObject(hdcSrc, hBrush);
1908 PatBlt(hdcSrc, pBitBlt->rclBounds.left, pBitBlt->rclBounds.top,
1909 pBitBlt->rclBounds.right - pBitBlt->rclBounds.left,
1910 pBitBlt->rclBounds.bottom - pBitBlt->rclBounds.top, PATCOPY);
1911 SelectObject(hdcSrc, hBrushOld);
1912 DeleteObject(hBrush);
1914 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1915 (const BYTE *)mr + pBitBlt->offBitsSrc, pbi, pBitBlt->iUsageSrc);
1916 hBmpOld = SelectObject(hdcSrc, hBmp);
1918 BitBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1919 hdcSrc, pBitBlt->xSrc, pBitBlt->ySrc, pBitBlt->dwRop);
1921 SelectObject(hdcSrc, hBmpOld);
1922 DeleteObject(hBmp);
1923 DeleteDC(hdcSrc);
1925 break;
1928 case EMR_STRETCHBLT:
1930 const EMRSTRETCHBLT *pStretchBlt = (const EMRSTRETCHBLT *)mr;
1932 TRACE("EMR_STRETCHBLT: %d, %d %dx%d -> %d, %d %dx%d. rop %08x offBitsSrc %d\n",
1933 pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1934 pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1935 pStretchBlt->dwRop, pStretchBlt->offBitsSrc);
1937 if(pStretchBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1938 PatBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1939 pStretchBlt->dwRop);
1940 } else { /* StretchBlt */
1941 HDC hdcSrc = CreateCompatibleDC(hdc);
1942 HBRUSH hBrush, hBrushOld;
1943 HBITMAP hBmp = 0, hBmpOld = 0;
1944 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pStretchBlt->offBmiSrc);
1946 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1947 SetWorldTransform(hdcSrc, &pStretchBlt->xformSrc);
1949 hBrush = CreateSolidBrush(pStretchBlt->crBkColorSrc);
1950 hBrushOld = SelectObject(hdcSrc, hBrush);
1951 PatBlt(hdcSrc, pStretchBlt->rclBounds.left, pStretchBlt->rclBounds.top,
1952 pStretchBlt->rclBounds.right - pStretchBlt->rclBounds.left,
1953 pStretchBlt->rclBounds.bottom - pStretchBlt->rclBounds.top, PATCOPY);
1954 SelectObject(hdcSrc, hBrushOld);
1955 DeleteObject(hBrush);
1957 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1958 (const BYTE *)mr + pStretchBlt->offBitsSrc, pbi, pStretchBlt->iUsageSrc);
1959 hBmpOld = SelectObject(hdcSrc, hBmp);
1961 StretchBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1962 hdcSrc, pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1963 pStretchBlt->dwRop);
1965 SelectObject(hdcSrc, hBmpOld);
1966 DeleteObject(hBmp);
1967 DeleteDC(hdcSrc);
1969 break;
1972 case EMR_ALPHABLEND:
1974 const EMRALPHABLEND *pAlphaBlend = (const EMRALPHABLEND *)mr;
1976 TRACE("EMR_ALPHABLEND: %d, %d %dx%d -> %d, %d %dx%d. blendfn %08x offBitsSrc %d\n",
1977 pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1978 pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1979 pAlphaBlend->dwRop, pAlphaBlend->offBitsSrc);
1981 if(pAlphaBlend->offBmiSrc == 0) {
1982 FIXME("EMR_ALPHABLEND: offBmiSrc == 0\n");
1983 } else {
1984 HDC hdcSrc = CreateCompatibleDC(hdc);
1985 HBITMAP hBmp = 0, hBmpOld = 0;
1986 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pAlphaBlend->offBmiSrc);
1987 void *bits;
1989 SetGraphicsMode(hdcSrc, GM_ADVANCED);
1990 SetWorldTransform(hdcSrc, &pAlphaBlend->xformSrc);
1992 hBmp = CreateDIBSection(hdc, pbi, pAlphaBlend->iUsageSrc, &bits, NULL, 0);
1993 memcpy(bits, (const BYTE *)mr + pAlphaBlend->offBitsSrc, pAlphaBlend->cbBitsSrc);
1994 hBmpOld = SelectObject(hdcSrc, hBmp);
1996 GdiAlphaBlend(hdc, pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1997 hdcSrc, pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1998 *(BLENDFUNCTION *)&pAlphaBlend->dwRop);
2000 SelectObject(hdcSrc, hBmpOld);
2001 DeleteObject(hBmp);
2002 DeleteDC(hdcSrc);
2004 break;
2007 case EMR_MASKBLT:
2009 const EMRMASKBLT *pMaskBlt = (const EMRMASKBLT *)mr;
2010 HDC hdcSrc = CreateCompatibleDC(hdc);
2011 HBRUSH hBrush, hBrushOld;
2012 HBITMAP hBmp, hBmpOld, hBmpMask;
2013 const BITMAPINFO *pbi;
2015 SetGraphicsMode(hdcSrc, GM_ADVANCED);
2016 SetWorldTransform(hdcSrc, &pMaskBlt->xformSrc);
2018 hBrush = CreateSolidBrush(pMaskBlt->crBkColorSrc);
2019 hBrushOld = SelectObject(hdcSrc, hBrush);
2020 PatBlt(hdcSrc, pMaskBlt->rclBounds.left, pMaskBlt->rclBounds.top,
2021 pMaskBlt->rclBounds.right - pMaskBlt->rclBounds.left,
2022 pMaskBlt->rclBounds.bottom - pMaskBlt->rclBounds.top, PATCOPY);
2023 SelectObject(hdcSrc, hBrushOld);
2024 DeleteObject(hBrush);
2026 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiMask);
2027 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
2028 1, 1, NULL);
2029 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
2030 (const BYTE *)mr + pMaskBlt->offBitsMask, pbi, pMaskBlt->iUsageMask);
2032 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiSrc);
2033 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
2034 (const BYTE *)mr + pMaskBlt->offBitsSrc, pbi, pMaskBlt->iUsageSrc);
2035 hBmpOld = SelectObject(hdcSrc, hBmp);
2036 MaskBlt(hdc,
2037 pMaskBlt->xDest,
2038 pMaskBlt->yDest,
2039 pMaskBlt->cxDest,
2040 pMaskBlt->cyDest,
2041 hdcSrc,
2042 pMaskBlt->xSrc,
2043 pMaskBlt->ySrc,
2044 hBmpMask,
2045 pMaskBlt->xMask,
2046 pMaskBlt->yMask,
2047 pMaskBlt->dwRop);
2048 SelectObject(hdcSrc, hBmpOld);
2049 DeleteObject(hBmp);
2050 DeleteObject(hBmpMask);
2051 DeleteDC(hdcSrc);
2052 break;
2055 case EMR_PLGBLT:
2057 const EMRPLGBLT *pPlgBlt = (const EMRPLGBLT *)mr;
2058 HDC hdcSrc = CreateCompatibleDC(hdc);
2059 HBRUSH hBrush, hBrushOld;
2060 HBITMAP hBmp, hBmpOld, hBmpMask;
2061 const BITMAPINFO *pbi;
2062 POINT pts[3];
2064 SetGraphicsMode(hdcSrc, GM_ADVANCED);
2065 SetWorldTransform(hdcSrc, &pPlgBlt->xformSrc);
2067 pts[0].x = pPlgBlt->aptlDest[0].x; pts[0].y = pPlgBlt->aptlDest[0].y;
2068 pts[1].x = pPlgBlt->aptlDest[1].x; pts[1].y = pPlgBlt->aptlDest[1].y;
2069 pts[2].x = pPlgBlt->aptlDest[2].x; pts[2].y = pPlgBlt->aptlDest[2].y;
2071 hBrush = CreateSolidBrush(pPlgBlt->crBkColorSrc);
2072 hBrushOld = SelectObject(hdcSrc, hBrush);
2073 PatBlt(hdcSrc, pPlgBlt->rclBounds.left, pPlgBlt->rclBounds.top,
2074 pPlgBlt->rclBounds.right - pPlgBlt->rclBounds.left,
2075 pPlgBlt->rclBounds.bottom - pPlgBlt->rclBounds.top, PATCOPY);
2076 SelectObject(hdcSrc, hBrushOld);
2077 DeleteObject(hBrush);
2079 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiMask);
2080 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
2081 1, 1, NULL);
2082 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
2083 (const BYTE *)mr + pPlgBlt->offBitsMask, pbi, pPlgBlt->iUsageMask);
2085 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiSrc);
2086 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
2087 (const BYTE *)mr + pPlgBlt->offBitsSrc, pbi, pPlgBlt->iUsageSrc);
2088 hBmpOld = SelectObject(hdcSrc, hBmp);
2089 PlgBlt(hdc,
2090 pts,
2091 hdcSrc,
2092 pPlgBlt->xSrc,
2093 pPlgBlt->ySrc,
2094 pPlgBlt->cxSrc,
2095 pPlgBlt->cySrc,
2096 hBmpMask,
2097 pPlgBlt->xMask,
2098 pPlgBlt->yMask);
2099 SelectObject(hdcSrc, hBmpOld);
2100 DeleteObject(hBmp);
2101 DeleteObject(hBmpMask);
2102 DeleteDC(hdcSrc);
2103 break;
2106 case EMR_SETDIBITSTODEVICE:
2108 const EMRSETDIBITSTODEVICE *pSetDIBitsToDevice = (const EMRSETDIBITSTODEVICE *)mr;
2110 SetDIBitsToDevice(hdc,
2111 pSetDIBitsToDevice->xDest,
2112 pSetDIBitsToDevice->yDest,
2113 pSetDIBitsToDevice->cxSrc,
2114 pSetDIBitsToDevice->cySrc,
2115 pSetDIBitsToDevice->xSrc,
2116 pSetDIBitsToDevice->ySrc,
2117 pSetDIBitsToDevice->iStartScan,
2118 pSetDIBitsToDevice->cScans,
2119 (const BYTE *)mr + pSetDIBitsToDevice->offBitsSrc,
2120 (const BITMAPINFO *)((const BYTE *)mr + pSetDIBitsToDevice->offBmiSrc),
2121 pSetDIBitsToDevice->iUsageSrc);
2122 break;
2125 case EMR_POLYTEXTOUTA:
2127 const EMRPOLYTEXTOUTA *pPolyTextOutA = (const EMRPOLYTEXTOUTA *)mr;
2128 POLYTEXTA *polytextA = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutA->cStrings * sizeof(POLYTEXTA));
2129 LONG i;
2130 XFORM xform, xformOld;
2131 int gModeOld;
2133 gModeOld = SetGraphicsMode(hdc, pPolyTextOutA->iGraphicsMode);
2134 GetWorldTransform(hdc, &xformOld);
2136 xform.eM11 = pPolyTextOutA->exScale;
2137 xform.eM12 = 0.0;
2138 xform.eM21 = 0.0;
2139 xform.eM22 = pPolyTextOutA->eyScale;
2140 xform.eDx = 0.0;
2141 xform.eDy = 0.0;
2142 SetWorldTransform(hdc, &xform);
2144 /* Set up POLYTEXTA structures */
2145 for(i = 0; i < pPolyTextOutA->cStrings; i++)
2147 polytextA[i].x = pPolyTextOutA->aemrtext[i].ptlReference.x;
2148 polytextA[i].y = pPolyTextOutA->aemrtext[i].ptlReference.y;
2149 polytextA[i].n = pPolyTextOutA->aemrtext[i].nChars;
2150 polytextA[i].lpstr = (LPCSTR)((const BYTE *)mr + pPolyTextOutA->aemrtext[i].offString);
2151 polytextA[i].uiFlags = pPolyTextOutA->aemrtext[i].fOptions;
2152 polytextA[i].rcl.left = pPolyTextOutA->aemrtext[i].rcl.left;
2153 polytextA[i].rcl.right = pPolyTextOutA->aemrtext[i].rcl.right;
2154 polytextA[i].rcl.top = pPolyTextOutA->aemrtext[i].rcl.top;
2155 polytextA[i].rcl.bottom = pPolyTextOutA->aemrtext[i].rcl.bottom;
2156 polytextA[i].pdx = (int *)((BYTE *)mr + pPolyTextOutA->aemrtext[i].offDx);
2158 PolyTextOutA(hdc, polytextA, pPolyTextOutA->cStrings);
2159 HeapFree(GetProcessHeap(), 0, polytextA);
2161 SetWorldTransform(hdc, &xformOld);
2162 SetGraphicsMode(hdc, gModeOld);
2163 break;
2166 case EMR_POLYTEXTOUTW:
2168 const EMRPOLYTEXTOUTW *pPolyTextOutW = (const EMRPOLYTEXTOUTW *)mr;
2169 POLYTEXTW *polytextW = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutW->cStrings * sizeof(POLYTEXTW));
2170 LONG i;
2171 XFORM xform, xformOld;
2172 int gModeOld;
2174 gModeOld = SetGraphicsMode(hdc, pPolyTextOutW->iGraphicsMode);
2175 GetWorldTransform(hdc, &xformOld);
2177 xform.eM11 = pPolyTextOutW->exScale;
2178 xform.eM12 = 0.0;
2179 xform.eM21 = 0.0;
2180 xform.eM22 = pPolyTextOutW->eyScale;
2181 xform.eDx = 0.0;
2182 xform.eDy = 0.0;
2183 SetWorldTransform(hdc, &xform);
2185 /* Set up POLYTEXTW structures */
2186 for(i = 0; i < pPolyTextOutW->cStrings; i++)
2188 polytextW[i].x = pPolyTextOutW->aemrtext[i].ptlReference.x;
2189 polytextW[i].y = pPolyTextOutW->aemrtext[i].ptlReference.y;
2190 polytextW[i].n = pPolyTextOutW->aemrtext[i].nChars;
2191 polytextW[i].lpstr = (LPCWSTR)((const BYTE *)mr + pPolyTextOutW->aemrtext[i].offString);
2192 polytextW[i].uiFlags = pPolyTextOutW->aemrtext[i].fOptions;
2193 polytextW[i].rcl.left = pPolyTextOutW->aemrtext[i].rcl.left;
2194 polytextW[i].rcl.right = pPolyTextOutW->aemrtext[i].rcl.right;
2195 polytextW[i].rcl.top = pPolyTextOutW->aemrtext[i].rcl.top;
2196 polytextW[i].rcl.bottom = pPolyTextOutW->aemrtext[i].rcl.bottom;
2197 polytextW[i].pdx = (int *)((BYTE *)mr + pPolyTextOutW->aemrtext[i].offDx);
2199 PolyTextOutW(hdc, polytextW, pPolyTextOutW->cStrings);
2200 HeapFree(GetProcessHeap(), 0, polytextW);
2202 SetWorldTransform(hdc, &xformOld);
2203 SetGraphicsMode(hdc, gModeOld);
2204 break;
2207 case EMR_FILLRGN:
2209 const EMRFILLRGN *pFillRgn = (const EMRFILLRGN *)mr;
2210 HRGN hRgn = ExtCreateRegion(NULL, pFillRgn->cbRgnData, (const RGNDATA *)pFillRgn->RgnData);
2211 FillRgn(hdc,
2212 hRgn,
2213 (handletable->objectHandle)[pFillRgn->ihBrush]);
2214 DeleteObject(hRgn);
2215 break;
2218 case EMR_FRAMERGN:
2220 const EMRFRAMERGN *pFrameRgn = (const EMRFRAMERGN *)mr;
2221 HRGN hRgn = ExtCreateRegion(NULL, pFrameRgn->cbRgnData, (const RGNDATA *)pFrameRgn->RgnData);
2222 FrameRgn(hdc,
2223 hRgn,
2224 (handletable->objectHandle)[pFrameRgn->ihBrush],
2225 pFrameRgn->szlStroke.cx,
2226 pFrameRgn->szlStroke.cy);
2227 DeleteObject(hRgn);
2228 break;
2231 case EMR_INVERTRGN:
2233 const EMRINVERTRGN *pInvertRgn = (const EMRINVERTRGN *)mr;
2234 HRGN hRgn = ExtCreateRegion(NULL, pInvertRgn->cbRgnData, (const RGNDATA *)pInvertRgn->RgnData);
2235 InvertRgn(hdc, hRgn);
2236 DeleteObject(hRgn);
2237 break;
2240 case EMR_PAINTRGN:
2242 const EMRPAINTRGN *pPaintRgn = (const EMRPAINTRGN *)mr;
2243 HRGN hRgn = ExtCreateRegion(NULL, pPaintRgn->cbRgnData, (const RGNDATA *)pPaintRgn->RgnData);
2244 PaintRgn(hdc, hRgn);
2245 DeleteObject(hRgn);
2246 break;
2249 case EMR_SETTEXTJUSTIFICATION:
2251 const EMRSETTEXTJUSTIFICATION *pSetTextJust = (const EMRSETTEXTJUSTIFICATION *)mr;
2252 SetTextJustification(hdc, pSetTextJust->nBreakExtra, pSetTextJust->nBreakCount);
2253 break;
2256 case EMR_SETLAYOUT:
2258 const EMRSETLAYOUT *pSetLayout = (const EMRSETLAYOUT *)mr;
2259 SetLayout(hdc, pSetLayout->iMode);
2260 break;
2263 case EMR_GRADIENTFILL:
2265 EMRGRADIENTFILL *grad = (EMRGRADIENTFILL *)mr;
2266 GdiGradientFill( hdc, grad->Ver, grad->nVer, grad->Ver + grad->nVer,
2267 grad->nTri, grad->ulMode );
2268 break;
2271 case EMR_GLSRECORD:
2272 case EMR_GLSBOUNDEDRECORD:
2273 case EMR_DRAWESCAPE:
2274 case EMR_EXTESCAPE:
2275 case EMR_STARTDOC:
2276 case EMR_SMALLTEXTOUT:
2277 case EMR_FORCEUFIMAPPING:
2278 case EMR_NAMEDESCAPE:
2279 case EMR_COLORCORRECTPALETTE:
2280 case EMR_SETICMPROFILEA:
2281 case EMR_SETICMPROFILEW:
2282 case EMR_TRANSPARENTBLT:
2283 case EMR_SETLINKEDUFI:
2284 case EMR_COLORMATCHTOTARGETW:
2285 case EMR_CREATECOLORSPACEW:
2287 default:
2288 /* From docs: If PlayEnhMetaFileRecord doesn't recognize a
2289 record then ignore and return TRUE. */
2290 FIXME("type %d is unimplemented\n", type);
2291 break;
2293 tmprc.left = tmprc.top = 0;
2294 tmprc.right = tmprc.bottom = 1000;
2295 LPtoDP(hdc, (POINT*)&tmprc, 2);
2296 TRACE("L:0,0 - 1000,1000 -> D:%s\n", wine_dbgstr_rect(&tmprc));
2298 return TRUE;
2302 /*****************************************************************************
2304 * EnumEnhMetaFile (GDI32.@)
2306 * Walk an enhanced metafile, calling a user-specified function _EnhMetaFunc_
2307 * for each
2308 * record. Returns when either every record has been used or
2309 * when _EnhMetaFunc_ returns FALSE.
2312 * RETURNS
2313 * TRUE if every record is used, FALSE if any invocation of _EnhMetaFunc_
2314 * returns FALSE.
2316 * BUGS
2317 * Ignores rect.
2319 * NOTES
2320 * This function behaves differently in Win9x and WinNT.
2322 * In WinNT, the DC's world transform is updated as the EMF changes
2323 * the Window/Viewport Extent and Origin or its world transform.
2324 * The actual Window/Viewport Extent and Origin are left untouched.
2326 * In Win9x, the DC is left untouched, and PlayEnhMetaFileRecord
2327 * updates the scaling itself but only just before a record that
2328 * writes anything to the DC.
2330 * I'm not sure where the data (enum_emh_data) is stored in either
2331 * version. For this implementation, it is stored before the handle
2332 * table, but it could be stored in the DC, in the EMF handle or in
2333 * TLS.
2334 * MJM 5 Oct 2002
2336 BOOL WINAPI EnumEnhMetaFile(
2337 HDC hdc, /* [in] device context to pass to _EnhMetaFunc_ */
2338 HENHMETAFILE hmf, /* [in] EMF to walk */
2339 ENHMFENUMPROC callback, /* [in] callback function */
2340 LPVOID data, /* [in] optional data for callback function */
2341 const RECT *lpRect /* [in] bounding rectangle for rendered metafile */
2344 BOOL ret;
2345 ENHMETAHEADER *emh;
2346 ENHMETARECORD *emr;
2347 DWORD offset;
2348 UINT i;
2349 HANDLETABLE *ht;
2350 INT savedMode = 0;
2351 XFORM savedXform;
2352 HPEN hPen = NULL;
2353 HBRUSH hBrush = NULL;
2354 HFONT hFont = NULL;
2355 HRGN hRgn = NULL;
2356 enum_emh_data *info;
2357 SIZE vp_size, win_size;
2358 POINT vp_org, win_org;
2359 INT mapMode = MM_TEXT, old_align = 0, old_rop2 = 0, old_arcdir = 0, old_polyfill = 0, old_stretchblt = 0;
2360 COLORREF old_text_color = 0, old_bk_color = 0;
2362 if(!lpRect && hdc)
2364 SetLastError(ERROR_INVALID_PARAMETER);
2365 return FALSE;
2368 emh = EMF_GetEnhMetaHeader(hmf);
2369 if(!emh) {
2370 SetLastError(ERROR_INVALID_HANDLE);
2371 return FALSE;
2374 info = HeapAlloc( GetProcessHeap(), 0,
2375 sizeof (enum_emh_data) + sizeof(HANDLETABLE) * emh->nHandles );
2376 if(!info)
2378 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2379 return FALSE;
2381 info->state.mode = MM_TEXT;
2382 info->state.wndOrgX = 0;
2383 info->state.wndOrgY = 0;
2384 info->state.wndExtX = 1;
2385 info->state.wndExtY = 1;
2386 info->state.vportOrgX = 0;
2387 info->state.vportOrgY = 0;
2388 info->state.vportExtX = 1;
2389 info->state.vportExtY = 1;
2390 info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
2391 info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
2392 info->state.world_transform.eDx = info->state.world_transform.eDy = 0;
2394 info->state.next = NULL;
2395 info->save_level = 0;
2396 info->saved_state = NULL;
2398 ht = (HANDLETABLE*) &info[1];
2399 ht->objectHandle[0] = hmf;
2400 for(i = 1; i < emh->nHandles; i++)
2401 ht->objectHandle[i] = NULL;
2403 if(hdc)
2405 savedMode = SetGraphicsMode(hdc, GM_ADVANCED);
2406 GetWorldTransform(hdc, &savedXform);
2407 GetViewportExtEx(hdc, &vp_size);
2408 GetWindowExtEx(hdc, &win_size);
2409 GetViewportOrgEx(hdc, &vp_org);
2410 GetWindowOrgEx(hdc, &win_org);
2411 mapMode = GetMapMode(hdc);
2413 /* save DC */
2414 hPen = GetCurrentObject(hdc, OBJ_PEN);
2415 hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
2416 hFont = GetCurrentObject(hdc, OBJ_FONT);
2418 hRgn = CreateRectRgn(0, 0, 0, 0);
2419 if (!GetClipRgn(hdc, hRgn))
2421 DeleteObject(hRgn);
2422 hRgn = 0;
2425 old_text_color = SetTextColor(hdc, RGB(0,0,0));
2426 old_bk_color = SetBkColor(hdc, RGB(0xff, 0xff, 0xff));
2427 old_align = SetTextAlign(hdc, 0);
2428 old_rop2 = SetROP2(hdc, R2_COPYPEN);
2429 old_arcdir = SetArcDirection(hdc, AD_COUNTERCLOCKWISE);
2430 old_polyfill = SetPolyFillMode(hdc, ALTERNATE);
2431 old_stretchblt = SetStretchBltMode(hdc, BLACKONWHITE);
2433 if ( IS_WIN9X() )
2435 /* Win95 leaves the vp/win ext/org info alone */
2436 info->init_transform.eM11 = 1.0;
2437 info->init_transform.eM12 = 0.0;
2438 info->init_transform.eM21 = 0.0;
2439 info->init_transform.eM22 = 1.0;
2440 info->init_transform.eDx = 0.0;
2441 info->init_transform.eDy = 0.0;
2443 else
2445 /* WinNT combines the vp/win ext/org info into a transform */
2446 double xscale, yscale;
2447 xscale = (double)vp_size.cx / (double)win_size.cx;
2448 yscale = (double)vp_size.cy / (double)win_size.cy;
2449 info->init_transform.eM11 = xscale;
2450 info->init_transform.eM12 = 0.0;
2451 info->init_transform.eM21 = 0.0;
2452 info->init_transform.eM22 = yscale;
2453 info->init_transform.eDx = (double)vp_org.x - xscale * (double)win_org.x;
2454 info->init_transform.eDy = (double)vp_org.y - yscale * (double)win_org.y;
2456 CombineTransform(&info->init_transform, &savedXform, &info->init_transform);
2459 if ( lpRect && WIDTH(emh->rclFrame) && HEIGHT(emh->rclFrame) )
2461 double xSrcPixSize, ySrcPixSize, xscale, yscale;
2462 XFORM xform;
2464 TRACE("rect: %s. rclFrame: (%d,%d)-(%d,%d)\n", wine_dbgstr_rect(lpRect),
2465 emh->rclFrame.left, emh->rclFrame.top, emh->rclFrame.right,
2466 emh->rclFrame.bottom);
2468 xSrcPixSize = (double) emh->szlMillimeters.cx / emh->szlDevice.cx;
2469 ySrcPixSize = (double) emh->szlMillimeters.cy / emh->szlDevice.cy;
2470 xscale = (double) WIDTH(*lpRect) * 100.0 /
2471 WIDTH(emh->rclFrame) * xSrcPixSize;
2472 yscale = (double) HEIGHT(*lpRect) * 100.0 /
2473 HEIGHT(emh->rclFrame) * ySrcPixSize;
2474 TRACE("xscale = %f, yscale = %f\n", xscale, yscale);
2476 xform.eM11 = xscale;
2477 xform.eM12 = 0;
2478 xform.eM21 = 0;
2479 xform.eM22 = yscale;
2480 xform.eDx = (double) lpRect->left - (double) WIDTH(*lpRect) / WIDTH(emh->rclFrame) * emh->rclFrame.left;
2481 xform.eDy = (double) lpRect->top - (double) HEIGHT(*lpRect) / HEIGHT(emh->rclFrame) * emh->rclFrame.top;
2483 CombineTransform(&info->init_transform, &xform, &info->init_transform);
2486 /* WinNT resets the current vp/win org/ext */
2487 if ( !IS_WIN9X() )
2489 SetMapMode(hdc, MM_TEXT);
2490 SetWindowOrgEx(hdc, 0, 0, NULL);
2491 SetViewportOrgEx(hdc, 0, 0, NULL);
2492 EMF_Update_MF_Xform(hdc, info);
2496 ret = TRUE;
2497 offset = 0;
2498 while(ret && offset < emh->nBytes)
2500 emr = (ENHMETARECORD *)((char *)emh + offset);
2502 if (offset + 8 > emh->nBytes ||
2503 offset > offset + emr->nSize ||
2504 offset + emr->nSize > emh->nBytes)
2506 WARN("record truncated\n");
2507 break;
2510 /* In Win9x mode we update the xform if the record will produce output */
2511 if (hdc && IS_WIN9X() && emr_produces_output(emr->iType))
2512 EMF_Update_MF_Xform(hdc, info);
2514 TRACE("Calling EnumFunc with record %s, size %d\n", get_emr_name(emr->iType), emr->nSize);
2515 ret = (*callback)(hdc, ht, emr, emh->nHandles, (LPARAM)data);
2516 offset += emr->nSize;
2519 if (hdc)
2521 SetStretchBltMode(hdc, old_stretchblt);
2522 SetPolyFillMode(hdc, old_polyfill);
2523 SetArcDirection(hdc, old_arcdir);
2524 SetROP2(hdc, old_rop2);
2525 SetTextAlign(hdc, old_align);
2526 SetBkColor(hdc, old_bk_color);
2527 SetTextColor(hdc, old_text_color);
2529 /* restore DC */
2530 SelectObject(hdc, hBrush);
2531 SelectObject(hdc, hPen);
2532 SelectObject(hdc, hFont);
2533 ExtSelectClipRgn(hdc, hRgn, RGN_COPY);
2534 DeleteObject(hRgn);
2536 SetWorldTransform(hdc, &savedXform);
2537 if (savedMode)
2538 SetGraphicsMode(hdc, savedMode);
2539 SetMapMode(hdc, mapMode);
2540 SetWindowOrgEx(hdc, win_org.x, win_org.y, NULL);
2541 SetWindowExtEx(hdc, win_size.cx, win_size.cy, NULL);
2542 SetViewportOrgEx(hdc, vp_org.x, vp_org.y, NULL);
2543 SetViewportExtEx(hdc, vp_size.cx, vp_size.cy, NULL);
2546 for(i = 1; i < emh->nHandles; i++) /* Don't delete element 0 (hmf) */
2547 if( (ht->objectHandle)[i] )
2548 DeleteObject( (ht->objectHandle)[i] );
2550 while (info->saved_state)
2552 EMF_dc_state *state = info->saved_state;
2553 info->saved_state = info->saved_state->next;
2554 HeapFree( GetProcessHeap(), 0, state );
2556 HeapFree( GetProcessHeap(), 0, info );
2557 return ret;
2560 static INT CALLBACK EMF_PlayEnhMetaFileCallback(HDC hdc, HANDLETABLE *ht,
2561 const ENHMETARECORD *emr,
2562 INT handles, LPARAM data)
2564 return PlayEnhMetaFileRecord(hdc, ht, emr, handles);
2567 /**************************************************************************
2568 * PlayEnhMetaFile (GDI32.@)
2570 * Renders an enhanced metafile into a specified rectangle *lpRect
2571 * in device context hdc.
2573 * RETURNS
2574 * Success: TRUE
2575 * Failure: FALSE
2577 BOOL WINAPI PlayEnhMetaFile(
2578 HDC hdc, /* [in] DC to render into */
2579 HENHMETAFILE hmf, /* [in] metafile to render */
2580 const RECT *lpRect /* [in] rectangle to place metafile inside */
2583 return EnumEnhMetaFile(hdc, hmf, EMF_PlayEnhMetaFileCallback, NULL,
2584 lpRect);
2587 /*****************************************************************************
2588 * DeleteEnhMetaFile (GDI32.@)
2590 * Deletes an enhanced metafile and frees the associated storage.
2592 BOOL WINAPI DeleteEnhMetaFile(HENHMETAFILE hmf)
2594 return EMF_Delete_HENHMETAFILE( hmf );
2597 /*****************************************************************************
2598 * CopyEnhMetaFileA (GDI32.@)
2600 * Duplicate an enhanced metafile.
2604 HENHMETAFILE WINAPI CopyEnhMetaFileA(
2605 HENHMETAFILE hmfSrc,
2606 LPCSTR file)
2608 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2609 HENHMETAFILE hmfDst;
2611 if(!emrSrc) return FALSE;
2612 if (!file) {
2613 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2614 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2615 hmfDst = EMF_Create_HENHMETAFILE( emrDst, emrSrc->nBytes, FALSE );
2616 if (!hmfDst)
2617 HeapFree( GetProcessHeap(), 0, emrDst );
2618 } else {
2619 HANDLE hFile;
2620 DWORD w;
2621 hFile = CreateFileA( file, GENERIC_WRITE | GENERIC_READ, 0,
2622 NULL, CREATE_ALWAYS, 0, 0);
2623 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2624 CloseHandle( hFile );
2625 /* Reopen file for reading only, so that apps can share
2626 read access to the file while hmf is still valid */
2627 hFile = CreateFileA( file, GENERIC_READ, FILE_SHARE_READ,
2628 NULL, OPEN_EXISTING, 0, 0);
2629 if(hFile == INVALID_HANDLE_VALUE) {
2630 ERR("Can't reopen emf for reading\n");
2631 return 0;
2633 hmfDst = EMF_GetEnhMetaFile( hFile );
2634 CloseHandle( hFile );
2636 return hmfDst;
2639 /*****************************************************************************
2640 * CopyEnhMetaFileW (GDI32.@)
2642 * See CopyEnhMetaFileA.
2646 HENHMETAFILE WINAPI CopyEnhMetaFileW(
2647 HENHMETAFILE hmfSrc,
2648 LPCWSTR file)
2650 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2651 HENHMETAFILE hmfDst;
2653 if(!emrSrc) return FALSE;
2654 if (!file) {
2655 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2656 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2657 hmfDst = EMF_Create_HENHMETAFILE( emrDst, emrSrc->nBytes, FALSE );
2658 if (!hmfDst)
2659 HeapFree( GetProcessHeap(), 0, emrDst );
2660 } else {
2661 HANDLE hFile;
2662 DWORD w;
2663 hFile = CreateFileW( file, GENERIC_WRITE | GENERIC_READ, 0,
2664 NULL, CREATE_ALWAYS, 0, 0);
2665 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2666 CloseHandle( hFile );
2667 /* Reopen file for reading only, so that apps can share
2668 read access to the file while hmf is still valid */
2669 hFile = CreateFileW( file, GENERIC_READ, FILE_SHARE_READ,
2670 NULL, OPEN_EXISTING, 0, 0);
2671 if(hFile == INVALID_HANDLE_VALUE) {
2672 ERR("Can't reopen emf for reading\n");
2673 return 0;
2675 hmfDst = EMF_GetEnhMetaFile( hFile );
2676 CloseHandle( hFile );
2678 return hmfDst;
2682 /* Struct to be used to be passed in the LPVOID parameter for cbEnhPaletteCopy */
2683 typedef struct tagEMF_PaletteCopy
2685 UINT cEntries;
2686 LPPALETTEENTRY lpPe;
2687 } EMF_PaletteCopy;
2689 /***************************************************************
2690 * Find the EMR_EOF record and then use it to find the
2691 * palette entries for this enhanced metafile.
2692 * The lpData is actually a pointer to an EMF_PaletteCopy struct
2693 * which contains the max number of elements to copy and where
2694 * to copy them to.
2696 * NOTE: To be used by GetEnhMetaFilePaletteEntries only!
2698 static INT CALLBACK cbEnhPaletteCopy( HDC a,
2699 HANDLETABLE *b,
2700 const ENHMETARECORD *lpEMR,
2701 INT c,
2702 LPARAM lpData )
2705 if ( lpEMR->iType == EMR_EOF )
2707 const EMREOF *lpEof = (const EMREOF *)lpEMR;
2708 EMF_PaletteCopy* info = (EMF_PaletteCopy*)lpData;
2709 DWORD dwNumPalToCopy = min( lpEof->nPalEntries, info->cEntries );
2711 TRACE( "copying 0x%08x palettes\n", dwNumPalToCopy );
2713 memcpy( info->lpPe, (LPCSTR)lpEof + lpEof->offPalEntries,
2714 sizeof( *(info->lpPe) ) * dwNumPalToCopy );
2716 /* Update the passed data as a return code */
2717 info->lpPe = NULL; /* Palettes were copied! */
2718 info->cEntries = dwNumPalToCopy;
2720 return FALSE; /* That's all we need */
2723 return TRUE;
2726 /*****************************************************************************
2727 * GetEnhMetaFilePaletteEntries (GDI32.@)
2729 * Copy the palette and report size
2731 * BUGS: Error codes (SetLastError) are not set on failures
2733 UINT WINAPI GetEnhMetaFilePaletteEntries( HENHMETAFILE hEmf,
2734 UINT cEntries,
2735 LPPALETTEENTRY lpPe )
2737 ENHMETAHEADER* enhHeader = EMF_GetEnhMetaHeader( hEmf );
2738 EMF_PaletteCopy infoForCallBack;
2740 TRACE( "(%p,%d,%p)\n", hEmf, cEntries, lpPe );
2742 if (!enhHeader) return 0;
2744 /* First check if there are any palettes associated with
2745 this metafile. */
2746 if ( enhHeader->nPalEntries == 0 ) return 0;
2748 /* Is the user requesting the number of palettes? */
2749 if ( lpPe == NULL ) return enhHeader->nPalEntries;
2751 /* Copy cEntries worth of PALETTEENTRY structs into the buffer */
2752 infoForCallBack.cEntries = cEntries;
2753 infoForCallBack.lpPe = lpPe;
2755 if ( !EnumEnhMetaFile( 0, hEmf, cbEnhPaletteCopy,
2756 &infoForCallBack, 0 ) )
2757 return GDI_ERROR;
2759 /* Verify that the callback executed correctly */
2760 if ( infoForCallBack.lpPe != NULL )
2762 /* Callback proc had error! */
2763 ERR( "cbEnhPaletteCopy didn't execute correctly\n" );
2764 return GDI_ERROR;
2767 return infoForCallBack.cEntries;
2770 /******************************************************************
2771 * extract_emf_from_comment
2773 * If the WMF was created by GetWinMetaFileBits, then extract the
2774 * original EMF that is stored in MFCOMMENT chunks.
2776 static HENHMETAFILE extract_emf_from_comment( const BYTE *buf, UINT mf_size )
2778 METAHEADER *mh = (METAHEADER *)buf;
2779 METARECORD *mr;
2780 emf_in_wmf_comment *chunk;
2781 WORD checksum = 0;
2782 DWORD size = 0, remaining, chunks;
2783 BYTE *emf_bits = NULL, *ptr;
2784 UINT offset;
2785 HENHMETAFILE emf = NULL;
2787 if (mf_size < sizeof(*mh)) return NULL;
2789 for (offset = mh->mtHeaderSize * 2; offset < mf_size; offset += (mr->rdSize * 2))
2791 mr = (METARECORD *)((char *)mh + offset);
2792 chunk = (emf_in_wmf_comment *)(mr->rdParm + 2);
2794 if (mr->rdFunction != META_ESCAPE || mr->rdParm[0] != MFCOMMENT) goto done;
2795 if (chunk->magic != WMFC_MAGIC) goto done;
2797 if (!emf_bits)
2799 size = remaining = chunk->emf_size;
2800 chunks = chunk->num_chunks;
2801 emf_bits = ptr = HeapAlloc( GetProcessHeap(), 0, size );
2802 if (!emf_bits) goto done;
2804 if (chunk->chunk_size > remaining) goto done;
2805 remaining -= chunk->chunk_size;
2806 if (chunk->remaining_size != remaining) goto done;
2807 memcpy( ptr, chunk->emf_data, chunk->chunk_size );
2808 ptr += chunk->chunk_size;
2809 if (--chunks == 0) break;
2812 for (offset = 0; offset < mf_size / 2; offset++)
2813 checksum += *((WORD *)buf + offset);
2814 if (checksum) goto done;
2816 emf = SetEnhMetaFileBits( size, emf_bits );
2818 done:
2819 HeapFree( GetProcessHeap(), 0, emf_bits );
2820 return emf;
2823 typedef struct wmf_in_emf_comment
2825 DWORD ident;
2826 DWORD iComment;
2827 DWORD nVersion;
2828 DWORD nChecksum;
2829 DWORD fFlags;
2830 DWORD cbWinMetaFile;
2831 } wmf_in_emf_comment;
2833 /******************************************************************
2834 * SetWinMetaFileBits (GDI32.@)
2836 * Translate from old style to new style.
2839 HENHMETAFILE WINAPI SetWinMetaFileBits(UINT cbBuffer, const BYTE *lpbBuffer, HDC hdcRef,
2840 const METAFILEPICT *lpmfp)
2842 static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
2843 HMETAFILE hmf = NULL;
2844 HENHMETAFILE ret = NULL;
2845 HDC hdc = NULL, hdcdisp = NULL;
2846 RECT rc, *prcFrame = NULL;
2847 LONG mm, xExt, yExt;
2848 INT horzsize, vertsize, horzres, vertres;
2850 TRACE("(%d, %p, %p, %p)\n", cbBuffer, lpbBuffer, hdcRef, lpmfp);
2852 hmf = SetMetaFileBitsEx(cbBuffer, lpbBuffer);
2853 if(!hmf)
2855 WARN("SetMetaFileBitsEx failed\n");
2856 return NULL;
2859 ret = extract_emf_from_comment( lpbBuffer, cbBuffer );
2860 if (ret) return ret;
2862 if(!hdcRef)
2863 hdcRef = hdcdisp = CreateDCW(szDisplayW, NULL, NULL, NULL);
2865 if (lpmfp)
2867 TRACE("mm = %d %dx%d\n", lpmfp->mm, lpmfp->xExt, lpmfp->yExt);
2869 mm = lpmfp->mm;
2870 xExt = lpmfp->xExt;
2871 yExt = lpmfp->yExt;
2873 else
2875 TRACE("lpmfp == NULL\n");
2877 /* Use the whole device surface */
2878 mm = MM_ANISOTROPIC;
2879 xExt = 0;
2880 yExt = 0;
2883 if (mm == MM_ISOTROPIC || mm == MM_ANISOTROPIC)
2885 if (xExt < 0 || yExt < 0)
2887 /* Use the whole device surface */
2888 xExt = 0;
2889 yExt = 0;
2892 /* Use the x and y extents as the frame box */
2893 if (xExt && yExt)
2895 rc.left = rc.top = 0;
2896 rc.right = xExt;
2897 rc.bottom = yExt;
2898 prcFrame = &rc;
2902 if(!(hdc = CreateEnhMetaFileW(hdcRef, NULL, prcFrame, NULL)))
2904 ERR("CreateEnhMetaFile failed\n");
2905 goto end;
2909 * Write the original METAFILE into the enhanced metafile.
2910 * It is encapsulated in a GDICOMMENT_WINDOWS_METAFILE record.
2912 if (mm != MM_TEXT)
2914 wmf_in_emf_comment *mfcomment;
2915 UINT mfcomment_size;
2917 mfcomment_size = sizeof (*mfcomment) + cbBuffer;
2918 mfcomment = HeapAlloc(GetProcessHeap(), 0, mfcomment_size);
2919 if (mfcomment)
2921 mfcomment->ident = GDICOMMENT_IDENTIFIER;
2922 mfcomment->iComment = GDICOMMENT_WINDOWS_METAFILE;
2923 mfcomment->nVersion = 0x00000300;
2924 mfcomment->nChecksum = 0; /* FIXME */
2925 mfcomment->fFlags = 0;
2926 mfcomment->cbWinMetaFile = cbBuffer;
2927 memcpy(&mfcomment[1], lpbBuffer, cbBuffer);
2928 GdiComment(hdc, mfcomment_size, (BYTE*) mfcomment);
2929 HeapFree(GetProcessHeap(), 0, mfcomment);
2931 SetMapMode(hdc, mm);
2935 horzsize = GetDeviceCaps(hdcRef, HORZSIZE);
2936 vertsize = GetDeviceCaps(hdcRef, VERTSIZE);
2937 horzres = GetDeviceCaps(hdcRef, HORZRES);
2938 vertres = GetDeviceCaps(hdcRef, VERTRES);
2940 if (!xExt || !yExt)
2942 /* Use the whole device surface */
2943 xExt = horzres;
2944 yExt = vertres;
2946 else
2948 xExt = MulDiv(xExt, horzres, 100 * horzsize);
2949 yExt = MulDiv(yExt, vertres, 100 * vertsize);
2952 /* set the initial viewport:window ratio as 1:1 */
2953 SetViewportExtEx(hdc, xExt, yExt, NULL);
2954 SetWindowExtEx(hdc, xExt, yExt, NULL);
2956 PlayMetaFile(hdc, hmf);
2958 ret = CloseEnhMetaFile(hdc);
2959 end:
2960 if (hdcdisp) DeleteDC(hdcdisp);
2961 DeleteMetaFile(hmf);
2962 return ret;