xref: /reactos/win32ss/gdi/gdi32/wine/enhmetafile.c (revision fc16259f)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * Enhanced metafile functions
3c2c66affSColin Finck  * Copyright 1998 Douglas Ridgway
4c2c66affSColin Finck  *           1999 Huw D M Davies
5c2c66affSColin Finck  *
6c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
7c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
8c2c66affSColin Finck  * License as published by the Free Software Foundation; either
9c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
10c2c66affSColin Finck  *
11c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
12c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14c2c66affSColin Finck  * Lesser General Public License for more details.
15c2c66affSColin Finck  *
16c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
17c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
18c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19c2c66affSColin Finck  *
20c2c66affSColin Finck  * NOTES:
21c2c66affSColin Finck  *
22c2c66affSColin Finck  * The enhanced format consists of the following elements:
23c2c66affSColin Finck  *
24c2c66affSColin Finck  *    A header
25c2c66affSColin Finck  *    A table of handles to GDI objects
26c2c66affSColin Finck  *    An array of metafile records
27c2c66affSColin Finck  *    A private palette
28c2c66affSColin Finck  *
29c2c66affSColin Finck  *
30c2c66affSColin Finck  *  The standard format consists of a header and an array of metafile records.
31c2c66affSColin Finck  *
32c2c66affSColin Finck  */
33c2c66affSColin Finck 
34c2c66affSColin Finck #include "config.h"
35c2c66affSColin Finck #include "wine/port.h"
36c2c66affSColin Finck 
37c2c66affSColin Finck #include <stdarg.h>
38c2c66affSColin Finck #include <stdlib.h>
39c2c66affSColin Finck #include <string.h>
40c2c66affSColin Finck #include <assert.h>
41c2c66affSColin Finck #include "windef.h"
42c2c66affSColin Finck #include "winbase.h"
43c2c66affSColin Finck #include "wingdi.h"
44c2c66affSColin Finck #include "winnls.h"
45c2c66affSColin Finck #include "winerror.h"
46c2c66affSColin Finck #include "gdi_private.h"
47c2c66affSColin Finck #include "wine/debug.h"
48c2c66affSColin Finck 
49c2c66affSColin Finck WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
50c2c66affSColin Finck 
51*fc16259fSJames Tabor 
52*fc16259fSJames Tabor static CRITICAL_SECTION enhmetafile_cs;
53*fc16259fSJames Tabor static CRITICAL_SECTION_DEBUG critsect_debug =
54*fc16259fSJames Tabor {
55*fc16259fSJames Tabor     0, 0, &enhmetafile_cs,
56*fc16259fSJames Tabor     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
57*fc16259fSJames Tabor       0, 0, { (DWORD_PTR)(__FILE__ ": enhmetafile_cs") }
58*fc16259fSJames Tabor };
59*fc16259fSJames Tabor static CRITICAL_SECTION enhmetafile_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
60*fc16259fSJames Tabor 
61c2c66affSColin Finck typedef struct
62c2c66affSColin Finck {
63c2c66affSColin Finck     ENHMETAHEADER  *emh;
64c2c66affSColin Finck     BOOL           on_disk;   /* true if metafile is on disk */
65c2c66affSColin Finck } ENHMETAFILEOBJ;
66c2c66affSColin Finck 
67c2c66affSColin Finck static const struct emr_name {
68c2c66affSColin Finck     DWORD type;
69c2c66affSColin Finck     const char *name;
70c2c66affSColin Finck } emr_names[] = {
71c2c66affSColin Finck #define X(p) {p, #p}
72c2c66affSColin Finck X(EMR_HEADER),
73c2c66affSColin Finck X(EMR_POLYBEZIER),
74c2c66affSColin Finck X(EMR_POLYGON),
75c2c66affSColin Finck X(EMR_POLYLINE),
76c2c66affSColin Finck X(EMR_POLYBEZIERTO),
77c2c66affSColin Finck X(EMR_POLYLINETO),
78c2c66affSColin Finck X(EMR_POLYPOLYLINE),
79c2c66affSColin Finck X(EMR_POLYPOLYGON),
80c2c66affSColin Finck X(EMR_SETWINDOWEXTEX),
81c2c66affSColin Finck X(EMR_SETWINDOWORGEX),
82c2c66affSColin Finck X(EMR_SETVIEWPORTEXTEX),
83c2c66affSColin Finck X(EMR_SETVIEWPORTORGEX),
84c2c66affSColin Finck X(EMR_SETBRUSHORGEX),
85c2c66affSColin Finck X(EMR_EOF),
86c2c66affSColin Finck X(EMR_SETPIXELV),
87c2c66affSColin Finck X(EMR_SETMAPPERFLAGS),
88c2c66affSColin Finck X(EMR_SETMAPMODE),
89c2c66affSColin Finck X(EMR_SETBKMODE),
90c2c66affSColin Finck X(EMR_SETPOLYFILLMODE),
91c2c66affSColin Finck X(EMR_SETROP2),
92c2c66affSColin Finck X(EMR_SETSTRETCHBLTMODE),
93c2c66affSColin Finck X(EMR_SETTEXTALIGN),
94c2c66affSColin Finck X(EMR_SETCOLORADJUSTMENT),
95c2c66affSColin Finck X(EMR_SETTEXTCOLOR),
96c2c66affSColin Finck X(EMR_SETBKCOLOR),
97c2c66affSColin Finck X(EMR_OFFSETCLIPRGN),
98c2c66affSColin Finck X(EMR_MOVETOEX),
99c2c66affSColin Finck X(EMR_SETMETARGN),
100c2c66affSColin Finck X(EMR_EXCLUDECLIPRECT),
101c2c66affSColin Finck X(EMR_INTERSECTCLIPRECT),
102c2c66affSColin Finck X(EMR_SCALEVIEWPORTEXTEX),
103c2c66affSColin Finck X(EMR_SCALEWINDOWEXTEX),
104c2c66affSColin Finck X(EMR_SAVEDC),
105c2c66affSColin Finck X(EMR_RESTOREDC),
106c2c66affSColin Finck X(EMR_SETWORLDTRANSFORM),
107c2c66affSColin Finck X(EMR_MODIFYWORLDTRANSFORM),
108c2c66affSColin Finck X(EMR_SELECTOBJECT),
109c2c66affSColin Finck X(EMR_CREATEPEN),
110c2c66affSColin Finck X(EMR_CREATEBRUSHINDIRECT),
111c2c66affSColin Finck X(EMR_DELETEOBJECT),
112c2c66affSColin Finck X(EMR_ANGLEARC),
113c2c66affSColin Finck X(EMR_ELLIPSE),
114c2c66affSColin Finck X(EMR_RECTANGLE),
115c2c66affSColin Finck X(EMR_ROUNDRECT),
116c2c66affSColin Finck X(EMR_ARC),
117c2c66affSColin Finck X(EMR_CHORD),
118c2c66affSColin Finck X(EMR_PIE),
119c2c66affSColin Finck X(EMR_SELECTPALETTE),
120c2c66affSColin Finck X(EMR_CREATEPALETTE),
121c2c66affSColin Finck X(EMR_SETPALETTEENTRIES),
122c2c66affSColin Finck X(EMR_RESIZEPALETTE),
123c2c66affSColin Finck X(EMR_REALIZEPALETTE),
124c2c66affSColin Finck X(EMR_EXTFLOODFILL),
125c2c66affSColin Finck X(EMR_LINETO),
126c2c66affSColin Finck X(EMR_ARCTO),
127c2c66affSColin Finck X(EMR_POLYDRAW),
128c2c66affSColin Finck X(EMR_SETARCDIRECTION),
129c2c66affSColin Finck X(EMR_SETMITERLIMIT),
130c2c66affSColin Finck X(EMR_BEGINPATH),
131c2c66affSColin Finck X(EMR_ENDPATH),
132c2c66affSColin Finck X(EMR_CLOSEFIGURE),
133c2c66affSColin Finck X(EMR_FILLPATH),
134c2c66affSColin Finck X(EMR_STROKEANDFILLPATH),
135c2c66affSColin Finck X(EMR_STROKEPATH),
136c2c66affSColin Finck X(EMR_FLATTENPATH),
137c2c66affSColin Finck X(EMR_WIDENPATH),
138c2c66affSColin Finck X(EMR_SELECTCLIPPATH),
139c2c66affSColin Finck X(EMR_ABORTPATH),
140c2c66affSColin Finck X(EMR_GDICOMMENT),
141c2c66affSColin Finck X(EMR_FILLRGN),
142c2c66affSColin Finck X(EMR_FRAMERGN),
143c2c66affSColin Finck X(EMR_INVERTRGN),
144c2c66affSColin Finck X(EMR_PAINTRGN),
145c2c66affSColin Finck X(EMR_EXTSELECTCLIPRGN),
146c2c66affSColin Finck X(EMR_BITBLT),
147c2c66affSColin Finck X(EMR_STRETCHBLT),
148c2c66affSColin Finck X(EMR_MASKBLT),
149c2c66affSColin Finck X(EMR_PLGBLT),
150c2c66affSColin Finck X(EMR_SETDIBITSTODEVICE),
151c2c66affSColin Finck X(EMR_STRETCHDIBITS),
152c2c66affSColin Finck X(EMR_EXTCREATEFONTINDIRECTW),
153c2c66affSColin Finck X(EMR_EXTTEXTOUTA),
154c2c66affSColin Finck X(EMR_EXTTEXTOUTW),
155c2c66affSColin Finck X(EMR_POLYBEZIER16),
156c2c66affSColin Finck X(EMR_POLYGON16),
157c2c66affSColin Finck X(EMR_POLYLINE16),
158c2c66affSColin Finck X(EMR_POLYBEZIERTO16),
159c2c66affSColin Finck X(EMR_POLYLINETO16),
160c2c66affSColin Finck X(EMR_POLYPOLYLINE16),
161c2c66affSColin Finck X(EMR_POLYPOLYGON16),
162c2c66affSColin Finck X(EMR_POLYDRAW16),
163c2c66affSColin Finck X(EMR_CREATEMONOBRUSH),
164c2c66affSColin Finck X(EMR_CREATEDIBPATTERNBRUSHPT),
165c2c66affSColin Finck X(EMR_EXTCREATEPEN),
166c2c66affSColin Finck X(EMR_POLYTEXTOUTA),
167c2c66affSColin Finck X(EMR_POLYTEXTOUTW),
168c2c66affSColin Finck X(EMR_SETICMMODE),
169c2c66affSColin Finck X(EMR_CREATECOLORSPACE),
170c2c66affSColin Finck X(EMR_SETCOLORSPACE),
171c2c66affSColin Finck X(EMR_DELETECOLORSPACE),
172c2c66affSColin Finck X(EMR_GLSRECORD),
173c2c66affSColin Finck X(EMR_GLSBOUNDEDRECORD),
174c2c66affSColin Finck X(EMR_PIXELFORMAT),
175c2c66affSColin Finck X(EMR_DRAWESCAPE),
176c2c66affSColin Finck X(EMR_EXTESCAPE),
177c2c66affSColin Finck X(EMR_STARTDOC),
178c2c66affSColin Finck X(EMR_SMALLTEXTOUT),
179c2c66affSColin Finck X(EMR_FORCEUFIMAPPING),
180c2c66affSColin Finck X(EMR_NAMEDESCAPE),
181c2c66affSColin Finck X(EMR_COLORCORRECTPALETTE),
182c2c66affSColin Finck X(EMR_SETICMPROFILEA),
183c2c66affSColin Finck X(EMR_SETICMPROFILEW),
184c2c66affSColin Finck X(EMR_ALPHABLEND),
185c2c66affSColin Finck X(EMR_SETLAYOUT),
186c2c66affSColin Finck X(EMR_TRANSPARENTBLT),
187c2c66affSColin Finck X(EMR_RESERVED_117),
188c2c66affSColin Finck X(EMR_GRADIENTFILL),
189c2c66affSColin Finck X(EMR_SETLINKEDUFI),
190c2c66affSColin Finck X(EMR_SETTEXTJUSTIFICATION),
191c2c66affSColin Finck X(EMR_COLORMATCHTOTARGETW),
192c2c66affSColin Finck X(EMR_CREATECOLORSPACEW)
193c2c66affSColin Finck #undef X
194c2c66affSColin Finck };
195c2c66affSColin Finck 
196c2c66affSColin Finck /****************************************************************************
197c2c66affSColin Finck  *         get_emr_name
198c2c66affSColin Finck  */
get_emr_name(DWORD type)199c2c66affSColin Finck static const char *get_emr_name(DWORD type)
200c2c66affSColin Finck {
201c2c66affSColin Finck     unsigned int i;
202c2c66affSColin Finck     for(i = 0; i < sizeof(emr_names) / sizeof(emr_names[0]); i++)
203c2c66affSColin Finck         if(type == emr_names[i].type) return emr_names[i].name;
204c2c66affSColin Finck     TRACE("Unknown record type %d\n", type);
205c2c66affSColin Finck    return NULL;
206c2c66affSColin Finck }
207c2c66affSColin Finck 
208c2c66affSColin Finck /***********************************************************************
209c2c66affSColin Finck  *          is_dib_monochrome
210c2c66affSColin Finck  *
211c2c66affSColin Finck  * Returns whether a DIB can be converted to a monochrome DDB.
212c2c66affSColin Finck  *
213c2c66affSColin Finck  * A DIB can be converted if its color table contains only black and
214c2c66affSColin Finck  * white. Black must be the first color in the color table.
215c2c66affSColin Finck  *
216c2c66affSColin Finck  * Note : If the first color in the color table is white followed by
217c2c66affSColin Finck  *        black, we can't convert it to a monochrome DDB with
218c2c66affSColin Finck  *        SetDIBits, because black and white would be inverted.
219c2c66affSColin Finck  */
is_dib_monochrome(const BITMAPINFO * info)220c2c66affSColin Finck static inline BOOL is_dib_monochrome( const BITMAPINFO* info )
221c2c66affSColin Finck {
222c2c66affSColin Finck     if (info->bmiHeader.biBitCount != 1) return FALSE;
223c2c66affSColin Finck 
224c2c66affSColin Finck     if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
225c2c66affSColin Finck     {
226c2c66affSColin Finck         const RGBTRIPLE *rgb = ((const BITMAPCOREINFO *) info)->bmciColors;
227c2c66affSColin Finck 
228c2c66affSColin Finck         /* Check if the first color is black */
229c2c66affSColin Finck         if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
230c2c66affSColin Finck         {
231c2c66affSColin Finck             rgb++;
232c2c66affSColin Finck             /* Check if the second color is white */
233c2c66affSColin Finck             return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
234c2c66affSColin Finck                  && (rgb->rgbtBlue == 0xff));
235c2c66affSColin Finck         }
236c2c66affSColin Finck         else return FALSE;
237c2c66affSColin Finck     }
238c2c66affSColin Finck     else  /* assume BITMAPINFOHEADER */
239c2c66affSColin Finck     {
240c2c66affSColin Finck         const RGBQUAD *rgb = info->bmiColors;
241c2c66affSColin Finck 
242c2c66affSColin Finck         /* Check if the first color is black */
243c2c66affSColin Finck         if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
244c2c66affSColin Finck             (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
245c2c66affSColin Finck         {
246c2c66affSColin Finck             rgb++;
247c2c66affSColin Finck 
248c2c66affSColin Finck             /* Check if the second color is white */
249c2c66affSColin Finck             return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
250c2c66affSColin Finck                  && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
251c2c66affSColin Finck         }
252c2c66affSColin Finck         else return FALSE;
253c2c66affSColin Finck     }
254c2c66affSColin Finck }
255c2c66affSColin Finck 
256c2c66affSColin Finck /****************************************************************************
257c2c66affSColin Finck  *          EMF_Create_HENHMETAFILE
258c2c66affSColin Finck  */
EMF_Create_HENHMETAFILE(ENHMETAHEADER * emh,DWORD filesize,BOOL on_disk)259*fc16259fSJames Tabor HENHMETAFILE EMF_Create_HENHMETAFILE(ENHMETAHEADER *emh, DWORD filesize, BOOL on_disk )
260c2c66affSColin Finck {
261c2c66affSColin Finck     HENHMETAFILE hmf;
262c2c66affSColin Finck     ENHMETAFILEOBJ *metaObj;
263c2c66affSColin Finck 
264c2c66affSColin Finck     if (emh->iType != EMR_HEADER)
265c2c66affSColin Finck     {
266c2c66affSColin Finck         SetLastError(ERROR_INVALID_DATA);
267c2c66affSColin Finck         return 0;
268c2c66affSColin Finck     }
269c2c66affSColin Finck     if (emh->dSignature != ENHMETA_SIGNATURE ||
270c2c66affSColin Finck         (emh->nBytes & 3)) /* refuse to load unaligned EMF as Windows does */
271c2c66affSColin Finck     {
272c2c66affSColin Finck         WARN("Invalid emf header type 0x%08x sig 0x%08x.\n",
273c2c66affSColin Finck              emh->iType, emh->dSignature);
274c2c66affSColin Finck         return 0;
275c2c66affSColin Finck     }
276*fc16259fSJames Tabor     if (filesize < emh->nBytes)
277*fc16259fSJames Tabor     {
278*fc16259fSJames Tabor         WARN("File truncated (got %u bytes, header says %u)\n", emh->nBytes, filesize);
279*fc16259fSJames Tabor         return 0;
280*fc16259fSJames Tabor     }
281c2c66affSColin Finck 
282c2c66affSColin Finck     if (!(metaObj = HeapAlloc( GetProcessHeap(), 0, sizeof(*metaObj) ))) return 0;
283c2c66affSColin Finck 
284c2c66affSColin Finck     metaObj->emh = emh;
285c2c66affSColin Finck     metaObj->on_disk = on_disk;
286c2c66affSColin Finck 
287c2c66affSColin Finck     if (!(hmf = alloc_gdi_handle( metaObj, OBJ_ENHMETAFILE, NULL )))
288c2c66affSColin Finck         HeapFree( GetProcessHeap(), 0, metaObj );
289c2c66affSColin Finck     return hmf;
290c2c66affSColin Finck }
291c2c66affSColin Finck 
292c2c66affSColin Finck /****************************************************************************
293c2c66affSColin Finck  *          EMF_Delete_HENHMETAFILE
294c2c66affSColin Finck  */
EMF_Delete_HENHMETAFILE(HENHMETAFILE hmf)295c2c66affSColin Finck static BOOL EMF_Delete_HENHMETAFILE( HENHMETAFILE hmf )
296c2c66affSColin Finck {
297*fc16259fSJames Tabor     ENHMETAFILEOBJ *metaObj;
298*fc16259fSJames Tabor     BOOL Ret = FALSE;
299c2c66affSColin Finck 
300*fc16259fSJames Tabor     EnterCriticalSection( &enhmetafile_cs );
301*fc16259fSJames Tabor     metaObj = free_gdi_handle( hmf );
302*fc16259fSJames Tabor     if(metaObj)
303*fc16259fSJames Tabor     {
304c2c66affSColin Finck         if(metaObj->on_disk)
305c2c66affSColin Finck             UnmapViewOfFile( metaObj->emh );
306c2c66affSColin Finck         else
307c2c66affSColin Finck             HeapFree( GetProcessHeap(), 0, metaObj->emh );
308*fc16259fSJames Tabor         HeapFree( GetProcessHeap(), 0, metaObj );
309*fc16259fSJames Tabor         Ret = TRUE;
310*fc16259fSJames Tabor     }
311*fc16259fSJames Tabor     LeaveCriticalSection( &enhmetafile_cs );
312*fc16259fSJames Tabor     return Ret;
313c2c66affSColin Finck }
314c2c66affSColin Finck 
315c2c66affSColin Finck /******************************************************************
316c2c66affSColin Finck  *         EMF_GetEnhMetaHeader
317c2c66affSColin Finck  *
318c2c66affSColin Finck  * Returns ptr to ENHMETAHEADER associated with HENHMETAFILE
319c2c66affSColin Finck  */
EMF_GetEnhMetaHeader(HENHMETAFILE hmf)320c2c66affSColin Finck static ENHMETAHEADER *EMF_GetEnhMetaHeader( HENHMETAFILE hmf )
321c2c66affSColin Finck {
322c2c66affSColin Finck     ENHMETAHEADER *ret = NULL;
323*fc16259fSJames Tabor     ENHMETAFILEOBJ *metaObj;
324*fc16259fSJames Tabor 
325*fc16259fSJames Tabor     EnterCriticalSection( &enhmetafile_cs );
326*fc16259fSJames Tabor     metaObj = GDI_GetObjPtr( hmf, OBJ_ENHMETAFILE );
327c2c66affSColin Finck     TRACE("hmf %p -> enhmetaObj %p\n", hmf, metaObj);
328c2c66affSColin Finck     if (metaObj)
329c2c66affSColin Finck     {
330c2c66affSColin Finck         ret = metaObj->emh;
331c2c66affSColin Finck         GDI_ReleaseObj( hmf );
332c2c66affSColin Finck     }
333*fc16259fSJames Tabor     LeaveCriticalSection( &enhmetafile_cs );
334c2c66affSColin Finck     return ret;
335c2c66affSColin Finck }
336c2c66affSColin Finck 
337c2c66affSColin Finck /*****************************************************************************
338c2c66affSColin Finck  *         EMF_GetEnhMetaFile
339c2c66affSColin Finck  *
340c2c66affSColin Finck  */
EMF_GetEnhMetaFile(HANDLE hFile)341c2c66affSColin Finck static HENHMETAFILE EMF_GetEnhMetaFile( HANDLE hFile )
342c2c66affSColin Finck {
343c2c66affSColin Finck     ENHMETAHEADER *emh;
344c2c66affSColin Finck     HANDLE hMapping;
345c2c66affSColin Finck     HENHMETAFILE hemf;
346*fc16259fSJames Tabor     DWORD filesize;
347*fc16259fSJames Tabor 
348*fc16259fSJames Tabor     filesize = GetFileSize( hFile, NULL );
349c2c66affSColin Finck 
350c2c66affSColin Finck     hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
351c2c66affSColin Finck     emh = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
352c2c66affSColin Finck     CloseHandle( hMapping );
353c2c66affSColin Finck 
354c2c66affSColin Finck     if (!emh) return 0;
355c2c66affSColin Finck 
356*fc16259fSJames Tabor     hemf = EMF_Create_HENHMETAFILE( emh, filesize, TRUE );
357c2c66affSColin Finck     if (!hemf)
358c2c66affSColin Finck         UnmapViewOfFile( emh );
359c2c66affSColin Finck     return hemf;
360c2c66affSColin Finck }
361c2c66affSColin Finck 
362c2c66affSColin Finck 
363c2c66affSColin Finck /*****************************************************************************
364c2c66affSColin Finck  *          GetEnhMetaFileA (GDI32.@)
365c2c66affSColin Finck  *
366c2c66affSColin Finck  *
367c2c66affSColin Finck  */
GetEnhMetaFileA(LPCSTR lpszMetaFile)368c2c66affSColin Finck HENHMETAFILE WINAPI GetEnhMetaFileA(
369c2c66affSColin Finck 	     LPCSTR lpszMetaFile  /* [in] filename of enhanced metafile */
370c2c66affSColin Finck     )
371c2c66affSColin Finck {
372c2c66affSColin Finck     HENHMETAFILE hmf;
373c2c66affSColin Finck     HANDLE hFile;
374c2c66affSColin Finck 
375c2c66affSColin Finck     hFile = CreateFileA(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
376c2c66affSColin Finck 			OPEN_EXISTING, 0, 0);
377c2c66affSColin Finck     if (hFile == INVALID_HANDLE_VALUE) {
378c2c66affSColin Finck         WARN("could not open %s\n", lpszMetaFile);
379c2c66affSColin Finck 	return 0;
380c2c66affSColin Finck     }
381c2c66affSColin Finck     hmf = EMF_GetEnhMetaFile( hFile );
382c2c66affSColin Finck     CloseHandle( hFile );
383c2c66affSColin Finck     return hmf;
384c2c66affSColin Finck }
385c2c66affSColin Finck 
386c2c66affSColin Finck /*****************************************************************************
387c2c66affSColin Finck  *          GetEnhMetaFileW  (GDI32.@)
388c2c66affSColin Finck  */
GetEnhMetaFileW(LPCWSTR lpszMetaFile)389c2c66affSColin Finck HENHMETAFILE WINAPI GetEnhMetaFileW(
390c2c66affSColin Finck              LPCWSTR lpszMetaFile)  /* [in] filename of enhanced metafile */
391c2c66affSColin Finck {
392c2c66affSColin Finck     HENHMETAFILE hmf;
393c2c66affSColin Finck     HANDLE hFile;
394c2c66affSColin Finck 
395c2c66affSColin Finck     hFile = CreateFileW(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
396c2c66affSColin Finck 			OPEN_EXISTING, 0, 0);
397c2c66affSColin Finck     if (hFile == INVALID_HANDLE_VALUE) {
398c2c66affSColin Finck         WARN("could not open %s\n", debugstr_w(lpszMetaFile));
399c2c66affSColin Finck 	return 0;
400c2c66affSColin Finck     }
401c2c66affSColin Finck     hmf = EMF_GetEnhMetaFile( hFile );
402c2c66affSColin Finck     CloseHandle( hFile );
403c2c66affSColin Finck     return hmf;
404c2c66affSColin Finck }
405c2c66affSColin Finck 
406c2c66affSColin Finck /*****************************************************************************
407c2c66affSColin Finck  *        GetEnhMetaFileHeader  (GDI32.@)
408c2c66affSColin Finck  *
409c2c66affSColin Finck  * Retrieves the record containing the header for the specified
410c2c66affSColin Finck  * enhanced-format metafile.
411c2c66affSColin Finck  *
412c2c66affSColin Finck  * RETURNS
413c2c66affSColin Finck  *  If buf is NULL, returns the size of buffer required.
414c2c66affSColin Finck  *  Otherwise, copy up to bufsize bytes of enhanced metafile header into
415c2c66affSColin Finck  *  buf.
416c2c66affSColin Finck  */
GetEnhMetaFileHeader(HENHMETAFILE hmf,UINT bufsize,LPENHMETAHEADER buf)417c2c66affSColin Finck UINT WINAPI GetEnhMetaFileHeader(
418c2c66affSColin Finck        HENHMETAFILE hmf,   /* [in] enhanced metafile */
419c2c66affSColin Finck        UINT bufsize,       /* [in] size of buffer */
420c2c66affSColin Finck        LPENHMETAHEADER buf /* [out] buffer */
421c2c66affSColin Finck     )
422c2c66affSColin Finck {
423c2c66affSColin Finck     LPENHMETAHEADER emh;
424c2c66affSColin Finck     UINT size;
425c2c66affSColin Finck 
426c2c66affSColin Finck     emh = EMF_GetEnhMetaHeader(hmf);
427c2c66affSColin Finck     if(!emh) return FALSE;
428c2c66affSColin Finck     size = emh->nSize;
429c2c66affSColin Finck     if (!buf) return size;
430c2c66affSColin Finck     size = min(size, bufsize);
431c2c66affSColin Finck     memmove(buf, emh, size);
432c2c66affSColin Finck     return size;
433c2c66affSColin Finck }
434c2c66affSColin Finck 
435c2c66affSColin Finck 
436c2c66affSColin Finck /*****************************************************************************
437c2c66affSColin Finck  *          GetEnhMetaFileDescriptionA  (GDI32.@)
438c2c66affSColin Finck  *
439c2c66affSColin Finck  * See GetEnhMetaFileDescriptionW.
440c2c66affSColin Finck  */
GetEnhMetaFileDescriptionA(HENHMETAFILE hmf,UINT size,LPSTR buf)441c2c66affSColin Finck UINT WINAPI GetEnhMetaFileDescriptionA(
442c2c66affSColin Finck        HENHMETAFILE hmf, /* [in] enhanced metafile */
443c2c66affSColin Finck        UINT size,        /* [in] size of buf */
444c2c66affSColin Finck        LPSTR buf         /* [out] buffer to receive description */
445c2c66affSColin Finck     )
446c2c66affSColin Finck {
447c2c66affSColin Finck      LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
448c2c66affSColin Finck      DWORD len;
449c2c66affSColin Finck      WCHAR *descrW;
450c2c66affSColin Finck 
451c2c66affSColin Finck      if(!emh) return FALSE;
452c2c66affSColin Finck      if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
453c2c66affSColin Finck      descrW = (WCHAR *) ((char *) emh + emh->offDescription);
454c2c66affSColin Finck      len = WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, NULL, 0, NULL, NULL );
455c2c66affSColin Finck 
456c2c66affSColin Finck      if (!buf || !size ) return len;
457c2c66affSColin Finck 
458c2c66affSColin Finck      len = min( size, len );
459c2c66affSColin Finck      WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, buf, len, NULL, NULL );
460c2c66affSColin Finck      return len;
461c2c66affSColin Finck }
462c2c66affSColin Finck 
463c2c66affSColin Finck /*****************************************************************************
464c2c66affSColin Finck  *          GetEnhMetaFileDescriptionW  (GDI32.@)
465c2c66affSColin Finck  *
466c2c66affSColin Finck  *  Copies the description string of an enhanced metafile into a buffer
467c2c66affSColin Finck  *  _buf_.
468c2c66affSColin Finck  *
469c2c66affSColin Finck  * RETURNS
470c2c66affSColin Finck  *  If _buf_ is NULL, returns size of _buf_ required. Otherwise, returns
471c2c66affSColin Finck  *  number of characters copied.
472c2c66affSColin Finck  */
GetEnhMetaFileDescriptionW(HENHMETAFILE hmf,UINT size,LPWSTR buf)473c2c66affSColin Finck UINT WINAPI GetEnhMetaFileDescriptionW(
474c2c66affSColin Finck        HENHMETAFILE hmf, /* [in] enhanced metafile */
475c2c66affSColin Finck        UINT size,        /* [in] size of buf */
476c2c66affSColin Finck        LPWSTR buf        /* [out] buffer to receive description */
477c2c66affSColin Finck     )
478c2c66affSColin Finck {
479c2c66affSColin Finck      LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
480c2c66affSColin Finck 
481c2c66affSColin Finck      if(!emh) return FALSE;
482c2c66affSColin Finck      if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
483c2c66affSColin Finck      if (!buf || !size ) return emh->nDescription;
484c2c66affSColin Finck 
485c2c66affSColin Finck      memmove(buf, (char *) emh + emh->offDescription, min(size,emh->nDescription)*sizeof(WCHAR));
486c2c66affSColin Finck      return min(size, emh->nDescription);
487c2c66affSColin Finck }
488c2c66affSColin Finck 
489c2c66affSColin Finck /****************************************************************************
490c2c66affSColin Finck  *    SetEnhMetaFileBits (GDI32.@)
491c2c66affSColin Finck  *
492c2c66affSColin Finck  *  Creates an enhanced metafile by copying _bufsize_ bytes from _buf_.
493c2c66affSColin Finck  */
SetEnhMetaFileBits(UINT bufsize,const BYTE * buf)494c2c66affSColin Finck HENHMETAFILE WINAPI SetEnhMetaFileBits(UINT bufsize, const BYTE *buf)
495c2c66affSColin Finck {
496c2c66affSColin Finck     ENHMETAHEADER *emh = HeapAlloc( GetProcessHeap(), 0, bufsize );
497c2c66affSColin Finck     HENHMETAFILE hmf;
498c2c66affSColin Finck     memmove(emh, buf, bufsize);
499*fc16259fSJames Tabor     hmf = EMF_Create_HENHMETAFILE( emh, bufsize, FALSE );
500c2c66affSColin Finck     if (!hmf)
501c2c66affSColin Finck         HeapFree( GetProcessHeap(), 0, emh );
502c2c66affSColin Finck     return hmf;
503c2c66affSColin Finck }
504c2c66affSColin Finck 
505c2c66affSColin Finck /*****************************************************************************
506c2c66affSColin Finck  *  GetEnhMetaFileBits (GDI32.@)
507c2c66affSColin Finck  *
508c2c66affSColin Finck  */
GetEnhMetaFileBits(HENHMETAFILE hmf,UINT bufsize,LPBYTE buf)509c2c66affSColin Finck UINT WINAPI GetEnhMetaFileBits(
510c2c66affSColin Finck     HENHMETAFILE hmf,
511c2c66affSColin Finck     UINT bufsize,
512c2c66affSColin Finck     LPBYTE buf
513c2c66affSColin Finck )
514c2c66affSColin Finck {
515c2c66affSColin Finck     LPENHMETAHEADER emh = EMF_GetEnhMetaHeader( hmf );
516c2c66affSColin Finck     UINT size;
517c2c66affSColin Finck 
518c2c66affSColin Finck     if(!emh) return 0;
519c2c66affSColin Finck 
520c2c66affSColin Finck     size = emh->nBytes;
521c2c66affSColin Finck     if( buf == NULL ) return size;
522c2c66affSColin Finck 
523c2c66affSColin Finck     size = min( size, bufsize );
524c2c66affSColin Finck     memmove(buf, emh, size);
525c2c66affSColin Finck     return size;
526c2c66affSColin Finck }
527c2c66affSColin Finck 
528c2c66affSColin Finck typedef struct EMF_dc_state
529c2c66affSColin Finck {
530c2c66affSColin Finck     INT   mode;
531c2c66affSColin Finck     XFORM world_transform;
532c2c66affSColin Finck     INT   wndOrgX;
533c2c66affSColin Finck     INT   wndOrgY;
534c2c66affSColin Finck     INT   wndExtX;
535c2c66affSColin Finck     INT   wndExtY;
536c2c66affSColin Finck     INT   vportOrgX;
537c2c66affSColin Finck     INT   vportOrgY;
538c2c66affSColin Finck     INT   vportExtX;
539c2c66affSColin Finck     INT   vportExtY;
540c2c66affSColin Finck     struct EMF_dc_state *next;
541c2c66affSColin Finck } EMF_dc_state;
542c2c66affSColin Finck 
543c2c66affSColin Finck typedef struct enum_emh_data
544c2c66affSColin Finck {
545c2c66affSColin Finck     XFORM init_transform;
546c2c66affSColin Finck     EMF_dc_state state;
547c2c66affSColin Finck     INT save_level;
548c2c66affSColin Finck     EMF_dc_state *saved_state;
549c2c66affSColin Finck } enum_emh_data;
550c2c66affSColin Finck 
551c2c66affSColin Finck #define ENUM_GET_PRIVATE_DATA(ht) \
552c2c66affSColin Finck     ((enum_emh_data*)(((unsigned char*)(ht))-sizeof (enum_emh_data)))
553c2c66affSColin Finck 
554c2c66affSColin Finck #define WIDTH(rect) ( (rect).right - (rect).left )
555c2c66affSColin Finck #define HEIGHT(rect) ( (rect).bottom - (rect).top )
556c2c66affSColin Finck 
557c2c66affSColin Finck #define IS_WIN9X() (GetVersion()&0x80000000)
558c2c66affSColin Finck 
EMF_Update_MF_Xform(HDC hdc,const enum_emh_data * info)559c2c66affSColin Finck static void EMF_Update_MF_Xform(HDC hdc, const enum_emh_data *info)
560c2c66affSColin Finck {
561c2c66affSColin Finck     XFORM mapping_mode_trans, final_trans;
562c2c66affSColin Finck     double scaleX, scaleY;
563c2c66affSColin Finck 
564c2c66affSColin Finck     scaleX = (double)info->state.vportExtX / (double)info->state.wndExtX;
565c2c66affSColin Finck     scaleY = (double)info->state.vportExtY / (double)info->state.wndExtY;
566c2c66affSColin Finck     mapping_mode_trans.eM11 = scaleX;
567c2c66affSColin Finck     mapping_mode_trans.eM12 = 0.0;
568c2c66affSColin Finck     mapping_mode_trans.eM21 = 0.0;
569c2c66affSColin Finck     mapping_mode_trans.eM22 = scaleY;
570c2c66affSColin Finck     mapping_mode_trans.eDx  = (double)info->state.vportOrgX - scaleX * (double)info->state.wndOrgX;
571c2c66affSColin Finck     mapping_mode_trans.eDy  = (double)info->state.vportOrgY - scaleY * (double)info->state.wndOrgY;
572c2c66affSColin Finck 
573c2c66affSColin Finck     CombineTransform(&final_trans, &info->state.world_transform, &mapping_mode_trans);
574c2c66affSColin Finck     CombineTransform(&final_trans, &final_trans, &info->init_transform);
575c2c66affSColin Finck 
576c2c66affSColin Finck     if (!SetWorldTransform(hdc, &final_trans))
577c2c66affSColin Finck     {
578c2c66affSColin Finck         ERR("World transform failed!\n");
579c2c66affSColin Finck     }
580c2c66affSColin Finck }
581c2c66affSColin Finck 
EMF_RestoreDC(enum_emh_data * info,INT level)582c2c66affSColin Finck static void EMF_RestoreDC( enum_emh_data *info, INT level )
583c2c66affSColin Finck {
584c2c66affSColin Finck     if (abs(level) > info->save_level || level == 0) return;
585c2c66affSColin Finck 
586c2c66affSColin Finck     if (level < 0) level = info->save_level + level + 1;
587c2c66affSColin Finck 
588c2c66affSColin Finck     while (info->save_level >= level)
589c2c66affSColin Finck     {
590c2c66affSColin Finck         EMF_dc_state *state = info->saved_state;
591c2c66affSColin Finck         info->saved_state = state->next;
592c2c66affSColin Finck         state->next = NULL;
593c2c66affSColin Finck         if (--info->save_level < level)
594c2c66affSColin Finck             info->state = *state;
595c2c66affSColin Finck         HeapFree( GetProcessHeap(), 0, state );
596c2c66affSColin Finck     }
597c2c66affSColin Finck }
598c2c66affSColin Finck 
EMF_SaveDC(enum_emh_data * info)599c2c66affSColin Finck static void EMF_SaveDC( enum_emh_data *info )
600c2c66affSColin Finck {
601c2c66affSColin Finck     EMF_dc_state *state = HeapAlloc( GetProcessHeap(), 0, sizeof(*state));
602c2c66affSColin Finck     if (state)
603c2c66affSColin Finck     {
604c2c66affSColin Finck         *state = info->state;
605c2c66affSColin Finck         state->next = info->saved_state;
606c2c66affSColin Finck         info->saved_state = state;
607c2c66affSColin Finck         info->save_level++;
608c2c66affSColin Finck         TRACE("save_level %d\n", info->save_level);
609c2c66affSColin Finck     }
610c2c66affSColin Finck }
611c2c66affSColin Finck 
EMF_SetMapMode(HDC hdc,enum_emh_data * info)612c2c66affSColin Finck static void EMF_SetMapMode(HDC hdc, enum_emh_data *info)
613c2c66affSColin Finck {
614c2c66affSColin Finck     INT horzSize = GetDeviceCaps( hdc, HORZSIZE );
615c2c66affSColin Finck     INT vertSize = GetDeviceCaps( hdc, VERTSIZE );
616c2c66affSColin Finck     INT horzRes  = GetDeviceCaps( hdc, HORZRES );
617c2c66affSColin Finck     INT vertRes  = GetDeviceCaps( hdc, VERTRES );
618c2c66affSColin Finck 
619c2c66affSColin Finck     TRACE("%d\n", info->state.mode);
620c2c66affSColin Finck 
621c2c66affSColin Finck     switch(info->state.mode)
622c2c66affSColin Finck     {
623c2c66affSColin Finck     case MM_TEXT:
624c2c66affSColin Finck         info->state.wndExtX   = 1;
625c2c66affSColin Finck         info->state.wndExtY   = 1;
626c2c66affSColin Finck         info->state.vportExtX = 1;
627c2c66affSColin Finck         info->state.vportExtY = 1;
628c2c66affSColin Finck         break;
629c2c66affSColin Finck     case MM_LOMETRIC:
630c2c66affSColin Finck     case MM_ISOTROPIC:
631c2c66affSColin Finck         info->state.wndExtX   = horzSize * 10;
632c2c66affSColin Finck         info->state.wndExtY   = vertSize * 10;
633c2c66affSColin Finck         info->state.vportExtX = horzRes;
634c2c66affSColin Finck         info->state.vportExtY = -vertRes;
635c2c66affSColin Finck         break;
636c2c66affSColin Finck     case MM_HIMETRIC:
637c2c66affSColin Finck         info->state.wndExtX   = horzSize * 100;
638c2c66affSColin Finck         info->state.wndExtY   = vertSize * 100;
639c2c66affSColin Finck         info->state.vportExtX = horzRes;
640c2c66affSColin Finck         info->state.vportExtY = -vertRes;
641c2c66affSColin Finck         break;
642c2c66affSColin Finck     case MM_LOENGLISH:
643c2c66affSColin Finck         info->state.wndExtX   = MulDiv(1000, horzSize, 254);
644c2c66affSColin Finck         info->state.wndExtY   = MulDiv(1000, vertSize, 254);
645c2c66affSColin Finck         info->state.vportExtX = horzRes;
646c2c66affSColin Finck         info->state.vportExtY = -vertRes;
647c2c66affSColin Finck         break;
648c2c66affSColin Finck     case MM_HIENGLISH:
649c2c66affSColin Finck         info->state.wndExtX   = MulDiv(10000, horzSize, 254);
650c2c66affSColin Finck         info->state.wndExtY   = MulDiv(10000, vertSize, 254);
651c2c66affSColin Finck         info->state.vportExtX = horzRes;
652c2c66affSColin Finck         info->state.vportExtY = -vertRes;
653c2c66affSColin Finck         break;
654c2c66affSColin Finck     case MM_TWIPS:
655c2c66affSColin Finck         info->state.wndExtX   = MulDiv(14400, horzSize, 254);
656c2c66affSColin Finck         info->state.wndExtY   = MulDiv(14400, vertSize, 254);
657c2c66affSColin Finck         info->state.vportExtX = horzRes;
658c2c66affSColin Finck         info->state.vportExtY = -vertRes;
659c2c66affSColin Finck         break;
660c2c66affSColin Finck     case MM_ANISOTROPIC:
661c2c66affSColin Finck         break;
662c2c66affSColin Finck     default:
663c2c66affSColin Finck         return;
664c2c66affSColin Finck     }
665c2c66affSColin Finck }
666c2c66affSColin Finck 
667c2c66affSColin Finck /***********************************************************************
668c2c66affSColin Finck  *           EMF_FixIsotropic
669c2c66affSColin Finck  *
670c2c66affSColin Finck  * Fix viewport extensions for isotropic mode.
671c2c66affSColin Finck  */
672c2c66affSColin Finck 
EMF_FixIsotropic(HDC hdc,enum_emh_data * info)673c2c66affSColin Finck static void EMF_FixIsotropic(HDC hdc, enum_emh_data *info)
674c2c66affSColin Finck {
675c2c66affSColin Finck     double xdim = fabs((double)info->state.vportExtX * GetDeviceCaps( hdc, HORZSIZE ) /
676c2c66affSColin Finck                   (GetDeviceCaps( hdc, HORZRES ) * info->state.wndExtX));
677c2c66affSColin Finck     double ydim = fabs((double)info->state.vportExtY * GetDeviceCaps( hdc, VERTSIZE ) /
678c2c66affSColin Finck                   (GetDeviceCaps( hdc, VERTRES ) * info->state.wndExtY));
679c2c66affSColin Finck 
680c2c66affSColin Finck     if (xdim > ydim)
681c2c66affSColin Finck     {
682c2c66affSColin Finck         INT mincx = (info->state.vportExtX >= 0) ? 1 : -1;
683c2c66affSColin Finck         info->state.vportExtX = floor(info->state.vportExtX * ydim / xdim + 0.5);
684c2c66affSColin Finck         if (!info->state.vportExtX) info->state.vportExtX = mincx;
685c2c66affSColin Finck     }
686c2c66affSColin Finck     else
687c2c66affSColin Finck     {
688c2c66affSColin Finck         INT mincy = (info->state.vportExtY >= 0) ? 1 : -1;
689c2c66affSColin Finck         info->state.vportExtY = floor(info->state.vportExtY * xdim / ydim + 0.5);
690c2c66affSColin Finck         if (!info->state.vportExtY) info->state.vportExtY = mincy;
691c2c66affSColin Finck     }
692c2c66affSColin Finck }
693c2c66affSColin Finck 
694c2c66affSColin Finck /*****************************************************************************
695c2c66affSColin Finck  *       emr_produces_output
696c2c66affSColin Finck  *
697c2c66affSColin Finck  * Returns TRUE if the record type writes something to the dc.  Used by
698c2c66affSColin Finck  * PlayEnhMetaFileRecord to determine whether it needs to update the
699c2c66affSColin Finck  * dc's xform when in win9x mode.
700c2c66affSColin Finck  *
701c2c66affSColin Finck  * FIXME: need to test which records should be here.
702c2c66affSColin Finck  */
emr_produces_output(int type)703c2c66affSColin Finck static BOOL emr_produces_output(int type)
704c2c66affSColin Finck {
705c2c66affSColin Finck     switch(type) {
706c2c66affSColin Finck     case EMR_POLYBEZIER:
707c2c66affSColin Finck     case EMR_POLYGON:
708c2c66affSColin Finck     case EMR_POLYLINE:
709c2c66affSColin Finck     case EMR_POLYBEZIERTO:
710c2c66affSColin Finck     case EMR_POLYLINETO:
711c2c66affSColin Finck     case EMR_POLYPOLYLINE:
712c2c66affSColin Finck     case EMR_POLYPOLYGON:
713c2c66affSColin Finck     case EMR_SETPIXELV:
714c2c66affSColin Finck     case EMR_MOVETOEX:
715c2c66affSColin Finck     case EMR_EXCLUDECLIPRECT:
716c2c66affSColin Finck     case EMR_INTERSECTCLIPRECT:
717c2c66affSColin Finck     case EMR_SELECTOBJECT:
718c2c66affSColin Finck     case EMR_ANGLEARC:
719c2c66affSColin Finck     case EMR_ELLIPSE:
720c2c66affSColin Finck     case EMR_RECTANGLE:
721c2c66affSColin Finck     case EMR_ROUNDRECT:
722c2c66affSColin Finck     case EMR_ARC:
723c2c66affSColin Finck     case EMR_CHORD:
724c2c66affSColin Finck     case EMR_PIE:
725c2c66affSColin Finck     case EMR_EXTFLOODFILL:
726c2c66affSColin Finck     case EMR_LINETO:
727c2c66affSColin Finck     case EMR_ARCTO:
728c2c66affSColin Finck     case EMR_POLYDRAW:
729c2c66affSColin Finck     case EMR_GDICOMMENT:
730c2c66affSColin Finck     case EMR_FILLRGN:
731c2c66affSColin Finck     case EMR_FRAMERGN:
732c2c66affSColin Finck     case EMR_INVERTRGN:
733c2c66affSColin Finck     case EMR_PAINTRGN:
734c2c66affSColin Finck     case EMR_BITBLT:
735c2c66affSColin Finck     case EMR_STRETCHBLT:
736c2c66affSColin Finck     case EMR_MASKBLT:
737c2c66affSColin Finck     case EMR_PLGBLT:
738c2c66affSColin Finck     case EMR_SETDIBITSTODEVICE:
739c2c66affSColin Finck     case EMR_STRETCHDIBITS:
740c2c66affSColin Finck     case EMR_EXTTEXTOUTA:
741c2c66affSColin Finck     case EMR_EXTTEXTOUTW:
742c2c66affSColin Finck     case EMR_POLYBEZIER16:
743c2c66affSColin Finck     case EMR_POLYGON16:
744c2c66affSColin Finck     case EMR_POLYLINE16:
745c2c66affSColin Finck     case EMR_POLYBEZIERTO16:
746c2c66affSColin Finck     case EMR_POLYLINETO16:
747c2c66affSColin Finck     case EMR_POLYPOLYLINE16:
748c2c66affSColin Finck     case EMR_POLYPOLYGON16:
749c2c66affSColin Finck     case EMR_POLYDRAW16:
750c2c66affSColin Finck     case EMR_POLYTEXTOUTA:
751c2c66affSColin Finck     case EMR_POLYTEXTOUTW:
752c2c66affSColin Finck     case EMR_SMALLTEXTOUT:
753c2c66affSColin Finck     case EMR_ALPHABLEND:
754c2c66affSColin Finck     case EMR_TRANSPARENTBLT:
755c2c66affSColin Finck         return TRUE;
756c2c66affSColin Finck     default:
757c2c66affSColin Finck         return FALSE;
758c2c66affSColin Finck     }
759c2c66affSColin Finck }
760c2c66affSColin Finck 
761c2c66affSColin Finck 
762c2c66affSColin Finck /*****************************************************************************
763c2c66affSColin Finck  *           PlayEnhMetaFileRecord  (GDI32.@)
764c2c66affSColin Finck  *
765c2c66affSColin Finck  *  Render a single enhanced metafile record in the device context hdc.
766c2c66affSColin Finck  *
767c2c66affSColin Finck  *  RETURNS
768c2c66affSColin Finck  *    TRUE (non zero) on success, FALSE on error.
769c2c66affSColin Finck  *  BUGS
770c2c66affSColin Finck  *    Many unimplemented records.
771c2c66affSColin Finck  *    No error handling on record play failures (ie checking return codes)
772c2c66affSColin Finck  *
773c2c66affSColin Finck  * NOTES
774c2c66affSColin Finck  *    WinNT actually updates the current world transform in this function
775c2c66affSColin Finck  *     whereas Win9x does not.
776c2c66affSColin Finck  */
PlayEnhMetaFileRecord(HDC hdc,LPHANDLETABLE handletable,const ENHMETARECORD * mr,UINT handles)777c2c66affSColin Finck BOOL WINAPI PlayEnhMetaFileRecord(
778c2c66affSColin Finck      HDC hdc,                   /* [in] device context in which to render EMF record */
779c2c66affSColin Finck      LPHANDLETABLE handletable, /* [in] array of handles to be used in rendering record */
780c2c66affSColin Finck      const ENHMETARECORD *mr,   /* [in] EMF record to render */
781c2c66affSColin Finck      UINT handles               /* [in] size of handle array */
782c2c66affSColin Finck      )
783c2c66affSColin Finck {
784c2c66affSColin Finck   int type;
785c2c66affSColin Finck   RECT tmprc;
786c2c66affSColin Finck   enum_emh_data *info = ENUM_GET_PRIVATE_DATA(handletable);
787c2c66affSColin Finck 
788c2c66affSColin Finck   TRACE("hdc = %p, handletable = %p, record = %p, numHandles = %d\n",
789c2c66affSColin Finck         hdc, handletable, mr, handles);
790c2c66affSColin Finck   if (!mr) return FALSE;
791c2c66affSColin Finck 
792c2c66affSColin Finck   type = mr->iType;
793c2c66affSColin Finck 
794c2c66affSColin Finck   TRACE("record %s\n", get_emr_name(type));
795c2c66affSColin Finck   switch(type)
796c2c66affSColin Finck     {
797c2c66affSColin Finck     case EMR_HEADER:
798c2c66affSColin Finck       break;
799c2c66affSColin Finck     case EMR_EOF:
800c2c66affSColin Finck       break;
801c2c66affSColin Finck     case EMR_GDICOMMENT:
802c2c66affSColin Finck       {
803c2c66affSColin Finck         const EMRGDICOMMENT *lpGdiComment = (const EMRGDICOMMENT *)mr;
804c2c66affSColin Finck         /* In an enhanced metafile, there can be both public and private GDI comments */
805c2c66affSColin Finck         GdiComment( hdc, lpGdiComment->cbData, lpGdiComment->Data );
806c2c66affSColin Finck         break;
807c2c66affSColin Finck       }
808c2c66affSColin Finck     case EMR_SETMAPMODE:
809c2c66affSColin Finck       {
810c2c66affSColin Finck         const EMRSETMAPMODE *pSetMapMode = (const EMRSETMAPMODE *)mr;
811c2c66affSColin Finck 
812c2c66affSColin Finck         if (info->state.mode == pSetMapMode->iMode &&
813c2c66affSColin Finck             (info->state.mode == MM_ISOTROPIC || info->state.mode == MM_ANISOTROPIC))
814c2c66affSColin Finck             break;
815c2c66affSColin Finck         info->state.mode = pSetMapMode->iMode;
816c2c66affSColin Finck         EMF_SetMapMode(hdc, info);
817c2c66affSColin Finck 
818c2c66affSColin Finck         if (!IS_WIN9X())
819c2c66affSColin Finck             EMF_Update_MF_Xform(hdc, info);
820c2c66affSColin Finck 
821c2c66affSColin Finck 	break;
822c2c66affSColin Finck       }
823c2c66affSColin Finck     case EMR_SETBKMODE:
824c2c66affSColin Finck       {
825c2c66affSColin Finck         const EMRSETBKMODE *pSetBkMode = (const EMRSETBKMODE *)mr;
826c2c66affSColin Finck 	SetBkMode(hdc, pSetBkMode->iMode);
827c2c66affSColin Finck 	break;
828c2c66affSColin Finck       }
829c2c66affSColin Finck     case EMR_SETBKCOLOR:
830c2c66affSColin Finck       {
831c2c66affSColin Finck         const EMRSETBKCOLOR *pSetBkColor = (const EMRSETBKCOLOR *)mr;
832c2c66affSColin Finck 	SetBkColor(hdc, pSetBkColor->crColor);
833c2c66affSColin Finck 	break;
834c2c66affSColin Finck       }
835c2c66affSColin Finck     case EMR_SETPOLYFILLMODE:
836c2c66affSColin Finck       {
837c2c66affSColin Finck         const EMRSETPOLYFILLMODE *pSetPolyFillMode = (const EMRSETPOLYFILLMODE *)mr;
838c2c66affSColin Finck 	SetPolyFillMode(hdc, pSetPolyFillMode->iMode);
839c2c66affSColin Finck 	break;
840c2c66affSColin Finck       }
841c2c66affSColin Finck     case EMR_SETROP2:
842c2c66affSColin Finck       {
843c2c66affSColin Finck         const EMRSETROP2 *pSetROP2 = (const EMRSETROP2 *)mr;
844c2c66affSColin Finck 	SetROP2(hdc, pSetROP2->iMode);
845c2c66affSColin Finck 	break;
846c2c66affSColin Finck       }
847c2c66affSColin Finck     case EMR_SETSTRETCHBLTMODE:
848c2c66affSColin Finck       {
849c2c66affSColin Finck 	const EMRSETSTRETCHBLTMODE *pSetStretchBltMode = (const EMRSETSTRETCHBLTMODE *)mr;
850c2c66affSColin Finck 	SetStretchBltMode(hdc, pSetStretchBltMode->iMode);
851c2c66affSColin Finck 	break;
852c2c66affSColin Finck       }
853c2c66affSColin Finck     case EMR_SETTEXTALIGN:
854c2c66affSColin Finck       {
855c2c66affSColin Finck 	const EMRSETTEXTALIGN *pSetTextAlign = (const EMRSETTEXTALIGN *)mr;
856c2c66affSColin Finck 	SetTextAlign(hdc, pSetTextAlign->iMode);
857c2c66affSColin Finck 	break;
858c2c66affSColin Finck       }
859c2c66affSColin Finck     case EMR_SETTEXTCOLOR:
860c2c66affSColin Finck       {
861c2c66affSColin Finck 	const EMRSETTEXTCOLOR *pSetTextColor = (const EMRSETTEXTCOLOR *)mr;
862c2c66affSColin Finck 	SetTextColor(hdc, pSetTextColor->crColor);
863c2c66affSColin Finck 	break;
864c2c66affSColin Finck       }
865c2c66affSColin Finck     case EMR_SAVEDC:
866c2c66affSColin Finck       {
867c2c66affSColin Finck         if (SaveDC( hdc ))
868c2c66affSColin Finck             EMF_SaveDC( info );
869c2c66affSColin Finck 	break;
870c2c66affSColin Finck       }
871c2c66affSColin Finck     case EMR_RESTOREDC:
872c2c66affSColin Finck       {
873c2c66affSColin Finck 	const EMRRESTOREDC *pRestoreDC = (const EMRRESTOREDC *)mr;
874c2c66affSColin Finck         TRACE("EMR_RESTORE: %d\n", pRestoreDC->iRelative);
875c2c66affSColin Finck         if (RestoreDC( hdc, pRestoreDC->iRelative ))
876c2c66affSColin Finck             EMF_RestoreDC( info, pRestoreDC->iRelative );
877c2c66affSColin Finck 	break;
878c2c66affSColin Finck       }
879c2c66affSColin Finck     case EMR_INTERSECTCLIPRECT:
880c2c66affSColin Finck       {
881c2c66affSColin Finck 	const EMRINTERSECTCLIPRECT *pClipRect = (const EMRINTERSECTCLIPRECT *)mr;
882c2c66affSColin Finck         TRACE("EMR_INTERSECTCLIPRECT: rect %d,%d - %d, %d\n",
883c2c66affSColin Finck               pClipRect->rclClip.left, pClipRect->rclClip.top,
884c2c66affSColin Finck               pClipRect->rclClip.right, pClipRect->rclClip.bottom);
885c2c66affSColin Finck         IntersectClipRect(hdc, pClipRect->rclClip.left, pClipRect->rclClip.top,
886c2c66affSColin Finck                           pClipRect->rclClip.right, pClipRect->rclClip.bottom);
887c2c66affSColin Finck 	break;
888c2c66affSColin Finck       }
889c2c66affSColin Finck     case EMR_SELECTOBJECT:
890c2c66affSColin Finck       {
891c2c66affSColin Finck 	const EMRSELECTOBJECT *pSelectObject = (const EMRSELECTOBJECT *)mr;
892c2c66affSColin Finck 	if( pSelectObject->ihObject & 0x80000000 ) {
893c2c66affSColin Finck 	  /* High order bit is set - it's a stock object
894c2c66affSColin Finck 	   * Strip the high bit to get the index.
895c2c66affSColin Finck 	   * See MSDN article Q142319
896c2c66affSColin Finck 	   */
897c2c66affSColin Finck 	  SelectObject( hdc, GetStockObject( pSelectObject->ihObject &
898c2c66affSColin Finck 					     0x7fffffff ) );
899c2c66affSColin Finck 	} else {
900c2c66affSColin Finck 	  /* High order bit wasn't set - not a stock object
901c2c66affSColin Finck 	   */
902c2c66affSColin Finck 	      SelectObject( hdc,
903c2c66affSColin Finck 			(handletable->objectHandle)[pSelectObject->ihObject] );
904c2c66affSColin Finck 	}
905c2c66affSColin Finck 	break;
906c2c66affSColin Finck       }
907c2c66affSColin Finck     case EMR_DELETEOBJECT:
908c2c66affSColin Finck       {
909c2c66affSColin Finck 	const EMRDELETEOBJECT *pDeleteObject = (const EMRDELETEOBJECT *)mr;
910c2c66affSColin Finck 	DeleteObject( (handletable->objectHandle)[pDeleteObject->ihObject]);
911c2c66affSColin Finck 	(handletable->objectHandle)[pDeleteObject->ihObject] = 0;
912c2c66affSColin Finck 	break;
913c2c66affSColin Finck       }
914c2c66affSColin Finck     case EMR_SETWINDOWORGEX:
915c2c66affSColin Finck       {
916c2c66affSColin Finck     	const EMRSETWINDOWORGEX *pSetWindowOrgEx = (const EMRSETWINDOWORGEX *)mr;
917c2c66affSColin Finck 
918c2c66affSColin Finck         info->state.wndOrgX = pSetWindowOrgEx->ptlOrigin.x;
919c2c66affSColin Finck         info->state.wndOrgY = pSetWindowOrgEx->ptlOrigin.y;
920c2c66affSColin Finck 
921c2c66affSColin Finck         TRACE("SetWindowOrgEx: %d,%d\n", info->state.wndOrgX, info->state.wndOrgY);
922c2c66affSColin Finck 
923c2c66affSColin Finck         if (!IS_WIN9X())
924c2c66affSColin Finck             EMF_Update_MF_Xform(hdc, info);
925c2c66affSColin Finck 
926c2c66affSColin Finck         break;
927c2c66affSColin Finck       }
928c2c66affSColin Finck     case EMR_SETWINDOWEXTEX:
929c2c66affSColin Finck       {
930c2c66affSColin Finck 	const EMRSETWINDOWEXTEX *pSetWindowExtEx = (const EMRSETWINDOWEXTEX *)mr;
931c2c66affSColin Finck 
932c2c66affSColin Finck         if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
933c2c66affSColin Finck 	    break;
934c2c66affSColin Finck         info->state.wndExtX = pSetWindowExtEx->szlExtent.cx;
935c2c66affSColin Finck         info->state.wndExtY = pSetWindowExtEx->szlExtent.cy;
936c2c66affSColin Finck         if (info->state.mode == MM_ISOTROPIC)
937c2c66affSColin Finck             EMF_FixIsotropic(hdc, info);
938c2c66affSColin Finck 
939c2c66affSColin Finck         TRACE("SetWindowExtEx: %d,%d\n",info->state.wndExtX, info->state.wndExtY);
940c2c66affSColin Finck 
941c2c66affSColin Finck         if (!IS_WIN9X())
942c2c66affSColin Finck             EMF_Update_MF_Xform(hdc, info);
943c2c66affSColin Finck 
944c2c66affSColin Finck 	break;
945c2c66affSColin Finck       }
946c2c66affSColin Finck     case EMR_SETVIEWPORTORGEX:
947c2c66affSColin Finck       {
948c2c66affSColin Finck 	const EMRSETVIEWPORTORGEX *pSetViewportOrgEx = (const EMRSETVIEWPORTORGEX *)mr;
949c2c66affSColin Finck 
950c2c66affSColin Finck         info->state.vportOrgX = pSetViewportOrgEx->ptlOrigin.x;
951c2c66affSColin Finck         info->state.vportOrgY = pSetViewportOrgEx->ptlOrigin.y;
952c2c66affSColin Finck         TRACE("SetViewportOrgEx: %d,%d\n", info->state.vportOrgX, info->state.vportOrgY);
953c2c66affSColin Finck 
954c2c66affSColin Finck         if (!IS_WIN9X())
955c2c66affSColin Finck             EMF_Update_MF_Xform(hdc, info);
956c2c66affSColin Finck 
957c2c66affSColin Finck 	break;
958c2c66affSColin Finck       }
959c2c66affSColin Finck     case EMR_SETVIEWPORTEXTEX:
960c2c66affSColin Finck       {
961c2c66affSColin Finck 	const EMRSETVIEWPORTEXTEX *pSetViewportExtEx = (const EMRSETVIEWPORTEXTEX *)mr;
962c2c66affSColin Finck 
963c2c66affSColin Finck         if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
964c2c66affSColin Finck 	    break;
965c2c66affSColin Finck         info->state.vportExtX = pSetViewportExtEx->szlExtent.cx;
966c2c66affSColin Finck         info->state.vportExtY = pSetViewportExtEx->szlExtent.cy;
967c2c66affSColin Finck         if (info->state.mode == MM_ISOTROPIC)
968c2c66affSColin Finck             EMF_FixIsotropic(hdc, info);
969c2c66affSColin Finck         TRACE("SetViewportExtEx: %d,%d\n", info->state.vportExtX, info->state.vportExtY);
970c2c66affSColin Finck 
971c2c66affSColin Finck         if (!IS_WIN9X())
972c2c66affSColin Finck             EMF_Update_MF_Xform(hdc, info);
973c2c66affSColin Finck 
974c2c66affSColin Finck 	break;
975c2c66affSColin Finck       }
976c2c66affSColin Finck     case EMR_CREATEPEN:
977c2c66affSColin Finck       {
978c2c66affSColin Finck 	const EMRCREATEPEN *pCreatePen = (const EMRCREATEPEN *)mr;
979c2c66affSColin Finck 	(handletable->objectHandle)[pCreatePen->ihPen] =
980c2c66affSColin Finck 	  CreatePenIndirect(&pCreatePen->lopn);
981c2c66affSColin Finck 	break;
982c2c66affSColin Finck       }
983c2c66affSColin Finck     case EMR_EXTCREATEPEN:
984c2c66affSColin Finck       {
985c2c66affSColin Finck 	const EMREXTCREATEPEN *pPen = (const EMREXTCREATEPEN *)mr;
986c2c66affSColin Finck 	LOGBRUSH lb;
987c2c66affSColin Finck 	lb.lbStyle = pPen->elp.elpBrushStyle;
988c2c66affSColin Finck 	lb.lbColor = pPen->elp.elpColor;
989c2c66affSColin Finck 	lb.lbHatch = pPen->elp.elpHatch;
990c2c66affSColin Finck 
991c2c66affSColin Finck 	if(pPen->offBmi || pPen->offBits)
992c2c66affSColin Finck 	  FIXME("EMR_EXTCREATEPEN: Need to copy brush bitmap\n");
993c2c66affSColin Finck 
994c2c66affSColin Finck 	(handletable->objectHandle)[pPen->ihPen] =
995c2c66affSColin Finck 	  ExtCreatePen(pPen->elp.elpPenStyle, pPen->elp.elpWidth, &lb,
996c2c66affSColin Finck                        pPen->elp.elpNumEntries, pPen->elp.elpNumEntries ? pPen->elp.elpStyleEntry : NULL);
997c2c66affSColin Finck 	break;
998c2c66affSColin Finck       }
999c2c66affSColin Finck     case EMR_CREATEBRUSHINDIRECT:
1000c2c66affSColin Finck       {
1001c2c66affSColin Finck 	const EMRCREATEBRUSHINDIRECT *pBrush = (const EMRCREATEBRUSHINDIRECT *)mr;
1002c2c66affSColin Finck         LOGBRUSH brush;
1003c2c66affSColin Finck         brush.lbStyle = pBrush->lb.lbStyle;
1004c2c66affSColin Finck         brush.lbColor = pBrush->lb.lbColor;
1005c2c66affSColin Finck         brush.lbHatch = pBrush->lb.lbHatch;
1006c2c66affSColin Finck         (handletable->objectHandle)[pBrush->ihBrush] = CreateBrushIndirect(&brush);
1007c2c66affSColin Finck 	break;
1008c2c66affSColin Finck       }
1009c2c66affSColin Finck     case EMR_EXTCREATEFONTINDIRECTW:
1010c2c66affSColin Finck       {
1011c2c66affSColin Finck 	const EMREXTCREATEFONTINDIRECTW *pFont = (const EMREXTCREATEFONTINDIRECTW *)mr;
1012c2c66affSColin Finck 	(handletable->objectHandle)[pFont->ihFont] =
1013c2c66affSColin Finck 	  CreateFontIndirectW(&pFont->elfw.elfLogFont);
1014c2c66affSColin Finck 	break;
1015c2c66affSColin Finck       }
1016c2c66affSColin Finck     case EMR_MOVETOEX:
1017c2c66affSColin Finck       {
1018c2c66affSColin Finck 	const EMRMOVETOEX *pMoveToEx = (const EMRMOVETOEX *)mr;
1019c2c66affSColin Finck 	MoveToEx(hdc, pMoveToEx->ptl.x, pMoveToEx->ptl.y, NULL);
1020c2c66affSColin Finck 	break;
1021c2c66affSColin Finck       }
1022c2c66affSColin Finck     case EMR_LINETO:
1023c2c66affSColin Finck       {
1024c2c66affSColin Finck 	const EMRLINETO *pLineTo = (const EMRLINETO *)mr;
1025c2c66affSColin Finck         LineTo(hdc, pLineTo->ptl.x, pLineTo->ptl.y);
1026c2c66affSColin Finck 	break;
1027c2c66affSColin Finck       }
1028c2c66affSColin Finck     case EMR_RECTANGLE:
1029c2c66affSColin Finck       {
1030c2c66affSColin Finck 	const EMRRECTANGLE *pRect = (const EMRRECTANGLE *)mr;
1031c2c66affSColin Finck 	Rectangle(hdc, pRect->rclBox.left, pRect->rclBox.top,
1032c2c66affSColin Finck 		  pRect->rclBox.right, pRect->rclBox.bottom);
1033c2c66affSColin Finck 	break;
1034c2c66affSColin Finck       }
1035c2c66affSColin Finck     case EMR_ELLIPSE:
1036c2c66affSColin Finck       {
1037c2c66affSColin Finck 	const EMRELLIPSE *pEllipse = (const EMRELLIPSE *)mr;
1038c2c66affSColin Finck 	Ellipse(hdc, pEllipse->rclBox.left, pEllipse->rclBox.top,
1039c2c66affSColin Finck 		pEllipse->rclBox.right, pEllipse->rclBox.bottom);
1040c2c66affSColin Finck 	break;
1041c2c66affSColin Finck       }
1042c2c66affSColin Finck     case EMR_POLYGON16:
1043c2c66affSColin Finck       {
1044c2c66affSColin Finck 	const EMRPOLYGON16 *pPoly = (const EMRPOLYGON16 *)mr;
1045c2c66affSColin Finck 	/* Shouldn't use Polygon16 since pPoly->cpts is DWORD */
1046c2c66affSColin Finck 	POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1047c2c66affSColin Finck 				pPoly->cpts * sizeof(POINT) );
1048c2c66affSColin Finck 	DWORD i;
1049c2c66affSColin Finck 	for(i = 0; i < pPoly->cpts; i++)
1050c2c66affSColin Finck 	{
1051c2c66affSColin Finck 	    pts[i].x = pPoly->apts[i].x;
1052c2c66affSColin Finck 	    pts[i].y = pPoly->apts[i].y;
1053c2c66affSColin Finck 	}
1054c2c66affSColin Finck 	Polygon(hdc, pts, pPoly->cpts);
1055c2c66affSColin Finck 	HeapFree( GetProcessHeap(), 0, pts );
1056c2c66affSColin Finck 	break;
1057c2c66affSColin Finck       }
1058c2c66affSColin Finck     case EMR_POLYLINE16:
1059c2c66affSColin Finck       {
1060c2c66affSColin Finck 	const EMRPOLYLINE16 *pPoly = (const EMRPOLYLINE16 *)mr;
1061c2c66affSColin Finck 	/* Shouldn't use Polyline16 since pPoly->cpts is DWORD */
1062c2c66affSColin Finck 	POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1063c2c66affSColin Finck 				pPoly->cpts * sizeof(POINT) );
1064c2c66affSColin Finck 	DWORD i;
1065c2c66affSColin Finck 	for(i = 0; i < pPoly->cpts; i++)
1066c2c66affSColin Finck 	{
1067c2c66affSColin Finck 	    pts[i].x = pPoly->apts[i].x;
1068c2c66affSColin Finck 	    pts[i].y = pPoly->apts[i].y;
1069c2c66affSColin Finck 	}
1070c2c66affSColin Finck 	Polyline(hdc, pts, pPoly->cpts);
1071c2c66affSColin Finck 	HeapFree( GetProcessHeap(), 0, pts );
1072c2c66affSColin Finck 	break;
1073c2c66affSColin Finck       }
1074c2c66affSColin Finck     case EMR_POLYLINETO16:
1075c2c66affSColin Finck       {
1076c2c66affSColin Finck 	const EMRPOLYLINETO16 *pPoly = (const EMRPOLYLINETO16 *)mr;
1077c2c66affSColin Finck 	/* Shouldn't use PolylineTo16 since pPoly->cpts is DWORD */
1078c2c66affSColin Finck 	POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1079c2c66affSColin Finck 				pPoly->cpts * sizeof(POINT) );
1080c2c66affSColin Finck 	DWORD i;
1081c2c66affSColin Finck 	for(i = 0; i < pPoly->cpts; i++)
1082c2c66affSColin Finck 	{
1083c2c66affSColin Finck 	    pts[i].x = pPoly->apts[i].x;
1084c2c66affSColin Finck 	    pts[i].y = pPoly->apts[i].y;
1085c2c66affSColin Finck 	}
1086c2c66affSColin Finck 	PolylineTo(hdc, pts, pPoly->cpts);
1087c2c66affSColin Finck 	HeapFree( GetProcessHeap(), 0, pts );
1088c2c66affSColin Finck 	break;
1089c2c66affSColin Finck       }
1090c2c66affSColin Finck     case EMR_POLYBEZIER16:
1091c2c66affSColin Finck       {
1092c2c66affSColin Finck 	const EMRPOLYBEZIER16 *pPoly = (const EMRPOLYBEZIER16 *)mr;
1093c2c66affSColin Finck 	/* Shouldn't use PolyBezier16 since pPoly->cpts is DWORD */
1094c2c66affSColin Finck 	POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1095c2c66affSColin Finck 				pPoly->cpts * sizeof(POINT) );
1096c2c66affSColin Finck 	DWORD i;
1097c2c66affSColin Finck 	for(i = 0; i < pPoly->cpts; i++)
1098c2c66affSColin Finck 	{
1099c2c66affSColin Finck 	    pts[i].x = pPoly->apts[i].x;
1100c2c66affSColin Finck 	    pts[i].y = pPoly->apts[i].y;
1101c2c66affSColin Finck 	}
1102c2c66affSColin Finck 	PolyBezier(hdc, pts, pPoly->cpts);
1103c2c66affSColin Finck 	HeapFree( GetProcessHeap(), 0, pts );
1104c2c66affSColin Finck 	break;
1105c2c66affSColin Finck       }
1106c2c66affSColin Finck     case EMR_POLYBEZIERTO16:
1107c2c66affSColin Finck       {
1108c2c66affSColin Finck 	const EMRPOLYBEZIERTO16 *pPoly = (const EMRPOLYBEZIERTO16 *)mr;
1109c2c66affSColin Finck 	/* Shouldn't use PolyBezierTo16 since pPoly->cpts is DWORD */
1110c2c66affSColin Finck 	POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1111c2c66affSColin Finck 				pPoly->cpts * sizeof(POINT) );
1112c2c66affSColin Finck 	DWORD i;
1113c2c66affSColin Finck 	for(i = 0; i < pPoly->cpts; i++)
1114c2c66affSColin Finck 	{
1115c2c66affSColin Finck 	    pts[i].x = pPoly->apts[i].x;
1116c2c66affSColin Finck 	    pts[i].y = pPoly->apts[i].y;
1117c2c66affSColin Finck 	}
1118c2c66affSColin Finck 	PolyBezierTo(hdc, pts, pPoly->cpts);
1119c2c66affSColin Finck 	HeapFree( GetProcessHeap(), 0, pts );
1120c2c66affSColin Finck 	break;
1121c2c66affSColin Finck       }
1122c2c66affSColin Finck     case EMR_POLYPOLYGON16:
1123c2c66affSColin Finck       {
1124c2c66affSColin Finck         const EMRPOLYPOLYGON16 *pPolyPoly = (const EMRPOLYPOLYGON16 *)mr;
1125c2c66affSColin Finck 	/* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1126c2c66affSColin Finck 	   pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1127c2c66affSColin Finck 
1128c2c66affSColin Finck         const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1129c2c66affSColin Finck         POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1130c2c66affSColin Finck 	DWORD i;
1131c2c66affSColin Finck 	for(i = 0; i < pPolyPoly->cpts; i++)
1132c2c66affSColin Finck         {
1133c2c66affSColin Finck             pt[i].x = pts[i].x;
1134c2c66affSColin Finck             pt[i].y = pts[i].y;
1135c2c66affSColin Finck         }
1136c2c66affSColin Finck 	PolyPolygon(hdc, pt, (const INT*)pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1137c2c66affSColin Finck 	HeapFree( GetProcessHeap(), 0, pt );
1138c2c66affSColin Finck 	break;
1139c2c66affSColin Finck       }
1140c2c66affSColin Finck     case EMR_POLYPOLYLINE16:
1141c2c66affSColin Finck       {
1142c2c66affSColin Finck         const EMRPOLYPOLYLINE16 *pPolyPoly = (const EMRPOLYPOLYLINE16 *)mr;
1143c2c66affSColin Finck 	/* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1144c2c66affSColin Finck 	   pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1145c2c66affSColin Finck 
1146c2c66affSColin Finck         const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1147c2c66affSColin Finck         POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1148c2c66affSColin Finck 	DWORD i;
1149c2c66affSColin Finck 	for(i = 0; i < pPolyPoly->cpts; i++)
1150c2c66affSColin Finck         {
1151c2c66affSColin Finck             pt[i].x = pts[i].x;
1152c2c66affSColin Finck             pt[i].y = pts[i].y;
1153c2c66affSColin Finck         }
1154c2c66affSColin Finck 	PolyPolyline(hdc, pt, pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1155c2c66affSColin Finck 	HeapFree( GetProcessHeap(), 0, pt );
1156c2c66affSColin Finck 	break;
1157c2c66affSColin Finck       }
1158c2c66affSColin Finck 
1159*fc16259fSJames Tabor     case EMR_POLYDRAW16:
1160*fc16259fSJames Tabor     {
1161*fc16259fSJames Tabor         const EMRPOLYDRAW16 *pPolyDraw16 = (const EMRPOLYDRAW16 *)mr;
1162*fc16259fSJames Tabor         const POINTS *ptl = pPolyDraw16->apts;
1163*fc16259fSJames Tabor         POINT *pts = HeapAlloc(GetProcessHeap(), 0, pPolyDraw16->cpts * sizeof(POINT));
1164*fc16259fSJames Tabor         DWORD i;
1165*fc16259fSJames Tabor 
1166*fc16259fSJames Tabor         /* NB abTypes array doesn't start at pPolyDraw16->abTypes. It's actually
1167*fc16259fSJames Tabor            pPolyDraw16->apts + pPolyDraw16->cpts. */
1168*fc16259fSJames Tabor         const BYTE *types = (BYTE*)(pPolyDraw16->apts + pPolyDraw16->cpts);
1169*fc16259fSJames Tabor 
1170*fc16259fSJames Tabor         if (!pts)
1171*fc16259fSJames Tabor             break;
1172*fc16259fSJames Tabor 
1173*fc16259fSJames Tabor         for (i = 0; i < pPolyDraw16->cpts; ++i)
1174*fc16259fSJames Tabor         {
1175*fc16259fSJames Tabor             pts[i].x = ptl[i].x;
1176*fc16259fSJames Tabor             pts[i].y = ptl[i].y;
1177*fc16259fSJames Tabor         }
1178*fc16259fSJames Tabor 
1179*fc16259fSJames Tabor         PolyDraw(hdc, pts, types, pPolyDraw16->cpts);
1180*fc16259fSJames Tabor         HeapFree(GetProcessHeap(), 0, pts);
1181*fc16259fSJames Tabor         break;
1182*fc16259fSJames Tabor     }
1183*fc16259fSJames Tabor 
1184c2c66affSColin Finck     case EMR_STRETCHDIBITS:
1185c2c66affSColin Finck       {
1186c2c66affSColin Finck 	const EMRSTRETCHDIBITS *pStretchDIBits = (const EMRSTRETCHDIBITS *)mr;
1187c2c66affSColin Finck 
1188c2c66affSColin Finck 	StretchDIBits(hdc,
1189c2c66affSColin Finck 		      pStretchDIBits->xDest,
1190c2c66affSColin Finck 		      pStretchDIBits->yDest,
1191c2c66affSColin Finck 		      pStretchDIBits->cxDest,
1192c2c66affSColin Finck 		      pStretchDIBits->cyDest,
1193c2c66affSColin Finck 		      pStretchDIBits->xSrc,
1194c2c66affSColin Finck 		      pStretchDIBits->ySrc,
1195c2c66affSColin Finck 		      pStretchDIBits->cxSrc,
1196c2c66affSColin Finck 		      pStretchDIBits->cySrc,
1197c2c66affSColin Finck 		      (const BYTE *)mr + pStretchDIBits->offBitsSrc,
1198c2c66affSColin Finck 		      (const BITMAPINFO *)((const BYTE *)mr + pStretchDIBits->offBmiSrc),
1199c2c66affSColin Finck 		      pStretchDIBits->iUsageSrc,
1200c2c66affSColin Finck 		      pStretchDIBits->dwRop);
1201c2c66affSColin Finck 	break;
1202c2c66affSColin Finck       }
1203c2c66affSColin Finck 
1204c2c66affSColin Finck     case EMR_EXTTEXTOUTA:
1205c2c66affSColin Finck     {
1206c2c66affSColin Finck 	const EMREXTTEXTOUTA *pExtTextOutA = (const EMREXTTEXTOUTA *)mr;
1207c2c66affSColin Finck 	RECT rc;
1208c2c66affSColin Finck         const INT *dx = NULL;
1209c2c66affSColin Finck         int old_mode;
1210c2c66affSColin Finck 
1211c2c66affSColin Finck 	rc.left = pExtTextOutA->emrtext.rcl.left;
1212c2c66affSColin Finck 	rc.top = pExtTextOutA->emrtext.rcl.top;
1213c2c66affSColin Finck 	rc.right = pExtTextOutA->emrtext.rcl.right;
1214c2c66affSColin Finck 	rc.bottom = pExtTextOutA->emrtext.rcl.bottom;
1215c2c66affSColin Finck         TRACE("EMR_EXTTEXTOUTA: x,y = %d, %d. rect = %s. flags %08x\n",
1216c2c66affSColin Finck               pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1217c2c66affSColin Finck               wine_dbgstr_rect(&rc), pExtTextOutA->emrtext.fOptions);
1218c2c66affSColin Finck 
1219c2c66affSColin Finck         old_mode = SetGraphicsMode(hdc, pExtTextOutA->iGraphicsMode);
1220c2c66affSColin Finck         /* Reselect the font back into the dc so that the transformation
1221c2c66affSColin Finck            gets updated. */
1222c2c66affSColin Finck         SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1223c2c66affSColin Finck 
1224c2c66affSColin Finck         /* Linux version of pstoedit produces EMFs with offDx set to 0.
1225c2c66affSColin Finck          * These files can be enumerated and played under Win98 just
1226c2c66affSColin Finck          * fine, but at least Win2k chokes on them.
1227c2c66affSColin Finck          */
1228c2c66affSColin Finck         if (pExtTextOutA->emrtext.offDx)
1229c2c66affSColin Finck             dx = (const INT *)((const BYTE *)mr + pExtTextOutA->emrtext.offDx);
1230c2c66affSColin Finck 
1231c2c66affSColin Finck 	ExtTextOutA(hdc, pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1232c2c66affSColin Finck 	    pExtTextOutA->emrtext.fOptions, &rc,
1233c2c66affSColin Finck 	    (LPCSTR)((const BYTE *)mr + pExtTextOutA->emrtext.offString), pExtTextOutA->emrtext.nChars,
1234c2c66affSColin Finck 	    dx);
1235c2c66affSColin Finck 
1236c2c66affSColin Finck         SetGraphicsMode(hdc, old_mode);
1237c2c66affSColin Finck 	break;
1238c2c66affSColin Finck     }
1239c2c66affSColin Finck 
1240c2c66affSColin Finck     case EMR_EXTTEXTOUTW:
1241c2c66affSColin Finck     {
1242c2c66affSColin Finck 	const EMREXTTEXTOUTW *pExtTextOutW = (const EMREXTTEXTOUTW *)mr;
1243c2c66affSColin Finck 	RECT rc;
1244c2c66affSColin Finck         const INT *dx = NULL;
1245c2c66affSColin Finck         int old_mode;
1246c2c66affSColin Finck 
1247c2c66affSColin Finck 	rc.left = pExtTextOutW->emrtext.rcl.left;
1248c2c66affSColin Finck 	rc.top = pExtTextOutW->emrtext.rcl.top;
1249c2c66affSColin Finck 	rc.right = pExtTextOutW->emrtext.rcl.right;
1250c2c66affSColin Finck 	rc.bottom = pExtTextOutW->emrtext.rcl.bottom;
1251c2c66affSColin Finck         TRACE("EMR_EXTTEXTOUTW: x,y = %d, %d.  rect = %s. flags %08x\n",
1252c2c66affSColin Finck               pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1253c2c66affSColin Finck               wine_dbgstr_rect(&rc), pExtTextOutW->emrtext.fOptions);
1254c2c66affSColin Finck 
1255c2c66affSColin Finck         old_mode = SetGraphicsMode(hdc, pExtTextOutW->iGraphicsMode);
1256c2c66affSColin Finck         /* Reselect the font back into the dc so that the transformation
1257c2c66affSColin Finck            gets updated. */
1258c2c66affSColin Finck         SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1259c2c66affSColin Finck 
1260c2c66affSColin Finck         /* Linux version of pstoedit produces EMFs with offDx set to 0.
1261c2c66affSColin Finck          * These files can be enumerated and played under Win98 just
1262c2c66affSColin Finck          * fine, but at least Win2k chokes on them.
1263c2c66affSColin Finck          */
1264c2c66affSColin Finck         if (pExtTextOutW->emrtext.offDx)
1265c2c66affSColin Finck             dx = (const INT *)((const BYTE *)mr + pExtTextOutW->emrtext.offDx);
1266c2c66affSColin Finck 
1267c2c66affSColin Finck 	ExtTextOutW(hdc, pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1268c2c66affSColin Finck 	    pExtTextOutW->emrtext.fOptions, &rc,
1269c2c66affSColin Finck 	    (LPCWSTR)((const BYTE *)mr + pExtTextOutW->emrtext.offString), pExtTextOutW->emrtext.nChars,
1270c2c66affSColin Finck 	    dx);
1271c2c66affSColin Finck 
1272c2c66affSColin Finck         SetGraphicsMode(hdc, old_mode);
1273c2c66affSColin Finck 	break;
1274c2c66affSColin Finck     }
1275c2c66affSColin Finck 
1276c2c66affSColin Finck     case EMR_CREATEPALETTE:
1277c2c66affSColin Finck       {
1278c2c66affSColin Finck 	const EMRCREATEPALETTE *lpCreatePal = (const EMRCREATEPALETTE *)mr;
1279c2c66affSColin Finck 
1280c2c66affSColin Finck 	(handletable->objectHandle)[ lpCreatePal->ihPal ] =
1281c2c66affSColin Finck 		CreatePalette( &lpCreatePal->lgpl );
1282c2c66affSColin Finck 
1283c2c66affSColin Finck 	break;
1284c2c66affSColin Finck       }
1285c2c66affSColin Finck 
1286c2c66affSColin Finck     case EMR_SELECTPALETTE:
1287c2c66affSColin Finck       {
1288c2c66affSColin Finck 	const EMRSELECTPALETTE *lpSelectPal = (const EMRSELECTPALETTE *)mr;
1289c2c66affSColin Finck 
1290c2c66affSColin Finck 	if( lpSelectPal->ihPal & 0x80000000 ) {
1291c2c66affSColin Finck 		SelectPalette( hdc, GetStockObject(lpSelectPal->ihPal & 0x7fffffff), TRUE);
1292c2c66affSColin Finck 	} else {
1293c2c66affSColin Finck 		SelectPalette( hdc, (handletable->objectHandle)[lpSelectPal->ihPal], TRUE);
1294c2c66affSColin Finck 	}
1295c2c66affSColin Finck 	break;
1296c2c66affSColin Finck       }
1297c2c66affSColin Finck 
1298c2c66affSColin Finck     case EMR_REALIZEPALETTE:
1299c2c66affSColin Finck       {
1300c2c66affSColin Finck 	RealizePalette( hdc );
1301c2c66affSColin Finck 	break;
1302c2c66affSColin Finck       }
1303c2c66affSColin Finck 
1304c2c66affSColin Finck     case EMR_EXTSELECTCLIPRGN:
1305c2c66affSColin Finck       {
1306c2c66affSColin Finck 	const EMREXTSELECTCLIPRGN *lpRgn = (const EMREXTSELECTCLIPRGN *)mr;
1307c2c66affSColin Finck #ifdef __REACTOS__
1308c2c66affSColin Finck 	const RGNDATA *pRgnData = (const RGNDATA *)lpRgn->RgnData;
1309c2c66affSColin Finck 	DWORD dwSize = sizeof(RGNDATAHEADER) + pRgnData->rdh.nCount * sizeof(RECT);
1310c2c66affSColin Finck #endif
1311c2c66affSColin Finck 	HRGN hRgn = 0;
1312c2c66affSColin Finck 
1313c2c66affSColin Finck         if (mr->nSize >= sizeof(*lpRgn) + sizeof(RGNDATAHEADER))
1314c2c66affSColin Finck #ifdef __REACTOS__
1315c2c66affSColin Finck             hRgn = ExtCreateRegion( &info->init_transform, dwSize, pRgnData );
1316c2c66affSColin Finck #else
1317c2c66affSColin Finck             hRgn = ExtCreateRegion( &info->init_transform, 0, (const RGNDATA *)lpRgn->RgnData );
1318c2c66affSColin Finck #endif
1319c2c66affSColin Finck 	ExtSelectClipRgn(hdc, hRgn, (INT)(lpRgn->iMode));
1320c2c66affSColin Finck 	/* ExtSelectClipRgn created a copy of the region */
1321c2c66affSColin Finck 	DeleteObject(hRgn);
1322c2c66affSColin Finck         break;
1323c2c66affSColin Finck       }
1324c2c66affSColin Finck 
1325c2c66affSColin Finck     case EMR_SETMETARGN:
1326c2c66affSColin Finck       {
1327c2c66affSColin Finck         SetMetaRgn( hdc );
1328c2c66affSColin Finck         break;
1329c2c66affSColin Finck       }
1330c2c66affSColin Finck 
1331c2c66affSColin Finck     case EMR_SETWORLDTRANSFORM:
1332c2c66affSColin Finck       {
1333c2c66affSColin Finck         const EMRSETWORLDTRANSFORM *lpXfrm = (const EMRSETWORLDTRANSFORM *)mr;
1334c2c66affSColin Finck         info->state.world_transform = lpXfrm->xform;
1335c2c66affSColin Finck 
1336c2c66affSColin Finck         if (!IS_WIN9X())
1337c2c66affSColin Finck             EMF_Update_MF_Xform(hdc, info);
1338c2c66affSColin Finck 
1339c2c66affSColin Finck         break;
1340c2c66affSColin Finck       }
1341c2c66affSColin Finck 
1342c2c66affSColin Finck     case EMR_POLYBEZIER:
1343c2c66affSColin Finck       {
1344c2c66affSColin Finck         const EMRPOLYBEZIER *lpPolyBez = (const EMRPOLYBEZIER *)mr;
1345c2c66affSColin Finck         PolyBezier(hdc, (const POINT*)lpPolyBez->aptl, (UINT)lpPolyBez->cptl);
1346c2c66affSColin Finck         break;
1347c2c66affSColin Finck       }
1348c2c66affSColin Finck 
1349c2c66affSColin Finck     case EMR_POLYGON:
1350c2c66affSColin Finck       {
1351c2c66affSColin Finck         const EMRPOLYGON *lpPoly = (const EMRPOLYGON *)mr;
1352c2c66affSColin Finck         Polygon( hdc, (const POINT*)lpPoly->aptl, (UINT)lpPoly->cptl );
1353c2c66affSColin Finck         break;
1354c2c66affSColin Finck       }
1355c2c66affSColin Finck 
1356c2c66affSColin Finck     case EMR_POLYLINE:
1357c2c66affSColin Finck       {
1358c2c66affSColin Finck         const EMRPOLYLINE *lpPolyLine = (const EMRPOLYLINE *)mr;
1359c2c66affSColin Finck         Polyline(hdc, (const POINT*)lpPolyLine->aptl, (UINT)lpPolyLine->cptl);
1360c2c66affSColin Finck         break;
1361c2c66affSColin Finck       }
1362c2c66affSColin Finck 
1363c2c66affSColin Finck     case EMR_POLYBEZIERTO:
1364c2c66affSColin Finck       {
1365c2c66affSColin Finck         const EMRPOLYBEZIERTO *lpPolyBezierTo = (const EMRPOLYBEZIERTO *)mr;
1366c2c66affSColin Finck         PolyBezierTo( hdc, (const POINT*)lpPolyBezierTo->aptl,
1367c2c66affSColin Finck 		      (UINT)lpPolyBezierTo->cptl );
1368c2c66affSColin Finck         break;
1369c2c66affSColin Finck       }
1370c2c66affSColin Finck 
1371c2c66affSColin Finck     case EMR_POLYLINETO:
1372c2c66affSColin Finck       {
1373c2c66affSColin Finck         const EMRPOLYLINETO *lpPolyLineTo = (const EMRPOLYLINETO *)mr;
1374c2c66affSColin Finck         PolylineTo( hdc, (const POINT*)lpPolyLineTo->aptl,
1375c2c66affSColin Finck 		    (UINT)lpPolyLineTo->cptl );
1376c2c66affSColin Finck         break;
1377c2c66affSColin Finck       }
1378c2c66affSColin Finck 
1379c2c66affSColin Finck     case EMR_POLYPOLYLINE:
1380c2c66affSColin Finck       {
1381c2c66affSColin Finck         const EMRPOLYPOLYLINE *pPolyPolyline = (const EMRPOLYPOLYLINE *)mr;
1382c2c66affSColin Finck 	/* NB Points at pPolyPolyline->aPolyCounts + pPolyPolyline->nPolys */
1383c2c66affSColin Finck 
1384c2c66affSColin Finck         PolyPolyline(hdc, (const POINT*)(pPolyPolyline->aPolyCounts +
1385c2c66affSColin Finck 				    pPolyPolyline->nPolys),
1386c2c66affSColin Finck 		     pPolyPolyline->aPolyCounts,
1387c2c66affSColin Finck 		     pPolyPolyline->nPolys );
1388c2c66affSColin Finck 
1389c2c66affSColin Finck         break;
1390c2c66affSColin Finck       }
1391c2c66affSColin Finck 
1392c2c66affSColin Finck     case EMR_POLYPOLYGON:
1393c2c66affSColin Finck       {
1394c2c66affSColin Finck         const EMRPOLYPOLYGON *pPolyPolygon = (const EMRPOLYPOLYGON *)mr;
1395c2c66affSColin Finck 
1396c2c66affSColin Finck 	/* NB Points at pPolyPolygon->aPolyCounts + pPolyPolygon->nPolys */
1397c2c66affSColin Finck 
1398c2c66affSColin Finck         PolyPolygon(hdc, (const POINT*)(pPolyPolygon->aPolyCounts +
1399c2c66affSColin Finck 				   pPolyPolygon->nPolys),
1400c2c66affSColin Finck 		    (const INT*)pPolyPolygon->aPolyCounts, pPolyPolygon->nPolys );
1401c2c66affSColin Finck         break;
1402c2c66affSColin Finck       }
1403c2c66affSColin Finck 
1404c2c66affSColin Finck     case EMR_SETBRUSHORGEX:
1405c2c66affSColin Finck       {
1406c2c66affSColin Finck         const EMRSETBRUSHORGEX *lpSetBrushOrgEx = (const EMRSETBRUSHORGEX *)mr;
1407c2c66affSColin Finck 
1408c2c66affSColin Finck         SetBrushOrgEx( hdc,
1409c2c66affSColin Finck                        (INT)lpSetBrushOrgEx->ptlOrigin.x,
1410c2c66affSColin Finck                        (INT)lpSetBrushOrgEx->ptlOrigin.y,
1411c2c66affSColin Finck                        NULL );
1412c2c66affSColin Finck 
1413c2c66affSColin Finck         break;
1414c2c66affSColin Finck       }
1415c2c66affSColin Finck 
1416c2c66affSColin Finck     case EMR_SETPIXELV:
1417c2c66affSColin Finck       {
1418c2c66affSColin Finck         const EMRSETPIXELV *lpSetPixelV = (const EMRSETPIXELV *)mr;
1419c2c66affSColin Finck 
1420c2c66affSColin Finck         SetPixelV( hdc,
1421c2c66affSColin Finck                    (INT)lpSetPixelV->ptlPixel.x,
1422c2c66affSColin Finck                    (INT)lpSetPixelV->ptlPixel.y,
1423c2c66affSColin Finck                    lpSetPixelV->crColor );
1424c2c66affSColin Finck 
1425c2c66affSColin Finck         break;
1426c2c66affSColin Finck       }
1427c2c66affSColin Finck 
1428c2c66affSColin Finck     case EMR_SETMAPPERFLAGS:
1429c2c66affSColin Finck       {
1430c2c66affSColin Finck         const EMRSETMAPPERFLAGS *lpSetMapperFlags = (const EMRSETMAPPERFLAGS *)mr;
1431c2c66affSColin Finck 
1432c2c66affSColin Finck         SetMapperFlags( hdc, lpSetMapperFlags->dwFlags );
1433c2c66affSColin Finck 
1434c2c66affSColin Finck         break;
1435c2c66affSColin Finck       }
1436c2c66affSColin Finck 
1437c2c66affSColin Finck     case EMR_SETCOLORADJUSTMENT:
1438c2c66affSColin Finck       {
1439c2c66affSColin Finck         const EMRSETCOLORADJUSTMENT *lpSetColorAdjust = (const EMRSETCOLORADJUSTMENT *)mr;
1440c2c66affSColin Finck 
1441c2c66affSColin Finck         SetColorAdjustment( hdc, &lpSetColorAdjust->ColorAdjustment );
1442c2c66affSColin Finck 
1443c2c66affSColin Finck         break;
1444c2c66affSColin Finck       }
1445c2c66affSColin Finck 
1446c2c66affSColin Finck     case EMR_OFFSETCLIPRGN:
1447c2c66affSColin Finck       {
1448c2c66affSColin Finck         const EMROFFSETCLIPRGN *lpOffsetClipRgn = (const EMROFFSETCLIPRGN *)mr;
1449c2c66affSColin Finck 
1450c2c66affSColin Finck         OffsetClipRgn( hdc,
1451c2c66affSColin Finck                        (INT)lpOffsetClipRgn->ptlOffset.x,
1452c2c66affSColin Finck                        (INT)lpOffsetClipRgn->ptlOffset.y );
1453c2c66affSColin Finck         FIXME("OffsetClipRgn\n");
1454c2c66affSColin Finck 
1455c2c66affSColin Finck         break;
1456c2c66affSColin Finck       }
1457c2c66affSColin Finck 
1458c2c66affSColin Finck     case EMR_EXCLUDECLIPRECT:
1459c2c66affSColin Finck       {
1460c2c66affSColin Finck         const EMREXCLUDECLIPRECT *lpExcludeClipRect = (const EMREXCLUDECLIPRECT *)mr;
1461c2c66affSColin Finck 
1462c2c66affSColin Finck         ExcludeClipRect( hdc,
1463c2c66affSColin Finck                          lpExcludeClipRect->rclClip.left,
1464c2c66affSColin Finck                          lpExcludeClipRect->rclClip.top,
1465c2c66affSColin Finck                          lpExcludeClipRect->rclClip.right,
1466c2c66affSColin Finck                          lpExcludeClipRect->rclClip.bottom  );
1467c2c66affSColin Finck         FIXME("ExcludeClipRect\n");
1468c2c66affSColin Finck 
1469c2c66affSColin Finck          break;
1470c2c66affSColin Finck       }
1471c2c66affSColin Finck 
1472c2c66affSColin Finck     case EMR_SCALEVIEWPORTEXTEX:
1473c2c66affSColin Finck       {
1474c2c66affSColin Finck         const EMRSCALEVIEWPORTEXTEX *lpScaleViewportExtEx = (const EMRSCALEVIEWPORTEXTEX *)mr;
1475c2c66affSColin Finck 
1476c2c66affSColin Finck         if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1477c2c66affSColin Finck 	    break;
1478c2c66affSColin Finck         if (!lpScaleViewportExtEx->xNum || !lpScaleViewportExtEx->xDenom ||
1479c2c66affSColin Finck             !lpScaleViewportExtEx->yNum || !lpScaleViewportExtEx->yDenom)
1480c2c66affSColin Finck             break;
1481c2c66affSColin Finck         info->state.vportExtX = MulDiv(info->state.vportExtX, lpScaleViewportExtEx->xNum,
1482c2c66affSColin Finck                                  lpScaleViewportExtEx->xDenom);
1483c2c66affSColin Finck         info->state.vportExtY = MulDiv(info->state.vportExtY, lpScaleViewportExtEx->yNum,
1484c2c66affSColin Finck                                  lpScaleViewportExtEx->yDenom);
1485c2c66affSColin Finck         if (info->state.vportExtX == 0) info->state.vportExtX = 1;
1486c2c66affSColin Finck         if (info->state.vportExtY == 0) info->state.vportExtY = 1;
1487c2c66affSColin Finck         if (info->state.mode == MM_ISOTROPIC)
1488c2c66affSColin Finck             EMF_FixIsotropic(hdc, info);
1489c2c66affSColin Finck 
1490c2c66affSColin Finck         TRACE("EMRSCALEVIEWPORTEXTEX %d/%d %d/%d\n",
1491c2c66affSColin Finck              lpScaleViewportExtEx->xNum,lpScaleViewportExtEx->xDenom,
1492c2c66affSColin Finck              lpScaleViewportExtEx->yNum,lpScaleViewportExtEx->yDenom);
1493c2c66affSColin Finck 
1494c2c66affSColin Finck         if (!IS_WIN9X())
1495c2c66affSColin Finck             EMF_Update_MF_Xform(hdc, info);
1496c2c66affSColin Finck 
1497c2c66affSColin Finck         break;
1498c2c66affSColin Finck       }
1499c2c66affSColin Finck 
1500c2c66affSColin Finck     case EMR_SCALEWINDOWEXTEX:
1501c2c66affSColin Finck       {
1502c2c66affSColin Finck         const EMRSCALEWINDOWEXTEX *lpScaleWindowExtEx = (const EMRSCALEWINDOWEXTEX *)mr;
1503c2c66affSColin Finck 
1504c2c66affSColin Finck         if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1505c2c66affSColin Finck 	    break;
1506c2c66affSColin Finck         if (!lpScaleWindowExtEx->xNum || !lpScaleWindowExtEx->xDenom ||
1507c2c66affSColin Finck             !lpScaleWindowExtEx->yNum || !lpScaleWindowExtEx->yDenom)
1508c2c66affSColin Finck             break;
1509c2c66affSColin Finck         info->state.wndExtX = MulDiv(info->state.wndExtX, lpScaleWindowExtEx->xNum,
1510c2c66affSColin Finck                                lpScaleWindowExtEx->xDenom);
1511c2c66affSColin Finck         info->state.wndExtY = MulDiv(info->state.wndExtY, lpScaleWindowExtEx->yNum,
1512c2c66affSColin Finck                                lpScaleWindowExtEx->yDenom);
1513c2c66affSColin Finck         if (info->state.wndExtX == 0) info->state.wndExtX = 1;
1514c2c66affSColin Finck         if (info->state.wndExtY == 0) info->state.wndExtY = 1;
1515c2c66affSColin Finck         if (info->state.mode == MM_ISOTROPIC)
1516c2c66affSColin Finck             EMF_FixIsotropic(hdc, info);
1517c2c66affSColin Finck 
1518c2c66affSColin Finck         TRACE("EMRSCALEWINDOWEXTEX %d/%d %d/%d\n",
1519c2c66affSColin Finck              lpScaleWindowExtEx->xNum,lpScaleWindowExtEx->xDenom,
1520c2c66affSColin Finck              lpScaleWindowExtEx->yNum,lpScaleWindowExtEx->yDenom);
1521c2c66affSColin Finck 
1522c2c66affSColin Finck         if (!IS_WIN9X())
1523c2c66affSColin Finck             EMF_Update_MF_Xform(hdc, info);
1524c2c66affSColin Finck 
1525c2c66affSColin Finck         break;
1526c2c66affSColin Finck       }
1527c2c66affSColin Finck 
1528c2c66affSColin Finck     case EMR_MODIFYWORLDTRANSFORM:
1529c2c66affSColin Finck       {
1530c2c66affSColin Finck         const EMRMODIFYWORLDTRANSFORM *lpModifyWorldTrans = (const EMRMODIFYWORLDTRANSFORM *)mr;
1531c2c66affSColin Finck 
1532c2c66affSColin Finck         switch(lpModifyWorldTrans->iMode) {
1533c2c66affSColin Finck         case MWT_IDENTITY:
1534c2c66affSColin Finck             info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
1535c2c66affSColin Finck             info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
1536c2c66affSColin Finck             info->state.world_transform.eDx  = info->state.world_transform.eDy  = 0;
1537c2c66affSColin Finck             if (!IS_WIN9X())
1538c2c66affSColin Finck                 EMF_Update_MF_Xform(hdc, info);
1539c2c66affSColin Finck             break;
1540c2c66affSColin Finck         case MWT_LEFTMULTIPLY:
1541c2c66affSColin Finck             CombineTransform(&info->state.world_transform, &lpModifyWorldTrans->xform,
1542c2c66affSColin Finck                              &info->state.world_transform);
1543c2c66affSColin Finck             if (!IS_WIN9X())
1544c2c66affSColin Finck                 ModifyWorldTransform(hdc, &lpModifyWorldTrans->xform, MWT_LEFTMULTIPLY);
1545c2c66affSColin Finck             break;
1546c2c66affSColin Finck         case MWT_RIGHTMULTIPLY:
1547c2c66affSColin Finck             CombineTransform(&info->state.world_transform, &info->state.world_transform,
1548c2c66affSColin Finck                              &lpModifyWorldTrans->xform);
1549c2c66affSColin Finck             if (!IS_WIN9X())
1550c2c66affSColin Finck                 EMF_Update_MF_Xform(hdc, info);
1551c2c66affSColin Finck             break;
1552c2c66affSColin Finck         default:
1553c2c66affSColin Finck             FIXME("Unknown imode %d\n", lpModifyWorldTrans->iMode);
1554c2c66affSColin Finck             break;
1555c2c66affSColin Finck         }
1556c2c66affSColin Finck         break;
1557c2c66affSColin Finck       }
1558c2c66affSColin Finck 
1559c2c66affSColin Finck     case EMR_ANGLEARC:
1560c2c66affSColin Finck       {
1561c2c66affSColin Finck         const EMRANGLEARC *lpAngleArc = (const EMRANGLEARC *)mr;
1562c2c66affSColin Finck 
1563c2c66affSColin Finck         AngleArc( hdc,
1564c2c66affSColin Finck                  (INT)lpAngleArc->ptlCenter.x, (INT)lpAngleArc->ptlCenter.y,
1565c2c66affSColin Finck                  lpAngleArc->nRadius, lpAngleArc->eStartAngle,
1566c2c66affSColin Finck                  lpAngleArc->eSweepAngle );
1567c2c66affSColin Finck 
1568c2c66affSColin Finck         break;
1569c2c66affSColin Finck       }
1570c2c66affSColin Finck 
1571c2c66affSColin Finck     case EMR_ROUNDRECT:
1572c2c66affSColin Finck       {
1573c2c66affSColin Finck         const EMRROUNDRECT *lpRoundRect = (const EMRROUNDRECT *)mr;
1574c2c66affSColin Finck 
1575c2c66affSColin Finck         RoundRect( hdc,
1576c2c66affSColin Finck                    lpRoundRect->rclBox.left,
1577c2c66affSColin Finck                    lpRoundRect->rclBox.top,
1578c2c66affSColin Finck                    lpRoundRect->rclBox.right,
1579c2c66affSColin Finck                    lpRoundRect->rclBox.bottom,
1580c2c66affSColin Finck                    lpRoundRect->szlCorner.cx,
1581c2c66affSColin Finck                    lpRoundRect->szlCorner.cy );
1582c2c66affSColin Finck 
1583c2c66affSColin Finck         break;
1584c2c66affSColin Finck       }
1585c2c66affSColin Finck 
1586c2c66affSColin Finck     case EMR_ARC:
1587c2c66affSColin Finck       {
1588c2c66affSColin Finck         const EMRARC *lpArc = (const EMRARC *)mr;
1589c2c66affSColin Finck 
1590c2c66affSColin Finck         Arc( hdc,
1591c2c66affSColin Finck              (INT)lpArc->rclBox.left,
1592c2c66affSColin Finck              (INT)lpArc->rclBox.top,
1593c2c66affSColin Finck              (INT)lpArc->rclBox.right,
1594c2c66affSColin Finck              (INT)lpArc->rclBox.bottom,
1595c2c66affSColin Finck              (INT)lpArc->ptlStart.x,
1596c2c66affSColin Finck              (INT)lpArc->ptlStart.y,
1597c2c66affSColin Finck              (INT)lpArc->ptlEnd.x,
1598c2c66affSColin Finck              (INT)lpArc->ptlEnd.y );
1599c2c66affSColin Finck 
1600c2c66affSColin Finck         break;
1601c2c66affSColin Finck       }
1602c2c66affSColin Finck 
1603c2c66affSColin Finck     case EMR_CHORD:
1604c2c66affSColin Finck       {
1605c2c66affSColin Finck         const EMRCHORD *lpChord = (const EMRCHORD *)mr;
1606c2c66affSColin Finck 
1607c2c66affSColin Finck         Chord( hdc,
1608c2c66affSColin Finck              (INT)lpChord->rclBox.left,
1609c2c66affSColin Finck              (INT)lpChord->rclBox.top,
1610c2c66affSColin Finck              (INT)lpChord->rclBox.right,
1611c2c66affSColin Finck              (INT)lpChord->rclBox.bottom,
1612c2c66affSColin Finck              (INT)lpChord->ptlStart.x,
1613c2c66affSColin Finck              (INT)lpChord->ptlStart.y,
1614c2c66affSColin Finck              (INT)lpChord->ptlEnd.x,
1615c2c66affSColin Finck              (INT)lpChord->ptlEnd.y );
1616c2c66affSColin Finck 
1617c2c66affSColin Finck         break;
1618c2c66affSColin Finck       }
1619c2c66affSColin Finck 
1620c2c66affSColin Finck     case EMR_PIE:
1621c2c66affSColin Finck       {
1622c2c66affSColin Finck         const EMRPIE *lpPie = (const EMRPIE *)mr;
1623c2c66affSColin Finck 
1624c2c66affSColin Finck         Pie( hdc,
1625c2c66affSColin Finck              (INT)lpPie->rclBox.left,
1626c2c66affSColin Finck              (INT)lpPie->rclBox.top,
1627c2c66affSColin Finck              (INT)lpPie->rclBox.right,
1628c2c66affSColin Finck              (INT)lpPie->rclBox.bottom,
1629c2c66affSColin Finck              (INT)lpPie->ptlStart.x,
1630c2c66affSColin Finck              (INT)lpPie->ptlStart.y,
1631c2c66affSColin Finck              (INT)lpPie->ptlEnd.x,
1632c2c66affSColin Finck              (INT)lpPie->ptlEnd.y );
1633c2c66affSColin Finck 
1634c2c66affSColin Finck        break;
1635c2c66affSColin Finck       }
1636c2c66affSColin Finck 
1637c2c66affSColin Finck     case EMR_ARCTO:
1638c2c66affSColin Finck       {
1639c2c66affSColin Finck         const EMRARC *lpArcTo = (const EMRARC *)mr;
1640c2c66affSColin Finck 
1641c2c66affSColin Finck         ArcTo( hdc,
1642c2c66affSColin Finck                (INT)lpArcTo->rclBox.left,
1643c2c66affSColin Finck                (INT)lpArcTo->rclBox.top,
1644c2c66affSColin Finck                (INT)lpArcTo->rclBox.right,
1645c2c66affSColin Finck                (INT)lpArcTo->rclBox.bottom,
1646c2c66affSColin Finck                (INT)lpArcTo->ptlStart.x,
1647c2c66affSColin Finck                (INT)lpArcTo->ptlStart.y,
1648c2c66affSColin Finck                (INT)lpArcTo->ptlEnd.x,
1649c2c66affSColin Finck                (INT)lpArcTo->ptlEnd.y );
1650c2c66affSColin Finck 
1651c2c66affSColin Finck         break;
1652c2c66affSColin Finck       }
1653c2c66affSColin Finck 
1654c2c66affSColin Finck     case EMR_EXTFLOODFILL:
1655c2c66affSColin Finck       {
1656c2c66affSColin Finck         const EMREXTFLOODFILL *lpExtFloodFill = (const EMREXTFLOODFILL *)mr;
1657c2c66affSColin Finck 
1658c2c66affSColin Finck         ExtFloodFill( hdc,
1659c2c66affSColin Finck                       (INT)lpExtFloodFill->ptlStart.x,
1660c2c66affSColin Finck                       (INT)lpExtFloodFill->ptlStart.y,
1661c2c66affSColin Finck                       lpExtFloodFill->crColor,
1662c2c66affSColin Finck                       (UINT)lpExtFloodFill->iMode );
1663c2c66affSColin Finck 
1664c2c66affSColin Finck         break;
1665c2c66affSColin Finck       }
1666c2c66affSColin Finck 
1667c2c66affSColin Finck     case EMR_POLYDRAW:
1668c2c66affSColin Finck       {
1669c2c66affSColin Finck         const EMRPOLYDRAW *lpPolyDraw = (const EMRPOLYDRAW *)mr;
1670c2c66affSColin Finck         PolyDraw( hdc,
1671c2c66affSColin Finck                   (const POINT*)lpPolyDraw->aptl,
1672c2c66affSColin Finck                   lpPolyDraw->abTypes,
1673c2c66affSColin Finck                   (INT)lpPolyDraw->cptl );
1674c2c66affSColin Finck 
1675c2c66affSColin Finck         break;
1676c2c66affSColin Finck       }
1677c2c66affSColin Finck 
1678c2c66affSColin Finck     case EMR_SETARCDIRECTION:
1679c2c66affSColin Finck       {
1680c2c66affSColin Finck         const EMRSETARCDIRECTION *lpSetArcDirection = (const EMRSETARCDIRECTION *)mr;
1681c2c66affSColin Finck         SetArcDirection( hdc, (INT)lpSetArcDirection->iArcDirection );
1682c2c66affSColin Finck         break;
1683c2c66affSColin Finck       }
1684c2c66affSColin Finck 
1685c2c66affSColin Finck     case EMR_SETMITERLIMIT:
1686c2c66affSColin Finck       {
1687c2c66affSColin Finck         const EMRSETMITERLIMIT *lpSetMiterLimit = (const EMRSETMITERLIMIT *)mr;
1688c2c66affSColin Finck         SetMiterLimit( hdc, lpSetMiterLimit->eMiterLimit, NULL );
1689c2c66affSColin Finck         break;
1690c2c66affSColin Finck       }
1691c2c66affSColin Finck 
1692c2c66affSColin Finck     case EMR_BEGINPATH:
1693c2c66affSColin Finck       {
1694c2c66affSColin Finck         BeginPath( hdc );
1695c2c66affSColin Finck         break;
1696c2c66affSColin Finck       }
1697c2c66affSColin Finck 
1698c2c66affSColin Finck     case EMR_ENDPATH:
1699c2c66affSColin Finck       {
1700c2c66affSColin Finck         EndPath( hdc );
1701c2c66affSColin Finck         break;
1702c2c66affSColin Finck       }
1703c2c66affSColin Finck 
1704c2c66affSColin Finck     case EMR_CLOSEFIGURE:
1705c2c66affSColin Finck       {
1706c2c66affSColin Finck         CloseFigure( hdc );
1707c2c66affSColin Finck         break;
1708c2c66affSColin Finck       }
1709c2c66affSColin Finck 
1710c2c66affSColin Finck     case EMR_FILLPATH:
1711c2c66affSColin Finck       {
1712c2c66affSColin Finck         /*const EMRFILLPATH lpFillPath = (const EMRFILLPATH *)mr;*/
1713c2c66affSColin Finck         FillPath( hdc );
1714c2c66affSColin Finck         break;
1715c2c66affSColin Finck       }
1716c2c66affSColin Finck 
1717c2c66affSColin Finck     case EMR_STROKEANDFILLPATH:
1718c2c66affSColin Finck       {
1719c2c66affSColin Finck         /*const EMRSTROKEANDFILLPATH lpStrokeAndFillPath = (const EMRSTROKEANDFILLPATH *)mr;*/
1720c2c66affSColin Finck         StrokeAndFillPath( hdc );
1721c2c66affSColin Finck         break;
1722c2c66affSColin Finck       }
1723c2c66affSColin Finck 
1724c2c66affSColin Finck     case EMR_STROKEPATH:
1725c2c66affSColin Finck       {
1726c2c66affSColin Finck         /*const EMRSTROKEPATH lpStrokePath = (const EMRSTROKEPATH *)mr;*/
1727c2c66affSColin Finck         StrokePath( hdc );
1728c2c66affSColin Finck         break;
1729c2c66affSColin Finck       }
1730c2c66affSColin Finck 
1731c2c66affSColin Finck     case EMR_FLATTENPATH:
1732c2c66affSColin Finck       {
1733c2c66affSColin Finck         FlattenPath( hdc );
1734c2c66affSColin Finck         break;
1735c2c66affSColin Finck       }
1736c2c66affSColin Finck 
1737c2c66affSColin Finck     case EMR_WIDENPATH:
1738c2c66affSColin Finck       {
1739c2c66affSColin Finck         WidenPath( hdc );
1740c2c66affSColin Finck         break;
1741c2c66affSColin Finck       }
1742c2c66affSColin Finck 
1743c2c66affSColin Finck     case EMR_SELECTCLIPPATH:
1744c2c66affSColin Finck       {
1745c2c66affSColin Finck         const EMRSELECTCLIPPATH *lpSelectClipPath = (const EMRSELECTCLIPPATH *)mr;
1746c2c66affSColin Finck         SelectClipPath( hdc, (INT)lpSelectClipPath->iMode );
1747c2c66affSColin Finck         break;
1748c2c66affSColin Finck       }
1749c2c66affSColin Finck 
1750c2c66affSColin Finck     case EMR_ABORTPATH:
1751c2c66affSColin Finck       {
1752c2c66affSColin Finck         AbortPath( hdc );
1753c2c66affSColin Finck         break;
1754c2c66affSColin Finck       }
1755c2c66affSColin Finck 
1756c2c66affSColin Finck     case EMR_CREATECOLORSPACE:
1757c2c66affSColin Finck       {
1758c2c66affSColin Finck         PEMRCREATECOLORSPACE lpCreateColorSpace = (PEMRCREATECOLORSPACE)mr;
1759c2c66affSColin Finck         (handletable->objectHandle)[lpCreateColorSpace->ihCS] =
1760c2c66affSColin Finck            CreateColorSpaceA( &lpCreateColorSpace->lcs );
1761c2c66affSColin Finck         break;
1762c2c66affSColin Finck       }
1763c2c66affSColin Finck 
1764c2c66affSColin Finck     case EMR_SETCOLORSPACE:
1765c2c66affSColin Finck       {
1766c2c66affSColin Finck         const EMRSETCOLORSPACE *lpSetColorSpace = (const EMRSETCOLORSPACE *)mr;
1767c2c66affSColin Finck         SetColorSpace( hdc,
1768c2c66affSColin Finck                        (handletable->objectHandle)[lpSetColorSpace->ihCS] );
1769c2c66affSColin Finck         break;
1770c2c66affSColin Finck       }
1771c2c66affSColin Finck 
1772c2c66affSColin Finck     case EMR_DELETECOLORSPACE:
1773c2c66affSColin Finck       {
1774c2c66affSColin Finck         const EMRDELETECOLORSPACE *lpDeleteColorSpace = (const EMRDELETECOLORSPACE *)mr;
1775c2c66affSColin Finck         DeleteColorSpace( (handletable->objectHandle)[lpDeleteColorSpace->ihCS] );
1776c2c66affSColin Finck         break;
1777c2c66affSColin Finck       }
1778c2c66affSColin Finck 
1779c2c66affSColin Finck     case EMR_SETICMMODE:
1780c2c66affSColin Finck       {
1781c2c66affSColin Finck         const EMRSETICMMODE *lpSetICMMode = (const EMRSETICMMODE *)mr;
1782c2c66affSColin Finck         SetICMMode( hdc, (INT)lpSetICMMode->iMode );
1783c2c66affSColin Finck         break;
1784c2c66affSColin Finck       }
1785c2c66affSColin Finck 
1786c2c66affSColin Finck     case EMR_PIXELFORMAT:
1787c2c66affSColin Finck       {
1788c2c66affSColin Finck         INT iPixelFormat;
1789c2c66affSColin Finck         const EMRPIXELFORMAT *lpPixelFormat = (const EMRPIXELFORMAT *)mr;
1790c2c66affSColin Finck 
1791c2c66affSColin Finck         iPixelFormat = ChoosePixelFormat( hdc, &lpPixelFormat->pfd );
1792c2c66affSColin Finck         SetPixelFormat( hdc, iPixelFormat, &lpPixelFormat->pfd );
1793c2c66affSColin Finck 
1794c2c66affSColin Finck         break;
1795c2c66affSColin Finck       }
1796c2c66affSColin Finck 
1797c2c66affSColin Finck     case EMR_SETPALETTEENTRIES:
1798c2c66affSColin Finck       {
1799c2c66affSColin Finck         const EMRSETPALETTEENTRIES *lpSetPaletteEntries = (const EMRSETPALETTEENTRIES *)mr;
1800c2c66affSColin Finck 
1801c2c66affSColin Finck         SetPaletteEntries( (handletable->objectHandle)[lpSetPaletteEntries->ihPal],
1802c2c66affSColin Finck                            (UINT)lpSetPaletteEntries->iStart,
1803c2c66affSColin Finck                            (UINT)lpSetPaletteEntries->cEntries,
1804c2c66affSColin Finck                            lpSetPaletteEntries->aPalEntries );
1805c2c66affSColin Finck 
1806c2c66affSColin Finck         break;
1807c2c66affSColin Finck       }
1808c2c66affSColin Finck 
1809c2c66affSColin Finck     case EMR_RESIZEPALETTE:
1810c2c66affSColin Finck       {
1811c2c66affSColin Finck         const EMRRESIZEPALETTE *lpResizePalette = (const EMRRESIZEPALETTE *)mr;
1812c2c66affSColin Finck 
1813c2c66affSColin Finck         ResizePalette( (handletable->objectHandle)[lpResizePalette->ihPal],
1814c2c66affSColin Finck                        (UINT)lpResizePalette->cEntries );
1815c2c66affSColin Finck 
1816c2c66affSColin Finck         break;
1817c2c66affSColin Finck       }
1818c2c66affSColin Finck 
1819c2c66affSColin Finck     case EMR_CREATEDIBPATTERNBRUSHPT:
1820c2c66affSColin Finck       {
1821c2c66affSColin Finck         const EMRCREATEDIBPATTERNBRUSHPT *lpCreate = (const EMRCREATEDIBPATTERNBRUSHPT *)mr;
1822c2c66affSColin Finck         LPVOID lpPackedStruct;
1823c2c66affSColin Finck 
1824c2c66affSColin Finck         /* Check that offsets and data are contained within the record
1825c2c66affSColin Finck          * (including checking for wrap-arounds).
1826c2c66affSColin Finck          */
1827c2c66affSColin Finck         if (    lpCreate->offBmi  + lpCreate->cbBmi  > mr->nSize
1828c2c66affSColin Finck              || lpCreate->offBits + lpCreate->cbBits > mr->nSize
1829c2c66affSColin Finck              || lpCreate->offBmi  + lpCreate->cbBmi  < lpCreate->offBmi
1830c2c66affSColin Finck              || lpCreate->offBits + lpCreate->cbBits < lpCreate->offBits )
1831c2c66affSColin Finck         {
1832c2c66affSColin Finck             ERR("Invalid EMR_CREATEDIBPATTERNBRUSHPT record\n");
1833c2c66affSColin Finck             break;
1834c2c66affSColin Finck         }
1835c2c66affSColin Finck 
1836c2c66affSColin Finck         /* This is a BITMAPINFO struct followed directly by bitmap bits */
1837c2c66affSColin Finck         lpPackedStruct = HeapAlloc( GetProcessHeap(), 0,
1838c2c66affSColin Finck                                     lpCreate->cbBmi + lpCreate->cbBits );
1839c2c66affSColin Finck         if(!lpPackedStruct)
1840c2c66affSColin Finck         {
1841c2c66affSColin Finck             SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1842c2c66affSColin Finck             break;
1843c2c66affSColin Finck         }
1844c2c66affSColin Finck 
1845c2c66affSColin Finck         /* Now pack this structure */
1846c2c66affSColin Finck         memcpy( lpPackedStruct,
1847c2c66affSColin Finck                 ((const BYTE *)lpCreate) + lpCreate->offBmi,
1848c2c66affSColin Finck                 lpCreate->cbBmi );
1849c2c66affSColin Finck         memcpy( ((BYTE*)lpPackedStruct) + lpCreate->cbBmi,
1850c2c66affSColin Finck                 ((const BYTE *)lpCreate) + lpCreate->offBits,
1851c2c66affSColin Finck                 lpCreate->cbBits );
1852c2c66affSColin Finck 
1853c2c66affSColin Finck         (handletable->objectHandle)[lpCreate->ihBrush] =
1854c2c66affSColin Finck            CreateDIBPatternBrushPt( lpPackedStruct,
1855c2c66affSColin Finck                                     (UINT)lpCreate->iUsage );
1856c2c66affSColin Finck 
1857c2c66affSColin Finck         HeapFree(GetProcessHeap(), 0, lpPackedStruct);
1858c2c66affSColin Finck         break;
1859c2c66affSColin Finck       }
1860c2c66affSColin Finck 
1861c2c66affSColin Finck     case EMR_CREATEMONOBRUSH:
1862c2c66affSColin Finck     {
1863c2c66affSColin Finck         const EMRCREATEMONOBRUSH *pCreateMonoBrush = (const EMRCREATEMONOBRUSH *)mr;
1864c2c66affSColin Finck         const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pCreateMonoBrush->offBmi);
1865c2c66affSColin Finck         HBITMAP hBmp;
1866c2c66affSColin Finck 
1867c2c66affSColin Finck         /* Need to check if the bitmap is monochrome, and if the
1868c2c66affSColin Finck            two colors are really black and white */
1869c2c66affSColin Finck         if (pCreateMonoBrush->iUsage == DIB_PAL_MONO)
1870c2c66affSColin Finck         {
1871c2c66affSColin Finck             BITMAP bm;
1872c2c66affSColin Finck 
1873c2c66affSColin Finck             /* Undocumented iUsage indicates a mono bitmap with no palette table,
1874c2c66affSColin Finck              * aligned to 32 rather than 16 bits.
1875c2c66affSColin Finck              */
1876c2c66affSColin Finck             bm.bmType = 0;
1877c2c66affSColin Finck             bm.bmWidth = pbi->bmiHeader.biWidth;
1878c2c66affSColin Finck             bm.bmHeight = abs(pbi->bmiHeader.biHeight);
1879c2c66affSColin Finck             bm.bmWidthBytes = 4 * ((pbi->bmiHeader.biWidth + 31) / 32);
1880c2c66affSColin Finck             bm.bmPlanes = pbi->bmiHeader.biPlanes;
1881c2c66affSColin Finck             bm.bmBitsPixel = pbi->bmiHeader.biBitCount;
1882c2c66affSColin Finck             bm.bmBits = (BYTE *)mr + pCreateMonoBrush->offBits;
1883c2c66affSColin Finck             hBmp = CreateBitmapIndirect(&bm);
1884c2c66affSColin Finck         }
1885c2c66affSColin Finck         else if (is_dib_monochrome(pbi))
1886c2c66affSColin Finck         {
1887c2c66affSColin Finck           /* Top-down DIBs have a negative height */
1888c2c66affSColin Finck           LONG height = pbi->bmiHeader.biHeight;
1889c2c66affSColin Finck 
1890c2c66affSColin Finck           hBmp = CreateBitmap(pbi->bmiHeader.biWidth, abs(height), 1, 1, NULL);
1891c2c66affSColin Finck           SetDIBits(hdc, hBmp, 0, pbi->bmiHeader.biHeight,
1892c2c66affSColin Finck               (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1893c2c66affSColin Finck         }
1894c2c66affSColin Finck         else
1895c2c66affSColin Finck         {
1896c2c66affSColin Finck             hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1897c2c66affSColin Finck               (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1898c2c66affSColin Finck         }
1899c2c66affSColin Finck 
1900c2c66affSColin Finck 	(handletable->objectHandle)[pCreateMonoBrush->ihBrush] = CreatePatternBrush(hBmp);
1901c2c66affSColin Finck 
1902c2c66affSColin Finck 	/* CreatePatternBrush created a copy of the bitmap */
1903c2c66affSColin Finck 	DeleteObject(hBmp);
1904c2c66affSColin Finck 	break;
1905c2c66affSColin Finck     }
1906c2c66affSColin Finck 
1907c2c66affSColin Finck     case EMR_BITBLT:
1908c2c66affSColin Finck     {
1909c2c66affSColin Finck 	const EMRBITBLT *pBitBlt = (const EMRBITBLT *)mr;
1910c2c66affSColin Finck 
1911c2c66affSColin Finck         if(pBitBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1912c2c66affSColin Finck             PatBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1913c2c66affSColin Finck                    pBitBlt->dwRop);
1914c2c66affSColin Finck         } else { /* BitBlt */
1915c2c66affSColin Finck             HDC hdcSrc = CreateCompatibleDC(hdc);
1916c2c66affSColin Finck             HBRUSH hBrush, hBrushOld;
1917c2c66affSColin Finck             HBITMAP hBmp = 0, hBmpOld = 0;
1918c2c66affSColin Finck             const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pBitBlt->offBmiSrc);
1919c2c66affSColin Finck 
1920c2c66affSColin Finck             SetGraphicsMode(hdcSrc, GM_ADVANCED);
1921c2c66affSColin Finck             SetWorldTransform(hdcSrc, &pBitBlt->xformSrc);
1922c2c66affSColin Finck 
1923c2c66affSColin Finck             hBrush = CreateSolidBrush(pBitBlt->crBkColorSrc);
1924c2c66affSColin Finck             hBrushOld = SelectObject(hdcSrc, hBrush);
1925c2c66affSColin Finck             PatBlt(hdcSrc, pBitBlt->rclBounds.left, pBitBlt->rclBounds.top,
1926c2c66affSColin Finck                    pBitBlt->rclBounds.right - pBitBlt->rclBounds.left,
1927c2c66affSColin Finck                    pBitBlt->rclBounds.bottom - pBitBlt->rclBounds.top, PATCOPY);
1928c2c66affSColin Finck             SelectObject(hdcSrc, hBrushOld);
1929c2c66affSColin Finck             DeleteObject(hBrush);
1930c2c66affSColin Finck 
1931c2c66affSColin Finck             hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1932c2c66affSColin Finck                                   (const BYTE *)mr + pBitBlt->offBitsSrc, pbi, pBitBlt->iUsageSrc);
1933c2c66affSColin Finck             hBmpOld = SelectObject(hdcSrc, hBmp);
1934c2c66affSColin Finck 
1935c2c66affSColin Finck             BitBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1936c2c66affSColin Finck                    hdcSrc, pBitBlt->xSrc, pBitBlt->ySrc, pBitBlt->dwRop);
1937c2c66affSColin Finck 
1938c2c66affSColin Finck             SelectObject(hdcSrc, hBmpOld);
1939c2c66affSColin Finck             DeleteObject(hBmp);
1940c2c66affSColin Finck             DeleteDC(hdcSrc);
1941c2c66affSColin Finck         }
1942c2c66affSColin Finck 	break;
1943c2c66affSColin Finck     }
1944c2c66affSColin Finck 
1945c2c66affSColin Finck     case EMR_STRETCHBLT:
1946c2c66affSColin Finck     {
1947c2c66affSColin Finck 	const EMRSTRETCHBLT *pStretchBlt = (const EMRSTRETCHBLT *)mr;
1948c2c66affSColin Finck 
1949c2c66affSColin Finck         TRACE("EMR_STRETCHBLT: %d, %d %dx%d -> %d, %d %dx%d. rop %08x offBitsSrc %d\n",
1950c2c66affSColin Finck 	       pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1951c2c66affSColin Finck 	       pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1952c2c66affSColin Finck 	       pStretchBlt->dwRop, pStretchBlt->offBitsSrc);
1953c2c66affSColin Finck 
1954c2c66affSColin Finck         if(pStretchBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1955c2c66affSColin Finck             PatBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1956c2c66affSColin Finck                    pStretchBlt->dwRop);
1957c2c66affSColin Finck         } else { /* StretchBlt */
1958c2c66affSColin Finck             HDC hdcSrc = CreateCompatibleDC(hdc);
1959c2c66affSColin Finck             HBRUSH hBrush, hBrushOld;
1960c2c66affSColin Finck             HBITMAP hBmp = 0, hBmpOld = 0;
1961c2c66affSColin Finck             const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pStretchBlt->offBmiSrc);
1962c2c66affSColin Finck 
1963c2c66affSColin Finck             SetGraphicsMode(hdcSrc, GM_ADVANCED);
1964c2c66affSColin Finck             SetWorldTransform(hdcSrc, &pStretchBlt->xformSrc);
1965c2c66affSColin Finck 
1966c2c66affSColin Finck             hBrush = CreateSolidBrush(pStretchBlt->crBkColorSrc);
1967c2c66affSColin Finck             hBrushOld = SelectObject(hdcSrc, hBrush);
1968c2c66affSColin Finck             PatBlt(hdcSrc, pStretchBlt->rclBounds.left, pStretchBlt->rclBounds.top,
1969c2c66affSColin Finck                    pStretchBlt->rclBounds.right - pStretchBlt->rclBounds.left,
1970c2c66affSColin Finck                    pStretchBlt->rclBounds.bottom - pStretchBlt->rclBounds.top, PATCOPY);
1971c2c66affSColin Finck             SelectObject(hdcSrc, hBrushOld);
1972c2c66affSColin Finck             DeleteObject(hBrush);
1973c2c66affSColin Finck 
1974c2c66affSColin Finck             hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1975c2c66affSColin Finck                                   (const BYTE *)mr + pStretchBlt->offBitsSrc, pbi, pStretchBlt->iUsageSrc);
1976c2c66affSColin Finck             hBmpOld = SelectObject(hdcSrc, hBmp);
1977c2c66affSColin Finck 
1978c2c66affSColin Finck             StretchBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1979c2c66affSColin Finck                        hdcSrc, pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1980c2c66affSColin Finck                        pStretchBlt->dwRop);
1981c2c66affSColin Finck 
1982c2c66affSColin Finck             SelectObject(hdcSrc, hBmpOld);
1983c2c66affSColin Finck             DeleteObject(hBmp);
1984c2c66affSColin Finck             DeleteDC(hdcSrc);
1985c2c66affSColin Finck         }
1986c2c66affSColin Finck 	break;
1987c2c66affSColin Finck     }
1988c2c66affSColin Finck 
1989c2c66affSColin Finck     case EMR_ALPHABLEND:
1990c2c66affSColin Finck     {
1991c2c66affSColin Finck 	const EMRALPHABLEND *pAlphaBlend = (const EMRALPHABLEND *)mr;
1992c2c66affSColin Finck 
1993c2c66affSColin Finck         TRACE("EMR_ALPHABLEND: %d, %d %dx%d -> %d, %d %dx%d. blendfn %08x offBitsSrc %d\n",
1994c2c66affSColin Finck 	       pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1995c2c66affSColin Finck 	       pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1996c2c66affSColin Finck 	       pAlphaBlend->dwRop, pAlphaBlend->offBitsSrc);
1997c2c66affSColin Finck 
1998c2c66affSColin Finck         if(pAlphaBlend->offBmiSrc == 0) {
1999c2c66affSColin Finck             FIXME("EMR_ALPHABLEND: offBmiSrc == 0\n");
2000c2c66affSColin Finck         } else {
2001c2c66affSColin Finck             HDC hdcSrc = CreateCompatibleDC(hdc);
2002c2c66affSColin Finck             HBITMAP hBmp = 0, hBmpOld = 0;
2003c2c66affSColin Finck             const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pAlphaBlend->offBmiSrc);
2004c2c66affSColin Finck             void *bits;
2005c2c66affSColin Finck 
2006c2c66affSColin Finck             SetGraphicsMode(hdcSrc, GM_ADVANCED);
2007c2c66affSColin Finck             SetWorldTransform(hdcSrc, &pAlphaBlend->xformSrc);
2008c2c66affSColin Finck 
2009c2c66affSColin Finck             hBmp = CreateDIBSection(hdc, pbi, pAlphaBlend->iUsageSrc, &bits, NULL, 0);
2010c2c66affSColin Finck             memcpy(bits, (const BYTE *)mr + pAlphaBlend->offBitsSrc, pAlphaBlend->cbBitsSrc);
2011c2c66affSColin Finck             hBmpOld = SelectObject(hdcSrc, hBmp);
2012c2c66affSColin Finck 
2013c2c66affSColin Finck             GdiAlphaBlend(hdc, pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
2014c2c66affSColin Finck                        hdcSrc, pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
2015c2c66affSColin Finck                           *(BLENDFUNCTION *)&pAlphaBlend->dwRop);
2016c2c66affSColin Finck 
2017c2c66affSColin Finck             SelectObject(hdcSrc, hBmpOld);
2018c2c66affSColin Finck             DeleteObject(hBmp);
2019c2c66affSColin Finck             DeleteDC(hdcSrc);
2020c2c66affSColin Finck         }
2021c2c66affSColin Finck 	break;
2022c2c66affSColin Finck     }
2023c2c66affSColin Finck 
2024c2c66affSColin Finck     case EMR_MASKBLT:
2025c2c66affSColin Finck     {
2026c2c66affSColin Finck 	const EMRMASKBLT *pMaskBlt = (const EMRMASKBLT *)mr;
2027c2c66affSColin Finck 	HDC hdcSrc = CreateCompatibleDC(hdc);
2028c2c66affSColin Finck 	HBRUSH hBrush, hBrushOld;
2029c2c66affSColin Finck 	HBITMAP hBmp, hBmpOld, hBmpMask;
2030c2c66affSColin Finck 	const BITMAPINFO *pbi;
2031c2c66affSColin Finck 
2032c2c66affSColin Finck         SetGraphicsMode(hdcSrc, GM_ADVANCED);
2033c2c66affSColin Finck 	SetWorldTransform(hdcSrc, &pMaskBlt->xformSrc);
2034c2c66affSColin Finck 
2035c2c66affSColin Finck 	hBrush = CreateSolidBrush(pMaskBlt->crBkColorSrc);
2036c2c66affSColin Finck 	hBrushOld = SelectObject(hdcSrc, hBrush);
2037c2c66affSColin Finck 	PatBlt(hdcSrc, pMaskBlt->rclBounds.left, pMaskBlt->rclBounds.top,
2038c2c66affSColin Finck 	       pMaskBlt->rclBounds.right - pMaskBlt->rclBounds.left,
2039c2c66affSColin Finck 	       pMaskBlt->rclBounds.bottom - pMaskBlt->rclBounds.top, PATCOPY);
2040c2c66affSColin Finck 	SelectObject(hdcSrc, hBrushOld);
2041c2c66affSColin Finck 	DeleteObject(hBrush);
2042c2c66affSColin Finck 
2043c2c66affSColin Finck 	pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiMask);
2044c2c66affSColin Finck 	hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
2045c2c66affSColin Finck 	             1, 1, NULL);
2046c2c66affSColin Finck 	SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
2047c2c66affSColin Finck 	  (const BYTE *)mr + pMaskBlt->offBitsMask, pbi, pMaskBlt->iUsageMask);
2048c2c66affSColin Finck 
2049c2c66affSColin Finck 	pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiSrc);
2050c2c66affSColin Finck 	hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
2051c2c66affSColin Finck 			      (const BYTE *)mr + pMaskBlt->offBitsSrc, pbi, pMaskBlt->iUsageSrc);
2052c2c66affSColin Finck 	hBmpOld = SelectObject(hdcSrc, hBmp);
2053c2c66affSColin Finck 	MaskBlt(hdc,
2054c2c66affSColin Finck 		pMaskBlt->xDest,
2055c2c66affSColin Finck 	        pMaskBlt->yDest,
2056c2c66affSColin Finck 	        pMaskBlt->cxDest,
2057c2c66affSColin Finck 	        pMaskBlt->cyDest,
2058c2c66affSColin Finck 	        hdcSrc,
2059c2c66affSColin Finck 	        pMaskBlt->xSrc,
2060c2c66affSColin Finck 	        pMaskBlt->ySrc,
2061c2c66affSColin Finck 	        hBmpMask,
2062c2c66affSColin Finck 		pMaskBlt->xMask,
2063c2c66affSColin Finck 		pMaskBlt->yMask,
2064c2c66affSColin Finck 	        pMaskBlt->dwRop);
2065c2c66affSColin Finck 	SelectObject(hdcSrc, hBmpOld);
2066c2c66affSColin Finck 	DeleteObject(hBmp);
2067c2c66affSColin Finck 	DeleteObject(hBmpMask);
2068c2c66affSColin Finck 	DeleteDC(hdcSrc);
2069c2c66affSColin Finck 	break;
2070c2c66affSColin Finck     }
2071c2c66affSColin Finck 
2072c2c66affSColin Finck     case EMR_PLGBLT:
2073c2c66affSColin Finck     {
2074c2c66affSColin Finck 	const EMRPLGBLT *pPlgBlt = (const EMRPLGBLT *)mr;
2075c2c66affSColin Finck 	HDC hdcSrc = CreateCompatibleDC(hdc);
2076c2c66affSColin Finck 	HBRUSH hBrush, hBrushOld;
2077c2c66affSColin Finck 	HBITMAP hBmp, hBmpOld, hBmpMask;
2078c2c66affSColin Finck 	const BITMAPINFO *pbi;
2079c2c66affSColin Finck 	POINT pts[3];
2080c2c66affSColin Finck 
2081c2c66affSColin Finck         SetGraphicsMode(hdcSrc, GM_ADVANCED);
2082c2c66affSColin Finck 	SetWorldTransform(hdcSrc, &pPlgBlt->xformSrc);
2083c2c66affSColin Finck 
2084c2c66affSColin Finck 	pts[0].x = pPlgBlt->aptlDest[0].x; pts[0].y = pPlgBlt->aptlDest[0].y;
2085c2c66affSColin Finck 	pts[1].x = pPlgBlt->aptlDest[1].x; pts[1].y = pPlgBlt->aptlDest[1].y;
2086c2c66affSColin Finck 	pts[2].x = pPlgBlt->aptlDest[2].x; pts[2].y = pPlgBlt->aptlDest[2].y;
2087c2c66affSColin Finck 
2088c2c66affSColin Finck 	hBrush = CreateSolidBrush(pPlgBlt->crBkColorSrc);
2089c2c66affSColin Finck 	hBrushOld = SelectObject(hdcSrc, hBrush);
2090c2c66affSColin Finck 	PatBlt(hdcSrc, pPlgBlt->rclBounds.left, pPlgBlt->rclBounds.top,
2091c2c66affSColin Finck 	       pPlgBlt->rclBounds.right - pPlgBlt->rclBounds.left,
2092c2c66affSColin Finck 	       pPlgBlt->rclBounds.bottom - pPlgBlt->rclBounds.top, PATCOPY);
2093c2c66affSColin Finck 	SelectObject(hdcSrc, hBrushOld);
2094c2c66affSColin Finck 	DeleteObject(hBrush);
2095c2c66affSColin Finck 
2096c2c66affSColin Finck 	pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiMask);
2097c2c66affSColin Finck 	hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
2098c2c66affSColin Finck 	             1, 1, NULL);
2099c2c66affSColin Finck 	SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
2100c2c66affSColin Finck 	  (const BYTE *)mr + pPlgBlt->offBitsMask, pbi, pPlgBlt->iUsageMask);
2101c2c66affSColin Finck 
2102c2c66affSColin Finck 	pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiSrc);
2103c2c66affSColin Finck 	hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
2104c2c66affSColin Finck 			      (const BYTE *)mr + pPlgBlt->offBitsSrc, pbi, pPlgBlt->iUsageSrc);
2105c2c66affSColin Finck 	hBmpOld = SelectObject(hdcSrc, hBmp);
2106c2c66affSColin Finck 	PlgBlt(hdc,
2107c2c66affSColin Finck 	       pts,
2108c2c66affSColin Finck 	       hdcSrc,
2109c2c66affSColin Finck 	       pPlgBlt->xSrc,
2110c2c66affSColin Finck 	       pPlgBlt->ySrc,
2111c2c66affSColin Finck 	       pPlgBlt->cxSrc,
2112c2c66affSColin Finck 	       pPlgBlt->cySrc,
2113c2c66affSColin Finck 	       hBmpMask,
2114c2c66affSColin Finck 	       pPlgBlt->xMask,
2115c2c66affSColin Finck 	       pPlgBlt->yMask);
2116c2c66affSColin Finck 	SelectObject(hdcSrc, hBmpOld);
2117c2c66affSColin Finck 	DeleteObject(hBmp);
2118c2c66affSColin Finck 	DeleteObject(hBmpMask);
2119c2c66affSColin Finck 	DeleteDC(hdcSrc);
2120c2c66affSColin Finck 	break;
2121c2c66affSColin Finck     }
2122c2c66affSColin Finck 
2123c2c66affSColin Finck     case EMR_SETDIBITSTODEVICE:
2124c2c66affSColin Finck     {
2125c2c66affSColin Finck 	const EMRSETDIBITSTODEVICE *pSetDIBitsToDevice = (const EMRSETDIBITSTODEVICE *)mr;
2126c2c66affSColin Finck 
2127c2c66affSColin Finck 	SetDIBitsToDevice(hdc,
2128c2c66affSColin Finck 			  pSetDIBitsToDevice->xDest,
2129c2c66affSColin Finck 			  pSetDIBitsToDevice->yDest,
2130c2c66affSColin Finck 			  pSetDIBitsToDevice->cxSrc,
2131c2c66affSColin Finck 			  pSetDIBitsToDevice->cySrc,
2132c2c66affSColin Finck 			  pSetDIBitsToDevice->xSrc,
2133c2c66affSColin Finck 			  pSetDIBitsToDevice->ySrc,
2134c2c66affSColin Finck 			  pSetDIBitsToDevice->iStartScan,
2135c2c66affSColin Finck 			  pSetDIBitsToDevice->cScans,
2136c2c66affSColin Finck 			  (const BYTE *)mr + pSetDIBitsToDevice->offBitsSrc,
2137c2c66affSColin Finck 			  (const BITMAPINFO *)((const BYTE *)mr + pSetDIBitsToDevice->offBmiSrc),
2138c2c66affSColin Finck 			  pSetDIBitsToDevice->iUsageSrc);
2139c2c66affSColin Finck 	break;
2140c2c66affSColin Finck     }
2141c2c66affSColin Finck 
2142c2c66affSColin Finck     case EMR_POLYTEXTOUTA:
2143c2c66affSColin Finck     {
2144c2c66affSColin Finck 	const EMRPOLYTEXTOUTA *pPolyTextOutA = (const EMRPOLYTEXTOUTA *)mr;
2145c2c66affSColin Finck 	POLYTEXTA *polytextA = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutA->cStrings * sizeof(POLYTEXTA));
2146c2c66affSColin Finck 	LONG i;
2147c2c66affSColin Finck 	XFORM xform, xformOld;
2148c2c66affSColin Finck 	int gModeOld;
2149c2c66affSColin Finck 
2150c2c66affSColin Finck 	gModeOld = SetGraphicsMode(hdc, pPolyTextOutA->iGraphicsMode);
2151c2c66affSColin Finck 	GetWorldTransform(hdc, &xformOld);
2152c2c66affSColin Finck 
2153c2c66affSColin Finck 	xform.eM11 = pPolyTextOutA->exScale;
2154c2c66affSColin Finck 	xform.eM12 = 0.0;
2155c2c66affSColin Finck 	xform.eM21 = 0.0;
2156c2c66affSColin Finck 	xform.eM22 = pPolyTextOutA->eyScale;
2157c2c66affSColin Finck 	xform.eDx = 0.0;
2158c2c66affSColin Finck 	xform.eDy = 0.0;
2159c2c66affSColin Finck 	SetWorldTransform(hdc, &xform);
2160c2c66affSColin Finck 
2161c2c66affSColin Finck 	/* Set up POLYTEXTA structures */
2162c2c66affSColin Finck 	for(i = 0; i < pPolyTextOutA->cStrings; i++)
2163c2c66affSColin Finck 	{
2164c2c66affSColin Finck 	    polytextA[i].x = pPolyTextOutA->aemrtext[i].ptlReference.x;
2165c2c66affSColin Finck 	    polytextA[i].y = pPolyTextOutA->aemrtext[i].ptlReference.y;
2166c2c66affSColin Finck 	    polytextA[i].n = pPolyTextOutA->aemrtext[i].nChars;
2167c2c66affSColin Finck 	    polytextA[i].lpstr = (LPCSTR)((const BYTE *)mr + pPolyTextOutA->aemrtext[i].offString);
2168c2c66affSColin Finck 	    polytextA[i].uiFlags = pPolyTextOutA->aemrtext[i].fOptions;
2169c2c66affSColin Finck 	    polytextA[i].rcl.left = pPolyTextOutA->aemrtext[i].rcl.left;
2170c2c66affSColin Finck 	    polytextA[i].rcl.right = pPolyTextOutA->aemrtext[i].rcl.right;
2171c2c66affSColin Finck 	    polytextA[i].rcl.top = pPolyTextOutA->aemrtext[i].rcl.top;
2172c2c66affSColin Finck 	    polytextA[i].rcl.bottom = pPolyTextOutA->aemrtext[i].rcl.bottom;
2173c2c66affSColin Finck 	    polytextA[i].pdx = (int *)((BYTE *)mr + pPolyTextOutA->aemrtext[i].offDx);
2174c2c66affSColin Finck 	}
2175c2c66affSColin Finck 	PolyTextOutA(hdc, polytextA, pPolyTextOutA->cStrings);
2176c2c66affSColin Finck 	HeapFree(GetProcessHeap(), 0, polytextA);
2177c2c66affSColin Finck 
2178c2c66affSColin Finck 	SetWorldTransform(hdc, &xformOld);
2179c2c66affSColin Finck 	SetGraphicsMode(hdc, gModeOld);
2180c2c66affSColin Finck 	break;
2181c2c66affSColin Finck     }
2182c2c66affSColin Finck 
2183c2c66affSColin Finck     case EMR_POLYTEXTOUTW:
2184c2c66affSColin Finck     {
2185c2c66affSColin Finck 	const EMRPOLYTEXTOUTW *pPolyTextOutW = (const EMRPOLYTEXTOUTW *)mr;
2186c2c66affSColin Finck 	POLYTEXTW *polytextW = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutW->cStrings * sizeof(POLYTEXTW));
2187c2c66affSColin Finck 	LONG i;
2188c2c66affSColin Finck 	XFORM xform, xformOld;
2189c2c66affSColin Finck 	int gModeOld;
2190c2c66affSColin Finck 
2191c2c66affSColin Finck 	gModeOld = SetGraphicsMode(hdc, pPolyTextOutW->iGraphicsMode);
2192c2c66affSColin Finck 	GetWorldTransform(hdc, &xformOld);
2193c2c66affSColin Finck 
2194c2c66affSColin Finck 	xform.eM11 = pPolyTextOutW->exScale;
2195c2c66affSColin Finck 	xform.eM12 = 0.0;
2196c2c66affSColin Finck 	xform.eM21 = 0.0;
2197c2c66affSColin Finck 	xform.eM22 = pPolyTextOutW->eyScale;
2198c2c66affSColin Finck 	xform.eDx = 0.0;
2199c2c66affSColin Finck 	xform.eDy = 0.0;
2200c2c66affSColin Finck 	SetWorldTransform(hdc, &xform);
2201c2c66affSColin Finck 
2202c2c66affSColin Finck 	/* Set up POLYTEXTW structures */
2203c2c66affSColin Finck 	for(i = 0; i < pPolyTextOutW->cStrings; i++)
2204c2c66affSColin Finck 	{
2205c2c66affSColin Finck 	    polytextW[i].x = pPolyTextOutW->aemrtext[i].ptlReference.x;
2206c2c66affSColin Finck 	    polytextW[i].y = pPolyTextOutW->aemrtext[i].ptlReference.y;
2207c2c66affSColin Finck 	    polytextW[i].n = pPolyTextOutW->aemrtext[i].nChars;
2208c2c66affSColin Finck 	    polytextW[i].lpstr = (LPCWSTR)((const BYTE *)mr + pPolyTextOutW->aemrtext[i].offString);
2209c2c66affSColin Finck 	    polytextW[i].uiFlags = pPolyTextOutW->aemrtext[i].fOptions;
2210c2c66affSColin Finck 	    polytextW[i].rcl.left = pPolyTextOutW->aemrtext[i].rcl.left;
2211c2c66affSColin Finck 	    polytextW[i].rcl.right = pPolyTextOutW->aemrtext[i].rcl.right;
2212c2c66affSColin Finck 	    polytextW[i].rcl.top = pPolyTextOutW->aemrtext[i].rcl.top;
2213c2c66affSColin Finck 	    polytextW[i].rcl.bottom = pPolyTextOutW->aemrtext[i].rcl.bottom;
2214c2c66affSColin Finck 	    polytextW[i].pdx = (int *)((BYTE *)mr + pPolyTextOutW->aemrtext[i].offDx);
2215c2c66affSColin Finck 	}
2216c2c66affSColin Finck 	PolyTextOutW(hdc, polytextW, pPolyTextOutW->cStrings);
2217c2c66affSColin Finck 	HeapFree(GetProcessHeap(), 0, polytextW);
2218c2c66affSColin Finck 
2219c2c66affSColin Finck 	SetWorldTransform(hdc, &xformOld);
2220c2c66affSColin Finck 	SetGraphicsMode(hdc, gModeOld);
2221c2c66affSColin Finck 	break;
2222c2c66affSColin Finck     }
2223c2c66affSColin Finck 
2224c2c66affSColin Finck     case EMR_FILLRGN:
2225c2c66affSColin Finck     {
2226c2c66affSColin Finck 	const EMRFILLRGN *pFillRgn = (const EMRFILLRGN *)mr;
2227c2c66affSColin Finck 	HRGN hRgn = ExtCreateRegion(NULL, pFillRgn->cbRgnData, (const RGNDATA *)pFillRgn->RgnData);
2228c2c66affSColin Finck 	FillRgn(hdc,
2229c2c66affSColin Finck 		hRgn,
2230c2c66affSColin Finck 		(handletable->objectHandle)[pFillRgn->ihBrush]);
2231c2c66affSColin Finck 	DeleteObject(hRgn);
2232c2c66affSColin Finck 	break;
2233c2c66affSColin Finck     }
2234c2c66affSColin Finck 
2235c2c66affSColin Finck     case EMR_FRAMERGN:
2236c2c66affSColin Finck     {
2237c2c66affSColin Finck 	const EMRFRAMERGN *pFrameRgn = (const EMRFRAMERGN *)mr;
2238c2c66affSColin Finck 	HRGN hRgn = ExtCreateRegion(NULL, pFrameRgn->cbRgnData, (const RGNDATA *)pFrameRgn->RgnData);
2239c2c66affSColin Finck 	FrameRgn(hdc,
2240c2c66affSColin Finck 		 hRgn,
2241c2c66affSColin Finck 		 (handletable->objectHandle)[pFrameRgn->ihBrush],
2242c2c66affSColin Finck 		 pFrameRgn->szlStroke.cx,
2243c2c66affSColin Finck 		 pFrameRgn->szlStroke.cy);
2244c2c66affSColin Finck 	DeleteObject(hRgn);
2245c2c66affSColin Finck 	break;
2246c2c66affSColin Finck     }
2247c2c66affSColin Finck 
2248c2c66affSColin Finck     case EMR_INVERTRGN:
2249c2c66affSColin Finck     {
2250c2c66affSColin Finck 	const EMRINVERTRGN *pInvertRgn = (const EMRINVERTRGN *)mr;
2251c2c66affSColin Finck 	HRGN hRgn = ExtCreateRegion(NULL, pInvertRgn->cbRgnData, (const RGNDATA *)pInvertRgn->RgnData);
2252c2c66affSColin Finck 	InvertRgn(hdc, hRgn);
2253c2c66affSColin Finck 	DeleteObject(hRgn);
2254c2c66affSColin Finck 	break;
2255c2c66affSColin Finck     }
2256c2c66affSColin Finck 
2257c2c66affSColin Finck     case EMR_PAINTRGN:
2258c2c66affSColin Finck     {
2259c2c66affSColin Finck 	const EMRPAINTRGN *pPaintRgn = (const EMRPAINTRGN *)mr;
2260c2c66affSColin Finck 	HRGN hRgn = ExtCreateRegion(NULL, pPaintRgn->cbRgnData, (const RGNDATA *)pPaintRgn->RgnData);
2261c2c66affSColin Finck 	PaintRgn(hdc, hRgn);
2262c2c66affSColin Finck 	DeleteObject(hRgn);
2263c2c66affSColin Finck 	break;
2264c2c66affSColin Finck     }
2265c2c66affSColin Finck 
2266c2c66affSColin Finck     case EMR_SETTEXTJUSTIFICATION:
2267c2c66affSColin Finck     {
2268c2c66affSColin Finck 	const EMRSETTEXTJUSTIFICATION *pSetTextJust = (const EMRSETTEXTJUSTIFICATION *)mr;
2269c2c66affSColin Finck 	SetTextJustification(hdc, pSetTextJust->nBreakExtra, pSetTextJust->nBreakCount);
2270c2c66affSColin Finck 	break;
2271c2c66affSColin Finck     }
2272c2c66affSColin Finck 
2273c2c66affSColin Finck     case EMR_SETLAYOUT:
2274c2c66affSColin Finck     {
2275c2c66affSColin Finck 	const EMRSETLAYOUT *pSetLayout = (const EMRSETLAYOUT *)mr;
2276c2c66affSColin Finck 	SetLayout(hdc, pSetLayout->iMode);
2277c2c66affSColin Finck 	break;
2278c2c66affSColin Finck     }
2279c2c66affSColin Finck 
2280c2c66affSColin Finck     case EMR_GRADIENTFILL:
2281c2c66affSColin Finck     {
2282c2c66affSColin Finck         EMRGRADIENTFILL *grad = (EMRGRADIENTFILL *)mr;
2283c2c66affSColin Finck         GdiGradientFill( hdc, grad->Ver, grad->nVer, grad->Ver + grad->nVer,
2284c2c66affSColin Finck                          grad->nTri, grad->ulMode );
2285c2c66affSColin Finck         break;
2286c2c66affSColin Finck     }
2287c2c66affSColin Finck 
2288*fc16259fSJames Tabor     case EMR_DRAWESCAPE:
2289*fc16259fSJames Tabor     {
2290*fc16259fSJames Tabor         PEMRESCAPE pemr = (PEMRESCAPE)mr;
2291*fc16259fSJames Tabor         DrawEscape( hdc, pemr->iEsc, pemr->cjIn, (LPCSTR)pemr->Data );
2292*fc16259fSJames Tabor         break;
2293*fc16259fSJames Tabor     }
2294*fc16259fSJames Tabor 
2295*fc16259fSJames Tabor     case EMR_EXTESCAPE:
2296*fc16259fSJames Tabor     {
2297*fc16259fSJames Tabor         PEMRESCAPE pemr = (PEMRESCAPE)mr;
2298*fc16259fSJames Tabor         ExtEscape( hdc, pemr->iEsc, pemr->cjIn, (LPCSTR)pemr->Data, 0, NULL );
2299*fc16259fSJames Tabor         break;
2300*fc16259fSJames Tabor     }
2301*fc16259fSJames Tabor 
2302*fc16259fSJames Tabor     case EMR_NAMEDESCAPE:
2303*fc16259fSJames Tabor     {
2304*fc16259fSJames Tabor         PEMRNAMEDESCAPE pemr = (PEMRNAMEDESCAPE)mr;
2305*fc16259fSJames Tabor         INT rounded_size = (pemr->cjIn+3) & ~3;
2306*fc16259fSJames Tabor         NamedEscape( hdc, (PWCHAR)&pemr->Data[rounded_size], pemr->iEsc, pemr->cjIn, (LPSTR)pemr->Data, 0, NULL );
2307*fc16259fSJames Tabor         break;
2308*fc16259fSJames Tabor     }
2309*fc16259fSJames Tabor 
2310c2c66affSColin Finck     case EMR_GLSRECORD:
2311c2c66affSColin Finck     case EMR_GLSBOUNDEDRECORD:
2312c2c66affSColin Finck     case EMR_STARTDOC:
2313c2c66affSColin Finck     case EMR_SMALLTEXTOUT:
2314c2c66affSColin Finck     case EMR_FORCEUFIMAPPING:
2315c2c66affSColin Finck     case EMR_COLORCORRECTPALETTE:
2316c2c66affSColin Finck     case EMR_SETICMPROFILEA:
2317c2c66affSColin Finck     case EMR_SETICMPROFILEW:
2318c2c66affSColin Finck     case EMR_TRANSPARENTBLT:
2319c2c66affSColin Finck     case EMR_SETLINKEDUFI:
2320c2c66affSColin Finck     case EMR_COLORMATCHTOTARGETW:
2321c2c66affSColin Finck     case EMR_CREATECOLORSPACEW:
2322c2c66affSColin Finck 
2323c2c66affSColin Finck     default:
2324c2c66affSColin Finck       /* From docs: If PlayEnhMetaFileRecord doesn't recognize a
2325c2c66affSColin Finck                     record then ignore and return TRUE. */
2326c2c66affSColin Finck       FIXME("type %d is unimplemented\n", type);
2327c2c66affSColin Finck       break;
2328c2c66affSColin Finck     }
2329c2c66affSColin Finck   tmprc.left = tmprc.top = 0;
2330c2c66affSColin Finck   tmprc.right = tmprc.bottom = 1000;
2331c2c66affSColin Finck   LPtoDP(hdc, (POINT*)&tmprc, 2);
2332c2c66affSColin Finck   TRACE("L:0,0 - 1000,1000 -> D:%s\n", wine_dbgstr_rect(&tmprc));
2333c2c66affSColin Finck 
2334c2c66affSColin Finck   return TRUE;
2335c2c66affSColin Finck }
2336c2c66affSColin Finck 
2337c2c66affSColin Finck /*****************************************************************************
2338c2c66affSColin Finck  *
2339c2c66affSColin Finck  *        EnumEnhMetaFile  (GDI32.@)
2340c2c66affSColin Finck  *
2341c2c66affSColin Finck  *  Walk an enhanced metafile, calling a user-specified function _EnhMetaFunc_
2342c2c66affSColin Finck  *  for each
2343c2c66affSColin Finck  *  record. Returns when either every record has been used or
2344c2c66affSColin Finck  *  when _EnhMetaFunc_ returns FALSE.
2345c2c66affSColin Finck  *
2346c2c66affSColin Finck  *
2347c2c66affSColin Finck  * RETURNS
2348c2c66affSColin Finck  *  TRUE if every record is used, FALSE if any invocation of _EnhMetaFunc_
2349c2c66affSColin Finck  *  returns FALSE.
2350c2c66affSColin Finck  *
2351c2c66affSColin Finck  * BUGS
2352c2c66affSColin Finck  *   Ignores rect.
2353c2c66affSColin Finck  *
2354c2c66affSColin Finck  * NOTES
2355c2c66affSColin Finck  *   This function behaves differently in Win9x and WinNT.
2356c2c66affSColin Finck  *
2357c2c66affSColin Finck  *   In WinNT, the DC's world transform is updated as the EMF changes
2358c2c66affSColin Finck  *    the Window/Viewport Extent and Origin or its world transform.
2359c2c66affSColin Finck  *    The actual Window/Viewport Extent and Origin are left untouched.
2360c2c66affSColin Finck  *
2361c2c66affSColin Finck  *   In Win9x, the DC is left untouched, and PlayEnhMetaFileRecord
2362c2c66affSColin Finck  *    updates the scaling itself but only just before a record that
2363c2c66affSColin Finck  *    writes anything to the DC.
2364c2c66affSColin Finck  *
2365c2c66affSColin Finck  *   I'm not sure where the data (enum_emh_data) is stored in either
2366c2c66affSColin Finck  *    version. For this implementation, it is stored before the handle
2367c2c66affSColin Finck  *    table, but it could be stored in the DC, in the EMF handle or in
2368c2c66affSColin Finck  *    TLS.
2369c2c66affSColin Finck  *             MJM  5 Oct 2002
2370c2c66affSColin Finck  */
EnumEnhMetaFile(HDC hdc,HENHMETAFILE hmf,ENHMFENUMPROC callback,LPVOID data,const RECT * lpRect)2371c2c66affSColin Finck BOOL WINAPI EnumEnhMetaFile(
2372c2c66affSColin Finck      HDC hdc,                /* [in] device context to pass to _EnhMetaFunc_ */
2373c2c66affSColin Finck      HENHMETAFILE hmf,       /* [in] EMF to walk */
2374c2c66affSColin Finck      ENHMFENUMPROC callback, /* [in] callback function */
2375c2c66affSColin Finck      LPVOID data,            /* [in] optional data for callback function */
2376c2c66affSColin Finck      const RECT *lpRect      /* [in] bounding rectangle for rendered metafile */
2377c2c66affSColin Finck     )
2378c2c66affSColin Finck {
2379c2c66affSColin Finck     BOOL ret;
2380c2c66affSColin Finck     ENHMETAHEADER *emh;
2381c2c66affSColin Finck     ENHMETARECORD *emr;
2382c2c66affSColin Finck     DWORD offset;
2383c2c66affSColin Finck     UINT i;
2384c2c66affSColin Finck     HANDLETABLE *ht;
2385c2c66affSColin Finck     INT savedMode = 0;
2386c2c66affSColin Finck     XFORM savedXform;
2387c2c66affSColin Finck     HPEN hPen = NULL;
2388c2c66affSColin Finck     HBRUSH hBrush = NULL;
2389c2c66affSColin Finck     HFONT hFont = NULL;
2390c2c66affSColin Finck     HRGN hRgn = NULL;
2391c2c66affSColin Finck     enum_emh_data *info;
2392c2c66affSColin Finck     SIZE vp_size, win_size;
2393c2c66affSColin Finck     POINT vp_org, win_org;
2394c2c66affSColin Finck     INT mapMode = MM_TEXT, old_align = 0, old_rop2 = 0, old_arcdir = 0, old_polyfill = 0, old_stretchblt = 0;
2395c2c66affSColin Finck     COLORREF old_text_color = 0, old_bk_color = 0;
2396c2c66affSColin Finck 
2397c2c66affSColin Finck     if(!lpRect && hdc)
2398c2c66affSColin Finck     {
2399c2c66affSColin Finck 	SetLastError(ERROR_INVALID_PARAMETER);
2400c2c66affSColin Finck 	return FALSE;
2401c2c66affSColin Finck     }
2402c2c66affSColin Finck 
2403c2c66affSColin Finck     emh = EMF_GetEnhMetaHeader(hmf);
2404c2c66affSColin Finck     if(!emh) {
2405c2c66affSColin Finck         SetLastError(ERROR_INVALID_HANDLE);
2406c2c66affSColin Finck         return FALSE;
2407c2c66affSColin Finck     }
2408c2c66affSColin Finck 
2409c2c66affSColin Finck     info = HeapAlloc( GetProcessHeap(), 0,
2410c2c66affSColin Finck 		    sizeof (enum_emh_data) + sizeof(HANDLETABLE) * emh->nHandles );
2411c2c66affSColin Finck     if(!info)
2412c2c66affSColin Finck     {
2413c2c66affSColin Finck 	SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2414c2c66affSColin Finck 	return FALSE;
2415c2c66affSColin Finck     }
2416c2c66affSColin Finck     info->state.mode = MM_TEXT;
2417c2c66affSColin Finck     info->state.wndOrgX = 0;
2418c2c66affSColin Finck     info->state.wndOrgY = 0;
2419c2c66affSColin Finck     info->state.wndExtX = 1;
2420c2c66affSColin Finck     info->state.wndExtY = 1;
2421c2c66affSColin Finck     info->state.vportOrgX = 0;
2422c2c66affSColin Finck     info->state.vportOrgY = 0;
2423c2c66affSColin Finck     info->state.vportExtX = 1;
2424c2c66affSColin Finck     info->state.vportExtY = 1;
2425c2c66affSColin Finck     info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
2426c2c66affSColin Finck     info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
2427c2c66affSColin Finck     info->state.world_transform.eDx  = info->state.world_transform.eDy =  0;
2428c2c66affSColin Finck 
2429c2c66affSColin Finck     info->state.next = NULL;
2430c2c66affSColin Finck     info->save_level = 0;
2431c2c66affSColin Finck     info->saved_state = NULL;
2432*fc16259fSJames Tabor     info->init_transform = info->state.world_transform;
2433c2c66affSColin Finck 
2434c2c66affSColin Finck     ht = (HANDLETABLE*) &info[1];
2435c2c66affSColin Finck     ht->objectHandle[0] = hmf;
2436c2c66affSColin Finck     for(i = 1; i < emh->nHandles; i++)
2437c2c66affSColin Finck         ht->objectHandle[i] = NULL;
2438c2c66affSColin Finck 
2439*fc16259fSJames Tabor     if(hdc && !is_meta_dc( hdc ))
2440c2c66affSColin Finck     {
2441c2c66affSColin Finck 	savedMode = SetGraphicsMode(hdc, GM_ADVANCED);
2442c2c66affSColin Finck 	GetWorldTransform(hdc, &savedXform);
2443c2c66affSColin Finck         GetViewportExtEx(hdc, &vp_size);
2444c2c66affSColin Finck         GetWindowExtEx(hdc, &win_size);
2445c2c66affSColin Finck         GetViewportOrgEx(hdc, &vp_org);
2446c2c66affSColin Finck         GetWindowOrgEx(hdc, &win_org);
2447c2c66affSColin Finck         mapMode = GetMapMode(hdc);
2448c2c66affSColin Finck 
2449c2c66affSColin Finck 	/* save DC */
2450c2c66affSColin Finck 	hPen = GetCurrentObject(hdc, OBJ_PEN);
2451c2c66affSColin Finck 	hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
2452c2c66affSColin Finck 	hFont = GetCurrentObject(hdc, OBJ_FONT);
2453c2c66affSColin Finck 
2454c2c66affSColin Finck         hRgn = CreateRectRgn(0, 0, 0, 0);
2455c2c66affSColin Finck         if (!GetClipRgn(hdc, hRgn))
2456c2c66affSColin Finck         {
2457c2c66affSColin Finck             DeleteObject(hRgn);
2458c2c66affSColin Finck             hRgn = 0;
2459c2c66affSColin Finck         }
2460c2c66affSColin Finck 
2461c2c66affSColin Finck         old_text_color = SetTextColor(hdc, RGB(0,0,0));
2462c2c66affSColin Finck         old_bk_color = SetBkColor(hdc, RGB(0xff, 0xff, 0xff));
2463c2c66affSColin Finck         old_align = SetTextAlign(hdc, 0);
2464c2c66affSColin Finck         old_rop2 = SetROP2(hdc, R2_COPYPEN);
2465c2c66affSColin Finck         old_arcdir = SetArcDirection(hdc, AD_COUNTERCLOCKWISE);
2466c2c66affSColin Finck         old_polyfill = SetPolyFillMode(hdc, ALTERNATE);
2467c2c66affSColin Finck         old_stretchblt = SetStretchBltMode(hdc, BLACKONWHITE);
2468c2c66affSColin Finck 
2469*fc16259fSJames Tabor         if (!IS_WIN9X() )
2470c2c66affSColin Finck         {
2471c2c66affSColin Finck             /* WinNT combines the vp/win ext/org info into a transform */
2472c2c66affSColin Finck             double xscale, yscale;
2473c2c66affSColin Finck             xscale = (double)vp_size.cx / (double)win_size.cx;
2474c2c66affSColin Finck             yscale = (double)vp_size.cy / (double)win_size.cy;
2475c2c66affSColin Finck             info->init_transform.eM11 = xscale;
2476c2c66affSColin Finck             info->init_transform.eM12 = 0.0;
2477c2c66affSColin Finck             info->init_transform.eM21 = 0.0;
2478c2c66affSColin Finck             info->init_transform.eM22 = yscale;
2479c2c66affSColin Finck             info->init_transform.eDx  = (double)vp_org.x - xscale * (double)win_org.x;
2480c2c66affSColin Finck             info->init_transform.eDy  = (double)vp_org.y - yscale * (double)win_org.y;
2481c2c66affSColin Finck 
2482c2c66affSColin Finck             CombineTransform(&info->init_transform, &savedXform, &info->init_transform);
2483c2c66affSColin Finck         }
2484c2c66affSColin Finck 
2485c2c66affSColin Finck         if ( lpRect && WIDTH(emh->rclFrame) && HEIGHT(emh->rclFrame) )
2486c2c66affSColin Finck         {
2487c2c66affSColin Finck             double xSrcPixSize, ySrcPixSize, xscale, yscale;
2488c2c66affSColin Finck             XFORM xform;
2489c2c66affSColin Finck 
2490c2c66affSColin Finck             TRACE("rect: %s. rclFrame: (%d,%d)-(%d,%d)\n", wine_dbgstr_rect(lpRect),
2491c2c66affSColin Finck                emh->rclFrame.left, emh->rclFrame.top, emh->rclFrame.right,
2492c2c66affSColin Finck                emh->rclFrame.bottom);
2493c2c66affSColin Finck 
2494c2c66affSColin Finck             xSrcPixSize = (double) emh->szlMillimeters.cx / emh->szlDevice.cx;
2495c2c66affSColin Finck             ySrcPixSize = (double) emh->szlMillimeters.cy / emh->szlDevice.cy;
2496c2c66affSColin Finck             xscale = (double) WIDTH(*lpRect) * 100.0 /
2497c2c66affSColin Finck                      WIDTH(emh->rclFrame) * xSrcPixSize;
2498c2c66affSColin Finck             yscale = (double) HEIGHT(*lpRect) * 100.0 /
2499c2c66affSColin Finck                      HEIGHT(emh->rclFrame) * ySrcPixSize;
2500c2c66affSColin Finck             TRACE("xscale = %f, yscale = %f\n", xscale, yscale);
2501c2c66affSColin Finck 
2502c2c66affSColin Finck             xform.eM11 = xscale;
2503c2c66affSColin Finck             xform.eM12 = 0;
2504c2c66affSColin Finck             xform.eM21 = 0;
2505c2c66affSColin Finck             xform.eM22 = yscale;
2506c2c66affSColin Finck             xform.eDx = (double) lpRect->left - (double) WIDTH(*lpRect) / WIDTH(emh->rclFrame) * emh->rclFrame.left;
2507c2c66affSColin Finck             xform.eDy = (double) lpRect->top - (double) HEIGHT(*lpRect) / HEIGHT(emh->rclFrame) * emh->rclFrame.top;
2508c2c66affSColin Finck 
2509c2c66affSColin Finck             CombineTransform(&info->init_transform, &xform, &info->init_transform);
2510c2c66affSColin Finck         }
2511c2c66affSColin Finck 
2512c2c66affSColin Finck         /* WinNT resets the current vp/win org/ext */
2513c2c66affSColin Finck         if ( !IS_WIN9X() )
2514c2c66affSColin Finck         {
2515c2c66affSColin Finck             SetMapMode(hdc, MM_TEXT);
2516c2c66affSColin Finck             SetWindowOrgEx(hdc, 0, 0, NULL);
2517c2c66affSColin Finck             SetViewportOrgEx(hdc, 0, 0, NULL);
2518c2c66affSColin Finck             EMF_Update_MF_Xform(hdc, info);
2519c2c66affSColin Finck         }
2520c2c66affSColin Finck     }
2521c2c66affSColin Finck 
2522c2c66affSColin Finck     ret = TRUE;
2523c2c66affSColin Finck     offset = 0;
2524c2c66affSColin Finck     while(ret && offset < emh->nBytes)
2525c2c66affSColin Finck     {
2526c2c66affSColin Finck 	emr = (ENHMETARECORD *)((char *)emh + offset);
2527c2c66affSColin Finck 
2528*fc16259fSJames Tabor         if (offset + 8 > emh->nBytes ||
2529*fc16259fSJames Tabor             offset > offset + emr->nSize ||
2530*fc16259fSJames Tabor             offset + emr->nSize > emh->nBytes)
2531*fc16259fSJames Tabor         {
2532*fc16259fSJames Tabor             WARN("record truncated\n");
2533*fc16259fSJames Tabor             break;
2534*fc16259fSJames Tabor         }
2535*fc16259fSJames Tabor 
2536c2c66affSColin Finck         /* In Win9x mode we update the xform if the record will produce output */
2537c2c66affSColin Finck         if (hdc && IS_WIN9X() && emr_produces_output(emr->iType))
2538c2c66affSColin Finck             EMF_Update_MF_Xform(hdc, info);
2539c2c66affSColin Finck 
2540c2c66affSColin Finck 	TRACE("Calling EnumFunc with record %s, size %d\n", get_emr_name(emr->iType), emr->nSize);
2541c2c66affSColin Finck 	ret = (*callback)(hdc, ht, emr, emh->nHandles, (LPARAM)data);
2542c2c66affSColin Finck 	offset += emr->nSize;
2543c2c66affSColin Finck     }
2544c2c66affSColin Finck 
2545*fc16259fSJames Tabor     if (hdc && !is_meta_dc( hdc ))
2546c2c66affSColin Finck     {
2547c2c66affSColin Finck         SetStretchBltMode(hdc, old_stretchblt);
2548c2c66affSColin Finck         SetPolyFillMode(hdc, old_polyfill);
2549c2c66affSColin Finck         SetArcDirection(hdc, old_arcdir);
2550c2c66affSColin Finck         SetROP2(hdc, old_rop2);
2551c2c66affSColin Finck         SetTextAlign(hdc, old_align);
2552c2c66affSColin Finck         SetBkColor(hdc, old_bk_color);
2553c2c66affSColin Finck         SetTextColor(hdc, old_text_color);
2554c2c66affSColin Finck 
2555c2c66affSColin Finck 	/* restore DC */
2556c2c66affSColin Finck 	SelectObject(hdc, hBrush);
2557c2c66affSColin Finck 	SelectObject(hdc, hPen);
2558c2c66affSColin Finck 	SelectObject(hdc, hFont);
2559c2c66affSColin Finck         ExtSelectClipRgn(hdc, hRgn, RGN_COPY);
2560c2c66affSColin Finck         DeleteObject(hRgn);
2561c2c66affSColin Finck 
2562c2c66affSColin Finck 	SetWorldTransform(hdc, &savedXform);
2563c2c66affSColin Finck 	if (savedMode)
2564c2c66affSColin Finck 	    SetGraphicsMode(hdc, savedMode);
2565c2c66affSColin Finck         SetMapMode(hdc, mapMode);
2566c2c66affSColin Finck         SetWindowOrgEx(hdc, win_org.x, win_org.y, NULL);
2567c2c66affSColin Finck         SetWindowExtEx(hdc, win_size.cx, win_size.cy, NULL);
2568c2c66affSColin Finck         SetViewportOrgEx(hdc, vp_org.x, vp_org.y, NULL);
2569c2c66affSColin Finck         SetViewportExtEx(hdc, vp_size.cx, vp_size.cy, NULL);
2570c2c66affSColin Finck     }
2571c2c66affSColin Finck 
2572c2c66affSColin Finck     for(i = 1; i < emh->nHandles; i++) /* Don't delete element 0 (hmf) */
2573c2c66affSColin Finck         if( (ht->objectHandle)[i] )
2574c2c66affSColin Finck 	    DeleteObject( (ht->objectHandle)[i] );
2575c2c66affSColin Finck 
2576c2c66affSColin Finck     while (info->saved_state)
2577c2c66affSColin Finck     {
2578c2c66affSColin Finck         EMF_dc_state *state = info->saved_state;
2579c2c66affSColin Finck         info->saved_state = info->saved_state->next;
2580c2c66affSColin Finck         HeapFree( GetProcessHeap(), 0, state );
2581c2c66affSColin Finck     }
2582c2c66affSColin Finck     HeapFree( GetProcessHeap(), 0, info );
2583c2c66affSColin Finck     return ret;
2584c2c66affSColin Finck }
2585c2c66affSColin Finck 
EMF_PlayEnhMetaFileCallback(HDC hdc,HANDLETABLE * ht,const ENHMETARECORD * emr,INT handles,LPARAM data)2586c2c66affSColin Finck static INT CALLBACK EMF_PlayEnhMetaFileCallback(HDC hdc, HANDLETABLE *ht,
2587c2c66affSColin Finck 						const ENHMETARECORD *emr,
2588c2c66affSColin Finck 						INT handles, LPARAM data)
2589c2c66affSColin Finck {
2590c2c66affSColin Finck     return PlayEnhMetaFileRecord(hdc, ht, emr, handles);
2591c2c66affSColin Finck }
2592c2c66affSColin Finck 
2593c2c66affSColin Finck /**************************************************************************
2594c2c66affSColin Finck  *    PlayEnhMetaFile  (GDI32.@)
2595c2c66affSColin Finck  *
2596c2c66affSColin Finck  *    Renders an enhanced metafile into a specified rectangle *lpRect
2597c2c66affSColin Finck  *    in device context hdc.
2598c2c66affSColin Finck  *
2599c2c66affSColin Finck  * RETURNS
2600c2c66affSColin Finck  *    Success: TRUE
2601c2c66affSColin Finck  *    Failure: FALSE
2602c2c66affSColin Finck  */
PlayEnhMetaFile(HDC hdc,HENHMETAFILE hmf,const RECT * lpRect)2603c2c66affSColin Finck BOOL WINAPI PlayEnhMetaFile(
2604c2c66affSColin Finck        HDC hdc,           /* [in] DC to render into */
2605c2c66affSColin Finck        HENHMETAFILE hmf,  /* [in] metafile to render */
2606c2c66affSColin Finck        const RECT *lpRect /* [in] rectangle to place metafile inside */
2607c2c66affSColin Finck       )
2608c2c66affSColin Finck {
2609*fc16259fSJames Tabor     return EnumEnhMetaFile(hdc, hmf, EMF_PlayEnhMetaFileCallback, NULL, lpRect);
2610c2c66affSColin Finck }
2611c2c66affSColin Finck 
2612c2c66affSColin Finck /*****************************************************************************
2613c2c66affSColin Finck  *  DeleteEnhMetaFile (GDI32.@)
2614c2c66affSColin Finck  *
2615c2c66affSColin Finck  *  Deletes an enhanced metafile and frees the associated storage.
2616c2c66affSColin Finck  */
DeleteEnhMetaFile(HENHMETAFILE hmf)2617c2c66affSColin Finck BOOL WINAPI DeleteEnhMetaFile(HENHMETAFILE hmf)
2618c2c66affSColin Finck {
2619c2c66affSColin Finck     return EMF_Delete_HENHMETAFILE( hmf );
2620c2c66affSColin Finck }
2621c2c66affSColin Finck 
2622c2c66affSColin Finck /*****************************************************************************
2623c2c66affSColin Finck  *  CopyEnhMetaFileA (GDI32.@)
2624c2c66affSColin Finck  *
2625c2c66affSColin Finck  * Duplicate an enhanced metafile.
2626c2c66affSColin Finck  *
2627c2c66affSColin Finck  *
2628c2c66affSColin Finck  */
CopyEnhMetaFileA(HENHMETAFILE hmfSrc,LPCSTR file)2629c2c66affSColin Finck HENHMETAFILE WINAPI CopyEnhMetaFileA(
2630c2c66affSColin Finck     HENHMETAFILE hmfSrc,
2631c2c66affSColin Finck     LPCSTR file)
2632c2c66affSColin Finck {
2633c2c66affSColin Finck     ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2634c2c66affSColin Finck     HENHMETAFILE hmfDst;
2635c2c66affSColin Finck 
2636c2c66affSColin Finck     if(!emrSrc) return FALSE;
2637c2c66affSColin Finck     if (!file) {
2638c2c66affSColin Finck         emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2639c2c66affSColin Finck 	memcpy( emrDst, emrSrc, emrSrc->nBytes );
2640*fc16259fSJames Tabor 	hmfDst = EMF_Create_HENHMETAFILE( emrDst, emrSrc->nBytes, FALSE );
2641c2c66affSColin Finck 	if (!hmfDst)
2642c2c66affSColin Finck 		HeapFree( GetProcessHeap(), 0, emrDst );
2643c2c66affSColin Finck     } else {
2644c2c66affSColin Finck         HANDLE hFile;
2645c2c66affSColin Finck         DWORD w;
2646c2c66affSColin Finck         hFile = CreateFileA( file, GENERIC_WRITE | GENERIC_READ, 0,
2647c2c66affSColin Finck 			     NULL, CREATE_ALWAYS, 0, 0);
2648c2c66affSColin Finck 	WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2649c2c66affSColin Finck 	CloseHandle( hFile );
2650c2c66affSColin Finck 	/* Reopen file for reading only, so that apps can share
2651c2c66affSColin Finck 	   read access to the file while hmf is still valid */
2652c2c66affSColin Finck         hFile = CreateFileA( file, GENERIC_READ, FILE_SHARE_READ,
2653c2c66affSColin Finck 			     NULL, OPEN_EXISTING, 0, 0);
2654c2c66affSColin Finck 	if(hFile == INVALID_HANDLE_VALUE) {
2655c2c66affSColin Finck 	    ERR("Can't reopen emf for reading\n");
2656c2c66affSColin Finck 	    return 0;
2657c2c66affSColin Finck 	}
2658c2c66affSColin Finck 	hmfDst = EMF_GetEnhMetaFile( hFile );
2659c2c66affSColin Finck         CloseHandle( hFile );
2660c2c66affSColin Finck     }
2661c2c66affSColin Finck     return hmfDst;
2662c2c66affSColin Finck }
2663c2c66affSColin Finck 
2664c2c66affSColin Finck /*****************************************************************************
2665c2c66affSColin Finck  *  CopyEnhMetaFileW (GDI32.@)
2666c2c66affSColin Finck  *
2667c2c66affSColin Finck  * See CopyEnhMetaFileA.
2668c2c66affSColin Finck  *
2669c2c66affSColin Finck  *
2670c2c66affSColin Finck  */
CopyEnhMetaFileW(HENHMETAFILE hmfSrc,LPCWSTR file)2671c2c66affSColin Finck HENHMETAFILE WINAPI CopyEnhMetaFileW(
2672c2c66affSColin Finck     HENHMETAFILE hmfSrc,
2673c2c66affSColin Finck     LPCWSTR file)
2674c2c66affSColin Finck {
2675c2c66affSColin Finck     ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2676c2c66affSColin Finck     HENHMETAFILE hmfDst;
2677c2c66affSColin Finck 
2678c2c66affSColin Finck     if(!emrSrc) return FALSE;
2679c2c66affSColin Finck     if (!file) {
2680c2c66affSColin Finck         emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2681c2c66affSColin Finck 	memcpy( emrDst, emrSrc, emrSrc->nBytes );
2682*fc16259fSJames Tabor 	hmfDst = EMF_Create_HENHMETAFILE( emrDst, emrSrc->nBytes, FALSE );
2683c2c66affSColin Finck 	if (!hmfDst)
2684c2c66affSColin Finck 		HeapFree( GetProcessHeap(), 0, emrDst );
2685c2c66affSColin Finck     } else {
2686c2c66affSColin Finck         HANDLE hFile;
2687c2c66affSColin Finck         DWORD w;
2688c2c66affSColin Finck         hFile = CreateFileW( file, GENERIC_WRITE | GENERIC_READ, 0,
2689c2c66affSColin Finck 			     NULL, CREATE_ALWAYS, 0, 0);
2690c2c66affSColin Finck 	WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2691c2c66affSColin Finck 	CloseHandle( hFile );
2692c2c66affSColin Finck 	/* Reopen file for reading only, so that apps can share
2693c2c66affSColin Finck 	   read access to the file while hmf is still valid */
2694c2c66affSColin Finck         hFile = CreateFileW( file, GENERIC_READ, FILE_SHARE_READ,
2695c2c66affSColin Finck 			     NULL, OPEN_EXISTING, 0, 0);
2696c2c66affSColin Finck 	if(hFile == INVALID_HANDLE_VALUE) {
2697c2c66affSColin Finck 	    ERR("Can't reopen emf for reading\n");
2698c2c66affSColin Finck 	    return 0;
2699c2c66affSColin Finck 	}
2700c2c66affSColin Finck 	hmfDst = EMF_GetEnhMetaFile( hFile );
2701c2c66affSColin Finck         CloseHandle( hFile );
2702c2c66affSColin Finck     }
2703c2c66affSColin Finck     return hmfDst;
2704c2c66affSColin Finck }
2705c2c66affSColin Finck 
2706c2c66affSColin Finck 
2707c2c66affSColin Finck /* Struct to be used to be passed in the LPVOID parameter for cbEnhPaletteCopy */
2708c2c66affSColin Finck typedef struct tagEMF_PaletteCopy
2709c2c66affSColin Finck {
2710c2c66affSColin Finck    UINT cEntries;
2711c2c66affSColin Finck    LPPALETTEENTRY lpPe;
2712c2c66affSColin Finck } EMF_PaletteCopy;
2713c2c66affSColin Finck 
2714c2c66affSColin Finck /***************************************************************
2715c2c66affSColin Finck  * Find the EMR_EOF record and then use it to find the
2716c2c66affSColin Finck  * palette entries for this enhanced metafile.
2717c2c66affSColin Finck  * The lpData is actually a pointer to an EMF_PaletteCopy struct
2718c2c66affSColin Finck  * which contains the max number of elements to copy and where
2719c2c66affSColin Finck  * to copy them to.
2720c2c66affSColin Finck  *
2721c2c66affSColin Finck  * NOTE: To be used by GetEnhMetaFilePaletteEntries only!
2722c2c66affSColin Finck  */
cbEnhPaletteCopy(HDC a,HANDLETABLE * b,const ENHMETARECORD * lpEMR,INT c,LPARAM lpData)2723c2c66affSColin Finck static INT CALLBACK cbEnhPaletteCopy( HDC a,
2724c2c66affSColin Finck                                HANDLETABLE *b,
2725c2c66affSColin Finck                                const ENHMETARECORD *lpEMR,
2726c2c66affSColin Finck                                INT c,
2727c2c66affSColin Finck                                LPARAM lpData )
2728c2c66affSColin Finck {
2729c2c66affSColin Finck 
2730c2c66affSColin Finck   if ( lpEMR->iType == EMR_EOF )
2731c2c66affSColin Finck   {
2732c2c66affSColin Finck     const EMREOF *lpEof = (const EMREOF *)lpEMR;
2733c2c66affSColin Finck     EMF_PaletteCopy* info = (EMF_PaletteCopy*)lpData;
2734c2c66affSColin Finck     DWORD dwNumPalToCopy = min( lpEof->nPalEntries, info->cEntries );
2735c2c66affSColin Finck 
2736c2c66affSColin Finck     TRACE( "copying 0x%08x palettes\n", dwNumPalToCopy );
2737c2c66affSColin Finck 
2738c2c66affSColin Finck     memcpy( info->lpPe, (LPCSTR)lpEof + lpEof->offPalEntries,
2739c2c66affSColin Finck             sizeof( *(info->lpPe) ) * dwNumPalToCopy );
2740c2c66affSColin Finck 
2741c2c66affSColin Finck     /* Update the passed data as a return code */
2742c2c66affSColin Finck     info->lpPe     = NULL; /* Palettes were copied! */
2743c2c66affSColin Finck     info->cEntries = dwNumPalToCopy;
2744c2c66affSColin Finck 
2745c2c66affSColin Finck     return FALSE; /* That's all we need */
2746c2c66affSColin Finck   }
2747c2c66affSColin Finck 
2748c2c66affSColin Finck   return TRUE;
2749c2c66affSColin Finck }
2750c2c66affSColin Finck 
2751c2c66affSColin Finck /*****************************************************************************
2752c2c66affSColin Finck  *  GetEnhMetaFilePaletteEntries (GDI32.@)
2753c2c66affSColin Finck  *
2754c2c66affSColin Finck  *  Copy the palette and report size
2755c2c66affSColin Finck  *
2756c2c66affSColin Finck  *  BUGS: Error codes (SetLastError) are not set on failures
2757c2c66affSColin Finck  */
GetEnhMetaFilePaletteEntries(HENHMETAFILE hEmf,UINT cEntries,LPPALETTEENTRY lpPe)2758c2c66affSColin Finck UINT WINAPI GetEnhMetaFilePaletteEntries( HENHMETAFILE hEmf,
2759c2c66affSColin Finck 					  UINT cEntries,
2760c2c66affSColin Finck 					  LPPALETTEENTRY lpPe )
2761c2c66affSColin Finck {
2762c2c66affSColin Finck   ENHMETAHEADER* enhHeader = EMF_GetEnhMetaHeader( hEmf );
2763c2c66affSColin Finck   EMF_PaletteCopy infoForCallBack;
2764c2c66affSColin Finck 
2765c2c66affSColin Finck   TRACE( "(%p,%d,%p)\n", hEmf, cEntries, lpPe );
2766c2c66affSColin Finck 
2767c2c66affSColin Finck   if (!enhHeader) return 0;
2768c2c66affSColin Finck 
2769c2c66affSColin Finck   /* First check if there are any palettes associated with
2770c2c66affSColin Finck      this metafile. */
2771c2c66affSColin Finck   if ( enhHeader->nPalEntries == 0 ) return 0;
2772c2c66affSColin Finck 
2773c2c66affSColin Finck   /* Is the user requesting the number of palettes? */
2774c2c66affSColin Finck   if ( lpPe == NULL ) return enhHeader->nPalEntries;
2775c2c66affSColin Finck 
2776c2c66affSColin Finck   /* Copy cEntries worth of PALETTEENTRY structs into the buffer */
2777c2c66affSColin Finck   infoForCallBack.cEntries = cEntries;
2778c2c66affSColin Finck   infoForCallBack.lpPe     = lpPe;
2779c2c66affSColin Finck 
2780c2c66affSColin Finck   if ( !EnumEnhMetaFile( 0, hEmf, cbEnhPaletteCopy,
2781c2c66affSColin Finck                          &infoForCallBack, 0 ) )
2782c2c66affSColin Finck       return GDI_ERROR;
2783c2c66affSColin Finck 
2784c2c66affSColin Finck   /* Verify that the callback executed correctly */
2785c2c66affSColin Finck   if ( infoForCallBack.lpPe != NULL )
2786c2c66affSColin Finck   {
2787c2c66affSColin Finck      /* Callback proc had error! */
2788c2c66affSColin Finck      ERR( "cbEnhPaletteCopy didn't execute correctly\n" );
2789c2c66affSColin Finck      return GDI_ERROR;
2790c2c66affSColin Finck   }
2791c2c66affSColin Finck 
2792c2c66affSColin Finck   return infoForCallBack.cEntries;
2793c2c66affSColin Finck }
2794c2c66affSColin Finck 
2795c2c66affSColin Finck /******************************************************************
2796c2c66affSColin Finck  *             extract_emf_from_comment
2797c2c66affSColin Finck  *
2798c2c66affSColin Finck  * If the WMF was created by GetWinMetaFileBits, then extract the
2799c2c66affSColin Finck  * original EMF that is stored in MFCOMMENT chunks.
2800c2c66affSColin Finck  */
extract_emf_from_comment(const BYTE * buf,UINT mf_size)2801c2c66affSColin Finck static HENHMETAFILE extract_emf_from_comment( const BYTE *buf, UINT mf_size )
2802c2c66affSColin Finck {
2803c2c66affSColin Finck     METAHEADER *mh = (METAHEADER *)buf;
2804c2c66affSColin Finck     METARECORD *mr;
2805c2c66affSColin Finck     emf_in_wmf_comment *chunk;
2806c2c66affSColin Finck     WORD checksum = 0;
2807c2c66affSColin Finck     DWORD size = 0, remaining, chunks;
2808c2c66affSColin Finck     BYTE *emf_bits = NULL, *ptr;
2809c2c66affSColin Finck     UINT offset;
2810c2c66affSColin Finck     HENHMETAFILE emf = NULL;
2811c2c66affSColin Finck 
2812c2c66affSColin Finck     if (mf_size < sizeof(*mh)) return NULL;
2813c2c66affSColin Finck 
2814c2c66affSColin Finck     for (offset = mh->mtHeaderSize * 2; offset < mf_size; offset += (mr->rdSize * 2))
2815c2c66affSColin Finck     {
2816c2c66affSColin Finck 	mr = (METARECORD *)((char *)mh + offset);
2817c2c66affSColin Finck         chunk = (emf_in_wmf_comment *)(mr->rdParm + 2);
2818c2c66affSColin Finck 
2819c2c66affSColin Finck         if (mr->rdFunction != META_ESCAPE || mr->rdParm[0] != MFCOMMENT) goto done;
2820c2c66affSColin Finck         if (chunk->magic != WMFC_MAGIC) goto done;
2821c2c66affSColin Finck 
2822c2c66affSColin Finck         if (!emf_bits)
2823c2c66affSColin Finck         {
2824c2c66affSColin Finck             size = remaining = chunk->emf_size;
2825c2c66affSColin Finck             chunks = chunk->num_chunks;
2826c2c66affSColin Finck             emf_bits = ptr = HeapAlloc( GetProcessHeap(), 0, size );
2827c2c66affSColin Finck             if (!emf_bits) goto done;
2828c2c66affSColin Finck         }
2829c2c66affSColin Finck         if (chunk->chunk_size > remaining) goto done;
2830c2c66affSColin Finck         remaining -= chunk->chunk_size;
2831c2c66affSColin Finck         if (chunk->remaining_size != remaining) goto done;
2832c2c66affSColin Finck         memcpy( ptr, chunk->emf_data, chunk->chunk_size );
2833c2c66affSColin Finck         ptr += chunk->chunk_size;
2834c2c66affSColin Finck         if (--chunks == 0) break;
2835c2c66affSColin Finck     }
2836c2c66affSColin Finck 
2837c2c66affSColin Finck     for (offset = 0; offset < mf_size / 2; offset++)
2838c2c66affSColin Finck         checksum += *((WORD *)buf + offset);
2839c2c66affSColin Finck     if (checksum) goto done;
2840c2c66affSColin Finck 
2841c2c66affSColin Finck     emf = SetEnhMetaFileBits( size, emf_bits );
2842c2c66affSColin Finck 
2843c2c66affSColin Finck done:
2844c2c66affSColin Finck     HeapFree( GetProcessHeap(), 0, emf_bits );
2845c2c66affSColin Finck     return emf;
2846c2c66affSColin Finck }
2847c2c66affSColin Finck 
2848c2c66affSColin Finck typedef struct wmf_in_emf_comment
2849c2c66affSColin Finck {
2850c2c66affSColin Finck     DWORD ident;
2851c2c66affSColin Finck     DWORD iComment;
2852c2c66affSColin Finck     DWORD nVersion;
2853c2c66affSColin Finck     DWORD nChecksum;
2854c2c66affSColin Finck     DWORD fFlags;
2855c2c66affSColin Finck     DWORD cbWinMetaFile;
2856c2c66affSColin Finck } wmf_in_emf_comment;
2857c2c66affSColin Finck 
2858c2c66affSColin Finck /******************************************************************
2859c2c66affSColin Finck  *         SetWinMetaFileBits   (GDI32.@)
2860c2c66affSColin Finck  *
2861c2c66affSColin Finck  *         Translate from old style to new style.
2862c2c66affSColin Finck  *
2863c2c66affSColin Finck  */
SetWinMetaFileBits(UINT cbBuffer,const BYTE * lpbBuffer,HDC hdcRef,const METAFILEPICT * lpmfp)2864c2c66affSColin Finck HENHMETAFILE WINAPI SetWinMetaFileBits(UINT cbBuffer, const BYTE *lpbBuffer, HDC hdcRef,
2865c2c66affSColin Finck                                        const METAFILEPICT *lpmfp)
2866c2c66affSColin Finck {
2867c2c66affSColin Finck     static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
2868c2c66affSColin Finck     HMETAFILE hmf = NULL;
2869c2c66affSColin Finck     HENHMETAFILE ret = NULL;
2870c2c66affSColin Finck     HDC hdc = NULL, hdcdisp = NULL;
2871c2c66affSColin Finck     RECT rc, *prcFrame = NULL;
2872c2c66affSColin Finck     LONG mm, xExt, yExt;
2873c2c66affSColin Finck     INT horzsize, vertsize, horzres, vertres;
2874c2c66affSColin Finck 
2875c2c66affSColin Finck     TRACE("(%d, %p, %p, %p)\n", cbBuffer, lpbBuffer, hdcRef, lpmfp);
2876c2c66affSColin Finck 
2877c2c66affSColin Finck     hmf = SetMetaFileBitsEx(cbBuffer, lpbBuffer);
2878c2c66affSColin Finck     if(!hmf)
2879c2c66affSColin Finck     {
2880c2c66affSColin Finck         WARN("SetMetaFileBitsEx failed\n");
2881c2c66affSColin Finck         return NULL;
2882c2c66affSColin Finck     }
2883c2c66affSColin Finck 
2884c2c66affSColin Finck     ret = extract_emf_from_comment( lpbBuffer, cbBuffer );
2885c2c66affSColin Finck     if (ret) return ret;
2886c2c66affSColin Finck 
2887c2c66affSColin Finck     if(!hdcRef)
2888c2c66affSColin Finck         hdcRef = hdcdisp = CreateDCW(szDisplayW, NULL, NULL, NULL);
2889c2c66affSColin Finck 
2890c2c66affSColin Finck     if (lpmfp)
2891c2c66affSColin Finck     {
2892c2c66affSColin Finck         TRACE("mm = %d %dx%d\n", lpmfp->mm, lpmfp->xExt, lpmfp->yExt);
2893c2c66affSColin Finck 
2894c2c66affSColin Finck         mm = lpmfp->mm;
2895c2c66affSColin Finck         xExt = lpmfp->xExt;
2896c2c66affSColin Finck         yExt = lpmfp->yExt;
2897c2c66affSColin Finck     }
2898c2c66affSColin Finck     else
2899c2c66affSColin Finck     {
2900c2c66affSColin Finck         TRACE("lpmfp == NULL\n");
2901c2c66affSColin Finck 
2902c2c66affSColin Finck         /* Use the whole device surface */
2903c2c66affSColin Finck         mm = MM_ANISOTROPIC;
2904c2c66affSColin Finck         xExt = 0;
2905c2c66affSColin Finck         yExt = 0;
2906c2c66affSColin Finck     }
2907c2c66affSColin Finck 
2908c2c66affSColin Finck     if (mm == MM_ISOTROPIC || mm == MM_ANISOTROPIC)
2909c2c66affSColin Finck     {
2910c2c66affSColin Finck         if (xExt < 0 || yExt < 0)
2911c2c66affSColin Finck         {
2912c2c66affSColin Finck           /* Use the whole device surface */
2913c2c66affSColin Finck           xExt = 0;
2914c2c66affSColin Finck           yExt = 0;
2915c2c66affSColin Finck         }
2916c2c66affSColin Finck 
2917c2c66affSColin Finck         /* Use the x and y extents as the frame box */
2918c2c66affSColin Finck         if (xExt && yExt)
2919c2c66affSColin Finck         {
2920c2c66affSColin Finck             rc.left = rc.top = 0;
2921c2c66affSColin Finck             rc.right = xExt;
2922c2c66affSColin Finck             rc.bottom = yExt;
2923c2c66affSColin Finck             prcFrame = &rc;
2924c2c66affSColin Finck         }
2925c2c66affSColin Finck     }
2926c2c66affSColin Finck 
2927c2c66affSColin Finck     if(!(hdc = CreateEnhMetaFileW(hdcRef, NULL, prcFrame, NULL)))
2928c2c66affSColin Finck     {
2929c2c66affSColin Finck         ERR("CreateEnhMetaFile failed\n");
2930c2c66affSColin Finck         goto end;
2931c2c66affSColin Finck     }
2932c2c66affSColin Finck 
2933c2c66affSColin Finck     /*
2934c2c66affSColin Finck      * Write the original METAFILE into the enhanced metafile.
2935c2c66affSColin Finck      * It is encapsulated in a GDICOMMENT_WINDOWS_METAFILE record.
2936c2c66affSColin Finck      */
2937c2c66affSColin Finck     if (mm != MM_TEXT)
2938c2c66affSColin Finck     {
2939c2c66affSColin Finck         wmf_in_emf_comment *mfcomment;
2940c2c66affSColin Finck         UINT mfcomment_size;
2941c2c66affSColin Finck 
2942c2c66affSColin Finck         mfcomment_size = sizeof (*mfcomment) + cbBuffer;
2943c2c66affSColin Finck         mfcomment = HeapAlloc(GetProcessHeap(), 0, mfcomment_size);
2944c2c66affSColin Finck         if (mfcomment)
2945c2c66affSColin Finck         {
2946c2c66affSColin Finck             mfcomment->ident = GDICOMMENT_IDENTIFIER;
2947c2c66affSColin Finck             mfcomment->iComment = GDICOMMENT_WINDOWS_METAFILE;
2948c2c66affSColin Finck             mfcomment->nVersion = 0x00000300;
2949c2c66affSColin Finck             mfcomment->nChecksum = 0; /* FIXME */
2950c2c66affSColin Finck             mfcomment->fFlags = 0;
2951c2c66affSColin Finck             mfcomment->cbWinMetaFile = cbBuffer;
2952c2c66affSColin Finck             memcpy(&mfcomment[1], lpbBuffer, cbBuffer);
2953c2c66affSColin Finck             GdiComment(hdc, mfcomment_size, (BYTE*) mfcomment);
2954c2c66affSColin Finck             HeapFree(GetProcessHeap(), 0, mfcomment);
2955c2c66affSColin Finck         }
2956c2c66affSColin Finck         SetMapMode(hdc, mm);
2957c2c66affSColin Finck     }
2958c2c66affSColin Finck 
2959c2c66affSColin Finck 
2960c2c66affSColin Finck     horzsize = GetDeviceCaps(hdcRef, HORZSIZE);
2961c2c66affSColin Finck     vertsize = GetDeviceCaps(hdcRef, VERTSIZE);
2962c2c66affSColin Finck     horzres = GetDeviceCaps(hdcRef, HORZRES);
2963c2c66affSColin Finck     vertres = GetDeviceCaps(hdcRef, VERTRES);
2964c2c66affSColin Finck 
2965c2c66affSColin Finck     if (!xExt || !yExt)
2966c2c66affSColin Finck     {
2967c2c66affSColin Finck         /* Use the whole device surface */
2968c2c66affSColin Finck        xExt = horzres;
2969c2c66affSColin Finck        yExt = vertres;
2970c2c66affSColin Finck     }
2971c2c66affSColin Finck     else
2972c2c66affSColin Finck     {
2973c2c66affSColin Finck         xExt = MulDiv(xExt, horzres, 100 * horzsize);
2974c2c66affSColin Finck         yExt = MulDiv(yExt, vertres, 100 * vertsize);
2975c2c66affSColin Finck     }
2976c2c66affSColin Finck 
2977c2c66affSColin Finck     /* set the initial viewport:window ratio as 1:1 */
2978c2c66affSColin Finck     SetViewportExtEx(hdc, xExt, yExt, NULL);
2979c2c66affSColin Finck     SetWindowExtEx(hdc,   xExt, yExt, NULL);
2980c2c66affSColin Finck 
2981c2c66affSColin Finck     PlayMetaFile(hdc, hmf);
2982c2c66affSColin Finck 
2983c2c66affSColin Finck     ret = CloseEnhMetaFile(hdc);
2984c2c66affSColin Finck end:
2985c2c66affSColin Finck     if (hdcdisp) DeleteDC(hdcdisp);
2986c2c66affSColin Finck     DeleteMetaFile(hmf);
2987c2c66affSColin Finck     return ret;
2988c2c66affSColin Finck }
2989