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