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