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